From 6bc1984355334968ed8930b2efcc9e05d2d58396 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 1 Oct 2025 15:58:40 -0500 Subject: [PATCH] Cleanup prompt logic. --- toys/pending/sh.c | 101 ++++++++++++++++------------------------------ 1 file changed, 35 insertions(+), 66 deletions(-) diff --git a/toys/pending/sh.c b/toys/pending/sh.c index 7563294c..137edd7c 100644 --- a/toys/pending/sh.c +++ b/toys/pending/sh.c @@ -3758,9 +3758,8 @@ static int wait_pipeline(struct sh_process *pp) // Truncated to 4k at the moment, waiting for somebody to complain. static void do_prompt(char *prompt) { - char *s, *ss, c, cc, *pp = toybuf; + char *s, *ss, *sss, c, cc, *pp = toybuf; int len, ll; - time_t t; if (!prompt) return; while ((len = sizeof(toybuf)-(pp-toybuf))>0 && *prompt) { @@ -3783,76 +3782,46 @@ static void do_prompt(char *prompt) // Ignore bash's "nonprintable" hack; query our cursor position instead. if (cc=='[' || cc==']') continue; else if (cc=='$') *pp++ = getuid() ? '$' : '#'; - else if (cc=='d'||cc=='D'||cc=='t'||cc=='T'||cc=='@'||cc=='A') { - t = time(NULL); - char *fmt = "%a %b %d"; - if (cc=='D') { - char *end; - if (*prompt!='{') { - *pp++ = '\\'; - *pp++ = 'D'; - continue; - } - prompt++; - if (!(end = strchr(prompt, '}'))) { - *pp++ = '\\'; - *pp++ = 'D'; - continue; - } - if (prompt==end) { - const char ascfmt[] = "%X"; - fmt = strncpy(xmalloc(sizeof(ascfmt)), ascfmt, sizeof(ascfmt)); - prompt++; - } else { - fmt = strncpy(xmalloc(end-prompt), prompt, end-prompt); - fmt[end-prompt] = '\0'; + 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; } - } else if (cc=='t') { - const char tfmt[] = "%H:%M:%S"; - fmt = strncpy(xmalloc(sizeof(tfmt)), tfmt, sizeof(tfmt)); - } else if (cc=='T') { - const char Tfmt[] = "%I:%M:%S"; - fmt = strncpy(xmalloc(sizeof(Tfmt)), Tfmt, sizeof(Tfmt)); - } else if (cc=='@') { - const char atfmt[] = "%I:%M %p"; - fmt = strncpy(xmalloc(sizeof(atfmt)), atfmt, sizeof(atfmt)); - } else if (cc=='A') { - const char Afmt[] = "%R"; - fmt = strncpy(xmalloc(sizeof(Afmt)), Afmt, sizeof(Afmt)); } - strftime(pp, len, fmt, localtime(&t)); - pp += strlen(pp); - if (cc!='d') free(fmt); + pp += strftime(pp, len, fmt, localtime(&tt)); + if (cc=='D') free(fmt); } else if (cc=='h' || cc=='H') { - *pp = 0; - gethostname(pp, len); - pp[len-1] = 0; - if (cc=='h' && (s = strchr(pp, '.'))) *s = 0; - pp += strlen(pp); - } else if (cc=='s') { - s = getbasename(*toys.argv); - while (*s && len--) *pp++ = *s++; - } else if (cc=='u') { - struct passwd *pw; - if ((pw = xgetpwuid(getuid()))) { - ll = strlen(pw->pw_name); - pp = stpncpy(pp, pw->pw_name, ll>len ? len : ll); - } - } else if (cc=='v'||cc=='V') { - ll = strlen(TOYBOX_VERSION); - pp = stpncpy(pp, TOYBOX_VERSION, ll>len ? len : ll); - } else if (cc=='w'||cc=='W') { - if ((s = getvar("PWD"))) { + if ((len = gethostname(pp, len)) && cc=='h' && (s = strchr(pp, '.'))) + len = s-pp; + } else if (cc=='s') + for (s = getbasename(TT.argv0); *s && len--; *pp++ = *s++); + else if (cc=='u') { + struct passwd *pw = bufgetpwuid(ll = getuid()); + char buf[16]; + + sprintf(buf, "%d", ll); + s = pw ? pw->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)) { - *pp++ = '~'; - if (--len && *s!='/') *pp++ = '/'; - len--; - } - if (len>0) { - ll = strlen(s); - pp = stpncpy(pp, s, ll>len ? len : ll); + if (*s && *s!='/') s = sss; + else if (cc!='W' || !*s) { + *pp++ = '~'; + if (--len && *s && *s!='/') *pp++ = '/', len--; + } } + if (len>0) pp += sprintf(pp, "%.*s", len-1, s); } } else if (!(c = unescape(cc))) { *pp++ = '\\'; -- 2.39.5