diff options
Diffstat (limited to 'libcli/smbreadline')
-rw-r--r-- | libcli/smbreadline/smbreadline.c | 184 | ||||
-rw-r--r-- | libcli/smbreadline/smbreadline.h | 30 | ||||
-rw-r--r-- | libcli/smbreadline/wscript_build | 8 | ||||
-rw-r--r-- | libcli/smbreadline/wscript_configure | 85 |
4 files changed, 307 insertions, 0 deletions
diff --git a/libcli/smbreadline/smbreadline.c b/libcli/smbreadline/smbreadline.c new file mode 100644 index 0000000..0a95c63 --- /dev/null +++ b/libcli/smbreadline/smbreadline.c @@ -0,0 +1,184 @@ +/* + Unix SMB/CIFS implementation. + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#include "includes.h" +#include "../lib/util/select.h" +#include "system/filesys.h" +#include "system/select.h" +#include "system/readline.h" +#include "libcli/smbreadline/smbreadline.h" + +#undef malloc + +#ifdef HAVE_LIBREADLINE +# 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 + +static bool smb_rl_done; + +#ifdef HAVE_LIBREADLINE +/* + * MacOS/X does not have rl_done in readline.h, but + * readline.so has it + */ +extern int rl_done; +#endif + +void smb_readline_done(void) +{ + smb_rl_done = true; +#ifdef HAVE_LIBREADLINE + rl_done = 1; +#endif +} + +/**************************************************************************** + Display the prompt and wait for input. Call callback() regularly +****************************************************************************/ + +static char *smb_readline_replacement(const char *prompt, void (*callback)(void), + char **(completion_fn)(const char *text, int start, int end)) +{ + char *line = NULL; + int fd = fileno(stdin); + char *ret; + + /* Prompt might be NULL in non-interactive mode. */ + if (prompt) { + printf("%s", prompt); + fflush(stdout); + } + + line = (char *)malloc(BUFSIZ); + if (!line) { + return NULL; + } + + while (!smb_rl_done) { + struct pollfd pfd; + + ZERO_STRUCT(pfd); + pfd.fd = fd; + pfd.events = POLLIN|POLLHUP; + + if (sys_poll_intr(&pfd, 1, 5000) == 1) { + ret = fgets(line, BUFSIZ, stdin); + if (ret == 0) { + SAFE_FREE(line); + } + return ret; + } + if (callback) { + callback(); + } + } + SAFE_FREE(line); + return NULL; +} + +/**************************************************************************** + Display the prompt and wait for input. Call callback() regularly. +****************************************************************************/ + +char *smb_readline(const char *prompt, void (*callback)(void), + char **(completion_fn)(const char *text, int start, int end)) +{ + char *ret; + bool interactive; + + interactive = isatty(fileno(stdin)) || getenv("CLI_FORCE_INTERACTIVE"); + if (!interactive) { + return smb_readline_replacement(NULL, callback, completion_fn); + } + +#ifdef HAVE_LIBREADLINE + + /* Aargh! Readline does bizarre things with the terminal width + that mucks up expect(1). Set CLI_NO_READLINE in the environment + to force readline not to be used. */ + + if (getenv("CLI_NO_READLINE")) + return smb_readline_replacement(prompt, callback, completion_fn); + + if (completion_fn) { + /* The callback prototype has changed slightly between + different versions of Readline, so the same function + works in all of them to date, but we get compiler + warnings in some. */ + rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn; + + /* + * We only want sensible characters as the word-break chars + * for the most part. This allows us to tab through a path. + */ + rl_basic_word_break_characters = " \t\n"; + } + +#ifdef HAVE_DECL_RL_EVENT_HOOK + if (callback) + rl_event_hook = (rl_hook_func_t *)callback; +#endif + ret = readline(prompt); + if (ret && *ret) + add_history(ret); + +#else + ret = smb_readline_replacement(prompt, callback, completion_fn); +#endif + + return ret; +} + +/**************************************************************************** + * return line buffer text + ****************************************************************************/ +const char *smb_readline_get_line_buffer(void) +{ +#if defined(HAVE_LIBREADLINE) + return rl_line_buffer; +#else + return NULL; +#endif +} + + +/**************************************************************************** + * set completion append character + ***************************************************************************/ +void smb_readline_ca_char(char c) +{ +#if defined(HAVE_LIBREADLINE) + rl_completion_append_character = c; +#endif +} diff --git a/libcli/smbreadline/smbreadline.h b/libcli/smbreadline/smbreadline.h new file mode 100644 index 0000000..9adc4b3 --- /dev/null +++ b/libcli/smbreadline/smbreadline.h @@ -0,0 +1,30 @@ +/* + Unix SMB/CIFS implementation. + 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 3 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, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef __SMBREADLINE_H__ +#define __SMBREADLINE_H__ + +char *smb_readline(const char *prompt, void (*callback)(void), + char **(completion_fn)(const char *text, int start, int end)); +const char *smb_readline_get_line_buffer(void); +void smb_readline_ca_char(char c); +void smb_readline_done(void); + +#endif /* __SMBREADLINE_H__ */ diff --git a/libcli/smbreadline/wscript_build b/libcli/smbreadline/wscript_build new file mode 100644 index 0000000..17699ea --- /dev/null +++ b/libcli/smbreadline/wscript_build @@ -0,0 +1,8 @@ +#!/usr/bin/env python + + +termlib=bld.env.READLINE_TERMLIB or '' + +bld.SAMBA_SUBSYSTEM('SMBREADLINE', + source='smbreadline.c', + deps=termlib + ' readline talloc') diff --git a/libcli/smbreadline/wscript_configure b/libcli/smbreadline/wscript_configure new file mode 100644 index 0000000..912ff53 --- /dev/null +++ b/libcli/smbreadline/wscript_configure @@ -0,0 +1,85 @@ +#!/usr/bin/env python + + +conf.CHECK_HEADERS('readline.h history.h readline/readline.h readline/history.h') +for termlib in ['ncurses', 'curses', 'termcap', 'terminfo', 'termlib', 'tinfo']: + if conf.CHECK_FUNCS_IN('tgetent', termlib): + conf.env['READLINE_TERMLIB'] = termlib + break + +# +# Check if we need to work around readline/readline.h +# deprecated declarations +# +if conf.CONFIG_SET('HAVE_READLINE_READLINE_H'): + if not conf.CHECK_CODE(''' + #include <readline/readline.h> + int main() {return 0;} + ''', + define='HAVE_WORKING_READLINE_READLINE_WITH_STRICT_PROTO', + cflags=conf.env['WERROR_CFLAGS'] + + ['-Wstrict-prototypes', + '-Werror=strict-prototypes'], + msg='for compiling <readline/readline.h> with strict prototypes', + addmain=False): + conf.CHECK_CODE(''' + #define _FUNCTION_DEF + #include <readline/readline.h> + int main() {return 0;} + ''', + cflags=conf.env['WERROR_CFLAGS'] + + ['-Wstrict-prototypes', + '-Werror=strict-prototypes'], + msg='for workaround to <readline/readline.h> strict prototypes issue', + define='HAVE_READLINE_READLINE_WORKAROUND', + addmain=False) + +conf.CHECK_CODE(''' +#ifdef HAVE_READLINE_READLINE_H +# ifdef HAVE_READLINE_READLINE_WORKAROUND +# define _FUNCTION_DEF +# endif +# 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 +# endif +#endif +int main(void) {rl_completion_t f; return 0;} +''', +'HAVE_RL_COMPLETION_FUNC_T', execute=False, addmain=False, +msg='Checking for rl_completion_t') + +conf.CHECK_CODE(''' +#ifdef HAVE_READLINE_READLINE_H +# ifdef HAVE_READLINE_READLINE_WORKAROUND +# define _FUNCTION_DEF +# endif +# 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 +# endif +#endif +int main(void) {CPPFunction f; return 0;} +''', +'HAVE_CPPFUNCTION', execute=False, addmain=False, +msg='Checking for CPPFunction') + +if conf.CHECK_FUNCS_IN('rl_completion_matches', 'readline'): + conf.DEFINE('HAVE_NEW_LIBREADLINE', 1) + +if conf.CHECK_FUNCS_IN('history_list', 'readline'): + conf.DEFINE('HAVE_HISTORY_LIST', 1) |