-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnutshparser.y
More file actions
422 lines (350 loc) · 10.7 KB
/
nutshparser.y
File metadata and controls
422 lines (350 loc) · 10.7 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
408
409
410
411
412
413
414
415
416
417
418
419
420
%{
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include "global.h"
int yylex();
int yyerror(char *s);
int runCD(char* arg);
int runSetAlias(char *name, char *word);
int exec_cmd(char** cmd, int arg_num);
%}
%union {char *string;}
%code {extern int cmd_num = 0;}
%code {extern char** args;}
%start cmd_line
%token <string> BYE CD STRING ALIAS SETENV END PRINTENV UNSETENV UNALIAS METACHARACTER
%%
cmd_line :
BYE END {exit(1); return 1; }
| CD END {runCD(varTable.word[1]); return 1;}
| CD STRING END {runCD($2); return 1;}
| ALIAS STRING STRING END {runSetAlias($2, $3); return 1;}
| ALIAS END {printAlias(); return 1;}
| SETENV STRING STRING END {setEnv($2, $3); return 1;}
| PRINTENV END {printEnv(); return 1;}
| UNSETENV STRING END {unsetEnv($2); return 1;}
| UNALIAS STRING END {unalias($2); return 1;}
| cmds END {parse_cmd(cmd_num); cmd_num = 0; YYACCEPT;}
;
cmds:
STRING {argTable.arg[cmd_num] = $1; cmd_num++;}
| STRING cmds {argTable.arg[cmd_num] = $1; cmd_num++;}
| METACHARACTER cmds {argTable.arg[cmd_num] = $1; cmd_num++;}
;
%%
int yyerror(char *s) {
printf("%s\n",s);
return 0;
}
int getPath(char* cmd, char** path_out)
{
char* path = malloc(sizeof(char)*256);
strcpy(path, "/usr/bin/");
strcat(path, cmd);
char* path2 = malloc(sizeof(char)*256);
strcpy(path2, "/bin/");
strcat(path2, cmd);
/* select the correct path by determining if file exists in that directory */
if(access(path, F_OK) == 0){
*path_out = path;
}
else if(access(path2, F_OK) == 0)
*path_out = path2;
//printf("\nPATH: %s\n", *path_out);
}
int parse_cmd(int arg_num)
{
char** partition_cmd = calloc(256, sizeof(char));
int partition_arg = 0;
/* argTable catchs commands backward, this reverse to
proper format */
char** reverse[arg_num+1];
int exe_index = arg_num-1;
for(int i = 0; i < arg_num; i++)
{
reverse[exe_index--] = argTable.arg[i];
}
reverse[arg_num] = NULL;
char* in_file = NULL;
char* out_file = NULL;
bool entered = true;
/* loop through entire argument.
Catches redirection i/o */
for(int i = 0; i < arg_num; i++)
{
if(strcmp(reverse[i], ">") == 0)
{
out_file = reverse[++i];
entered = false;
//for(int j = 0; j < partition_arg; j++)
//printf("\n >>>>> pi: %d arg: %s\n", j, partition_cmd[j]);
int pid = fork();
if(pid == 0)
{
if(out_file != NULL)
{
int fd;
if((fd = open(out_file, O_WRONLY | O_CREAT | O_TRUNC, 0644)) < 0)
{
perror("Unable to open file");
exit(1);
}
dup2(fd, STDOUT_FILENO);
close(fd);
}
exec_cmd(partition_cmd, partition_arg);
exit(1);
}
}
else if(strcmp(reverse[i], "<") == 0)
{
in_file = reverse[++i];
entered = false;
int pid = fork();
//for(int j = 0; j < partition_arg; j++)
//printf("\n <<<<< pi: %d arg: %s\n", j, partition_cmd[j]);
if(pid == 0)
{
if(in_file != NULL)
{
int fd;
if(fd = open(in_file, O_RDONLY) < 0)
{
perror("Unable to open file");
exit(1);
}
dup2(fd, STDIN_FILENO);;
close(fd);
}
//partition_cmd[partition_arg] = reverse[i];
if(strcmp(i+1, NULL))
partition_cmd[partition_arg] = reverse[i];
exec_cmd(partition_cmd, partition_arg);
exit(1);
}
}
else if(strcmp(reverse[i], "|") == 0)
{
char** pipe_right = calloc(256, sizeof(char));
int pipe_num = 0;
i++;
while(i < arg_num && strcmp(reverse[i],"|") != 0 && strcmp(reverse[i],">") && strcmp(reverse[i],"|"))
{
pipe_right[pipe_num++] = reverse[i++];
}
int in = 0;
int out = 1;
pid_t pid1;
pid_t pid2;
int fd[2];
pipe(fd);
pid1 = fork();
if(pid1 == 0)
{
char* path_out = NULL;
getPath(pipe_right[0], &path_out);
pipe_right[0] = path_out;
close(fd[out]);
dup2(fd[in], STDIN_FILENO);
close(fd[in]);
execv(pipe_right[0], pipe_right);
perror("Error child exec failed");
exit(1);
}
pid2 = fork();
if(pid2 == 0){
char* path_out = NULL;
getPath(partition_cmd[0], &path_out);
partition_cmd[0] = path_out;
close(fd[in]);
dup2(fd[out], STDOUT_FILENO);
close(fd[out]);
execv(partition_cmd[0], partition_cmd);
perror("Error parent exec failed");
exit(1);
}
close(fd[0]);
close(fd[1]);
waitpid(0);
waitpid(0);
}
partition_cmd[partition_arg++] = reverse[i];
//printf("\narg num: %d\n", partition_arg);
//for(int j = 0; j < partition_arg; j++)
//printf("\ni: %d arg: %s\n", j, partition_cmd[j]);
/* Executes command if no I/O is present */
if(i == arg_num-1 && entered)
{
exec_cmd(partition_cmd, partition_arg);
}
}
}
int exec_cmd(char** cmd, int arg_num)
{
char* path[256];
strcpy(path, "/usr/bin/");
strcat(path, cmd[0]);
char* path2[256];
strcpy(path2, "/bin/");
strcat(path2, cmd[0]);
/* select the correct path by determining if file exists in that directory */
if(access(path, F_OK) == 0){
cmd[0] = path;
}
else if(access(path2, F_OK) == 0)
cmd[0] = path2;
cmd[arg_num] = NULL;
/* fork to call execv to execute shell command
waits to finish to display result before asking for next input */
pid_t pid;
int status;
if((pid = fork()) < 0){
printf("Fork failed\n");
exit(1);
}
else if(pid == 0){
if(status = execv(cmd[0], cmd) < 0){
perror("Error child exec failed");
perror("Error exec failed\n");
exit(1);
}
}
else{
waitpid(pid, &status, 0);
}
//free(cmd);
return 1;
}
int runCD(char* arg) {
if (arg[0] != '/') { // arg is relative path
char* temp;
temp = malloc(1000);
strcpy(temp, varTable.word[0]);
strcat(varTable.word[0], "/");
strcat(varTable.word[0], arg);
if(chdir(varTable.word[0]) == 0) {
strcpy(aliasTable.word[0], varTable.word[0]);
strcpy(aliasTable.word[1], varTable.word[0]);
char *pointer = strrchr(aliasTable.word[1], '/');
while(*pointer != '\0') {
*pointer ='\0';
pointer++;
}
}
else {
strcpy(varTable.word[0], temp);
printf("%s Directory not found\n", arg);
return 1;
}
free(temp);
}
else { // arg is absolute path
if(chdir(arg) == 0){
strcpy(aliasTable.word[0], arg);
strcpy(aliasTable.word[1], arg);
strcpy(varTable.word[0], arg);
char *pointer = strrchr(aliasTable.word[1], '/');
while(*pointer != '\0') {
*pointer ='\0';
pointer++;
}
}
else {
printf("Directory not found\n");
return 1;
}
}
return 1;
}
int runSetAlias(char *name, char *word) {
for (int i = 0; i < aliasIndex; i++) {
if(strcmp(name, word) == 0){
printf("Error, expansion of \"%s\" would create a loop.\n", name);
return 1;
}
else if((strcmp(aliasTable.name[i], name) == 0) && (strcmp(aliasTable.word[i], word) == 0)){
printf("Error, expansion of \"%s\" would create a loop.\n", name);
return 1;
}
else if(strcmp(aliasTable.name[i], name) == 0) {
strcpy(aliasTable.word[i], word);
return 1;
}
}
strcpy(aliasTable.name[aliasIndex], name);
strcpy(aliasTable.word[aliasIndex], word);
aliasIndex++;
return 1;
}
int setEnv(char* var, char* word){
for (int i = 0; i < varIndex; i++){
if (strcmp(varTable.var[i], var) == 0){
strcpy(varTable.word[i], word);
printf("Eviroment Variable %s Updated \n", var);
return 1;
}
}
strcpy(varTable.var[varIndex], var);
strcpy(varTable.word[varIndex], word);
varIndex++;
printf("Eviroment Variable %s Added \n", var);
return 1;
}
int printEnv(){
for (int i = 0; i < varIndex; i++){
printf("%s = %s\n", varTable.var[i], varTable.word[i]);
}
return 1;
}
int unsetEnv(char *variable){
int var_location = -1;
for (int i = 0; i < varIndex; i++){
if(strcmp(varTable.var[i], variable) == 0){
var_location = i;
}
}
if (var_location != -1){
strcpy(varTable.var[var_location], "");
strcpy(varTable.word[var_location], "");
for(int i = var_location + 1; i < varIndex; i++){
strcpy(varTable.var[i - 1], varTable.var[i]);
strcpy(varTable.word[i - 1], varTable.word[i]);
}
strcpy(varTable.var[varIndex - 1], "");
strcpy(varTable.word[varIndex - 1], "");
varIndex--;
}
return 1;
}
int unalias(char *variable){
int var_location = -1;
for (int i = 0; i < aliasIndex; i++){
if(strcmp(aliasTable.name[i], variable) == 0){
var_location = i;
}
}
if (var_location != -1){
strcpy(aliasTable.name[var_location], "");
strcpy(aliasTable.word[var_location], "");
for(int i = var_location + 1; i < aliasIndex; i++){
strcpy(aliasTable.name[i - 1], aliasTable.name[i]);
strcpy(aliasTable.word[i - 1], aliasTable.word[i]);
}
strcpy(aliasTable.name[aliasIndex - 1], "");
strcpy(aliasTable.word[aliasIndex - 1], "");
aliasIndex--;
}
useAlias = true;
return 1;
}
int printAlias(){
for (int i = 0; i < aliasIndex; i++){
printf("%s = %s\n", aliasTable.name[i], aliasTable.word[i]);
}
return 1;
}