1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cmndline.cc,v 1.6 1999/11/17 05:59:29 jgg Exp $
4 /* ######################################################################
6 Command Line Class - Sophisticated command line parser
8 ##################################################################### */
10 // Include files /*{{{*/
12 #pragma implementation "dsync/cmndline.h"
14 #include <dsync/cmndline.h>
15 #include <dsync/error.h>
16 #include <dsync/strutl.h>
19 // CommandLine::CommandLine - Constructor /*{{{*/
20 // ---------------------------------------------------------------------
22 CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
23 Conf(Conf), FileList(0)
27 // CommandLine::~CommandLine - Destructor /*{{{*/
28 // ---------------------------------------------------------------------
30 CommandLine::~CommandLine()
35 // CommandLine::Parse - Main action member /*{{{*/
36 // ---------------------------------------------------------------------
38 bool CommandLine::Parse(int argc,const char **argv)
41 FileList = new const char *[argc];
42 const char **Files = FileList;
44 for (I = 1; I != argc; I++)
46 const char *Opt = argv[I];
48 // It is not an option
57 // Double dash signifies the end of option processing
58 if (*Opt == '-' && Opt[1] == 0)
61 // Single dash is a short option
64 // Iterate over each letter
67 // Search for the option
69 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
71 return _error->Error("Command line option '%c' [from %s] is not known.",*Opt,argv[I]);
73 if (HandleOpt(I,argc,argv,Opt,A) == false)
83 // Match up to a = against the list
84 const char *OptEnd = Opt;
86 for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
87 for (A = ArgList; A->end() == false &&
88 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
90 // Failed, look for a word after the first - (no-foo)
91 bool PreceedMatch = false;
94 for (; Opt != OptEnd && *Opt != '-'; Opt++);
97 return _error->Error("Command line option %s is not understood",argv[I]);
100 for (A = ArgList; A->end() == false &&
101 stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
104 if (A->end() == true && OptEnd - Opt != 1)
105 return _error->Error("Command line option %s is not understood",argv[I]);
107 // The option could be a single letter option prefixed by a no-..
108 if (A->end() == true)
110 for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
112 if (A->end() == true)
113 return _error->Error("Command line option %s is not understood",argv[I]);
116 // The option is not boolean
117 if (A->IsBoolean() == false)
118 return _error->Error("Command line option %s is not boolean",argv[I]);
124 if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
128 // Copy any remaining file names over
129 for (; I != argc; I++)
136 // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
137 // ---------------------------------------------------------------------
138 /* This is a helper function for parser, it looks at a given argument
139 and looks for specific patterns in the string, it gets tokanized
140 -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
141 bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
142 const char *&Opt,Args *A,bool PreceedMatch)
144 const char *Argument = 0;
145 bool CertainArg = false;
148 /* Determine the possible location of an option or 0 if their is
150 if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
152 if (I + 1 < argc && argv[I+1][0] != '-')
153 Argument = argv[I+1];
155 // Equals was specified but we fell off the end!
156 if (Opt[1] == '=' && Argument == 0)
157 return _error->Error("Option %s requires an argument.",argv[I]);
174 // Option is an argument set
175 if ((A->Flags & HasArg) == HasArg)
178 return _error->Error("Option %s requires an argument.",argv[I]);
182 // Parse a configuration file
183 if ((A->Flags & ConfigFile) == ConfigFile)
184 return ReadConfigFile(*Conf,Argument);
186 // Arbitary item specification
187 if ((A->Flags & ArbItem) == ArbItem)
190 for (J = Argument; *J != 0 && *J != '='; J++);
192 return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
198 return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
199 Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
202 Conf->Set(string(Argument,J-Argument),string(J+1));
207 const char *I = A->ConfName;
208 for (; *I != 0 && *I != ' '; I++);
210 Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument);
212 Conf->Set(A->ConfName,string(I) + Argument);
217 // Option is an integer level
218 if ((A->Flags & IntLevel) == IntLevel)
220 // There might be an argument
224 unsigned long Value = strtol(Argument,&EndPtr,10);
226 // Conversion failed and the argument was specified with an =s
227 if (EndPtr == Argument && CertainArg == true)
228 return _error->Error("Option %s requires an integer argument, not '%s'",argv[I],Argument);
230 // Conversion was ok, set the value and return
231 if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0)
233 Conf->Set(A->ConfName,Value);
240 // Increase the level
241 Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
245 // Option is a boolean
246 int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
248 // Look for an argument.
251 // Look at preceeding text
255 if (PreceedMatch == false)
258 if (strlen(argv[I]) >= sizeof(Buffer))
259 return _error->Error("Option '%s' is too long",argv[I]);
261 // Skip the leading dash
262 const char *J = argv[I];
263 for (; *J != 0 && *J == '-'; J++);
265 const char *JEnd = J;
266 for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
269 strncpy(Buffer,J,JEnd - J);
270 Buffer[JEnd - J] = 0;
279 Sense = StringToBool(Argument);
283 if (Argument != Buffer)
291 if (CertainArg == true)
292 return _error->Error("Sense %s is not understood, try true or false.",Argument);
297 // Indeterminate sense depends on the flag
300 if ((A->Flags & InvBoolean) == InvBoolean)
306 Conf->Set(A->ConfName,Sense);
310 // CommandLine::FileSize - Count the number of filenames /*{{{*/
311 // ---------------------------------------------------------------------
313 unsigned int CommandLine::FileSize() const
315 unsigned int Count = 0;
316 for (const char **I = FileList; I != 0 && *I != 0; I++)
321 // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
322 // ---------------------------------------------------------------------
324 bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
327 for (I = 0; Map[I].Match != 0; I++)
329 if (strcmp(FileList[0],Map[I].Match) == 0)
331 bool Res = Map[I].Handler(*this);
332 if (Res == false && _error->PendingError() == false)
333 _error->Error("Handler silently failed");
339 if (Map[I].Match == 0)
342 _error->Error("Invalid operation %s",FileList[0]);