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 2 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, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 #include "system/readline.h"
28 /*******************************************************************
29 Similar to sys_select() but catch EINTR and continue.
30 This is what sys_select() used to do in Samba.
31 ********************************************************************/
33 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
36 fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
37 struct timeval tval2, *ptval;
39 readfds2 = (readfds ? &readfds_buf : NULL);
40 writefds2 = (writefds ? &writefds_buf : NULL);
41 errorfds2 = (errorfds ? &errorfds_buf : NULL);
42 ptval = (tval ? &tval2 : NULL);
46 readfds_buf = *readfds;
48 writefds_buf = *writefds;
50 errorfds_buf = *errorfds;
54 /* We must use select and not sys_select here. If we use
55 sys_select we'd lose the fact a signal occurred when sys_select
56 read a byte from the pipe. Fix from Mark Weaver
57 <mark-clist@npsl.co.uk>
60 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
61 } while (ret == -1 && errno == EINTR);
64 *readfds = readfds_buf;
66 *writefds = writefds_buf;
68 *errorfds = errorfds_buf;
73 /****************************************************************************
74 Display the prompt and wait for input. Call callback() regularly
75 ****************************************************************************/
77 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
78 char **(completion_fn)(const char *text, int start, int end))
82 struct timeval timeout;
83 int fd = STDIN_FILENO;
86 do_debug("%s", prompt);
95 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
96 ret = x_fgets(line, sizeof(line), x_stdin);
104 /****************************************************************************
105 Display the prompt and wait for input. Call callback() regularly.
106 ****************************************************************************/
108 char *smb_readline(const char *prompt, void (*callback)(void),
109 char **(completion_fn)(const char *text, int start, int end))
112 if (isatty(STDIN_FILENO)) {
115 /* Aargh! Readline does bizzare things with the terminal width
116 that mucks up expect(1). Set CLI_NO_READLINE in the environment
117 to force readline not to be used. */
119 if (getenv("CLI_NO_READLINE"))
120 return smb_readline_replacement(prompt, callback, completion_fn);
123 /* The callback prototype has changed slightly between
124 different versions of Readline, so the same function
125 works in all of them to date, but we get compiler
127 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
131 rl_event_hook = (Function *)callback;
132 ret = readline(prompt);
138 return smb_readline_replacement(prompt, callback, completion_fn);
141 /****************************************************************************
142 * return line buffer text
143 ****************************************************************************/
144 const char *smb_readline_get_line_buffer(void)
146 #if defined(HAVE_LIBREADLINE)
147 return rl_line_buffer;
153 /****************************************************************************
154 * set completion append character
155 ***************************************************************************/
156 void smb_readline_ca_char(char c)
158 #if defined(HAVE_LIBREADLINE)
159 rl_completion_append_character = c;