From b7b1198be76e46c6e151a4092ba101c9c0a33693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Wed, 2 Sep 2020 01:07:31 +0200 Subject: [PATCH] Fix libedit a bit more and discourage from using it --- README.adoc | 3 +++ json-rpc-shell.c | 14 ++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index 9c4fec4..646c93a 100644 --- a/README.adoc +++ b/README.adoc @@ -47,6 +47,9 @@ Build dependencies: CMake, pkg-config, help2man, Runtime dependencies: libev, Jansson, cURL, openssl, readline or libedit >= 2013-07-12, +Avoid libedit if you can, in general it works but at the moment history is +acting up and I have no clue about fixing it. + $ git clone --recursive https://git.janouch.name/p/json-rpc-shell.git $ mkdir json-rpc-shell/build $ cd json-rpc-shell/build diff --git a/json-rpc-shell.c b/json-rpc-shell.c index 8f74a06..8f1e147 100644 --- a/json-rpc-shell.c +++ b/json-rpc-shell.c @@ -619,7 +619,6 @@ input_el_on_run_editor (EditLine *editline, int key) static void input_el_install_prompt (struct input_el *self) { - // XXX: the ignore doesn't quite work, see https://gnats.netbsd.org/47539 el_set (self->editline, EL_PROMPT_ESC, input_el_make_prompt, INPUT_START_IGNORE); } @@ -634,7 +633,7 @@ input_el_start (struct input *input, const char *program_name) el_set (self->editline, EL_CLIENTDATA, self); input_el_install_prompt (self); el_set (self->editline, EL_SIGNAL, false); - el_set (self->editline, EL_UNBUFFERED, true); + el_set (self->editline, EL_UNBUFFERED, isatty (fileno (stdin))); el_set (self->editline, EL_EDITOR, "emacs"); el_wset (self->editline, EL_HIST, history_w, self->history); @@ -834,6 +833,17 @@ input_el_on_tty_readable (struct input *input) int count = 0; const wchar_t *buf = el_wgets (self->editline, &count); + // Editline works in a funny NO_TTY mode when the input is not a tty, + // we cannot use EL_UNBUFFERED and expect sane results then + int unbuffered = 0; + if (!el_get (self->editline, EL_UNBUFFERED, &unbuffered) && !unbuffered) + { + char *entered_line = buf ? input_el_wcstombs (buf) : NULL; + self->super.on_input (entered_line, self->super.user_data); + free (entered_line); + return; + } + // Process data from our newline handler (async-friendly handling) if (self->entered_line) {