create-tarball: Adapt script to changed directory structure.
[Samba/gbeck.git] / source3 / lib / readline.c
blob254f55c86a14f8dce32e062994cb166970a7684f
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 /****************************************************************************
49 Display the prompt and wait for input. Call callback() regularly
50 ****************************************************************************/
52 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
53 char **(completion_fn)(const char *text, int start, int end))
55 fd_set fds;
56 char *line = NULL;
57 struct timeval timeout;
58 int fd = x_fileno(x_stdin);
59 char *ret;
61 /* Prompt might be NULL in non-interactive mode. */
62 if (prompt) {
63 x_fprintf(x_stdout, "%s", prompt);
64 x_fflush(x_stdout);
67 line = (char *)SMB_MALLOC(BUFSIZ);
68 if (!line) {
69 return NULL;
72 while (1) {
73 timeout.tv_sec = 5;
74 timeout.tv_usec = 0;
76 FD_ZERO(&fds);
77 FD_SET(fd,&fds);
79 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
80 ret = x_fgets(line, BUFSIZ, x_stdin);
81 if (ret == 0) {
82 SAFE_FREE(line);
84 return ret;
86 if (callback) {
87 callback();
92 /****************************************************************************
93 Display the prompt and wait for input. Call callback() regularly.
94 ****************************************************************************/
96 char *smb_readline(const char *prompt, void (*callback)(void),
97 char **(completion_fn)(const char *text, int start, int end))
99 char *ret;
100 bool interactive;
102 interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
103 if (!interactive) {
104 return smb_readline_replacement(NULL, callback, completion_fn);
107 #if HAVE_LIBREADLINE
109 /* Aargh! Readline does bizzare things with the terminal width
110 that mucks up expect(1). Set CLI_NO_READLINE in the environment
111 to force readline not to be used. */
113 if (getenv("CLI_NO_READLINE"))
114 return smb_readline_replacement(prompt, callback, completion_fn);
116 if (completion_fn) {
117 /* The callback prototype has changed slightly between
118 different versions of Readline, so the same function
119 works in all of them to date, but we get compiler
120 warnings in some. */
121 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
124 #if HAVE_DECL_RL_EVENT_HOOK
125 if (callback)
126 rl_event_hook = (Function *)callback;
127 #endif
128 ret = readline(prompt);
129 if (ret && *ret)
130 add_history(ret);
132 #else
133 ret = smb_readline_replacement(prompt, callback, completion_fn);
134 #endif
136 return ret;
139 /****************************************************************************
140 * return line buffer text
141 ****************************************************************************/
142 const char *smb_readline_get_line_buffer(void)
144 #if defined(HAVE_LIBREADLINE)
145 return rl_line_buffer;
146 #else
147 return NULL;
148 #endif
152 /****************************************************************************
153 * set completion append character
154 ***************************************************************************/
155 void smb_readline_ca_char(char c)
157 #if defined(HAVE_LIBREADLINE)
158 rl_completion_append_character = c;
159 #endif
162 /****************************************************************************
163 history
164 ****************************************************************************/
165 int cmd_history(void)
167 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
168 HIST_ENTRY **hlist;
169 int i;
171 hlist = history_list();
173 for (i = 0; hlist && hlist[i]; i++) {
174 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
176 #else
177 DEBUG(0,("no history without readline support\n"));
178 #endif
180 return 0;