Imported from ../bash-4.0-rc1.tar.gz.
[platform/upstream/bash.git] / builtins / getopts.def
index a9c5116..c077c8e 100644 (file)
@@ -1,30 +1,32 @@
 This file is getopts.def, from which is created getopts.c.
 It implements the builtin "getopts" in Bash.
 
 This file is getopts.def, from which is created getopts.c.
 It implements the builtin "getopts" in Bash.
 
-Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
+Copyright (C) 1987-2004 Free Software Foundation, Inc.
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
 
 This file is part of GNU Bash, the Bourne Again SHell.
 
-Bash is free software; you can redistribute it and/or modify it under
-the terms of the GNU General Public License as published by the Free
-Software Foundation; either version 1, or (at your option) any later
-version.
+Bash is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
 
 
-Bash is distributed in the hope that it will be useful, but WITHOUT ANY
-WARRANTY; without even the implied warranty of MERCHANTABILITY or
-FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-for more details.
+Bash is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU General Public License for more details.
 
 
-You should have received a copy of the GNU General Public License along
-with Bash; see the file COPYING.  If not, write to the Free Software
-Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
+You should have received a copy of the GNU General Public License
+along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 
 $PRODUCES getopts.c
 
 $BUILTIN getopts
 $FUNCTION getopts_builtin
 $SHORT_DOC getopts optstring name [arg]
 
 $PRODUCES getopts.c
 
 $BUILTIN getopts
 $FUNCTION getopts_builtin
 $SHORT_DOC getopts optstring name [arg]
-Getopts is used by shell procedures to parse positional parameters.
+Parse option arguments.
+
+Getopts is used by shell procedures to parse positional parameters
+as options.
 
 OPTSTRING contains the option letters to be recognized; if a letter
 is followed by a colon, the option is expected to have an argument,
 
 OPTSTRING contains the option letters to be recognized; if a letter
 is followed by a colon, the option is expected to have an argument,
@@ -39,12 +41,12 @@ getopts places that argument into the shell variable OPTARG.
 
 getopts reports errors in one of two ways.  If the first character
 of OPTSTRING is a colon, getopts uses silent error reporting.  In
 
 getopts reports errors in one of two ways.  If the first character
 of OPTSTRING is a colon, getopts uses silent error reporting.  In
-this mode, no error messages are printed.  If an illegal option is
+this mode, no error messages are printed.  If an invalid option is
 seen, getopts places the option character found into OPTARG.  If a
 required argument is not found, getopts places a ':' into NAME and
 sets OPTARG to the option character found.  If getopts is not in
 seen, getopts places the option character found into OPTARG.  If a
 required argument is not found, getopts places a ':' into NAME and
 sets OPTARG to the option character found.  If getopts is not in
-silent mode, and an illegal option is seen, getopts places '?' into
-NAME and unsets OPTARG.  If a required option is not found, a '?'
+silent mode, and an invalid option is seen, getopts places '?' into
+NAME and unsets OPTARG.  If a required argument is not found, a '?'
 is placed in NAME, OPTARG is unset, and a diagnostic message is
 printed.
 
 is placed in NAME, OPTARG is unset, and a diagnostic message is
 printed.
 
@@ -54,6 +56,10 @@ OPTSTRING is not a colon.  OPTERR has the value 1 by default.
 
 Getopts normally parses the positional parameters ($0 - $9), but if
 more arguments are given, they are parsed instead.
 
 Getopts normally parses the positional parameters ($0 - $9), but if
 more arguments are given, they are parsed instead.
+
+Exit Status:
+Returns success if an option is found; fails if the end of options is
+encountered or an error occurs.
 $END
 
 #include <config.h>
 $END
 
 #include <config.h>
@@ -61,6 +67,9 @@ $END
 #include <stdio.h>
 
 #if defined (HAVE_UNISTD_H)
 #include <stdio.h>
 
 #if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
 #  include <unistd.h>
 #endif
 
 #  include <unistd.h>
 #endif
 
