OS/X does not have rl_done in readline.h
[Samba.git] / source / lib / readline.c
blobcf7809b05cfb5ccfcc5d500c0396c123c07004b1
1 /*
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/>.
21 #include "includes.h"
23 #ifdef HAVE_LIBREADLINE
24 # ifdef HAVE_READLINE_READLINE_H
25 # include <readline/readline.h>
26 # ifdef HAVE_READLINE_HISTORY_H
27 # include <readline/history.h>
28 # endif
29 # else
30 # ifdef HAVE_READLINE_H
31 # include <readline.h>
32 # ifdef HAVE_HISTORY_H
33 # include <history.h>
34 # endif
35 # else
36 # undef HAVE_LIBREADLINE
37 # endif
38 # endif
39 #endif
41 #ifdef HAVE_NEW_LIBREADLINE
42 # define RL_COMPLETION_CAST (rl_completion_func_t *)
43 #else
44 /* This type is missing from libreadline<4.0 (approximately) */
45 # define RL_COMPLETION_CAST
46 #endif /* HAVE_NEW_LIBREADLINE */
48 static bool smb_rl_done;
50 #if HAVE_LIBREADLINE
52 * * MacOS/X does not have rl_done in readline.h, but
53 * * readline.so has it
54 * */
55 extern int rl_done;
56 #endif
58 void smb_readline_done(void)
60 smb_rl_done = true;
61 #if HAVE_LIBREADLINE
62 rl_done = 1;
63 #endif
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))
73 fd_set fds;
74 char *line = NULL;
75 struct timeval timeout;
76 int fd = x_fileno(x_stdin);
77 char *ret;
79 /* Prompt might be NULL in non-interactive mode. */
80 if (prompt) {
81 x_fprintf(x_stdout, "%s", prompt);
82 x_fflush(x_stdout);
85 line = (char *)SMB_MALLOC(BUFSIZ);
86 if (!line) {
87 return NULL;
90 while (!smb_rl_done) {
91 timeout.tv_sec = 5;
92 timeout.tv_usec = 0;
94 FD_ZERO(&fds);
95 FD_SET(fd,&fds);
97 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
98 ret = x_fgets(line, BUFSIZ, x_stdin);
99 if (ret == 0) {
100 SAFE_FREE(line);
102 return ret;
104 if (callback) {
105 callback();
108 return NULL;
111 /****************************************************************************
112 Display the prompt and wait for input. Call callback() regularly.
113 ****************************************************************************/
115 char *smb_readline(const char *prompt, void (*callback)(void),
116 char **(completion_fn)(const char *text, int start, int end))
118 char *ret;
119 bool interactive;
121 interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
122 if (!interactive) {
123 return smb_readline_replacement(NULL, callback, completion_fn);
126 #if HAVE_LIBREADLINE
128 /* Aargh! Readline does bizzare things with the terminal width
129 that mucks up expect(1). Set CLI_NO_READLINE in the environment
130 to force readline not to be used. */
132 if (getenv("CLI_NO_READLINE"))
133 return smb_readline_replacement(prompt, callback, completion_fn);
135 if (completion_fn) {
136 /* The callback prototype has changed slightly between
137 different versions of Readline, so the same function
138 works in all of them to date, but we get compiler
139 warnings in some. */
140 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
143 #if HAVE_DECL_RL_EVENT_HOOK
144 if (callback)
145 rl_event_hook = (Function *)callback;
146 #endif
147 ret = readline(prompt);
148 if (ret && *ret)
149 add_history(ret);
151 #else
152 ret = smb_readline_replacement(prompt, callback, completion_fn);
153 #endif
155 return ret;
158 /****************************************************************************
159 * return line buffer text
160 ****************************************************************************/
161 const char *smb_readline_get_line_buffer(void)
163 #if defined(HAVE_LIBREADLINE)
164 return rl_line_buffer;
165 #else
166 return NULL;
167 #endif
171 /****************************************************************************
172 * set completion append character
173 ***************************************************************************/
174 void smb_readline_ca_char(char c)
176 #if defined(HAVE_LIBREADLINE)
177 rl_completion_append_character = c;
178 #endif
181 /****************************************************************************
182 history
183 ****************************************************************************/
184 int cmd_history(void)
186 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
187 HIST_ENTRY **hlist;
188 int i;
190 hlist = history_list();
192 for (i = 0; hlist && hlist[i]; i++) {
193 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
195 #else
196 DEBUG(0,("no history without readline support\n"));
197 #endif
199 return 0;