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