cmndline.cc 9.1 KB

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