Oops, Tim says rl_completion_func_t doesn't exist in all versions of
[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 callback prototype has changed slightly between
77                    different versions of Readline, so the same
78                    function works in all of them to date, but we get
79                    compiler warnings in some. */
80                 rl_attempted_completion_function = completion_fn;
81         }
82
83         if (callback) rl_event_hook = (Function *)callback;
84         ret = readline(prompt);
85         if (ret && *ret) add_history(ret);
86         return ret;
87 #else
88         return smb_readline_replacement(prompt, callback, completion_fn);
89 #endif
90 }
91
92 /****************************************************************************
93 history
94 ****************************************************************************/
95 int cmd_history(void)
96 {
97 #if defined(HAVE_LIBREADLINE)
98         HIST_ENTRY **hlist;
99         int i;
100
101         hlist = history_list();
102         
103         for (i = 0; hlist && hlist[i]; i++) {
104                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
105         }
106 #else
107         DEBUG(0,("no history without readline support\n"));
108 #endif
109
110         return 0;
111 }