/[bison]/bison/lib/getopt.c
ViewVC logotype

Diff of /bison/lib/getopt.c

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

revision 1.1 by akim, Tue Mar 28 13:30:57 2000 UTC revision 1.2 by eggert, Fri Oct 26 07:26:00 2001 UTC
# Line 1  Line 1 
1  /* Getopt for GNU.  /* Getopt for GNU.
2     NOTE: The canonical source of this file is maintained with the GNU     NOTE: getopt is now part of the C library, so if you don't know what
3     C Library.  Bugs can be reported to bug-glibc@gnu.org.     "Keep this file name-space clean" means, talk to drepper@gnu.org
4       before changing it!
5     Copyright (C) 1987, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99     Copyright (C) 1987,88,89,90,91,92,93,94,95,96,98,99,2000,2001
6          Free Software Foundation, Inc.          Free Software Foundation, Inc.
7       This file is part of the GNU C Library.
8    
9     This program is free software; you can redistribute it and/or modify it     The GNU C Library is free software; you can redistribute it and/or
10     under the terms of the GNU General Public License as published by the     modify it under the terms of the GNU Lesser General Public
11     Free Software Foundation; either version 2, or (at your option) any     License as published by the Free Software Foundation; either
12     later version.     version 2.1 of the License, or (at your option) any later version.
13    
14     This program is distributed in the hope that it will be useful,     The GNU C Library 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     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17     GNU General Public License for more details.     Lesser General Public License for more details.
18    
19     You should have received a copy of the GNU General Public License     You should have received a copy of the GNU Lesser General Public
20     along with this program; if not, write to the Free Software Foundation,     License along with the GNU C Library; if not, write to the Free
21     Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */     Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22       02111-1307 USA.  */
23    
24  /* 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>.
25     Ditto for AIX 3.2 and <stdlib.h>.  */     Ditto for AIX 3.2 and <stdlib.h>.  */
# Line 27  Line 29 
29    
30  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
31  # include <config.h>  # include <config.h>
32  #else  #endif
33  # if !defined __STDC__ || !__STDC__  
34    #if !defined __STDC__ || !__STDC__
35  /* This is a separate conditional since some stdc systems  /* This is a separate conditional since some stdc systems
36     reject `defined (const)'.  */     reject `defined (const)'.  */
37  #  ifndef const  # ifndef const
38  #   define const  #  define const
 #  endif  
39  # endif  # endif
40  #endif  #endif
41    
# Line 75  Line 77 
77  #endif  #endif
78    
79  #ifndef _  #ifndef _
80  /* This is for other GNU distributions with internationalized messages.  /* This is for other GNU distributions with internationalized messages.  */
81     When compiling libc, the _ macro is predefined.  */  # if (HAVE_LIBINTL_H && ENABLE_NLS) || defined _LIBC
 # ifdef HAVE_LIBINTL_H  
82  #  include <libintl.h>  #  include <libintl.h>
83  #  define _(msgid)      gettext (msgid)  #  ifndef _
84    #   define _(msgid)     gettext (msgid)
85    #  endif
86  # else  # else
87  #  define _(msgid)      (msgid)  #  define _(msgid)      (msgid)
88  # endif  # endif
# Line 250  static int last_nonopt; Line 253  static int last_nonopt;
253  /* Bash 2.0 gives us an environment variable containing flags  /* Bash 2.0 gives us an environment variable containing flags
254     indicating ARGV elements that should not be considered arguments.  */     indicating ARGV elements that should not be considered arguments.  */
255    
256    #ifdef USE_NONOPTION_FLAGS
257  /* Defined in getopt_init.c  */  /* Defined in getopt_init.c  */
258  extern char *__getopt_nonoption_flags;  extern char *__getopt_nonoption_flags;
259    
260  static int nonoption_flags_max_len;  static int nonoption_flags_max_len;
261  static int nonoption_flags_len;  static int nonoption_flags_len;
262    #endif
263    
264  static int original_argc;  static int original_argc;
265  static char *const *original_argv;  static char *const *original_argv;
# Line 275  store_args_and_env (int argc, char *cons Line 280  store_args_and_env (int argc, char *cons
280  text_set_element (__libc_subinit, store_args_and_env);  text_set_element (__libc_subinit, store_args_and_env);
281  # endif /* text_set_element */  # endif /* text_set_element */
282    
283  # define SWAP_FLAGS(ch1, ch2) \  # ifdef USE_NONOPTION_FLAGS
284    #  define SWAP_FLAGS(ch1, ch2) \
285    if (nonoption_flags_len > 0)                                                \    if (nonoption_flags_len > 0)                                                \
286      {                                                                         \      {                                                                         \
287        char __tmp = __getopt_nonoption_flags[ch1];                             \        char __tmp = __getopt_nonoption_flags[ch1];                             \
288        __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \        __getopt_nonoption_flags[ch1] = __getopt_nonoption_flags[ch2];          \
289        __getopt_nonoption_flags[ch2] = __tmp;                                  \        __getopt_nonoption_flags[ch2] = __tmp;                                  \
290      }      }
291    # else
292    #  define SWAP_FLAGS(ch1, ch2)
293    # endif
294  #else   /* !_LIBC */  #else   /* !_LIBC */
295  # define SWAP_FLAGS(ch1, ch2)  # define SWAP_FLAGS(ch1, ch2)
296  #endif  /* _LIBC */  #endif  /* _LIBC */
# Line 313  exchange (argv) Line 322  exchange (argv)
322       It leaves the longer segment in the right place overall,       It leaves the longer segment in the right place overall,
323       but it consists of two parts that need to be swapped next.  */       but it consists of two parts that need to be swapped next.  */
324    
325  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
326    /* First make sure the handling of the `__getopt_nonoption_flags'    /* First make sure the handling of the `__getopt_nonoption_flags'
327       string can work normally.  Our top argument must be in the range       string can work normally.  Our top argument must be in the range
328       of the string.  */       of the string.  */
# Line 417  _getopt_initialize (argc, argv, optstrin Line 426  _getopt_initialize (argc, argv, optstrin
426    else    else
427      ordering = PERMUTE;      ordering = PERMUTE;
428    
429  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
430    if (posixly_correct == NULL    if (posixly_correct == NULL
431        && argc == original_argc && argv == original_argv)        && argc == original_argc && argv == original_argv)
432      {      {
# Line 515  _getopt_internal (argc, argv, optstring, Line 524  _getopt_internal (argc, argv, optstring,
524       int *longind;       int *longind;
525       int long_only;       int long_only;
526  {  {
527      int print_errors = opterr;
528      if (optstring[0] == ':')
529        print_errors = 0;
530    
531      if (argc < 1)
532        return -1;
533    
534    optarg = NULL;    optarg = NULL;
535    
536    if (optind == 0 || !__getopt_initialized)    if (optind == 0 || !__getopt_initialized)
# Line 529  _getopt_internal (argc, argv, optstring, Line 545  _getopt_internal (argc, argv, optstring,
545       Either it does not have option syntax, or there is an environment flag       Either it does not have option syntax, or there is an environment flag
546       from the shell indicating it is not an option.  The later information       from the shell indicating it is not an option.  The later information
547       is only used when the used in the GNU libc.  */       is only used when the used in the GNU libc.  */
548  #ifdef _LIBC  #if defined _LIBC && defined USE_NONOPTION_FLAGS
549  # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \  # define NONOPTION_P (argv[optind][0] != '-' || argv[optind][1] == '\0'       \
550                        || (optind < nonoption_flags_len                        \                        || (optind < nonoption_flags_len                        \
551                            && __getopt_nonoption_flags[optind] == '1'))                            && __getopt_nonoption_flags[optind] == '1'))
# Line 664  _getopt_internal (argc, argv, optstring, Line 680  _getopt_internal (argc, argv, optstring,
680                  pfound = p;                  pfound = p;
681                  indfound = option_index;                  indfound = option_index;
682                }                }
683              else              else if (long_only
684                         || pfound->has_arg != p->has_arg
685                         || pfound->flag != p->flag
686                         || pfound->val != p->val)
687                /* Second or later nonexact match found.  */                /* Second or later nonexact match found.  */
688                ambig = 1;                ambig = 1;
689            }            }
690    
691        if (ambig && !exact)        if (ambig && !exact)
692          {          {
693            if (opterr)            if (print_errors)
694              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),              fprintf (stderr, _("%s: option `%s' is ambiguous\n"),
695                       argv[0], argv[optind]);                       argv[0], argv[optind]);
696            nextchar += strlen (nextchar);            nextchar += strlen (nextchar);
# Line 692  _getopt_internal (argc, argv, optstring, Line 711  _getopt_internal (argc, argv, optstring,
711                  optarg = nameend + 1;                  optarg = nameend + 1;
712                else                else
713                  {                  {
714                    if (opterr)                    if (print_errors)
715                      {                      {
716                        if (argv[optind - 1][1] == '-')                        if (argv[optind - 1][1] == '-')
717                          /* --option */                          /* --option */
# Line 718  _getopt_internal (argc, argv, optstring, Line 737  _getopt_internal (argc, argv, optstring,
737                  optarg = argv[optind++];                  optarg = argv[optind++];
738                else                else
739                  {                  {
740                    if (opterr)                    if (print_errors)
741                      fprintf (stderr,                      fprintf (stderr,
742                             _("%s: option `%s' requires an argument\n"),                             _("%s: option `%s' requires an argument\n"),
743                             argv[0], argv[optind - 1]);                             argv[0], argv[optind - 1]);
# Line 745  _getopt_internal (argc, argv, optstring, Line 764  _getopt_internal (argc, argv, optstring,
764        if (!long_only || argv[optind][1] == '-'        if (!long_only || argv[optind][1] == '-'
765            || my_index (optstring, *nextchar) == NULL)            || my_index (optstring, *nextchar) == NULL)
766          {          {
767            if (opterr)            if (print_errors)
768              {              {
769                if (argv[optind][1] == '-')                if (argv[optind][1] == '-')
770                  /* --option */                  /* --option */
# Line 775  _getopt_internal (argc, argv, optstring, Line 794  _getopt_internal (argc, argv, optstring,
794    
795      if (temp == NULL || c == ':')      if (temp == NULL || c == ':')
796        {        {
797          if (opterr)          if (print_errors)
798            {            {
799              if (posixly_correct)              if (posixly_correct)
800                /* 1003.2 specifies the format of this message.  */                /* 1003.2 specifies the format of this message.  */
# Line 809  _getopt_internal (argc, argv, optstring, Line 828  _getopt_internal (argc, argv, optstring,
828            }            }
829          else if (optind == argc)          else if (optind == argc)
830            {            {
831              if (opterr)              if (print_errors)
832                {                {
833                  /* 1003.2 specifies the format of this message.  */                  /* 1003.2 specifies the format of this message.  */
834                  fprintf (stderr, _("%s: option requires an argument -- %c\n"),                  fprintf (stderr, _("%s: option requires an argument -- %c\n"),
# Line 858  _getopt_internal (argc, argv, optstring, Line 877  _getopt_internal (argc, argv, optstring,
877              }              }
878          if (ambig && !exact)          if (ambig && !exact)
879            {            {
880              if (opterr)              if (print_errors)
881                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),                fprintf (stderr, _("%s: option `-W %s' is ambiguous\n"),
882                         argv[0], argv[optind]);                         argv[0], argv[optind]);
883              nextchar += strlen (nextchar);              nextchar += strlen (nextchar);
# Line 876  _getopt_internal (argc, argv, optstring, Line 895  _getopt_internal (argc, argv, optstring,
895                    optarg = nameend + 1;                    optarg = nameend + 1;
896                  else                  else
897                    {                    {
898                      if (opterr)                      if (print_errors)
899                        fprintf (stderr, _("\                        fprintf (stderr, _("\
900  %s: option `-W %s' doesn't allow an argument\n"),  %s: option `-W %s' doesn't allow an argument\n"),
901                                 argv[0], pfound->name);                                 argv[0], pfound->name);
# Line 891  _getopt_internal (argc, argv, optstring, Line 910  _getopt_internal (argc, argv, optstring,
910                    optarg = argv[optind++];                    optarg = argv[optind++];
911                  else                  else
912                    {                    {
913                      if (opterr)                      if (print_errors)
914                        fprintf (stderr,                        fprintf (stderr,
915                                 _("%s: option `%s' requires an argument\n"),                                 _("%s: option `%s' requires an argument\n"),
916                                 argv[0], argv[optind - 1]);                                 argv[0], argv[optind - 1]);
# Line 938  _getopt_internal (argc, argv, optstring, Line 957  _getopt_internal (argc, argv, optstring,
957                }                }
958              else if (optind == argc)              else if (optind == argc)
959                {                {
960                  if (opterr)                  if (print_errors)
961                    {                    {
962                      /* 1003.2 specifies the format of this message.  */                      /* 1003.2 specifies the format of this message.  */
963                      fprintf (stderr,                      fprintf (stderr,
964                             _("%s: option requires an argument -- %c\n"),                               _("%s: option requires an argument -- %c\n"),
965                             argv[0], c);                               argv[0], c);
966                    }                    }
967                  optopt = c;                  optopt = c;
968                  if (optstring[0] == ':')                  if (optstring[0] == ':')

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

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