getopt_ulflags.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * universal getopt_ulflags implementation for busybox
  4. *
  5. * Copyright (C) 2003 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 character that is set in the external string
  56. bb_opt_complementaly (see below for more info).
  57. static const struct option bb_default_long_options[]
  58. This struct allows you to define long options. The syntax for
  59. declaring the array is just like that of getopt's longopts.
  60. static const struct option applet_long_options[] = {
  61. { "verbose", 0, 0, "v" },
  62. { 0, 0, 0, 0 }
  63. };
  64. bb_applet_long_options = applet_long_options;
  65. The first parameter is the long option name that you would pass
  66. to the applet (without the dashes).
  67. The second field determines whether the option has an argument.
  68. You can set this to 0, 1, or 2, or you can use the long named
  69. defines of no_argument, required_argument, and optional_argument.
  70. The third argument is used only when the long option does not
  71. have a corresponding short option. In that case, it should be
  72. an integer pointer. Otherwise (and normally), it should just
  73. bet set to NULL.
  74. The last argument is the corresponding short option (if there
  75. is one of course).
  76. Note: a good applet will make long options configurable via the
  77. config process and not a required feature. The current standard
  78. is to name the config option CONFIG_FEATURE_<applet>_LONG_OPTIONS.
  79. const char *bb_opt_complementaly
  80. ":" The colon (":") is used to separate groups of two or more chars
  81. and/or groups of chars and special characters (stating some
  82. conditions to be checked).
  83. "abc" If groups of two or more chars are specified, the first char
  84. is the main option and the other chars are secondary options.
  85. Their flags will be turned on if the main option is found even
  86. if they are not specifed on the command line. For example:
  87. bb_opt_complementaly = "abc";
  88. flags = bb_getopt_ulflags(argc, argv, "abcd")
  89. If getopt() finds "-a" on the command line, then
  90. bb_getopt_ulflags's return value will be as if "-a -b -c" were
  91. found.
  92. Special characters:
  93. "-" A dash between two options causes the second of the two
  94. to be unset (and ignored) if it is given on the command line.
  95. For example:
  96. The du applet has the options "-s" and "-d depth". If
  97. bb_getopt_ulflags finds -s, then -d is unset or if it finds -d
  98. then -s is unset. (Note: busybox implements the GNU
  99. "--max-depth" option as "-d".) To obtain this behavior, you
  100. set bb_opt_complementaly = "s-d:d-s". Only one flag value is
  101. added to bb_getopt_ulflags's return value depending on the
  102. position of the options on the command line. If one of the
  103. two options requires an argument pointer (":" in applet_opts
  104. as in "d:") optarg is set accordingly.
  105. char *smax_print_depth;
  106. bb_opt_complementaly = "s-d:d-s";
  107. opt = bb_getopt_ulflags(argc, argv, "sd:", &smax_print_depth);
  108. if (opt & 2) {
  109. max_print_depth = bb_xgetularg10_bnd(smax_print_depth,
  110. 0, INT_MAX);
  111. }
  112. "~" A tilde between two options, or between an option and a group
  113. of options, means that they are mutually exclusive. Unlike
  114. the "-" case above, an error will be forced if the options
  115. are used together.
  116. For example:
  117. The cut applet must have only one type of list specified, so
  118. -b, -c and -f are mutally exclusive and should raise an error
  119. if specified together. In this case you must set
  120. bb_opt_complementaly = "b~cf:c~bf:f~bc". If two of the
  121. mutually exclusive options are found, bb_getopt_ulflags's
  122. return value will have the error flag set (BB_GETOPT_ERROR) so
  123. that we can check for it:
  124. if (flags & BB_GETOPT_ERROR)
  125. bb_show_usage();
  126. "*" A star after a char in bb_opt_complementaly means that the
  127. option can occur multiple times:
  128. For example:
  129. The grep applet can have one or more "-e pattern" arguments.
  130. In this case you should use bb_getopt_ulflags() as follows:
  131. llist_t *patterns = NULL;
  132. (this pointer must be initializated to NULL if the list is empty
  133. as required by *llist_add_to(llist_t *old_head, char *new_item).)
  134. bb_opt_complementaly = "e*";
  135. bb_getopt_ulflags(argc, argv, "e:", &patterns);
  136. $ grep -e user -e root /etc/passwd
  137. root:x:0:0:root:/root:/bin/bash
  138. user:x:500:500::/home/user:/bin/bash
  139. */
  140. const char *bb_opt_complementaly;
  141. typedef struct {
  142. unsigned char opt;
  143. char list_flg;
  144. unsigned long switch_on;
  145. unsigned long switch_off;
  146. unsigned long incongruously;
  147. void **optarg; /* char **optarg or llist_t **optarg */
  148. } t_complementaly;
  149. /* You can set bb_applet_long_options for parse called long options */
  150. static const struct option bb_default_long_options[] = {
  151. /* { "help", 0, NULL, '?' }, */
  152. { 0, 0, 0, 0 }
  153. };
  154. const struct option *bb_applet_long_options = bb_default_long_options;
  155. unsigned long
  156. bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...)
  157. {
  158. unsigned long flags = 0;
  159. t_complementaly complementaly[sizeof(flags) * 8 + 1];
  160. int c;
  161. const unsigned char *s;
  162. t_complementaly *on_off;
  163. va_list p;
  164. va_start (p, applet_opts);
  165. /* skip GNU extension */
  166. s = applet_opts;
  167. if(*s == '+' || *s == '-')
  168. s++;
  169. c = 0;
  170. on_off = complementaly;
  171. for (; *s; s++) {
  172. if(c >= (sizeof(flags)*8))
  173. break;
  174. on_off->opt = *s;
  175. on_off->switch_on = (1 << c);
  176. on_off->list_flg = 0;
  177. on_off->switch_off = 0;
  178. on_off->incongruously = 0;
  179. on_off->optarg = NULL;
  180. if (s[1] == ':') {
  181. on_off->optarg = va_arg (p, void **);
  182. do
  183. s++;
  184. while (s[1] == ':');
  185. }
  186. on_off++;
  187. c++;
  188. }
  189. on_off->opt = 0;
  190. c = 0;
  191. for (s = bb_opt_complementaly; s && *s; s++) {
  192. t_complementaly *pair;
  193. if (*s == ':') {
  194. c = 0;
  195. continue;
  196. }
  197. if (c)
  198. continue;
  199. for (on_off = complementaly; on_off->opt; on_off++)
  200. if (on_off->opt == *s)
  201. break;
  202. pair = on_off;
  203. for(s++; *s && *s != ':'; s++) {
  204. if (*s == '-' || *s == '~') {
  205. c = *s;
  206. } else if(*s == '*') {
  207. pair->list_flg++;
  208. } else {
  209. unsigned long *pair_switch = &(pair->switch_on);
  210. if(c)
  211. pair_switch = c == '-' ? &(pair->switch_off) : &(pair->incongruously);
  212. for (on_off = complementaly; on_off->opt; on_off++)
  213. if (on_off->opt == *s) {
  214. *pair_switch |= on_off->switch_on;
  215. break;
  216. }
  217. }
  218. }
  219. s--;
  220. }
  221. while ((c = getopt_long (argc, argv, applet_opts,
  222. bb_applet_long_options, NULL)) > 0) {
  223. for (on_off = complementaly; on_off->opt != c; on_off++) {
  224. if(!on_off->opt)
  225. bb_show_usage ();
  226. }
  227. if(flags & on_off->incongruously)
  228. flags |= BB_GETOPT_ERROR;
  229. flags &= ~on_off->switch_off;
  230. flags |= on_off->switch_on;
  231. if(on_off->list_flg) {
  232. *(llist_t **)(on_off->optarg) =
  233. llist_add_to(*(llist_t **)(on_off->optarg), optarg);
  234. } else if (on_off->optarg) {
  235. *(char **)(on_off->optarg) = optarg;
  236. }
  237. }
  238. return flags;
  239. }