-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.c
executable file
·281 lines (253 loc) · 5.67 KB
/
executor.c
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
/*Youming Zhang
UID: 114537867
CMSC 0301
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sysexits.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <errno.h>
#include "command.h"
#include "executor.h"
static void print_tree(struct tree *t);
int execute(struct tree *t) {
pid_t pid;
int status,fd_in=-1,fd_out=-1;
/*check the tree*/
if(t==NULL){
return 0;
}
/********NONE conjunction**********/
if(t->conjunction==NONE){
/*exit command*/
if(strncmp(t->argv[0],"exit",4)==0){
exit(EXIT_SUCCESS);
}
/*cd command*/
else if(strcmp(t->argv[0],"cd")==0){
if(t->argv[1]==NULL){/*go home */
chdir(getenv("HOME"));
}else{
if((chdir(t->argv[1]))<0){
perror("No such directory");
exit(EX_OSERR);
}
}
}else{
if((pid=fork())<0){
perror("fork error");
exit(EX_OSERR);
}
if(pid){/*parent code*/
wait(&status);
if (WEXITSTATUS(status)) {
fflush(stdout);
return -1;
}
return status;
}else{
/*********check input**********/
if(t->input!=NULL){
/*open fd*/
if((fd_in=open(t->input,O_RDONLY))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_in,STDIN_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end input*/
close(fd_in);
/********check ouput**********/
if(t->output!=NULL){
/*open fd*/
if((fd_out=open(t->output,O_WRONLY|O_CREAT|O_TRUNC,0664))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_out,STDOUT_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end output*/
close(fd_out);
/*system("ps -l");*/
if(execvp(t->argv[0],t->argv) < 0){
fprintf(stderr,"Fail to execute %s \n",t->argv[0]);
fflush(stdout);
exit(EXIT_FAILURE);
} /*end child*/
exit(EXIT_SUCCESS);
}
}
}/*end NONE*/
/**************AND************/
if(t->conjunction==AND){
/*********check input**********/
if(t->input!=NULL){
/*open fd*/
if((fd_in=open(t->input,O_RDONLY))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_in,STDIN_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end input*/
close(fd_in);
/********check ouput**********/
if(t->output!=NULL){
/*open fd*/
if((fd_out=open(t->output,O_WRONLY|O_CREAT|O_TRUNC,0664))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_out,STDOUT_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end output*/
close(fd_out);
if(execute(t->left)==0){
execute(t->right);
}
return 0;
}/*end AND conjunction*/
/************OR***************/
if(t->conjunction==OR){
}
/***********SEMI*************/
if(t->conjunction==SEMI){
}
/***********PIPE************/
if(t->conjunction==PIPE){
int fd[2];
pid_t tmp_pid;
/*********check input**********/
if(t->input!=NULL){
/*open fd*/
if((fd_in=open(t->input,O_RDONLY))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_in,STDIN_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end input*/
close(fd_in);
/********check ouput**********/
if(t->output!=NULL){
/*open fd*/
if((fd_out=open(t->output,O_WRONLY|O_CREAT|O_TRUNC,0664))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_out,STDOUT_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end output*/
close(fd_out);
if(t->left->output!=NULL){
printf("Ambiguous output redirect.\n");
return EXIT_FAILURE;
}
if(t->right->input!=NULL){
printf("Ambiguous intput redirect.\n");
return EXIT_FAILURE;
}
/*piping*/
if(pipe(fd)<0){
perror("pipe error");
}
/*fork*/
if((tmp_pid=fork())<0){
perror("fork error");
}
if(tmp_pid){/*parent code*/
wait(&tmp_pid);
close(fd[1]); /*close write*/
dup2(fd[0],STDIN_FILENO);
execute(t->right);
close(fd[0]);
}else{
close(fd[0]);
dup2(fd[1],STDOUT_FILENO);
execute(t->left);
close(fd[1]);
}
return 0;
}/*end piping*/
/******************SUBSHELL**********************/
if(t->conjunction==SUBSHELL){
int pid2,status2;
/*********check input**********/
if(t->input!=NULL){
/*open fd*/
if((fd_in=open(t->input,O_RDONLY))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_in,STDIN_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end input*/
close(fd_in);
/********check ouput**********/
if(t->output!=NULL){
/*open fd*/
if((fd_out=open(t->output,O_WRONLY|O_CREAT|O_TRUNC,0664))<0){
perror("read fail");
exit(EXIT_FAILURE);
}
/*dup2*/
if((dup2(fd_out,STDOUT_FILENO))<0){
perror("dup2 fail");
exit(EXIT_FAILURE);
}
}/*end output*/
close(fd_out);
if((pid2=fork())<0){
perror("fork error");
exit(EX_OSERR);
}
if(pid2){
wait(&pid2);
}else{
execute(t->left);
exit(EXIT_SUCCESS);
}
return 0;
}/*end subshell*/
return 0;
}
static void print_tree(struct tree *t) {
if (t != NULL) {
print_tree(t->left);
if (t->conjunction == NONE) {
printf("NONE: %s, ", t->argv[0]);
} else {
printf("%s, ", conj[t->conjunction]);
}
printf("IR: %s, ", t->input);
printf("OR: %s\n", t->output);
print_tree(t->right);
}
}