]> git.sur5r.net Git - cc65/blob - src/grc65/main.c
dc281c593e23beeca1886bc5c4bd5028ad6a3b3e
[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 "fname.h"
26 #include "abend.h"
27 #include "chartype.h"
28 #include "target.h"
29 #include "xmalloc.h"
30
31 /* I hope that no one will be able to create a .grc bigger than this... */
32 #define BLOODY_BIG_BUFFER 65000
33
34
35
36 struct menuitem {
37     char *name;
38     char *type;
39     char *target;
40     struct menuitem *next;
41 };
42
43 struct menu {
44     char *name;
45     int top, left;
46     int bot, right;
47     char *type;
48     struct menuitem *item;
49 };
50
51 struct appheader {
52     int year, month, day, hour, min;
53     int mode;
54     int dostype;
55     int geostype;
56     int structure;
57     char *dosname;
58     char *classname;
59     char *version;
60     char *author;
61     char *info;
62     char *icon;
63 };
64
65 const char *mainToken[] = {"MENU", "HEADER", "ICON", "DIALOG", "VLIR", ""};
66
67 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
68                            "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
69
70 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
71
72 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
73
74 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
75
76 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
77
78 const int BSWTab[] = {0, 0x005, 0x007, 0x00b, 0x011, 0x017, 0x01d, 0x023,
79     0x025, 0x029, 0x02d, 0x033, 0x039, 0x03c, 0x041, 0x043, 0x04a, 0x04f,
80     0x052, 0x056, 0x05a, 0x05f, 0x063, 0x068, 0x06d, 0x072, 0x077, 0x079,
81     0x07c, 0x080, 0x084, 0x088, 0x08e, 0x094, 0x09a, 0x09f, 0x0a4, 0x0a9,
82     0x0ad, 0x0b1, 0x0b6, 0x0bc, 0x0be, 0x0c2, 0x0c8, 0x0cc, 0x0d4, 0x0da,
83     0x0e0, 0x0e5, 0x0eb, 0x0f0, 0x0f5, 0x0f9, 0x0fe, 0x104, 0x10c, 0x112,
84     0x118, 0x11e, 0x121, 0x129, 0x12c, 0x132, 0x13a, 0x13e, 0x143, 0x148,
85     0x14d, 0x152, 0x157, 0x15a, 0x15f, 0x164, 0x166, 0x168, 0x16d, 0x16f,
86     0x177, 0x17c, 0x182, 0x187, 0x18c, 0x18f, 0x193, 0x196, 0x19b, 0x1a1,
87     0x1a9, 0x1af, 0x1b4, 0x1ba, 0x1be, 0x1c0, 0x1c4, 0x1ca, 0x1d2, 0x1dd};
88
89 const unsigned char icon1[] = {255, 255, 255, 128,   0,   1, 128,   0,   1,
90                                128,   0,   1, 128,   0,   1, 128,   0,   1,
91                                128,   0,   1, 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, 255, 255, 255};
96
97 char *ProgName; /* for AbEnd, later remove and use common/cmdline.h */
98
99 char *outputCName = NULL, *outputSName = NULL;
100 FILE *outputCFile, *outputSFile;
101 int CFnum = 0, SFnum = 0;
102 int apple = 0;
103 char outputCMode[2] = "w";
104 char outputSMode[2] = "w";
105
106
107 void printUsage(void) {
108
109     printf("Usage: %s [options] file\n"
110            "Options:\n"
111            "\t-h, -?\t\tthis help\n"
112            "\t-o name\t\tname C output file\n"
113            "\t-s name\t\tname asm output file\n"
114            "\t-t sys\t\tset target system\n",
115            ProgName);
116 }
117
118
119 void printCHeader(void) {
120
121     fprintf(outputCFile,
122         "//\n"
123         "//\tThis file was generated by the GEOS Resource Compiler\n"
124         "//\n"
125         "//\tDO NOT EDIT! Any changes will be lost!\n"
126         "//\n"
127         "//\tEdit proper resource file instead.\n"
128         "//\n\n");
129 }
130
131
132 void printSHeader(void) {
133
134     fprintf(outputSFile,
135         ";\n"
136         ";\tThis file was generated by the GEOS Resource Compiler\n"
137         ";\n"
138         ";\tDO NOT EDIT! Any changes will be lost!\n"
139         ";\n"
140         ";\tEdit proper resource file instead.\n"
141         ";\n\n");
142 }
143
144
145 void openCFile(void) {
146
147     if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
148         AbEnd("can't open file %s for writing: %s\n", outputCName, strerror(errno));
149     }
150
151     if (CFnum == 0) {
152         outputCMode[0] = 'a';
153         printCHeader();
154         CFnum++;
155     }
156 }
157
158
159 void openSFile(void) {
160
161     if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
162         AbEnd("can't open file %s for writing: %s\n", outputSName, strerror(errno));
163     }
164
165     if (SFnum == 0) {
166         outputSMode[0] = 'a';
167         printSHeader();
168         SFnum++;
169     }
170 }
171
172
173 int findToken(const char **tokenTbl, const char *token) {
174
175     /* takes as input table of tokens and token, returns position in table or -1 if not found */
176     int a = 0;
177
178     while (strlen(tokenTbl[a]) != 0) {
179         if (strcmp(tokenTbl[a], token) == 0) break;
180         a++;
181     }
182
183     if (strlen(tokenTbl[a]) == 0) a = -1;
184     return a;
185 }
186
187
188 char *nextPhrase() {
189     return strtok(NULL, "\"");
190 }
191
192
193 char *nextWord() {
194     return strtok(NULL, " ");
195 }
196
197
198 void setLen(char *name, unsigned len) {
199     if (strlen(name) > len)
200         name[len] = '\0';
201 }
202
203
204 void fillOut(char *name, int len, char *filler) {
205
206     int a;
207
208     setLen(name, len);
209     fprintf(outputSFile, "\t.byte \"%s\"\n", name);
210
211     a = strlen(name);
212     if (a < len) {
213         fprintf(outputSFile, "\t.res  (%i - %i), %s\n", len, a, filler);
214     }
215 }
216
217
218 char *bintos(unsigned char a, char out[7]) {
219
220     int i=0;
221
222     for (; i < 8; i++) {
223         out[7 - i] = ((a & 1) == 0) ? '0' : '1';
224         a = a >> 1;
225     }
226     out[i] = '\0';
227
228     return out;
229 }
230
231
232 int getNameSize(const char *word) {
233
234     /* count length of a word using BSW 9 font table */
235     int a = 0, i = 0;
236
237     while (word[i] != '\0') {
238         a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
239         i++;
240     }
241
242     return a;
243 }
244
245
246 void DoMenu(void) {
247
248     int a, size, tmpsize, item = 0;
249     char *token;
250     char namebuff[255] = "";
251     struct menu myMenu;
252     struct menuitem *curItem, *newItem;
253
254     openCFile();
255
256     myMenu.name = nextWord();
257     myMenu.left = atoi(nextWord());
258     myMenu.top = atoi(nextWord());
259     myMenu.type = nextWord();
260
261     if (strcmp(nextWord(), "{") != 0) {
262         AbEnd("menu '%s' description has no opening bracket!\n", myMenu.name);
263     }
264     curItem = xmalloc(sizeof(struct menuitem));
265     myMenu.item = curItem;
266     do {
267         token = nextWord();
268         if (strcmp(token, "}") == 0) break;
269         if (token[strlen(token) - 1] != '"') {
270             strcpy(namebuff, token);
271             do {
272                 token = nextWord();
273                 strcat(namebuff, " ");
274                 strcat(namebuff, token);
275             } while (token[strlen(token) - 1] != '"');
276             token = xmalloc(strlen(namebuff));
277             strcpy(token, namebuff);
278         }
279         curItem->name = token;
280         curItem->type = nextWord();
281         curItem->target = nextWord();
282         newItem = xmalloc(sizeof(struct menuitem));
283         curItem->next = newItem;
284         curItem = newItem;
285         item++;
286         } while (strcmp(token, "}") != 0);
287     if (item == 0) AbEnd("menu '%s' has 0 items!\n", myMenu.name);
288     if (item > 31) AbEnd("menu '%s' has too many items!\n", myMenu.name);
289
290     curItem->next = NULL;
291
292     /* count menu sizes */
293     size = 0;
294     curItem = myMenu.item;
295     if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
296         /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
297         myMenu.bot = myMenu.top + 15;
298         for (a = 0; a != item; a++) {
299             size += getNameSize(curItem->name);
300             curItem = curItem->next;
301         }
302     } else {
303         /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
304         myMenu.bot = myMenu.top + (14 * item);
305         for (a = 0; a != item; a++) {
306             tmpsize = getNameSize(curItem->name);
307             size = (size > tmpsize) ? size : tmpsize;
308             curItem = curItem->next;
309         }
310     }
311     myMenu.right = myMenu.left + size - 1;
312
313     curItem = myMenu.item;
314     for (a = 0; a != item; a++) {
315         /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
316         if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
317             fprintf(outputCFile,
318                 "void %s (void);\n",
319                 curItem->target);
320         }
321         curItem=curItem->next;
322     }
323
324     fprintf(outputCFile,
325         "\n"
326         "const void %s = {\n"
327         "\t(char)%i, (char)%i,\n"
328         "\t(int)%i, (int)%i,\n"
329         "\t(char)(%i | %s),\n",
330         myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
331
332     curItem = myMenu.item;
333     for (a = 0; a != item; a++) {
334         fprintf(outputCFile,
335             "\t%s, (char)%s, (int)",
336             curItem->name, curItem->type);
337         if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
338             fprintf(outputCFile,
339                 "&");
340         fprintf(outputCFile,
341             "%s,\n",
342             curItem->target);
343         curItem = curItem->next;
344     }
345
346     fprintf(outputCFile,
347         "};\n\n");
348
349     if (fclose(outputCFile) != 0)
350         AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
351 }
352
353
354 void DoHeader(void) {
355
356     time_t t;
357     struct tm *my_tm;
358
359     struct appheader myHead;
360     char *token;
361     char i1[9], i2[9], i3[9];
362     int a, b;
363
364     openSFile();
365
366     token = nextWord();
367
368     a = findToken(hdrFTypes, token);
369
370     if (apple == 1) {
371         switch (a) {
372             case 0:
373                 myHead.geostype = 0x82;
374                 break;
375             default:
376                 AbEnd("filetype '%s' is not supported yet\n", token);
377         }
378     } else {
379         switch (a) {
380             case 0:
381                 myHead.geostype = 6;
382                 break;
383             case 1:
384                 myHead.geostype = 14;
385                 break;
386             default:
387                 AbEnd("filetype '%s' is not supported yet\n", token);
388         }
389     }
390
391     myHead.dosname = nextPhrase();
392     nextPhrase();
393     myHead.classname = nextPhrase();
394     nextPhrase();
395     myHead.version = nextPhrase();
396
397     /* put default values into myHead here */
398     myHead.author = "cc65";
399     myHead.info = "Program compiled with cc65 and GEOSLib.";
400     myHead.dostype = 128;
401     if (apple == 0) myHead.dostype += 3;
402     myHead.structure = 0;
403     myHead.mode = 0;
404     myHead.icon = NULL;
405
406     t = time(NULL);
407     my_tm = localtime(&t);
408
409     myHead.year = my_tm->tm_year;
410     myHead.month = my_tm->tm_mon+1;
411     myHead.day = my_tm->tm_mday;
412     myHead.hour = my_tm->tm_hour;
413     myHead.min = my_tm->tm_min;
414
415     if (strcmp(nextWord(), "{") != 0) {
416         AbEnd("header '%s' has no opening bracket!\n", myHead.dosname);
417     }
418
419     do {
420         token = nextWord();
421         if (strcmp(token, "}") == 0) break;
422         switch (a = findToken(hdrFields, token)) {
423             case -1:
424                 AbEnd("unknown field '%s' in header '%s'\n", token, myHead.dosname);
425                 break;
426             case 0: /* author */
427                 myHead.author = nextPhrase();
428                 break;
429             case 1: /* info */
430                 myHead.info = nextPhrase();
431                 break;
432             case 2: /* date */
433                 myHead.year = atoi(nextWord());
434                 myHead.month = atoi(nextWord());
435                 myHead.day = atoi(nextWord());
436                 myHead.hour = atoi(nextWord());
437                 myHead.min = atoi(nextWord());
438                 break;
439             case 3: /* dostype */
440                 switch (b = findToken(hdrDOSTp, nextWord())) {
441                     case -1:
442                         AbEnd("unknown dostype in header '%s'\n", myHead.dosname);
443                         break;
444                     default:
445                         if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
446                         break;
447                 }
448                 break;
449             case 4: /* mode */
450                 switch (b = findToken(hdrModes, nextWord())) {
451                     case -1:
452                         AbEnd("unknown mode in header '%s'\n", myHead.dosname);
453                     case 0:
454                         if (apple == 0) myHead.mode = 0x40;
455                         break;
456                     case 1:
457                         if (apple == 0) myHead.mode = 0x00;
458                         break;
459                     case 2:
460                         if (apple == 0) myHead.mode = 0xc0;
461                         break;
462                     case 3:
463                         if (apple == 0) myHead.mode = 0x80;
464                         break;
465                 }
466                 break;
467             case 5: /* structure */
468                 switch (b = findToken(hdrStructTp, nextWord())) {
469                     case -1:
470                         AbEnd("unknown structure type in header '%s'\n", myHead.dosname);
471                     case 0:
472                     case 1:
473                         myHead.structure = 0;
474                         break;
475                     case 2:
476                     case 3:
477                         myHead.structure = 1;
478                         break;
479                 }
480                 break;
481             case 6: /* icon */
482                 myHead.icon = nextPhrase();
483                 break;
484         }
485
486     } while (strcmp(token, "}") != 0);
487
488     /* OK, all information is gathered, do flushout */
489
490     fprintf(outputSFile,
491         "\n"
492         "\t\t.segment \"DIRENTRY\"\n\n");
493
494     if (apple == 1) {
495
496         fprintf(outputSFile,
497             "\t.byte %i << 4 | %u\n",
498             myHead.structure + 2, (unsigned) strlen(myHead.dosname));
499
500         fillOut(myHead.dosname, 15, "0");
501
502         fprintf(outputSFile,
503             "\t.byte $%02x\n"
504             "\t.word 0\n"
505             "\t.word 0\n"
506             "\t.byte 0, 0, 0\n"
507             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
508             "\t.byte 0\n"
509             "\t.byte 0\n"
510             "\t.byte 0\n"
511             "\t.word 0\n"
512             "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
513             "\t.word 0\n",
514             myHead.geostype,
515             myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min,
516             myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min);
517
518     } else {
519
520         fprintf(outputSFile,
521             "\t.byte %i\n"
522             "\t.word 0\n",
523             myHead.dostype);
524
525         fillOut(myHead.dosname, 16, "$a0");
526
527         fprintf(outputSFile,
528             "\t.word 0\n"
529             "\t.byte %i\n"
530             "\t.byte %i\n"
531             "\t.byte %i, %i, %i, %i, %i\n\n"
532             "\t.word 0\n"
533             "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
534             myHead.structure, myHead.geostype,
535             myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
536     }
537
538     fprintf(outputSFile,
539         "\n"
540         "\t\t.segment \"FILEINFO\"\n\n"
541         "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
542         "\t.byte 3, 21, 63 | $80\n");
543
544     if (myHead.icon != NULL) {
545         fprintf(outputSFile,
546             "\t.incbin \"%s\", 0, 63\n",
547             myHead.icon);
548     } else {
549         for (a = 0; a != 63; a = a + 3) {
550             fprintf(outputSFile,
551                 "\t.byte %%%s, %%%s, %%%s\n",
552                 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
553         }
554     }
555
556     fprintf(outputSFile,
557         "\t.byte %i, %i, %i\n"
558         "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
559         myHead.dostype, myHead.geostype, myHead.structure);
560
561     fillOut(myHead.classname, 12, "$20");
562
563     fillOut(myHead.version, 4, "0");
564
565     fprintf(outputSFile,
566         "\t.byte 0, 0, 0\n"
567         "\t.byte %i\n\n",
568         myHead.mode);
569
570     setLen(myHead.author, 62);
571     fprintf(outputSFile,
572         "\t.byte \"%s\"\n"
573         "\t.byte 0\n"
574         "\t.res  (63 - %i)\n\n",
575         myHead.author, (int)(strlen(myHead.author) + 1));
576
577     setLen(myHead.info, 95);
578     fprintf(outputSFile,
579         "\t.byte \"%s\"\n"
580         "\t.byte 0\n\n",
581         myHead.info);
582
583     if (fclose (outputSFile) != 0)
584         AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
585 }
586
587
588 void DoVLIR(void) {
589
590     char *token;
591     int record, lastrecord;
592     int vlirsize, vlirtable[127];
593
594     openSFile();
595
596     vlirsize = strtol(nextWord(), NULL, 0);
597
598     if (strcmp(nextWord(), "{") != 0) {
599         AbEnd ("VLIR description has no opening bracket!\n");
600     }
601
602     lastrecord = -1;
603     memset(vlirtable, 0, sizeof(vlirtable));
604
605     do {
606         token = nextWord();
607         if (strcmp(token, "}") == 0) break;
608
609         record = atoi(token);
610         if (record < 0 || record > 126) {
611             AbEnd("VLIR record %i is out of range 0-126.\n", record);
612         }
613         if (vlirtable[record] == 1) {
614             AbEnd("VLIR record %i is defined twice.\n", record);
615         }
616
617         vlirtable[record] = 1;
618         if (record > lastrecord) lastrecord = record;
619     } while (strcmp(token, "}") != 0);
620
621     if (lastrecord == -1) {
622         AbEnd("There must be at least one VLIR record.\n");
623     }
624
625     /* always include record 0 */
626     vlirtable[0] = 1;
627
628     /* OK, all information is gathered, do flushout */
629
630     fprintf(outputSFile,
631         "\n"
632         "\t\t.segment \"RECORDS\"\n\n"
633         "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
634         vlirsize);
635
636     for (record = 0; record <= lastrecord; record++) {
637         if (vlirtable[record] == 1) {
638             fprintf(outputSFile,
639                 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
640                 record, record);
641         }
642     }
643     fprintf(outputSFile,
644         "\n");
645
646     for (record = 0; record <= lastrecord; record++) {
647         if (vlirtable[record] == 1) {
648             fprintf(outputSFile,
649                 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) /    254) + 1\n"
650                 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
651                 record, record, record, record);
652         } else {
653             fprintf(outputSFile,
654                 "\t.byte $00\n"
655                 "\t.byte $FF\n");
656         }
657     }
658     fprintf(outputSFile,
659         "\n");
660
661     if (fclose(outputSFile) != 0)
662         AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
663
664     openCFile();
665
666     fprintf(outputCFile,
667         "extern void _OVERLAYADDR__;\n"
668         "extern void _OVERLAYSIZE__;\n\n"
669         "#define OVERLAY_ADDR (char*)   &_OVERLAYADDR__\n"
670         "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
671
672     if (fclose(outputCFile) != 0)
673         AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
674 }
675
676
677 char *filterInput(FILE *F, char *tbl) {
678
679     /* loads file into buffer filtering it out */
680     int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
681
682     while (1) {
683         a = getc(F);
684         if ((a == '\n') || (a == '\015')) a = ' ';
685         if (a == ',' && quote) a = ' ';
686         if (a == '\042') quote =! quote;
687         if (quote) {
688             if ((a == '{') || (a == '(')) bracket++;
689             if ((a == '}') || (a == ')')) bracket--;
690         }
691         if (a == EOF) {
692             tbl[i] = '\0';
693             xrealloc(tbl, i + 1);
694             break;
695         }
696         if (IsSpace(a)) {
697             if ((prevchar != ' ') && (prevchar != -1)) {
698                 tbl[i++] = ' ';
699                 prevchar = ' ';
700             }
701         } else {
702             if (a == ';' && quote) {
703                 do {
704                     a = getc(F);
705                 } while (a != '\n');
706                 fseek(F, -1, SEEK_CUR);
707             } else {
708                 tbl[i++] = a;
709                 prevchar = a;
710             }
711         }
712     }
713
714     if (bracket != 0) AbEnd("there are unclosed brackets!\n");
715
716     return tbl;
717 }
718
719
720 void processFile(const char *filename) {
721
722     FILE *F;
723
724     char *str;
725     char *token;
726
727     int head = 0; /* number of processed HEADER sections */
728     int vlir = 0; /* number of processed VLIR sections */
729
730     if ((F = fopen(filename, "r")) == 0) {
731         AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
732     }
733
734     str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
735
736     token = strtok(str, " ");
737
738     do {
739         if (str != NULL) {
740             switch (findToken(mainToken, token)) {
741                 case 0:
742                     DoMenu();
743                     break;
744                 case 1:
745                     if (++head != 1) {
746                         AbEnd("more than one HEADER section, aborting.\n");
747                     } else {
748                         DoHeader();
749                     }
750                     break;
751                 case 2: break; /* icon not implemented yet */
752                 case 3: break; /* dialog not implemented yet */
753                 case 4:
754                     if (++vlir != 1) {
755                         AbEnd("more than one VLIR section, aborting.\n");
756                     } else {
757                         DoVLIR();
758                     }
759                     break;
760                 default:
761                     AbEnd("unknown section %s.\n",token);
762                     break;
763             }
764         }
765         token = nextWord();
766     } while (token != NULL);
767 }
768
769
770 int main(int argc, char *argv[]) {
771
772     int ffile = 0, i = 1;
773
774     ProgName = argv[0];
775
776     while (i < argc) {
777         const char *arg = argv[i];
778
779         if (arg[0] == '-') {
780             switch (arg[1]) {
781                 case 'o':
782                     outputCName = argv[++i];
783                     break;
784                 case 's':
785                     outputSName = argv[++i];
786                     break;
787                 case 't':
788                     switch (FindTarget(argv[++i])) {
789                         case TGT_GEOS_CBM:
790                             apple = 0;
791                             break;
792                         case TGT_GEOS_APPLE:
793                             apple = 1;
794                             break;
795                         default:
796                             AbEnd("unknown target system type %s\n", argv[i]);
797                     }
798                     break;
799                 case 'h':
800                 case '?':
801                     printUsage();
802                     exit(EXIT_SUCCESS);
803                     break;
804                 default:
805                     AbEnd("unknown option %s\n", arg);
806             }
807         } else {
808             ffile++;
809
810             if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
811             if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
812
813             processFile(arg);
814         }
815
816         i++;
817     }
818
819     if (ffile == 0) AbEnd("no input file\n");
820
821     return EXIT_SUCCESS;
822 }