| … | |
… | |
| 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 | |
120 | |
| 142 | strcpy(BUFFER, arg); |
121 | strcpy(BUFFER, arg); |
| 143 | return BUFFER; |
122 | return BUFFER; |
| 144 | } |
123 | } |
| 145 | |
124 | |
| … | |
… | |
| 147 | * Each character in arg may take upto four characters in the |
126 | * 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 |
127 | * 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 |
128 | * and an opening quote! We need also the global opening and closing |
| 150 | * quote, and one extra character for '\0'. |
129 | * quote, and one extra character for '\0'. |
| 151 | */ |
130 | */ |
| 152 | BUFFER = our_malloc(strlen(arg)*4+3); |
131 | BUFFER = xmalloc(strlen(arg) * 4 + 3); |
| 153 | |
132 | |
| 154 | bufptr = BUFFER; |
133 | bufptr = BUFFER; |
| 155 | *bufptr++ = '\''; |
134 | *bufptr++ = '\''; |
| 156 | |
135 | |
| 157 | while (*argptr) { |
136 | while (*argptr) { |
| … | |
… | |
| 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 | } |
| … | |
… | |
| 406 | } |
385 | } |
| 407 | |
386 | |
| 408 | |
387 | |
| 409 | if (argv[1][0] != '-' || compatible) { |
388 | if (argv[1][0] != '-' || compatible) { |
| 410 | quote = 0; |
389 | quote = 0; |
| 411 | optstr = our_malloc(strlen(argv[1]) + 1); |
390 | optstr = xmalloc(strlen(argv[1]) + 1); |
| 412 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
391 | strcpy(optstr, argv[1] + strspn(argv[1], "-+")); |
| 413 | argv[1] = argv[0]; |
392 | argv[1] = argv[0]; |
| 414 | return generate_output(argv + 1, argc - 1, optstr, |
393 | return generate_output(argv + 1, argc - 1, optstr, |
| 415 | long_options); |
394 | long_options); |
| 416 | } |
395 | } |
| … | |
… | |
| 422 | getopt_long_fp = getopt_long_only; |
401 | getopt_long_fp = getopt_long_only; |
| 423 | break; |
402 | break; |
| 424 | case 'h': |
403 | case 'h': |
| 425 | print_help(); |
404 | print_help(); |
| 426 | case 'o': |
405 | case 'o': |
| 427 | if (optstr) |
|
|
| 428 | free(optstr); |
406 | free(optstr); |
| 429 | optstr = our_malloc(strlen(optarg)+1); |
407 | optstr = xmalloc(strlen(optarg) + 1); |
| 430 | strcpy(optstr, optarg); |
408 | strcpy(optstr, optarg); |
| 431 | break; |
409 | break; |
| 432 | case 'l': |
410 | case 'l': |
| 433 | add_long_options(optarg); |
411 | add_long_options(optarg); |
| 434 | break; |
412 | break; |
| 435 | case 'n': |
413 | case 'n': |
| 436 | if (name) |
|
|
| 437 | free(name); |
414 | free(name); |
| 438 | name = our_malloc(strlen(optarg)+1); |
415 | name = xmalloc(strlen(optarg) + 1); |
| 439 | strcpy(name, optarg); |
416 | strcpy(name, optarg); |
| 440 | break; |
417 | break; |
| 441 | case 'q': |
418 | case 'q': |
| 442 | quiet_errors = 1; |
419 | quiet_errors = 1; |
| 443 | break; |
420 | break; |
| … | |
… | |
| 464 | |
441 | |
| 465 | if (!optstr) { |
442 | if (!optstr) { |
| 466 | if (optind >= argc) |
443 | if (optind >= argc) |
| 467 | parse_error(_("missing optstring argument")); |
444 | parse_error(_("missing optstring argument")); |
| 468 | else { |
445 | else { |
| 469 | optstr=our_malloc(strlen(argv[optind])+1); |
446 | optstr = xmalloc(strlen(argv[optind]) + 1); |
| 470 | strcpy(optstr, argv[optind]); |
447 | strcpy(optstr, argv[optind]); |
| 471 | optind++; |
448 | optind++; |
| 472 | } |
449 | } |
| 473 | } |
450 | } |
| 474 | if (name) |
451 | if (name) |