-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
164 lines (133 loc) · 3.5 KB
/
main.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
package main
import (
"bufio"
"fmt"
"os"
"os/exec"
"strings"
"time"
"gash/globals"
"gash/history"
"gash/prompt"
"gash/unixSignals"
)
func check(e error) {
if e != nil {
// panic(e)
fmt.Println(e)
}
}
func decisionTree(b []byte, executionStatus bool, prevCommand string) bool {
if !executionStatus {
var c []byte = make([]byte, 1)
var d []byte = make([]byte, 1)
if string(b) == string(byte(27)) {
os.Stdin.Read(c)
os.Stdin.Read(d)
if string(c) == string(byte(91)) {
if string(d) == string(byte(65)) {
// read history
globals.LineNumber -= 1
input := history.ReadGashHistory(globals.LineNumber)
input = strings.TrimSuffix(input, "\n")
fmt.Print(globals.ClearLine)
prompt.Prompt()
fmt.Print(input)
prevCommand = input
os.Stdin.Read(b)
executionStatus = decisionTree(b, executionStatus, prevCommand)
} else if string(d) == string(byte(66)) {
// read latest
globals.LineNumber += 1
input := history.ReadGashHistory(globals.LineNumber)
input = strings.TrimSuffix(input, "\n")
fmt.Print(globals.ClearLine)
prompt.Prompt()
fmt.Print(input)
prevCommand = input
os.Stdin.Read(b)
executionStatus = decisionTree(b, executionStatus, prevCommand)
}
}
} else {
input := ""
if prevCommand == "" {
fmt.Print(string(b))
// Enable chacter display on screen
exec.Command("stty", "-F", "/dev/tty", "echo").Run()
reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
check(err)
input = string(b) + input
history.EditGashHistory(input)
executionStatus = true
t1 := time.Now()
if err := execInput(input); err != nil {
fmt.Fprintln(os.Stderr, err)
}
t2 := time.Now()
diff := t2.Sub(t1)
fmt.Println("time elapsed: ", diff)
} else {
fmt.Print(string(b))
input = prevCommand + string(b)
input = strings.TrimSuffix(input, "\n")
// Enable chacter display on screen
exec.Command("stty", "-F", "/dev/tty", "echo").Run()
reader := bufio.NewReader(os.Stdin)
extra_input, err := reader.ReadString('\n')
check(err)
input = input + extra_input
history.EditGashHistory(input)
executionStatus = true
t1 := time.Now()
if err := execInput(input); err != nil {
fmt.Fprintln(os.Stderr, err)
}
t2 := time.Now()
diff := t2.Sub(t1)
fmt.Println("time elapsed: ", diff)
}
}
}
return executionStatus
}
func execInput(input string) error {
input = strings.TrimSuffix(input, "\n")
args := strings.Split(input, " ")
switch args[0] {
case "cd":
// 'cd' to home dir with empty path not yet supported.
if len(args) < 2 || args[1] == "" {
dir := "/home/" + "anurag"
return os.Chdir(dir)
}
// Change the directory and return the error
return os.Chdir(args[1])
case "exit":
os.Exit(0)
}
cmd := exec.Command(args[0], args[1:]...)
// Set the correct output device
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}
func main() {
// disable input buffering
// TODO: check validity of this
// exec.Command("stty", "-F", "/dev/tty", "cbreak", "min", "1").Run()
var b []byte = make([]byte, 1)
executionStatus := false
for {
// disble chacter display on screen
// exec.Command("stty", "-F", "/dev/tty", "-echo").Run()
globals.LineNumber = history.FileLines() + 1
prevCommand := ""
prompt.Prompt()
// Invoke Unix Signals Handler
go unixSignals.SingHandler()
os.Stdin.Read(b)
decisionTree(b, executionStatus, prevCommand)
}
}