-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.y
More file actions
407 lines (363 loc) · 8.24 KB
/
shell.y
File metadata and controls
407 lines (363 loc) · 8.24 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
/*
* 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
*
*/
%token <string_val> WORD
%token EFILE NOTOKEN GREAT NEWLINE GREATGREAT PIPE AMPERSAND GREATGREATAMPERSAND GREATAMPERSAND LESS
%union {
char *string_val;
}
%{
#define yylex yylex
#define MAXFILENAME 1024
#include <assert.h>
#include <string>
#include <stdio.h>
#include <string.h>
#include "command.h"
#include <sys/types.h>
#include <regex.h>
#include <dirent.h>
void yyerror(const char * s);
void sortArrayStrings(char*** array, int nEntries);
void expandWildcardsIfNecessary(char* arg);
void expandWildcards(const char* prefix, char* suffix);
void checkSubDir(char* arg);
char* toRegex(char* arg);
int yylex();
int n = 0;
int max = 20;
char** items;
%}
%%
goal: command_list;
arg_list:
arg_list WORD {
// Command::_currentSimpleCommand->insertArgument($2);
// printf("test2: %s\n", $2);
checkSubDir($2);
// expandWildcardsIfNecessary($2);
}
|
;
cmd_and_args:
WORD {
// printf(" Yacc: insert command \"%s\"\n", $1);
if(!strcmp($1,"exit")) {
printf("\n Good bye!!\n\n");
exit(0);
}
Command::_currentSimpleCommand = new SimpleCommand();
Command::_currentSimpleCommand->insertArgument( $1 );
}
arg_list {
Command::_currentCommand.insertSimpleCommand( Command::_currentSimpleCommand );
}
;
pipe_list:
pipe_list PIPE cmd_and_args
| cmd_and_args
;
io_modifier:
GREATGREAT WORD {
//printf(" Yacc: insert output append \"%s\"\n", $2);
Command::_currentCommand._background=1;
Command::_currentCommand._outFile = strdup($2);
Command::_currentCommand._append = 1;
}
| GREAT WORD {
//printf(" Yacc: insert output \"%s\"\n", $2);
Command::_currentCommand._outFile = $2;
if (!Command::_currentCommand._iomodified) {
Command::_currentCommand._iomodified=true;
}
else {
yyerror("Ambiguous output redirect.\n");
Command::_currentCommand._iomodified=false;
}
}
| GREATGREATAMPERSAND WORD {
//printf(" Yacc: insert output stderr append \"%s\"\n", $2);
Command::_currentCommand._background=1;
Command::_currentCommand._errFile=strdup($2);
Command::_currentCommand._outFile =strdup($2);
Command::_currentCommand._append = 1;
}
| GREATAMPERSAND WORD {
//printf(" Yacc: insert output stderr \"%s\"\n", $2);
Command::_currentCommand._errFile= strdup($2);
Command::_currentCommand._outFile = strdup($2);
}
| LESS WORD {
//printf(" Yacc: insert input \"%s\"\n", $2);
Command::_currentCommand._inFile = $2;
}
;
io_modifier_list:
io_modifier_list io_modifier
|
;
background_optional:
AMPERSAND {
Command::_currentCommand._background = 1;
}
|
;
command_line:
pipe_list io_modifier_list
background_optional NEWLINE {
//printf(" Yacc: Execute command\n");
Command::_currentCommand.execute();
}
|
pipe_list io_modifier_list
background_optional EFILE {
//printf(" Yacc: Execute command\n");
Command::_currentCommand.execute();
}
| NEWLINE
| EFILE {
//printf(" Yacc: Execute command\n");
exit(1);
}
| error NEWLINE{yyerrok;}
command_list:
command_line |
command_list command_line
;
%%
void
yyerror(const char * s)
{
fprintf(stderr,"%s", s);
}
void
expandWildcardsIfNecessary(char* arg)
{
//Returns if arg does not contain '*' or '?'
if (strchr(arg, '*')== NULL && strchr(arg, '?') == NULL){
Command::_currentSimpleCommand->insertArgument(arg);
//printf("TEST IS : %s\n",arg);
return;
}
else {
char* reg = toRegex(arg);
//printf("%s\n",reg);
regex_t re;
int result = regcomp(&re, reg, REG_NOSUB|REG_EXTENDED);
if (result!= 0){
perror("compile");
return;
}
DIR * dir = opendir(".");
if (dir == NULL) {
perror("opendir");
return;
}
struct dirent * ent;
int maxEntries = 20;
int nEntries = 0;
char** array = (char**) malloc(maxEntries * sizeof(char*));
while ((ent = readdir(dir))!= NULL) {
//check if name matches
regmatch_t match;
if(regexec(&re,ent->d_name,1,&match,0)==0) {
if(nEntries == maxEntries) {
maxEntries *=2;
array = (char**)realloc(array, maxEntries* sizeof(char*));
assert(array!=NULL);
}
if(ent->d_name[0] == '.'){
if(arg[0] == '.') {
array[nEntries++] = strdup(ent->d_name);
}
}
else {
array[nEntries++] = strdup(ent->d_name);
}
}
}
closedir(dir);
sortArrayStrings(&array,nEntries);
for(int i = 0; i < nEntries ; i++) {
//printf("%s\n", array[i]);
Command::_currentSimpleCommand->insertArgument(array[i]);
}
free(array);
}
}
void
sortArrayStrings(char*** array1, int nEntries) {
char** array = *array1;
char* temp;
for (int i = 0; i < nEntries; i++){
for(int j = 0; j < nEntries; j++) {
if ((strcmp(array[i],array[j])) < 0) {
temp = strdup(array[i]);
array[i] = strdup(array[j]);
array[j] = strdup(temp);
}
}
}
free(temp);
}
void
checkSubDir(char* arg) {
if ((strchr(arg,'/'))== NULL) {
expandWildcardsIfNecessary(arg);
}
else {
if (arg[0] == '/') {
items = (char**) malloc(max * sizeof(char*));
expandWildcards("",arg);
sortArrayStrings(&items,n);
for(int i = 0; i < n ; i++) {
//printf("item is %s\n", items[i]);
Command::_currentSimpleCommand->insertArgument(strdup(items[i]));
}
free(items);
n = 0;
}
else {
Command::_currentSimpleCommand->insertArgument(strdup(arg));
}
}
}
void
expandWildcards(const char* prefix, char* suffix) {
if(suffix[0] == 0) {
//suffix is empty. Put prefix in argument.
//printf("prefix is : %s\n", prefix);
//check for double // start
if (prefix[0] == '/' && prefix[1] == '/') {
prefix++;
}
if ( n == max) {
max *=2;
items = (char**)realloc(items, max*sizeof(char*));
assert(items!=NULL);
}
items[n++] = strdup(prefix);
return;
}
//Obtain the next component in the suffix
//Also, advance the suffix.
char * s = strchr(suffix, '/');
//printf("prefix is : %s\n", s);
char component[MAXFILENAME];
if(s!=NULL) { //Copy up to the first "/"
strncpy(component,suffix,s-suffix);
//printf("component is %s\n", component);
suffix = s + 1;
}
else { //Last part of the path. Copy whole thing.
strncpy(component,suffix,strlen(suffix));
suffix = suffix + strlen(suffix);
}
//Now expand the component
char newPrefix[MAXFILENAME];
if (strchr(component, '*')== NULL && strchr(component, '?') == NULL){
//No wildcard found
if(prefix[0] == '/' && strlen(prefix) == 1) {
sprintf(newPrefix, "%s%s", prefix, component);
}else if(prefix[0] == 0) {
sprintf(newPrefix,"/%s",component);
}
else {
sprintf(newPrefix, "%s/%s", prefix, component);
}
//printf("new prefix is : %s\n",newPrefix);
expandWildcards(newPrefix,suffix);
return;
}
//Component has wildcard
//Change component to regExpression
char* reg = toRegex(component);
regex_t re;
int result = regcomp(&re, reg, REG_NOSUB|REG_EXTENDED);
if (result!= 0){
perror("compile");
return;
}
char* dir;
//If prefix is empty then list current directory
//else directory is the prefix
if (prefix[0] == 0){
*dir = '.';
}
else {
dir = strdup(prefix);
}
DIR * d = opendir(dir);
if (d== NULL) {
return;
}
struct dirent * ent;
//Check what entries match
while ((ent = readdir(d)) != NULL) {
//check if name match
regmatch_t match;
if(regexec(&re,ent->d_name,1,&match,0)==0) {
//entry matches. Add name of entry to the prefix and call expandwilCard() recursively
//printf("%s\n", ent->d_name);
if (ent->d_name[0] == '.') {
if(component[0] == '.') {
sprintf(newPrefix,"%s/%s", prefix, ent->d_name);
expandWildcards(newPrefix,suffix);
}
}
else {
sprintf(newPrefix,"%s/%s", prefix, ent->d_name);
expandWildcards(newPrefix,suffix);
}
}
}
closedir(d);
}
char* toRegex(char* arg) {
char* reg = (char*)malloc(2*strlen(arg)+10);
char* a = arg;
char * r = reg;
*r = '^';
r++; //match begining of line with ^
while (*a) {
if(*a == '*') {
*r='.';
r++;
*r='*';
r++;
}
else if(*a=='?'){
*r='.';
r++;
}
else if(*a=='.'){
*r='\\';
r++;
*r='.';
r++;
}
else {
*r = *a;
r++;
}
a++;
}
*r='$';
r++;
*r=0; //match end of line and add null char
return reg;
}
#if 0
main()
{
yyparse();
}
#endif