Code

Merged branch 'json'.
authorSebastian Harl <sh@tokkee.org>
Sun, 11 Dec 2016 16:42:18 +0000 (17:42 +0100)
committerSebastian Harl <sh@tokkee.org>
Sun, 11 Dec 2016 16:42:18 +0000 (17:42 +0100)
src/tools/sysdb/command.c
src/tools/sysdb/command.h
src/tools/sysdb/input.c
src/tools/sysdb/json.c
src/tools/sysdb/json.h
src/tools/sysdb/main.c

index 4de2b44e4fc79245027a22c5c2932bd82b46f742..dd8b2980c7ca6efb784bd7c6e8a889618d207596 100644 (file)
@@ -48,7 +48,7 @@
 #include <string.h>
 
 static void
-ok_printer(sdb_strbuf_t *buf)
+ok_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf)
 {
        const char *msg = sdb_strbuf_string(buf);
        if (msg && *msg)
@@ -58,7 +58,7 @@ ok_printer(sdb_strbuf_t *buf)
 } /* ok_printer */
 
 static void
-log_printer(sdb_strbuf_t *buf)
+log_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf)
 {
        uint32_t prio = 0;
 
@@ -73,7 +73,7 @@ log_printer(sdb_strbuf_t *buf)
 } /* log_printer */
 
 static void
