Module Name: src
Committed By: kre
Date: Mon Feb 4 11:16:41 UTC 2019
Modified Files:
src/bin/sh: error.c error.h eval.c eval.h main.c parser.c sh.1
Log Message:
PR bin/53919
Suppress shell error messages while expanding $ENV (which also causes
errors while expanding $PS1 $PS2 and $PS4 to be suppressed as well).
This allows any random garbage that happens to be in ENV to not
cause noise when the shell starts (which is effectively all it did).
On a parse error (for any of those vars) we also use "" as the result,
which will be a null prompt, and avoid attempting to open any file for ENV.
This does not in any way change what happens for a correctly parsed command
substitution (either when it is executed when permitted for one of the
prompts, or when it is not (which is always for ENV)) and commands run
from those can still produce error output (but shell errors remain suppressed).
To generate a diff of this commit:
cvs rdiff -u -r1.42 -r1.43 src/bin/sh/error.c
cvs rdiff -u -r1.21 -r1.22 src/bin/sh/error.h
cvs rdiff -u -r1.170 -r1.171 src/bin/sh/eval.c
cvs rdiff -u -r1.22 -r1.23 src/bin/sh/eval.h
cvs rdiff -u -r1.80 -r1.81 src/bin/sh/main.c
cvs rdiff -u -r1.164 -r1.165 src/bin/sh/parser.c
cvs rdiff -u -r1.217 -r1.218 src/bin/sh/sh.1
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/error.c
diff -u src/bin/sh/error.c:1.42 src/bin/sh/error.c:1.43
--- src/bin/sh/error.c:1.42 Mon Jan 21 14:29:12 2019
+++ src/bin/sh/error.c Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: error.c,v 1.42 2019/01/21 14:29:12 kre Exp $ */
+/* $NetBSD: error.c,v 1.43 2019/02/04 11:16:41 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)error.c 8.2 (Berkeley) 5/4/95";
#else
-__RCSID("$NetBSD: error.c,v 1.42 2019/01/21 14:29:12 kre Exp $");
+__RCSID("$NetBSD: error.c,v 1.43 2019/02/04 11:16:41 kre Exp $");
#endif
#endif /* not lint */
@@ -69,6 +69,9 @@ struct jmploc *handler;
int exception;
volatile int suppressint;
volatile int intpending;
+volatile int errors_suppressed;
+const char * volatile currentcontext;
+
static void exverror(int, const char *, va_list) __dead;
@@ -133,10 +136,16 @@ exvwarning(int sv_errno, const char *msg
if (output.buf != NULL && output.nextc != output.buf &&
output.nextc[-1] == '\n')
flushout(&output);
+
+ if (errors_suppressed)
+ return;
+
if (commandname)
outfmt(&errout, "%s: ", commandname);
else
outfmt(&errout, "%s: ", getprogname());
+ if (currentcontext != NULL)
+ outfmt(&errout, "%s: ", currentcontext);
if (msg != NULL) {
doformat(&errout, msg, ap);
if (sv_errno >= 0)
Index: src/bin/sh/error.h
diff -u src/bin/sh/error.h:1.21 src/bin/sh/error.h:1.22
--- src/bin/sh/error.h:1.21 Sun Aug 19 23:50:27 2018
+++ src/bin/sh/error.h Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: error.h,v 1.21 2018/08/19 23:50:27 kre Exp $ */
+/* $NetBSD: error.h,v 1.22 2019/02/04 11:16:41 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -61,6 +61,9 @@ struct jmploc {
jmp_buf loc;
};
+extern volatile int errors_suppressed;
+extern const char * volatile currentcontext;
+
extern struct jmploc *handler;
extern int exception;
extern int exerrno; /* error for EXEXEC */
Index: src/bin/sh/eval.c
diff -u src/bin/sh/eval.c:1.170 src/bin/sh/eval.c:1.171
--- src/bin/sh/eval.c:1.170 Mon Jan 21 14:18:59 2019
+++ src/bin/sh/eval.c Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: eval.c,v 1.170 2019/01/21 14:18:59 kre Exp $ */
+/* $NetBSD: eval.c,v 1.171 2019/02/04 11:16:41 kre Exp $ */
/*-
* Copyright (c) 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)eval.c 8.9 (Berkeley) 6/8/95";
#else
-__RCSID("$NetBSD: eval.c,v 1.170 2019/01/21 14:18:59 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.171 2019/02/04 11:16:41 kre Exp $");
#endif
#endif /* not lint */
@@ -1502,6 +1502,23 @@ dotcmd(int argc, char **argv)
}
/*
+ * allow dotfile function nesting to be manipulated
+ * (for read_profile). This allows profile files to
+ * be treated as if they were used as '.' commands,
+ * (approximately) and in particular, for "return" to work.
+ */
+int
+set_dot_funcnest(int new)
+{
+ int rv = dot_funcnest;
+
+ if (new >= 0)
+ dot_funcnest = new;
+
+ return rv;
+}
+
+/*
* Take commands from a file. To be compatible we should do a path
* search for the file, which is necessary to find sub-commands.
*/
Index: src/bin/sh/eval.h
diff -u src/bin/sh/eval.h:1.22 src/bin/sh/eval.h:1.23
--- src/bin/sh/eval.h:1.22 Mon Dec 3 06:43:19 2018
+++ src/bin/sh/eval.h Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: eval.h,v 1.22 2018/12/03 06:43:19 kre Exp $ */
+/* $NetBSD: eval.h,v 1.23 2019/02/04 11:16:41 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -75,6 +75,7 @@ enum skipstate current_skipstate(void);
void save_skipstate(struct skipsave *);
void restore_skipstate(const struct skipsave *);
void stop_skipping(void); /* reset internal skipping state to SKIPNONE */
+int set_dot_funcnest(int);
/*
* Only for use by reset() in init.c!
Index: src/bin/sh/main.c
diff -u src/bin/sh/main.c:1.80 src/bin/sh/main.c:1.81
--- src/bin/sh/main.c:1.80 Sat Jan 19 14:20:22 2019
+++ src/bin/sh/main.c Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: main.c,v 1.80 2019/01/19 14:20:22 kre Exp $ */
+/* $NetBSD: main.c,v 1.81 2019/02/04 11:16:41 kre 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.80 2019/01/19 14:20:22 kre Exp $");
+__RCSID("$NetBSD: main.c,v 1.81 2019/02/04 11:16:41 kre Exp $");
#endif
#endif /* not lint */
@@ -347,7 +347,9 @@ read_profile(const char *name)
if (vflag)
vflag = 0, vflag_set = 1;
}
+ (void)set_dot_funcnest(1); /* allow profile to "return" */
cmdloop(0);
+ (void)set_dot_funcnest(0);
if (qflag) {
if (xflag_set)
xflag = 1;
Index: src/bin/sh/parser.c
diff -u src/bin/sh/parser.c:1.164 src/bin/sh/parser.c:1.165
--- src/bin/sh/parser.c:1.164 Tue Jan 22 14:32:17 2019
+++ src/bin/sh/parser.c Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-/* $NetBSD: parser.c,v 1.164 2019/01/22 14:32:17 kre Exp $ */
+/* $NetBSD: parser.c,v 1.165 2019/02/04 11:16:41 kre Exp $ */
/*-
* Copyright (c) 1991, 1993
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)parser.c 8.7 (Berkeley) 5/16/95";
#else
-__RCSID("$NetBSD: parser.c,v 1.164 2019/01/22 14:32:17 kre Exp $");
+__RCSID("$NetBSD: parser.c,v 1.165 2019/02/04 11:16:41 kre Exp $");
#endif
#endif /* not lint */
@@ -2621,12 +2621,14 @@ expandonstack(char *ps, int cmdsub, int
struct jmploc *const savehandler = handler;
struct parsefile *const savetopfile = getcurrentfile();
const int save_x = xflag;
+ const int save_e_s = errors_suppressed;
struct parse_state new_state = init_parse_state;
struct parse_state *const saveparser = psp.v_current_parser;
const char *result = NULL;
if (!setjmp(jmploc.loc)) {
handler = &jmploc;
+ errors_suppressed = 1;
psp.v_current_parser = &new_state;
setinputstring(ps, 1, lineno);
@@ -2654,18 +2656,19 @@ expandonstack(char *ps, int cmdsub, int
xflag = save_x;
popfilesupto(savetopfile);
handler = savehandler;
+ errors_suppressed = save_e_s;
if (exception == EXEXIT)
longjmp(handler->loc, 1);
if (exception == EXINT)
exraise(SIGINT);
- return ps;
+ return "";
}
psp.v_current_parser = saveparser;
xflag = save_x;
popfilesupto(savetopfile);
handler = savehandler;
-
+ errors_suppressed = save_e_s;
if (result == NULL)
result = ps;
Index: src/bin/sh/sh.1
diff -u src/bin/sh/sh.1:1.217 src/bin/sh/sh.1:1.218
--- src/bin/sh/sh.1:1.217 Mon Jan 21 14:09:24 2019
+++ src/bin/sh/sh.1 Mon Feb 4 11:16:41 2019
@@ -1,4 +1,4 @@
-.\" $NetBSD: sh.1,v 1.217 2019/01/21 14:09:24 kre Exp $
+.\" $NetBSD: sh.1,v 1.218 2019/02/04 11:16:41 kre Exp $
.\" Copyright (c) 1991, 1993
.\" The Regents of the University of California. All rights reserved.
.\"
@@ -31,7 +31,7 @@
.\"
.\" @(#)sh.1 8.6 (Berkeley) 5/4/95
.\"
-.Dd December 12, 2018
+.Dd February 04, 2019
.Dt SH 1
.\" everything except c o and s (keep them ordered)
.ds flags abCEeFfhIiLmnpquVvXx
@@ -129,6 +129,9 @@ a login shell.
This is normally done automatically by the system
when the user first logs in.
A login shell first reads commands
+(as if by using the
+.Dq \&.
+command)
from the files
.Pa /etc/profile
and
@@ -149,12 +152,18 @@ the shell then performs parameter and ar
expansion on the value of
.Ev ENV ,
(these are described later)
-and then reads commands from the file name that results.
-If
+and if no errors occurred,
+then reads commands from the file name that results.
+Note that no error messages result from these
+expansions, to verify that
+.Ev ENV
+is correct, as desired, use:
+.Dl eval printf '%s\e\en' Dq \&${ENV}
+Otherwise if
.Ev ENV
-contains a command substitution, or one of the
-other expansions fails, or if there are no expansions
-to expand, the value of
+appears to contain a command substitution,
+which is not performed here,
+or if there were no expansions to expand, the value of
.Ev ENV
is used as the file name.
.Pp
@@ -4060,6 +4069,11 @@ Names the file sourced at startup by the
Unused by this shell after initialization,
but is usually passed through the environment to
descendant shells.
+See the
+.Sx Invocation
+section above for details of how
+.Ev ENV
+is processed and used.
.It Ev EUSER
Set to the login name of the effective user id running the shell,
as returned by
@@ -4105,7 +4119,6 @@ This is normally set to
.Aq tab ,
and
.Aq newline .
-See the
.Sx White Space Splitting
section for more details.
.It Ev LANG
@@ -4188,8 +4201,13 @@ If
.Ic promptcmds
is not set and the prompt string uses command substitution,
the prompt used will be an appropriate error string.
-For other expansion errors, a message will be output,
-and the unexpanded string will then be used as the prompt.
+For other expansion errors, the prompt will become an
+empty string, without an error message.
+To verify parsing of
+.Ev PS1 ,
+the method suggested for
+.Ev ENV
+can be used.
.It Ev PS2
The secondary prompt string, which defaults to
.Dq Li "> " .
@@ -4198,9 +4216,9 @@ After expansion (as for
it is written whenever more input is required to complete the
current command.
.It Ev PS4
-Output, after expansion like
+is Output, after expansion like
.Ev PS1 ,
-before each line when execution trace
+as a prefix for each line when execution trace
.Ic ( set Fl x )
is enabled.
.Ev PS4