cmndline.cc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: cmndline.cc,v 1.5 1998/10/24 20:14:34 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 <strutl.h>
  15. /*}}}*/
  16. // CommandLine::CommandLine - Constructor /*{{{*/
  17. // ---------------------------------------------------------------------
  18. /* */
  19. CommandLine::CommandLine(Args *AList,Configuration *Conf) : ArgList(AList),
  20. Conf(Conf), FileList(0)
  21. {
  22. }
  23. /*}}}*/
  24. // CommandLine::~CommandLine - Destructor /*{{{*/
  25. // ---------------------------------------------------------------------
  26. /* */
  27. CommandLine::~CommandLine()
  28. {
  29. delete [] FileList;
  30. }
  31. /*}}}*/
  32. // CommandLine::Parse - Main action member /*{{{*/
  33. // ---------------------------------------------------------------------
  34. /* */
  35. bool CommandLine::Parse(int argc,const char **argv)
  36. {
  37. delete [] FileList;
  38. FileList = new const char *[argc];
  39. const char **Files = FileList;
  40. int I;
  41. for (I = 1; I != argc; I++)
  42. {
  43. const char *Opt = argv[I];
  44. // It is not an option
  45. if (*Opt != '-')
  46. {
  47. *Files++ = Opt;
  48. continue;
  49. }
  50. Opt++;
  51. // Double dash signifies the end of option processing
  52. if (*Opt == '-' && Opt[1] == 0)
  53. break;
  54. // Single dash is a short option
  55. if (*Opt != '-')
  56. {
  57. // Iterate over each letter
  58. while (*Opt != 0)
  59. {
  60. // Search for the option
  61. Args *A;
  62. for (A = ArgList; A->end() == false && A->ShortOpt != *Opt; A++);
  63. if (A->end() == true)
  64. return _error->Error("Command line option '%c' [from %s] is not known.",*Opt,argv[I]);
  65. if (HandleOpt(I,argc,argv,Opt,A) == false)
  66. return false;
  67. if (*Opt != 0)
  68. Opt++;
  69. }
  70. continue;
  71. }
  72. Opt++;
  73. // Match up to a = against the list
  74. const char *OptEnd = Opt;
  75. Args *A;
  76. for (; *OptEnd != 0 && *OptEnd != '='; OptEnd++);
  77. for (A = ArgList; A->end() == false &&
  78. stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++);
  79. // Failed, look for a word after the first - (no-foo)
  80. bool PreceedMatch = false;
  81. if (A->end() == true)
  82. {
  83. for (; Opt != OptEnd && *Opt != '-'; Opt++);
  84. if (Opt == OptEnd)
  85. return _error->Error("Command line option %s is not understood",argv[I]);
  86. Opt++;
  87. cout << Opt << endl;
  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. Conf->Set(A->ConfName,Argument);
  180. return true;
  181. }
  182. // Option is an integer level
  183. if ((A->Flags & IntLevel) == IntLevel)
  184. {
  185. // There might be an argument
  186. if (Argument != 0)
  187. {
  188. char *EndPtr;
  189. unsigned long Value = strtol(Argument,&EndPtr,10);
  190. // Conversion failed and the argument was specified with an =s
  191. if (EndPtr == Argument && CertainArg == true)
  192. return _error->Error("Option %s requires an integer argument, not '%s'",argv[I],Argument);
  193. // Conversion was ok, set the value and return
  194. if (EndPtr != Argument)
  195. {
  196. Conf->Set(A->ConfName,Value);
  197. Opt += strlen(Opt);
  198. I += IncI;
  199. return true;
  200. }
  201. }
  202. // Increase the level
  203. Conf->Set(A->ConfName,Conf->FindI(A->ConfName)+1);
  204. return true;
  205. }
  206. // Option is a boolean
  207. int Sense = -1; // -1 is unspecified, 0 is yes 1 is no
  208. // Look for an argument.
  209. while (1)
  210. {
  211. // Look at preceeding text
  212. char Buffer[300];
  213. if (Argument == 0)
  214. {
  215. if (PreceedMatch == false)
  216. break;
  217. if (strlen(argv[I]) >= sizeof(Buffer))
  218. return _error->Error("Option '%s' is too long",argv[I]);
  219. // Skip the leading dash
  220. const char *J = argv[I];
  221. for (; *J != 0 && *J == '-'; J++);
  222. const char *JEnd = J;
  223. for (; *JEnd != 0 && *JEnd != '-'; JEnd++);
  224. if (*JEnd != 0)
  225. {
  226. strncpy(Buffer,J,JEnd - J);
  227. Buffer[JEnd - J] = 0;
  228. Argument = Buffer;
  229. CertainArg = true;
  230. }
  231. else
  232. break;
  233. }
  234. // Check for boolean
  235. Sense = StringToBool(Argument);
  236. if (Sense >= 0)
  237. {
  238. // Eat the argument
  239. if (Argument != Buffer)
  240. {
  241. Opt += strlen(Opt);
  242. I += IncI;
  243. }
  244. break;
  245. }
  246. if (CertainArg == true)
  247. return _error->Error("Sense %s is not understood, try true or false.",Argument);
  248. Argument = 0;
  249. }
  250. // Indeterminate sense depends on the flag
  251. if (Sense == -1)
  252. {
  253. if ((A->Flags & InvBoolean) == InvBoolean)
  254. Sense = 0;
  255. else
  256. Sense = 1;
  257. }
  258. Conf->Set(A->ConfName,Sense);
  259. return true;
  260. }
  261. /*}}}*/
  262. // CommandLine::FileSize - Count the number of filenames /*{{{*/
  263. // ---------------------------------------------------------------------
  264. /* */
  265. unsigned int CommandLine::FileSize() const
  266. {
  267. unsigned int Count = 0;
  268. for (const char **I = FileList; I != 0 && *I != 0; I++)
  269. Count++;
  270. return Count;
  271. }
  272. /*}}}*/