]>
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>
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::Parse - Main action member /*{{{*/
42 // ---------------------------------------------------------------------
44 bool CommandLine::Parse(int argc
,const char **argv
)
47 FileList
= new const char *[argc
];
48 const char **Files
= FileList
;
50 for (I
= 1; I
!= argc
; I
++)
52 const char *Opt
= argv
[I
];
54 // It is not an option
63 // Double dash signifies the end of option processing
64 if (*Opt
== '-' && Opt
[1] == 0)
70 // Single dash is a short option
73 // Iterate over each letter
76 // Search for the option
78 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
80 return _error
->Error(_("Command line option '%c' [from %s] is not known."),*Opt
,argv
[I
]);
82 if (HandleOpt(I
,argc
,argv
,Opt
,A
) == false)
92 // Match up to a = against the list
93 const char *OptEnd
= Opt
;
95 for (; *OptEnd
!= 0 && *OptEnd
!= '='; OptEnd
++);
96 for (A
= ArgList
; A
->end() == false &&
97 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
99 // Failed, look for a word after the first - (no-foo)
100 bool PreceedMatch
= false;
101 if (A
->end() == true)
103 for (; Opt
!= OptEnd
&& *Opt
!= '-'; Opt
++);
106 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
109 for (A
= ArgList
; A
->end() == false &&
110 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
113 if (A
->end() == true && OptEnd
- Opt
!= 1)
114 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
116 // The option could be a single letter option prefixed by a no-..
117 if (A
->end() == true)
119 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
121 if (A
->end() == true)
122 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
125 // The option is not boolean
126 if (A
->IsBoolean() == false)
127 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
133 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
137 // Copy any remaining file names over
138 for (; I
!= argc
; I
++)
142 SaveInConfig(argc
, argv
);
147 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This is a helper function for parser, it looks at a given argument
150 and looks for specific patterns in the string, it gets tokanized
151 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
152 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
153 const char *&Opt
,Args
*A
,bool PreceedMatch
)
155 const char *Argument
= 0;
156 bool CertainArg
= false;
159 /* Determine the possible location of an option or 0 if their is
161 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
163 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
164 Argument
= argv
[I
+1];
166 // Equals was specified but we fell off the end!
167 if (Opt
[1] == '=' && Argument
== 0)
168 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
185 // Option is an argument set
186 if ((A
->Flags
& HasArg
) == HasArg
)
189 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
193 // Parse a configuration file
194 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
195 return ReadConfigFile(*Conf
,Argument
);
197 // Arbitrary item specification
198 if ((A
->Flags
& ArbItem
) == ArbItem
)
201 for (J
= Argument
; *J
!= 0 && *J
!= '='; J
++);
203 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
209 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
210 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
213 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
218 const char *I
= A
->ConfName
;
219 for (; *I
!= 0 && *I
!= ' '; I
++);
221 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
223 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
228 // Option is an integer level
229 if ((A
->Flags
& IntLevel
) == IntLevel
)
231 // There might be an argument
235 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
237 // Conversion failed and the argument was specified with an =s
238 if (EndPtr
== Argument
&& CertainArg
== true)
239 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
241 // Conversion was ok, set the value and return
242 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
244 Conf
->Set(A
->ConfName
,Value
);
251 // Increase the level
252 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
256 // Option is a boolean
257 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
259 // Look for an argument.
262 // Look at preceeding text
266 if (PreceedMatch
== false)
269 if (strlen(argv
[I
]) >= sizeof(Buffer
))
270 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
272 // Skip the leading dash
273 const char *J
= argv
[I
];
274 for (; *J
!= 0 && *J
== '-'; J
++);
276 const char *JEnd
= J
;
277 for (; *JEnd
!= 0 && *JEnd
!= '-'; JEnd
++);
280 strncpy(Buffer
,J
,JEnd
- J
);
281 Buffer
[JEnd
- J
] = 0;
290 Sense
= StringToBool(Argument
);
294 if (Argument
!= Buffer
)
302 if (CertainArg
== true)
303 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
308 // Indeterminate sense depends on the flag
311 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
317 Conf
->Set(A
->ConfName
,Sense
);
321 // CommandLine::FileSize - Count the number of filenames /*{{{*/
322 // ---------------------------------------------------------------------
324 unsigned int CommandLine::FileSize() const
326 unsigned int Count
= 0;
327 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
332 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
333 // ---------------------------------------------------------------------
335 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
338 for (I
= 0; Map
[I
].Match
!= 0; I
++)
340 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
342 bool Res
= Map
[I
].Handler(*this);
343 if (Res
== false && _error
->PendingError() == false)
344 _error
->Error("Handler silently failed");
350 if (Map
[I
].Match
== 0)
353 _error
->Error(_("Invalid operation %s"),FileList
[0]);
359 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
360 // ---------------------------------------------------------------------
361 /* We save the commandline here to have it around later for e.g. logging.
362 It feels a bit like a hack here and isn't bulletproof, but it is better
363 than nothing after all. */
364 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
366 char cmdline
[100 + argc
* 50];
367 unsigned int length
= 0;
368 bool lastWasOption
= false;
369 bool closeQuote
= false;
370 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
372 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
374 cmdline
[length
] = argv
[i
][j
];
375 if (lastWasOption
== true && argv
[i
][j
] == '=')
377 // That is possibly an option: Quote it if it includes spaces,
378 // the benefit is that this will eliminate also most false positives
379 const char* c
= &argv
[i
][j
+1];
380 for (; *c
!= '\0' && *c
!= ' '; ++c
);
381 if (*c
== '\0') continue;
382 cmdline
[++length
] = '"';
386 if (closeQuote
== true)
387 cmdline
[length
++] = '"';
388 // Problem: detects also --hello
389 if (cmdline
[length
-1] == 'o')
390 lastWasOption
= true;
391 cmdline
[length
] = ' ';
393 cmdline
[--length
] = '\0';
394 _config
->Set("CommandLine::AsString", cmdline
);