-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.c
291 lines (262 loc) · 7.15 KB
/
tree.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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include "tree.h"
#include "word_list.h"
#define INPUT "<"
#define OUTPUT ">"
#define APPEND ">>"
#define PIPE "|"
#define BACKGROUND "&"
#define NEXT ";"
#define AAND "&&"
#define OOR "||"
#define OPEN "("
#define CLOSE ")"
jmp_buf jbuf;
jmp_buf notree;
int root_is_set = 0;
tree Root = NULL;
char specialChars[] = {'|', '&', ';', '>', '<', '(', ')'};
char* specialWords[] = {"&", "|", "&&", "||", ";", ">>", ">", "<", "(", ")"};
char* betweens[] = {"&", "|", "&&", "||", ";"};
void error(char *str){
printf("\x1B[33mError\x1B[0m: %s", str);
printf("\n");
longjmp(notree, 1);
}
void error_tree(char *str, tree Node){
freeTree(Root); // Cleanup of the partially constructed tree
printf("\x1B[33mError\x1B[0m: %s", str);
printf("\n");
longjmp(jbuf, 1);
}
void freeTreeNoSub(tree t){
if (t != NULL){
//freeTree(t->psubcmd);
freeTree(t->pipe);
freeTree(t->next);
for (int i = 0; i < t->argc; i++){
free(t->argv[i]);
}
free(t->infile);
free(t->outfile);
free(t->argv);
free(t);
}
}
void freeTree(tree t){
if (t != NULL){
freeTree(t->psubcmd);
freeTree(t->pipe);
freeTree(t->next);
for (int i = 0; i < t->argc; i++){
free(t->argv[i]);
}
free(t->infile);
free(t->outfile);
free(t->argv);
free(t);
}
}
tree createNode(tree Node) {
tree newNode = (tree)malloc(sizeof(struct Tree));
newNode->argc = 0;
newNode->argv = NULL;
newNode->infile=NULL;
newNode->outfile=NULL;
newNode->backgrnd = 0;
newNode->type = 0;
newNode->append=0;
newNode->psubcmd = NULL;
newNode->pipe = NULL;
newNode->next = NULL;
return newNode;
}
void add_argv(tree Node, char* str){
Node->argv = realloc(Node->argv, (Node->argc + 1) * (sizeof(char*)));
if(str != NULL){
Node->argv[Node->argc] = strdup(str);
} else {
Node->argv[Node->argc] = NULL;
}
Node->argc++;
}
int checkBrackets(Array* wordList){
struct nod {
struct nod* next;
};
typedef struct nod* st;
st stack = NULL, tmp;
for (int i = 0; i < wordList->used; i++){
if (strcmp(wordList->array[i], "(") == 0){
tmp = (st)malloc(sizeof(struct nod));
if (tmp == NULL) {
printf("Stack malloc error");
exit(-1);
}
tmp->next = stack;
stack = tmp;
}
if (strcmp(wordList->array[i], ")") == 0){
if (stack == NULL)
return 7;
tmp = stack;
stack = stack->next;
free(tmp);
}
}
if (stack == NULL){
return 0;
}
while (stack != NULL){
st tmp = stack;
stack = stack->next;
free(tmp);
}
return 9;
}
int isSplitter(char * str){
int j;
for(j = 0; j < sizeof(betweens) / sizeof(char*); j++){
if (strcmp(str, betweens[j]) == 0){
return 1;
}
}
return 0;
}
int isSpecialWord(char * str){
int j;
for(j = 0; j < sizeof(specialWords) / sizeof(char*); j++){
if (strcmp(str, specialWords[j]) == 0){
return 1;
}
}
return 0;
}
int checkwordList(Array *wordList){
int i, j, balance;
char * files[] = {INPUT, OUTPUT, APPEND};
if ((balance = checkBrackets(wordList))) return balance; // Check brackets balance
for(j = 1; j < sizeof(betweens) / sizeof(char*); j++){ //Check last symbol
if (strcmp(wordList->array[wordList->used - 1], betweens[j]) == 0){
return 1;
}
}
for (i = 0; i < wordList->used; i++){
if (strcmp(wordList->array[i], OPEN) == 0){ // "("
if (strcmp(wordList->array[i + 1], CLOSE) == 0) return 7; // "()"
if(i != 0){
if (strcmp(wordList->array[i - 1], OPEN) == 0) continue; // "("
if (!isSplitter(wordList->array[i - 1])) return 6; // "(" only after splitter
}
}
if (strcmp(wordList->array[i], CLOSE) == 0){ // ")"
if (strcmp(wordList->array[i - 1], BACKGROUND) == 0) continue;
if (isSplitter(wordList->array[i - 1])) return 7; // ")" after splitter
if (i != wordList->used - 1){
if (!isSpecialWord(wordList->array[i + 1])) return 10; // unexpected command after ")"
}
}
for(j = 0; j < sizeof(files) / sizeof(char*); j++){ // FILEs
if (strcmp(wordList->array[i], files[j]) == 0){
if (i == wordList->used - 1){
return 3;
}
if (isSpecialWord(wordList->array[i+1])){ // only file name
return 5;
}
}
}
for(j = 1; j < sizeof(betweens) / sizeof(char*); j++){ // NEXT (splitter)
if (strcmp(wordList->array[i], betweens[j]) == 0){
if (i == 0) return 2; // Splitter at the beggining
if (i == wordList->used - 1){ // Splitter at the end
return 1;
}
if (isSplitter(wordList->array[i+1])){ // Two splitters in a row
return 2;
}
}
}
if (strcmp(wordList->array[i], BACKGROUND) == 0){ // "&"
if (i == 0) return 2; // Splitter at the beggining
if (i == wordList->used - 1){ // If "&" at the end of the list - Success
return 0;
}
if (isSplitter(wordList->array[i+1])) { // If splitter after "&"
return 2;
}
}
}
return 0; // List is correct
}
tree listToNode(tree Node, Array *wordList, int start, int* newstart){ // until |
int i, j;
if (wordList->used == 0) return NULL; // if empty list of words
if (start == wordList->used) return NULL; // if end of list
if (strcmp(wordList->array[start], CLOSE) == 0) return NULL; // if "()" together
Node = createNode(Node);
if (strcmp(wordList->array[start], OPEN) == 0){ // "("
Node->psubcmd = listToNode(Node->psubcmd, wordList, start + 1, newstart);
start = *newstart; // Continue after corresponding ")"
}
for (i = start; i < wordList->used; i++){
if (strcmp(wordList->array[i], CLOSE) == 0){ // ")"
*newstart = i + 1;
add_argv(Node, NULL);
return Node;
}
if (strcmp(wordList->array[i], INPUT) == 0){ // "<"
Node->infile = strdup(wordList->array[++i]);
} else
if (strcmp(wordList->array[i], OUTPUT) == 0){ // ">"
Node->outfile = strdup(wordList->array[++i]);
} else
if (strcmp(wordList->array[i], APPEND) == 0){ // ">>"
Node->outfile = strdup(wordList->array[++i]);
Node->append = 1;
} else
if (strcmp(wordList->array[i], NEXT) == 0){ // ";"
add_argv(Node, NULL);
Node->type = 0;
Node->next = listToNode(Node->next, wordList, i + 1, newstart);
return Node;
} else
if (strcmp(wordList->array[i], AAND) == 0){ // "&&"
add_argv(Node, NULL);
Node->type = 1;
Node->next = listToNode(Node->next, wordList, i + 1, newstart);
return Node;
} else
if (strcmp(wordList->array[i], OOR) == 0){ // "||"
add_argv(Node, NULL);
Node->type = 2;
Node->next = listToNode(Node->next, wordList, i + 1, newstart);
return Node;
} else
if (strcmp(wordList->array[i], PIPE) == 0){ // "|"
add_argv(Node, NULL);
Node->pipe = listToNode(Node->pipe, wordList, i + 1, newstart);
return Node;
} else
if (strcmp(wordList->array[i], BACKGROUND) == 0){ // "&"
add_argv(Node, NULL);
Node->backgrnd = 1;
int separates = 1;
if (i == wordList->used - 1){ // If "&" at the end of the entire list
return Node;
}
if (strcmp(wordList->array[i + 1], OPEN) == 0) continue; // "("
if (strcmp(wordList->array[i + 1], CLOSE) == 0) continue; // ")"
if (!isSplitter(wordList->array[i + 1])){ // If "&" separates commands
Node->next = listToNode(Node->next, wordList, i + 1, newstart);
return Node;
}
} else {
add_argv(Node, wordList->array[i]);
}
}
add_argv(Node, NULL);
return Node;
}