-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.y
606 lines (478 loc) · 11.3 KB
/
parser.y
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
%defines
%error-verbose
%locations
%{
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define __PARSER_H_INTERNAL_INCLUDE
#include "parser.h"
/*
* globalAllocMem is a pointer to an array of pointers. keeps track of the addresses to be freed
* globalAllocCount is number of allocated pointers
* globalAllocSize is the size of the allocated memory pool.
* needsFree is set when there is something in the memory pool.
*
* command_root is the root of the parse tree.
*/
static GenericPointer *globalAllocMem = NULL;
static size_t globalAllocCount = 0;
static size_t globalAllocSize = 0;
static bool needsFree = false;
static command_t *command_root = NULL;
void yyerror(const char* str);
/*
* this function makes sure that the memory pool is large enough.
* if the memory pool is empty it allocates newSize * sizeof(GenericPointer) bytes of memory.
* otherwise it reallocates (newSize + globalAllocSize) *sizeof(GenericPointer) bytes of memory.
*/
static void ensureSize(size_t newSize)
{
GenericPointer *newPtr;
assert(newSize > 0);
if (globalAllocSize == 0) {
assert(globalAllocMem == NULL);
globalAllocSize = newSize;
globalAllocMem = (GenericPointer *)malloc(sizeof(GenericPointer) * globalAllocSize);
if (globalAllocMem == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
return;
}
assert(globalAllocMem != NULL);
if (globalAllocSize >= newSize) {
return;
}
globalAllocSize += newSize;
newPtr = (GenericPointer *)realloc((void *)globalAllocMem, sizeof(GenericPointer) * globalAllocSize);
if (newPtr == NULL) {
fprintf(stderr, "realloc() failed\n");
exit(EXIT_FAILURE);
}
globalAllocMem = newPtr;
}
/*
* this function adds ptr to the memory pool.
*/
void add_to_memory_pool(const void *ptr)
{
if (ptr == NULL) {
fprintf(stderr, "malloc() failed\n");
exit(EXIT_FAILURE);
}
ensureSize(globalAllocCount + 1);
globalAllocMem[globalAllocCount++] = (GenericPointer)ptr;
}
/*
* this function creates a simple_command_t
*
* exe_name is the name of the executable
* params is a list of parameters
* red is a structure of redirections
*/
static simple_command_t *bind_parts(word_t *exe_name, word_t *params, redirect_t red)
{
simple_command_t *s = (simple_command_t *) malloc(sizeof(simple_command_t));
add_to_memory_pool(s);
memset(s, 0, sizeof(*s));
assert(exe_name != NULL);
assert(exe_name->next_word == NULL);
s->verb = exe_name;
s->params = params;
s->in = red.red_i;
s->out = red.red_o;
s->err = red.red_e;
s->io_flags = red.red_flags;
s->up = NULL;
return s;
}
/*
* this function creates a command_t storing a simple command pointed by scmd.
*
* cmd1 and cmd2 must to be NULL and op must be OP_NONE
*/
static command_t* new_command(simple_command_t *scmd)
{
command_t *c = (command_t *) malloc(sizeof(command_t));
add_to_memory_pool(c);
memset(c, 0, sizeof(*c));
c->up = c->cmd1 = c->cmd2 = NULL;
c->op = OP_NONE;
assert(scmd != NULL);
c->scmd = scmd;
scmd->up = c;
return c;
}
/*
* this function creates a command_t storing a compound command.
*
* it takes two arguments, cmd1 and cmd2, which are the two commands that need to be joined,
* and an operator op.
*
* scmd needs to be NULL
*/
static command_t* bind_commands(command_t *cmd1, command_t *cmd2, operator_t op)
{
command_t *c = (command_t *) malloc(sizeof(command_t));
add_to_memory_pool(c);
memset(c, 0, sizeof(*c));
c->up = NULL;
assert(cmd1 != NULL);
assert(cmd1->up == NULL);
c->cmd1 = cmd1;
cmd1->up = c;
assert(cmd2 != NULL);
assert(cmd2->up == NULL);
assert(cmd1 != cmd2);
c->cmd2 = cmd2;
cmd2->up = c;
assert((op > OP_NONE) && (op < OP_DUMMY));
c->op = op;
c->scmd = NULL;
return c;
}
/*
* this function creates a new word_t from the contents of str.
* if str is the name of an environment variable, expand must be true
*/
static word_t *new_word(const char *str, bool expand)
{
word_t *w = (word_t *) malloc(sizeof(word_t));
add_to_memory_pool(w);
memset(w, 0, sizeof(*w));
assert(str != NULL);
w->string = str;
w->expand = expand;
w->next_part = NULL;
w->next_word = NULL;
return w;
}
/*
* adds w to lsts part list.
*/
static word_t* add_part_to_word(word_t * w, word_t * lst)
{
word_t *crt = lst;
assert(lst != NULL);
assert(w != NULL);
/*
we could insert at the beginnig and then invert the list
but this would make the code a bit more complicated
thus, we assume we have small lists and O(n*n) is acceptable
*/
while (crt->next_part != NULL) {
crt = crt->next_part;
assert((crt == NULL) || (crt->next_word == NULL));
}
crt->next_part = w;
assert(w->next_part == NULL);
assert(w->next_word == NULL);
return lst;
}
/*
* adds w to the lst word list
*/
static word_t* add_word_to_list(word_t *w, word_t *lst)
{
word_t *crt = lst;
assert(w != NULL);
if (crt == NULL) {
assert(w->next_word == NULL);
return w;
}
assert(lst != NULL);
/*
same as above
*/
while (crt->next_word != NULL) {
crt = crt->next_word;
}
crt->next_word = w;
assert(w->next_word == NULL);
return lst;
}
%}
%union {
command_t * command_un;
const char * string_un;
redirect_t redirect_un;
simple_command_t * simple_command_un;
word_t * exe_un;
word_t * params_un;
word_t * word_un;
}
%token NOT_ACCEPTED_CHAR INVALID_ENVIRONMENT_VAR UNEXPECTED_EOF CHARS_AFTER_EOL
%token END_OF_FILE END_OF_LINE BLANK
%token REDIRECT_OE REDIRECT_O REDIRECT_E INDIRECT
%token REDIRECT_APPEND_E REDIRECT_APPEND_O
%token <string_un> WORD
%token <string_un> ENV_VAR
%left SEQUENTIAL
%left PARALLEL
%left CONDITIONAL_NZERO CONDITIONAL_ZERO
%left PIPE
%type <command_un> command
%type <exe_un> exe_name
%type <params_un> params
%type <redirect_un> redirect
%type <simple_command_un> simple_command
%type <word_un> word
%start command_tree
%%
command_tree:
command END_OF_LINE {
command_root = $1;
YYACCEPT;
}
| command END_OF_FILE {
command_root = $1;
YYACCEPT;
}
| END_OF_LINE {
command_root = NULL;
YYACCEPT;
}
| END_OF_FILE {
command_root = NULL;
YYACCEPT;
}
| BLANK END_OF_LINE {
command_root = NULL;
YYACCEPT;
}
| BLANK END_OF_FILE {
command_root = NULL;
YYACCEPT;
}
;
command:
simple_command {
$$ = new_command($1);
}
| command SEQUENTIAL command {
$$ = bind_commands($1, $3, OP_SEQUENTIAL);
}
| command PARALLEL command {
$$ = bind_commands($1, $3, OP_PARALLEL);
}
| command CONDITIONAL_ZERO command {
$$ = bind_commands($1, $3, OP_CONDITIONAL_ZERO);
}
| command CONDITIONAL_NZERO command {
$$ = bind_commands($1, $3, OP_CONDITIONAL_NZERO);
}
| command PIPE command {
$$ = bind_commands($1, $3, OP_PIPE);
}
;
simple_command:
exe_name BLANK params redirect {
$$ = bind_parts($1, $3, $4);
}
| exe_name BLANK params BLANK redirect {
$$ = bind_parts($1, $3, $5);
}
| exe_name redirect {
$$ = bind_parts($1, NULL, $2);
}
| exe_name BLANK redirect {
$$ = bind_parts($1, NULL, $3);
}
;
exe_name:
word {
$$ = $1;
}
| BLANK word {
$$ = $2;
}
;
params:
params BLANK word {
$$ = add_word_to_list($3, $1);
assert($$ == $1);
}
| word {
$$ = $1;
}
;
redirect:
{ /* empty */
$$.red_o = NULL;
$$.red_i = NULL;
$$.red_e = NULL;
$$.red_flags = IO_REGULAR;
}
| redirect REDIRECT_OE word {
$1.red_o = add_word_to_list($3, $1.red_o);
$1.red_e = add_word_to_list($3, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_E word {
$1.red_e = add_word_to_list($3, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_O word {
$1.red_o = add_word_to_list($3, $1.red_o);
$$ = $1;
}
| redirect REDIRECT_APPEND_E word {
$1.red_e = add_word_to_list($3, $1.red_e);
$1.red_flags |= IO_ERR_APPEND;
$$ = $1;
}
| redirect REDIRECT_APPEND_O word {
$1.red_o = add_word_to_list($3, $1.red_o);
$1.red_flags |= IO_OUT_APPEND;
$$ = $1;
}
| redirect INDIRECT word {
$1.red_i = add_word_to_list($3, $1.red_i);
$$ = $1;
}
| redirect REDIRECT_OE word BLANK {
$1.red_o = add_word_to_list($3, $1.red_o);
$1.red_e = add_word_to_list($3, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_E word BLANK {
$1.red_e = add_word_to_list($3, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_O word BLANK {
$1.red_o = add_word_to_list($3, $1.red_o);
$$ = $1;
}
| redirect REDIRECT_APPEND_E word BLANK {
$1.red_e = add_word_to_list($3, $1.red_e);
$1.red_flags |= IO_ERR_APPEND;
$$ = $1;
}
| redirect REDIRECT_APPEND_O word BLANK {
$1.red_o = add_word_to_list($3, $1.red_o);
$1.red_flags |= IO_OUT_APPEND;
$$ = $1;
}
| redirect INDIRECT word BLANK {
$1.red_i = add_word_to_list($3, $1.red_i);
$$ = $1;
}
| redirect REDIRECT_OE BLANK word {
$1.red_o = add_word_to_list($4, $1.red_o);
$1.red_e = add_word_to_list($4, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_E BLANK word {
$1.red_e = add_word_to_list($4, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_O BLANK word {
$1.red_o = add_word_to_list($4, $1.red_o);
$$ = $1;
}
| redirect REDIRECT_APPEND_E BLANK word {
$1.red_e = add_word_to_list($4, $1.red_e);
$1.red_flags |= IO_ERR_APPEND;
$$ = $1;
}
| redirect REDIRECT_APPEND_O BLANK word {
$1.red_o = add_word_to_list($4, $1.red_o);
$1.red_flags |= IO_OUT_APPEND;
$$ = $1;
}
| redirect INDIRECT BLANK word {
$1.red_i = add_word_to_list($4, $1.red_i);
$$ = $1;
}
| redirect REDIRECT_OE BLANK word BLANK {
$1.red_o = add_word_to_list($4, $1.red_o);
$1.red_e = add_word_to_list($4, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_E BLANK word BLANK {
$1.red_e = add_word_to_list($4, $1.red_e);
$$ = $1;
}
| redirect REDIRECT_O BLANK word BLANK {
$1.red_o = add_word_to_list($4, $1.red_o);
$$ = $1;
}
| redirect REDIRECT_APPEND_O BLANK word BLANK {
$1.red_o = add_word_to_list($4, $1.red_o);
$1.red_flags |= IO_OUT_APPEND;
$$ = $1;
}
| redirect REDIRECT_APPEND_E BLANK word BLANK {
$1.red_o = add_word_to_list($4, $1.red_o);
$1.red_flags |= IO_ERR_APPEND;
$$ = $1;
}
| redirect INDIRECT BLANK word BLANK {
$1.red_i = add_word_to_list($4, $1.red_i);
$$ = $1;
}
;
word:
word WORD {
$$ = add_part_to_word(new_word($2, false), $1);
}
| word ENV_VAR {
$$ = add_part_to_word(new_word($2, true), $1);
}
| WORD {
$$ = new_word($1, false);
}
| ENV_VAR {
$$ = new_word($1, true);
}
;
%%
bool parse_line(const char * line, command_t ** root)
{
if (*root != NULL) {
/* see the comment in parser.h */
assert(false);
return false;
}
if (line == NULL) {
/* see the comment in parser.h */
assert(false);
return false;
}
free_parse_memory();
flex_parse_string(line);
needsFree = true;
command_root = NULL;
yylloc.first_line = yylloc.last_line = 1;
yylloc.first_column = yylloc.last_column = 0;
if (yyparse() != 0) {
/* yyparse failed */
return false;
}
*root = command_root;
return true;
}
void free_parse_memory()
{
if (needsFree) {
flex_free_buffers();
while (globalAllocCount != 0) {
globalAllocCount--;
assert(globalAllocMem[globalAllocCount] != NULL);
free(globalAllocMem[globalAllocCount]);
globalAllocMem[globalAllocCount] = NULL;
}
if (globalAllocMem != NULL) {
free((void *)globalAllocMem);
globalAllocMem = NULL;
}
globalAllocSize = 0;
needsFree = false;
}
}
void yyerror(const char* str)
{
parse_error(str, yylloc.first_column);
}