-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.y
More file actions
141 lines (122 loc) · 2.7 KB
/
parser.y
File metadata and controls
141 lines (122 loc) · 2.7 KB
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
/* Yacc grammar file.
Define the grammar for a command.
*/
%{
#include<stdio.h>
#include <stdlib.h>
#include "command.h"
int yylex(void);
int yyerror(char* s);
command_t *curr_cmd;
sub_command_t *curr_sub_cmd;
bool continue_execution;
/*
* Command to be written to prompt user.
*
* Eventually want to print the working directory
*/
void prompt() {
char cwd[1024];
getcwd(cwd, 1024);
printf("%s $ ", cwd);
}
%}
%token NEWLINE FORWARD BACKWARD FORWARDFORWARD FORWARDAMPERSAND AMPERSAND PIPE WORD UNRECOGNIZED FORWARDFORWARDAMPERSAND AMPERSANDAMPERSAND TWOFORWARD TWOFORWARDFORWARD WORD_NOWC
%union {
char *str; /* Ptr to constant string (strings are malloc'd) */
};
%type <str> WORD;
%type <str> WORD_NOWC;
%%
goal: command ;
cmd:
WORD {
curr_sub_cmd = sub_command_factory();
command_insert(curr_cmd, curr_sub_cmd);
sub_command_insert(curr_sub_cmd, $1);
}
cmd_and_args:
cmd_and_args WORD {
insert_expand_wildcards(curr_sub_cmd, $2);
}
| cmd_and_args WORD_NOWC {
sub_command_insert(curr_sub_cmd, $2);
}
| cmd ;
pipe_list:
pipe_list PIPE cmd_and_args
| cmd_and_args
;
io_modifier:
FORWARDFORWARD WORD {
curr_cmd->out_file = $2;
curr_cmd->append_out = true;
}
| FORWARD WORD {
curr_cmd->out_file = $2;
curr_cmd->append_out = false;
}
| TWOFORWARD WORD {
curr_cmd->err_file = $2;
curr_cmd->append_err = false;
}
| TWOFORWARDFORWARD WORD{
curr_cmd->err_file = $2;
curr_cmd->append_err = true;
}
| FORWARDFORWARDAMPERSAND WORD {
curr_cmd->err_file = $2;
curr_cmd->out_file = $2;
curr_cmd->append_out = true;
curr_cmd->append_err = true;
}
| FORWARDAMPERSAND WORD {
curr_cmd->err_file = $2;
curr_cmd->out_file = $2;
curr_cmd->append_out = false;
curr_cmd->append_err = false;
}
| BACKWARD WORD {
curr_cmd->in_file = $2;
}
;
io_modifier_list:
io_modifier io_modifier_list
| ;
background:
AMPERSAND {
curr_cmd->background = true;
}
| ;
command_line:
pipe_list io_modifier_list background {
if (continue_execution) {
continue_execution = execute(curr_cmd);
}
free(curr_cmd);
curr_cmd = command_factory();
} ;
command_list:
command_line
| command_list AMPERSANDAMPERSAND command_line
| ;
command:
command command_list NEWLINE {
delete_command(curr_cmd);
curr_cmd = command_factory();
prompt();
}
| ;
%%
int main()
{
setbuf(stdout, NULL);
curr_cmd = command_factory();
continue_execution = true;
prompt();
while (1) {
yyparse();
}
delete_command(curr_cmd);
return 0;
}