diff options
| author | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 20:30:22 -0600 |
|---|---|---|
| committer | Sean Kurtz <seankurtz75@gmail.com> | 2026-11-25 20:30:22 -0600 |
| commit | deb0ed011fe2cfc7058facb3f63b7feaeb8d4a8e (patch) | |
| tree | a05698851d28feb88d9bcf95a3530792b719b16b /src/interact.c | |
| parent | 456955dc33f24b1e9857bdc3c11788a2358e954c (diff) | |
| download | zash-deb0ed011fe2cfc7058facb3f63b7feaeb8d4a8e.tar.gz | |
interactive: a prompt's width counts columns, not bytes
The built-in editor skipped `ESC [ ...` sequences when working out where
the prompt ends. Colour and titles are not all CSI: an OSC title or
hyperlink runs to BEL or `ESC \`, DCS runs the same way, and `tput sgr0`
is the three-byte `ESC ( B` before `ESC [ m`. A prompt built with
`$(tput ...)` was measured wrong, which costs the cursor position and
with it the wrapped-line arithmetic.
`esc_seq_len()` in interact.c now walks what a terminal understands, and
edit_builtin.c uses it. The libedit build shares the walk: it brackets
control runs for `EL_PROMPT_ESC`, and adds the space libedit needs when
a prompt ends in one, since libedit attaches a literal run to the
character after it.
bench/editor.py measures the two costs run.sh cannot see, start to first
prompt and per-key echo, on a pty; bench/README.md has the table. The
release, z/OS and fuzz builds move to -O3 while the sanitizer and
coverage builds stay low: inlined frames make their reports harder to
read than the few percent they would gain.
Diffstat (limited to 'src/interact.c')
| -rw-r--r-- | src/interact.c | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/src/interact.c b/src/interact.c index 4311596..575e057 100644 --- a/src/interact.c +++ b/src/interact.c @@ -8,6 +8,7 @@ * editing options is on and the terminal can do it) and records the line * in the history. */ +#include <ctype.h> #include <errno.h> #include <limits.h> #include <pwd.h> @@ -39,6 +40,65 @@ static void put_protected(strbuf *sb, const char *s) { } /** + * @brief Does this byte end a terminal control sequence? + * + * The final byte of a CSI sequence is a letter or one of the symbols + * below; the parameters and intermediates that come first are digits + * and punctuation. Spelled as characters, not as a byte range, so the + * EBCDIC build reads its own prompts the same way. + * + * @param c Candidate byte. + * @return true if it ends the sequence. + */ +static bool esc_final(char c) { + return isalpha((unsigned char)c) || + (c && strchr("@[\\]^_`{|}~", c) != nullptr); +} + +/** + * @brief Length of the terminal control sequence that starts at s. + * + * A prompt's sequences come from the terminal's terminfo entry, from + * another shell's configuration, or from whatever a search turned up, so + * every form is expected: CSI (`ESC [`, parameters and intermediates to + * a final byte), OSC (`ESC ]`, to BEL or `ESC \` and used for titles and + * hyperlinks), DCS and its relatives (to `ESC \`), and the two- and + * three-byte escapes. The three-byte ones matter: `tput sgr0` is + * `ESC ( B ESC [ m`, and `ESC ( B` resets the character set back to + * ASCII, so a prompt built with `$(tput ...)` contains it. An + * unfinished sequence runs to the end of the text. + * + * @param s Text starting with CH_ESC. + * @return Number of bytes in the sequence, at least 1. + */ +size_t esc_seq_len(const char *s) { + size_t i = 1; + if (!s[i]) + return i; + if (s[i] == '[') { /* CSI */ + for (i++; s[i] && !esc_final(s[i]); i++) + ; + return s[i] ? i + 1 : i; + } + if (s[i] == ']' || s[i] == 'P' || s[i] == 'X' || s[i] == '^' || + s[i] == '_') { /* OSC, DCS, SOS, PM, APC: to ST */ + for (i++; s[i]; i++) { + if (s[i] == '\a') + return i + 1; + if (s[i] == CH_ESC && s[i + 1] == '\\') + return i + 2; + } + return i; + } + if (strchr(" !\"#$%&'()*+,-./", s[i])) { /* ESC ( B, ESC # 8, ... */ + for (i++; s[i] && !esc_final(s[i]); i++) + ; + return s[i] ? i + 1 : i; + } + return i + 1; /* ESC 7, ESC =, ESC > */ +} + +/** * @brief Replace the FreeBSD-style backslash escapes of a prompt string. * * | Escape | Becomes | |
