blob: 9c409d7cfe203d276dc9aa2d801690f858af084b [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,
swissChiliabccdfc2021-01-08 21:39:12 -080014 *out = NULL;
swissChili97b5d8b2020-08-15 20:00:54 -070015
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
swissChiliabccdfc2021-01-08 21:39:12 -080035 if (!out)
36 {
37 fprintf(stderr, "-o flag is now mandatory\n");
38 return 1;
39 }
40
swissChili97b5d8b2020-08-15 20:00:54 -070041 fseek(in, 0, SEEK_END);
42 ssize_t len = ftell(in);
43 fseek(in, 0, SEEK_SET);
44
45 char *text = malloc(len + 1);
46 fread(text, len, 1, in);
47 text[len] = 0;
48
swissChiliabccdfc2021-01-08 21:39:12 -080049 uint32_t built = assemble(text, out);
swissChili97b5d8b2020-08-15 20:00:54 -070050
51 free(text);
swissChili97b5d8b2020-08-15 20:00:54 -070052}