Skip to content

Commit

Permalink
Fix line continuation debris, add test, minor in-passing cleanups.
Browse files Browse the repository at this point in the history
  • Loading branch information
landley committed Apr 29, 2023
1 parent 88ea9c9 commit 60afef1
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion tests/sh.test
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export EVAL="timeout 10 $SH -c"

testing 'trailing $ is literal' 'echo $' '$\n' '' ''
# TODO testing 'empty +() is literal' 'echo +()' '+()\n' '' ''

shxpect 'queued work after HERE' I$'<<0;echo hello\n' E"> " I$'0\n' O$'hello\n'
shxpect '$_ preserved on assignment error' I$'true hello; a=1 b=2 c=${}\n' \
E E"$P" I$'echo $_\n' O$'hello\n'
Expand Down Expand Up @@ -151,7 +152,6 @@ testing 'cd in renamed dir' \
rm -rf one

testing "smoketest" "echo hello" "hello\n" "" ""
testing "line break" $'ec\\\nho hello' 'hello\n' '' ''
testing "assignment" 'x=y; echo $x' 'y\n' '' ''
testing "+= assignment" 'x+=abc; y=def; y+=$x; echo $y' 'defabc\n' '' ''
testing "eval" "eval echo hello" "hello\n" "" ""
Expand Down Expand Up @@ -541,6 +541,8 @@ shxpect '${/newline/}' I$'x=$\'\na\';echo ${x/\n' E'> ' I$'/b}\n' O$'ba\n' E'> '
shxpect 'line continuation' I$'echo "hello" \\\n' E'> ' I$'> blah\n' E"$P" \
I$'wc blah\n' O$'1 1 6 blah\n'
shxpect 'line continuation2' I$'echo ABC\\\n' E'> ' I$'DEF\n' O$'ABCDEF\n'
testing "line continuation3" $'ec\\\nho hello' 'hello\n' '' ''
testing "line continuation4" $'if true | \\\n(true);then echo true;fi' 'true\n' '' ''

# Race condition (in bash, but not in toysh) can say 43.
testing 'SECONDS' 'readonly SECONDS=41; sleep 1; echo $SECONDS' '42\n' '' ''
Expand Down
8 changes: 4 additions & 4 deletions toys/pending/sh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1121,7 +1121,7 @@ static char *parse_word(char *start, int early, int quote)

// \? $() ${} $[] ?() *() +() @() !()
else {
if (ii=='\\') { // TODO why end[1] here? sh -c $'abc\\\ndef' Add test.
if (ii=='\\') {
if (!*end || (*end=='\n' && !end[1])) return early ? end : 0;
} else if (ii=='$' && -1!=(qq = stridx("({[", *end))) {
if (strstart(&end, "((")) {
Expand Down Expand Up @@ -2138,12 +2138,11 @@ static int expand_arg_nobrace(struct sh_arg *arg, char *str, unsigned flags,
} else if (*slice=='/') {
struct sh_arg wild = {0};

s = slashcopy(ss = slice+(xx = !!strchr("/#%", slice[1]))+1, "/}",
&wild);
xx = !!strchr("/#%", slice[1]);
s = slashcopy(ss = slice+xx+1, "/}", &wild);
ss += (long)wild.v[wild.c];
ss = (*ss == '/') ? slashcopy(ss+1, "}", 0) : 0;
jj = ss ? strlen(ss) : 0;
ll = 0;
for (ll = 0; ifs[ll];) {
// TODO nocasematch option
if (0<(dd = wildcard_match(ifs+ll, s, &wild, 0))) {
Expand Down Expand Up @@ -3042,6 +3041,7 @@ static int parse_line(char *line, struct sh_pipeline **ppl,
// Do we need to request another line to finish word (find ending quote)?
if (!end) {
// Save unparsed bit of this line, we'll need to re-parse it.
if (*start=='\\' && (!start[1] || start[1]=='\n')) start++;
arg_add(arg, xstrndup(start, strlen(start)));
arg->c = -arg->c;
free(delete);
Expand Down

0 comments on commit 60afef1

Please sign in to comment.