-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexec.c
More file actions
178 lines (159 loc) · 3.63 KB
/
exec.c
File metadata and controls
178 lines (159 loc) · 3.63 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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <string.h>
#include <wait.h>
#include <fcntl.h>
#include <signal.h>
#include <dirent.h>
#include "tokens.h"
#include "lexer.h"
#include "ast.h"
#include "parse.h"
int exec_shell(struct ShellNode *node);
int execute_buldin(struct CommandNode* node){
if (!strcmp(node->name, "cd")){
if (node-> argv[1]==NULL || node->argv[2]){
printf("АШИПКА!!\n");
return 1;
}
else {chdir(node->argv[1]); return 0;}
}
}
int exec_command(struct CommandNode* node){
int input=0, output=1;
if (node->file_in){
input=open(node->file_in, O_RDONLY, 0400);
if (input<0) {
perror("open");
//ошибка
exit(1);
}
dup2(input, 0);
close(input);
}
if (node->file_out){
if (node->append){
output=open(node->file_out, O_WRONLY| O_APPEND| O_CREAT, 0666);
if (output<0) {
perror("open");
//ошибка
exit(1);
}
}
else{
output=open(node->file_out, O_WRONLY| O_TRUNC | O_CREAT, 0666);
if (output<0) {
perror("open");
//ошибка
exit(1);
}
}
dup2(output, 1);
close(output);
}
execvp(node->name, node->argv);
perror("execvp");
exit(1);
}
bool is_builtin(struct CommandNode* node){
return !strcmp(node->argv[0], "cd");
}
int num_pipeline_nodes(struct PipelineNode * node){
if (!node) return 0;
return 1+num_pipeline_nodes(node->pipeline);
}
int exec_pipeline(struct PipelineNode * node){
int num=num_pipeline_nodes(node);
if (num==1 && node->type==COMMAND && is_builtin(node->command)){
return execute_buldin(node->command);
}
int (*pipe_arr)[2];
pipe_arr=malloc(sizeof(*pipe_arr)*(num-1));
for (int i=0; i<num-1; i++){
pipe(pipe_arr[i]);
}
pid_t pd;
int i;
for (i=0; i<num; i++, node=node->pipeline){
if ((pd=fork())==0){
signal(SIGINT, SIG_DFL);
break;
}
}
if (!pd){
if (i){
dup2(pipe_arr[i-1][0], 0);
}
if (i!=(num-1)){
dup2(pipe_arr[i][1], 1);
}
for (int k=0; k<(num-1); k++){
close(pipe_arr[k][1]);
close(pipe_arr[k][0]);
}
if (node->type==COMMAND){
exit(exec_command(node->command));
}
else{
exit(exec_shell(node->shell));
}
}
else {
for (int k=0; k<(num-1); k++){
close(pipe_arr[k][1]);
close(pipe_arr[k][0]);
}
int st;
waitpid(pd, &st, 0);
if (WIFEXITED(st) && WEXITSTATUS(st)){
//exec command завершилась по exit(1)
return st;
}
else return 0;
}
}
int exec_conditional(struct ConditionalNode* node){
int result;
result=exec_pipeline(node->pipeline);
if (node->conditional){
if ((node->if_false && result!=0) || (!(node->if_false) && result==0)){
result=exec_conditional(node->conditional);
return result;
}
else{
// не нужно выполнять conditional
return result;
}
}
}
int exec_shell(struct ShellNode *node){
int result;
if (node->background){
pid_t pd;
if ((pd=fork())==0){
if ((pd=fork())==0){
setpgid(0,0);
signal(SIGINT, SIG_DFL);
int blackhole=open("/dev/null", O_RDONLY);
dup2(blackhole, 0);
close(blackhole);
result=exec_conditional(node->conditional);
}
exit(0);
}
else {
int st;
waitpid(pd, &st, 0);
}
}
else {
result=exec_conditional(node->conditional);
if (node->shell){
result=exec_shell(node->shell);
}
}
return result;
}