Code

sysdb: Clear the current command from the buffer after error.
[sysdb.git] / src / tools / sysdb / command.c
1 /*
2  * SysDB - src/tools/sysdb/command.c
3  * Copyright (C) 2014 Sebastian 'tokkee' Harl <sh@tokkee.org>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
19  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
28 #if HAVE_CONFIG_H
29 #       include "config.h"
30 #endif /* HAVE_CONFIG_H */
32 #include "sysdb.h"
34 #include "tools/sysdb/command.h"
35 #include "tools/sysdb/input.h"
37 #include "frontend/proto.h"
38 #include "utils/error.h"
39 #include "utils/proto.h"
40 #include "utils/strbuf.h"
42 #include <errno.h>
44 #include <assert.h>
45 #include <ctype.h>
46 #include <stdlib.h>
47 #include <string.h>
49 static void
50 log_printer(sdb_strbuf_t *buf)
51 {
52         uint32_t prio = sdb_proto_get_int(buf, 0);
54         if (prio == UINT32_MAX) {
55                 printf("ERROR: Received a LOG message with invalid "
56                                 "or missing priority\n");
57                 return;
58         }
59         sdb_strbuf_skip(buf, 0, sizeof(prio));
61         printf("%s: %s\n", SDB_LOG_PRIO_TO_STRING((int)prio),
62                         sdb_strbuf_string(buf));
63 } /* log_printer */
65 static void
66 data_printer(sdb_strbuf_t *buf)
67 {
68         size_t len = sdb_strbuf_len(buf);
70         if ((! len) || (len == sizeof(uint32_t))) {
71                 /* empty command or empty reply */
72                 return;
73         }
74         else if (len < sizeof(uint32_t)) {
75                 printf("ERROR: Received a DATA message with invalid "
76                                 "or missing data-type\n");
77                 return;
78         }
80         /* At the moment, we don't care about the result type. We simply print the
81          * result without further parsing it. */
82         sdb_strbuf_skip(buf, 0, sizeof(uint32_t));
83         printf("%s\n", sdb_strbuf_string(buf));
84 } /* data_printer */
86 static struct {
87         int status;
88         void (*printer)(sdb_strbuf_t *);
89 } response_printers[] = {
90         { SDB_CONNECTION_LOG,  log_printer },
91         { SDB_CONNECTION_DATA, data_printer },
92 };
94 /*
95  * public API
96  */
98 int
99 sdb_command_print_reply(sdb_client_t *client)
101         sdb_strbuf_t *recv_buf;
102         const char *result;
103         uint32_t rcode = 0;
105         int status = 0;
106         size_t i;
108         recv_buf = sdb_strbuf_create(1024);
109         if (! recv_buf)
110                 return -1;
112         if (sdb_client_recv(client, &rcode, recv_buf) < 0)
113                 rcode = UINT32_MAX;
115         if (sdb_client_eof(client)) {
116                 sdb_strbuf_destroy(recv_buf);
117                 return -1;
118         }
120         if (rcode == UINT32_MAX) {
121                 printf("ERROR: ");
122                 status = -1;
123         }
124         else
125                 status = (int)rcode;
127         for (i = 0; i < SDB_STATIC_ARRAY_LEN(response_printers); ++i) {
128                 if (status == response_printers[i].status) {
129                         response_printers[i].printer(recv_buf);
130                         sdb_strbuf_destroy(recv_buf);
131                         return status;
132                 }
133         }
135         result = sdb_strbuf_string(recv_buf);
136         if (result && *result)
137                 printf("%s\n", result);
138         else if (rcode == UINT32_MAX) {
139                 char errbuf[1024];
140                 printf("%s\n", sdb_strerror(errno, errbuf, sizeof(errbuf)));
141         }
143         sdb_strbuf_destroy(recv_buf);
144         return status;
145 } /* sdb_command_print_reply */
147 char *
148 sdb_command_exec(sdb_input_t *input)
150         const char *query;
151         uint32_t query_len;
153         char *data = NULL;
155         query = sdb_strbuf_string(input->input);
156         query_len = (uint32_t)input->query_len;
158         assert(input->query_len <= input->tokenizer_pos);
160         /* removing leading and trailing newlines */
161         while (query_len && (*query == '\n')) {
162                 ++query;
163                 --query_len;
164         }
165         while (query_len && (query[query_len - 1]) == '\n')
166                 --query_len;
168         if (query_len) {
169                 data = strndup(query, query_len);
170                 /* ignore errors; we'll only hide the command from the caller */
172                 sdb_client_send(input->client, SDB_CONNECTION_QUERY, query_len, query);
174                 /* The server will send back *something*, either error/log messages
175                  * and/or the reply to the query. Here, we don't care about what it
176                  * sends back. We'll wait for the first reply and then return to the
177                  * main loop which will handle any subsequent replies, including
178                  * eventually the reply to the query (if it's not the first reply). */
179                 /* TODO: wait for the actual reply instead */
180                 if (sdb_command_print_reply(input->client) < 0) {
181                         if (data)
182                                 free(data);
183                         data = NULL;
184                 }
185         }
187         sdb_strbuf_skip(input->input, 0, input->query_len);
188         input->tokenizer_pos -= input->query_len;
189         input->query_len = 0;
190         return data;
191 } /* sdb_command_exec */
193 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */