From 6c84d09c19ece08acc22753083165dad5945e1d9 Mon Sep 17 00:00:00 2001 From: Chet Ramey Date: Wed, 28 Jul 2021 10:27:04 -0400 Subject: [PATCH] make $0 available to non-interactive shell startup files --- CWRU/CWRU.chlog | 18 ++++++++++++++++++ doc/bash.1 | 16 ++++++++++------ doc/bashref.texi | 13 ++++++++----- shell.c | 14 ++++++++++++++ tests/RUN-ONE-TEST | 2 +- trap.c | 2 +- 6 files changed, 52 insertions(+), 13 deletions(-) diff --git a/CWRU/CWRU.chlog b/CWRU/CWRU.chlog index e68a40fb..4765a62a 100644 --- a/CWRU/CWRU.chlog +++ b/CWRU/CWRU.chlog @@ -10777,3 +10777,21 @@ variables.c - assign_in_env: if NAME is not a valid shell identifier, free it after printing the error message and before returning. These are the rest of the fixes from Siteshwar Vashisht + + 7/22 + ---- +shell.c + - main: set dollar_vars[0] to shell_script_filename before calling + run_startup_files() in the non-interactive case. Restore it after + run_startup_files returns so we can get better error messages if + we can't open a script file. Suggested by several people, originally + by Marc Aurèle La France back in 2/2021 (in a + different form) and most recently by Tapani Tarvainen + + + 7/28 + ---- +trap.c + - any_signals_trapped: return that a signal is trapped only if it's + not ignored. This is an additional opportunity for optimization, + reported in https://bugzilla.redhat.com/show_bug.cgi?id=1981926 diff --git a/doc/bash.1 b/doc/bash.1 index df0658da..3f4461c4 100644 --- a/doc/bash.1 +++ b/doc/bash.1 @@ -701,9 +701,11 @@ below under .BR "ARITHMETIC EVALUATION" . If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. -The \fIexpression\fP is expanded as if it were within double quotes, -but double quote characters in \fIexpression\fP are not special and -are removed. +The \fIexpression\fP +undergoes the same expansions +as if it were within double quotes, +but double quote characters in \fIexpression\fP are not treated specially +and are removed. .TP \fB[[\fP \fIexpression\fP \fB]]\fP Return a status of 0 or 1 depending on the evaluation of @@ -716,7 +718,7 @@ and filename expansion. The shell performs tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal on those words -(as if the words were enclosed in double quotes). +(the expansions that would occur if the words were enclosed in double quotes). Conditional operators such as \fB\-f\fP must be unquoted to be recognized as primaries. .if t .sp 0.5 @@ -3519,8 +3521,10 @@ and the substitution of the result. The format for arithmetic expansion is: .PP The .I expression -is treated as if it were within double quotes, but a double quote -inside the parentheses is not treated specially. +undergoes the same expansions +as if it were within double quotes, +but double quote characters in \fIexpression\fP are not treated specially +and are removed. All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal. The result is treated as the arithmetic expression to be evaluated. diff --git a/doc/bashref.texi b/doc/bashref.texi index 5805c588..aa77dda4 100644 --- a/doc/bashref.texi +++ b/doc/bashref.texi @@ -1127,8 +1127,9 @@ done The arithmetic @var{expression} is evaluated according to the rules described below (@pxref{Shell Arithmetic}). -The @var{expression} is expanded as if it were within double quotes, -but double quote characters in @var{expression} are not special and +The @var{expression} undergoes the same expansions +as if it were within double quotes, +but double quote characters in @var{expression} are not treated specially are removed. If the value of the expression is non-zero, the return status is 0; otherwise the return status is 1. @@ -1150,7 +1151,7 @@ and filename expansion. The shell performs tilde expansion, parameter and variable expansion, arithmetic expansion, command substitution, process substitution, and quote removal on those words -(as if the words were enclosed in double quotes). +(the expansions that would occur if the words were enclosed in double quotes). Conditional operators such as @samp{-f} must be unquoted to be recognized as primaries. @@ -2539,8 +2540,10 @@ and the substitution of the result. The format for arithmetic expansion is: $(( @var{expression} )) @end example -The @var{expression} is treated as if it were within double quotes, but -a double quote inside the parentheses is not treated specially. +The @var{expression} undergoes the same expansions +as if it were within double quotes, +but double quote characters in @var{expression} are not treated specially +and are removed. All tokens in the expression undergo parameter and variable expansion, command substitution, and quote removal. The result is treated as the arithmetic expression to be evaluated. diff --git a/shell.c b/shell.c index ce8087f7..82c4985a 100644 --- a/shell.c +++ b/shell.c @@ -694,10 +694,24 @@ main (argc, argv, env) /* The startup files are run with `set -e' temporarily disabled. */ if (locally_skip_execution == 0 && running_setuid == 0) { + char *t; + old_errexit_flag = exit_immediately_on_error; exit_immediately_on_error = 0; + /* Temporarily set $0 while running startup files, then restore it so + we get better error messages when trying to open script files. */ + if (shell_script_filename) + { + t = dollar_vars[0]; + dollar_vars[0] = exec_argv0 ? savestring (exec_argv0) : savestring (shell_script_filename); + } run_startup_files (); + if (shell_script_filename) + { + free (dollar_vars[0]); + dollar_vars[0] = t; + } exit_immediately_on_error += old_errexit_flag; } diff --git a/tests/RUN-ONE-TEST b/tests/RUN-ONE-TEST index 0b063810..c8bef8dd 100755 --- a/tests/RUN-ONE-TEST +++ b/tests/RUN-ONE-TEST @@ -1,4 +1,4 @@ -BUILD_DIR=/usr/local/build/chet/bash/bash-current +BUILD_DIR=/usr/local/build/bash/bash-current THIS_SH=$BUILD_DIR/bash PATH=$PATH:$BUILD_DIR diff --git a/trap.c b/trap.c index dd1c9a56..080ae40f 100644 --- a/trap.c +++ b/trap.c @@ -569,7 +569,7 @@ any_signals_trapped () register int i; for (i = 1; i < NSIG; i++) - if (sigmodes[i] & SIG_TRAPPED) + if ((sigmodes[i] & SIG_TRAPPED) && (sigmodes[i] & SIG_IGNORED) == 0) return i; return -1; }