aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 13:56:58 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2006-09-29 13:56:58 +0000
commitf0d6cc8ca97454e64c041c571320d2288d1f2cd9 (patch)
tree79677c5dcb629bfa56f6fd8ea8bfa68d2fdee409
parentb02ef82c99819607df32d3261cef1d674d67893a (diff)
downloadbusybox-w32-f0d6cc8ca97454e64c041c571320d2288d1f2cd9.tar.gz
busybox-w32-f0d6cc8ca97454e64c041c571320d2288d1f2cd9.tar.bz2
busybox-w32-f0d6cc8ca97454e64c041c571320d2288d1f2cd9.zip
getopt_ulflags: fix indentation in comment (needs to be 8 _spaces_
in order to look correct to both 4 tab and 8 tab population). Add comment about :: (which indicates optional argument).
-rw-r--r--libbb/getopt_ulflags.c372
1 files changed, 189 insertions, 183 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c
index 7ad26551a..988f3f9f5 100644
--- a/libbb/getopt_ulflags.c
+++ b/libbb/getopt_ulflags.c
@@ -10,268 +10,274 @@
10#include "libbb.h" 10#include "libbb.h"
11#include <getopt.h> 11#include <getopt.h>
12 12
13/* Documentation 13/* Documentation
14 14
15unsigned long 15unsigned long
16bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...) 16bb_getopt_ulflags(int argc, char **argv, const char *applet_opts, ...)
17 17
18 The command line options must be declared in const char 18 The command line options must be declared in const char
19 *applet_opts as a string of chars, for example: 19 *applet_opts as a string of chars, for example:
20 20
21 flags = bb_getopt_ulflags(argc, argv, "rnug"); 21 flags = bb_getopt_ulflags(argc, argv, "rnug");
22 22
23 If one of the given options is found, a flag value is added to 23 If one of the given options is found, a flag value is added to
24 the return value (an unsigned long). 24 the return value (an unsigned long).
25 25
26 The flag value is determined by the position of the char in 26 The flag value is determined by the position of the char in
27 applet_opts string. For example, in the above case: 27 applet_opts string. For example, in the above case:
28 28
29 flags = bb_getopt_ulflags(argc, argv, "rnug"); 29 flags = bb_getopt_ulflags(argc, argv, "rnug");
30 30
31 "r" will add 1 (bit 0) 31 "r" will add 1 (bit 0)
32 "n" will add 2 (bit 1) 32 "n" will add 2 (bit 1)
33 "u will add 4 (bit 2) 33 "u will add 4 (bit 2)
34 "g" will add 8 (bit 3) 34 "g" will add 8 (bit 3)
35 35
36 and so on. You can also look at the return value as a bit 36 and so on. You can also look at the return value as a bit
37 field and each option sets one bit. 37 field and each option sets one bit.
38 38
39 On exit, global variable optind is set so that if you 39 On exit, global variable optind is set so that if you
40 will do argc -= optind; argv += optind; then 40 will do argc -= optind; argv += optind; then
41 argc will be equal to number of remaining non-option 41 argc will be equal to number of remaining non-option
42 arguments, first one would be in argv[0], next in argv[1] and so on 42 arguments, first one would be in argv[0], next in argv[1] and so on
43 (options and their parameters will be moved into argv[] 43 (options and their parameters will be moved into argv[]
44 positions prior to argv[optind]). 44 positions prior to argv[optind]).
45 45
46 ":" If one of the options requires an argument, then add a ":" 46 ":" If one of the options requires an argument, then add a ":"
47 after the char in applet_opts and provide a pointer to store 47 after the char in applet_opts and provide a pointer to store
48 the argument. For example: 48 the argument. For example:
49 49
50 char *pointer_to_arg_for_a; 50 char *pointer_to_arg_for_a;
51 char *pointer_to_arg_for_b; 51 char *pointer_to_arg_for_b;
52 char *pointer_to_arg_for_c; 52 char *pointer_to_arg_for_c;
53 char *pointer_to_arg_for_d; 53 char *pointer_to_arg_for_d;
54 54
55 flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:", 55 flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:",
56 &pointer_to_arg_for_a, &pointer_to_arg_for_b, 56 &pointer_to_arg_for_a, &pointer_to_arg_for_b,
57 &pointer_to_arg_for_c, &pointer_to_arg_for_d); 57 &pointer_to_arg_for_c, &pointer_to_arg_for_d);
58 58
59 The type of the pointer (char* or llist_t*) may be controlled 59 The type of the pointer (char* or llist_t*) may be controlled
60 by the "::" special separator that is set in the external string 60 by the "::" special separator that is set in the external string
61 bb_opt_complementally (see below for more info). 61 bb_opt_complementally (see below for more info).
62
63 "::" If option can have an *optional* argument, then add a "::"
64 after its char in applet_opts and provide a pointer to store
65 the argument. Note that optional arguments _must_
66 immediately follow the option: -oparam, not -o param.
62 67
63 "+" If the first character in the applet_opts string is a plus, 68 "+" If the first character in the applet_opts string is a plus,
64 then option processing will stop as soon as a non-option is 69 then option processing will stop as soon as a non-option is
65 encountered in the argv array. Useful for applets like env 70 encountered in the argv array. Useful for applets like env
66 which should not process arguments to subprograms: 71 which should not process arguments to subprograms:
67 env -i ls -d / 72 env -i ls -d /
68 Here we want env to process just the '-i', not the '-d'. 73 Here we want env to process just the '-i', not the '-d'.
69 74
70const struct option *bb_applet_long_options 75const struct option *bb_applet_long_options
71 76
72 This struct allows you to define long options. The syntax for 77 This struct allows you to define long options. The syntax for
73 declaring the array is just like that of getopt's longopts. 78 declaring the array is just like that of getopt's longopts.
74 (see getopt(3)) 79 (see getopt(3))
75 80
76 static const struct option applet_long_options[] = { 81 static const struct option applet_long_options[] = {
77 //name,has_arg,flag,val 82 //name,has_arg,flag,val
78 { "verbose", 0, 0, 'v' }, 83 { "verbose", 0, 0, 'v' },
79 { 0, 0, 0, 0 } 84 { 0, 0, 0, 0 }
80 }; 85 };
81 bb_applet_long_options = applet_long_options; 86 bb_applet_long_options = applet_long_options;
82 87
83 The last member of struct option (val) typically is set to 88 The last member of struct option (val) typically is set to
84 matching short option from applet_opts. If there is no matching 89 matching short option from applet_opts. If there is no matching
85 char in applet_opts, then: 90 char in applet_opts, then:
86 - return bit have next position after short options 91 - return bit have next position after short options
87 - if has_arg is not "no_argument", use ptr for arg also 92 - if has_arg is not "no_argument", use ptr for arg also
88 - bb_opt_complementally affects it too 93 - bb_opt_complementally affects it too
89 94
90 Note: a good applet will make long options configurable via the 95 Note: a good applet will make long options configurable via the
91 config process and not a required feature. The current standard 96 config process and not a required feature. The current standard
92 is to name the config option CONFIG_FEATURE_<applet>_LONG_OPTIONS. 97 is to name the config option CONFIG_FEATURE_<applet>_LONG_OPTIONS.
93 98
94const char *bb_opt_complementally 99const char *bb_opt_complementally
95 100
96 this should be bb_opt_complementary, but we'll just keep it as 101 this should be bb_opt_complementary, but we'll just keep it as
97 bb_opt_complementally due to the Russian origins 102 bb_opt_complementally due to the Russian origins
98 103
99 ":" The colon (":") is used to separate groups of two or more chars 104 ":" The colon (":") is used to separate groups of two or more chars
100 and/or groups of chars and special characters (stating some 105 and/or groups of chars and special characters (stating some
101 conditions to be checked). 106 conditions to be checked).
102 107
103 "abc" If groups of two or more chars are specified, the first char 108 "abc" If groups of two or more chars are specified, the first char
104 is the main option and the other chars are secondary options. 109 is the main option and the other chars are secondary options.
105 Their flags will be turned on if the main option is found even 110 Their flags will be turned on if the main option is found even
106 if they are not specifed on the command line. For example: 111 if they are not specifed on the command line. For example:
107 112
108 bb_opt_complementally = "abc"; 113 bb_opt_complementally = "abc";
109 flags = bb_getopt_ulflags(argc, argv, "abcd") 114 flags = bb_getopt_ulflags(argc, argv, "abcd")
110 115
111 If getopt() finds "-a" on the command line, then 116 If getopt() finds "-a" on the command line, then
112 bb_getopt_ulflags's return value will be as if "-a -b -c" were 117 bb_getopt_ulflags's return value will be as if "-a -b -c" were
113 found. 118 found.
114 119
115 "ww" Adjacent double options have a counter associated which indicates 120 "ww" Adjacent double options have a counter associated which indicates
116 the number of occurences of the option. 121 the number of occurences of the option.
117 For example the ps applet needs: 122 For example the ps applet needs:
118 if w is given once, GNU ps sets the width to 132, 123 if w is given once, GNU ps sets the width to 132,
119 if w is given more than once, it is "unlimited" 124 if w is given more than once, it is "unlimited"
120 125
121 int w_counter = 0; 126 int w_counter = 0;
122 bb_opt_complementally = "ww"; 127 bb_opt_complementally = "ww";
123 bb_getopt_ulflags(argc, argv, "w", &w_counter); 128 bb_getopt_ulflags(argc, argv, "w", &w_counter);
124 if (w_counter) 129 if (w_counter)
125 width = (w_counter == 1) ? 132 : INT_MAX; 130 width = (w_counter == 1) ? 132 : INT_MAX;
126 else 131 else
127 get_terminal_width(...&width...); 132 get_terminal_width(...&width...);
128 133
129 w_counter is a pointer to an integer. It has to be passed to 134 w_counter is a pointer to an integer. It has to be passed to
130 bb_getopt_ulflags() after all other option argument sinks. 135 bb_getopt_ulflags() after all other option argument sinks.
131 136
132 For example: accept multiple -v to indicate the level of verbosity 137 For example: accept multiple -v to indicate the level of verbosity
133 and for each -b optarg, add optarg to my_b. Finally, if b is given, 138 and for each -b optarg, add optarg to my_b. Finally, if b is given,
134 turn off c and vice versa: 139 turn off c and vice versa:
135 140
136 llist_t *my_b = NULL; 141 llist_t *my_b = NULL;
137 int verbose_level = 0; 142 int verbose_level = 0;
138 bb_opt_complementally = "vv:b::b-c:c-b"; 143 bb_opt_complementally = "vv:b::b-c:c-b";
139 f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level); 144 f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level);
140 if (f & 2) // -c after -b unsets -b flag 145 if (f & 2) // -c after -b unsets -b flag
141 while (my_b) { dosomething_with(my_b->data); my_b = my_b->link; } 146 while (my_b) { dosomething_with(my_b->data); my_b = my_b->link; }
142 if (my_b) // but llist is stored if -b is specified 147 if (my_b) // but llist is stored if -b is specified
143 free_llist(my_b); 148 free_llist(my_b);
144 if (verbose_level) bb_printf("verbose level is %d\n", verbose_level); 149 if (verbose_level) bb_printf("verbose level is %d\n", verbose_level);
145 150
146Special characters: 151Special characters:
147 152
148 "-" A dash between two options causes the second of the two 153 "-" A dash between two options causes the second of the two
149 to be unset (and ignored) if it is given on the command line. 154 to be unset (and ignored) if it is given on the command line.
150 155
151 [FIXME: what if they are the same? like "x-x"? Is it ever useful?] 156 [FIXME: what if they are the same? like "x-x"? Is it ever useful?]
152 157
153 For example: 158 For example:
154 The du applet has the options "-s" and "-d depth". If 159 The du applet has the options "-s" and "-d depth". If
155 bb_getopt_ulflags finds -s, then -d is unset or if it finds -d 160 bb_getopt_ulflags finds -s, then -d is unset or if it finds -d
156 then -s is unset. (Note: busybox implements the GNU 161 then -s is unset. (Note: busybox implements the GNU
157 "--max-depth" option as "-d".) To obtain this behavior, you 162 "--max-depth" option as "-d".) To obtain this behavior, you
158 set bb_opt_complementally = "s-d:d-s". Only one flag value is 163 set bb_opt_complementally = "s-d:d-s". Only one flag value is
159 added to bb_getopt_ulflags's return value depending on the 164 added to bb_getopt_ulflags's return value depending on the
160 position of the options on the command line. If one of the 165 position of the options on the command line. If one of the
161 two options requires an argument pointer (":" in applet_opts 166 two options requires an argument pointer (":" in applet_opts
162 as in "d:") optarg is set accordingly. 167 as in "d:") optarg is set accordingly.
163 168
164 char *smax_print_depth; 169 char *smax_print_depth;
165 170
166 bb_opt_complementally = "s-d:d-s:x-x"; 171 bb_opt_complementally = "s-d:d-s:x-x";
167 opt = bb_getopt_ulflags(argc, argv, "sd:x", &smax_print_depth); 172 opt = bb_getopt_ulflags(argc, argv, "sd:x", &smax_print_depth);
168 173
169 if (opt & 2) 174 if (opt & 2)
170 max_print_depth = atoi(smax_print_depth); 175 max_print_depth = atoi(smax_print_depth);
171 if (opt & 4) 176 if (opt & 4)
172 printf("Detected odd -x usage\n"); 177 printf("Detected odd -x usage\n");
173 178
174 "-" A dash as the first char in a bb_opt_complementally group forces 179 "-" A dash as the first char in a bb_opt_complementally group forces
175 all arguments to be treated as options, even if they have 180 all arguments to be treated as options, even if they have
176 no leading dashes. Next char in this case can't be a digit (0-9), 181 no leading dashes. Next char in this case can't be a digit (0-9),
177 use ':' or end of line. For example: 182 use ':' or end of line. For example:
178 183
179 bb_opt_complementally = "-:w-x:x-w"; 184 bb_opt_complementally = "-:w-x:x-w";
180 bb_getopt_ulflags(argc, argv, "wx"); 185 bb_getopt_ulflags(argc, argv, "wx");
181 186
182 Allows any arguments to be given without a dash (./program w x) 187 Allows any arguments to be given without a dash (./program w x)
183 as well as with a dash (./program -x). 188 as well as with a dash (./program -x).
184 189
185 "-N" A dash as the first char in a bb_opt_complementally group followed 190 "-N" A dash as the first char in a bb_opt_complementally group followed
186 by a single digit (0-9) means that at least N non-option 191 by a single digit (0-9) means that at least N non-option
187 arguments must be present on the command line 192 arguments must be present on the command line
188 193
189 "V-" An option with dash before colon or end-of-line results in 194 "V-" An option with dash before colon or end-of-line results in
190 bb_show_usage being called if this option is encountered. 195 bb_show_usage being called if this option is encountered.
191 This is typically used to implement "print verbose usage message 196 This is typically used to implement "print verbose usage message
192 and exit" option. 197 and exit" option.
193 198
194 "--" A double dash between two options, or between an option and a group 199 "--" A double dash between two options, or between an option and a group
195 of options, means that they are mutually exclusive. Unlike 200 of options, means that they are mutually exclusive. Unlike
196 the "-" case above, an error will be forced if the options 201 the "-" case above, an error will be forced if the options
197 are used together. 202 are used together.
198 203
199 For example: 204 For example:
200 The cut applet must have only one type of list specified, so 205 The cut applet must have only one type of list specified, so
201 -b, -c and -f are mutally exclusive and should raise an error 206 -b, -c and -f are mutally exclusive and should raise an error
202 if specified together. In this case you must set 207 if specified together. In this case you must set
203 bb_opt_complementally = "b--cf:c--bf:f--bc". If two of the 208 bb_opt_complementally = "b--cf:c--bf:f--bc". If two of the
204 mutually exclusive options are found, bb_getopt_ulflags's 209 mutually exclusive options are found, bb_getopt_ulflags's
205 return value will have the error flag set (BB_GETOPT_ERROR) so 210 return value will have the error flag set (BB_GETOPT_ERROR) so
206 that we can check for it: 211 that we can check for it:
207 212
208 if (flags & BB_GETOPT_ERROR) 213 if (flags & BB_GETOPT_ERROR)
209 bb_show_usage(); 214 bb_show_usage();
210 215
211 "x--x" Variation of the above, it means that -x option should occur 216 "x--x" Variation of the above, it means that -x option should occur
212 at most once. 217 at most once.
213 218
214 "?" A "?" as the first char in a bb_opt_complementally group means: 219 "?" A "?" as the first char in a bb_opt_complementally group means:
215 if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage 220 if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage
216 and exit instead. Next char after '?' can't be a digit. 221 and exit instead. Next char after '?' can't be a digit.
217 222
218 "?N" A "?" as the first char in a bb_opt_complementally group followed 223 "?N" A "?" as the first char in a bb_opt_complementally group followed
219 by a single digit (0-9) means that at most N arguments must be present 224 by a single digit (0-9) means that at most N arguments must be present
220 on the command line. 225 on the command line.
221 226
222 "::" A double colon after a char in bb_opt_complementally means that the 227 "::" A double colon after a char in bb_opt_complementally means that the
223 option can occur multiple times. Each occurrence will be saved as 228 option can occur multiple times. Each occurrence will be saved as
224 a llist_t element instead of char*. 229 a llist_t element instead of char*.
225 230
226 For example: 231 For example:
227 The grep applet can have one or more "-e pattern" arguments. 232 The grep applet can have one or more "-e pattern" arguments.
228 In this case you should use bb_getopt_ulflags() as follows: 233 In this case you should use bb_getopt_ulflags() as follows:
229 234
230 llist_t *patterns = NULL; 235 llist_t *patterns = NULL;
231 236
232 (this pointer must be initializated to NULL if the list is empty 237 (this pointer must be initializated to NULL if the list is empty
233 as required by *llist_add_to(llist_t *old_head, char *new_item).) 238 as required by *llist_add_to(llist_t *old_head, char *new_item).)
234 239
235 bb_opt_complementally = "e::"; 240 bb_opt_complementally = "e::";
236 241
237 bb_getopt_ulflags(argc, argv, "e:", &patterns); 242 bb_getopt_ulflags(argc, argv, "e:", &patterns);
238 $ grep -e user -e root /etc/passwd 243 $ grep -e user -e root /etc/passwd
239 root:x:0:0:root:/root:/bin/bash 244 root:x:0:0:root:/root:/bin/bash
240 user:x:500:500::/home/user:/bin/bash 245 user:x:500:500::/home/user:/bin/bash
241 246
242 "--" A double dash at the beginning of bb_opt_complementally means the 247 "--" A double dash at the beginning of bb_opt_complementally means the
243 argv[1] string should always be treated as options, even if it isn't 248 argv[1] string should always be treated as options, even if it isn't
244 prefixed with a "-". This is useful for special syntax in applets 249 prefixed with a "-". This is useful for special syntax in applets
245 such as "ar" and "tar": 250 such as "ar" and "tar":
246 tar xvf foo.tar 251 tar xvf foo.tar
247 252
248 "?" An "?" between an option and a group of options means that 253 "?" An "?" between an option and a group of options means that
249 at least one of them is required to occur if the first option 254 at least one of them is required to occur if the first option
250 occurs in preceding command line arguments. 255 occurs in preceding command line arguments.
251 256
252 For example from "id" applet: 257 For example from "id" applet:
253 258
254 // Don't allow -n -r -rn -ug -rug -nug -rnug 259 // Don't allow -n -r -rn -ug -rug -nug -rnug
255 bb_opt_complementally = "r?ug:n?ug:?u--g:g--u"; 260 bb_opt_complementally = "r?ug:n?ug:?u--g:g--u";
256 flags = bb_getopt_ulflags(argc, argv, "rnug"); 261 flags = bb_getopt_ulflags(argc, argv, "rnug");
257 262
258 This example allowed only: 263 This example allowed only:
259 $ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng 264 $ id; id -u; id -g; id -ru; id -nu; id -rg; id -ng; id -rnu; id -rng
260 265
261 "X" A bb_opt_complementally group with just a single letter means 266 "X" A bb_opt_complementally group with just a single letter means
262 that this option is required. If more than one such group exists, 267 that this option is required. If more than one such group exists,
263 at least one option is required to occur (not all of them). 268 at least one option is required to occur (not all of them).
264 For example from "start-stop-daemon" applet: 269 For example from "start-stop-daemon" applet:
265 270
266 // Don't allow -KS -SK, but -S or -K is required 271 // Don't allow -KS -SK, but -S or -K is required
267 bb_opt_complementally = "K:S:?K--S:S--K"; 272 bb_opt_complementally = "K:S:?K--S:S--K";
268 flags = bb_getopt_ulflags(argc, argv, "KS...); 273 flags = bb_getopt_ulflags(argc, argv, "KS...);
269 274
270 Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as
271 "?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args;
272 if there is a '-2' option then unset '-3', '-X' and '-a'; if there is
273 a '-2' and after it a '-x' then error out.
274 275
276 Don't forget to use ':'. For example, "?322-22-23X-x-a"
277 is interpreted as "?3:22:-2:2-2:2-3Xa:2--x" -
278 max 3 args; count uses of '-2'; min 2 args; if there is
279 a '-2' option then unset '-3', '-X' and '-a'; if there is
280 a '-2' and after it a '-x' then error out.
275*/ 281*/
276 282
277/* this should be bb_opt_complementary, but we'll just keep it as 283/* this should be bb_opt_complementary, but we'll just keep it as