-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.go
More file actions
828 lines (760 loc) · 16.4 KB
/
shell.go
File metadata and controls
828 lines (760 loc) · 16.4 KB
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
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
package tish
import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"os"
"os/user"
"strconv"
"strings"
"time"
"github.com/midbel/rw"
"github.com/midbel/shlex"
"github.com/midbel/tish/internal/parser"
"github.com/midbel/tish/internal/stdio"
"github.com/midbel/tish/internal/token"
"github.com/midbel/tish/internal/words"
"golang.org/x/sync/errgroup"
)
const shell = "tish"
var (
ErrExit = errors.New("exit")
ErrReadOnly = errors.New("read only")
ErrEmpty = errors.New("empty command")
)
type ExitCode int8
const (
Success ExitCode = iota
Failure
)
func (e ExitCode) Success() bool {
return e == Success
}
func (e ExitCode) Failure() bool {
return !e.Success()
}
func (e ExitCode) Error() string {
return fmt.Sprintf("%d", e)
}
type Shell struct {
locals Environment
alias map[string][]string
commands map[string]Command
find CommandFinder
depth int
echo bool
env map[string]string
Stack
now time.Time
rand *rand.Rand
stdin io.Reader
stdout io.Writer
stderr io.Writer
context struct {
// PID of last executed command
pid int
// exit code of last executed command
code int
// name of last executed command
name string
// arguments of last executed command
args []string
}
builtins map[string]Builtin
}
func New(options ...ShellOption) (*Shell, error) {
sh := Shell{
now: time.Now(),
Stack: DirectoryStack(),
alias: make(map[string][]string),
commands: make(map[string]Command),
env: make(map[string]string),
builtins: builtins,
}
sh.rand = rand.New(rand.NewSource(sh.now.Unix()))
cwd, _ := os.Getwd()
sh.Stack.Chdir(cwd)
for i := range options {
if err := options[i](&sh); err != nil {
return nil, err
}
}
if sh.stdin == nil {
sh.stdin = rw.Empty()
}
if sh.locals == nil {
sh.locals = EmptyEnv()
}
return &sh, nil
}
func (s *Shell) Close() error {
for _, c := range []interface{}{s.stdin, s.stdout, s.stderr} {
if c == nil {
continue
}
if c, ok := c.(io.Closer); ok {
c.Close()
}
}
return nil
}
func (s *Shell) Exit() {
os.Exit(s.context.code)
}
func (s *Shell) SetIn(r io.Reader) {
s.stdin = r
}
func (s *Shell) SetOut(w io.Writer) {
s.stdout = w
}
func (s *Shell) SetErr(w io.Writer) {
s.stderr = w
}
// implements CommandFinder.Find
func (s *Shell) Find(ctx context.Context, name string) (Command, error) {
if s.find != nil {
c, err := s.find.Find(ctx, name)
if err == nil {
return c, nil
}
}
if c, ok := s.commands[name]; ok {
return c, nil
}
return nil, fmt.Errorf("%s: command not found", name)
}
func (s *Shell) SetEcho(echo bool) {
s.echo = echo
}
// Rxport defines an environment variable from the shell
func (s *Shell) Export(ident, value string) {
s.env[ident] = value
}
// Unexport removes an environment variable from the shell
func (s *Shell) Unexport(ident string) {
delete(s.env, ident)
}
// Alias define a shell alias to the Shell
func (s *Shell) Alias(ident, script string) error {
alias, err := shlex.Split(strings.NewReader(script))
if err != nil {
return err
}
s.alias[ident] = alias
return nil
}
// Unalias remove a shell alias from the Shell
func (s *Shell) Unalias(ident string) {
delete(s.alias, ident)
}
func (s *Shell) Subshell() (*Shell, error) {
options := []ShellOption{
WithEnv(s),
WithCwd(s.Cwd()),
WithStdout(s.stdout),
WithStderr(s.stderr),
WithStdin(s.stdin),
}
if s.echo {
options = append(options, WithEcho())
}
sub, err := New(options...)
if err != nil {
return nil, err
}
sub.depth = s.depth + 1
for n, str := range s.alias {
sub.alias[n] = str
}
return sub, nil
}
func (s *Shell) SetEnv(env Environment) {
s.locals = env
}
// implements Environment.Resolve
func (s *Shell) Resolve(ident string) ([]string, error) {
str, err := s.locals.Resolve(ident)
if err == nil && len(str) > 0 {
return str, nil
}
if v, ok := s.env[ident]; ok {
return []string{v}, nil
}
if str = s.resolveSpecials(ident); len(str) > 0 {
return str, nil
}
return nil, err
}
// implements Environment.Define
func (s *Shell) Define(ident string, values []string) error {
if _, ok := specials[ident]; ok {
return ErrReadOnly
}
return s.locals.Define(ident, values)
}
// implements Environment.Delete
func (s *Shell) Delete(ident string) error {
if _, ok := specials[ident]; ok {
return ErrReadOnly
}
return s.locals.Delete(ident)
}
func (s *Shell) Expand(str string, args []string) ([]string, error) {
env := getEnvShell(s)
return parser.Expand(str, args, env)
}
func (s *Shell) Dry(str, cmd string, args []string) error {
s.setContext(cmd, args)
defer s.clearContext()
env := getEnvShell(s)
return parser.ExpandWith(str, args, env, func(str [][]string) {
for i := range str {
io.WriteString(s.stdout, strings.Join(str[i], " "))
io.WriteString(s.stdout, "\n")
}
})
}
func (s *Shell) Register(list ...Command) {
for i := range list {
s.commands[list[i].Command()] = list[i]
}
}
func (s *Shell) Run(ctx context.Context, r io.Reader, cmd string, args []string) error {
s.setContext(cmd, args)
defer s.clearContext()
var (
p = parser.NewParser(r)
ret error
)
for {
ex, err := p.Parse()
if err != nil {
if errors.Is(err, io.EOF) {
return ret
}
return err
}
ret = s.execute(ctx, ex)
}
}
func (s *Shell) Execute(ctx context.Context, str, cmd string, args []string) error {
return s.Run(ctx, strings.NewReader(str), cmd, args)
}
func (s *Shell) execute(ctx context.Context, ex words.Executer) error {
var err error
switch ex := ex.(type) {
case words.ExecSimple:
err = s.executeSingle(ctx, ex.Expander, ex.Redirect)
case words.ExecList:
for i := range ex {
if err = s.execute(ctx, ex[i]); err != nil {
break
}
}
case words.ExecSubshell:
return s.executeSubshell(ctx, ex)
case words.ExecAssign:
err = s.executeAssign(ex)
case words.ExecAnd:
if err = s.execute(ctx, ex.Left); err != nil || s.context.code != 0 {
break
}
err = s.execute(ctx, ex.Right)
case words.ExecOr:
if err = s.execute(ctx, ex.Left); err == nil || s.context.code == 0 {
break
}
err = s.execute(ctx, ex.Right)
case words.ExecPipe:
err = s.executePipe(ctx, ex)
case words.ExecFor:
err = s.executeFor(ctx, ex)
case words.ExecWhile:
err = s.executeWhile(ctx, ex)
case words.ExecUntil:
err = s.executeUntil(ctx, ex)
case words.ExecIf:
err = s.executeIf(ctx, ex)
case words.ExecCase:
err = s.executeCase(ctx, ex)
case words.ExecBreak:
err = words.ErrBreak
case words.ExecContinue:
err = words.ErrContinue
case words.ExecTest:
err = s.executeTest(ctx, ex)
default:
err = fmt.Errorf("unsupported executer type %T", ex)
}
return err
}
func (s *Shell) executeSubshell(ctx context.Context, ex words.ExecSubshell) error {
sh, err := s.Subshell()
if err != nil {
return err
}
for i := range ex {
if err := sh.execute(ctx, ex[i]); err != nil {
return err
}
}
return nil
}
func (s *Shell) executeCase(ctx context.Context, ex words.ExecCase) error {
var (
env = getEnvShell(s)
word, err = ex.Word.Expand(env, false)
)
if err != nil {
return err
}
if len(word) != 1 {
return fmt.Errorf("too many word expanded %s", word)
}
for _, i := range ex.List {
var found bool
for j := range i.List {
vs, err := i.List[j].Expand(env, false)
if err != nil {
return err
}
if len(vs) != 1 {
return fmt.Errorf("too many word expanded %s", vs)
}
if found = vs[0] == word[0]; found {
break
}
}
if found {
return s.execute(ctx, i.Body)
}
}
if ex.Default != nil {
return s.execute(ctx, ex.Default)
}
return nil
}
func (s *Shell) executeTest(_ context.Context, ex words.ExecTest) error {
ok, err := ex.Test(getEnvShell(s))
if err != nil || !ok {
s.context.code = 1
} else {
s.context.code = 0
}
return err
}
func (s *Shell) executeFor(ctx context.Context, ex words.ExecFor) error {
var (
env = getEnvShell(s)
list, err = ex.Expand(env, false)
)
if err != nil || len(list) == 0 {
return s.execute(ctx, ex.Alt)
}
for i := range list {
if err := s.Define(ex.Ident, []string{list[i]}); err != nil {
return err
}
if err := s.execute(ctx, ex.Body); err != nil {
if errors.Is(err, words.ErrBreak) {
break
}
if errors.Is(err, words.ErrContinue) {
continue
}
return err
}
}
return nil
}
func (s *Shell) executeWhile(ctx context.Context, ex words.ExecWhile) error {
var it int
for {
err := s.execute(ctx, ex.Cond)
if err != nil {
return err
}
if s.context.code != 0 {
break
}
it++
err = s.execute(ctx, ex.Body)
if err != nil {
if errors.Is(err, words.ErrBreak) {
break
}
if errors.Is(err, words.ErrContinue) {
continue
}
return err
}
}
if it == 0 {
return s.execute(ctx, ex.Alt)
}
return nil
}
func (s *Shell) executeUntil(ctx context.Context, ex words.ExecUntil) error {
var it int
for {
err := s.execute(ctx, ex.Cond)
if err != nil {
return err
}
if s.context.code == 0 {
break
}
it++
err = s.execute(ctx, ex.Body)
if err != nil {
if errors.Is(err, words.ErrBreak) {
break
}
if errors.Is(err, words.ErrContinue) {
continue
}
return err
}
}
if it == 0 {
return s.execute(ctx, ex.Alt)
}
return nil
}
func (s *Shell) executeIf(ctx context.Context, ex words.ExecIf) error {
err := s.execute(ctx, ex.Cond)
if err != nil {
return err
}
if s.context.code == 0 {
return s.execute(ctx, ex.Csq)
}
return s.execute(ctx, ex.Alt)
}
func (s *Shell) executeSingle(ctx context.Context, ex words.Expander, redirect []words.ExpandRedirect) error {
str, err := s.expand(ex)
if err != nil {
return err
}
s.trace(str)
cmd := s.resolveCommand(ctx, str)
cmd.SetOut(s.stdout)
cmd.SetErr(s.stderr)
cmd.SetIn(s.stdin)
// rd, err := s.setupRedirect(redirect, false)
// if err != nil {
// return err
// }
// defer rd.Close()
//
// cmd.SetOut(rd.out)
// cmd.SetErr(rd.err)
// cmd.SetIn(rd.in)
// err = cmd.Run()
cmd.Run()
s.updateContext(cmd)
return nil
}
func (s *Shell) executePipe(ctx context.Context, ex words.ExecPipe) error {
get := func(ex words.Executer) (Command, error) {
sex, ok := ex.(words.ExecSimple)
if !ok {
return nil, fmt.Errorf("single command expected")
}
str, err := s.expand(sex.Expander)
if err != nil {
return nil, err
}
cmd := s.resolveCommand(ctx, str)
cmd.SetErr(s.stderr)
cmd.SetIn(s.stdin)
return cmd, nil
}
run := func(cmd Command, update bool) func() error {
return func() error {
err := cmd.Run()
if update {
s.updateContext(cmd)
}
return err
}
}
var (
last = len(ex.List) - 1
in io.ReadCloser
grp errgroup.Group
)
cmd, err := get(ex.List[last].Executer)
if err != nil {
return err
}
cmd.SetOut(s.stdout)
cmd.SetErr(s.stderr)
for i := last - 1; i >= 0; i-- {
curr, err := get(ex.List[i].Executer)
if err != nil {
return err
}
if in, err = curr.StdoutPipe(); err != nil {
return err
}
defer in.Close()
cmd.SetIn(in)
grp.Go(run(cmd, i == last-1))
cmd = curr
}
cmd.SetIn(s.stdin)
err = cmd.Run()
if err1 := grp.Wait(); err1 != nil && err == nil {
err = err1
}
return err
}
func (s *Shell) executeAssign(ex words.ExecAssign) error {
var (
env = getEnvShell(s)
str, err = ex.Expand(env, true)
)
if err != nil {
return err
}
return s.Define(ex.Ident, str)
}
func (s *Shell) expand(ex words.Expander) ([]string, error) {
var (
env = getEnvShell(s)
str, err = ex.Expand(env, true)
)
if err != nil {
return nil, err
}
if len(str) == 0 {
return nil, ErrEmpty
}
alias, ok := s.alias[str[0]]
if ok {
as := make([]string, len(alias))
copy(as, alias)
str = append(as, str[1:]...)
}
return str, nil
}
func (s *Shell) setContext(name string, args []string) {
s.context.name = name
s.context.args = append(s.context.args[:0], args...)
}
func (s *Shell) updateContext(cmd Command) {
var pid, code int
if cmd == nil {
code = 255
} else {
pid, code = cmd.Exit()
}
s.context.pid = pid
s.context.code = code
}
func (s *Shell) clearContext() {
s.context.name = ""
s.context.args = nil
}
func (s *Shell) resolveCommand(ctx context.Context, str []string) Command {
if b, ok := s.builtins[str[0]]; ok && b.IsEnabled() {
b.shell = s
b.Args = str[1:]
return &b
}
var cmd Command
if c, err := s.Find(ctx, str[0]); err == nil {
cmd = c
} else {
cmd = StandardContext(ctx, str[0], s.Cwd(), str[1:])
}
if a, ok := cmd.(interface{ SetArgs([]string) }); ok {
a.SetArgs(str[1:])
}
if e, ok := cmd.(interface{ SetEnv([]string) }); ok {
e.SetEnv(s.environ())
}
return cmd
}
func (s *Shell) resolveSpecials(ident string) []string {
var ret []string
switch ident {
case varShell:
ret = append(ret, shell)
case varSub:
ret = append(ret, strconv.Itoa(s.depth))
case varHome:
u, err := user.Current()
if err == nil {
ret = append(ret, u.HomeDir)
}
case varSeconds:
sec := time.Since(s.now).Seconds()
ret = append(ret, strconv.FormatInt(int64(sec), 10))
case varPwd:
ret = append(ret, s.Cwd())
case varOld:
// ret = append(ret, s.old)
case varPid, varShellPid:
str := strconv.Itoa(os.Getpid())
ret = append(ret, str)
case varPpid:
str := strconv.Itoa(os.Getppid())
ret = append(ret, str)
case varRand:
str := strconv.Itoa(s.rand.Int())
ret = append(ret, str)
case varScript:
ret = append(ret, s.context.name)
case varNarg:
ret = append(ret, strconv.Itoa(len(s.context.args)))
case varExit:
ret = append(ret, strconv.Itoa(s.context.code))
case varLastPid:
ret = append(ret, strconv.Itoa(s.context.pid))
case varArgsStr:
ret = append(ret, strings.Join(s.context.args, " "))
case varArgsArr:
ret = append(ret, s.context.args...)
default:
n, err := strconv.Atoi(ident)
if err != nil {
break
}
var arg string
if n >= 1 && n <= len(s.context.args) {
arg = s.context.args[n-1]
}
ret = append(ret, arg)
}
return ret
}
func (s *Shell) trace(str []string) {
if !s.echo {
return
}
fmt.Fprintln(s.stdout, strings.Join(str, " "))
}
func (s *Shell) environ() []string {
var str []string
for n, v := range s.env {
str = append(str, fmt.Sprintf("%s=%s", n, v))
}
return str
}
func (s *Shell) setupRedirect(rs []words.ExpandRedirect, pipe bool) (redirect, error) {
var (
stdin *os.File
stdout *os.File
stderr *os.File
rd redirect
env = getEnvShell(s)
)
for _, r := range rs {
str, err := r.Expand(env, true)
if err != nil {
return rd, err
}
switch file := str[0]; r.Type {
case token.RedirectIn:
stdin, err = replaceFile(file, flagRead, stdin)
case token.RedirectOut:
if stdout == stderr {
stdout = nil
}
stdout, err = replaceFile(file, flagWrite, stdout)
case token.RedirectErr:
if stderr == stdout {
stderr = nil
}
stderr, err = replaceFile(file, flagWrite, stderr)
case token.RedirectBoth:
var fd *os.File
if fd, err = replaceFile(file, flagWrite, stdout, stderr); err == nil {
stdout, stderr = fd, fd
}
case token.AppendOut:
if stdout.Fd() == stderr.Fd() {
stdout = nil
}
stdout, err = replaceFile(file, flagAppend, stdout)
case token.AppendErr:
if stderr == stdout {
stderr = nil
}
stderr, err = replaceFile(file, flagAppend, stderr)
case token.AppendBoth:
var fd *os.File
if fd, err = replaceFile(file, flagAppend, stdout, stderr); err == nil {
stdout, stderr = fd, fd
}
default:
err = fmt.Errorf("unknown/unsupported redirection")
}
if err != nil {
return rd, err
}
}
rd.in = fileOrReader(stdin, s.stdin, pipe)
rd.out = fileOrWriter(stdout, s.stdout, pipe)
rd.err = fileOrWriter(stderr, s.stderr, pipe)
return rd, nil
}
const (
flagRead = os.O_CREATE | os.O_RDONLY
flagWrite = os.O_CREATE | os.O_WRONLY
flagAppend = os.O_CREATE | os.O_WRONLY | os.O_APPEND
)
func replaceFile(file string, flag int, list ...*os.File) (*os.File, error) {
fd, err := os.OpenFile(file, flag, 0644)
if err != nil {
return nil, err
}
for i := range list {
if list[i] == nil {
continue
}
list[i].Close()
}
return fd, nil
}
func fileOrWriter(f *os.File, w io.Writer, pipe bool) io.Writer {
if f == nil {
if pipe {
return nil // w
}
return stdio.Writer(w)
}
return stdio.Writer(w)
}
func fileOrReader(f *os.File, r io.Reader, pipe bool) io.ReadCloser {
if f == nil {
if pipe {
return nil // stdio.Reader(r)
}
if r == nil {
r = rw.Empty()
}
return stdio.Reader(r)
}
return stdio.Reader(f)
}
type redirect struct {
in io.Reader
out io.Writer
err io.Writer
}
func (r redirect) Close() error {
for _, c := range []interface{}{r.in, r.out, r.err} {
if c == nil {
continue
}
if c, ok := c.(io.Closer); ok {
c.Close()
}
}
return nil
}