Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh Implement the NETBSD_SHELL readonly unexportable unim...



details:   https://anonhg.NetBSD.org/src/rev/fc72f005e7ce
branches:  trunk
changeset: 344464:fc72f005e7ce
user:      christos <christos%NetBSD.org@localhost>
date:      Thu Mar 31 16:16:35 2016 +0000

description:
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name).  For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)

diffstat:

 bin/sh/expand.c  |   6 ++--
 bin/sh/main.c    |   5 +--
 bin/sh/options.c |  31 +++++++++++++++++------
 bin/sh/options.h |   6 +++-
 bin/sh/var.c     |  74 ++++++++++++++++++++++++++++++++++++++++++++++---------
 bin/sh/var.h     |  21 ++++++++-------
 bin/sh/version.h |  40 ++++++++++++++++++++++++++++++
 7 files changed, 145 insertions(+), 38 deletions(-)

diffs (truncated from 406 to 300 lines):

diff -r d1769b812bfa -r fc72f005e7ce bin/sh/expand.c
--- a/bin/sh/expand.c   Thu Mar 31 16:12:52 2016 +0000
+++ b/bin/sh/expand.c   Thu Mar 31 16:16:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: expand.c,v 1.100 2016/03/31 13:27:44 christos Exp $    */
+/*     $NetBSD: expand.c,v 1.101 2016/03/31 16:16:35 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)expand.c   8.5 (Berkeley) 5/15/95";
 #else
-__RCSID("$NetBSD: expand.c,v 1.100 2016/03/31 13:27:44 christos Exp $");
+__RCSID("$NetBSD: expand.c,v 1.101 2016/03/31 16:16:35 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -907,7 +907,7 @@
                expdest = cvtnum(num, expdest);
                break;
        case '-':
-               for (i = 0; optlist[i].name; i++) {
+               for (i = 0; optlist[i].name || optlist[i].letter; i++) {
                        if (optlist[i].val && optlist[i].letter)
                                STPUTC(optlist[i].letter, expdest);
                }
diff -r d1769b812bfa -r fc72f005e7ce bin/sh/main.c
--- a/bin/sh/main.c     Thu Mar 31 16:12:52 2016 +0000
+++ b/bin/sh/main.c     Thu Mar 31 16:16:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.63 2016/03/27 14:34:46 christos Exp $       */
+/*     $NetBSD: main.c,v 1.64 2016/03/31 16:16:35 christos Exp $       */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.7 (Berkeley) 7/19/95";
 #else
-__RCSID("$NetBSD: main.c,v 1.63 2016/03/27 14:34:46 christos Exp $");
+__RCSID("$NetBSD: main.c,v 1.64 2016/03/31 16:16:35 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -82,7 +82,6 @@
 
 int rootpid;
 int rootshell;
-int posix;
 #if PROFILE
 short profile_buf[16384];
 extern int etext();
diff -r d1769b812bfa -r fc72f005e7ce bin/sh/options.c
--- a/bin/sh/options.c  Thu Mar 31 16:12:52 2016 +0000
+++ b/bin/sh/options.c  Thu Mar 31 16:16:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: options.c,v 1.45 2016/03/08 14:08:39 christos Exp $    */
+/*     $NetBSD: options.c,v 1.46 2016/03/31 16:16:35 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)options.c  8.2 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: options.c,v 1.45 2016/03/08 14:08:39 christos Exp $");
+__RCSID("$NetBSD: options.c,v 1.46 2016/03/31 16:16:35 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -224,25 +224,40 @@
 minus_o(char *name, int val)
 {
        size_t i;
+       const char *sep = ": ";
 
        if (name == NULL) {
                if (val) {
-                       out1str("Current option settings\n");
+                       out1str("Current option settings");
                        for (i = 0; i < NOPTS; i++) {
-                               out1fmt("%-16s%s\n", optlist[i].name,
+                               if (optlist[i].name == NULL)  {
+                                       out1fmt("%s%c%c", sep,
+                                           "+-"[optlist[i].val],
+                                           optlist[i].letter);
+                                       sep = ", ";
+                               }
+                       }
+                       out1c('\n');
+                       for (i = 0; i < NOPTS; i++) {
+                               if (optlist[i].name)
+                                   out1fmt("%-16s%s\n", optlist[i].name,
                                        optlist[i].val ? "on" : "off");
                        }
                } else {
                        out1str("set");
                        for (i = 0; i < NOPTS; i++) {
-                               out1fmt(" %co %s",
+                               if (optlist[i].name)
+                                   out1fmt(" %co %s",
                                        "+-"[optlist[i].val], optlist[i].name);
+                               else
+                                   out1fmt(" %c%c", "+-"[optlist[i].val],
+                                       optlist[i].letter);
                        }
-                       out1str("\n");
+                       out1c('\n');
                }
        } else {
                for (i = 0; i < NOPTS; i++)
-                       if (equal(name, optlist[i].name)) {
+                       if (optlist[i].name && equal(name, optlist[i].name)) {
                                set_opt_val(i, val);
                                return;
                        }
@@ -366,7 +381,7 @@
 setcmd(int argc, char **argv)
 {
        if (argc == 1)
-               return showvars(0, 0, 1);
+               return showvars(0, 0, 1, 0);
        INTOFF;
        options(0);
        optschanged();
diff -r d1769b812bfa -r fc72f005e7ce bin/sh/options.h
--- a/bin/sh/options.h  Thu Mar 31 16:12:52 2016 +0000
+++ b/bin/sh/options.h  Thu Mar 31 16:16:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: options.h,v 1.24 2016/02/23 18:30:16 christos Exp $    */
+/*     $NetBSD: options.h,v 1.25 2016/03/31 16:16:35 christos Exp $    */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -105,9 +105,11 @@
 #define        pflag optlist[20].val
 DEF_OPT( "trackall",   'h' )   /* [U] locate cmds in funcs when defined */
 #define        hflag optlist[21].val
+DEF_OPT( "posix",      0  )    /* operate in posix mode */
+#define        posix optlist[22].val
 #ifdef DEBUG
 DEF_OPT( "debug",      0 )     /* enable debug prints */
-#define        debug optlist[22].val
+#define        debug optlist[23].val
 #endif
 
 #ifdef DEFINE_OPTIONS
diff -r d1769b812bfa -r fc72f005e7ce bin/sh/var.c
--- a/bin/sh/var.c      Thu Mar 31 16:12:52 2016 +0000
+++ b/bin/sh/var.c      Thu Mar 31 16:16:35 2016 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: var.c,v 1.48 2016/03/27 14:34:46 christos Exp $        */
+/*     $NetBSD: var.c,v 1.49 2016/03/31 16:16:35 christos Exp $        */
 
 /*-
  * Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)var.c      8.3 (Berkeley) 5/4/95";
 #else
-__RCSID("$NetBSD: var.c,v 1.48 2016/03/27 14:34:46 christos Exp $");
+__RCSID("$NetBSD: var.c,v 1.49 2016/03/31 16:16:35 christos Exp $");
 #endif
 #endif /* not lint */
 
@@ -98,6 +98,7 @@
 struct var vps1;
 struct var vps2;
 struct var vps4;
+struct var vvers;
 struct var voptind;
 
 char ifs_default[] = " \t\n";
@@ -113,6 +114,8 @@
          NULL },
        { &vmpath,      VSTRFIXED|VTEXTFIXED|VUNSET,    "MAILPATH=",
          NULL },
