-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_mapping.c
272 lines (233 loc) · 8.89 KB
/
command_mapping.c
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
#include "command_mapping.h"
shell_status command_mapping(Command* user_command){
assert(user_command);
shell_status status;
status = tokenizing(user_command);
if(status != TOKENIZING_SUCCESS) return status;
status = command_mapping_type(user_command);
if(status != VALID_COMMAND_TYPE) return status;
status = check_command_parameter(user_command);
if(status != VALID_PARAMETERS) return status;
return status;
}
shell_status tokenizing(Command* user_command){
assert(user_command);
static char raw_command[COMMAND_MAX_LEN];
char cm;
int i = 0, comma_cnt = 0, len_raw, flag = 0, flag2 = 0;
strncpy (raw_command, user_command->raw_command, COMMAND_MAX_LEN);
user_command->token_cnt = 0;
user_command->tokens[user_command->token_cnt] = strtok(raw_command, " ,\t\n");
while (user_command->token_cnt <= TOKEN_MAX_NUM && user_command->tokens[user_command->token_cnt])
user_command->tokens[++user_command->token_cnt] = strtok (NULL, " ,\t\n");
if(!user_command->tokens[0]) return INVALID_INPUT;
len_raw = (int)strlen(user_command->raw_command);
// ex) du 1,,1 [x]
for(i=0;i<len_raw;i++){
if(user_command->raw_command[i] == ',')
comma_cnt++;
}
// ex) du , 1 1 [x]
flag = 0; // flag 가 1이라는 것은 아직 0번째 토큰을 지나지 않았다는 의미.
for(i=0;i<len_raw;i++){
cm = user_command->raw_command[i];
if(flag == 0){
if(cm == ',') return INVALID_INPUT;
if(cm != ' ' && cm != '\t') flag = 1;
continue;
} else if(flag == 1){
if(cm == ',') return INVALID_INPUT;
if(cm == ' ' || cm == '\t') flag = 2;
continue;
}
if(user_command->raw_command[i] == ' ') continue;
if(user_command->raw_command[i] == '\t') continue;
if(user_command->raw_command[i] != ',') break;
else return INVALID_INPUT;
}
// ex) du 1 1 , [x]
for(i=len_raw-2;i>=0;i--){
if(user_command->raw_command[i] == ' ') continue;
if(user_command->raw_command[i] == '\t') continue;
if(user_command->raw_command[i] != ',') break;
else return INVALID_INPUT;
}
// if(comma_cnt >= 3) return INVALID_INPUT;
if(user_command->token_cnt <= 2 && comma_cnt == 0)
return TOKENIZING_SUCCESS;
if((int)user_command->token_cnt != comma_cnt + 2)
return INVALID_INPUT;
if((int)user_command->token_cnt > TOKEN_MAX_NUM)
return TOO_MANY_TOKEN;
if((int)user_command->token_cnt <= 0)
return INVALID_INPUT;
// ex) f 1 ,, 2 3 [x]
// , 다음에는 [ ] [\t]이 나오다가 [ ] [\t] [,]이 아닌 값이 나와야한다.
flag = 0; // 콤마가 나오면 flag 는 1 로 놓자.
for(i=0;i<len_raw;i++){
cm = user_command->raw_command[i];
if(flag == 1){
if(cm == ',') return INVALID_PARAMETERS;
if(cm == ' ' || cm == '\t') continue;
flag = 0;
continue;
}
if(cm == ','){
flag = 1;
continue;
}
}
return TOKENIZING_SUCCESS;
}
shell_status command_mapping_type(Command *user_command){
assert(user_command);
char* first_token = user_command->tokens[0];
if(COMPARE_STRING(first_token, "h") ||
COMPARE_STRING(first_token, "help")) {
user_command->type = TYPE_HELP;
}else if(COMPARE_STRING(first_token, "d") ||
COMPARE_STRING(first_token, "dir")) {
user_command->type = TYPE_DIR;
}else if(COMPARE_STRING(first_token, "q") ||
COMPARE_STRING(first_token, "quit")) {
user_command->type = TYPE_QUIT;
} else if(COMPARE_STRING(first_token, "hi") ||
COMPARE_STRING(first_token, "history")){
user_command->type = TYPE_HISTORY;
} else if(COMPARE_STRING(first_token, "du") ||
COMPARE_STRING(first_token, "dump")){
user_command->type = TYPE_DUMP;
} else if(COMPARE_STRING(first_token, "e") ||
COMPARE_STRING(first_token, "edit")){
user_command->type = TYPE_EDIT;
} else if(COMPARE_STRING(first_token, "f") ||
COMPARE_STRING(first_token, "fill")){
user_command->type = TYPE_FILL;
} else if(COMPARE_STRING(first_token, "reset")){
user_command->type = TYPE_RESET;
} else if(COMPARE_STRING(first_token, "opcode")){
user_command->type = TYPE_OPCODE;
} else if(COMPARE_STRING(first_token, "opcodelist")){
user_command->type = TYPE_OPCODELIST;
} else {
return INVALID_COMMAND_TYPE;
}
return VALID_COMMAND_TYPE;
}
shell_status check_command_parameter(Command* user_command){
assert(user_command);
if((user_command->type == TYPE_QUIT ||
user_command->type == TYPE_HELP ||
user_command->type == TYPE_HISTORY ||
user_command->type == TYPE_DIR ||
user_command->type == TYPE_RESET ||
user_command->type == TYPE_OPCODELIST) &&
user_command->token_cnt > 1
)
return INVALID_PARAMETERS;
if(user_command->type == TYPE_OPCODE)
return check_opcode_parameters(user_command);
if(user_command->type == TYPE_EDIT)
return check_edit_parameters(user_command);
if(user_command->type == TYPE_FILL)
return check_fill_parameters(user_command);
if(user_command->type == TYPE_DUMP)
return check_dump_parameters(user_command);
return VALID_PARAMETERS;
}
bool is_zero(char* str){
int len = (int)strlen(str);
int i;
for(i=0;i<len;i++)
if(str[i] != '0')
return false;
return true;
}
bool is_valid_hex(char* str){
int l = (int)strlen(str), i;
for(i=0;i<l;i++) {
if ('0' <= str[i] &&
str[i] <= '9')
continue;
if('A' <= str[i] &&
str[i] <= 'F')
continue;
if('a' <= str[i] &&
str[i] <= 'f')
continue;
return false;
}
return true;
}
bool is_valid_address(char* str){
int target = (int)strtol(str, NULL, 16);
if(target < 0) return false; // 0 보다 큰지 검증
if(target == 0 && !is_zero(str)) return false; // 올바른 hex 값인지 검증
if(target >= MB) return false; // 범위 내에 있는지 검증
if(!is_valid_hex(str)) return false; // 올바른 hex 값인지 검증
return true;
}
// [TODO] dump 파라미터 검증 함수 구현은 일단 완료했는데, 테스트 해보자....
shell_status check_dump_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_DUMP);
int tok1, tok2;
if(user_command->token_cnt > 1) {
if (!is_valid_address(user_command->tokens[1]))
return INVALID_PARAMETERS;
else if (user_command->token_cnt == 2)
return VALID_PARAMETERS;
if (!is_valid_address(user_command->tokens[2]))
return INVALID_PARAMETERS;
tok1 = (int) strtol(user_command->tokens[1], NULL, 16);
tok2 = (int) strtol(user_command->tokens[2], NULL, 16);
if (tok1 > tok2) return INVALID_PARAMETERS;
}
if(user_command->token_cnt > 3)
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
// [TODO] opcode 파라미터 검증 구현
shell_status check_opcode_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_OPCODE);
if(user_command->token_cnt == 1) return MISSING_REQUIRE_PARAMETER;
if(user_command->token_cnt >= 3) return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
// [TODO] edit parameter 검증 구현 완료!
shell_status check_edit_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_EDIT);
int value;
if(user_command->token_cnt != 3)
return INVALID_PARAMETERS;
if(!is_valid_address(user_command->tokens[1]))
return INVALID_PARAMETERS;
if(!is_valid_hex(user_command->tokens[2]))
return INVALID_PARAMETERS;
value = (int) strtol(user_command->tokens[2], NULL, 16);
if(!(0 <= value && value <= 0xFF))
return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}
// [TODO] fill 파라미터 검증 구현 완료!!!
shell_status check_fill_parameters(Command *user_command){
assert(user_command);
assert(user_command->type == TYPE_FILL);
int value, start, end;
if(user_command->token_cnt != 4)
return INVALID_PARAMETERS;
if(!is_valid_address(user_command->tokens[1]))
return INVALID_PARAMETERS;
if(!is_valid_address(user_command->tokens[2]))
return INVALID_PARAMETERS;
start = (int) strtol(user_command->tokens[1], NULL, 16);
end = (int) strtol(user_command->tokens[2], NULL, 16);
if (start > end) return INVALID_PARAMETERS;
value = (int) strtol(user_command->tokens[3], NULL, 16);
if(!is_valid_hex(user_command->tokens[3]))
return INVALID_PARAMETERS;
if(!(0 <= value && value <= 0xFF)) return INVALID_PARAMETERS;
return VALID_PARAMETERS;
}