-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc.c
385 lines (291 loc) · 8.3 KB
/
cc.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/* cc.c - compiler driver ncc, the new c compiler
Copyright (c) 2021 Charles E. Youse (charles@gnuless.org). All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
/* override the default root of the compiler installation
by defining ROOT, or override the paths individually. */
#ifndef ROOT
#define ROOT "/usr"
#endif
#ifndef CPP
#define CPP ROOT "/bin/cpp"
#endif
#ifndef CC1
#define CC1 ROOT "/lib/cc1"
#endif
#ifndef AS
#define AS ROOT "/bin/as"
#endif
#ifndef LD
#define LD ROOT "/bin/ld"
#endif
#ifndef CRT0
#define CRT0 ROOT "/lib/crt0.o"
#endif
#ifndef LIBC
#define LIBC ROOT "/lib/libc.a"
#endif
#ifndef INC
#define INC ROOT "/include"
#endif
/* struct list is an expandable container used to build argument
vectors for exec(). we grow the vectors in LIST_INC increments.
lists do not own the strings they point to, only the pointers. */
#define LIST_INC 10
struct list
{
int capacity;
int len;
char **s;
};
struct list cpp; /* templates */
struct list cc1;
struct list as;
struct list ld;
struct list args; /* arguments built for current command */
struct list temps; /* list of temp files (delete before exit) */
#define EXEC_FILE 0 /* file types */
#define C_FILE 'c'
#define CC1_FILE 'i'
#define AS_FILE 's'
#define LD_FILE 'o'
#define LIB_FILE 'a'
int goal = EXEC_FILE; /* file type for result(s) */
char *a_out; /* target executable name */
int n_flag; /* no standard include files */
/* call before exit to axe all the temp files */
void remove_temps(void)
{
int i;
for (i = 0; i < temps.len; ++i)
unlink(temps.s[i]);
}
/* print an error message, clean up and abort */
void error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "cc: ");
vfprintf(stderr, fmt, args);
va_end(args);
fputc('\n', stderr);
remove_temps();
exit(1);
}
/* allocate memory or bomb */
void *mem(size_t len)
{
void *p = malloc(len);
if (p == 0)
error("out of memory");
return p;
}
/* add strings to the end of list, growing
it as necessary. terminate with 0. */
void add(struct list *list, ...)
{
va_list ss;
char **new;
char *s;
va_start(ss, list);
while (s = va_arg(ss, char *)) {
if (list->len == list->capacity) {
new = mem(sizeof(char *) * (list->capacity + LIST_INC + 1));
if (list->s) {
memcpy(new, list->s, sizeof(char *) * (list->capacity));
free(list->s);
}
list->s = new;
list->capacity += LIST_INC;
}
list->s[list->len++] = s;
list->s[list->len] = 0;
}
}
/* truncate dst and make it a copy of src.
this could be more efficient, but why? */
void copy(struct list *dst, struct list *src)
{
int i;
dst->len = 0;
if (dst->capacity)
dst->s[0] = 0;
for (i = 0; i < src->len; ++i)
add(dst, src->s[i], 0);
}
/* return the type of file based on its extension */
int type(char *path)
{
char *dot;
dot = strrchr(path, '.');
if (dot) {
++dot;
switch (*dot)
{
case C_FILE:
case CC1_FILE:
case AS_FILE:
case LD_FILE:
case LIB_FILE:
return *dot;
}
}
error("'%s': unknown file type", path);
}
/* return a copy of the name in question with its
extension changed to the file type specified.
notice that we make no attempt to track these
allocations, we are dirty, dirty, lazy leakers. */
char *morph(char *path, int type)
{
char *dot;
char *new;
new = mem(strlen(path) + 1);
strcpy(new, path);
dot = strrchr(new, '.');
*++dot = type;
return new;
}
/* run the command indicated by args, which
will output to out_path. abort on error. */
void run(struct list *args, char *out_path)
{
pid_t pid;
int status;
if ((pid = fork()) == 0) {
execvp(args->s[0], args->s);
error("can't exec '%s': %s", args->s[0], strerror(errno));
}
if (pid == -1)
error("can't fork: %s", strerror(errno));
while (pid != wait(&status))
;
if (status != 0) {
add(&temps, out_path, 0);
error("compilation terminated abnormally");
}
}
int main(int argc, char **argv)
{
char *src;
char *new;
add(&cpp, CPP, 0);
add(&cc1, CC1, 0);
add(&as, AS, "-o", 0);
add(&ld, LD, "-e", "cstart", "-o", 0);
++argv;
while (*argv && (*argv[0] == '-')) {
switch ((*argv)[1]) {
case 'D':
case 'I':
add(&cpp, *argv, 0);
break;
case 'd':
add(&cc1, *argv, 0);
break;
case 'n':
++n_flag;
break;
case 'S':
case 'P':
case 'c':
if ((*argv)[2])
error("malformed goal option");
if (goal != EXEC_FILE)
error("conflicting goal options");
switch ((*argv)[1])
{
case 'S': goal = AS_FILE; break;
case 'P': goal = CC1_FILE; break;
case 'c': goal = LD_FILE; break;
}
break;
case 'o':
if ((*argv)[2] || (argv[1] == 0))
error("malformed output option (-o)");
if (a_out)
error("duplicate output specified (-o)");
++argv;
a_out = *argv;
break;
default:
error("unrecognized option: -%c\n", (*argv)[1]);
}
++argv;
}
if (a_out == 0)
a_out = "a.out";
if (n_flag == 0)
add(&cpp, "-I" INC, 0);
add(&ld, a_out, CRT0, 0);
if (*argv == 0)
error("no input files");
while (*argv) {
src = *argv;
switch (type(src)) {
case C_FILE:
new = morph(src, CC1_FILE);
copy(&args, &cpp);
add(&args, src, 0);
add(&args, new, 0);
run(&args, new);
if (goal == CC1_FILE)
break;
add(&temps, new, 0);
src = new;
case CC1_FILE:
new = morph(src, AS_FILE);
copy(&args, &cc1);
add(&args, src, 0);
add(&args, new, 0);
run(&args, new);
if (goal == AS_FILE)
break;
src = new;
add(&temps, new, 0);
case AS_FILE:
new = morph(src, LD_FILE);
copy(&args, &as);
add(&args, new, 0);
add(&args, src, 0);
run(&args, new);
if (goal == LD_FILE)
break;
add(&temps, new, 0);
src = new;
case LD_FILE:
add(&ld, src, 0);
}
argv++;
}
if (goal == EXEC_FILE) {
add(&ld, LIBC, 0);
run(&ld, a_out);
}
remove_temps();
return 0;
}
/* vi: set ts=4 expandtab: */