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];
57 FILE *in
= fopen(file
, "r");
59 dprintf(2, "error opening file %s\n", file
);
64 struct FILE_container
{
69 if(!cppoutfn
) output
.f
= open_memstream(&output
.buf
, &output
.len
);
70 else output
.f
= fopen(cppoutfn
, "w");
71 dprintf(1, "preprocessing %s ...", file
);
72 int ret
= cpp_run(cpp
, in
, output
.f
, file
);
79 if(!cppoutfn
) in
= freopen_r(output
.f
, &output
.buf
, &output
.len
);
82 in
= fopen(cppoutfn
, "r");
88 AS_open_stream(a
, in
);
90 dprintf(1, "assembling %s -> %s ... ", file
, outn
);
91 int ret
= AS_assemble(a
, outn
);
94 if(!ret
) dprintf(1, "FAIL\n");
95 else dprintf(1, "OK\n");