-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiosh.cpp
More file actions
257 lines (238 loc) · 8.69 KB
/
iosh.cpp
File metadata and controls
257 lines (238 loc) · 8.69 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
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
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iostream>
#include <fstream>
#include "global.h"
std::vector<token> vTokens;
std::string sPrompt = "ios";
std::string sCurDir = "";
bool bDebugFlag = false;
using namespace std;
void printError(string s){
cout << "Error: " << s << endl;
}
bool runcmd(int cmd_poss){
//build cmd array and args
string cmd_passed = vTokens[cmd_poss].getValue();
int num_of_args = 2;
int iter = cmd_poss+1;
while(vTokens[iter].getType() == WORD){
num_of_args++;
iter++;
}
const char* cmd = cmd_passed.c_str();
char* args[num_of_args];
char *cmdstr = new char [cmd_passed.length()+1];
strcpy (cmdstr, cmd_passed.c_str());
args[0] = cmdstr;
args[num_of_args-1] = NULL;
for(int i=cmd_poss+1; i<num_of_args-1;i++){
string s = vTokens[i].getValue();
char *cstr = new char [s.length()+1];
strcpy (cstr, s.c_str());
args[i] = cstr;
}
if(fork()){
wait(0);
}
else {
execvp(cmd,args);
cout << "Command not found: " << cmd << endl;
exit(EXIT_SUCCESS);
}
return true;
}
bool parser(){
int firstType = vTokens[0].getType();
string firstValue = vTokens[0].getValue();
int commandpos =0;
bool haspipe = false;
if(firstValue != "exit" && firstValue != "quit"){
switch( firstType ){
case ENDOFFILE:
return false;
case METACHAR:
if( firstValue == "#" )
return true;
else
printError("metachar default");
break;
case 260:
break;
case WORD:
//test for piping
for(int i=1; i<vTokens.size()-1; i++){
if(vTokens[i].getValue() == "<" || vTokens[i].getValue() == ">"){
haspipe = true;
}
}
if( firstValue == "setprompt" ) {
if(vTokens[1].getType()==STRING)
sPrompt = vTokens[1].getValue()+ " ";
else
printError("Usage: setprompt <string>");
}
else if( firstValue == "debug" ) {
if(vTokens[1].getType()==WORD) {
if(vTokens[1].getValue()=="on"){
bDebugFlag = true;
}
else if (vTokens[1].getValue()=="off"){
bDebugFlag = false;
}
else
printError("Usage: debug on (or off)");
}
else
printError("Usage: debug on (or off)");
break;
}
else if( firstValue == "chdir" ) {
string s = vTokens[1].getValue();
char *cstr = new char [s.length()+1];
strcpy (cstr, s.c_str());
int retValue;
retValue = chdir(cstr);
if(retValue){
printError("chdir error");
}
} else if (haspipe) {
int ifd;
int ofd;
//find optional input file
bool hasinfile = false;
string ifilename;
for(int i=0; i<vTokens.size();i++){
if(vTokens[i].getValue() == "<"){
if((i-1)>=0){
ifilename = vTokens[i-1].getValue();
hasinfile = true;
commandpos = i+1;
} else {
printError("No inFile given");
}
}
}
if(hasinfile){
char *icstrfile = new char [ifilename.length()+1];
strcpy (icstrfile, ifilename.c_str());
if ((ifd = open(icstrfile, O_RDONLY)) < 0) {
perror("inFile Open");
hasinfile = false;
}
}
//find optional outfile
bool hasoutfile = false;
string ofilename;
for(int i=0; i<vTokens.size();i++){
if(vTokens[i].getValue() == ">"){
ofilename = vTokens[i+1].getValue();
hasoutfile = true;
}
}
if(hasoutfile){
char *ocstrfile = new char [ofilename.length()+1];
strcpy (ocstrfile, ofilename.c_str());
//create file always
FILE * pFile;
pFile = fopen(ocstrfile, "w");
fclose(pFile);
if ((ofd = open(ocstrfile, O_WRONLY)) < 0) {
perror("outFile Open");
hasoutfile = false;
}
}
//infile successfully opened, build args list
if(hasinfile && !hasoutfile){
int fdhold = dup(0);
dup2(ifd, 0);
if(!runcmd(commandpos)){
cout << "No Command found: " << firstValue << endl;
cout << "Usage: <command> <arguments>" << endl;
};
//redirect io
dup2(fdhold, 0);
}
//output file successfully open
if(hasoutfile && !hasinfile){
int fdhold = dup(1);
dup2(ofd, 1);
if(!runcmd(commandpos)){
cout << "No Command found: " << firstValue << endl;
cout << "Usage: <command> <arguments>" << endl;
};
//redirect io
dup2(fdhold, 1);
}
if(hasinfile && hasoutfile){
//read file into memory
char buf[512];
int nbytes; /* number of bytes read */
/* Open file fd ... */
/* Then read up to 512 bytes from file fd */
if ((nbytes = read(ifd, buf, sizeof(buf))) < 0) {
perror("inFile read");
}
int fdhold = dup(1);
int fdhold2 = dup(0);
dup2(ofd, 1);
dup2(ifd, 0);
if(!runcmd(commandpos)){
cout << "No Command found: " << firstValue << endl;
cout << "Usage: <command> <arguments>" << endl;
};
//redirect io
dup2(fdhold, 1);
dup2(fdhold2, 0);
}
}
else {
//attempt to run the command
if(!runcmd(commandpos)){
cout << "No Command found: " << firstValue << endl;
cout << "Usage: <command> <arguments>" << endl;
};
}
break;
default:
cout<<firstValue;
printError("firstType default");
break;
}
return true;
}
else {
return false;
}
}
void printPrompt(){
if(sCurDir == "")
{
cout << sPrompt << ":~$ ";
}
else{
cout << sPrompt << ":~" << sCurDir << "$ ";
}
}
int main(){
int lookahead;
do{
// reset vTokens
vTokens.clear();
printPrompt();
do{
lookahead = yylex();
} while(lookahead != ENDOFLINE && lookahead !=0);
if(bDebugFlag){
// possible move this stuff to a function and add more
cout << "Tokens: " << endl;
for( int i = 0; i < vTokens.size(); i++ ){
cout << " Token " << i << ":" << endl
<< " Type: " << vTokens[i].getType() << endl
<< " Value: " << vTokens[i].getValue() << endl;
}
}
} while (parser()&& lookahead!=0);
return 0;
}