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 "../lib/util/select.h"
23 #include "system/filesys.h"
24 #include "system/select.h"
25 #include "system/readline.h"
26 #include "libcli/smbreadline/smbreadline.h"
30 #ifdef HAVE_LIBREADLINE
31 # ifdef HAVE_READLINE_READLINE_H
32 # include <readline/readline.h>
33 # ifdef HAVE_READLINE_HISTORY_H
34 # include <readline/history.h>
37 # ifdef HAVE_READLINE_H
38 # include <readline.h>
39 # ifdef HAVE_HISTORY_H
43 # undef HAVE_LIBREADLINE
48 static bool smb_rl_done
;
52 * MacOS/X does not have rl_done in readline.h, but
58 void smb_readline_done(void)
66 /****************************************************************************
67 Display the prompt and wait for input. Call callback() regularly
68 ****************************************************************************/
70 static char *smb_readline_replacement(const char *prompt
, void (*callback
)(void),
71 char **(completion_fn
)(const char *text
, int start
, int end
))
74 int fd
= x_fileno(x_stdin
);
77 /* Prompt might be NULL in non-interactive mode. */
79 x_fprintf(x_stdout
, "%s", prompt
);
83 line
= (char *)malloc(BUFSIZ
);
88 while (!smb_rl_done
) {
93 pfd
.events
= POLLIN
|POLLHUP
;
95 if (sys_poll_intr(&pfd
, 1, 5000) == 1) {
96 ret
= x_fgets(line
, BUFSIZ
, x_stdin
);
110 /****************************************************************************
111 Display the prompt and wait for input. Call callback() regularly.
112 ****************************************************************************/
114 char *smb_readline(const char *prompt
, void (*callback
)(void),
115 char **(completion_fn
)(const char *text
, int start
, int end
))
120 interactive
= isatty(x_fileno(x_stdin
)) || getenv("CLI_FORCE_INTERACTIVE");
122 return smb_readline_replacement(NULL
, callback
, completion_fn
);
127 /* Aargh! Readline does bizzare things with the terminal width
128 that mucks up expect(1). Set CLI_NO_READLINE in the environment
129 to force readline not to be used. */
131 if (getenv("CLI_NO_READLINE"))
132 return smb_readline_replacement(prompt
, callback
, completion_fn
);
135 /* The callback prototype has changed slightly between
136 different versions of Readline, so the same function
137 works in all of them to date, but we get compiler
139 rl_attempted_completion_function
= RL_COMPLETION_CAST completion_fn
;
142 #if HAVE_DECL_RL_EVENT_HOOK
144 rl_event_hook
= (rl_hook_func_t
*)callback
;
146 ret
= readline(prompt
);
151 ret
= smb_readline_replacement(prompt
, callback
, completion_fn
);
157 /****************************************************************************
158 * return line buffer text
159 ****************************************************************************/
160 const char *smb_readline_get_line_buffer(void)
162 #if defined(HAVE_LIBREADLINE)
163 return rl_line_buffer
;
170 /****************************************************************************
171 * set completion append character
172 ***************************************************************************/
173 void smb_readline_ca_char(char c
)
175 #if defined(HAVE_LIBREADLINE)
176 rl_completion_append_character
= c
;