samba: share select wrappers.
[Samba/gebeck_regimport.git] / source4 / lib / smbreadline / smbreadline.c
blobfa59e5d7d53ae8fb86bc3523817bb252a1db9849
1 /*
2 Unix SMB/CIFS implementation.
3 Samba readline wrapper implementation
4 Copyright (C) Simo Sorce 2001
5 Copyright (C) Andrew Tridgell 2001
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.
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.
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/>.
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"
27 /****************************************************************************
28 Display the prompt and wait for input. Call callback() regularly
29 ****************************************************************************/
31 static char *smb_readline_replacement(const char *prompt, void (*callback)(void),
32 char **(completion_fn)(const char *text, int start, int end))
34 fd_set fds;
35 char *line;
36 struct timeval timeout;
37 int fd = STDIN_FILENO;
38 char *ret;
40 printf("%s", prompt);
41 fflush(stdout);
43 line = (char *)malloc(BUFSIZ);
44 if (!line) {
45 return NULL;
48 while (1) {
49 timeout.tv_sec = 5;
50 timeout.tv_usec = 0;
52 FD_ZERO(&fds);
53 FD_SET(fd,&fds);
55 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
56 ret = x_fgets(line, BUFSIZ, x_stdin);
57 return ret;
59 if (callback)
60 callback();
64 /****************************************************************************
65 Display the prompt and wait for input. Call callback() regularly.
66 ****************************************************************************/
68 char *smb_readline(const char *prompt, void (*callback)(void),
69 char **(completion_fn)(const char *text, int start, int end))
71 #if HAVE_LIBREADLINE
72 if (isatty(STDIN_FILENO)) {
73 char *ret;
75 /* Aargh! Readline does bizzare things with the terminal width
76 that mucks up expect(1). Set CLI_NO_READLINE in the environment
77 to force readline not to be used. */
79 if (getenv("CLI_NO_READLINE"))
80 return smb_readline_replacement(prompt, callback, completion_fn);
82 if (completion_fn) {
83 /* The callback prototype has changed slightly between
84 different versions of Readline, so the same function
85 works in all of them to date, but we get compiler
86 warnings in some. */
87 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
90 #if HAVE_DECL_RL_EVENT_HOOK
91 if (callback)
92 rl_event_hook = (Function *)callback;
93 #endif
94 ret = readline(prompt);
95 if (ret && *ret)
96 add_history(ret);
97 return ret;
98 } else
99 #endif
100 return smb_readline_replacement(prompt, callback, completion_fn);
103 /****************************************************************************
104 * return line buffer text
105 ****************************************************************************/
106 const char *smb_readline_get_line_buffer(void)
108 #if defined(HAVE_LIBREADLINE)
109 return rl_line_buffer;
110 #else
111 return NULL;
112 #endif
115 /****************************************************************************
116 * set completion append character
117 ***************************************************************************/
118 void smb_readline_ca_char(char c)
120 #if defined(HAVE_LIBREADLINE)
121 rl_completion_append_character = c;
122 #endif