swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 1 | #include "as.h" |
swissChili | c382994 | 2020-09-06 19:36:04 -0700 | [diff] [blame] | 2 | #include "map.h" |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 3 | |
| 4 | #include <stdio.h> |
| 5 | #include <stdlib.h> |
| 6 | #include <bits/getopt_core.h> |
| 7 | #include <unistd.h> |
| 8 | |
swissChili | c382994 | 2020-09-06 19:36:04 -0700 | [diff] [blame] | 9 | // TODO: handle all the possible IO errors |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 10 | int main(int argc, char **argv) |
| 11 | { |
| 12 | char c; |
| 13 | FILE *in = stdin, |
swissChili | abccdfc | 2021-01-08 21:39:12 -0800 | [diff] [blame] | 14 | *out = NULL; |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 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 | |
swissChili | abccdfc | 2021-01-08 21:39:12 -0800 | [diff] [blame] | 35 | if (!out) |
| 36 | { |
| 37 | fprintf(stderr, "-o flag is now mandatory\n"); |
| 38 | return 1; |
| 39 | } |
| 40 | |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 41 | 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 | |
swissChili | abccdfc | 2021-01-08 21:39:12 -0800 | [diff] [blame] | 49 | uint32_t built = assemble(text, out); |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 50 | |
| 51 | free(text); |
swissChili | 97b5d8b | 2020-08-15 20:00:54 -0700 | [diff] [blame] | 52 | } |