-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmyshell.cpp
306 lines (294 loc) · 9.97 KB
/
myshell.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include <unistd.h> // getpid(), getcwd()
#include <sys/types.h> // type definitions, e.g., pid_t
#include <sys/wait.h> // wait()
#include <signal.h> // signal name constants and kill()
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <sys/stat.h> //for open/close
#include <fcntl.h> // for open/close
using namespace std;
FILE *input=NULL;
int batch = 0;
int endSh=0;
char* argv[100];
int bg = 0;
void batchParse(char* line);
void execute(char* cmd, char *argv[],int argn);
void parse(char* line);
void setNull();
void multipipe(char *cmd,char* args[], int argn);
int main(int argc,char *argv[]){
if(!endSh){
if(argc==1&&!endSh){ //interactive mode
while(true&&!endSh){
setNull(); //set argv to null
string cmdLine;
printf("myshell> ");
if(getline(cin,cmdLine)==0){endSh=1;}
char *cmd = new char[cmdLine.length()+1];
strcpy(cmd,cmdLine.c_str());
if((cmd!=" ")&&(cmd!="\n")&&(!endSh)){
setNull();
batchParse(cmd);
}
}
}
else if((argc==2)&&!endSh){ //batch mode
//TODO: errors in batch mode must stop code and print “Error: command exited with non-zero exit code”
batch = 1;
ifstream file(argv[1]);
string nextinst;
while(getline(file,nextinst)&&!endSh){
//parse command
char *inst = new char[nextinst.length()+1];
strcpy(inst,nextinst.c_str());
if((inst!="")&&(inst!=" ")&&(inst!="\n")&&(inst!="\t")&&(inst!="\0")){
batchParse(inst);
}
}
argc=0;
}
else if(endSh){ return 0; }
else{
cout<< "Invalid number of arguments passed" << endl;
return -1;
}
}
return 0;
}
void setNull(){
for(int i=0; i<100; i++){
argv[i]=NULL;
}
}
//parses each line into commands
void batchParse(char* line){ //seperate commands based on ; and new lines
char *p = strtok(line, ";\n");
while (p) {
if((p!=" ")&&(p!="\n")&&(p!="\0")){
if(batch){cout << "Command: " << p << endl;} //TODO: USE FFLUSH TO PRINT ASAP(or not because thats dumb
parse(p);
}
p = strtok(NULL, ";\n");
}
}
//parses each command into the command and arguments
void parse(char* line){ //seperate commands and arguments based in whitespace
int i = 0;
while(line[i]!='\0'){
if(line[i]=='&'){bg=1;line[i]='\0';}
i++;
}
string buf; // Have a buffer string
stringstream ss(line); // Insert the string into a stream
vector<char*> tokens; // Create vector to hold our words
while (ss >> buf){
char *temp = new char[buf.length()+1];
strcpy(temp,buf.c_str());
tokens.push_back(temp);
}
char** argv = new char*[tokens.size()+1]; //set char array to hold args
for ( int k = 0; k < tokens.size(); k++ ){ //TODO: EXECUTE COMMANDS
argv[k] = tokens[k];
}
argv[tokens.size()] = NULL;
execute(argv[0],argv, tokens.size());
bg=0;
}
//executes commands
void execute(char* cmd, char *args[],int argn){ //TODO: EXECUTE JOBS, deal with background processes
//argn = # of arguments and the command, so ls -a would be argn 2, just as argv[0] is ls and argv[1] is -a
int piping = 0;
for(int i=0; i<argn; i++){
if(strcmp(args[i],"|")==0){piping=1;}
}
if(strcmp(args[argn-1],"&")==0){
bg=1;
}
if((strcmp(cmd,"exit")==0)||(strcmp(cmd,"quit")==0)){ //built-in command quit
endSh=1;
}
else if(piping){
for(int i=0; i<argn; i++){
if(strcmp(args[i],"|")==0){
args[i] = NULL;
char* right[argn-i];
int c = 0;
for(int j=i; j<argn-1; j++){
right[j-i]=args[j+1];
c++;
}
right[c]=NULL;
int p[2];
pipe(p);
pid_t lpid = fork();
if(lpid==0){//left (child)
dup2(2,1);
dup2(p[1],STDOUT_FILENO);
execvp(cmd,args);
}
else{//right (parent)
pid_t rpid = fork();
close(p[1]);
if(rpid == 0){ //right child
dup2(2,1);
dup2(p[0],STDIN_FILENO);
multipipe(right[0],right,c);
//close(p[1]);
}
else{ //parent
if(!bg){waitpid(rpid,0,0);}
}
}
break;
}
}
}
else{
if(strcmp(cmd,"cd")==0){ //built-in command cd
if(args[1]==NULL){
chdir("/");
}
else{
chdir(args[1]);
}
}
else{ //standard forking process
pid_t childID = fork();
if(childID<0){
perror("Error when forked");
endSh=1;
}
else if(childID==0){ //Child process
for(int i=0; i<argn; i++){
if(strcmp(args[i],">")==0){
int newstdout = open(args[i+1],O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO);
close(1);
dup(newstdout);
close(newstdout);
args[i]=NULL;
}
else if(strcmp(args[i],"<")==0){
int newstdin = open(args[i+1],O_RDONLY);
close(0);
dup(newstdin);
close(newstdin);
args[i]=NULL;
}
}
execvp(cmd,args);
perror(cmd); //only happens when there is an error
endSh=1;
}
else{
if(!bg){ //if not a background process, wait for it
if (waitpid(childID,0,0)<0){//parent process
endSh=1;
perror("Error when waiting for child");
}
}
}
}
}
}
void multipipe(char *cmd,char* args[], int argn){
int piping = 0;
for(int i=0; i<argn; i++){
if(strcmp(args[i],"|")==0){piping=1;}
}
if(strcmp(args[argn-1],"&")==0){
bg=1;
}
if((strcmp(cmd,"exit")==0)||(strcmp(cmd,"quit")==0)){ //built-in command quit
endSh=1;
}
else if(piping){
for(int i=0; i<argn; i++){
if(strcmp(args[i],"|")==0){
args[i] = NULL;
char* right[argn-i];
int c = 0;
for(int j=i; j<argn-1; j++){
right[j-i]=args[j+1];
c++;
}
right[c]=NULL;
int p[2];
pipe(p);
pid_t lpid = fork();
if(lpid==0){//left (child)
dup2(2,1);
dup2(p[1],STDOUT_FILENO);
execvp(cmd,args);
}
else{//right (parent)
pid_t rpid = fork();
close(p[1]);
if(rpid == 0){ //right child
dup2(2,1);
dup2(p[0],STDIN_FILENO);
multipipe(right[0],right,c);
//close(p[1]);
}
else{ //parent
if(!bg){waitpid(rpid,0,0);}
}
}
break;
}
}
}
else{
if(strcmp(cmd,"cd")==0){ //built-in command cd
if(args[1]==NULL){
chdir("/");
}
else{
chdir(args[1]);
}
}
else{ //standard forking process
pid_t childID = fork();
if(childID<0){
perror("Error when forked");
endSh=1;
}
else if(childID==0){ //Child process
for(int i=0; i<argn; i++){
if(strcmp(args[i],">")==0){
int newstdout = open(args[i+1],O_WRONLY|O_CREAT,S_IRWXU|S_IRWXG|S_IRWXO);
close(1);
dup(newstdout);
close(newstdout);
args[i]=NULL;
}
else if(strcmp(args[i],"<")==0){
int newstdin = open(args[i+1],O_RDONLY);
close(0);
dup(newstdin);
close(newstdin);
args[i]=NULL;
}
}
execvp(cmd,args);
perror(cmd); //only happens when there is an error
endSh=1;
}
else{
if(!bg){ //if not a background process, wait for it
if (waitpid(childID,0,0)<0){//parent process
endSh=1;
perror("Error when waiting for child");
}
}
}
}
}
endSh=1;
}