|
|
@@ -20,26 +20,76 @@ const (
|
|
20
|
20
|
GetCommandResultError
|
|
21
|
21
|
)
|
|
22
|
22
|
|
|
23
|
|
-func getCommand(ctx *Context) (string, error) {
|
|
|
23
|
+type Command struct {
|
|
|
24
|
+ command_type CommandType
|
|
|
25
|
+ tokens []string
|
|
|
26
|
+}
|
|
|
27
|
+
|
|
|
28
|
+func makeCommandNone() Command {
|
|
|
29
|
+ return Command{
|
|
|
30
|
+ command_type: CommandTypeNone,
|
|
|
31
|
+ tokens: nil,
|
|
|
32
|
+ }
|
|
|
33
|
+}
|
|
|
34
|
+
|
|
|
35
|
+func makeCommand(command_type CommandType, tokens []string) Command {
|
|
|
36
|
+ if command_type == CommandTypeNone {
|
|
|
37
|
+ return Command{
|
|
|
38
|
+ command_type: command_type,
|
|
|
39
|
+ tokens: nil,
|
|
|
40
|
+ }
|
|
|
41
|
+ } else {
|
|
|
42
|
+ return Command{
|
|
|
43
|
+ command_type: command_type,
|
|
|
44
|
+ tokens: tokens,
|
|
|
45
|
+ }
|
|
|
46
|
+ }
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+type CommandType int
|
|
|
50
|
+
|
|
|
51
|
+const (
|
|
|
52
|
+ CommandTypeNone CommandType = iota
|
|
|
53
|
+ CommandTypeExternal
|
|
|
54
|
+ CommandTypeBuiltinExit
|
|
|
55
|
+)
|
|
|
56
|
+
|
|
|
57
|
+func getCommand(ctx *Context) (Command, error) {
|
|
24
|
58
|
fmt.Print("$ ")
|
|
25
|
59
|
command_line, err := bufio.NewReader(ctx.stdin).ReadString('\n')
|
|
26
|
60
|
if err != nil {
|
|
27
|
|
- return "", err
|
|
|
61
|
+ return makeCommandNone(), err
|
|
28
|
62
|
}
|
|
29
|
|
- command, err := parseCommand(ctx, command_line)
|
|
|
63
|
+ command, err := parseCommand(command_line)
|
|
30
|
64
|
if err != nil {
|
|
31
|
|
- return "", err
|
|
|
65
|
+ fmt.Fprintln(ctx.stderr, err)
|
|
|
66
|
+ return makeCommandNone(), nil
|
|
32
|
67
|
}
|
|
33
|
68
|
return command, nil
|
|
34
|
69
|
}
|
|
35
|
70
|
|
|
36
|
|
-func parseCommand(ctx *Context, command_line string) (string, error) {
|
|
37
|
|
- _ = ctx
|
|
38
|
|
- return command_line[:len(command_line)-1], nil
|
|
|
71
|
+func parseTokens(str string) []string {
|
|
|
72
|
+ tokens := make([]string, 2)
|
|
|
73
|
+ tokens[0] = str[:len(str)-1]
|
|
|
74
|
+ return tokens
|
|
39
|
75
|
}
|
|
40
|
76
|
|
|
41
|
|
-func handleCommand(ctx Context, command string) {
|
|
42
|
|
- fmt.Fprintln(ctx.stderr, command+": command not found")
|
|
|
77
|
+func parseCommand(command_line string) (Command, error) {
|
|
|
78
|
+ tokens := parseTokens(command_line)
|
|
|
79
|
+ if tokens[0] == "exit" {
|
|
|
80
|
+ return makeCommand(CommandTypeBuiltinExit, tokens), nil
|
|
|
81
|
+ }
|
|
|
82
|
+ return makeCommandNone(), fmt.Errorf("%s: command not found", tokens[0])
|
|
|
83
|
+}
|
|
|
84
|
+
|
|
|
85
|
+func handleCommand(ctx Context, command Command) {
|
|
|
86
|
+ _ = ctx
|
|
|
87
|
+ switch command.command_type {
|
|
|
88
|
+ case CommandTypeNone:
|
|
|
89
|
+ return
|
|
|
90
|
+ case CommandTypeBuiltinExit:
|
|
|
91
|
+ os.Exit(0)
|
|
|
92
|
+ }
|
|
43
|
93
|
}
|
|
44
|
94
|
|
|
45
|
95
|
func main() {
|