-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing.c
106 lines (93 loc) · 2.59 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
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>
#include "parsing.h"
#include "parser.h"
#include "command.h"
static scommand parse_scommand(Parser p)
{
/* Devuelve NULL cuando hay un error de parseo */
assert(p != NULL && !parser_at_eof(p));
scommand simple_command = scommand_new();
char *command_text = strdup("");
arg_kind_t argument_type;
parser_skip_blanks(p);
command_text = parser_next_argument(p, &argument_type);
/* Si la cadena es vacia devuelvo un scommand vacio */
if (command_text == NULL)
{
simple_command = scommand_destroy(simple_command);
}
while (command_text != NULL)
{
if (argument_type == ARG_NORMAL)
{
scommand_push_back(simple_command, command_text);
}
else if (argument_type == ARG_INPUT)
{
scommand_set_redir_in(simple_command, command_text);
}
else if (argument_type == ARG_OUTPUT)
{
scommand_set_redir_out(simple_command, command_text);
}
command_text = parser_next_argument(p, &argument_type);
}
free(command_text);
command_text = NULL;
return simple_command;
}
pipeline parse_pipeline(Parser p)
{
assert(p != NULL && !parser_at_eof(p));
pipeline result = pipeline_new();
scommand cmd = NULL;
bool error = false, another_pipe = false;
bool wait, garbage;
bool secuencial = false;
cmd = parse_scommand(p);
error = (cmd == NULL); /* Comando inválido al empezar */
if (!error)
{
pipeline_push_back(result, cmd);
}
parser_skip_blanks(p);
parser_op_pipe(p, &another_pipe);
if (another_pipe)
{
cmd = parse_scommand(p);
error = (cmd == NULL);
if (!error)
{
pipeline_push_back(result, cmd);
}
}
parser_op_background(p, &wait);
pipeline_set_wait(result, !(wait)); // si hay un & no espera
parser_op_background(p, &secuencial); // si hay otro & es secuencial
if (secuencial)
{
wait = false;
cmd = parse_scommand(p);
error = (cmd == NULL);
if (!error)
{
pipeline_push_back(result, cmd);
}
pipeline_set_wait(result, !(wait));
}
pipeline_set_secuencial(result, secuencial);
/* Tolerancia a espacios posteriores */
parser_skip_blanks(p);
/* Consumir todo lo que hay inclusive el \n */
parser_garbage(p, &garbage);
/* Si hubo error, hacemos cleanup */
if (error)
{
pipeline_destroy(result);
result = NULL;
}
return result;
}