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
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
|
implement WmMash;
include "sys.m";
sys: Sys;
FileIO: import sys;
include "draw.m";
draw: Draw;
Context: import draw;
include "tk.m";
tk: Tk;
include "tkclient.m";
tkclient: Tkclient;
include "plumbmsg.m";
plumbmsg: Plumbmsg;
Msg: import plumbmsg;
include "workdir.m";
workdir: Workdir;
WmMash: module
{
init: fn(ctxt: ref Draw->Context, args: list of string);
};
Command: module
{
tkinit: fn(ctxt: ref Draw->Context, t: ref Tk->Toplevel, args: list of string);
};
BS: con 8; # ^h backspace character
BSW: con 23; # ^w bacspace word
BSL: con 21; # ^u backspace line
EOT: con 4; # ^d end of file
ESC: con 27; # hold mode
HIWAT: con 2000; # maximum number of lines in transcript
LOWAT: con 1500; # amount to reduce to after high water
Name: con "Mash";
Rdreq: adt
{
off: int;
nbytes: int;
fid: int;
rc: chan of (array of byte, string);
};
shwin_cfg := array[] of {
"menu .m",
".m add command -text Cut -command {send edit cut}",
".m add command -text Paste -command {send edit paste}",
".m add command -text Snarf -command {send edit snarf}",
".m add command -text Send -command {send edit send}",
"frame .b -bd 1 -relief ridge",
"frame .ft -bd 0",
"scrollbar .ft.scroll -width 14 -bd 0 -relief ridge -command {.ft.t yview}",
"text .ft.t -bd 1 -relief flat -width 520 -height 7c -yscrollcommand {.ft.scroll set}",
"pack .ft.scroll -side left -fill y",
"pack .ft.t -fill both -expand 1",
"pack .Wm_t -fill x",
"pack .b -anchor w -fill x",
"pack .ft -fill both -expand 1",
"pack propagate . 0",
"focus .ft.t",
"bind .ft.t <Key> {send keys {%A}}",
"bind .ft.t <Control-d> {send keys {%A}}",
"bind .ft.t <Control-h> {send keys {%A}}",
"bind .ft.t <Button-1> +{grab set .ft.t; send but1 pressed}",
"bind .ft.t <Double-Button-1> +{grab set .ft.t; send but1 pressed}",
"bind .ft.t <ButtonRelease-1> +{grab release .ft.t; send but1 released}",
"bind .ft.t <ButtonPress-2> {send but2 %X %Y}",
"bind .ft.t <Motion-Button-2-Button-1> {}",
"bind .ft.t <Motion-ButtonPress-2> {}",
"bind .ft.t <ButtonPress-3> {send but3 pressed}",
"bind .ft.t <ButtonRelease-3> {send but3 released %x %y}",
"bind .ft.t <Motion-Button-3> {}",
"bind .ft.t <Motion-Button-3-Button-1> {}",
"bind .ft.t <Double-Button-3> {}",
"bind .ft.t <Double-ButtonRelease-3> {}",
"update"
};
rdreq: list of Rdreq;
menuindex := "0";
holding := 0;
plumbed := 0;
rawon := 0;
rawinput := "";
init(ctxt: ref Context, argv: list of string)
{
s: string;
sys = load Sys Sys->PATH;
if (ctxt == nil) {
sys->fprint(sys->fildes(2), "mash: no window context\n");
raise "fail:bad context";
}
draw = load Draw Draw->PATH;
tk = load Tk Tk->PATH;
tkclient = load Tkclient Tkclient->PATH;
plumbmsg = load Plumbmsg Plumbmsg->PATH;
sys->pctl(Sys->FORKNS | Sys->NEWPGRP, nil);
tkclient->init();
if(plumbmsg->init(1, nil, 0) >= 0){
plumbed = 1;
workdir = load Workdir Workdir->PATH;
}
argv = tl argv; # strip off command name
(t, titlectl) := tkclient->toplevel(ctxt, "", Name, Tkclient->Appl);
edit := chan of string;
tk->namechan(t, edit, "edit");
# mash := chan of string;
# tk->namechan(t, mash, "mash");
tkcmds(t, shwin_cfg);
tkclient->onscreen(t, nil);
tkclient->startinput(t, "kbd"::"ptr"::nil);
ioc := chan of (int, ref FileIO, ref FileIO, string);
spawn newsh(ctxt, t, ioc, argv);
(pid, file, filectl, consfile) := <-ioc;
if(file == nil || filectl == nil) {
sys->print("newsh: %r\n");
return;
}
keys := chan of string;
tk->namechan(t, keys, "keys");
but1 := chan of string;
tk->namechan(t, but1, "but1");
but2 := chan of string;
tk->namechan(t, but2, "but2");
but3 := chan of string;
tk->namechan(t, but3, "but3");
button1 := 0;
button3 := 0;
rdrpc: Rdreq;
# outpoint is place in text to insert characters printed by programs
tk->cmd(t, ".ft.t mark set outpoint end; .ft.t mark gravity outpoint left");
for(;;) alt {
c := <-t.ctxt.kbd =>
tk->keyboard(t, c);
c := <-t.ctxt.ptr =>
tk->pointer(t, *c);
c := <-t.ctxt.ctl or
c = <-t.wreq =>
tkclient->wmctl(t, c);
menu := <-titlectl =>
if(menu == "exit") {
kill(pid);
return;
}
tkclient->wmctl(t, menu);
tk->cmd(t, "focus .ft.t");
ecmd := <-edit =>
editor(t, ecmd);
sendinput(t);
tk->cmd(t, "focus .ft.t");
c := <-keys =>
cut(t, 1);
if(rawon) {
rawinput += c[1:2];
rawinput = sendraw(rawinput);
break;
}
char := c[1];
if(char == '\\')
char = c[2];
update := ";.ft.t see insert;update";
case char {
* =>
tk->cmd(t, ".ft.t insert insert "+c+update);
'\n' or EOT =>
tk->cmd(t, ".ft.t insert insert "+c+update);
sendinput(t);
BS =>
tk->cmd(t, ".ft.t tkTextDelIns -c"+update);
BSL =>
tk->cmd(t, ".ft.t tkTextDelIns -l"+update);
BSW =>
tk->cmd(t, ".ft.t tkTextDelIns -w"+update);
ESC =>
holding ^= 1;
color := "blue";
if(!holding){
color = "black";
tkclient->settitle(t, Name);
sendinput(t);
}else
tkclient->settitle(t, Name+" (holding)");
tk->cmd(t, ".ft.t configure -foreground "+color+update);
}
c := <-but1 =>
button1 = (c == "pressed");
button3 = 0; # abort any pending button 3 action
c := <-but2 =>
if(button1){
cut(t, 1);
tk->cmd(t, "update");
break;
}
(nil, l) := sys->tokenize(c, " ");
x := int hd l - 50;
y := int hd tl l - int tk->cmd(t, ".m yposition "+menuindex) - 10;
tk->cmd(t, ".m activate "+menuindex+"; .m post "+string x+" "+string y+
"; grab set .m; update");
button3 = 0; # abort any pending button 3 action
c := <-but3 =>
if(c == "pressed"){
button3 = 1;
if(button1){
paste(t);
tk->cmd(t, "update");
}
break;
}
if(plumbed == 0 || button3 == 0 || button1 != 0)
break;
button3 = 0;
# Plumb message triggered by release of button 3
(nil, l) := sys->tokenize(c, " ");
x := int hd tl l;
y := int hd tl tl l;
index := tk->cmd(t, ".ft.t index @"+string x+","+string y);
selindex := tk->cmd(t, ".ft.t tag ranges sel");
if(selindex != "")
insel := tk->cmd(t, ".ft.t compare sel.first <= "+index)=="1" &&
tk->cmd(t, ".ft.t compare sel.last >= "+index)=="1";
else
insel = 0;
attr := "";
if(insel)
text := tk->cmd(t, ".ft.t get sel.first sel.last");
else{
# have line with text in it
# now extract whitespace-bounded string around click
(nil, w) := sys->tokenize(index, ".");
charno := int hd tl w;
left := tk->cmd(t, ".ft.t index {"+index+" linestart}");
right := tk->cmd(t, ".ft.t index {"+index+" lineend}");
line := tk->cmd(t, ".ft.t get "+left+" "+right);
for(i:=charno; i>0; --i)
if(line[i-1]==' ' || line[i-1]=='\t')
break;
for(j:=charno; j<len line; j++)
if(line[j]==' ' || line[j]=='\t')
break;
text = line[i:j];
attr = "click="+string (charno-i);
}
msg := ref Msg(
"WmSh",
"",
workdir->init(),
"text",
attr,
array of byte text);
if(msg.send() < 0)
sys->fprint(sys->fildes(2), "sh: plumbing write error: %r\n");
rdrpc = <-filectl.read =>
if(rdrpc.rc == nil)
continue;
rdrpc.rc <-= ( nil, "not allowed" );
(nil, data, nil, wc) := <-filectl.write =>
if(wc == nil) {
# consctl closed - revert to cooked mode
rawon = 0;
continue;
}
(nc, cmdlst) := sys->tokenize(string data, " \n");
if(nc == 1) {
case hd cmdlst {
"rawon" =>
rawon = 1;
rawinput = "";
# discard previous input
advance := string (len tk->cmd(t, ".ft.t get outpoint end") +1);
tk->cmd(t, ".ft.t mark set outpoint outpoint+" + advance + "chars");
"rawoff" =>
rawon = 0;
* =>
wc <-= (0, "unknown consctl request");
continue;
}
wc <-= (len data, nil);
continue;
}
wc <-= (0, "unknown consctl request");
rdrpc = <-file.read =>
if(rdrpc.rc == nil) {
(ok, nil) := sys->stat(consfile);
if (ok < 0)
return;
continue;
}
append(rdrpc);
sendinput(t);
(off, data, fid, wc) := <-file.write =>
if(wc == nil) {
(ok, nil) := sys->stat(consfile);
if (ok < 0)
return;
continue;
}
cdata := stripbs(t, string data);
ncdata := string len cdata + "chars;";
moveins := insat(t, "outpoint");
tk->cmd(t, ".ft.t insert outpoint '"+ cdata);
wc <-= (len data, nil);
data = nil;
s = ".ft.t mark set outpoint outpoint+" + ncdata;
s += ".ft.t see outpoint;";
if(moveins)
s += ".ft.t mark set insert insert+" + ncdata;
s += "update";
tk->cmd(t, s);
nlines := int tk->cmd(t, ".ft.t index end");
if(nlines > HIWAT){
s = ".ft.t delete 1.0 "+ string (nlines-LOWAT) +".0;update";
tk->cmd(t, s);
}
}
}
RPCread: type (int, int, int, chan of (array of byte, string));
append(r: RPCread)
{
t := r :: nil;
while(rdreq != nil) {
t = hd rdreq :: t;
rdreq = tl rdreq;
}
rdreq = t;
}
insat(t: ref Tk->Toplevel, mark: string): int
{
return tk->cmd(t, ".ft.t compare insert == "+mark) == "1";
}
insininput(t: ref Tk->Toplevel): int
{
if(tk->cmd(t, ".ft.t compare insert >= outpoint") != "1")
return 0;
return tk->cmd(t, ".ft.t compare {insert linestart} == {outpoint linestart}") == "1";
}
isalnum(s: string): int
{
if(s == "")
return 0;
c := s[0];
if('a' <= c && c <= 'z')
return 1;
if('A' <= c && c <= 'Z')
return 1;
if('0' <= c && c <= '9')
return 1;
if(c == '_')
return 1;
if(c > 16rA0)
return 1;
return 0;
}
stripbs(t: ref Tk->Toplevel, s: string): string
{
l := len s;
for(i := 0; i < l; i++)
if(s[i] == '\b') {
pre := "";
rem := "";
if(i + 1 < l)
rem = s[i+1:];
if(i == 0) { # erase existing character in line
if(tk->cmd(t, ".ft.t get " +
"{outpoint linestart} outpoint") != "")
tk->cmd(t, ".ft.t delete outpoint-1char");
} else {
if(s[i-1] != '\n') # don't erase newlines
i--;
if(i)
pre = s[:i];
}
s = pre + rem;
l = len s;
i = len pre - 1;
}
return s;
}
editor(t: ref Tk->Toplevel, ecmd: string)
{
s, snarf: string;
case ecmd {
"cut" =>
menuindex = "0";
cut(t, 1);
"paste" =>
menuindex = "1";
paste(t);
"snarf" =>
menuindex = "2";
if(tk->cmd(t, ".ft.t tag ranges sel") == "")
break;
snarf = tk->cmd(t, ".ft.t get sel.first sel.last");
tkclient->snarfput(snarf);
"send" =>
menuindex = "3";
if(tk->cmd(t, ".ft.t tag ranges sel") != ""){
snarf = tk->cmd(t, ".ft.t get sel.first sel.last");
tkclient->snarfput(snarf);
}else
snarf = tkclient->snarfget();
if(snarf != "")
s = snarf;
else
return;
if(s[len s-1] != '\n' && s[len s-1] != EOT)
s[len s] = '\n';
tk->cmd(t, ".ft.t see end; .ft.t insert end '"+s);
tk->cmd(t, ".ft.t mark set insert end");
tk->cmd(t, ".ft.t tag remove sel sel.first sel.last");
}
tk->cmd(t, "update");
}
cut(t: ref Tk->Toplevel, snarfit: int)
{
if(tk->cmd(t, ".ft.t tag ranges sel") == "")
return;
if(snarfit)
tkclient->snarfput(tk->cmd(t, ".ft.t get sel.first sel.last"));
tk->cmd(t, ".ft.t delete sel.first sel.last");
}
paste(t: ref Tk->Toplevel)
{
snarf := tkclient->snarfget();
if(snarf == "")
return;
cut(t, 0);
tk->cmd(t, ".ft.t insert insert '"+snarf);
tk->cmd(t, ".ft.t tag add sel insert-"+string len snarf+"chars insert");
sendinput(t);
}
sendinput(t: ref Tk->Toplevel)
{
if(holding)
return;
input := tk->cmd(t, ".ft.t get outpoint end");
slen := len input;
if(slen == 0 || rdreq == nil)
return;
r := hd rdreq;
for(i := 0; i < slen; i++)
if(input[i] == '\n' || input[i] == EOT)
break;
if(i >= slen && slen < r.nbytes)
return;
if(i >= r.nbytes)
i = r.nbytes-1;
advance := string (i+1);
if(input[i] == EOT)
input = input[0:i];
else
input = input[0:i+1];
rdreq = tl rdreq;
alt {
r.rc <-= (array of byte input, "") =>
tk->cmd(t, ".ft.t mark set outpoint outpoint+" + advance + "chars");
* =>
# requester has disappeared; ignore his request and try again
sendinput(t);
}
}
sendraw(input : string) : string
{
i := len input;
if(i == 0 || rdreq == nil)
return input;
r := hd rdreq;
rdreq = tl rdreq;
if(i > r.nbytes)
i = r.nbytes;
alt {
r.rc <-= (array of byte input[0:i], "") =>
input = input[i:];
* =>
;# requester has disappeared; ignore his request and try again
}
return input;
}
newsh(ctxt: ref Context, t: ref Tk->Toplevel, ioc: chan of (int, ref FileIO, ref FileIO, string), args: list of string)
{
pid := sys->pctl(sys->NEWFD, nil);
sh := load Command "/dis/mash.dis";
if(sh == nil) {
ioc <-= (0, nil, nil, nil);
return;
}
tty := "cons."+string pid;
sys->bind("#s","/chan",sys->MBEFORE);
fio := sys->file2chan("/chan", tty);
fioctl := sys->file2chan("/chan", tty + "ctl");
ioc <-= (pid, fio, fioctl, "/chan/"+tty);
if(fio == nil || fioctl == nil)
return;
sys->bind("/chan/"+tty, "/dev/cons", sys->MREPL);
sys->bind("/chan/"+tty+"ctl", "/dev/consctl", sys->MREPL);
fd0 := sys->open("/dev/cons", sys->OREAD|sys->ORCLOSE);
fd1 := sys->open("/dev/cons", sys->OWRITE);
fd2 := sys->open("/dev/cons", sys->OWRITE);
sh->tkinit(ctxt, t, "mash" :: args);
}
kill(pid: int)
{
fd := sys->open("#p/"+string pid+"/ctl", sys->OWRITE);
if(fd != nil)
sys->fprint(fd, "killgrp");
}
tkcmds(t: ref Tk->Toplevel, cfg: array of string)
{
for(i := 0; i < len cfg; i++)
tk->cmd(t, cfg[i]);
}
|