]> git.sur5r.net Git - cc65/blob - src/co65/main.c
Move the version numbers from the interface of the version module into a new
[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-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,
267              "co65 V%s - (C) Copyright 1998-2009 Ullrich von Bassewitz\n",
268              GetVersionAsString ());
269 }
270
271
272
273 static void OptZeropageLabel (const char* Opt attribute ((unused)), const char* Arg)
274 /* Handle the --zeropage-label option */
275 {
276     /* Check for a label name */
277     CheckLabelName (Arg);
278
279     /* Set the label */
280     ZeropageLabel = xstrdup (Arg);
281 }
282
283
284
285 static void OptZeropageName (const char* Opt attribute ((unused)), const char* Arg)
286 /* Handle the --zeropage-name option */
287 {
288     /* Check for a valid name */
289     CheckSegName (Arg);
290
291     /* Set the name */
292     ZeropageSeg = xstrdup (Arg);
293 }
294
295
296
297 static void DoConversion (void)
298 /* Do file conversion */
299 {
300     /* Read the o65 file into memory */
301     O65Data* D = ReadO65File (InputName);
302
303     /* Do the conversion */
304     Convert (D);
305
306     /* Free the o65 module data */
307     /* ### */
308
309 }
310
311
312
313 int main (int argc, char* argv [])
314 /* Converter main program */
315 {
316     /* Program long options */
317     static const LongOpt OptTab[] = {
318         { "--bss-label",        1,      OptBssLabel             },
319         { "--bss-name",         1,      OptBssName              },
320         { "--code-label",       1,      OptCodeLabel            },
321         { "--code-name",        1,      OptCodeName             },
322         { "--data-label",       1,      OptDataLabel            },
323         { "--data-name",        1,      OptDataName             },
324         { "--debug",            0,      OptDebug                },
325         { "--debug-info",       0,      OptDebugInfo            },
326         { "--help",             0,      OptHelp                 },
327         { "--no-output",        0,      OptNoOutput             },
328         { "--o65-model",        1,      OptO65Model             },
329         { "--verbose",          0,      OptVerbose              },
330         { "--version",          0,      OptVersion              },
331         { "--zeropage-label",   1,      OptZeropageLabel        },
332         { "--zeropage-name",    1,      OptZeropageName         },
333     };
334
335     unsigned I;
336
337     /* Initialize the cmdline module */
338     InitCmdLine (&argc, &argv, "co65");
339
340     /* Check the parameters */
341     I = 1;
342     while (I < ArgCount) {
343
344         /* Get the argument */
345         const char* Arg = ArgVec [I];
346
347         /* Check for an option */
348         if (Arg [0] == '-') {
349             switch (Arg [1]) {
350
351                 case '-':
352                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
353                     break;
354
355                 case 'g':
356                     OptDebugInfo (Arg, 0);
357                     break;
358
359                 case 'h':
360                     OptHelp (Arg, 0);
361                     break;
362
363                 case 'm':
364                     OptO65Model (Arg, GetArg (&I, 2));
365                     break;
366
367                 case 'n':
368                     OptNoOutput (Arg, 0);
369                     break;
370
371                 case 'o':
372                     OutputName = GetArg (&I, 2);
373                     break;
374
375                 case 'v':
376                     OptVerbose (Arg, 0);
377                     break;
378
379                 case 'V':
380                     OptVersion (Arg, 0);
381                     break;
382
383                 default:
384                     UnknownOption (Arg);
385                     break;
386
387             }
388         } else {
389             /* Filename. Check if we already had one */
390             if (InputName) {
391                 Error ("Don't know what to do with `%s'\n", Arg);
392             } else {
393                 InputName = Arg;
394             }
395         }
396
397         /* Next argument */
398         ++I;
399     }
400
401     /* Do we have an input file? */
402     if (InputName == 0) {
403         Error ("No input file\n");
404     }
405
406     /* Generate the name of the output file if none was specified */
407     if (OutputName == 0) {
408         OutputName = MakeFilename (InputName, AsmExt);
409     }
410
411     /* Do the conversion */
412     DoConversion ();
413
414     /* Return an apropriate exit code */
415     return EXIT_SUCCESS;
416 }
417
418
419