-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcompiler.c
235 lines (201 loc) · 5.28 KB
/
compiler.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
#include <unistd.h>
#include <errno.h>
#include <dirent.h>
#include <fnmatch.h>
#include <stddef.h>
#define __USE_XOPEN_EXTENDED
#include <ftw.h>
#include "compiler.h"
#ifdef __USE_GNU
#define var_environ environ
#else
#define var_environ __environ
#endif
unsigned long progress_max_g;
unsigned long progress_g;
int run_child(const char* bin, const char* cwd, PyObject* args, int* pfd, int child_read_fd) {
/*
* Check cwd points to the valid directory
*/
DIR* dir = opendir(cwd);
if (dir == NULL)
return -1;
closedir(dir);
/*
* So basically, this whole block just checks whether bin exists?
* is it neccessary? I mean, we are only running clang here anyway...
*/
// char* current_wd = getcwd(0, 0);
// if (chdir(cwd)<0) {
// free(current_wd);
// return -1;
// }
// int ra = access(bin, F_OK | X_OK);
// if (ra) {
// free(current_wd);
// return -1;
// }
// chdir(current_wd);
// free(current_wd);
/*
* Do some magic pipes trickery
*/
int fd[2];
int fdr[2];
int r = pipe(fd);
if (r<0) return r;
if (child_read_fd) {
int r = pipe(fdr);
if (r<0) return r;
}
pid_t pid = fork();
if (pid > 0) {
/* We're at the parent */
close(fd[1]);
if (child_read_fd) {
close(fdr[0]);
close(fdr[1]);
}
}
else if (pid == 0) {
/* Hello child! */
char** argv = malloc((PyList_Size(args)+1)*sizeof(const char*));
Py_ssize_t u;
for (u=0; u<PyList_Size(args); ++u) {
argv[u] = (char*)PyString_get_c_str(PyList_GetItem(args,u));
}
argv[0] = (char*)bin;
argv[u] = 0;
/*
* What is with this dup2 in loops?
*/
while ((dup2(fd[1], STDOUT_FILENO) == -1) && (errno == EINTR)) {}
while ((dup2(fd[1], STDERR_FILENO) == -1) && (errno == EINTR)) {}
close(fd[1]);
close(fd[0]);
if (child_read_fd) {
while ((dup2(fdr[0], STDIN_FILENO) == -1) && (errno == EINTR)) {}
close(fdr[0]);
close(fdr[1]);
}
if (chdir(cwd) < 0)
return -1;
execve(bin,argv,var_environ);
exit(errno);
}
else {
/* Fork failed */
return -1;
}
*pfd = fd[0];
return pid;
}
PyObject* splitn(const char* str, size_t size) {
while(size && isspace(*str)) {
str++;
size--;
}
while(size && isspace(str[size-1])) {
--size;
}
PyObject* lst = PyList_New(0);
char* newl = strnstr(str,"\n",size);
while(newl) {
ptrdiff_t part_size = newl-str;
PyList_Append(lst,PyUnicode_FromStringAndSize(str,part_size));
str+=part_size+1;
size-=part_size+1;
newl = strnstr(str,"\n",size);
}
if (size) {
PyList_Append(lst,PyUnicode_FromStringAndSize(str,size));
}
return lst;
}
PyObject* check_file_access(PyObject* fns, const char* cwd) {
size_t cwdLen = strlen(cwd);
const char* fn = PyString_get_c_str(fns);
PyObject* rfn;
if (fn[0]=='/') {
if (access(fn,F_OK)==-1) {
return 0;
}
rfn = Py_BuildValue("s",fn);
}
else {
char *fullpath = malloc(cwdLen + strlen(fn) + 2);
sprintf(fullpath, "%s/%s", cwd, fn);
int r = access(fullpath,F_OK);
if (r==-1) {
free(fullpath);
return 0;
}
rfn = Py_BuildValue("s",fullpath);
free(fullpath);
}
return rfn;
}
int strtab_contains(const char** tab,size_t num,char* s) {
size_t i;
for (i=0; i<num; ++i) {
if (!strcmp(tab[i],s)) return 1;
}
return 0;
}
static inline int startsWith(const char *pre, const char *str) {
size_t lenpre = strlen(pre),
lenstr = strlen(str);
return lenstr < lenpre ? 0 : strncmp(pre, str, lenpre) == 0;
}
/*
* Returns index of first item in the sequence that starts with the given prefix (or -1 if not found)
* If the item matches the prefix perfectly, match is set to 1 (otherwise it is set to 0)
*/
Py_ssize_t PySequence_Contains_Prefix(PyObject* seq,PyObject* prefix,int* match) {
Py_ssize_t n;
PyObject *it; /* iter(seq) */
if (seq == NULL || prefix == NULL) {
return -1;
}
it = PyObject_GetIter(seq);
if (it == NULL) {
return -1;
}
n = 0;
for (;;) {
int cmp;
PyObject *item = PyIter_Next(it);
if (item == NULL) {
if (PyErr_Occurred()) {
goto Fail;
}
break;
}
cmp = startsWith(PyString_get_c_str(prefix),PyString_get_c_str(item));
if (cmp) {
if (!strcmp(PyString_get_c_str(prefix),PyString_get_c_str(item))) {
*match = 1;
}
else {
*match = 0;
}
goto Done;
}
Py_DECREF(item);
n++;
}
Fail:
n = -1;
Done:
Py_DECREF(it);
return n;
}
static int unlink_cb(const char *fpath, const struct stat *sb, int typeflag, struct FTW *ftwbuf) {
int rv = remove(fpath);
if (rv)
perror(fpath);
return rv;
}
int rmrf(char *path) {
return nftw(path, unlink_cb, 64, FTW_DEPTH | FTW_PHYS);
}