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
|
implement Gather;
include "sys.m";
sys: Sys;
include "draw.m";
draw: Draw;
Point, Rect, Display, Image, Font: import draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "commandline.m";
commandline: Commandline;
Cmdline: import commandline;
include "sh.m";
Gather: module {
init: fn(ctxt: ref Draw->Context, argv: list of string);
};
CLIENTDIR: con "/dis/spree/clients";
drawctxt: ref Draw->Context;
cliquefd: ref Sys->FD;
stderr: ref Sys->FD;
mnt, dir: string;
init(ctxt: ref Draw->Context, argv: list of string)
{
sys = load Sys Sys->PATH;
stderr = sys->fildes(2);
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
if (tkclient == nil) {
sys->fprint(stderr, "gather: cannot load %s: %r\n", Tkclient->PATH);
raise "fail:bad module";
}
tkclient->init();
commandline = load Commandline Commandline->PATH;
if(commandline == nil) {
sys->fprint(stderr, "gather: cannot load %s: %r\n", Commandline->PATH);
raise "fail:bad module";
}
commandline->init();
drawctxt = ctxt;
cliquefd = sys->fildes(0);
if (len argv >= 3) {
mnt = hd tl argv;
dir = hd tl tl argv;
} else
sys->fprint(stderr, "gather: expected mnt, dir args\n");
client1();
}
client1()
{
(win, winctl) := tkclient->toplevel(drawctxt, nil, "Gathering", Tkclient->Appl);
ech := chan of string;
tk->namechan(win, ech, "e");
(chat, chatevent) := Cmdline.new(win, ".chat", nil);
updatech := chan of string;
spawn readproc(updatech);
cmd(win, "button .b -text Start -command {send e start}");
cmd(win, "pack .b -side top -anchor w");
cmd(win, "pack .chat -fill both -expand 1");
cmd(win, "pack propagate . 0");
cmd(win, "update");
tkclient->onscreen(win, nil);
tkclient->startinput(win, "kbd"::"ptr"::nil);
for (;;) alt {
s := <-win.ctxt.kbd =>
tk->keyboard(win, s);
s := <-win.ctxt.ptr =>
tk->pointer(win, *s);
s := <-win.ctxt.ctl or
s = <-win.wreq or
s = <-winctl =>
tkclient->wmctl(win, s);
line := <-updatech =>
(n, toks) := sys->tokenize(line, " ");
if (toks == nil)
continue;
case hd toks {
"clienttype" =>
chat.addtext("starting " + hd tl toks + " session...\n");
cmd(win, "update");
path := CLIENTDIR + "/" + hd tl toks + ".dis";
mod := load Command path;
if (mod == nil) {
chat.addtext(sys->sprint("could not load %s: %r\n", path));
chat.addtext("bye bye\n");
cliquefd = nil;
} else {
win = nil;
chat = nil;
startclient(mod, hd tl toks :: mnt :: dir :: tl tl toks);
exit;
}
"chat" =>
chat.addtext(hd tl toks + ": " + concat(tl tl toks) + "\n");
"title" =>
tkclient->settitle(win, "Gather " + concat(tl toks));
"join" or
"leave" or
"watch" or
"unwatch" =>
chat.addtext(line + "\n");
* =>
chat.addtext("unknown update: " + line + "\n");
}
cmd(win, "update");
c := <-chatevent =>
lines := chat.event(c);
for (; lines != nil; lines = tl lines)
cliquecmd("chat " + hd lines, chat);
c := <-ech =>
cliquecmd(c, chat);
}
}
cliquecmd(s: string, chat: ref Cmdline)
{
if (sys->fprint(cliquefd, "%s", s) == -1) {
chat.addtext(sys->sprint("command failed: %r\n"));
cmd(chat.top, "update");
}
}
prefixed(s: string, prefix: string): int
{
return len s >= len prefix && s[0:len prefix] == prefix;
}
readproc(updatech: chan of string)
{
buf := array[Sys->ATOMICIO] of byte;
while ((n := sys->read(cliquefd, buf, Sys->ATOMICIO)) > 0) {
(nil, lines) := sys->tokenize(string buf[0:n], "\n");
for (; lines != nil; lines = tl lines) {
updatech <-= hd lines;
if (prefixed(hd lines, "clienttype"))
exit;
}
}
updatech <-= nil;
}
startclient(mod: Command, argv: list of string)
{
{
mod->init(drawctxt, argv);
} exception e {
"*" =>
sys->print("client %s broken: %s\n", hd argv, e);
}
}
cmd(win: ref Tk->Toplevel, s: string): string
{
r := tk->cmd(win, s);
if(len r > 0 && r[0] == '!')
sys->print("error executing '%s': %s\n", s, r[1:]);
return r;
}
concat(l: list of string): string
{
if (l == nil)
return nil;
s := hd l;
for (l = tl l; l != nil; l = tl l)
s += " " + hd l;
return s;
}
|