386BSD 0.1 development
[unix-history] / usr / src / bin / sh / options.c
index 189d0aa..1325db7 100644 (file)
@@ -5,11 +5,37 @@
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
  *
  * This code is derived from software contributed to Berkeley by
  * Kenneth Almquist.
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)options.c  5.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)options.c  5.2 (Berkeley) 3/13/91";
 #endif /* not lint */
 
 #include "shell.h"
 #endif /* not lint */
 
 #include "shell.h"
@@ -99,13 +125,23 @@ options(cmdline) {
        int val;
        int c;
 
        int val;
        int c;
 
-       minusc = NULL;
+       if (cmdline)
+               minusc = NULL;
        while ((p = *argptr) != NULL) {
                argptr++;
                if ((c = *p++) == '-') {
                        val = 1;
        while ((p = *argptr) != NULL) {
                argptr++;
                if ((c = *p++) == '-') {
                        val = 1;
-                       if (p[0] == '\0' || p[0] == '-' && p[1] == '\0')
+                        if (p[0] == '\0' || p[0] == '-' && p[1] == '\0') {
+                                if (!cmdline) {
+                                        /* "-" means turn off -x and -v */
+                                        if (p[0] == '\0')
+                                                xflag = vflag = 0;
+                                        /* "--" means reset params */
+                                        else if (*argptr == NULL)
+                                                setparam(argptr);
+                                }
                                break;    /* "-" or  "--" terminates options */
                                break;    /* "-" or  "--" terminates options */
+                       }
                } else if (c == '+') {
                        val = 0;
                } else {
                } else if (c == '+') {
                        val = 0;
                } else {
@@ -115,7 +151,7 @@ options(cmdline) {
                while ((c = *p++) != '\0') {
                        if (c == 'c' && cmdline) {
                                char *q;
                while ((c = *p++) != '\0') {
                        if (c == 'c' && cmdline) {
                                char *q;
-#ifdef NOHACK
+#ifdef NOHACK  /* removing this code allows sh -ce 'foo' for compat */
                                if (*p == '\0')
 #endif
                                        q = *argptr++;
                                if (*p == '\0')
 #endif
                                        q = *argptr++;
@@ -312,13 +348,11 @@ out:
        return 0;
 }
 
        return 0;
 }
 
-
 /*
  * Standard option processing (a la getopt) for builtin routines.  The
  * only argument that is passed to nextopt is the option string; the
 /*
  * Standard option processing (a la getopt) for builtin routines.  The
  * only argument that is passed to nextopt is the option string; the
- * other arguments are unnecessary.  It return the character, or -1 on
- * end of input.  This routine assumes that all characters in optstring
- * are positive.
+ * other arguments are unnecessary.  It return the character, or '\0' on
+ * end of input.
  */
 
 int
  */
 
 int
@@ -331,10 +365,10 @@ nextopt(optstring)
        if ((p = optptr) == NULL || *p == '\0') {
                p = *argptr;
                if (p == NULL || *p != '-' || *++p == '\0')
        if ((p = optptr) == NULL || *p == '\0') {
                p = *argptr;
                if (p == NULL || *p != '-' || *++p == '\0')
-                       return -1;
+                       return '\0';
                argptr++;
                if (p[0] == '-' && p[1] == '\0')        /* check for "--" */
                argptr++;
                if (p[0] == '-' && p[1] == '\0')        /* check for "--" */
-                       return -1;
+                       return '\0';
        }
        c = *p++;
        for (q = optstring ; *q != c ; ) {
        }
        c = *p++;
        for (q = optstring ; *q != c ; ) {