Code

sysdb: Use non-callback readline mode when non-interactive.
authorSebastian Harl <sh@tokkee.org>
Sat, 19 Jul 2014 12:06:33 +0000 (14:06 +0200)
committerSebastian Harl <sh@tokkee.org>
Sat, 19 Jul 2014 12:06:33 +0000 (14:06 +0200)
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
src/tools/sysdb/input.h

index 8d91048e35c5714fe7522f112aa5e736208b5a85..21a2e5d3112fe2b2da323ea2af782e8b95cb2626 100644 (file)
@@ -50,6 +50,8 @@
 #include "utils/error.h"
 #include "utils/strbuf.h"
 
 #include "utils/error.h"
 #include "utils/strbuf.h"
 
+#include <errno.h>
+
 #include <sys/select.h>
 
 #include <stdio.h>
 #include <sys/select.h>
 
 #include <stdio.h>
@@ -131,7 +133,8 @@ handle_input(char *line)
        sdb_strbuf_append(sysdb_input->input, "\n");
        free(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 */
 } /* handle_input */
 
 /* wait for a new line of data to be available */
@@ -145,13 +148,19 @@ input_readline(void)
 
        const char *prompt = "sysdb=> ";
 
 
        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);
        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;
        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;
 
        /* 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 */
 
        return 0;
 } /* sdb_input_init */
 
index 35b37b800b80f2b6651a43d195beee174bca3c8f..ccdce039881ca27893c53e3d5e0cb6ee6ea4cca0 100644 (file)
@@ -38,10 +38,11 @@ typedef struct {
        size_t tokenizer_pos;
        size_t query_len;
 
        size_t tokenizer_pos;
        size_t query_len;
 
+       _Bool interactive;
        _Bool eof;
 } sdb_input_t;
 
        _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:
 
 /*
  * sysdb_input: