r10245: Get rid of XFILE in a few places.
[Samba.git] / source4 / lib / cmdline / readline.c
blob42810c8697c6099492f083857d6eb1107fb81821
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 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.
22 #include "includes.h"
23 #include "pstring.h"
25 #include <unistd.h>
27 #ifdef HAVE_LIBREADLINE
28 # ifdef HAVE_READLINE_READLINE_H
29 # include <readline/readline.h>
30 # ifdef HAVE_READLINE_HISTORY_H
31 # include <readline/history.h>
32 # endif
33 # else
34 # ifdef HAVE_READLINE_H
35 # include <readline.h>
36 # ifdef HAVE_HISTORY_H
37 # include <history.h>
38 # endif
39 # else
40 # undef HAVE_LIBREADLINE
41 # endif
42 # endif
43 #endif
45 #ifdef HAVE_NEW_LIBREADLINE
46 # define RL_COMPLETION_CAST (rl_completion_func_t *)
47 #else
48 /* This type is missing from libreadline<4.0 (approximately) */
49 # define RL_COMPLETION_CAST
50 #endif /* HAVE_NEW_LIBREADLINE */
52 /****************************************************************************
53 Display the prompt and wait for input. Call callback() regularly
54 ****************************************************************************/
56 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
57 char **(completion_fn)(const char *text, int start, int end))
59 fd_set fds;
60 static pstring line;
61 struct timeval timeout;
62 int fd = STDIN_FILENO;
63 char *ret;
65 do_debug("%s", prompt);
67 while (1) {
68 timeout.tv_sec = 5;
69 timeout.tv_usec = 0;
71 FD_ZERO(&fds);
72 FD_SET(fd,&fds);
74 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
75 ret = x_fgets(line, sizeof(line), x_stdin);
76 return ret;
78 if (callback)
79 callback();
83 /****************************************************************************
84 Display the prompt and wait for input. Call callback() regularly.
85 ****************************************************************************/
87 char *smb_readline(const char *prompt, void (*callback)(void),
88 char **(completion_fn)(const char *text, int start, int end))
90 #if HAVE_LIBREADLINE
91 if (isatty(STDIN_FILENO)) {
92 char *ret;
94 /* Aargh! Readline does bizzare things with the terminal width
95 that mucks up expect(1). Set CLI_NO_READLINE in the environment
96 to force readline not to be used. */
98 if (getenv("CLI_NO_READLINE"))
99 return smb_readline_replacement(prompt, callback, completion_fn);
101 if (completion_fn) {
102 /* The callback prototype has changed slightly between
103 different versions of Readline, so the same function
104 works in all of them to date, but we get compiler
105 warnings in some. */
106 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
109 if (callback)
110 rl_event_hook = (Function *)callback;
111 ret = readline(prompt);
112 if (ret && *ret)
113 add_history(ret);
114 return ret;
115 } else
116 #endif
117 return smb_readline_replacement(prompt, callback, completion_fn);
120 /****************************************************************************
121 * return line buffer text
122 ****************************************************************************/
123 const char *smb_readline_get_line_buffer(void)
125 #if defined(HAVE_LIBREADLINE)
126 return rl_line_buffer;
127 #else
128 return NULL;
129 #endif
132 /****************************************************************************
133 * set completion append character
134 ***************************************************************************/
135 void smb_readline_ca_char(char c)
137 #if defined(HAVE_LIBREADLINE)
138 rl_completion_append_character = c;
139 #endif
143 /****************************************************************************
144 history
145 ****************************************************************************/
146 int cmd_history(const char **cmd_ptr)
148 #if defined(HAVE_LIBREADLINE)
149 HIST_ENTRY **hlist;
150 int i;
152 hlist = history_list();
154 for (i = 0; hlist && hlist[i]; i++) {
155 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
157 #else
158 DEBUG(0,("no history without readline support\n"));
159 #endif
161 return 0;