… | |
… | |
62 | #else |
62 | #else |
63 | #include "getopt.h" |
63 | #include "getopt.h" |
64 | #endif |
64 | #endif |
65 | |
65 | |
66 | #include "nls.h" |
66 | #include "nls.h" |
|
|
67 | #include "xalloc.h" |
67 | |
68 | |
68 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
69 | /* NON_OPT is the code that is returned when a non-option is found in '+' |
69 | * mode */ |
70 | * mode */ |
70 | #define NON_OPT 1 |
71 | #define NON_OPT 1 |
71 | /* LONG_OPT is the code that is returned when a long option is found. */ |
72 | /* 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. */ |
80 | static shell_t shell = BASH; /* The shell we generate output for. */ |
80 | static int quiet_errors = 0; /* 0 is not quiet. */ |
81 | static int quiet_errors = 0; /* 0 is not quiet. */ |
81 | static int quiet_output = 0; /* 0 is not quiet. */ |
82 | static int quiet_output = 0; /* 0 is not quiet. */ |
82 | static int quote = 1; /* 1 is do quote. */ |
83 | static int quote = 1; /* 1 is do quote. */ |
83 | |
84 | |
|
|
85 | /* Allow changing which getopt is in use with function pointer */ |
84 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
86 | int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, |
85 | const struct option * longopts, int *longindex); |
87 | const struct option * longopts, int *longindex); |
86 | |
88 | |
87 | /* Function prototypes */ |
89 | /* 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); |
90 | static const char *normalize(const char *arg); |
91 | static int generate_output(char *argv[], int argc, const char *optstr, |
91 | static int generate_output(char *argv[], int argc, const char *optstr, |
92 | const struct option *longopts); |
92 | const struct option *longopts); |
93 | int main(int argc, char *argv[]); |
93 | int main(int argc, char *argv[]); |
94 | static void parse_error(const char *message); |
94 | static void parse_error(const char *message); |
95 | static void add_long_options(char *options); |
95 | static void add_long_options(char *options); |
96 | static void add_longopt(const char *name, int has_arg); |
96 | static void add_longopt(const char *name, int has_arg); |
97 | static void print_help(void); |
97 | static void print_help(void); |
98 | static void set_shell(const char *new_shell); |
98 | static void set_shell(const char *new_shell); |
99 | |
99 | |
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 | /* |
100 | /* |
121 | * This function 'normalizes' a single argument: it puts single quotes |
101 | * This function 'normalizes' a single argument: it puts single quotes |
122 | * around it and escapes other special characters. If quote is false, it |
102 | * around it and escapes other special characters. If quote is false, it |
123 | * just returns its argument. |
103 | * just returns its argument. |
124 | * |
104 | * |
… | |
… | |
130 | { |
110 | { |
131 | static char *BUFFER = NULL; |
111 | static char *BUFFER = NULL; |
132 | const char *argptr = arg; |
112 | const char *argptr = arg; |
133 | char *bufptr; |
113 | char *bufptr; |
134 | |
114 | |
135 | if (BUFFER != NULL) |
|
|
136 | free(BUFFER); |
115 | free(BUFFER); |
137 | |
116 | |
138 | if (!quote) { |
117 | if (!quote) { |
139 | /* Just copy arg */ |
118 | /* Just copy arg */ |
140 | BUFFER = our_malloc(strlen(arg)+1); |
119 | BUFFER = xmalloc(strlen(arg) + 1); |
141 | |
|
|
142 | strcpy(BUFFER, arg); |
120 | strcpy(BUFFER, arg); |
143 | return BUFFER; |
121 | return BUFFER; |
144 | } |
122 | } |
145 | |
123 | |
146 | /* |
124 | /* |
147 | * Each character in arg may take upto four characters in the |
125 | * 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 |
126 | * 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 |
127 | * and an opening quote! We need also the global opening and closing |
150 | * quote, and one extra character for '\0'. |
128 | * quote, and one extra character for '\0'. |
151 | */ |
129 | */ |
152 | BUFFER = our_malloc(strlen(arg)*4+3); |
130 | BUFFER = xmalloc(strlen(arg) * 4 + 3); |
153 | |
131 | |
154 | bufptr = BUFFER; |
132 | bufptr = BUFFER; |
155 | *bufptr++ = '\''; |
133 | *bufptr++ = '\''; |
156 | |
134 | |
157 | while (*argptr) { |
135 | while (*argptr) { |
… | |
… | |
242 | * we already sent a message, we just exit with a helpful hint. |
220 | * we already sent a message, we just exit with a helpful hint. |
243 | */ |
221 | */ |
244 | static void __attribute__ ((__noreturn__)) parse_error(const char *message) |
222 | static void __attribute__ ((__noreturn__)) parse_error(const char *message) |
245 | { |
223 | { |
246 | if (message) |
224 | if (message) |
247 | fprintf(stderr, "getopt: %s\n", message); |
225 | fprintf(stderr, "%s: %s\n", ,program_short_invocation_name, message); |
248 | fputs(_("Try `getopt --help' for more information.\n"), stderr); |
226 | fprintf(stderr, _("Try `%s --help' for more information.\n"), |
|
|
227 | program_invocation_short_name); |
249 | exit(PARAMETER_EXIT_CODE); |
228 | exit(PARAMETER_EXIT_CODE); |
250 | } |
229 | } |
251 | |
230 | |
252 | static struct option *long_options = NULL; |
231 | static struct option *long_options = NULL; |
253 | static int long_options_length = 0; /* Length of array */ |
232 | static int long_options_length = 0; /* Length of array */ |
… | |
… | |
267 | long_options_nr = 0; |
246 | long_options_nr = 0; |
268 | } |
247 | } |
269 | |
248 | |
270 | if (long_options_nr == long_options_length) { |
249 | if (long_options_nr == long_options_length) { |
271 | long_options_length += LONG_OPTIONS_INCR; |
250 | long_options_length += LONG_OPTIONS_INCR; |
272 | long_options = our_realloc(long_options, |
251 | long_options = xrealloc(long_options, |
273 | sizeof(struct option) * |
252 | sizeof(struct option) * |
274 | long_options_length); |
253 | long_options_length); |
275 | } |
254 | } |
276 | |
255 | |
277 | long_options[long_options_nr].name = NULL; |
256 | long_options[long_options_nr].name = NULL; |
278 | long_options[long_options_nr].has_arg = 0; |
257 | long_options[long_options_nr].has_arg = 0; |
279 | long_options[long_options_nr].flag = NULL; |
258 | long_options[long_options_nr].flag = NULL; |
… | |
… | |
282 | if (long_options_nr) { |
261 | if (long_options_nr) { |
283 | /* Not for init! */ |
262 | /* Not for init! */ |
284 | long_options[long_options_nr - 1].has_arg = has_arg; |
263 | long_options[long_options_nr - 1].has_arg = has_arg; |
285 | long_options[long_options_nr - 1].flag = NULL; |
264 | long_options[long_options_nr - 1].flag = NULL; |
286 | long_options[long_options_nr - 1].val = LONG_OPT; |
265 | long_options[long_options_nr - 1].val = LONG_OPT; |
287 | tmp = our_malloc(strlen(name) + 1); |
266 | tmp = xmalloc(strlen(name) + 1); |
288 | strcpy(tmp, name); |
267 | strcpy(tmp, name); |
289 | long_options[long_options_nr - 1].name = tmp; |
268 | long_options[long_options_nr - 1].name = tmp; |
290 | } |
269 | } |
291 | long_options_nr++; |
270 | long_options_nr++; |
292 | } |
271 | } |
… | |
… | |
341 | { |
320 | { |
342 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
321 | fputs(_("Usage: getopt optstring parameters\n"), stderr); |
343 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
322 | fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); |
344 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
323 | fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); |
345 | fputs(_(" parameters\n"), stderr); |
324 | fputs(_(" parameters\n"), stderr); |
|
|
325 | fputs(_("\nOptions:\n"), stderr); |
346 | fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); |
326 | fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); |
347 | fputs(_(" -h, --help This small usage guide\n"), stderr); |
327 | fputs(_(" -h, --help This small usage guide\n"), stderr); |
348 | fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); |
328 | 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); |
329 | 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); |
330 | fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr); |
351 | fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); |
331 | fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); |
352 | fputs(_(" -Q, --quiet-output No normal output\n"), stderr); |
332 | fputs(_(" -Q, --quiet-output No normal output\n"), stderr); |
353 | fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); |
333 | fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr); |
354 | fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); |
334 | fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); |
355 | fputs(_(" -u, --unqote Do not quote the output\n"), stderr); |
335 | fputs(_(" -u, --unquote Do not quote the output\n"), stderr); |
356 | fputs(_(" -V, --version Output version information\n"), stderr); |
336 | fputs(_(" -V, --version Output version information\n"), stderr); |
|
|
337 | fputc('\n', stderr); |
|
|
338 | |
357 | exit(PARAMETER_EXIT_CODE); |
339 | exit(PARAMETER_EXIT_CODE); |
358 | } |
340 | } |
359 | |
341 | |
360 | int main(int argc, char *argv[]) |
342 | int main(int argc, char *argv[]) |
361 | { |
343 | { |
… | |
… | |
406 | } |
388 | } |
407 | |
389 | |
408 | |
390 | |
409 | if (argv[1][0] != '-' || compatible) { |
391 | if (argv[1][0] != '-' || compatible) { |
410 | quote = 0; |
392 | quote = 0; |
411 | optstr = our_malloc(strlen(argv[1]) + 1); |
393 | optstr = xmalloc(strlen(argv[1]) + 1); |
412 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
394 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
413 | argv[1] = argv[0]; |
395 | argv[1] = argv[0]; |
414 | return generate_output(argv + 1, argc - 1, optstr, |
396 | return generate_output(argv + 1, argc - 1, optstr, |
415 | long_options); |
397 | long_options); |
416 | } |
398 | } |
… | |
… | |
422 | getopt_long_fp = getopt_long_only; |
404 | getopt_long_fp = getopt_long_only; |
423 | break; |
405 | break; |
424 | case 'h': |
406 | case 'h': |
425 | print_help(); |
407 | print_help(); |
426 | case 'o': |
408 | case 'o': |
427 | if (optstr) |
|
|
428 | free(optstr); |
409 | free(optstr); |
429 | optstr = our_malloc(strlen(optarg)+1); |
410 | optstr = xmalloc(strlen(optarg) + 1); |
430 | strcpy(optstr, optarg); |
411 | strcpy(optstr, optarg); |
431 | break; |
412 | break; |
432 | case 'l': |
413 | case 'l': |
433 | add_long_options(optarg); |
414 | add_long_options(optarg); |
434 | break; |
415 | break; |
435 | case 'n': |
416 | case 'n': |
436 | if (name) |
|
|
437 | free(name); |
417 | free(name); |
438 | name = our_malloc(strlen(optarg)+1); |
418 | name = xmalloc(strlen(optarg) + 1); |
439 | strcpy(name, optarg); |
419 | strcpy(name, optarg); |
440 | break; |
420 | break; |
441 | case 'q': |
421 | case 'q': |
442 | quiet_errors = 1; |
422 | quiet_errors = 1; |
443 | break; |
423 | break; |
… | |
… | |
451 | return TEST_EXIT_CODE; |
431 | return TEST_EXIT_CODE; |
452 | case 'u': |
432 | case 'u': |
453 | quote = 0; |
433 | quote = 0; |
454 | break; |
434 | break; |
455 | case 'V': |
435 | case 'V': |
456 | printf(_("getopt (enhanced) 1.1.4\n")); |
436 | printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version); |
457 | return EXIT_SUCCESS; |
437 | return EXIT_SUCCESS; |
458 | case '?': |
438 | case '?': |
459 | case ':': |
439 | case ':': |
460 | parse_error(NULL); |
440 | parse_error(NULL); |
461 | default: |
441 | default: |
… | |
… | |
464 | |
444 | |
465 | if (!optstr) { |
445 | if (!optstr) { |
466 | if (optind >= argc) |
446 | if (optind >= argc) |
467 | parse_error(_("missing optstring argument")); |
447 | parse_error(_("missing optstring argument")); |
468 | else { |
448 | else { |
469 | optstr=our_malloc(strlen(argv[optind])+1); |
449 | optstr = xmalloc(strlen(argv[optind]) + 1); |
470 | strcpy(optstr, argv[optind]); |
450 | strcpy(optstr, argv[optind]); |
471 | optind++; |
451 | optind++; |
472 | } |
452 | } |
473 | } |
453 | } |
474 | if (name) |
454 | if (name) |