]> git.sur5r.net Git - cc65/blob - src/grc65/main.c
1b417c64d17ab9e213c4736fe5b8a2c7588df8b5
[cc65] / src / grc65 / main.c
1 /* GEOS resource compiler
2
3    by Maciej 'YTM/Elysium' Witkowiak
4
5    see GEOSLib documentation for license info
6 */
7
8 /* - make it work, then do it better
9    - more or less comments? it was hard to code, should be even harder to
10      understand =D
11    - add loadable icons feature (binary - 63 bytes)
12 */
13
14 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <string.h>
21 #include <errno.h>
22 #include <time.h>
23
24 /* common stuff */
25 #include "abend.h"
26 #include "cmdline.h"
27 #include "fname.h"
28 #include "chartype.h"
29 #include "target.h"
30 #include "version.h"
31 #include "xmalloc.h"
32
33 /* I hope that no one will be able to create a .grc bigger than this... */
34 #define BLOODY_BIG_BUFFER 65000
35
36
37
38 struct menuitem {
39     char *name;
40     char *type;
41     char *target;
42     struct menuitem *next;
43 };
44
45 struct menu {
46     char *name;
47     int top, left;
48     int bot, right;
49     char *type;
50     struct menuitem *item;
51 };
52
53 struct appheader {
54     int year, month, day, hour, min;
55     int mode;
56     int dostype;
57     int geostype;
58     int structure;
59     char *dosname;
60     char *classname;
61     char *version;
62     char *author;
63     char *info;
64     char *icon;
65 };
66
67 const char *mainToken[] = {"MENU", "HEADER", "ICON", "DIALOG", "MEMORY", ""};
68
69 const char *toggle[] = {"off", "no", "0", "on", "yes", "1", ""};
70
71 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
72                            "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
73
74 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
75
76 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
77
78 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
79
80 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
81
82 const char *memFields[] = {"stacksize", "overlaysize", "overlaynums", "backbuffer", ""};
83
84 const int BSWTab[] = {0, 0x005, 0x007, 0x00b, 0x011, 0x017, 0x01d, 0x023,
85     0x025, 0x029, 0x02d, 0x033, 0x039, 0x03c, 0x041, 0x043, 0x04a, 0x04f,
86     0x052, 0x056, 0x05a, 0x05f, 0x063, 0x068, 0x06d, 0x072, 0x077, 0x079,
87     0x07c, 0x080, 0x084, 0x088, 0x08e, 0x094, 0x09a, 0x09f, 0x0a4, 0x0a9,
88     0x0ad, 0x0b1, 0x0b6, 0x0bc, 0x0be, 0x0c2, 0x0c8, 0x0cc, 0x0d4, 0x0da,
89     0x0e0, 0x0e5, 0x0eb, 0x0f0, 0x0f5, 0x0f9, 0x0fe, 0x104, 0x10c, 0x112,
90     0x118, 0x11e, 0x121, 0x129, 0x12c, 0x132, 0x13a, 0x13e, 0x143, 0x148,
91     0x14d, 0x152, 0x157, 0x15a, 0x15f, 0x164, 0x166, 0x168, 0x16d, 0x16f,
92     0x177, 0x17c, 0x182, 0x187, 0x18c, 0x18f, 0x193, 0x196, 0x19b, 0x1a1,
93     0x1a9, 0x1af, 0x1b4, 0x1ba, 0x1be, 0x1c0, 0x1c4, 0x1ca, 0x1d2, 0x1dd};
94
95 const unsigned char icon1[] = {255, 255, 255, 128,   0,   1, 128,   0,   1,
96                                128,   0,   1, 128,   0,   1, 128,   0,   1,
97                                128,   0,   1, 128,   0,   1, 128,   0,   1,
98                                128,   0,   1, 128,   0,   1, 128,   0,   1,
99                                128,   0,   1, 128,   0,   1, 128,   0,   1,
100                                128,   0,   1, 128,   0,   1, 128,   0,   1,
101                                128,   0,   1, 128,   0,   1, 255, 255, 255};
102
103 const char *outputCName = NULL;
104 const char *outputSName = NULL;
105 FILE *outputCFile, *outputSFile;
106 int CFnum = 0, SFnum = 0;
107 int apple = 0;
108 char outputCMode[2] = "w";
109 char outputSMode[2] = "w";
110
111
112 static void Usage (void)
113 {
114     printf (
115         "Usage: %s [options] file\n"
116         "Short options:\n"
117         "  -V\t\t\tPrint the version number\n"
118         "  -h\t\t\tHelp (this text)\n"
119         "  -o name\t\tName the C output file\n"
120         "  -s name\t\tName the asm output file\n"
121         "  -t sys\t\tSet the target system\n"
122         "\n"
123         "Long options:\n"
124         "  --help\t\tHelp (this text)\n"
125         "  --target sys\t\tSet the target system\n"
126         "  --version\t\tPrint the version number\n",
127         ProgName);
128 }
129
130
131 static void OptHelp (const char* Opt attribute ((unused)),
132                      const char* Arg attribute ((unused)))
133 /* Print usage information and exit */
134 {
135     Usage ();
136     exit (EXIT_SUCCESS);
137 }
138
139
140 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
141 /* Set the target system */
142 {
143     switch (FindTarget (Arg)) {
144
145         case TGT_GEOS_CBM:
146             apple = 0;
147             break;
148
149         case TGT_GEOS_APPLE:
150             apple = 1;
151             break;
152
153         case TGT_UNKNOWN:
154             AbEnd ("Unknown target system `%s'", Arg);
155             break;
156
157         default:
158             /* Target is known but unsupported */
159             AbEnd ("Unsupported target system `%s'", Arg);
160             break;
161     }
162 }
163
164
165 static void OptVersion (const char* Opt attribute ((unused)),
166                         const char* Arg attribute ((unused)))
167 /* Print the program version */
168 {
169     fprintf (stderr, "grc65 V%s\n", GetVersionAsString ());
170 }
171
172
173
174 static void printCHeader (void)
175 {
176     fprintf (outputCFile,
177         "//\n"
178         "//\tThis file was generated by the GEOS Resource Compiler\n"
179         "//\n"
180         "//\tDO NOT EDIT! Any changes will be lost!\n"
181         "//\n"
182         "//\tEdit proper resource file instead.\n"
183         "//\n\n");
184 }
185
186
187 static void printSHeader (void)
188 {
189     fprintf (outputSFile,
190         ";\n"
191         ";\tThis file was generated by the GEOS Resource Compiler\n"
192         ";\n"
193         ";\tDO NOT EDIT! Any changes will be lost!\n"
194         ";\n"
195         ";\tEdit proper resource file instead.\n"
196         ";\n\n");
197 }
198
199
200 static void openCFile (void)
201 {
202     if ((outputCFile = fopen (outputCName,outputCMode)) == 0) {
203         AbEnd ("Can't open file %s for writing: %s", outputCName, strerror (errno));
204     }
205
206     if (CFnum == 0) {
207         outputCMode[0] = 'a';
208         printCHeader ();
209         CFnum++;
210     }
211 }
212
213
214 static void openSFile (void)
215 {
216     if ((outputSFile = fopen (outputSName, outputSMode)) == 0) {
217         AbEnd ("Can't open file %s for writing: %s", outputSName, strerror (errno));
218     }
219
220     if (SFnum == 0) {
221         outputSMode[0] = 'a';
222         printSHeader ();
223         SFnum++;
224     }
225 }
226
227
228 static int findToken (const char **tokenTbl, const char *token)
229 {
230     /* takes as input table of tokens and token, returns position in table or -1 if not found */
231     int a = 0;
232
233     while (strlen (tokenTbl[a]) != 0) {
234         if (strcmp (tokenTbl[a], token) == 0) break;
235         a++;
236     }
237
238     if (strlen (tokenTbl[a]) == 0) a = -1;
239     return a;
240 }
241
242
243 static char *nextPhrase (void)
244 {
245     return strtok (NULL, "\"");
246 }
247
248
249 static char *nextWord (void)
250 {
251     return strtok (NULL, " ");
252 }
253
254
255 static void setLen (char *name, unsigned len)
256 {
257     if (strlen (name) > len) {
258         name[len] = '\0';
259     }
260 }
261
262
263 static void fillOut (char *name, int len, char *filler)
264 {
265     int a;
266
267     setLen (name, len);
268     fprintf (outputSFile, "\t.byte \"%s\"\n", name);
269
270     a = strlen (name);
271     if (a < len) {
272         fprintf (outputSFile, "\t.res  (%i - %i), %s\n", len, a, filler);
273     }
274 }
275
276
277 static char *bintos (unsigned char a, char out[7])
278 {
279     int i=0;
280
281     for (; i < 8; i++) {
282         out[7 - i] = ((a & 1) == 0) ? '0' : '1';
283         a = a >> 1;
284     }
285     out[i] = '\0';
286
287     return out;
288 }
289
290
291 static int getNameSize (const char *word)
292 {
293     /* count length of a word using BSW 9 font table */
294     int a = 0, i = 0;
295
296     while (word[i] != '\0') {
297         a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
298         i++;
299     }
300
301     return a;
302 }
303
304
305 static void DoMenu (void)
306 {
307     int a, size, tmpsize, item = 0;
308     char *token;
309     char namebuff[255] = "";
310     struct menu myMenu;
311     struct menuitem *curItem, *newItem;
312
313     openCFile ();
314
315     myMenu.name = nextWord ();
316     myMenu.left = atoi (nextWord ());
317     myMenu.top = atoi (nextWord ());
318     myMenu.type = nextWord ();
319
320     if (strcmp (nextWord (), "{") != 0) {
321         AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
322     }
323     curItem = xmalloc (sizeof(struct menuitem));
324     myMenu.item = curItem;
325
326     for (;;) {
327         token = nextWord ();
328         if (strcmp (token, "}") == 0) break;
329         if (token[strlen (token) - 1] != '"') {
330             strcpy (namebuff, token);
331             do {
332                 token = nextWord ();
333                 strcat (namebuff, " ");
334                 strcat (namebuff, token);
335             } while (token[strlen (token) - 1] != '"');
336             token = xmalloc (strlen (namebuff));
337             strcpy (token, namebuff);
338         }
339         curItem->name = token;
340         curItem->type = nextWord ();
341         curItem->target = nextWord ();
342         newItem = xmalloc (sizeof(struct menuitem));
343         curItem->next = newItem;
344         curItem = newItem;
345         item++;
346     }
347
348     if (item == 0) AbEnd ("Menu '%s' has 0 items!", myMenu.name);
349     if (item > 31) AbEnd ("Menu '%s' has too many items!", myMenu.name);
350
351     curItem->next = NULL;
352
353     /* count menu sizes */
354     size = 0;
355     curItem = myMenu.item;
356     if (strstr (myMenu.type, "HORIZONTAL") != NULL) {
357         /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
358         myMenu.bot = myMenu.top + 15;
359         for (a = 0; a != item; a++) {
360             size += getNameSize (curItem->name);
361             curItem = curItem->next;
362         }
363     } else {
364         /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
365         myMenu.bot = myMenu.top + (14 * item);
366         for (a = 0; a != item; a++) {
367             tmpsize = getNameSize (curItem->name);
368             size = (size > tmpsize) ? size : tmpsize;
369             curItem = curItem->next;
370         }
371     }
372     myMenu.right = myMenu.left + size - 1;
373
374     curItem = myMenu.item;
375     for (a = 0; a != item; a++) {
376         /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
377         if ((strstr (curItem->type, "MENU_ACTION") != NULL) || (strstr (curItem->type, "DYN_SUB_MENU") != NULL)) {
378             fprintf (outputCFile,
379                 "void %s (void);\n",
380                 curItem->target);
381         }
382         curItem=curItem->next;
383     }
384
385     fprintf (outputCFile,
386         "\n"
387         "const void %s = {\n"
388         "\t(char)%i, (char)%i,\n"
389         "\t(int)%i, (int)%i,\n"
390         "\t(char)(%i | %s),\n",
391         myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
392
393     curItem = myMenu.item;
394     for (a = 0; a != item; a++) {
395         fprintf (outputCFile,
396             "\t%s, (char)%s, (int)",
397             curItem->name, curItem->type);
398         if ((strstr (curItem->type, "SUB_MENU") != NULL) && (strstr (curItem->type, "DYN_SUB_MENU") == NULL)) {
399             fprintf (outputCFile,
400                 "&");
401         }
402         fprintf (outputCFile,
403             "%s,\n",
404             curItem->target);
405         curItem = curItem->next;
406     }
407
408     fprintf (outputCFile,
409         "};\n\n");
410
411     if (fclose (outputCFile) != 0) {
412         AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
413     }
414 }
415
416
417 static void DoHeader (void)
418 {
419     time_t t;
420     struct tm *my_tm;
421
422     struct appheader myHead;
423     char *token;
424     char i1[9], i2[9], i3[9];
425     int i;
426
427     openSFile ();
428
429     token = nextWord ();
430
431     i = findToken (hdrFTypes, token);
432
433     if (apple == 1) {
434         switch (i) {
435             case 0:
436                 myHead.geostype = 0x82;
437                 break;
438             default:
439                 AbEnd ("Filetype '%s' is not supported yet", token);
440         }
441     } else {
442         switch (i) {
443             case 0:
444                 myHead.geostype = 6;
445                 break;
446             case 1:
447                 myHead.geostype = 14;
448                 break;
449             default:
450                 AbEnd ("Filetype '%s' is not supported yet", token);
451         }
452     }
453
454     myHead.dosname = nextPhrase ();
455     nextPhrase ();
456     myHead.classname = nextPhrase ();
457     nextPhrase ();
458     myHead.version = nextPhrase ();
459
460     /* put default values into myHead here */
461     myHead.author = "cc65";
462     myHead.info = "Program compiled with cc65 and GEOSLib.";
463     myHead.dostype = 128;
464     if (apple == 0) myHead.dostype += 3;
465     myHead.structure = 0;
466     myHead.mode = 0;
467     myHead.icon = NULL;
468
469     t = time (NULL);
470     my_tm = localtime (&t);
471
472     myHead.year = my_tm->tm_year % 100;
473     myHead.month = my_tm->tm_mon + 1;
474     myHead.day = my_tm->tm_mday;
475     myHead.hour = my_tm->tm_hour;
476     myHead.min = my_tm->tm_min;
477
478     if (strcmp (nextWord (), "{") != 0) {
479         AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
480     }
481
482     for (;;) {
483         token = nextWord ();
484         if (strcmp (token, "}") == 0) break;
485         switch (findToken (hdrFields, token)) {
486             case -1:
487                 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
488                 break;
489
490             case 0: /* author */
491                 myHead.author = nextPhrase ();
492                 break;
493
494             case 1: /* info */
495                 myHead.info = nextPhrase ();
496                 break;
497
498             case 2: /* date */
499                 myHead.year = atoi (nextWord ());
500                 myHead.month = atoi (nextWord ());
501                 myHead.day = atoi (nextWord ());
502                 myHead.hour = atoi (nextWord ());
503                 myHead.min = atoi (nextWord ());
504                 break;
505
506             case 3: /* dostype */
507                 switch (i = findToken (hdrDOSTp, nextWord ())) {
508                     case -1:
509                         AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
510                         break;
511                     default:
512                         if (apple == 0) myHead.dostype = i / 2 + 128 + 1;
513                         break;
514                 }
515                 break;
516
517             case 4: /* mode */
518                 switch (findToken (hdrModes, nextWord ())) {
519                     case -1:
520                         AbEnd ("Unknown mode in header '%s'", myHead.dosname);
521                     case 0:
522                         if (apple == 0) myHead.mode = 0x40;
523                         break;
524                     case 1:
525                         if (apple == 0) myHead.mode = 0x00;
526                         break;
527                     case 2:
528                         if (apple == 0) myHead.mode = 0xc0;
529                         break;
530                     case 3:
531                         if (apple == 0) myHead.mode = 0x80;
532                         break;
533                 }
534                 break;
535
536             case 5: /* structure */
537                 switch (findToken (hdrStructTp, nextWord ())) {
538                     case -1:
539                         AbEnd ("unknown structure type in header '%s'", myHead.dosname);
540                     case 0:
541                     case 1:
542                         myHead.structure = 0;
543                         break;
544                     case 2:
545                     case 3:
546                         myHead.structure = 1;
547                         break;
548                 }
549                 break;
550
551             case 6: /* icon */
552                 myHead.icon = nextPhrase ();
553                 break;
554         }
555     }
556
557     /* OK, all information is gathered, do flushout */
558
559     fprintf (outputSFile,
560         "\t\t.segment \"DIRENTRY\"\n\n");
561
562     if (apple == 1) {
563
564         if (myHead.structure == 0) {
565             fprintf (outputSFile,
566                 "\t.import __VLIR0_START__, __VLIR0_LAST__, __BSS_SIZE__\n\n");
567         }
568         fprintf (outputSFile,
569             "\t.byte %i << 4 | %u\n",
570             myHead.structure + 2, (unsigned)strlen (myHead.dosname));
571
572         fillOut (myHead.dosname, 15, "0");
573
574         fprintf (outputSFile,
575             "\t.byte $%02x\n"
576             "\t.word 0\n"
577             "\t.word 0\n"
578             "\t.word %s\n"
579             "\t.byte 0\n"
580             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
581             "\t.byte 0\n"
582             "\t.byte 0\n"
583             "\t.byte 0\n"
584             "\t.word 0\n"
585             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
586             "\t.word 0\n\n",
587             myHead.geostype,
588             myHead.structure == 0 ?
589                 "__VLIR0_LAST__ - __VLIR0_START__ - __BSS_SIZE__" : "0",
590             myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min,
591             myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
592
593     } else {
594
595         fprintf (outputSFile,
596             "\t.byte %i\n"
597             "\t.word 0\n",
598             myHead.dostype);
599
600         fillOut (myHead.dosname, 16, "$a0");
601
602         fprintf (outputSFile,
603             "\t.word 0\n"
604             "\t.byte %i\n"
605             "\t.byte %i\n"
606             "\t.byte %i, %i, %i, %i, %i\n\n"
607             "\t.word 0\n"
608             "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
609             myHead.structure, myHead.geostype,
610             myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
611     }
612
613     fprintf (outputSFile,
614         "\t\t.segment \"FILEINFO\"\n\n"
615         "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
616         "\t.byte 3, 21, 63 | $80\n");
617
618     if (myHead.icon != NULL) {
619         fprintf (outputSFile,
620             "\t.incbin \"%s\", 0, 63\n",
621             myHead.icon);
622     } else {
623         for (i = 0; i != 63; i = i + 3) {
624             fprintf (outputSFile,
625                 "\t.byte %%%s, %%%s, %%%s\n",
626                 bintos (icon1[i], i1), bintos (icon1[i+1], i2), bintos (icon1[i+2], i3));
627         }
628     }
629
630     fprintf (outputSFile,
631         "\t.byte %i, %i, %i\n"
632         "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
633         myHead.dostype, myHead.geostype, myHead.structure);
634
635     fillOut (myHead.classname, 12, "$20");
636
637     fillOut (myHead.version, 4, "0");
638
639     fprintf (outputSFile,
640         "\t.byte 0, 0, 0\n"
641         "\t.byte %i\n\n",
642         myHead.mode);
643
644     setLen (myHead.author, 62);
645     fprintf (outputSFile,
646         "\t.byte \"%s\"\n"
647         "\t.byte 0\n"
648         "\t.res  (63 - %i)\n\n",
649         myHead.author, (int)(strlen (myHead.author) + 1));
650
651     setLen (myHead.info, 95);
652     fprintf (outputSFile,
653         "\t.byte \"%s\"\n"
654         "\t.byte 0\n\n",
655         myHead.info);
656
657     if (fclose (outputSFile) != 0) {
658         AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
659     }
660 }
661
662
663 static void DoMemory (void)
664 {
665     char *token;
666     int stacksize, overlaysize;
667     int overlaytable[127];
668     int number, lastnumber;
669     int backbuffer;
670
671     openSFile ();
672
673     stacksize = -1;
674     overlaysize = -1;
675     memset (overlaytable, 0, sizeof (overlaytable));
676     lastnumber = -1;
677     backbuffer = -1;
678
679     if (strcmp (nextWord (), "{") != 0) {
680         AbEnd ("MEMORY description has no opening bracket!");
681     }
682
683     token = NULL;
684     for (;;) {
685         if (token == NULL) token = nextWord ();
686         if (strcmp (token, "}") == 0) break;
687         switch (findToken (memFields, token)) {
688             case -1:
689                 AbEnd ("Unknown field '%s' in MEMORY description", token);
690                 break;
691
692             case 0: /* stacksize */
693                 stacksize = strtol (nextWord (), NULL, 0);
694                 token = NULL;
695                 break;
696
697             case 1: /* overlaysize */
698                 overlaysize = strtol (nextWord (), NULL, 0);
699                 token = NULL;
700                 break;
701
702             case 2: /* overlaynums */
703                 do {
704                     token = nextWord ();
705                     if (IsDigit (token[0]) == 0) break;
706
707                     number = atoi (token);
708                     if (number < 0 || number > 126) {
709                         AbEnd ("Overlay number %i is out of range 0-126", number);
710                     }
711                     if (overlaytable[number] == 1) {
712                         AbEnd ("Overlay number %i is defined twice", number);
713                     }
714
715                     overlaytable[number] = 1;
716                     if (number > lastnumber) lastnumber = number;
717
718                 } while (IsDigit (token[0]) != 0);
719
720                 if (lastnumber == -1) {
721                     AbEnd ("There must be at least one overlay number");
722                 }
723
724                 /* always include number 0 */
725                 overlaytable[0] = 1;
726                 break;
727
728             case 3: /* backbuffer */
729                 switch (findToken (toggle, nextWord ())) {
730                     case -1:
731                         AbEnd ("Unknown value for 'backbuffer'");
732                         break;
733                     case 0:
734                     case 1:
735                     case 2:
736                         backbuffer = 0;
737                         break;
738                     default:
739                         backbuffer = 1;
740                         break;
741                 }
742                 token = NULL;
743                 break;
744         }
745     }
746
747     /* OK, all information is gathered, do flushout */
748
749     if (lastnumber != -1) {
750         fprintf (outputSFile,
751             "\t\t.segment \"RECORDS\"\n\n");
752
753         if (apple == 1) {
754
755             for (number = 0; number <= lastnumber; number++) {
756                 fprintf (outputSFile,
757                     "\t.byte %s\n",
758                     overlaytable[number] == 1 ? "$00" : "$FF");
759             }
760             fprintf (outputSFile,
761                 "\n");
762
763             for (number = 0; number <= lastnumber; number++) {
764                 if (overlaytable[number] == 1) {
765                     fprintf (outputSFile,
766                         "\t\t.segment \"VLIRIDX%i\"\n\n"
767                         "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n\n"
768                         "\t.res  255\n"
769                         "\t.byte .lobyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n"
770                         "\t.res  255\n"
771                         "\t.byte .hibyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n\n",
772                         number, number, number,
773                         number == 0 ? ", __BSS_SIZE__" : "",
774                         number, number,
775                         number == 0 ? " - __BSS_SIZE__" : "",
776                         number, number,
777                         number == 0 ? " - __BSS_SIZE__" : "");
778                 }
779             }
780
781         } else {
782
783             for (number = 0; number <= lastnumber; number++) {
784                 if (overlaytable[number] == 1) {
785                     fprintf (outputSFile,
786                         "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n",
787                         number, number, number == 0 ? ", __BSS_SIZE__" : "");
788                 }
789             }
790             fprintf (outputSFile,
791                 "\n");
792
793             for (number = 0; number <= lastnumber; number++) {
794                 if (overlaytable[number] == 1) {
795                     fprintf (outputSFile,
796                         "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) /    254) + 1\n"
797                         "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) .MOD 254) + 2\n",
798                         number, number, number == 0 ? " - __BSS_SIZE__" : "",
799                         number, number, number == 0 ? " - __BSS_SIZE__" : "");
800                 } else {
801                     fprintf (outputSFile,
802                         "\t.byte $00\n"
803                         "\t.byte $FF\n");
804                 }
805             }
806             fprintf (outputSFile,
807                 "\n");
808         }
809
810         openCFile ();
811
812         fprintf (outputCFile,
813             "extern void _OVERLAYADDR__[];\n"
814             "extern void _OVERLAYSIZE__[];\n\n"
815             "#define OVERLAY_ADDR (char*)   _OVERLAYADDR__\n"
816             "#define OVERLAY_SIZE (unsigned)_OVERLAYSIZE__\n\n");
817
818         if (fclose (outputCFile) != 0) {
819             AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
820         }
821     }
822
823     if (stacksize != -1) {
824         fprintf (outputSFile,
825             "\t.export __STACKSIZE__ : absolute = $%04x\n\n",
826             stacksize);
827     }
828
829     if (overlaysize != -1) {
830         fprintf (outputSFile,
831             "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
832             overlaysize);
833     }
834
835     if (backbuffer != -1) {
836         fprintf (outputSFile,
837             "\t.export __BACKBUFSIZE__ : absolute = $%04x\n\n",
838             backbuffer ? 0x2000 : 0x0000);
839     }
840
841     if (fclose (outputSFile) != 0) {
842         AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
843     }
844 }
845
846
847 static char *filterInput (FILE *F, char *tbl)
848 {
849     /* loads file into buffer filtering it out */
850     int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
851
852     for (;;) {
853         a = getc(F);
854         if ((a == '\n') || (a == '\015')) a = ' ';
855         if (a == ',' && quote) a = ' ';
856         if (a == '\042') quote =! quote;
857         if (quote) {
858             if ((a == '{') || (a == '(')) bracket++;
859             if ((a == '}') || (a == ')')) bracket--;
860         }
861         if (a == EOF) {
862             tbl[i] = '\0';
863             xrealloc (tbl, i + 1);
864             break;
865         }
866         if (IsSpace (a)) {
867             if ((prevchar != ' ') && (prevchar != -1)) {
868                 tbl[i++] = ' ';
869                 prevchar = ' ';
870             }
871         } else {
872             if (a == ';' && quote) {
873                 do {
874                     a = getc (F);
875                 } while (a != '\n');
876                 fseek (F, -1, SEEK_CUR);
877             } else {
878                 tbl[i++] = a;
879                 prevchar = a;
880             }
881         }
882     }
883
884     if (bracket != 0) AbEnd ("There are unclosed brackets!");
885
886     return tbl;
887 }
888
889
890 static void processFile (const char *filename)
891 {
892     FILE *F;
893
894     char *str;
895     char *token;
896
897     int head = 0;   /* number of processed HEADER sections */
898     int memory = 0; /* number of processed MEMORY sections */
899
900     if ((F = fopen (filename, "r")) == 0) {
901         AbEnd ("Can't open file %s for reading: %s", filename, strerror (errno));
902     }
903
904     str = filterInput (F, xmalloc (BLOODY_BIG_BUFFER));
905
906     token = strtok (str, " ");
907
908     do {
909         if (str != NULL) {
910             switch (findToken (mainToken, token)) {
911                 case 0:
912                     DoMenu ();
913                     break;
914                 case 1:
915                     if (++head != 1) {
916                         AbEnd ("More than one HEADER section, aborting");
917                     } else {
918                         DoHeader ();
919                     }
920                     break;
921                 case 2: break; /* icon not implemented yet */
922                 case 3: break; /* dialog not implemented yet */
923                 case 4:
924                     if (++memory != 1) {
925                         AbEnd ("More than one MEMORY section, aborting");
926                     } else {
927                         DoMemory ();
928                     }
929                     break;
930                 default:
931                     AbEnd ("Unknown section '%s'", token);
932                     break;
933             }
934         }
935         token = nextWord ();
936     } while (token != NULL);
937 }
938
939
940 int main (int argc, char *argv[])
941 {
942     /* Program long options */
943     static const LongOpt OptTab[] = {
944         { "--help",    0, OptHelp},
945         { "--target",  1, OptTarget},
946         { "--version", 0, OptVersion},
947     };
948
949     unsigned ffile = 0;
950     unsigned I;
951
952     /* Initialize the cmdline module */
953     InitCmdLine (&argc, &argv, "grc65");
954
955     /* Check the parameters */
956     I = 1;
957     while (I < ArgCount) {
958
959         /* Get the argument */
960         const char* Arg = ArgVec [I];
961
962         /* Check for an option */
963         if (Arg[0] == '-') {
964             switch (Arg[1]) {
965
966                 case '-':
967                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
968                     break;
969
970                 case 'o':
971                     outputCName = GetArg (&I, 2);
972                     break;
973
974                 case 's':
975                     outputSName = GetArg (&I, 2);
976                     break;
977
978                 case 't':
979                     OptTarget (Arg, GetArg (&I, 2));
980                     break;
981
982                 case 'h':
983                 case '?':
984                     OptHelp (Arg, 0);
985                     break;
986
987                 case 'V':
988                     OptVersion (Arg, 0);
989                     break;
990
991                 default:
992                     UnknownOption (Arg);
993             }
994
995         } else {
996             ffile++;
997
998             if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
999             if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
1000
1001             processFile (Arg);
1002         }
1003
1004         /* Next argument */
1005         ++I;
1006     }
1007
1008     if (ffile == 0) AbEnd ("No input file");
1009
1010     return EXIT_SUCCESS;
1011 }