r12592: Remove some useless dependencies
[bbaumbach/samba-autobuild/.git] / source4 / lib / replace / 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 #include "pstring.h"
24
25 #include <unistd.h>
26 #include "system/readline.h"
27
28 /****************************************************************************
29  Display the prompt and wait for input. Call callback() regularly
30 ****************************************************************************/
31
32 static char *smb_readline_replacement(const char *prompt, void (*callback)(void), 
33                                       char **(completion_fn)(const char *text, int start, int end))
34 {
35         fd_set fds;
36         static pstring line;
37         struct timeval timeout;
38         int fd = STDIN_FILENO;
39         char *ret;
40
41         do_debug("%s", prompt);
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,NULL,NULL,&timeout) == 1) {
51                         ret = x_fgets(line, sizeof(line), x_stdin);
52                         return ret;
53                 }
54                 if (callback)
55                         callback();
56         }
57 }
58
59 /****************************************************************************
60  Display the prompt and wait for input. Call callback() regularly.
61 ****************************************************************************/
62
63 char *smb_readline(const char *prompt, void (*callback)(void), 
64                    char **(completion_fn)(const char *text, int start, int end))
65 {
66 #if HAVE_LIBREADLINE
67         if (isatty(STDIN_FILENO)) {
68                 char *ret;
69
70                 /* Aargh!  Readline does bizzare things with the terminal width
71                 that mucks up expect(1).  Set CLI_NO_READLINE in the environment
72                 to force readline not to be used. */
73
74                 if (getenv("CLI_NO_READLINE"))
75                         return smb_readline_replacement(prompt, callback, completion_fn);
76
77                 if (completion_fn) {
78                         /* The callback prototype has changed slightly between
79                         different versions of Readline, so the same function
80                         works in all of them to date, but we get compiler
81                         warnings in some.  */
82                         rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
83                 }
84
85                 if (callback)
86                         rl_event_hook = (Function *)callback;
87                 ret = readline(prompt);
88                 if (ret && *ret)
89                         add_history(ret);
90                 return ret;
91         } else
92 #endif
93         return smb_readline_replacement(prompt, callback, completion_fn);
94 }
95
96 /****************************************************************************
97  * return line buffer text
98  ****************************************************************************/
99 const char *smb_readline_get_line_buffer(void)
100 {
101 #if defined(HAVE_LIBREADLINE)
102         return rl_line_buffer;
103 #else
104         return NULL;
105 #endif
106 }
107
108 /****************************************************************************
109  * set completion append character
110  ***************************************************************************/
111 void smb_readline_ca_char(char c)
112 {
113 #if defined(HAVE_LIBREADLINE)
114         rl_completion_append_character = c;
115 #endif
116 }
117
118
119