lib/readline.c

説明を見る。
00001 /* 
00002    Unix SMB/CIFS implementation.
00003    Samba readline wrapper implementation
00004    Copyright (C) Simo Sorce 2001
00005    Copyright (C) Andrew Tridgell 2001
00006    
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    
00012    This program is distributed in the hope that it will be useful,
00013    but WITHOUT ANY WARRANTY; without even the implied warranty of
00014    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015    GNU General Public License for more details.
00016    
00017    You should have received a copy of the GNU General Public License
00018    along with this program; if not, write to the Free Software
00019    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00020 */
00021 
00022 #include "includes.h"
00023 
00024 #ifdef HAVE_LIBREADLINE
00025 #  ifdef HAVE_READLINE_READLINE_H
00026 #    include <readline/readline.h>
00027 #    ifdef HAVE_READLINE_HISTORY_H
00028 #      include <readline/history.h>
00029 #    endif
00030 #  else
00031 #    ifdef HAVE_READLINE_H
00032 #      include <readline.h>
00033 #      ifdef HAVE_HISTORY_H
00034 #        include <history.h>
00035 #      endif
00036 #    else
00037 #      undef HAVE_LIBREADLINE
00038 #    endif
00039 #  endif
00040 #endif
00041 
00042 #ifdef HAVE_NEW_LIBREADLINE
00043 #  define RL_COMPLETION_CAST (rl_completion_func_t *)
00044 #else
00045 /* This type is missing from libreadline<4.0  (approximately) */
00046 #  define RL_COMPLETION_CAST
00047 #endif /* HAVE_NEW_LIBREADLINE */
00048 
00049 /****************************************************************************
00050  Display the prompt and wait for input. Call callback() regularly
00051 ****************************************************************************/
00052 
00053 static char *smb_readline_replacement(const char *prompt, void (*callback)(void), 
00054                                 char **(completion_fn)(const char *text, int start, int end))
00055 {
00056         fd_set fds;
00057         static pstring line;
00058         struct timeval timeout;
00059         int fd = x_fileno(x_stdin);
00060         char *ret;
00061 
00062         /* Prompt might be NULL in non-interactive mode. */
00063         if (prompt) {
00064                 x_fprintf(x_stdout, "%s", prompt);
00065                 x_fflush(x_stdout);
00066         }
00067 
00068         while (1) {
00069                 timeout.tv_sec = 5;
00070                 timeout.tv_usec = 0;
00071 
00072                 FD_ZERO(&fds);
00073                 FD_SET(fd,&fds);
00074         
00075                 if (sys_select_intr(fd+1,&fds,NULL,NULL,&timeout) == 1) {
00076                         ret = x_fgets(line, sizeof(line), x_stdin);
00077                         return ret;
00078                 }
00079                 if (callback)
00080                         callback();
00081         }
00082 }
00083 
00084 /****************************************************************************
00085  Display the prompt and wait for input. Call callback() regularly.
00086 ****************************************************************************/
00087 
00088 char *smb_readline(const char *prompt, void (*callback)(void), 
00089                    char **(completion_fn)(const char *text, int start, int end))
00090 {
00091         char *ret;
00092         BOOL interactive;
00093 
00094         interactive = isatty(x_fileno(x_stdin)) || getenv("CLI_FORCE_INTERACTIVE");
00095         if (!interactive) {
00096             return smb_readline_replacement(NULL, callback, completion_fn);
00097         }
00098 
00099 #if HAVE_LIBREADLINE
00100 
00101         /* Aargh!  Readline does bizzare things with the terminal width
00102         that mucks up expect(1).  Set CLI_NO_READLINE in the environment
00103         to force readline not to be used. */
00104 
00105         if (getenv("CLI_NO_READLINE"))
00106                 return smb_readline_replacement(prompt, callback, completion_fn);
00107 
00108         if (completion_fn) {
00109                 /* The callback prototype has changed slightly between
00110                 different versions of Readline, so the same function
00111                 works in all of them to date, but we get compiler
00112                 warnings in some.  */
00113                 rl_attempted_completion_function = RL_COMPLETION_CAST completion_fn;
00114         }
00115 
00116 #if HAVE_DECL_RL_EVENT_HOOK
00117         if (callback)
00118                 rl_event_hook = (Function *)callback;
00119 #endif
00120         ret = readline(prompt);
00121         if (ret && *ret)
00122                 add_history(ret);
00123 
00124 #else
00125         ret = smb_readline_replacement(prompt, callback, completion_fn);
00126 #endif
00127 
00128         return ret;
00129 }
00130 
00131 /****************************************************************************
00132  * return line buffer text
00133  ****************************************************************************/
00134 const char *smb_readline_get_line_buffer(void)
00135 {
00136 #if defined(HAVE_LIBREADLINE)
00137         return rl_line_buffer;
00138 #else
00139         return NULL;
00140 #endif
00141 }
00142 
00143 
00144 /****************************************************************************
00145  * set completion append character
00146  ***************************************************************************/
00147 void smb_readline_ca_char(char c)
00148 {
00149 #if defined(HAVE_LIBREADLINE)
00150         rl_completion_append_character = c;
00151 #endif
00152 }
00153 
00154 /****************************************************************************
00155 history
00156 ****************************************************************************/
00157 int cmd_history(void)
00158 {
00159 #if defined(HAVE_LIBREADLINE) && defined(HAVE_HISTORY_LIST)
00160         HIST_ENTRY **hlist;
00161         int i;
00162 
00163         hlist = history_list();
00164         
00165         for (i = 0; hlist && hlist[i]; i++) {
00166                 DEBUG(0, ("%d: %s\n", i, hlist[i]->line));
00167         }
00168 #else
00169         DEBUG(0,("no history without readline support\n"));
00170 #endif
00171 
00172         return 0;
00173 }
00174 

Sambaに対してSat Aug 29 21:22:58 2009に生成されました。  doxygen 1.4.7