]> git.sur5r.net Git - cc65/blob - src/grc65/main.c
Change option processing and options to be more compatible with the other
[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", "VLIR", ""};
68
69 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
70                            "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
71
72 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
73
74 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
75
76 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
77
78 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
79
80 const int BSWTab[] = {0, 0x005, 0x007, 0x00b, 0x011, 0x017, 0x01d, 0x023,
81     0x025, 0x029, 0x02d, 0x033, 0x039, 0x03c, 0x041, 0x043, 0x04a, 0x04f,
82     0x052, 0x056, 0x05a, 0x05f, 0x063, 0x068, 0x06d, 0x072, 0x077, 0x079,
83     0x07c, 0x080, 0x084, 0x088, 0x08e, 0x094, 0x09a, 0x09f, 0x0a4, 0x0a9,
84     0x0ad, 0x0b1, 0x0b6, 0x0bc, 0x0be, 0x0c2, 0x0c8, 0x0cc, 0x0d4, 0x0da,
85     0x0e0, 0x0e5, 0x0eb, 0x0f0, 0x0f5, 0x0f9, 0x0fe, 0x104, 0x10c, 0x112,
86     0x118, 0x11e, 0x121, 0x129, 0x12c, 0x132, 0x13a, 0x13e, 0x143, 0x148,
87     0x14d, 0x152, 0x157, 0x15a, 0x15f, 0x164, 0x166, 0x168, 0x16d, 0x16f,
88     0x177, 0x17c, 0x182, 0x187, 0x18c, 0x18f, 0x193, 0x196, 0x19b, 0x1a1,
89     0x1a9, 0x1af, 0x1b4, 0x1ba, 0x1be, 0x1c0, 0x1c4, 0x1ca, 0x1d2, 0x1dd};
90
91 const unsigned char icon1[] = {255, 255, 255, 128,   0,   1, 128,   0,   1,
92                                128,   0,   1, 128,   0,   1, 128,   0,   1,
93                                128,   0,   1, 128,   0,   1, 128,   0,   1,
94                                128,   0,   1, 128,   0,   1, 128,   0,   1,
95                                128,   0,   1, 128,   0,   1, 128,   0,   1,
96                                128,   0,   1, 128,   0,   1, 128,   0,   1,
97                                128,   0,   1, 128,   0,   1, 255, 255, 255};
98
99 const char *outputCName = NULL;
100 const char *outputSName = NULL;
101 FILE *outputCFile, *outputSFile;
102 int CFnum = 0, SFnum = 0;
103 int apple = 0;
104 char outputCMode[2] = "w";
105 char outputSMode[2] = "w";
106
107
108 static void Usage (void) {
109
110     printf ("Usage: %s [options] file\n"
111             "Short options:\n"
112             "  -V\t\t\tPrint the version number\n"
113             "  -h\t\t\tHelp (this text)\n"
114             "  -o name\t\tName the C output file\n"
115             "  -s name\t\tName the asm output file\n"
116             "  -t sys\t\tSet the target system\n"
117             "\n"
118             "Long options:\n"
119             "  --help\t\tHelp (this text)\n"
120             "  --target sys\t\tSet the target system\n"
121             "  --version\t\tPrint the version number\n",
122             ProgName);
123 }
124
125
126 static void OptHelp (const char* Opt attribute ((unused)),
127                      const char* Arg attribute ((unused)))
128 /* Print usage information and exit */
129 {
130     Usage ();
131     exit (EXIT_SUCCESS);
132 }
133
134
135
136 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
137 /* Set the target system */
138 {
139     switch (FindTarget(Arg)) {
140
141         case TGT_GEOS_CBM:
142             apple = 0;
143             break;
144
145         case TGT_GEOS_APPLE:
146             apple = 1;
147             break;
148
149         case TGT_UNKNOWN:
150             AbEnd ("Unknown target system `%s'", Arg);
151             break;
152
153         default:
154             /* Target is known but unsupported */
155             AbEnd ("Unsupported target system `%s'", Arg);
156             break;
157
158     }
159 }
160
161
162
163 static void OptVersion (const char* Opt attribute ((unused)),
164                         const char* Arg attribute ((unused)))
165 /* Print the assembler version */
166 {
167     fprintf (stderr,
168              "grc65 V%s - (C) Copyright, Maciej 'YTM/Elysium' Witkowiak\n",
169              GetVersionAsString ());
170 }
171
172
173
174 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 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 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 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 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 char *nextPhrase() {
244     return strtok(NULL, "\"");
245 }
246
247
248 char *nextWord() {
249     return strtok(NULL, " ");
250 }
251
252
253 void setLen(char *name, unsigned len) {
254     if (strlen(name) > len)
255         name[len] = '\0';
256 }
257
258
259 void fillOut(char *name, int len, char *filler) {
260
261     int a;
262
263     setLen(name, len);
264     fprintf(outputSFile, "\t.byte \"%s\"\n", name);
265
266     a = strlen(name);
267     if (a < len) {
268         fprintf(outputSFile, "\t.res  (%i - %i), %s\n", len, a, filler);
269     }
270 }
271
272
273 char *bintos(unsigned char a, char out[7]) {
274
275     int i=0;
276
277     for (; i < 8; i++) {
278         out[7 - i] = ((a & 1) == 0) ? '0' : '1';
279         a = a >> 1;
280     }
281     out[i] = '\0';
282
283     return out;
284 }
285
286
287 int getNameSize(const char *word) {
288
289     /* count length of a word using BSW 9 font table */
290     int a = 0, i = 0;
291
292     while (word[i] != '\0') {
293         a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
294         i++;
295     }
296
297     return a;
298 }
299
300
301 void DoMenu(void) {
302
303     int a, size, tmpsize, item = 0;
304     char *token;
305     char namebuff[255] = "";
306     struct menu myMenu;
307     struct menuitem *curItem, *newItem;
308
309     openCFile();
310
311     myMenu.name = nextWord();
312     myMenu.left = atoi(nextWord());
313     myMenu.top = atoi(nextWord());
314     myMenu.type = nextWord();
315
316     if (strcmp(nextWord(), "{") != 0) {
317         AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
318     }
319     curItem = xmalloc(sizeof(struct menuitem));
320     myMenu.item = curItem;
321     do {
322         token = nextWord();
323         if (strcmp(token, "}") == 0) break;
324         if (token[strlen(token) - 1] != '"') {
325             strcpy(namebuff, token);
326             do {
327                 token = nextWord();
328                 strcat(namebuff, " ");
329                 strcat(namebuff, token);
330             } while (token[strlen(token) - 1] != '"');
331             token = xmalloc(strlen(namebuff));
332             strcpy(token, namebuff);
333         }
334         curItem->name = token;
335         curItem->type = nextWord();
336         curItem->target = nextWord();
337         newItem = xmalloc(sizeof(struct menuitem));
338         curItem->next = newItem;
339         curItem = newItem;
340         item++;
341         } while (strcmp(token, "}") != 0);
342     if (item == 0) AbEnd ("Menu '%s' has 0 items!", myMenu.name);
343     if (item > 31) AbEnd ("Menu '%s' has too many items!", myMenu.name);
344
345     curItem->next = NULL;
346
347     /* count menu sizes */
348     size = 0;
349     curItem = myMenu.item;
350     if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
351         /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
352         myMenu.bot = myMenu.top + 15;
353         for (a = 0; a != item; a++) {
354             size += getNameSize(curItem->name);
355             curItem = curItem->next;
356         }
357     } else {
358         /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
359         myMenu.bot = myMenu.top + (14 * item);
360         for (a = 0; a != item; a++) {
361             tmpsize = getNameSize(curItem->name);
362             size = (size > tmpsize) ? size : tmpsize;
363             curItem = curItem->next;
364         }
365     }
366     myMenu.right = myMenu.left + size - 1;
367
368     curItem = myMenu.item;
369     for (a = 0; a != item; a++) {
370         /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
371         if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
372             fprintf(outputCFile,
373                 "void %s (void);\n",
374                 curItem->target);
375         }
376         curItem=curItem->next;
377     }
378
379     fprintf(outputCFile,
380         "\n"
381         "const void %s = {\n"
382         "\t(char)%i, (char)%i,\n"
383         "\t(int)%i, (int)%i,\n"
384         "\t(char)(%i | %s),\n",
385         myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
386
387     curItem = myMenu.item;
388     for (a = 0; a != item; a++) {
389         fprintf(outputCFile,
390             "\t%s, (char)%s, (int)",
391             curItem->name, curItem->type);
392         if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
393             fprintf(outputCFile,
394                 "&");
395         fprintf(outputCFile,
396             "%s,\n",
397             curItem->target);
398         curItem = curItem->next;
399     }
400
401     fprintf(outputCFile,
402         "};\n\n");
403
404     if (fclose(outputCFile) != 0)
405         AbEnd ("Error closing %s: %s", outputCName, strerror(errno));
406 }
407
408
409 void DoHeader(void) {
410
411     time_t t;
412     struct tm *my_tm;
413
414     struct appheader myHead;
415     char *token;
416     char i1[9], i2[9], i3[9];
417     int a, b;
418
419     openSFile();
420
421     token = nextWord();
422
423     a = findToken(hdrFTypes, token);
424
425     if (apple == 1) {
426         switch (a) {
427             case 0:
428                 myHead.geostype = 0x82;
429                 break;
430             default:
431                 AbEnd ("Filetype '%s' is not supported yet", token);
432         }
433     } else {
434         switch (a) {
435             case 0:
436                 myHead.geostype = 6;
437                 break;
438             case 1:
439                 myHead.geostype = 14;
440                 break;
441             default:
442                 AbEnd ("Filetype '%s' is not supported yet", token);
443         }
444     }
445
446     myHead.dosname = nextPhrase();
447     nextPhrase();
448     myHead.classname = nextPhrase();
449     nextPhrase();
450     myHead.version = nextPhrase();
451
452     /* put default values into myHead here */
453     myHead.author = "cc65";
454     myHead.info = "Program compiled with cc65 and GEOSLib.";
455     myHead.dostype = 128;
456     if (apple == 0) myHead.dostype += 3;
457     myHead.structure = 0;
458     myHead.mode = 0;
459     myHead.icon = NULL;
460
461     t = time(NULL);
462     my_tm = localtime(&t);
463
464     myHead.year = my_tm->tm_year;
465     myHead.month = my_tm->tm_mon+1;
466     myHead.day = my_tm->tm_mday;
467     myHead.hour = my_tm->tm_hour;
468     myHead.min = my_tm->tm_min;
469
470     if (strcmp(nextWord(), "{") != 0) {
471         AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
472     }
473
474     do {
475         token = nextWord();
476         if (strcmp(token, "}") == 0) break;
477         switch (a = findToken(hdrFields, token)) {
478             case -1:
479                 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
480                 break;
481             case 0: /* author */
482                 myHead.author = nextPhrase();
483                 break;
484             case 1: /* info */
485                 myHead.info = nextPhrase();
486                 break;
487             case 2: /* date */
488                 myHead.year = atoi(nextWord());
489                 myHead.month = atoi(nextWord());
490                 myHead.day = atoi(nextWord());
491                 myHead.hour = atoi(nextWord());
492                 myHead.min = atoi(nextWord());
493                 break;
494             case 3: /* dostype */
495                 switch (b = findToken(hdrDOSTp, nextWord())) {
496                     case -1:
497                         AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
498                         break;
499                     default:
500                         if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
501                         break;
502                 }
503                 break;
504             case 4: /* mode */
505                 switch (b = findToken(hdrModes, nextWord())) {
506                     case -1:
507                         AbEnd ("Unknown mode in header '%s'", myHead.dosname);
508                     case 0:
509                         if (apple == 0) myHead.mode = 0x40;
510                         break;
511                     case 1:
512                         if (apple == 0) myHead.mode = 0x00;
513                         break;
514                     case 2:
515                         if (apple == 0) myHead.mode = 0xc0;
516                         break;
517                     case 3:
518                         if (apple == 0) myHead.mode = 0x80;
519                         break;
520                 }
521                 break;
522             case 5: /* structure */
523                 switch (b = findToken(hdrStructTp, nextWord())) {
524                     case -1:
525                         AbEnd ("unknown structure type in header '%s'", myHead.dosname);
526                     case 0:
527                     case 1:
528                         myHead.structure = 0;
529                         break;
530                     case 2:
531                     case 3:
532                         myHead.structure = 1;
533                         break;
534                 }
535                 break;
536             case 6: /* icon */
537                 myHead.icon = nextPhrase();
538                 break;
539         }
540
541     } while (strcmp(token, "}") != 0);
542
543     /* OK, all information is gathered, do flushout */
544
545     fprintf(outputSFile,
546         "\n"
547         "\t\t.segment \"DIRENTRY\"\n\n");
548
549     if (apple == 1) {
550
551         fprintf(outputSFile,
552             "\t.byte %i << 4 | %u\n",
553             myHead.structure + 2, (unsigned) strlen(myHead.dosname));
554
555         fillOut(myHead.dosname, 15, "0");
556
557         fprintf(outputSFile,
558             "\t.byte $%02x\n"
559             "\t.word 0\n"
560             "\t.word 0\n"
561             "\t.byte 0, 0, 0\n"
562             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
563             "\t.byte 0\n"
564             "\t.byte 0\n"
565             "\t.byte 0\n"
566             "\t.word 0\n"
567             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
568             "\t.word 0\n",
569             myHead.geostype,
570             myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min,
571             myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min);
572
573     } else {
574
575         fprintf(outputSFile,
576             "\t.byte %i\n"
577             "\t.word 0\n",
578             myHead.dostype);
579
580         fillOut(myHead.dosname, 16, "$a0");
581
582         fprintf(outputSFile,
583             "\t.word 0\n"
584             "\t.byte %i\n"
585             "\t.byte %i\n"
586             "\t.byte %i, %i, %i, %i, %i\n\n"
587             "\t.word 0\n"
588             "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
589             myHead.structure, myHead.geostype,
590             myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
591     }
592
593     fprintf(outputSFile,
594         "\n"
595         "\t\t.segment \"FILEINFO\"\n\n"
596         "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
597         "\t.byte 3, 21, 63 | $80\n");
598
599     if (myHead.icon != NULL) {
600         fprintf(outputSFile,
601             "\t.incbin \"%s\", 0, 63\n",
602             myHead.icon);
603     } else {
604         for (a = 0; a != 63; a = a + 3) {
605             fprintf(outputSFile,
606                 "\t.byte %%%s, %%%s, %%%s\n",
607                 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
608         }
609     }
610
611     fprintf(outputSFile,
612         "\t.byte %i, %i, %i\n"
613         "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
614         myHead.dostype, myHead.geostype, myHead.structure);
615
616     fillOut(myHead.classname, 12, "$20");
617
618     fillOut(myHead.version, 4, "0");
619
620     fprintf(outputSFile,
621         "\t.byte 0, 0, 0\n"
622         "\t.byte %i\n\n",
623         myHead.mode);
624
625     setLen(myHead.author, 62);
626     fprintf(outputSFile,
627         "\t.byte \"%s\"\n"
628         "\t.byte 0\n"
629         "\t.res  (63 - %i)\n\n",
630         myHead.author, (int)(strlen(myHead.author) + 1));
631
632     setLen(myHead.info, 95);
633     fprintf(outputSFile,
634         "\t.byte \"%s\"\n"
635         "\t.byte 0\n\n",
636         myHead.info);
637
638     if (fclose (outputSFile) != 0)
639         AbEnd ("Error closing %s: %s", outputSName, strerror(errno));
640 }
641
642
643 void DoVLIR(void) {
644
645     char *token;
646     int record, lastrecord;
647     int vlirsize, vlirtable[127];
648
649     openSFile();
650
651     vlirsize = strtol(nextWord(), NULL, 0);
652
653     if (strcmp(nextWord(), "{") != 0) {
654         AbEnd ("VLIR description has no opening bracket!");
655     }
656
657     lastrecord = -1;
658     memset(vlirtable, 0, sizeof(vlirtable));
659
660     do {
661         token = nextWord();
662         if (strcmp(token, "}") == 0) break;
663
664         record = atoi(token);
665         if (record < 0 || record > 126) {
666             AbEnd ("VLIR record %i is out of range 0-126.", record);
667         }
668         if (vlirtable[record] == 1) {
669             AbEnd ("VLIR record %i is defined twice.", record);
670         }
671
672         vlirtable[record] = 1;
673         if (record > lastrecord) lastrecord = record;
674     } while (strcmp(token, "}") != 0);
675
676     if (lastrecord == -1) {
677         AbEnd ("There must be at least one VLIR record.");
678     }
679
680     /* always include record 0 */
681     vlirtable[0] = 1;
682
683     /* OK, all information is gathered, do flushout */
684
685     fprintf(outputSFile,
686         "\n"
687         "\t\t.segment \"RECORDS\"\n\n"
688         "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
689         vlirsize);
690
691     for (record = 0; record <= lastrecord; record++) {
692         if (vlirtable[record] == 1) {
693             fprintf(outputSFile,
694                 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
695                 record, record);
696         }
697     }
698     fprintf(outputSFile,
699         "\n");
700
701     for (record = 0; record <= lastrecord; record++) {
702         if (vlirtable[record] == 1) {
703             fprintf(outputSFile,
704                 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) /    254) + 1\n"
705                 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
706                 record, record, record, record);
707         } else {
708             fprintf(outputSFile,
709                 "\t.byte $00\n"
710                 "\t.byte $FF\n");
711         }
712     }
713     fprintf(outputSFile,
714         "\n");
715
716     if (fclose(outputSFile) != 0)
717         AbEnd ("Error closing %s: %s", outputSName, strerror(errno));
718
719     openCFile();
720
721     fprintf(outputCFile,
722         "extern void _OVERLAYADDR__;\n"
723         "extern void _OVERLAYSIZE__;\n\n"
724         "#define OVERLAY_ADDR (char*)   &_OVERLAYADDR__\n"
725         "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
726
727     if (fclose(outputCFile) != 0)
728         AbEnd ("Error closing %s: %s", outputCName, strerror(errno));
729 }
730
731
732 char *filterInput(FILE *F, char *tbl) {
733
734     /* loads file into buffer filtering it out */
735     int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
736
737     while (1) {
738         a = getc(F);
739         if ((a == '\n') || (a == '\015')) a = ' ';
740         if (a == ',' && quote) a = ' ';
741         if (a == '\042') quote =! quote;
742         if (quote) {
743             if ((a == '{') || (a == '(')) bracket++;
744             if ((a == '}') || (a == ')')) bracket--;
745         }
746         if (a == EOF) {
747             tbl[i] = '\0';
748             xrealloc(tbl, i + 1);
749             break;
750         }
751         if (IsSpace(a)) {
752             if ((prevchar != ' ') && (prevchar != -1)) {
753                 tbl[i++] = ' ';
754                 prevchar = ' ';
755             }
756         } else {
757             if (a == ';' && quote) {
758                 do {
759                     a = getc(F);
760                 } while (a != '\n');
761                 fseek(F, -1, SEEK_CUR);
762             } else {
763                 tbl[i++] = a;
764                 prevchar = a;
765             }
766         }
767     }
768
769     if (bracket != 0) AbEnd ("There are unclosed brackets!");
770
771     return tbl;
772 }
773
774
775 void processFile(const char *filename) {
776
777     FILE *F;
778
779     char *str;
780     char *token;
781
782     int head = 0; /* number of processed HEADER sections */
783     int vlir = 0; /* number of processed VLIR sections */
784
785     if ((F = fopen(filename, "r")) == 0) {
786         AbEnd ("Can't open file %s for reading: %s", filename, strerror(errno));
787     }
788
789     str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
790
791     token = strtok(str, " ");
792
793     do {
794         if (str != NULL) {
795             switch (findToken(mainToken, token)) {
796                 case 0:
797                     DoMenu();
798                     break;
799                 case 1:
800                     if (++head != 1) {
801                         AbEnd ("More than one HEADER section, aborting.");
802                     } else {
803                         DoHeader();
804                     }
805                     break;
806                 case 2: break; /* icon not implemented yet */
807                 case 3: break; /* dialog not implemented yet */
808                 case 4:
809                     if (++vlir != 1) {
810                         AbEnd ("More than one VLIR section, aborting.");
811                     } else {
812                         DoVLIR();
813                     }
814                     break;
815                 default:
816                     AbEnd ("Unknown section %s.",token);
817                     break;
818             }
819         }
820         token = nextWord();
821     } while (token != NULL);
822 }
823
824
825 int main(int argc, char *argv[]) {
826
827     /* Program long options */
828     static const LongOpt OptTab[] = {
829         { "--help",             0,      OptHelp                 },
830         { "--target",           1,      OptTarget               },
831         { "--version",          0,      OptVersion              },
832     };
833
834     unsigned ffile = 0;
835     unsigned I;
836
837
838     /* Initialize the cmdline module */
839     InitCmdLine (&argc, &argv, "grc65");
840
841     /* Check the parameters */
842     I = 1;
843     while (I < ArgCount) {
844
845         /* Get the argument */
846         const char* Arg = ArgVec [I];
847
848         /* Check for an option */
849         if (Arg[0] == '-') {
850             switch (Arg[1]) {
851
852                 case '-':
853                     LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
854                     break;
855
856                 case 'o':
857                     outputCName = GetArg (&I, 2);
858                     break;
859
860                 case 's':
861                     outputSName = GetArg (&I, 2);
862                     break;
863
864                 case 't':
865                     OptTarget (Arg, GetArg (&I, 2));
866                     break;
867
868                 case 'h':
869                 case '?':
870                     OptHelp (Arg, 0);
871                     break;
872
873                 case 'V':
874                     OptVersion (Arg, 0);
875                     break;
876
877                 default:
878                     UnknownOption (Arg);
879             }
880
881         } else {
882             ffile++;
883
884             if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
885             if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
886
887             processFile(Arg);
888         }
889
890         /* Next argument */
891         ++I;
892     }
893
894     if (ffile == 0) AbEnd ("No input file");
895
896     return EXIT_SUCCESS;
897 }