-
Notifications
You must be signed in to change notification settings - Fork 34
/
acme.go
741 lines (668 loc) · 16.4 KB
/
acme.go
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
package main
import (
"flag"
"fmt"
"image"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"strconv"
"strings"
"syscall"
"time"
"9fans.net/go/draw"
"9fans.net/go/plumb"
"github.com/rjkroege/edwood/frame"
)
const (
NSnarf = 1000
)
var (
snarfrune [NSnarf + 1]rune
command *Command
swapscrollButtons bool
)
func timefmt( /*Fmt* */ ) int {
return 0
}
// var threaddebuglevel = flag.Int("D", 0, "Thread Debug Level") // TODO(flux): Unused?
var globalautoindentflag = flag.Bool("a", false, "Global AutoIntent")
var bartflagflag = flag.Bool("b", false, "Bart's Flag")
var ncolflag = flag.Int("c", -1, "Number of columns (> 0)")
var varfontflag = flag.String("f", "/lib/font/bit/lucsans/euro.8.font", "Variable Width Font")
var fixedfontflag = flag.String("F", "/lib/font/bit/lucm/unicode.9.font", "Fixed Width Font")
var loadfileflag = flag.String("l", "", "Load file name")
var mtptflag = flag.String("m", "", "Mountpoint")
var swapscrollbuttonsflag = flag.Bool("r", false, "Swap scroll buttons")
var winsize = flag.String("W", "1024x768", "Window Size (WidthxHeight)")
var ncol int = 2
var mainpid int
func main() {
// rfork(RFENVG|RFNAMEG); TODO(flux): I'm sure these are vitally(?) important.
runtime.GOMAXPROCS(7)
flag.Parse()
ncol = *ncolflag
globalautoindent = *globalautoindentflag
loadfile := *loadfileflag
mtpt = *mtptflag
bartflag = *bartflagflag
swapscrollbuttons = *swapscrollbuttonsflag
cputype = os.Getenv("cputype")
objtype = os.Getenv("objtype")
home = os.Getenv("HOME")
acmeshell = os.Getenv("acmeshell")
p := os.Getenv("tabstop")
if p != "" {
mt, _ := strconv.ParseInt(p, 10, 32)
maxtab = uint(mt)
}
if maxtab == 0 {
maxtab = 4
}
if loadfile != "" {
fontnames := LoadFonts(loadfile) // Overrides fonts selected up to here.
if len(fontnames) == 2 {
*varfontflag = fontnames[0]
*fixedfontflag = fontnames[1]
}
}
os.Setenv("font", *varfontflag)
// TODO(flux): this must be 9p open? It's unused in the C code after its opening.
// Is it just somehow to keep it open?
//snarffd = open("/dev/snarf", OREAD|OCEXEC);
wdir, _ = os.Getwd()
var err error
var display *draw.Display
display, err = draw.Init(nil, *varfontflag, "edwood", *winsize)
if err != nil {
log.Fatalf("can't open display: %v\n", err)
}
if err := display.Attach(draw.Refnone); err != nil {
panic("failed to attach to window")
}
display.ScreenImage.Draw(display.ScreenImage.R, display.White, nil, image.ZP)
mousectl = display.InitMouse()
keyboardctl = display.InitKeyboard()
mainpid = os.Getpid()
tagfont = *varfontflag
iconinit(display)
cwait = make(chan *os.ProcessState)
ccommand = make(chan *Command)
ckill = make(chan string)
cxfidalloc = make(chan *Xfid)
cxfidfree = make(chan *Xfid)
cnewwindow = make(chan *Window)
mouseexit0 = make(chan int)
csignal = make(chan os.Signal, 1)
cerr = make(chan error)
cedit = make(chan int)
cexit = make(chan struct{})
cwarn = make(chan uint)
mousectl = display.InitMouse()
mouse = &mousectl.Mouse
startplumbing()
fsysinit()
// disk = NewDisk() TODO(flux): Let's be sure we'll avoid this paging stuff
const WindowsPerCol = 6
row.Init(display.ScreenImage.R, display)
if loadfile == "" || row.Load(loadfile, true) != nil {
// Open the files from the command line, up to WindowsPerCol each
files := flag.Args()
if ncol < 0 {
if len(files) == 0 {
ncol = 2
} else {
ncol = (len(files) + (WindowsPerCol - 1)) / WindowsPerCol
if ncol < 2 {
ncol = 2
}
}
}
if ncol == 0 {
ncol = 2
}
for i := 0; i < ncol; i++ {
row.Add(nil, -1)
}
rightmostcol := row.col[len(row.col)-1]
if len(files) == 0 {
readfile(row.col[len(row.col)-1], wdir)
} else {
for i, filename := range files {
// guide always goes in the rightmost column
if filepath.Base(filename) == "guide" || i/WindowsPerCol >= len(row.col) {
readfile(rightmostcol, filename)
} else {
readfile(row.col[i/WindowsPerCol], filename)
}
}
}
}
display.Flush()
// After row is initialized
acmeerrorinit()
go mousethread(display)
go keyboardthread(display)
go waitthread()
go newwindowthread()
go xfidallocthread(display)
signal.Notify(csignal /*, hangupsignals...*/)
for {
select {
case <-cexit:
shutdown(os.Interrupt)
case s := <-csignal:
shutdown(s)
}
}
}
func readfile(c *Column, filename string) {
w := c.Add(nil, nil, 0)
abspath, _ := filepath.Abs(filename)
w.SetName(abspath)
w.body.Load(0, filename, true)
w.body.file.mod = false
w.dirty = false
w.SetTag()
w.Resize(w.r, false, true)
w.body.ScrDraw(w.body.fr.GetFrameFillStatus().Nchars)
w.tag.SetSelect(w.tag.file.b.Nc(), w.tag.file.b.Nc())
xfidlog(w, "new")
}
var fontCache map[string]*draw.Font = make(map[string]*draw.Font)
func fontget(name string, display *draw.Display) *draw.Font {
var font *draw.Font
var ok bool
if font, ok = fontCache[name]; !ok {
f, err := display.OpenFont(name)
if err != nil {
warning(nil, "can't open font file %s: %v\n", name, err)
return nil
}
fontCache[name] = f
font = f
}
return font
}
var boxcursor = draw.Cursor{
Point: image.Point{-7, -7},
Clr: [32]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F,
0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},
Set: [32]byte{0x00, 0x00, 0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE,
0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E, 0x70, 0x0E,
0x7F, 0xFE, 0x7F, 0xFE, 0x7F, 0xFE, 0x00, 0x00},
}
func iconinit(display *draw.Display) {
//TODO(flux): Probably should de-globalize colors.
if tagcolors[frame.ColBack] == nil {
tagcolors[frame.ColBack] = display.AllocImageMix(draw.Palebluegreen, draw.White)
tagcolors[frame.ColHigh], _ = display.AllocImage(image.Rect(0, 0, 1, 1), display.ScreenImage.Pix, true, draw.Palegreygreen)
tagcolors[frame.ColBord], _ = display.AllocImage(image.Rect(0, 0, 1, 1), display.ScreenImage.Pix, true, draw.Purpleblue)
tagcolors[frame.ColText] = display.Black
tagcolors[frame.ColHText] = display.Black
textcolors[frame.ColBack] = display.AllocImageMix(draw.Paleyellow, draw.White)
textcolors[frame.ColHigh], _ = display.AllocImage(image.Rect(0, 0, 1, 1), display.ScreenImage.Pix, true, draw.Darkyellow)
textcolors[frame.ColBord], _ = display.AllocImage(image.Rect(0, 0, 1, 1), display.ScreenImage.Pix, true, draw.Yellowgreen)
textcolors[frame.ColText] = display.Black
textcolors[frame.ColHText] = display.Black
}
// ...
r := image.Rect(0, 0, display.ScaleSize(Scrollwid+ButtonBorder), fontget(tagfont, display).Height+1)
button, _ = display.AllocImage(r, display.ScreenImage.Pix, false, draw.Notacolor)
button.Draw(r, tagcolors[frame.ColBack], nil, r.Min)
r.Max.X -= display.ScaleSize(ButtonBorder)
button.Border(r, display.ScaleSize(ButtonBorder), tagcolors[frame.ColBord], image.ZP)
r = button.R
modbutton, _ = display.AllocImage(r, display.ScreenImage.Pix, false, draw.Notacolor)
modbutton.Draw(r, tagcolors[frame.ColBack], nil, r.Min)
r.Max.X -= display.ScaleSize(ButtonBorder)
modbutton.Border(r, display.ScaleSize(ButtonBorder), tagcolors[frame.ColBord], image.ZP)
r = r.Inset(display.ScaleSize(ButtonBorder))
tmp, _ := display.AllocImage(image.Rect(0, 0, 1, 1), display.ScreenImage.Pix, true, draw.Medblue)
modbutton.Draw(r, tmp, nil, image.ZP)
r = button.R
colbutton, _ = display.AllocImage(r, display.ScreenImage.Pix, false, draw.Purpleblue)
but2col, _ = display.AllocImage(r, display.ScreenImage.Pix, true, 0xAA0000FF)
but3col, _ = display.AllocImage(r, display.ScreenImage.Pix, true, 0x006600FF)
}
func ismtpt(filename string) bool {
if mtpt == "" {
return false
}
return strings.HasPrefix(filename, mtpt) && (mtpt[len(mtpt)-1] == '/' || filename[len(mtpt)] == '/' || len(filename) == len(mtpt))
}
func mousethread(display *draw.Display) {
// TODO(rjk): Do we need this?
runtime.LockOSThread()
for {
row.lk.Lock()
flushwarnings()
row.lk.Unlock()
display.Flush()
select {
case <-mousectl.Resize:
if err := display.Attach(draw.Refnone); err != nil {
panic("failed to attach to window")
}
display.ScreenImage.Draw(display.ScreenImage.R, display.White, nil, image.ZP)
iconinit(display)
ScrlResize(display)
row.Resize(display.ScreenImage.R)
case mousectl.Mouse = <-mousectl.C:
MovedMouse(mousectl.Mouse)
case <-cwarn:
break
case pm := <-cplumb:
if pm.Type == "text" {
act := findattr(pm.Attr, "action")
if act == "" || act == "showfile" {
plumblook(pm)
} else if act == "showdata" {
plumbshow(pm)
}
}
}
}
}
func findattr(attr *plumb.Attribute, s string) string {
for attr != nil {
if attr.Name == s {
return attr.Value
}
attr = attr.Next
}
return ""
}
func MovedMouse(m draw.Mouse) {
row.lk.Lock()
defer row.lk.Unlock()
t := row.Which(m.Point)
if t != mousetext && t != nil && t.w != nil &&
(mousetext == nil || mousetext.w == nil || t.w.id != mousetext.w.id) {
xfidlog(t.w, "focus")
}
if t != mousetext && mousetext != nil && mousetext.w != nil {
mousetext.w.Lock('M')
mousetext.eq0 = ^0
mousetext.w.Commit(mousetext)
mousetext.w.Unlock()
}
mousetext = t
if t == nil {
return
}
w := t.w
if t == nil || m.Buttons == 0 {
return
}
but := 0
switch m.Buttons {
case 1:
but = 1
case 2:
but = 2
case 4:
but = 3
}
barttext = t
if t.what == Body && m.Point.In(t.scrollr) {
if but != 0 {
if swapscrollButtons {
switch but {
case 1:
but = 3
case 3:
but = 1
}
}
w.Lock('M')
defer w.Unlock()
t.eq0 = ^0
t.Scroll(but)
}
return
}
/* scroll Buttons, wheels, etc. */
if w != nil && (m.Buttons&(8|16)) != 0 {
if m.Buttons&8 != 0 {
but = Kscrolloneup
} else {
but = Kscrollonedown
}
w.Lock('M')
defer w.Unlock()
t.eq0 = ^0
t.Type(rune(but))
return
}
if m.Point.In(t.scrollr) {
if but != 0 {
switch t.what {
case Columntag:
row.DragCol(t.col, but)
case Tag:
t.col.DragWin(t.w, but)
if t.w != nil {
barttext = &t.w.body
}
}
if t.col != nil {
activecol = t.col
}
}
return
}
if m.Buttons != 0 {
if w != nil {
w.Lock('M')
defer w.Unlock()
}
t.eq0 = ^0
if w != nil {
w.Commit(t)
} else {
t.Commit(true)
}
switch {
case m.Buttons&1 != 0:
t.Select()
if w != nil {
w.SetTag()
}
argtext = t
seltext = t
if t.col != nil {
activecol = t.col /* button 1 only */
}
if t.w != nil && t == &t.w.body {
activewin = t.w
}
case m.Buttons&2 != 0:
if q0, q1, argt, ok := t.Select2(); ok {
execute(t, q0, q1, false, argt)
}
case m.Buttons&4 != 0:
if q0, q1, ok := t.Select3(); ok {
look3(t, q0, q1, false)
}
}
return
}
return
}
func keyboardthread(display *draw.Display) {
var (
timer *time.Timer
t *Text
)
emptyTimer := make(<-chan time.Time)
timerchan := emptyTimer
typetext := (*Text)(nil)
for {
select {
case <-timerchan:
t = typetext
if t != nil && t.what == Tag {
t.w.Lock('K')
t.w.Commit(t)
t.w.Unlock()
display.Flush()
}
case r := <-keyboardctl.C:
for {
typetext = row.Type(r, mouse.Point)
t = typetext
if t != nil && t.col != nil && !(r == draw.KeyDown || r == draw.KeyLeft || r == draw.KeyRight) { /* scrolling doesn't change activecol */
activecol = t.col
}
if t != nil && t.w != nil {
t.w.body.file.curtext = &t.w.body
}
if timer != nil {
timer.Stop()
}
if t != nil && t.what == Tag { // Wait 500 msec to commit a tag.
timer = time.NewTimer(500 * time.Millisecond)
timerchan = timer.C
} else {
timer = nil
timerchan = emptyTimer
}
select {
case r = <-keyboardctl.C:
continue
default:
display.Flush()
}
break
}
}
}
}
/*
* There is a race between process exiting and our finding out it was ever created.
* This structure keeps a list of processes that have exited we haven't heard of.
*/
type Pid struct {
pid int
msg string
next *Pid // TODO(flux) turn this into a slice of Pid
}
func waitthread() {
var lc, c *Command
var pids *Pid
Freecmd := func() {
if c != nil {
if c.iseditcommand {
cedit <- 0
}
fsysdelid(c.md)
}
}
for {
Switch:
select {
case err := <-cerr:
row.lk.Lock()
warning(nil, "%s", err)
row.display.Flush()
row.lk.Unlock()
case cmd := <-ckill:
found := false
for c = command; c != nil; c = c.next {
/* -1 for blank */
if c.name == cmd {
if err := c.proc.Kill(); err != nil {
warning(nil, "kill %v: %v\n", cmd, err)
}
found = true
}
}
if !found {
warning(nil, "Kill: no process %v\n", cmd)
}
case w := <-cwait:
pid := w.Pid()
for c = command; c != nil; c = c.next {
if c.pid == pid {
if lc != nil {
lc.next = c.next
} else {
command = c.next
}
break
}
lc = c
}
row.lk.Lock()
t := &row.tag
t.Commit(true)
if c == nil {
/* helper processes use this exit status */
// TODO(flux): I don't understand what this libthread code is doing
Untested()
if strings.HasPrefix(w.String(), "libthread") {
p := &Pid{}
p.pid = pid
p.msg = w.String()
p.next = pids
pids = p
}
} else {
if search(t, []rune(c.name)) {
t.Delete(t.q0, t.q1, true)
t.SetSelect(0, 0)
}
if !w.Success() {
warning(c.md, "%s: %s\n", c.name, w.String())
}
row.display.Flush()
}
row.lk.Unlock()
Freecmd()
case c = <-ccommand:
/* has this command already exited? */
lastp := (*Pid)(nil)
for p := pids; p != nil; p = p.next {
if p.pid == c.pid {
if p.msg != "" {
warning(c.md, "%s\n", p.msg)
}
if lastp == nil {
pids = p.next
} else {
lastp.next = p.next
}
Freecmd()
break Switch
}
lastp = p
}
c.next = command
command = c
row.lk.Lock()
t := &row.tag
t.Commit(true)
t.Insert(0, []rune(c.name), true)
t.SetSelect(0, 0)
row.display.Flush()
row.lk.Unlock()
}
}
}
// maintain a linked list of Xfid
// TODO(flux): It would be more idiomatic to prep one up front, and block on sending
// it instead of using a send and a receive to get one.
// Frankly, it would be more idiomatic to let the GC take care of them,
// though that would require an exit signal in xfidctl.
func xfidallocthread(d *draw.Display) {
xfree := (*Xfid)(nil)
for {
select {
case <-cxfidalloc:
x := xfree
if x != nil {
xfree = x.next
} else {
x = &Xfid{}
x.c = make(chan func(*Xfid))
go xfidctl(x, d)
}
cxfidalloc <- x
case x := <-cxfidfree:
x.next = xfree
xfree = x
break
}
}
}
func newwindowthread() {
var w *Window
for {
/* only fsysproc is talking to us, so synchronization is trivial */
<-cnewwindow
w = makenewwindow(nil)
w.SetTag()
xfidlog(w, "new")
cnewwindow <- w
}
}
func plumbproc() {
}
func killprocs() {
fsysclose()
/*
for _, c := range command {
c.Signal(os.Interrupt)
}
*/
}
var dumping bool
var hangupsignals = []os.Signal{
os.Signal(syscall.SIGINT),
os.Signal(syscall.SIGHUP),
os.Signal(syscall.SIGQUIT),
os.Signal(syscall.SIGSTOP),
}
// TODO(rjk): I'm not sure that this is the right thing to do? It fails to
// handle the situation that is most interesting: trying to save the state
// if we would otherwise crash. It's also conceivably racy.
func shutdown(s os.Signal) {
for _, sig := range hangupsignals {
if sig == s {
if !dumping && os.Getpid() == mainpid {
killprocs()
dumping = true
row.Dump("")
} else {
os.Exit(0)
}
}
}
return
}
func acmeerrorinit() {
var pfd [2]int
// TODO(fhs): Syscall package is not portable.
// Perhaps use io.Pipe since exec.Cmd takes io.Reader/io.Writer.
err := syscall.Pipe(pfd[:])
if err != nil {
acmeerror("can't create pipe", nil)
}
syscall.CloseOnExec(pfd[0])
syscall.CloseOnExec(pfd[1])
errorfd := pfd[0]
erroutfd = pfd[1]
if errorfd < 0 {
acmeerror("can't re-open acmeerror file", nil)
}
go func() {
var buf [BUFSIZE]byte
errorf := os.NewFile(uintptr(errorfd), "Global Error File/Pipe")
for {
n, _ := errorf.Read(buf[:])
if n == 0 {
return
}
cerr <- fmt.Errorf(string(buf[:n]))
}
}()
}
const MAXSNARF = 100 * 1024
func acmeputsnarf() {
r := make([]rune, snarfbuf.Nc())
snarfbuf.Read(0, r[:snarfbuf.Nc()])
row.display.WriteSnarf([]byte(string(r)))
}
func acmegetsnarf() {
b := make([]byte, MAXSNARF)
n, _, _ := row.display.ReadSnarf(b)
r, _, _ := cvttorunes(b, n)
snarfbuf.Reset()
snarfbuf.Insert(0, r)
}