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