Code

sysdb: Added a query to the history after executing it.
authorSebastian Harl <sh@tokkee.org>
Fri, 10 Jan 2014 09:01:15 +0000 (10:01 +0100)
committerSebastian Harl <sh@tokkee.org>
Fri, 10 Jan 2014 09:01:15 +0000 (10:01 +0100)
Mimic zsh's histignorespace option and ignore commands when the first
character is a space.

src/tools/sysdb/command.c
src/tools/sysdb/command.h
src/tools/sysdb/input.c
src/tools/sysdb/input.h
src/tools/sysdb/scanner.l

index b568aaec3289f0b5dd29c1db48d1d232e7ab2288..30e6419e334d1b105749db8861826f2adf5f2883 100644 (file)
 
 #include <assert.h>
 #include <ctype.h>
 
 #include <assert.h>
 #include <ctype.h>
+#include <string.h>
 
 /*
  * public API
  */
 
 
 /*
  * public API
  */
 
-int
+char *
 sdb_command_exec(sdb_input_t *input)
 {
        const char *query;
        uint32_t query_len;
 
 sdb_command_exec(sdb_input_t *input)
 {
        const char *query;
        uint32_t query_len;
 
+       char *data = NULL;
+
        query = sdb_strbuf_string(input->input);
        query_len = (uint32_t)input->query_len;
 
        query = sdb_strbuf_string(input->input);
        query_len = (uint32_t)input->query_len;
 
@@ -67,7 +70,10 @@ sdb_command_exec(sdb_input_t *input)
 
                recv_buf = sdb_strbuf_create(1024);
                if (! recv_buf)
 
                recv_buf = sdb_strbuf_create(1024);
                if (! recv_buf)
-                       return -1;
+                       return NULL;
+
+               data = strndup(query, query_len);
+               /* ignore errors; we'll only hide the command from the caller */
 
                sdb_client_send(input->client, CONNECTION_QUERY, query_len, query);
                if (sdb_client_recv(input->client, &rcode, recv_buf) < 0)
 
                sdb_client_send(input->client, CONNECTION_QUERY, query_len, query);
                if (sdb_client_recv(input->client, &rcode, recv_buf) < 0)
@@ -83,7 +89,7 @@ sdb_command_exec(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;
-       return 0;
+       return data;
 } /* sdb_command_exec */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 } /* sdb_command_exec */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
index 3cf9fcad2025e4dc951fdae3763252f24650dddd..06b1ce9f26080faaec92cb91af59a36fe648a679 100644 (file)
 
 /*
  * sdb_command_exec:
 
 /*
  * sdb_command_exec:
- * Execute the current command buffer.
+ * Execute the current command buffer and return the query as send to the
+ * server. The query buffer points to dynamically allocated memory which has
+ * to be free'd by the caller.
  *
  * Returns:
  *
  * Returns:
- *  - 0 on success
- *  - a negative value else
+ *  - the query (nul-terminated string) on success
+ *  - NULL else
  */
  */
-int
+char *
 sdb_command_exec(sdb_input_t *input);
 
 #endif /* SYSDB_COMMAND_H */
 sdb_command_exec(sdb_input_t *input);
 
 #endif /* SYSDB_COMMAND_H */
index d5deb1dd61b9cf6f26a215ae0efba955a233a48d..dcd12b033d76cbaa61836013617bccde9acb64e4 100644 (file)
@@ -30,6 +30,7 @@
 #endif /* HAVE_CONFIG_H */
 
 #include "tools/sysdb/input.h"
 #endif /* HAVE_CONFIG_H */
 
 #include "tools/sysdb/input.h"
+#include "tools/sysdb/command.h"
 
 #include "utils/strbuf.h"
 
 
 #include "utils/strbuf.h"
 
@@ -112,5 +113,19 @@ sdb_input_readline(sdb_input_t *input, char *buf,
        return (ssize_t)len;
 } /* sdb_input_readline */
 
        return (ssize_t)len;
 } /* sdb_input_readline */
 
+int
+sdb_input_exec_query(sdb_input_t *input)
+{
+       char *query = sdb_command_exec(input);
+
+       if (! query)
+               return -1;
+
+       if (*query != ' ')
+               add_history(query);
+       free(query);
+       return 0;
+} /* sdb_input_exec_query */
+
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */
 
index 7fd8d587de8961f70a2107048ca782ed53a39659..00b915beabea13735804bc3c1ebc480630d1aa84 100644 (file)
@@ -56,6 +56,14 @@ ssize_t
 sdb_input_readline(sdb_input_t *input, char *buf,
                int *n_chars, size_t max_chars);
 
 sdb_input_readline(sdb_input_t *input, char *buf,
                int *n_chars, size_t max_chars);
 
+/*
+ * sdb_input_exec_query:
+ * Execute the query currently stored in the input buffer. Waits for the
+ * server's reply and prints errors or returned data to standard output.
+ */
+int
+sdb_input_exec_query(sdb_input_t *input);
+
 /*
  * scanner
  */
 /*
  * scanner
  */
index c9b527e7d9cdb74785f891b6ea99553ff478a9b3..518d299bea4707eabd1843b82f375e0c58f6f6c3 100644 (file)
@@ -33,7 +33,6 @@
  */
 
 #include "tools/sysdb/input.h"
  */
 
 #include "tools/sysdb/input.h"
-#include "tools/sysdb/command.h"
 
 #include <string.h>
 
 
 #include <string.h>
 
@@ -90,7 +89,7 @@ identifier    ([A-Za-z_][A-Za-z_0-9$]*)
         * The following rules are specific to the command line tool.
         */
 
         * The following rules are specific to the command line tool.
         */
 
-";"    { APPEND(); sdb_command_exec(sdb_input); }
+";"    { APPEND(); sdb_input_exec_query(sdb_input); }
 
 .      { APPEND(); }
 
 
 .      { APPEND(); }