/[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 363
31 * Version 1.0.5: Tue Jun 22 1999 31 * Version 1.0.5: Tue Jun 22 1999
32 * Make -u option work (not present) 32 * Make -u option work (not present)
33 * Version 1.0.6: Tue Jun 27 2000 33 * Version 1.0.6: Tue Jun 27 2000
34 * No important changes 34 * No important changes
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ś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: Sun Aug 12 2012
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.
61#include <getopt.h> 63#include <getopt.h>
62#else 64#else
63#include "getopt.h" 65#include "getopt.h"
64#endif 66#endif
65 67
68#include "util-linux-compat.h"
66#include "nls.h" 69#include "nls.h"
70#include "xalloc.h"
67 71
68/* NON_OPT is the code that is returned when a non-option is found in '+' 72/* NON_OPT is the code that is returned when a non-option is found in '+'
69 * mode */ 73 * mode */
70#define NON_OPT 1 74#define NON_OPT 1
71/* LONG_OPT is the code that is returned when a long option is found. */ 75/* 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. */ 83static shell_t shell = BASH; /* The shell we generate output for. */
80static int quiet_errors = 0; /* 0 is not quiet. */ 84static int quiet_errors = 0; /* 0 is not quiet. */
81static int quiet_output = 0; /* 0 is not quiet. */ 85static int quiet_output = 0; /* 0 is not quiet. */
82static int quote = 1; /* 1 is do quote. */ 86static int quote = 1; /* 1 is do quote. */
83 87
88/* Allow changing which getopt is in use with function pointer */
84int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr, 89int (*getopt_long_fp) (int argc, char *const *argv, const char *optstr,
85 const struct option * longopts, int *longindex); 90 const struct option * longopts, int *longindex);
86 91
87/* Function prototypes */ 92/* Function prototypes */
88void *our_malloc(size_t size);
89void *our_realloc(void *ptr, size_t size);
90static const char *normalize(const char *arg); 93static const char *normalize(const char *arg);
91static int generate_output(char *argv[], int argc, const char *optstr, 94static int generate_output(char *argv[], int argc, const char *optstr,
92 const struct option *longopts); 95 const struct option *longopts);
93int main(int argc, char *argv[]); 96int main(int argc, char *argv[]);
94static void parse_error(const char *message); 97static void parse_error(const char *message);
95static void add_long_options(char *options); 98static void add_long_options(char *options);
96static void add_longopt(const char *name, int has_arg); 99static void add_longopt(const char *name, int has_arg);
97static void print_help(void); 100static void print_help(void);
98static void set_shell(const char *new_shell); 101static void set_shell(const char *new_shell);
99 102
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/* 103/*
121 * This function 'normalizes' a single argument: it puts single quotes 104 * This function 'normalizes' a single argument: it puts single quotes
122 * around it and escapes other special characters. If quote is false, it 105 * around it and escapes other special characters. If quote is false, it
123 * just returns its argument. 106 * just returns its argument.
124 * 107 *
130{ 113{
131 static char *BUFFER = NULL; 114 static char *BUFFER = NULL;
132 const char *argptr = arg; 115 const char *argptr = arg;
133 char *bufptr; 116 char *bufptr;
134 117
135 if (BUFFER != NULL)
136 free(BUFFER); 118 free(BUFFER);
137 119
138 if (!quote) { 120 if (!quote) {
139 /* Just copy arg */ 121 /* Just copy arg */
140 BUFFER = our_malloc(strlen(arg)+1); 122 BUFFER = xmalloc(strlen(arg) + 1);
141
142 strcpy(BUFFER, arg); 123 strcpy(BUFFER, arg);
143 return BUFFER; 124 return BUFFER;
144 } 125 }
145 126
146 /* 127 /*
147 * Each character in arg may take upto four characters in the 128 * 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 129 * 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 130 * and an opening quote! We need also the global opening and closing
150 * quote, and one extra character for '\0'. 131 * quote, and one extra character for '\0'.
151 */ 132 */
152 BUFFER = our_malloc(strlen(arg)*4+3); 133 BUFFER = xmalloc(strlen(arg) * 4 + 3);
153 134
154 bufptr = BUFFER; 135 bufptr = BUFFER;
155 *bufptr++ = '\''; 136 *bufptr++ = '\'';
156 137
157 while (*argptr) { 138 while (*argptr) {
217 if (opt == LONG_OPT) { 198 if (opt == LONG_OPT) {
218 printf(" --%s", longopts[longindex].name); 199 printf(" --%s", longopts[longindex].name);
219 if (longopts[longindex].has_arg) 200 if (longopts[longindex].has_arg)
220 printf(" %s", normalize(optarg ? optarg : "")); 201 printf(" %s", normalize(optarg ? optarg : ""));
221 } else if (opt == NON_OPT) 202 } else if (opt == NON_OPT)
222 printf(" %s", normalize(optarg)); 203 printf(" %s", normalize(optarg ? optarg : ""));
223 else { 204 else {
224 printf(" -%c", opt); 205 printf(" -%c", opt);
225 charptr = strchr(optstr, opt); 206 charptr = strchr(optstr, opt);
226 if (charptr != NULL && *++charptr == ':') 207 if (charptr != NULL && *++charptr == ':')
227 printf(" %s", normalize(optarg ? optarg : "")); 208 printf(" %s", normalize(optarg ? optarg : ""));
242 * we already sent a message, we just exit with a helpful hint. 223 * we already sent a message, we just exit with a helpful hint.
243 */ 224 */
244static void __attribute__ ((__noreturn__)) parse_error(const char *message) 225static void __attribute__ ((__noreturn__)) parse_error(const char *message)
245{ 226{
246 if (message) 227 if (message)
247 fprintf(stderr, "getopt: %s\n", message); 228 fprintf(stderr, "%s: %s\n", program_invocation_short_name, message);
248 fputs(_("Try `getopt --help' for more information.\n"), stderr); 229 fprintf(stderr, _("Try `%s --help' for more information.\n"),
230 program_invocation_short_name);
249 exit(PARAMETER_EXIT_CODE); 231 exit(PARAMETER_EXIT_CODE);
250} 232}
251 233
252static struct option *long_options = NULL; 234static struct option *long_options = NULL;
253static int long_options_length = 0; /* Length of array */ 235static int long_options_length = 0; /* Length of array */
267 long_options_nr = 0; 249 long_options_nr = 0;
268 } 250 }
269 251
270 if (long_options_nr == long_options_length) { 252 if (long_options_nr == long_options_length) {
271 long_options_length += LONG_OPTIONS_INCR; 253 long_options_length += LONG_OPTIONS_INCR;
272 long_options = our_realloc(long_options, 254 long_options = xrealloc(long_options,
273 sizeof(struct option) * 255 sizeof(struct option) *
274 long_options_length); 256 long_options_length);
275 } 257 }
276 258
277 long_options[long_options_nr].name = NULL; 259 long_options[long_options_nr].name = NULL;
278 long_options[long_options_nr].has_arg = 0; 260 long_options[long_options_nr].has_arg = 0;
279 long_options[long_options_nr].flag = NULL; 261 long_options[long_options_nr].flag = NULL;
280 long_options[long_options_nr].val = 0; 262 long_options[long_options_nr].val = 0;
281 263
282 if (long_options_nr) { 264 if (long_options_nr && name) {
283 /* Not for init! */ 265 /* Not for init! */
284 long_options[long_options_nr - 1].has_arg = has_arg; 266 long_options[long_options_nr - 1].has_arg = has_arg;
285 long_options[long_options_nr - 1].flag = NULL; 267 long_options[long_options_nr - 1].flag = NULL;
286 long_options[long_options_nr - 1].val = LONG_OPT; 268 long_options[long_options_nr - 1].val = LONG_OPT;
287 tmp = our_malloc(strlen(name) + 1); 269 tmp = xmalloc(strlen(name) + 1);
288 strcpy(tmp, name); 270 strcpy(tmp, name);
289 long_options[long_options_nr - 1].name = tmp; 271 long_options[long_options_nr - 1].name = tmp;
290 } 272 }
291 long_options_nr++; 273 long_options_nr++;
292} 274}
337 ("unknown shell after -s or --shell argument")); 319 ("unknown shell after -s or --shell argument"));
338} 320}
339 321
340static void __attribute__ ((__noreturn__)) print_help(void) 322static void __attribute__ ((__noreturn__)) print_help(void)
341{ 323{
342 fputs(_("Usage: getopt optstring parameters\n"), stderr); 324 fputs(USAGE_HEADER, stderr);
343 fputs(_(" getopt [options] [--] optstring parameters\n"), stderr); 325 fprintf(stderr, _(
344 fputs(_(" getopt [options] -o|--options optstring [options] [--]\n"), stderr); 326 " %1$s optstring parameters\n"
345 fputs(_(" parameters\n"), stderr); 327 " %1$s [options] [--] optstring parameters\n"
328 " %1$s [options] -o|--options optstring [options] [--] parameters\n"),
329 program_invocation_short_name);
330
331 fputs(USAGE_OPTIONS, stderr);
346 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr); 332 fputs(_(" -a, --alternative Allow long options starting with single -\n"), stderr);
347 fputs(_(" -h, --help This small usage guide\n"), stderr);
348 fputs(_(" -l, --longoptions=longopts Long options to be recognized\n"), stderr); 333 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); 334 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); 335 fputs(_(" -o, --options <optstring> Short options to be recognized\n"), stderr);
351 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr); 336 fputs(_(" -q, --quiet Disable error reporting by getopt(3)\n"), stderr);
352 fputs(_(" -Q, --quiet-output No normal output\n"), stderr); 337 fputs(_(" -Q, --quiet-output No normal output\n"), stderr);
353 fputs(_(" -s, --shell=shell Set shell quoting conventions\n"), stderr); 338 fputs(_(" -s, --shell <shell> Set shell quoting conventions\n"), stderr);
354 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr); 339 fputs(_(" -T, --test Test for getopt(1) version\n"), stderr);
355 fputs(_(" -u, --unqote Do not quote the output\n"), stderr); 340 fputs(_(" -u, --unquoted Do not quote the output\n"), stderr);
356 fputs(_(" -V, --version Output version information\n"), stderr); 341 fputs(USAGE_SEPARATOR, stderr);
342 fputs(USAGE_HELP, stderr);
343 fputs(USAGE_VERSION, stderr);
344 fprintf(stderr, USAGE_MAN_TAIL("getopt(1)"));
357 exit(PARAMETER_EXIT_CODE); 345 exit(PARAMETER_EXIT_CODE);
358} 346}
359 347
360int main(int argc, char *argv[]) 348int main(int argc, char *argv[])
361{ 349{
378 {"alternative", no_argument, NULL, 'a'}, 366 {"alternative", no_argument, NULL, 'a'},
379 {"name", required_argument, NULL, 'n'}, 367 {"name", required_argument, NULL, 'n'},
380 {"version", no_argument, NULL, 'V'}, 368 {"version", no_argument, NULL, 'V'},
381 {NULL, 0, NULL, 0} 369 {NULL, 0, NULL, 0}
382 }; 370 };
383#if WITHOUT_GETTEXT
384#else
385 setlocale(LC_ALL, ""); 371 setlocale(LC_ALL, "");
386 bindtextdomain(PACKAGE, LOCALEDIR); 372 bindtextdomain(PACKAGE, LOCALEDIR);
387 textdomain(PACKAGE); 373 textdomain(PACKAGE);
388#endif
389 374
390 init_longopt(); 375 init_longopt();
391 getopt_long_fp = getopt_long; 376 getopt_long_fp = getopt_long;
392 377
393 if (getenv("GETOPT_COMPATIBLE")) 378 if (getenv("GETOPT_COMPATIBLE"))
406 } 391 }
407 392
408 393
409 if (argv[1][0] != '-' || compatible) { 394 if (argv[1][0] != '-' || compatible) {
410 quote = 0; 395 quote = 0;
411 optstr = our_malloc(strlen(argv[1]) + 1); 396 optstr = xmalloc(strlen(argv[1]) + 1);
412 strcpy(optstr, argv[1] + strspn(argv[1], "-+")); 397 strcpy(optstr, argv[1] + strspn(argv[1], "-+"));
413 argv[1] = argv[0]; 398 argv[1] = argv[0];
414 return generate_output(argv + 1, argc - 1, optstr, 399 return generate_output(argv + 1, argc - 1, optstr,
415 long_options); 400 long_options);
416 } 401 }
422 getopt_long_fp = getopt_long_only; 407 getopt_long_fp = getopt_long_only;
423 break; 408 break;
424 case 'h': 409 case 'h':
425 print_help(); 410 print_help();
426 case 'o': 411 case 'o':
427 if (optstr)
428 free(optstr); 412 free(optstr);
429 optstr = our_malloc(strlen(optarg)+1); 413 optstr = xmalloc(strlen(optarg) + 1);
430 strcpy(optstr, optarg); 414 strcpy(optstr, optarg);
431 break; 415 break;
432 case 'l': 416 case 'l':
433 add_long_options(optarg); 417 add_long_options(optarg);
434 break; 418 break;
435 case 'n': 419 case 'n':
436 if (name)
437 free(name); 420 free(name);
438 name = our_malloc(strlen(optarg)+1); 421 name = xmalloc(strlen(optarg) + 1);
439 strcpy(name, optarg); 422 strcpy(name, optarg);
440 break; 423 break;
441 case 'q': 424 case 'q':
442 quiet_errors = 1; 425 quiet_errors = 1;
443 break; 426 break;
451 return TEST_EXIT_CODE; 434 return TEST_EXIT_CODE;
452 case 'u': 435 case 'u':
453 quote = 0; 436 quote = 0;
454 break; 437 break;
455 case 'V': 438 case 'V':
456 printf(_("getopt (enhanced) 1.1.4\n")); 439 printf(UTIL_LINUX_VERSION);
457 return EXIT_SUCCESS; 440 return EXIT_SUCCESS;
458 case '?': 441 case '?':
459 case ':': 442 case ':':
460 parse_error(NULL); 443 parse_error(NULL);
461 default: 444 default:
464 447
465 if (!optstr) { 448 if (!optstr) {
466 if (optind >= argc) 449 if (optind >= argc)
467 parse_error(_("missing optstring argument")); 450 parse_error(_("missing optstring argument"));
468 else { 451 else {
469 optstr=our_malloc(strlen(argv[optind])+1); 452 optstr = xmalloc(strlen(argv[optind]) + 1);
470 strcpy(optstr, argv[optind]); 453 strcpy(optstr, argv[optind]);
471 optind++; 454 optind++;
472 } 455 }
473 } 456 }
474 if (name) 457 if (name)

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

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