r3323: more warning reductions
[bbaumbach/samba-autobuild/.git] / source4 / lib / cmdline / 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 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
53 static 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         do_debug("%s", prompt);
63
64         while (1) {
65                 timeout.tv_sec = 5;
66                 timeout.tv_usec = 0;
67
68                 FD_ZERO(&fds);
69                 FD_SET(fd,&fds);
70         
71                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
72                         ret = x_fgets(line, sizeof(line), x_stdin);
73                         return ret;
74                 }
75                 if (callback)
76                         callback();
77         }
78 }
79
80 /****************************************************************************
81  Display the prompt and wait for input. Call callback() regularly.
82 ****************************************************************************/
83
84 char *smb_readline(const char *prompt, void (*callback)(void), 
85                    char **(completion_fn)(const char *text, int start, int end))
86 {
87 #if HAVE_LIBREADLINE
88         if (isatty(x_fileno(x_stdin))) {
89                 char *ret;
90
91                 /* Aargh!  Readline does bizzare things with the terminal width
92                 that mucks up expect(1).  Set CLI_NO_READLINE in the environment
93                 to force readline not to be used. */
94
95                 if (getenv("CLI_NO_READLINE"))
96                         return smb_readline_replacement(prompt, callback, completion_fn);
97
98                 if (completion_fn) {
99                         /* The callback prototype has changed slightly between
100                         different versions of Readline, so the same function
101                         works in all of them to date, but we get compiler
102                         warnings in some.  */
103                         rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
104                 }
105
106                 if (callback)
107                         rl_event_hook = (Function *)callback;
108                 ret = readline(prompt);
109                 if (ret && *ret)
110                         add_history(ret);
111                 return ret;
112         } else
113 #endif
114         return smb_readline_replacement(prompt, callback, completion_fn);
115 }
116
117 /****************************************************************************
118  * return line buffer text
119  ****************************************************************************/
120 const char *smb_readline_get_line_buffer(void)
121 {
122 #if defined(HAVE_LIBREADLINE)
123         return rl_line_buffer;
124 #else
125         return NULL;
126 #endif
127 }
128
129 /****************************************************************************
130  * set completion append character
131  ***************************************************************************/
132 void smb_readline_ca_char(char c)
133 {
134 #if defined(HAVE_LIBREADLINE)
135         rl_completion_append_character = c;
136 #endif
137 }
138
139
140 /****************************************************************************
141 history
142 ****************************************************************************/
143 int cmd_history(const char **cmd_ptr)
144 {
145 #if defined(HAVE_LIBREADLINE)
146         HIST_ENTRY **hlist;
147         int i;
148
149         hlist = history_list();
150         
151         for (i = 0; hlist && hlist[i]; i++) {
152                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
153         }
154 #else
155         DEBUG(0,("no history without readline support\n"));
156 #endif
157
158         return 0;
159 }