source: branches/samba-3.0/source/lib/readline.c@ 105

Last change on this file since 105 was 1, checked in by Paul Smedley, 18 years ago

Initial code import

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