cmndline.cc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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/cmndline.h>
  13. #include <apt-pkg/error.h>
  14. #include <apt-pkg/strutl.h>
  15. #include <apti18n.h>
  16. /*}}}*/
  17. using namespace std;
  18. // CommandLine::CommandLine - Constructor /*{{{*/
  19. // ---------------------------------------------------------------------
  20. /* */
  21. CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
  22. Conf(Conf), FileList(0)
  23. {
  24. }
  25. /*}}}*/
  26. // CommandLine::~CommandLine - Destructor /*{{{*/
  27. // ---------------------------------------------------------------------
  28. /* */
  29. CommandLine::~CommandLine()
  30. {
  31. delete [] FileList;
  32. }
  33. /*}}}*/
  34. // CommandLine::Parse - Main action member /*{{{*/
  35. // ---------------------------------------------------------------------
  36. /* */
  37. bool CommandLine::Parse(int argc,const char **argv)
  38. {
  39. delete [] FileList;
  40. FileList = new const char *[argc];
  41. const char **Files = FileList;
  42. int I;
  43. for (I = 1; I != argc; I++)
  44. {
  45. const char *Opt = argv[I];
  46. // It is not an option
  47. if (*Opt != '-')
  48. {
  49. *Files++ = Opt;
  50. continue;
  51. }
  52. Opt++;
  53. // Double dash signifies the end of option processing
  54. if (*Opt == '-' && Opt[1] == 0)
  55. {
  56. I++;
  57. break;
  58. }
  59. // Single dash is a short option
  60. if (*Opt != '-')
  61. {
  62. // Iterate over each letter
  63. while (*Opt != 0)
  64. {
  65. // Search for the option
  66. Args *A;
  67. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  68. if (A->end() == true)
  69. return _error->Error(_("Command line option '%c' [from %s] is not known."),*Opt,argv[I]);
  70. if (HandleOpt(I,argc,argv,Opt,A) == false)
  71. return false;
  72. if (*Opt != 0)
  73. Opt++;
  74. }
  75. continue;
  76. }
  77. Opt++;
  78. // Match up to a = against the list
  79. const char *OptEnd = Opt;
  80. Args *A;
  81. for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
  82. for (A = ArgList; A->end() == false &&
  83. stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
  84. // Failed, look for a word after the first - (no-foo)
  85. bool PreceedMatch = false;
  86. if (A->end() == true)
  87. {
  88. for (; Opt != OptEnd && *Opt != '-'; Opt++);
  89. if (Opt == OptEnd)
  90. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  91. Opt++;
  92. for (A = ArgList; A->end() == false &&
  93. stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
  94. // Failed again..
  95. if (A->end() == true && OptEnd - Opt != 1)
  96. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  97. // The option could be a single letter option prefixed by a no-..
  98. if (A->end() == true)
  99. {
  100. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  101. if (A->end() == true)
  102. return _error->Error(_("Command line option %s is not understood"),argv[I]);
  103. }
  104. // The option is not boolean
  105. if (A->IsBoolean() == false)
  106. return _error->Error(_("Command line option %s is not boolean"),argv[I]);
  107. PreceedMatch = true;
  108. }
  109. // Deal with it.
  110. OptEnd--;
  111. if (HandleOpt(I,argc,argv,OptEnd,A,PreceedMatch) == false)
  112. return false;
  113. }
  114. // Copy any remaining file names over
  115. for (; I != argc; I++)
  116. *Files++ = argv[I];
  117. *Files = 0;
  118. SaveInConfig(argc, argv);
  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. // Arbitrary 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 specification 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 specification 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. /*}}}*/
  308. // CommandLine::SaveInConfig - for output later in a logfile or so /*{{{*/
  309. // ---------------------------------------------------------------------
  310. /* We save the commandline here to have it around later for e.g. logging.
  311. It feels a bit like a hack here and isn't bulletproof, but it is better
  312. than nothing after all. */
  313. void CommandLine::SaveInConfig(unsigned int const &argc, char const * const * const argv)
  314. {
  315. char cmdline[100 + argc * 50];
  316. unsigned int length = 0;
  317. bool lastWasOption = false;
  318. bool closeQuote = false;
  319. for (unsigned int i = 0; i < argc && length < sizeof(cmdline); ++i, ++length)
  320. {
  321. for (unsigned int j = 0; argv[i][j] != '\0' && length < sizeof(cmdline)-1; ++j, ++length)
  322. {
  323. cmdline[length] = argv[i][j];
  324. if (lastWasOption == true && argv[i][j] == '=')
  325. {
  326. // That is possibly an option: Quote it if it includes spaces,
  327. // the benefit is that this will eliminate also most false positives
  328. const char* c = &argv[i][j+1];
  329. for (; *c != '\0' && *c != ' '; ++c);
  330. if (*c == '\0') continue;
  331. cmdline[++length] = '"';
  332. closeQuote = true;
  333. }
  334. }
  335. if (closeQuote == true)
  336. cmdline[length++] = '"';
  337. // Problem: detects also --hello
  338. if (cmdline[length-1] == 'o')
  339. lastWasOption = true;
  340. cmdline[length] = ' ';
  341. }
  342. cmdline[--length] = '\0';
  343. _config->Set("CommandLine::AsString", cmdline);
  344. }
  345. /*}}}*/