-
Notifications
You must be signed in to change notification settings - Fork 1
/
script.c
288 lines (241 loc) · 6.2 KB
/
script.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
281
282
283
284
285
286
287
288
//Adam Rosenberg
//Objective here is to create a shell that accepts the whoosh language. Supports simotaneous process running, assigning output of programs to variables, and pipelining between n number of programs.
#include <stdlib.h>
#include <stdio.h>
#include "csapp.h"
#include "ast.h"
#include <signal.h>
#include "fail.h"
static void run_script(script *scr);
static void run_command(script_command *command);
static void set_var(script_var *var, int new_value);
static void run_single_command(script_group* group);
static void run_or(script_group* group);
static void lay_pipe(script_group* group);
/* You probably shouldn't change main at all. */
int main(int argc, char **argv) {
script *scr;
if ((argc != 1) && (argc != 2)) {
fprintf(stderr, "usage: %s [<script-file>]\n", argv[0]);
exit(1);
}
scr = parse_script_file((argc > 1) ? argv[1] : NULL);
run_script(scr);
return 0;
}
static void run_script(script *scr) {
//script_var* result = scr->groups->result_to;
int i = 0;
for (; i < scr->num_groups; i++) {
script_group currgroup = scr->groups[i];
switch (currgroup.mode) {
case GROUP_SINGLE: {
run_single_command(&currgroup);
break;
}
case GROUP_AND: {
int i = 0;
for (; i < currgroup.repeats; i++) {
lay_pipe(&currgroup);
}
break;
}
case GROUP_OR: {
int i = 0;
for(; i < currgroup.repeats; i++){
run_or(&currgroup);
}
break;
}
default:
break;
}
}
}
static void run_single_command(script_group* group) {
if (group->result_to != NULL) {
script_var * var = group->result_to;
int i = 0;
for (; i < group->num_commands; i++) {
script_command* command = group->commands;
pid_t pid = Fork();
if (pid == 0) {
Setpgid(0, 0);
run_command(command);
} else {
int status;
Waitpid(pid, &status, 0);
int waitstatusvalue = 0;
if (WIFEXITED(status)) {
waitstatusvalue = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
waitstatusvalue = WTERMSIG(status) * -1;
} else if (WIFSTOPPED(status)) {
waitstatusvalue = WSTOPSIG(status);
}
set_var(var, waitstatusvalue);
}
}
} else if (group->num_commands == 1) {
int i = 0;
for (; i < group->repeats; i++) {
pid_t pid = Fork();
if (pid == 0) {
run_command(&group->commands[0]);
} else {
int status;
Waitpid(pid, &status, 0);
}
}
}
}
static void lay_pipe(script_group* group) {
script_command * commands = group->commands;
if (group->result_to != NULL) {
script_var * var = group->result_to;
int i = 0;
for (; i < group->num_commands; i++) {
script_command* command = group->commands;
pid_t pid = Fork();
if (pid == 0) {
Setpgid(0, 0);
run_command(command);
} else {
int status;
Waitpid(pid, &status, 0);
int waitstatusvalue = 0;
if (WIFEXITED(status)) {
waitstatusvalue = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
waitstatusvalue = WTERMSIG(status) * -1;
} else if (WIFSTOPPED(status)) {
waitstatusvalue = WSTOPSIG(status);
}
set_var(var, waitstatusvalue);
}
}
} else {
int i, input, fds[2];
input = 0;
pid_t parent = Fork();
if (parent == 0) {
for (; i < group->num_commands - 1; i++) {
pid_t pid;
pipe(fds);
pid = Fork();
if (pid == 0) {
if (input != 0) {
Dup2(input, 0);
close(input);
}
if (fds[1] != 1) {
Dup2(fds[1], 1);
close(fds[1]);
}
run_command(&commands[i]);
} else {
close(fds[1]);
input = fds[0];
if (commands[i].pid_to != NULL) {
set_var(commands[i].pid_to, pid);
}
}
}
if (input != 0) {
Dup2(input, 0);
}
run_command(&commands[i]);
} else {
int status;
Waitpid(parent, &status, 0);
if (commands[group->num_commands - 1].pid_to != NULL) {
set_var(commands[group->num_commands - 1].pid_to, parent);
}
}
}
}
static void run_or(script_group* group) {
if (group->result_to != NULL) {
script_var * var = group->result_to;
int i = 0;
pid_t processes[group->num_commands];
script_command* commands = group ->commands;
for (; i < group->num_commands; i++) {
//script_command* command = group->commands;
pid_t pid = Fork();
if (pid == 0) {
//Setpgid(0, 0);
run_command(&commands[i]);
}else{
processes[i] = pid;
if (commands[i].pid_to != NULL) {
set_var(commands[i].pid_to, pid);
}
}
}
int status;
pid_t first = Wait(&status);
//Waitpid(0, &status, 0);
i = 0;
for(; i < group->num_commands; i++){
kill(processes[i], SIGKILL);
}
int waitstatusvalue = 0;
if (WIFEXITED(status)) {
waitstatusvalue = WEXITSTATUS(status);
} else if (WIFSIGNALED(status)) {
waitstatusvalue = WTERMSIG(status) * -1;
} else if (WIFSTOPPED(status)) {
waitstatusvalue = WSTOPSIG(status);
}
set_var(var, waitstatusvalue);
}else{
pid_t processes[group->num_commands];
int i = 0;
script_command* commands = group ->commands;
for (; i < group->num_commands; i++) {
pid_t pid = Fork();
if (pid == 0) {
//Setpgid(0, 0);
run_command(&commands[i]);
}else{
if (commands[i].pid_to != 0) {
set_var(commands[i].pid_to, pid);
}
}
}
int status;
Wait(&status);
i = 0;
for(; i < group->num_commands; i++){
kill(processes[i], SIGKILL);
}
}
}
/* This run_command function is a good start, but note that it runs
the command as a replacement for the `whoosh` script, instead of
creating a new process. */
static void run_command(script_command *command) {
const char **argv;
int i;
argv = malloc(sizeof(char *) * (command->num_arguments + 2));
argv[0] = command->program;
for (i = 0; i < command->num_arguments; i++) {
if (command->arguments[i].kind == ARGUMENT_LITERAL)
argv[i + 1] = command->arguments[i].u.literal;
else
argv[i + 1] = command->arguments[i].u.var->value;
}
argv[command->num_arguments + 1] = NULL;
Execve(argv[0], (char * const *) argv, environ);
free(argv);
}
/* You'll likely want to use this set_var function for converting a
numeric value to a string and installing it as a variable's
value: */
static void set_var(script_var *var, int new_value) {
char buffer[32];
free((void *) var->value);
snprintf(buffer, sizeof(buffer), "%d", new_value);
var->value = strdup(buffer);
}