+       { &vvers,       VSTRFIXED|VTEXTFIXED|VNOEXPORT, "NETBSD_SHELL=",
+         NULL },
        { &vpath,       VSTRFIXED|VTEXTFIXED,           "PATH=" _PATH_DEFPATH,
          changepath },
        /*
@@ -145,6 +148,7 @@
 INCLUDE <stdio.h>
 INCLUDE <unistd.h>
 INCLUDE "var.h"
+INCLUDE "version.h"
 MKINIT char **environ;
 INIT {
        char **envp;
@@ -165,6 +169,7 @@
        snprintf(buf, sizeof(buf), "%d", (int)getppid());
        setvar("PPID", buf, VREADONLY);
        setvar("IFS", ifs_default, VTEXTFIXED);
+       setvar("NETBSD_SHELL", NETBSD_SHELL, VTEXTFIXED|VREADONLY|VNOEXPORT);
 }
 #endif
 
@@ -294,7 +299,7 @@
        struct var *vp, **vpp;
        int nlen;
 
-       if (aflag)
+       if (aflag && !(flags & VNOEXPORT))
                flags |= VEXPORT;
        vp = find_var(s, &vpp, &nlen);
        if (vp != NULL) {
@@ -311,6 +316,8 @@
                        ckfree(vp->text);
 
                vp->flags &= ~(VTEXTFIXED|VSTACK|VUNSET);
+               if (flags & VNOEXPORT)
+                       vp->flags &= ~VEXPORT;
                vp->flags |= flags & ~VNOFUNC;
                vp->text = s;
 
@@ -529,7 +536,7 @@
  */
 
 int
-showvars(const char *name, int flag, int show_value)
+showvars(const char *name, int flag, int show_value, const char *xtra)
 {
        struct var **vpp;
        struct var *vp;
@@ -565,6 +572,8 @@
                vp = *vpp;
                if (name)
                        out1fmt("%s ", name);
+               if (xtra)
+                       out1fmt("%s ", xtra);
                for (p = vp->text ; *p != '=' ; p++)
                        out1c(*p);
                if (!(vp->flags & VUNSET) && show_value) {
@@ -589,27 +598,68 @@
        char *name;
        const char *p;
        int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
-       int pflg;
+       int pflg = 0;
+       int nflg = 0;
+       int xflg = 0;
+       int res;
+       int c;
+
 
-       pflg = nextopt("p") == 'p' ? 3 : 0;
-       if (argc <= 1 || pflg) {
-               showvars( pflg ? argv[0] : 0, flag, pflg );
+       while ((c = nextopt("npx")) != '\0') {
+               switch (c) {
+               case 'p':
+                       if (nflg)
+                               return 1;
+                       pflg = 3;
+                       break;
+               case 'n':
+                       if (pflg || xflg || flag == VREADONLY)
+                               return 1;
+                       nflg = 1;
+                       break;
+               case 'x':
+                       if (nflg || flag == VREADONLY)
+                               return 1;
+                       flag = VNOEXPORT;
+                       xflg = 1;
+                       break;
+               default:
+                       return 1;
+               }
+       }
+
+       if (nflg && *argptr == NULL)
+               return 1;
+
+       if (pflg || *argptr == NULL) {
+               showvars( pflg ? argv[0] : 0, flag, pflg,
+                   pflg && xflg ? "-x" : NULL );
                return 0;
        }
 
+       res = 0;
        while ((name = *argptr++) != NULL) {
                if ((p = strchr(name, '=')) != NULL) {
                        p++;
                } else {
                        vp = find_var(name, NULL, NULL);
                        if (vp != NULL) {
-                               vp->flags |= flag;
+                               if (nflg)
+                                       vp->flags &= ~flag;
+                               else if (flag&VEXPORT && vp->flags&VNOEXPORT)
+                                       res = 1;
+                               else {
+                                       vp->flags |= flag;
+                                       if (flag == VNOEXPORT)
+                                               vp->flags &= ~VEXPORT;
+                               }
                                continue;



Home | Main Index | Thread Index | Old Index