]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
Added the remove command
[apt.git] / apt-pkg / contrib / cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: cmndline.cc,v 1.3 1998/10/08 04:55:01 jgg Exp $
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>
16 #include <strutl.h>
17 /*}}}*/
18
19 // CommandLine::CommandLine - Constructor /*{{{*/
20 // ---------------------------------------------------------------------
21 /* */
22 CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
23 Conf(Conf), FileList(0)
24 {
25 }
26 /*}}}*/
27 // CommandLine::~CommandLine - Destructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 CommandLine::~CommandLine()
31 {
32 delete [] FileList;
33 }
34 /*}}}*/
35 // CommandLine::Parse - Main action member /*{{{*/
36 // ---------------------------------------------------------------------
37 /* */
38 bool CommandLine::Parse(int argc,const char **argv)
39 {
40 delete [] FileList;
41 FileList = new const char *[argc];
42 const char **Files = FileList;
43 int I;
44 for (I = 1; I != argc; I++)
45 {
46 const char *Opt = argv[I];
47
48 // It is not an option
49 if (*Opt != '-')
50 {
51 *Files++ = Opt;
52 continue;
53 }
54
55 Opt++;
56
57 // Double dash signifies the end of option processing
58 if (*Opt == '-' && Opt[1] == 0)
59 break;
60
61 // Single dash is a short option
62 if (*Opt != '-')
63 {
64 // Iterate over each letter
65 while (*Opt != 0)
66 {
67 // Search for the option
68 Args *A;
69 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
70 if (A->end() == true)
71 return _error->Error("Command line option '%c' [from %s] is not known.",*Opt,argv[I]);
72
73 if (HandleOpt(I,argc,argv,Opt,A) == false)
74 return false;
75 if (*Opt != 0)
76 Opt++;
77 }
78 continue;
79 }
80
81 Opt++;
82
83 // Match up to a = against the list
84 const char *OptEnd = Opt;
85 Args *A;
86 for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
87 for (A = ArgList; A->end() == false &&
88 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
89
90 // Failed, look for a word after the first - (no-foo)
91 if (A->end() == true)
92 {
93 for (; Opt != OptEnd && *Opt != '-'; Opt++);
94
95 if (Opt == OptEnd)
96 return _error->Error("Command line option %s is not understood",argv[I]);
97 Opt++;
98
99 for (A = ArgList; A->end() == false &&
100 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
101
102 // Failed again..
103 if (A->end() == true && OptEnd - Opt != 1)
104 return _error->Error("Command line option %s is not understood",argv[I]);
105
106 // The option could be a single letter option prefixed by a no-..
107 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
108
109 if (A->end() == true)
110 return _error->Error("Command line option %s is not understood",argv[I]);
111
112 // The option is not boolean
113 if (A->IsBoolean() == false)
114 return _error->Error("Command line option %s is not boolean",argv[I]);
115 }
116
117 // Deal with it.
118 OptEnd--;
119 if (HandleOpt(I,argc,argv,OptEnd,A) == false)
120 return false;
121 }
122
123 // Copy any remaining file names over
124 for (; I != argc; I++)
125 *Files++ = argv[I];
126 *Files = 0;
127
128 return true;
129 }
130 /*}}}*/
131 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
132 // ---------------------------------------------------------------------
133 /* This is a helper function for parser, it looks at a given argument
134 and looks for specific patterns in the string, it gets tokanized
135 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
136 bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
137 const char *&Opt,Args *A)
138 {
139 const char *Argument = 0;
140 bool CertainArg = false;
141 int IncI = 0;
142
143 /* Determine the possible location of an option or 0 if their is
144 no option */
145 if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
146 {
147 if (I + 1 < argc && argv[I+1][0] != '-')
148 Argument = argv[I+1];
149
150 // Equals was specified but we fell off the end!
151 if (Opt[1] == '=' && Argument == 0)
152 return _error->Error("Option %s requires an argument.",argv[I]);
153 if (Opt[1] == '=')
154 CertainArg = true;
155
156 IncI = 1;
157 }
158 else
159 {
160 if (Opt[1] == '=')
161 {
162 CertainArg = true;
163 Argument = Opt + 2;
164 }
165 else
166 Argument = Opt + 1;
167 }
168
169 // Option is an argument set
170 if ((A->Flags & HasArg) == HasArg)
171 {
172 if (Argument == 0)
173 return _error->Error("Option %s requires an argument.",argv[I]);
174 Opt += strlen(Opt);
175 I += IncI;
176
177 // Parse a configuration file
178 if ((A->Flags & ConfigFile) == ConfigFile)
179 return ReadConfigFile(*Conf,Argument);
180
181 // Arbitary item specification
182 if ((A->Flags & ArbItem) == ArbItem)
183 {
184 const char *J;
185 for (J = Argument; *J != 0 && *J != '='; J++);
186 if (*J == 0)
187 return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
188
189 // = is trailing
190 if (J[1] == 0)
191 {
192 if (I+1 >= argc)
193 return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
194 Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
195 }
196 else
197 Conf->Set(string(Argument,J-Argument),string(J+1));
198
199 return true;
200 }
201
202 Conf->Set(A->ConfName,Argument);
203 return true;
204 }
205
206 // Option is an integer level
207 if ((A->Flags & IntLevel) == IntLevel)
208 {
209 // There might be an argument
210 if (Argument != 0)
211 {
212 char *EndPtr;
213 unsigned long Value = strtol(Argument,&EndPtr,10);
214
215 // Conversion failed and the argument was specified with an =s
216 if (EndPtr == Argument && CertainArg == true)
217 return _error->Error("Option %s requires an integer argument, not '%s'",argv[I],Argument);
218
219 // Conversion was ok, set the value and return
220 if (EndPtr != Argument)
221 {
222 Conf->Set(A->ConfName,Value);
223 Opt += strlen(Opt);
224 I += IncI;
225 return true;
226 }
227 }
228
229 // Increase the level
230 Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
231 return true;
232 }
233
234 // Option is a boolean
235 int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
236
237 // Look for an argument.
238 while (1)
239 {
240 // Look at preceeding text
241 char Buffer[300];
242 if (Argument == 0)
243 {
244 if (strlen(argv[I]) >= sizeof(Buffer))
245 return _error->Error("Option '%s' is too long",argv[I]);
246
247 const char *J = argv[I];
248 for (; *J != 0 && *J == '-'; J++);
249 const char *JEnd = J;
250 for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
251 if (*JEnd != 0)
252 {
253 strncpy(Buffer,J,JEnd - J);
254 Buffer[JEnd - J] = 0;
255 Argument = Buffer;
256 CertainArg = true;
257 }
258 else
259 break;
260 }
261
262 // Check for positives
263 if (strcasecmp(Argument,"yes") == 0 ||
264 strcasecmp(Argument,"true") == 0 ||
265 strcasecmp(Argument,"with") == 0 ||
266 strcasecmp(Argument,"enable") == 0)
267 {
268 Sense = 1;
269
270 // Eat the argument
271 if (Argument != Buffer)
272 {
273 Opt += strlen(Opt);
274 I += IncI;
275 }
276 break;
277 }
278
279 // Check for negatives
280 if (strcasecmp(Argument,"no") == 0 ||
281 strcasecmp(Argument,"false") == 0 ||
282 strcasecmp(Argument,"without") == 0 ||
283 strcasecmp(Argument,"disable") == 0)
284 {
285 Sense = 0;
286
287 // Eat the argument
288 if (Argument != Buffer)
289 {
290 Opt += strlen(Opt);
291 I += IncI;
292 }
293 break;
294 }
295
296 if (CertainArg == true)
297 return _error->Error("Sense %s is not understood, try true or false.",Argument);
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 /*}}}*/
315 // CommandLine::FileSize - Count the number of filenames /*{{{*/
316 // ---------------------------------------------------------------------
317 /* */
318 unsigned 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 /*}}}*/