|
|
@@ -5,6 +5,7 @@ import (
|
|
5
|
5
|
"fmt"
|
|
6
|
6
|
"io"
|
|
7
|
7
|
"os"
|
|
|
8
|
+ "strings"
|
|
8
|
9
|
)
|
|
9
|
10
|
|
|
10
|
11
|
type Context struct {
|
|
|
@@ -52,6 +53,7 @@ const (
|
|
52
|
53
|
CommandTypeNone CommandType = iota
|
|
53
|
54
|
CommandTypeExternal
|
|
54
|
55
|
CommandTypeBuiltinExit
|
|
|
56
|
+ CommandTypeBuiltinEcho
|
|
55
|
57
|
)
|
|
56
|
58
|
|
|
57
|
59
|
func getCommand(ctx *Context) (Command, error) {
|
|
|
@@ -68,17 +70,20 @@ func getCommand(ctx *Context) (Command, error) {
|
|
68
|
70
|
return command, nil
|
|
69
|
71
|
}
|
|
70
|
72
|
|
|
71
|
|
-func parseTokens(str string) []string {
|
|
|
73
|
+func tokenize(str string) []string {
|
|
72
|
74
|
tokens := make([]string, 2)
|
|
73
|
|
- tokens[0] = str[:len(str)-1]
|
|
|
75
|
+ tokens = strings.Fields(str)
|
|
74
|
76
|
return tokens
|
|
75
|
77
|
}
|
|
76
|
78
|
|
|
77
|
79
|
func parseCommand(command_line string) (Command, error) {
|
|
78
|
|
- tokens := parseTokens(command_line)
|
|
|
80
|
+ tokens := tokenize(command_line)
|
|
79
|
81
|
if tokens[0] == "exit" {
|
|
80
|
82
|
return makeCommand(CommandTypeBuiltinExit, tokens), nil
|
|
81
|
83
|
}
|
|
|
84
|
+ if tokens[0] == "echo" {
|
|
|
85
|
+ return makeCommand(CommandTypeBuiltinEcho, tokens), nil
|
|
|
86
|
+ }
|
|
82
|
87
|
return makeCommandNone(), fmt.Errorf("%s: command not found", tokens[0])
|
|
83
|
88
|
}
|
|
84
|
89
|
|
|
|
@@ -89,6 +94,8 @@ func handleCommand(ctx Context, command Command) {
|
|
89
|
94
|
return
|
|
90
|
95
|
case CommandTypeBuiltinExit:
|
|
91
|
96
|
os.Exit(0)
|
|
|
97
|
+ case CommandTypeBuiltinEcho:
|
|
|
98
|
+ fmt.Fprintln(ctx.stdout, strings.Join(command.tokens[1:], " "))
|
|
92
|
99
|
}
|
|
93
|
100
|
}
|
|
94
|
101
|
|