@@ -72,11 +81,13 @@ $END
 #include "getopt.h"
 
 #define G_EOF          -1
 #include "getopt.h"
 
 #define G_EOF          -1
-#define G_ILLEGAL_OPT  -2
+#define G_INVALID_OPT  -2
 #define G_ARG_MISSING  -3
 
 extern char *this_command_name;
 #define G_ARG_MISSING  -3
 
 extern char *this_command_name;
-extern WORD_LIST *rest_of_args;
+
+static int getopts_bind_variable __P((char *, char *));
+static int dogetopts __P((int, char **));
 
 /* getopts_reset is magic code for when OPTIND is reset.  N is the
    value that has just been assigned to OPTIND. */
 
 /* getopts_reset is magic code for when OPTIND is reset.  N is the
    value that has just been assigned to OPTIND. */
@@ -96,12 +107,12 @@ getopts_bind_variable (name, value)
 
   if (legal_identifier (name))
     {
 
   if (legal_identifier (name))
     {
-      v = bind_variable (name, value);
+      v = bind_variable (name, value, 0);
       return (v && (readonly_p (v) == 0)) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
     }
   else
     {
       return (v && (readonly_p (v) == 0)) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
     }
   else
     {
-      builtin_error ("`%s': not a valid identifier", name);
+      sh_invalidid (name);
       return (EXECUTION_FAILURE);
     }
 }
       return (EXECUTION_FAILURE);
     }
 }
@@ -110,9 +121,9 @@ getopts_bind_variable (name, value)
    (identical to that of ksh-88).  The special handling is enabled if
    the first character of the option string is a colon; this handling
    disables diagnostic messages concerning missing option arguments
    (identical to that of ksh-88).  The special handling is enabled if
    the first character of the option string is a colon; this handling
    disables diagnostic messages concerning missing option arguments
-   and illegal option characters.  The handling is as follows.
+   and invalid option characters.  The handling is as follows.
 
 
-   ILLEGAL OPTIONS:
+   INVALID OPTIONS:
         name -> "?"
         if (special_error) then
                 OPTARG = option character found
         name -> "?"
         if (special_error) then
                 OPTARG = option character found
@@ -178,6 +189,8 @@ dogetopts (argc, argv)
     {
       for (i = 0; i < 10 && dollar_vars[i]; i++)
        ;
     {
       for (i = 0; i < 10 && dollar_vars[i]; i++)
        ;
+
+      sh_getopt_restore_state (dollar_vars);
       ret = sh_getopt (i, dollar_vars, optstr);
     }
   else
       ret = sh_getopt (i, dollar_vars, optstr);
     }
   else
@@ -189,12 +202,13 @@ dogetopts (argc, argv)
        ;
       for (words = rest_of_args; words; words = words->next, i++)
        ;
        ;
       for (words = rest_of_args; words; words = words->next, i++)
        ;
-      v = (char **)xmalloc ((i + 1) * sizeof (char *));
+      v = strvec_create (i + 1);
       for (i = 0; i < 10 && dollar_vars[i]; i++)
       for (i = 0; i < 10 && dollar_vars[i]; i++)
-        v[i] = dollar_vars[i];
+       v[i] = dollar_vars[i];
       for (words = rest_of_args; words; words = words->next, i++)
       for (words = rest_of_args; words; words = words->next, i++)
-        v[i] = words->word->word;
+       v[i] = words->word->word;
       v[i] = (char *)NULL;
       v[i] = (char *)NULL;
+      sh_getopt_restore_state (v);
       ret = sh_getopt (i, v, optstr);
       free (v);
     }
       ret = sh_getopt (i, v, optstr);
       free (v);
     }
@@ -202,7 +216,8 @@ dogetopts (argc, argv)
   if (special_error)
     sh_opterr = old_opterr;
 
   if (special_error)
     sh_opterr = old_opterr;
 
