agssim: allow interactive mode with file
[rofl0r-agsutils.git] / agsdisas.c
blob7d96edc8cb016ccc0677df870d62fd8e56f76a00
1 #define _GNU_SOURCE
2 #include "DataFile.h"
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include "version.h"
7 #define ADS ":::AGSdisas " VERSION " by rofl0r:::"
9 static int usage(char *argv0) {
10 dprintf(2, ADS "\nusage:\n%s [-oblf] file.o [file.s]\n"
11 "pass input and optionally output filename.\n"
12 "options:\n"
13 "-o : dump offset comments in disassembly\n"
14 "-b : dump hexadecimal bytecode comments in disassembly\n"
15 "-f : dump informative original fixups section\n"
16 "-l : remove linenumber debug assembly directives [produces smaller files]\n"
17 , argv0);
18 exit(1);
21 static void disas(char *o, char *s, int flags) {
22 //ARF_find_code_start
23 AF f_b, *f = &f_b;
24 ASI sc;
25 if(AF_open(f, o)) {
26 ASI *i = ASI_read_script(f, &sc) ? &sc : 0;
27 dprintf(1, "disassembling %s -> %s", o, s);
28 if(!i || !ASI_disassemble(f, i, s, flags)) dprintf(1, " FAIL");
29 dprintf(1, "\n");
30 AF_close(f);
34 int main(int argc, char**argv) {
35 int flags = 0, c;
36 while ((c = getopt(argc, argv, "oblf")) != EOF) switch(c) {
37 case 'o': flags |= DISAS_DEBUG_OFFSETS; break;
38 case 'b': flags |= DISAS_DEBUG_BYTECODE; break;
39 case 'l': flags |= DISAS_SKIP_LINENO; break;
40 case 'f': flags |= DISAS_DEBUG_FIXUPS; break;
41 default: return usage(argv[0]);
43 if(!argv[optind]) return usage(argv[0]);
44 char *o = argv[optind], *s, out[256];
45 if(!argv[optind+1]) {
46 size_t l = strlen(o);
47 snprintf(out, 256, "%s", o);
48 out[l-1] = 's'; // overflow me!
49 s = out;
50 } else s = argv[optind+1];
51 if(!strcmp(o, s)) {
52 dprintf(2, "error: input and output file (%s) identical!\n", o);
53 return 1;
56 disas(o, s, flags);
57 return 0;