]>
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 | |
453b82a3 DK |
21 | #include <stddef.h> |
22 | #include <stdlib.h> | |
23 | #include <string.h> | |
24 | #include <string> | |
25 | ||
ea542140 | 26 | #include <apti18n.h> |
08e8f724 | 27 | /*}}}*/ |
584e4558 | 28 | using namespace std; |
08e8f724 AL |
29 | |
30 | // CommandLine::CommandLine - Constructor /*{{{*/ | |
31 | // --------------------------------------------------------------------- | |
32 | /* */ | |
33 | CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList), | |
34 | Conf(Conf), FileList(0) | |
35 | { | |
ad7e0941 DK |
36 | } |
37 | CommandLine::CommandLine() : ArgList(NULL), Conf(NULL), FileList(0) | |
38 | { | |
e1b74f61 AL |
39 | } |
40 | /*}}}*/ | |
41 | // CommandLine::~CommandLine - Destructor /*{{{*/ | |
42 | // --------------------------------------------------------------------- | |
43 | /* */ | |
44 | CommandLine::~CommandLine() | |
45 | { | |
46 | delete [] FileList; | |
08e8f724 AL |
47 | } |
48 | /*}}}*/ | |
b9179170 MV |
49 | // CommandLine::GetCommand - return the first non-option word /*{{{*/ |
50 | char const * CommandLine::GetCommand(Dispatch const * const Map, | |
51 | unsigned int const argc, char const * const * const argv) | |
52 | { | |
c4b91cbe DK |
53 | // if there is a -- on the line there must be the word we search for either |
54 | // before it (as -- marks the end of the options) or right after it (as we can't | |
55 | // decide if the command is actually an option, given that in theory, you could | |
56 | // have parameters named like commands) | |
b9179170 MV |
57 | for (size_t i = 1; i < argc; ++i) |
58 | { | |
59 | if (strcmp(argv[i], "--") != 0) | |
60 | continue; | |
c4b91cbe DK |
61 | // check if command is before -- |
62 | for (size_t k = 1; k < i; ++k) | |
b9179170 | 63 | for (size_t j = 0; Map[j].Match != NULL; ++j) |
c4b91cbe | 64 | if (strcmp(argv[k], Map[j].Match) == 0) |
b9179170 | 65 | return Map[j].Match; |
c4b91cbe DK |
66 | // see if the next token after -- is the command |
67 | ++i; | |
68 | if (i < argc) | |
b9179170 MV |
69 | for (size_t j = 0; Map[j].Match != NULL; ++j) |
70 | if (strcmp(argv[i], Map[j].Match) == 0) | |
71 | return Map[j].Match; | |
c4b91cbe | 72 | // we found a --, but not a command |
b9179170 MV |
73 | return NULL; |
74 | } | |
75 | // no --, so search for the first word matching a command | |
76 | // FIXME: How like is it that an option parameter will be also a valid Match ? | |
77 | for (size_t i = 1; i < argc; ++i) | |
78 | { | |
79 | if (*(argv[i]) == '-') | |
80 | continue; | |
81 | for (size_t j = 0; Map[j].Match != NULL; ++j) | |
82 | if (strcmp(argv[i], Map[j].Match) == 0) | |
83 | return Map[j].Match; | |
84 | } | |
85 | return NULL; | |
86 | } | |
87 | /*}}}*/ | |
08e8f724 AL |
88 | // CommandLine::Parse - Main action member /*{{{*/ |
89 | // --------------------------------------------------------------------- | |
90 | /* */ | |
91 | bool CommandLine::Parse(int argc,const char **argv) | |
92 | { | |
e1b74f61 | 93 | delete [] FileList; |
08e8f724 AL |
94 | FileList = new const char *[argc]; |
95 | const char **Files = FileList; | |
96 | int I; | |
97 | for (I = 1; I != argc; I++) | |
98 | { | |
99 | const char *Opt = argv[I]; | |
100 | ||
101 | // It is not an option | |
102 | if (*Opt != '-') | |
103 | { | |
104 | *Files++ = Opt; | |
105 | continue; | |
106 | } | |
107 | ||
108 | Opt++; | |
109 | ||
110 | // Double dash signifies the end of option processing | |
111 | if (*Opt == '-' && Opt[1] == 0) | |
343bd48e AL |
112 | { |
113 | I++; | |
08e8f724 | 114 | break; |
343bd48e | 115 | } |
08e8f724 AL |
116 | |
117 | // Single dash is a short option | |
118 | if (*Opt != '-') | |
119 | { | |
120 | // Iterate over each letter | |
121 | while (*Opt != 0) | |
122 | { | |
123 | // Search for the option | |
124 | Args *A; | |
125 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
126 | if (A->end() == true) | |
71510743 | 127 | return _error->Error(_("Command line option '%c' [from %s] is not understood in combination with the other options."),*Opt,argv[I]); |
08e8f724 AL |
128 | |
129 | if (HandleOpt(I,argc,argv,Opt,A) == false) | |
130 | return false; | |
131 | if (*Opt != 0) | |
132 | Opt++; | |
133 | } | |
134 | continue; | |
135 | } | |
136 | ||
137 | Opt++; | |
138 | ||
139 | // Match up to a = against the list | |
08e8f724 | 140 | Args *A; |
404528bd | 141 | const char *OptEnd = strchrnul(Opt, '='); |
ae2be086 DH |
142 | for (A = ArgList; A->end() == false && |
143 | (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); | |
144 | ++A); | |
08e8f724 AL |
145 | |
146 | // Failed, look for a word after the first - (no-foo) | |
0d47bd08 | 147 | bool PreceedMatch = false; |
08e8f724 AL |
148 | if (A->end() == true) |
149 | { | |
404528bd DK |
150 | Opt = (const char*) memchr(Opt, '-', OptEnd - Opt); |
151 | if (Opt == NULL) | |
b7bbde25 | 152 | return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); |
08e8f724 AL |
153 | Opt++; |
154 | ||
155 | for (A = ArgList; A->end() == false && | |
7a6d9076 DK |
156 | (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); |
157 | ++A); | |
08e8f724 AL |
158 | |
159 | // Failed again.. | |
160 | if (A->end() == true && OptEnd - Opt != 1) | |
b7bbde25 | 161 | return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); |
0d47bd08 | 162 | |
08e8f724 | 163 | // The option could be a single letter option prefixed by a no-.. |
08e8f724 | 164 | if (A->end() == true) |
0d47bd08 AL |
165 | { |
166 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
167 | ||
168 | if (A->end() == true) | |
b7bbde25 | 169 | return _error->Error(_("Command line option %s is not understood in combination with the other options"),argv[I]); |
0d47bd08 | 170 | } |
e1b74f61 AL |
171 | |
172 | // The option is not boolean | |
173 | if (A->IsBoolean() == false) | |
b2e465d6 | 174 | return _error->Error(_("Command line option %s is not boolean"),argv[I]); |
0d47bd08 | 175 | PreceedMatch = true; |
08e8f724 AL |
176 | } |
177 | ||
178 | // Deal with it. | |
179 | OptEnd--; | |
0d47bd08 | 180 | if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false) |
08e8f724 AL |
181 | return false; |
182 | } | |
183 | ||
184 | // Copy any remaining file names over | |
185 | for (; I != argc; I++) | |
186 | *Files++ = argv[I]; | |
187 | *Files = 0; | |
2bb25574 DK |
188 | |
189 | SaveInConfig(argc, argv); | |
190 | ||
08e8f724 AL |
191 | return true; |
192 | } | |
193 | /*}}}*/ | |
194 | // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/ | |
195 | // --------------------------------------------------------------------- | |
196 | /* This is a helper function for parser, it looks at a given argument | |
197 | and looks for specific patterns in the string, it gets tokanized | |
198 | -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */ | |
199 | bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], | |
0d47bd08 | 200 | const char *&Opt,Args *A,bool PreceedMatch) |
08e8f724 AL |
201 | { |
202 | const char *Argument = 0; | |
203 | bool CertainArg = false; | |
204 | int IncI = 0; | |
205 | ||
206 | /* Determine the possible location of an option or 0 if their is | |
207 | no option */ | |
bc7a59dd | 208 | if (Opt[1] == 0) |
08e8f724 AL |
209 | { |
210 | if (I + 1 < argc && argv[I+1][0] != '-') | |
211 | Argument = argv[I+1]; | |
bc7a59dd | 212 | |
08e8f724 AL |
213 | IncI = 1; |
214 | } | |
215 | else | |
216 | { | |
217 | if (Opt[1] == '=') | |
218 | { | |
219 | CertainArg = true; | |
220 | Argument = Opt + 2; | |
221 | } | |
222 | else | |
223 | Argument = Opt + 1; | |
224 | } | |
0d47bd08 | 225 | |
08e8f724 AL |
226 | // Option is an argument set |
227 | if ((A->Flags & HasArg) == HasArg) | |
228 | { | |
229 | if (Argument == 0) | |
b2e465d6 | 230 | return _error->Error(_("Option %s requires an argument."),argv[I]); |
08e8f724 AL |
231 | Opt += strlen(Opt); |
232 | I += IncI; | |
233 | ||
234 | // Parse a configuration file | |
235 | if ((A->Flags & ConfigFile) == ConfigFile) | |
236 | return ReadConfigFile(*Conf,Argument); | |
e1b74f61 | 237 | |
0da8987a | 238 | // Arbitrary item specification |
e1b74f61 AL |
239 | if ((A->Flags & ArbItem) == ArbItem) |
240 | { | |
bc7a59dd DK |
241 | const char * const J = strchr(Argument, '='); |
242 | if (J == nullptr) | |
bac2e715 | 243 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); |
e1b74f61 | 244 | |
bc7a59dd | 245 | Conf->Set(string(Argument,J-Argument), J+1); |
e1b74f61 AL |
246 | return true; |
247 | } | |
08e8f724 | 248 | |
404528bd | 249 | const char *I = strchrnul(A->ConfName, ' '); |
7f25bdff AL |
250 | if (*I == ' ') |
251 | Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument); | |
252 | else | |
253 | Conf->Set(A->ConfName,string(I) + Argument); | |
254 | ||
08e8f724 AL |
255 | return true; |
256 | } | |
257 | ||
258 | // Option is an integer level | |
259 | if ((A->Flags & IntLevel) == IntLevel) | |
260 | { | |
261 | // There might be an argument | |
262 | if (Argument != 0) | |
263 | { | |
264 | char *EndPtr; | |
265 | unsigned long Value = strtol(Argument,&EndPtr,10); | |
266 | ||
267 | // Conversion failed and the argument was specified with an =s | |
268 | if (EndPtr == Argument && CertainArg == true) | |
b2e465d6 | 269 | return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument); |
08e8f724 AL |
270 | |
271 | // Conversion was ok, set the value and return | |
9435cc9b | 272 | if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0) |
08e8f724 AL |
273 | { |
274 | Conf->Set(A->ConfName,Value); | |
275 | Opt += strlen(Opt); | |
276 | I += IncI; | |
277 | return true; | |
278 | } | |
279 | } | |
280 | ||
281 | // Increase the level | |
282 | Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1); | |
283 | return true; | |
284 | } | |
285 | ||
286 | // Option is a boolean | |
287 | int Sense = -1; // -1 is unspecified, 0 is yes 1 is no | |
288 | ||
289 | // Look for an argument. | |
290 | while (1) | |
291 | { | |
1e3f4083 | 292 | // Look at preceding text |
08e8f724 AL |
293 | char Buffer[300]; |
294 | if (Argument == 0) | |
295 | { | |
0d47bd08 AL |
296 | if (PreceedMatch == false) |
297 | break; | |
298 | ||
08e8f724 | 299 | if (strlen(argv[I]) >= sizeof(Buffer)) |
b2e465d6 | 300 | return _error->Error(_("Option '%s' is too long"),argv[I]); |
0d47bd08 AL |
301 | |
302 | // Skip the leading dash | |
08e8f724 AL |
303 | const char *J = argv[I]; |
304 | for (; *J != 0 && *J == '-'; J++); | |
404528bd DK |
305 | |
306 | const char *JEnd = strchr(J, '-'); | |
307 | if (JEnd != NULL) | |
08e8f724 AL |
308 | { |
309 | strncpy(Buffer,J,JEnd - J); | |
310 | Buffer[JEnd - J] = 0; | |
311 | Argument = Buffer; | |
312 | CertainArg = true; | |
313 | } | |
314 | else | |
315 | break; | |
316 | } | |
317 | ||
3b5421b4 AL |
318 | // Check for boolean |
319 | Sense = StringToBool(Argument); | |
320 | if (Sense >= 0) | |
08e8f724 | 321 | { |
08e8f724 AL |
322 | // Eat the argument |
323 | if (Argument != Buffer) | |
324 | { | |
325 | Opt += strlen(Opt); | |
326 | I += IncI; | |
327 | } | |
328 | break; | |
329 | } | |
330 | ||
08e8f724 | 331 | if (CertainArg == true) |
b2e465d6 | 332 | return _error->Error(_("Sense %s is not understood, try true or false."),Argument); |
08e8f724 AL |
333 | |
334 | Argument = 0; | |
335 | } | |
336 | ||
337 | // Indeterminate sense depends on the flag | |
338 | if (Sense == -1) | |
339 | { | |
340 | if ((A->Flags & InvBoolean) == InvBoolean) | |
341 | Sense = 0; | |
342 | else | |
343 | Sense = 1; | |
344 | } | |
345 | ||
346 | Conf->Set(A->ConfName,Sense); | |
347 | return true; | |
348 | } | |
349 | /*}}}*/ | |
bc4af0b9 | 350 | // CommandLine::FileSize - Count the number of filenames /*{{{*/ |
e1b74f61 AL |
351 | // --------------------------------------------------------------------- |
352 | /* */ | |
353 | unsigned int CommandLine::FileSize() const | |
354 | { | |
355 | unsigned int Count = 0; | |
356 | for (const char **I = FileList; I != 0 && *I != 0; I++) | |
357 | Count++; | |
358 | return Count; | |
359 | } | |
360 | /*}}}*/ | |
bc4af0b9 | 361 | // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ |
6079b276 | 362 | bool CommandLine::DispatchArg(Dispatch const * const Map,bool NoMatch) |
cbbee23e DK |
363 | { |
364 | int I; | |
365 | for (I = 0; Map[I].Match != 0; I++) | |
366 | { | |
367 | if (strcmp(FileList[0],Map[I].Match) == 0) | |
368 | { | |
369 | bool Res = Map[I].Handler(*this); | |
370 | if (Res == false && _error->PendingError() == false) | |
371 | _error->Error("Handler silently failed"); | |
372 | return Res; | |
373 | } | |
374 | } | |
375 | ||
376 | // No matching name | |
377 | if (Map[I].Match == 0) | |
378 | { | |
379 | if (NoMatch == true) | |
380 | _error->Error(_("Invalid operation %s"),FileList[0]); | |
381 | } | |
382 | ||
383 | return false; | |
384 | } | |
b0b4efb9 | 385 | bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) |
bc4af0b9 | 386 | { |
6079b276 DK |
387 | Dispatch const * const Map2 = Map; |
388 | return DispatchArg(Map2, NoMatch); | |
bc4af0b9 AL |
389 | } |
390 | /*}}}*/ | |
2bb25574 DK |
391 | // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/ |
392 | // --------------------------------------------------------------------- | |
393 | /* We save the commandline here to have it around later for e.g. logging. | |
394 | It feels a bit like a hack here and isn't bulletproof, but it is better | |
395 | than nothing after all. */ | |
396 | void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv) | |
397 | { | |
093e9f5d | 398 | char cmdline[100 + argc * 50]; |
70e0c168 | 399 | memset(cmdline, 0, sizeof(cmdline)); |
2bb25574 DK |
400 | unsigned int length = 0; |
401 | bool lastWasOption = false; | |
402 | bool closeQuote = false; | |
093e9f5d | 403 | for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length) |
2bb25574 DK |
404 | { |
405 | for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length) | |
406 | { | |
407 | cmdline[length] = argv[i][j]; | |
408 | if (lastWasOption == true && argv[i][j] == '=') | |
409 | { | |
410 | // That is possibly an option: Quote it if it includes spaces, | |
411 | // the benefit is that this will eliminate also most false positives | |
404528bd DK |
412 | const char* c = strchr(&argv[i][j+1], ' '); |
413 | if (c == NULL) continue; | |
2bb25574 DK |
414 | cmdline[++length] = '"'; |
415 | closeQuote = true; | |
416 | } | |
417 | } | |
418 | if (closeQuote == true) | |
419 | cmdline[length++] = '"'; | |
420 | // Problem: detects also --hello | |
421 | if (cmdline[length-1] == 'o') | |
422 | lastWasOption = true; | |
423 | cmdline[length] = ' '; | |
424 | } | |
425 | cmdline[--length] = '\0'; | |
426 | _config->Set("CommandLine::AsString", cmdline); | |
427 | } | |
428 | /*}}}*/ | |
b9179170 MV |
429 | CommandLine::Args CommandLine::MakeArgs(char ShortOpt, char const *LongOpt, char const *ConfName, unsigned long Flags)/*{{{*/ |
430 | { | |
431 | /* In theory, this should be a constructor for CommandLine::Args instead, | |
432 | but this breaks compatibility as gcc thinks this is a c++11 initializer_list */ | |
433 | CommandLine::Args arg; | |
434 | arg.ShortOpt = ShortOpt; | |
435 | arg.LongOpt = LongOpt; | |
436 | arg.ConfName = ConfName; | |
437 | arg.Flags = Flags; | |
438 | return arg; | |
439 | } | |
440 | /*}}}*/ |