-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx_cmd_parser.c
More file actions
248 lines (224 loc) · 6.59 KB
/
mx_cmd_parser.c
File metadata and controls
248 lines (224 loc) · 6.59 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <mx_cmd_parser.h>
#include <mx_linkedlist.h>
#include <mx_debug.h>
#include <mx_utils.h>
#include <unistd.h>
#define MAX_PRECEDENCE_LEVEL 5
/*
* The table the maintians the precedence array.
*/
linked_list_t cmd_precedence_array[MAX_PRECEDENCE_LEVEL];
/*
* Print the command token, information.
*/
void
parser_print_cmd_token (void *data)
{
int index = 0;
cmd_token_t *cmd_token = data;
printf("{");
while (index < MAX_CMD_ARGS) {
if (cmd_token->args[index]) {
printf("[%d]%s,",index, cmd_token->args[index]);
}
index++;
}
printf("RFD:%d,WFD:%d",cmd_token->pipefds[0], cmd_token->pipefds[1]);
printf("} ");
}
/**
* Print the command precedence array.
*/
void
print_cmd_precedence_array ()
{
int index = 0;
linked_list_t *linked_list = NULL;
while (index < MAX_PRECEDENCE_LEVEL) {
linked_list = &cmd_precedence_array[index];
printf("\nPrecedence %d: total commands %d:", index,
linked_list_get_node_count(linked_list));
linked_list_walk(linked_list, parser_print_cmd_token);
index++;
}
printf("\n");
}
/*
* Free the parser command token.
*/
void parser_cmd_token_free_func (void *data)
{
cmd_token_t *cmd_token = data;
int args_index = 0;
log("Free %s token", cmd_token->args[0]);
while (args_index < MAX_CMD_ARGS) {
if (cmd_token->args[args_index]) {
free(cmd_token->args[args_index]);
}
args_index++;
}
free(cmd_token);
}
/*
* Get the total number of command tokens.
*/
int parser_get_total_commands ()
{
int i = 0, cnt = 0;
while (i < MAX_PRECEDENCE_LEVEL) {
cnt += linked_list_get_node_count(&cmd_precedence_array[i]);
i++;
}
return cnt;
}
/*
* Initialize the precedence array.
*/
void parser_init_precedence_array ()
{
int i = 0;
while (i < MAX_PRECEDENCE_LEVEL) {
linked_list_init(&cmd_precedence_array[i], parser_cmd_token_free_func);
i++;
}
}
/*
* Cleanup the tokens.
*/
void parser_cleanup ()
{
int i = 0;
while (i < MAX_PRECEDENCE_LEVEL) {
linked_list_destroy(&cmd_precedence_array[i]);
i++;
}
}
/*
* Add the token to the precedence list.
*/
bool parser_add_cmd_token_to_precedence_array
(linked_list_t *list,
cmd_token_t *cmd_token)
{
return (linked_list_add_node(list, (void *)cmd_token));
}
/*
* Create a new command token.
*/
cmd_token_t* parser_cmd_token_new ()
{
cmd_token_t *cmd_token = NULL;
cmd_token = malloc(sizeof(cmd_token_t));
if (!cmd_token) {
return NULL;
}
memset(cmd_token, 0, sizeof(cmd_token_t));
cmd_token->pipefds[0] = -1;
cmd_token->pipefds[1] = -1;
return (cmd_token);
}
/*
* Parse the input command.
*/
bool parse_shell_input_cmd (char* inputcmdbuff, int cmdlen)
{
char ch;
int curr_precedence_level = MAX_PRECEDENCE_LEVEL;
int cmd_index = 0, cmd_arg_index = 0, curr_index = 0;
int sub_cmd_start_index = 0;
cmd_token_t *cmd_token = NULL;
int cmd_param_start_index = 0;
int cnt = 0;
parser_init_precedence_array();
while (cmd_index < cmdlen -1) {
ch = inputcmdbuff[cmd_index];
switch (ch) {
case '(':
// New precedence level.
curr_precedence_level--;
if (curr_precedence_level < 0) {
print_cmd_error(inputcmdbuff, cmd_index);
printf("\nOnly %d level of precedence is supported\n",
MAX_PRECEDENCE_LEVEL);
goto parser_cleanup;
}
if (cmd_token) {
// we are still parsing the command.
print_cmd_error(inputcmdbuff, cmd_index-1);
printf("\nEvery command should end with a \',\'\n");
goto parser_cleanup;
}
break;
case ')':
if (cmd_token &&
!parser_add_cmd_token_to_precedence_array(
&cmd_precedence_array[curr_precedence_level],
cmd_token)) {
goto parser_cleanup;
}
cmd_token = NULL;
curr_precedence_level++;
if (curr_precedence_level > MAX_PRECEDENCE_LEVEL) {
print_cmd_error(inputcmdbuff, cmd_index);
printf("\nOnly %d level of precedence is supported\n",
MAX_PRECEDENCE_LEVEL);
goto parser_cleanup;
}
if ((curr_precedence_level == MAX_PRECEDENCE_LEVEL) &&
cmd_index !=(cmdlen-2)) {
// We have some more cmds to parse but we have reached EOB
print_cmd_error(inputcmdbuff, cmd_index);
printf("\nMismatched braces\n");
goto parser_cleanup;
}
break;
case ',':
// New command;
if (cmd_token && !parser_add_cmd_token_to_precedence_array(
&cmd_precedence_array[curr_precedence_level],
cmd_token)) {
goto parser_cleanup;
}
cmd_token = NULL;
break;
case ' ':
break;
default:
cmd_param_start_index = cmd_index;
if (!cmd_token) {
// this is start of a new token.
cmd_token = parser_cmd_token_new();
cmd_arg_index = 0;
} else {
cmd_arg_index++;
}
while (cmd_index < cmdlen) {
ch = inputcmdbuff[cmd_index];
if (ch == ' ' || ch == ',' || ch == '(' || ch == ')') {
cmd_index--;
break;
}
cmd_index++;
}
if (cmd_index == cmdlen) {
print_cmd_error(inputcmdbuff, cmd_param_start_index);
printf("\nIllegal Command provided\n");
goto parser_cleanup;
}
cmd_token->args[cmd_arg_index] =
strndup(inputcmdbuff+ cmd_param_start_index,
cmd_index-cmd_param_start_index+1);
}
cmd_index++;
}
#ifdef DEBUG
// print_cmd_precedence_array();
#endif
return true;
parser_cleanup:
return false;
}