-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate.go
276 lines (213 loc) · 5.88 KB
/
validate.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/**
* Validate commands entered from the user interface.
*
* Commands coming from the user interface are shorter versions of the actual
* DBGp commands. This affords ease of use. Here we verify these shorter
* commands so that we can later prepare the full DBGp commands.
*/
package command
import (
"fmt"
"strconv"
)
/**
* Wrapper for validating any command coming from the interface.
*/
func Validate(cmd string, args []string) (err error) {
DBGpCmd := resolveAlias(cmd)
switch DBGpCmd {
default:
err = fmt.Errorf("Unknown command.")
case "breakpoint_set":
err = validateBreakpointArgs(args)
case "breakpoint_get":
err = validateBreakpointGetArgs(args)
case "breakpoint_remove":
err = validateBreakpointRemoveArgs(args)
case "breakpoint_list":
err = validateCmdWithNoArg("breakpoint_list", args)
case "context_get":
err = validateContextGetArgs(args)
case "eval":
err = validateCmdWithNoArg("eval", args)
case "run":
err = validateCmdWithNoArg("run", args)
case "dbgp":
err = validateRawDBGpArgs(args)
case "property_get":
err = validatePropertyGetArgs(args)
case "feature_set":
err = validateFeatureSetArgs(args)
case "stack_get":
err = validateCmdWithNoArg("stack_get", args)
case "source":
err = validateSourceArgs(args)
case "stop":
err = validateCmdWithNoArg("stop", args)
case "status":
err = validateCmdWithNoArg("status", args)
case "step_into":
err = validateCmdWithNoArg("step_into", args)
case "step_out":
err = validateCmdWithNoArg("step_out", args)
case "step_over":
err = validateCmdWithNoArg("step_over", args)
}
return err
}
/**
* Validate the Breakpoint command.
*/
func validateBreakpointArgs(args []string) (err error) {
if len(args) != 2 {
err = fmt.Errorf("Usage: breakpoint_set filepath line-number")
return err
}
line_number, err := strconv.ParseInt(args[1], 10, 64)
if nil != err || line_number < 1 {
err = fmt.Errorf("Expecting line number as the second argument. %s given.", args[1])
}
return err
}
/**
* Validate the Breakpoint get command.
*/
func validateBreakpointGetArgs(args []string) (err error) {
if len(args) != 1 {
err = fmt.Errorf("Usage: breakpoint_get breakpoint-id")
return err
}
breakpointId, err := strconv.ParseInt(args[0], 10, 64)
_ = breakpointId
if nil != err {
err = fmt.Errorf("Expecting breakpoint ID as the first argument. %s given.", args[0])
}
return err
}
/**
* Validate the Breakpoint remove command.
*/
func validateBreakpointRemoveArgs(args []string) (err error) {
if len(args) != 1 {
err = fmt.Errorf("Usage: breakpoint_remove breakpoint-id")
return err
}
breakpointId, err := strconv.ParseInt(args[0], 10, 64)
_ = breakpointId
if nil != err {
err = fmt.Errorf("Expecting a breakpoint ID as the only argument. %s given.", args[0])
}
return err
}
/**
* Validate any command that does not take any argument except TX ID.
*
* Example: run, stop, etc.
*/
func validateCmdWithNoArg(cmd string, args []string) (err error) {
if 0 != len(args) {
err = fmt.Errorf("The \"%s\" command does not take any argument.", cmd)
}
return err
}
/**
* Validate the Source command.
*
* Valid formats:
* - source line-number line-count
* - source filepath
* Examples:
* - source 14 5: This should return 5 lines *starting* at line number 14.
* - source foo/bar/baz.php: This should return the complete source code of
* baz.php.
*/
func validateSourceArgs(args []string) (err error) {
argCount := len(args)
if argCount != 1 && argCount != 2 {
err = fmt.Errorf("The \"source\" command takes a filepath OR two numbers as argument.")
return err
}
if argCount == 2 {
lineNumber, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
lineCount, err := strconv.ParseInt(args[1], 10, 64)
if err != nil {
return err
}
if lineNumber < 1 {
err = fmt.Errorf("Invalid line number.")
}
if lineCount < 1 {
err = fmt.Errorf("Invalid line count.")
}
}
return err
}
/**
* Validate the Raw DBGp command.
*
* Valid format: dbgp dbgp-command [dbgp-command-args]
* Example: dbgp breakpoint_list
* This should execute the breakpoint_list command without validation.
*/
func validateRawDBGpArgs(args []string) (err error) {
if len(args) < 1 {
err = fmt.Errorf("The \"dbgp\" command expects at least one argument.")
return err
}
return err
}
/**
* Validate the property_get command.
*
* Format: property_get VARIABLE-NAME, property_get global GLOBAL-VARIABLE-NAME,
* property_get local LOCAL-VARIABLE-NAME,
*/
func validatePropertyGetArgs(args []string) (err error) {
if len(args) < 1 {
err = fmt.Errorf("The \"property_get\" command takes an *optional* global or local context name and a variable name as its arguments.")
return err
}
if (args[0] == globalContextLabel || args[0] == localContextLabel) && len(args) < 2 {
err = fmt.Errorf("The \"property_get\" command needs at least a variable name as its argument.")
return err
}
return err
}
/**
* Validate the arguments for the context_get command.
*
* Acceptable command formats: context_get, context_get local/global,
* context_get local/global STACK-DEPTH-NUM
*/
func validateContextGetArgs(args []string) (err error) {
argCount := len(args)
if argCount == 0 {
return err
}
if argCount == 1 && (args[0] != localContextLabel && args[0] != globalContextLabel) {
err = fmt.Errorf("Invalid context name. Acceptable values: %s, %s. %s given.", localContextLabel, globalContextLabel, args[0])
return err
}
if argCount == 2 {
_, err = strconv.Atoi(args[1])
} else if argCount > 2 {
err = fmt.Errorf("Too many arguments.")
}
return err
}
/**
* feature_set command.
*
* Acceptable command format: feature_set feature-name feature-value
*/
func validateFeatureSetArgs(args []string) (err error) {
argCount := len(args)
if argCount != 2 {
err = fmt.Errorf("The feature_set command takes two arguments.")
return err
}
return err
}