From 498c6c0d39c932b38a6ba1641119c0b5f97d1399 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Sat, 19 Jul 2014 14:06:33 +0200 Subject: [PATCH] sysdb: Use non-callback readline mode when non-interactive. libedit's implementation of the callback-mode does not play well in non-interactive mode (e.g. it does not correctly detect EOF) but works fine when using readline(). Also, there's no need for the somewhat more complex callback mode in non-interactive operation anyway. --- src/tools/sysdb/input.c | 23 ++++++++++++++++------- src/tools/sysdb/input.h | 3 ++- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index 8d91048..21a2e5d 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -50,6 +50,8 @@ #include "utils/error.h" #include "utils/strbuf.h" +#include + #include #include @@ -131,7 +133,8 @@ handle_input(char *line) sdb_strbuf_append(sysdb_input->input, "\n"); free(line); - rl_callback_handler_remove(); + if (sysdb_input->interactive) + rl_callback_handler_remove(); } /* handle_input */ /* wait for a new line of data to be available */ @@ -145,13 +148,19 @@ input_readline(void) const char *prompt = "sysdb=> "; + len = sdb_strbuf_len(sysdb_input->input); + + if (! sysdb_input->interactive) { + char *line = readline(""); + handle_input(line); + return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len); + } + if (sysdb_input->query_len) prompt = "sysdb-> "; rl_callback_handler_install(prompt, handle_input); client_fd = sdb_client_sockfd(sysdb_input->client); - - len = sdb_strbuf_len(sysdb_input->input); while ((sdb_strbuf_len(sysdb_input->input) == len) && (! sysdb_input->eof)) { int n; @@ -208,10 +217,10 @@ sdb_input_init(sdb_input_t *input) /* register input handler */ sysdb_input = input; - if (! isatty(STDIN_FILENO)) - return -1; - - term_rawmode(); + input->interactive = isatty(STDIN_FILENO) != 0; + errno = 0; + if (input->interactive) + term_rawmode(); return 0; } /* sdb_input_init */ diff --git a/src/tools/sysdb/input.h b/src/tools/sysdb/input.h index 35b37b8..ccdce03 100644 --- a/src/tools/sysdb/input.h +++ b/src/tools/sysdb/input.h @@ -38,10 +38,11 @@ typedef struct { size_t tokenizer_pos; size_t query_len; + _Bool interactive; _Bool eof; } sdb_input_t; -#define SDB_INPUT_INIT { NULL, NULL, 0, 0, 0 } +#define SDB_INPUT_INIT { NULL, NULL, 0, 0, 1, 0 } /* * sysdb_input: -- 2.30.2