浏览代码

Do token number asserts outside Command (replace .tokens with .name and .args)

Alois Mahdal 6 天前
父节点
当前提交
df61ce2938
共有 1 个文件被更改,包括 22 次插入12 次删除
  1. 22
    12
      app/main.go

+ 22
- 12
app/main.go 查看文件

34
 
34
 
35
 type Command struct {
35
 type Command struct {
36
 	command_type CommandType
36
 	command_type CommandType
37
-	tokens       []string
37
+	args         []string
38
 	exec_path    string
38
 	exec_path    string
39
+	name         string
39
 }
40
 }
40
 
41
 
41
 func makeCommandNone() Command {
42
 func makeCommandNone() Command {
42
 	return Command{
43
 	return Command{
43
 		command_type: CommandTypeNone,
44
 		command_type: CommandTypeNone,
44
-		tokens:       nil,
45
+		args:         nil,
45
 		exec_path:    "",
46
 		exec_path:    "",
47
+		name:         "",
46
 	}
48
 	}
47
 }
49
 }
48
 
50
 
53
 	case CommandTypeExternal:
55
 	case CommandTypeExternal:
54
 		panic("invalid usage")
56
 		panic("invalid usage")
55
 	default:
57
 	default:
58
+		if len(tokens) < 1 {
59
+			panic("assertion failed: token list must not be empty")
60
+		}
56
 		return Command{
61
 		return Command{
57
 			command_type: command_type,
62
 			command_type: command_type,
58
-			tokens:       tokens,
63
+			name:         tokens[0],
64
+			args:         tokens[1:],
59
 		}
65
 		}
60
 	}
66
 	}
61
 }
67
 }
62
 
68
 
63
 func makeCommandExternal(tokens []string, exec_path string) Command {
69
 func makeCommandExternal(tokens []string, exec_path string) Command {
70
+	if len(tokens) < 1 {
71
+		panic("assertion failed: token list must not be empty")
72
+	}
64
 	return Command{
73
 	return Command{
65
 		command_type: CommandTypeExternal,
74
 		command_type: CommandTypeExternal,
66
-		tokens:       tokens,
75
+		name:         tokens[0],
76
+		args:         tokens[1:],
67
 		exec_path:    exec_path,
77
 		exec_path:    exec_path,
68
 	}
78
 	}
69
 }
79
 }
153
 		os.Exit(0)
163
 		os.Exit(0)
154
 
164
 
155
 	case CommandTypeBuiltinEcho:
165
 	case CommandTypeBuiltinEcho:
156
-		fmt.Fprintln(ctx.stdout, strings.Join(command.tokens[1:], " "))
166
+		fmt.Fprintln(ctx.stdout, strings.Join(command.args, " "))
157
 		return 0
167
 		return 0
158
 
168
 
159
 	case CommandTypeBuiltinPwd:
169
 	case CommandTypeBuiltinPwd:
160
-		if len(command.tokens) != 1 {
170
+		if len(command.args) != 0 {
161
 			fmt.Fprintln(ctx.stderr, "usage: pwd")
171
 			fmt.Fprintln(ctx.stderr, "usage: pwd")
162
 			return 2
172
 			return 2
163
 		}
173
 		}
169
 		return 0
179
 		return 0
170
 
180
 
171
 	case CommandTypeBuiltinType:
181
 	case CommandTypeBuiltinType:
172
-		if len(command.tokens) != 2 {
182
+		if len(command.args) != 1 {
173
 			fmt.Fprintln(ctx.stderr, "usage: type COMMAND")
183
 			fmt.Fprintln(ctx.stderr, "usage: type COMMAND")
174
 			return 2
184
 			return 2
175
 		}
185
 		}
176
-		parsed, err := parseCommand(command.tokens[1])
186
+		parsed, err := parseCommand(command.args[0])
177
 		if err != nil {
187
 		if err != nil {
178
 			fmt.Fprintln(ctx.stderr, err)
188
 			fmt.Fprintln(ctx.stderr, err)
179
 			return 0
189
 			return 0
182
 		case CommandTypeNone:
192
 		case CommandTypeNone:
183
 			panic("impossible parseCommand() result")
193
 			panic("impossible parseCommand() result")
184
 		case CommandTypeExternal:
194
 		case CommandTypeExternal:
185
-			fmt.Fprintf(ctx.stdout, "%s is %s\n", parsed.tokens[0], parsed.exec_path)
195
+			fmt.Fprintf(ctx.stdout, "%s is %s\n", parsed.name, parsed.exec_path)
186
 		default:
196
 		default:
187
-			fmt.Fprintf(ctx.stdout, "%s is a shell builtin\n", parsed.tokens[0])
197
+			fmt.Fprintf(ctx.stdout, "%s is a shell builtin\n", parsed.name)
188
 		}
198
 		}
189
 		return 0
199
 		return 0
190
 
200
 
191
 	case CommandTypeExternal:
201
 	case CommandTypeExternal:
192
-		cmd := exec.Command(command.exec_path, command.tokens[1:]...)
193
-		cmd.Args = command.tokens
202
+		cmd := exec.Command(command.exec_path, command.args...)
203
+		cmd.Args[0] = command.name
194
 		cmd.Stdout = ctx.stdout
204
 		cmd.Stdout = ctx.stdout
195
 		cmd.Stderr = ctx.stderr
205
 		cmd.Stderr = ctx.stderr
196
 		cmd.Stdin = ctx.stdin
206
 		cmd.Stdin = ctx.stdin