Module Name: src
Committed By: christos
Date: Thu Mar 31 16:16:35 UTC 2016
Modified Files:
src/bin/sh: expand.c main.c options.c options.h var.c var.h
Added Files:
src/bin/sh: version.h
Log Message:
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@)
To generate a diff of this commit:
cvs rdiff -u -r1.100 -r1.101 src/bin/sh/expand.c
cvs rdiff -u -r1.63 -r1.64 src/bin/sh/main.c
cvs rdiff -u -r1.45 -r1.46 src/bin/sh/options.c
cvs rdiff -u -r1.24 -r1.25 src/bin/sh/options.h
cvs rdiff -u -r1.48 -r1.49 src/bin/sh/var.c
cvs rdiff -u -r1.27 -r1.28 src/bin/sh/var.h
cvs rdiff -u -r0 -r1.1 src/bin/sh/version.h
Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.
Modified files:
Index: src/bin/sh/expand.c
diff -u src/bin/sh/expand.c:1.100 src/bin/sh/expand.c:1.101
--- src/bin/sh/expand.c:1.100 Thu Mar 31 09:27:44 2016
+++ src/bin/sh/expand.c Thu Mar 31 12:16:35 2016
@@ -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 @@ numvar:
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);
}
Index: src/bin/sh/main.c
diff -u src/bin/sh/main.c:1.63 src/bin/sh/main.c:1.64
--- src/bin/sh/main.c:1.63 Sun Mar 27 10:34:46 2016
+++ src/bin/sh/main.c Thu Mar 31 12:16:35 2016
@@ -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 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19
#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 @@ __RCSID("$NetBSD: main.c,v 1.63 2016/03/
int rootpid;
int rootshell;
-int posix;
#if PROFILE
short profile_buf[16384];
extern int etext();
Index: src/bin/sh/options.c
diff -u src/bin/sh/options.c:1.45 src/bin/sh/options.c:1.46
--- src/bin/sh/options.c:1.45 Tue Mar 8 09:08:39 2016
+++ src/bin/sh/options.c Thu Mar 31 12:16:35 2016
@@ -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 @@ STATIC void
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 @@ int
setcmd(int argc, char **argv)
{
if (argc == 1)
- return showvars(0, 0, 1);
+ return showvars(0, 0, 1, 0);
INTOFF;
options(0);
optschanged();
Index: src/bin/sh/options.h
diff -u src/bin/sh/options.h:1.24 src/bin/sh/options.h:1.25
--- src/bin/sh/options.h:1.24 Tue Feb 23 13:30:16 2016
+++ src/bin/sh/options.h Thu Mar 31 12:16:35 2016
@@ -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 @@ DEF_OPT( "nopriv", 'p' ) /* preserve pri
#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
Index: src/bin/sh/var.c
diff -u src/bin/sh/var.c:1.48 src/bin/sh/var.c:1.49
--- src/bin/sh/var.c:1.48 Sun Mar 27 10:34:46 2016
+++ src/bin/sh/var.c Thu Mar 31 12:16:35 2016
@@ -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 vpath;
struct var vps1;
struct var vps2;
struct var vps4;
+struct var vvers;
struct var voptind;
char ifs_default[] = " \t\n";
@@ -113,6 +114,8 @@ const struct varinit varinit[] = {
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 @@ STATIC struct var *find_var(const char *
INCLUDE <stdio.h>
INCLUDE <unistd.h>
INCLUDE "var.h"
+INCLUDE "version.h"
MKINIT char **environ;
INIT {
char **envp;
@@ -165,6 +169,7 @@ INIT {
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 @@ setvareq(char *s, int flags)
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 @@ setvareq(char *s, int flags)
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 @@ sort_var(const void *v_v1, const void *v
*/
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 @@ showvars(const char *name, int flag, int
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 @@ exportcmd(int argc, char **argv)
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;
+
+
+ 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;
+ }
+ }
- pflg = nextopt("p") == 'p' ? 3 : 0;
- if (argc <= 1 || pflg) {
- showvars( pflg ? argv[0] : 0, flag, pflg );
+ 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;
}
}
- setvar(name, p, flag);
+ if (!nflg)
+ setvar(name, p, flag);
}
- return 0;
+ return res;
}
@@ -770,7 +820,7 @@ unsetvar(const char *s, int unexport)
if (vp == NULL)
return 0;
- if (vp->flags & VREADONLY)
+ if (vp->flags & VREADONLY && !unexport)
return 1;
INTOFF;
Index: src/bin/sh/var.h
diff -u src/bin/sh/var.h:1.27 src/bin/sh/var.h:1.28
--- src/bin/sh/var.h:1.27 Sun Mar 27 10:34:46 2016
+++ src/bin/sh/var.h Thu Mar 31 12:16:35 2016
@@ -1,4 +1,4 @@
-/* $NetBSD: var.h,v 1.27 2016/03/27 14:34:46 christos Exp $ */
+/* $NetBSD: var.h,v 1.28 2016/03/31 16:16:35 christos Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -39,14 +39,15 @@
*/
/* flags */
-#define VEXPORT 0x01 /* variable is exported */
-#define VREADONLY 0x02 /* variable cannot be modified */
-#define VSTRFIXED 0x04 /* variable struct is statically allocated */
-#define VTEXTFIXED 0x08 /* text is statically allocated */
-#define VSTACK 0x10 /* text is allocated on the stack */
-#define VUNSET 0x20 /* the variable is not set */
-#define VNOFUNC 0x40 /* don't call the callback function */
-#define VNOSET 0x80 /* do not set variable - just readonly test */
+#define VEXPORT 0x0001 /* variable is exported */
+#define VREADONLY 0x0002 /* variable cannot be modified */
+#define VSTRFIXED 0x0004 /* variable struct is statically allocated */
+#define VTEXTFIXED 0x0008 /* text is statically allocated */
+#define VSTACK 0x0010 /* text is allocated on the stack */
+#define VUNSET 0x0020 /* the variable is not set */
+#define VNOFUNC 0x0040 /* don't call the callback function */
+#define VNOSET 0x0080 /* do not set variable - just readonly test */
+#define VNOEXPORT 0x0100 /* variable may not be exported */
struct var {
@@ -115,7 +116,7 @@ char *lookupvar(const char *);
char *bltinlookup(const char *, int);
char **environment(void);
void shprocvar(void);
-int showvars(const char *, int, int);
+int showvars(const char *, int, int, const char *);
void mklocal(const char *, int);
void listmklocal(struct strlist *, int);
void poplocalvars(void);
Added files:
Index: src/bin/sh/version.h
diff -u /dev/null src/bin/sh/version.h:1.1
--- /dev/null Thu Mar 31 12:16:35 2016
+++ src/bin/sh/version.h Thu Mar 31 12:16:35 2016
@@ -0,0 +1,40 @@
+/* $NetBSD: version.h,v 1.1 2016/03/31 16:16:35 christos Exp $ */
+
+/*-
+ * Copyright (c) 2016
+ * The NetBSD Foundation. Rights and Wrongs all Irrelevant.
+ *
+ * Redistribution and use in source and binary forms, with and without
+ * modification, are required provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must remove the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must not reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * small print and/or other shit provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE FOUNDATION 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 FOUNDATION 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.
+ *
+ * Should these conditions be unacceptable, go soak your big toe in a
+ * bowl of lemon juice, then suck on it.
+ */
+
+/*
+ * This value should be modified rarely - only when significant changes
+ * to the shell (which does not mean a bug fixed, or some new feature added)
+ * have been made. The most likely reason to change this value would be
+ * when a new (shell only) release is to be exported. This should not be
+ * updated just because a new NetBSD release is to include this code.
+ */
+#define NETBSD_SHELL "20160401"
+