00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00046 # define RL_COMPLETION_CAST
00047 #endif
00048
00049
00050
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
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
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
00102
00103
00104
00105 if (getenv("CLI_NO_READLINE"))
00106 return smb_readline_replacement(prompt, callback, completion_fn);
00107
00108 if (completion_fn) {
00109
00110
00111
00112
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
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
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
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