summaryrefslogtreecommitdiff
path: root/source3/lib/readline.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2001-03-18 23:41:53 +0000
committerAndrew Tridgell <tridge@samba.org>2001-03-18 23:41:53 +0000
commitbc25293f96fb559b875d03e6ddbd9079b4af9dff (patch)
treedf695c86979cc74c6311967e68e256f5fb2fc5f5 /source3/lib/readline.c
parentcf313f6232e02577b0d79cc90e74cf8f20a81896 (diff)
downloadsamba-bc25293f96fb559b875d03e6ddbd9079b4af9dff.tar.gz
samba-bc25293f96fb559b875d03e6ddbd9079b4af9dff.tar.bz2
samba-bc25293f96fb559b875d03e6ddbd9079b4af9dff.zip
much simpler readline code
should work with readline 2.x (This used to be commit 7940b6b0cf614ac72266d9e600220c9a9dbd2a43)
Diffstat (limited to 'source3/lib/readline.c')
-rw-r--r--source3/lib/readline.c117
1 files changed, 44 insertions, 73 deletions
diff --git a/source3/lib/readline.c b/source3/lib/readline.c
index d031fa2fb4..f0d8b1267f 100644
--- a/source3/lib/readline.c
+++ b/source3/lib/readline.c
@@ -2,7 +2,7 @@
Unix SMB/Netbios implementation.
Version 3.0
Samba readline wrapper implementation
- Copyright (C) Simo Sorce 2001,
+ Copyright (C) Simo Sorce 2001
Copyright (C) Andrew Tridgell 2001
This program is free software; you can redistribute it and/or modify
@@ -22,87 +22,59 @@
#include "includes.h"
-/* user input through readline callback */
-static char *command_line;
-static int *readline_event;
-
-/****************************************************************************
-samba readline callback function
-****************************************************************************/
-static int smb_rl_callback_handler(char *line_read)
-{
- if (!command_line) return RL_ERROR;
-
- if (line_read)
- {
- pstrcpy(command_line, line_read);
-#if defined(HAVE_LIBREADLINE)
-#if defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H)
- if (strlen(line_read)) add_history(line_read);
- free(line_read);
-#endif
-#endif
- *readline_event = RL_GOT_LINE;
- } else {
- *readline_event = RL_GOT_EOF;
- }
- return 0;
-}
-
-void smb_rl_read_char (void)
-{
#ifdef HAVE_LIBREADLINE
- *readline_event = RL_NO_EVENTS;
- rl_callback_read_char ();
-#else
- pstring line;
- fgets(line, sizeof(line), stdin);
- smb_rl_callback_handler(line);
+# ifdef HAVE_READLINE_READLINE_H
+# include <readline/readline.h>
+# ifdef HAVE_READLINE_HISTORY_H
+# include <readline/history.h>
+# endif
+# else
+# ifdef HAVE_READLINE_H
+# include <readline.h>
+# ifdef HAVE_HISTORY_H
+# include <history.h>
+# endif
+# else
+# undef HAVE_LIBREADLINE
+# endif
+# endif
#endif
-}
/****************************************************************************
-init samba readline
+display the prompt and wait for input. Call callback() regularly
****************************************************************************/
-void init_smb_readline(char *prg_name, char *cline_ptr, int *event_ptr)
-{
- command_line = cline_ptr;
- readline_event = event_ptr;
-
-#ifdef HAVE_LIBREADLINE
- rl_readline_name = prg_name;
- rl_already_prompted = 1;
- rl_callback_handler_install(NULL, (VFunction *)&smb_rl_callback_handler);
-#endif
-}
-
-/****************************************************************************
-display the prompt
-****************************************************************************/
-void smb_readline_prompt(char *prompt)
+char *smb_readline(char *prompt, void (*callback)(void))
{
+ char *ret;
+#if HAVE_LIBREADLINE
+ rl_event_hook = (Function *)callback;
+ ret = readline(prompt);
+ if (ret && *ret) add_history(ret);
+ return ret;
+#else
+ fd_set fds;
extern FILE *dbf;
-
+ static pstring line;
+ struct timeval timeout;
+ int fd = fileno(stdin);
+
fprintf(dbf, "%s", prompt);
fflush(dbf);
-#ifdef HAVE_LIBREADLINE
- rl_callback_handler_remove();
- rl_callback_handler_install(prompt, (VFunction *)&smb_rl_callback_handler);
-#endif
-}
+ while (1) {
+ timeout.tv_sec = 5;
+ timeout.tv_usec = 0;
-/****************************************************************************
-removes readline callback handler
-****************************************************************************/
-void smb_readline_remove_handler(void)
-{
-#ifdef HAVE_LIBREADLINE
- rl_callback_handler_remove ();
+ FD_ZERO(&fds);
+ FD_SET(fd,&fds);
+
+ if (sys_select_intr(fd+1,&fds,&timeout) == 1) {
+ ret = fgets(line, sizeof(line), stdin);
+ return ret;
+ }
+ if (callback) callback();
+ }
#endif
-
- readline_event = NULL;
- command_line = NULL;
}
/****************************************************************************
@@ -110,11 +82,11 @@ history
****************************************************************************/
void cmd_history(void)
{
-#if defined(HAVE_LIBREADLINE) && (defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H))
+#if defined(HAVE_LIBREADLINE)
HIST_ENTRY **hlist;
int i;
- hlist = history_list ();
+ hlist = history_list();
for (i = 0; hlist && hlist[i]; i++) {
DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
@@ -123,4 +95,3 @@ void cmd_history(void)
DEBUG(0,("no history without readline support\n"));
#endif
}
-