-  /* Set the OPTIND variable in any case, to handle "--" skipping. */
+  /* Set the OPTIND variable in any case, to handle "--" skipping.  It's
+     highly unlikely that 14 digits will be too few. */
   if (sh_optind < 10)
     {
       numval[14] = sh_optind + '0';
   if (sh_optind < 10)
     {
       numval[14] = sh_optind + '0';
@@ -219,40 +234,41 @@ dogetopts (argc, argv)
        }
       while (n /= 10);
     }
        }
       while (n /= 10);
     }
-  bind_variable ("OPTIND", numval + i);
+  bind_variable ("OPTIND", numval + i, 0);
 
   /* If an error occurred, decide which one it is and set the return
      code appropriately.  In all cases, the option character in error
 
   /* If an error occurred, decide which one it is and set the return
      code appropriately.  In all cases, the option character in error
-     is in OPTOPT.  If an illegal option was encountered, OPTARG is
+     is in OPTOPT.  If an invalid option was encountered, OPTARG is
      NULL.  If a required option argument was missing, OPTARG points
      to a NULL string (that is, sh_optarg[0] == 0). */
   if (ret == '?')
     {
       if (sh_optarg == NULL)
      NULL.  If a required option argument was missing, OPTARG points
      to a NULL string (that is, sh_optarg[0] == 0). */
   if (ret == '?')
     {
       if (sh_optarg == NULL)
-       ret = G_ILLEGAL_OPT;
+       ret = G_INVALID_OPT;
       else if (sh_optarg[0] == '\0')
        ret = G_ARG_MISSING;
     }
            
   if (ret == G_EOF)
     {
       else if (sh_optarg[0] == '\0')
        ret = G_ARG_MISSING;
     }
            
   if (ret == G_EOF)
     {
+      unbind_variable ("OPTARG");
       getopts_bind_variable (name, "?");
       return (EXECUTION_FAILURE);
     }
 
       getopts_bind_variable (name, "?");
       return (EXECUTION_FAILURE);
     }
 
-  if (ret == G_ILLEGAL_OPT)
+  if (ret == G_INVALID_OPT)
     {
     {
-      /* Illegal option encountered. */
+      /* Invalid option encountered. */
       ret = getopts_bind_variable (name, "?");
 
       if (special_error)
        {
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
       ret = getopts_bind_variable (name, "?");
 
       if (special_error)
        {
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
-         bind_variable ("OPTARG", strval);
+         bind_variable ("OPTARG", strval, 0);
        }
       else
        }
       else
-       makunbound ("OPTARG", shell_variables);
+       unbind_variable ("OPTARG");
 
       return (ret);
     }
 
       return (ret);
     }
@@ -266,17 +282,17 @@ dogetopts (argc, argv)
 
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
 
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
-         bind_variable ("OPTARG", strval);
+         bind_variable ("OPTARG", strval, 0);
        }
       else
        {
          ret = getopts_bind_variable (name, "?");
        }
       else
        {
          ret = getopts_bind_variable (name, "?");
-         makunbound ("OPTARG", shell_variables);
+         unbind_variable ("OPTARG");
        }
       return (ret);
     }                  
 
        }
       return (ret);
     }                  
 
-  bind_variable ("OPTARG", sh_optarg);
+  bind_variable ("OPTARG", sh_optarg, 0);
 
   strval[0] = (char) ret;
   strval[1] = '\0';
 
   strval[0] = (char) ret;
   strval[1] = '\0';
@@ -298,14 +314,10 @@ getopts_builtin (list)
     }
 
   reset_internal_getopt ();
     }
 
   reset_internal_getopt ();
-  while ((ret = internal_getopt (list, "")) != -1)
+  if (internal_getopt (list, "") != -1)
     {
     {
-      switch (ret)
-       {
-       default:
-         builtin_usage ();
-         return (EX_USAGE);
-       }
+      builtin_usage ();
+      return (EX_USAGE);
     }
   list = loptend;
 
     }
   list = loptend;