Code

sysdb: Only try to reconnect before executing a command or on an empty line.
[sysdb.git] / src / tools / sysdb / command.c
index 7737a2e15e35e6c9084227669f6f40471ea463e3..6c3114d8e692274d8223c7601c3082d5174b5fe2 100644 (file)
@@ -91,6 +91,15 @@ 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;
+       input->have_input = 0;
+} /* clear_query */
+
 /*
  * public API
  */
@@ -168,25 +177,26 @@ sdb_command_exec(sdb_input_t *input)
        if (query_len) {
                data = strndup(query, query_len);
                /* ignore errors; we'll only hide the command from the caller */
+       }
 
-               sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
-
-               /* The server will send back *something*, either error/log messages
-                * and/or the reply to the query. Here, we don't care about what it
-                * 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);
-                       data = NULL;
+       if (sdb_client_eof(input->client)) {
+               if (sdb_input_reconnect()) {
+                       clear_query(input);
+                       return data;
                }
        }
-
-       sdb_strbuf_skip(input->input, 0, input->query_len);
-       input->tokenizer_pos -= input->query_len;
-       input->query_len = 0;
+       else if (! query_len)
+               return NULL;
+
+       sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
+
+       /* The server will send back *something*, either error/log messages
+        * and/or the reply to the query. Here, we don't care about what it
+        * 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). */
+       sdb_command_print_reply(input->client);
+       clear_query(input);
        return data;
 } /* sdb_command_exec */