getopt_long.c (126452) | getopt_long.c (126518) |
---|---|
1/* $OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $ */ 2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ 3 4/* 5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above --- 48 unchanged lines hidden (view full) --- 57 */ 58 59#if 0 60#if defined(LIBC_SCCS) && !defined(lint) 61static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $"; 62#endif /* LIBC_SCCS and not lint */ 63#endif 64#include <sys/cdefs.h> | 1/* $OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $ */ 2/* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ 3 4/* 5 * Copyright (c) 2002 Todd C. Miller <Todd.Miller@courtesan.com> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above --- 48 unchanged lines hidden (view full) --- 57 */ 58 59#if 0 60#if defined(LIBC_SCCS) && !defined(lint) 61static char *rcsid = "$OpenBSD: getopt_long.c,v 1.16 2004/02/04 18:17:25 millert Exp $"; 62#endif /* LIBC_SCCS and not lint */ 63#endif 64#include <sys/cdefs.h> |
65__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt_long.c 126452 2004-03-01 17:57:05Z ache $"); | 65__FBSDID("$FreeBSD: head/lib/libc/stdlib/getopt_long.c 126518 2004-03-03 03:05:21Z ache $"); |
66 67#include <err.h> 68#include <errno.h> 69#include <getopt.h> 70#include <stdlib.h> 71#include <string.h> 72 73#define GNU_COMPATIBLE /* Be more compatible, configure's use us! */ --- 18 unchanged lines hidden (view full) --- 92 93/* return values */ 94#define BADCH (int)'?' 95#define BADARG ((*options == ':') ? (int)':' : (int)'?') 96#define INORDER (int)1 97 98#define EMSG "" 99 | 66 67#include <err.h> 68#include <errno.h> 69#include <getopt.h> 70#include <stdlib.h> 71#include <string.h> 72 73#define GNU_COMPATIBLE /* Be more compatible, configure's use us! */ --- 18 unchanged lines hidden (view full) --- 92 93/* return values */ 94#define BADCH (int)'?' 95#define BADARG ((*options == ':') ? (int)':' : (int)'?') 96#define INORDER (int)1 97 98#define EMSG "" 99 |
100#ifdef GNU_COMPATIBLE 101#define NO_PREFIX (-1) 102#define D_PREFIX 0 103#define DD_PREFIX 1 104#define W_PREFIX 2 105#endif 106 |
|
100static int getopt_internal(int, char * const *, const char *, 101 const struct option *, int *, int); 102static int parse_long_options(char * const *, const char *, 103 const struct option *, int *, int); 104static int gcd(int, int); 105static void permute_args(int, int, int, char * const *); 106 107static char *place = EMSG; /* option letter processing */ 108 109/* XXX: set optreset to 1 rather than these two */ 110static int nonopt_start = -1; /* first non option argument (for permute) */ 111static int nonopt_end = -1; /* first option after non options (for permute) */ 112 113/* Error messages */ 114static const char recargchar[] = "option requires an argument -- %c"; | 107static int getopt_internal(int, char * const *, const char *, 108 const struct option *, int *, int); 109static int parse_long_options(char * const *, const char *, 110 const struct option *, int *, int); 111static int gcd(int, int); 112static void permute_args(int, int, int, char * const *); 113 114static char *place = EMSG; /* option letter processing */ 115 116/* XXX: set optreset to 1 rather than these two */ 117static int nonopt_start = -1; /* first non option argument (for permute) */ 118static int nonopt_end = -1; /* first option after non options (for permute) */ 119 120/* Error messages */ 121static const char recargchar[] = "option requires an argument -- %c"; |
115/* From P1003.2 */ 116static const char illoptchar[] = "illegal option -- %c"; | 122static const char illoptchar[] = "illegal option -- %c"; /* From P1003.2 */ |
117#ifdef GNU_COMPATIBLE | 123#ifdef GNU_COMPATIBLE |
124static int dash_prefix = NO_PREFIX; |
|
118static const char gnuoptchar[] = "invalid option -- %c"; 119 | 125static const char gnuoptchar[] = "invalid option -- %c"; 126 |
120static const char recargstring[] = "option `--%s' requires an argument"; 121static const char ambig[] = "option `--%.*s' is ambiguous"; 122static const char noarg[] = "option `--%.*s' doesn't allow an argument"; 123static const char illoptstring[] = "unrecognized option `--%s'"; | 127static const char recargstring[] = "option `%s%s' requires an argument"; 128static const char ambig[] = "option `%s%.*s' is ambiguous"; 129static const char noarg[] = "option `%s%.*s' doesn't allow an argument"; 130static const char illoptstring[] = "unrecognized option `%s%s'"; |
124#else 125static const char recargstring[] = "option requires an argument -- %s"; 126static const char ambig[] = "ambiguous option -- %.*s"; 127static const char noarg[] = "option doesn't take an argument -- %.*s"; 128static const char illoptstring[] = "unknown option -- %s"; 129#endif 130 131/* --- 56 unchanged lines hidden (view full) --- 188 * Parse long options in argc/argv argument vector. 189 * Returns -1 if short_too is set and the option does not match long_options. 190 */ 191static int 192parse_long_options(char * const *nargv, const char *options, 193 const struct option *long_options, int *idx, int short_too) 194{ 195 char *current_argv, *has_equal; | 131#else 132static const char recargstring[] = "option requires an argument -- %s"; 133static const char ambig[] = "ambiguous option -- %.*s"; 134static const char noarg[] = "option doesn't take an argument -- %.*s"; 135static const char illoptstring[] = "unknown option -- %s"; 136#endif 137 138/* --- 56 unchanged lines hidden (view full) --- 195 * Parse long options in argc/argv argument vector. 196 * Returns -1 if short_too is set and the option does not match long_options. 197 */ 198static int 199parse_long_options(char * const *nargv, const char *options, 200 const struct option *long_options, int *idx, int short_too) 201{ 202 char *current_argv, *has_equal; |
203#ifdef GNU_COMPATIBLE 204 char *current_dash; 205#endif |
|
196 size_t current_argv_len; 197 int i, match; 198 199 current_argv = place; | 206 size_t current_argv_len; 207 int i, match; 208 209 current_argv = place; |
210#ifdef GNU_COMPATIBLE 211 switch (dash_prefix) { 212 case D_PREFIX: 213 current_dash = "-"; 214 break; 215 case DD_PREFIX: 216 current_dash = "--"; 217 break; 218 case W_PREFIX: 219 current_dash = "-W "; 220 break; 221 default: 222 current_dash = ""; 223 break; 224 } 225#endif |
|
200 match = -1; 201 202 optind++; 203 204 if ((has_equal = strchr(current_argv, '=')) != NULL) { 205 /* argument found (--option=arg) */ 206 current_argv_len = has_equal - current_argv; 207 has_equal++; --- 18 unchanged lines hidden (view full) --- 226 if (short_too && current_argv_len == 1) 227 continue; 228 229 if (match == -1) /* partial match */ 230 match = i; 231 else { 232 /* ambiguous abbreviation */ 233 if (PRINT_ERROR) | 226 match = -1; 227 228 optind++; 229 230 if ((has_equal = strchr(current_argv, '=')) != NULL) { 231 /* argument found (--option=arg) */ 232 current_argv_len = has_equal - current_argv; 233 has_equal++; --- 18 unchanged lines hidden (view full) --- 252 if (short_too && current_argv_len == 1) 253 continue; 254 255 if (match == -1) /* partial match */ 256 match = i; 257 else { 258 /* ambiguous abbreviation */ 259 if (PRINT_ERROR) |
234 warnx(ambig, (int)current_argv_len, | 260 warnx(ambig, 261#ifdef GNU_COMPATIBLE 262 current_dash, 263#endif 264 (int)current_argv_len, |
235 current_argv); 236 optopt = 0; 237 return (BADCH); 238 } 239 } 240 if (match != -1) { /* option found */ 241 if (long_options[match].has_arg == no_argument 242 && has_equal) { 243 if (PRINT_ERROR) | 265 current_argv); 266 optopt = 0; 267 return (BADCH); 268 } 269 } 270 if (match != -1) { /* option found */ 271 if (long_options[match].has_arg == no_argument 272 && has_equal) { 273 if (PRINT_ERROR) |
244 warnx(noarg, (int)current_argv_len, | 274 warnx(noarg, 275#ifdef GNU_COMPATIBLE 276 current_dash, 277#endif 278 (int)current_argv_len, |
245 current_argv); 246 /* 247 * XXX: GNU sets optopt to val regardless of flag 248 */ 249 if (long_options[match].flag == NULL) 250 optopt = long_options[match].val; 251 else 252 optopt = 0; --- 14 unchanged lines hidden (view full) --- 267 if ((long_options[match].has_arg == required_argument) 268 && (optarg == NULL)) { 269 /* 270 * Missing argument; leading ':' indicates no error 271 * should be generated. 272 */ 273 if (PRINT_ERROR) 274 warnx(recargstring, | 279 current_argv); 280 /* 281 * XXX: GNU sets optopt to val regardless of flag 282 */ 283 if (long_options[match].flag == NULL) 284 optopt = long_options[match].val; 285 else 286 optopt = 0; --- 14 unchanged lines hidden (view full) --- 301 if ((long_options[match].has_arg == required_argument) 302 && (optarg == NULL)) { 303 /* 304 * Missing argument; leading ':' indicates no error 305 * should be generated. 306 */ 307 if (PRINT_ERROR) 308 warnx(recargstring, |
309#ifdef GNU_COMPATIBLE 310 current_dash, 311#endif |
|
275 current_argv); 276 /* 277 * XXX: GNU sets optopt to val regardless of flag 278 */ 279 if (long_options[match].flag == NULL) 280 optopt = long_options[match].val; 281 else 282 optopt = 0; 283 --optind; 284 return (BADARG); 285 } 286 } else { /* unknown option */ 287 if (short_too) { 288 --optind; 289 return (-1); 290 } 291 if (PRINT_ERROR) | 312 current_argv); 313 /* 314 * XXX: GNU sets optopt to val regardless of flag 315 */ 316 if (long_options[match].flag == NULL) 317 optopt = long_options[match].val; 318 else 319 optopt = 0; 320 --optind; 321 return (BADARG); 322 } 323 } else { /* unknown option */ 324 if (short_too) { 325 --optind; 326 return (-1); 327 } 328 if (PRINT_ERROR) |
292 warnx(illoptstring, current_argv); | 329 warnx(illoptstring, 330#ifdef GNU_COMPATIBLE 331 current_dash, 332#endif 333 current_argv); |
293 optopt = 0; 294 return (BADCH); 295 } 296 if (idx) 297 *idx = match; 298 if (long_options[match].flag) { 299 *long_options[match].flag = long_options[match].val; 300 return (0); --- 125 unchanged lines hidden (view full) --- 426 * Check long options if: 427 * 1) we were passed some 428 * 2) the arg is not just "-" 429 * 3) either the arg starts with -- we are getopt_long_only() 430 */ 431 if (long_options != NULL && place != nargv[optind] && 432 (*place == '-' || (flags & FLAG_LONGONLY))) { 433 short_too = 0; | 334 optopt = 0; 335 return (BADCH); 336 } 337 if (idx) 338 *idx = match; 339 if (long_options[match].flag) { 340 *long_options[match].flag = long_options[match].val; 341 return (0); --- 125 unchanged lines hidden (view full) --- 467 * Check long options if: 468 * 1) we were passed some 469 * 2) the arg is not just "-" 470 * 3) either the arg starts with -- we are getopt_long_only() 471 */ 472 if (long_options != NULL && place != nargv[optind] && 473 (*place == '-' || (flags & FLAG_LONGONLY))) { 474 short_too = 0; |
434 if (*place == '-') | 475#ifdef GNU_COMPATIBLE 476 dash_prefix = D_PREFIX; 477#endif 478 if (*place == '-') { |
435 place++; /* --foo long option */ | 479 place++; /* --foo long option */ |
436 else if (*place != ':' && strchr(options, *place) != NULL) | 480#ifdef GNU_COMPATIBLE 481 dash_prefix = DD_PREFIX; 482#endif 483 } else if (*place != ':' && strchr(options, *place) != NULL) |
437 short_too = 1; /* could be short option too */ 438 439 optchar = parse_long_options(nargv, options, long_options, 440 idx, short_too); 441 if (optchar != -1) { 442 place = EMSG; 443 return (optchar); 444 } --- 29 unchanged lines hidden (view full) --- 474 else if (++optind >= nargc) { /* no arg */ 475 place = EMSG; 476 if (PRINT_ERROR) 477 warnx(recargchar, optchar); 478 optopt = optchar; 479 return (BADARG); 480 } else /* white space */ 481 place = nargv[optind]; | 484 short_too = 1; /* could be short option too */ 485 486 optchar = parse_long_options(nargv, options, long_options, 487 idx, short_too); 488 if (optchar != -1) { 489 place = EMSG; 490 return (optchar); 491 } --- 29 unchanged lines hidden (view full) --- 521 else if (++optind >= nargc) { /* no arg */ 522 place = EMSG; 523 if (PRINT_ERROR) 524 warnx(recargchar, optchar); 525 optopt = optchar; 526 return (BADARG); 527 } else /* white space */ 528 place = nargv[optind]; |
529#ifdef GNU_COMPATIBLE 530 dash_prefix = W_PREFIX; 531#endif |
|
482 optchar = parse_long_options(nargv, options, long_options, 483 idx, 0); 484 place = EMSG; 485 return (optchar); 486 } 487 if (*++oli != ':') { /* doesn't take argument */ 488 if (!*place) 489 ++optind; --- 85 unchanged lines hidden --- | 532 optchar = parse_long_options(nargv, options, long_options, 533 idx, 0); 534 place = EMSG; 535 return (optchar); 536 } 537 if (*++oli != ':') { /* doesn't take argument */ 538 if (!*place) 539 ++optind; --- 85 unchanged lines hidden --- |