forked from vsinha/shell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.y
More file actions
304 lines (256 loc) · 6.69 KB
/
shell.y
File metadata and controls
304 lines (256 loc) · 6.69 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
/*
* CS-252 Spring 2013
* shell.y: parser for shell
*
* This parser compiles the following grammar:
*
* cmd [arg]* [> filename]
*
* you must extend it to understand the complete shell grammar
*
*/
%token <string_val> WORD
%token NOTOKEN NEWLINE GREAT LESS PIPE AMPERSAND GREATGREAT GREATGREATAMPERSAND GREATAMPERSAND
%union {
char *string_val;
}
%{
//#define yylex yylex
#define MAXFILENAME 1024
#include <dirent.h> // wildcard expansion directory stuff
#include <fcntl.h> // open() arguments
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // strcmp
#include <unistd.h>
#include "command.h"
#include "trace.h"
void yyerror(const char * s);
int yylex();
void expandWildcard(char * prefix, char * suffix) {
if (suffix[0] == 0) {
// suffix is empty, put prefix in argument
Command::_currentArgCollector->addArg(strdup(prefix));
return;
}
// obtain the next component in the suffix && advance suffix
// if the first character is a '/', get the position of the 2nd
char * s = NULL;
if (suffix[0] == '/') {
s = strchr( (char*)(suffix+1), '/');
} else {
s = strchr(suffix, '/');
}
char component[MAXFILENAME] = ""; // must initialize this
if ( s != NULL ) { // copy up to the first "/"
strncpy(component, suffix, s - suffix);
suffix = s + 1;
} else { // last part of path, copy the whole thing
strcpy(component, suffix);
suffix = suffix + strlen(suffix);
}
// expand the component
char newPrefix[MAXFILENAME];
if ( strchr(component, '*') == NULL && strchr(component, '?') == NULL ) {
// component has no wildcards
// only do this if prefix is empty
if ( prefix == NULL || prefix[0] == 0 ) {
sprintf(newPrefix, "%s", component);
} else {
sprintf(newPrefix, "%s/%s", prefix, component);
}
expandWildcard(newPrefix, suffix);
return;
}
// component has wildcards, convert it to regex
// allocate enough space for regex
char * reg = (char*)malloc(2*strlen(component)+10);
char * a = component;
char * r = reg;
// copy over all characters, converting to regex representation
*r = '^'; r++;
while (*a) {
if ( *a == '*' ) { // * -> .*
*r = '.'; r++;
*r = '*'; r++;
} else if ( *a == '?' ) { // ? -> .*
*r = '.'; r++;
} else if ( *a == '.' ) { // . -> \\.
*r = '\\'; r++;
*r = '.'; r++;
} else if ( *a == '/' ) { // / -> ' ' (remove slash)
// do nothing
} else {
*r = *a; r++;
}
a++;
}
*r = '$'; r++; // end of line
*r = 0; // null terminator
// compile regex
regex_t re;
if ( regcomp(&re, reg, REG_EXTENDED|REG_NOSUB) != 0 ) {
perror("regcomp");
exit(1);
}
// if prefix is empty list current directory
char * dir_name;
if ( prefix == NULL ) {
const char * dot_char = ".";
dir_name = strdup(dot_char);
} else {
dir_name = prefix;
}
// open the directory
DIR * dir = opendir(dir_name);
if (dir == NULL) {
return;
}
struct dirent * ent;
while ( (ent = readdir(dir)) != NULL ) {
//check if name matches
if (regexec(&re, ent->d_name, (size_t)0, NULL, 0) == 0) {
if (prefix == NULL || prefix[0] == 0) {
sprintf(newPrefix, "%s", ent->d_name);
} else {
sprintf(newPrefix, "%s/%s", prefix, ent->d_name);
}
if (ent->d_name[0] == '.') { // only add items beginning with . if regex also begins with .
if (component[0] == '.') {
expandWildcard(newPrefix, suffix);
}
} else {
expandWildcard(newPrefix, suffix);
}
}
}
}
%}
%%
goal:
commands
;
commands:
command
| commands command
;
command:
pipe_list iomodifier_list background_opt NEWLINE {
Command::_currentCommand.execute();
}
| NEWLINE {
Command::_currentCommand.clear();
Command::_currentCommand.prompt();
}
| error NEWLINE { yyerrok; }
;
pipe_list:
pipe_list PIPE {
TRACE("found pipe\n");
} command_and_args
| command_and_args
;
command_and_args:
command_word arg_list {
//TRACE("adding command: %s\n", Command::_currentSimpleCommand->Command );
Command::_currentCommand.insertSimpleCommand( Command::_currentSimpleCommand );
}
;
arg_list:
arg_list argument
| /* can be empty */
;
argument:
WORD {
// expand wildcards
expandWildcard(NULL, $1);
// sort the arguments
Command::_currentArgCollector->sortArgs();
int i;
for (i = 0; i < Command::_currentArgCollector->nArgs; i++) {
//add all the sorted arguments
TRACE("adding argument %s\n", Command::_currentArgCollector->argArray[i]);
Command::_currentSimpleCommand->insertArgument(Command::_currentArgCollector->argArray[i]);
}
// reset the argCollector
Command::_currentArgCollector->clear();
}
;
command_word:
WORD {
Command::_currentSimpleCommand = new SimpleCommand();
Command::_currentArgCollector = new ArgCollector();
TRACE("inserting argument %s\n", $1);
Command::_currentSimpleCommand->insertArgument( $1 );
}
;
iomodifier_list:
iomodifier_list iomodifier
| /* empty */
;
iomodifier:
GREATGREAT WORD {
// if we've already set _outfile, we're doing something ambiguous
if (Command::_currentCommand._outFile != 0) {
Command::_currentCommand._ambiguous = 1;
}
// append stdout to file
Command::_currentCommand._openOptions = O_WRONLY | O_APPEND | O_CREAT;
Command::_currentCommand._outFile = $2;
}
| GREAT WORD {
// if we have already set _outfile, we are doing something ambiguous
if (Command::_currentCommand._outFile != 0) {
Command::_currentCommand._ambiguous = 1;
}
// rewrite the file if it already exists
Command::_currentCommand._openOptions = O_WRONLY | O_CREAT | O_TRUNC;
Command::_currentCommand._outFile = $2;
}
| GREATGREATAMPERSAND WORD {
// if we've already set _outfile, we're doing something ambiguous
if (Command::_currentCommand._outFile != 0) {
Command::_currentCommand._ambiguous = 1;
}
//redirect stdout and stderr to file and append
Command::_currentCommand._openOptions = O_WRONLY | O_APPEND | O_CREAT;
Command::_currentCommand._outFile = $2;
Command::_currentCommand._errFile = $2;
}
| GREATAMPERSAND WORD {
// if we've already set _outfile, we're doing something ambiguous
if (Command::_currentCommand._outFile != 0) {
Command::_currentCommand._ambiguous = 1;
}
//redirect stdout and stderr to file and truncate
Command::_currentCommand._openOptions = O_WRONLY | O_CREAT | O_TRUNC;
Command::_currentCommand._outFile = $2;
Command::_currentCommand._errFile = $2;
}
| LESS WORD {
// if we've already set _infile, we're doing something ambiguous
if (Command::_currentCommand._inputFile != 0) {
Command::_currentCommand._ambiguous = 1;
}
Command::_currentCommand._inputFile = $2;
}
;
background_opt:
AMPERSAND {
Command::_currentCommand._background = 1;
}
| /* empty */
;
%%
void
yyerror(const char * s)
{
fprintf(stderr,"%s", s);
}
#if 0
main()
{
yyparse();
}
#endif