/** * @file edit_libedit.c * @brief The line editor implemented with libedit (BSD editline). * * This is the compile-time alternative to edit_builtin.c: * * make EDIT=libedit LDLIBS=-ledit * * It provides the same two functions, edit_usable() and edit_readline(), * so nothing else in the shell knows which editor is linked. libedit * brings its own key bindings (including a fuller vi mode) and reads * `~/.editrc`. It also brings a dependency on the library and on * terminfo or termcap, which is why it is not the default. * * The shell's own history list (history.c) stays the single source of * 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 #include #include #include #include #include #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. */ static EditLine *el; /** * @brief libedit's copy of the history. */ static History *el_hist; /** * @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 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. * @param e Unused. * @return The prompt set by edit_readline(). */ static char *prompt_cb(EditLine *e) { (void)e; return cur_prompt ? cur_prompt : ""; } /** * @brief Can lines be edited on this terminal? * @return true if stdin and stderr are terminals and TERM is not "dumb". */ bool edit_usable(void) { const char *term = var_get("TERM"); if (!term || !*term || !strcmp(term, "dumb")) return false; return isatty(0) && isatty(2); } /** * @brief Read one line with editing. * * @param prompt Text to show before the line. * @param len Receives the length of the line. * @return The line without its newline, valid until the next call; * nullptr at end of input. */ char *edit_readline(const char *prompt, size_t *len) { static char *line; HistEvent ev; if (!el) { el = el_init("zash", stdin, stderr, stderr); el_hist = history_init(); if (!el || !el_hist) return nullptr; history(el_hist, &ev, H_SETSIZE, 100000); el_set(el, EL_HIST, history, el_hist); el_set(el, EL_PROMPT_ESC, prompt_cb, PROMPT_MARK); el_set(el, EL_SIGNAL, 0); el_source(el, nullptr); } el_set(el, EL_EDITOR, opt.vi ? "vi" : "emacs"); history(el_hist, &ev, H_CLEAR); for (int i = 0; i < hist_count(); i++) history(el_hist, &ev, H_ENTER, hist_entry(i)); free(cur_prompt); cur_prompt = prompt_marked(prompt); for (;;) { int n = 0; const char *s = el_gets(el, &n); if (s && n > 0) { size_t k = (size_t)n; while (k && (s[k - 1] == '\n' || s[k - 1] == '\r')) k--; free(line); line = xmalloc(k + 1); memcpy(line, s, k); line[k] = '\0'; *len = k; return line; } if (n < 0 && errno == EINTR) { trap_poll(); /* ^C abandons the line and does not return */ continue; } return nullptr; } } int edit_bind(const char *spec) { sh_warn("bind: %s: this editor reads its bindings from ~/.editrc", spec); return -1; } void edit_bind_list(void) { sh_warn("bind: -l: this editor reads its bindings from ~/.editrc"); }