forked from jacobbrown72/jShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.y
More file actions
187 lines (156 loc) · 3.87 KB
/
Copy pathshell.y
File metadata and controls
187 lines (156 loc) · 3.87 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shell.h"
int yylex(void);
void yyerror(char*);
char* str;
int str_length;
%}
%union {
int i;
char *s;
char *w;
}
/*token definitions for built in functions*/
%token <i> SETENV PRINTENV UNSETENV CD ALIAS UNALIAS BYE
/*token definitions for metacharacters*/
%token <i> LT GT VERT QUOTE WACK AMP STDERR STDOUT STDIN END WHITESPACE
%token <w> WORD
%token <s> STRING
%start line
%%
line: END {YYACCEPT;}
| cmds END {YYACCEPT;}
| cmds AMP END {amp = 1; YYACCEPT;}
| cmds redir END {YYACCEPT;}
| cmds redir AMP END{amp = 1; YYACCEPT;}
cmds: cmd.args
| cmd.args VERT cmds
cmd.args: cmd args
cmd: SETENV {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "setenv");
new_cmd->bi_type = SET;
new_cmd->num_args = 0;
cmd_counter++;
bi = SET;
}
| PRINTENV {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "printenv");
new_cmd->bi_type = PRINT;
new_cmd->num_args = 0;
cmd_counter++;
bi = PRINT;
}
| UNSETENV {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "unsetenv");
new_cmd->bi_type = UNSET;
new_cmd->num_args = 0;
cmd_counter++;
bi = UNSET;
}
| CD {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "cd");
new_cmd->bi_type = CHANGE;
new_cmd->num_args = 0;
cmd_counter++;
bi = CHANGE;
}
| ALIAS {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "alias");
new_cmd->bi_type = AL;
new_cmd->num_args = 0;
cmd_counter++;
bi = AL;
}
| UNALIAS {
arg_counter = 0;
Cmd* new_cmd= &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "unalias");
new_cmd->bi_type = UNAL;
new_cmd->num_args = 0;
cmd_counter++;
bi = UNAL;
}
| BYE {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, "bye");
new_cmd->bi_type = BY;
new_cmd->num_args = 0;
cmd_counter++;
bi = BY;
}
| WORD {
arg_counter = 0;
Cmd* new_cmd = &cmd_table[cmd_counter];
strcpy(new_cmd->cmdname, str);
new_cmd->bi_type = 0;
new_cmd->num_args = 0;
cmd_counter++;
}
args: /*no arguments*/
| args WORD {
Cmd* my_cmd = &cmd_table[cmd_counter-1];
strcpy(my_cmd->arguments[arg_counter], str);
arg_counter++;
my_cmd->num_args = arg_counter;
}
| args quote {
Cmd* my_cmd = &cmd_table[cmd_counter-1];
strcpy(my_cmd->arguments[arg_counter], temp);
arg_counter++;
my_cmd->num_args = arg_counter;
}
quote: QUOTE words QUOTE
| QUOTE QUOTE {strcpy(temp, "");}
words: WORD {
strcat(temp, str);
}
| words WORD {
strcat(temp, " ");
strcat(temp, str);
}
redir: input_red output_red err_red
| input_red err_red output_red
| output_red input_red err_red
| output_red err_red input_red
| err_red input_red output_red
| err_red output_red input_red
input_red: LT infile {
inFile_red = 1;
}
output_red: GT outfile {
outFile_red = 1;
}
| GT GT outfile {
outFile_red = 1;
append = 1;
}
err_red: STDERR GT errfile {
errFile_red = 1;
}
infile: WORD {
strcpy(inFile, str);
}
outfile: WORD {
strcpy(outFile, str);
}
errfile: WORD {
strcpy(errFile, str);
}
%%
void yyerror(char *s) {
fprintf(stderr, "%s\n", s);
}