RIP BOOL. Convert BOOL -> bool. I found a few interesting
[nivanova/samba-autobuild/.git] / source3 / lib / readline.c
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
48 /****************************************************************************
49  Display the prompt and wait for input. Call callback() regularly
50 ****************************************************************************/
51
52 static char *smb_readline_replacement(const char *prompt, void (*callback)(void), 
53                                 char **(completion_fn)(const char *text, int start, int end))
54 {
55         fd_set fds;
56         static pstring line;
57         struct timeval timeout;
58         int fd = x_fileno(x_stdin);
59         char *ret;
60
61         /* Prompt might be NULL in non-interactive mode. */
62         if (prompt) {
63                 x_fprintf(x_stdout, "%s", prompt);
64                 x_fflush(x_stdout);
65         }
66
67         while (1) {
68                 timeout.tv_sec = 5;
69                 timeout.tv_usec = 0;
70
71                 FD_ZERO(&fds);
72                 FD_SET(fd,&fds);
73         
74                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
75                         ret = x_fgets(line, sizeof(line), x_stdin);
76                         return ret;
77                 }
78                 if (callback)
79                         callback();
80         }
81 }
82
83 /****************************************************************************
84  Display the prompt and wait for input. Call callback() regularly.
85 ****************************************************************************/
86
87 char *smb_readline(const char *prompt, void (*callback)(void), 
88                    char **(completion_fn)(const char *text, int start, int end))
89 {
90         char *ret;
91         bool interactive;
92
93         interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
94         if (!interactive) {
95             return smb_readline_replacement(NULL, callback, completion_fn);
96         }
97
98 #if HAVE_LIBREADLINE
99
100         /* Aargh!  Readline does bizzare things with the terminal width
101         that mucks up expect(1).  Set CLI_NO_READLINE in the environment
102         to force readline not to be used. */
103
104         if (getenv("CLI_NO_READLINE"))
105                 return smb_readline_replacement(prompt, callback, completion_fn);
106
107         if (completion_fn) {
108                 /* The callback prototype has changed slightly between
109                 different versions of Readline, so the same function
110                 works in all of them to date, but we get compiler
111                 warnings in some.  */
112                 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
113         }
114
115 #if HAVE_DECL_RL_EVENT_HOOK
116         if (callback)
117                 rl_event_hook = (Function *)callback;
118 #endif
119         ret = readline(prompt);
120         if (ret && *ret)
121                 add_history(ret);
122
123 #else
124         ret = smb_readline_replacement(prompt, callback, completion_fn);
125 #endif
126
127         return ret;
128 }
129
130 /****************************************************************************
131  * return line buffer text
132  ****************************************************************************/
133 const char *smb_readline_get_line_buffer(void)
134 {
135 #if defined(HAVE_LIBREADLINE)
136         return rl_line_buffer;
137 #else
138         return NULL;
139 #endif
140 }
141
142
143 /****************************************************************************
144  * set completion append character
145  ***************************************************************************/
146 void smb_readline_ca_char(char c)
147 {
148 #if defined(HAVE_LIBREADLINE)
149         rl_completion_append_character = c;
150 #endif
151 }
152
153 /****************************************************************************
154 history
155 ****************************************************************************/
156 int cmd_history(void)
157 {
158 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
159         HIST_ENTRY **hlist;
160         int i;
161
162         hlist = history_list();
163         
164         for (i = 0; hlist && hlist[i]; i++) {
165                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
166         }
167 #else
168         DEBUG(0,("no history without readline support\n"));
169 #endif
170
171         return 0;
172 }
173