2 Unix SMB/CIFS implementation.
3 Samba readline wrapper implementation
4 Copyright (C) Simo Sorce 2001
5 Copyright (C) Andrew Tridgell 2001
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "system/filesys.h"
24 #include "system/select.h"
25 #include "system/readline.h"
27 /*******************************************************************
28 Similar to sys_select() but catch EINTR and continue.
29 This is what sys_select() used to do in Samba.
30 ********************************************************************/
32 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
35 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
36 struct timeval tval2, *ptval;
38 readfds2 = (readfds ? &readfds_buf : NULL);
39 writefds2 = (writefds ? &writefds_buf : NULL);
40 errorfds2 = (errorfds ? &errorfds_buf : NULL);
41 ptval = (tval ? &tval2 : NULL);
45 readfds_buf = *readfds;
47 writefds_buf = *writefds;
49 errorfds_buf = *errorfds;
53 /* We must use select and not sys_select here. If we use
54 sys_select we'd lose the fact a signal occurred when sys_select
55 read a byte from the pipe. Fix from Mark Weaver
56 <mark-clist@npsl.co.uk>
59 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
60 } while (ret == -1 && errno == EINTR);
63 *readfds = readfds_buf;
65 *writefds = writefds_buf;
67 *errorfds = errorfds_buf;
72 /****************************************************************************
73 Display the prompt and wait for input. Call callback() regularly
74 ****************************************************************************/
76 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
77 char **(completion_fn)(const char *text, int start, int end))
81 struct timeval timeout;
82 int fd = STDIN_FILENO;
85 do_debug("%s", prompt);
94 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
95 ret = x_fgets(line, sizeof(line), x_stdin);
103 /****************************************************************************
104 Display the prompt and wait for input. Call callback() regularly.
105 ****************************************************************************/
107 char *smb_readline(const char *prompt, void (*callback)(void),
108 char **(completion_fn)(const char *text, int start, int end))
111 if (isatty(STDIN_FILENO)) {
114 /* Aargh! Readline does bizzare things with the terminal width
115 that mucks up expect(1). Set CLI_NO_READLINE in the environment
116 to force readline not to be used. */
118 if (getenv("CLI_NO_READLINE"))
119 return smb_readline_replacement(prompt, callback, completion_fn);
122 /* The callback prototype has changed slightly between
123 different versions of Readline, so the same function
124 works in all of them to date, but we get compiler
126 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
129 #if HAVE_DECL_RL_EVENT_HOOK
131 rl_event_hook = (Function *)callback;
133 ret = readline(prompt);
139 return smb_readline_replacement(prompt, callback, completion_fn);
142 /****************************************************************************
143 * return line buffer text
144 ****************************************************************************/
145 const char *smb_readline_get_line_buffer(void)
147 #if defined(HAVE_LIBREADLINE)
148 return rl_line_buffer;
154 /****************************************************************************
155 * set completion append character
156 ***************************************************************************/
157 void smb_readline_ca_char(char c)
159 #if defined(HAVE_LIBREADLINE)
160 rl_completion_append_character = c;