getopt_long.c (ee43cb7a22d6a8d901ea7b69e7d33015b20a3fbd) | getopt_long.c (f2fd86b76ea12bb6557e14f9ab52d4197a1cf99b) |
---|---|
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 --- 56 unchanged lines hidden (view full) --- 65__FBSDID("$FreeBSD$"); 66 67#include <err.h> 68#include <errno.h> 69#include <getopt.h> 70#include <stdlib.h> 71#include <string.h> 72 | 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 --- 56 unchanged lines hidden (view full) --- 65__FBSDID("$FreeBSD$"); 66 67#include <err.h> 68#include <errno.h> 69#include <getopt.h> 70#include <stdlib.h> 71#include <string.h> 72 |
73#ifdef notyet | 73#define GNU_COMPATIBLE /* Be more compatible, configure's use us! */ 74 75#ifndef GNU_COMPATIBLE |
74#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ 75#endif 76 77#ifdef REPLACE_GETOPT 78int opterr = 1; /* if error message should be printed */ 79int optind = 1; /* index into parent argv vector */ 80int optopt = '?'; /* character checked for validity */ 81int optreset; /* reset getopt */ --- 23 unchanged lines hidden (view full) --- 105static char *place = EMSG; /* option letter processing */ 106 107/* XXX: set optreset to 1 rather than these two */ 108static int nonopt_start = -1; /* first non option argument (for permute) */ 109static int nonopt_end = -1; /* first option after non options (for permute) */ 110 111/* Error messages */ 112static const char recargchar[] = "option requires an argument -- %c"; | 76#define REPLACE_GETOPT /* use this getopt as the system getopt(3) */ 77#endif 78 79#ifdef REPLACE_GETOPT 80int opterr = 1; /* if error message should be printed */ 81int optind = 1; /* index into parent argv vector */ 82int optopt = '?'; /* character checked for validity */ 83int optreset; /* reset getopt */ --- 23 unchanged lines hidden (view full) --- 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"; |
115/* From P1003.2 */ 116static const char illoptchar[] = "illegal option -- %c"; 117#ifdef GNU_COMPATIBLE 118static const char gnuoptchar[] = "invalid option -- %c"; 119 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'"; 124#else |
|
113static const char recargstring[] = "option requires an argument -- %s"; 114static const char ambig[] = "ambiguous option -- %.*s"; 115static const char noarg[] = "option doesn't take an argument -- %.*s"; | 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"; |
116static const char illoptchar[] = "unknown option -- %c"; | |
117static const char illoptstring[] = "unknown option -- %s"; | 128static const char illoptstring[] = "unknown option -- %s"; |
129#endif |
|
118 119/* 120 * Compute the greatest common divisor of a and b. 121 */ 122static int 123gcd(int a, int b) 124{ 125 int c; --- 169 unchanged lines hidden (view full) --- 295 * Parse argc/argv argument vector. Called by user level routines. 296 */ 297static int 298getopt_internal(int nargc, char * const *nargv, const char *options, 299 const struct option *long_options, int *idx, int flags) 300{ 301 char *oli; /* option letter list index */ 302 int optchar, short_too; | 130 131/* 132 * Compute the greatest common divisor of a and b. 133 */ 134static int 135gcd(int a, int b) 136{ 137 int c; --- 169 unchanged lines hidden (view full) --- 307 * Parse argc/argv argument vector. Called by user level routines. 308 */ 309static int 310getopt_internal(int nargc, char * const *nargv, const char *options, 311 const struct option *long_options, int *idx, int flags) 312{ 313 char *oli; /* option letter list index */ 314 int optchar, short_too; |
303 static int posixly_correct = -1; | 315 int posixly_correct; |
304 305 if (options == NULL) 306 return (-1); 307 308 /* 309 * Disable GNU extensions if POSIXLY_CORRECT is set or options 310 * string begins with a '+'. 311 */ | 316 317 if (options == NULL) 318 return (-1); 319 320 /* 321 * Disable GNU extensions if POSIXLY_CORRECT is set or options 322 * string begins with a '+'. 323 */ |
312 if (posixly_correct == -1) 313 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); | 324 posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); 325#ifdef GNU_COMPATIBLE 326 if (*options == '-') 327 flags |= FLAG_ALLARGS; 328 else if (posixly_correct || *options == '+') 329 flags &= ~FLAG_PERMUTE; 330#else |
314 if (posixly_correct || *options == '+') 315 flags &= ~FLAG_PERMUTE; | 331 if (posixly_correct || *options == '+') 332 flags &= ~FLAG_PERMUTE; |
316 /* 317 * Code "else if (*options == '-')" was here. 318 * Try to be more GNU compatible, configure's use us! 319 */ 320 if (*options == '-') | 333 else if (*options == '-') |
321 flags |= FLAG_ALLARGS; | 334 flags |= FLAG_ALLARGS; |
335#endif |
|
322 if (*options == '+' || *options == '-') 323 options++; 324 325 /* 326 * XXX Some GNU programs (like cvs) set optind to 0 instead of 327 * XXX using optreset. Work around this braindamage. 328 */ 329 if (optind == 0) --- 107 unchanged lines hidden (view full) --- 437 * If the user specified "-" and '-' isn't listed in 438 * options, return -1 (non-option) as per POSIX. 439 * Otherwise, it is an unknown option character (or ':'). 440 */ 441 if (optchar == (int)'-' && *place == '\0') 442 return (-1); 443 if (!*place) 444 ++optind; | 336 if (*options == '+' || *options == '-') 337 options++; 338 339 /* 340 * XXX Some GNU programs (like cvs) set optind to 0 instead of 341 * XXX using optreset. Work around this braindamage. 342 */ 343 if (optind == 0) --- 107 unchanged lines hidden (view full) --- 451 * If the user specified "-" and '-' isn't listed in 452 * options, return -1 (non-option) as per POSIX. 453 * Otherwise, it is an unknown option character (or ':'). 454 */ 455 if (optchar == (int)'-' && *place == '\0') 456 return (-1); 457 if (!*place) 458 ++optind; |
459#ifdef GNU_COMPATIBLE |
|
445 if (PRINT_ERROR) | 460 if (PRINT_ERROR) |
461 warnx(posixly_correct ? illoptchar : gnuoptchar, 462 optchar); 463#else 464 if (PRINT_ERROR) |
|
446 warnx(illoptchar, optchar); | 465 warnx(illoptchar, optchar); |
466#endif |
|
447 optopt = optchar; 448 return (BADCH); 449 } 450 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 451 /* -W long-option */ 452 if (*place) /* no space */ 453 /* NOTHING */; 454 else if (++optind >= nargc) { /* no arg */ --- 100 unchanged lines hidden --- | 467 optopt = optchar; 468 return (BADCH); 469 } 470 if (long_options != NULL && optchar == 'W' && oli[1] == ';') { 471 /* -W long-option */ 472 if (*place) /* no space */ 473 /* NOTHING */; 474 else if (++optind >= nargc) { /* no arg */ --- 100 unchanged lines hidden --- |