-
Notifications
You must be signed in to change notification settings - Fork 9
/
shell.l
195 lines (148 loc) · 2.7 KB
/
shell.l
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
/*
*
* CS-252 Fall 2017
* shell.l: lexical analyzer for shell
* You have to extend it.
*
*/
%{
#include <cstring>
#include <unistd.h>
#include "y.tab.hh"
#include "command.hh"
//////////// Start added code ///////////
extern "C" char * read_line();
int mygetc(FILE * f) {
static char *p;
char ch;
if (!isatty(0)) {
// stdin is not a tty. Call real getc
return getc(f);
}
// stdin is a tty. Call our read_line.
if (p==NULL || *p == 0) {
char * s = read_line();
p = s;
}
ch = *p;
p++;
return ch;
}
#undef getc
#define getc(f) mygetc(f)
/////////// End added code ///////////
static void yyunput (int c,char *buf_ptr );
void myunputc(int c) {
unput(c);
}
%}
%%
\n {
return NEWLINE;
}
[ \t] {
/* Discard spaces and tabs */
}
">" {
return GREAT;
}
">>" {
return GREATGREAT;
}
"2>" {
return TWOGREAT;
}
"|" {
return PIPE;
}
"&" {
return AMPERSAND;
}
"<" {
return LESS;
}
">&" {
return GREATAMPERSAND;
}
">>&" {
return GREATGREATAMPERSAND;
}
`[^\n`]*` {
char * command = strdup(yytext+1);
if(command[yyleng-2] == '`')
command[yyleng-2] = '\0';
int tmpin = dup(0);
int tmpout = dup(1);
int fdpipein[2];
int fdpipeout[2];
pipe(fdpipein);
pipe(fdpipeout);
write(fdpipein[1], command, strlen(command));
write(fdpipein[1], "\n", 1);
write(fdpipein[1], "exit", 4);
write(fdpipein[1], "\n", 1);
close(fdpipein[1]);
dup2(fdpipein[0], 0);
close(fdpipein[0]);
dup2(fdpipeout[1], 1);
close(fdpipeout[1]);
int ret = fork();
if (ret == 0) {
execvp("/proc/self/exe", NULL);
_exit(1);
} else if (ret < 0) {
perror("fork");
exit(1);
}
dup2(tmpin, 0);
dup2(tmpout, 1);
close(tmpin);
close(tmpout);
char ch;
char * buffer = (char *) malloc (4096);
int i = 0;
// Read from the pipe the output of the subshell
while (read(fdpipeout[0], &ch, 1)) {
if (ch == '\n') buffer[i++] = ' ';
else buffer[i++] = ch;
}
buffer[i] = '\0';
for (i = strlen(buffer); i >= 0; i--) {
unput(buffer[i]);
}
}
["][^\n\"]*["] {
/* quotes */
yylval.string_val = strdup(yytext+1);
if (yylval.string_val[yyleng-2] == '"')
yylval.string_val[yyleng-2] = 0;
return WORD;
}
[^ \t\n|><&]*\\[^ \t\n]* {
/* escaping */
//printf("yyval=%s\n",yylval.string_val);
int i = 0;
char * escape = (char *)malloc(200);
char * temp = yytext;
while(*temp){
if (*temp == '\\'){
if(*(temp+1) == '\\'){
temp = temp + 2;
escape[i++] = '\\';
} else {
escape[i++] = *(++temp);
}
} else {
escape[i++] = *temp;
}
temp++;
}
escape[i] = '\0';
yylval.string_val = strdup(escape);
return WORD;
}
[^ \t\n|><][^ \t\n|><]* {
/* Assume that file names have only alpha chars */
yylval.string_val = strdup(yytext);
return WORD;
}