blob: 8c2b577f460c3d07c16548fc7dc9a6ce3c579b7a [file] [log] [blame]
swissChili97b5d8b2020-08-15 20:00:54 -07001#include "as.h"
swissChilic3829942020-09-06 19:36:04 -07002#include "map.h"
swissChili97b5d8b2020-08-15 20:00:54 -07003
4#include <stdio.h>
5#include <stdlib.h>
6#include <bits/getopt_core.h>
7#include <unistd.h>
8
swissChilic3829942020-09-06 19:36:04 -07009// TODO: handle all the possible IO errors
swissChili97b5d8b2020-08-15 20:00:54 -070010int main(int argc, char **argv)
11{
12 char c;
13 FILE *in = stdin,
14 *out = stdout;
15
16 while ((c = getopt(argc, argv, "i:o:")) != -1)
17 {
18 switch (c)
19 {
20 case 'i':
21 in = fopen(optarg, "r");
22 break;
23 case 'o':
24 out = fopen(optarg, "w");
25 break;
26 case 'h':
27 case '?':
28 printf("6502 assembler\n"
29 "Usage:\n"
30 " -i <input> set input file (default stdin)\n"
31 " -o <output> set output file (default stdout)\n");
32 }
33 }
34
35 fseek(in, 0, SEEK_END);
36 ssize_t len = ftell(in);
37 fseek(in, 0, SEEK_SET);
38
39 char *text = malloc(len + 1);
40 fread(text, len, 1, in);
41 text[len] = 0;
42
swissChilic3829942020-09-06 19:36:04 -070043 FILE *temp = tmpfile();
44
45 map_t *macros = new_map();
46 uint32_t processed = preproc(text, temp, macros, 0);
47 free_map_items(macros);
48
49 fseek(temp, 0, SEEK_END);
50 ssize_t temp_len = ftell(in);
51 fseek(temp, 0, SEEK_SET);
52
53 char *processed_text = malloc(len + 1);
54 fread(processed_text, len, 1, in);
55 text[temp_len] = 0;
56
57 uint32_t built = assemble(processed_text, out);
swissChili97b5d8b2020-08-15 20:00:54 -070058
59 free(text);
swissChilic3829942020-09-06 19:36:04 -070060 free(processed_text);
swissChili97b5d8b2020-08-15 20:00:54 -070061}