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.
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
void runonce(time_t msec_timeout=-1)
Exactly the same as: if (select(timeout)) callback();.
virtual bool isok() const
return true if the stream is actually usable right now
WvString popstr()
get the first string in the list, or an empty string if the list is empty.
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
void putch(int ch)
Puts a single character into the buffer as an int.
virtual void close()
Close the stream if it is open; isok() becomes false from now on.
WvString is an implementation of a simple and efficient printable-string class.
virtual bool isreadable()
Returns true if the stream is readable.
the data structure used by pre_select()/post_select() and internally by select().
char * getline(time_t wait_msec=0, char separator='\n', int readahead=1024)
Read up to one line of data from the stream and return a pointer to the internal buffer containing th...
WvString join(const char *joinchars=" ") const
concatenates all elements of the list seperating on joinchars
virtual bool post_select(SelectInfo &si)
post_select() is called after ::select(), and returns true if this object is now ready.
virtual size_t read(void *buf, size_t count)
read a data block on the stream.
virtual bool isok() const
return true if the stream is actually usable right now
virtual bool isok() const
return true if the stream is actually usable right now
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
void setcallback(IWvStreamCallback _callfunc)
define the callback function for this stream, called whenever the callback() member is run,...
size_t used() const
Returns the number of elements in the buffer currently available for reading.
WvStream-based Unix domain socket connection class.
This is a WvList of WvStrings, and is a really handy way to parse strings.
void wvtcl_decode(WvList< WvString > &l, WvStringParm _s, const WvStringMask &splitchars=WVTCL_SPLITCHARS, bool do_unescape=true)
split a tcl-style list.
virtual void pre_select(SelectInfo &si)
pre_select() sets up for eventually calling ::select().