From: Sebastian Harl Date: Fri, 10 Jan 2014 09:01:15 +0000 (+0100) Subject: sysdb: Added a query to the history after executing it. X-Git-Tag: sysdb-0.1.0~241 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=a2edc622eb8aa5ff956757a846405d7cbd05c646;p=sysdb.git sysdb: Added a query to the history after executing it. Mimic zsh's histignorespace option and ignore commands when the first character is a space. --- diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index b568aae..30e6419 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -37,17 +37,20 @@ #include #include +#include /* * public API */ -int +char * 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; @@ -67,7 +70,10 @@ sdb_command_exec(sdb_input_t *input) 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) @@ -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; - return 0; + return data; } /* sdb_command_exec */ /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/tools/sysdb/command.h b/src/tools/sysdb/command.h index 3cf9fca..06b1ce9 100644 --- a/src/tools/sysdb/command.h +++ b/src/tools/sysdb/command.h @@ -32,13 +32,15 @@ /* * 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: - * - 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 */ diff --git a/src/tools/sysdb/input.c b/src/tools/sysdb/input.c index d5deb1d..dcd12b0 100644 --- a/src/tools/sysdb/input.c +++ b/src/tools/sysdb/input.c @@ -30,6 +30,7 @@ #endif /* HAVE_CONFIG_H */ #include "tools/sysdb/input.h" +#include "tools/sysdb/command.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 */ +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 : */ diff --git a/src/tools/sysdb/input.h b/src/tools/sysdb/input.h index 7fd8d58..00b915b 100644 --- a/src/tools/sysdb/input.h +++ b/src/tools/sysdb/input.h @@ -56,6 +56,14 @@ ssize_t 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 */ diff --git a/src/tools/sysdb/scanner.l b/src/tools/sysdb/scanner.l index c9b527e..518d299 100644 --- a/src/tools/sysdb/scanner.l +++ b/src/tools/sysdb/scanner.l @@ -33,7 +33,6 @@ */ #include "tools/sysdb/input.h" -#include "tools/sysdb/command.h" #include @@ -90,7 +89,7 @@ identifier ([A-Za-z_][A-Za-z_0-9$]*) * The following rules are specific to the command line tool. */ -";" { APPEND(); sdb_command_exec(sdb_input); } +";" { APPEND(); sdb_input_exec_query(sdb_input); } . { APPEND(); }