-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathshell.y
More file actions
279 lines (233 loc) · 6.23 KB
/
shell.y
File metadata and controls
279 lines (233 loc) · 6.23 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
/*
* CS-252
* shell.y: parser for shell
*
* This parser compiles the following grammar:
*
* cmd [arg]* [> filename]
*
* you must extend it to understand the complete shell grammar
*
*/
%code requires
{
#include <string>
#include <string.h>
#if __cplusplus > 199711L
#define register // Deprecated in C++11 so remove the keyword
#endif
}
%union
{
char *string_val;
// Example of using a c++ type in yacc
std::string *cpp_string;
}
%token <string_val> WORD
%token NOTOKEN GREAT NEWLINE PIPE GREATGREAT AMPERSAND LESS GREATAMPERSAND GREATGREATAMPERSAND TWOGREAT
%{
//#define yylex yylex
#include <cstdio>
#include "command.hh"
void yyerror(const char * s);
int yylex();
void expandWildCardsIfNecessary(char * arg);
void expandWildCards(char * prefix, char * arg);
int cmpfunc(const void * file1, const void * file2);
bool is_dir(const char * path);
%}
%%
goal:
commands
;
commands:
command
| commands command
;
command: simple_command
;
simple_command:
pipe_list iomodifier_list background_optional NEWLINE {
//printf(" Yacc: Execute command\n");
Command::_currentCommand.execute();
}
| NEWLINE { Command::_currentCommand.prompt();}
| error NEWLINE { yyerrok; }
;
command_and_args:
command_word argument_list {
Command::_currentCommand.
insertSimpleCommand( Command::_currentSimpleCommand );
}
;
argument_list:
argument_list argument
| /* can be empty */
;
argument:
WORD {
//printf(" Yacc: insert argument \"%s\"\n", $1);
if(strcmp(Command::_currentSimpleCommand->_arguments[0], "echo") == 0 && strchr($1, '?'))
Command::_currentSimpleCommand->insertArgument( $1 );
else
expandWildCardsIfNecessary($1);
}
;
command_word:
WORD {
//printf(" Yacc: insert command \"%s\"\n", $1);
Command::_currentSimpleCommand = new SimpleCommand();
Command::_currentSimpleCommand->insertArgument( $1 );
}
;
pipe_list:
pipe_list PIPE command_and_args
| command_and_args
;
iomodifier_opt:
GREAT WORD {
//printf(" Yacc: insert output \"%s\"\n", $2);
Command::_currentCommand._outFile = strdup($2);
Command::_currentCommand._outCounter++;
}
| GREATGREAT WORD {
//printf(" GREATGREAT WORD: insert output \"%s\"\n", $2);
Command::_currentCommand._outFile = strdup($2);
Command::_currentCommand._append = 1;
Command::_currentCommand._outCounter++;
}
| GREATAMPERSAND WORD {
//printf(" Yacc: insert output \"%s\"\n", $2);
Command::_currentCommand._outFile = strdup($2);
Command::_currentCommand._errFile = strdup($2);
Command::_currentCommand._outCounter++;
}
| GREATGREATAMPERSAND WORD {
//printf(" Yacc: insert output \"%s\"\n", $2);
Command::_currentCommand._outFile = strdup($2);
Command::_currentCommand._errFile = strdup($2);
Command::_currentCommand._append = 1;
Command::_currentCommand._outCounter++;
}
| LESS WORD {
//printf(" Yacc: insert input \"%s\"\n", $2);
Command::_currentCommand._inFile = strdup($2);
Command::_currentCommand._inCounter++;
}
| TWOGREAT WORD {
//printf(" Yacc: insert output \"%s\"\n", $2);
Command::_currentCommand._errFile = strdup($2);
}
;
iomodifier_list:
iomodifier_list iomodifier_opt
| iomodifier_opt
| /* can be empty */
;
background_optional:
AMPERSAND {
Command::_currentCommand._background = 1;
}
| /* can be empty */
;
%%
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++) Command::_currentSimpleCommand->insertArgument(entries[i]);
}
else {
Command::_currentSimpleCommand->insertArgument(arg);
}
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;
if (temp[0] == '/') *(save++) = *(temp++);
while (*temp != '/' && *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) {
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);
}
}
}
}
closedir(dir);
} else {
char * preToSend = (char *) malloc (100);
if (prefix) sprintf(preToSend, "%s/%s", prefix, dir);
else preToSend = strdup(dir);
if (*temp) expandWildCards(preToSend, ++temp);
}
}
void
yyerror(const char * s)
{
fprintf(stderr,"%s", s);
}
#if 0
main()
{
yyparse();
}
#endif