… | |
… | |
35 | * Version 1.1.0: Tue Jun 30 2000 |
35 | * Version 1.1.0: Tue Jun 30 2000 |
36 | * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz |
36 | * Added NLS support (partly written by Arkadiusz Mi<B6>kiewicz |
37 | * <misiek@pld.org.pl>) |
37 | * <misiek@pld.org.pl>) |
38 | * Version 1.1.4: Mon Nov 7 2005 |
38 | * Version 1.1.4: Mon Nov 7 2005 |
39 | * Fixed a few type's in the manpage |
39 | * Fixed a few type's in the manpage |
|
|
40 | * Version 1.1.5: Mon Nov 7 2005 |
|
|
41 | * Sync with util-linux-2.21, fixed build problems, many new translations |
40 | */ |
42 | */ |
41 | |
43 | |
42 | /* Exit codes: |
44 | /* Exit codes: |
43 | * 0) No errors, succesful operation. |
45 | * 0) No errors, succesful operation. |
44 | * 1) getopt(3) returned an error. |
46 | * 1) getopt(3) returned an error. |
… | |
… | |
62 | #else |
64 | #else |
63 | #include "getopt.h" |
65 | #include "getopt.h" |
64 | #endif |
66 | #endif |
65 | |
67 | |
66 | #include "nls.h" |
68 | #include "nls.h" |
|
|
69 | #include "xalloc.h" |
67 | |
70 | |
68 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
71 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
69 | * mode */ |
72 | * mode */ |
70 | #define NON_OPT 1 |
73 | #define NON_OPT 1 |
71 | /* LONG_OPT is the code that is returned when a long option is found. */ |
74 | /* LONG_OPT is the code that is returned when a long option is found. */ |
… | |
… | |
79 | static shell_t shell = BASH; /* The shell we generate output for. */ |
82 | static shell_t shell = BASH; /* The shell we generate output for. */ |
80 | static int quiet_errors = 0; /* 0 is not quiet. */ |
83 | static int quiet_errors = 0; /* 0 is not quiet. */ |
81 | static int quiet_output = 0; /* 0 is not quiet. */ |
84 | static int quiet_output = 0; /* 0 is not quiet. */ |
82 | static int quote = 1; /* 1 is do quote. */ |
85 | static int quote = 1; /* 1 is do quote. */ |
83 | |
86 | |
|
|
87 | /* Allow changing which getopt is in use with function pointer */ |
84 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
88 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
85 | const struct option * longopts, int *longindex); |
89 | const struct option * longopts, int *longindex); |
86 | |
90 | |
87 | /* Function prototypes */ |
91 | /* Function prototypes */ |
88 | void *our_malloc(size_t size); |
|
|
89 | void *our_realloc(void *ptr, size_t size); |
|
|
90 | static const char *normalize(const char *arg); |
92 | static const char *normalize(const char *arg); |
91 | static int generate_output(char *argv[], int argc, const char *optstr, |
93 | static int generate_output(char *argv[], int argc, const char *optstr, |
92 | const struct option *longopts); |
94 | const struct option *longopts); |
93 | int main(int argc, char *argv[]); |
95 | int main(int argc, char *argv[]); |
94 | static void parse_error(const char *message); |
96 | static void parse_error(const char *message); |
95 | static void add_long_options(char *options); |
97 | static void add_long_options(char *options); |
96 | static void add_longopt(const char *name, int has_arg); |
98 | static void add_longopt(const char *name, int has_arg); |
97 | static void print_help(void); |
99 | static void print_help(void); |
98 | static void set_shell(const char *new_shell); |
100 | static void set_shell(const char *new_shell); |
99 | |
101 | |
100 | void *our_malloc(size_t size) |
|
|
101 | { |
|
|
102 | void *ret=malloc(size); |
|
|
103 | if (! ret) { |
|
|
104 | fprintf(stderr, ("%s: Out of memory!\n"), "getopt"); |
|
|
105 | exit(XALLOC_EXIT_CODE); |
|
|
106 | } |
|
|
107 | return(ret); |
|
|
108 | } |
|
|
109 | |
|
|
110 | void *our_realloc(void *ptr, size_t size) |
|
|
111 | { |
|
|
112 | void *ret=realloc(ptr, size); |
|
|
113 | if (! ret && size) { |
|
|
114 | fprintf(stderr, ("%s: Out of memory!\n"), "getopt"); |
|
|
115 | exit(XALLOC_EXIT_CODE); |
|
|
116 | } |
|
|
117 | return(ret); |
|
|
118 | } |
|
|
119 | |
|
|
120 | /* |
102 | /* |
121 | * This function 'normalizes' a single argument: it puts single quotes |
103 | * This function 'normalizes' a single argument: it puts single quotes |
122 | * around it and escapes other special characters. If quote is false, it |
104 | * around it and escapes other special characters. If quote is false, it |
123 | * just returns its argument. |
105 | * just returns its argument. |
124 | * |
106 | * |
… | |
… | |
130 | { |
112 | { |
131 | static char *BUFFER = NULL; |
113 | static char *BUFFER = NULL; |
132 | const char *argptr = arg; |
114 | const char *argptr = arg; |
133 | char *bufptr; |
115 | char *bufptr; |
134 | |
116 | |
135 | if (BUFFER != NULL) |
|
|
136 | free(BUFFER); |
117 | free(BUFFER); |
137 | |
118 | |
138 | if (!quote) { |
119 | if (!quote) { |
139 | /* Just copy arg */ |
120 | /* Just copy arg */ |
140 | BUFFER = our_malloc(strlen(arg)+1); |
121 | BUFFER = xmalloc(strlen(arg) + 1); |
141 | |
|
|
142 | strcpy(BUFFER, arg); |
122 | strcpy(BUFFER, arg); |
143 | return BUFFER; |
123 | return BUFFER; |
144 | } |
124 | } |
145 | |
125 | |
146 | /* |
126 | /* |
147 | * Each character in arg may take upto four characters in the |
127 | * Each character in arg may take upto four characters in the |
148 | * result: For a quote we need a closing quote, a backslash, a quote |
128 | * result: For a quote we need a closing quote, a backslash, a quote |
149 | * and an opening quote! We need also the global opening and closing |
129 | * and an opening quote! We need also the global opening and closing |
150 | * quote, and one extra character for '\0'. |
130 | * quote, and one extra character for '\0'. |
151 | */ |
131 | */ |
152 | BUFFER = our_malloc(strlen(arg)*4+3); |
132 | BUFFER = xmalloc(strlen(arg) * 4 + 3); |
153 | |
133 | |
154 | bufptr = BUFFER; |
134 | bufptr = BUFFER; |
155 | *bufptr++ = '\''; |
135 | *bufptr++ = '\''; |
156 | |
136 | |
157 | while (*argptr) { |
137 | while (*argptr) { |
… | |
… | |
242 | * we already sent a message, we just exit with a helpful hint. |
222 | * we already sent a message, we just exit with a helpful hint. |
243 | */ |
223 | */ |
244 | static void __attribute__ ((__noreturn__)) parse_error(const char *message) |
224 | static void __attribute__ ((__noreturn__)) parse_error(const char *message) |
245 | { |
225 | { |
246 | if (message) |
226 | if (message) |
247 | fprintf(stderr, "getopt: %s\n", message); |
227 | fprintf(stderr, "%s: %s\n", program_invocation_short_name, message); |
248 | fputs(_("Try `getopt --help' for more information.\n"), stderr); |
228 | fprintf(stderr, _("Try `%s --help' for more information.\n"), |
|
|
229 | program_invocation_short_name); |
249 | exit(PARAMETER_EXIT_CODE); |
230 | exit(PARAMETER_EXIT_CODE); |
250 | } |
231 | } |
251 | |
232 | |
252 | static struct option *long_options = NULL; |
233 | static struct option *long_options = NULL; |
253 | static int long_options_length = 0; /* Length of array */ |
234 | static int long_options_length = 0; /* Length of array */ |
… | |
… | |
267 | long_options_nr = 0; |
248 | long_options_nr = 0; |
268 | } |
249 | } |
269 | |
250 | |
270 | if (long_options_nr == long_options_length) { |
251 | if (long_options_nr == long_options_length) { |
271 | long_options_length += LONG_OPTIONS_INCR; |
252 | long_options_length += LONG_OPTIONS_INCR; |
272 | long_options = our_realloc(long_options, |
253 | long_options = xrealloc(long_options, |
273 | sizeof(struct option) * |
254 | sizeof(struct option) * |
274 | long_options_length); |
255 | long_options_length); |
275 | } |
256 | } |
276 | |
257 | |
277 | long_options[long_options_nr].name = NULL; |
258 | long_options[long_options_nr].name = NULL; |
278 | long_options[long_options_nr].has_arg = 0; |
259 | long_options[long_options_nr].has_arg = 0; |
279 | long_options[long_options_nr].flag = NULL; |
260 | long_options[long_options_nr].flag = NULL; |
… | |
… | |
282 | if (long_options_nr) { |
263 | if (long_options_nr) { |
283 | /* Not for init! */ |
264 | /* Not for init! */ |
284 | long_options[long_options_nr - 1].has_arg = has_arg; |
265 | long_options[long_options_nr - 1].has_arg = has_arg; |
285 | long_options[long_options_nr - 1].flag = NULL; |
266 | long_options[long_options_nr - 1].flag = NULL; |
286 | long_options[long_options_nr - 1].val = LONG_OPT; |
267 | long_options[long_options_nr - 1].val = LONG_OPT; |
287 | tmp = our_malloc(strlen(name) + 1); |
268 | tmp = xmalloc(strlen(name) + 1); |
288 | strcpy(tmp, name); |
269 | strcpy(tmp, name); |
289 | long_options[long_options_nr - 1].name = tmp; |
270 | long_options[long_options_nr - 1].name = tmp; |
290 | } |
271 | } |
291 | long_options_nr++; |
272 | long_options_nr++; |
292 | } |
273 | } |
… | |
… | |
341 | { |
322 | { |
342 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
323 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
343 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
324 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
344 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
325 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
345 | fputs(_(" parameters\n"), stderr); |
326 | fputs(_(" parameters\n"), stderr); |
|
|
327 | fputs(_("\nOptions:\n"), stderr); |
346 | fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); |
328 | fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); |
347 | fputs(_(" -h, --help This small usage guide\n"), stderr); |
329 | fputs(_(" -h, --help This small usage guide\n"), stderr); |
348 | fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); |
330 | fputs(_(" -l, --longoptions <longopts> Long options to be recognized\n"), stderr); |
349 | fputs(_(" -n, --name=progname The name under which errors are reported\n"), stderr); |
331 | fputs(_(" -n, --name <progname> The name under which errors are reported\n"), stderr); |
350 | fputs(_(" -o, --options=optstring Short options to be recognized\n"), stderr); |
332 | fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr); |
351 | fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); |
333 | fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); |
352 | fputs(_(" -Q, --quiet-output No normal output\n"), stderr); |
334 | fputs(_(" -Q, --quiet-output No normal output\n"), stderr); |
353 | fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); |
335 | fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr); |
354 | fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); |
336 | fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); |
355 | fputs(_(" -u, --unqote Do not quote the output\n"), stderr); |
337 | fputs(_(" -u, --unquote Do not quote the output\n"), stderr); |
356 | fputs(_(" -V, --version Output version information\n"), stderr); |
338 | fputs(_(" -V, --version Output version information\n"), stderr); |
|
|
339 | fputc('\n', stderr); |
|
|
340 | |
357 | exit(PARAMETER_EXIT_CODE); |
341 | exit(PARAMETER_EXIT_CODE); |
358 | } |
342 | } |
359 | |
343 | |
360 | int main(int argc, char *argv[]) |
344 | int main(int argc, char *argv[]) |
361 | { |
345 | { |
… | |
… | |
378 | {"alternative", no_argument, NULL, 'a'}, |
362 | {"alternative", no_argument, NULL, 'a'}, |
379 | {"name", required_argument, NULL, 'n'}, |
363 | {"name", required_argument, NULL, 'n'}, |
380 | {"version", no_argument, NULL, 'V'}, |
364 | {"version", no_argument, NULL, 'V'}, |
381 | {NULL, 0, NULL, 0} |
365 | {NULL, 0, NULL, 0} |
382 | }; |
366 | }; |
383 | #if WITHOUT_GETTEXT |
|
|
384 | #else |
|
|
385 | setlocale(LC_ALL, ""); |
367 | setlocale(LC_ALL, ""); |
386 | bindtextdomain(PACKAGE, LOCALEDIR); |
368 | bindtextdomain(PACKAGE, LOCALEDIR); |
387 | textdomain(PACKAGE); |
369 | textdomain(PACKAGE); |
388 | #endif |
|
|
389 | |
370 | |
390 | init_longopt(); |
371 | init_longopt(); |
391 | getopt_long_fp = getopt_long; |
372 | getopt_long_fp = getopt_long; |
392 | |
373 | |
393 | if (getenv("GETOPT_COMPATIBLE")) |
374 | if (getenv("GETOPT_COMPATIBLE")) |
… | |
… | |
406 | } |
387 | } |
407 | |
388 | |
408 | |
389 | |
409 | if (argv[1][0] != '-' || compatible) { |
390 | if (argv[1][0] != '-' || compatible) { |
410 | quote = 0; |
391 | quote = 0; |
411 | optstr = our_malloc(strlen(argv[1]) + 1); |
392 | optstr = xmalloc(strlen(argv[1]) + 1); |
412 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
393 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
413 | argv[1] = argv[0]; |
394 | argv[1] = argv[0]; |
414 | return generate_output(argv + 1, argc - 1, optstr, |
395 | return generate_output(argv + 1, argc - 1, optstr, |
415 | long_options); |
396 | long_options); |
416 | } |
397 | } |
… | |
… | |
422 | getopt_long_fp = getopt_long_only; |
403 | getopt_long_fp = getopt_long_only; |
423 | break; |
404 | break; |
424 | case 'h': |
405 | case 'h': |
425 | print_help(); |
406 | print_help(); |
426 | case 'o': |
407 | case 'o': |
427 | if (optstr) |
|
|
428 | free(optstr); |
408 | free(optstr); |
429 | optstr = our_malloc(strlen(optarg)+1); |
409 | optstr = xmalloc(strlen(optarg) + 1); |
430 | strcpy(optstr, optarg); |
410 | strcpy(optstr, optarg); |
431 | break; |
411 | break; |
432 | case 'l': |
412 | case 'l': |
433 | add_long_options(optarg); |
413 | add_long_options(optarg); |
434 | break; |
414 | break; |
435 | case 'n': |
415 | case 'n': |
436 | if (name) |
|
|
437 | free(name); |
416 | free(name); |
438 | name = our_malloc(strlen(optarg)+1); |
417 | name = xmalloc(strlen(optarg) + 1); |
439 | strcpy(name, optarg); |
418 | strcpy(name, optarg); |
440 | break; |
419 | break; |
441 | case 'q': |
420 | case 'q': |
442 | quiet_errors = 1; |
421 | quiet_errors = 1; |
443 | break; |
422 | break; |
… | |
… | |
451 | return TEST_EXIT_CODE; |
430 | return TEST_EXIT_CODE; |
452 | case 'u': |
431 | case 'u': |
453 | quote = 0; |
432 | quote = 0; |
454 | break; |
433 | break; |
455 | case 'V': |
434 | case 'V': |
456 | printf(_("getopt (enhanced) 1.1.4\n")); |
435 | printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version); |
457 | return EXIT_SUCCESS; |
436 | return EXIT_SUCCESS; |
458 | case '?': |
437 | case '?': |
459 | case ':': |
438 | case ':': |
460 | parse_error(NULL); |
439 | parse_error(NULL); |
461 | default: |
440 | default: |
… | |
… | |
464 | |
443 | |
465 | if (!optstr) { |
444 | if (!optstr) { |
466 | if (optind >= argc) |
445 | if (optind >= argc) |
467 | parse_error(_("missing optstring argument")); |
446 | parse_error(_("missing optstring argument")); |
468 | else { |
447 | else { |
469 | optstr=our_malloc(strlen(argv[optind])+1); |
448 | optstr = xmalloc(strlen(argv[optind]) + 1); |
470 | strcpy(optstr, argv[optind]); |
449 | strcpy(optstr, argv[optind]); |
471 | optind++; |
450 | optind++; |
472 | } |
451 | } |
473 | } |
452 | } |
474 | if (name) |
453 | if (name) |