1 | /*****************************************************************************/ |
2 | /* */ |
3 | /* main.c */ |
4 | /* */ |
5 | /* Main program for the co65 object file converter */ |
6 | /* */ |
7 | /* */ |
8 | /* */ |
9 | /* (C) 2003-2009, Ullrich von Bassewitz */ |
10 | /* Roemerstrasse 52 */ |
11 | /* D-70794 Filderstadt */ |
12 | /* EMail: uz@cc65.org */ |
13 | /* */ |
14 | /* */ |
15 | /* This software is provided 'as-is', without any expressed or implied */ |
16 | /* warranty. In no event will the authors be held liable for any damages */ |
17 | /* arising from the use of this software. */ |
18 | /* */ |
19 | /* Permission is granted to anyone to use this software for any purpose, */ |
20 | /* including commercial applications, and to alter it and redistribute it */ |
21 | /* freely, subject to the following restrictions: */ |
22 | /* */ |
23 | /* 1. The origin of this software must not be misrepresented; you must not */ |
24 | /* claim that you wrote the original software. If you use this software */ |
25 | /* in a product, an acknowledgment in the product documentation would be */ |
26 | /* appreciated but is not required. */ |
27 | /* 2. Altered source versions must be plainly marked as such, and must not */ |
28 | /* be misrepresented as being the original software. */ |
29 | /* 3. This notice may not be removed or altered from any source */ |
30 | /* distribution. */ |
31 | /* */ |
32 | /*****************************************************************************/ |
33 | |
34 | |
35 | |
36 | #include <stdio.h> |
37 | #include <stdlib.h> |
38 | #include <string.h> |
39 | #include <errno.h> |
40 | #include <time.h> |
41 | |
42 | /* common */ |
43 | #include "chartype.h" |
44 | #include "cmdline.h" |
45 | #include "debugflag.h" |
46 | #include "fname.h" |
47 | #include "print.h" |
48 | #include "segnames.h" |
49 | #include "version.h" |
50 | #include "xmalloc.h" |
51 | #include "xsprintf.h" |
52 | |
53 | /* co65 */ |
54 | #include "convert.h" |
55 | #include "error.h" |
56 | #include "global.h" |
57 | #include "model.h" |
58 | #include "o65.h" |
59 | |
60 | |
61 | |
62 | /*****************************************************************************/ |
63 | /* Code */ |
64 | /*****************************************************************************/ |
65 | |
66 | |
67 | |
68 | static void Usage (void) |
69 | /* Print usage information and exit */ |
70 | { |
71 | printf ("Usage: %s [options] file\n" |
72 | "Short options:\n" |
73 | " -V\t\t\tPrint the version number\n" |
74 | " -g\t\t\tAdd debug info to object file\n" |
75 | " -h\t\t\tHelp (this text)\n" |
76 | " -m model\t\tOverride the o65 model\n" |
77 | " -n\t\t\tDon't generate an output file\n" |
78 | " -o name\t\tName the output file\n" |
79 | " -v\t\t\tIncrease verbosity\n" |
80 | "\n" |
81 | "Long options:\n" |
82 | " --bss-label name\tDefine and export a BSS segment label\n" |
83 | " --bss-name seg\tSet the name of the BSS segment\n" |
84 | " --code-label name\tDefine and export a CODE segment label\n" |
85 | " --code-name seg\tSet the name of the CODE segment\n" |
86 | " --data-label name\tDefine and export a DATA segment label\n" |
87 | " --data-name seg\tSet the name of the DATA segment\n" |
88 | " --debug-info\t\tAdd debug info to object file\n" |
89 | " --help\t\tHelp (this text)\n" |
90 | " --no-output\t\tDon't generate an output file\n" |
91 | " --o65-model model\tOverride the o65 model\n" |
92 | " --verbose\t\tIncrease verbosity\n" |
93 | " --version\t\tPrint the version number\n" |
94 | " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n" |
95 | " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n" , |
96 | ProgName); |
97 | } |
98 | |
99 | |
100 | |
101 | static void CheckLabelName (const char* Label) |
102 | /* Check if the given label is a valid label name */ |
103 | { |
104 | const char* L = Label; |
105 | |
106 | if (strlen (L) < 256 && (IsAlpha (*L) || *L== '_')) { |
107 | while (*++L) { |
108 | if (!IsAlNum (*L) && *L != '_') { |
109 | break; |
110 | } |
111 | } |
112 | } |
113 | |
114 | if (*L) { |
115 | Error ("Label name '%s' is invalid" , Label); |
116 | } |
117 | } |
118 | |
119 | |
120 | |
121 | static void CheckSegName (const char* Seg) |
122 | /* Abort if the given name is not a valid segment name */ |
123 | { |
124 | /* Print an error and abort if the name is not ok */ |
125 | if (!ValidSegName (Seg)) { |
126 | Error ("Segment name '%s' is invalid" , Seg); |
127 | } |
128 | } |
129 | |
130 | |
131 | |
132 | static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg) |
133 | /* Handle the --bss-label option */ |
134 | { |
135 | /* Check for a label name */ |
136 | CheckLabelName (Arg); |
137 | |
138 | /* Set the label */ |
139 | BssLabel = xstrdup (Arg); |
140 | } |
141 | |
142 | |
143 | |
144 | static void OptBssName (const char* Opt attribute ((unused)), const char* Arg) |
145 | /* Handle the --bss-name option */ |
146 | { |
147 | /* Check for a valid name */ |
148 | CheckSegName (Arg); |
149 | |
150 | /* Set the name */ |
151 | BssSeg = xstrdup (Arg); |
152 | } |
153 | |
154 | |
155 | |
156 | static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg) |
157 | /* Handle the --code-label option */ |
158 | { |
159 | /* Check for a label name */ |
160 | CheckLabelName (Arg); |
161 | |
162 | /* Set the label */ |
163 | CodeLabel = xstrdup (Arg); |
164 | } |
165 | |
166 | |
167 | |
168 | static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg) |
169 | /* Handle the --code-name option */ |
170 | { |
171 | /* Check for a valid name */ |
172 | CheckSegName (Arg); |
173 | |
174 | /* Set the name */ |
175 | CodeSeg = xstrdup (Arg); |
176 | } |
177 | |
178 | |
179 | |
180 | static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg) |
181 | /* Handle the --data-label option */ |
182 | { |
183 | /* Check for a label name */ |
184 | CheckLabelName (Arg); |
185 | |
186 | /* Set the label */ |
187 | DataLabel = xstrdup (Arg); |
188 | } |
189 | |
190 | |
191 | |
192 | static void OptDataName (const char* Opt attribute ((unused)), const char* Arg) |
193 | /* Handle the --data-name option */ |
194 | { |
195 | /* Check for a valid name */ |
196 | CheckSegName (Arg); |
197 | |
198 | /* Set the name */ |
199 | DataSeg = xstrdup (Arg); |
200 | } |
201 | |
202 | |
203 | |
204 | static void OptDebug (const char* Opt attribute ((unused)), |
205 | const char* Arg attribute ((unused))) |
206 | /* Enable debugging code */ |
207 | { |
208 | ++Debug; |
209 | } |
210 | |
211 | |
212 | |
213 | static void OptDebugInfo (const char* Opt attribute ((unused)), |
214 | const char* Arg attribute ((unused))) |
215 | /* Add debug info to the object file */ |
216 | { |
217 | DebugInfo = 1; |
218 | } |
219 | |
220 | |
221 | |
222 | static void OptHelp (const char* Opt attribute ((unused)), |
223 | const char* Arg attribute ((unused))) |
224 | /* Print usage information and exit */ |
225 | { |
226 | Usage (); |
227 | exit (EXIT_SUCCESS); |
228 | } |
229 | |
230 | |
231 | |
232 | static void OptNoOutput (const char* Opt attribute ((unused)), |
233 | const char* Arg attribute ((unused))) |
234 | /* Handle the --no-output option */ |
235 | { |
236 | NoOutput = 1; |
237 | } |
238 | |
239 | |
240 | |
241 | static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg) |
242 | /* Handle the --o65-model option */ |
243 | { |
244 | /* Search for the model name */ |
245 | Model = FindModel (Arg); |
246 | if (Model == O65_MODEL_INVALID) { |
247 | Error ("Unknown o65 model '%s'" , Arg); |
248 | } |
249 | } |
250 | |
251 | |
252 | |
253 | static void OptVerbose (const char* Opt attribute ((unused)), |
254 | const char* Arg attribute ((unused))) |
255 | /* Increase verbosity */ |
256 | { |
257 | ++Verbosity; |
258 | } |
259 | |
260 | |
261 | |
262 | static void OptVersion (const char* Opt attribute ((unused)), |
263 | const char* Arg attribute ((unused))) |
264 | /* Print the assembler version */ |
265 | { |
266 | fprintf (stderr, "%s V%s\n" , ProgName, GetVersionAsString ()); |
267 | exit(EXIT_SUCCESS); |
268 | } |
269 | |
270 | |
271 | |
272 | static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg) |
273 | /* Handle the --zeropage-label option */ |
274 | { |
275 | /* Check for a label name */ |
276 | CheckLabelName (Arg); |
277 | |
278 | /* Set the label */ |
279 | ZeropageLabel = xstrdup (Arg); |
280 | } |
281 | |
282 | |
283 | |
284 | static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg) |
285 | /* Handle the --zeropage-name option */ |
286 | { |
287 | /* Check for a valid name */ |
288 | CheckSegName (Arg); |
289 | |
290 | /* Set the name */ |
291 | ZeropageSeg = xstrdup (Arg); |
292 | } |
293 | |
294 | |
295 | |
296 | static void DoConversion (void) |
297 | /* Do file conversion */ |
298 | { |
299 | /* Read the o65 file into memory */ |
300 | O65Data* D = ReadO65File (InputName); |
301 | |
302 | /* Do the conversion */ |
303 | Convert (D); |
304 | |
305 | /* Free the o65 module data */ |
306 | /* ### */ |
307 | |
308 | } |
309 | |
310 | |
311 | |
312 | int main (int argc, char* argv []) |
313 | /* Converter main program */ |
314 | { |
315 | /* Program long options */ |
316 | static const LongOpt OptTab[] = { |
317 | { "--bss-label" , 1, OptBssLabel }, |
318 | { "--bss-name" , 1, OptBssName }, |
319 | { "--code-label" , 1, OptCodeLabel }, |
320 | { "--code-name" , 1, OptCodeName }, |
321 | { "--data-label" , 1, OptDataLabel }, |
322 | { "--data-name" , 1, OptDataName }, |
323 | { "--debug" , 0, OptDebug }, |
324 | { "--debug-info" , 0, OptDebugInfo }, |
325 | { "--help" , 0, OptHelp }, |
326 | { "--no-output" , 0, OptNoOutput }, |
327 | { "--o65-model" , 1, OptO65Model }, |
328 | { "--verbose" , 0, OptVerbose }, |
329 | { "--version" , 0, OptVersion }, |
330 | { "--zeropage-label" , 1, OptZeropageLabel }, |
331 | { "--zeropage-name" , 1, OptZeropageName }, |
332 | }; |
333 | |
334 | unsigned I; |
335 | |
336 | /* Initialize the cmdline module */ |
337 | InitCmdLine (&argc, &argv, "co65" ); |
338 | |
339 | /* Check the parameters */ |
340 | I = 1; |
341 | while (I < ArgCount) { |
342 | |
343 | /* Get the argument */ |
344 | const char* Arg = ArgVec [I]; |
345 | |
346 | /* Check for an option */ |
347 | if (Arg [0] == '-') { |
348 | switch (Arg [1]) { |
349 | |
350 | case '-': |
351 | LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0])); |
352 | break; |
353 | |
354 | case 'g': |
355 | OptDebugInfo (Arg, 0); |
356 | break; |
357 | |
358 | case 'h': |
359 | OptHelp (Arg, 0); |
360 | break; |
361 | |
362 | case 'm': |
363 | OptO65Model (Arg, GetArg (&I, 2)); |
364 | break; |
365 | |
366 | case 'n': |
367 | OptNoOutput (Arg, 0); |
368 | break; |
369 | |
370 | case 'o': |
371 | OutputName = GetArg (&I, 2); |
372 | break; |
373 | |
374 | case 'v': |
375 | OptVerbose (Arg, 0); |
376 | break; |
377 | |
378 | case 'V': |
379 | OptVersion (Arg, 0); |
380 | break; |
381 | |
382 | default: |
383 | UnknownOption (Arg); |
384 | break; |
385 | |
386 | } |
387 | } else { |
388 | /* Filename. Check if we already had one */ |
389 | if (InputName) { |
390 | Error ("Don't know what to do with '%s'" , Arg); |
391 | } else { |
392 | InputName = Arg; |
393 | } |
394 | } |
395 | |
396 | /* Next argument */ |
397 | ++I; |
398 | } |
399 | |
400 | /* Do we have an input file? */ |
401 | if (InputName == 0) { |
402 | Error ("No input file" ); |
403 | } |
404 | |
405 | /* Generate the name of the output file if none was specified */ |
406 | if (OutputName == 0) { |
407 | OutputName = MakeFilename (InputName, AsmExt); |
408 | } |
409 | |
410 | /* Do the conversion */ |
411 | DoConversion (); |
412 | |
413 | /* Return an apropriate exit code */ |
414 | return EXIT_SUCCESS; |
415 | } |
416 | |