This commit was manufactured by cvs2svn to create branch 'SAMBA_3_0'.(This used to...
[samba.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 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_NEW_LIBREADLINE
25 #  define RL_COMPLETION_CAST (rl_completion_func_t *)
26 #else
27 /* This type is missing from libreadline<4.0  (approximately) */
28 #  define RL_COMPLETION_CAST
29 #endif /* HAVE_NEW_LIBREADLINE */
30
31 /****************************************************************************
32  Display the prompt and wait for input. Call callback() regularly
33 ****************************************************************************/
34
35 static char *smb_readline_replacement(char *prompt, void (*callback)(void), 
36                                 char **(completion_fn)(char *text, int start, int end))
37 {
38         fd_set fds;
39         static pstring line;
40         struct timeval timeout;
41         int fd = fileno(stdin);
42         char *ret;
43
44         x_fprintf(dbf, "%s", prompt);
45         x_fflush(dbf);
46
47         while (1) {
48                 timeout.tv_sec = 5;
49                 timeout.tv_usec = 0;
50
51                 FD_ZERO(&fds);
52                 FD_SET(fd,&fds);
53         
54                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
55                         ret = fgets(line, sizeof(line), stdin);
56                         return ret;
57                 }
58                 if (callback)
59                         callback();
60         }
61 }
62
63 /****************************************************************************
64  Display the prompt and wait for input. Call callback() regularly.
65 ****************************************************************************/
66
67 char *smb_readline(char *prompt, void (*callback)(void), 
68                    char **(completion_fn)(char *text, int start, int end))
69 {
70 #if HAVE_LIBREADLINE
71         if (isatty(fileno(stdin))) {
72                 char *ret;
73
74                 /* Aargh!  Readline does bizzare things with the terminal width
75                 that mucks up expect(1).  Set CLI_NO_READLINE in the environment
76                 to force readline not to be used. */
77
78                 if (getenv("CLI_NO_READLINE"))
79                         return smb_readline_replacement(prompt, callback, completion_fn);
80
81                 if (completion_fn) {
82                         /* The callback prototype has changed slightly between
83                         different versions of Readline, so the same function
84                         works in all of them to date, but we get compiler
85                         warnings in some.  */
86                         rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
87                 }
88
89                 if (callback)
90                         rl_event_hook = (Function *)callback;
91                 ret = readline(prompt);
92                 if (ret && *ret)
93                         add_history(ret);
94                 return ret;
95         } else
96 #endif
97         return smb_readline_replacement(prompt, callback, completion_fn);
98 }
99
100 /****************************************************************************
101 history
102 ****************************************************************************/
103 int cmd_history(void)
104 {
105 #if defined(HAVE_LIBREADLINE)
106         HIST_ENTRY **hlist;
107         int i;
108
109         hlist = history_list();
110         
111         for (i = 0; hlist && hlist[i]; i++) {
112                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
113         }
114 #else
115         DEBUG(0,("no history without readline support\n"));
116 #endif
117
118         return 0;
119 }