-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgppc.c
63 lines (52 loc) · 1.14 KB
/
gppc.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
// SPDX-License-Identifier: Beerware
/*
* 2020 by Marek Behun <[email protected]>
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <getopt.h>
#include "instr.h"
static void __attribute__((noreturn)) usage(FILE *fp, int ec)
{
fprintf(fp, "Usage: gppc -o <output> <input>\n\n");
exit(ec);
}
static FILE *xfopen(const char *path, const char *mode)
{
FILE *fp = fopen(path, mode);
if (!fp)
die("Cannot open file %s: %m", path);
return fp;
}
int main(int argc, char **argv)
{
const char *outf = NULL;
int res, i, opt;
FILE *fp;
u32 *out;
while ((opt = getopt(argc, argv, "ho:")) != -1) {
switch (opt) {
case 'h':
usage(stdout, EXIT_SUCCESS);
case 'o':
if (outf)
usage(stderr, EXIT_FAILURE);
outf = optarg;
break;
default:
usage(stderr, EXIT_FAILURE);
}
}
fp = optind < argc ? xfopen(argv[optind], "r") : stdin;
res = assemble(&out, fp, optind < argc ? argv[optind] : "<STDIN>");
fclose(fp);
for (i = 0; i < res; ++i)
out[i] = htole32(out[i]);
fp = outf ? xfopen(outf, "w") : stdout;
if (fwrite(out, res * sizeof(u32), 1, fp) < 1)
die("Cannot write");
fclose(fp);
free(out);
return 0;
}