better parser error messages; add -p option to source builtin

This commit is contained in:
Chet Ramey
2024-06-21 10:38:39 -04:00
parent dbb48b9786
commit 886e4e68be
17 changed files with 2057 additions and 1912 deletions
+20 -7
View File
@@ -139,6 +139,19 @@ abc
def
ghi
ok
./source8.sub: line 36: improbable-filename: No such file or directory
./source8.sub: line 37: improbable-filename: No such file or directory
an improbable filename
an improbable filename
an improbable filename
an improbable filename
file in the current directory
./source8.sub: line 51: .: cwd-filename: file not found
file in the current directory
bash: line 1: .: cwd-filename: file not found
bash: line 1: .: cwd-filename: file not found
file in the current directory
file in the current directory
AVAR
foo
foo
@@ -149,11 +162,11 @@ AVAR
foo
declare -x foo=""
declare -x FOO="\$\$"
./builtins.tests: line 239: declare: FOO: not found
./builtins.tests: line 242: declare: FOO: not found
declare -x FOO="\$\$"
ok
ok
./builtins.tests: line 271: kill: 4096: invalid signal specification
./builtins.tests: line 274: kill: 4096: invalid signal specification
1
a\n\n\nb
a
@@ -332,7 +345,7 @@ A star (*) next to a name means that the command is disabled.
! PIPELINE history [-c] [-d offset] [n] or hist>
job_spec [&] if COMMANDS; then COMMANDS; [ elif C>
(( expression )) jobs [-lnprs] [jobspec ...] or jobs >
. filename [arguments] kill [-s sigspec | -n signum | -sigs>
. [-p path] filename [arguments] kill [-s sigspec | -n signum | -sigs>
: let arg [arg ...]
[ arg... ] local [option] name[=value] ...
[[ expression ]] logout [n]
@@ -349,7 +362,7 @@ A star (*) next to a name means that the command is disabled.
complete [-abcdefgjksuv] [-pr] [-DEI]> set [-abefhkmnptuvxBCEHPT] [-o optio>
compopt [-o|+o option] [-DEI] [name .> shift [n]
continue [n] shopt [-pqsu] [-o] [optname ...]
coproc [NAME] command [redirections] source filename [arguments]
coproc [NAME] command [redirections] source [-p path] filename [argument>
declare [-aAfFgiIlnrtux] [name[=value> suspend [-f]
dirs [-clpv] [+N] [-N] test [expr]
disown [-h] [-ar] [jobspec ... | pid > time [-p] pipeline
@@ -425,7 +438,7 @@ A star (*) next to a name means that the command is disabled.
! PIPELINE history [-c] [-d offset] [n] or hist>
job_spec [&] if COMMANDS; then COMMANDS; [ elif C>
(( expression )) jobs [-lnprs] [jobspec ...] or jobs >
. filename [arguments] kill [-s sigspec | -n signum | -sigs>
. [-p path] filename [arguments] kill [-s sigspec | -n signum | -sigs>
: let arg [arg ...]
[ arg... ] local [option] name[=value] ...
[[ expression ]] logout [n]
@@ -442,7 +455,7 @@ A star (*) next to a name means that the command is disabled.
complete [-abcdefgjksuv] [-pr] [-DEI]> set [-abefhkmnptuvxBCEHPT] [-o optio>
compopt [-o|+o option] [-DEI] [name .> shift [n]
continue [n] shopt [-pqsu] [-o] [optname ...]
coproc [NAME] command [redirections] source filename [arguments]
coproc [NAME] command [redirections] source [-p path] filename [argument>
declare [-aAfFgiIlnrtux] [name[=value> suspend [-f]
dirs [-clpv] [+N] [-N] test [expr]
disown [-h] [-ar] [jobspec ... | pid > time [-p] pipeline
@@ -489,5 +502,5 @@ popd: usage: popd [-n] [+N | -N]
./builtins12.sub: line 36: popd: +8: directory stack index out of range
/tmp /
/
./builtins.tests: line 322: exit: status: numeric argument required
./builtins.tests: line 325: exit: status: numeric argument required
after non-numeric arg to exit: 2
+3
View File
@@ -208,6 +208,9 @@ ${THIS_SH} ./source6.sub
# test bugs with source called from multiline aliases and other contexts
${THIS_SH} ./source7.sub
# test source/. -p path
${THIS_SH} ./source8.sub
# in posix mode, assignment statements preceding special builtins are
# reflected in the shell environment. `.' and `eval' need special-case
# code.
+2 -2
View File
@@ -157,7 +157,7 @@ bash: -c: line 1: unexpected token `EOF', expected `)'
bash: -c: line 2: syntax error: unexpected end of file
bash: -c: line 1: unexpected EOF while looking for `]]'
bash: -c: line 2: syntax error: unexpected end of file
bash: -c: line 1: syntax error in conditional expression
bash: -c: line 1: syntax error in conditional expression: unexpected token `]'
bash: -c: line 1: syntax error near `]'
bash: -c: line 1: `[[ ( -t X ) ]'
bash: -c: line 1: unexpected argument `&' to conditional unary operator
@@ -178,7 +178,7 @@ bash: -c: line 1: `[[ 4 > & ]]'
bash: -c: line 1: unexpected token `&' in conditional command
bash: -c: line 1: syntax error near `&'
bash: -c: line 1: `[[ & ]]'
bash: -c: line 1: conditional binary operator expected
bash: -c: line 1: unexpected token `7', conditional binary operator expected
bash: -c: line 1: syntax error near `7'
bash: -c: line 1: `[[ -Q 7 ]]'
bash: -c: line 1: unexpected argument `<' to conditional unary operator
+5 -5
View File
@@ -81,11 +81,11 @@ bash: line 1: PWD: readonly variable
bash: line 1: OLDPWD: readonly variable
1
./errors.tests: line 236: .: filename argument required
.: usage: . filename [arguments]
.: usage: . [-p path] filename [arguments]
./errors.tests: line 237: source: filename argument required
source: usage: source filename [arguments]
source: usage: source [-p path] filename [arguments]
./errors.tests: line 240: .: -i: invalid option
.: usage: . filename [arguments]
.: usage: . [-p path] filename [arguments]
./errors.tests: line 243: set: -q: invalid option
set: usage: set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [-] [arg ...]
./errors.tests: line 246: enable: sh: not a shell builtin
@@ -141,7 +141,7 @@ set: usage: set [-abefhkmnptuvxBCEHPT] [-o option-name] [--] [-] [arg ...]
./errors.tests: line 348: xx: readonly variable
1
./errors1.sub: line 14: .: -i: invalid option
.: usage: . filename [arguments]
.: usage: . [-p path] filename [arguments]
./errors1.sub: line 22: shift: -4: shift count out of range
./errors1.sub: line 23: shift: -4: shift count out of range
./errors1.sub: line 27: break: -1: loop count out of range
@@ -255,7 +255,7 @@ ok 6
./errors8.sub: line 16: /notthere: No such file or directory
ok 7
./errors8.sub: line 17: .: -x: invalid option
.: usage: . filename [arguments]
.: usage: . [-p path] filename [arguments]
ok 8
DEBUG
./errors9.sub: line 6: [[: ++: arithmetic syntax error: operand expected (error token is "+")
+62
View File
@@ -0,0 +1,62 @@
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# test various uses of source -p
: ${THIS_SH:=./bash}
PATH=/bin:/usr/bin:/sbin:/usr/sbin
: ${TMPDIR:=/var/tmp}
export SDIR=${TMPDIR}/source-$$
mkdir -p $SDIR || { echo "$SDIR: cannot create" >&2; exit 1; }
FN=${SDIR}/improbable-filename
cat >$FN << EOF
echo an improbable filename
EOF
cat >cwd-filename <<EOF
echo file in the current directory
EOF
trap 'rm -rf ${SDIR} ; rm -f cwd-filename' 0
# not found in $PATH
. improbable-filename
source improbable-filename
# searches path supplied with -p
. -p $SDIR:. improbable-filename
source -p $SDIR:. improbable-filename
# -p works even without sourcepath
shopt -u sourcepath
. -p $SDIR:. improbable-filename
source -p $SDIR:. improbable-filename
shopt -s sourcepath
# does not search the current directory with -p
. cwd-filename
. -p $SDIR cwd-filename
# but does if . is in the path argument
source -p $SDIR:. cwd-filename
# but does not in posix mode, where special builtins failing exits the shell
${THIS_SH} -c 'set -o posix ; . cwd-filename' bash
${THIS_SH} -c 'set -o posix ; . -p $SDIR cwd-filename' bash
# a null PATH argument is the same as "."
. -p '' cwd-filename
source -p '' cwd-filename