]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.15 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
6 Command Line Class - Sophisticated command line parser
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>.
11 ##################################################################### */
13 // Include files /*{{{*/
14 #include <apt-pkg/cmndline.h>
15 #include <apt-pkg/error.h>
16 #include <apt-pkg/strutl.h>
22 // CommandLine::CommandLine - Constructor /*{{{*/
23 // ---------------------------------------------------------------------
25 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
26 Conf(Conf
), FileList(0)
30 // CommandLine::~CommandLine - Destructor /*{{{*/
31 // ---------------------------------------------------------------------
33 CommandLine::~CommandLine()
38 // CommandLine::Parse - Main action member /*{{{*/
39 // ---------------------------------------------------------------------
41 bool CommandLine::Parse(int argc
,const char **argv
)
44 FileList
= new const char *[argc
];
45 const char **Files
= FileList
;
47 for (I
= 1; I
!= argc
; I
++)
49 const char *Opt
= argv
[I
];
51 // It is not an option
60 // Double dash signifies the end of option processing
61 if (*Opt
== '-' && Opt
[1] == 0)
67 // Single dash is a short option
70 // Iterate over each letter
73 // Search for the option
75 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
77 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
79 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
89 // Match up to a = against the list
91 const char *OptEnd
= strchrnul(Opt
, '=');
92 for (A
= ArgList
; A
->end() == false &&
93 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
95 // Failed, look for a word after the first - (no-foo)
96 bool PreceedMatch
= false;
99 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
101 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
104 for (A
= ArgList
; A
->end() == false &&
105 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
108 if (A
->end() == true && OptEnd
- Opt
!= 1)
109 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
111 // The option could be a single letter option prefixed by a no-..
112 if (A
->end() == true)
114 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
116 if (A
->end() == true)
117 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
120 // The option is not boolean
121 if (A
->IsBoolean() == false)
122 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
128 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
132 // Copy any remaining file names over
133 for (; I
!= argc
; I
++)
137 SaveInConfig(argc
, argv
);
142 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
143 // ---------------------------------------------------------------------
144 /* This is a helper function for parser, it looks at a given argument
145 and looks for specific patterns in the string, it gets tokanized
146 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
147 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
148 const char *&Opt
,Args
*A
,bool PreceedMatch
)
150 const char *Argument
= 0;
151 bool CertainArg
= false;
154 /* Determine the possible location of an option or 0 if their is
156 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
158 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
159 Argument
= argv
[I
+1];
161 // Equals was specified but we fell off the end!
162 if (Opt
[1] == '=' && Argument
== 0)
163 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
180 // Option is an argument set
181 if ((A
->Flags
& HasArg
) == HasArg
)
184 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
188 // Parse a configuration file
189 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
190 return ReadConfigFile(*Conf
,Argument
);
192 // Arbitrary item specification
193 if ((A
->Flags
& ArbItem
) == ArbItem
)
195 const char *J
= strchr(Argument
, '=');
197 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
203 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
204 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
207 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
212 const char *I
= strchrnul(A
->ConfName
, ' ');
214 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
216 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
221 // Option is an integer level
222 if ((A
->Flags
& IntLevel
) == IntLevel
)
224 // There might be an argument
228 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
230 // Conversion failed and the argument was specified with an =s
231 if (EndPtr
== Argument
&& CertainArg
== true)
232 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
234 // Conversion was ok, set the value and return
235 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
237 Conf
->Set(A
->ConfName
,Value
);
244 // Increase the level
245 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
249 // Option is a boolean
250 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
252 // Look for an argument.
255 // Look at preceeding text
259 if (PreceedMatch
== false)
262 if (strlen(argv
[I
]) >= sizeof(Buffer
))
263 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
265 // Skip the leading dash
266 const char *J
= argv
[I
];
267 for (; *J
!= 0 && *J
== '-'; J
++);
269 const char *JEnd
= strchr(J
, '-');
272 strncpy(Buffer
,J
,JEnd
- J
);
273 Buffer
[JEnd
- J
] = 0;
282 Sense
= StringToBool(Argument
);
286 if (Argument
!= Buffer
)
294 if (CertainArg
== true)
295 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
300 // Indeterminate sense depends on the flag
303 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
309 Conf
->Set(A
->ConfName
,Sense
);
313 // CommandLine::FileSize - Count the number of filenames /*{{{*/
314 // ---------------------------------------------------------------------
316 unsigned int CommandLine::FileSize() const
318 unsigned int Count
= 0;
319 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
324 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
325 // ---------------------------------------------------------------------
327 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
330 for (I
= 0; Map
[I
].Match
!= 0; I
++)
332 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
334 bool Res
= Map
[I
].Handler(*this);
335 if (Res
== false && _error
->PendingError() == false)
336 _error
->Error("Handler silently failed");
342 if (Map
[I
].Match
== 0)
345 _error
->Error(_("Invalid operation %s"),FileList
[0]);
351 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
352 // ---------------------------------------------------------------------
353 /* We save the commandline here to have it around later for e.g. logging.
354 It feels a bit like a hack here and isn't bulletproof, but it is better
355 than nothing after all. */
356 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
358 char cmdline
[100 + argc
* 50];
359 unsigned int length
= 0;
360 bool lastWasOption
= false;
361 bool closeQuote
= false;
362 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
364 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
366 cmdline
[length
] = argv
[i
][j
];
367 if (lastWasOption
== true && argv
[i
][j
] == '=')
369 // That is possibly an option: Quote it if it includes spaces,
370 // the benefit is that this will eliminate also most false positives
371 const char* c
= strchr(&argv
[i
][j
+1], ' ');
372 if (c
== NULL
) continue;
373 cmdline
[++length
] = '"';
377 if (closeQuote
== true)
378 cmdline
[length
++] = '"';
379 // Problem: detects also --hello
380 if (cmdline
[length
-1] == 'o')
381 lastWasOption
= true;
382 cmdline
[length
] = ' ';
384 cmdline
[--length
] = '\0';
385 _config
->Set("CommandLine::AsString", cmdline
);