]>
Commit | Line | Data |
---|---|---|
08e8f724 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
e1b74f61 | 3 | // $Id: cmndline.cc,v 1.2 1998/09/26 05:34:24 jgg Exp $ |
08e8f724 AL |
4 | /* ###################################################################### |
5 | ||
6 | Command Line Class - Sophisticated command line parser | |
7 | ||
8 | ##################################################################### */ | |
9 | /*}}}*/ | |
10 | // Include files /*{{{*/ | |
11 | #ifdef __GNUG__ | |
12 | #pragma implementation "apt-pkg/cmndline.h" | |
13 | #endif | |
14 | #include <apt-pkg/cmndline.h> | |
15 | #include <apt-pkg/error.h> | |
16 | #include <strutl.h> | |
17 | /*}}}*/ | |
18 | ||
19 | // CommandLine::CommandLine - Constructor /*{{{*/ | |
20 | // --------------------------------------------------------------------- | |
21 | /* */ | |
22 | CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList), | |
23 | Conf(Conf), FileList(0) | |
24 | { | |
e1b74f61 AL |
25 | } |
26 | /*}}}*/ | |
27 | // CommandLine::~CommandLine - Destructor /*{{{*/ | |
28 | // --------------------------------------------------------------------- | |
29 | /* */ | |
30 | CommandLine::~CommandLine() | |
31 | { | |
32 | delete [] FileList; | |
08e8f724 AL |
33 | } |
34 | /*}}}*/ | |
35 | // CommandLine::Parse - Main action member /*{{{*/ | |
36 | // --------------------------------------------------------------------- | |
37 | /* */ | |
38 | bool CommandLine::Parse(int argc,const char **argv) | |
39 | { | |
e1b74f61 | 40 | delete [] FileList; |
08e8f724 AL |
41 | FileList = new const char *[argc]; |
42 | const char **Files = FileList; | |
43 | int I; | |
44 | for (I = 1; I != argc; I++) | |
45 | { | |
46 | const char *Opt = argv[I]; | |
47 | ||
48 | // It is not an option | |
49 | if (*Opt != '-') | |
50 | { | |
51 | *Files++ = Opt; | |
52 | continue; | |
53 | } | |
54 | ||
55 | Opt++; | |
56 | ||
57 | // Double dash signifies the end of option processing | |
58 | if (*Opt == '-' && Opt[1] == 0) | |
59 | break; | |
60 | ||
61 | // Single dash is a short option | |
62 | if (*Opt != '-') | |
63 | { | |
64 | // Iterate over each letter | |
65 | while (*Opt != 0) | |
66 | { | |
67 | // Search for the option | |
68 | Args *A; | |
69 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
70 | if (A->end() == true) | |
71 | return _error->Error("Command line option '%c' [from %s] is not known.",*Opt,argv[I]); | |
72 | ||
73 | if (HandleOpt(I,argc,argv,Opt,A) == false) | |
74 | return false; | |
75 | if (*Opt != 0) | |
76 | Opt++; | |
77 | } | |
78 | continue; | |
79 | } | |
80 | ||
81 | Opt++; | |
82 | ||
83 | // Match up to a = against the list | |
84 | const char *OptEnd = Opt; | |
85 | Args *A; | |
86 | for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++); | |
87 | for (A = ArgList; A->end() == false && | |
88 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
89 | ||
90 | // Failed, look for a word after the first - (no-foo) | |
91 | if (A->end() == true) | |
92 | { | |
93 | for (; Opt != OptEnd && *Opt != '-'; Opt++); | |
94 | ||
95 | if (Opt == OptEnd) | |
96 | return _error->Error("Command line option %s is not understood",argv[I]); | |
97 | Opt++; | |
98 | ||
99 | for (A = ArgList; A->end() == false && | |
100 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
101 | ||
102 | // Failed again.. | |
103 | if (A->end() == true && OptEnd - Opt != 1) | |
104 | return _error->Error("Command line option %s is not understood",argv[I]); | |
105 | ||
106 | // The option could be a single letter option prefixed by a no-.. | |
107 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
108 | ||
109 | if (A->end() == true) | |
110 | return _error->Error("Command line option %s is not understood",argv[I]); | |
e1b74f61 AL |
111 | |
112 | // The option is not boolean | |
113 | if (A->IsBoolean() == false) | |
114 | return _error->Error("Command line option %s is not boolean",argv[I]); | |
08e8f724 AL |
115 | } |
116 | ||
117 | // Deal with it. | |
118 | OptEnd--; | |
119 | if (HandleOpt(I,argc,argv,OptEnd,A) == false) | |
120 | return false; | |
121 | } | |
122 | ||
123 | // Copy any remaining file names over | |
124 | for (; I != argc; I++) | |
125 | *Files++ = argv[I]; | |
126 | *Files = 0; | |
127 | ||
128 | return true; | |
129 | } | |
130 | /*}}}*/ | |
131 | // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/ | |
132 | // --------------------------------------------------------------------- | |
133 | /* This is a helper function for parser, it looks at a given argument | |
134 | and looks for specific patterns in the string, it gets tokanized | |
135 | -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */ | |
136 | bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], | |
137 | const char *&Opt,Args *A) | |
138 | { | |
139 | const char *Argument = 0; | |
140 | bool CertainArg = false; | |
141 | int IncI = 0; | |
142 | ||
143 | /* Determine the possible location of an option or 0 if their is | |
144 | no option */ | |
145 | if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0)) | |
146 | { | |
147 | if (I + 1 < argc && argv[I+1][0] != '-') | |
148 | Argument = argv[I+1]; | |
149 | ||
150 | // Equals was specified but we fell off the end! | |
151 | if (Opt[1] == '=' && Argument == 0) | |
152 | return _error->Error("Option %s requires an argument.",argv[I]); | |
153 | if (Opt[1] == '=') | |
154 | CertainArg = true; | |
155 | ||
156 | IncI = 1; | |
157 | } | |
158 | else | |
159 | { | |
160 | if (Opt[1] == '=') | |
161 | { | |
162 | CertainArg = true; | |
163 | Argument = Opt + 2; | |
164 | } | |
165 | else | |
166 | Argument = Opt + 1; | |
167 | } | |
168 | ||
169 | // Option is an argument set | |
170 | if ((A->Flags & HasArg) == HasArg) | |
171 | { | |
172 | if (Argument == 0) | |
173 | return _error->Error("Option %s requires an argument.",argv[I]); | |
174 | Opt += strlen(Opt); | |
175 | I += IncI; | |
176 | ||
177 | // Parse a configuration file | |
178 | if ((A->Flags & ConfigFile) == ConfigFile) | |
179 | return ReadConfigFile(*Conf,Argument); | |
e1b74f61 AL |
180 | |
181 | // Arbitary item specification | |
182 | if ((A->Flags & ArbItem) == ArbItem) | |
183 | { | |
184 | const char *J; | |
185 | for (J = Argument; *J != 0 && *J != '='; J++); | |
186 | if (*J == 0) | |
187 | return _error->Error("Option %s: Configuration item sepecification must have an =.",argv[I]); | |
188 | ||
189 | Conf->Set(string(Argument,J-Argument),string(J+1)); | |
190 | ||
191 | return true; | |
192 | } | |
08e8f724 AL |
193 | |
194 | Conf->Set(A->ConfName,Argument); | |
195 | return true; | |
196 | } | |
197 | ||
198 | // Option is an integer level | |
199 | if ((A->Flags & IntLevel) == IntLevel) | |
200 | { | |
201 | // There might be an argument | |
202 | if (Argument != 0) | |
203 | { | |
204 | char *EndPtr; | |
205 | unsigned long Value = strtol(Argument,&EndPtr,10); | |
206 | ||
207 | // Conversion failed and the argument was specified with an =s | |
208 | if (EndPtr == Argument && CertainArg == true) | |
209 | return _error->Error("Option %s requires an integer argument, not '%s'",argv[I],Argument); | |
210 | ||
211 | // Conversion was ok, set the value and return | |
212 | if (EndPtr != Argument) | |
213 | { | |
214 | Conf->Set(A->ConfName,Value); | |
215 | Opt += strlen(Opt); | |
216 | I += IncI; | |
217 | return true; | |
218 | } | |
219 | } | |
220 | ||
221 | // Increase the level | |
222 | Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1); | |
223 | return true; | |
224 | } | |
225 | ||
226 | // Option is a boolean | |
227 | int Sense = -1; // -1 is unspecified, 0 is yes 1 is no | |
228 | ||
229 | // Look for an argument. | |
230 | while (1) | |
231 | { | |
232 | // Look at preceeding text | |
233 | char Buffer[300]; | |
234 | if (Argument == 0) | |
235 | { | |
236 | if (strlen(argv[I]) >= sizeof(Buffer)) | |
237 | return _error->Error("Option '%s' is too long",argv[I]); | |
238 | ||
239 | const char *J = argv[I]; | |
240 | for (; *J != 0 && *J == '-'; J++); | |
241 | const char *JEnd = J; | |
242 | for (; *JEnd != 0 && *JEnd != '-'; JEnd++); | |
243 | if (*JEnd != 0) | |
244 | { | |
245 | strncpy(Buffer,J,JEnd - J); | |
246 | Buffer[JEnd - J] = 0; | |
247 | Argument = Buffer; | |
248 | CertainArg = true; | |
249 | } | |
250 | else | |
251 | break; | |
252 | } | |
253 | ||
254 | // Check for positives | |
255 | if (strcasecmp(Argument,"yes") == 0 || | |
256 | strcasecmp(Argument,"true") == 0 || | |
257 | strcasecmp(Argument,"with") == 0 || | |
258 | strcasecmp(Argument,"enable") == 0) | |
259 | { | |
260 | Sense = 1; | |
261 | ||
262 | // Eat the argument | |
263 | if (Argument != Buffer) | |
264 | { | |
265 | Opt += strlen(Opt); | |
266 | I += IncI; | |
267 | } | |
268 | break; | |
269 | } | |
270 | ||
271 | // Check for negatives | |
272 | if (strcasecmp(Argument,"no") == 0 || | |
273 | strcasecmp(Argument,"false") == 0 || | |
274 | strcasecmp(Argument,"without") == 0 || | |
275 | strcasecmp(Argument,"disable") == 0) | |
276 | { | |
277 | Sense = 0; | |
278 | ||
279 | // Eat the argument | |
280 | if (Argument != Buffer) | |
281 | { | |
282 | Opt += strlen(Opt); | |
283 | I += IncI; | |
284 | } | |
285 | break; | |
286 | } | |
287 | ||
288 | if (CertainArg == true) | |
289 | return _error->Error("Sense %s is not understood, try true or false.",Argument); | |
290 | ||
291 | Argument = 0; | |
292 | } | |
293 | ||
294 | // Indeterminate sense depends on the flag | |
295 | if (Sense == -1) | |
296 | { | |
297 | if ((A->Flags & InvBoolean) == InvBoolean) | |
298 | Sense = 0; | |
299 | else | |
300 | Sense = 1; | |
301 | } | |
302 | ||
303 | Conf->Set(A->ConfName,Sense); | |
304 | return true; | |
305 | } | |
306 | /*}}}*/ | |
e1b74f61 AL |
307 | // CommandLine::FileSize - Count the number of filenames /*{{{*/ |
308 | // --------------------------------------------------------------------- | |
309 | /* */ | |
310 | unsigned int CommandLine::FileSize() const | |
311 | { | |
312 | unsigned int Count = 0; | |
313 | for (const char **I = FileList; I != 0 && *I != 0; I++) | |
314 | Count++; | |
315 | return Count; | |
316 | } | |
317 | /*}}}*/ |