2a2d1707101404169febb67d0ac643aaa98ec649
1 /*
2 * SysDB - src/tools/sysdb/input.c
3 * Copyright (C) 2013 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 /*
29 * This module implements the core of the command line tool. It handles all
30 * input from the user and the remote server, interacting with the scanner and
31 * command handling as needed.
32 *
33 * The main loop is managed by the flex scanner which parses the user input.
34 * It will call into this module (using sdb_input_readline()) whenever it
35 * needs further input to continue parsing. Whenever it finds a full query
36 * (terminated by a semicolon), it will hand the query back to this module
37 * (using sdb_input_exec_query()) which will then execute it.
38 *
39 * Most of the process life-time will be spend waiting for input. User input
40 * and (asynchronous) server replies are handled at the same time.
41 */
43 #if HAVE_CONFIG_H
44 # include "config.h"
45 #endif /* HAVE_CONFIG_H */
47 #include "tools/sysdb/input.h"
48 #include "tools/sysdb/command.h"
50 #include "utils/error.h"
51 #include "utils/strbuf.h"
53 #include <errno.h>
55 #include <sys/select.h>
57 #include <stdio.h>
58 #include <stdlib.h>
60 #include <string.h>
62 #include <termios.h>
63 #include <unistd.h>
65 #if HAVE_EDITLINE_READLINE_H
66 # include <editline/readline.h>
67 # if HAVE_EDITLINE_HISTORY_H
68 # include <editline/history.h>
69 # endif
70 #elif HAVE_READLINE_READLINE_H
71 # include <readline/readline.h>
72 # if HAVE_READLINE_HISTORY_H
73 # include <readline/history.h>
74 # endif
75 #elif HAVE_READLINE_H
76 # include <readline.h>
77 # if HAVE_HISTORY_H
78 # include <history.h>
79 # endif
80 #endif /* READLINEs */
82 extern int yylex(void);
84 /*
85 * public variables
86 */
88 sdb_input_t *sysdb_input = NULL;
90 /*
91 * private variables
92 */
94 static struct termios orig_term_attrs;
95 static bool have_orig_term_attrs;
97 /*
98 * private helper functions
99 */
101 static void
102 reset_term_attrs(void)
103 {
104 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term_attrs);
105 } /* reset_term_attrs */
107 static void
108 term_rawmode(void)
109 {
110 struct termios attrs;
112 if (! have_orig_term_attrs) {
113 memset(&orig_term_attrs, 0, sizeof(orig_term_attrs));
114 tcgetattr(STDIN_FILENO, &orig_term_attrs);
115 atexit(reset_term_attrs);
116 have_orig_term_attrs = 1;
117 }
119 /* setup terminal to operate in non-canonical mode
120 * and single character input */
121 memset(&attrs, 0, sizeof(attrs));
122 tcgetattr(STDIN_FILENO, &attrs);
123 attrs.c_lflag &= (tcflag_t)(~ICANON);
124 attrs.c_cc[VMIN] = 1;
125 tcsetattr(STDIN_FILENO, TCSANOW, &attrs);
126 } /* term_rawmode */
128 static void
129 handle_input(char *line)
130 {
131 if (! line) {
132 sysdb_input->eof = 1;
133 return;
134 }
136 sdb_strbuf_append(sysdb_input->input, "%s\n", line);
137 free(line);
139 if (sysdb_input->interactive)
140 rl_callback_handler_remove();
141 } /* handle_input */
143 /* wait for a new line of data to be available */
144 static ssize_t
145 input_readline(void)
146 {
147 size_t len;
149 fd_set fds;
150 int client_fd;
152 const char *prompt = "sysdb=> ";
154 len = sdb_strbuf_len(sysdb_input->input);
156 if (! sysdb_input->interactive) {
157 char *line = readline("");
158 handle_input(line);
159 return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
160 }
162 if (sysdb_input->have_input)
163 prompt = " -> ";
164 if (sdb_client_eof(sysdb_input->client))
165 prompt = "!-> ";
167 rl_callback_handler_install(prompt, handle_input);
168 client_fd = sdb_client_sockfd(sysdb_input->client);
169 while ((sdb_strbuf_len(sysdb_input->input) == len)
170 && (! sysdb_input->eof)) {
171 bool connected = !sdb_client_eof(sysdb_input->client);
172 int max_fd, n;
174 /* XXX: some versions of libedit don't properly reset the terminal in
175 * rl_callback_read_char(); detect those versions */
176 term_rawmode();
178 FD_ZERO(&fds);
179 FD_SET(STDIN_FILENO, &fds);
180 max_fd = STDIN_FILENO;
182 if (connected) {
183 FD_SET(client_fd, &fds);
184 if (client_fd > max_fd)
185 max_fd = client_fd;
186 }
188 n = select(max_fd + 1, &fds, NULL, NULL, /* timeout = */ NULL);
189 if (n < 0)
190 return (ssize_t)n;
191 else if (! n)
192 continue;
194 /* handle user input with highest priority */
195 if (FD_ISSET(STDIN_FILENO, &fds)) {
196 rl_callback_read_char();
197 continue;
198 }
200 if ((! connected) || (! FD_ISSET(client_fd, &fds)))
201 continue;
203 /* some response / error message from the server pending */
204 /* XXX: clear current line */
205 printf("\n");
206 sdb_command_print_reply(sysdb_input);
208 if (sdb_client_eof(sysdb_input->client)) {
209 rl_callback_handler_remove();
210 /* XXX */
211 sdb_log(SDB_LOG_ERR, "Remote side closed the connection.");
212 /* return EOF -> restart scanner */
213 return 0;
214 }
215 else
216 rl_forced_update_display();
217 }
219 /* new data available */
220 return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
221 } /* input_readline */
223 /*
224 * public API
225 */
227 int
228 sdb_input_init(sdb_input_t *input)
229 {
230 /* register input handler */
231 sysdb_input = input;
233 input->interactive = isatty(STDIN_FILENO) != 0;
234 errno = 0;
235 if (input->interactive)
236 term_rawmode();
237 return 0;
238 } /* sdb_input_init */
240 void
241 sdb_input_reset(sdb_input_t *input)
242 {
243 sdb_input_t reset = SDB_INPUT_INIT;
245 if (! input)
246 return;
248 if (input->client)
249 sdb_client_destroy(input->client);
250 if (input->user)
251 free(input->user);
252 if (input->input)
253 sdb_strbuf_destroy(input->input);
255 *input = reset;
256 } /* sdb_input_reset */
258 int
259 sdb_input_mainloop(void)
260 {
261 while (! sysdb_input->eof)
262 yylex();
263 return 0;
264 } /* sdb_input_mainloop */
266 ssize_t
267 sdb_input_readline(char *buf, size_t *n_chars, size_t max_chars)
268 {
269 const char *data;
270 size_t len;
272 len = sdb_strbuf_len(sysdb_input->input) - sysdb_input->tokenizer_pos;
274 if (! len) {
275 ssize_t n = input_readline();
276 if (n <= 0) {
277 *n_chars = 0; /* YY_NULL */
278 return n;
279 }
280 len += (size_t)n;
281 }
283 len = (len < max_chars) ? len : max_chars;
284 data = sdb_strbuf_string(sysdb_input->input);
285 data += sysdb_input->tokenizer_pos;
286 strncpy(buf, data, len);
288 sysdb_input->tokenizer_pos += len;
289 *n_chars = (int)len;
290 return (ssize_t)len;
291 } /* sdb_input_readline */
293 int
294 sdb_input_exec_query(void)
295 {
296 char *query = NULL;
298 HIST_ENTRY *hist;
299 const char *prev = NULL;
301 if (! sysdb_input->have_input) {
302 /* empty line */
303 if (sdb_client_eof(sysdb_input->client))
304 sdb_input_reconnect();
305 return 0;
306 }
308 query = sdb_command_exec(sysdb_input);
309 if (! query)
310 return -1;
312 hist = history_get(history_length);
313 if (hist)
314 prev = hist->line;
316 if (*query != ' ')
317 if ((! prev) || strcmp(prev, query))
318 add_history(query);
319 free(query);
320 return 0;
321 } /* sdb_input_exec_query */
323 int
324 sdb_input_reconnect(void)
325 {
326 sdb_client_close(sysdb_input->client);
327 if (sdb_client_connect(sysdb_input->client, sysdb_input->user)) {
328 sdb_log(SDB_LOG_ERR, "Failed to reconnect to SysDBd");
329 return -1;
330 }
331 sdb_log(SDB_LOG_INFO, "Successfully reconnected to SysDBd");
332 sdb_command_print_server_version(sysdb_input);
333 return 0;
334 } /* sdb_input_reconnect */
336 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */