r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / readline.c
blob24d16ea34fbffe67bccd9e92ff06b0727c138402
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"
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>
29 # endif
30 # else
31 # ifdef HAVE_READLINE_H
32 # include <readline.h>
33 # ifdef HAVE_HISTORY_H
34 # include <history.h>
35 # endif
36 # else
37 # undef HAVE_LIBREADLINE
38 # endif
39 # endif
40 #endif
42 #ifdef HAVE_NEW_LIBREADLINE
43 # define RL_COMPLETION_CAST (rl_completion_func_t *)
44 #else
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(const char *prompt, void (*callback)(void),
54 char **(completion_fn)(const char *text, int start, int end))
56 fd_set fds;
57 static pstring line;
58 struct timeval timeout;
59 int fd = x_fileno(x_stdin);
60 char *ret;
62 /* Prompt might be NULL in non-interactive mode. */
63 if (prompt) {
64 x_fprintf(x_stdout, "%s", prompt);
65 x_fflush(x_stdout);
68 while (1) {
69 timeout.tv_sec = 5;
70 timeout.tv_usec = 0;
72 FD_ZERO(&fds);
73 FD_SET(fd,&fds);
75 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
76 ret = x_fgets(line, sizeof(line), x_stdin);
77 return ret;
79 if (callback)
80 callback();
84 /****************************************************************************
85 Display the prompt and wait for input. Call callback() regularly.
86 ****************************************************************************/
88 char *smb_readline(const char *prompt, void (*callback)(void),
89 char **(completion_fn)(const char *text, int start, int end))
91 char *ret;
92 BOOL interactive;
94 interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
95 if (!interactive) {
96 return smb_readline_replacement(NULL, callback, completion_fn);
99 #if HAVE_LIBREADLINE
101 /* Aargh! Readline does bizzare things with the terminal width
102 that mucks up expect(1). Set CLI_NO_READLINE in the environment
103 to force readline not to be used. */
105 if (getenv("CLI_NO_READLINE"))
106 return smb_readline_replacement(prompt, callback, completion_fn);
108 if (completion_fn) {
109 /* The callback prototype has changed slightly between
110 different versions of Readline, so the same function
111 works in all of them to date, but we get compiler
112 warnings in some. */
113 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
116 #if HAVE_DECL_RL_EVENT_HOOK
117 if (callback)
118 rl_event_hook = (Function *)callback;
119 #endif
120 ret = readline(prompt);
121 if (ret && *ret)
122 add_history(ret);
124 #else
125 ret = smb_readline_replacement(prompt, callback, completion_fn);
126 #endif
128 return ret;
131 /****************************************************************************
132 * return line buffer text
133 ****************************************************************************/
134 const char *smb_readline_get_line_buffer(void)
136 #if defined(HAVE_LIBREADLINE)
137 return rl_line_buffer;
138 #else
139 return NULL;
140 #endif
144 /****************************************************************************
145 * set completion append character
146 ***************************************************************************/
147 void smb_readline_ca_char(char c)
149 #if defined(HAVE_LIBREADLINE)
150 rl_completion_append_character = c;
151 #endif
154 /****************************************************************************
155 history
156 ****************************************************************************/
157 int cmd_history(void)
159 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
160 HIST_ENTRY **hlist;
161 int i;
163 hlist = history_list();
165 for (i = 0; hlist && hlist[i]; i++) {
166 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
168 #else
169 DEBUG(0,("no history without readline support\n"));
170 #endif
172 return 0;