/[public]/getopt/trunk/getopt.c
ViewVC logotype

Diff of /getopt/trunk/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 321 Revision 325
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. */
79static shell_t shell = BASH; /* The shell we generate output for. */ 80static shell_t shell = BASH; /* The shell we generate output for. */
80static int quiet_errors = 0; /* 0 is not quiet. */ 81static int quiet_errors = 0; /* 0 is not quiet. */
81static int quiet_output = 0; /* 0 is not quiet. */ 82static int quiet_output = 0; /* 0 is not quiet. */
82static int quote = 1; /* 1 is do quote. */ 83static int quote = 1; /* 1 is do quote. */
83 84
85/* Allow changing which getopt is in use with function pointer */
84int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, 86int (*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 */
88void *our_malloc(size_t size);
89void *our_realloc(void *ptr, size_t size);
90static const char *normalize(const char *arg); 90static const char *normalize(const char *arg);
91static int generate_output(char *argv[], int argc, const char *optstr, 91static int generate_output(char *argv[], int argc, const char *optstr,
92 const struct option *longopts); 92 const struct option *longopts);
93int main(int argc, char *argv[]); 93int main(int argc, char *argv[]);
94static void parse_error(const char *message); 94static void parse_error(const char *message);
95static void add_long_options(char *options); 95static void add_long_options(char *options);
96static void add_longopt(const char *name, int has_arg); 96static void add_longopt(const char *name, int has_arg);
97static void print_help(void); 97static void print_help(void);
98static void set_shell(const char *new_shell); 98static void set_shell(const char *new_shell);
99 99
100void *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
110void *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 */
244static void __attribute__ ((__noreturn__)) parse_error(const char *message) 222static 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_invocation_short_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
252static struct option *long_options = NULL; 231static struct option *long_options = NULL;
253static int long_options_length = 0; /* Length of array */ 232static 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
360int main(int argc, char *argv[]) 342int main(int argc, char *argv[])
361{ 343{
378 {"alternative", no_argument, NULL, 'a'}, 360 {"alternative", no_argument, NULL, 'a'},
379 {"name", required_argument, NULL, 'n'}, 361 {"name", required_argument, NULL, 'n'},
380 {"version", no_argument, NULL, 'V'}, 362 {"version", no_argument, NULL, 'V'},
381 {NULL, 0, NULL, 0} 363 {NULL, 0, NULL, 0}
382 }; 364 };
383#if WITHOUT_GETTEXT
384#else
385 setlocale(LC_ALL, ""); 365 setlocale(LC_ALL, "");
386 bindtextdomain(PACKAGE, LOCALEDIR); 366 bindtextdomain(PACKAGE, LOCALEDIR);
387 textdomain(PACKAGE); 367 textdomain(PACKAGE);
388#endif
389 368
390 init_longopt(); 369 init_longopt();
391 getopt_long_fp = getopt_long; 370 getopt_long_fp = getopt_long;
392 371
393 if (getenv("GETOPT_COMPATIBLE")) 372 if (getenv("GETOPT_COMPATIBLE"))
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;
451 return TEST_EXIT_CODE; 428 return TEST_EXIT_CODE;
452 case 'u': 429 case 'u':
453 quote = 0; 430 quote = 0;
454 break; 431 break;
455 case 'V': 432 case 'V':
456 printf(_("getopt (enhanced) 1.1.4\n")); 433 printf(_("%s (enhanced) %s\n"), program_invocation_short_name, program_version);
457 return EXIT_SUCCESS; 434 return EXIT_SUCCESS;
458 case '?': 435 case '?':
459 case ':': 436 case ':':
460 parse_error(NULL); 437 parse_error(NULL);
461 default: 438 default:
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)

Legend:
Removed from v.321  
changed lines
  Added in v.325

frodo@frodo.looijaard.name
ViewVC Help
Powered by ViewVC 1.1.26