source: trunk/server/source3/lib/readline.c@ 463

Last change on this file since 463 was 454, checked in by Silvan Scherrer, 15 years ago

Samba Server 3.5: merged changes from 3.3

File size: 5.8 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 Samba readline wrapper implementation
4 Copyright (C) Simo Sorce 2001
5 Copyright (C) Andrew Tridgell 2001
6
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.
11
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.
16
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/>.
19*/
20
21#include "includes.h"
22
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
40
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 */
47
48static bool smb_rl_done;
49
50#if HAVE_LIBREADLINE
51/*
52 * MacOS/X does not have rl_done in readline.h, but
53 * readline.so has it
54 */
55extern int rl_done;
56#endif
57
58void smb_readline_done(void)
59{
60 smb_rl_done = true;
61#if HAVE_LIBREADLINE
62 rl_done = 1;
63#endif
64}
65
66/****************************************************************************
67 Display the prompt and wait for input. Call callback() regularly
68****************************************************************************/
69
70static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
71 char **(completion_fn)(const char *text, int start, int end))
72{
73 fd_set fds;
74 char *line = NULL;
75 struct timeval timeout;
76 int fd = x_fileno(x_stdin);
77 char *ret;
78
79 /* Prompt might be NULL in non-interactive mode. */
80 if (prompt) {
81 x_fprintf(x_stdout, "%s", prompt);
82 x_fflush(x_stdout);
83 }
84
85 line = (char *)SMB_MALLOC(BUFSIZ);
86 if (!line) {
87 return NULL;
88 }
89
90 while (!smb_rl_done) {
91 timeout.tv_sec = 5;
92 timeout.tv_usec = 0;
93
94 FD_ZERO(&fds);
95 FD_SET(fd,&fds);
96
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);
101 }
102 return ret;
103 }
104#ifdef __OS2__
105#include <conio.h>
106 {
107 char * cp = line;
108 char c;
109 int flag = 1;
110 while (flag && cp < line + sizeof(line) - 1)
111 {
112 c = getch();
113 switch (c)
114 {
115 case 8:
116 {
117 if (cp > line)
118 {
119 cp--;
120 putchar(8);
121 putchar(32);
122 putchar(8);
123 }
124 } break;
125 case 0: break;
126 case 10: // generally should not be
127 case 13:
128 {
129 flag = 0;
130 } break;
131 case 27:
132 {
133 for (; cp > line; cp--)
134 {
135 putchar(8);
136 putchar(32);
137 putchar(8);
138 }
139 } break;
140 default:
141 {
142 *cp++ = c;
143 putchar(c);
144 }
145 }
146 }
147 *cp = 0;
148 printf("\n");
149 return line;
150 }
151#endif /* __OS2__ */
152
153 if (callback) {
154 callback();
155 }
156 }
157 SAFE_FREE(line);
158 return NULL;
159}
160
161/****************************************************************************
162 Display the prompt and wait for input. Call callback() regularly.
163****************************************************************************/
164
165char *smb_readline(const char *prompt, void (*callback)(void),
166 char **(completion_fn)(const char *text, int start, int end))
167{
168 char *ret;
169 bool interactive;
170
171 interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
172 if (!interactive) {
173 return smb_readline_replacement(NULL, callback, completion_fn);
174 }
175
176#if HAVE_LIBREADLINE
177
178 /* Aargh! Readline does bizzare things with the terminal width
179 that mucks up expect(1). Set CLI_NO_READLINE in the environment
180 to force readline not to be used. */
181
182 if (getenv("CLI_NO_READLINE"))
183 return smb_readline_replacement(prompt, callback, completion_fn);
184
185 if (completion_fn) {
186 /* The callback prototype has changed slightly between
187 different versions of Readline, so the same function
188 works in all of them to date, but we get compiler
189 warnings in some. */
190 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
191 }
192
193#if HAVE_DECL_RL_EVENT_HOOK
194 if (callback)
195 rl_event_hook = (Function *)callback;
196#endif
197 ret = readline(prompt);
198 if (ret && *ret)
199 add_history(ret);
200
201#else
202 ret = smb_readline_replacement(prompt, callback, completion_fn);
203#endif
204
205 return ret;
206}
207
208/****************************************************************************
209 * return line buffer text
210 ****************************************************************************/
211const char *smb_readline_get_line_buffer(void)
212{
213#if defined(HAVE_LIBREADLINE)
214 return rl_line_buffer;
215#else
216 return NULL;
217#endif
218}
219
220
221/****************************************************************************
222 * set completion append character
223 ***************************************************************************/
224void smb_readline_ca_char(char c)
225{
226#if defined(HAVE_LIBREADLINE)
227 rl_completion_append_character = c;
228#endif
229}
230
231/****************************************************************************
232history
233****************************************************************************/
234int cmd_history(void)
235{
236#if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
237 HIST_ENTRY **hlist;
238 int i;
239
240 hlist = history_list();
241
242 for (i = 0; hlist && hlist[i]; i++) {
243 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
244 }
245#else
246 DEBUG(0,("no history without readline support\n"));
247#endif
248
249 return 0;
250}
Note: See TracBrowser for help on using the repository browser.