]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: cmndline.cc,v 1.15 2003/02/10 01:40:58 doogie Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Command Line Class - Sophisticated command line parser | |
7 | ||
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 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | // Include files /*{{{*/ | |
14 | #include<config.h> | |
15 | ||
16 | #include <apt-pkg/configuration.h> | |
17 | #include <apt-pkg/cmndline.h> | |
18 | #include <apt-pkg/error.h> | |
19 | #include <apt-pkg/strutl.h> | |
20 | ||
21 | #include <apti18n.h> | |
22 | /*}}}*/ | |
23 | using namespace std; | |
24 | ||
25 | // CommandLine::CommandLine - Constructor /*{{{*/ | |
26 | // --------------------------------------------------------------------- | |
27 | /* */ | |
28 | CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList), | |
29 | Conf(Conf), FileList(0) | |
30 | { | |
31 | } | |
32 | /*}}}*/ | |
33 | // CommandLine::~CommandLine - Destructor /*{{{*/ | |
34 | // --------------------------------------------------------------------- | |
35 | /* */ | |
36 | CommandLine::~CommandLine() | |
37 | { | |
38 | delete [] FileList; | |
39 | } | |
40 | /*}}}*/ | |
41 | // CommandLine::Parse - Main action member /*{{{*/ | |
42 | // --------------------------------------------------------------------- | |
43 | /* */ | |
44 | bool CommandLine::Parse(int argc,const char **argv) | |
45 | { | |
46 | delete [] FileList; | |
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) | |
65 | { | |
66 | I++; | |
67 | break; | |
68 | } | |
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) | |
80 | return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]); | |
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 | Args *A; | |
94 | const char *OptEnd = strchrnul(Opt, '='); | |
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) | |
99 | bool PreceedMatch = false; | |
100 | if (A->end() == true) | |
101 | { | |
102 | Opt = (const char*) memchr(Opt, '-', OptEnd - Opt); | |
103 | if (Opt == NULL) | |
104 | return _error->Error(_("Command line option %s is not understood"),argv[I]); | |
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) | |
112 | return _error->Error(_("Command line option %s is not understood"),argv[I]); | |
113 | ||
114 | // The option could be a single letter option prefixed by a no-.. | |
115 | if (A->end() == true) | |
116 | { | |
117 | for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++); | |
118 | ||
119 | if (A->end() == true) | |
120 | return _error->Error(_("Command line option %s is not understood"),argv[I]); | |
121 | } | |
122 | ||
123 | // The option is not boolean | |
124 | if (A->IsBoolean() == false) | |
125 | return _error->Error(_("Command line option %s is not boolean"),argv[I]); | |
126 | PreceedMatch = true; | |
127 | } | |
128 | ||
129 | // Deal with it. | |
130 | OptEnd--; | |
131 | if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false) | |
132 | return false; | |
133 | } | |
134 | ||
135 | // Copy any remaining file names over | |
136 | for (; I != argc; I++) | |
137 | *Files++ = argv[I]; | |
138 | *Files = 0; | |
139 | ||
140 | SaveInConfig(argc, argv); | |
141 | ||
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[], | |
151 | const char *&Opt,Args *A,bool PreceedMatch) | |
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) | |
166 | return _error->Error(_("Option %s requires an argument."),argv[I]); | |
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 | } | |
182 | ||
183 | // Option is an argument set | |
184 | if ((A->Flags & HasArg) == HasArg) | |
185 | { | |
186 | if (Argument == 0) | |
187 | return _error->Error(_("Option %s requires an argument."),argv[I]); | |
188 | Opt += strlen(Opt); | |
189 | I += IncI; | |
190 | ||
191 | // Parse a configuration file | |
192 | if ((A->Flags & ConfigFile) == ConfigFile) | |
193 | return ReadConfigFile(*Conf,Argument); | |
194 | ||
195 | // Arbitrary item specification | |
196 | if ((A->Flags & ArbItem) == ArbItem) | |
197 | { | |
198 | const char *J = strchr(Argument, '='); | |
199 | if (J == NULL) | |
200 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); | |
201 | ||
202 | // = is trailing | |
203 | if (J[1] == 0) | |
204 | { | |
205 | if (I+1 >= argc) | |
206 | return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]); | |
207 | Conf->Set(string(Argument,J-Argument),string(argv[I++ +1])); | |
208 | } | |
209 | else | |
210 | Conf->Set(string(Argument,J-Argument),string(J+1)); | |
211 | ||
212 | return true; | |
213 | } | |
214 | ||
215 | const char *I = strchrnul(A->ConfName, ' '); | |
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 | ||
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) | |
235 | return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument); | |
236 | ||
237 | // Conversion was ok, set the value and return | |
238 | if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0) | |
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 | { | |
262 | if (PreceedMatch == false) | |
263 | break; | |
264 | ||
265 | if (strlen(argv[I]) >= sizeof(Buffer)) | |
266 | return _error->Error(_("Option '%s' is too long"),argv[I]); | |
267 | ||
268 | // Skip the leading dash | |
269 | const char *J = argv[I]; | |
270 | for (; *J != 0 && *J == '-'; J++); | |
271 | ||
272 | const char *JEnd = strchr(J, '-'); | |
273 | if (JEnd != NULL) | |
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 | ||
284 | // Check for boolean | |
285 | Sense = StringToBool(Argument); | |
286 | if (Sense >= 0) | |
287 | { | |
288 | // Eat the argument | |
289 | if (Argument != Buffer) | |
290 | { | |
291 | Opt += strlen(Opt); | |
292 | I += IncI; | |
293 | } | |
294 | break; | |
295 | } | |
296 | ||
297 | if (CertainArg == true) | |
298 | return _error->Error(_("Sense %s is not understood, try true or false."),Argument); | |
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 | /*}}}*/ | |
316 | // CommandLine::FileSize - Count the number of filenames /*{{{*/ | |
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 | /*}}}*/ | |
327 | // CommandLine::DispatchArg - Do something with the first arg /*{{{*/ | |
328 | // --------------------------------------------------------------------- | |
329 | /* */ | |
330 | bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch) | |
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) | |
346 | { | |
347 | if (NoMatch == true) | |
348 | _error->Error(_("Invalid operation %s"),FileList[0]); | |
349 | } | |
350 | ||
351 | return false; | |
352 | } | |
353 | /*}}}*/ | |
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 | { | |
361 | char cmdline[100 + argc * 50]; | |
362 | unsigned int length = 0; | |
363 | bool lastWasOption = false; | |
364 | bool closeQuote = false; | |
365 | for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length) | |
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 | |
374 | const char* c = strchr(&argv[i][j+1], ' '); | |
375 | if (c == NULL) continue; | |
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 | /*}}}*/ |