Correctly fix smbclient to terminate on eof from server.
[Samba/gebeck_regimport.git] / source3 / lib / readline.c
blobfd57799b578ba8e58bb117882bcec10a1d4fabc8
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 void smb_readline_done(void)
52 smb_rl_done = true;
53 #if HAVE_LIBREADLINE
54 rl_done = 1;
55 #endif
58 /****************************************************************************
59 Display the prompt and wait for input. Call callback() regularly
60 ****************************************************************************/
62 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
63 char **(completion_fn)(const char *text, int start, int end))
65 fd_set fds;
66 char *line = NULL;
67 struct timeval timeout;
68 int fd = x_fileno(x_stdin);
69 char *ret;
71 /* Prompt might be NULL in non-interactive mode. */
72 if (prompt) {
73 x_fprintf(x_stdout, "%s", prompt);
74 x_fflush(x_stdout);
77 line = (char *)SMB_MALLOC(BUFSIZ);
78 if (!line) {
79 return NULL;
82 while (!smb_rl_done) {
83 timeout.tv_sec = 5;
84 timeout.tv_usec = 0;
86 FD_ZERO(&fds);
87 FD_SET(fd,&fds);
89 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
90 ret = x_fgets(line, BUFSIZ, x_stdin);
91 if (ret == 0) {
92 SAFE_FREE(line);
94 return ret;
96 if (callback) {
97 callback();
100 return NULL;
103 /****************************************************************************
104 Display the prompt and wait for input. Call callback() regularly.
105 ****************************************************************************/
107 char *smb_readline(const char *prompt, void (*callback)(void),
108 char **(completion_fn)(const char *text, int start, int end))
110 char *ret;
111 bool interactive;
113 interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
114 if (!interactive) {
115 return smb_readline_replacement(NULL, callback, completion_fn);
118 #if HAVE_LIBREADLINE
120 /* Aargh! Readline does bizzare things with the terminal width
121 that mucks up expect(1). Set CLI_NO_READLINE in the environment
122 to force readline not to be used. */
124 if (getenv("CLI_NO_READLINE"))
125 return smb_readline_replacement(prompt, callback, completion_fn);
127 if (completion_fn) {
128 /* The callback prototype has changed slightly between
129 different versions of Readline, so the same function
130 works in all of them to date, but we get compiler
131 warnings in some. */
132 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
135 #if HAVE_DECL_RL_EVENT_HOOK
136 if (callback)
137 rl_event_hook = (Function *)callback;
138 #endif
139 ret = readline(prompt);
140 if (ret && *ret)
141 add_history(ret);
143 #else
144 ret = smb_readline_replacement(prompt, callback, completion_fn);
145 #endif
147 return ret;
150 /****************************************************************************
151 * return line buffer text
152 ****************************************************************************/
153 const char *smb_readline_get_line_buffer(void)
155 #if defined(HAVE_LIBREADLINE)
156 return rl_line_buffer;
157 #else
158 return NULL;
159 #endif
163 /****************************************************************************
164 * set completion append character
165 ***************************************************************************/
166 void smb_readline_ca_char(char c)
168 #if defined(HAVE_LIBREADLINE)
169 rl_completion_append_character = c;
170 #endif
173 /****************************************************************************
174 history
175 ****************************************************************************/
176 int cmd_history(void)
178 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
179 HIST_ENTRY **hlist;
180 int i;
182 hlist = history_list();
184 for (i = 0; hlist && hlist[i]; i++) {
185 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
187 #else
188 DEBUG(0,("no history without readline support\n"));
189 #endif
191 return 0;