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.
24 #ifdef HAVE_LIBREADLINE
25 # ifdef HAVE_READLINE_READLINE_H
26 # include <readline/readline.h>
27 # ifdef HAVE_READLINE_HISTORY_H
28 # include <readline/history.h>
31 # ifdef HAVE_READLINE_H
32 # include <readline.h>
33 # ifdef HAVE_HISTORY_H
37 # undef HAVE_LIBREADLINE
42 #ifdef HAVE_NEW_LIBREADLINE
43 # define RL_COMPLETION_CAST (rl_completion_func_t *)
45 /* This type is missing from libreadline<4.0 (approximately) */
46 # define RL_COMPLETION_CAST
47 #endif /* HAVE_NEW_LIBREADLINE */
49 /****************************************************************************
50 Display the prompt and wait for input. Call callback() regularly
51 ****************************************************************************/
53 static char *smb_readline_replacement(char *prompt
, void (*callback
)(void),
54 char **(completion_fn
)(const char *text
, int start
, int end
))
58 struct timeval timeout
;
59 int fd
= x_fileno(x_stdin
);
62 x_fprintf(dbf
, "%s", prompt
);
72 if (sys_select_intr(fd
+1,&fds
,NULL
,NULL
,&timeout
) == 1) {
73 ret
= x_fgets(line
, sizeof(line
), x_stdin
);
81 /****************************************************************************
82 Display the prompt and wait for input. Call callback() regularly.
83 ****************************************************************************/
85 char *smb_readline(char *prompt
, void (*callback
)(void),
86 char **(completion_fn
)(const char *text
, int start
, int end
))
89 if (isatty(x_fileno(x_stdin
))) {
92 /* Aargh! Readline does bizzare things with the terminal width
93 that mucks up expect(1). Set CLI_NO_READLINE in the environment
94 to force readline not to be used. */
96 if (getenv("CLI_NO_READLINE"))
97 return smb_readline_replacement(prompt
, callback
, completion_fn
);
100 /* The callback prototype has changed slightly between
101 different versions of Readline, so the same function
102 works in all of them to date, but we get compiler
104 rl_attempted_completion_function
= RL_COMPLETION_CAST completion_fn
;
108 rl_event_hook
= (Function
*)callback
;
109 ret
= readline(prompt
);
115 return smb_readline_replacement(prompt
, callback
, completion_fn
);
118 /****************************************************************************
119 * return line buffer text
120 ****************************************************************************/
121 const char *smb_readline_get_line_buffer(void)
123 #if defined(HAVE_LIBREADLINE)
124 return rl_line_buffer
;
131 /****************************************************************************
132 * set completion append character
133 ***************************************************************************/
134 void smb_readline_ca_char(char c
)
136 #if defined(HAVE_LIBREADLINE)
137 rl_completion_append_character
= c
;
141 /****************************************************************************
143 ****************************************************************************/
144 int cmd_history(void)
146 #if defined(HAVE_LIBREADLINE)
150 hlist
= history_list();
152 for (i
= 0; hlist
&& hlist
[i
]; i
++) {
153 DEBUG(0, ("%d: %s\n", i
, hlist
[i
]->line
));
156 DEBUG(0,("no history without readline support\n"));