Code

sysdb: Simplified prompt used for continuation lines.
[sysdb.git] / src / tools / sysdb / input.c
index 52036028d2905d35555705ec026e2bc30f96c55e..a1e7b714b689a85e946587624948a6d237eef78c 100644 (file)
@@ -159,8 +159,8 @@ input_readline(void)
                return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
        }
 
-       if (sysdb_input->query_len)
-               prompt = "sysdb-> ";
+       if (sysdb_input->have_input)
+               prompt = "     -> ";
        if (sdb_client_eof(sysdb_input->client))
                prompt = "!-> ";
 
@@ -208,7 +208,7 @@ input_readline(void)
                if (sdb_client_eof(sysdb_input->client)) {
                        rl_callback_handler_remove();
                        /* XXX */
-                       printf("Remote side closed the connection.\n");
+                       sdb_log(SDB_LOG_ERR, "Remote side closed the connection.");
                        /* return EOF -> restart scanner */
                        return 0;
                }
@@ -275,24 +275,44 @@ 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 *current_hist;
-       const char *hist_line = 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;
 
-       current_hist = current_history();
-       if (current_hist)
-               hist_line = current_hist->line;
+       hist = history_get(history_length);
+       if (hist)
+               prev = hist->line;
 
        if (*query != ' ')
-               if ((! hist_line) || strcmp(hist_line, query))
+               if ((! prev) || strcmp(prev, query))
                        add_history(query);
        free(query);
        return 0;
 } /* sdb_input_exec_query */
 
+int
+sdb_input_reconnect(void)
+{
+       sdb_client_close(sysdb_input->client);
+       if (sdb_client_connect(sysdb_input->client, sysdb_input->user)) {
+               sdb_log(SDB_LOG_ERR, "Failed to reconnect to SysDBd");
+               return -1;
+       }
+       sdb_log(SDB_LOG_INFO, "Successfully reconnected to SysDBd");
+       return 0;
+} /* sdb_input_reconnect */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */