]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/cmndline.cc
2086d91ca88b10b68f8697e3b2aa4580ee999753
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>
25 // CommandLine::CommandLine - Constructor /*{{{*/
26 // ---------------------------------------------------------------------
28 CommandLine::CommandLine(Args
*AList
,Configuration
*Conf
) : ArgList(AList
),
29 Conf(Conf
), FileList(0)
33 // CommandLine::~CommandLine - Destructor /*{{{*/
34 // ---------------------------------------------------------------------
36 CommandLine::~CommandLine()
41 // CommandLine::GetCommand - return the first non-option word /*{{{*/
42 char const * CommandLine::GetCommand(Dispatch
const * const Map
,
43 unsigned int const argc
, char const * const * const argv
)
45 // if there is a -- on the line there must be the word we search for around it
46 // as -- marks the end of the options, just not sure if the command can be
47 // considered an option or not, so accept both
48 for (size_t i
= 1; i
< argc
; ++i
)
50 if (strcmp(argv
[i
], "--") != 0)
54 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
55 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
59 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
60 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
64 // no --, so search for the first word matching a command
65 // FIXME: How like is it that an option parameter will be also a valid Match ?
66 for (size_t i
= 1; i
< argc
; ++i
)
68 if (*(argv
[i
]) == '-')
70 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
71 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
77 // CommandLine::Parse - Main action member /*{{{*/
78 // ---------------------------------------------------------------------
80 bool CommandLine::Parse(int argc
,const char **argv
)
83 FileList
= new const char *[argc
];
84 const char **Files
= FileList
;
86 for (I
= 1; I
!= argc
; I
++)
88 const char *Opt
= argv
[I
];
90 // It is not an option
99 // Double dash signifies the end of option processing
100 if (*Opt
== '-' && Opt
[1] == 0)
106 // Single dash is a short option
109 // Iterate over each letter
112 // Search for the option
114 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
115 if (A
->end() == true)
116 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
118 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
128 // Match up to a = against the list
130 const char *OptEnd
= strchrnul(Opt
, '=');
131 for (A
= ArgList
; A
->end() == false &&
132 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
135 // Failed, look for a word after the first - (no-foo)
136 bool PreceedMatch
= false;
137 if (A
->end() == true)
139 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
141 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
144 for (A
= ArgList
; A
->end() == false &&
145 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
149 if (A
->end() == true && OptEnd
- Opt
!= 1)
150 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
152 // The option could be a single letter option prefixed by a no-..
153 if (A
->end() == true)
155 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
157 if (A
->end() == true)
158 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
161 // The option is not boolean
162 if (A
->IsBoolean() == false)
163 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
169 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
173 // Copy any remaining file names over
174 for (; I
!= argc
; I
++)
178 SaveInConfig(argc
, argv
);
183 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
184 // ---------------------------------------------------------------------
185 /* This is a helper function for parser, it looks at a given argument
186 and looks for specific patterns in the string, it gets tokanized
187 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
188 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
189 const char *&Opt
,Args
*A
,bool PreceedMatch
)
191 const char *Argument
= 0;
192 bool CertainArg
= false;
195 /* Determine the possible location of an option or 0 if their is
197 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
199 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
200 Argument
= argv
[I
+1];
202 // Equals was specified but we fell off the end!
203 if (Opt
[1] == '=' && Argument
== 0)
204 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
221 // Option is an argument set
222 if ((A
->Flags
& HasArg
) == HasArg
)
225 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
229 // Parse a configuration file
230 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
231 return ReadConfigFile(*Conf
,Argument
);
233 // Arbitrary item specification
234 if ((A
->Flags
& ArbItem
) == ArbItem
)
236 const char *J
= strchr(Argument
, '=');
238 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
244 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
245 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
248 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
253 const char *I
= strchrnul(A
->ConfName
, ' ');
255 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
257 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
262 // Option is an integer level
263 if ((A
->Flags
& IntLevel
) == IntLevel
)
265 // There might be an argument
269 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
271 // Conversion failed and the argument was specified with an =s
272 if (EndPtr
== Argument
&& CertainArg
== true)
273 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
275 // Conversion was ok, set the value and return
276 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
278 Conf
->Set(A
->ConfName
,Value
);
285 // Increase the level
286 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
290 // Option is a boolean
291 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
293 // Look for an argument.
296 // Look at preceeding text
300 if (PreceedMatch
== false)
303 if (strlen(argv
[I
]) >= sizeof(Buffer
))
304 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
306 // Skip the leading dash
307 const char *J
= argv
[I
];
308 for (; *J
!= 0 && *J
== '-'; J
++);
310 const char *JEnd
= strchr(J
, '-');
313 strncpy(Buffer
,J
,JEnd
- J
);
314 Buffer
[JEnd
- J
] = 0;
323 Sense
= StringToBool(Argument
);
327 if (Argument
!= Buffer
)
335 if (CertainArg
== true)
336 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
341 // Indeterminate sense depends on the flag
344 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
350 Conf
->Set(A
->ConfName
,Sense
);
354 // CommandLine::FileSize - Count the number of filenames /*{{{*/
355 // ---------------------------------------------------------------------
357 unsigned int CommandLine::FileSize() const
359 unsigned int Count
= 0;
360 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
365 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
366 // ---------------------------------------------------------------------
368 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
371 for (I
= 0; Map
[I
].Match
!= 0; I
++)
373 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
375 bool Res
= Map
[I
].Handler(*this);
376 if (Res
== false && _error
->PendingError() == false)
377 _error
->Error("Handler silently failed");
383 if (Map
[I
].Match
== 0)
386 _error
->Error(_("Invalid operation %s"),FileList
[0]);
392 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
393 // ---------------------------------------------------------------------
394 /* We save the commandline here to have it around later for e.g. logging.
395 It feels a bit like a hack here and isn't bulletproof, but it is better
396 than nothing after all. */
397 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
399 char cmdline
[100 + argc
* 50];
400 memset(cmdline
, 0, sizeof(cmdline
));
401 unsigned int length
= 0;
402 bool lastWasOption
= false;
403 bool closeQuote
= false;
404 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
406 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
408 cmdline
[length
] = argv
[i
][j
];
409 if (lastWasOption
== true && argv
[i
][j
] == '=')
411 // That is possibly an option: Quote it if it includes spaces,
412 // the benefit is that this will eliminate also most false positives
413 const char* c
= strchr(&argv
[i
][j
+1], ' ');
414 if (c
== NULL
) continue;
415 cmdline
[++length
] = '"';
419 if (closeQuote
== true)
420 cmdline
[length
++] = '"';
421 // Problem: detects also --hello
422 if (cmdline
[length
-1] == 'o')
423 lastWasOption
= true;
424 cmdline
[length
] = ' ';
426 cmdline
[--length
] = '\0';
427 _config
->Set("CommandLine::AsString", cmdline
);
430 CommandLine::Args
CommandLine::MakeArgs(char ShortOpt
, char const *LongOpt
, char const *ConfName
, unsigned long Flags
)/*{{{*/
432 /* In theory, this should be a constructor for CommandLine::Args instead,
433 but this breaks compatibility as gcc thinks this is a c++11 initializer_list */
434 CommandLine::Args arg
;
435 arg
.ShortOpt
= ShortOpt
;
436 arg
.LongOpt
= LongOpt
;
437 arg
.ConfName
= ConfName
;