Bash-4.2 distribution sources and documentation
[platform/upstream/bash.git] / builtins / getopts.def
index a2a82ff..1d2a68a 100644 (file)
@@ -1,30 +1,32 @@
 This file is getopts.def, from which is created getopts.c.
 It implements the builtin "getopts" in Bash.
 
-Copyright (C) 1987-2002 Free Software Foundation, Inc.
+Copyright (C) 1987-2004 Free Software Foundation, Inc.
 
 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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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]
-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,
@@ -44,7 +46,7 @@ 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 invalid option is seen, getopts places '?' into
-NAME and unsets OPTARG.  If a required option is not found, a '?'
+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.
 
@@ -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.
+
+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>
@@ -101,8 +107,10 @@ getopts_bind_variable (name, value)
 
   if (legal_identifier (name))
     {
-      v = bind_variable (name, value);
-      return (v && (readonly_p (v) == 0)) ? EXECUTION_SUCCESS : EXECUTION_FAILURE;
+      v = bind_variable (name, value, 0);
+      if (v && (readonly_p (v) || noassign_p (v)))
+       return (EX_MISCERROR);
+      return (v ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
     }
   else
     {
@@ -228,7 +236,7 @@ dogetopts (argc, argv)
        }
       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
@@ -245,6 +253,7 @@ dogetopts (argc, argv)
            
   if (ret == G_EOF)
     {
+      unbind_variable ("OPTARG");
       getopts_bind_variable (name, "?");
       return (EXECUTION_FAILURE);
     }
@@ -258,7 +267,7 @@ dogetopts (argc, argv)
        {
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
-         bind_variable ("OPTARG", strval);
+         bind_variable ("OPTARG", strval, 0);
        }
       else
        unbind_variable ("OPTARG");
@@ -275,7 +284,7 @@ dogetopts (argc, argv)
 
          strval[0] = (char)sh_optopt;
          strval[1] = '\0';
-         bind_variable ("OPTARG", strval);
+         bind_variable ("OPTARG", strval, 0);
        }
       else
        {
@@ -285,7 +294,7 @@ dogetopts (argc, argv)
       return (ret);
     }                  
 
-  bind_variable ("OPTARG", sh_optarg);
+  bind_variable ("OPTARG", sh_optarg, 0);
 
   strval[0] = (char) ret;
   strval[1] = '\0';