diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2012-02-09 18:17:29 +0100 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2012-02-09 18:17:29 +0100 |
commit | 594db1e62a060a0a5646f6840112189fd0ce3b81 (patch) | |
tree | 040b8eacc712ffb622b755cdcf6df055ae3e5fab /util-linux/getopt.c | |
parent | 9106107a509cfb23806e46765ea2704cdd130654 (diff) | |
download | busybox-w32-594db1e62a060a0a5646f6840112189fd0ce3b81.tar.gz busybox-w32-594db1e62a060a0a5646f6840112189fd0ce3b81.tar.bz2 busybox-w32-594db1e62a060a0a5646f6840112189fd0ce3b81.zip |
getopt: simple code shrink; expand help text
function old new delta
packed_usage 28978 29184 +206
getopt_main 656 632 -24
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux/getopt.c')
-rw-r--r-- | util-linux/getopt.c | 52 |
1 files changed, 36 insertions, 16 deletions
diff --git a/util-linux/getopt.c b/util-linux/getopt.c index c45edf8ca..6bad3efc2 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c | |||
@@ -32,30 +32,48 @@ | |||
32 | */ | 32 | */ |
33 | 33 | ||
34 | //usage:#define getopt_trivial_usage | 34 | //usage:#define getopt_trivial_usage |
35 | //usage: "[OPTIONS]" | 35 | //usage: "[OPTIONS] [--] OPTSTRING PARAMS" |
36 | //usage:#define getopt_full_usage "\n\n" | 36 | //usage:#define getopt_full_usage "\n\n" |
37 | //usage: IF_LONG_OPTS( | 37 | //usage: IF_LONG_OPTS( |
38 | //usage: " -a,--alternative Allow long options starting with single -" | 38 | //usage: " -a,--alternative Allow long options starting with single -" |
39 | //usage: "\n -l,--longoptions=longopts Long options to be recognized" | 39 | //usage: "\n -l,--longoptions=LOPT[,...] Long options to be recognized" |
40 | //usage: "\n -n,--name=progname The name under which errors are reported" | 40 | //usage: "\n -n,--name=PROGNAME The name under which errors are reported" |
41 | //usage: "\n -o,--options=optstring Short options to be recognized" | 41 | //usage: "\n -o,--options=OPTSTRING Short options to be recognized" |
42 | //usage: "\n -q,--quiet Disable error reporting by getopt(3)" | 42 | //usage: "\n -q,--quiet Disable error reporting by getopt(3)" |
43 | //usage: "\n -Q,--quiet-output No normal output" | 43 | //usage: "\n -Q,--quiet-output No normal output" |
44 | //usage: "\n -s,--shell=shell Set shell quoting conventions" | 44 | //usage: "\n -s,--shell=SHELL Set shell quoting conventions" |
45 | //usage: "\n -T,--test Test for getopt(1) version" | 45 | //usage: "\n -T,--test Test for getopt(1) version" |
46 | //usage: "\n -u,--unquoted Don't quote the output" | 46 | //usage: "\n -u,--unquoted Don't quote the output" |
47 | //usage: ) | 47 | //usage: ) |
48 | //usage: IF_NOT_LONG_OPTS( | 48 | //usage: IF_NOT_LONG_OPTS( |
49 | //usage: " -a Allow long options starting with single -" | 49 | //usage: " -a Allow long options starting with single -" |
50 | //usage: "\n -l longopts Long options to be recognized" | 50 | //usage: "\n -l LOPT[,...] Long options to be recognized" |
51 | //usage: "\n -n progname The name under which errors are reported" | 51 | //usage: "\n -n PROGNAME The name under which errors are reported" |
52 | //usage: "\n -o optstring Short options to be recognized" | 52 | //usage: "\n -o OPTSTRING Short options to be recognized" |
53 | //usage: "\n -q Disable error reporting by getopt(3)" | 53 | //usage: "\n -q Disable error reporting by getopt(3)" |
54 | //usage: "\n -Q No normal output" | 54 | //usage: "\n -Q No normal output" |
55 | //usage: "\n -s shell Set shell quoting conventions" | 55 | //usage: "\n -s SHELL Set shell quoting conventions" |
56 | //usage: "\n -T Test for getopt(1) version" | 56 | //usage: "\n -T Test for getopt(1) version" |
57 | //usage: "\n -u Don't quote the output" | 57 | //usage: "\n -u Don't quote the output" |
58 | //usage: ) | 58 | //usage: ) |
59 | //usage: "\n" | ||
60 | //usage: "\nExample:" | ||
61 | //usage: "\n" | ||
62 | //usage: "\nO=`getopt -l bb: -- ab:c:: \"$@\"`" | ||
63 | //usage: "\n[ $? = 0 ] || exit 1" | ||
64 | //usage: "\neval set -- \"$O\"" | ||
65 | //usage: "\nwhile true; do" | ||
66 | //usage: "\n case \"$1\" in" | ||
67 | //usage: "\n -a) echo A; shift;;" | ||
68 | //usage: "\n -b|--bb) echo \"B:'$2'\"; shift 2;;" | ||
69 | //usage: "\n -c) case \"$2\" in" | ||
70 | //usage: "\n \"\") echo C; shift 2;;" | ||
71 | //usage: "\n *) echo \"C:'$2'\"; shift 2;;" | ||
72 | //usage: "\n esac;;" | ||
73 | //usage: "\n --) shift; break;;" | ||
74 | //usage: "\n *) echo Error; exit 1;;" | ||
75 | //usage: "\n esac" | ||
76 | //usage: "\ndone" | ||
59 | //usage: | 77 | //usage: |
60 | //usage:#define getopt_example_usage | 78 | //usage:#define getopt_example_usage |
61 | //usage: "$ cat getopt.test\n" | 79 | //usage: "$ cat getopt.test\n" |
@@ -339,6 +357,7 @@ static const char getopt_longopts[] ALIGN1 = | |||
339 | int getopt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 357 | int getopt_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
340 | int getopt_main(int argc, char **argv) | 358 | int getopt_main(int argc, char **argv) |
341 | { | 359 | { |
360 | int n; | ||
342 | char *optstr = NULL; | 361 | char *optstr = NULL; |
343 | char *name = NULL; | 362 | char *name = NULL; |
344 | unsigned opt; | 363 | unsigned opt; |
@@ -351,7 +370,7 @@ int getopt_main(int argc, char **argv) | |||
351 | 370 | ||
352 | compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */ | 371 | compatible = getenv("GETOPT_COMPATIBLE"); /* used as yes/no flag */ |
353 | 372 | ||
354 | if (argc == 1) { | 373 | if (!argv[1]) { |
355 | if (compatible) { | 374 | if (compatible) { |
356 | /* For some reason, the original getopt gave no error | 375 | /* For some reason, the original getopt gave no error |
357 | when there were no arguments. */ | 376 | when there were no arguments. */ |
@@ -362,10 +381,10 @@ int getopt_main(int argc, char **argv) | |||
362 | } | 381 | } |
363 | 382 | ||
364 | if (argv[1][0] != '-' || compatible) { | 383 | if (argv[1][0] != '-' || compatible) { |
365 | char *s; | 384 | char *s = argv[1]; |
366 | 385 | ||
367 | option_mask32 |= OPT_u; /* quoting off */ | 386 | option_mask32 |= OPT_u; /* quoting off */ |
368 | s = xstrdup(argv[1] + strspn(argv[1], "-+")); | 387 | s = xstrdup(s + strspn(s, "-+")); |
369 | argv[1] = argv[0]; | 388 | argv[1] = argv[0]; |
370 | return generate_output(argv+1, argc-1, s, long_options); | 389 | return generate_output(argv+1, argc-1, s, long_options); |
371 | } | 390 | } |
@@ -392,12 +411,13 @@ int getopt_main(int argc, char **argv) | |||
392 | } | 411 | } |
393 | 412 | ||
394 | /* All options controlling the applet have now been parsed */ | 413 | /* All options controlling the applet have now been parsed */ |
414 | n = optind - 1; | ||
395 | if (!optstr) { | 415 | if (!optstr) { |
396 | if (optind >= argc) | 416 | optstr = argv[++n]; |
417 | if (!optstr) | ||
397 | bb_error_msg_and_die("missing optstring argument"); | 418 | bb_error_msg_and_die("missing optstring argument"); |
398 | optstr = argv[optind++]; | ||
399 | } | 419 | } |
400 | 420 | ||
401 | argv[optind-1] = name ? name : argv[0]; | 421 | argv[n] = name ? name : argv[0]; |
402 | return generate_output(argv+optind-1, argc-optind+1, optstr, long_options); | 422 | return generate_output(argv + n, argc - n, optstr, long_options); |
403 | } | 423 | } |