forked from platinasystems/goes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
goes.go
791 lines (736 loc) · 16.7 KB
/
goes.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
// Copyright © 2015-2020 Platina Systems, Inc. All rights reserved.
// Use of this source code is governed by the GPL-2 license described in the
// LICENSE file.
// +build linux
// Package goes, combined with a compatibly configured Linux kernel, provides a
// monolithic embedded system.
package goes
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"os/signal"
"path/filepath"
"strings"
"sync"
"syscall"
"unicode/utf8"
"github.com/platinasystems/goes/cmd"
"github.com/platinasystems/goes/external/flags"
"github.com/platinasystems/goes/external/parms"
"github.com/platinasystems/goes/internal/prog"
"github.com/platinasystems/goes/internal/shellutils"
"github.com/platinasystems/goes/lang"
"github.com/platinasystems/url"
)
const (
VerboseQuiet = iota
VerboseVerify
VerboseDebug
)
type Blocker interface {
Block(*Goes, shellutils.List) (*shellutils.List,
func(io.Reader, io.Writer, io.Writer) error,
error)
}
type akaer interface {
Aka() string
}
type goeser interface {
Goes(*Goes)
}
type Goes struct {
// These uppercased fields may/should be assigned at instantiation
NAME, USAGE string
APROPOS, MAN lang.Alt
ByName map[string]cmd.Cmd
Catline io.ReadWriter
Status error
Verbosity int
cache cache
parent *Goes
EnvMap map[string]string
FunctionMap map[string]Function
inTest bool
}
type Function struct {
Name string
Definition []string
RunFun func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error
}
/*
All goes go-routines should add them selves to the WG WaitGroup and quit on
Stop like this,
goes.WG.Add(1)
go func() {
defer goes.WG.Done()
for {
select {
case <-goes.Stop:
return
default:
...
}
}
}
*/
var (
Stop chan struct{}
IntSig chan os.Signal
WG sync.WaitGroup
)
func (g *Goes) ProcessPipeline(ls shellutils.List) (*shellutils.List, *shellutils.Word, func(io.Reader, io.Writer, io.Writer) error, error) {
var (
closers []io.Closer
term shellutils.Word
)
isLast := false
pipeline := make([]func(io.Reader, io.Writer, io.Writer) error, 0)
for len(ls.Cmds) != 0 && !isLast {
cl := ls.Cmds[0]
term = cl.Term
if term.String() != "|" {
isLast = true
}
var runfun func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error
name := cl.Cmds[0].String()
if v := g.ByName[name]; v != nil {
if method, found := v.(Blocker); found {
var (
newls *shellutils.List
err error
)
newls, runfun, err = method.Block(g, ls)
if err != nil {
return nil, nil, nil, err
}
ls = *newls
cl = ls.Cmds[0]
ls.Cmds = ls.Cmds[1:]
term = cl.Term
if term.String() != "|" {
isLast = true
}
pipeline = append(pipeline, runfun)
continue
}
}
runfun, err := g.ProcessCommand(cl, &closers)
if err != nil {
return nil, nil, nil, err
}
ls.Cmds = ls.Cmds[1:]
pipeline = append(pipeline, runfun)
}
pipefun, err := g.MakePipefun(pipeline, &closers)
return &ls, &term, pipefun, err
}
func (g *Goes) isStdinRedirected(stdin io.Reader) bool {
if f, ok := stdin.(*os.File); ok {
if f == os.Stdin {
return false
}
return true
}
return true
}
func (g *Goes) isStdoutRedirected(stdout io.Writer) bool {
if f, ok := stdout.(*os.File); ok {
if f == os.Stdout {
return false
}
return true
}
return true
}
func (g *Goes) isStderrRedirected(stderr io.Writer) bool {
if f, ok := stderr.(*os.File); ok {
if f == os.Stderr {
return false
}
return true
}
return true
}
func (g *Goes) isRedirected(stdin io.Reader, stdout io.Writer, stderr io.Writer) bool {
return g.isStdinRedirected(stdin) || g.isStdoutRedirected(stdout) ||
g.isStderrRedirected(stderr)
}
func (g *Goes) errOrInt(err error) error {
if err != nil {
return err
}
select {
case <-IntSig:
return fmt.Errorf("Command interrupted")
default:
}
return nil
}
func (g *Goes) ProcessCommand(cl shellutils.Cmdline, closers *[]io.Closer) (func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, error) {
runfun := func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
envMap, args := cl.Slice(func(k string) string {
v, def := g.EnvMap[k]
if def {
return v
}
return os.Getenv(k)
})
// Add to our context environment if this command only set variables
if len(args) == 0 {
if len(envMap) != 0 {
if g.EnvMap == nil {
g.EnvMap = envMap
} else {
for k, v := range envMap {
g.EnvMap[k] = v
}
}
g.Status = nil // Successfully set variables
}
return nil
}
name := args[0]
// check for function invocation
if f, x := g.FunctionMap[name]; x {
return g.errOrInt(f.RunFun(stdin, stdout, stderr))
}
// check for built in command
if v := g.ByName[name]; v != nil {
k := cmd.WhatKind(v)
if k.IsDaemon() {
return fmt.Errorf(
"use `goes-daemons start %s`",
name)
}
if g.isRedirected(stdin, stdout, stderr) {
if k.IsCantPipe() {
return fmt.Errorf("%s: can't pipe", name)
}
}
if k.IsDontFork() || g.inTest ||
name == os.Args[0] {
if method, found := v.(goeser); found {
method.Goes(g)
}
return g.errOrInt(g.Main(args...))
}
} else if builtin, found := g.Builtins()[name]; found {
return builtin(args[1:]...)
} else {
return fmt.Errorf("%s: command not found", name)
}
in := stdin
if !g.isStdinRedirected(stdin) {
var iparm *parms.Parms
iparm, args = parms.New(args, "<", "<<", "<<-")
if fn := iparm.ByName["<"]; len(fn) > 0 {
rc, err := url.Open(fn)
if err != nil {
return err
}
in = rc
*closers = append(*closers, rc)
} else if len(iparm.ByName["<<"]) > 0 ||
len(iparm.ByName["<<-"]) > 0 {
var trim bool
lbl := iparm.ByName["<<"]
if len(lbl) == 0 {
lbl = iparm.ByName["<<-"]
trim = true
}
r, w, err := os.Pipe()
if err != nil {
return err
}
in = r
*closers = append(*closers, r)
WG.Add(1)
go func(w io.WriteCloser, lbl string) {
defer WG.Done()
defer w.Close()
prompt := "<<" + fn + " "
for {
g.Catline.Write([]byte(prompt))
buf := make([]byte, 1024)
n, err := g.Catline.Read(buf)
s := string(buf[0:n])
if err != nil || s == lbl {
break
}
if trim {
s = strings.TrimLeft(s, " \t")
}
fmt.Fprintln(w, s)
}
}(w, lbl)
}
}
out := stdout
if !g.isStdoutRedirected(stdout) {
var oparm *parms.Parms
oparm, args = parms.New(args, ">", ">>", ">>>", ">>>>")
if fn := oparm.ByName[">"]; len(fn) > 0 {
wc, err := url.Create(fn)
if err != nil {
return err
}
out = wc
*closers = append(*closers, wc)
} else if fn = oparm.ByName[">>"]; len(fn) > 0 {
wc, err := url.Append(fn)
if err != nil {
return err
}
out = wc
*closers = append(*closers, wc)
} else if fn := oparm.ByName[">>>"]; len(fn) > 0 {
wc, err := url.Create(fn)
if err != nil {
return err
}
out = io.MultiWriter(os.Stdout, wc)
*closers = append(*closers, wc)
} else if fn := oparm.ByName[">>"]; len(fn) > 0 {
wc, err := url.Append(fn)
if err != nil {
return err
}
out = io.MultiWriter(os.Stdout, wc)
*closers = append(*closers, wc)
}
}
var envStr []string
if len(envMap) != 0 {
envStr = make([]string, 0)
for k, v := range envMap {
envStr = append(envStr, fmt.Sprintf("%s=%s", k, v))
}
}
if g.Verbosity >= VerboseVerify {
fmt.Println("+", strings.Join(envStr, " "), strings.Join(args, " "))
}
x := g.Fork(args...)
if len(envStr) != 0 {
x.Env = os.Environ()
for _, s := range envStr {
x.Env = append(x.Env, s)
}
}
x.Stdin = in
x.Stdout = out
x.Stderr = stderr
if err := x.Start(); err != nil {
err = fmt.Errorf("child: %v: %v", x.Args, err)
return err
}
if !g.isStdoutRedirected(stdout) { // fixme not a pipe
err := x.Wait()
g.Status = err
if err != nil &&
err.Error() != "exit status 1" {
fmt.Fprintln(os.Stderr, err)
}
} else {
WG.Add(1)
go func(x *exec.Cmd) {
defer WG.Done()
err := x.Wait()
if err != nil &&
err.Error() != "exit status 1" {
fmt.Fprintln(os.Stderr, err)
}
if x.Stdout != os.Stdout {
m, found := x.Stdout.(io.Closer)
if found {
m.Close()
}
}
if x.Stdin != os.Stdin {
m, found := x.Stdin.(io.Closer)
if found {
m.Close()
}
}
}(x)
}
return g.errOrInt(nil)
}
return runfun, nil
}
func (g *Goes) MakePipefun(pipeline []func(io.Reader, io.Writer, io.Writer) error, closers *[]io.Closer) (func(io.Reader, io.Writer, io.Writer) error, error) {
pipefun := func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
var (
err error
pin *os.File
)
defer func() {
for _, c := range *closers {
c.Close()
}
}()
in := stdin
end := len(pipeline) - 1
for i, runfun := range pipeline {
out := stdout
if i != end {
var pout *os.File
pin, pout, err = os.Pipe()
if err != nil {
break
}
out = pout
}
err = runfun(in, out, stderr)
if err != nil {
break
}
in = pin
}
return err
}
return pipefun, nil
}
func Replace(s, name string) string {
return strings.Replace(s, "goes", name, -1)
}
func (g *Goes) String() string {
name := g.NAME
if len(name) == 0 {
name = "goes"
}
return name
}
func (g *Goes) Goes(parent *Goes) {
g.parent = parent
}
// Fork returns an exec.Cmd ready to Run or Output this program with the
// given args.
func (g *Goes) Fork(args ...string) *exec.Cmd {
if g.Verbosity >= VerboseDebug {
fmt.Printf("F*$=%v %v\n", g.Status, args)
}
a := append(g.Path(), args...)
x := prog.Command(a...)
return x
}
// Run a command in the current context.
//
// If len(args) == 1 and args[0] doesn't match a mapped command, this will run
// the "cli".
//
// If the args has "-help", or "--help", this runs ByName("help").Main(args...)
// to print text.
//
// Similarly for "-apropos", "-complete", "-man", and "-usage".
//
// If the command is a daemon, this fork exec's itself twice to disassociate
// the daemon from the tty and initiating process.
func (g *Goes) Main(args ...string) error {
_ = syscall.Setrlimit(syscall.RLIMIT_CORE,
&syscall.Rlimit{Cur: 0xffffffff, Max: 0xffffffff})
Stop = make(chan struct{})
if strings.HasSuffix(os.Args[0], ".test") {
g.inTest = true
} else if len(args) > 0 {
if strings.HasSuffix(args[0], "__debug_bin") {
g.inTest = true
args = args[1:]
} else if args[0] == "/proc/self/exe" {
args = args[1:]
}
}
if len(args) > 0 {
base := filepath.Base(args[0])
switch {
case g.NAME == "goes-installer":
if len(args) == 1 {
args[0] = "install"
} else {
args = args[1:]
}
case base == g.NAME:
// e.g. ./goes-MACHINE ...
fallthrough
case base == "goes":
args = args[1:]
}
}
IntSig = make(chan os.Signal, 1)
var v cmd.Cmd
var k cmd.Kind
var found bool
if len(args) > 0 {
v, found = g.ByName[args[0]]
if found {
k = cmd.WhatKind(v)
}
}
if !found {
cli, clifound := g.ByName["cli"]
if clifound {
cli.(goeser).Goes(g)
}
cliFlags, cliArgs := flags.New(args, "-debug", "-f", "-no-liner", "-x")
if cliFlags.ByName["-debug"] && g.Verbosity < VerboseDebug {
g.Verbosity = VerboseDebug
}
if n := len(cliArgs); n == 0 {
if cli != nil {
if cliFlags.ByName["-no-liner"] {
cliArgs = append(cliArgs, "-no-liner")
}
if cliFlags.ByName["-x"] {
cliArgs = append(cliArgs, "-x")
}
g.Status = cli.Main(cliArgs...)
return g.Status
} else if def, found := g.ByName[""]; found {
g.Status = def.Main()
return g.Status
}
fmt.Println(Usage(g))
g.Status = nil
return nil
} else if n == 1 {
// only check for script if args[0] isn't a command
buf, err := ioutil.ReadFile(cliArgs[0])
if cliArgs[0] == "-" || (err == nil && utf8.Valid(buf) &&
bytes.HasPrefix(buf, []byte("#!/usr/bin/goes"))) {
// e.g. /usr/bin/goes SCRIPT
if cli == nil {
g.Status = fmt.Errorf("has no cli")
return g.Status
}
for _, t := range []string{"-f", "-x"} {
if cliFlags.ByName[t] {
cliArgs = append(cliArgs, t)
}
}
g.Status = cli.Main(cliArgs...)
return g.Status
}
args = cliArgs
} else {
g.swap(args)
}
}
if builtin, found := g.Builtins()[args[0]]; found {
g.Status = builtin(args[1:]...)
return g.Status
} else if len(args) == 1 && strings.HasPrefix(args[0], "-") {
arg0 := strings.TrimLeft(args[0], "-")
if arg0 == "apropos" {
fmt.Println(g.Apropos())
return nil
} else if builtin, found := g.Builtins()[arg0]; found {
g.Status = builtin()
return g.Status
}
}
if g.shift(args) {
v, found = g.ByName[args[0]]
}
if g.Verbosity >= VerboseDebug {
fmt.Printf("$=%v %v\n", g.Status, args)
}
if !found {
if v, found = g.ByName[""]; !found {
g.Status =
fmt.Errorf("%s: ambiguous or missing command",
args[0])
return g.Status
}
// e.g. ip -s add [default "show"]
args = append([]string{""}, args...)
} else if method, found := v.(goeser); found {
method.Goes(g)
}
if k.IsDaemon() {
sig := make(chan os.Signal)
quit := make(chan struct{})
signal.Notify(sig, syscall.SIGTERM)
WG.Add(1)
go func() {
defer WG.Done()
select {
case <-quit:
case t := <-sig:
fmt.Println(t)
if t == syscall.SIGTERM {
close(Stop)
method, found := v.(io.Closer)
if found {
method.Close()
}
}
}
}()
err := v.Main(args[1:]...)
close(quit)
WG.Wait()
signal.Stop(sig)
return err
}
err := v.Main(args[1:]...)
if err != nil && !k.IsDaemon() {
name := args[0]
if len(name) == 0 {
if method, found := v.(akaer); found {
name = fmt.Sprint("(", method.Aka(), ")")
}
}
err = fmt.Errorf("%s: %w", name, err)
}
g.Status = err
return err
}
// shift the first unambiguous longest prefix match command to args[0], so,
//
// OPTIONS... COMMAND [ARGS]...
//
// becomes
//
// COMMAND OPTIONS... [ARGS]...
//
// e.g.
//
// ip -s li
//
// becomes
//
// ip link -s
func (g *Goes) shift(args []string) bool {
for i := range args {
if _, found := g.ByName[args[i]]; found {
if i > 0 {
name := args[i]
copy(args[1:i+1], args[:i])
args[0] = name
}
return true
}
var matches int
var last string
for _, name := range g.Names() {
if strings.HasPrefix(name, args[i]) {
last = name
matches++
}
}
if matches == 1 {
if i > 0 {
copy(args[1:i+1], args[:i])
}
args[0] = last
return true
}
}
return false
}
// swap hyphen prefaced helper flags with command, so,
//
// COMMAND [-[-]]HELPER [ARGS]...
//
// becomes
//
// HELPER COMMAND [ARGS]...
//
// and
//
// -[-]HELPER [ARGS]...
//
// becomes
//
// HELPER [ARGS]...
func (g *Goes) swap(args []string) {
n := len(args)
if n > 0 && strings.HasPrefix(args[0], "-") {
opt := strings.TrimLeft(args[0], "-")
if _, found := g.Builtins()[opt]; found {
args[0] = opt
}
} else if n > 1 {
opt := strings.TrimLeft(args[1], "-")
if _, found := g.Builtins()[opt]; found {
args[1] = args[0]
args[0] = opt
}
}
}
func (g *Goes) ensureTerminated(ls shellutils.List) (*shellutils.List, error) {
for {
term := ""
for _, cl := range ls.Cmds {
term = cl.Term.String()
if term != "||" && term != "&&" && term != "|" {
return &ls, nil
}
}
newls, err := shellutils.Parse(fmt.Sprintf("%s>>", term), g.Catline)
if err != nil {
return nil, err
}
for _, cl := range (*newls).Cmds {
ls.Cmds = append(ls.Cmds, cl)
}
}
}
type piperun struct {
f func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error
t shellutils.Word
}
func (g *Goes) ProcessList(ls shellutils.List) (*shellutils.List, *shellutils.Word, func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, error) {
var (
pipeline []piperun
term shellutils.Word
)
newls, err := g.ensureTerminated(ls)
if err != nil {
return nil, nil, nil, err
}
ls = *newls
for len(ls.Cmds) != 0 {
nextls, term, runner, err := g.ProcessPipeline(ls)
if err != nil {
return nil, nil, nil, err
}
ls = *nextls
pipeline = append(pipeline, piperun{f: runner, t: *term})
if term.String() != "&&" && term.String() != "||" {
break
}
}
listfun, err := g.MakeListFunc(pipeline)
return &ls, &term, listfun, err
}
func (g *Goes) MakeListFunc(pipeline []piperun) (func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error, error) {
listfun := func(stdin io.Reader, stdout io.Writer, stderr io.Writer) error {
var err error
skipNext := false
for _, runfun := range pipeline {
term := runfun.t
if !skipNext {
err = runfun.f(stdin, stdout, stderr)
if err != nil {
g.Status = err
}
skipNext = false
}
if g.Status != nil {
if term.String() == "&&" {
skipNext = true
}
} else {
if term.String() == "||" {
skipNext = true
}
}
}
return err
}
return listfun, nil
}