Code

sysdb: Split sdb_command_exec() into two functions.
authorSebastian Harl <sh@tokkee.org>
Tue, 4 Feb 2014 21:56:23 +0000 (22:56 +0100)
committerSebastian Harl <sh@tokkee.org>
Tue, 4 Feb 2014 21:56:23 +0000 (22:56 +0100)
Separate the handling of the server's reply to make it reusable by other parts
of the code.

src/tools/sysdb/command.c
src/tools/sysdb/command.h

index f948d3aed65a5c18c78565819bdfac522b1cbe37..0abf381e95527234d8e2ef8aa608e25d3446c9ad 100644 (file)
  * 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);
index 06b1ce9f26080faaec92cb91af59a36fe648a679..726e27a59f05f1229ecf79ac8874df625203b6a5 100644 (file)
 #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