getopt_ulflags.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * universal getopt_ulflags implementation for busybox
  4. *
  5. * Copyright (C) 2003-2005 Vladimir Oleynik <dzo@simtreas.ru>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <getopt.h>
  23. #include <string.h>
  24. #include <assert.h>
  25. #include <stdlib.h>
  26. #include "libbb.h"
  27. /* Documentation !
  28. unsigned long
  29. bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
  30. The command line options must be declared in const char
  31. *applet_opts as a string of chars, for example:
  32. flags = bb_getopt_ulflags(argc, argv, "rnug");
  33. If one of the given options is found, a flag value is added to
  34. the return value (an unsigned long).
  35. The flag value is determined by the position of the char in
  36. applet_opts string. For example, in the above case:
  37. flags = bb_getopt_ulflags(argc, argv, "rnug");
  38. "r" will add 1 (bit 1 : 0x01)
  39. "n" will add 2 (bit 2 : 0x02)
  40. "u will add 4 (bit 3 : 0x03)
  41. "g" will add 8 (bit 4 : 0x04)
  42. and so on. You can also look at the return value as a bit
  43. field and each option sets one of bits.
  44. ":" If one of the options requires an argument, then add a ":"
  45. after the char in applet_opts and provide a pointer to store
  46. the argument. For example:
  47. char *pointer_to_arg_for_a;
  48. char *pointer_to_arg_for_b;
  49. char *pointer_to_arg_for_c;
  50. char *pointer_to_arg_for_d;
  51. flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:",
  52. &pointer_to_arg_for_a, &pointer_to_arg_for_b,
  53. &pointer_to_arg_for_c, &pointer_to_arg_for_d);
  54. The type of the pointer (char* or llist_t *) may be controlled
  55. by the "::" special separator that is set in the external string
  56. bb_opt_complementally (see below for more info).
  57. "+" If the first character in the applet_opts string is a plus,
  58. then option processing will stop as soon as a non-option is
  59. encountered in the argv array. Useful for applets like env
  60. which should not process arguments to subprograms:
  61. env -i ls -d /
  62. Here we want env to process just the '-i', not the '-d'.
  63. static const struct option bb_default_long_options[]
  64. This struct allows you to define long options. The syntax for
  65. declaring the array is just like that of getopt's longopts.
  66. (see getopt(3))
  67. static const struct option applet_long_options[] = {
  68. { "verbose", 0, 0, v },
  69. { 0, 0, 0, 0 }
  70. };
  71. bb_applet_long_options = applet_long_options;
  72. The last argument (val) can undefined from applet_opts.
  73. If you use this, then:
  74. - return bit have next position after short options
  75. - if has_arg is not "no_argument", use ptr for arg also
  76. - bb_opt_complementally have effects for this too
  77. Note: a good applet will make long options configurable via the
  78. config process and not a required feature. The current standard
  79. is to name the config option CONFIG_FEATURE_<applet>_LONG_OPTIONS.
  80. const char *bb_opt_complementally
  81. this should be bb_opt_complementary, but we'll just keep it as
  82. bb_opt_complementally due to the Russian origins
  83. ":" The colon (":") is used to separate groups of two or more chars
  84. and/or groups of chars and special characters (stating some
  85. conditions to be checked).
  86. "abc" If groups of two or more chars are specified, the first char
  87. is the main option and the other chars are secondary options.
  88. Their flags will be turned on if the main option is found even
  89. if they are not specifed on the command line. For example:
  90. bb_opt_complementally = "abc";
  91. flags = bb_getopt_ulflags(argc, argv, "abcd")
  92. If getopt() finds "-a" on the command line, then
  93. bb_getopt_ulflags's return value will be as if "-a -b -c" were
  94. found.
  95. "ww" Adjacent double options have a counter associated which indicates
  96. the number of occurances of the option.
  97. For example the ps applet needs:
  98. if w is given once, GNU ps sets the width to 132,
  99. if w is given more than once, it is "unlimited"
  100. int w_counter = 0;
  101. bb_opt_complementally = "ww";
  102. bb_getopt_ulflags(argc, argv, "w", &w_counter);
  103. if(w_counter)
  104. width = (w_counter == 1) ? 132 : INT_MAX;
  105. else
  106. get_terminal_width(...&width...);
  107. w_counter is a pointer to an integer. It has to be passed to
  108. bb_getopt_ulflags() after all other option argument sinks.
  109. For example: accept multiple -v to indicate the level of verbosity
  110. and for each -b optarg, add optarg to my_b. Finally, if b is given,
  111. turn off c and vice versa:
  112. llist_t *my_b = NULL;
  113. int verbose_level = 0;
  114. bb_opt_complementally = "vv:b::b-c:c-b";
  115. f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level);
  116. if((f & 2)) // -c after -b unset this -b flag
  117. while (my_b) { dosomething_with(my_b->data) ; my_b = my_b->link; }
  118. if(my_b) // but llist stored always if -b found
  119. free_llist(my_b);
  120. if (verbose_level) bb_printf("verbose level is %d\n", verbose_level);
  121. Special characters:
  122. "-" A dash between two options causes the second of the two
  123. to be unset (and ignored or triggered) if it is given on
  124. the command line.
  125. For example:
  126. The du applet has the options "-s" and "-d depth". If
  127. bb_getopt_ulflags finds -s, then -d is unset or if it finds -d
  128. then -s is unset. (Note: busybox implements the GNU
  129. "--max-depth" option as "-d".) To obtain this behavior, you
  130. set bb_opt_complementally = "s-d:d-s". Only one flag value is
  131. added to bb_getopt_ulflags's return value depending on the
  132. position of the options on the command line. If one of the
  133. two options requires an argument pointer (":" in applet_opts
  134. as in "d:") optarg is set accordingly.
  135. char *smax_print_depth;
  136. bb_opt_complementally = "s-d:d-s:x-x";
  137. opt = bb_getopt_ulflags(argc, argv, "sd:x", &smax_print_depth);
  138. if (opt & 2) {
  139. max_print_depth = atoi(smax_print_depth);
  140. }
  141. if(opt & 4)
  142. printf("Detected odd -x usaging\n");
  143. "-" A dash as the first char in a bb_opt_complementally group means to
  144. convert the arguments as option. Next char for this case can't set
  145. [0-9], recomended use ':' or end of line. For example:
  146. bb_opt_complementally = "-:w-x:x-w";
  147. bb_getopt_ulflags(argc, argv, "wx");
  148. Allows any arguments to be given without a dash (./program w x)
  149. as well as with a dash (./program -x). Why unset -w see above.
  150. "-N" A dash as the first char in a bb_opt_complementally group with
  151. number 0-9 as one char is means check minimal arguments required.
  152. "V-" A option with dash before colon or end line indicate: call
  153. bb_show_usage if this option give, for example verbose
  154. usage option.
  155. "--" A double dash between two options, or between an option and a group
  156. of options, means that they are mutually exclusive. Unlike
  157. the "-" case above, an error will be forced if the options
  158. are used together.
  159. For example:
  160. The cut applet must have only one type of list specified, so
  161. -b, -c and -f are mutally exclusive and should raise an error
  162. if specified together. In this case you must set
  163. bb_opt_complementally = "b--cf:c--bf:f--bc". If two of the
  164. mutually exclusive options are found, bb_getopt_ulflags's
  165. return value will have the error flag set (BB_GETOPT_ERROR) so
  166. that we can check for it:
  167. if (flags & BB_GETOPT_ERROR)
  168. bb_show_usage();
  169. "?" A "ask" as the first char in a bb_opt_complementally group give:
  170. if previous point set BB_GETOPT_ERROR, don't return and
  171. call previous example internally. Next char for this case can't
  172. set to [0-9], recomended use ':' or end of line.
  173. "?N" A "ask" as the first char in a bb_opt_complementally group with
  174. number 0-9 as one char is means check maximal arguments possible.
  175. "::" A double colon after a char in bb_opt_complementally means that the
  176. option can occur multiple times:
  177. For example:
  178. The grep applet can have one or more "-e pattern" arguments.
  179. In this case you should use bb_getopt_ulflags() as follows:
  180. llist_t *patterns = NULL;
  181. (this pointer must be initializated to NULL if the list is empty
  182. as required by *llist_add_to(llist_t *old_head, char *new_item).)
  183. bb_opt_complementally = "e::";
  184. bb_getopt_ulflags(argc, argv, "e:", &patterns);
  185. $ grep -e user -e root /etc/passwd
  186. root:x:0:0:root:/root:/bin/bash
  187. user:x:500:500::/home/user:/bin/bash
  188. "--" A double dash at the beginning of bb_opt_complementally means the
  189. argv[1] string should always be treated as options, even if it isn't
  190. prefixed with a "-". This is to support the special syntax in applets
  191. such as "ar" and "tar":
  192. tar xvf foo.tar
  193. "?" An "ask" between main and group options causes the second of the two
  194. to be depending required as or if first is given on the command line.
  195. For example from "id" applet:
  196. // Don't allow -n -r -rn -ug -rug -nug -rnug
  197. bb_opt_complementally = "r?ug:n?ug:?u--g:g--u";
  198. flags = bb_getopt_ulflags(argc, argv, "rnug");
  199. This example allowed only:
  200. $ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng
  201. "X" A one options in bb_opt_complementally group means
  202. requires this option always with "or" logic if more one specified,
  203. checked after switch off from complementally logic.
  204. For example from "start-stop-daemon" applet:
  205. // Don't allow -KS -SK, but -S or -K required
  206. bb_opt_complementally = "K:S:?K--S:S--K";
  207. flags = bb_getopt_ulflags(argc, argv, "KS...);
  208. "x--x" give error if double or more used -x option
  209. Don't forget ':' store. For example "?322-22-23X-x-a" interpretet as
  210. "?3:22:-2:2-2:2-3Xa:2--x": max args is 3, count -2 usaged, min args is 2,
  211. -2 option triggered, unset -3 and -X and -a if -2 any usaged, give error if
  212. after -2 the -x option usaged.
  213. */
  214. /* this should be bb_opt_complementary, but we'll just keep it as
  215. bb_opt_complementally due to the Russian origins */
  216. const char *bb_opt_complementally;
  217. typedef struct {
  218. int opt;
  219. int list_flg;
  220. unsigned long switch_on;
  221. unsigned long switch_off;
  222. unsigned long incongruously;
  223. unsigned long requires;
  224. void **optarg; /* char **optarg or llist_t **optarg */
  225. int *counter;
  226. } t_complementally;
  227. /* You can set bb_applet_long_options for parse called long options */
  228. static const struct option bb_default_long_options[] = {
  229. /* { "help", 0, NULL, '?' }, */
  230. { 0, 0, 0, 0 }
  231. };
  232. const struct option *bb_applet_long_options = bb_default_long_options;
  233. unsigned long
  234. bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
  235. {
  236. unsigned long flags = 0;
  237. unsigned long requires = 0;
  238. t_complementally complementally[sizeof(flags) * 8 + 1];
  239. int c;
  240. const unsigned char *s;
  241. t_complementally *on_off;
  242. va_list p;
  243. const struct option *l_o;
  244. unsigned long trigger;
  245. #ifdef CONFIG_PS
  246. char **pargv = NULL;
  247. #endif
  248. int min_arg = 0;
  249. int max_arg = -1;
  250. #define SHOW_USAGE_IF_ERROR 1
  251. #define ALL_ARGV_IS_OPTS 2
  252. #define FIRST_ARGV_IS_OPT 4
  253. #define FREE_FIRST_ARGV_IS_OPT 8
  254. int spec_flgs = 0;
  255. va_start (p, applet_opts);
  256. c = 0;
  257. on_off = complementally;
  258. memset(on_off, 0, sizeof(complementally));
  259. /* skip GNU extension */
  260. s = (const unsigned char *)applet_opts;
  261. if(*s == '+' || *s == '-')
  262. s++;
  263. for (; *s; s++) {
  264. if(c >= (int)(sizeof(flags)*8))
  265. break;
  266. on_off->opt = *s;
  267. on_off->switch_on = (1 << c);
  268. if (s[1] == ':') {
  269. on_off->optarg = va_arg (p, void **);
  270. do
  271. s++;
  272. while (s[1] == ':');
  273. }
  274. on_off++;
  275. c++;
  276. }
  277. for(l_o = bb_applet_long_options; l_o->name; l_o++) {
  278. if(l_o->flag)
  279. continue;
  280. for(on_off = complementally; on_off->opt != 0; on_off++)
  281. if(on_off->opt == l_o->val)
  282. break;
  283. if(on_off->opt == 0) {
  284. if(c >= (int)(sizeof(flags)*8))
  285. break;
  286. on_off->opt = l_o->val;
  287. on_off->switch_on = (1 << c);
  288. if(l_o->has_arg != no_argument)
  289. on_off->optarg = va_arg (p, void **);
  290. c++;
  291. }
  292. }
  293. for (s = (const unsigned char *)bb_opt_complementally; s && *s; s++) {
  294. t_complementally *pair;
  295. unsigned long *pair_switch;
  296. if (*s == ':')
  297. continue;
  298. c = s[1];
  299. if(*s == '?') {
  300. if(c < '0' || c > '9') {
  301. spec_flgs |= SHOW_USAGE_IF_ERROR;
  302. } else {
  303. max_arg = c - '0';
  304. s++;
  305. }
  306. continue;
  307. }
  308. if(*s == '-') {
  309. if(c < '0' || c > '9') {
  310. if(c == '-') {
  311. spec_flgs |= FIRST_ARGV_IS_OPT;
  312. s++;
  313. } else
  314. spec_flgs |= ALL_ARGV_IS_OPTS;
  315. } else {
  316. min_arg = c - '0';
  317. s++;
  318. }
  319. continue;
  320. }
  321. for (on_off = complementally; on_off->opt; on_off++)
  322. if (on_off->opt == *s)
  323. break;
  324. if(c == ':' && s[2] == ':') {
  325. on_off->list_flg++;
  326. continue;
  327. }
  328. if(c == ':' || c == '\0') {
  329. requires |= on_off->switch_on;
  330. continue;
  331. }
  332. if(c == '-' && (s[2] == ':' || s[2] == '\0')) {
  333. flags |= on_off->switch_on;
  334. on_off->incongruously |= on_off->switch_on;
  335. s++;
  336. continue;
  337. }
  338. if(c == *s) {
  339. on_off->counter = va_arg (p, int *);
  340. s++;
  341. }
  342. pair = on_off;
  343. pair_switch = &(pair->switch_on);
  344. for(s++; *s && *s != ':'; s++) {
  345. if(*s == '?') {
  346. pair_switch = &(pair->requires);
  347. } else if (*s == '-') {
  348. if(pair_switch == &(pair->switch_off))
  349. pair_switch = &(pair->incongruously);
  350. else
  351. pair_switch = &(pair->switch_off);
  352. } else {
  353. for (on_off = complementally; on_off->opt; on_off++)
  354. if (on_off->opt == *s) {
  355. *pair_switch |= on_off->switch_on;
  356. break;
  357. }
  358. }
  359. }
  360. s--;
  361. }
  362. va_end (p);
  363. #if defined(CONFIG_AR) || defined(CONFIG_TAR)
  364. if((spec_flgs & FIRST_ARGV_IS_OPT)) {
  365. if(argv[1] && argv[1][0] != '-' && argv[1][0] != '\0') {
  366. argv[1] = bb_xasprintf("-%s", argv[1]);
  367. if(ENABLE_FEATURE_CLEAN_UP)
  368. spec_flgs |= FREE_FIRST_ARGV_IS_OPT;
  369. }
  370. }
  371. #endif
  372. while ((c = getopt_long (argc, argv, applet_opts,
  373. bb_applet_long_options, NULL)) >= 0) {
  374. #ifdef CONFIG_PS
  375. loop_arg_is_opt:
  376. #endif
  377. for (on_off = complementally; on_off->opt != c; on_off++) {
  378. /* c==0 if long opt have non NULL flag */
  379. if(on_off->opt == 0 && c != 0)
  380. bb_show_usage ();
  381. }
  382. if(flags & on_off->incongruously) {
  383. if((spec_flgs & SHOW_USAGE_IF_ERROR))
  384. bb_show_usage ();
  385. flags |= BB_GETOPT_ERROR;
  386. }
  387. trigger = on_off->switch_on & on_off->switch_off;
  388. flags &= ~(on_off->switch_off ^ trigger);
  389. flags |= on_off->switch_on ^ trigger;
  390. flags ^= trigger;
  391. if(on_off->counter)
  392. (*(on_off->counter))++;
  393. if(on_off->list_flg) {
  394. *(llist_t **)(on_off->optarg) =
  395. llist_add_to(*(llist_t **)(on_off->optarg), optarg);
  396. } else if (on_off->optarg) {
  397. *(char **)(on_off->optarg) = optarg;
  398. }
  399. #ifdef CONFIG_PS
  400. if(pargv != NULL)
  401. break;
  402. #endif
  403. }
  404. #ifdef CONFIG_PS
  405. if((spec_flgs & ALL_ARGV_IS_OPTS)) {
  406. /* process argv is option, for example "ps" applet */
  407. if(pargv == NULL)
  408. pargv = argv + optind;
  409. while(*pargv) {
  410. c = **pargv;
  411. if(c == '\0') {
  412. pargv++;
  413. } else {
  414. (*pargv)++;
  415. goto loop_arg_is_opt;
  416. }
  417. }
  418. }
  419. #endif
  420. #if (defined(CONFIG_AR) || defined(CONFIG_TAR)) && \
  421. defined(CONFIG_FEATURE_CLEAN_UP)
  422. if((spec_flgs & FREE_FIRST_ARGV_IS_OPT))
  423. free(argv[1]);
  424. #endif
  425. /* check depending requires for given options */
  426. for (on_off = complementally; on_off->opt; on_off++) {
  427. if(on_off->requires && (flags & on_off->switch_on) &&
  428. (flags & on_off->requires) == 0)
  429. bb_show_usage ();
  430. }
  431. if(requires && (flags & requires) == 0)
  432. bb_show_usage ();
  433. argc -= optind;
  434. if(argc < min_arg || (max_arg >= 0 && argc > max_arg))
  435. bb_show_usage ();
  436. return flags;
  437. }