few edits
[Samba.git] / source / lib / readline.c
blob91f9a1b31d53889efe4498f3a7ed185d73fc687b
1 /*
2 Unix SMB/Netbios implementation.
3 Version 3.0
4 Samba readline wrapper implementation
5 Copyright (C) Simo Sorce 2001
6 Copyright (C) Andrew Tridgell 2001
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 #include "includes.h"
25 /****************************************************************************
26 Display the prompt and wait for input. Call callback() regularly
27 ****************************************************************************/
29 char *smb_readline(char *prompt, void (*callback)(void),
30 char **(completion_fn)(const char *text, int start, int end))
32 char *ret;
33 int fd = fileno(stdin);
35 #if HAVE_LIBREADLINE
38 * Current versions of readline on Linux seem to have
39 * problems with EOF on a pipe.
42 if (isatty(fd)) {
43 if (completion_fn)
44 rl_attempted_completion_function = completion_fn;
46 if (callback)
47 rl_event_hook = (Function *)callback;
48 ret = readline(prompt);
49 if (ret && *ret)
50 add_history(ret);
51 return ret;
52 } else
53 #endif
55 fd_set fds;
56 extern FILE *dbf;
57 static pstring line;
58 struct timeval timeout;
60 fprintf(dbf, "%s", prompt);
61 fflush(dbf);
63 while (1) {
64 timeout.tv_sec = 5;
65 timeout.tv_usec = 0;
67 FD_ZERO(&fds);
68 FD_SET(fd,&fds);
70 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
71 ret = fgets(line, sizeof(line), stdin);
72 return ret;
74 if (callback)
75 callback();
80 /****************************************************************************
81 history
82 ****************************************************************************/
83 void cmd_history(void)
85 #if defined(HAVE_LIBREADLINE)
86 HIST_ENTRY **hlist;
87 int i;
89 hlist = history_list();
91 for (i = 0; hlist && hlist[i]; i++) {
92 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
94 #else
95 DEBUG(0,("no history without readline support\n"));
96 #endif