-data_printer(sdb_strbuf_t *buf)
+data_printer(sdb_input_t __attribute__((unused)) *input, sdb_strbuf_t *buf)
 {
        size_t len = sdb_strbuf_len(buf);
 
@@ -90,14 +90,14 @@ data_printer(sdb_strbuf_t *buf)
        /* At the moment, we don't care about the result type. We simply print the
         * result without further parsing it. */
        sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
-       if (sdb_json_print(buf))
+       if (sdb_json_print(input, buf))
                sdb_log(SDB_LOG_ERR, "Failed to print result");
        printf("\n");
 } /* data_printer */
 
 static struct {
        int status;
-       void (*printer)(sdb_strbuf_t *);
+       void (*printer)(sdb_input_t *, sdb_strbuf_t *);
 } response_printers[] = {
        { SDB_CONNECTION_OK,   ok_printer },
        { SDB_CONNECTION_LOG,  log_printer },
@@ -118,7 +118,7 @@ clear_query(sdb_input_t *input)
  */
 
 int
-sdb_command_print_reply(sdb_client_t *client)
+sdb_command_print_reply(sdb_input_t *input)
 {
        sdb_strbuf_t *recv_buf;
        const char *result;
@@ -131,10 +131,10 @@ sdb_command_print_reply(sdb_client_t *client)
        if (! recv_buf)
                return -1;
 
-       if (sdb_client_recv(client, &rcode, recv_buf) < 0)
+       if (sdb_client_recv(input->client, &rcode, recv_buf) < 0)
                rcode = UINT32_MAX;
 
-       if (sdb_client_eof(client)) {
+       if (sdb_client_eof(input->client)) {
                sdb_strbuf_destroy(recv_buf);
                return -1;
        }
@@ -144,7 +144,7 @@ sdb_command_print_reply(sdb_client_t *client)
 
        for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
                if (status == response_printers[i].status) {
-                       response_printers[i].printer(recv_buf);
+                       response_printers[i].printer(input, recv_buf);
                        sdb_strbuf_destroy(recv_buf);
                        return status;
                }
@@ -203,7 +203,7 @@ sdb_command_exec(sdb_input_t *input)
        /* The server may send back log messages but will eventually reply to the
         * query, which is either DATA or ERROR. */
        while (42) {
-               int status = sdb_command_print_reply(input->client);
+               int status = sdb_command_print_reply(input);
                if (status < 0) {
                        sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
                        break;
index 04ad5e156a7cd87992a9c8691adc2414eb4fe013..b2e5fa7ccc7363ea6efd51a091c06fb1c3caaac7 100644 (file)
@@ -40,7 +40,7 @@
  *  - a positive value if the server returned an error
  */
 int
-sdb_command_print_reply(sdb_client_t *client);
+sdb_command_print_reply(sdb_input_t *input);
 
 /*
  * sdb_command_exec:
index f0f86ea2a9bff1e264013bf39c3ab31758919c63..2a2d1707101404169febb67d0ac643aaa98ec649 100644 (file)
@@ -203,7 +203,7 @@ input_readline(void)
                /* some response / error message from the server pending */
                /* XXX: clear current line */
                printf("\n");
-               sdb_command_print_reply(sysdb_input->client);
+               sdb_command_print_reply(sysdb_input);
 
                if (sdb_client_eof(sysdb_input->client)) {
                        rl_callback_handler_remove();
index 703b22e8ef1ed5087ab6af48000d14054657a32f..1792cdac9b3f53eacb9c0b8ef238426a5e465b3b 100644 (file)
@@ -107,7 +107,7 @@ printer(void __attribute__((unused)) *ctx, const char *str, size_t len)
  */
 
 int
-sdb_json_print(sdb_strbuf_t *buf)
+sdb_json_print(sdb_input_t *input, sdb_strbuf_t *buf)
 {
 #ifdef HAVE_LIBYAJL
        const unsigned char *json;
@@ -119,6 +119,12 @@ sdb_json_print(sdb_strbuf_t *buf)
 
        int ret = 0;
 
+       if (!input->interactive) {
+               /* no formatting */
+               printf("%s\n", sdb_strbuf_string(buf));
+               return 0;
+       }
+
        gen = yajl_gen_alloc(/* alloc_funcs */ NULL);
        if (! gen)
                return -1;
index 9fc1a70d3fb49fc42272e66ef4d2b7379ed0dd3b..08def61f726e361b94bf028b8098722740196066 100644 (file)
@@ -25,6 +25,8 @@
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include "tools/sysdb/input.h"
+
 #include "utils/strbuf.h"
 
 #ifndef SYSDB_JSON_H
@@ -36,7 +38,7 @@
  * printed to the standard output channel.
  */
 int
-sdb_json_print(sdb_strbuf_t *buf);
+sdb_json_print(sdb_input_t *input, sdb_strbuf_t *buf);
 
 #endif /* SYSDB_JSON_H */
 
index f502027f04353e115dd427cdd429816569a16116..b45ef878873b1b0dc943dbe38efa0555d6d042d2 100644 (file)
@@ -164,7 +164,7 @@ exit_version(void)
 } /* exit_version */
 
 static int
-execute_commands(sdb_client_t *client, sdb_llist_t *commands)
+execute_commands(sdb_input_t *input, sdb_llist_t *commands)
 {
        sdb_llist_iter_t *iter;
        int status = 0;
@@ -178,7 +178,7 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands)
        while (sdb_llist_iter_has_next(iter)) {
                sdb_object_t *obj = sdb_llist_iter_get_next(iter);
 
-               if (sdb_client_send(client, SDB_CONNECTION_QUERY,
+               if (sdb_client_send(input->client, SDB_CONNECTION_QUERY,
                                        (uint32_t)strlen(obj->name), obj->name) <= 0) {
                        sdb_log(SDB_LOG_ERR, "Failed to send command '%s' to server",
                                        obj->name);
@@ -190,7 +190,7 @@ execute_commands(sdb_client_t *client, sdb_llist_t *commands)
                 * but eventually see the reply to the query, which is either DATA or
                 * ERROR. */
                while (42) {
-                       status = sdb_command_print_reply(client);
+                       status = sdb_command_print_reply(input);
                        if (status < 0) {
                                sdb_log(SDB_LOG_ERR, "Failed to read reply from server");
                                break;
@@ -307,6 +307,9 @@ main(int argc, char **argv)
                sdb_input_reset(&input);
                exit(1);
        }
+       input.input = sdb_strbuf_create(2048);
+       sdb_input_init(&input);
+
        canonicalize_ssl_options();
        if (sdb_client_set_ssl_options(input.client, &ssl_options)) {
                sdb_log(SDB_LOG_ERR, "Failed to apply SSL options");
@@ -322,7 +325,9 @@ main(int argc, char **argv)
        }
 
        if (commands) {
-               int status = execute_commands(input.client, commands);
+               int status;
+               input.interactive = 0;
+               status = execute_commands(&input, commands);
                sdb_llist_destroy(commands);
                sdb_input_reset(&input);
                if ((status != SDB_CONNECTION_OK) && (status != SDB_CONNECTION_DATA))
@@ -353,14 +358,12 @@ main(int argc, char **argv)
                }
        }
 
-       input.input = sdb_strbuf_create(2048);
-       sdb_input_init(&input);
        sdb_input_mainloop();
 
        sdb_client_shutdown(input.client, SHUT_WR);
        while (! sdb_client_eof(input.client)) {
                /* wait for remaining data to arrive */
-               sdb_command_print_reply(input.client);
+               sdb_command_print_reply(&input);
        }
 
        if (hist_file[0] != '\0') {