comparison lwdisasm/main.c @ 408:2a94b2e64621

Started creation of lwdisasm
author lost@l-w.ca
date Mon, 02 Aug 2010 13:24:07 -0600
parents
children cba03436c720
comparison
equal deleted inserted replaced
407:b2e007c28b8f 408:2a94b2e64621
1 /*
2 main.c
3
4 Copyright © 2010 William Astle
5
6 This file is part of LWTOOLS.
7
8 LWTOOLS is free software: you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation, either version 3 of the License, or (at your option) any later
11 version.
12
13 This program is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include <config.h>
23
24 #include <argp.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include <lw_alloc.h>
29 #include <lw_string.h>
30
31 #include "lwdisasm.h"
32
33 /* command line option handling */
34 const char *argp_program_version = "lwdisasmasm from " PACKAGE_STRING;
35 const char *argp_program_bug_address = PACKAGE_BUGREPORT;
36 char *program_name;
37
38 static struct argp_option options[] =
39 {
40 { "output", 'o', "FILE", 0, "Output to FILE"},
41 { "debug", 'd', "LEVEL", OPTION_ARG_OPTIONAL, "Set debug mode"},
42 { "format", 'f', "TYPE", 0, "Select input format: decb, raw, obj, os9"},
43 { "decb", 'b', 0, 0, "Read DECB .bin format input, equivalent of --format=decb"},
44 { "raw", 'r', 0, 0, "Read raw binary format input, equivalent of --format=raw"},
45 { "obj", 0x100, 0, 0, "Read proprietary object file format, equivalent of --format=obj" },
46 { "6809", '9', 0, 0, "Set disassembler to 6809 only mode" },
47 { "6309", '3', 0, 0, "Set disassembler to 6309 mode (default)" },
48 { "data", 0x101, "RANGE", 0, "Set an address range as data" },
49 { "code", 0x102, "RANGE", 0, "Set an address range as code" },
50 { "entry", 0x103, "ADDR", 0, "Start disassembling at ADDR" },
51 { "base", 0x104, "ADDR", 0, "Set the base address of a raw file" },
52 { 0 }
53 };
54
55
56 static error_t parse_opts(int key, char *arg, struct argp_state *state)
57 {
58 disasmstate_t *as = state -> input;
59 int rangelow, rangehigh;
60 char *e;
61
62 switch (key)
63 {
64
65 case 0x101: // data range
66 rangelow = strtol(arg, &e, 0);
67 if (*e != ':')
68 {
69 fprintf(stderr, "Invalid range: %s\n", arg);
70 exit(1);
71 }
72 rangehigh = strtol(e + 1, NULL, 0);
73 if (rangelow < 0 || rangehigh < 0 || rangehigh < rangelow)
74 {
75 fprintf(stderr, "Invalid range: %s\n", arg);
76 exit(1);
77 }
78 // register the range as data
79 break;
80
81 case 0x102: // code range
82 rangelow = strtol(arg, &e, 0);
83 if (*e != ':')
84 {
85 fprintf(stderr, "Invalid range: %s\n", arg);
86 exit(1);
87 }
88 rangehigh = strtol(e + 1, NULL, 0);
89 if (rangelow < 0 || rangehigh < 0 || rangehigh < rangelow)
90 {
91 fprintf(stderr, "Invalid range: %s\n", arg);
92 exit(1);
93 }
94 // register the range as code
95 break;
96
97 case 0x103: // entry
98 as -> entry = strtol(arg, NULL, 0);
99 break;
100
101 case 0x104: // base
102 as -> base = strtol(arg, NULL, 0);
103 break;
104
105 case 'o':
106 if (as -> output_file)
107 lw_free(as -> output_file);
108 as -> output_file = lw_strdup(arg);
109 break;
110
111 case 'd':
112 if (!arg)
113 as -> debug_level = 50;
114 else
115 as -> debug_level = atoi(arg);
116 break;
117
118 case 'b':
119 as -> input_type = INPUT_DECB;
120 break;
121
122 case 'r':
123 as -> input_type = INPUT_RAW;
124 break;
125
126 case 0x100:
127 as -> input_type = INPUT_OBJ;
128 break;
129
130 case 'f':
131 if (!strcasecmp(arg, "decb"))
132 as -> input_type = INPUT_DECB;
133 else if (!strcasecmp(arg, "raw"))
134 as -> input_type = INPUT_RAW;
135 else if (!strcasecmp(arg, "obj"))
136 as -> input_type = INPUT_OBJ;
137 else if (!strcasecmp(arg, "os9"))
138 as -> input_type = INPUT_OS9;
139 else
140 {
141 fprintf(stderr, "Invalid input format: %s\n", arg);
142 exit(1);
143 }
144 break;
145
146 case '9':
147 as -> target = TARGET_6809;
148 break;
149
150 case '3':
151 as -> target = TARGET_6309;
152 break;
153
154 case ARGP_KEY_END:
155 break;
156
157 case ARGP_KEY_ARG:
158 if (as -> input_file)
159 {
160 fprintf(stderr, "Only one input file allowed\n");
161 exit(1);
162 }
163 as -> input_file = lw_strdup(arg);
164 break;
165
166 default:
167 return ARGP_ERR_UNKNOWN;
168 }
169 return 0;
170 }
171
172 static struct argp argp =
173 {
174 options,
175 parse_opts,
176 "<input file>",
177 "LWDISASM, a HD6309 and MC6809 disassembler"
178 };
179
180 /*
181 main function; parse command line, set up disassembler state, and run the
182 disassembler on the first file
183 */
184
185 int main(int argc, char **argv)
186 {
187 disasmstate_t as = { 0 };
188 FILE *fp;
189
190 /* assembler state */
191 program_name = argv[0];
192
193 /* parse command line arguments */
194 argp_parse(&argp, argc, argv, 0, 0, &as);
195
196 if (as.input_file == NULL)
197 {
198 fprintf(stderr, "No input files specified.\n");
199 exit(1);
200 }
201
202 fp = fopen(as.input_file, "rb");
203 if (!fp)
204 {
205 perror("Cannot open input file");
206 exit(1);
207 }
208
209 fseek(fp, 0, SEEK_END);
210 as.filelen = ftell(fp);
211 rewind(fp);
212
213 as.filedata = lw_alloc(as.filelen);
214 0 == fread(as.filedata, as.filelen, 1, fp);
215
216 fclose(fp);
217
218 exit(0);
219 }