much simpler readline code
authorAndrew Tridgell <tridge@samba.org>
Sun, 18 Mar 2001 23:41:53 +0000 (23:41 +0000)
committerAndrew Tridgell <tridge@samba.org>
Sun, 18 Mar 2001 23:41:53 +0000 (23:41 +0000)
should work with readline 2.x

source/client/client.c
source/include/includes.h
source/include/proto.h
source/include/smb_readline.h [deleted file]
source/lib/readline.c

index 082594dbf25e76eb5693256c84c6c925538c46db..83e1c696a4d0e66cb6c6e627354e4e6bbaff96b7 100644 (file)
@@ -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 ();
 }
 
 
index f2c185e0863bbfcda9b043b900710de667d40dfa..f81a5e33624355ba726bb59781754d8aca314f59 100644 (file)
@@ -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 */
index 2abbada5a7c72b2fec98efe37ddaae405d2de87c..d2d96c4c60bd9526e07aa2618c5c5a511fd147f4 100644 (file)
@@ -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/source/include/smb_readline.h b/source/include/smb_readline.h
deleted file mode 100644 (file)
index 1b46c5f..0000000
+++ /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 <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
-
-#define RL_NO_EVENTS 0
-#define RL_GOT_LINE 1
-#define RL_GOT_EOF 2
-#define RL_ERROR -1
-
-
-
index d031fa2fb49683de5a71f8e0a33d0737f2550c82..f0d8b1267f1620b930534ec219f5e47bb3c6b33e 100644 (file)
@@ -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
 
 #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
 }
-