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