]>
Commit | Line | Data |
---|---|---|
08e8f724 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
bac2e715 | 3 | // $Id: cmndline.cc,v 1.15 2003/02/10 01:40:58 doogie Exp $ |
08e8f724 AL |
4 | /* ###################################################################### |
5 | ||
6 | Command Line Class - Sophisticated command line parser | |
7 | ||
7da2b375 AL |
8 | This source is placed in the Public Domain, do with it what you will |
9 | It was originally written by Jason Gunthorpe <jgg@debian.org>. | |
10 | ||
08e8f724 AL |
11 | ##################################################################### */ |
12 | /*}}}*/ | |
13 | // Include files /*{{{*/ | |
ea542140 DK |
14 | #include<config.h> |
15 | ||
472ff00e | 16 | #include <apt-pkg/configuration.h> |
08e8f724 AL |
17 | #include <apt-pkg/cmndline.h> |
18 | #include <apt-pkg/error.h> | |
cdcc6d34 | 19 | #include <apt-pkg/strutl.h> |
b2e465d6 | 20 | |
ea542140 | 21 | #include <apti18n.h> |
08e8f724 | 22 | /*}}}*/ |
584e4558 | 23 | using namespace std; |
08e8f724 AL |
24 | |
25 | // CommandLine::CommandLine - Constructor /*{{{*/ | |
26 | // --------------------------------------------------------------------- | |
27 | /* */ | |
28 | CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList), | |
29 | Conf(Conf), FileList(0) | |
30 | { | |
e1b74f61 AL |
31 | } |
32 | /*}}}*/ | |
33 | // CommandLine::~CommandLine - Destructor /*{{{*/ | |
34 | // --------------------------------------------------------------------- | |
35 | /* */ | |
36 | CommandLine::~CommandLine() | |
37 | { | |
38 | delete [] FileList; | |
08e8f724 AL |
39 | } |
40 | /*}}}*/ | |
41 | // CommandLine::Parse - Main action member /*{{{*/ | |
42 | // --------------------------------------------------------------------- | |
43 | /* */ | |
44 | bool CommandLine::Parse(int argc,const char **argv) | |
45 | { | |
e1b74f61 | 46 | delete [] FileList; |
08e8f724 AL |
47 | FileList = new const char *[argc]; |
48 | const char **Files = FileList; | |
49 | int I; | |
50 | for (I = 1; I != argc; I++) | |
51 | { | |
52 | const char *Opt = argv[I]; | |
53 | ||
54 | // It is not an option | |
55 | if (*Opt != '-') | |
56 | { | |
57 | *Files++ = Opt; | |
58 | continue; | |
59 | } | |
60 | ||
61 | Opt++; | |
62 | ||
63 | // Double dash signifies the end of option processing | |
64 | if (*Opt == '-' && Opt[1] == 0) | |
343bd48e AL |
65 | { |
66 | I++; | |
08e8f724 | 67 | break; |
343bd48e | 68 | } |
08e8f724 AL |
69 | |
70 | // Single dash is a short option | |
71 | if (*Opt != '-') | |
72 | { | |
73 | // Iterate over each letter | |
74 | while (*Opt != 0) | |
75 | { | |
76 | // Search for the option | |
77 | Args *A; | |
78 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
79 | if (A->end() == true) | |
b2e465d6 | 80 | return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]); |
08e8f724 AL |
81 | |
82 | if (HandleOpt(I,argc,argv,Opt,A) == false) | |
83 | return false; | |
84 | if (*Opt != 0) | |
85 | Opt++; | |
86 | } | |
87 | continue; | |
88 | } | |
89 | ||
90 | Opt++; | |
91 | ||
92 | // Match up to a = against the list | |
93 | const char *OptEnd = Opt; | |
94 | Args *A; | |
95 | for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++); | |
96 | for (A = ArgList; A->end() == false && | |
97 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
98 | ||
99 | // Failed, look for a word after the first - (no-foo) | |
0d47bd08 | 100 | bool PreceedMatch = false; |
08e8f724 AL |
101 | if (A->end() == true) |
102 | { | |
103 | for (; Opt != OptEnd && *Opt != '-'; Opt++); | |
104 | ||
105 | if (Opt == OptEnd) | |
b2e465d6 | 106 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
08e8f724 AL |
107 | Opt++; |
108 | ||
109 | for (A = ArgList; A->end() == false && | |
110 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
111 | ||
112 | // Failed again.. | |
113 | if (A->end() == true && OptEnd - Opt != 1) | |
b2e465d6 | 114 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
0d47bd08 | 115 | |
08e8f724 | 116 | // The option could be a single letter option prefixed by a no-.. |
08e8f724 | 117 | if (A->end() == true) |
0d47bd08 AL |
118 | { |
119 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
120 | ||
121 | if (A->end() == true) | |
b2e465d6 | 122 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
0d47bd08 | 123 | } |
e1b74f61 AL |
124 | |
125 | // The option is not boolean | |
126 | if (A->IsBoolean() == false) | |
b2e465d6 | 127 | return _error->Error(_("Command line option %s is not boolean"),argv[I]); |
0d47bd08 | 128 | PreceedMatch = true; |
08e8f724 AL |
129 | } |
130 | ||
131 | // Deal with it. | |
132 | OptEnd--; | |
0d47bd08 | 133 | if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false) |
08e8f724 AL |
134 | return false; |
135 | } | |
136 | ||
137 | // Copy any remaining file names over | |
138 | for (; I != argc; I++) | |
139 | *Files++ = argv[I]; | |
140 | *Files = 0; | |
2bb25574 DK |
141 | |
142 | SaveInConfig(argc, argv); | |
143 | ||
08e8f724 AL |
144 | return true; |
145 | } | |
146 | /*}}}*/ | |
147 | // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/ | |
148 | // --------------------------------------------------------------------- | |
149 | /* This is a helper function for parser, it looks at a given argument | |
150 | and looks for specific patterns in the string, it gets tokanized | |
151 | -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */ | |
152 | bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], | |
0d47bd08 | 153 | const char *&Opt,Args *A,bool PreceedMatch) |
08e8f724 AL |
154 | { |
155 | const char *Argument = 0; | |
156 | bool CertainArg = false; | |
157 | int IncI = 0; | |
158 | ||
159 | /* Determine the possible location of an option or 0 if their is | |
160 | no option */ | |
161 | if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0)) | |
162 | { | |
163 | if (I + 1 < argc && argv[I+1][0] != '-') | |
164 | Argument = argv[I+1]; | |
165 | ||
166 | // Equals was specified but we fell off the end! | |
167 | if (Opt[1] == '=' && Argument == 0) | |
b2e465d6 | 168 | return _error->Error(_("Option %s requires an argument."),argv[I]); |
08e8f724 AL |
169 | if (Opt[1] == '=') |
170 | CertainArg = true; | |
171 | ||
172 | IncI = 1; | |
173 | } | |
174 | else | |
175 | { | |
176 | if (Opt[1] == '=') | |
177 | { | |
178 | CertainArg = true; | |
179 | Argument = Opt + 2; | |
180 | } | |
181 | else | |
182 | Argument = Opt + 1; | |
183 | } | |
0d47bd08 | 184 | |
08e8f724 AL |
185 | // Option is an argument set |
186 | if ((A->Flags & HasArg) == HasArg) | |
187 | { | |
188 | if (Argument == 0) | |
b2e465d6 | 189 | return _error->Error(_("Option %s requires an argument."),argv[I]); |
08e8f724 AL |
190 | Opt += strlen(Opt); |
191 | I += IncI; | |
192 | ||
193 | // Parse a configuration file | |
194 | if ((A->Flags & ConfigFile) == ConfigFile) | |
195 | return ReadConfigFile(*Conf,Argument); | |
e1b74f61 | 196 | |
0da8987a | 197 | // Arbitrary item specification |
e1b74f61 AL |
198 | if ((A->Flags & ArbItem) == ArbItem) |
199 | { | |
200 | const char *J; | |
201 | for (J = Argument; *J != 0 && *J != '='; J++); | |
202 | if (*J == 0) | |
bac2e715 | 203 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); |
e1b74f61 | 204 | |
7e798dd7 AL |
205 | // = is trailing |
206 | if (J[1] == 0) | |
207 | { | |
208 | if (I+1 >= argc) | |
bac2e715 | 209 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); |
7e798dd7 AL |
210 | Conf->Set(string(Argument,J-Argument),string(argv[I++ +1])); |
211 | } | |
212 | else | |
213 | Conf->Set(string(Argument,J-Argument),string(J+1)); | |
e1b74f61 AL |
214 | |
215 | return true; | |
216 | } | |
08e8f724 | 217 | |
7f25bdff AL |
218 | const char *I = A->ConfName; |
219 | for (; *I != 0 && *I != ' '; I++); | |
220 | if (*I == ' ') | |
221 | Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument); | |
222 | else | |
223 | Conf->Set(A->ConfName,string(I) + Argument); | |
224 | ||
08e8f724 AL |
225 | return true; |
226 | } | |
227 | ||
228 | // Option is an integer level | |
229 | if ((A->Flags & IntLevel) == IntLevel) | |
230 | { | |
231 | // There might be an argument | |
232 | if (Argument != 0) | |
233 | { | |
234 | char *EndPtr; | |
235 | unsigned long Value = strtol(Argument,&EndPtr,10); | |
236 | ||
237 | // Conversion failed and the argument was specified with an =s | |
238 | if (EndPtr == Argument && CertainArg == true) | |
b2e465d6 | 239 | return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument); |
08e8f724 AL |
240 | |
241 | // Conversion was ok, set the value and return | |
9435cc9b | 242 | if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0) |
08e8f724 AL |
243 | { |
244 | Conf->Set(A->ConfName,Value); | |
245 | Opt += strlen(Opt); | |
246 | I += IncI; | |
247 | return true; | |
248 | } | |
249 | } | |
250 | ||
251 | // Increase the level | |
252 | Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1); | |
253 | return true; | |
254 | } | |
255 | ||
256 | // Option is a boolean | |
257 | int Sense = -1; // -1 is unspecified, 0 is yes 1 is no | |
258 | ||
259 | // Look for an argument. | |
260 | while (1) | |
261 | { | |
262 | // Look at preceeding text | |
263 | char Buffer[300]; | |
264 | if (Argument == 0) | |
265 | { | |
0d47bd08 AL |
266 | if (PreceedMatch == false) |
267 | break; | |
268 | ||
08e8f724 | 269 | if (strlen(argv[I]) >= sizeof(Buffer)) |
b2e465d6 | 270 | return _error->Error(_("Option '%s' is too long"),argv[I]); |
0d47bd08 AL |
271 | |
272 | // Skip the leading dash | |
08e8f724 AL |
273 | const char *J = argv[I]; |
274 | for (; *J != 0 && *J == '-'; J++); | |
0d47bd08 | 275 | |
08e8f724 AL |
276 | const char *JEnd = J; |
277 | for (; *JEnd != 0 && *JEnd != '-'; JEnd++); | |
278 | if (*JEnd != 0) | |
279 | { | |
280 | strncpy(Buffer,J,JEnd - J); | |
281 | Buffer[JEnd - J] = 0; | |
282 | Argument = Buffer; | |
283 | CertainArg = true; | |
284 | } | |
285 | else | |
286 | break; | |
287 | } | |
288 | ||
3b5421b4 AL |
289 | // Check for boolean |
290 | Sense = StringToBool(Argument); | |
291 | if (Sense >= 0) | |
08e8f724 | 292 | { |
08e8f724 AL |
293 | // Eat the argument |
294 | if (Argument != Buffer) | |
295 | { | |
296 | Opt += strlen(Opt); | |
297 | I += IncI; | |
298 | } | |
299 | break; | |
300 | } | |
301 | ||
08e8f724 | 302 | if (CertainArg == true) |
b2e465d6 | 303 | return _error->Error(_("Sense %s is not understood, try true or false."),Argument); |
08e8f724 AL |
304 | |
305 | Argument = 0; | |
306 | } | |
307 | ||
308 | // Indeterminate sense depends on the flag | |
309 | if (Sense == -1) | |
310 | { | |
311 | if ((A->Flags & InvBoolean) == InvBoolean) | |
312 | Sense = 0; | |
313 | else | |
314 | Sense = 1; | |
315 | } | |
316 | ||
317 | Conf->Set(A->ConfName,Sense); | |
318 | return true; | |
319 | } | |
320 | /*}}}*/ | |
bc4af0b9 | 321 | // CommandLine::FileSize - Count the number of filenames /*{{{*/ |
e1b74f61 AL |
322 | // --------------------------------------------------------------------- |
323 | /* */ | |
324 | unsigned int CommandLine::FileSize() const | |
325 | { | |
326 | unsigned int Count = 0; | |
327 | for (const char **I = FileList; I != 0 && *I != 0; I++) | |
328 | Count++; | |
329 | return Count; | |
330 | } | |
331 | /*}}}*/ | |
bc4af0b9 AL |
332 | // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ |
333 | // --------------------------------------------------------------------- | |
334 | /* */ | |
b0b4efb9 | 335 | bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) |
bc4af0b9 AL |
336 | { |
337 | int I; | |
338 | for (I = 0; Map[I].Match != 0; I++) | |
339 | { | |
340 | if (strcmp(FileList[0],Map[I].Match) == 0) | |
341 | { | |
342 | bool Res = Map[I].Handler(*this); | |
343 | if (Res == false && _error->PendingError() == false) | |
344 | _error->Error("Handler silently failed"); | |
345 | return Res; | |
346 | } | |
347 | } | |
348 | ||
349 | // No matching name | |
350 | if (Map[I].Match == 0) | |
b0b4efb9 AL |
351 | { |
352 | if (NoMatch == true) | |
b2e465d6 | 353 | _error->Error(_("Invalid operation %s"),FileList[0]); |
b0b4efb9 AL |
354 | } |
355 | ||
bc4af0b9 AL |
356 | return false; |
357 | } | |
358 | /*}}}*/ | |
2bb25574 DK |
359 | // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/ |
360 | // --------------------------------------------------------------------- | |
361 | /* We save the commandline here to have it around later for e.g. logging. | |
362 | It feels a bit like a hack here and isn't bulletproof, but it is better | |
363 | than nothing after all. */ | |
364 | void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv) | |
365 | { | |
093e9f5d | 366 | char cmdline[100 + argc * 50]; |
2bb25574 DK |
367 | unsigned int length = 0; |
368 | bool lastWasOption = false; | |
369 | bool closeQuote = false; | |
093e9f5d | 370 | for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length) |
2bb25574 DK |
371 | { |
372 | for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length) | |
373 | { | |
374 | cmdline[length] = argv[i][j]; | |
375 | if (lastWasOption == true && argv[i][j] == '=') | |
376 | { | |
377 | // That is possibly an option: Quote it if it includes spaces, | |
378 | // the benefit is that this will eliminate also most false positives | |
379 | const char* c = &argv[i][j+1]; | |
380 | for (; *c != '\0' && *c != ' '; ++c); | |
381 | if (*c == '\0') continue; | |
382 | cmdline[++length] = '"'; | |
383 | closeQuote = true; | |
384 | } | |
385 | } | |
386 | if (closeQuote == true) | |
387 | cmdline[length++] = '"'; | |
388 | // Problem: detects also --hello | |
389 | if (cmdline[length-1] == 'o') | |
390 | lastWasOption = true; | |
391 | cmdline[length] = ' '; | |
392 | } | |
393 | cmdline[--length] = '\0'; | |
394 | _config->Set("CommandLine::AsString", cmdline); | |
395 | } | |
396 | /*}}}*/ |