From: Sebastian Harl Date: Thu, 11 Dec 2014 22:42:14 +0000 (+0100) Subject: sysdb: Only try to reconnect before executing a command or on an empty line. X-Git-Tag: sysdb-0.7.0~112^2~2 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=b6c730c2d0cf400127da5ca96b51abd4ad335c0c;ds=sidebyside sysdb: Only try to reconnect before executing a command or on an empty line. --- diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index 3126c7b..6c3114d 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -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; + input->have_input = 0; } /* clear_query */ /* diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index a88b226..898464a 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -133,9 +133,6 @@ handle_input(char *line) return; } - if (sdb_client_eof(sysdb_input->client)) - sdb_input_reconnect(); - 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); } - if (sysdb_input->query_len) + if (sysdb_input->have_input) 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) { - char *query = sdb_command_exec(sysdb_input); + char *query = 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; diff --git a/src/tools/sysdb/input.h b/src/tools/sysdb/input.h index 0aeabf8..b8501f4 100644 --- a/src/tools/sysdb/input.h +++ b/src/tools/sysdb/input.h @@ -39,11 +39,14 @@ typedef struct { 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; -#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: diff --git a/src/tools/sysdb/scanner.l b/src/tools/sysdb/scanner.l index 1404690..16defa6 100644 --- a/src/tools/sysdb/scanner.l +++ b/src/tools/sysdb/scanner.l @@ -48,6 +48,8 @@ #define APPEND() \ do { \ + if (! isspace((int)yytext[0])) \ + sysdb_input->have_input = 1; \ sysdb_input->query_len += strlen(yytext); \ } while (0) @@ -63,7 +65,7 @@ %x CSC -newline ([\n\r]+) +newline (\n|\r\n) simple_comment ("--"[^\n\r]*) /* @@ -99,10 +101,17 @@ string ('([^']|'')*') * The following rules are specific to the command line tool. */ +";\n" { 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(); } -{newline} { APPEND(); } %%