]>
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)
37 CommandLine::CommandLine() : ArgList(NULL
), Conf(NULL
), FileList(0)
41 // CommandLine::~CommandLine - Destructor /*{{{*/
42 // ---------------------------------------------------------------------
44 CommandLine::~CommandLine()
49 // CommandLine::GetCommand - return the first non-option word /*{{{*/
50 char const * CommandLine::GetCommand(Dispatch
const * const Map
,
51 unsigned int const argc
, char const * const * const argv
)
53 // if there is a -- on the line there must be the word we search for either
54 // before it (as -- marks the end of the options) or right after it (as we can't
55 // decide if the command is actually an option, given that in theory, you could
56 // have parameters named like commands)
57 for (size_t i
= 1; i
< argc
; ++i
)
59 if (strcmp(argv
[i
], "--") != 0)
61 // check if command is before --
62 for (size_t k
= 1; k
< i
; ++k
)
63 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
64 if (strcmp(argv
[k
], Map
[j
].Match
) == 0)
66 // see if the next token after -- is the command
69 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
70 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
72 // we found a --, but not a command
75 // no --, so search for the first word matching a command
76 // FIXME: How like is it that an option parameter will be also a valid Match ?
77 for (size_t i
= 1; i
< argc
; ++i
)
79 if (*(argv
[i
]) == '-')
81 for (size_t j
= 0; Map
[j
].Match
!= NULL
; ++j
)
82 if (strcmp(argv
[i
], Map
[j
].Match
) == 0)
88 // CommandLine::Parse - Main action member /*{{{*/
89 // ---------------------------------------------------------------------
91 bool CommandLine::Parse(int argc
,const char **argv
)
94 FileList
= new const char *[argc
];
95 const char **Files
= FileList
;
97 for (I
= 1; I
!= argc
; I
++)
99 const char *Opt
= argv
[I
];
101 // It is not an option
110 // Double dash signifies the end of option processing
111 if (*Opt
== '-' && Opt
[1] == 0)
117 // Single dash is a short option
120 // Iterate over each letter
123 // Search for the option
125 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
126 if (A
->end() == true)
127 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
129 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
139 // Match up to a = against the list
141 const char *OptEnd
= strchrnul(Opt
, '=');
142 for (A
= ArgList
; A
->end() == false &&
143 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
146 // Failed, look for a word after the first - (no-foo)
147 bool PreceedMatch
= false;
148 if (A
->end() == true)
150 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
152 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
155 for (A
= ArgList
; A
->end() == false &&
156 (A
->LongOpt
== 0 || stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0);
160 if (A
->end() == true && OptEnd
- Opt
!= 1)
161 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
163 // The option could be a single letter option prefixed by a no-..
164 if (A
->end() == true)
166 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
168 if (A
->end() == true)
169 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
172 // The option is not boolean
173 if (A
->IsBoolean() == false)
174 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
180 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
184 // Copy any remaining file names over
185 for (; I
!= argc
; I
++)
189 SaveInConfig(argc
, argv
);
194 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
195 // ---------------------------------------------------------------------
196 /* This is a helper function for parser, it looks at a given argument
197 and looks for specific patterns in the string, it gets tokanized
198 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
199 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
200 const char *&Opt
,Args
*A
,bool PreceedMatch
)
202 const char *Argument
= 0;
203 bool CertainArg
= false;
206 /* Determine the possible location of an option or 0 if their is
208 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
210 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
211 Argument
= argv
[I
+1];
213 // Equals was specified but we fell off the end!
214 if (Opt
[1] == '=' && Argument
== 0)
215 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
232 // Option is an argument set
233 if ((A
->Flags
& HasArg
) == HasArg
)
236 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
240 // Parse a configuration file
241 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
242 return ReadConfigFile(*Conf
,Argument
);
244 // Arbitrary item specification
245 if ((A
->Flags
& ArbItem
) == ArbItem
)
247 const char *J
= strchr(Argument
, '=');
249 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
255 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
256 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
259 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
264 const char *I
= strchrnul(A
->ConfName
, ' ');
266 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
268 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
273 // Option is an integer level
274 if ((A
->Flags
& IntLevel
) == IntLevel
)
276 // There might be an argument
280 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
282 // Conversion failed and the argument was specified with an =s
283 if (EndPtr
== Argument
&& CertainArg
== true)
284 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
286 // Conversion was ok, set the value and return
287 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
289 Conf
->Set(A
->ConfName
,Value
);
296 // Increase the level
297 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
301 // Option is a boolean
302 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
304 // Look for an argument.
307 // Look at preceding text
311 if (PreceedMatch
== false)
314 if (strlen(argv
[I
]) >= sizeof(Buffer
))
315 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
317 // Skip the leading dash
318 const char *J
= argv
[I
];
319 for (; *J
!= 0 && *J
== '-'; J
++);
321 const char *JEnd
= strchr(J
, '-');
324 strncpy(Buffer
,J
,JEnd
- J
);
325 Buffer
[JEnd
- J
] = 0;
334 Sense
= StringToBool(Argument
);
338 if (Argument
!= Buffer
)
346 if (CertainArg
== true)
347 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
352 // Indeterminate sense depends on the flag
355 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
361 Conf
->Set(A
->ConfName
,Sense
);
365 // CommandLine::FileSize - Count the number of filenames /*{{{*/
366 // ---------------------------------------------------------------------
368 unsigned int CommandLine::FileSize() const
370 unsigned int Count
= 0;
371 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
376 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
377 // ---------------------------------------------------------------------
379 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
382 for (I
= 0; Map
[I
].Match
!= 0; I
++)
384 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
386 bool Res
= Map
[I
].Handler(*this);
387 if (Res
== false && _error
->PendingError() == false)
388 _error
->Error("Handler silently failed");
394 if (Map
[I
].Match
== 0)
397 _error
->Error(_("Invalid operation %s"),FileList
[0]);
403 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
404 // ---------------------------------------------------------------------
405 /* We save the commandline here to have it around later for e.g. logging.
406 It feels a bit like a hack here and isn't bulletproof, but it is better
407 than nothing after all. */
408 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
410 char cmdline
[100 + argc
* 50];
411 memset(cmdline
, 0, sizeof(cmdline
));
412 unsigned int length
= 0;
413 bool lastWasOption
= false;
414 bool closeQuote
= false;
415 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
417 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
419 cmdline
[length
] = argv
[i
][j
];
420 if (lastWasOption
== true && argv
[i
][j
] == '=')
422 // That is possibly an option: Quote it if it includes spaces,
423 // the benefit is that this will eliminate also most false positives
424 const char* c
= strchr(&argv
[i
][j
+1], ' ');
425 if (c
== NULL
) continue;
426 cmdline
[++length
] = '"';
430 if (closeQuote
== true)
431 cmdline
[length
++] = '"';
432 // Problem: detects also --hello
433 if (cmdline
[length
-1] == 'o')
434 lastWasOption
= true;
435 cmdline
[length
] = ' ';
437 cmdline
[--length
] = '\0';
438 _config
->Set("CommandLine::AsString", cmdline
);
441 CommandLine::Args
CommandLine::MakeArgs(char ShortOpt
, char const *LongOpt
, char const *ConfName
, unsigned long Flags
)/*{{{*/
443 /* In theory, this should be a constructor for CommandLine::Args instead,
444 but this breaks compatibility as gcc thinks this is a c++11 initializer_list */
445 CommandLine::Args arg
;
446 arg
.ShortOpt
= ShortOpt
;
447 arg
.LongOpt
= LongOpt
;
448 arg
.ConfName
= ConfName
;