Source-Changes-HG archive

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

[src/trunk]: src/bin/sh PR bin/53919



details:   https://anonhg.NetBSD.org/src/rev/a8e5df02574d
branches:  trunk
changeset: 448521:a8e5df02574d
user:      kre <kre%NetBSD.org@localhost>
date:      Mon Feb 04 11:16:41 2019 +0000

description:
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).

diffstat:

 bin/sh/error.c  |  13 +++++++++++--
 bin/sh/error.h  |   5 ++++-
 bin/sh/eval.c   |  21 +++++++++++++++++++--
 bin/sh/eval.h   |   3 ++-
 bin/sh/main.c   |   6 ++++--
 bin/sh/parser.c |  11 +++++++----
 bin/sh/sh.1     |  42 ++++++++++++++++++++++++++++++------------
 7 files changed, 77 insertions(+), 24 deletions(-)

diffs (truncated from 306 to 300 lines):

diff -r 7224125e9fcd -r a8e5df02574d bin/sh/error.c
--- a/bin/sh/error.c    Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/error.c    Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
 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 @@
        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)
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/error.h
--- a/bin/sh/error.h    Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/error.h    Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
        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 */
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/eval.c
--- a/bin/sh/eval.c     Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/eval.c     Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
 }
 
 /*
+ * 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.
  */
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/eval.h
--- a/bin/sh/eval.h     Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/eval.h     Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
 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!
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/main.c
--- a/bin/sh/main.c     Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/main.c     Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
 #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 @@
            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;
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/parser.c
--- a/bin/sh/parser.c   Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/parser.c   Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
        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 @@
                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;
diff -r 7224125e9fcd -r a8e5df02574d bin/sh/sh.1
--- a/bin/sh/sh.1       Mon Feb 04 10:48:46 2019 +0000
+++ b/bin/sh/sh.1       Mon Feb 04 11:16:41 2019 +0000
@@ -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 @@
 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 @@
 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
-contains a command substitution, or one of the
-other expansions fails, or if there are no expansions
-to expand, the value of
+is correct, as desired, use:
+.Dl eval printf '%s\e\en' Dq \&${ENV}
+Otherwise if
+.Ev ENV
+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 @@
 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 @@
 .Aq tab ,
 and
 .Aq newline .
-See the
 .Sx White Space Splitting
 section for more details.
 .It Ev LANG
@@ -4188,8 +4201,13 @@
 .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 @@
 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



Home | Main Index | Thread Index | Old Index