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


appl
Author(s): petercai
autogenerated on Tue Jan 7 2014 11:02:29