7 #define LDOPTS "lLsgmpe" /* neatld options */
8 #define CCOPTS "cIEDW" /* neatcc options */
9 #define AROPTS "IDOlLome" /* options with an argument */
11 static void die(char *msg
)
13 write(2, msg
, strlen(msg
));
17 int main(int argc
, char *argv
[], char *envp
[])
19 char *ccargs
[MAXARGS
]; /* neatcc options */
20 char *ldargs
[MAXARGS
]; /* neatld options */
21 int opt
[MAXARGS
]; /* opt[i] is one if argv[i] is an option */
22 int optarg
[MAXARGS
]; /* argv[i + 1] is an argument of argv[i] */
23 int ccargc
= 0; /* number of neatcc options */
24 int ldargc
= 0; /* number of neatld options */
25 int nold
= 0; /* compile only */
28 die("neatcc: ncc/nld wrapper\n");
29 /* looking for options that prevent linking + initialize opt[] and optarg[] */
30 for (i
= 1; i
< argc
; i
++) {
31 opt
[i
] = argv
[i
][0] == '-' ? argv
[i
][1] : 0;
32 optarg
[i
] = opt
[i
] > 0 && strchr(AROPTS
, opt
[i
]) && !argv
[i
][2];
33 nold
= nold
|| opt
[i
] == 'c' || opt
[i
] == 'E';
35 /* initialize compiler options */
36 ccargs
[ccargc
++] = NCC
;
37 ccargs
[ccargc
++] = "-Dfloat=long";
38 ccargs
[ccargc
++] = "-Ddouble=long";
39 ccargs
[ccargc
++] = "-D__extension__=";
40 ccargs
[ccargc
++] = "-I" NLC
;
41 for (i
= 1; i
< argc
; i
+= 1 + optarg
[i
]) {
42 if (opt
[i
] && strchr(CCOPTS
, opt
[i
]) || (nold
&& opt
[i
] == 'o')) {
43 ccargs
[ccargc
++] = argv
[i
];
45 ccargs
[ccargc
++] = argv
[i
+ 1];
48 /* invoke neatcc for every .c file */
49 for (i
= 1; i
< argc
; i
+= 1 + optarg
[i
]) {
51 char *end
= strchr(arg
, '\0');
52 if (!opt
[i
] && arg
+ 2 < end
&& end
[-2] == '.' && end
[-1] == 'c') {
55 ccargs
[ccargc
+ 1] = NULL
;
57 execve(ccargs
[0], ccargs
, envp
);
58 die("neatcc: could not find ncc\n");
61 if (wait(&st
) < 0 || WEXITSTATUS(st
))
63 end
[-1] = 'o'; /* for linker */
68 ldargs
[ldargc
++] = NLD
;
69 for (i
= 1; i
< argc
; i
+= 1 + optarg
[i
]) {
70 if (!opt
[i
] || !strchr(CCOPTS
, opt
[i
])) {
71 ldargs
[ldargc
++] = argv
[i
];
73 ldargs
[ldargc
++] = argv
[i
+ 1];
76 ldargs
[ldargc
++] = NLC
"/start.o";
77 ldargs
[ldargc
++] = NLC
"/libc.a";
78 ldargs
[ldargc
] = NULL
;
79 execve(ldargs
[0], ldargs
, envp
);
80 die("neatcc: could not find nld\n");