-
Notifications
You must be signed in to change notification settings - Fork 0
/
executor.c.backup
167 lines (136 loc) · 4.47 KB
/
executor.c.backup
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
/* --- Secret UID --- */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "command.h"
#include "executor.h"
#ifndef _WIN32_WINNT
#include <sys/wait.h>
#include <sysexits.h>
#include <err.h>
#else
#define fork() 0
#define wait(a) 0
#define WEXITSTATUS(a) 0
#define EX_OSERR -1
#define err(a, b) 0
#define pipe(a) 0
#endif
#define CMD_CD "cd"
#define CMD_EXIT "exit"
#define FILE_PERMISSION (0664)
#define AMBI_INPUT_RDR "Ambiguous input redirect."
#define AMBI_OUTPUT_RDR "Ambiguous output redirect."
/* static void print_tree(struct tree *);todo delete */
static int execute_pipe(struct tree *t, const int *fdi, const int *fdo);
int execute(struct tree *t) {
#define first (t->argv[0])
#define second (t->argv[1])
if (t->conjunction == AND) {
if (!execute(t->left)) {
execute(t->right);
}
} else if (t->conjunction == PIPE) {
int pfd[2];
pipe(pfd);
if (!execute_pipe(t->left, 0, pfd + 1)) {
execute_pipe(t->right, pfd, 0);
}
} else if (t->conjunction == NONE) {
if (strcmp(first, CMD_EXIT) == 0) {
exit(EXIT_SUCCESS);
} else if (strcmp(first, CMD_CD) == 0) {
char *dir = second ? second : getenv("HOME");
if (chdir(dir) == -1) {
perror(dir);
}
} else {
return execute_pipe(t, 0, 0);
}
}
return EXIT_SUCCESS;
}
static int execute_pipe(struct tree *t, const int *fdi, const int *fdo) {
if (fork()) { /* parent */
int status;
wait(&status);
return WEXITSTATUS(status);
} else { /* child */
int fd, ambi_rdr = 0;
if (t->input && fdi) {
ambi_rdr = 1;
} else if (t->input) {
if ((fd = open(t->input, O_RDONLY)) < 0) {
err(EX_OSERR, "File opening (read) failed");
}
if (dup2(fd, STDIN_FILENO) < 0) {
err(EX_OSERR, "dup2 (read) failed");
}
close(fd);
} else if (fdi) {
if (dup2(*fdi, STDIN_FILENO) < 0) {
err(EX_OSERR, "dup2 (read) failed");
}
}
if (t->output && fdo) {
ambi_rdr = 2;
} else if (t->output) {
if ((fd = open(t->output,
O_WRONLY | O_CREAT | O_TRUNC,
FILE_PERMISSION)) < 0) {
err(EX_OSERR, "File opening (read) failed");
}
if (dup2(fd, STDOUT_FILENO) < 0) {
err(EX_OSERR, "dup2 (read) failed");
}
close(fd);
} else if (fdo) {
if (dup2(*fdo, STDOUT_FILENO) < 0) {
err(EX_OSERR, "dup2 (read) failed");
}
}
if (ambi_rdr) {
printf(ambi_rdr == 2 ? AMBI_OUTPUT_RDR : AMBI_INPUT_RDR);
exit(EXIT_FAILURE);
}
if (execvp(first, t->argv) == -1) {
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
#undef first
#undef second
}
/*
clear
make
(cd testing && ../d8sh < public00.in >! r0 && diff r0 public00.output)
(cd testing && ../d8sh < public01.in >! r1 && diff r1 public01.output)
(cd testing && ../d8sh < public02.in >! r2 && diff r2 public02.output)
(cd testing && ../d8sh < public03.in >! r3 && diff r3 public03.output)
(cd testing && ../d8sh < public04.in >! r4 && diff r4 public04.output)
(cd testing && ../d8sh < public05.in >! r5 && diff r5 public05.output)
(cd testing && ../d8sh < public06.in >! r6 && diff r6 public06.output)
(cd testing && ../d8sh < public07.in >! r7 && diff r7 public07.output)
(cd testing && ../d8sh < public08.in >! r8 && diff r8 public08.output)
(cd testing && ../d8sh < public09.in >! r9 && diff r9 public09.output)
(cd testing && ../d8sh < public10.in >! r10 && diff r10 public10.output)
(cd testing && ../d8sh < public11.in >! r11 && diff r11 public11.output)
(cd testing && ../d8sh < public12.in >! r12 && diff r12 public12.output)
(cd testing && ../d8sh < public13.in >! r13 && diff r13 public13.output)
todo delete
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);
}
}*/