cmndline.cc 9.2 KB

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