root/source4/lib/smbreadline/smbreadline.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_select_intr
  2. smb_readline_replacement
  3. smb_readline
  4. smb_readline_get_line_buffer
  5. smb_readline_ca_char

   1 /* 
   2    Unix SMB/CIFS implementation.
   3    Samba readline wrapper implementation
   4    Copyright (C) Simo Sorce 2001
   5    Copyright (C) Andrew Tridgell 2001
   6    
   7    This program is free software; you can redistribute it and/or modify
   8    it under the terms of the GNU General Public License as published by
   9    the Free Software Foundation; either version 3 of the License, or
  10    (at your option) any later version.
  11    
  12    This program is distributed in the hope that it will be useful,
  13    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15    GNU General Public License for more details.
  16    
  17    You should have received a copy of the GNU General Public License
  18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  19 */
  20 
  21 #include "includes.h"
  22 #include "system/filesys.h"
  23 #include "system/select.h"
  24 #include "system/readline.h"
  25 #include "lib/smbreadline/smbreadline.h"
  26 
  27 /*******************************************************************
  28  Similar to sys_select() but catch EINTR and continue.
  29  This is what sys_select() used to do in Samba.
  30 ********************************************************************/
  31 
  32 static int sys_select_intr(int maxfd, fd_set *readfds, fd_set *writefds, fd_set *errorfds, struct timeval *tval)
     /* [<][>][^][v][top][bottom][index][help] */
  33 {
  34         int ret;
  35         fd_set *readfds2, readfds_buf, *writefds2, writefds_buf, *errorfds2, errorfds_buf;
  36         struct timeval tval2, *ptval;
  37 
  38         readfds2 = (readfds ? &readfds_buf : NULL);
  39         writefds2 = (writefds ? &writefds_buf : NULL);
  40         errorfds2 = (errorfds ? &errorfds_buf : NULL);
  41         ptval = (tval ? &tval2 : NULL);
  42 
  43         do {
  44                 if (readfds)
  45                         readfds_buf = *readfds;
  46                 if (writefds)
  47                         writefds_buf = *writefds;
  48                 if (errorfds)
  49                         errorfds_buf = *errorfds;
  50                 if (tval)
  51                         tval2 = *tval;
  52 
  53                 /* We must use select and not sys_select here. If we use
  54                    sys_select we'd lose the fact a signal occurred when sys_select
  55                    read a byte from the pipe. Fix from Mark Weaver
  56                    <mark-clist@npsl.co.uk>
  57                 */
  58 
  59                 ret = select(maxfd, readfds2, writefds2, errorfds2, ptval);
  60         } while (ret == -1 && errno == EINTR);
  61 
  62         if (readfds)
  63                 *readfds = readfds_buf;
  64         if (writefds)
  65                 *writefds = writefds_buf;
  66         if (errorfds)
  67                 *errorfds = errorfds_buf;
  68 
  69         return ret;
  70 }
  71 
  72 /****************************************************************************
  73  Display the prompt and wait for input. Call callback() regularly
  74 ****************************************************************************/
  75 
  76 static char *smb_readline_replacement(const char *prompt, void (*callback)(void), 
     /* [<][>][^][v][top][bottom][index][help] */
  77                                       char **(completion_fn)(const char *text, int start, int end))
  78 {
  79         fd_set fds;
  80         char *line;
  81         struct timeval timeout;
  82         int fd = STDIN_FILENO;
  83         char *ret;
  84 
  85         printf("%s", prompt);
  86 
  87         line = (char *)malloc(BUFSIZ);
  88         if (!line) {
  89                 return NULL;
  90         }
  91 
  92         while (1) {
  93                 timeout.tv_sec = 5;
  94                 timeout.tv_usec = 0;
  95 
  96                 FD_ZERO(&fds);
  97                 FD_SET(fd,&fds);
  98 
  99                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
 100                         ret = x_fgets(line, BUFSIZ, x_stdin);
 101                         return ret;
 102                 }
 103                 if (callback)
 104                         callback();
 105         }
 106 }
 107 
 108 /****************************************************************************
 109  Display the prompt and wait for input. Call callback() regularly.
 110 ****************************************************************************/
 111 
 112 char *smb_readline(const char *prompt, void (*callback)(void), 
     /* [<][>][^][v][top][bottom][index][help] */
 113                    char **(completion_fn)(const char *text, int start, int end))
 114 {
 115 #if HAVE_LIBREADLINE
 116         if (isatty(STDIN_FILENO)) {
 117                 char *ret;
 118 
 119                 /* Aargh!  Readline does bizzare things with the terminal width
 120                 that mucks up expect(1).  Set CLI_NO_READLINE in the environment
 121                 to force readline not to be used. */
 122 
 123                 if (getenv("CLI_NO_READLINE"))
 124                         return smb_readline_replacement(prompt, callback, completion_fn);
 125 
 126                 if (completion_fn) {
 127                         /* The callback prototype has changed slightly between
 128                         different versions of Readline, so the same function
 129                         works in all of them to date, but we get compiler
 130                         warnings in some.  */
 131                         rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
 132                 }
 133 
 134 #if HAVE_DECL_RL_EVENT_HOOK
 135                 if (callback)
 136                         rl_event_hook = (Function *)callback;
 137 #endif
 138                 ret = readline(prompt);
 139                 if (ret && *ret)
 140                         add_history(ret);
 141                 return ret;
 142         } else
 143 #endif
 144         return smb_readline_replacement(prompt, callback, completion_fn);
 145 }
 146 
 147 /****************************************************************************
 148  * return line buffer text
 149  ****************************************************************************/
 150 const char *smb_readline_get_line_buffer(void)
     /* [<][>][^][v][top][bottom][index][help] */
 151 {
 152 #if defined(HAVE_LIBREADLINE)
 153         return rl_line_buffer;
 154 #else
 155         return NULL;
 156 #endif
 157 }
 158 
 159 /****************************************************************************
 160  * set completion append character
 161  ***************************************************************************/
 162 void smb_readline_ca_char(char c)
     /* [<][>][^][v][top][bottom][index][help] */
 163 {
 164 #if defined(HAVE_LIBREADLINE)
 165         rl_completion_append_character = c;
 166 #endif
 167 }
 168 
 169 
 170 

/* [<][>][^][v][top][bottom][index][help] */