-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.c
94 lines (74 loc) · 2.54 KB
/
parsing.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
#include <assert.h>
#include <check.h>
#include <stdbool.h>
#include <stdlib.h>
#include "command.h"
#include "parser.h"
#include "parsing.h"
static scommand parse_scommand(Parser p) {
assert(p != NULL);
scommand new_scmd = scommand_new();
arg_kind_t arg_type = ARG_INPUT;
char *arg_cmd = parser_next_argument(p, &arg_type);
while (arg_cmd != NULL) {
// We've the cmd argument in *arg_cmd
if (arg_type == ARG_NORMAL) {
scommand_push_back(new_scmd, arg_cmd);
} else if (arg_type == ARG_INPUT) {
scommand_set_redir_in(new_scmd, arg_cmd);
} else if (arg_type == ARG_OUTPUT) {
scommand_set_redir_out(new_scmd, arg_cmd);
}
// New argument
arg_cmd = NULL;
arg_cmd = parser_next_argument(p, &arg_type);
}
// See if there are some errors
if (scommand_is_empty(new_scmd) || arg_type == ARG_INPUT || arg_type == ARG_OUTPUT) {
new_scmd = scommand_destroy(new_scmd);
new_scmd = NULL;
}
return new_scmd;
}
pipeline parse_pipeline(Parser p) {
assert(p != NULL && !parser_at_eof(p));
pipeline result = pipeline_new();
scommand act_scmd = NULL;
bool error = false, another_pipe = true;
act_scmd = parse_scommand(p);
error = (act_scmd == NULL); // Invalid command to begin
while (another_pipe && !error) {
// Push into a pipeline
pipeline_push_back(result, act_scmd);
// See final options (end line, pipe, background or EOF)
// Is there a pipe?
parser_op_pipe(p, &another_pipe);
if (another_pipe) {
// Parsing the next scommand
act_scmd = parse_scommand(p);
error = (act_scmd == NULL); // Invalid command
}
}
if (!error) {
// Is there a background?
bool there_is_background = false;
parser_op_background(p, &there_is_background);
pipeline_set_wait(result, !there_is_background);
// Erase the rest of the parser
bool there_are_other_symbols = false;
parser_garbage(p, &there_are_other_symbols);
error = there_are_other_symbols;
} else {
// Erase the rest of the parser
bool there_are_other_symbols = false;
parser_garbage(p, &there_are_other_symbols);
}
// Erase the pipeline in case of an error
if (error) {
result = pipeline_destroy(result);
result = NULL;
printf("Command syntax Error\n");
}
// Return the pipeline or NULL (in case of a syntax error)
return result;
}