From: Sebastian Harl Date: Tue, 4 Feb 2014 21:56:23 +0000 (+0100) Subject: sysdb: Split sdb_command_exec() into two functions. X-Git-Tag: sysdb-0.1.0~225 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=50e174b8a18431d969d27cfaca58c8de14029ea4 sysdb: Split sdb_command_exec() into two functions. Separate the handling of the server's reply to make it reusable by other parts of the code. --- diff --git a/src/tools/sysdb/command.c b/src/tools/sysdb/command.c index f948d3a..0abf381 100644 --- a/src/tools/sysdb/command.c +++ b/src/tools/sysdb/command.c @@ -46,6 +46,34 @@ * public API */ +int +sdb_command_print_reply(sdb_input_t *input) +{ + sdb_strbuf_t *recv_buf; + const char *result; + uint32_t rcode = 0; + + recv_buf = sdb_strbuf_create(1024); + if (! recv_buf) + return -1; + + if (sdb_client_recv(input->client, &rcode, recv_buf) < 0) + rcode = UINT32_MAX; + + if (rcode == UINT32_MAX) + printf("ERROR: "); + result = sdb_strbuf_string(recv_buf); + if (result && *result) + printf("%s\n", result); + else if (rcode == UINT32_MAX) { + char errbuf[1024]; + printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); + } + + sdb_strbuf_destroy(recv_buf); + return 0; +} /* sdb_command_print_reply */ + char * sdb_command_exec(sdb_input_t *input) { @@ -68,32 +96,12 @@ sdb_command_exec(sdb_input_t *input) --query_len; if (query_len) { - sdb_strbuf_t *recv_buf; - const char *result; - uint32_t rcode = 0; - - recv_buf = sdb_strbuf_create(1024); - if (! recv_buf) - 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) - rcode = UINT32_MAX; - - if (rcode == UINT32_MAX) - printf("ERROR: "); - result = sdb_strbuf_string(recv_buf); - if (result && *result) - printf("%s\n", result); - else if (rcode == UINT32_MAX) { - char errbuf[1024]; - printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf))); - } - - sdb_strbuf_destroy(recv_buf); + if (sdb_command_print_reply(input)) + return NULL; } sdb_strbuf_skip(input->input, 0, input->query_len); diff --git a/src/tools/sysdb/command.h b/src/tools/sysdb/command.h index 06b1ce9..726e27a 100644 --- a/src/tools/sysdb/command.h +++ b/src/tools/sysdb/command.h @@ -30,12 +30,26 @@ #ifndef SYSDB_COMMAND_H #define SYSDB_COMMAND_H 1 +/* + * sdb_command_print_reply: + * Read a reply from the server and print it to the standard output channel. + * + * Returns: + * - 0 on success + * - a negative value else + */ +int +sdb_command_print_reply(sdb_input_t *input); + /* * sdb_command_exec: * 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. * + * The function waits for the server's reply and prints it to the standard + * output channel. + * * Returns: * - the query (nul-terminated string) on success * - NULL else