summaryrefslogtreecommitdiff
path: root/source3/lib/readline.c
diff options
context:
space:
mode:
authorAndrew Tridgell <tridge@samba.org>2001-03-18 13:24:57 +0000
committerAndrew Tridgell <tridge@samba.org>2001-03-18 13:24:57 +0000
commit9c4927d3624c594ea6647d946540ce286a948805 (patch)
tree26f8e58db0ff576c04b5867c2481b6d89ee69861 /source3/lib/readline.c
parentabb183317342fbfdc75a6858d427e550f8f7123d (diff)
downloadsamba-9c4927d3624c594ea6647d946540ce286a948805.tar.gz
samba-9c4927d3624c594ea6647d946540ce286a948805.tar.bz2
samba-9c4927d3624c594ea6647d946540ce286a948805.zip
much better readline support from Simo Sorce, with some mods from me
to make it a bit simpler (This used to be commit e1487eb2c4626dbe0cc3b17606eda702cedef28b)
Diffstat (limited to 'source3/lib/readline.c')
-rw-r--r--source3/lib/readline.c126
1 files changed, 126 insertions, 0 deletions
diff --git a/source3/lib/readline.c b/source3/lib/readline.c
new file mode 100644
index 0000000000..d031fa2fb4
--- /dev/null
+++ b/source3/lib/readline.c
@@ -0,0 +1,126 @@
+/*
+ Unix SMB/Netbios implementation.
+ Version 3.0
+ Samba readline wrapper implementation
+ Copyright (C) Simo Sorce 2001,
+ Copyright (C) Andrew Tridgell 2001
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#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);
+#endif
+}
+
+/****************************************************************************
+init samba readline
+****************************************************************************/
+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)
+{
+ extern FILE *dbf;
+
+ fprintf(dbf, "%s", prompt);
+ fflush(dbf);
+
+#ifdef HAVE_LIBREADLINE
+ rl_callback_handler_remove();
+ rl_callback_handler_install(prompt, (VFunction *)&smb_rl_callback_handler);
+#endif
+}
+
+/****************************************************************************
+removes readline callback handler
+****************************************************************************/
+void smb_readline_remove_handler(void)
+{
+#ifdef HAVE_LIBREADLINE
+ rl_callback_handler_remove ();
+#endif
+
+ readline_event = NULL;
+ command_line = NULL;
+}
+
+/****************************************************************************
+history
+****************************************************************************/
+void cmd_history(void)
+{
+#if defined(HAVE_LIBREADLINE) && (defined(HAVE_READLINE_HISTORY_H) || defined(HAVE_HISTORY_H))
+ HIST_ENTRY **hlist;
+ int i;
+
+ hlist = history_list ();
+
+ for (i = 0; hlist && hlist[i]; i++) {
+ DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
+ }
+#else
+ DEBUG(0,("no history without readline support\n"));
+#endif
+}
+