cmndline.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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<config.h>
  12. #include <apt-pkg/configuration.h>
  13. #include <apt-pkg/cmndline.h>
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/strutl.h>
  16. #include <apti18n.h>
  17. /*}}}*/
  18. using namespace std;
  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::GetCommand - return the first non-option word /*{{{*/
  36. char const * CommandLine::GetCommand(Dispatch const * const Map,
  37. unsigned int const argc, char const * const * const argv)
  38. {
  39. // if there is a -- on the line there must be the word we search for around it
  40. // as -- marks the end of the options, just not sure if the command can be
  41. // considered an option or not, so accept both
  42. for (size_t i = 1; i < argc; ++i)
  43. {
  44. if (strcmp(argv[i], "--") != 0)
  45. continue;
  46. ++i;
  47. if (i < argc)
  48. for (size_t j = 0; Map[j].Match != NULL; ++j)
  49. if (strcmp(argv[i], Map[j].Match) == 0)
  50. return Map[j].Match;
  51. i -= 2;
  52. if (i != 0)
  53. for (size_t j = 0; Map[j].Match != NULL; ++j)
  54. if (strcmp(argv[i], Map[j].Match) == 0)
  55. return Map[j].Match;
  56. return NULL;
  57. }
  58. // no --, so search for the first word matching a command
  59. // FIXME: How like is it that an option parameter will be also a valid Match ?
  60. for (size_t i = 1; i < argc; ++i)
  61. {
  62. if (*(argv[i]) == '-')
  63. continue;
  64. for (size_t j = 0; Map[j].Match != NULL; ++j)
  65. if (strcmp(argv[i], Map[j].Match) == 0)
  66. return Map[j].Match;
  67. }
  68. return NULL;
  69. }
  70. /*}}}*/
  71. // CommandLine::Parse - Main action member /*{{{*/
  72. // ---------------------------------------------------------------------
  73. /* */
  74. bool CommandLine::Parse(int argc,const char **argv)
  75. {
  76. delete [] FileList;
  77. FileList = new const char *[argc];
  78. const char **Files = FileList;
  79. int I;
  80. for (I = 1; I != argc; I++)
  81. {
  82. const char *Opt = argv[I];
  83. // It is not an option
  84. if (*Opt != '-')
  85. {
  86. *Files++ = Opt;
  87. continue;
  88. }
  89. Opt++;
  90. // Double dash signifies the end of option processing
  91. if (*Opt == '-' && Opt[1] == 0)
  92. {
  93. I++;
  94. break;
  95. }
  96. // Single dash is a short option
  97. if (*Opt != '-')
  98. {
  99. // Iterate over each letter
  100. while (*Opt != 0)
  101. {
  102. // Search for the option
  103. Args *A;
  104. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  105. if (A->end() == true)
  106. return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]);
  107. if (HandleOpt(I,argc,argv,Opt,A) == false)
  108. return false;
  109. if (*Opt != 0)
  110. Opt++;
  111. }
  112. continue;
  113. }
  114. Opt++;
  115. // Match up to a = against the list
  116. Args *A;
  117. const char *OptEnd = strchrnul(Opt, '=');
  118. for (A = ArgList; A->end() == false &&
  119. (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0);
  120. ++A);
  121. // Failed, look for a word after the first - (no-foo)
  122. bool PreceedMatch = false;
  123. if (A->end() == true)
  124. {
  125. Opt = (const char*) memchr(Opt, '-', OptEnd - Opt);
  126. if (Opt == NULL)
  127. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  128. Opt++;
  129. for (A = ArgList; A->end() == false &&
  130. (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0);
  131. ++A);
  132. // Failed again..
  133. if (A->end() == true && OptEnd - Opt != 1)
  134. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  135. // The option could be a single letter option prefixed by a no-..
  136. if (A->end() == true)
  137. {
  138. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  139. if (A->end() == true)
  140. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  141. }
  142. // The option is not boolean
  143. if (A->IsBoolean() == false)
  144. return _error->Error(_("Command line option %s is not boolean"),argv[I]);
  145. PreceedMatch = true;
  146. }
  147. // Deal with it.
  148. OptEnd--;
  149. if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
  150. return false;
  151. }
  152. // Copy any remaining file names over
  153. for (; I != argc; I++)
  154. *Files++ = argv[I];
  155. *Files = 0;
  156. SaveInConfig(argc, argv);
  157. return true;
  158. }
  159. /*}}}*/
  160. // CommandLine::HandleOpt - Handle a single option including all flags /*{{{*/
  161. // ---------------------------------------------------------------------
  162. /* This is a helper function for parser, it looks at a given argument
  163. and looks for specific patterns in the string, it gets tokanized
  164. -ruffly- like -*[yes|true|enable]-(o|longopt)[=][ ][argument] */
  165. bool CommandLine::HandleOpt(int &I,int argc,const char *argv[],
  166. const char *&Opt,Args *A,bool PreceedMatch)
  167. {
  168. const char *Argument = 0;
  169. bool CertainArg = false;
  170. int IncI = 0;
  171. /* Determine the possible location of an option or 0 if their is
  172. no option */
  173. if (Opt[1] == 0 || (Opt[1] == '=' && Opt[2] == 0))
  174. {
  175. if (I + 1 < argc && argv[I+1][0] != '-')
  176. Argument = argv[I+1];
  177. // Equals was specified but we fell off the end!
  178. if (Opt[1] == '=' && Argument == 0)
  179. return _error->Error(_("Option %s requires an argument."),argv[I]);
  180. if (Opt[1] == '=')
  181. CertainArg = true;
  182. IncI = 1;
  183. }
  184. else
  185. {
  186. if (Opt[1] == '=')
  187. {
  188. CertainArg = true;
  189. Argument = Opt + 2;
  190. }
  191. else
  192. Argument = Opt + 1;
  193. }
  194. // Option is an argument set
  195. if ((A->Flags & HasArg) == HasArg)
  196. {
  197. if (Argument == 0)
  198. return _error->Error(_("Option %s requires an argument."),argv[I]);
  199. Opt += strlen(Opt);
  200. I += IncI;
  201. // Parse a configuration file
  202. if ((A->Flags & ConfigFile) == ConfigFile)
  203. return ReadConfigFile(*Conf,Argument);
  204. // Arbitrary item specification
  205. if ((A->Flags & ArbItem) == ArbItem)
  206. {
  207. const char *J = strchr(Argument, '=');
  208. if (J == NULL)
  209. return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]);
  210. // = is trailing
  211. if (J[1] == 0)
  212. {
  213. if (I+1 >= argc)
  214. return _error->Error(_("Option %s: Configuration item specification must have an =<val>."),argv[I]);
  215. Conf->Set(string(Argument,J-Argument),string(argv[I++ +1]));
  216. }
  217. else
  218. Conf->Set(string(Argument,J-Argument),string(J+1));
  219. return true;
  220. }
  221. const char *I = strchrnul(A->ConfName, ' ');
  222. if (*I == ' ')
  223. Conf->Set(string(A->ConfName,0,I-A->ConfName),string(I+1) + Argument);
  224. else
  225. Conf->Set(A->ConfName,string(I) + Argument);
  226. return true;
  227. }
  228. // Option is an integer level
  229. if ((A->Flags & IntLevel) == IntLevel)
  230. {
  231. // There might be an argument
  232. if (Argument != 0)
  233. {
  234. char *EndPtr;
  235. unsigned long Value = strtol(Argument,&EndPtr,10);
  236. // Conversion failed and the argument was specified with an =s
  237. if (EndPtr == Argument && CertainArg == true)
  238. return _error->Error(_("Option %s requires an integer argument, not '%s'"),argv[I],Argument);
  239. // Conversion was ok, set the value and return
  240. if (EndPtr != 0 && EndPtr != Argument && *EndPtr == 0)
  241. {
  242. Conf->Set(A->ConfName,Value);
  243. Opt += strlen(Opt);
  244. I += IncI;
  245. return true;
  246. }
  247. }
  248. // Increase the level
  249. Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
  250. return true;
  251. }
  252. // Option is a boolean
  253. int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
  254. // Look for an argument.
  255. while (1)
  256. {
  257. // Look at preceding text
  258. char Buffer[300];
  259. if (Argument == 0)
  260. {
  261. if (PreceedMatch == false)
  262. break;
  263. if (strlen(argv[I]) >= sizeof(Buffer))
  264. return _error->Error(_("Option '%s' is too long"),argv[I]);
  265. // Skip the leading dash
  266. const char *J = argv[I];
  267. for (; *J != 0 && *J == '-'; J++);
  268. const char *JEnd = strchr(J, '-');
  269. if (JEnd != NULL)
  270. {
  271. strncpy(Buffer,J,JEnd - J);
  272. Buffer[JEnd - J] = 0;
  273. Argument = Buffer;
  274. CertainArg = true;
  275. }
  276. else
  277. break;
  278. }
  279. // Check for boolean
  280. Sense = StringToBool(Argument);
  281. if (Sense >= 0)
  282. {
  283. // Eat the argument
  284. if (Argument != Buffer)
  285. {
  286. Opt += strlen(Opt);
  287. I += IncI;
  288. }
  289. break;
  290. }
  291. if (CertainArg == true)
  292. return _error->Error(_("Sense %s is not understood, try true or false."),Argument);
  293. Argument = 0;
  294. }
  295. // Indeterminate sense depends on the flag
  296. if (Sense == -1)
  297. {
  298. if ((A->Flags & InvBoolean) == InvBoolean)
  299. Sense = 0;
  300. else
  301. Sense = 1;
  302. }
  303. Conf->Set(A->ConfName,Sense);
  304. return true;
  305. }
  306. /*}}}*/
  307. // CommandLine::FileSize - Count the number of filenames /*{{{*/
  308. // ---------------------------------------------------------------------
  309. /* */
  310. unsigned int CommandLine::FileSize() const
  311. {
  312. unsigned int Count = 0;
  313. for (const char **I = FileList; I != 0 && *I != 0; I++)
  314. Count++;
  315. return Count;
  316. }
  317. /*}}}*/
  318. // CommandLine::DispatchArg - Do something with the first arg /*{{{*/
  319. // ---------------------------------------------------------------------
  320. /* */
  321. bool CommandLine::DispatchArg(Dispatch *Map,bool NoMatch)
  322. {
  323. int I;
  324. for (I = 0; Map[I].Match != 0; I++)
  325. {
  326. if (strcmp(FileList[0],Map[I].Match) == 0)
  327. {
  328. bool Res = Map[I].Handler(*this);
  329. if (Res == false && _error->PendingError() == false)
  330. _error->Error("Handler silently failed");
  331. return Res;
  332. }
  333. }
  334. // No matching name
  335. if (Map[I].Match == 0)
  336. {
  337. if (NoMatch == true)
  338. _error->Error(_("Invalid operation %s"),FileList[0]);
  339. }
  340. return false;
  341. }
  342. /*}}}*/
  343. // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
  344. // ---------------------------------------------------------------------
  345. /* We save the commandline here to have it around later for e.g. logging.
  346. It feels a bit like a hack here and isn't bulletproof, but it is better
  347. than nothing after all. */
  348. void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv)
  349. {
  350. char cmdline[100 + argc * 50];
  351. memset(cmdline, 0, sizeof(cmdline));
  352. unsigned int length = 0;
  353. bool lastWasOption = false;
  354. bool closeQuote = false;
  355. for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length)
  356. {
  357. for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length)
  358. {
  359. cmdline[length] = argv[i][j];
  360. if (lastWasOption == true && argv[i][j] == '=')
  361. {
  362. // That is possibly an option: Quote it if it includes spaces,
  363. // the benefit is that this will eliminate also most false positives
  364. const char* c = strchr(&argv[i][j+1], ' ');
  365. if (c == NULL) continue;
  366. cmdline[++length] = '"';
  367. closeQuote = true;
  368. }
  369. }
  370. if (closeQuote == true)
  371. cmdline[length++] = '"';
  372. // Problem: detects also --hello
  373. if (cmdline[length-1] == 'o')
  374. lastWasOption = true;
  375. cmdline[length] = ' ';
  376. }
  377. cmdline[--length] = '\0';
  378. _config->Set("CommandLine::AsString", cmdline);
  379. }
  380. /*}}}*/
  381. CommandLine::Args CommandLine::MakeArgs(char ShortOpt, char const *LongOpt, char const *ConfName, unsigned long Flags)/*{{{*/
  382. {
  383. /* In theory, this should be a constructor for CommandLine::Args instead,
  384. but this breaks compatibility as gcc thinks this is a c++11 initializer_list */
  385. CommandLine::Args arg;
  386. arg.ShortOpt = ShortOpt;
  387. arg.LongOpt = LongOpt;
  388. arg.ConfName = ConfName;
  389. arg.Flags = Flags;
  390. return arg;
  391. }
  392. /*}}}*/