-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.c
243 lines (216 loc) · 6.49 KB
/
parser.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
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "parser.h"
#define bool char
#define true 1
#define false 0
struct words{
char *data;
struct words *next;
};
void free_array(char** args)
{
char** str;
for(str = args; *str != NULL; str++){
free(*str);
}
free(args);
}
void destruct_words(struct words *p)
{
struct words *q;
while(p != NULL){
q = p->next;
free(p);
p = q;
}
}
void destruct_chain(struct exec_node *p)
{
struct exec_node *q;
while(p != NULL){
q = p->next;
free_array(p->args);
free(p->input);
free(p->output);
free(p);
p = q;
}
}
char** convert_words2array(struct words * list, long n)
{
char ** a = (char **) malloc (sizeof(char *) * (n+1));
struct words *p;
long int i;
for(i = 0, p = list; i < n; i++, p = p->next){
a[i] = p->data;
}
a[n] = NULL;
return a;
}
struct exec_node * parse_string(int * bg_run, int * err_status)
{
struct exec_node *chain, *node, *prev_node = NULL;
char** args;
char *string;
struct words *list, *word, *prev_word = NULL;
/*buffer defenitions */
const long int DEFAULT_BUFFER_SIZE = 64;
char *buffer = (char*) malloc (sizeof(char) * DEFAULT_BUFFER_SIZE);
long int buffer_size = DEFAULT_BUFFER_SIZE, buffer_actual_size = 0;
int quotes_on = false;
enum dirrection{
ARGUMENTS,
STDIN,
STDOUT
} dirr = ARGUMENTS;
long int count;
int ch;
*err_status = 0;
*bg_run = false;
node = (struct exec_node*) malloc (sizeof(*node));
node->input = NULL;
node->output = NULL;
node->args = NULL;
node->next = NULL;
chain = node;
word = (struct words*) malloc (sizeof(*word)); /* head word */
word->data = NULL;
word->next = NULL;
list = word;
count = 0;
do{
ch = getchar();
if(ch == EOF){
*err_status |= EOF_ERROR;
}
if(*bg_run == true && ch != EOF && !isspace(ch)){
*err_status |= BAD_AMPERSAND;
}
if(ch == '&'){
*bg_run = true;
}
if(ch == '"'){
quotes_on ^= true;
} else
if((!quotes_on && (isspace(ch) || ch == '&' || ch == '|' || ch == '<' || ch == '>'))
|| ch == EOF || ch == '\n'){
/* drop buffer to structure if not empty */
if(buffer_actual_size != 0){
long int i;
string = (char*) malloc((buffer_actual_size + 1) * sizeof(char));
for(i = 0; i < buffer_actual_size; i++){
string[i] = buffer[i];
}
string[buffer_actual_size] = '\0';
switch(dirr){
case ARGUMENTS:
word->next = (struct words *) malloc (sizeof(*word));
word->data = string;
prev_word = word;
word = word->next;
word->next = NULL;
count++;
break;
case STDIN:
if(node->input != NULL){
*err_status |= DUPLICATE_STDIN;
free(node->input);
}
node->input = string;
break;
case STDOUT:
if(node->output != NULL){
*err_status |= DUPLICATE_STDOUT;
free(node->output);
}
node->output = string;
break;
}
dirr = ARGUMENTS;
buffer_actual_size = 0;
}
} else {
/* add symbol to buffer */
if(buffer_actual_size >= buffer_size){
/* realloc buffer */
char *p;
long int i;
p = (char*) malloc (sizeof(char) * (buffer_size << 2));
/* copy buffer to a new place */
for(i = 0; i < buffer_size; i++){
p[i] = buffer[i];
}
free(buffer);
buffer = p;
buffer_size <<= 2;
}
buffer[buffer_actual_size++] = ch;
}
/* specian situations */
if(!quotes_on){
switch(ch){
case '&':
*bg_run = true;
break;
case '>':
dirr = STDOUT;
break;
case '<':
dirr = STDIN;
break;
default:
break;
}
}
/* new program args token */
if((ch == '|' && !quotes_on) || ch == '\n' || ch == EOF){
/* we need to create new convayor chain element */
/* check for empty string */
free(word);
if(prev_word == NULL){
list = NULL;
} else {
prev_word->next = NULL;
}
args = convert_words2array(list, count);
destruct_words(list);
/* push 'args' into chain */
node->args = args;
if(ch == '|'){
prev_node = node;
node->next = (struct exec_node*) malloc(sizeof(*node));
node = node->next;
node->next = NULL;
node->input = NULL;
node->output = NULL;
}
word = (struct words*) malloc (sizeof(*word)); /* head word */
word->data = NULL;
word->next = NULL;
list = word;
count = 0;
dirr = ARGUMENTS;
}
} while(ch != '\n' && ch != EOF);
/* free memory */
free(buffer);
/* check if the the string was empty */
if(chain->args[0] == NULL && chain->next == NULL){
destruct_chain(chain);
chain = NULL;
}
/* check for errors */
/* convayor error */
if(chain != NULL){
for(node = chain; node != NULL; node = node->next){
*err_status |= (node->args[0] == NULL) ? BAD_CONVEYOR : 0;
}
}
/* quote error */
*err_status |= (quotes_on) ? BAD_QUOTES : 0;
args = convert_words2array(list, count);
destruct_words(list);
return chain;
}