Code

sysdb: Hide implementation details in the "input" module.
[sysdb.git] / src / tools / sysdb / input.c
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/strbuf.h"
52 #include <sys/select.h>
54 #include <stdio.h>
55 #include <stdlib.h>
57 #include <string.h>
59 #include <termios.h>
60 #include <unistd.h>
62 #if HAVE_EDITLINE_READLINE_H
63 #       include <editline/readline.h>
64 #       if HAVE_EDITLINE_HISTORY_H
65 #               include <editline/history.h>
66 #       endif
67 #elif HAVE_READLINE_READLINE_H
68 #       include <readline/readline.h>
69 #       if HAVE_READLINE_HISTORY_H
70 #               include <readline/history.h>
71 #       endif
72 #elif HAVE_READLINE_H
73 #       include <readline.h>
74 #       if HAVE_HISTORY_H
75 #               include <history.h>
76 #       endif
77 #endif /* READLINEs */
79 extern int yylex(void);
81 /*
82  * public variables
83  */
85 sdb_input_t *sysdb_input = NULL;
87 /*
88  * private variables
89  */
91 static struct termios orig_term_attrs;
93 /*
94  * private helper functions
95  */
97 static void
98 reset_term_attrs(void)
99 {
100         tcsetattr(STDIN_FILENO, TCSANOW, &orig_term_attrs);
101 } /* reset_term_attrs */
103 static void
104 term_rawmode(void)
106         struct termios attrs;
108         /* setup terminal to operate in non-canonical mode
109          * and single character input */
110         memset(&orig_term_attrs, 0, sizeof(orig_term_attrs));
111         tcgetattr(STDIN_FILENO, &orig_term_attrs);
112         atexit(reset_term_attrs);
114         memset(&attrs, 0, sizeof(attrs));
115         tcgetattr(STDIN_FILENO, &attrs);
116         attrs.c_lflag &= (tcflag_t)(~ICANON);
117         attrs.c_cc[VMIN] = 1;
118         tcsetattr(STDIN_FILENO, TCSANOW, &attrs);
119 } /* term_rawmode */
121 static void
122 handle_input(char *line)
124         if (! line) {
125                 sysdb_input->eof = 1;
126                 return;
127         }
129         sdb_strbuf_append(sysdb_input->input, line);
130         sdb_strbuf_append(sysdb_input->input, "\n");
131         free(line);
133         rl_callback_handler_remove();
134 } /* handle_input */
136 /* wait for a new line of data to be available */
137 static ssize_t
138 input_readline(void)
140         size_t len;
142         fd_set fds;
143         int client_fd;
145         const char *prompt = "sysdb=> ";
147         if (sysdb_input->query_len)
148                 prompt = "sysdb-> ";
150         rl_callback_handler_install(prompt, handle_input);
151         client_fd = sdb_client_sockfd(sysdb_input->client);
153         len = sdb_strbuf_len(sysdb_input->input);
154         while ((sdb_strbuf_len(sysdb_input->input) == len)
155                         && (! sysdb_input->eof)) {
156                 int n;
158                 /* XXX: some versions of libedit don't properly reset the terminal in
159                  * rl_callback_read_char(); detect those versions */
160                 term_rawmode();
162                 FD_ZERO(&fds);
163                 FD_SET(STDIN_FILENO, &fds);
164                 FD_SET(client_fd, &fds);
166                 n = select(client_fd + 1, &fds, NULL, NULL, /* timeout = */ NULL);
167                 if (n < 0)
168                         return (ssize_t)n;
169                 else if (! n)
170                         continue;
172                 /* handle user input with highest priority */
173                 if (FD_ISSET(STDIN_FILENO, &fds)) {
174                         rl_callback_read_char();
175                         continue;
176                 }
178                 if (! FD_ISSET(client_fd, &fds))
179                         continue;
181                 /* some response / error message from the server pending */
182                 /* XXX: clear current line */
183                 printf("\n");
184                 sdb_command_print_reply(sysdb_input);
185                 rl_forced_update_display();
186         }
188         /* new data available */
189         return (ssize_t)(sdb_strbuf_len(sysdb_input->input) - len);
190 } /* input_readline */
192 /*
193  * public API
194  */
196 int
197 sdb_input_init(sdb_input_t *input)
199         /* register input handler */
200         sysdb_input = input;
202         if (! isatty(STDIN_FILENO))
203                 return -1;
205         term_rawmode();
206         return 0;
207 } /* sdb_input_init */
209 int
210 sdb_input_mainloop(void)
212         yylex();
213         return 0;
214 } /* sdb_input_mainloop */
216 ssize_t
217 sdb_input_readline(char *buf, int *n_chars, size_t max_chars)
219         const char *data;
220         size_t len;
222         len = sdb_strbuf_len(sysdb_input->input) - sysdb_input->tokenizer_pos;
224         if (! len) {
225                 ssize_t n = input_readline();
226                 if (n <= 0) {
227                         *n_chars = 0; /* YY_NULL */
228                         return n;
229                 }
230                 len += (size_t)n;
231         }
233         len = (len < max_chars) ? len : max_chars;
234         data = sdb_strbuf_string(sysdb_input->input);
235         data += sysdb_input->tokenizer_pos;
236         strncpy(buf, data, len);
238         sysdb_input->tokenizer_pos += len;
239         *n_chars = (int)len;
240         return (ssize_t)len;
241 } /* sdb_input_readline */
243 int
244 sdb_input_exec_query(void)
246         char *query = sdb_command_exec(sysdb_input);
248         HIST_ENTRY *current_hist;
249         const char *hist_line = NULL;
251         if (! query)
252                 return -1;
254         current_hist = current_history();
255         if (current_hist)
256                 hist_line = current_hist->line;
258         if (*query != ' ')
259                 if ((! hist_line) || strcmp(hist_line, query))
260                         add_history(query);
261         free(query);
262         return 0;
263 } /* sdb_input_exec_query */
265 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */