cmndline.cc 9.0 KB

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