Code

sysdb: Only try to reconnect before executing a command or on an empty line.
authorSebastian Harl <sh@tokkee.org>
Thu, 11 Dec 2014 22:42:14 +0000 (23:42 +0100)
committerSebastian Harl <sh@tokkee.org>
Thu, 11 Dec 2014 22:42:14 +0000 (23:42 +0100)
src/tools/sysdb/command.c
src/tools/sysdb/input.c
src/tools/sysdb/input.h
src/tools/sysdb/scanner.l

index 3126c7bc9d42ec7a73ad439eade0f29804748d9f..6c3114d8e692274d8223c7601c3082d5174b5fe2 100644 (file)
@@ -97,6 +97,7 @@ clear_query(sdb_input_t *input)
        sdb_strbuf_skip(input->input, 0, input->query_len);
        input->tokenizer_pos -= input->query_len;
        input->query_len = 0;
        sdb_strbuf_skip(input->input, 0, input->query_len);
        input->tokenizer_pos -= input->query_len;
        input->query_len = 0;
+       input->have_input = 0;
 } /* clear_query */
 
 /*
 } /* clear_query */
 
 /*
index a88b2266147de67a659cd383e802a0a61701f7ce..898464af0b550118aa0367337a4e4b85460930c6 100644 (file)
@@ -133,9 +133,6 @@ handle_input(char *line)
                return;
        }
 
                return;
        }
 
-       if (sdb_client_eof(sysdb_input->client))
-               sdb_input_reconnect();
-
        sdb_strbuf_append(sysdb_input->input, "%s\n", line);
        free(line);
 
        sdb_strbuf_append(sysdb_input->input, "%s\n", line);
        free(line);
 
@@ -162,7 +159,7 @@ input_readline(void)
                return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
        }
 
                return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
        }
 
-       if (sysdb_input->query_len)
+       if (sysdb_input->have_input)
                prompt = "sysdb-> ";
        if (sdb_client_eof(sysdb_input->client))
                prompt = "!-> ";
                prompt = "sysdb-> ";
        if (sdb_client_eof(sysdb_input->client))
                prompt = "!-> ";
@@ -278,11 +275,19 @@ sdb_input_readline(char *buf, size_t *n_chars, size_t max_chars)
 int
 sdb_input_exec_query(void)
 {
 int
 sdb_input_exec_query(void)
 {
-       char *query = sdb_command_exec(sysdb_input);
+       char *query = NULL;
 
        HIST_ENTRY *hist;
        const char *prev = NULL;
 
 
        HIST_ENTRY *hist;
        const char *prev = NULL;
 
+       if (! sysdb_input->have_input) {
+               /* empty line */
+               if (sdb_client_eof(sysdb_input->client))
+                       sdb_input_reconnect();
+               return 0;
+       }
+
+       query = sdb_command_exec(sysdb_input);
        if (! query)
                return -1;
 
        if (! query)
                return -1;
 
index 0aeabf8b7b653197f98ffa0678d13b769e0c21e2..b8501f472383cd454608c19aebcab8688e784c60 100644 (file)
@@ -39,11 +39,14 @@ typedef struct {
        size_t tokenizer_pos;
        size_t query_len;
 
        size_t tokenizer_pos;
        size_t query_len;
 
+       /* indicates that we've had non-empty input */
+       bool have_input;
+
        bool interactive;
        bool eof;
 } sdb_input_t;
 
        bool interactive;
        bool eof;
 } sdb_input_t;
 
-#define SDB_INPUT_INIT { NULL, NULL, NULL, 0, 0, 1, 0 }
+#define SDB_INPUT_INIT { NULL, NULL, NULL, 0, 0, 0, 1, 0 }
 
 /*
  * sysdb_input:
 
 /*
  * sysdb_input:
index 1404690a1256354f078ab87f863e516d57ff270e..16defa61859085dc20dad932c8f812986af494e3 100644 (file)
@@ -48,6 +48,8 @@
 
 #define APPEND() \
        do { \
 
 #define APPEND() \
        do { \
+               if (! isspace((int)yytext[0])) \
+                       sysdb_input->have_input = 1; \
                sysdb_input->query_len += strlen(yytext); \
        } while (0)
 
                sysdb_input->query_len += strlen(yytext); \
        } while (0)
 
@@ -63,7 +65,7 @@
 
 %x CSC
 
 
 %x CSC
 
-newline                        ([\n\r]+)
+newline                        (\n|\r\n)
 simple_comment ("--"[^\n\r]*)
 
 /*
 simple_comment ("--"[^\n\r]*)
 
 /*
@@ -99,10 +101,17 @@ string             ('([^']|'')*')
         * The following rules are specific to the command line tool.
         */
 
         * The following rules are specific to the command line tool.
         */
 
+";\n"          { APPEND(); sdb_input_exec_query(); }
 ";"                    { APPEND(); sdb_input_exec_query(); }
 
 ";"                    { APPEND(); sdb_input_exec_query(); }
 
+{newline}      {
+                               APPEND();
+                               if (! sysdb_input->have_input)
+                                       /* give the input module a chance to do stuff on empty lines */
+                                       sdb_input_exec_query();
+                       }
+
 .                      { APPEND(); }
 .                      { APPEND(); }
-{newline}      { APPEND(); }
 
 %%
 
 
 %%