OpenCOBOL 1.1pre-rel
getopt.c
Go to the documentation of this file.
00001 /* Getopt for GNU.
00002    NOTE: getopt is now part of the C library, so if you don't know what
00003    "Keep this file name-space clean" means, talk to drepper@gnu.org
00004    before changing it!
00005    Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002
00006         Free Software Foundation, Inc.
00007    This file is part of the GNU C Library.
00008 
00009    The GNU C Library is free software; you can redistribute it and/or
00010    modify it under the terms of the GNU Lesser General Public
00011    License as published by the Free Software Foundation; either
00012    version 2.1 of the License, or (at your option) any later version.
00013 
00014    The GNU C Library is distributed in the hope that it will be useful,
00015    but WITHOUT ANY WARRANTY; without even the implied warranty of
00016    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017    Lesser General Public License for more details.
00018 
00019    You should have received a copy of the GNU Lesser General Public
00020    License along with the GNU C Library; if not, write to
00021    the Free Software Foundation, 51 Franklin Street, Fifth Floor
00022    Boston, MA 02110-1301 USA */
00023 
00024 /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
00025    Ditto for AIX 3.2 and <stdlib.h>.  */
00026 #ifndef _NO_PROTO
00027 # define _NO_PROTO
00028 #endif
00029 
00030 #ifdef HAVE_CONFIG_H
00031 # include <config.h>
00032 #endif
00033 
00034 #if !defined __STDC__ || !__STDC__
00035 /* This is a separate conditional since some stdc systems
00036    reject `defined (const)'.  */
00037 # ifndef const
00038 #  define const
00039 # endif
00040 #endif
00041 
00042 #include <stdio.h>
00043 
00044 /* Comment out all this code if we are using the GNU C Library, and are not
00045    actually compiling the library itself.  This code is part of the GNU C
00046    Library, but also included in many other GNU distributions.  Compiling
00047    and linking in this code is a waste when using the GNU C library
00048    (especially if it is a shared library).  Rather than having every GNU
00049    program understand `configure --with-gnu-libc' and omit the object files,
00050    it is simpler to just do this in the source for each such file.  */
00051 
00052 #define GETOPT_INTERFACE_VERSION 2
00053 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2
00054 # include <gnu-versions.h>
00055 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION
00056 #  define ELIDE_CODE
00057 # endif
00058 #endif
00059 
00060 #ifndef ELIDE_CODE
00061 
00062 
00063 /* This needs to come after some library #include
00064    to get __GNU_LIBRARY__ defined.  */
00065 #ifdef  __GNU_LIBRARY__
00066 /* Don't include stdlib.h for non-GNU C libraries because some of them
00067    contain conflicting prototypes for getopt.  */
00068 # include <stdlib.h>
00069 #ifdef  HAVE_UNISTD_H
00070 # include <unistd.h>
00071 #endif
00072 #endif  /* GNU C library.  */
00073 
00074 #ifdef VMS
00075 # include <unixlib.h>
00076 # if HAVE_STRING_H - 0
00077 #  include <string.h>
00078 # endif
00079 #endif
00080 
00081 #ifndef _
00082 /* This is for other GNU distributions with internationalized messages.  */
00083 # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
00084 #  include <libintl.h>
00085 #  ifndef _
00086 #   define _(msgid)     gettext (msgid)
00087 #  endif
00088 # else
00089 #  define _(msgid)      (msgid)
00090 # endif
00091 # if defined _LIBC && defined USE_IN_LIBIO
00092 #  include <wchar.h>
00093 # endif
00094 #endif
00095 
00096 #ifndef attribute_hidden
00097 # define attribute_hidden
00098 #endif
00099 
00100 /* This version of `getopt' appears to the caller like standard Unix `getopt'
00101    but it behaves differently for the user, since it allows the user
00102    to intersperse the options with the other arguments.
00103 
00104    As `getopt' works, it permutes the elements of ARGV so that,
00105    when it is done, all the options precede everything else.  Thus
00106    all application programs are extended to handle flexible argument order.
00107 
00108    Setting the environment variable POSIXLY_CORRECT disables permutation.
00109    Then the behavior is completely standard.
00110 
00111    GNU application programs can use a third alternative mode in which
00112    they can distinguish the relative order of options and other arguments.  */
00113 
00114 #include "getopt.h"
00115 
00116 /* For communication from `getopt' to the caller.
00117    When `getopt' finds an option that takes an argument,
00118    the argument value is returned here.
00119    Also, when `ordering' is RETURN_IN_ORDER,
00120    each non-option ARGV-element is returned here.  */
00121 
00122 char *optarg;
00123 
00124 /* Index in ARGV of the next element to be scanned.
00125    This is used for communication to and from the caller
00126    and for communication between successive calls to `getopt'.
00127 
00128    On entry to `getopt', zero means this is the first call; initialize.
00129 
00130    When `getopt' returns -1, this is the index of the first of the
00131    non-option elements that the caller should itself scan.
00132 
00133    Otherwise, `optind' communicates from one call to the next
00134    how much of ARGV has been scanned so far.  */
00135 
00136 /* 1003.2 says this must be 1 before any call.  */
00137 int optind = 1;
00138 
00139 /* Formerly, initialization of getopt depended on optind==0, which
00140    causes problems with re-calling getopt as programs generally don't
00141    know that. */
00142 
00143 int __getopt_initialized attribute_hidden;
00144 
00145 /* The next char to be scanned in the option-element
00146    in which the last option character we returned was found.
00147    This allows us to pick up the scan where we left off.
00148 
00149    If this is zero, or a null string, it means resume the scan
00150    by advancing to the next ARGV-element.  */
00151 
00152 static char *nextchar;
00153 
00154 /* Callers store zero here to inhibit the error message
00155    for unrecognized options.  */
00156 
00157 int opterr = 1;
00158 
00159 /* Set to an option character which was unrecognized.
00160    This must be initialized on some systems to avoid linking in the
00161    system's own getopt implementation.  */
00162 
00163 int optopt = '?';
00164 
00165 /* Describe how to deal with options that follow non-option ARGV-elements.
00166 
00167    If the caller did not specify anything,
00168    the default is REQUIRE_ORDER if the environment variable
00169    POSIXLY_CORRECT is defined, PERMUTE otherwise.
00170 
00171    REQUIRE_ORDER means don't recognize them as options;
00172    stop option processing when the first non-option is seen.
00173    This is what Unix does.
00174    This mode of operation is selected by either setting the environment
00175    variable POSIXLY_CORRECT, or using `+' as the first character
00176    of the list of option characters.
00177 
00178    PERMUTE is the default.  We permute the contents of ARGV as we scan,
00179    so that eventually all the non-options are at the end.  This allows options
00180    to be given in any order, even with programs that were not written to
00181    expect this.
00182 
00183    RETURN_IN_ORDER is an option available to programs that were written
00184    to expect options and other ARGV-elements in any order and that care about
00185    the ordering of the two.  We describe each non-option ARGV-element
00186    as if it were the argument of an option with character code 1.
00187    Using `-' as the first character of the list of option characters
00188    selects this mode of operation.
00189 
00190    The special argument `--' forces an end of option-scanning regardless
00191    of the value of `ordering'.  In the case of RETURN_IN_ORDER, only
00192    `--' can cause `getopt' to return -1 with `optind' != ARGC.  */
00193 
00194 static enum
00195 {
00196   REQUIRE_ORDER, PERMUTE, RETURN_IN_ORDER
00197 } ordering;
00198 
00199 /* Value of POSIXLY_CORRECT environment variable.  */
00200 static char *posixly_correct;
00201 
00202 #ifdef  __GNU_LIBRARY__
00203 /* We want to avoid inclusion of string.h with non-GNU libraries
00204    because there are many ways it can cause trouble.
00205    On some systems, it contains special magic macros that don't work
00206    in GCC.  */
00207 # include <string.h>
00208 # define my_index       strchr
00209 #else
00210 
00211 # if HAVE_STRING_H
00212 #  include <string.h>
00213 # else
00214 #  include <strings.h>
00215 # endif
00216 
00217 /* Avoid depending on library functions or files
00218    whose names are inconsistent.  */
00219 
00220 #ifndef getenv
00221 extern char *getenv ();
00222 #endif
00223 
00224 static char *
00225 my_index (str, chr)
00226      const char *str;
00227      int chr;
00228 {
00229   while (*str)
00230     {
00231       if (*str == chr)
00232         return (char *) str;
00233       str++;
00234     }
00235   return 0;
00236 }
00237 
00238 /* If using GCC, we can safely declare strlen this way.
00239    If not using GCC, it is ok not to declare it.  */
00240 #ifdef __GNUC__
00241 /* Note that Motorola Delta 68k R3V7 comes with GCC but not stddef.h.
00242    That was relevant to code that was here before.  */
00243 # if (!defined __STDC__ || !__STDC__) && !defined strlen
00244 /* gcc with -traditional declares the built-in strlen to return int,
00245    and has done so at least since version 2.4.5. -- rms.  */
00246 extern int strlen (const char *);
00247 # endif /* not __STDC__ */
00248 #endif /* __GNUC__ */
00249 
00250 #endif /* not __GNU_LIBRARY__ */
00251 
00252 /* Handle permutation of arguments.  */
00253 
00254 /* Describe the part of ARGV that contains non-options that have
00255    been skipped.  `first_nonopt' is the index in ARGV of the first of them;
00256    `last_nonopt' is the index after the last of them.  */
00257 
00258 static int first_nonopt;
00259 static int last_nonopt;
00260 
00261 #ifdef _LIBC
00262 /* Stored original parameters.
00263    XXX This is no good solution.  We should rather copy the args so
00264    that we can compare them later.  But we must not use malloc(3).  */
00265 extern int __libc_argc;
00266 extern char **__libc_argv;
00267 
00268 /* Bash 2.0 gives us an environment variable containing flags
00269    indicating ARGV elements that should not be considered arguments.  */
00270 
00271 # ifdef USE_NONOPTION_FLAGS
00272 /* Defined in getopt_init.c  */
00273 extern char *__getopt_nonoption_flags;
00274 
00275 static int nonoption_flags_max_len;
00276 static int nonoption_flags_len;
00277 # endif
00278 
00279 # ifdef USE_NONOPTION_FLAGS
00280 #  define SWAP_FLAGS(ch1, ch2) \
00281   if (nonoption_flags_len > 0)                                                \
00282     {                                                                         \
00283       char __tmp = __getopt_nonoption_flags[ch1];                             \
00284       __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
00285       __getopt_nonoption_flags[ch2] = __tmp;                                  \
00286     }
00287 # else
00288 #  define SWAP_FLAGS(ch1, ch2)
00289 # endif
00290 #else   /* !_LIBC */
00291 # define SWAP_FLAGS(ch1, ch2)
00292 #endif  /* _LIBC */
00293 
00294 /* Exchange two adjacent subsequences of ARGV.
00295    One subsequence is elements [first_nonopt,last_nonopt)
00296    which contains all the non-options that have been skipped so far.
00297    The other is elements [last_nonopt,optind), which contains all
00298    the options processed since those non-options were skipped.
00299 
00300    `first_nonopt' and `last_nonopt' are relocated so that they describe
00301    the new indices of the non-options in ARGV after they are moved.  */
00302 
00303 #if defined __STDC__ && __STDC__
00304 static void exchange (char **);
00305 #endif
00306 
00307 static void
00308 exchange (argv)
00309      char **argv;
00310 {
00311   int bottom = first_nonopt;
00312   int middle = last_nonopt;
00313   int top = optind;
00314   char *tem;
00315 
00316   /* Exchange the shorter segment with the far end of the longer segment.
00317      That puts the shorter segment into the right place.
00318      It leaves the longer segment in the right place overall,
00319      but it consists of two parts that need to be swapped next.  */
00320 
00321 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00322   /* First make sure the handling of the `__getopt_nonoption_flags'
00323      string can work normally.  Our top argument must be in the range
00324      of the string.  */
00325   if (nonoption_flags_len > 0 && top >= nonoption_flags_max_len)
00326     {
00327       /* We must extend the array.  The user plays games with us and
00328          presents new arguments.  */
00329       char *new_str = malloc (top + 1);
00330       if (new_str == NULL)
00331         nonoption_flags_len = nonoption_flags_max_len = 0;
00332       else
00333         {
00334           memset (__mempcpy (new_str, __getopt_nonoption_flags,
00335                              nonoption_flags_max_len),
00336                   '\0', top + 1 - nonoption_flags_max_len);
00337           nonoption_flags_max_len = top + 1;
00338           __getopt_nonoption_flags = new_str;
00339         }
00340     }
00341 #endif
00342 
00343   while (top > middle && middle > bottom)
00344     {
00345       if (top - middle > middle - bottom)
00346         {
00347           /* Bottom segment is the short one.  */
00348           int len = middle - bottom;
00349           register int i;
00350 
00351           /* Swap it with the top part of the top segment.  */
00352           for (i = 0; i < len; i++)
00353             {
00354               tem = argv[bottom + i];
00355               argv[bottom + i] = argv[top - (middle - bottom) + i];
00356               argv[top - (middle - bottom) + i] = tem;
00357               SWAP_FLAGS (bottom + i, top - (middle - bottom) + i);
00358             }
00359           /* Exclude the moved bottom segment from further swapping.  */
00360           top -= len;
00361         }
00362       else
00363         {
00364           /* Top segment is the short one.  */
00365           int len = top - middle;
00366           register int i;
00367 
00368           /* Swap it with the bottom part of the bottom segment.  */
00369           for (i = 0; i < len; i++)
00370             {
00371               tem = argv[bottom + i];
00372               argv[bottom + i] = argv[middle + i];
00373               argv[middle + i] = tem;
00374               SWAP_FLAGS (bottom + i, middle + i);
00375             }
00376           /* Exclude the moved top segment from further swapping.  */
00377           bottom += len;
00378         }
00379     }
00380 
00381   /* Update records for the slots the non-options now occupy.  */
00382 
00383   first_nonopt += (optind - last_nonopt);
00384   last_nonopt = optind;
00385 }
00386 
00387 /* Initialize the internal data when the first call is made.  */
00388 
00389 #if defined __STDC__ && __STDC__
00390 static const char *_getopt_initialize (int, char *const *, const char *);
00391 #endif
00392 static const char *
00393 _getopt_initialize (argc, argv, optstring)
00394      int argc;
00395      char *const *argv;
00396      const char *optstring;
00397 {
00398   /* Start processing options with ARGV-element 1 (since ARGV-element 0
00399      is the program name); the sequence of previously skipped
00400      non-option ARGV-elements is empty.  */
00401 
00402   first_nonopt = last_nonopt = optind;
00403 
00404   nextchar = NULL;
00405 
00406   posixly_correct = getenv ("POSIXLY_CORRECT");
00407 
00408   /* Determine how to handle the ordering of options and nonoptions.  */
00409 
00410   if (optstring[0] == '-')
00411     {
00412       ordering = RETURN_IN_ORDER;
00413       ++optstring;
00414     }
00415   else if (optstring[0] == '+')
00416     {
00417       ordering = REQUIRE_ORDER;
00418       ++optstring;
00419     }
00420   else if (posixly_correct != NULL)
00421     ordering = REQUIRE_ORDER;
00422   else
00423     ordering = PERMUTE;
00424 
00425 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00426   if (posixly_correct == NULL
00427       && argc == __libc_argc && argv == __libc_argv)
00428     {
00429       if (nonoption_flags_max_len == 0)
00430         {
00431           if (__getopt_nonoption_flags == NULL
00432               || __getopt_nonoption_flags[0] == '\0')
00433             nonoption_flags_max_len = -1;
00434           else
00435             {
00436               const char *orig_str = __getopt_nonoption_flags;
00437               int len = nonoption_flags_max_len = strlen (orig_str);
00438               if (nonoption_flags_max_len < argc)
00439                 nonoption_flags_max_len = argc;
00440               __getopt_nonoption_flags =
00441                 (char *) malloc (nonoption_flags_max_len);
00442               if (__getopt_nonoption_flags == NULL)
00443                 nonoption_flags_max_len = -1;
00444               else
00445                 memset (__mempcpy (__getopt_nonoption_flags, orig_str, len),
00446                         '\0', nonoption_flags_max_len - len);
00447             }
00448         }
00449       nonoption_flags_len = nonoption_flags_max_len;
00450     }
00451   else
00452     nonoption_flags_len = 0;
00453 #endif
00454 
00455   return optstring;
00456 }
00457 
00458 /* Scan elements of ARGV (whose length is ARGC) for option characters
00459    given in OPTSTRING.
00460 
00461    If an element of ARGV starts with '-', and is not exactly "-" or "--",
00462    then it is an option element.  The characters of this element
00463    (aside from the initial '-') are option characters.  If `getopt'
00464    is called repeatedly, it returns successively each of the option characters
00465    from each of the option elements.
00466 
00467    If `getopt' finds another option character, it returns that character,
00468    updating `optind' and `nextchar' so that the next call to `getopt' can
00469    resume the scan with the following option character or ARGV-element.
00470 
00471    If there are no more option characters, `getopt' returns -1.
00472    Then `optind' is the index in ARGV of the first ARGV-element
00473    that is not an option.  (The ARGV-elements have been permuted
00474    so that those that are not options now come last.)
00475 
00476    OPTSTRING is a string containing the legitimate option characters.
00477    If an option character is seen that is not listed in OPTSTRING,
00478    return '?' after printing an error message.  If you set `opterr' to
00479    zero, the error message is suppressed but we still return '?'.
00480 
00481    If a char in OPTSTRING is followed by a colon, that means it wants an arg,
00482    so the following text in the same ARGV-element, or the text of the following
00483    ARGV-element, is returned in `optarg'.  Two colons mean an option that
00484    wants an optional arg; if there is text in the current ARGV-element,
00485    it is returned in `optarg', otherwise `optarg' is set to zero.
00486 
00487    If OPTSTRING starts with `-' or `+', it requests different methods of
00488    handling the non-option ARGV-elements.
00489    See the comments about RETURN_IN_ORDER and REQUIRE_ORDER, above.
00490 
00491    Long-named options begin with `--' instead of `-'.
00492    Their names may be abbreviated as long as the abbreviation is unique
00493    or is an exact match for some defined option.  If they have an
00494    argument, it follows the option name in the same ARGV-element, separated
00495    from the option name by a `=', or else the in next ARGV-element.
00496    When `getopt' finds a long-named option, it returns 0 if that option's
00497    `flag' field is nonzero, the value of the option's `val' field
00498    if the `flag' field is zero.
00499 
00500    The elements of ARGV aren't really const, because we permute them.
00501    But we pretend they're const in the prototype to be compatible
00502    with other systems.
00503 
00504    LONGOPTS is a vector of `struct option' terminated by an
00505    element containing a name which is zero.
00506 
00507    LONGIND returns the index in LONGOPT of the long-named option found.
00508    It is only valid when a long-named option has been found by the most
00509    recent call.
00510 
00511    If LONG_ONLY is nonzero, '-' as well as '--' can introduce
00512    long-named options.  */
00513 
00514 int
00515 _getopt_internal (argc, argv, optstring, longopts, longind, long_only)
00516      int argc;
00517      char *const *argv;
00518      const char *optstring;
00519      const struct option *longopts;
00520      int *longind;
00521      int long_only;
00522 {
00523   int print_errors = opterr;
00524   if (optstring[0] == ':')
00525     print_errors = 0;
00526 
00527   if (argc < 1)
00528     return -1;
00529 
00530   optarg = NULL;
00531 
00532   if (optind == 0 || !__getopt_initialized)
00533     {
00534       if (optind == 0)
00535         optind = 1;     /* Don't scan ARGV[0], the program name.  */
00536       optstring = _getopt_initialize (argc, argv, optstring);
00537       __getopt_initialized = 1;
00538     }
00539 
00540   /* Test whether ARGV[optind] points to a non-option argument.
00541      Either it does not have option syntax, or there is an environment flag
00542      from the shell indicating it is not an option.  The later information
00543      is only used when the used in the GNU libc.  */
00544 #if defined _LIBC && defined USE_NONOPTION_FLAGS
00545 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \
00546                       || (optind < nonoption_flags_len                        \
00547                           && __getopt_nonoption_flags[optind] == '1'))
00548 #else
00549 # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0')
00550 #endif
00551 
00552   if (nextchar == NULL || *nextchar == '\0')
00553     {
00554       /* Advance to the next ARGV-element.  */
00555 
00556       /* Give FIRST_NONOPT & LAST_NONOPT rational values if OPTIND has been
00557          moved back by the user (who may also have changed the arguments).  */
00558       if (last_nonopt > optind)
00559         last_nonopt = optind;
00560       if (first_nonopt > optind)
00561         first_nonopt = optind;
00562 
00563       if (ordering == PERMUTE)
00564         {
00565           /* If we have just processed some options following some non-options,
00566              exchange them so that the options come first.  */
00567 
00568           if (first_nonopt != last_nonopt && last_nonopt != optind)
00569             exchange ((char **) argv);
00570           else if (last_nonopt != optind)
00571             first_nonopt = optind;
00572 
00573           /* Skip any additional non-options
00574              and extend the range of non-options previously skipped.  */
00575 
00576           while (optind < argc && NONOPTION_P)
00577             optind++;
00578           last_nonopt = optind;
00579         }
00580 
00581       /* The special ARGV-element `--' means premature end of options.
00582          Skip it like a null option,
00583          then exchange with previous non-options as if it were an option,
00584          then skip everything else like a non-option.  */
00585 
00586       if (optind != argc && !strcmp (argv[optind], "--"))
00587         {
00588           optind++;
00589 
00590           if (first_nonopt != last_nonopt && last_nonopt != optind)
00591             exchange ((char **) argv);
00592           else if (first_nonopt == last_nonopt)
00593             first_nonopt = optind;
00594           last_nonopt = argc;
00595 
00596           optind = argc;
00597         }
00598 
00599       /* If we have done all the ARGV-elements, stop the scan
00600          and back over any non-options that we skipped and permuted.  */
00601 
00602       if (optind == argc)
00603         {
00604           /* Set the next-arg-index to point at the non-options
00605              that we previously skipped, so the caller will digest them.  */
00606           if (first_nonopt != last_nonopt)
00607             optind = first_nonopt;
00608           return -1;
00609         }
00610 
00611       /* If we have come to a non-option and did not permute it,
00612          either stop the scan or describe it to the caller and pass it by.  */
00613 
00614       if (NONOPTION_P)
00615         {
00616           if (ordering == REQUIRE_ORDER)
00617             return -1;
00618           optarg = argv[optind++];
00619           return 1;
00620         }
00621 
00622       /* We have found another option-ARGV-element.
00623          Skip the initial punctuation.  */
00624 
00625       nextchar = (argv[optind] + 1
00626                   + (longopts != NULL && argv[optind][1] == '-'));
00627     }
00628 
00629   /* Decode the current option-ARGV-element.  */
00630 
00631   /* Check whether the ARGV-element is a long option.
00632 
00633      If long_only and the ARGV-element has the form "-f", where f is
00634      a valid short option, don't consider it an abbreviated form of
00635      a long option that starts with f.  Otherwise there would be no
00636      way to give the -f short option.
00637 
00638      On the other hand, if there's a long option "fubar" and
00639      the ARGV-element is "-fu", do consider that an abbreviation of
00640      the long option, just like "--fu", and not "-f" with arg "u".
00641 
00642      This distinction seems to be the most useful approach.  */
00643 
00644   if (longopts != NULL
00645       && (argv[optind][1] == '-'
00646           || (long_only && (argv[optind][2] || !my_index (optstring, argv[optind][1])))))
00647     {
00648       char *nameend;
00649       const struct option *p;
00650       const struct option *pfound = NULL;
00651       int exact = 0;
00652       int ambig = 0;
00653       int indfound = -1;
00654       int option_index;
00655 
00656       for (nameend = nextchar; *nameend && *nameend != '='; nameend++)
00657         /* Do nothing.  */ ;
00658 
00659       /* Test all long options for either exact match
00660          or abbreviated matches.  */
00661       for (p = longopts, option_index = 0; p->name; p++, option_index++)
00662         if (!strncmp (p->name, nextchar, nameend - nextchar))
00663           {
00664             if ((unsigned int) (nameend - nextchar)
00665                 == (unsigned int) strlen (p->name))
00666               {
00667                 /* Exact match found.  */
00668                 pfound = p;
00669                 indfound = option_index;
00670                 exact = 1;
00671                 break;
00672               }
00673             else if (pfound == NULL)
00674               {
00675                 /* First nonexact match found.  */
00676                 pfound = p;
00677                 indfound = option_index;
00678               }
00679             else if (long_only
00680                      || pfound->has_arg != p->has_arg
00681                      || pfound->flag != p->flag
00682                      || pfound->val != p->val)
00683               /* Second or later nonexact match found.  */
00684               ambig = 1;
00685           }
00686 
00687       if (ambig && !exact)
00688         {
00689           if (print_errors)
00690             {
00691 #if defined _LIBC && defined USE_IN_LIBIO
00692               char *buf;
00693 
00694               if (__asprintf (&buf, _("%s: option `%s' is ambiguous\n"),
00695                               argv[0], argv[optind]) >= 0)
00696                 {
00697 
00698                   if (_IO_fwide (stderr, 0) > 0)
00699                     __fwprintf (stderr, L"%s", buf);
00700                   else
00701                     fputs (buf, stderr);
00702 
00703                   free (buf);
00704                 }
00705 #else
00706               fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
00707                        argv[0], argv[optind]);
00708 #endif
00709             }
00710           nextchar += strlen (nextchar);
00711           optind++;
00712           optopt = 0;
00713           return '?';
00714         }
00715 
00716       if (pfound != NULL)
00717         {
00718           option_index = indfound;
00719           optind++;
00720           if (*nameend)
00721             {
00722               /* Don't test has_arg with >, because some C compilers don't
00723                  allow it to be used on enums.  */
00724               if (pfound->has_arg)
00725                 optarg = nameend + 1;
00726               else
00727                 {
00728                   if (print_errors)
00729                     {
00730 #if defined _LIBC && defined USE_IN_LIBIO
00731                       char *buf;
00732                       int n;
00733 #endif
00734 
00735                       if (argv[optind - 1][1] == '-')
00736                         {
00737                           /* --option */
00738 #if defined _LIBC && defined USE_IN_LIBIO
00739                           n = __asprintf (&buf, _("\
00740 %s: option `--%s' doesn't allow an argument\n"),
00741                                           argv[0], pfound->name);
00742 #else
00743                           fprintf (stderr, _("\
00744 %s: option `--%s' doesn't allow an argument\n"),
00745                                    argv[0], pfound->name);
00746 #endif
00747                         }
00748                       else
00749                         {
00750                           /* +option or -option */
00751 #if defined _LIBC && defined USE_IN_LIBIO
00752                           n = __asprintf (&buf, _("\
00753 %s: option `%c%s' doesn't allow an argument\n"),
00754                                           argv[0], argv[optind - 1][0],
00755                                           pfound->name);
00756 #else
00757                           fprintf (stderr, _("\
00758 %s: option `%c%s' doesn't allow an argument\n"),
00759                                    argv[0], argv[optind - 1][0], pfound->name);
00760 #endif
00761                         }
00762 
00763 #if defined _LIBC && defined USE_IN_LIBIO
00764                       if (n >= 0)
00765                         {
00766                           if (_IO_fwide (stderr, 0) > 0)
00767                             __fwprintf (stderr, L"%s", buf);
00768                           else
00769                             fputs (buf, stderr);
00770 
00771                           free (buf);
00772                         }
00773 #endif
00774                     }
00775 
00776                   nextchar += strlen (nextchar);
00777 
00778                   optopt = pfound->val;
00779                   return '?';
00780                 }
00781             }
00782           else if (pfound->has_arg == 1)
00783             {
00784               if (optind < argc)
00785                 optarg = argv[optind++];
00786               else
00787                 {
00788                   if (print_errors)
00789                     {
00790 #if defined _LIBC && defined USE_IN_LIBIO
00791                       char *buf;
00792 
00793                       if (__asprintf (&buf, _("\
00794 %s: option `%s' requires an argument\n"),
00795                                       argv[0], argv[optind - 1]) >= 0)
00796                         {
00797                           if (_IO_fwide (stderr, 0) > 0)
00798                             __fwprintf (stderr, L"%s", buf);
00799                           else
00800                             fputs (buf, stderr);
00801 
00802                           free (buf);
00803                         }
00804 #else
00805                       fprintf (stderr,
00806                                _("%s: option `%s' requires an argument\n"),
00807                                argv[0], argv[optind - 1]);
00808 #endif
00809                     }
00810                   nextchar += strlen (nextchar);
00811                   optopt = pfound->val;
00812                   return optstring[0] == ':' ? ':' : '?';
00813                 }
00814             }
00815           nextchar += strlen (nextchar);
00816           if (longind != NULL)
00817             *longind = option_index;
00818           if (pfound->flag)
00819             {
00820               *(pfound->flag) = pfound->val;
00821               return 0;
00822             }
00823           return pfound->val;
00824         }
00825 
00826       /* Can't find it as a long option.  If this is not getopt_long_only,
00827          or the option starts with '--' or is not a valid short
00828          option, then it's an error.
00829          Otherwise interpret it as a short option.  */
00830       if (!long_only || argv[optind][1] == '-'
00831           || my_index (optstring, *nextchar) == NULL)
00832         {
00833           if (print_errors)
00834             {
00835 #if defined _LIBC && defined USE_IN_LIBIO
00836               char *buf;
00837               int n;
00838 #endif
00839 
00840               if (argv[optind][1] == '-')
00841                 {
00842                   /* --option */
00843 #if defined _LIBC && defined USE_IN_LIBIO
00844                   n = __asprintf (&buf, _("%s: unrecognized option `--%s'\n"),
00845                                   argv[0], nextchar);
00846 #else
00847                   fprintf (stderr, _("%s: unrecognized option `--%s'\n"),
00848                            argv[0], nextchar);
00849 #endif
00850                 }
00851               else
00852                 {
00853                   /* +option or -option */
00854 #if defined _LIBC && defined USE_IN_LIBIO
00855                   n = __asprintf (&buf, _("%s: unrecognized option `%c%s'\n"),
00856                                   argv[0], argv[optind][0], nextchar);
00857 #else
00858                   fprintf (stderr, _("%s: unrecognized option `%c%s'\n"),
00859                            argv[0], argv[optind][0], nextchar);
00860 #endif
00861                 }
00862 
00863 #if defined _LIBC && defined USE_IN_LIBIO
00864               if (n >= 0)
00865                 {
00866                   if (_IO_fwide (stderr, 0) > 0)
00867                     __fwprintf (stderr, L"%s", buf);
00868                   else
00869                     fputs (buf, stderr);
00870 
00871                   free (buf);
00872                 }
00873 #endif
00874             }
00875           nextchar = (char *) "";
00876           optind++;
00877           optopt = 0;
00878           return '?';
00879         }
00880     }
00881 
00882   /* Look at and handle the next short option-character.  */
00883 
00884   {
00885     char c = *nextchar++;
00886     char *temp = my_index (optstring, c);
00887 
00888     /* Increment `optind' when we start to process its last character.  */
00889     if (*nextchar == '\0')
00890       ++optind;
00891 
00892     if (temp == NULL || c == ':')
00893       {
00894         if (print_errors)
00895           {
00896 #if defined _LIBC && defined USE_IN_LIBIO
00897               char *buf;
00898               int n;
00899 #endif
00900 
00901             if (posixly_correct)
00902               {
00903                 /* 1003.2 specifies the format of this message.  */
00904 #if defined _LIBC && defined USE_IN_LIBIO
00905                 n = __asprintf (&buf, _("%s: illegal option -- %c\n"),
00906                                 argv[0], c);
00907 #else
00908                 fprintf (stderr, _("%s: illegal option -- %c\n"), argv[0], c);
00909 #endif
00910               }
00911             else
00912               {
00913 #if defined _LIBC && defined USE_IN_LIBIO
00914                 n = __asprintf (&buf, _("%s: invalid option -- %c\n"),
00915                                 argv[0], c);
00916 #else
00917                 fprintf (stderr, _("%s: invalid option -- %c\n"), argv[0], c);
00918 #endif
00919               }
00920 
00921 #if defined _LIBC && defined USE_IN_LIBIO
00922             if (n >= 0)
00923               {
00924                 if (_IO_fwide (stderr, 0) > 0)
00925                   __fwprintf (stderr, L"%s", buf);
00926                 else
00927                   fputs (buf, stderr);
00928 
00929                 free (buf);
00930               }
00931 #endif
00932           }
00933         optopt = c;
00934         return '?';
00935       }
00936     /* Convenience. Treat POSIX -W foo same as long option --foo */
00937     if (temp[0] == 'W' && temp[1] == ';')
00938       {
00939         char *nameend;
00940         const struct option *p;
00941         const struct option *pfound = NULL;
00942         int exact = 0;
00943         int ambig = 0;
00944         int indfound = 0;
00945         int option_index;
00946 
00947         /* This is an option that requires an argument.  */
00948         if (*nextchar != '\0')
00949           {
00950             optarg = nextchar;
00951             /* If we end this ARGV-element by taking the rest as an arg,
00952                we must advance to the next element now.  */
00953             optind++;
00954           }
00955         else if (optind == argc)
00956           {
00957             if (print_errors)
00958               {
00959                 /* 1003.2 specifies the format of this message.  */
00960 #if defined _LIBC && defined USE_IN_LIBIO
00961                 char *buf;
00962 
00963                 if (__asprintf (&buf,
00964                                 _("%s: option requires an argument -- %c\n"),
00965                                 argv[0], c) >= 0)
00966                   {
00967                     if (_IO_fwide (stderr, 0) > 0)
00968                       __fwprintf (stderr, L"%s", buf);
00969                     else
00970                       fputs (buf, stderr);
00971 
00972                     free (buf);
00973                   }
00974 #else
00975                 fprintf (stderr, _("%s: option requires an argument -- %c\n"),
00976                          argv[0], c);
00977 #endif
00978               }
00979             optopt = c;
00980             if (optstring[0] == ':')
00981               c = ':';
00982             else
00983               c = '?';
00984             return c;
00985           }
00986         else
00987           /* We already incremented `optind' once;
00988              increment it again when taking next ARGV-elt as argument.  */
00989           optarg = argv[optind++];
00990 
00991         /* optarg is now the argument, see if it's in the
00992            table of longopts.  */
00993 
00994         for (nextchar = nameend = optarg; *nameend && *nameend != '='; nameend++)
00995           /* Do nothing.  */ ;
00996 
00997         /* Test all long options for either exact match
00998            or abbreviated matches.  */
00999         for (p = longopts, option_index = 0; p->name; p++, option_index++)
01000           if (!strncmp (p->name, nextchar, nameend - nextchar))
01001             {
01002               if ((unsigned int) (nameend - nextchar) == strlen (p->name))
01003                 {
01004                   /* Exact match found.  */
01005                   pfound = p;
01006                   indfound = option_index;
01007                   exact = 1;
01008                   break;
01009                 }
01010               else if (pfound == NULL)
01011                 {
01012                   /* First nonexact match found.  */
01013                   pfound = p;
01014                   indfound = option_index;
01015                 }
01016               else
01017                 /* Second or later nonexact match found.  */
01018                 ambig = 1;
01019             }
01020         if (ambig && !exact)
01021           {
01022             if (print_errors)
01023               {
01024 #if defined _LIBC && defined USE_IN_LIBIO
01025                 char *buf;
01026 
01027                 if (__asprintf (&buf, _("%s: option `-W %s' is ambiguous\n"),
01028                                 argv[0], argv[optind]) >= 0)
01029                   {
01030                     if (_IO_fwide (stderr, 0) > 0)
01031                       __fwprintf (stderr, L"%s", buf);
01032                     else
01033                       fputs (buf, stderr);
01034 
01035                     free (buf);
01036                   }
01037 #else
01038                 fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
01039                          argv[0], argv[optind]);
01040 #endif
01041               }
01042             nextchar += strlen (nextchar);
01043             optind++;
01044             return '?';
01045           }
01046         if (pfound != NULL)
01047           {
01048             option_index = indfound;
01049             if (*nameend)
01050               {
01051                 /* Don't test has_arg with >, because some C compilers don't
01052                    allow it to be used on enums.  */
01053                 if (pfound->has_arg)
01054                   optarg = nameend + 1;
01055                 else
01056                   {
01057                     if (print_errors)
01058                       {
01059 #if defined _LIBC && defined USE_IN_LIBIO
01060                         char *buf;
01061 
01062                         if (__asprintf (&buf, _("\
01063 %s: option `-W %s' doesn't allow an argument\n"),
01064                                         argv[0], pfound->name) >= 0)
01065                           {
01066                             if (_IO_fwide (stderr, 0) > 0)
01067                               __fwprintf (stderr, L"%s", buf);
01068                             else
01069                               fputs (buf, stderr);
01070 
01071                             free (buf);
01072                           }
01073 #else
01074                         fprintf (stderr, _("\
01075 %s: option `-W %s' doesn't allow an argument\n"),
01076                                  argv[0], pfound->name);
01077 #endif
01078                       }
01079 
01080                     nextchar += strlen (nextchar);
01081                     return '?';
01082                   }
01083               }
01084             else if (pfound->has_arg == 1)
01085               {
01086                 if (optind < argc)
01087                   optarg = argv[optind++];
01088                 else
01089                   {
01090                     if (print_errors)
01091                       {
01092 #if defined _LIBC && defined USE_IN_LIBIO
01093                         char *buf;
01094 
01095                         if (__asprintf (&buf, _("\
01096 %s: option `%s' requires an argument\n"),
01097                                         argv[0], argv[optind - 1]) >= 0)
01098                           {
01099                             if (_IO_fwide (stderr, 0) > 0)
01100                               __fwprintf (stderr, L"%s", buf);
01101                             else
01102                               fputs (buf, stderr);
01103 
01104                             free (buf);
01105                           }
01106 #else
01107                         fprintf (stderr,
01108                                  _("%s: option `%s' requires an argument\n"),
01109                                  argv[0], argv[optind - 1]);
01110 #endif
01111                       }
01112                     nextchar += strlen (nextchar);
01113                     return optstring[0] == ':' ? ':' : '?';
01114                   }
01115               }
01116             nextchar += strlen (nextchar);
01117             if (longind != NULL)
01118               *longind = option_index;
01119             if (pfound->flag)
01120               {
01121                 *(pfound->flag) = pfound->val;
01122                 return 0;
01123               }
01124             return pfound->val;
01125           }
01126           nextchar = NULL;
01127           return 'W';   /* Let the application handle it.   */
01128       }
01129     if (temp[1] == ':')
01130       {
01131         if (temp[2] == ':')
01132           {
01133             /* This is an option that accepts an argument optionally.  */
01134             if (*nextchar != '\0')
01135               {
01136                 optarg = nextchar;
01137                 optind++;
01138               }
01139             else
01140               optarg = NULL;
01141             nextchar = NULL;
01142           }
01143         else
01144           {
01145             /* This is an option that requires an argument.  */
01146             if (*nextchar != '\0')
01147               {
01148                 optarg = nextchar;
01149                 /* If we end this ARGV-element by taking the rest as an arg,
01150                    we must advance to the next element now.  */
01151                 optind++;
01152               }
01153             else if (optind == argc)
01154               {
01155                 if (print_errors)
01156                   {
01157                     /* 1003.2 specifies the format of this message.  */
01158 #if defined _LIBC && defined USE_IN_LIBIO
01159                     char *buf;
01160 
01161                     if (__asprintf (&buf, _("\
01162 %s: option requires an argument -- %c\n"),
01163                                     argv[0], c) >= 0)
01164                       {
01165                         if (_IO_fwide (stderr, 0) > 0)
01166                           __fwprintf (stderr, L"%s", buf);
01167                         else
01168                           fputs (buf, stderr);
01169 
01170                         free (buf);
01171                       }
01172 #else
01173                     fprintf (stderr,
01174                              _("%s: option requires an argument -- %c\n"),
01175                              argv[0], c);
01176 #endif
01177                   }
01178                 optopt = c;
01179                 if (optstring[0] == ':')
01180                   c = ':';
01181                 else
01182                   c = '?';
01183               }
01184             else
01185               /* We already incremented `optind' once;
01186                  increment it again when taking next ARGV-elt as argument.  */
01187               optarg = argv[optind++];
01188             nextchar = NULL;
01189           }
01190       }
01191     return c;
01192   }
01193 }
01194 
01195 int
01196 getopt (argc, argv, optstring)
01197      int argc;
01198      char *const *argv;
01199      const char *optstring;
01200 {
01201   return _getopt_internal (argc, argv, optstring,
01202                            (const struct option *) 0,
01203                            (int *) 0,
01204                            0);
01205 }
01206 
01207 #endif  /* Not ELIDE_CODE.  */
01208 
01209 #ifdef TEST
01210 
01211 /* Compile with -DTEST to make an executable for use in testing
01212    the above definition of `getopt'.  */
01213 
01214 int
01215 main (argc, argv)
01216      int argc;
01217      char **argv;
01218 {
01219   int c;
01220   int digit_optind = 0;
01221 
01222   while (1)
01223     {
01224       int this_option_optind = optind ? optind : 1;
01225 
01226       c = getopt (argc, argv, "abc:d:0123456789");
01227       if (c == -1)
01228         break;
01229 
01230       switch (c)
01231         {
01232         case '0':
01233         case '1':
01234         case '2':
01235         case '3':
01236         case '4':
01237         case '5':
01238         case '6':
01239         case '7':
01240         case '8':
01241         case '9':
01242           if (digit_optind != 0 && digit_optind != this_option_optind)
01243             printf ("digits occur in two different argv-elements.\n");
01244           digit_optind = this_option_optind;
01245           printf ("option %c\n", c);
01246           break;
01247 
01248         case 'a':
01249           printf ("option a\n");
01250           break;
01251 
01252         case 'b':
01253           printf ("option b\n");
01254           break;
01255 
01256         case 'c':
01257           printf ("option c with value `%s'\n", optarg);
01258           break;
01259 
01260         case '?':
01261           break;
01262 
01263         default:
01264           printf ("?? getopt returned character code 0%o ??\n", c);
01265         }
01266     }
01267 
01268   if (optind < argc)
01269     {
01270       printf ("non-option ARGV-elements: ");
01271       while (optind < argc)
01272         printf ("%s ", argv[optind++]);
01273       printf ("\n");
01274     }
01275 
01276   exit (0);
01277 }
01278 
01279 #endif /* TEST */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines