]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/cmndline.cc
Increase the range of the ID type for deps
[apt.git] / apt-pkg / contrib / cmndline.cc
CommitLineData
08e8f724
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
343bd48e 3// $Id: cmndline.cc,v 1.12 2001/06/09 22:39:48 jgg Exp $
08e8f724
AL
4/* ######################################################################
5
6 Command Line Class - Sophisticated command line parser
7
8 ##################################################################### */
9 /*}}}*/
10// Include files /*{{{*/
11#ifdef __GNUG__
12#pragma implementation "apt-pkg/cmndline.h"
13#endif
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
AL
19 /*}}}*/
20
21// CommandLine::CommandLine - Constructor /*{{{*/
22// ---------------------------------------------------------------------
23/* */
24CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
25 Conf(Conf), FileList(0)
26{
e1b74f61
AL
27}
28 /*}}}*/
29// CommandLine::~CommandLine - Destructor /*{{{*/
30// ---------------------------------------------------------------------
31/* */
32CommandLine::~CommandLine()
33{
34 delete [] FileList;
08e8f724
AL
35}
36 /*}}}*/
37// CommandLine::Parse - Main action member /*{{{*/
38// ---------------------------------------------------------------------
39/* */
40bool CommandLine::Parse(int argc,const char **argv)
41{
e1b74f61 42 delete [] FileList;
08e8f724
AL
43 FileList = new const char *[argc];
44 const char **Files = FileList;
45 int I;
46 for (I = 1; I != argc; I++)
47 {
48 const char *Opt = argv[I];
49
50 // It is not an option
51 if (*Opt != '-')
52 {
53 *Files++ = Opt;
54 continue;
55 }
56
57 Opt++;
58
59 // Double dash signifies the end of option processing
60 if (*Opt == '-' && Opt[1] == 0)
343bd48e
AL
61 {
62 I++;
08e8f724 63 break;
343bd48e 64 }
08e8f724
AL
65
66 // Single dash is a short option
67 if (*Opt != '-')
68 {
69 // Iterate over each letter
70 while (*Opt != 0)
71 {
72 // Search for the option
73 Args *A;
74 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
75 if (A->end() == true)
b2e465d6 76 return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]);
08e8f724
AL
77
78 if (HandleOpt(I,argc,argv,Opt,A) == false)
79 return false;
80 if (*Opt != 0)
81 Opt++;
82 }
83 continue;
84 }
85
86 Opt++;
87
88 // Match up to a = against the list
89 const char *OptEnd = Opt;
90 Args *A;
91 for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
92 for (A = ArgList; A->end() == false &&
93 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
94
95 // Failed, look for a word after the first - (no-foo)
0d47bd08 96 bool PreceedMatch = false;
08e8f724
AL
97 if (A->end() == true)
98 {
99 for (; Opt != OptEnd && *Opt != '-'; Opt++);
100
101 if (Opt == OptEnd)
b2e465d6 102 return _error->Error(_("Command line option %s is not understood"),argv[I]);
08e8f724
AL
103 Opt++;
104
105 for (A = ArgList; A->end() == false &&
106 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
107
108 // Failed again..
109 if (A->end() == true && OptEnd - Opt != 1)
b2e465d6 110 return _error->Error(_("Command line option %s is not understood"),argv[I]);
0d47bd08 111
08e8f724 112 // The option could be a single letter option prefixed by a no-..
08e8f724 113 if (A->end() == true)
0d47bd08
AL
114 {
115 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
116
117 if (A->end() == true)
b2e465d6 118 return _error->Error(_("Command line option %s is not understood"),argv[I]);
0d47bd08 119 }
e1b74f61
AL
120
121 // The option is not boolean
122 if (A->IsBoolean() == false)
b2e465d6 123 return _error->Error(_("Command line option %s is not boolean"),argv[I]);
0d47bd08 124 PreceedMatch = true;
08e8f724
AL
125 }
126
127 // Deal with it.
128 OptEnd--;
0d47bd08 129 if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
08e8f724
AL
130 return false;
131 }
132
133 // Copy any remaining file names over
134 for (; I != argc; I++)
135 *Files++ = argv[I];
136 *Files = 0;
137
138 return true;
139}
140 /*}}}*/
141// CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
142// ---------------------------------------------------------------------
143/* This is a helper function for parser, it looks at a given argument
144 and looks for specific patterns in the string, it gets tokanized
145 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
146bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
0d47bd08 147 const char *&Opt,Args *A,bool PreceedMatch)
08e8f724
AL
148{
149 const char *Argument = 0;
150 bool CertainArg = false;
151 int IncI = 0;
152
153 /* Determine the possible location of an option or 0 if their is
154 no option */
155 if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
156 {
157 if (I + 1 < argc && argv[I+1][0] != '-')
158 Argument = argv[I+1];
159
160 // Equals was specified but we fell off the end!
161 if (Opt[1] == '=' && Argument == 0)
b2e465d6 162 return _error->Error(_("Option %s requires an argument."),argv[I]);
08e8f724
AL
163 if (Opt[1] == '=')
164 CertainArg = true;
165
166 IncI = 1;
167 }
168 else
169 {
170 if (Opt[1] == '=')
171 {
172 CertainArg = true;
173 Argument = Opt + 2;
174 }
175 else
176 Argument = Opt + 1;
177 }
0d47bd08 178
08e8f724
AL
179 // Option is an argument set
180 if ((A->Flags & HasArg) == HasArg)
181 {
182 if (Argument == 0)
b2e465d6 183 return _error->Error(_("Option %s requires an argument."),argv[I]);
08e8f724
AL
184 Opt += strlen(Opt);
185 I += IncI;
186
187 // Parse a configuration file
188 if ((A->Flags & ConfigFile) == ConfigFile)
189 return ReadConfigFile(*Conf,Argument);
e1b74f61
AL
190
191 // Arbitary item specification
192 if ((A->Flags & ArbItem) == ArbItem)
193 {
194 const char *J;
195 for (J = Argument; *J != 0 && *J != '='; J++);
196 if (*J == 0)
b2e465d6 197 return _error->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv[I]);
e1b74f61 198
7e798dd7
AL
199 // = is trailing
200 if (J[1] == 0)
201 {
202 if (I+1 >= argc)
b2e465d6 203 return _error->Error(_("Option %s: Configuration item sepecification must have an =<val>."),argv[I]);
7e798dd7
AL
204 Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
205 }
206 else
207 Conf->Set(string(Argument,J-Argument),string(J+1));
e1b74f61
AL
208
209 return true;
210 }
08e8f724 211
7f25bdff
AL
212 const char *I = A->ConfName;
213 for (; *I != 0 && *I != ' '; I++);
214 if (*I == ' ')
215 Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument);
216 else
217 Conf->Set(A->ConfName,string(I) + Argument);
218
08e8f724
AL
219 return true;
220 }
221
222 // Option is an integer level
223 if ((A->Flags & IntLevel) == IntLevel)
224 {
225 // There might be an argument
226 if (Argument != 0)
227 {
228 char *EndPtr;
229 unsigned long Value = strtol(Argument,&EndPtr,10);
230
231 // Conversion failed and the argument was specified with an =s
232 if (EndPtr == Argument && CertainArg == true)
b2e465d6 233 return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument);
08e8f724
AL
234
235 // Conversion was ok, set the value and return
9435cc9b 236 if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0)
08e8f724
AL
237 {
238 Conf->Set(A->ConfName,Value);
239 Opt += strlen(Opt);
240 I += IncI;
241 return true;
242 }
243 }
244
245 // Increase the level
246 Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
247 return true;
248 }
249
250 // Option is a boolean
251 int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
252
253 // Look for an argument.
254 while (1)
255 {
256 // Look at preceeding text
257 char Buffer[300];
258 if (Argument == 0)
259 {
0d47bd08
AL
260 if (PreceedMatch == false)
261 break;
262
08e8f724 263 if (strlen(argv[I]) >= sizeof(Buffer))
b2e465d6 264 return _error->Error(_("Option '%s' is too long"),argv[I]);
0d47bd08
AL
265
266 // Skip the leading dash
08e8f724
AL
267 const char *J = argv[I];
268 for (; *J != 0 && *J == '-'; J++);
0d47bd08 269
08e8f724
AL
270 const char *JEnd = J;
271 for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
272 if (*JEnd != 0)
273 {
274 strncpy(Buffer,J,JEnd - J);
275 Buffer[JEnd - J] = 0;
276 Argument = Buffer;
277 CertainArg = true;
278 }
279 else
280 break;
281 }
282
3b5421b4
AL
283 // Check for boolean
284 Sense = StringToBool(Argument);
285 if (Sense >= 0)
08e8f724 286 {
08e8f724
AL
287 // Eat the argument
288 if (Argument != Buffer)
289 {
290 Opt += strlen(Opt);
291 I += IncI;
292 }
293 break;
294 }
295
08e8f724 296 if (CertainArg == true)
b2e465d6 297 return _error->Error(_("Sense %s is not understood, try true or false."),Argument);
08e8f724
AL
298
299 Argument = 0;
300 }
301
302 // Indeterminate sense depends on the flag
303 if (Sense == -1)
304 {
305 if ((A->Flags & InvBoolean) == InvBoolean)
306 Sense = 0;
307 else
308 Sense = 1;
309 }
310
311 Conf->Set(A->ConfName,Sense);
312 return true;
313}
314 /*}}}*/
bc4af0b9 315// CommandLine::FileSize - Count the number of filenames /*{{{*/
e1b74f61
AL
316// ---------------------------------------------------------------------
317/* */
318unsigned int CommandLine::FileSize() const
319{
320 unsigned int Count = 0;
321 for (const char **I = FileList; I != 0 && *I != 0; I++)
322 Count++;
323 return Count;
324}
325 /*}}}*/
bc4af0b9
AL
326// CommandLine::DispatchArg - Do something with the first arg /*{{{*/
327// ---------------------------------------------------------------------
328/* */
b0b4efb9 329bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
bc4af0b9
AL
330{
331 int I;
332 for (I = 0; Map[I].Match != 0; I++)
333 {
334 if (strcmp(FileList[0],Map[I].Match) == 0)
335 {
336 bool Res = Map[I].Handler(*this);
337 if (Res == false && _error->PendingError() == false)
338 _error->Error("Handler silently failed");
339 return Res;
340 }
341 }
342
343 // No matching name
344 if (Map[I].Match == 0)
b0b4efb9
AL
345 {
346 if (NoMatch == true)
b2e465d6 347 _error->Error(_("Invalid operation %s"),FileList[0]);
b0b4efb9
AL
348 }
349
bc4af0b9
AL
350 return false;
351}
352 /*}}}*/