summaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorSean Kurtz <seankurtz75@gmail.com>2026-11-25 20:30:22 -0600
committerSean Kurtz <seankurtz75@gmail.com>2026-11-25 20:30:22 -0600
commitdeb0ed011fe2cfc7058facb3f63b7feaeb8d4a8e (patch)
treea05698851d28feb88d9bcf95a3530792b719b16b /src
parent456955dc33f24b1e9857bdc3c11788a2358e954c (diff)
downloadzash-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')
-rw-r--r--src/edit_builtin.c13
-rw-r--r--src/edit_libedit.c86
-rw-r--r--src/interact.c60
-rw-r--r--src/interact.h8
4 files changed, 155 insertions, 12 deletions
diff --git a/src/edit_builtin.c b/src/edit_builtin.c
index 4b8904b..4ae0bb2 100644
--- a/src/edit_builtin.c
+++ b/src/edit_builtin.c
@@ -299,7 +299,10 @@ static size_t word_right(size_t pos) {
/**
* @brief Columns a prompt occupies on screen.
*
- * Colour and other ANSI control sequences in the prompt take no room.
+ * Control sequences in the prompt are for the terminal, not the screen:
+ * colour, a title, a character-set reset from `tput`, a hyperlink. They
+ * are skipped, including the ones a terminal's terminfo entry writes as
+ * more than `ESC [` and a final byte.
*
* @param s The prompt (one line).
* @return Its width in columns.
@@ -307,12 +310,8 @@ static size_t word_right(size_t pos) {
static size_t prompt_width(const char *s) {
size_t w = 0;
while (*s) {
- if (*s == CH_ESC && s[1] == '[') {
- s += 2;
- while (*s && (isdigit((unsigned char)*s) || *s == ';' || *s == '?'))
- s++;
- if (*s)
- s++;
+ if (*s == CH_ESC) {
+ s += esc_seq_len(s);
continue;
}
w += cell_width(*s) ? 1 : 0;
diff --git a/src/edit_libedit.c b/src/edit_libedit.c
index d9282fc..a76844a 100644
--- a/src/edit_libedit.c
+++ b/src/edit_libedit.c
@@ -16,6 +16,12 @@
* truth, because `fc` works on it. Before each line is read the list is
* copied into libedit's history so that the arrow keys and searches see
* the same entries.
+ *
+ * A prompt is written by libedit and counted as it is written, so the
+ * colour sequences of PS1 would be counted as visible columns and the
+ * cursor would land to the right of where it belongs. Libedit is told
+ * the ignore character (EL_PROMPT_ESC) and every control sequence is
+ * bracketed with it, which prints the sequence but does not count it.
*/
#include <errno.h>
#include <histedit.h>
@@ -24,10 +30,14 @@
#include <string.h>
#include <unistd.h>
+#include "charset.h"
#include "exec.h"
#include "interact.h"
#include "shell.h"
+/** @brief The character that brackets a run libedit must not count. */
+enum { PROMPT_MARK = '\1' };
+
/**
* @brief The libedit editor, created on first use.
*/
@@ -37,9 +47,74 @@ static EditLine *el;
*/
static History *el_hist;
/**
- * @brief The prompt for the line being read.
+ * @brief The prompt for the line being read, marked for libedit.
+ */
+static char *cur_prompt;
+
+/**
+ * @brief End of the run of control sequences that starts at s.
+ *
+ * Adjacent sequences share one mark pair: libedit attaches the run to
+ * the character after the closing mark, and that character has to be a
+ * printable one.
+ *
+ * @param s Text starting with CH_ESC.
+ * @return The first byte after the run.
+ */
+static const char *ctl_run_end(const char *s) {
+ do
+ s += esc_seq_len(s);
+ while (*s == CH_ESC);
+ return s;
+}
+
+/**
+ * @brief Bracket the parts of a prompt that take no room on the screen.
+ *
+ * libedit counts every character of the prompt as a column unless it is
+ * marked. Its literal run is attached to the character after the closing
+ * mark, whose width is the run's width, so a prompt that ends with a
+ * control sequence gets a space from here to be that character: libedit
+ * would drop the run otherwise (prompt.c: "We lose the last literal"),
+ * and counting the sequence instead would put the cursor out by its
+ * length. The extra space is visible and is counted, so the line still
+ * starts where libedit thinks it does.
+ *
+ * @param s The prompt.
+ * @return The prompt with its control sequences marked, for free().
*/
-static const char *cur_prompt = "";
+static char *prompt_marked(const char *s) {
+ size_t n = strlen(s);
+ const char *last = nullptr;
+ for (const char *p = s; *p;) {
+ if (*p != CH_ESC) {
+ p++;
+ continue;
+ }
+ n += 2;
+ last = ctl_run_end(p);
+ p = last;
+ }
+ bool trailing = last && !*last;
+ char *out = xmalloc(n + (trailing ? 2 : 1));
+ char *o = out;
+ for (const char *p = s; *p;) {
+ if (*p != CH_ESC) {
+ *o++ = *p++;
+ continue;
+ }
+ const char *e = ctl_run_end(p);
+ *o++ = PROMPT_MARK;
+ memcpy(o, p, (size_t)(e - p));
+ o += e - p;
+ *o++ = PROMPT_MARK;
+ p = e;
+ }
+ if (trailing)
+ *o++ = ' ';
+ *o = '\0';
+ return out;
+}
/**
* @brief libedit callback: the prompt for the line being read.
@@ -48,7 +123,7 @@ static const char *cur_prompt = "";
*/
static char *prompt_cb(EditLine *e) {
(void)e;
- return (char *)cur_prompt;
+ return cur_prompt ? cur_prompt : "";
}
/**
@@ -80,7 +155,7 @@ char *edit_readline(const char *prompt, size_t *len) {
return nullptr;
history(el_hist, &ev, H_SETSIZE, 100000);
el_set(el, EL_HIST, history, el_hist);
- el_set(el, EL_PROMPT, prompt_cb);
+ el_set(el, EL_PROMPT_ESC, prompt_cb, PROMPT_MARK);
el_set(el, EL_SIGNAL, 0);
el_source(el, nullptr);
}
@@ -89,7 +164,8 @@ char *edit_readline(const char *prompt, size_t *len) {
for (int i = 0; i < hist_count(); i++)
history(el_hist, &ev, H_ENTER, hist_entry(i));
- cur_prompt = prompt;
+ free(cur_prompt);
+ cur_prompt = prompt_marked(prompt);
for (;;) {
int n = 0;
const char *s = el_gets(el, &n);
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 |
diff --git a/src/interact.h b/src/interact.h
index 09a5393..065de38 100644
--- a/src/interact.h
+++ b/src/interact.h
@@ -60,6 +60,14 @@ typedef struct completion {
} completion;
const char *expand_quietly(const char *raw, bool escapes);
+
+/**
+ * @brief Length of the terminal control sequence at s, which is ESC.
+ * @param s Text starting with the escape character.
+ * @return Bytes in the sequence; control sequences in a prompt are for
+ * the terminal, not for the screen, so they take no columns.
+ */
+size_t esc_seq_len(const char *s);
void complete_line(const char *line, size_t cursor, completion *c);
void complete_free(completion *c);