From a2edc622eb8aa5ff956757a846405d7cbd05c646 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Fri, 10 Jan 2014 10:01:15 +0100 Subject: [PATCH] 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. --- src/tools/sysdb/command.c | 12 +++++++++--- src/tools/sysdb/command.h | 10 ++++++---- src/tools/sysdb/input.c | 15 +++++++++++++++ src/tools/sysdb/input.h | 8 ++++++++ src/tools/sysdb/scanner.l | 3 +-- 5 files changed, 39 insertions(+), 9 deletions(-) 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(); } -- 2.30.2