]>
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
94 const char *OptEnd
= strchrnul(Opt
, '=');
95 for (A
= ArgList
; A
->end() == false &&
96 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
98 // Failed, look for a word after the first - (no-foo)
99 bool PreceedMatch
= false;
100 if (A
->end() == true)
102 Opt
= (const char*) memchr(Opt
, '-', OptEnd
- Opt
);
104 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
107 for (A
= ArgList
; A
->end() == false &&
108 stringcasecmp(Opt
,OptEnd
,A
->LongOpt
) != 0; A
++);
111 if (A
->end() == true && OptEnd
- Opt
!= 1)
112 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
114 // The option could be a single letter option prefixed by a no-..
115 if (A
->end() == true)
117 for (A
= ArgList
; A
->end() == false && A
->ShortOpt
!= *Opt
; A
++);
119 if (A
->end() == true)
120 return _error
->Error(_("Command line option %s is not understood"),argv
[I
]);
123 // The option is not boolean
124 if (A
->IsBoolean() == false)
125 return _error
->Error(_("Command line option %s is not boolean"),argv
[I
]);
131 if (HandleOpt(I
,argc
,argv
,OptEnd
,A
,PreceedMatch
) == false)
135 // Copy any remaining file names over
136 for (; I
!= argc
; I
++)
140 SaveInConfig(argc
, argv
);
145 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
146 // ---------------------------------------------------------------------
147 /* This is a helper function for parser, it looks at a given argument
148 and looks for specific patterns in the string, it gets tokanized
149 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
150 bool CommandLine::HandleOpt(int &I
,int argc
,const char *argv
[],
151 const char *&Opt
,Args
*A
,bool PreceedMatch
)
153 const char *Argument
= 0;
154 bool CertainArg
= false;
157 /* Determine the possible location of an option or 0 if their is
159 if (Opt
[1] == 0 || (Opt
[1] == '=' && Opt
[2] == 0))
161 if (I
+ 1 < argc
&& argv
[I
+1][0] != '-')
162 Argument
= argv
[I
+1];
164 // Equals was specified but we fell off the end!
165 if (Opt
[1] == '=' && Argument
== 0)
166 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
183 // Option is an argument set
184 if ((A
->Flags
& HasArg
) == HasArg
)
187 return _error
->Error(_("Option %s requires an argument."),argv
[I
]);
191 // Parse a configuration file
192 if ((A
->Flags
& ConfigFile
) == ConfigFile
)
193 return ReadConfigFile(*Conf
,Argument
);
195 // Arbitrary item specification
196 if ((A
->Flags
& ArbItem
) == ArbItem
)
198 const char *J
= strchr(Argument
, '=');
200 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
206 return _error
->Error(_("Option %s: Configuration item specification must have an =<val>."),argv
[I
]);
207 Conf
->Set(string(Argument
,J
-Argument
),string(argv
[I
++ +1]));
210 Conf
->Set(string(Argument
,J
-Argument
),string(J
+1));
215 const char *I
= strchrnul(A
->ConfName
, ' ');
217 Conf
->Set(string(A
->ConfName
,0,I
-A
->ConfName
),string(I
+1) + Argument
);
219 Conf
->Set(A
->ConfName
,string(I
) + Argument
);
224 // Option is an integer level
225 if ((A
->Flags
& IntLevel
) == IntLevel
)
227 // There might be an argument
231 unsigned long Value
= strtol(Argument
,&EndPtr
,10);
233 // Conversion failed and the argument was specified with an =s
234 if (EndPtr
== Argument
&& CertainArg
== true)
235 return _error
->Error(_("Option %s requires an integer argument, not '%s'"),argv
[I
],Argument
);
237 // Conversion was ok, set the value and return
238 if (EndPtr
!= 0 && EndPtr
!= Argument
&& *EndPtr
== 0)
240 Conf
->Set(A
->ConfName
,Value
);
247 // Increase the level
248 Conf
->Set(A
->ConfName
,Conf
->FindI(A
->ConfName
)+1);
252 // Option is a boolean
253 int Sense
= -1; // -1 is unspecified, 0 is yes 1 is no
255 // Look for an argument.
258 // Look at preceeding text
262 if (PreceedMatch
== false)
265 if (strlen(argv
[I
]) >= sizeof(Buffer
))
266 return _error
->Error(_("Option '%s' is too long"),argv
[I
]);
268 // Skip the leading dash
269 const char *J
= argv
[I
];
270 for (; *J
!= 0 && *J
== '-'; J
++);
272 const char *JEnd
= strchr(J
, '-');
275 strncpy(Buffer
,J
,JEnd
- J
);
276 Buffer
[JEnd
- J
] = 0;
285 Sense
= StringToBool(Argument
);
289 if (Argument
!= Buffer
)
297 if (CertainArg
== true)
298 return _error
->Error(_("Sense %s is not understood, try true or false."),Argument
);
303 // Indeterminate sense depends on the flag
306 if ((A
->Flags
& InvBoolean
) == InvBoolean
)
312 Conf
->Set(A
->ConfName
,Sense
);
316 // CommandLine::FileSize - Count the number of filenames /*{{{*/
317 // ---------------------------------------------------------------------
319 unsigned int CommandLine::FileSize() const
321 unsigned int Count
= 0;
322 for (const char **I
= FileList
; I
!= 0 && *I
!= 0; I
++)
327 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
328 // ---------------------------------------------------------------------
330 bool CommandLine::DispatchArg(Dispatch
*Map
,bool NoMatch
)
333 for (I
= 0; Map
[I
].Match
!= 0; I
++)
335 if (strcmp(FileList
[0],Map
[I
].Match
) == 0)
337 bool Res
= Map
[I
].Handler(*this);
338 if (Res
== false && _error
->PendingError() == false)
339 _error
->Error("Handler silently failed");
345 if (Map
[I
].Match
== 0)
348 _error
->Error(_("Invalid operation %s"),FileList
[0]);
354 // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
355 // ---------------------------------------------------------------------
356 /* We save the commandline here to have it around later for e.g. logging.
357 It feels a bit like a hack here and isn't bulletproof, but it is better
358 than nothing after all. */
359 void CommandLine::SaveInConfig(unsigned int const &argc
, char const * const * const argv
)
361 char cmdline
[100 + argc
* 50];
362 unsigned int length
= 0;
363 bool lastWasOption
= false;
364 bool closeQuote
= false;
365 for (unsigned int i
= 0; i
< argc
&& length
< sizeof(cmdline
); ++i
, ++length
)
367 for (unsigned int j
= 0; argv
[i
][j
] != '\0' && length
< sizeof(cmdline
)-1; ++j
, ++length
)
369 cmdline
[length
] = argv
[i
][j
];
370 if (lastWasOption
== true && argv
[i
][j
] == '=')
372 // That is possibly an option: Quote it if it includes spaces,
373 // the benefit is that this will eliminate also most false positives
374 const char* c
= strchr(&argv
[i
][j
+1], ' ');
375 if (c
== NULL
) continue;
376 cmdline
[++length
] = '"';
380 if (closeQuote
== true)
381 cmdline
[length
++] = '"';
382 // Problem: detects also --hello
383 if (cmdline
[length
-1] == 'o')
384 lastWasOption
= true;
385 cmdline
[length
] = ' ';
387 cmdline
[--length
] = '\0';
388 _config
->Set("CommandLine::AsString", cmdline
);