agsdisas: switch to default silent output
[rofl0r-agsutils.git] / agsdisas.c
blob6fd9905f74a8e5a11e5613675240262c0bc8d348
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 "-v : verbose (show filenames)\n"
14 "-o : dump offset comments in disassembly\n"
15 "-b : dump hexadecimal bytecode comments in disassembly\n"
16 "-f : dump informative original fixups section\n"
17 "-l : remove linenumber debug assembly directives [produces smaller files]\n"
18 , argv0);
19 exit(1);
22 static int disas(char *o, char *s, int flags, int verbose) {
23 //ARF_find_code_start
24 AF f_b, *f = &f_b;
25 ASI sc;
26 int err = 0;
27 if(AF_open(f, o)) {
28 ASI *i = ASI_read_script(f, &sc) ? &sc : 0;
29 if(verbose) printf("disassembling %s -> %s", o, s);
30 if(!i || !ASI_disassemble(f, i, s, flags)) err = 1;
31 if(verbose) {
32 if(err) printf(" FAIL\n");
33 else printf(" OK\n");
35 AF_close(f);
37 return err;
40 int main(int argc, char**argv) {
41 int flags = 0, c, verbose = 0;
42 while ((c = getopt(argc, argv, "voblf")) != EOF) switch(c) {
43 case 'v': verbose = 1; break;
44 case 'o': flags |= DISAS_DEBUG_OFFSETS; break;
45 case 'b': flags |= DISAS_DEBUG_BYTECODE; break;
46 case 'l': flags |= DISAS_SKIP_LINENO; break;
47 case 'f': flags |= DISAS_DEBUG_FIXUPS; break;
48 default: return usage(argv[0]);
50 if(!argv[optind]) return usage(argv[0]);
51 char *o = argv[optind], *s, out[256];
52 if(!argv[optind+1]) {
53 size_t l = strlen(o);
54 snprintf(out, 256, "%s", o);
55 out[l-1] = 's'; // overflow me!
56 s = out;
57 } else s = argv[optind+1];
58 if(!strcmp(o, s)) {
59 dprintf(2, "error: input and output file (%s) identical!\n", o);
60 return 1;
63 return disas(o, s, flags, verbose);