1 /*****************************************************************************/
5 /* Main program for the co65 object file converter */
9 /* (C) 2003 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
45 #include "debugflag.h"
62 /*****************************************************************************/
64 /*****************************************************************************/
68 static void Usage (void)
69 /* Print usage information and exit */
72 "Usage: %s [options] file\n"
74 " -V\t\t\tPrint the version number\n"
75 " -g\t\t\tAdd debug info to object file\n"
76 " -h\t\t\tHelp (this text)\n"
77 " -m model\t\tOverride the o65 model\n"
78 " -o name\t\tName the output file\n"
79 " -v\t\t\tIncrease verbosity\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 " --o65-model model\tOverride the o65 model\n"
91 " --verbose\t\tIncrease verbosity\n"
92 " --version\t\tPrint the version number\n"
93 " --zeropage-label name\tDefine and export a ZEROPAGE segment label\n"
94 " --zeropage-name seg\tSet the name of the ZEROPAGE segment\n",
100 static void CheckLabelName (const char* Label)
101 /* Check if the given label is a valid label name */
103 const char* L = Label;
105 if (strlen (L) < 256 && (IsAlpha (*L) || *L== '_')) {
107 if (!IsAlNum (*L) && *L != '_') {
114 Error ("Label name `%s' is invalid", Label);
120 static void CheckSegName (const char* Seg)
121 /* Abort if the given name is not a valid segment name */
123 /* Print an error and abort if the name is not ok */
124 if (!ValidSegName (Seg)) {
125 Error ("Segment name `%s' is invalid", Seg);
131 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
132 /* Handle the --bss-label option */
134 /* Check for a label name */
135 CheckLabelName (Arg);
138 BssLabel = xstrdup (Arg);
143 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
144 /* Handle the --bss-name option */
146 /* Check for a valid name */
150 BssSeg = xstrdup (Arg);
155 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
156 /* Handle the --code-label option */
158 /* Check for a label name */
159 CheckLabelName (Arg);
162 CodeLabel = xstrdup (Arg);
167 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
168 /* Handle the --code-name option */
170 /* Check for a valid name */
174 CodeSeg = xstrdup (Arg);
179 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
180 /* Handle the --data-label option */
182 /* Check for a label name */
183 CheckLabelName (Arg);
186 DataLabel = xstrdup (Arg);
191 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
192 /* Handle the --data-name option */
194 /* Check for a valid name */
198 DataSeg = xstrdup (Arg);
203 static void OptDebug (const char* Opt attribute ((unused)),
204 const char* Arg attribute ((unused)))
205 /* Enable debugging code */
212 static void OptDebugInfo (const char* Opt attribute ((unused)),
213 const char* Arg attribute ((unused)))
214 /* Add debug info to the object file */
221 static void OptHelp (const char* Opt attribute ((unused)),
222 const char* Arg attribute ((unused)))
223 /* Print usage information and exit */
231 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
232 /* Handle the --o65-model option */
234 /* Search for the model name */
235 Model = FindModel (Arg);
236 if (Model == O65_MODEL_INVALID) {
237 Error ("Unknown o65 model `%s'", Arg);
243 static void OptVerbose (const char* Opt attribute ((unused)),
244 const char* Arg attribute ((unused)))
245 /* Increase verbosity */
252 static void OptVersion (const char* Opt attribute ((unused)),
253 const char* Arg attribute ((unused)))
254 /* Print the assembler version */
257 "co65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
258 VER_MAJOR, VER_MINOR, VER_PATCH);
263 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
264 /* Handle the --zeropage-label option */
266 /* Check for a label name */
267 CheckLabelName (Arg);
270 ZeropageLabel = xstrdup (Arg);
275 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
276 /* Handle the --zeropage-name option */
278 /* Check for a valid name */
282 ZeropageSeg = xstrdup (Arg);
287 static void ConvertOneFile (const char* InputFile, const char* OutputFile)
288 /* Do file conversion */
290 /* Read the o65 file into memory */
291 O65Data* D = ReadO65File (InputFile);
293 /* Do the conversion */
294 Convert (D, OutputFile);
296 /* Free the o65 module data */
303 int main (int argc, char* argv [])
304 /* Converter main program */
306 /* Program long options */
307 static const LongOpt OptTab[] = {
308 { "--bss-label", 1, OptBssLabel },
309 { "--bss-name", 1, OptBssName },
310 { "--code-label", 1, OptCodeLabel },
311 { "--code-name", 1, OptCodeName },
312 { "--data-label", 1, OptDataLabel },
313 { "--data-name", 1, OptDataName },
314 { "--debug", 0, OptDebug },
315 { "--debug-info", 0, OptDebugInfo },
316 { "--help", 0, OptHelp },
317 { "--o65-model", 1, OptO65Model },
318 { "--verbose", 0, OptVerbose },
319 { "--version", 0, OptVersion },
320 { "--zeropage-label", 1, OptZeropageLabel },
321 { "--zeropage-name", 1, OptZeropageName },
326 /* Initialize the cmdline module */
327 InitCmdLine (&argc, &argv, "co65");
329 /* Check the parameters */
331 while (I < ArgCount) {
333 /* Get the argument */
334 const char* Arg = ArgVec [I];
336 /* Check for an option */
337 if (Arg [0] == '-') {
341 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
345 OptDebugInfo (Arg, 0);
353 OptO65Model (Arg, GetArg (&I, 2));
357 OutputName = GetArg (&I, 2);
374 /* Filename. Check if we already had one */
376 Error ("Don't know what to do with `%s'\n", Arg);
386 /* Do we have an input file? */
387 if (InputName == 0) {
388 Error ("No input file\n");
391 /* Generate the name of the output file if none was specified */
392 if (OutputName == 0) {
393 OutputName = MakeFilename (InputName, AsmExt);
396 /* Do the conversion */
397 ConvertOneFile (InputName, OutputName);
399 /* Return an apropriate exit code */