]> git.sur5r.net Git - cc65/blob - src/co65/main.c
Working
[cc65] / src / co65 / main.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                  main.c                                   */
4 /*                                                                           */
5 /*              Main program for the co65 object file converter              */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2003      Ullrich von Bassewitz                                       */
10 /*               Römerstrasse 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     fprintf (stderr,
72              "Usage: %s [options] file\n"
73              "Short options:\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"
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              "  --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",
95              ProgName);
96 }
97
98
99
100 static void CheckLabelName (const char* Label)
101 /* Check if the given label is a valid label name */
102 {
103     const char* L = Label;
104
105     if (strlen (L) < 256 && (IsAlpha (*L) || *L== '_')) {
106         while (*++L) {
107             if (!IsAlNum (*L) && *L != '_') {
108                 break;
109             }
110         }
111     }
112
113     if (*L) {
114         Error ("Label name `%s' is invalid", Label);
115     }
116 }
117
118
119
120 static void CheckSegName (const char* Seg)
121 /* Abort if the given name is not a valid segment name */
122 {
123     /* Print an error and abort if the name is not ok */
124     if (!ValidSegName (Seg)) {
125         Error ("Segment name `%s' is invalid", Seg);
126     }
127 }
128
129
130
131 static void OptBssLabel (const char* Opt attribute ((unused)), const char* Arg)
132 /* Handle the --bss-label option */
133 {
134     /* Check for a label name */
135     CheckLabelName (Arg);
136
137     /* Set the label */
138     BssLabel = xstrdup (Arg);
139 }
140
141
142
143 static void OptBssName (const char* Opt attribute ((unused)), const char* Arg)
144 /* Handle the --bss-name option */
145 {
146     /* Check for a valid name */
147     CheckSegName (Arg);
148
149     /* Set the name */
150     BssSeg = xstrdup (Arg);
151 }
152
153
154
155 static void OptCodeLabel (const char* Opt attribute ((unused)), const char* Arg)
156 /* Handle the --code-label option */
157 {
158     /* Check for a label name */
159     CheckLabelName (Arg);
160
161     /* Set the label */
162     CodeLabel = xstrdup (Arg);
163 }
164
165
166
167 static void OptCodeName (const char* Opt attribute ((unused)), const char* Arg)
168 /* Handle the --code-name option */
169 {
170     /* Check for a valid name */
171     CheckSegName (Arg);
172
173     /* Set the name */
174     CodeSeg = xstrdup (Arg);
175 }
176
177
178
179 static void OptDataLabel (const char* Opt attribute ((unused)), const char* Arg)
180 /* Handle the --data-label option */
181 {
182     /* Check for a label name */
183     CheckLabelName (Arg);
184
185     /* Set the label */
186     DataLabel = xstrdup (Arg);
187 }
188
189
190
191 static void OptDataName (const char* Opt attribute ((unused)), const char* Arg)
192 /* Handle the --data-name option */
193 {
194     /* Check for a valid name */
195     CheckSegName (Arg);
196
197     /* Set the name */
198     DataSeg = xstrdup (Arg);
199 }
200
201
202
203 static void OptDebug (const char* Opt attribute ((unused)),
204                       const char* Arg attribute ((unused)))
205 /* Enable debugging code */
206 {
207     ++Debug;
208 }
209
210
211
212 static void OptDebugInfo (const char* Opt attribute ((unused)),
213                           const char* Arg attribute ((unused)))
214 /* Add debug info to the object file */
215 {
216     DebugInfo = 1;
217 }
218
219
220
221 static void OptHelp (const char* Opt attribute ((unused)),
222                      const char* Arg attribute ((unused)))
223 /* Print usage information and exit */
224 {
225     Usage ();
226     exit (EXIT_SUCCESS);
227 }
228
229
230
231 static void OptO65Model (const char* Opt attribute ((unused)), const char* Arg)
232 /* Handle the --o65-model option */
233 {
234     /* Search for the model name */
235     Model = FindModel (Arg);
236     if (Model == O65_MODEL_INVALID) {
237         Error ("Unknown o65 model `%s'", Arg);
238     }
239 }
240
241
242
243 static void OptVerbose (const char* Opt attribute ((unused)),
244                         const char* Arg attribute ((unused)))
245 /* Increase verbosity */
246 {
247     ++Verbosity;
248 }
249
250
251
252 static void OptVersion (const char* Opt attribute ((unused)),
253                         const char* Arg attribute ((unused)))
254 /* Print the assembler version */
255 {
256     fprintf (stderr,
257              "co65 V%u.%u.%u - (C) Copyright 1998-2003 Ullrich von Bassewitz\n",
258              VER_MAJOR, VER_MINOR, VER_PATCH);
259 }
260
261
262
263 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
264 /* Handle the --zeropage-label option */
265 {
266     /* Check for a label name */
267     CheckLabelName (Arg);
268
269     /* Set the label */
270     ZeropageLabel = xstrdup (Arg);
271 }
272
273
274
275 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
276 /* Handle the --zeropage-name option */
277 {
278     /* Check for a valid name */
279     CheckSegName (Arg);
280
281     /* Set the name */
282     ZeropageSeg = xstrdup (Arg);
283 }
284
285
286
287 static void ConvertOneFile (const char* InputFile, const char* OutputFile)
288 /* Do file conversion */
289 {
290     /* Read the o65 file into memory */
291     O65Data* D = ReadO65File (InputFile);
292
293     /* Do the conversion */
294     Convert (D, OutputFile);
295
296     /* Free the o65 module data */
297     /* ### */
298
299 }
300
301
302
303 int main (int argc, char* argv [])
304 /* Converter main program */
305 {
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         },
322     };
323
324     unsigned I;
325
326     /* Initialize the cmdline module */
327     InitCmdLine (&argc, &argv, "co65");
328
329     /* Check the parameters */
330     I = 1;
331     while (I < ArgCount) {
332
333         /* Get the argument */
334         const char* Arg = ArgVec [I];
335
336         /* Check for an option */
337         if (Arg [0] == '-') {
338             switch (Arg [1]) {
339
340                 case '-':
341                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
342                     break;
343
344                 case 'g':
345                     OptDebugInfo (Arg, 0);
346                     break;
347
348                 case 'h':
349                     OptHelp (Arg, 0);
350                     break;
351
352                 case 'm':
353                     OptO65Model (Arg, GetArg (&I, 2));
354                     break;
355
356                 case 'o':
357                     OutputName = GetArg (&I, 2);
358                     break;
359
360                 case 'v':
361                     OptVerbose (Arg, 0);
362                     break;
363
364                 case 'V':
365                     OptVersion (Arg, 0);
366                     break;
367
368                 default:
369                     UnknownOption (Arg);
370                     break;
371
372             }
373         } else {
374             /* Filename. Check if we already had one */
375             if (InputName) {
376                 Error ("Don't know what to do with `%s'\n", Arg);
377             } else {
378                 InputName = Arg;
379             }
380         }
381
382         /* Next argument */
383         ++I;
384     }
385
386     /* Do we have an input file? */
387     if (InputName == 0) {
388         Error ("No input file\n");
389     }
390
391     /* Generate the name of the output file if none was specified */
392     if (OutputName == 0) {
393         OutputName = MakeFilename (InputName, AsmExt);
394     }
395
396     /* Do the conversion */
397     ConvertOneFile (InputName, OutputName);
398
399     /* Return an apropriate exit code */
400     return EXIT_SUCCESS;
401 }
402
403
404