diff options
| author | Rob Landley <rob@landley.net> | 2006-05-03 21:23:15 +0000 |
|---|---|---|
| committer | Rob Landley <rob@landley.net> | 2006-05-03 21:23:15 +0000 |
| commit | f76cd964ec6531ddb7a1094f2e2a8d9629c7fef3 (patch) | |
| tree | bd2a6678c9fcda7b8fc20f2b950d57a76aa71607 | |
| parent | 425e7584a48575435bff7c702bddb8461f90a095 (diff) | |
| download | busybox-w32-f76cd964ec6531ddb7a1094f2e2a8d9629c7fef3.tar.gz busybox-w32-f76cd964ec6531ddb7a1094f2e2a8d9629c7fef3.tar.bz2 busybox-w32-f76cd964ec6531ddb7a1094f2e2a8d9629c7fef3.zip | |
Whitespace and documentation cleanup from Dennis Vlasenko.
| -rw-r--r-- | libbb/getopt_ulflags.c | 113 |
1 files changed, 60 insertions, 53 deletions
diff --git a/libbb/getopt_ulflags.c b/libbb/getopt_ulflags.c index 199147b76..76bdeed75 100644 --- a/libbb/getopt_ulflags.c +++ b/libbb/getopt_ulflags.c | |||
| @@ -26,7 +26,7 @@ | |||
| 26 | #include <stdlib.h> | 26 | #include <stdlib.h> |
| 27 | #include "libbb.h" | 27 | #include "libbb.h" |
| 28 | 28 | ||
| 29 | /* Documentation ! | 29 | /* Documentation |
| 30 | 30 | ||
| 31 | unsigned long | 31 | unsigned long |
| 32 | bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) | 32 | bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) |
| @@ -44,13 +44,13 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) | |||
| 44 | 44 | ||
| 45 | flags = bb_getopt_ulflags(argc, argv, "rnug"); | 45 | flags = bb_getopt_ulflags(argc, argv, "rnug"); |
| 46 | 46 | ||
| 47 | "r" will add 1 (bit 1 : 0x01) | 47 | "r" will add 1 (bit 0) |
| 48 | "n" will add 2 (bit 2 : 0x02) | 48 | "n" will add 2 (bit 1) |
| 49 | "u will add 4 (bit 3 : 0x03) | 49 | "u will add 4 (bit 2) |
| 50 | "g" will add 8 (bit 4 : 0x04) | 50 | "g" will add 8 (bit 3) |
| 51 | 51 | ||
| 52 | and so on. You can also look at the return value as a bit | 52 | and so on. You can also look at the return value as a bit |
| 53 | field and each option sets one of bits. | 53 | field and each option sets one bit. |
| 54 | 54 | ||
| 55 | ":" If one of the options requires an argument, then add a ":" | 55 | ":" If one of the options requires an argument, then add a ":" |
| 56 | after the char in applet_opts and provide a pointer to store | 56 | after the char in applet_opts and provide a pointer to store |
| @@ -62,10 +62,10 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) | |||
| 62 | char *pointer_to_arg_for_d; | 62 | char *pointer_to_arg_for_d; |
| 63 | 63 | ||
| 64 | flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:", | 64 | flags = bb_getopt_ulflags(argc, argv, "a:b:c:d:", |
| 65 | &pointer_to_arg_for_a, &pointer_to_arg_for_b, | 65 | &pointer_to_arg_for_a, &pointer_to_arg_for_b, |
| 66 | &pointer_to_arg_for_c, &pointer_to_arg_for_d); | 66 | &pointer_to_arg_for_c, &pointer_to_arg_for_d); |
| 67 | 67 | ||
| 68 | The type of the pointer (char* or llist_t *) may be controlled | 68 | The type of the pointer (char* or llist_t*) may be controlled |
| 69 | by the "::" special separator that is set in the external string | 69 | by the "::" special separator that is set in the external string |
| 70 | bb_opt_complementally (see below for more info). | 70 | bb_opt_complementally (see below for more info). |
| 71 | 71 | ||
| @@ -76,23 +76,24 @@ bb_getopt_ulflags (int argc, char **argv, const char *applet_opts, ...) | |||
| 76 | env -i ls -d / | 76 | env -i ls -d / |
| 77 | Here we want env to process just the '-i', not the '-d'. | 77 | Here we want env to process just the '-i', not the '-d'. |
| 78 | 78 | ||
| 79 | static const struct option bb_default_long_options[] | 79 | const struct option *bb_applet_long_options |
| 80 | 80 | ||
| 81 | This struct allows you to define long options. The syntax for | 81 | This struct allows you to define long options. The syntax for |
| 82 | declaring the array is just like that of getopt's longopts. | 82 | declaring the array is just like that of getopt's longopts. |
| 83 | (see getopt(3)) | 83 | (see getopt(3)) |
| 84 | 84 | ||
| 85 | static const struct option applet_long_options[] = { | 85 | static const struct option applet_long_options[] = { |
| 86 | { "verbose", 0, 0, v }, | 86 | { "verbose", 0, 0, 'v' }, |
| 87 | { 0, 0, 0, 0 } | 87 | { 0, 0, 0, 0 } |
| 88 | }; | 88 | }; |
| 89 | bb_applet_long_options = applet_long_options; | 89 | bb_applet_long_options = applet_long_options; |
| 90 | 90 | ||
| 91 | The last argument (val) can undefined from applet_opts. | 91 | The last member of struct option (val) typically is set to |
| 92 | If you use this, then: | 92 | matching short option from applet_opts. If there is no matching |
| 93 | char in applet_opts, then: | ||
| 93 | - return bit have next position after short options | 94 | - return bit have next position after short options |
| 94 | - if has_arg is not "no_argument", use ptr for arg also | 95 | - if has_arg is not "no_argument", use ptr for arg also |
| 95 | - bb_opt_complementally have effects for this too | 96 | - bb_opt_complementally affects it too |
| 96 | 97 | ||
| 97 | Note: a good applet will make long options configurable via the | 98 | Note: a good applet will make long options configurable via the |
| 98 | config process and not a required feature. The current standard | 99 | config process and not a required feature. The current standard |
| @@ -120,7 +121,7 @@ const char *bb_opt_complementally | |||
| 120 | found. | 121 | found. |
| 121 | 122 | ||
| 122 | "ww" Adjacent double options have a counter associated which indicates | 123 | "ww" Adjacent double options have a counter associated which indicates |
| 123 | the number of occurances of the option. | 124 | the number of occurences of the option. |
| 124 | For example the ps applet needs: | 125 | For example the ps applet needs: |
| 125 | if w is given once, GNU ps sets the width to 132, | 126 | if w is given once, GNU ps sets the width to 132, |
| 126 | if w is given more than once, it is "unlimited" | 127 | if w is given more than once, it is "unlimited" |
| @@ -144,17 +145,18 @@ const char *bb_opt_complementally | |||
| 144 | int verbose_level = 0; | 145 | int verbose_level = 0; |
| 145 | bb_opt_complementally = "vv:b::b-c:c-b"; | 146 | bb_opt_complementally = "vv:b::b-c:c-b"; |
| 146 | f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level); | 147 | f = bb_getopt_ulflags(argc, argv, "vb:c", &my_b, &verbose_level); |
| 147 | if((f & 2)) // -c after -b unset this -b flag | 148 | if((f & 2)) // -c after -b unsets -b flag |
| 148 | while (my_b) { dosomething_with(my_b->data) ; my_b = my_b->link; } | 149 | while(my_b) { dosomething_with(my_b->data) ; my_b = my_b->link; } |
| 149 | if(my_b) // but llist stored always if -b found | 150 | if(my_b) // but llist is stored if -b is specified |
| 150 | free_llist(my_b); | 151 | free_llist(my_b); |
| 151 | if (verbose_level) bb_printf("verbose level is %d\n", verbose_level); | 152 | if(verbose_level) bb_printf("verbose level is %d\n", verbose_level); |
| 152 | 153 | ||
| 153 | Special characters: | 154 | Special characters: |
| 154 | 155 | ||
| 155 | "-" A dash between two options causes the second of the two | 156 | "-" A dash between two options causes the second of the two |
| 156 | to be unset (and ignored or triggered) if it is given on | 157 | to be unset (and ignored) if it is given on the command line. |
| 157 | the command line. | 158 | |
| 159 | [FIXME: what if they are the same? like "x-x"? Is it ever useful?] | ||
| 158 | 160 | ||
| 159 | For example: | 161 | For example: |
| 160 | The du applet has the options "-s" and "-d depth". If | 162 | The du applet has the options "-s" and "-d depth". If |
| @@ -172,28 +174,30 @@ Special characters: | |||
| 172 | bb_opt_complementally = "s-d:d-s:x-x"; | 174 | bb_opt_complementally = "s-d:d-s:x-x"; |
| 173 | opt = bb_getopt_ulflags(argc, argv, "sd:x", &smax_print_depth); | 175 | opt = bb_getopt_ulflags(argc, argv, "sd:x", &smax_print_depth); |
| 174 | 176 | ||
| 175 | if (opt & 2) { | 177 | if (opt & 2) |
| 176 | max_print_depth = atoi(smax_print_depth); | 178 | max_print_depth = atoi(smax_print_depth); |
| 177 | } | 179 | if (opt & 4) |
| 178 | if(opt & 4) | 180 | printf("Detected odd -x usage\n"); |
| 179 | printf("Detected odd -x usaging\n"); | ||
| 180 | 181 | ||
| 181 | "-" A dash as the first char in a bb_opt_complementally group means to | 182 | "-" A dash as the first char in a bb_opt_complementally group forces |
| 182 | convert the arguments as option. Next char for this case can't set | 183 | all arguments to be treated as options, even if they have |
| 183 | [0-9], recomended use ':' or end of line. For example: | 184 | no leading dashes. Next char in this case can't be a digit (0-9), |
| 185 | use ':' or end of line. For example: | ||
| 184 | 186 | ||
| 185 | bb_opt_complementally = "-:w-x:x-w"; | 187 | bb_opt_complementally = "-:w-x:x-w"; |
| 186 | bb_getopt_ulflags(argc, argv, "wx"); | 188 | bb_getopt_ulflags(argc, argv, "wx"); |
| 187 | 189 | ||
| 188 | Allows any arguments to be given without a dash (./program w x) | 190 | Allows any arguments to be given without a dash (./program w x) |
| 189 | as well as with a dash (./program -x). Why unset -w see above. | 191 | as well as with a dash (./program -x). |
| 190 | 192 | ||
| 191 | "-N" A dash as the first char in a bb_opt_complementally group with | 193 | "-N" A dash as the first char in a bb_opt_complementally group followed |
| 192 | number 0-9 as one char is means check minimal arguments required. | 194 | by a single digit (0-9) means that at least N non-option |
| 195 | arguments must be present on the command line | ||
| 193 | 196 | ||
| 194 | "V-" A option with dash before colon or end line indicate: call | 197 | "V-" An option with dash before colon or end-of-line results in |
| 195 | bb_show_usage if this option give, for example verbose | 198 | bb_show_usage being called if this option is encountered. |
| 196 | usage option. | 199 | This is typically used to implement "print verbose usage message |
| 200 | and exit" option. | ||
| 197 | 201 | ||
| 198 | "--" A double dash between two options, or between an option and a group | 202 | "--" A double dash between two options, or between an option and a group |
| 199 | of options, means that they are mutually exclusive. Unlike | 203 | of options, means that they are mutually exclusive. Unlike |
| @@ -212,16 +216,17 @@ Special characters: | |||
| 212 | if (flags & BB_GETOPT_ERROR) | 216 | if (flags & BB_GETOPT_ERROR) |
| 213 | bb_show_usage(); | 217 | bb_show_usage(); |
| 214 | 218 | ||
| 215 | "?" A "ask" as the first char in a bb_opt_complementally group give: | 219 | "?" A "?" as the first char in a bb_opt_complementally group means: |
| 216 | if previous point set BB_GETOPT_ERROR, don't return and | 220 | if BB_GETOPT_ERROR is detected, don't return, call bb_show_usage |
| 217 | call previous example internally. Next char for this case can't | 221 | and exit instead. Next char after '?' can't be a digit. |
| 218 | set to [0-9], recomended use ':' or end of line. | ||
| 219 | 222 | ||
| 220 | "?N" A "ask" as the first char in a bb_opt_complementally group with | 223 | "?N" A "?" as the first char in a bb_opt_complementally group followed |
| 221 | number 0-9 as one char is means check maximal arguments possible. | 224 | by a single digit (0-9) means that at most N arguments must be present |
| 225 | on the command line. | ||
| 222 | 226 | ||
| 223 | "::" 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 |
| 224 | option can occur multiple times: | 228 | option can occur multiple times. Each occurrence will be saved as |
| 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. |
| @@ -245,8 +250,10 @@ Special characters: | |||
| 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 "ask" between main and group options causes the second of the two | 253 | "?" An "?" between an option and a group of options means that |
| 249 | to be depending required as or if first is given on the command line. | 254 | at least one of them is required to occur if the first option |
| 255 | occurs in preceding command line arguments. | ||
| 256 | |||
| 250 | For example from "id" applet: | 257 | For example from "id" applet: |
| 251 | 258 | ||
| 252 | // Don't allow -n -r -rn -ug -rug -nug -rnug | 259 | // Don't allow -n -r -rn -ug -rug -nug -rnug |
| @@ -256,22 +263,22 @@ Special characters: | |||
| 256 | This example allowed only: | 263 | This example allowed only: |
| 257 | $ 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 |
| 258 | 265 | ||
| 259 | "X" A one options in bb_opt_complementally group means | 266 | "X" A bb_opt_complementally group with just a single letter means |
| 260 | requires this option always with "or" logic if more one specified, | 267 | that this this option is required. If more than one such group exists, |
| 261 | checked after switch off from complementally logic. | 268 | at least one option is required to occur (not all of them). |
| 262 | For example from "start-stop-daemon" applet: | 269 | For example from "start-stop-daemon" applet: |
| 263 | 270 | ||
| 264 | // Don't allow -KS -SK, but -S or -K required | 271 | // Don't allow -KS -SK, but -S or -K is required |
| 265 | bb_opt_complementally = "K:S:?K--S:S--K"; | 272 | bb_opt_complementally = "K:S:?K--S:S--K"; |
| 266 | flags = bb_getopt_ulflags(argc, argv, "KS...); | 273 | flags = bb_getopt_ulflags(argc, argv, "KS...); |
| 267 | 274 | ||
| 268 | 275 | ||
| 269 | "x--x" give error if double or more used -x option | 276 | "x--x" give error if double or more used -x option |
| 270 | 277 | ||
| 271 | Don't forget ':' store. For example "?322-22-23X-x-a" interpretet as | 278 | Don't forget to use ':'. For example "?322-22-23X-x-a" is interpreted as |
| 272 | "?3:22:-2:2-2:2-3Xa:2--x": max args is 3, count -2 usaged, min args is 2, | 279 | "?3:22:-2:2-2:2-3Xa:2--x": max 3 args; count uses of '-2'; min 2 args; |
| 273 | -2 option triggered, unset -3 and -X and -a if -2 any usaged, give error if | 280 | if there is a '-2' option then unset '-3', '-X' and '-a'; if there is |
| 274 | after -2 the -x option usaged. | 281 | a '-2' and after it a '-x' then error out. |
| 275 | 282 | ||
| 276 | */ | 283 | */ |
| 277 | 284 | ||
