cmndline.cc 9.1 KB

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