Code

sysdb: Do an attempt to reconnect on all user input.
[sysdb.git] / src / tools / sysdb / command.c
index 143661868d49204c9436bdd2e370a1346881df71..5044c0e5213aac42cb4456cd9dafe465264f4010 100644 (file)
@@ -91,6 +91,14 @@ static struct {
        { SDB_CONNECTION_DATA, data_printer },
 };
 
+static void
+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;
+} /* clear_query */
+
 /*
  * public API
  */
@@ -165,6 +173,13 @@ sdb_command_exec(sdb_input_t *input)
        while (query_len && (query[query_len - 1]) == '\n')
                --query_len;
 
+       if (sdb_client_eof(input->client)) {
+               if (sdb_input_reconnect()) {
+                       clear_query(input);
+                       return NULL;
+               }
+       }
+
        if (query_len) {
                data = strndup(query, query_len);
                /* ignore errors; we'll only hide the command from the caller */
@@ -176,16 +191,15 @@ sdb_command_exec(sdb_input_t *input)
                 * sends back. We'll wait for the first reply and then return to the
                 * main loop which will handle any subsequent replies, including
                 * eventually the reply to the query (if it's not the first reply). */
+               /* TODO: wait for the actual reply instead */
                if (sdb_command_print_reply(input->client) < 0) {
                        if (data)
                                free(data);
-                       return NULL;
+                       data = NULL;
                }
        }
 
-       sdb_strbuf_skip(input->input, 0, input->query_len);
-       input->tokenizer_pos -= input->query_len;
-       input->query_len = 0;
+       clear_query(input);
        return data;
 } /* sdb_command_exec */