From bc25293f96fb559b875d03e6ddbd9079b4af9dff Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Sun, 18 Mar 2001 23:41:53 +0000 Subject: much simpler readline code should work with readline 2.x (This used to be commit 7940b6b0cf614ac72266d9e600220c9a9dbd2a43) --- source3/client/client.c | 95 ++++++++++++++++----------------- source3/include/includes.h | 2 - source3/include/proto.h | 5 +- source3/include/smb_readline.h | 46 ---------------- source3/lib/readline.c | 117 ++++++++++++++++------------------------- 5 files changed, 89 insertions(+), 176 deletions(-) delete mode 100644 source3/include/smb_readline.h diff --git a/source3/client/client.c b/source3/client/client.c index 082594dbf2..83e1c696a4 100644 --- a/source3/client/client.c +++ b/source3/client/client.c @@ -93,10 +93,6 @@ pstring fileselection = ""; extern file_info def_finfo; -/* readline variables */ -static pstring command_line; -static int readline_event; - /* timing globals */ int get_total_size = 0; int get_total_time_ms = 0; @@ -933,7 +929,6 @@ static BOOL do_mkdir(char *name) ****************************************************************************/ static void cmd_quit(void) { - smb_readline_remove_handler(); cli_shutdown(cli); exit(0); } @@ -1719,40 +1714,6 @@ static void cmd_help(void) } } -/**************************************************************************** -wait for keyboard activity, swallowing network packets -****************************************************************************/ -static void wait_keyboard(void) -{ - fd_set fds; - struct timeval timeout; - - while (1) { - FD_ZERO(&fds); - FD_SET(cli->fd,&fds); - FD_SET(fileno(stdin),&fds); - - timeout.tv_sec = 20; - timeout.tv_usec = 0; - sys_select_intr(MAX(cli->fd,fileno(stdin))+1,&fds,&timeout); - - if (FD_ISSET(fileno(stdin),&fds)) - { - smb_rl_read_char(); - if (readline_event != RL_NO_EVENTS) return; - } - - /* We deliberately use receive_smb instead of - client_receive_smb as we want to receive - session keepalives and then drop them here. - */ - if (FD_ISSET(cli->fd,&fds)) - receive_smb(cli->fd,cli->inbuf,0); - - cli_chkpath(cli, "\\"); - } -} - /**************************************************************************** process a -c command string ****************************************************************************/ @@ -1792,6 +1753,43 @@ static void process_command_string(char *cmd) } +/**************************************************************************** +make sure we swallow keepalives during idle time +****************************************************************************/ +static void readline_callback(void) +{ + fd_set fds; + struct timeval timeout; + static time_t last_t; + time_t t; + + t = time(NULL); + + if (t - last_t < 5) return; + + last_t = t; + + again: + FD_ZERO(&fds); + FD_SET(cli->fd,&fds); + + timeout.tv_sec = 0; + timeout.tv_usec = 0; + sys_select_intr(cli->fd+1,&fds,&timeout); + + /* We deliberately use receive_smb instead of + client_receive_smb as we want to receive + session keepalives and then drop them here. + */ + if (FD_ISSET(cli->fd,&fds)) { + receive_smb(cli->fd,cli->inbuf,0); + goto again; + } + + cli_chkpath(cli, "\\"); +} + + /**************************************************************************** process commands on stdin ****************************************************************************/ @@ -1799,30 +1797,26 @@ static void process_stdin(void) { char *ptr; - init_smb_readline("smbclient", command_line, &readline_event); - - while (!feof(stdin)) { + while (1) { fstring tok; fstring prompt; + char *line; int i; /* display a prompt */ slprintf(prompt, sizeof(prompt), "smb: %s> ", cur_dir); - smb_readline_prompt(prompt); - - wait_keyboard(); + line = smb_readline(prompt, readline_callback); - if (readline_event == RL_GOT_EOF) /* got an eof */ - break; + if (!line) break; /* special case - first char is ! */ - if (*command_line == '!') { - system(command_line + 1); + if (*line == '!') { + system(line + 1); continue; } /* and get the first part of the command */ - ptr = command_line; + ptr = line; if (!next_token(&ptr,tok,NULL,sizeof(tok))) continue; if ((i = process_tok(tok)) >= 0) { @@ -1833,7 +1827,6 @@ static void process_stdin(void) DEBUG(0,("%s: command not found\n",tok)); } } - smb_readline_remove_handler (); } diff --git a/source3/include/includes.h b/source3/include/includes.h index f2c185e086..f81a5e3362 100644 --- a/source3/include/includes.h +++ b/source3/include/includes.h @@ -616,8 +616,6 @@ extern int errno; #include "messages.h" #include "util_list.h" -#include "smb_readline.h" /* SSS: samba readline support */ - #ifndef UBI_BINTREE_H #include "ubi_Cache.h" #endif /* UBI_BINTREE_H */ diff --git a/source3/include/proto.h b/source3/include/proto.h index 2abbada5a7..d2d96c4c60 100644 --- a/source3/include/proto.h +++ b/source3/include/proto.h @@ -166,10 +166,7 @@ void pidfile_create(char *name); /*The following definitions come from lib/readline.c */ -void smb_rl_read_char (void); -void init_smb_readline(char *prg_name, char *cline_ptr, int *event_ptr); -void smb_readline_prompt(char *prompt); -void smb_readline_remove_handler(void); +char *smb_readline(char *prompt, void (*callback)(void)); void cmd_history(void); /*The following definitions come from lib/replace.c */ diff --git a/source3/include/smb_readline.h b/source3/include/smb_readline.h deleted file mode 100644 index 1b46c5f6ea..0000000000 --- a/source3/include/smb_readline.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - Unix SMB/Netbios implementation. - Version 3.0. - SMB client - Copyright (C) Simo Sorce 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. -*/ - -#ifdef HAVE_LIBREADLINE -# ifdef HAVE_READLINE_READLINE_H -# include -# ifdef HAVE_READLINE_HISTORY_H -# include -# endif -# else -# ifdef HAVE_READLINE_H -# include -# ifdef HAVE_HISTORY_H -# include -# endif -# else -# undef HAVE_LIBREADLINE -# endif -# endif -#endif - -#define RL_NO_EVENTS 0 -#define RL_GOT_LINE 1 -#define RL_GOT_EOF 2 -#define RL_ERROR -1 - - - 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 +# ifdef HAVE_READLINE_HISTORY_H +# include +# endif +# else +# ifdef HAVE_READLINE_H +# include +# ifdef HAVE_HISTORY_H +# include +# 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 } - -- cgit