Skip to content

Commit

Permalink
Fix syntax checking for "if true; then echo hello | fi", fix some tests,
Browse files Browse the repository at this point in the history
slightly more elaborate debug output.
  • Loading branch information
landley committed May 26, 2020
1 parent bc6ce66 commit 6b6436c
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion scripts/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ source scripts/runtest.sh
source scripts/portability.sh

TOPDIR="$PWD"
FILES="$PWD"/tests/files
export FILES="$PWD"/tests/files

trap 'kill $(jobs -p) 2>/dev/null; exit 1' INT

Expand Down
4 changes: 3 additions & 1 deletion tests/sh.test
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ testing "exec3" '$C -c "{ exec readlink /proc/self/fd/0;} < /proc/self/exe"' \
"$(readlink -f $C)\n" "" ""
testing 'arg shift' "$SH -c '"'for i in "" 2 1 1 1; do echo $? $1; shift $i; done'"' one two three four five" \
"0 two\n0 three\n0 five\n0\n1\n" "" ""
testing '(subshell)' '$C -c "(echo hello)"' 'hello\n' '' ''
testing '(subshell)' '$SH -c "(echo hello)"' 'hello\n' '' ''
testing 'syntax' '$SH -c "if true; then echo hello | fi" 2>/dev/null || echo x'\
'x\n' '' ''

# The bash man page is lying when it says $_ starts with an absolute path.
ln -s $(which $SH) bash
Expand Down
10 changes: 4 additions & 6 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,7 @@ if (BUGBUG>1) dprintf(255, "{%d:%s}\n", pl->type, ex ? ex : (sp->expect ? "*" :
// Parse next word and detect overflow (too many nested quotes).
if ((end = parse_word(start, 0)) == (void *)1) goto flush;

if (BUGBUG>1) dprintf(255, "[%.*s] ", end ? (int)(end-start) : 0, start);
if (BUGBUG>1) dprintf(255, "[%.*s:%s] ", end ? (int)(end-start) : 0, start, ex ? : "");
// Is this a new pipeline segment?
if (!pl) {
pl = xzalloc(sizeof(struct sh_pipeline));
Expand Down Expand Up @@ -1891,11 +1891,9 @@ if (BUGBUG>1) dprintf(255, "[%.*s] ", end ? (int)(end-start) : 0, start);

// If we got here we expect a specific word to end this block: is this it?
else if (!strcmp(s, ex)) {

// can't "if | then" or "while && do", only ; & or newline works
if (last && (strcmp(ex, "then") || strcmp(last, "&"))) {
s = end;
goto flush;
}
if (last && strcmp(last, "&")) goto flush;

free(dlist_lpop(&sp->expect));
pl->type = anystr(s, tails) ? 3 : 2;
Expand Down Expand Up @@ -2617,7 +2615,7 @@ if (BUGBUG) dprintf(255, "line=%s\n", new);

// returns 0 if line consumed, command if it needs more data
prompt = parse_line(new, &scratch);
if (BUGBUG) dump_state(&scratch);
if (BUGBUG) dprintf(255, "prompt=%d\n", prompt), dump_state(&scratch);
if (prompt != 1) {
// TODO: ./blah.sh one two three: put one two three in scratch.arg
if (!prompt) run_function(scratch.pipeline);
Expand Down

0 comments on commit 6b6436c

Please sign in to comment.