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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
|
implement Shellbuiltin;
include "sys.m";
sys: Sys;
include "draw.m";
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "sh.m";
sh: Sh;
Listnode, Context: import sh;
myself: Shellbuiltin;
tklock: chan of int;
chans := array[23] of list of (string, chan of string);
wins := array[16] of list of (int, ref Tk->Toplevel);
winid := 0;
badmodule(ctxt: ref Context, p: string)
{
ctxt.fail("bad module", sys->sprint("tk: cannot load %s: %r", p));
}
initbuiltin(ctxt: ref Context, shmod: Sh): string
{
sys = load Sys Sys->PATH;
sh = shmod;
myself = load Shellbuiltin "$self";
if (myself == nil) badmodule(ctxt, "self");
tk = load Tk Tk->PATH;
if (tk == nil) badmodule(ctxt, Tk->PATH);
tkclient = load Tkclient Tkclient->PATH;
if (tkclient == nil) badmodule(ctxt, Tkclient->PATH);
tkclient->init();
tklock = chan[1] of int;
ctxt.addbuiltin("tk", myself);
ctxt.addbuiltin("chan", myself);
ctxt.addbuiltin("send", myself);
ctxt.addsbuiltin("tk", myself);
ctxt.addsbuiltin("recv", myself);
ctxt.addsbuiltin("alt", myself);
ctxt.addsbuiltin("tkquote", myself);
return nil;
}
whatis(nil: ref Sh->Context, nil: Sh, nil: string, nil: int): string
{
return nil;
}
getself(): Shellbuiltin
{
return myself;
}
runbuiltin(ctxt: ref Context, nil: Sh,
cmd: list of ref Listnode, nil: int): string
{
case (hd cmd).word {
"tk" => return builtin_tk(ctxt, cmd);
"chan" => return builtin_chan(ctxt, cmd);
"send" => return builtin_send(ctxt, cmd);
}
return nil;
}
runsbuiltin(ctxt: ref Context, nil: Sh,
cmd: list of ref Listnode): list of ref Listnode
{
case (hd cmd).word {
"tk" => return sbuiltin_tk(ctxt, cmd);
"recv" => return sbuiltin_recv(ctxt, cmd);
"alt" => return sbuiltin_alt(ctxt, cmd);
"tkquote" => return sbuiltin_tkquote(ctxt, cmd);
}
return nil;
}
builtin_tk(ctxt: ref Context, argv: list of ref Listnode): string
{
# usage: tk window _title_ _options_
# tk wintitle _winid_ _title_
# tk _winid_ _cmd_
if (tl argv == nil)
ctxt.fail("usage", "usage: tk (<winid>|window|onscreen|winctlwintitle|del|namechan) args...");
argv = tl argv;
w := (hd argv).word;
case w {
"window" =>
remark(ctxt, string makewin(ctxt, tl argv));
"wintitle" =>
argv = tl argv;
# change the title of a window
if (len argv != 2 || !isnum((hd argv).word))
ctxt.fail("usage", "usage: tk wintitle winid title");
tkclient->settitle(egetwin(ctxt, hd argv), word(hd tl argv));
"winctl" =>
argv = tl argv;
if (len argv != 2 || !isnum((hd argv).word))
ctxt.fail("usage", "usage: tk winctl winid cmd");
wid := (hd argv).word;
win := egetwin(ctxt, hd argv);
rq := word(hd tl argv);
if (rq == "exit") {
delwin(int wid);
delchan(wid);
}
tkclient->wmctl(win, rq);
"onscreen" =>
argv = tl argv;
if (len argv < 1 || !isnum((hd argv).word))
ctxt.fail("usage", "usage: tk onscreen winid [how]");
how := "";
if(tl argv != nil)
how = word(hd tl argv);
win := egetwin(ctxt, hd argv);
tkclient->startinput(win, "ptr" :: "kbd" :: nil);
tkclient->onscreen(win, how);
"namechan" =>
argv = tl argv;
n := len argv;
if (n < 2 || n > 3 || !isnum((hd argv).word))
ctxt.fail("usage", "usage: tk namechan winid chan [name]");
name: string;
if (n == 3)
name = word(hd tl tl argv);
else
name = word(hd tl argv);
tk->namechan(egetwin(ctxt, hd argv), egetchan(ctxt, hd tl argv), name);
"del" =>
if (len argv < 2)
ctxt.fail("usage", "usage: tk del id...");
for (argv = tl argv; argv != nil; argv = tl argv) {
id := (hd argv).word;
if (isnum(id))
delwin(int id);
delchan(id);
}
* =>
e := tkcmd(ctxt, argv);
if (e != nil)
remark(ctxt, e);
if (e != nil && e[0] == '!')
return e;
}
return nil;
}
remark(ctxt: ref Context, s: string)
{
if (ctxt.options() & ctxt.INTERACTIVE)
sys->print("%s\n", s);
}
# create a new window (and its associated channel)
makewin(ctxt: ref Context, argv: list of ref Listnode): int
{
if (argv == nil)
ctxt.fail("usage", "usage: tk window title options");
if (ctxt.drawcontext == nil)
ctxt.fail("no draw context", sys->sprint("tk: no graphics context available"));
(title, options) := (word(hd argv), concat(tl argv));
(top, topchan) := tkclient->toplevel(ctxt.drawcontext, options, title, Tkclient->Appl);
newid := addwin(top);
addchan(string newid, topchan);
return newid;
}
builtin_chan(ctxt: ref Context, argv: list of ref Listnode): string
{
# create a new channel
argv = tl argv;
if (argv == nil)
ctxt.fail("usage", "usage: chan name....");
for (; argv != nil; argv = tl argv) {
name := (hd argv).word;
if (name == nil || isnum(name))
ctxt.fail("bad chan", "tk: bad channel name "+q(name));
if (addchan(name, chan of string) == nil)
ctxt.fail("bad chan", "tk: channel "+q(name)+" already exists");
}
return nil;
}
builtin_send(ctxt: ref Context, argv: list of ref Listnode): string
{
if (len argv != 3)
ctxt.fail("usage", "usage: send chan arg");
argv = tl argv;
c := egetchan(ctxt, hd argv);
c <-= word(hd tl argv);
return nil;
}
sbuiltin_tk(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
# usage: tk _winid_ _command_
# tk window _title_ _options_
argv = tl argv;
if (argv == nil)
ctxt.fail("usage", "tk (window|wid) args");
case (hd argv).word {
"window" =>
return ref Listnode(nil, string makewin(ctxt, tl argv)) :: nil;
"winids" =>
ret: list of ref Listnode;
for (i := 0; i < len wins; i++)
for (wl := wins[i]; wl != nil; wl = tl wl)
ret = ref Listnode(nil, string (hd wl).t0) :: ret;
return ret;
* =>
return ref Listnode(nil, tkcmd(ctxt, argv)) :: nil;
}
}
sbuiltin_alt(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
# usage: alt chan ...
argv = tl argv;
if (argv == nil)
ctxt.fail("usage", "usage: alt chan...");
nc := len argv;
kbd := array[nc] of chan of int;
ptr := array[nc] of chan of ref Draw->Pointer;
ca := array[nc * 3] of chan of string;
win := array[nc] of ref Tk->Toplevel;
cname := array[nc] of string;
i := 0;
for (; argv != nil; argv = tl argv) {
w := (hd argv).word;
ca[i*3] = egetchan(ctxt, hd argv);
cname[i] = w;
if(isnum(w)){
win[i] = egetwin(ctxt, hd argv);
ca[i*3+1] = win[i].ctxt.ctl;
ca[i*3+2] = win[i].wreq;
ptr[i] = win[i].ctxt.ptr;
kbd[i] = win[i].ctxt.kbd;
}
i++;
}
for(;;) alt{
(n, key) := <-kbd =>
tk->keyboard(win[n], key);
(n, p) := <-ptr =>
tk->pointer(win[n], *p);
(n, v) := <-ca =>
return ref Listnode(nil, cname[n/3]) :: ref Listnode(nil, v) :: nil;
}
}
sbuiltin_recv(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
# usage: recv chan
if (len argv != 2)
ctxt.fail("usage", "usage: recv chan");
ch := hd tl argv;
c := egetchan(ctxt, ch);
if(!isnum(ch.word))
return ref Listnode(nil, <-c) :: nil;
win := egetwin(ctxt, ch);
for(;;)alt{
key := <-win.ctxt.kbd =>
tk->keyboard(win, key);
p := <-win.ctxt.ptr =>
tk->pointer(win, *p);
s := <-win.ctxt.ctl or
s = <-win.wreq or
s = <-c =>
return ref Listnode(nil, s) :: nil;
}
}
sbuiltin_tkquote(ctxt: ref Context, argv: list of ref Listnode): list of ref Listnode
{
if (len argv != 2)
ctxt.fail("usage", "usage: tkquote arg");
return ref Listnode(nil, tk->quote(word(hd tl argv))) :: nil;
}
tkcmd(ctxt: ref Context, argv: list of ref Listnode): string
{
if (argv == nil || !isnum((hd argv).word))
ctxt.fail("usage", "usage: tk winid command");
return tk->cmd(egetwin(ctxt, hd argv), concat(tl argv));
}
hashfn(s: string, n: int): int
{
h := 0;
m := len s;
for(i:=0; i<m; i++){
h = 65599*h+s[i];
}
return (h & 16r7fffffff) % n;
}
q(s: string): string
{
return "'" + s + "'";
}
egetchan(ctxt: ref Context, n: ref Listnode): chan of string
{
if ((c := getchan(n.word)) == nil)
ctxt.fail("bad chan", "tk: bad channel name "+ q(n.word));
return c;
}
# assumes that n.word has been checked and found to be numeric.
egetwin(ctxt: ref Context, n: ref Listnode): ref Tk->Toplevel
{
wid := int n.word;
if (wid < 0 || (top := getwin(wid)) == nil)
ctxt.fail("bad win", "tk: unknown window id " + q(n.word));
return top;
}
getchan(name: string): chan of string
{
n := hashfn(name, len chans);
for (cl := chans[n]; cl != nil; cl = tl cl) {
(cname, c) := hd cl;
if (cname == name)
return c;
}
return nil;
}
addchan(name: string, c: chan of string): chan of string
{
n := hashfn(name, len chans);
tklock <-= 1;
if (getchan(name) == nil)
chans[n] = (name, c) :: chans[n];
<-tklock;
return c;
}
delchan(name: string)
{
n := hashfn(name, len chans);
tklock <-= 1;
ncl: list of (string, chan of string);
for (cl := chans[n]; cl != nil; cl = tl cl) {
(cname, nil) := hd cl;
if (cname != name)
ncl = hd cl :: ncl;
}
chans[n] = ncl;
<-tklock;
}
addwin(top: ref Tk->Toplevel): int
{
tklock <-= 1;
id := winid++;
slot := id % len wins;
wins[slot] = (id, top) :: wins[slot];
<-tklock;
return id;
}
delwin(id: int)
{
tklock <-= 1;
slot := id % len wins;
nwl: list of (int, ref Tk->Toplevel);
for (wl := wins[slot]; wl != nil; wl = tl wl) {
(wid, nil) := hd wl;
if (wid != id)
nwl = hd wl :: nwl;
}
wins[slot] = nwl;
<-tklock;
}
getwin(id: int): ref Tk->Toplevel
{
slot := id % len wins;
for (wl := wins[slot]; wl != nil; wl = tl wl) {
(wid, top) := hd wl;
if (wid == id)
return top;
}
return nil;
}
word(n: ref Listnode): string
{
if (n.word != nil)
return n.word;
if (n.cmd != nil)
n.word = sh->cmd2string(n.cmd);
return n.word;
}
isnum(s: string): int
{
for (i := 0; i < len s; i++)
if (s[i] > '9' || s[i] < '0')
return 0;
return 1;
}
concat(argv: list of ref Listnode): string
{
if (argv == nil)
return nil;
s := word(hd argv);
for (argv = tl argv; argv != nil; argv = tl argv)
s += " " + word(hd argv);
return s;
}
lockproc(c: chan of int)
{
sys->pctl(Sys->NEWFD|Sys->NEWNS, nil);
for(;;){
c <-= 1;
<-c;
}
}
|