Quieten compiler warnings about a callback function prototype that has
[samba.git] / source3 / lib / readline.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    Samba readline wrapper implementation
5    Copyright (C) Simo Sorce 2001
6    Copyright (C) Andrew Tridgell 2001
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24
25
26 /****************************************************************************
27 display the prompt and wait for input. Call callback() regularly
28 ****************************************************************************/
29 static char *smb_readline_replacement(char *prompt, void (*callback)(void), 
30                                       char **(completion_fn)(char *text, 
31                                                              int start, 
32                                                              int end))
33 {
34         fd_set fds;
35         static pstring line;
36         struct timeval timeout;
37         int fd = fileno(stdin);
38         char *ret;
39
40         x_fprintf(dbf, "%s", prompt);
41         x_fflush(dbf);
42
43         while (1) {
44                 timeout.tv_sec = 5;
45                 timeout.tv_usec = 0;
46
47                 FD_ZERO(&fds);
48                 FD_SET(fd,&fds);
49         
50                 if (sys_select_intr(fd+1,&fds,&timeout) == 1) {
51                         ret = fgets(line, sizeof(line), stdin);
52                         return ret;
53                 }
54                 if (callback) callback();
55         }
56 }
57
58 /****************************************************************************
59 display the prompt and wait for input. Call callback() regularly
60 ****************************************************************************/
61 char *smb_readline(char *prompt, void (*callback)(void), 
62                    char **(completion_fn)(char *text, int start, int end))
63 {
64 #if HAVE_LIBREADLINE
65         char *ret;
66
67         /* Aargh!  Readline does bizzare things with the terminal width
68            that mucks up expect(1).  Set CLI_NO_READLINE in the environment
69            to force readline not to be used. */
70
71         if (getenv("CLI_NO_READLINE"))
72                 return smb_readline_replacement(prompt, callback, 
73                                                 completion_fn);
74
75         if (completion_fn) {
76                 /* The cast is here because the callback prototype has
77                    changed slightly between different versions of
78                    Readline.  The same function works in all of them
79                    to date, but we get compiler warnings without the
80                    cast. */
81                 rl_attempted_completion_function =
82                         (rl_completion_func_t *) completion_fn;
83         }
84
85         if (callback) rl_event_hook = (Function *)callback;
86         ret = readline(prompt);
87         if (ret && *ret) add_history(ret);
88         return ret;
89 #else
90         return smb_readline_replacement(prompt, callback, completion_fn);
91 #endif
92 }
93
94 /****************************************************************************
95 history
96 ****************************************************************************/
97 int cmd_history(void)
98 {
99 #if defined(HAVE_LIBREADLINE)
100         HIST_ENTRY **hlist;
101         int i;
102
103         hlist = history_list();
104         
105         for (i = 0; hlist && hlist[i]; i++) {
106                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
107         }
108 #else
109         DEBUG(0,("no history without readline support\n"));
110 #endif
111
112         return 0;
113 }