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 "system/filesys.h"
|
---|
23 | #include "system/select.h"
|
---|
24 | #include "system/readline.h"
|
---|
25 | #include "lib/smbreadline/smbreadline.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 | static 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 | char *line;
|
---|
81 | struct timeval timeout;
|
---|
82 | int fd = STDIN_FILENO;
|
---|
83 | char *ret;
|
---|
84 |
|
---|
85 | printf("%s", prompt);
|
---|
86 | fflush(stdout);
|
---|
87 |
|
---|
88 | line = (char *)malloc(BUFSIZ);
|
---|
89 | if (!line) {
|
---|
90 | return NULL;
|
---|
91 | }
|
---|
92 |
|
---|
93 | while (1) {
|
---|
94 | timeout.tv_sec = 5;
|
---|
95 | timeout.tv_usec = 0;
|
---|
96 |
|
---|
97 | FD_ZERO(&fds);
|
---|
98 | FD_SET(fd,&fds);
|
---|
99 |
|
---|
100 | if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
|
---|
101 | ret = x_fgets(line, BUFSIZ, x_stdin);
|
---|
102 | return ret;
|
---|
103 | }
|
---|
104 | if (callback)
|
---|
105 | callback();
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /****************************************************************************
|
---|
110 | Display the prompt and wait for input. Call callback() regularly.
|
---|
111 | ****************************************************************************/
|
---|
112 |
|
---|
113 | char *smb_readline(const char *prompt, void (*callback)(void),
|
---|
114 | char **(completion_fn)(const char *text, int start, int end))
|
---|
115 | {
|
---|
116 | #if HAVE_LIBREADLINE
|
---|
117 | if (isatty(STDIN_FILENO)) {
|
---|
118 | char *ret;
|
---|
119 |
|
---|
120 | /* Aargh! Readline does bizzare things with the terminal width
|
---|
121 | that mucks up expect(1). Set CLI_NO_READLINE in the environment
|
---|
122 | to force readline not to be used. */
|
---|
123 |
|
---|
124 | if (getenv("CLI_NO_READLINE"))
|
---|
125 | return smb_readline_replacement(prompt, callback, completion_fn);
|
---|
126 |
|
---|
127 | if (completion_fn) {
|
---|
128 | /* The callback prototype has changed slightly between
|
---|
129 | different versions of Readline, so the same function
|
---|
130 | works in all of them to date, but we get compiler
|
---|
131 | warnings in some. */
|
---|
132 | rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
|
---|
133 | }
|
---|
134 |
|
---|
135 | #if HAVE_DECL_RL_EVENT_HOOK
|
---|
136 | if (callback)
|
---|
137 | rl_event_hook = (Function *)callback;
|
---|
138 | #endif
|
---|
139 | ret = readline(prompt);
|
---|
140 | if (ret && *ret)
|
---|
141 | add_history(ret);
|
---|
142 | return ret;
|
---|
143 | } else
|
---|
144 | #endif
|
---|
145 | return smb_readline_replacement(prompt, callback, completion_fn);
|
---|
146 | }
|
---|
147 |
|
---|
148 | /****************************************************************************
|
---|
149 | * return line buffer text
|
---|
150 | ****************************************************************************/
|
---|
151 | const char *smb_readline_get_line_buffer(void)
|
---|
152 | {
|
---|
153 | #if defined(HAVE_LIBREADLINE)
|
---|
154 | return rl_line_buffer;
|
---|
155 | #else
|
---|
156 | return NULL;
|
---|
157 | #endif
|
---|
158 | }
|
---|
159 |
|
---|
160 | /****************************************************************************
|
---|
161 | * set completion append character
|
---|
162 | ***************************************************************************/
|
---|
163 | void smb_readline_ca_char(char c)
|
---|
164 | {
|
---|
165 | #if defined(HAVE_LIBREADLINE)
|
---|
166 | rl_completion_append_character = c;
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 |
|
---|
171 |
|
---|