9 #define ADS ":::AGSsemble " VERSION " by rofl0r:::"
11 static int usage(char *argv0
) {
12 dprintf(2, ADS
"\nusage:\n%s [-E] [-i file.i] [-I includedir] [-D preproc define] file.s [file.o]\n"
13 "pass an ags assembly filename.\n"
14 "-E: invoke built-in C preprocessor 'tinycpp' on the input file before assembling\n"
15 "-I includedir - add include dir for CPP\n"
16 "-D define - add define for CPP\n"
17 "-i file save preprocessor output to file\n"
18 "if optional second filename is ommited, will write into file.o\n", argv0
);
22 static FILE *freopen_r(FILE *f
, char **buf
, size_t *size
) {
25 return fmemopen(*buf
, *size
, "r");
28 int main(int argc
, char** argv
) {
29 struct cpp
* cpp
= cpp_new();
30 char *tmp
, *cppoutfn
= 0;
32 while ((c
= getopt(argc
, argv
, "Ei:I:D:")) != EOF
) switch(c
) {
33 case 'E': flags
|= 1; break;
34 case 'i': cppoutfn
= optarg
; break;
35 case 'I': cpp_add_includedir(cpp
, optarg
); break;
37 if((tmp
= strchr(optarg
, '='))) *tmp
= ' ';
38 cpp_add_define(cpp
, optarg
);
40 default: return usage(argv
[0]);
42 if(!argv
[optind
]) return usage(argv
[0]);
43 char* file
= argv
[optind
];
44 char out
[256], *outn
;
46 size_t l
= strlen(file
);
48 snprintf(out
, 256, "%s", file
);
49 p
= strrchr(out
, '.');
55 } else outn
= argv
[optind
+1];
56 if(!strcmp(outn
, file
)) {
57 dprintf(2, "error: input and output file (%s) identical!\n", file
);
61 FILE *in
= fopen(file
, "r");
63 dprintf(2, "error opening file %s\n", file
);
68 struct FILE_container
{
73 if(!cppoutfn
) output
.f
= open_memstream(&output
.buf
, &output
.len
);
74 else output
.f
= fopen(cppoutfn
, "w");
75 dprintf(1, "preprocessing %s ...", file
);
76 int ret
= cpp_run(cpp
, in
, output
.f
, file
);
83 if(!cppoutfn
) in
= freopen_r(output
.f
, &output
.buf
, &output
.len
);
86 in
= fopen(cppoutfn
, "r");
92 AS_open_stream(a
, in
);
94 dprintf(1, "assembling %s -> %s ... ", file
, outn
);
95 int ret
= AS_assemble(a
, outn
);
98 if(!ret
) dprintf(1, "FAIL\n");
99 else dprintf(1, "OK\n");