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