]>
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 either
51 // before it (as -- marks the end of the options) or right after it (as we can't
52 // decide if the command is actually an option, given that in theory, you could
53 // have parameters named like commands)
54 for (size_t i
= 1; i
< argc
; ++i
)
56 if (strcmp(argv
[i
], "--") != 0)
58 // check if command is before --
59 for (size_t k
= 1; k
< i
; ++k
)
60 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
61 if (strcmp(argv
[k
], Map
[j
].Match
) == 0)
63 // see if the next token after -- is the command
66 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
67 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
69 // we found a --, but not a command
72 // no --, so search for the first word matching a command
73 // FIXME: How like is it that an option parameter will be also a valid Match ?
74 for (size_t i
= 1; i
< argc
; ++i
)
76 if (*(argv
[i
]) == '-')
78 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
79 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
85 // CommandLine::Parse - Main action member /*{{{*/
86 // ---------------------------------------------------------------------
88 bool CommandLine::Parse(int argc
,const char **argv
)
91 FileList
= new const char *[argc
];
92 const char **Files
= FileList
;
94 for (I
= 1; I
!= argc
; I
++)
96 const char *Opt
= argv
[I
];
98 // It is not an option
107 // Double dash signifies the end of option processing
108 if (*Opt
== '-' && Opt
[1] == 0)
114 // Single dash is a short option
117 // Iterate over each letter
120 // Search for the option
122 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
123 if (A
->end() == true)
124 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
126 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
136 // Match up to a = against the list
138 const char *OptEnd
= strchrnul(Opt
, '=');
139 for (A
= ArgList
; A
->end() == false &&
140 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
143 // Failed, look for a word after the first - (no-foo)
144 bool PreceedMatch
= false;
145 if (A
->end() == true)
147 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
149 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
152 for (A
= ArgList
; A
->end() == false &&
153 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
157 if (A
->end() == true && OptEnd
- Opt
!= 1)
158 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
160 // The option could be a single letter option prefixed by a no-..
161 if (A
->end() == true)
163 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
165 if (A
->end() == true)
166 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
169 // The option is not boolean
170 if (A
->IsBoolean() == false)
171 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
177 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
181 // Copy any remaining file names over
182 for (; I
!= argc
; I
++)
186 SaveInConfig(argc
, argv
);
191 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
192 // ---------------------------------------------------------------------
193 /* This is a helper function for parser, it looks at a given argument
194 and looks for specific patterns in the string, it gets tokanized
195 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
196 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
197 const char *&Opt
,Args
*A
,bool PreceedMatch
)
199 const char *Argument
= 0;
200 bool CertainArg
= false;
203 /* Determine the possible location of an option or 0 if their is
205 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
207 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
208 Argument
= argv
[I
+1];
210 // Equals was specified but we fell off the end!
211 if (Opt
[1] == '=' && Argument
== 0)
212 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
229 // Option is an argument set
230 if ((A
->Flags
& HasArg
) == HasArg
)
233 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
237 // Parse a configuration file
238 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
239 return ReadConfigFile(*Conf
,Argument
);
241 // Arbitrary item specification
242 if ((A
->Flags
& ArbItem
) == ArbItem
)
244 const char *J
= strchr(Argument
, '=');
246 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
252 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
253 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
256 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
261 const char *I
= strchrnul(A
->ConfName
, ' ');
263 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
265 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
270 // Option is an integer level
271 if ((A
->Flags
& IntLevel
) == IntLevel
)
273 // There might be an argument
277 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
279 // Conversion failed and the argument was specified with an =s
280 if (EndPtr
== Argument
&& CertainArg
== true)
281 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
283 // Conversion was ok, set the value and return
284 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
286 Conf
->Set(A
->ConfName
,Value
);
293 // Increase the level
294 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
298 // Option is a boolean
299 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
301 // Look for an argument.
304 // Look at preceding text
308 if (PreceedMatch
== false)
311 if (strlen(argv
[I
]) >= sizeof(Buffer
))
312 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
314 // Skip the leading dash
315 const char *J
= argv
[I
];
316 for (; *J
!= 0 && *J
== '-'; J
++);
318 const char *JEnd
= strchr(J
, '-');
321 strncpy(Buffer
,J
,JEnd
- J
);
322 Buffer
[JEnd
- J
] = 0;
331 Sense
= StringToBool(Argument
);
335 if (Argument
!= Buffer
)
343 if (CertainArg
== true)
344 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
349 // Indeterminate sense depends on the flag
352 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
358 Conf
->Set(A
->ConfName
,Sense
);
362 // CommandLine::FileSize - Count the number of filenames /*{{{*/
363 // ---------------------------------------------------------------------
365 unsigned int CommandLine::FileSize() const
367 unsigned int Count
= 0;
368 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
373 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
374 // ---------------------------------------------------------------------
376 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
379 for (I
= 0; Map
[I
].Match
!= 0; I
++)
381 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
383 bool Res
= Map
[I
].Handler(*this);
384 if (Res
== false && _error
->PendingError() == false)
385 _error
->Error("Handler silently failed");
391 if (Map
[I
].Match
== 0)
394 _error
->Error(_("Invalid operation %s"),FileList
[0]);
400 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
401 // ---------------------------------------------------------------------
402 /* We save the commandline here to have it around later for e.g. logging.
403 It feels a bit like a hack here and isn't bulletproof, but it is better
404 than nothing after all. */
405 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
407 char cmdline
[100 + argc
* 50];
408 memset(cmdline
, 0, sizeof(cmdline
));
409 unsigned int length
= 0;
410 bool lastWasOption
= false;
411 bool closeQuote
= false;
412 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
414 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
416 cmdline
[length
] = argv
[i
][j
];
417 if (lastWasOption
== true && argv
[i
][j
] == '=')
419 // That is possibly an option: Quote it if it includes spaces,
420 // the benefit is that this will eliminate also most false positives
421 const char* c
= strchr(&argv
[i
][j
+1], ' ');
422 if (c
== NULL
) continue;
423 cmdline
[++length
] = '"';
427 if (closeQuote
== true)
428 cmdline
[length
++] = '"';
429 // Problem: detects also --hello
430 if (cmdline
[length
-1] == 'o')
431 lastWasOption
= true;
432 cmdline
[length
] = ' ';
434 cmdline
[--length
] = '\0';
435 _config
->Set("CommandLine::AsString", cmdline
);
438 CommandLine::Args
CommandLine::MakeArgs(char ShortOpt
, char const *LongOpt
, char const *ConfName
, unsigned long Flags
)/*{{{*/
440 /* In theory, this should be a constructor for CommandLine::Args instead,
441 but this breaks compatibility as gcc thinks this is a c++11 initializer_list */
442 CommandLine::Args arg
;
443 arg
.ShortOpt
= ShortOpt
;
444 arg
.LongOpt
= LongOpt
;
445 arg
.ConfName
= ConfName
;