b515c4e10931db0c8e599d9820b991d974a6fb6b
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 <sys/select.h>
55 #include <stdio.h>
56 #include <stdlib.h>
58 #include <string.h>
60 #include <termios.h>
61 #include <unistd.h>
63 #if HAVE_EDITLINE_READLINE_H
64 # include <editline/readline.h>
65 # if HAVE_EDITLINE_HISTORY_H
66 # include <editline/history.h>
67 # endif
68 #elif HAVE_READLINE_READLINE_H
69 # include <readline/readline.h>
70 # if HAVE_READLINE_HISTORY_H
71 # include <readline/history.h>
72 # endif
73 #elif HAVE_READLINE_H
74 # include <readline.h>
75 # if HAVE_HISTORY_H
76 # include <history.h>
77 # endif
78 #endif /* READLINEs */
80 extern int yylex(void);
82 /*
83 * public variables
84 */
86 sdb_input_t *sysdb_input = NULL;
88 /*
89 * private variables
90 */
92 static struct termios orig_term_attrs;
94 /*
95 * private helper functions
96 */
98 static void
99 reset_term_attrs(void)
100 {
101 tcsetattr(STDIN_FILENO, TCSANOW, &orig_term_attrs);
102 } /* reset_term_attrs */
104 static void
105 term_rawmode(void)
106 {
107 struct termios attrs;
109 /* setup terminal to operate in non-canonical mode
110 * and single character input */
111 memset(&orig_term_attrs, 0, sizeof(orig_term_attrs));
112 tcgetattr(STDIN_FILENO, &orig_term_attrs);
113 atexit(reset_term_attrs);
115 memset(&attrs, 0, sizeof(attrs));
116 tcgetattr(STDIN_FILENO, &attrs);
117 attrs.c_lflag &= (tcflag_t)(~ICANON);
118 attrs.c_cc[VMIN] = 1;
119 tcsetattr(STDIN_FILENO, TCSANOW, &attrs);
120 } /* term_rawmode */
122 static void
123 handle_input(char *line)
124 {
125 if (! line) {
126 sysdb_input->eof = 1;
127 return;
128 }
130 sdb_strbuf_append(sysdb_input->input, line);
131 sdb_strbuf_append(sysdb_input->input, "\n");
132 free(line);
134 rl_callback_handler_remove();
135 } /* handle_input */
137 /* wait for a new line of data to be available */
138 static ssize_t
139 input_readline(void)
140 {
141 size_t len;
143 fd_set fds;
144 int client_fd;
146 const char *prompt = "sysdb=> ";
148 if (sysdb_input->query_len)
149 prompt = "sysdb-> ";
151 rl_callback_handler_install(prompt, handle_input);
152 client_fd = sdb_client_sockfd(sysdb_input->client);
154 len = sdb_strbuf_len(sysdb_input->input);
155 while ((sdb_strbuf_len(sysdb_input->input) == len)
156 && (! sysdb_input->eof)) {
157 int n;
159 /* XXX: some versions of libedit don't properly reset the terminal in
160 * rl_callback_read_char(); detect those versions */
161 term_rawmode();
163 FD_ZERO(&fds);
164 FD_SET(STDIN_FILENO, &fds);
165 FD_SET(client_fd, &fds);
167 n = select(client_fd + 1, &fds, NULL, NULL, /* timeout = */ NULL);
168 if (n < 0)
169 return (ssize_t)n;
170 else if (! n)
171 continue;
173 /* handle user input with highest priority */
174 if (FD_ISSET(STDIN_FILENO, &fds)) {
175 rl_callback_read_char();
176 continue;
177 }
179 if (! FD_ISSET(client_fd, &fds))
180 continue;
182 if (sdb_client_eof(sysdb_input->client)) {
183 /* XXX: try to reconnect */
184 printf("\n");
185 sdb_log(SDB_LOG_ERR, "Remote side closed the connection.");
186 /* return EOF */
187 return 0;
188 }
190 /* some response / error message from the server pending */
191 /* XXX: clear current line */
192 printf("\n");
193 sdb_command_print_reply(sysdb_input->client);
194 rl_forced_update_display();
195 }
197 /* new data available */
198 return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
199 } /* input_readline */
201 /*
202 * public API
203 */
205 int
206 sdb_input_init(sdb_input_t *input)
207 {
208 /* register input handler */
209 sysdb_input = input;
211 if (! isatty(STDIN_FILENO))
212 return -1;
214 term_rawmode();
215 return 0;
216 } /* sdb_input_init */
218 int
219 sdb_input_mainloop(void)
220 {
221 yylex();
222 return 0;
223 } /* sdb_input_mainloop */
225 ssize_t
226 sdb_input_readline(char *buf, int *n_chars, size_t max_chars)
227 {
228 const char *data;
229 size_t len;
231 len = sdb_strbuf_len(sysdb_input->input) - sysdb_input->tokenizer_pos;
233 if (! len) {
234 ssize_t n = input_readline();
235 if (n <= 0) {
236 *n_chars = 0; /* YY_NULL */
237 return n;
238 }
239 len += (size_t)n;
240 }
242 len = (len < max_chars) ? len : max_chars;
243 data = sdb_strbuf_string(sysdb_input->input);
244 data += sysdb_input->tokenizer_pos;
245 strncpy(buf, data, len);
247 sysdb_input->tokenizer_pos += len;
248 *n_chars = (int)len;
249 return (ssize_t)len;
250 } /* sdb_input_readline */
252 int
253 sdb_input_exec_query(void)
254 {
255 char *query = sdb_command_exec(sysdb_input);
257 HIST_ENTRY *current_hist;
258 const char *hist_line = NULL;
260 if (! query)
261 return -1;
263 current_hist = current_history();
264 if (current_hist)
265 hist_line = current_hist->line;
267 if (*query != ' ')
268 if ((! hist_line) || strcmp(hist_line, query))
269 add_history(query);
270 free(query);
271 return 0;
272 } /* sdb_input_exec_query */
274 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */