-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshell_flex.ll
231 lines (199 loc) · 5.4 KB
/
shell_flex.ll
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
%{
// Copyright 2016 Hunter L. Allen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/* remove register keyword */
#define register
#include <string.h>
#include "shell_bison.hh"
#include "shell-readline.hpp"
/******************* Input From read-shell ************************/
#include <unistd.h>
std::shared_ptr<std::string> lastLine = nullptr;
read_line reader;
int mygetc (FILE * f) {
static std::shared_ptr<std::string> p = nullptr;
static int get_file = 0;
char ch;
if (!Command::currentCommand.is_interactive()) return getc(f);
if (p == nullptr || !p->size()) {
if (get_file) {
p = reader.getStashed();
get_file = 0;
lastLine = p;
} else {
reader.tty_raw_mode();
reader();
reader.unset_tty_raw_mode();
p = reader.get();
lastLine = p;
}
}
ch = p->at(0);
p->erase(p->begin(), p->begin() + 1);
return ch;
}
#undef getc
#define getc(f) mygetc(f)
/******************* Ending of shell input *************************/
static void yyunput (int c,char *buf_ptr);
void myunputc(int c) {
unput(c);
}
/* Utils copied from esh for handling quoted strings in fancy flex mode stuff */
char* strbuf = nullptr;
int stringIndex = 0;
int strbufSize = 0;
#define DEFAULT_STRBUF_SIZE 256
void strbufWrite(char c) {
if( stringIndex >= strbufSize ) {
strbuf = (char*)realloc(strbuf,sizeof(char)*(strbufSize*=2));
}
strbuf[stringIndex++] = c;
}
/* end fancy string utils */
%}
%option noyywrap
%x QUOTED_STRING
%x LINE_COMMENT
%%
\n { return NEWLINE; }
[ ] { /* Discard spaces */ }
\t { /* tabs */ return TAB; }
<INITIAL>"#" BEGIN(LINE_COMMENT);
<LINE_COMMENT>[\n] BEGIN(INITIAL);
<LINE_COMMENT>[.]+ { /* Ignore anything in a line comment */ }
"&&" return ANDAND;
">" return GREAT;
">&" return GREATAND;
"<" return LESS;
">>" return TOOGREAT;
">>&" return TOOGREATAND;
"|" return PIPE;
"&" return AMPERSAND;
^"exit" { std::cout<<"Bye!"<<std::endl; exit(0); }
^"time" { return TIME; }
^"source" { return SRC; }
^"alias" { return ALIAS; }
^"popd" { return POPD; }
`(\\.|[^`"])*` {
/* Subshell, by Drew Barthel */
/* Remove backticks */
char* string = strdup(yytext+1);
string[strlen(string)-1]='\n';
/* Set up temp string and pipes */
char buffer[1024];
int pipeOne[2], pipeTwo[2]; //Double the pipes? double the fun!
if (pipe(pipeOne) == -1) {
perror("subshell pipe");
free(string);
return 1;
} else if (pipe(pipeTwo) == -1) {
perror("subshell pipe");
free(string);
return 1;
}
/* Housekeeping shit */
int tempIn = dup(0);
int tempOut = dup(1);
dup2(pipeOne[1], 1); close(pipeOne[1]);
dup2(pipeTwo[0], 0); close(pipeTwo[0]);
// Get ready world.
pid_t ret = fork(); //Deekeeds
switch(ret) {
case(-1): {
perror("fork-subshell");
exit(1);
} case (0): {
//Housekeeping
dup2(pipeOne[0], 0);
dup2(pipeTwo[1], 1);
close(pipeOne[0]);
close(pipeTwo[1]);
// Set up for self call
char* args[2];
args[0] = strdup("/proc/self/exe"); //Call self
args[1] = nullptr;
// Call self
execvp(args[0], args);
// You shouldn't be around these parts boy
perror("execvp-subshell");
exit(1);
} default: {
/* Write to pipe. */
size_t length = strlen(string), i = 0;
for(;i < length && write(1, string + i,1); i++);
//Housekeeping
dup2(tempOut, 1);
close(tempOut);
close(pipeTwo[1]);
// Read from pipe.
char* temp = buffer;
char c = 0;
while(read(0,&c,1)) {
if(c=='\n') *temp = ' ';
else *temp = c;
temp++;
} temp--;
/* Clear uneeded things */
while(temp>=buffer){unput(*temp); temp--;}
// Final housecleaning
dup2(tempIn, 0);
close(tempIn);
break;
}
/* Wait for all processes */
} waitpid(ret,nullptr,0);
free(string);
}
\" {
BEGIN QUOTED_STRING;
stringIndex = 0;
Command::currentCommand.set_expand(false);
strbuf = new char[(strbufSize = DEFAULT_STRBUF_SIZE)];
}
<QUOTED_STRING>\\n {
strbufWrite('\n');
}
<QUOTED_STRING>\\t {
strbufWrite('\t');
}
<QUOTED_STRING>\\\" {
strbufWrite('\"');
}
<QUOTED_STRING>\" {
strbufWrite('\0');
yylval.string_val = strbuf;
BEGIN 0;
return WORD;
}
<QUOTED_STRING>\n {
std::cout<<"> "<<std::flush;
strbufWrite('\n');
}
<QUOTED_STRING>. {
strbufWrite(*yytext);
}
(([^ \"\t\n\|\>\<\&\[\]])|(\\.))+ {
yylval.string_val = new char[strlen(yytext) + 1];
strcpy(yylval.string_val, yytext);
size_t strs_len = strlen(yylval.string_val);
for(size_t i = 0; i < strs_len; ++i) {
if(yylval.string_val[i] == '\\') {
for(size_t j = i; j < strs_len; ++j) {
yylval.string_val[j] = yylval.string_val[j+1];
}
}
}
return WORD;
}