/[groff]/groff/src/libs/libgroff/getopt.c
ViewVC logotype

Diff of /groff/src/libs/libgroff/getopt.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.9 by wl, Thu Jun 16 09:47:49 2005 UTC revision 1.10 by wl, Thu Jun 23 11:33:00 2005 UTC
# Line 3  Line 3 
3     "Keep this file name-space clean" means, talk to drepper@gnu.org     "Keep this file name-space clean" means, talk to drepper@gnu.org
4     before changing it!     before changing it!
5     Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004     Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001,2002,2003,2004
6          Free Software Foundation, Inc.          Free Software Foundation, Inc.
7     This file is part of the GNU C Library.     This file is part of the GNU C Library.
8    
9     The GNU C Library is free software; you can redistribute it and/or     This program is free software; you can redistribute it and/or modify
10     modify it under the terms of the GNU Lesser General Public     it under the terms of the GNU General Public License as published by
11     License as published by the Free Software Foundation; either     the Free Software Foundation; either version 2, or (at your option)
12     version 2.1 of the License, or (at your option) any later version.     any later version.
13    
14     The GNU C Library is distributed in the hope that it will be useful,     This program is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     Lesser General Public License for more details.     GNU General Public License for more details.
18    
19     You should have received a copy of the GNU Lesser General Public     You should have received a copy of the GNU General Public License along
20     License along with the GNU C Library; if not, write to the Free     with this program; if not, write to the Free Software Foundation,
21     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA     Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
    02111-1307 USA.  */  
22    
23  /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.  /* This tells Alpha OSF/1 not to define a getopt prototype in <stdio.h>.
24     Ditto for AIX 3.2 and <stdlib.h>.  */     Ditto for AIX 3.2 and <stdlib.h>.  */
# Line 33  Line 32 
32    
33  #include <stdio.h>  #include <stdio.h>
34    
 /* Comment out all this code if we are using the GNU C Library, and are not  
    actually compiling the library itself.  This code is part of the GNU C  
    Library, but also included in many other GNU distributions.  Compiling  
    and linking in this code is a waste when using the GNU C library  
    (especially if it is a shared library).  Rather than having every GNU  
    program understand `configure --with-gnu-libc' and omit the object files,  
    it is simpler to just do this in the source for each such file.  */  
   
 #define GETOPT_INTERFACE_VERSION 2  
 #if !defined _LIBC && defined __GLIBC__ && __GLIBC__ >= 2  
 # include <gnu-versions.h>  
 # if _GNU_GETOPT_INTERFACE_VERSION == GETOPT_INTERFACE_VERSION  
 #  define ELIDE_CODE  
 # endif  
 #endif  
   
 #ifndef ELIDE_CODE  
   
   
