From 81691df43ebcd879ca0a6aea54cc1e7773ba5e94 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 2 Oct 2026 02:31:37 -0500 Subject: [PATCH] More prompt work/tests. --- tests/sh.test | 68 +++++++++++++++---------- toys/pending/sh.c | 126 +++++++++++++++++++++++----------------------- 2 files changed, 104 insertions(+), 90 deletions(-) diff --git a/tests/sh.test b/tests/sh.test index 4ee149ef..608b2cab 100644 --- a/tests/sh.test +++ b/tests/sh.test @@ -80,13 +80,13 @@ testing 'default exports' "env -i $C --noprofile --norc -c \$'env\n\n' | sort" \ "PWD=$(pwd)\nSHLVL=1\n_=$(which env)\n" "" "" # root and container tests -skipnot [ $UID -eq 0 ] +[ $UID -eq 0 -a -n "$(type -t unshare)" ] || SKIP=999 shxpect I$'echo $LOGNAME\n' O$'root\n' E'# ' # bash caches hostname at startup, so providing known hostname awkward -skipnot [ $UID -eq 0 -a -n "$(type -t unshare)" ] toyonly testing '${x@P} hostname' \ - $'unshare -u $C -c \'hostname drift.wood; abc="\h \H \h"; echo ${abc@P}\'' \ + $'unshare -u "$C" -c \'hostname drift.wood; abc="\h \H \h"; echo ${abc@P}\'' \ 'drift drift.wood drift\n' '' '' +SKIP=0 # TODO toysh order of operations not matching bash $BROKEN testing "leading assignment fail" \ @@ -293,38 +293,52 @@ tst '${x,y}' 'x=BBB; echo ${x,B}' 'bBB\n' tst '${x,,y}' 'x=POTATO; echo ${x,,} ${x,,?} ${x,,*} ${x,,T}' \ 'potato potato potato POtAtO\n' +# TODO: dDtT@A vV w !# [] +# Some of these tests are up top because root or container access needed +tst '${x@P} \j jobs' 'x="-\\j-";echo ${x@P};sleep .5 & echo ${x@P}' '-0-\n-1-\n' +tst '${x@P} \x\a\e\n\r\\' 'x="\\x\\a\\e\\n\\r\\\\"; echo -n "${x@P}"' \ + '\\x\a\e\n\r\\' +tst '${x@P} \l' 'x=\\l; echo "${x@P}" < /dev/tty' 'tty\n' +tst '${x@P} \$' 'x="\$"; echo "${x@P} "' "$P\n" +tst '${x@P} \s' 'x=\\s; echo "${x@P}"' "$(basename $C)\n" +tst '${x@P} \u' 'x=\\u; echo "${x@P}"' "$(whoami)\n" +tst '${x@P} \W' 'x=\\W; echo "${x@P}"' "$(basename $PWD)\n" +tst '${x@P} \w' 'HOME=/one PWD=/one/two x=\\w; echo "${x@P}"' '~/two\n' +tst '${x@P} \W is strrchr not basename' 'PWD=/one/two/ x=\\W;echo "${x@P}"' '\n' + # UuLQEPAKak *@ # // Of COURSE unicode case switch can change utf8 encoding length # // Lower case U+0069 becomes U+0130 in turkish. # // Greek U+0390 becomes 3 characters TODO test this -testing '${x@U} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x@U} expands"' \ - 'ȿɀɐɑɒ ⱾⱿⱯⱭⱰ expands\n' '' '' -testing '${x@u} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x@u} expands"' \ - 'ȿɀɐɑɒ Ȿɀɐɑɒ expands\n' '' '' -testing '${x@L} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x@L} expanded without OOB"' \ - 'ȺȾ ⱥⱦ expanded without OOB\n' '' '' -testing '${x^^} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x^^} expands"' \ - 'ȿɀɐɑɒ ⱾⱿⱯⱭⱰ expands\n' '' '' -testing '${x^} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x^} expands"' \ - 'ȿɀɐɑɒ Ȿɀɐɑɒ expands\n' '' '' -testing '${x,,} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x,,} expanded without OOB"' \ - 'ȺȾ ⱥⱦ expanded without OOB\n' '' '' -testing '${x,} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x,} expanded without OOB"' \ - 'ȺȾ ⱥȾ expanded without OOB\n' '' '' +# TODO test contraction and staying same size as well. See: +# https://gist.github.com/rendello/d37552507a389656e248f3255a618127 + +tst '${x@U} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x@U} expands"' \ + 'ȿɀɐɑɒ ⱾⱿⱯⱭⱰ expands\n' +tst '${x@u} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x@u} expands"' \ + 'ȿɀɐɑɒ Ȿɀɐɑɒ expands\n' +tst '${x@L} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x@L} expanded without OOB"' \ + 'ȺȾ ⱥⱦ expanded without OOB\n' +tst '${x^^} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x^^} expands"' \ + 'ȿɀɐɑɒ ⱾⱿⱯⱭⱰ expands\n' +tst '${x^} uppercase' 'LC_ALL=C.UTF-8 x=ȿɀɐɑɒ; echo "$x ${x^} expands"' \ + 'ȿɀɐɑɒ Ȿɀɐɑɒ expands\n' +tst '${x,,} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x,,} expanded without OOB"' \ + 'ȺȾ ⱥⱦ expanded without OOB\n' +tst '${x,} lowercase' 'LC_ALL=C.UTF-8 x=ȺȾ; echo "$x ${x,} expanded without OOB"' \ + 'ȺȾ ⱥȾ expanded without OOB\n' mkdir -p abc/def/ghi touch www -testing 'wildcards' 'echo w[v-x]w w[x-v]w abc/*/ghi' \ - 'www w[x-v]w abc/def/ghi\n' '' '' -testing 'hidden wildcards' \ - 'touch .abc abc && echo *bc && echo and && echo .*bc' \ - 'abc\nand\n.abc\n' '' '' - -testing "backtick1" 'x=fred; echo `echo $x`' 'fred\n' "" "" -testing "backtick2" 'x=fred; echo `x=y; echo $x`; echo $x' 'y\nfred\n' "" "" -testing '$(( ) )' 'echo ab$((echo hello) | tr e x)cd' "abhxllocd\n" "" "" -$BROKEN testing '$((x=y)) lifetime' 'a=boing; echo $a $a$((a=4))$a $a' 'boing boing44 4\n' '' '' +tst 'wildcards' 'echo w[v-x]w w[x-v]w abc/*/ghi' 'www w[x-v]w abc/def/ghi\n' +tst 'hidden wildcards' 'touch .abc abc && echo *bc && echo and && echo .*bc' \ + 'abc\nand\n.abc\n' + +tst "backtick1" 'x=fred; echo `echo $x`' 'fred\n' +tst "backtick2" 'x=fred; echo `x=y; echo $x`; echo $x' 'y\nfred\n' +tst '$(( ) )' 'echo ab$((echo hello) | tr e x)cd' "abhxllocd\n" +$BROKEN tst '$((x=y)) lifetime' 'a=boing; echo $a $a$((a=4))$a $a' 'boing boing44 4\n' # ((math)) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index f2be296b..bace0eda 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -2013,76 +2013,76 @@ static char *slashcopy(char *s, char *c, struct sh_arg *deck) // parse $PS1 style escapes into a buffer static int get_prompt(char *buf, int blen, char *prompt) { - char *s, *ss, *sss, c, cc, *pp = buf; + char *s, *ss, *sss, cc, *pp = buf; int len, ll; if (!prompt) return 0; while ((len = blen-(pp-buf))>0 && *prompt) { - c = *(prompt++); + if ('\\' != (cc = *(prompt++)) || !*prompt) { + *pp++ = cc; - if (c=='!') { - if (*prompt=='!') prompt++; - else { - pp += snprintf(pp, len, "%ld", TT.ff->lineno); - continue; - } - } else if (c=='\\') { - cc = *(prompt++); - if (!cc) { - *pp++ = c; - break; - } + continue; + } - // \nnn \dD{}hHjlstT@AuvVwW!#$ - // Ignore bash's "nonprintable" hack; query our cursor position instead. - if (cc=='[' || cc==']') continue; - else if (cc=='$') *pp++ = getuid() ? '$' : '#'; - else if (strchr("DdtT@A", cc)) { - char *end, *fmt = (char *[]){0, "%a %b %d", "%H:%M:%S", "%I:%M:%S", - "%I:%M %p", "%R"}[stridx("dtT@A", cc)]; - time_t tt = time(0); - - if (!fmt) { - // todo: slashcopy? Would allow escaped \} but can't handle missing } - if (*prompt!='{' || !(end = strchr(prompt+1, '}'))) *pp++ = cc; - else { - if (end==prompt+1) fmt = "%X"; - else fmt = xstrndup(prompt, end-prompt); - prompt = end+1; - } + // \nnn \dD{}hHjlstT@AuvVwW!#$ + if ((cc = *prompt++)=='!') pp += snprintf(pp, len, "%ld",TT.ff->lineno); + // Ignore bash's "nonprintable" hack; query our cursor position instead. + else if (cc=='[' || cc==']') continue; + else if (cc=='$') *pp++ = getuid() ? '$' : '#'; + else if (strchr("DdtT@A", cc)) { + char *end, *fmt = (char *[]){0, "%a %b %d", "%H:%M:%S", "%I:%M:%S", + "%I:%M %p", "%R"}[stridx("dtT@A", cc)]; + time_t tt = time(0); + + if (!fmt) { + // todo: slashcopy? Would allow escaped \} but can't handle missing } + if (*prompt!='{' || !(end = strchr(prompt+1, '}'))) *pp++ = cc; + else { + if (end==prompt+1) fmt = "%X"; + else fmt = xstrndup(prompt, end-prompt); + prompt = end+1; } - pp += strftime(pp, len, fmt, localtime(&tt)); - if (cc=='D') free(fmt); - } else if (cc=='h' || cc=='H') { - if (!gethostname(s = pp, len)) - for (; (pp-s)pw_name : buf; - if (pw) pp += sprintf(pp, "%.*s", len-1, s); - } else if (cc=='v'||cc=='V') - pp += sprintf(pp, "%.*s", len-1, TOYBOX_VERSION); - else if (cc=='w'||cc=='W') { - if ((s = sss = getvar("PWD"))) { - if ((ss = getvar("HOME")) && strstart(&s, ss)) { - if (*s && *s!='/') s = sss; - else if (cc!='W' || !*s) { - *pp++ = '~'; - if (--len && *s && *s!='/') *pp++ = '/', len--; - } + } + pp += strftime(pp, len, fmt, localtime(&tt)); + if (cc=='D') free(fmt); + } else if (cc=='h' || cc=='H') { + if (!gethostname(s = pp, len)) + for (; (pp-s)pw_name : buf; + if (pw) pp += sprintf(pp, "%.*s", len-1, s); + } else if (cc=='v'||cc=='V') + pp += sprintf(pp, "%.*s", len-1, TOYBOX_VERSION); + else if (cc=='w'||cc=='W') { + if ((s = sss = getvar("PWD"))) { + if (cc=='W') { + if ((ss = strrchr(s, '/'))) s = ss+1; + } else if ((ss = getvar("HOME")) && strstart(&sss, ss)) { + if (!*sss || *sss=='/') { + *pp++ = '~'; + s = sss; } - if (len>0) pp += sprintf(pp, "%.*s", len-1, s); } - } else if (!(c = unescape(cc))) { - *pp++ = '\\'; - if (--len) *pp++ = c; - } else *pp++ = c; - } else *pp++ = c; + if (len>0) pp += sprintf(pp, "%.*s", len-1, s); + } + } else if ((cc = unescape(cc))) *pp++ = cc; + else { + *pp++ = '\\'; + if (--len) *pp++ = prompt[-1]; + } } return pp-buf; @@ -2094,7 +2094,7 @@ static void do_prompt(char *buf) { int len = get_prompt(toybuf, sizeof(toybuf), buf); - writeall(2, toybuf, minof(len,sizeof(toybuf))); + writeall(2, toybuf, minof(len, sizeof(toybuf))); } #define NO_QUOTE (1<<0) // quote removal @@ -2512,7 +2512,7 @@ barf: ss[yy] = 0; push_arg(delete, ifs = ss); } else if (cc && strchr("UuL", cc)) for (ss = ifs; *ss; ss += dd) { - // TODO can this be merged with ^, logic above? No pattern match... + // TODO merge with ^, logic above (this has no pattern match) dd = getutf8(ss, 4, &jj); if (jj != (ll = (cc=='L' ? towlower : towupper)(jj))) { yy = ss-ifs; -- 2.39.5