File: wsd.cc

package info (click to toggle)
wvstreams 4.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,820 kB
  • ctags: 7,837
  • sloc: cpp: 64,203; ansic: 4,154; sh: 4,094; makefile: 549; perl: 402
file content (221 lines) | stat: -rw-r--r-- 5,398 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include "wvfdstream.h"
#include "wvistreamlist.h"
#include "wvstrutils.h"
#include "wvunixsocket.h"
#include <readline/readline.h>
#include <readline/history.h>

#ifndef MACOS // The version of READLINE shipped with MacOS is brain damaged.

class WvReadLineStream : public WvStream
{
    static WvReadLineStream *me;
    WvStream *base;
    WvString prompt;
    WvDynBuf line_buf;
    WvStringList commands;

    virtual size_t uread(void *_buf, size_t count)
    {
        size_t result = 0;
        char *buf = (char *)_buf;
        while (count > 0 && line_buf.used() > 0)
        {
            size_t chunk = line_buf.optgettable();
            if (chunk > count)
                chunk = count;
            memcpy(buf, line_buf.get(chunk), chunk);
            count -= chunk;
            buf += chunk;
            result += chunk;
        }
        return result;
    }

    virtual size_t uwrite(const void *_buf, size_t count)
    {
        const char *buf = (const char *)_buf;
        for (size_t i=0; i<count; ++i)
        {
            if (buf[i] == '\n')
                rl_crlf();
            else
                rl_show_char(buf[i]);
        }
        return count;        
    }

    static void readline_callback(char *str)
    {
        if (str == NULL)
            return;
        size_t len = strlen(str);
        if (len == 0)
            return;
        me->line_buf.put(str, len);
        me->line_buf.putch('\n');
        add_history(str);
    }

    static int readline_getc(FILE *)
    {
        char ch;
        assert(me->base->read(&ch, 1) == 1);
        return ch;
    }

    static char *readline_command_completion_function(const char *text, int state)
    {
        static int skip = 0;
        if (state == 0)
            skip = 0;
        int my_skip = skip;
        size_t len = strlen(text);
        WvStringList::Iter i(me->commands);
        for (i.rewind(); i.next(); )
        {
            if (my_skip-- > 0)
                continue;
            ++skip;
            if (i->len() >= len && strncmp(*i, text, len) == 0)
                return strdup(*i);
        }
        return NULL;       
    }

    virtual void pre_select(SelectInfo &si)
    {
        if (si.wants.readable && line_buf.used() > 0)
            si.msec_timeout = 0;
        
        base->pre_select(si);
    }

    virtual bool post_select(SelectInfo &si)
    {
        bool now = false;
        if (si.wants.readable && line_buf.used() > 0)
            now = true;

        while (base->isreadable())
            rl_callback_read_char();
        return base->post_select(si) || now;
    }

public:

    WvReadLineStream(WvStream *_base, WvStringParm _prompt)
    {
        base = _base;
        prompt = _prompt;

        assert(!me);
        me = this;
        set_wsname("readline on %s", base->wsname());
        rl_already_prompted = 1;
        rl_completion_entry_function = readline_command_completion_function;
        rl_callback_handler_install(prompt, readline_callback);
        rl_getc_function = readline_getc;
    }

    ~WvReadLineStream()
    {
        rl_getc_function = NULL;
        rl_callback_handler_remove();
        me = NULL;
    }

    virtual bool isok() const
    {
        return WvStream::isok() && base->isok();
    }

    void display_prompt()
    {
        base->print("%s", prompt);
        rl_already_prompted = 1;
    }

    void set_commands(const WvStringList &_commands)
    {
        commands.zap();
        WvStringList::Iter i(_commands);
        for (i.rewind(); i.next(); )
            commands.append(*i);
    }

    const char *wstype() const { return "WvReadLineStream"; }
};


WvReadLineStream *WvReadLineStream::me = NULL;


void remote_cb(WvStream &remote, WvReadLineStream &local)
{
    const char *line = remote.getline();
    if (line == NULL)
        return;

    WvStringList words;
    wvtcl_decode(words, line);

    WvString first = words.popstr();
    bool last_line = !!first && first != "-";
    if (last_line)
        local.print("%s ", first);
    local.print("%s\n", words.join(" "));
    if (last_line)
        local.display_prompt();

    if (words.popstr() == "Commands availible:")
        local.set_commands(words);
}


void local_cb(WvReadLineStream &local, WvStream &remote)
{
    const char *line = local.getline();
    if (line == NULL)
        return;

    if (strcmp(line, "quit") == 0)
        remote.close();

    remote.print("%s\n", line);
}


int main(int argc, char **argv)
{
    WvReadLineStream readlinestream(wvcon, "> ");

    const char *sockname = "/tmp/weaver.wsd";
    if (argc >= 2)
        sockname = argv[1];

    WvUnixConn *s = new WvUnixConn(sockname);
    if (!s->isok())
    {
        wverr->print("Failed to connect to %s: %s\n",
                sockname, s->errstr());
        return 1;
    }
    s->set_wsname("%s", sockname);
    s->print("help\n");

    s->setcallback(wv::bind(remote_cb, wv::ref(*s), wv::ref(readlinestream)));
    WvIStreamList::globallist.append(s, true, "wvstreams debugger client");

    readlinestream.setcallback(wv::bind(local_cb, wv::ref(readlinestream),
					wv::ref(*s)));
    WvIStreamList::globallist.append(&readlinestream, false, 
                                     "wvstreams debugger readline");

    while (s->isok() && readlinestream.isok())
        WvIStreamList::globallist.runonce();

    return 0;
}

#endif // Apple brain damaged Readline.