diff options
-rw-r--r-- | util-linux/getopt.c | 59 |
1 files changed, 32 insertions, 27 deletions
diff --git a/util-linux/getopt.c b/util-linux/getopt.c index 1ae0c59db..58df1c823 100644 --- a/util-linux/getopt.c +++ b/util-linux/getopt.c | |||
@@ -35,27 +35,32 @@ | |||
35 | //usage: "[OPTIONS] [--] OPTSTRING PARAMS" | 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: IF_FEATURE_GETOPT_LONG( |
39 | //usage: "\n -l,--longoptions=LOPT[,...] Long options to be recognized" | 39 | //usage: " -a,--alternative Allow long options starting with single -\n" |
40 | //usage: "\n -n,--name=PROGNAME The name under which errors are reported" | 40 | //usage: " -l,--longoptions=LOPT[,...] Long options to recognize\n" |
41 | //usage: "\n -o,--options=OPTSTRING Short options to be recognized" | 41 | //usage: ) |
42 | //usage: "\n -q,--quiet Disable error reporting by getopt(3)" | 42 | //usage: " -n,--name=PROGNAME The name under which errors are reported" |
43 | //usage: "\n -o,--options=OPTSTRING Short options to recognize" | ||
44 | //usage: "\n -q,--quiet No error messages on unrecognized options" | ||
43 | //usage: "\n -Q,--quiet-output No normal output" | 45 | //usage: "\n -Q,--quiet-output No normal output" |
44 | //usage: "\n -s,--shell=SHELL Set shell quoting conventions" | 46 | //usage: "\n -s,--shell=SHELL Set shell quoting conventions" |
45 | //usage: "\n -T,--test Test for getopt(1) version" | 47 | //usage: "\n -T,--test Version test (exits with 4)" |
46 | //usage: "\n -u,--unquoted Don't quote the output" | 48 | //usage: "\n -u,--unquoted Don't quote output" |
47 | //usage: ) | 49 | //usage: ) |
48 | //usage: IF_NOT_LONG_OPTS( | 50 | //usage: IF_NOT_LONG_OPTS( |
49 | //usage: " -a Allow long options starting with single -" | 51 | //usage: IF_FEATURE_GETOPT_LONG( |
50 | //usage: "\n -l LOPT[,...] Long options to be recognized" | 52 | //usage: " -a Allow long options starting with single -\n" |
51 | //usage: "\n -n PROGNAME The name under which errors are reported" | 53 | //usage: " -l LOPT[,...] Long options to recognize\n" |
52 | //usage: "\n -o OPTSTRING Short options to be recognized" | 54 | //usage: ) |
53 | //usage: "\n -q Disable error reporting by getopt(3)" | 55 | //usage: " -n PROGNAME The name under which errors are reported" |
56 | //usage: "\n -o OPTSTRING Short options to recognize" | ||
57 | //usage: "\n -q No error messages on unrecognized options" | ||
54 | //usage: "\n -Q No normal output" | 58 | //usage: "\n -Q No normal output" |
55 | //usage: "\n -s SHELL Set shell quoting conventions" | 59 | //usage: "\n -s SHELL Set shell quoting conventions" |
56 | //usage: "\n -T Test for getopt(1) version" | 60 | //usage: "\n -T Version test (exits with 4)" |
57 | //usage: "\n -u Don't quote the output" | 61 | //usage: "\n -u Don't quote output" |
58 | //usage: ) | 62 | //usage: ) |
63 | //usage: IF_FEATURE_GETOPT_LONG( /* example uses -l, needs FEATURE_GETOPT_LONG */ | ||
59 | //usage: "\n" | 64 | //usage: "\n" |
60 | //usage: "\nExample:" | 65 | //usage: "\nExample:" |
61 | //usage: "\n" | 66 | //usage: "\n" |
@@ -73,6 +78,7 @@ | |||
73 | //usage: "\n *) echo Error; exit 1;;" | 78 | //usage: "\n *) echo Error; exit 1;;" |
74 | //usage: "\n esac" | 79 | //usage: "\n esac" |
75 | //usage: "\ndone" | 80 | //usage: "\ndone" |
81 | //usage: ) | ||
76 | //usage: | 82 | //usage: |
77 | //usage:#define getopt_example_usage | 83 | //usage:#define getopt_example_usage |
78 | //usage: "$ cat getopt.test\n" | 84 | //usage: "$ cat getopt.test\n" |
@@ -214,11 +220,6 @@ static const char *normalize(const char *arg) | |||
214 | static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts) | 220 | static int generate_output(char **argv, int argc, const char *optstr, const struct option *longopts) |
215 | { | 221 | { |
216 | int exit_code = 0; /* We assume everything will be OK */ | 222 | int exit_code = 0; /* We assume everything will be OK */ |
217 | int opt; | ||
218 | #if ENABLE_FEATURE_GETOPT_LONG | ||
219 | int longindex; | ||
220 | #endif | ||
221 | const char *charptr; | ||
222 | 223 | ||
223 | if (quiet_errors) /* No error reporting from getopt(3) */ | 224 | if (quiet_errors) /* No error reporting from getopt(3) */ |
224 | opterr = 0; | 225 | opterr = 0; |
@@ -233,13 +234,14 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru | |||
233 | #endif | 234 | #endif |
234 | 235 | ||
235 | while (1) { | 236 | while (1) { |
236 | opt = | ||
237 | #if ENABLE_FEATURE_GETOPT_LONG | 237 | #if ENABLE_FEATURE_GETOPT_LONG |
238 | alternative ? | 238 | int longindex; |
239 | getopt_long_only(argc, argv, optstr, longopts, &longindex) : | 239 | int opt = alternative |
240 | getopt_long(argc, argv, optstr, longopts, &longindex); | 240 | ? getopt_long_only(argc, argv, optstr, longopts, &longindex) |
241 | : getopt_long(argc, argv, optstr, longopts, &longindex) | ||
242 | ; | ||
241 | #else | 243 | #else |
242 | getopt(argc, argv, optstr); | 244 | int opt = getopt(argc, argv, optstr); |
243 | #endif | 245 | #endif |
244 | if (opt == -1) | 246 | if (opt == -1) |
245 | break; | 247 | break; |
@@ -257,9 +259,10 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru | |||
257 | if (opt == NON_OPT) | 259 | if (opt == NON_OPT) |
258 | printf(" %s", normalize(optarg)); | 260 | printf(" %s", normalize(optarg)); |
259 | else { | 261 | else { |
262 | const char *charptr; | ||
260 | printf(" -%c", opt); | 263 | printf(" -%c", opt); |
261 | charptr = strchr(optstr, opt); | 264 | charptr = strchr(optstr, opt); |
262 | if (charptr != NULL && *++charptr == ':') | 265 | if (charptr && *++charptr == ':') |
263 | printf(" %s", | 266 | printf(" %s", |
264 | normalize(optarg ? optarg : "")); | 267 | normalize(optarg ? optarg : "")); |
265 | } | 268 | } |
@@ -267,9 +270,11 @@ static int generate_output(char **argv, int argc, const char *optstr, const stru | |||
267 | } | 270 | } |
268 | 271 | ||
269 | if (!quiet_output) { | 272 | if (!quiet_output) { |
273 | unsigned idx; | ||
270 | printf(" --"); | 274 | printf(" --"); |
271 | while (optind < argc) | 275 | idx = optind; |
272 | printf(" %s", normalize(argv[optind++])); | 276 | while (argv[idx]) |
277 | printf(" %s", normalize(argv[idx++])); | ||
273 | bb_putchar('\n'); | 278 | bb_putchar('\n'); |
274 | } | 279 | } |
275 | return exit_code; | 280 | return exit_code; |