1 #include "wvfdstream.h"
2 #include "wvistreamlist.h"
4 #include "wvunixsocket.h"
5 #include <readline/readline.h>
6 #include <readline/history.h>
8 #ifndef MACOS // The version of READLINE shipped with MacOS is brain damaged.
18 virtual size_t uread(
void *_buf,
size_t count)
21 char *buf = (
char *)_buf;
22 while (count > 0 && line_buf.
used() > 0)
27 memcpy(buf, line_buf.
get(chunk), chunk);
35 virtual size_t uwrite(
const void *_buf,
size_t count)
37 const char *buf = (
const char *)_buf;
38 for (
size_t i=0; i<count; ++i)
48 static void readline_callback(
char *str)
52 size_t len = strlen(str);
55 me->line_buf.put(str, len);
56 me->line_buf.
putch(
'\n');
60 static int readline_getc(FILE *)
63 assert(me->base->
read(&ch, 1) == 1);
67 static char *readline_command_completion_function(
const char *text,
int state)
73 size_t len = strlen(text);
74 WvStringList::Iter i(me->commands);
75 for (i.rewind(); i.next(); )
80 if (i->len() >= len && strncmp(*i, text, len) == 0)
88 if (si.wants.readable && line_buf.
used() > 0)
97 if (si.wants.readable && line_buf.
used() > 0)
101 rl_callback_read_char();
114 set_wsname(
"readline on %s", base->wsname());
115 rl_already_prompted = 1;
116 rl_completion_entry_function = readline_command_completion_function;
117 rl_callback_handler_install(prompt, readline_callback);
118 rl_getc_function = readline_getc;
123 rl_getc_function = NULL;
124 rl_callback_handler_remove();
133 void display_prompt()
135 base->print(
"%s", prompt);
136 rl_already_prompted = 1;
142 WvStringList::Iter i(_commands);
143 for (i.rewind(); i.next(); )
147 const char *wstype()
const {
return "WvReadLineStream"; }
156 const char *line = remote.
getline();
164 bool last_line = !!first && first !=
"-";
166 local.print(
"%s ", first);
167 local.print(
"%s\n", words.
join(
" "));
169 local.display_prompt();
171 if (words.
popstr() ==
"Commands availible:")
172 local.set_commands(words);
178 const char *line = local.
getline();
182 if (strcmp(line,
"quit") == 0)
185 remote.print(
"%s\n", line);
189 int main(
int argc,
char **argv)
193 const char *sockname =
"/tmp/weaver.wsd";
200 wverr->print(
"Failed to connect to %s: %s\n",
201 sockname, s->errstr());
204 s->set_wsname(
"%s", sockname);
207 s->
setcallback(wv::bind(remote_cb, wv::ref(*s), wv::ref(readlinestream)));
208 WvIStreamList::globallist.append(s,
true,
"wvstreams debugger client");
210 readlinestream.setcallback(wv::bind(local_cb, wv::ref(readlinestream),
212 WvIStreamList::globallist.append(&readlinestream,
false,
213 "wvstreams debugger readline");
215 while (s->
isok() && readlinestream.isok())
216 WvIStreamList::globallist.runonce();
221 #endif // Apple brain damaged Readline.