123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056 |
- #ifndef _NO_PROTO
- # define _NO_PROTO
- #endif
- #ifdef HAVE_CONFIG_H
- # include <config.h>
- #endif
- #if !defined __STDC__ || !__STDC__
- # ifndef const
- # define const
- # endif
- #endif
- #include <stdio.h>
- #define GETOPT_INTERFACE_VERSION 2
- #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
- # include <gnu-versions.h>
- # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
- # define ELIDE_CODE
- # endif
- #endif
- #ifndef ELIDE_CODE
- #ifdef __GNU_LIBRARY__
- # include <stdlib.h>
- # include <unistd.h>
- #endif
- #ifdef VMS
- # include <unixlib.h>
- # if HAVE_STRING_H - 0
- # include <string.h>
- # endif
- #endif
- #ifndef _
- # if defined HAVE_LIBINTL_H || defined _LIBC
- # include <libintl.h>
- # ifndef _
- # define _(msgid) gettext (msgid)
- # endif
- # else
- # define _(msgid) (msgid)
- # endif
- #endif
- #include "getopt.h"
- char *optarg;
- int optind = 1;
- int __getopt_initialized;
- static char *nextchar;
- int opterr = 1;
- int optopt = '?';
- static enum
- {
- REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
- } ordering;
- static char *posixly_correct;
- #ifdef __GNU_LIBRARY__
- # include <string.h>
- # define my_index strchr
- #else
- # if HAVE_STRING_H
- # include <string.h>
- # else
- # include <strings.h>
- # endif
- #ifndef getenv
- extern char *getenv ();
- #endif
- static char *
- my_index (str, chr)
- const char *str;
- int chr;
- {
- while (*str)
- {
- if (*str == chr)
- return (char *) str;
- str++;
- }
- return 0;
- }
- #ifdef __GNUC__
- # if (!defined __STDC__ || !__STDC__) && !defined strlen
- extern int strlen (const char *);
- # endif
- #endif
- #endif
- static int first_nonopt;
- static int last_nonopt;
- #ifdef _LIBC
- extern int __libc_argc;
- extern char **__libc_argv;
- # ifdef USE_NONOPTION_FLAGS
- extern char *__getopt_nonoption_flags;
- static int nonoption_flags_max_len;
- static int nonoption_flags_len;
- # endif
- # ifdef USE_NONOPTION_FLAGS
- # define SWAP_FLAGS(ch1, ch2) \
- if (nonoption_flags_len > 0) \
- { \
- char __tmp = __getopt_nonoption_flags[ch1]; \
- __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \
- __getopt_nonoption_flags[ch2] = __tmp; \
- }
- # else
- # define SWAP_FLAGS(ch1, ch2)
- # endif
- #else
- # define SWAP_FLAGS(ch1, ch2)
- #endif
- #if defined __STDC__ && __STDC__
- static void exchange (char **);
- #endif
- static void
- exchange (argv)
- char **argv;
- {
- int bottom = first_nonopt;
- int middle = last_nonopt;
- int top = optind;
- char *tem;
-
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
-
- if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
- {
-
- char *new_str = malloc (top + 1);
- if (new_str == NULL)
- nonoption_flags_len = nonoption_flags_max_len = 0;
- else
- {
- memset (__mempcpy (new_str, __getopt_nonoption_flags,
- nonoption_flags_max_len),
- '\0', top + 1 - nonoption_flags_max_len);
- nonoption_flags_max_len = top + 1;
- __getopt_nonoption_flags = new_str;
- }
- }
- #endif
- while (top > middle && middle > bottom)
- {
- if (top - middle > middle - bottom)
- {
-
- int len = middle - bottom;
- register int i;
-
- for (i = 0; i < len; i++)
- {
- tem = argv[bottom + i];
- argv[bottom + i] = argv[top - (middle - bottom) + i];
- argv[top - (middle - bottom) + i] = tem;
- SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
- }
-
- top -= len;
- }
- else
- {
-
- int len = top - middle;
- register int i;
-
- for (i = 0; i < len; i++)
- {
- tem = argv[bottom + i];
- argv[bottom + i] = argv[middle + i];
- argv[middle + i] = tem;
- SWAP_FLAGS (bottom + i, middle + i);
- }
-
- bottom += len;
- }
- }
-
- first_nonopt += (optind - last_nonopt);
- last_nonopt = optind;
- }
- #if defined __STDC__ && __STDC__
- static const char *_getopt_initialize (int, char *const *, const char *);
- #endif
- static const char *
- _getopt_initialize (argc, argv, optstring)
- int argc;
- char *const *argv;
- const char *optstring;
- {
-
- first_nonopt = last_nonopt = optind;
- nextchar = NULL;
- posixly_correct = getenv ("POSIXLY_CORRECT");
-
- if (optstring[0] == '-')
- {
- ordering = RETURN_IN_ORDER;
- ++optstring;
- }
- else if (optstring[0] == '+')
- {
- ordering = REQUIRE_ORDER;
- ++optstring;
- }
- else if (posixly_correct != NULL)
- ordering = REQUIRE_ORDER;
- else
- ordering = PERMUTE;
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
- if (posixly_correct == NULL
- && argc == __libc_argc && argv == __libc_argv)
- {
- if (nonoption_flags_max_len == 0)
- {
- if (__getopt_nonoption_flags == NULL
- || __getopt_nonoption_flags[0] == '\0')
- nonoption_flags_max_len = -1;
- else
- {
- const char *orig_str = __getopt_nonoption_flags;
- int len = nonoption_flags_max_len = strlen (orig_str);
- if (nonoption_flags_max_len < argc)
- nonoption_flags_max_len = argc;
- __getopt_nonoption_flags =
- (char *) malloc (nonoption_flags_max_len);
- if (__getopt_nonoption_flags == NULL)
- nonoption_flags_max_len = -1;
- else
- memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
- '\0', nonoption_flags_max_len - len);
- }
- }
- nonoption_flags_len = nonoption_flags_max_len;
- }
- else
- nonoption_flags_len = 0;
- #endif
- return optstring;
- }
- int
- _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
- int argc;
- char *const *argv;
- const char *optstring;
- const struct option *longopts;
- int *longind;
- int long_only;
- {
- int print_errors = opterr;
- if (optstring[0] == ':')
- print_errors = 0;
- if (argc < 1)
- return -1;
- optarg = NULL;
- if (optind == 0 || !__getopt_initialized)
- {
- if (optind == 0)
- optind = 1;
- optstring = _getopt_initialize (argc, argv, optstring);
- __getopt_initialized = 1;
- }
-
- #if defined _LIBC && defined USE_NONOPTION_FLAGS
- # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \
- || (optind < nonoption_flags_len \
- && __getopt_nonoption_flags[optind] == '1'))
- #else
- # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
- #endif
- if (nextchar == NULL || *nextchar == '\0')
- {
- /* Advance to the next ARGV-element. */
- /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
- moved back by the user (who may also have changed the arguments). */
- if (last_nonopt > optind)
- last_nonopt = optind;
- if (first_nonopt > optind)
- first_nonopt = optind;
- if (ordering == PERMUTE)
- {
-
- if (first_nonopt != last_nonopt && last_nonopt != optind)
- exchange ((char **) argv);
- else if (last_nonopt != optind)
- first_nonopt = optind;
-
- while (optind < argc && NONOPTION_P)
- optind++;
- last_nonopt = optind;
- }
-
- if (optind != argc && !strcmp (argv[optind], "--"))
- {
- optind++;
- if (first_nonopt != last_nonopt && last_nonopt != optind)
- exchange ((char **) argv);
- else if (first_nonopt == last_nonopt)
- first_nonopt = optind;
- last_nonopt = argc;
- optind = argc;
- }
-
- if (optind == argc)
- {
-
- if (first_nonopt != last_nonopt)
- optind = first_nonopt;
- return -1;
- }
-
- if (NONOPTION_P)
- {
- if (ordering == REQUIRE_ORDER)
- return -1;
- optarg = argv[optind++];
- return 1;
- }
-
- nextchar = (argv[optind] + 1
- + (longopts != NULL && argv[optind][1] == '-'));
- }
-
-
- if (longopts != NULL
- && (argv[optind][1] == '-'
- || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
- {
- char *nameend;
- const struct option *p;
- const struct option *pfound = NULL;
- int exact = 0;
- int ambig = 0;
- int indfound = -1;
- int option_index;
- for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
- ;
-
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
- if (!strncmp (p->name, nextchar, nameend - nextchar))
- {
- if ((unsigned int) (nameend - nextchar)
- == (unsigned int) strlen (p->name))
- {
-
- pfound = p;
- indfound = option_index;
- exact = 1;
- break;
- }
- else if (pfound == NULL)
- {
-
- pfound = p;
- indfound = option_index;
- }
- else if (long_only
- || pfound->has_arg != p->has_arg
- || pfound->flag != p->flag
- || pfound->val != p->val)
-
- ambig = 1;
- }
- if (ambig && !exact)
- {
- if (print_errors)
- fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
- argv[0], argv[optind]);
- nextchar += strlen (nextchar);
- optind++;
- optopt = 0;
- return '?';
- }
- if (pfound != NULL)
- {
- option_index = indfound;
- optind++;
- if (*nameend)
- {
-
- if (pfound->has_arg)
- optarg = nameend + 1;
- else
- {
- if (print_errors)
- {
- if (argv[optind - 1][1] == '-')
-
- fprintf (stderr,
- _("%s: option `--%s' doesn't allow an argument\n"),
- argv[0], pfound->name);
- else
-
- fprintf (stderr,
- _("%s: option `%c%s' doesn't allow an argument\n"),
- argv[0], argv[optind - 1][0], pfound->name);
- }
- nextchar += strlen (nextchar);
- optopt = pfound->val;
- return '?';
- }
- }
- else if (pfound->has_arg == 1)
- {
- if (optind < argc)
- optarg = argv[optind++];
- else
- {
- if (print_errors)
- fprintf (stderr,
- _("%s: option `%s' requires an argument\n"),
- argv[0], argv[optind - 1]);
- nextchar += strlen (nextchar);
- optopt = pfound->val;
- return optstring[0] == ':' ? ':' : '?';
- }
- }
- nextchar += strlen (nextchar);
- if (longind != NULL)
- *longind = option_index;
- if (pfound->flag)
- {
- *(pfound->flag) = pfound->val;
- return 0;
- }
- return pfound->val;
- }
-
- if (!long_only || argv[optind][1] == '-'
- || my_index (optstring, *nextchar) == NULL)
- {
- if (print_errors)
- {
- if (argv[optind][1] == '-')
-
- fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
- argv[0], nextchar);
- else
-
- fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
- argv[0], argv[optind][0], nextchar);
- }
- nextchar = (char *) "";
- optind++;
- optopt = 0;
- return '?';
- }
- }
-
- {
- char c = *nextchar++;
- char *temp = my_index (optstring, c);
-
- if (*nextchar == '\0')
- ++optind;
- if (temp == NULL || c == ':')
- {
- if (print_errors)
- {
- if (posixly_correct)
-
- fprintf (stderr, _("%s: illegal option -- %c\n"),
- argv[0], c);
- else
- fprintf (stderr, _("%s: invalid option -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- return '?';
- }
-
- if (temp[0] == 'W' && temp[1] == ';')
- {
- char *nameend;
- const struct option *p;
- const struct option *pfound = NULL;
- int exact = 0;
- int ambig = 0;
- int indfound = 0;
- int option_index;
-
- if (*nextchar != '\0')
- {
- optarg = nextchar;
-
- optind++;
- }
- else if (optind == argc)
- {
- if (print_errors)
- {
-
- fprintf (stderr, _("%s: option requires an argument -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- if (optstring[0] == ':')
- c = ':';
- else
- c = '?';
- return c;
- }
- else
-
- optarg = argv[optind++];
-
- for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
- ;
-
- for (p = longopts, option_index = 0; p->name; p++, option_index++)
- if (!strncmp (p->name, nextchar, nameend - nextchar))
- {
- if ((unsigned int) (nameend - nextchar) == strlen (p->name))
- {
-
- pfound = p;
- indfound = option_index;
- exact = 1;
- break;
- }
- else if (pfound == NULL)
- {
-
- pfound = p;
- indfound = option_index;
- }
- else
-
- ambig = 1;
- }
- if (ambig && !exact)
- {
- if (print_errors)
- fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
- argv[0], argv[optind]);
- nextchar += strlen (nextchar);
- optind++;
- return '?';
- }
- if (pfound != NULL)
- {
- option_index = indfound;
- if (*nameend)
- {
-
- if (pfound->has_arg)
- optarg = nameend + 1;
- else
- {
- if (print_errors)
- fprintf (stderr, _("\
- %s: option `-W %s' doesn't allow an argument\n"),
- argv[0], pfound->name);
- nextchar += strlen (nextchar);
- return '?';
- }
- }
- else if (pfound->has_arg == 1)
- {
- if (optind < argc)
- optarg = argv[optind++];
- else
- {
- if (print_errors)
- fprintf (stderr,
- _("%s: option `%s' requires an argument\n"),
- argv[0], argv[optind - 1]);
- nextchar += strlen (nextchar);
- return optstring[0] == ':' ? ':' : '?';
- }
- }
- nextchar += strlen (nextchar);
- if (longind != NULL)
- *longind = option_index;
- if (pfound->flag)
- {
- *(pfound->flag) = pfound->val;
- return 0;
- }
- return pfound->val;
- }
- nextchar = NULL;
- return 'W';
- }
- if (temp[1] == ':')
- {
- if (temp[2] == ':')
- {
-
- if (*nextchar != '\0')
- {
- optarg = nextchar;
- optind++;
- }
- else
- optarg = NULL;
- nextchar = NULL;
- }
- else
- {
-
- if (*nextchar != '\0')
- {
- optarg = nextchar;
-
- optind++;
- }
- else if (optind == argc)
- {
- if (print_errors)
- {
-
- fprintf (stderr,
- _("%s: option requires an argument -- %c\n"),
- argv[0], c);
- }
- optopt = c;
- if (optstring[0] == ':')
- c = ':';
- else
- c = '?';
- }
- else
-
- optarg = argv[optind++];
- nextchar = NULL;
- }
- }
- return c;
- }
- }
- int
- getopt (argc, argv, optstring)
- int argc;
- char *const *argv;
- const char *optstring;
- {
- return _getopt_internal (argc, argv, optstring,
- (const struct option *) 0,
- (int *) 0,
- 0);
- }
- #endif
- #ifdef TEST
- int
- main (argc, argv)
- int argc;
- char **argv;
- {
- int c;
- int digit_optind = 0;
- while (1)
- {
- int this_option_optind = optind ? optind : 1;
- c = getopt (argc, argv, "abc:d:0123456789");
- if (c == -1)
- break;
- switch (c)
- {
- case '0':
- case '1':
- case '2':
- case '3':
- case '4':
- case '5':
- case '6':
- case '7':
- case '8':
- case '9':
- if (digit_optind != 0 && digit_optind != this_option_optind)
- printf ("digits occur in two different argv-elements.\n");
- digit_optind = this_option_optind;
- printf ("option %c\n", c);
- break;
- case 'a':
- printf ("option a\n");
- break;
- case 'b':
- printf ("option b\n");
- break;
- case 'c':
- printf ("option c with value `%s'\n", optarg);
- break;
- case '?':
- break;
- default:
- printf ("?? getopt returned character code 0%o ??\n", c);
- }
- }
- if (optind < argc)
- {
- printf ("non-option ARGV-elements: ");
- while (optind < argc)
- printf ("%s ", argv[optind++]);
- printf ("\n");
- }
- exit (0);
- }
- #endif
|