cmndline.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cmndline.cc,v 1.6 1999/11/17 05:59:29 jgg Exp $
  4. /* ######################################################################
  5. Command Line Class - Sophisticated command line parser
  6. ##################################################################### */
  7. /*}}}*/
  8. // Include files /*{{{*/
  9. #ifdef __GNUG__
  10. #pragma implementation "dsync/cmndline.h"
  11. #endif
  12. #include <dsync/cmndline.h>
  13. #include <dsync/error.h>
  14. #include <dsync/strutl.h>
  15. /*}}}*/
  16. // CommandLine::CommandLine - Constructor /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
  20. Conf(Conf), FileList(0)
  21. {
  22. }
  23. /*}}}*/
  24. // CommandLine::~CommandLine - Destructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. CommandLine::~CommandLine()
  28. {
  29. delete [] FileList;
  30. }
  31. /*}}}*/
  32. // CommandLine::Parse - Main action member /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. bool CommandLine::Parse(int argc,const char **argv)
  36. {
  37. delete [] FileList;
  38. FileList = new const char *[argc];
  39. const char **Files = FileList;
  40. int I;
  41. for (I = 1; I != argc; I++)
  42. {
  43. const char *Opt = argv[I];
  44. // It is not an option
  45. if (*Opt != '-')
  46. {
  47. *Files++ = Opt;
  48. continue;
  49. }
  50. Opt++;
  51. // Double dash signifies the end of option processing
  52. if (*Opt == '-' && Opt[1] == 0)
  53. break;
  54. // Single dash is a short option
  55. if (*Opt != '-')
  56. {
  57. // Iterate over each letter
  58. while (*Opt != 0)
  59. {
  60. // Search for the option
  61. Args *A;
  62. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  63. if (A->end() == true)
  64. return _error->Error("Command line option '%c' [from %s] is not known.",*Opt,argv[I]);
  65. if (HandleOpt(I,argc,argv,Opt,A) == false)
  66. return false;
  67. if (*Opt != 0)
  68. Opt++;
  69. }
  70. continue;
  71. }
  72. Opt++;
  73. // Match up to a = against the list
  74. const char *OptEnd = Opt;
  75. Args *A;
  76. for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
  77. for (A = ArgList; A->end() == false &&
  78. stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
  79. // Failed, look for a word after the first - (no-foo)
  80. bool PreceedMatch = false;
  81. if (A->end() == true)
  82. {
  83. for (; Opt != OptEnd && *Opt != '-'; Opt++);
  84. if (Opt == OptEnd)
  85. return _error->Error("Command line option %s is not understood",argv[I]);
  86. Opt++;
  87. for (A = ArgList; A->end() == false &&
  88. stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
  89. // Failed again..
  90. if (A->end() == true && OptEnd - Opt != 1)
  91. return _error->Error("Command line option %s is not understood",argv[I]);
  92. // The option could be a single letter option prefixed by a no-..
  93. if (A->end() == true)
  94. {
  95. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  96. if (A->end() == true)
  97. return _error->Error("Command line option %s is not understood",argv[I]);
  98. }
  99. // The option is not boolean
  100. if (A->IsBoolean() == false)
  101. return _error->Error("Command line option %s is not boolean",argv[I]);
  102. PreceedMatch = true;
  103. }
  104. // Deal with it.
  105. OptEnd--;
  106. if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
  107. return false;
  108. }
  109. // Copy any remaining file names over
  110. for (; I != argc; I++)
  111. *Files++ = argv[I];
  112. *Files = 0;
  113. return true;
  114. }
  115. /*}}}*/
  116. // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
  117. // ---------------------------------------------------------------------
  118. /* This is a helper function for parser, it looks at a given argument
  119. and looks for specific patterns in the string, it gets tokanized
  120. -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
  121. bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
  122. const char *&Opt,Args *A,bool PreceedMatch)
  123. {
  124. const char *Argument = 0;
  125. bool CertainArg = false;
  126. int IncI = 0;
  127. /* Determine the possible location of an option or 0 if their is
  128. no option */
  129. if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
  130. {
  131. if (I + 1 < argc && argv[I+1][0] != '-')
  132. Argument = argv[I+1];
  133. // Equals was specified but we fell off the end!
  134. if (Opt[1] == '=' && Argument == 0)
  135. return _error->Error("Option %s requires an argument.",argv[I]);
  136. if (Opt[1] == '=')
  137. CertainArg = true;
  138. IncI = 1;
  139. }
  140. else
  141. {
  142. if (Opt[1] == '=')
  143. {
  144. CertainArg = true;
  145. Argument = Opt + 2;
  146. }
  147. else
  148. Argument = Opt + 1;
  149. }
  150. // Option is an argument set
  151. if ((A->Flags & HasArg) == HasArg)
  152. {
  153. if (Argument == 0)
  154. return _error->Error("Option %s requires an argument.",argv[I]);
  155. Opt += strlen(Opt);
  156. I += IncI;
  157. // Parse a configuration file
  158. if ((A->Flags & ConfigFile) == ConfigFile)
  159. return ReadConfigFile(*Conf,Argument);
  160. // Arbitary item specification
  161. if ((A->Flags & ArbItem) == ArbItem)
  162. {
  163. const char *J;
  164. for (J = Argument; *J != 0 && *J != '='; J++);
  165. if (*J == 0)
  166. return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
  167. // = is trailing
  168. if (J[1] == 0)
  169. {
  170. if (I+1 >= argc)
  171. return _error->Error("Option %s: Configuration item sepecification must have an =<val>.",argv[I]);
  172. Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
  173. }
  174. else
  175. Conf->Set(string(Argument,J-Argument),string(J+1));
  176. return true;
  177. }
  178. const char *I = A->ConfName;
  179. for (; *I != 0 && *I != ' '; I++);
  180. if (*I == ' ')
  181. Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument);
  182. else
  183. Conf->Set(A->ConfName,string(I) + Argument);
  184. return true;
  185. }
  186. // Option is an integer level
  187. if ((A->Flags & IntLevel) == IntLevel)
  188. {
  189. // There might be an argument
  190. if (Argument != 0)
  191. {
  192. char *EndPtr;
  193. unsigned long Value = strtol(Argument,&EndPtr,10);
  194. // Conversion failed and the argument was specified with an =s
  195. if (EndPtr == Argument && CertainArg == true)
  196. return _error->Error("Option %s requires an integer argument, not '%s'",argv[I],Argument);
  197. // Conversion was ok, set the value and return
  198. if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0)
  199. {
  200. Conf->Set(A->ConfName,Value);
  201. Opt += strlen(Opt);
  202. I += IncI;
  203. return true;
  204. }
  205. }
  206. // Increase the level
  207. Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
  208. return true;
  209. }
  210. // Option is a boolean
  211. int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
  212. // Look for an argument.
  213. while (1)
  214. {
  215. // Look at preceeding text
  216. char Buffer[300];
  217. if (Argument == 0)
  218. {
  219. if (PreceedMatch == false)
  220. break;
  221. if (strlen(argv[I]) >= sizeof(Buffer))
  222. return _error->Error("Option '%s' is too long",argv[I]);
  223. // Skip the leading dash
  224. const char *J = argv[I];
  225. for (; *J != 0 && *J == '-'; J++);
  226. const char *JEnd = J;
  227. for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
  228. if (*JEnd != 0)
  229. {
  230. strncpy(Buffer,J,JEnd - J);
  231. Buffer[JEnd - J] = 0;
  232. Argument = Buffer;
  233. CertainArg = true;
  234. }
  235. else
  236. break;
  237. }
  238. // Check for boolean
  239. Sense = StringToBool(Argument);
  240. if (Sense >= 0)
  241. {
  242. // Eat the argument
  243. if (Argument != Buffer)
  244. {
  245. Opt += strlen(Opt);
  246. I += IncI;
  247. }
  248. break;
  249. }
  250. if (CertainArg == true)
  251. return _error->Error("Sense %s is not understood, try true or false.",Argument);
  252. Argument = 0;
  253. }
  254. // Indeterminate sense depends on the flag
  255. if (Sense == -1)
  256. {
  257. if ((A->Flags & InvBoolean) == InvBoolean)
  258. Sense = 0;
  259. else
  260. Sense = 1;
  261. }
  262. Conf->Set(A->ConfName,Sense);
  263. return true;
  264. }
  265. /*}}}*/
  266. // CommandLine::FileSize - Count the number of filenames /*{{{*/
  267. // ---------------------------------------------------------------------
  268. /* */
  269. unsigned int CommandLine::FileSize() const
  270. {
  271. unsigned int Count = 0;
  272. for (const char **I = FileList; I != 0 && *I != 0; I++)
  273. Count++;
  274. return Count;
  275. }
  276. /*}}}*/
  277. // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* */
  280. bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
  281. {
  282. int I;
  283. for (I = 0; Map[I].Match != 0; I++)
  284. {
  285. if (strcmp(FileList[0],Map[I].Match) == 0)
  286. {
  287. bool Res = Map[I].Handler(*this);
  288. if (Res == false && _error->PendingError() == false)
  289. _error->Error("Handler silently failed");
  290. return Res;
  291. }
  292. }
  293. // No matching name
  294. if (Map[I].Match == 0)
  295. {
  296. if (NoMatch == true)
  297. _error->Error("Invalid operation %s",FileList[0]);
  298. }
  299. return false;
  300. }
  301. /*}}}*/