-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyntax_parser.y
More file actions
335 lines (296 loc) · 7.18 KB
/
syntax_parser.y
File metadata and controls
335 lines (296 loc) · 7.18 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
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
%code requires
{
#include <string>
#if __cplusplus > 199711L
#define register // Deprecated in C++11 so remove the keyword
#endif
}
%union
{
char *string_val;
std::string *cpp_string;
}
%token <cpp_string> WORD
%token NOTOKEN GREAT NEWLINE LESS PIPE GREATGREAT TWOGREAT GREATAMPERSAND GREATGREATAMPERSAND AMPERSAND CD SETENV UNSETENV PRINTENV PROCESS_SUB_OPEN PROCESS_SUB_CLOSE
%{
#include <cstdio>
#include "shell_interpreter.hh"
#include <regex.h>
#include <dirent.h>
#include <cstdlib>
#include <unistd.h>
#include <string.h>
void yyerror(const char * s);
int yylex();
void expandWildCardsIfNecessary(char * arg);
int cmpfunc(const void * file1, const void * file2);
void expandWildCards(char * prefix, char * arg);
%}
%%
goal:
commands
;
commands:
command
| commands command
;
command: simple_command
;
simple_command:
pipe_list iomodifier_opt_list background_optional NEWLINE {
#ifdef PRINTING
printf(" Yacc: Execute command\n");
#endif
Shell::_currentCommand.execute();
}
| NEWLINE
| error NEWLINE { yyerrok; }
;
iomodifier_opt_list:
iomodifier_opt_list iomodifier_opt
|
;
command_and_args:
command_word argument_list {
Shell::_currentCommand.
insertSimpleCommand( Command::_currentSimpleCommand );
}
| cd
| UNSETENV
| SETENV
| PRINTENV
;
pipe_list:
pipe_list PIPE command_and_args
| command_and_args
;
argument_list:
argument_list WORD {
char * arg = strdup($2->c_str());
if(Command::_currentSimpleCommand->_arguments.size() > 0 && strcmp(Command::_currentSimpleCommand->_arguments[0]->c_str(), "echo") == 0 && strchr(arg, '?')) {
Command::_currentSimpleCommand->insertArgument( $2 );
}
else {
expandWildCardsIfNecessary(arg);
}
if (arg) {
free(arg);
}
}
| /* can be empty */
;
cd:
CD WORD {
char* path = (char*)$2->c_str();
if (chdir(path) == -1) {
fprintf(stderr, "cd: can't cd to %s\n", path);
}
}
|
CD {
char* path = getenv((char*) "HOME");
if (chdir(path) == -1) {
fprintf(stderr, "cd: can't cd to %s\n", path);
}
}
;
command_word:
WORD {
#ifdef PRINTING
printf(" Yacc: insert command \"%s\"\n", $1->c_str());
#endif
Command::_currentSimpleCommand = new SimpleCommand();
Command::_currentSimpleCommand->insertArgument( $1 );
}
;
iomodifier_opt:
GREATGREAT WORD {
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._append = true;
}
| GREAT WORD {
#ifdef PRINTING
printf(" Yacc: insert output \"%s\"\n", $2->c_str());
#endif
if (Shell::_currentCommand._outFile != NULL) {
printf("Ambiguous output redirect.\n"); // Call your error function
}
Shell::_currentCommand._outFile = $2;
}
| GREATGREATAMPERSAND WORD {
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._errFile = $2;
Shell::_currentCommand._append = true;
}
| GREATAMPERSAND WORD {
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._errFile = $2;
}
| LESS WORD {
#ifdef PRINTING
printf(" Yacc: insert input \"%s\"\n", $2->c_str());
#endif
Shell::_currentCommand._inFile = $2;
}
| TWOGREAT WORD {
Shell::_currentCommand._errFile = $2;
}
;
background_optional:
AMPERSAND
{
Shell::_currentCommand._background = true;
}
|
;
%%
int maxEntries = 20;
int nEntries = 0;
char ** entries;
void expandWildCardsIfNecessary(char * arg) {
maxEntries = 20;
nEntries = 0;
entries = (char **) malloc (maxEntries * sizeof(char *));
if (strchr(arg, '*') || strchr(arg, '?')) {
expandWildCards(NULL, arg);
qsort(entries, nEntries, sizeof(char *), cmpfunc);
for (int i = 0; i < nEntries; i++) {
std::string * curr = new std::string(entries[i]);
Command::_currentSimpleCommand->insertArgument(curr);
}
}
else {
std::string * newarg = new std::string(arg);
Command::_currentSimpleCommand->insertArgument(newarg);
}
for (int i = 0; i < nEntries; i++) {
if (entries[i]) {
free(entries[i]); // Free each strdup'd string
}
}
if (entries) {
free(entries);
}
return;
}
int cmpfunc (const void *file1, const void *file2) {
const char *_file1 = *(const char **)file1;
const char *_file2 = *(const char **)file2;
return strcmp(_file1, _file2);
}
void expandWildCards(char * prefix, char * arg) {
char * temp = arg;
char * save = (char *) malloc (strlen(arg) + 10);
char * dir = save;
char * toDelete = dir;
if (temp[0] == '/') {
*save = *temp;
save++; temp++;
}
while (*temp != '/' && *temp) {
*save = *temp;
save++; temp++;
}
*save = '\0';
if (strchr(dir, '*') || strchr(dir, '?')) {
if (!prefix && arg[0] == '/') {
prefix = strdup("/");
dir++;
}
char * reg = (char *) malloc (2*strlen(arg) + 10);
char * a = dir;
char * r = reg;
*(r++) = '^';
while (*a) {
if (*a == '*') {
*(r++) = '.';
*(r++) = '*';
}
else if (*a == '?') {
*(r++) = '.';
}
else if (*a == '.') {
*(r++) = '\\';
*(r++) = '.';
}
else {
*(r++) = *a;
}
a++;
}
*(r++) = '$'; *r = '\0';
regex_t re;
int expbuf = regcomp(&re, reg, REG_EXTENDED|REG_NOSUB);
char * toOpen = strdup((prefix)?prefix:".");
DIR * dir = opendir(toOpen);
if (dir == NULL) {
free(toDelete);
perror("opendir");
return;
}
struct dirent * ent;
regmatch_t match;
while ((ent = readdir(dir)) != NULL) {
if (!regexec(&re, ent->d_name, 1, &match, 0)) {
if (*temp) {
if (ent->d_type == DT_DIR) {
char * nPrefix = (char *) malloc (150);
if (!strcmp(toOpen, ".")) {
nPrefix = strdup(ent->d_name);
}
else if (!strcmp(toOpen, "/")) {
sprintf(nPrefix, "%s%s", toOpen, ent->d_name);
}
else {
sprintf(nPrefix, "%s/%s", toOpen, ent->d_name);
}
expandWildCards(nPrefix, (*temp == '/')?++temp:temp);
}
} else {
if (nEntries == maxEntries) {
maxEntries *= 2;
entries = (char **) realloc (entries, maxEntries * sizeof(char *));
}
char * argument = (char *) malloc (100);
argument[0] = '\0';
if (prefix) sprintf(argument, "%s/%s", prefix, ent->d_name);
if (ent->d_name[0] == '.') {
if (arg[0] == '.') {
entries[nEntries++] = (argument[0] != '\0')?strdup(argument):strdup(ent->d_name);
}
} else {
entries[nEntries++] = (argument[0] != '\0')?strdup(argument):strdup(ent->d_name);
}
free(argument);
}
}
}
regfree(&re);
closedir(dir);
if (toOpen) {
free(toOpen);
}
if (reg) {
free(reg);
}
} else {
char * preToSend = (char *) malloc (100);
if (prefix) {
sprintf(preToSend, "%s/%s", prefix, dir);
}
else {
preToSend = strdup(dir);
}
if (*temp) {
expandWildCards(preToSend, ++temp);
}
}
if (toDelete) {
free(toDelete);
}
}
void
yyerror(const char * s)
{
fprintf(stderr,"%s", s);
}