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/>.
22 #include "system/filesys.h"
23 #include "system/select.h"
24 #include "system/readline.h"
25 #include "lib/smbreadline/smbreadline.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);
87 line = (char *)malloc(BUFSIZ);
99 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
100 ret = x_fgets(line, BUFSIZ, x_stdin);
108 /****************************************************************************
109 Display the prompt and wait for input. Call callback() regularly.
110 ****************************************************************************/
112 char *smb_readline(const char *prompt, void (*callback)(void),
113 char **(completion_fn)(const char *text, int start, int end))
116 if (isatty(STDIN_FILENO)) {
119 /* Aargh! Readline does bizzare things with the terminal width
120 that mucks up expect(1). Set CLI_NO_READLINE in the environment
121 to force readline not to be used. */
123 if (getenv("CLI_NO_READLINE"))
124 return smb_readline_replacement(prompt, callback, completion_fn);
127 /* The callback prototype has changed slightly between
128 different versions of Readline, so the same function
129 works in all of them to date, but we get compiler
131 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
134 #if HAVE_DECL_RL_EVENT_HOOK
136 rl_event_hook = (Function *)callback;
138 ret = readline(prompt);
144 return smb_readline_replacement(prompt, callback, completion_fn);
147 /****************************************************************************
148 * return line buffer text
149 ****************************************************************************/
150 const char *smb_readline_get_line_buffer(void)
152 #if defined(HAVE_LIBREADLINE)
153 return rl_line_buffer;
159 /****************************************************************************
160 * set completion append character
161 ***************************************************************************/
162 void smb_readline_ca_char(char c)
164 #if defined(HAVE_LIBREADLINE)
165 rl_completion_append_character = c;