-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.y
More file actions
170 lines (148 loc) · 3.75 KB
/
Copy pathshell.y
File metadata and controls
170 lines (148 loc) · 3.75 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
%{
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Command.h"
#include "executor.h"
void yyerror(const char *s);
int yylex(void);
void expand_variables(void);
int is_builtin(const char *cmd);
void execute_builtin(void);
extern int yyline;
extern FILE *yyin;
void reset_commands(void) {
int i, j;
for (i = 0; i < num_commands; i++) {
for (j = 0; j < commands[i].argc; j++) {
free(commands[i].args[j]);
}
if (commands[i].input_file) free(commands[i].input_file);
if (commands[i].output_file) free(commands[i].output_file);
}
memset(commands, 0, sizeof(commands));
num_commands = 0;
current_cmd = 0;
is_pipeline = 0;
}
void init_command(void) {
commands[current_cmd].argc = 0;
commands[current_cmd].input_file = NULL;
commands[current_cmd].output_file = NULL;
commands[current_cmd].append_output = 0;
commands[current_cmd].background = 0;
}
%}
%union {
char *string;
}
%token <string> WORD VARREF
%token NEWLINE LESS GREAT GREATGREAT PIPE AMPERSAND EQUALS
%type <string> word_or_var
%%
input:
| input command_line
;
command_line:
NEWLINE {
reset_commands();
printf("SlimHady'sShell> ");
fflush(stdout);
}
| command NEWLINE {
if (num_commands > 0 && commands[0].argc > 0) {
expand_variables();
if (is_pipeline) {
execute_pipeline();
} else if (is_builtin(commands[0].args[0])) {
execute_builtin();
} else {
execute_command();
}
}
reset_commands();
printf("SlimHady'sShell> ");
fflush(stdout);
}
| assignment NEWLINE {
reset_commands();
printf("SlimHady'sShell> ");
fflush(stdout);
}
| error NEWLINE {
yyerrok;
reset_commands();
printf("SlimHady'sShell> ");
fflush(stdout);
}
;
assignment:
WORD EQUALS word_or_var {
setenv($1, $3, 1);
free($1);
free($3);
}
;
command:
simple_command
| pipeline
;
pipeline:
simple_command PIPE {
is_pipeline = 1;
current_cmd++;
} simple_command {
num_commands = 2;
}
;
simple_command:
cmd_name args redirects
| cmd_name args redirects AMPERSAND {
commands[current_cmd].background = 1;
}
;
cmd_name:
word_or_var {
init_command();
if (commands[current_cmd].argc < MAX_ARGS - 1) {
commands[current_cmd].args[commands[current_cmd].argc++] = $1;
commands[current_cmd].args[commands[current_cmd].argc] = NULL;
}
num_commands = current_cmd + 1;
}
;
args:
| args word_or_var {
if (commands[current_cmd].argc < MAX_ARGS - 1) {
commands[current_cmd].args[commands[current_cmd].argc++] = $2;
commands[current_cmd].args[commands[current_cmd].argc] = NULL;
}
}
;
redirects:
| redirects redirect
;
redirect:
LESS word_or_var {
if (commands[current_cmd].input_file) free(commands[current_cmd].input_file);
commands[current_cmd].input_file = $2;
}
| GREAT word_or_var {
if (commands[current_cmd].output_file) free(commands[current_cmd].output_file);
commands[current_cmd].output_file = $2;
commands[current_cmd].append_output = 0;
}
| GREATGREAT word_or_var {
if (commands[current_cmd].output_file) free(commands[current_cmd].output_file);
commands[current_cmd].output_file = $2;
commands[current_cmd].append_output = 1;
}
;
word_or_var:
WORD { $$ = $1; }
| VARREF { $$ = $1; }
;
%%
void yyerror(const char *s) {
fprintf(stderr, "Error: %s at line %d\n", s, yyline);
}