-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.y
598 lines (545 loc) · 15.6 KB
/
shell.y
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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* 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>
#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 <cpp_string> WORD
%token NOTOKEN GREAT NEWLINE
%token GREAT2 GGCONT GCONT LCONT GGREAT CONT LESS GUARD
%{
//#define yylex yylex
#include <cstdio>
#include <string>
#include <string.h>
#include <dirent.h>
#include <regex.h>
#include <math.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include "shell.hh"
char * tilExp(const char *);
char ** dirExp(const char *, int);
std::string * envExp(std::string*);
void yyerror(const char * s);
int yylex();
void inplaceMerge(char**, size_t);
extern "C" void * recallocarray(void *, size_t, size_t, size_t);
%}
%%
goal:
commandline
| goal commandline
;
commandline:
commands bgmodifier NEWLINE {
//printf(" Yacc: Execute command\n");
Shell::_currentCommand.execute();
}
| NEWLINE {
//printf(" Yacc: Empty Line\n");
Shell::_currentCommand.execute();
}
| error NEWLINE { yyerrok; }
;
commands: simple_command
| commands GUARD simple_command {
//printf(" Yacc: Command pipeline\n");
}
;
simple_command:
command_word argument_list io {
Shell::_currentCommand.
insertSimpleCommand( Command::_currentSimpleCommand );
}
;
argument_list:
argument_list argument
| /* can be empty */
;
argument:
WORD {
std::string * env = envExp($1);
if (env->find('?') != std::string::npos || env->find('*') != std::string::npos) {
char ** exp = dirExp(env->c_str(), 0);
// iterate through the array and put everything into arguement
int i = 0;
while(exp[i++]);
if (i == 1) {
Command::_currentSimpleCommand->insertArgument( env );
} else {
delete env;
//printf("i: %d\n", i);
inplaceMerge(exp, i - 1);
i = 0;
while(exp[i]) {
Command::_currentSimpleCommand->insertArgument( new std::string(exp[i]) );
free(exp[i]);
i++;
}
}
free(exp);
} else if (env->c_str()[0] == '~') {
if (env->find('/') != std::string::npos) {
char * home = tilExp(env->substr(0, env->find('/')).c_str());
std::string * newArg = new std::string();
newArg->append(home);
free(home);
newArg->append(env->substr(env->find('/')));
delete env;
Command::_currentSimpleCommand->insertArgument( newArg );
} else {
char * home = tilExp(env->substr(0).c_str());
delete env;
Command::_currentSimpleCommand->insertArgument( new std::string(home) );
free(home);
}
} else {
Command::_currentSimpleCommand->insertArgument( env );
}
//printf(" Yacc: insert argument \"%s\"\n", $1->c_str());
}
;
command_word:
WORD {
//printf(" Yacc: insert command \"%s\"\n", $1->c_str());
Command::_currentSimpleCommand = new SimpleCommand();
Command::_currentSimpleCommand->insertArgument( $1 );
}
;
io:
io iomodifiers
|
;
iomodifiers:
LESS WORD {
//printf(" Yacc: insert input \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._inFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._inFile = $2;
}
| GREAT WORD {
//printf(" Yacc: insert output \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._outFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._outFile = $2;
}
| GCONT WORD {
//printf(" Yacc: insert background output \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._outFile || Shell::_currentCommand._errFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._errFile = $2;
}
| GGREAT WORD {
//printf(" Yacc: insert append output \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._outFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._appendO = true;
}
| GGCONT WORD {
//printf(" Yacc: insert append background output \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._outFile || Shell::_currentCommand._errFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._outFile = $2;
Shell::_currentCommand._errFile = $2;
Shell::_currentCommand._appendO = true;
Shell::_currentCommand._appendE = true;
}
| GREAT2 WORD {
//printf(" Yacc: insert error output \"%s\"\n", $2->c_str());
if (Shell::_currentCommand._errFile) {
printf("Ambiguous output redirect.\n");
Shell::_currentCommand.clear();
}
Shell::_currentCommand._errFile = $2;
}
;
bgmodifier:
CONT {
//printf(" Yacc: The command will be ran in the background\n");
Shell::_currentCommand._background = true;
}
| /*can be empty*/
;
%%
void
yyerror(const char * s)
{
fprintf(stderr,"%s\n", s);
Shell::prompt();
yyparse();
}
// inplace Mergesort for string
void inplaceMerge(char ** ptr, size_t len) {
// if only 1 string to sort, dont bother
if (len < 2) return;
// comparing between two strings
if (len == 2) {
int i = 0;
// iterate until the first character mismatch
while(ptr[0][i] == ptr[1][i]) i++;
// if their place is reversed, switch back
if (ptr[0][i] > ptr[1][i]) {
char* temp = ptr[0];
ptr[0] = ptr[1];
ptr[1] = temp;
}
return;
}
// ptr1 and ptr2 point to the head of their respective sorted subarrays
char ** ptr1 = ptr;
char ** ptr2 = ptr + (len / 2);
inplaceMerge(ptr1, len / 2);
inplaceMerge(ptr2, (int)ceil((float)len / 2));
// if ptr1 reaches ptr2 (array 1 is empty) or
// ptr2 reaches the end (array 2 is empty)
// the entire array is then sorted.
while(ptr1 != ptr2 && ptr2 != ptr + len) {
int i = 0;
// since all entries are unique, we dont have to worry about SEGEV here
while((*ptr1)[i] == (*ptr2)[i]) i++;
// if the first element is smaller, simply increment
if ((*ptr1)[i] < (*ptr2)[i]) ptr1++;
// if the second is smaller, shift the array right and
// insert it at the first pointer location
else {
char * temp = *ptr2;
for (i = 0; i < ptr2 - ptr1; i++) *(ptr2 - i) = *(ptr2 - i - 1);
*ptr1 = temp;
ptr1++;
ptr2++;
}
}
return;
}
// wildcard to regex
char * w2r (char * input) {
std::string reg = std::string();
reg.push_back('^');
switch(*input) {
case '*':
// "non-blank character" my ass
reg.append("([^\\.].*|\"\")");
break;
case '?':
reg.append("[^\\.]");
break;
case '+':
reg.append("\\+");
break;
case '[':
reg.append("\\[");
break;
case ']':
reg.append("\\]");
break;
case '(':
reg.append("\\(");
break;
case ')':
reg.append("\\)");
break;
case '.':
reg.append("\\.");
break;
default:
reg.push_back(*input);
}
for (int i = 1; i < strlen(input); i++) {
switch(input[i]) {
case '*':
reg.append(".*");
break;
case '?':
reg.append(".");
break;
case '+':
reg.append("\\+");
break;
case '[':
reg.append("\\[");
break;
case ']':
reg.append("\\]");
break;
case '(':
reg.append("\\(");
break;
case ')':
reg.append("\\)");
break;
case '.':
reg.append("\\.");
break;
default:
reg.push_back(input[i]);
}
}
reg.push_back('$');
char * output = (char *)calloc(strlen(reg.c_str()) + 1, sizeof(char));
strcpy(output, reg.c_str());
return(output);
}
// environment variable expansion for specific input
char * mallocEnvExp(const char * input) {
char * env;
if (!strcmp(input, "$")) {
pid_t pid = getpid();
int lengthD = pid == 0 ? 1 : (int)floor(log10(abs(pid))) + 1;
char * output = (char*)calloc(lengthD + 1, sizeof(char));
sprintf(output, "%d", pid);
return output;
}
if (!strcmp(input, "?")) {
int lengthD = Shell::lstRtn == 0 ? 1 : (int)floor(log10(abs(Shell::lstRtn))) + 1;
char * output = (char*)calloc(lengthD + 1, sizeof(char));
sprintf(output, "%d", Shell::lstRtn);
return output;
}
if (!strcmp(input, "!")) {
int lengthD = Shell::lstPid == 0 ? 1 : (int)floor(log10(abs(Shell::lstPid))) + 1;
char * output = (char*)calloc(lengthD + 1, sizeof(char));
sprintf(output, "%d", Shell::lstPid);
return output;
}
if (!strcmp(input, "_")) {
if (Shell::lstArg != NULL) {
char * output = (char*)calloc(strlen(Shell::lstArg) + 1, sizeof(char));
strcpy(output, Shell::lstArg);
return output;
} else return (char*)calloc(1, sizeof(char));
}
if (!strcmp(input, "SHELL")) {
return realpath(Shell::argv, NULL);
}
if ((env = getenv(input)) != NULL) {
char * output = (char*)calloc(strlen(env) + 1, sizeof(char));
strcpy(output, env);
return output;
} else return (char *)calloc(1, sizeof(char));
}
// environment variable expansion within arbitrary string
std::string * envExp(std::string* input) {
int a = input->find('$');
int b = input->find('{');
int c = input->find('}');
if ((a != std::string::npos) && (b == a + 1) && (c > b)) {
char * exp = mallocEnvExp(input->substr(b + 1, c - b - 1).c_str());
input->erase(a, c - a + 1);
input->insert(a, exp);
free(exp);
return envExp(input);
} else return input;
}
// takes a directory and wildcard arg and recursively expands all subdir
char ** expandedPaths(const char * dirA, const char * arg, int mode) {
char ** output = (char **)calloc(8, sizeof(char*));
int outputI = 0;
int outputSize = 8;
char * currentDir;
char * rest = NULL;
// extract the wildcard to expand in the current dir
if (!strchr(arg, '/')) {
// terminating case
currentDir = (char *)calloc(strlen(arg) + 1, sizeof(char));
strcpy(currentDir, arg);
} else {
int len = strchr(arg, '/') - arg;
currentDir = (char *)calloc(len + 1, sizeof(char));
strncpy(currentDir, arg, len);
rest = (char *)calloc(strlen(arg) - len, sizeof(char));
strcpy(rest, arg + len + 1);
}
// regex conversion
char * regExp = w2r(currentDir);
free(currentDir);
currentDir = NULL;
// regex init
regex_t re;
int result = regcomp( &re, regExp, REG_EXTENDED|REG_NOSUB);
if( result != 0 ) {
fprintf( stderr, "Bad regular expresion \"%s\"\n", regExp );
exit( -1 );
}
// recursive calls
DIR * dir = opendir(dirA);
struct dirent * ent;
while ((ent = readdir(dir)) != NULL) {
regmatch_t match;
char *name = ent->d_name;
unsigned char type = ent->d_type;
if (regexec( &re, name, 1, &match, 0) == 0) {
if (rest == NULL) {
// terminating case
if (mode == 1 && type == DT_DIR) {
output[outputI] = (char *)calloc(strlen(name) + 2, sizeof(char));
strcpy(output[outputI], name);
output[outputI][strlen(name)] = '/';
} else {
output[outputI] = (char *)calloc(strlen(name) + 1, sizeof(char));
strcpy(output[outputI], name);
}
outputI++;
// dynamically sized array
if(outputI == outputSize) {
outputSize *= 2;
output = (char **)recallocarray(output, outputSize, sizeof(char *), outputSize / 2);
}
} else if (type == DT_DIR
&& strcmp(name, ".")
&& strcmp(name, "..")) {
// recursive case
int i = 0;
// construct a new dir to recurse on
char * newDir = (char *)calloc(strlen(dirA) + strlen(name) + 2, sizeof(char));
strcpy(newDir, dirA);
strcpy(newDir + strlen(dirA), name);
newDir[strlen(dirA) + strlen(name)] = '/';
// recursively acquire the expansion under new dir
char ** recOut = expandedPaths(newDir, rest, mode);
while(recOut[i]) {
switch (mode) {
case 0:
// append output to new dir and return
output[outputI] = (char *)calloc(strlen(recOut[i]) + strlen(name) + 2, sizeof(char));
strcpy(output[outputI], name);
output[outputI][strlen(name)] = '/';
strcpy(output[outputI] + strlen(name) + 1, recOut[i]);
break;
case 1:
// only return the leaf nodes
output[outputI] = (char *)calloc(strlen(recOut[i]) + 1, sizeof(char));
strcpy(output[outputI], recOut[i]);
}
free(recOut[i]);
i++;
outputI++;
// dynamically sized array
if(outputI == outputSize) {
outputSize *= 2;
output = (char **)recallocarray(output, outputSize, sizeof(char *), outputSize / 2);
}
}
free(recOut);
free(newDir);
}
}
}
regfree(&re);
closedir(dir);
free(rest);
free(regExp);
return output;
}
// tilda expansion
char * tilExp(const char * input) {
char * dir;
if(!strcmp(input, "~")) {
char * home = getenv("HOME");
dir = (char *)calloc(strlen(home) + 1, sizeof(char));
strcpy(dir, home);
} else {
dir = (char *)calloc(strlen(input) + 7, sizeof(char));
strcpy(dir, "/homes/");
strcpy(dir + 7, input + 1);
}
return dir;
}
// takes dir with wildcard and tilda and expands it
// mode: 0-full expansion 1-leaf expansion
char ** dirExp(const char * input, int mode) {
char ** exp;
char ** output;
int len = 0;
if (*input == '/') {
// starting from root dir
exp = expandedPaths("/", input + 1, mode);
if (mode == 1) return exp;
while(exp[len]) len++;
output = (char **)calloc(len + 1, sizeof(char *));
for(int i = 0; i < len; i++) {
output[i] = (char *)calloc(strlen(exp[i]) + 2, sizeof(char));
output[i][0] = '/';
strcpy(output[i] + 1, exp[i]);
free(exp[i]);
}
free(exp);
return output;
} else if (*input == '~') {
// expand home first
// isolate the username
char * home = (char *)calloc(strchr(input, '/') - input + 1, sizeof(char));
strncpy(home, input, strchr(input, '/') - input);
// get tilda expansion
char * dir = tilExp(home);
free(home);
// adding slash to the end
dir = (char *)realloc(dir, strlen(dir) + 1);
dir[strlen(dir) + 1] = 0;
dir[strlen(dir)] = '/';
// get the rest of the argument
char * rest = (char *)calloc(strchr(input, '/') - input, sizeof(char));
strcpy(rest, strchr(input, '/') + 1);
// acquire expanded result
exp = expandedPaths(dir, rest, mode);
free(rest);
if (mode == 1) {
// only leaf nodes required
free(dir);
return exp;
}
// full expansion: append and return
while(exp[len]) len++;
output = (char **)calloc(len + 1, sizeof(char *));
for(int i = 0; i < len; i++) {
output[i] = (char *)calloc(strlen(exp[i]) + strlen(dir) + 1, sizeof(char));
strcpy(output[i], dir);
strcpy(output[i] + strlen(dir), exp[i]);
free(exp[i]);
}
free(exp);
free(dir);
return output;
} else return expandedPaths(".", input, mode);
}
// dir expansion for read-line.c
extern "C" char ** expPath(const char * input, int mode) {
return dirExp(input, mode);
}
#if 0
main()
{
yyparse();
}
#endif