]>
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 /*{{{*/
16 #include <apt-pkg/configuration.h>
17 #include <apt-pkg/cmndline.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/strutl.h>
30 // CommandLine::CommandLine - Constructor /*{{{*/
31 // ---------------------------------------------------------------------
33 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
34 Conf(Conf
), FileList(0)
38 // CommandLine::~CommandLine - Destructor /*{{{*/
39 // ---------------------------------------------------------------------
41 CommandLine::~CommandLine()
46 // CommandLine::GetCommand - return the first non-option word /*{{{*/
47 char const * CommandLine::GetCommand(Dispatch
const * const Map
,
48 unsigned int const argc
, char const * const * const argv
)
50 // if there is a -- on the line there must be the word we search for around it
51 // as -- marks the end of the options, just not sure if the command can be
52 // considered an option or not, so accept both
53 for (size_t i
= 1; i
< argc
; ++i
)
55 if (strcmp(argv
[i
], "--") != 0)
59 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
60 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
64 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
65 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
69 // no --, so search for the first word matching a command
70 // FIXME: How like is it that an option parameter will be also a valid Match ?
71 for (size_t i
= 1; i
< argc
; ++i
)
73 if (*(argv
[i
]) == '-')
75 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
76 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
82 // CommandLine::Parse - Main action member /*{{{*/
83 // ---------------------------------------------------------------------
85 bool CommandLine::Parse(int argc
,const char **argv
)
88 FileList
= new const char *[argc
];
89 const char **Files
= FileList
;
91 for (I
= 1; I
!= argc
; I
++)
93 const char *Opt
= argv
[I
];
95 // It is not an option
104 // Double dash signifies the end of option processing
105 if (*Opt
== '-' && Opt
[1] == 0)
111 // Single dash is a short option
114 // Iterate over each letter
117 // Search for the option
119 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
120 if (A
->end() == true)
121 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
123 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
133 // Match up to a = against the list
135 const char *OptEnd
= strchrnul(Opt
, '=');
136 for (A
= ArgList
; A
->end() == false &&
137 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
140 // Failed, look for a word after the first - (no-foo)
141 bool PreceedMatch
= false;
142 if (A
->end() == true)
144 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
146 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
149 for (A
= ArgList
; A
->end() == false &&
150 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
154 if (A
->end() == true && OptEnd
- Opt
!= 1)
155 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
157 // The option could be a single letter option prefixed by a no-..
158 if (A
->end() == true)
160 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
162 if (A
->end() == true)
163 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
166 // The option is not boolean
167 if (A
->IsBoolean() == false)
168 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
174 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
178 // Copy any remaining file names over
179 for (; I
!= argc
; I
++)
183 SaveInConfig(argc
, argv
);
188 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
189 // ---------------------------------------------------------------------
190 /* This is a helper function for parser, it looks at a given argument
191 and looks for specific patterns in the string, it gets tokanized
192 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
193 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
194 const char *&Opt
,Args
*A
,bool PreceedMatch
)
196 const char *Argument
= 0;
197 bool CertainArg
= false;
200 /* Determine the possible location of an option or 0 if their is
202 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
204 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
205 Argument
= argv
[I
+1];
207 // Equals was specified but we fell off the end!
208 if (Opt
[1] == '=' && Argument
== 0)
209 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
226 // Option is an argument set
227 if ((A
->Flags
& HasArg
) == HasArg
)
230 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
234 // Parse a configuration file
235 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
236 return ReadConfigFile(*Conf
,Argument
);
238 // Arbitrary item specification
239 if ((A
->Flags
& ArbItem
) == ArbItem
)
241 const char *J
= strchr(Argument
, '=');
243 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
249 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
250 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
253 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
258 const char *I
= strchrnul(A
->ConfName
, ' ');
260 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
262 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
267 // Option is an integer level
268 if ((A
->Flags
& IntLevel
) == IntLevel
)
270 // There might be an argument
274 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
276 // Conversion failed and the argument was specified with an =s
277 if (EndPtr
== Argument
&& CertainArg
== true)
278 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
280 // Conversion was ok, set the value and return
281 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
283 Conf
->Set(A
->ConfName
,Value
);
290 // Increase the level
291 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
295 // Option is a boolean
296 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
298 // Look for an argument.
301 // Look at preceding text
305 if (PreceedMatch
== false)
308 if (strlen(argv
[I
]) >= sizeof(Buffer
))
309 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
311 // Skip the leading dash
312 const char *J
= argv
[I
];
313 for (; *J
!= 0 && *J
== '-'; J
++);
315 const char *JEnd
= strchr(J
, '-');
318 strncpy(Buffer
,J
,JEnd
- J
);
319 Buffer
[JEnd
- J
] = 0;
328 Sense
= StringToBool(Argument
);
332 if (Argument
!= Buffer
)
340 if (CertainArg
== true)
341 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
346 // Indeterminate sense depends on the flag
349 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
355 Conf
->Set(A
->ConfName
,Sense
);
359 // CommandLine::FileSize - Count the number of filenames /*{{{*/
360 // ---------------------------------------------------------------------
362 unsigned int CommandLine::FileSize() const
364 unsigned int Count
= 0;
365 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
370 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
371 // ---------------------------------------------------------------------
373 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
376 for (I
= 0; Map
[I
].Match
!= 0; I
++)
378 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
380 bool Res
= Map
[I
].Handler(*this);
381 if (Res
== false && _error
->PendingError() == false)
382 _error
->Error("Handler silently failed");
388 if (Map
[I
].Match
== 0)
391 _error
->Error(_("Invalid operation %s"),FileList
[0]);
397 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
398 // ---------------------------------------------------------------------
399 /* We save the commandline here to have it around later for e.g. logging.
400 It feels a bit like a hack here and isn't bulletproof, but it is better
401 than nothing after all. */
402 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
404 char cmdline
[100 + argc
* 50];
405 memset(cmdline
, 0, sizeof(cmdline
));
406 unsigned int length
= 0;
407 bool lastWasOption
= false;
408 bool closeQuote
= false;
409 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
411 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
413 cmdline
[length
] = argv
[i
][j
];
414 if (lastWasOption
== true && argv
[i
][j
] == '=')
416 // That is possibly an option: Quote it if it includes spaces,
417 // the benefit is that this will eliminate also most false positives
418 const char* c
= strchr(&argv
[i
][j
+1], ' ');
419 if (c
== NULL
) continue;
420 cmdline
[++length
] = '"';
424 if (closeQuote
== true)
425 cmdline
[length
++] = '"';
426 // Problem: detects also --hello
427 if (cmdline
[length
-1] == 'o')
428 lastWasOption
= true;
429 cmdline
[length
] = ' ';
431 cmdline
[--length
] = '\0';
432 _config
->Set("CommandLine::AsString", cmdline
);
435 CommandLine::Args
CommandLine::MakeArgs(char ShortOpt
, char const *LongOpt
, char const *ConfName
, unsigned long Flags
)/*{{{*/
437 /* In theory, this should be a constructor for CommandLine::Args instead,
438 but this breaks compatibility as gcc thinks this is a c++11 initializer_list */
439 CommandLine::Args arg
;
440 arg
.ShortOpt
= ShortOpt
;
441 arg
.LongOpt
= LongOpt
;
442 arg
.ConfName
= ConfName
;