]>
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 | |
08e8f724 | 93 | Args *A; |
404528bd | 94 | const char *OptEnd = strchrnul(Opt, '='); |
08e8f724 AL |
95 | for (A = ArgList; A->end() == false && |
96 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
97 | ||
98 | // Failed, look for a word after the first - (no-foo) | |
0d47bd08 | 99 | bool PreceedMatch = false; |
08e8f724 AL |
100 | if (A->end() == true) |
101 | { | |
404528bd DK |
102 | Opt = (const char*) memchr(Opt, '-', OptEnd - Opt); |
103 | if (Opt == NULL) | |
b2e465d6 | 104 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
08e8f724 AL |
105 | Opt++; |
106 | ||
107 | for (A = ArgList; A->end() == false && | |
108 | stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); | |
109 | ||
110 | // Failed again.. | |
111 | if (A->end() == true && OptEnd - Opt != 1) | |
b2e465d6 | 112 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
0d47bd08 | 113 | |
08e8f724 | 114 | // The option could be a single letter option prefixed by a no-.. |
08e8f724 | 115 | if (A->end() == true) |
0d47bd08 AL |
116 | { |
117 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
118 | ||
119 | if (A->end() == true) | |
b2e465d6 | 120 | return _error->Error(_("Command line option %s is not understood"),argv[I]); |
0d47bd08 | 121 | } |
e1b74f61 AL |
122 | |
123 | // The option is not boolean | |
124 | if (A->IsBoolean() == false) | |
b2e465d6 | 125 | return _error->Error(_("Command line option %s is not boolean"),argv[I]); |
0d47bd08 | 126 | PreceedMatch = true; |
08e8f724 AL |
127 | } |
128 | ||
129 | // Deal with it. | |
130 | OptEnd--; | |
0d47bd08 | 131 | if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false) |
08e8f724 AL |
132 | return false; |
133 | } | |
134 | ||
135 | // Copy any remaining file names over | |
136 | for (; I != argc; I++) | |
137 | *Files++ = argv[I]; | |
138 | *Files = 0; | |
2bb25574 DK |
139 | |
140 | SaveInConfig(argc, argv); | |
141 | ||
08e8f724 AL |
142 | return true; |
143 | } | |
144 | /*}}}*/ | |
145 | // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/ | |
146 | // --------------------------------------------------------------------- | |
147 | /* This is a helper function for parser, it looks at a given argument | |
148 | and looks for specific patterns in the string, it gets tokanized | |
149 | -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */ | |
150 | bool CommandLine::HandleOpt(int &I,int argc,const char *argv[], | |
0d47bd08 | 151 | const char *&Opt,Args *A,bool PreceedMatch) |
08e8f724 AL |
152 | { |
153 | const char *Argument = 0; | |
154 | bool CertainArg = false; | |
155 | int IncI = 0; | |
156 | ||
157 | /* Determine the possible location of an option or 0 if their is | |
158 | no option */ | |
159 | if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0)) | |
160 | { | |
161 | if (I + 1 < argc && argv[I+1][0] != '-') | |
162 | Argument = argv[I+1]; | |
163 | ||
164 | // Equals was specified but we fell off the end! | |
165 | if (Opt[1] == '=' && Argument == 0) | |
b2e465d6 | 166 | return _error->Error(_("Option %s requires an argument."),argv[I]); |
08e8f724 AL |
167 | if (Opt[1] == '=') |
168 | CertainArg = true; | |
169 | ||
170 | IncI = 1; | |
171 | } | |
172 | else | |
173 | { | |
174 | if (Opt[1] == '=') | |
175 | { | |
176 | CertainArg = true; | |
177 | Argument = Opt + 2; | |
178 | } | |
179 | else | |
180 | Argument = Opt + 1; | |
181 | } | |
0d47bd08 | 182 | |
08e8f724 AL |
183 | // Option is an argument set |
184 | if ((A->Flags & HasArg) == HasArg) | |
185 | { | |
186 | if (Argument == 0) | |
b2e465d6 | 187 | return _error->Error(_("Option %s requires an argument."),argv[I]); |
08e8f724 AL |
188 | Opt += strlen(Opt); |
189 | I += IncI; | |
190 | ||
191 | // Parse a configuration file | |
192 | if ((A->Flags & ConfigFile) == ConfigFile) | |
193 | return ReadConfigFile(*Conf,Argument); | |
e1b74f61 | 194 | |
0da8987a | 195 | // Arbitrary item specification |
e1b74f61 AL |
196 | if ((A->Flags & ArbItem) == ArbItem) |
197 | { | |
404528bd DK |
198 | const char *J = strchr(Argument, '='); |
199 | if (J == NULL) | |
bac2e715 | 200 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); |
e1b74f61 | 201 | |
7e798dd7 AL |
202 | // = is trailing |
203 | if (J[1] == 0) | |
204 | { | |
205 | if (I+1 >= argc) | |
bac2e715 | 206 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); |
7e798dd7 AL |
207 | Conf->Set(string(Argument,J-Argument),string(argv[I++ +1])); |
208 | } | |
209 | else | |
210 | Conf->Set(string(Argument,J-Argument),string(J+1)); | |
e1b74f61 AL |
211 | |
212 | return true; | |
213 | } | |
08e8f724 | 214 | |
404528bd | 215 | const char *I = strchrnul(A->ConfName, ' '); |
7f25bdff AL |
216 | if (*I == ' ') |
217 | Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument); | |
218 | else | |
219 | Conf->Set(A->ConfName,string(I) + Argument); | |
220 | ||
08e8f724 AL |
221 | return true; |
222 | } | |
223 | ||
224 | // Option is an integer level | |
225 | if ((A->Flags & IntLevel) == IntLevel) | |
226 | { | |
227 | // There might be an argument | |
228 | if (Argument != 0) | |
229 | { | |
230 | char *EndPtr; | |
231 | unsigned long Value = strtol(Argument,&EndPtr,10); | |
232 | ||
233 | // Conversion failed and the argument was specified with an =s | |
234 | if (EndPtr == Argument && CertainArg == true) | |
b2e465d6 | 235 | return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument); |
08e8f724 AL |
236 | |
237 | // Conversion was ok, set the value and return | |
9435cc9b | 238 | if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0) |
08e8f724 AL |
239 | { |
240 | Conf->Set(A->ConfName,Value); | |
241 | Opt += strlen(Opt); | |
242 | I += IncI; | |
243 | return true; | |
244 | } | |
245 | } | |
246 | ||
247 | // Increase the level | |
248 | Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1); | |
249 | return true; | |
250 | } | |
251 | ||
252 | // Option is a boolean | |
253 | int Sense = -1; // -1 is unspecified, 0 is yes 1 is no | |
254 | ||
255 | // Look for an argument. | |
256 | while (1) | |
257 | { | |
258 | // Look at preceeding text | |
259 | char Buffer[300]; | |
260 | if (Argument == 0) | |
261 | { | |
0d47bd08 AL |
262 | if (PreceedMatch == false) |
263 | break; | |
264 | ||
08e8f724 | 265 | if (strlen(argv[I]) >= sizeof(Buffer)) |
b2e465d6 | 266 | return _error->Error(_("Option '%s' is too long"),argv[I]); |
0d47bd08 AL |
267 | |
268 | // Skip the leading dash | |
08e8f724 AL |
269 | const char *J = argv[I]; |
270 | for (; *J != 0 && *J == '-'; J++); | |
404528bd DK |
271 | |
272 | const char *JEnd = strchr(J, '-'); | |
273 | if (JEnd != NULL) | |
08e8f724 AL |
274 | { |
275 | strncpy(Buffer,J,JEnd - J); | |
276 | Buffer[JEnd - J] = 0; | |
277 | Argument = Buffer; | |
278 | CertainArg = true; | |
279 | } | |
280 | else | |
281 | break; | |
282 | } | |
283 | ||
3b5421b4 AL |
284 | // Check for boolean |
285 | Sense = StringToBool(Argument); | |
286 | if (Sense >= 0) | |
08e8f724 | 287 | { |
08e8f724 AL |
288 | // Eat the argument |
289 | if (Argument != Buffer) | |
290 | { | |
291 | Opt += strlen(Opt); | |
292 | I += IncI; | |
293 | } | |
294 | break; | |
295 | } | |
296 | ||
08e8f724 | 297 | if (CertainArg == true) |
b2e465d6 | 298 | return _error->Error(_("Sense %s is not understood, try true or false."),Argument); |
08e8f724 AL |
299 | |
300 | Argument = 0; | |
301 | } | |
302 | ||
303 | // Indeterminate sense depends on the flag | |
304 | if (Sense == -1) | |
305 | { | |
306 | if ((A->Flags & InvBoolean) == InvBoolean) | |
307 | Sense = 0; | |
308 | else | |
309 | Sense = 1; | |
310 | } | |
311 | ||
312 | Conf->Set(A->ConfName,Sense); | |
313 | return true; | |
314 | } | |
315 | /*}}}*/ | |
bc4af0b9 | 316 | // CommandLine::FileSize - Count the number of filenames /*{{{*/ |
e1b74f61 AL |
317 | // --------------------------------------------------------------------- |
318 | /* */ | |
319 | unsigned int CommandLine::FileSize() const | |
320 | { | |
321 | unsigned int Count = 0; | |
322 | for (const char **I = FileList; I != 0 && *I != 0; I++) | |
323 | Count++; | |
324 | return Count; | |
325 | } | |
326 | /*}}}*/ | |
bc4af0b9 AL |
327 | // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ |
328 | // --------------------------------------------------------------------- | |
329 | /* */ | |
b0b4efb9 | 330 | bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) |
bc4af0b9 AL |
331 | { |
332 | int I; | |
333 | for (I = 0; Map[I].Match != 0; I++) | |
334 | { | |
335 | if (strcmp(FileList[0],Map[I].Match) == 0) | |
336 | { | |
337 | bool Res = Map[I].Handler(*this); | |
338 | if (Res == false && _error->PendingError() == false) | |
339 | _error->Error("Handler silently failed"); | |
340 | return Res; | |
341 | } | |
342 | } | |
343 | ||
344 | // No matching name | |
345 | if (Map[I].Match == 0) | |
b0b4efb9 AL |
346 | { |
347 | if (NoMatch == true) | |
b2e465d6 | 348 | _error->Error(_("Invalid operation %s"),FileList[0]); |
b0b4efb9 AL |
349 | } |
350 | ||
bc4af0b9 AL |
351 | return false; |
352 | } | |
353 | /*}}}*/ | |
2bb25574 DK |
354 | // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/ |
355 | // --------------------------------------------------------------------- | |
356 | /* We save the commandline here to have it around later for e.g. logging. | |
357 | It feels a bit like a hack here and isn't bulletproof, but it is better | |
358 | than nothing after all. */ | |
359 | void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv) | |
360 | { | |
093e9f5d | 361 | char cmdline[100 + argc * 50]; |
2bb25574 DK |
362 | unsigned int length = 0; |
363 | bool lastWasOption = false; | |
364 | bool closeQuote = false; | |
093e9f5d | 365 | for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length) |
2bb25574 DK |
366 | { |
367 | for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length) | |
368 | { | |
369 | cmdline[length] = argv[i][j]; | |
370 | if (lastWasOption == true && argv[i][j] == '=') | |
371 | { | |
372 | // That is possibly an option: Quote it if it includes spaces, | |
373 | // the benefit is that this will eliminate also most false positives | |
404528bd DK |
374 | const char* c = strchr(&argv[i][j+1], ' '); |
375 | if (c == NULL) continue; | |
2bb25574 DK |
376 | cmdline[++length] = '"'; |
377 | closeQuote = true; | |
378 | } | |
379 | } | |
380 | if (closeQuote == true) | |
381 | cmdline[length++] = '"'; | |
382 | // Problem: detects also --hello | |
383 | if (cmdline[length-1] == 'o') | |
384 | lastWasOption = true; | |
385 | cmdline[length] = ' '; | |
386 | } | |
387 | cmdline[--length] = '\0'; | |
388 | _config->Set("CommandLine::AsString", cmdline); | |
389 | } | |
390 | /*}}}*/ |