-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathagssemble.c
102 lines (94 loc) · 2.62 KB
/
agssemble.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
#define _GNU_SOURCE
#define __STDC_WANT_LIB_EXT2__ 1
#include <stdio.h>
#include "Assembler.h"
#include "DataFile.h"
#include "preproc.h"
#include <stdlib.h>
#include <unistd.h>
#include "version.h"
#define ADS ":::AGSsemble " VERSION " by rofl0r:::"
static int usage(char *argv0) {
fprintf(stderr, ADS "\nusage:\n%s [-E] [-i file.i] [-I includedir] [-D preproc define] file.s [file.o]\n"
"pass an ags assembly filename.\n"
"-E: invoke built-in C preprocessor 'tinycpp' on the input file before assembling\n"
"-I includedir - add include dir for CPP\n"
"-D define - add define for CPP\n"
"-i file save preprocessor output to file\n"
"if optional second filename is ommited, will write into file.o\n", argv0);
return 1;
}
static FILE *freopen_r(FILE *f, char **buf, size_t *size) {
fflush(f);
fclose(f);
return fmemopen(*buf, *size, "r");
}
int main(int argc, char** argv) {
struct cpp* cpp = cpp_new();
char *tmp, *cppoutfn = 0;
int flags = 0, c;
while ((c = getopt(argc, argv, "Ei:I:D:")) != EOF) switch(c) {
case 'E': flags |= 1; break;
case 'i': cppoutfn = optarg; break;
case 'I': cpp_add_includedir(cpp, optarg); break;
case 'D':
if((tmp = strchr(optarg, '='))) *tmp = ' ';
cpp_add_define(cpp, optarg);
break;
default: return usage(argv[0]);
}
if(!argv[optind]) return usage(argv[0]);
char* file = argv[optind];
char out [256], *outn;
if(!argv[optind+1]) {
size_t l = strlen(file);
char *p;
snprintf(out, 256, "%s", file);
p = strrchr(out, '.');
if(!p) p = out + l;
*(p++) = '.';
*(p++) = 'o';
*p = 0;
outn = out;
} else outn = argv[optind+1];
if(!strcmp(outn, file)) {
fprintf(stderr, "error: input and output file (%s) identical!\n", file);
return 1;
}
FILE *in = fopen(file, "r");
if(!in) {
fprintf(stderr, "error opening file %s\n", file);
return 1;
}
if(flags & 1) {
struct FILE_container {
FILE *f;
char *buf;
size_t len;
} output = {0};
if(!cppoutfn) output.f = open_memstream(&output.buf, &output.len);
else output.f = fopen(cppoutfn, "w");
fprintf(stdout, "preprocessing %s ...", file);
int ret = cpp_run(cpp, in, output.f, file);
if(!ret) {
fprintf(stdout, "FAIL\n");
return 1;
}
fprintf(stdout, "OK\n");
fclose(in);
if(!cppoutfn) in = freopen_r(output.f, &output.buf, &output.len);
else {
fclose(output.f);
in = fopen(cppoutfn, "r");
}
}
// cpp_free(cpp); // FIXME this crashes on windows, find out why
AS a_b, *a = &a_b;
AS_open_stream(a, in);
fprintf(stdout, "assembling %s -> %s ... ", file, outn);
int ret = AS_assemble(a, outn);
AS_close(a);
if(!ret) fprintf(stdout, "FAIL\n");
else fprintf(stdout, "OK\n");
return !ret;
}