-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnutshscanner.l
266 lines (238 loc) · 10 KB
/
nutshscanner.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
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
%{
// This is ONLY a demo micro-shell whose purpose is to illustrate the need for and how to handle nested alias substitutions and how to use Flex start conditions.
// This is to help students learn these specific capabilities, the code is by far not a complete nutshell by any means.
// Only "alias name word", "cd word", and "bye" run.
#include "nutshparser.tab.h"
#include <string.h>
#include "global.h"
#include <dirent.h>
struct aTable aliasTable;
char* subAliases(char* name){
for (int i = 0; i < aliasIndex; i++) {
if(strcmp(aliasTable.name[i], name) == 0) {
return aliasTable.word[i];
}
}
return name;
}
bool ifAlias(char* name){
for (int i = 0; i < aliasIndex; i++) {
if(strcmp(aliasTable.name[i], name) == 0) {
return true;
}
}
return false;
}
bool commandExist(char* name);
%}
%array
%option noyywrap
%x string_condition
CHAR [.A-Za-z0-9!\/_-][.A-Za-z0-9!\/_-]*
ANYCHAR [)(*&%$#@!`;,\.a-zA-Z0-9'/*_=~ -][)(*&%$#@!`;,\.a-zA-Z0-9'/*_=~ -]*
%%
<string_condition>{ANYCHAR}+ { yylval.string = strdup(yytext); return STRING; }
<string_condition>[\"] {BEGIN(INITIAL);}
[ \t] { }
bye {
yylval.string = strdup(yytext);
if(!comInProgress) {comInProgress = !comInProgress; return BYE;}
else return STRING;
}
cd {
yylval.string = strdup(yytext); cd = true;
if(!comInProgress) {comInProgress = !comInProgress; return CD;}
else return STRING;
}
alias {
yylval.string = strdup(yytext); runAlias = true;
if(!comInProgress) {comInProgress = !comInProgress; return ALIAS;}
else return STRING;
}
unalias {
yylval.string = strdup(yytext); runAlias = true; unaliasCheck = true;
if(!comInProgress) {comInProgress = !comInProgress; return UNALIAS;}
else return STRING;
}
setenv {
yylval.string = strdup(yytext); runAlias = true;
if(!comInProgress) {comInProgress = !comInProgress; return SETENV;}
else return STRING;
}
unsetenv {
yylval.string = strdup(yytext);
if(!comInProgress) {comInProgress = !comInProgress; return UNSETENV;}
else return STRING;
}
printenv { return PRINTENV; }
[~.]?([/]{F_DName}*)* {
if (!comInProgress){ if(commandExist(yytext)) { comInProgress = true; return COMMAND; }
}
else { yylval.string = strdup(yytext); return STRING;}
}
[/]({F_DName}+[/])*{F_DName}+ {
if (!comInProgress){ if(commandExist(yytext)) { comInProgress = true; return COMMAND; }
}
else { yylval.string = strdup(yytext); return STRING;}
}
[a-zA-Z]+ {
//std::cout << "YYTEXT: " << yytext << std::endl;
char* yycopy = strdup( yytext );
//std::cout << runAlias << std::endl;
if (!runAlias)
{
std::string str = std::string(yytext);
std::string substr = str.substr(0, str.find(' '));
yycopy = const_cast<char*>(substr.c_str());
std::cout << "YYCOPY1: " << yycopy << std::endl;
if ( commandExist(yycopy) && cd != true)
{
printf("COMMAND\n");
yylval.string = strdup(yycopy); comInProgress = true;
return COMMAND;
}
}
else if (unaliasCheck)
{
std::cout << "UNALIASCHECK" << std::endl;
unaliasCheck = false;
yylval.string = strdup(yytext);
return STRING;
}
// else
// {
if(ifAlias(yytext))
{
//printf("yytext1: %s\n", yytext);
printf("IS AN ALIAS\n");
//source: https://www.cs.princeton.edu/~appel/modern/c/software/flex/flex.html
char *yycopy = strdup( subAliases(yytext) );
std::cout << yycopy << std::endl;
std::cout << "STRCMP:" << strcmp(yycopy, yytext) << std::endl;
if (!runAlias)
{
std::string str = std::string(aliases[yycopy].c_str());
std::string substr = str.substr(0, str.find(' '));
char* temp = const_cast<char*>(substr.c_str());
std::cout << "YYCOPY2: " << yycopy << std::endl;
std::cout << "STR: " << str << std::endl;
if ( commandExist(temp) && cd != true) //const_cast<char*>(aliases[temp].c_str())
{
//printf("COMMANDDIF\n");
std::cout << "YYTEXT: " << std::endl;
yylval.string = strdup(str.c_str());
return COMMAND;
}
if ( strcmp(yycopy, yytext) == 0 ) // so we don't run subAliases again if there no more nested aliases
{
printf("EHLLO\n");
yylval.string = strdup(yytext);
return STRING;
}
}
else if ( strcmp(yycopy, yytext) == 0 ) // so we don't run subAliases again if there no more nested aliases
{
printf("EHLLO\n");
yylval.string = strdup(yytext);
return STRING;
}
for ( int i = strlen(subAliases(yytext)) - 1; i >= 0; --i )
{
unput( yycopy[i] );
//std::cout << "YYCOPY[I]: " << yycopy[i] << std::endl;
}
free( yycopy );
}
else
{
//printf("yytext2: %s\n", yytext);
yylval.string = strdup(yytext);
return STRING;
};
//yylval.string = strdup(yytext);
//printf("GOES HERE INSTEAD\n");
//return STRING;
//}
}
}
"<" { yylval.string = strdup(yytext); if (cd || runAlias || unaliasCheck) { return STRING; } else { return IO; } }
">"[>]? { yylval.string = strdup(yytext); if (cd || runAlias || unaliasCheck) { return STRING; } else { return IO; } }
"2>"(&1)? { yylval.string = strdup(yytext); if (cd || runAlias || unaliasCheck) { return STRING; } else { return IOR; } }
"|" { if (cd || runAlias || unaliasCheck) { return STRING; } else { comInProgress = false; return PIPE; } }
"&" { if (cd || runAlias || unaliasCheck) { return STRING; } else { return AMPERSAND; } }
-[a-zA-Z0-9]* { yylval.string = strdup(yytext); return STRING; }
-[a-zA-Z=a-zA-Z]* { yylval.string = strdup(yytext); return STRING; }
"\n" { comInProgress = false; return END; }
; { comInProgress = false; return END; }
[\"] { BEGIN(string_condition); }
{CHAR}+ { if(ifAlias(yytext)) {
//printf("yytext: %s\n", yytext);
//source: https://www.cs.princeton.edu/~appel/modern/c/software/flex/flex.html
char *yycopy = strdup( subAliases(yytext) );
for ( int i = strlen(subAliases(yytext)) - 1; i >= 0; --i )
unput( yycopy[i] );
free( yycopy );
} else {
//printf("yytext: %s\n", yytext);
yylval.string = strdup(yytext);
return STRING;
};
}
. { printf("last resort: %s\n", yytext); yylval.string = strdup(yytext); return STRING;}
%%
bool commandExist(char* name)
{
DIR *dir;
struct dirent *ent;
bool found = false;
// std::cout << "COMMAND NAME: "<< name << std::endl;
// path_to_command/command
if( name[0] == '/')
{
std::string str(name);
std::string path = str.substr(0, str.find_last_of("/"));
if ((dir = opendir (path.c_str())) != NULL) {
/* compare all the files and directories within directory */
std::string c_name = str.substr(str.find_last_of("/")+1);
while ((ent = readdir (dir)) != NULL && !found) {
if( strcmp(ent->d_name, c_name.c_str()) == 0 )
{
found = true;
//printf ("Found! [%s]\n", name);
yylval.string = strdup(yytext);
}
}
closedir (dir);
} else {
/* could not open directory */
printf("Couldn't open directory\n");
printf("Error: [%s]\n", strerror(errno));
//return EXIT_FAILURE;
}
}
else
{
//Check all paths
std::string c(name);
int i=0;
while(i<myPaths.size() && !found){
c = name;
std::size_t f_pos = myPaths[i].find_last_of("/");
if(f_pos != myPaths[i].length() - 1 || f_pos == std::string::npos){
c = myPaths[i] + "/" + c;
}
else if (f_pos != std::string::npos)
c = myPaths[i] + c;
//printf("New c: [%s]\n", c.c_str());
char* temp = new char[c.length()+1];
strcpy(temp, c.c_str());
found = commandExist(temp);
if(found){
yylval.string = strdup(c.c_str());
break;
}
i++;
}
}
return found;
}