r15406: Move 'smbreadline' out of libreplace as it doesn't replace functionality
[jelmer/samba4-debian.git] / source / lib / smbreadline / smbreadline.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  Similar to sys_select() but catch EINTR and continue.
30  This is what sys_select() used to do in Samba.
31 ********************************************************************/
32
33 int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
34 {
35         int ret;
36         fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
37         struct timeval tval2, *ptval;
38
39         readfds2 = (readfds ? &readfds_buf : NULL);
40         writefds2 = (writefds ? &writefds_buf : NULL);
41         errorfds2 = (errorfds ? &errorfds_buf : NULL);
42         ptval = (tval ? &tval2 : NULL);
43
44         do {
45                 if (readfds)
46                         readfds_buf = *readfds;
47                 if (writefds)
48                         writefds_buf = *writefds;
49                 if (errorfds)
50                         errorfds_buf = *errorfds;
51                 if (tval)
52                         tval2 = *tval;
53
54                 /* We must use select and not sys_select here. If we use
55                    sys_select we'd lose the fact a signal occurred when sys_select
56                    read a byte from the pipe. Fix from Mark Weaver
57                    <mark-clist@npsl.co.uk>
58                 */
59
60                 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
61         } while (ret == -1 && errno == EINTR);
62
63         if (readfds)
64                 *readfds = readfds_buf;
65         if (writefds)
66                 *writefds = writefds_buf;
67         if (errorfds)
68                 *errorfds = errorfds_buf;
69
70         return ret;
71 }
72
73 /****************************************************************************
74  Display the prompt and wait for input. Call callback() regularly
75 ****************************************************************************/
76
77 static char *smb_readline_replacement(const char *prompt, void (*callback)(void), 
78                                       char **(completion_fn)(const char *text, int start, int end))
79 {
80         fd_set fds;
81         static pstring line;
82         struct timeval timeout;
83         int fd = STDIN_FILENO;
84         char *ret;
85
86         do_debug("%s", prompt);
87
88         while (1) {
89                 timeout.tv_sec = 5;
90                 timeout.tv_usec = 0;
91
92                 FD_ZERO(&fds);
93                 FD_SET(fd,&fds);
94         
95                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
96                         ret = x_fgets(line, sizeof(line), x_stdin);
97                         return ret;
98                 }
99                 if (callback)
100                         callback();
101         }
102 }
103
104 /****************************************************************************
105  Display the prompt and wait for input. Call callback() regularly.
106 ****************************************************************************/
107
108 char *smb_readline(const char *prompt, void (*callback)(void), 
109                    char **(completion_fn)(const char *text, int start, int end))
110 {
111 #if HAVE_LIBREADLINE
112         if (isatty(STDIN_FILENO)) {
113                 char *ret;
114
115                 /* Aargh!  Readline does bizzare things with the terminal width
116                 that mucks up expect(1).  Set CLI_NO_READLINE in the environment
117                 to force readline not to be used. */
118
119                 if (getenv("CLI_NO_READLINE"))
120                         return smb_readline_replacement(prompt, callback, completion_fn);
121
122                 if (completion_fn) {
123                         /* The callback prototype has changed slightly between
124                         different versions of Readline, so the same function
125                         works in all of them to date, but we get compiler
126                         warnings in some.  */
127                         rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
128                 }
129
130                 if (callback)
131                         rl_event_hook = (Function *)callback;
132                 ret = readline(prompt);
133                 if (ret && *ret)
134                         add_history(ret);
135                 return ret;
136         } else
137 #endif
138         return smb_readline_replacement(prompt, callback, completion_fn);
139 }
140
141 /****************************************************************************
142  * return line buffer text
143  ****************************************************************************/
144 const char *smb_readline_get_line_buffer(void)
145 {
146 #if defined(HAVE_LIBREADLINE)
147         return rl_line_buffer;
148 #else
149         return NULL;
150 #endif
151 }
152
153 /****************************************************************************
154  * set completion append character
155  ***************************************************************************/
156 void smb_readline_ca_char(char c)
157 {
158 #if defined(HAVE_LIBREADLINE)
159         rl_completion_append_character = c;
160 #endif
161 }
162
163
164