+++ /dev/null
-/* Getopt for GNU.\r
- NOTE: getopt is now part of the C library, so if you don't know what\r
- "Keep this file name-space clean" means, talk to drepper@gnu.org\r
- before changing it!\r
-\r
- Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99\r
- Free Software Foundation, Inc.\r
-\r
- The GNU C Library is free software; you can redistribute it and/or\r
- modify it under the terms of the GNU Library General Public License as\r
- published by the Free Software Foundation; either version 2 of the\r
- License, or (at your option) any later version.\r
-\r
- The GNU C Library is distributed in the hope that it will be useful,\r
- but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- Library General Public License for more details.\r
-\r
- You should have received a copy of the GNU Library General Public\r
- License along with the GNU C Library; see the file COPYING.LIB. If not,\r
- write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
- Boston, MA 02111-1307, USA. */\r
-\r
-/* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.\r
- Ditto for AIX 3.2 and <stdlib.h>. */\r
-#ifndef _NO_PROTO\r
-# define _NO_PROTO\r
-#endif\r
-\r
-#ifdef HAVE_CONFIG_H\r
-# include "config.h"\r
-#endif\r
-\r
-#if !defined __STDC__ || !__STDC__\r
-/* This is a separate conditional since some stdc systems\r
- reject `defined (const)'. */\r
-# ifndef const\r
-# define const\r
-# endif\r
-#endif\r
-\r
-#include <stdio.h>\r
-\r
-/* Comment out all this code if we are using the GNU C Library, and are not\r
- actually compiling the library itself. This code is part of the GNU C\r
- Library, but also included in many other GNU distributions. Compiling\r
- and linking in this code is a waste when using the GNU C library\r
- (especially if it is a shared library). Rather than having every GNU\r
- program understand `configure --with-gnu-libc' and omit the object files,\r
- it is simpler to just do this in the source for each such file. */\r
-\r
-#define GETOPT_INTERFACE_VERSION 2\r
-#if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2\r
-# include <gnu-versions.h>\r
-# if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION\r
-# define ELIDE_CODE\r
-# endif\r
-#endif\r
-\r
-#ifndef ELIDE_CODE\r
-\r
-\r
-/* This needs to come after some library #include\r
- to get __GNU_LIBRARY__ defined. */\r
-#ifdef __GNU_LIBRARY__\r
-/* Don't include stdlib.h for non-GNU C libraries because some of them\r
- contain conflicting prototypes for getopt. */\r
-# include <stdlib.h>\r
-# include <unistd.h>\r
-#endif /* GNU C library. */\r
-\r
-#ifdef VMS\r
-# include <unixlib.h>\r
-# if HAVE_STRING_H - 0\r
-# include <string.h>\r
-# endif\r
-#endif\r
-\r
-#ifndef _\r
-/* This is for other GNU distributions with internationalized messages.\r
- When compiling libc, the _ macro is predefined. */\r
-# ifdef HAVE_LIBINTL_H\r
-# include <libintl.h>\r
-# define _(msgid) gettext (msgid)\r
-# else\r
-# define _(msgid) (msgid)\r
-# endif\r
-#endif\r
-\r
-/* This version of `getopt' appears to the caller like standard Unix `getopt'\r
- but it behaves differently for the user, since it allows the user\r
- to intersperse the options with the other arguments.\r
-\r
- As `getopt' works, it permutes the elements of ARGV so that,\r
- when it is done, all the options precede everything else. Thus\r
- all application programs are extended to handle flexible argument order.\r
-\r
- Setting the environment variable POSIXLY_CORRECT disables permutation.\r
- Then the behavior is completely standard.\r
-\r
- GNU application programs can use a third alternative mode in which\r
- they can distinguish the relative order of options and other arguments. */\r
-\r
-#include "getopt_win.h"\r
-\r
-/* For communication from `getopt' to the caller.\r
- When `getopt' finds an option that takes an argument,\r
- the argument value is returned here.\r
- Also, when `ordering' is RETURN_IN_ORDER,\r
- each non-option ARGV-element is returned here. */\r
-\r
-char *optarg;\r
-\r
-/* Index in ARGV of the next element to be scanned.\r
- This is used for communication to and from the caller\r
- and for communication between successive calls to `getopt'.\r
-\r
- On entry to `getopt', zero means this is the first call; initialize.\r
-\r
- When `getopt' returns -1, this is the index of the first of the\r
- non-option elements that the caller should itself scan.\r
-\r
- Otherwise, `optind' communicates from one call to the next\r
- how much of ARGV has been scanned so far. */\r
-\r
-/* 1003.2 says this must be 1 before any call. */\r
-int optind = 1;\r
-\r
-/* Formerly, initialization of getopt depended on optind==0, which\r
- causes problems with re-calling getopt as programs generally don't\r
- know that. */\r
-\r
-int __getopt_initialized;\r
-\r
-/* The next char to be scanned in the option-element\r
- in which the last option character we returned was found.\r
- This allows us to pick up the scan where we left off.\r
-\r
- If this is zero, or a null string, it means resume the scan\r
- by advancing to the next ARGV-element. */\r
-\r
-static char *nextchar;\r
-\r
-/* Callers store zero here to inhibit the error message\r
- for unrecognized options. */\r
-\r
-int opterr = 1;\r
-\r
-/* Set to an option character which was unrecognized.\r
- This must be initialized on some systems to avoid linking in the\r
- system's own getopt implementation. */\r
-\r
-int optopt = '?';\r
-\r
-/* Describe how to deal with options that follow non-option ARGV-elements.\r
-\r
- If the caller did not specify anything,\r
- the default is REQUIRE_ORDER if the environment variable\r
- POSIXLY_CORRECT is defined, PERMUTE otherwise.\r
-\r
- REQUIRE_ORDER means don't recognize them as options;\r
- stop option processing when the first non-option is seen.\r
- This is what Unix does.\r
- This mode of operation is selected by either setting the environment\r
- variable POSIXLY_CORRECT, or using `+' as the first character\r
- of the list of option characters.\r
-\r
- PERMUTE is the default. We permute the contents of ARGV as we scan,\r
- so that eventually all the non-options are at the end. This allows options\r
- to be given in any order, even with programs that were not written to\r
- expect this.\r
-\r
- RETURN_IN_ORDER is an option available to programs that were written\r
- to expect options and other ARGV-elements in any order and that care about\r
- the ordering of the two. We describe each non-option ARGV-element\r
- as if it were the argument of an option with character code 1.\r
- Using `-' as the first character of the list of option characters\r
- selects this mode of operation.\r
-\r
- The special argument `--' forces an end of option-scanning regardless\r
- of the value of `ordering'. In the case of RETURN_IN_ORDER, only\r
- `--' can cause `getopt' to return -1 with `optind' != ARGC. */\r
-\r
-static enum\r
-{\r
- REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER\r
-} ordering;\r
-\r
-/* Value of POSIXLY_CORRECT environment variable. */\r
-static char *posixly_correct;\r
-\f\r
-#ifdef __GNU_LIBRARY__\r
-/* We want to avoid inclusion of string.h with non-GNU libraries\r
- because there are many ways it can cause trouble.\r
- On some systems, it contains special magic macros that don't work\r
- in GCC. */\r
-# include <string.h>\r
-# define my_index strchr\r
-#else\r
-\r
-#include <string.h>\r
-\r
-/* Avoid depending on library functions or files\r
- whose names are inconsistent. */\r
-\r
-#ifndef getenv\r
-extern char *getenv ();\r
-#endif\r
-\r
-static char *\r
-my_index (str, chr)\r
- const char *str;\r
- int chr;\r
-{\r
- while (*str)\r
- {\r
- if (*str == chr)\r
- return (char *) str;\r
- str++;\r
- }\r
- return 0;\r
-}\r
-\r
-/* If using GCC, we can safely declare strlen this way.\r
- If not using GCC, it is ok not to declare it. */\r
-#ifdef __GNUC__\r
-/* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.\r
- That was relevant to code that was here before. */\r
-# if (!defined __STDC__ || !__STDC__) && !defined strlen\r
-/* gcc with -traditional declares the built-in strlen to return int,\r
- and has done so at least since version 2.4.5. -- rms. */\r
-extern int strlen (const char *);\r
-# endif /* not __STDC__ */\r
-#endif /* __GNUC__ */\r
-\r
-#endif /* not __GNU_LIBRARY__ */\r
-\f\r
-/* Handle permutation of arguments. */\r
-\r
-/* Describe the part of ARGV that contains non-options that have\r
- been skipped. `first_nonopt' is the index in ARGV of the first of them;\r
- `last_nonopt' is the index after the last of them. */\r
-\r
-static int first_nonopt;\r
-static int last_nonopt;\r
-\r
-#ifdef _LIBC\r
-/* Bash 2.0 gives us an environment variable containing flags\r
- indicating ARGV elements that should not be considered arguments. */\r
-\r
-/* Defined in getopt_init.c */\r
-extern char *__getopt_nonoption_flags;\r
-\r
-static int nonoption_flags_max_len;\r
-static int nonoption_flags_len;\r
-\r
-static int original_argc;\r
-static char *const *original_argv;\r
-\r
-/* Make sure the environment variable bash 2.0 puts in the environment\r
- is valid for the getopt call we must make sure that the ARGV passed\r
- to getopt is that one passed to the process. */\r
-static void\r
-__attribute__ ((unused))\r
-store_args_and_env (int argc, char *const *argv)\r
-{\r
- /* XXX This is no good solution. We should rather copy the args so\r
- that we can compare them later. But we must not use malloc(3). */\r
- original_argc = argc;\r
- original_argv = argv;\r
-}\r
-# ifdef text_set_element\r
-text_set_element (__libc_subinit, store_args_and_env);\r
-# endif /* text_set_element */\r
-\r
-# define SWAP_FLAGS(ch1, ch2) \\r
- if (nonoption_flags_len > 0) \\r
- { \\r
- char __tmp = __getopt_nonoption_flags[ch1]; \\r
- __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2]; \\r
- __getopt_nonoption_flags[ch2] = __tmp; \\r
- }\r
-#else /* !_LIBC */\r
-# define SWAP_FLAGS(ch1, ch2)\r
-#endif /* _LIBC */\r
-\r
-/* Exchange two adjacent subsequences of ARGV.\r
- One subsequence is elements [first_nonopt,last_nonopt)\r
- which contains all the non-options that have been skipped so far.\r
- The other is elements [last_nonopt,optind), which contains all\r
- the options processed since those non-options were skipped.\r
-\r
- `first_nonopt' and `last_nonopt' are relocated so that they describe\r
- the new indices of the non-options in ARGV after they are moved. */\r
-\r
-#if defined __STDC__ && __STDC__\r
-static void exchange (char **);\r
-#endif\r
-\r
-static void\r
-exchange (argv)\r
- char **argv;\r
-{\r
- int bottom = first_nonopt;\r
- int middle = last_nonopt;\r
- int top = optind;\r
- char *tem;\r
-\r
- /* Exchange the shorter segment with the far end of the longer segment.\r
- That puts the shorter segment into the right place.\r
- It leaves the longer segment in the right place overall,\r
- but it consists of two parts that need to be swapped next. */\r
-\r
-#ifdef _LIBC\r
- /* First make sure the handling of the `__getopt_nonoption_flags'\r
- string can work normally. Our top argument must be in the range\r
- of the string. */\r
- if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)\r
- {\r
- /* We must extend the array. The user plays games with us and\r
- presents new arguments. */\r
- char *new_str = malloc (top + 1);\r
- if (new_str == NULL)\r
- nonoption_flags_len = nonoption_flags_max_len = 0;\r
- else\r
- {\r
- memset (__mempcpy (new_str, __getopt_nonoption_flags,\r
- nonoption_flags_max_len),\r
- '\0', top + 1 - nonoption_flags_max_len);\r
- nonoption_flags_max_len = top + 1;\r
- __getopt_nonoption_flags = new_str;\r
- }\r
- }\r
-#endif\r
-\r
- while (top > middle && middle > bottom)\r
- {\r
- if (top - middle > middle - bottom)\r
- {\r
- /* Bottom segment is the short one. */\r
- int len = middle - bottom;\r
- register int i;\r
-\r
- /* Swap it with the top part of the top segment. */\r
- for (i = 0; i < len; i++)\r
- {\r
- tem = argv[bottom + i];\r
- argv[bottom + i] = argv[top - (middle - bottom) + i];\r
- argv[top - (middle - bottom) + i] = tem;\r
- SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);\r
- }\r
- /* Exclude the moved bottom segment from further swapping. */\r
- top -= len;\r
- }\r
- else\r
- {\r
- /* Top segment is the short one. */\r
- int len = top - middle;\r
- register int i;\r
-\r
- /* Swap it with the bottom part of the bottom segment. */\r
- for (i = 0; i < len; i++)\r
- {\r
- tem = argv[bottom + i];\r
- argv[bottom + i] = argv[middle + i];\r
- argv[middle + i] = tem;\r
- SWAP_FLAGS (bottom + i, middle + i);\r
- }\r
- /* Exclude the moved top segment from further swapping. */\r
- bottom += len;\r
- }\r
- }\r
-\r
- /* Update records for the slots the non-options now occupy. */\r
-\r
- first_nonopt += (optind - last_nonopt);\r
- last_nonopt = optind;\r
-}\r
-\r
-/* Initialize the internal data when the first call is made. */\r
-\r
-#if defined __STDC__ && __STDC__\r
-static const char *_getopt_initialize (int, char *const *, const char *);\r
-#endif\r
-static const char *\r
-_getopt_initialize (argc, argv, optstring)\r
- int argc;\r
- char *const *argv;\r
- const char *optstring;\r
-{\r
- /* Start processing options with ARGV-element 1 (since ARGV-element 0\r
- is the program name); the sequence of previously skipped\r
- non-option ARGV-elements is empty. */\r
-\r
- first_nonopt = last_nonopt = optind;\r
-\r
- nextchar = NULL;\r
-\r
- posixly_correct = getenv ("POSIXLY_CORRECT");\r
-\r
- /* Determine how to handle the ordering of options and nonoptions. */\r
-\r
- if (optstring[0] == '-')\r
- {\r
- ordering = RETURN_IN_ORDER;\r
- ++optstring;\r
- }\r
- else if (optstring[0] == '+')\r
- {\r
- ordering = REQUIRE_ORDER;\r
- ++optstring;\r
- }\r
- else if (posixly_correct != NULL)\r
- ordering = REQUIRE_ORDER;\r
- else\r
- ordering = PERMUTE;\r
-\r
-#ifdef _LIBC\r
- if (posixly_correct == NULL\r
- && argc == original_argc && argv == original_argv)\r
- {\r
- if (nonoption_flags_max_len == 0)\r
- {\r
- if (__getopt_nonoption_flags == NULL\r
- || __getopt_nonoption_flags[0] == '\0')\r
- nonoption_flags_max_len = -1;\r
- else\r
- {\r
- const char *orig_str = __getopt_nonoption_flags;\r
- int len = nonoption_flags_max_len = strlen (orig_str);\r
- if (nonoption_flags_max_len < argc)\r
- nonoption_flags_max_len = argc;\r
- __getopt_nonoption_flags =\r
- (char *) malloc (nonoption_flags_max_len);\r
- if (__getopt_nonoption_flags == NULL)\r
- nonoption_flags_max_len = -1;\r
- else\r
- memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),\r
- '\0', nonoption_flags_max_len - len);\r
- }\r
- }\r
- nonoption_flags_len = nonoption_flags_max_len;\r
- }\r
- else\r
- nonoption_flags_len = 0;\r
-#endif\r
-\r
- return optstring;\r
-}\r
-\f\r
-/* Scan elements of ARGV (whose length is ARGC) for option characters\r
- given in OPTSTRING.\r
-\r
- If an element of ARGV starts with '-', and is not exactly "-" or "--",\r
- then it is an option element. The characters of this element\r
- (aside from the initial '-') are option characters. If `getopt'\r
- is called repeatedly, it returns successively each of the option characters\r
- from each of the option elements.\r
-\r
- If `getopt' finds another option character, it returns that character,\r
- updating `optind' and `nextchar' so that the next call to `getopt' can\r
- resume the scan with the following option character or ARGV-element.\r
-\r
- If there are no more option characters, `getopt' returns -1.\r
- Then `optind' is the index in ARGV of the first ARGV-element\r
- that is not an option. (The ARGV-elements have been permuted\r
- so that those that are not options now come last.)\r
-\r
- OPTSTRING is a string containing the legitimate option characters.\r
- If an option character is seen that is not listed in OPTSTRING,\r
- return '?' after printing an error message. If you set `opterr' to\r
- zero, the error message is suppressed but we still return '?'.\r
-\r
- If a char in OPTSTRING is followed by a colon, that means it wants an arg,\r
- so the following text in the same ARGV-element, or the text of the following\r
- ARGV-element, is returned in `optarg'. Two colons mean an option that\r
- wants an optional arg; if there is text in the current ARGV-element,\r
- it is returned in `optarg', otherwise `optarg' is set to zero.\r
-\r
- If OPTSTRING starts with `-' or `+', it requests different methods of\r
- handling the non-option ARGV-elements.\r
- See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.\r
-\r
- Long-named options begin with `--' instead of `-'.\r
- Their names may be abbreviated as long as the abbreviation is unique\r
- or is an exact match for some defined option. If they have an\r
- argument, it follows the option name in the same ARGV-element, separated\r
- from the option name by a `=', or else the in next ARGV-element.\r
- When `getopt' finds a long-named option, it returns 0 if that option's\r
- `flag' field is nonzero, the value of the option's `val' field\r
- if the `flag' field is zero.\r
-\r
- The elements of ARGV aren't really const, because we permute them.\r
- But we pretend they're const in the prototype to be compatible\r
- with other systems.\r
-\r
- LONGOPTS is a vector of `struct option' terminated by an\r
- element containing a name which is zero.\r
-\r
- LONGIND returns the index in LONGOPT of the long-named option found.\r
- It is only valid when a long-named option has been found by the most\r
- recent call.\r
-\r
- If LONG_ONLY is nonzero, '-' as well as '--' can introduce\r
- long-named options. */\r
-\r
-int\r
-_getopt_internal (argc, argv, optstring, longopts, longind, long_only)\r
- int argc;\r
- char *const *argv;\r
- const char *optstring;\r
- const struct option *longopts;\r
- int *longind;\r
- int long_only;\r
-{\r
- optarg = NULL;\r
-\r
- if (optind == 0 || !__getopt_initialized)\r
- {\r
- if (optind == 0)\r
- optind = 1; /* Don't scan ARGV[0], the program name. */\r
- optstring = _getopt_initialize (argc, argv, optstring);\r
- __getopt_initialized = 1;\r
- }\r
-\r
- /* Test whether ARGV[optind] points to a non-option argument.\r
- Either it does not have option syntax, or there is an environment flag\r
- from the shell indicating it is not an option. The later information\r
- is only used when the used in the GNU libc. */\r
-#ifdef _LIBC\r
-# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0' \\r
- || (optind < nonoption_flags_len \\r
- && __getopt_nonoption_flags[optind] == '1'))\r
-#else\r
-# define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')\r
-#endif\r
-\r
- if (nextchar == NULL || *nextchar == '\0')\r
- {\r
- /* Advance to the next ARGV-element. */\r
-\r
- /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been\r
- moved back by the user (who may also have changed the arguments). */\r
- if (last_nonopt > optind)\r
- last_nonopt = optind;\r
- if (first_nonopt > optind)\r
- first_nonopt = optind;\r
-\r
- if (ordering == PERMUTE)\r
- {\r
- /* If we have just processed some options following some non-options,\r
- exchange them so that the options come first. */\r
-\r
- if (first_nonopt != last_nonopt && last_nonopt != optind)\r
- exchange ((char **) argv);\r
- else if (last_nonopt != optind)\r
- first_nonopt = optind;\r
-\r
- /* Skip any additional non-options\r
- and extend the range of non-options previously skipped. */\r
-\r
- while (optind < argc && NONOPTION_P)\r
- optind++;\r
- last_nonopt = optind;\r
- }\r
-\r
- /* The special ARGV-element `--' means premature end of options.\r
- Skip it like a null option,\r
- then exchange with previous non-options as if it were an option,\r
- then skip everything else like a non-option. */\r
-\r
- if (optind != argc && !strcmp (argv[optind], "--"))\r
- {\r
- optind++;\r
-\r
- if (first_nonopt != last_nonopt && last_nonopt != optind)\r
- exchange ((char **) argv);\r
- else if (first_nonopt == last_nonopt)\r
- first_nonopt = optind;\r
- last_nonopt = argc;\r
-\r
- optind = argc;\r
- }\r
-\r
- /* If we have done all the ARGV-elements, stop the scan\r
- and back over any non-options that we skipped and permuted. */\r
-\r
- if (optind == argc)\r
- {\r
- /* Set the next-arg-index to point at the non-options\r
- that we previously skipped, so the caller will digest them. */\r
- if (first_nonopt != last_nonopt)\r
- optind = first_nonopt;\r
- return -1;\r
- }\r
-\r
- /* If we have come to a non-option and did not permute it,\r
- either stop the scan or describe it to the caller and pass it by. */\r
-\r
- if (NONOPTION_P)\r
- {\r
- if (ordering == REQUIRE_ORDER)\r
- return -1;\r
- optarg = argv[optind++];\r
- return 1;\r
- }\r
-\r
- /* We have found another option-ARGV-element.\r
- Skip the initial punctuation. */\r
-\r
- nextchar = (argv[optind] + 1\r
- + (longopts != NULL && argv[optind][1] == '-'));\r
- }\r
-\r
- /* Decode the current option-ARGV-element. */\r
-\r
- /* Check whether the ARGV-element is a long option.\r
-\r
- If long_only and the ARGV-element has the form "-f", where f is\r
- a valid short option, don't consider it an abbreviated form of\r
- a long option that starts with f. Otherwise there would be no\r
- way to give the -f short option.\r
-\r
- On the other hand, if there's a long option "fubar" and\r
- the ARGV-element is "-fu", do consider that an abbreviation of\r
- the long option, just like "--fu", and not "-f" with arg "u".\r
-\r
- This distinction seems to be the most useful approach. */\r
-\r
- if (longopts != NULL\r
- && (argv[optind][1] == '-'\r
- || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))\r
- {\r
- char *nameend;\r
- const struct option *p;\r
- const struct option *pfound = NULL;\r
- int exact = 0;\r
- int ambig = 0;\r
- int indfound = -1;\r
- int option_index;\r
-\r
- for (nameend = nextchar; *nameend && *nameend != '='; nameend++)\r
- /* Do nothing. */ ;\r
-\r
- /* Test all long options for either exact match\r
- or abbreviated matches. */\r
- for (p = longopts, option_index = 0; p->name; p++, option_index++)\r
- if (!strncmp (p->name, nextchar, nameend - nextchar))\r
- {\r
- if ((unsigned int) (nameend - nextchar)\r
- == (unsigned int) strlen (p->name))\r
- {\r
- /* Exact match found. */\r
- pfound = p;\r
- indfound = option_index;\r
- exact = 1;\r
- break;\r
- }\r
- else if (pfound == NULL)\r
- {\r
- /* First nonexact match found. */\r
- pfound = p;\r
- indfound = option_index;\r
- }\r
- else\r
- /* Second or later nonexact match found. */\r
- ambig = 1;\r
- }\r
-\r
- if (ambig && !exact)\r
- {\r
- if (opterr)\r
- fprintf (stderr, _("%s: option `%s' is ambiguous\n"),\r
- argv[0], argv[optind]);\r
- nextchar += strlen (nextchar);\r
- optind++;\r
- optopt = 0;\r
- return '?';\r
- }\r
-\r
- if (pfound != NULL)\r
- {\r
- option_index = indfound;\r
- optind++;\r
- if (*nameend)\r
- {\r
- /* Don't test has_arg with >, because some C compilers don't\r
- allow it to be used on enums. */\r
- if (pfound->has_arg)\r
- optarg = nameend + 1;\r
- else\r
- {\r
- if (opterr)\r
- {\r
- if (argv[optind - 1][1] == '-')\r
- /* --option */\r
- fprintf (stderr,\r
- _("%s: option `--%s' doesn't allow an argument\n"),\r
- argv[0], pfound->name);\r
- else\r
- /* +option or -option */\r
- fprintf (stderr,\r
- _("%s: option `%c%s' doesn't allow an argument\n"),\r
- argv[0], argv[optind - 1][0], pfound->name);\r
- }\r
-\r
- nextchar += strlen (nextchar);\r
-\r
- optopt = pfound->val;\r
- return '?';\r
- }\r
- }\r
- else if (pfound->has_arg == 1)\r
- {\r
- if (optind < argc)\r
- optarg = argv[optind++];\r
- else\r
- {\r
- if (opterr)\r
- fprintf (stderr,\r
- _("%s: option `%s' requires an argument\n"),\r
- argv[0], argv[optind - 1]);\r
- nextchar += strlen (nextchar);\r
- optopt = pfound->val;\r
- return optstring[0] == ':' ? ':' : '?';\r
- }\r
- }\r
- nextchar += strlen (nextchar);\r
- if (longind != NULL)\r
- *longind = option_index;\r
- if (pfound->flag)\r
- {\r
- *(pfound->flag) = pfound->val;\r
- return 0;\r
- }\r
- return pfound->val;\r
- }\r
-\r
- /* Can't find it as a long option. If this is not getopt_long_only,\r
- or the option starts with '--' or is not a valid short\r
- option, then it's an error.\r
- Otherwise interpret it as a short option. */\r
- if (!long_only || argv[optind][1] == '-'\r
- || my_index (optstring, *nextchar) == NULL)\r
- {\r
- if (opterr)\r
- {\r
- if (argv[optind][1] == '-')\r
- /* --option */\r
- fprintf (stderr, _("%s: unrecognized option `--%s'\n"),\r
- argv[0], nextchar);\r
- else\r
- /* +option or -option */\r
- fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),\r
- argv[0], argv[optind][0], nextchar);\r
- }\r
- nextchar = (char *) "";\r
- optind++;\r
- optopt = 0;\r
- return '?';\r
- }\r
- }\r
-\r
- /* Look at and handle the next short option-character. */\r
-\r
- {\r
- char c = *nextchar++;\r
- char *temp = my_index (optstring, c);\r
-\r
- /* Increment `optind' when we start to process its last character. */\r
- if (*nextchar == '\0')\r
- ++optind;\r
-\r
- if (temp == NULL || c == ':')\r
- {\r
- if (opterr)\r
- {\r
- if (posixly_correct)\r
- /* 1003.2 specifies the format of this message. */\r
- fprintf (stderr, _("%s: illegal option -- %c\n"),\r
- argv[0], c);\r
- else\r
- fprintf (stderr, _("%s: invalid option -- %c\n"),\r
- argv[0], c);\r
- }\r
- optopt = c;\r
- return '?';\r
- }\r
- /* Convenience. Treat POSIX -W foo same as long option --foo */\r
- if (temp[0] == 'W' && temp[1] == ';')\r
- {\r
- char *nameend;\r
- const struct option *p;\r
- const struct option *pfound = NULL;\r
- int exact = 0;\r
- int ambig = 0;\r
- int indfound = 0;\r
- int option_index;\r
-\r
- /* This is an option that requires an argument. */\r
- if (*nextchar != '\0')\r
- {\r
- optarg = nextchar;\r
- /* If we end this ARGV-element by taking the rest as an arg,\r
- we must advance to the next element now. */\r
- optind++;\r
- }\r
- else if (optind == argc)\r
- {\r
- if (opterr)\r
- {\r
- /* 1003.2 specifies the format of this message. */\r
- fprintf (stderr, _("%s: option requires an argument -- %c\n"),\r
- argv[0], c);\r
- }\r
- optopt = c;\r
- if (optstring[0] == ':')\r
- c = ':';\r
- else\r
- c = '?';\r
- return c;\r
- }\r
- else\r
- /* We already incremented `optind' once;\r
- increment it again when taking next ARGV-elt as argument. */\r
- optarg = argv[optind++];\r
-\r
- /* optarg is now the argument, see if it's in the\r
- table of longopts. */\r
-\r
- for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)\r
- /* Do nothing. */ ;\r
-\r
- /* Test all long options for either exact match\r
- or abbreviated matches. */\r
- for (p = longopts, option_index = 0; p->name; p++, option_index++)\r
- if (!strncmp (p->name, nextchar, nameend - nextchar))\r
- {\r
- if ((unsigned int) (nameend - nextchar) == strlen (p->name))\r
- {\r
- /* Exact match found. */\r
- pfound = p;\r
- indfound = option_index;\r
- exact = 1;\r
- break;\r
- }\r
- else if (pfound == NULL)\r
- {\r
- /* First nonexact match found. */\r
- pfound = p;\r
- indfound = option_index;\r
- }\r
- else\r
- /* Second or later nonexact match found. */\r
- ambig = 1;\r
- }\r
- if (ambig && !exact)\r
- {\r
- if (opterr)\r
- fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),\r
- argv[0], argv[optind]);\r
- nextchar += strlen (nextchar);\r
- optind++;\r
- return '?';\r
- }\r
- if (pfound != NULL)\r
- {\r
- option_index = indfound;\r
- if (*nameend)\r
- {\r
- /* Don't test has_arg with >, because some C compilers don't\r
- allow it to be used on enums. */\r
- if (pfound->has_arg)\r
- optarg = nameend + 1;\r
- else\r
- {\r
- if (opterr)\r
- fprintf (stderr, _("\\r
-%s: option `-W %s' doesn't allow an argument\n"),\r
- argv[0], pfound->name);\r
-\r
- nextchar += strlen (nextchar);\r
- return '?';\r
- }\r
- }\r
- else if (pfound->has_arg == 1)\r
- {\r
- if (optind < argc)\r
- optarg = argv[optind++];\r
- else\r
- {\r
- if (opterr)\r
- fprintf (stderr,\r
- _("%s: option `%s' requires an argument\n"),\r
- argv[0], argv[optind - 1]);\r
- nextchar += strlen (nextchar);\r
- return optstring[0] == ':' ? ':' : '?';\r
- }\r
- }\r
- nextchar += strlen (nextchar);\r
- if (longind != NULL)\r
- *longind = option_index;\r
- if (pfound->flag)\r
- {\r
- *(pfound->flag) = pfound->val;\r
- return 0;\r
- }\r
- return pfound->val;\r
- }\r
- nextchar = NULL;\r
- return 'W'; /* Let the application handle it. */\r
- }\r
- if (temp[1] == ':')\r
- {\r
- if (temp[2] == ':')\r
- {\r
- /* This is an option that accepts an argument optionally. */\r
- if (*nextchar != '\0')\r
- {\r
- optarg = nextchar;\r
- optind++;\r
- }\r
- else\r
- optarg = NULL;\r
- nextchar = NULL;\r
- }\r
- else\r
- {\r
- /* This is an option that requires an argument. */\r
- if (*nextchar != '\0')\r
- {\r
- optarg = nextchar;\r
- /* If we end this ARGV-element by taking the rest as an arg,\r
- we must advance to the next element now. */\r
- optind++;\r
- }\r
- else if (optind == argc)\r
- {\r
- if (opterr)\r
- {\r
- /* 1003.2 specifies the format of this message. */\r
- fprintf (stderr,\r
- _("%s: option requires an argument -- %c\n"),\r
- argv[0], c);\r
- }\r
- optopt = c;\r
- if (optstring[0] == ':')\r
- c = ':';\r
- else\r
- c = '?';\r
- }\r
- else\r
- /* We already incremented `optind' once;\r
- increment it again when taking next ARGV-elt as argument. */\r
- optarg = argv[optind++];\r
- nextchar = NULL;\r
- }\r
- }\r
- return c;\r
- }\r
-}\r
-\r
-int\r
-getopt (argc, argv, optstring)\r
- int argc;\r
- char *const *argv;\r
- const char *optstring;\r
-{\r
- return _getopt_internal (argc, argv, optstring,\r
- (const struct option *) 0,\r
- (int *) 0,\r
- 0);\r
-}\r
-\r
-#endif /* Not ELIDE_CODE. */\r
-\f\r
-#ifdef TEST\r
-\r
-/* Compile with -DTEST to make an executable for use in testing\r
- the above definition of `getopt'. */\r
-\r
-int\r
-main (argc, argv)\r
- int argc;\r
- char **argv;\r
-{\r
- int c;\r
- int digit_optind = 0;\r
-\r
- while (1)\r
- {\r
- int this_option_optind = optind ? optind : 1;\r
-\r
- c = getopt (argc, argv, "abc:d:0123456789");\r
- if (c == -1)\r
- break;\r
-\r
- switch (c)\r
- {\r
- case '0':\r
- case '1':\r
- case '2':\r
- case '3':\r
- case '4':\r
- case '5':\r
- case '6':\r
- case '7':\r
- case '8':\r
- case '9':\r
- if (digit_optind != 0 && digit_optind != this_option_optind)\r
- printf ("digits occur in two different argv-elements.\n");\r
- digit_optind = this_option_optind;\r
- printf ("option %c\n", c);\r
- break;\r
-\r
- case 'a':\r
- printf ("option a\n");\r
- break;\r
-\r
- case 'b':\r
- printf ("option b\n");\r
- break;\r
-\r
- case 'c':\r
- printf ("option c with value `%s'\n", optarg);\r
- break;\r
-\r
- case '?':\r
- break;\r
-\r
- default:\r
- printf ("?? getopt returned character code 0%o ??\n", c);\r
- }\r
- }\r
-\r
- if (optind < argc)\r
- {\r
- printf ("non-option ARGV-elements: ");\r
- while (optind < argc)\r
- printf ("%s ", argv[optind++]);\r
- printf ("\n");\r
- }\r
-\r
- exit (0);\r
-}\r
-\r
-#endif /* TEST */\r
+++ /dev/null
-/* Declarations for getopt.\r
- Copyright (C) 1989,90,91,92,93,94,96,97,98 Free Software Foundation, Inc.\r
- This file is part of the GNU C Library.\r
-\r
- The GNU C Library is free software; you can redistribute it and/or\r
- modify it under the terms of the GNU Library General Public License as\r
- published by the Free Software Foundation; either version 2 of the\r
- License, or (at your option) any later version.\r
-\r
- The GNU C Library is distributed in the hope that it will be useful,\r
- but WITHOUT ANY WARRANTY; without even the implied warranty of\r
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
- Library General Public License for more details.\r
-\r
- You should have received a copy of the GNU Library General Public\r
- License along with the GNU C Library; see the file COPYING.LIB. If not,\r
- write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,\r
- Boston, MA 02111-1307, USA. */\r
-\r
-#ifndef _GETOPT_H\r
-\r
-#ifndef __need_getopt\r
-# define _GETOPT_H 1\r
-#endif\r
-\r
-#ifdef __cplusplus\r
-extern "C" {\r
-#endif\r
-\r
-/* For communication from `getopt' to the caller.\r
- When `getopt' finds an option that takes an argument,\r
- the argument value is returned here.\r
- Also, when `ordering' is RETURN_IN_ORDER,\r
- each non-option ARGV-element is returned here. */\r
-\r
-extern char *optarg;\r
-\r
-/* Index in ARGV of the next element to be scanned.\r
- This is used for communication to and from the caller\r
- and for communication between successive calls to `getopt'.\r
-\r
- On entry to `getopt', zero means this is the first call; initialize.\r
-\r
- When `getopt' returns -1, this is the index of the first of the\r
- non-option elements that the caller should itself scan.\r
-\r
- Otherwise, `optind' communicates from one call to the next\r
- how much of ARGV has been scanned so far. */\r
-\r
-extern int optind;\r
-\r
-/* Callers store zero here to inhibit the error message `getopt' prints\r
- for unrecognized options. */\r
-\r
-extern int opterr;\r
-\r
-/* Set to an option character which was unrecognized. */\r
-\r
-extern int optopt;\r
-\r
-#ifndef __need_getopt\r
-/* Describe the long-named options requested by the application.\r
- The LONG_OPTIONS argument to getopt_long or getopt_long_only is a vector\r
- of `struct option' terminated by an element containing a name which is\r
- zero.\r
-\r
- The field `has_arg' is:\r
- no_argument (or 0) if the option does not take an argument,\r
- required_argument (or 1) if the option requires an argument,\r
- optional_argument (or 2) if the option takes an optional argument.\r
-\r
- If the field `flag' is not NULL, it points to a variable that is set\r
- to the value given in the field `val' when the option is found, but\r
- left unchanged if the option is not found.\r
-\r
- To have a long-named option do something other than set an `int' to\r
- a compiled-in constant, such as set a value from `optarg', set the\r
- option's `flag' field to zero and its `val' field to a nonzero\r
- value (the equivalent single-letter option character, if there is\r
- one). For long options that have a zero `flag' field, `getopt'\r
- returns the contents of the `val' field. */\r
-\r
-struct option\r
-{\r
-# if defined __STDC__ && __STDC__\r
- const char *name;\r
-# else\r
- char *name;\r
-# endif\r
- /* has_arg can't be an enum because some compilers complain about\r
- type mismatches in all the code that assumes it is an int. */\r
- int has_arg;\r
- int *flag;\r
- int val;\r
-};\r
-\r
-/* Names for the values of the `has_arg' field of `struct option'. */\r
-\r
-# define no_argument 0\r
-# define required_argument 1\r
-# define optional_argument 2\r
-#endif /* need getopt */\r
-\r
-\r
-/* Get definitions and prototypes for functions to process the\r
- arguments in ARGV (ARGC of them, minus the program name) for\r
- options given in OPTS.\r
-\r
- Return the option character from OPTS just read. Return -1 when\r
- there are no more options. For unrecognized options, or options\r
- missing arguments, `optopt' is set to the option letter, and '?' is\r
- returned.\r
-\r
- The OPTS string is a list of characters which are recognized option\r
- letters, optionally followed by colons, specifying that that letter\r
- takes an argument, to be placed in `optarg'.\r
-\r
- If a letter in OPTS is followed by two colons, its argument is\r
- optional. This behavior is specific to the GNU `getopt'.\r
-\r
- The argument `--' causes premature termination of argument\r
- scanning, explicitly telling `getopt' that there are no more\r
- options.\r
-\r
- If OPTS begins with `--', then non-option arguments are treated as\r
- arguments to the option '\0'. This behavior is specific to the GNU\r
- `getopt'. */\r
-\r
-#if defined __STDC__ && __STDC__\r
-# ifdef __GNU_LIBRARY__\r
-/* Many other libraries have conflicting prototypes for getopt, with\r
- differences in the consts, in stdlib.h. To avoid compilation\r
- errors, only prototype getopt for the GNU C library. */\r
-extern int getopt (int __argc, char *const *__argv, const char *__shortopts);\r
-# else /* not __GNU_LIBRARY__ */\r
-extern int getopt ();\r
-# endif /* __GNU_LIBRARY__ */\r
-\r
-# ifndef __need_getopt\r
-extern int getopt_long (int __argc, char *const *__argv, const char *__shortopts,\r
- const struct option *__longopts, int *__longind);\r
-extern int getopt_long_only (int __argc, char *const *__argv,\r
- const char *__shortopts,\r
- const struct option *__longopts, int *__longind);\r
-\r
-/* Internal only. Users should not call this directly. */\r
-extern int _getopt_internal (int __argc, char *const *__argv,\r
- const char *__shortopts,\r
- const struct option *__longopts, int *__longind,\r
- int __long_only);\r
-# endif\r
-#else /* not __STDC__ */\r
-extern int getopt ();\r
-# ifndef __need_getopt\r
-extern int getopt_long ();\r
-extern int getopt_long_only ();\r
-\r
-extern int _getopt_internal ();\r
-# endif\r
-#endif /* __STDC__ */\r
-\r
-#ifdef __cplusplus\r
-}\r
-#endif\r
-\r
-/* Make sure we later can get all the definitions and declarations. */\r
-#undef __need_getopt\r
-\r
-#endif /* getopt.h */\r