github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/exp/ash/ash.go (about) 1 // Copyright 2012-2018 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "flag" 9 "log" 10 "os" 11 "os/exec" 12 "strings" 13 14 "github.com/u-root/u-root/pkg/complete" 15 "github.com/u-root/u-root/pkg/termios" 16 ) 17 18 var ( 19 debug = flag.Bool("d", false, "enable debug prints") 20 test = flag.Bool("t", false, "test mode -- do completions, don't run commands") 21 v = func(string, ...interface{}) {} 22 ) 23 24 func verbose(f string, a ...interface{}) { 25 v(f+"\r\n", a...) 26 } 27 28 func main() { 29 tty() 30 flag.Parse() 31 if *debug { 32 v = log.Printf 33 complete.Debug = verbose 34 } 35 t, err := termios.New() 36 if err != nil { 37 log.Fatal(err) 38 } 39 r, err := t.Raw() 40 if err != nil { 41 log.Printf("non-fatal cannot get tty: %v", err) 42 } 43 defer func() { 44 if err := t.Set(r); err != nil { 45 log.Print(err) 46 } 47 }() 48 49 f := complete.NewFileCompleter("") 50 p, err := complete.NewPathCompleter() 51 if err != nil { 52 log.Fatal(err) 53 } 54 55 bin := complete.NewMultiCompleter(complete.NewStringCompleter([]string{"exit"}), p, f) 56 l := complete.NewNewerLineReader(bin, f) 57 l.Prompt = "% " 58 for !l.EOF { 59 if err := l.ReadLine(t, t); err != nil { 60 log.Printf("looperr: %v", err) 61 continue 62 } 63 if _, err := t.Write([]byte("\r\n")); err != nil { 64 log.Print(err) 65 } 66 if l.FullLine == "" { 67 continue 68 } 69 if l.Exact == "" { 70 f := strings.Fields(l.FullLine) 71 if *test { 72 log.Printf("%v", f) 73 if _, err := t.Write([]byte("\r\n")); err != nil { 74 log.Print(err) 75 } 76 continue 77 } 78 79 if err := t.Set(r); err != nil { 80 log.Print(err) 81 } 82 cmd := exec.Command(f[0], f[1:]...) 83 cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr 84 85 if err := cmd.Run(); err != nil { 86 log.Print(err) 87 } 88 89 foreground() 90 if _, err := t.Raw(); err != nil { 91 log.Print(err) 92 } 93 } 94 } 95 }