]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cmndline.cc
merged from donkult
[apt.git] / apt-pkg / contrib / cmndline.cc
CommitLineData
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 /*{{{*/
08e8f724
AL
14#include <apt-pkg/cmndline.h>
15#include <apt-pkg/error.h>
cdcc6d34 16#include <apt-pkg/strutl.h>
b2e465d6
AL
17
18#include <apti18n.h>
08e8f724 19 /*}}}*/
584e4558 20using namespace std;
08e8f724
AL
21
22// CommandLine::CommandLine - Constructor /*{{{*/
23// ---------------------------------------------------------------------
24/* */
25CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
26 Conf(Conf), FileList(0)
27{
e1b74f61
AL
28}
29 /*}}}*/
30// CommandLine::~CommandLine - Destructor /*{{{*/
31// ---------------------------------------------------------------------
32/* */
33CommandLine::~CommandLine()
34{
35 delete [] FileList;
08e8f724
AL
36}
37 /*}}}*/
38// CommandLine::Parse - Main action member /*{{{*/
39// ---------------------------------------------------------------------
40/* */
41bool CommandLine::Parse(int argc,const char **argv)
42{
e1b74f61 43 delete [] FileList;
08e8f724
AL
44 FileList = new const char *[argc];
45 const char **Files = FileList;
46 int I;
47 for (I = 1; I != argc; I++)
48 {
49 const char *Opt = argv[I];
50
51 // It is not an option
52 if (*Opt != '-')
53 {
54 *Files++ = Opt;
55 continue;
56 }
57
58 Opt++;
59
60 // Double dash signifies the end of option processing
61 if (*Opt == '-' && Opt[1] == 0)
343bd48e
AL
62 {
63 I++;
08e8f724 64 break;
343bd48e 65 }
08e8f724
AL
66
67 // Single dash is a short option
68 if (*Opt != '-')
69 {
70 // Iterate over each letter
71 while (*Opt != 0)
72 {
73 // Search for the option
74 Args *A;
75 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
76 if (A->end() == true)
b2e465d6 77 return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]);
08e8f724
AL
78
79 if (HandleOpt(I,argc,argv,Opt,A) == false)
80 return false;
81 if (*Opt != 0)
82 Opt++;
83 }
84 continue;
85 }
86
87 Opt++;
88
89 // Match up to a = against the list
90 const char *OptEnd = Opt;
91 Args *A;
92 for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
93 for (A = ArgList; A->end() == false &&
94 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
95
96 // Failed, look for a word after the first - (no-foo)
0d47bd08 97 bool PreceedMatch = false;
08e8f724
AL
98 if (A->end() == true)
99 {
100 for (; Opt != OptEnd && *Opt != '-'; Opt++);
101
102 if (Opt == OptEnd)
b2e465d6 103 return _error->Error(_("Command line option %s is not understood"),argv[I]);
08e8f724
AL
104 Opt++;
105
106 for (A = ArgList; A->end() == false &&
107 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
108
109 // Failed again..
110 if (A->end() == true && OptEnd - Opt != 1)
b2e465d6 111 return _error->Error(_("Command line option %s is not understood"),argv[I]);
0d47bd08 112
08e8f724 113 // The option could be a single letter option prefixed by a no-..
08e8f724 114 if (A->end() == true)
0d47bd08
AL
115 {
116 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
117
118 if (A->end() == true)
b2e465d6 119 return _error->Error(_("Command line option %s is not understood"),argv[I]);
0d47bd08 120 }
e1b74f61
AL
121
122 // The option is not boolean
123 if (A->IsBoolean() == false)
b2e465d6 124 return _error->Error(_("Command line option %s is not boolean"),argv[I]);
0d47bd08 125 PreceedMatch = true;
08e8f724
AL
126 }
127
128 // Deal with it.
129 OptEnd--;
0d47bd08 130 if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
08e8f724
AL
131 return false;
132 }
133
134 // Copy any remaining file names over
135 for (; I != argc; I++)
136 *Files++ = argv[I];
137 *Files = 0;
2bb25574
DK
138
139 SaveInConfig(argc, argv);
140
08e8f724
AL
141 return true;
142}
143 /*}}}*/
144// CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
145// ---------------------------------------------------------------------
146/* This is a helper function for parser, it looks at a given argument
147 and looks for specific patterns in the string, it gets tokanized
148 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
149bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
0d47bd08 150 const char *&Opt,Args *A,bool PreceedMatch)
08e8f724
AL
151{
152 const char *Argument = 0;
153 bool CertainArg = false;
154 int IncI = 0;
155
156 /* Determine the possible location of an option or 0 if their is
157 no option */
158 if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
159 {
160 if (I + 1 < argc && argv[I+1][0] != '-')
161 Argument = argv[I+1];
162
163 // Equals was specified but we fell off the end!
164 if (Opt[1] == '=' && Argument == 0)
b2e465d6 165 return _error->Error(_("Option %s requires an argument."),argv[I]);
08e8f724
AL
166 if (Opt[1] == '=')
167 CertainArg = true;
168
169 IncI = 1;
170 }
171 else
172 {
173 if (Opt[1] == '=')
174 {
175 CertainArg = true;
176 Argument = Opt + 2;
177 }
178 else
179 Argument = Opt + 1;
180 }
0d47bd08 181
08e8f724
AL
182 // Option is an argument set
183 if ((A->Flags & HasArg) == HasArg)
184 {
185 if (Argument == 0)
b2e465d6 186 return _error->Error(_("Option %s requires an argument."),argv[I]);
08e8f724
AL
187 Opt += strlen(Opt);
188 I += IncI;
189
190 // Parse a configuration file
191 if ((A->Flags & ConfigFile) == ConfigFile)
192 return ReadConfigFile(*Conf,Argument);
e1b74f61 193
0da8987a 194 // Arbitrary item specification
e1b74f61
AL
195 if ((A->Flags & ArbItem) == ArbItem)
196 {
197 const char *J;
198 for (J = Argument; *J != 0 && *J != '='; J++);
199 if (*J == 0)
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
7f25bdff
AL
215 const char *I = A->ConfName;
216 for (; *I != 0 && *I != ' '; I++);
217 if (*I == ' ')
218 Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument);
219 else
220 Conf->Set(A->ConfName,string(I) + Argument);
221
08e8f724
AL
222 return true;
223 }
224
225 // Option is an integer level
226 if ((A->Flags & IntLevel) == IntLevel)
227 {
228 // There might be an argument
229 if (Argument != 0)
230 {
231 char *EndPtr;
232 unsigned long Value = strtol(Argument,&EndPtr,10);
233
234 // Conversion failed and the argument was specified with an =s
235 if (EndPtr == Argument && CertainArg == true)
b2e465d6 236 return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument);
08e8f724
AL
237
238 // Conversion was ok, set the value and return
9435cc9b 239 if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0)
08e8f724
AL
240 {
241 Conf->Set(A->ConfName,Value);
242 Opt += strlen(Opt);
243 I += IncI;
244 return true;
245 }
246 }
247
248 // Increase the level
249 Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
250 return true;
251 }
252
253 // Option is a boolean
254 int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
255
256 // Look for an argument.
257 while (1)
258 {
259 // Look at preceeding text
260 char Buffer[300];
261 if (Argument == 0)
262 {
0d47bd08
AL
263 if (PreceedMatch == false)
264 break;
265
08e8f724 266 if (strlen(argv[I]) >= sizeof(Buffer))
b2e465d6 267 return _error->Error(_("Option '%s' is too long"),argv[I]);
0d47bd08
AL
268
269 // Skip the leading dash
08e8f724
AL
270 const char *J = argv[I];
271 for (; *J != 0 && *J == '-'; J++);
0d47bd08 272
08e8f724
AL
273 const char *JEnd = J;
274 for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
275 if (*JEnd != 0)
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
3b5421b4
AL
286 // Check for boolean
287 Sense = StringToBool(Argument);
288 if (Sense >= 0)
08e8f724 289 {
08e8f724
AL
290 // Eat the argument
291 if (Argument != Buffer)
292 {
293 Opt += strlen(Opt);
294 I += IncI;
295 }
296 break;
297 }
298
08e8f724 299 if (CertainArg == true)
b2e465d6 300 return _error->Error(_("Sense %s is not understood, try true or false."),Argument);
08e8f724
AL
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 /*}}}*/
bc4af0b9 318// CommandLine::FileSize - Count the number of filenames /*{{{*/
e1b74f61
AL
319// ---------------------------------------------------------------------
320/* */
321unsigned 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 /*}}}*/
bc4af0b9
AL
329// CommandLine::DispatchArg - Do something with the first arg /*{{{*/
330// ---------------------------------------------------------------------
331/* */
b0b4efb9 332bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
bc4af0b9
AL
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)
b0b4efb9
AL
348 {
349 if (NoMatch == true)
b2e465d6 350 _error->Error(_("Invalid operation %s"),FileList[0]);
b0b4efb9
AL
351 }
352
bc4af0b9
AL
353 return false;
354}
355 /*}}}*/
2bb25574
DK
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. */
361void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv)
362{
093e9f5d 363 char cmdline[100 + argc * 50];
2bb25574
DK
364 unsigned int length = 0;
365 bool lastWasOption = false;
366 bool closeQuote = false;
093e9f5d 367 for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length)
2bb25574
DK
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 = &argv[i][j+1];
377 for (; *c != '\0' && *c != ' '; ++c);
378 if (*c == '\0') continue;
379 cmdline[++length] = '"';
380 closeQuote = true;
381 }
382 }
383 if (closeQuote == true)
384 cmdline[length++] = '"';
385 // Problem: detects also --hello
386 if (cmdline[length-1] == 'o')
387 lastWasOption = true;
388 cmdline[length] = ' ';
389 }
390 cmdline[--length] = '\0';
391 _config->Set("CommandLine::AsString", cmdline);
392}
393 /*}}}*/