35  /* This needs to come after some library #include  /* This needs to come after some library #include
36     to get __GNU_LIBRARY__ defined.  */     to get __GNU_LIBRARY__ defined.  */
37  #ifdef  __GNU_LIBRARY__  #ifdef  __GNU_LIBRARY__
# Line 82  Line 62 
62  # define attribute_hidden  # define attribute_hidden
63  #endif  #endif
64    
65  /* This version of `getopt' appears to the caller like standard Unix `getopt'  /* Unlike standard Unix `getopt', functions like `getopt_long'
66     but it behaves differently for the user, since it allows the user     let the user intersperse the options with the other arguments.
    to intersperse the options with the other arguments.  
67    
68     As `getopt' works, it permutes the elements of ARGV so that,     As `getopt_long' works, it permutes the elements of ARGV so that,
69     when it is done, all the options precede everything else.  Thus     when it is done, all the options precede everything else.  Thus
70     all application programs are extended to handle flexible argument order.     all application programs are extended to handle flexible argument order.
71    
72     Setting the environment variable POSIXLY_CORRECT disables permutation.     Using `getopt' or setting the environment variable POSIXLY_CORRECT
73     Then the behavior is completely standard.     disables permutation.
74       Then the application's behavior is completely standard.
75    
76     GNU application programs can use a third alternative mode in which     GNU application programs can use a third alternative mode in which
77     they can distinguish the relative order of options and other arguments.  */     they can distinguish the relative order of options and other arguments.  */
# Line 270  exchange (char **argv, struct _getopt_da Line 250  exchange (char **argv, struct _getopt_da
250  /* Initialize the internal data when the first call is made.  */  /* Initialize the internal data when the first call is made.  */
251    
252  static const char *  static const char *
253  _getopt_initialize (int argc, char *const *argv, const char *optstring,  _getopt_initialize (int argc, char **argv, const char *optstring,
254                      struct _getopt_data *d)                      int posixly_correct, struct _getopt_data *d)
255  {  {
256    /* Start processing options with ARGV-element 1 (since ARGV-element 0    /* Start processing options with ARGV-element 1 (since ARGV-element 0
257       is the program name); the sequence of previously skipped       is the program name); the sequence of previously skipped
# Line 281  _getopt_initialize (int argc, char *cons Line 261  _getopt_initialize (int argc, char *cons
261    
262    d->__nextchar = NULL;    d->__nextchar = NULL;
263    
264    d->__posixly_correct = !!getenv ("POSIXLY_CORRECT");    d->__posixly_correct = posixly_correct || !!getenv ("POSIXLY_CORRECT");
265    
266    /* Determine how to handle the ordering of options and nonoptions.  */    /* Determine how to handle the ordering of options and nonoptions.  */
267    
# Line 375  _getopt_initialize (int argc, char *cons Line 355  _getopt_initialize (int argc, char *cons
355     `flag' field is nonzero, the value of the option's `val' field     `flag' field is nonzero, the value of the option's `val' field
356     if the `flag' field is zero.     if the `flag' field is zero.
357    
    The elements of ARGV aren't really const, because we permute them.  
    But we pretend they're const in the prototype to be compatible  
    with other systems.  
   
358     LONGOPTS is a vector of `struct option' terminated by an     LONGOPTS is a vector of `struct option' terminated by an
359     element containing a name which is zero.     element containing a name which is zero.
360    
# Line 387  _getopt_initialize (int argc, char *cons Line 363  _getopt_initialize (int argc, char *cons
363     recent call.     recent call.
364    
365     If LONG_ONLY is nonzero, '-' as well as '--' can introduce     If LONG_ONLY is nonzero, '-' as well as '--' can introduce
366     long-named options.  */     long-named options.
367    
368       If POSIXLY_CORRECT is nonzero, behave as if the POSIXLY_CORRECT
369       environment variable were set.  */
370    
371  int  int
372  _getopt_internal_r (int argc, char *const *argv, const char *optstring,  _getopt_internal_r (int argc, char **argv, const char *optstring,
373                      const struct option *longopts, int *longind,                      const struct option *longopts, int *longind,
374                      int long_only, struct _getopt_data *d)                      int long_only, int posixly_correct, struct _getopt_data *d)
375  {  {
376    int print_errors = d->opterr;    int print_errors = d->opterr;
377    if (optstring[0] == ':')    if (optstring[0] == ':')
# Line 407  _getopt_internal_r (int argc, char *cons Line 386  _getopt_internal_r (int argc, char *cons
386      {      {
387        if (d->optind == 0)        if (d->optind == 0)
388          d->optind = 1;  /* Don't scan ARGV[0], the program name.  */          d->optind = 1;  /* Don't scan ARGV[0], the program name.  */
389        optstring = _getopt_initialize (argc, argv, optstring, d);        optstring = _getopt_initialize (argc, argv, optstring,
390                                          posixly_correct, d);
391        d->__initialized = 1;        d->__initialized = 1;
392      }      }
393    
# Line 1155  _getopt_internal_r (int argc, char *cons Line 1135  _getopt_internal_r (int argc, char *cons
1135  }  }
1136    
1137  int  int
1138  _getopt_internal (int argc, char *const *argv, const char *optstring,  _getopt_internal (int argc, char **argv, const char *optstring,
1139                    const struct option *longopts, int *longind, int long_only)                    const struct option *longopts, int *longind,
1140                      int long_only, int posixly_correct)
1141  {  {
1142    int result;    int result;
1143    
1144    getopt_data.optind = optind;    getopt_data.optind = optind;
1145    getopt_data.opterr = opterr;    getopt_data.opterr = opterr;
1146    
1147    result = _getopt_internal_r (argc, argv, optstring, longopts,    result = _getopt_internal_r (argc, argv, optstring, longopts, longind,
1148                                 longind, long_only, &getopt_data);                                 long_only, posixly_correct, &getopt_data);
1149    
1150    optind = getopt_data.optind;    optind = getopt_data.optind;
1151    optarg = getopt_data.optarg;    optarg = getopt_data.optarg;
# Line 1173  _getopt_internal (int argc, char *const Line 1154  _getopt_internal (int argc, char *const
1154    return result;    return result;
1155  }  }
1156    
1157    /* glibc gets a LSB-compliant getopt.
1158       Standalone applications get a POSIX-compliant getopt.  */
1159    #if _LIBC
1160    enum { POSIXLY_CORRECT = 0 };
1161    #else
1162    enum { POSIXLY_CORRECT = 1 };
1163    #endif
1164    
1165  int  int
1166  getopt (int argc, char *const *argv, const char *optstring)  getopt (int argc, char *const *argv, const char *optstring)
1167  {  {
1168    return _getopt_internal (argc, argv, optstring,    return _getopt_internal (argc, (char **) argv, optstring, NULL, NULL, 0,
1169                             (const struct option *) 0,                             POSIXLY_CORRECT);
                            (int *) 0,  
                            0);  
1170  }  }
1171    
 #endif  /* Not ELIDE_CODE.  */  
1172    
1173  #ifdef TEST  #ifdef TEST
1174    

Legend:
Removed from v.1.9  
changed lines
  Added in v.1.10

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26