1 /* GEOS resource compiler
3 by Maciej 'YTM/Elysium' Witkowiak
5 see GEOSLib documentation for license info
8 /* - make it work, then do it better
9 - more or less comments? it was hard to code, should be even harder to
11 - add loadable icons feature (binary - 63 bytes)
14 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
33 /* I hope that no one will be able to create a .grc bigger than this... */
34 #define BLOODY_BIG_BUFFER 65000
42 struct menuitem *next;
50 struct menuitem *item;
54 int year, month, day, hour, min;
67 const char *mainToken[] = {"MENU", "HEADER", "ICON", "DIALOG", "VLIR", ""};
69 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
70 "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
72 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
74 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
76 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
78 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
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};
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};
99 const char *outputCName = NULL;
100 const char *outputSName = NULL;
101 FILE *outputCFile, *outputSFile;
102 int CFnum = 0, SFnum = 0;
104 char outputCMode[2] = "w";
105 char outputSMode[2] = "w";
108 static void Usage (void)
110 printf ("Usage: %s [options] file\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"
119 " --help\t\tHelp (this text)\n"
120 " --target sys\t\tSet the target system\n"
121 " --version\t\tPrint the version number\n",
126 static void OptHelp (const char* Opt attribute ((unused)),
127 const char* Arg attribute ((unused)))
128 /* Print usage information and exit */
135 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
136 /* Set the target system */
138 switch (FindTarget (Arg)) {
149 AbEnd ("Unknown target system `%s'", Arg);
153 /* Target is known but unsupported */
154 AbEnd ("Unsupported target system `%s'", Arg);
161 static void OptVersion (const char* Opt attribute ((unused)),
162 const char* Arg attribute ((unused)))
163 /* Print the assembler version */
166 "grc65 V%s - (C) Copyright, Maciej 'YTM/Elysium' Witkowiak\n",
167 GetVersionAsString ());
172 static void printCHeader (void)
174 fprintf (outputCFile,
176 "//\tThis file was generated by the GEOS Resource Compiler\n"
178 "//\tDO NOT EDIT! Any changes will be lost!\n"
180 "//\tEdit proper resource file instead.\n"
185 static void printSHeader (void)
187 fprintf (outputSFile,
189 ";\tThis file was generated by the GEOS Resource Compiler\n"
191 ";\tDO NOT EDIT! Any changes will be lost!\n"
193 ";\tEdit proper resource file instead.\n"
198 static void openCFile (void)
200 if ((outputCFile = fopen (outputCName,outputCMode)) == 0) {
201 AbEnd ("Can't open file %s for writing: %s", outputCName, strerror (errno));
205 outputCMode[0] = 'a';
212 static void openSFile (void)
214 if ((outputSFile = fopen (outputSName, outputSMode)) == 0) {
215 AbEnd ("Can't open file %s for writing: %s", outputSName, strerror (errno));
219 outputSMode[0] = 'a';
226 static int findToken (const char **tokenTbl, const char *token)
228 /* takes as input table of tokens and token, returns position in table or -1 if not found */
231 while (strlen (tokenTbl[a]) != 0) {
232 if (strcmp (tokenTbl[a], token) == 0) break;
236 if (strlen (tokenTbl[a]) == 0) a = -1;
241 static char *nextPhrase (void)
243 return strtok (NULL, "\"");
247 static char *nextWord (void)
249 return strtok (NULL, " ");
253 static void setLen (char *name, unsigned len)
255 if (strlen (name) > len)
260 static void fillOut (char *name, int len, char *filler)
265 fprintf (outputSFile, "\t.byte \"%s\"\n", name);
269 fprintf (outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
274 static char *bintos (unsigned char a, char out[7])
279 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
288 static int getNameSize (const char *word)
290 /* count length of a word using BSW 9 font table */
293 while (word[i] != '\0') {
294 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
302 static void DoMenu (void)
304 int a, size, tmpsize, item = 0;
306 char namebuff[255] = "";
308 struct menuitem *curItem, *newItem;
312 myMenu.name = nextWord ();
313 myMenu.left = atoi (nextWord ());
314 myMenu.top = atoi (nextWord ());
315 myMenu.type = nextWord ();
317 if (strcmp(nextWord (), "{") != 0) {
318 AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
320 curItem = xmalloc (sizeof(struct menuitem));
321 myMenu.item = curItem;
324 if (strcmp (token, "}") == 0) break;
325 if (token[strlen(token) - 1] != '"') {
326 strcpy (namebuff, token);
329 strcat (namebuff, " ");
330 strcat (namebuff, token);
331 } while (token[strlen(token) - 1] != '"');
332 token = xmalloc (strlen (namebuff));
333 strcpy (token, namebuff);
335 curItem->name = token;
336 curItem->type = nextWord ();
337 curItem->target = nextWord ();
338 newItem = xmalloc (sizeof(struct menuitem));
339 curItem->next = newItem;
342 } while (strcmp (token, "}") != 0);
343 if (item == 0) AbEnd ("Menu '%s' has 0 items!", myMenu.name);
344 if (item > 31) AbEnd ("Menu '%s' has too many items!", myMenu.name);
346 curItem->next = NULL;
348 /* count menu sizes */
350 curItem = myMenu.item;
351 if (strstr (myMenu.type, "HORIZONTAL") != NULL) {
352 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
353 myMenu.bot = myMenu.top + 15;
354 for (a = 0; a != item; a++) {
355 size += getNameSize (curItem->name);
356 curItem = curItem->next;
359 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
360 myMenu.bot = myMenu.top + (14 * item);
361 for (a = 0; a != item; a++) {
362 tmpsize = getNameSize (curItem->name);
363 size = (size > tmpsize) ? size : tmpsize;
364 curItem = curItem->next;
367 myMenu.right = myMenu.left + size - 1;
369 curItem = myMenu.item;
370 for (a = 0; a != item; a++) {
371 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
372 if ((strstr (curItem->type, "MENU_ACTION") != NULL) || (strstr (curItem->type, "DYN_SUB_MENU") != NULL)) {
373 fprintf (outputCFile,
377 curItem=curItem->next;
380 fprintf (outputCFile,
382 "const void %s = {\n"
383 "\t(char)%i, (char)%i,\n"
384 "\t(int)%i, (int)%i,\n"
385 "\t(char)(%i | %s),\n",
386 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
388 curItem = myMenu.item;
389 for (a = 0; a != item; a++) {
390 fprintf (outputCFile,
391 "\t%s, (char)%s, (int)",
392 curItem->name, curItem->type);
393 if ((strstr (curItem->type, "SUB_MENU") != NULL) && (strstr (curItem->type, "DYN_SUB_MENU") == NULL))
394 fprintf (outputCFile,
396 fprintf (outputCFile,
399 curItem = curItem->next;
402 fprintf (outputCFile,
405 if (fclose (outputCFile) != 0)
406 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
410 static void DoHeader (void)
415 struct appheader myHead;
417 char i1[9], i2[9], i3[9];
424 a = findToken(hdrFTypes, token);
429 myHead.geostype = 0x82;
432 AbEnd ("Filetype '%s' is not supported yet", token);
440 myHead.geostype = 14;
443 AbEnd ("Filetype '%s' is not supported yet", token);
447 myHead.dosname = nextPhrase ();
449 myHead.classname = nextPhrase ();
451 myHead.version = nextPhrase ();
453 /* put default values into myHead here */
454 myHead.author = "cc65";
455 myHead.info = "Program compiled with cc65 and GEOSLib.";
456 myHead.dostype = 128;
457 if (apple == 0) myHead.dostype += 3;
458 myHead.structure = 0;
463 my_tm = localtime (&t);
465 myHead.year = my_tm->tm_year % 100;
466 myHead.month = my_tm->tm_mon + 1;
467 myHead.day = my_tm->tm_mday;
468 myHead.hour = my_tm->tm_hour;
469 myHead.min = my_tm->tm_min;
471 if (strcmp (nextWord (), "{") != 0) {
472 AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
477 if (strcmp (token, "}") == 0) break;
478 switch (a = findToken (hdrFields, token)) {
480 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
483 myHead.author = nextPhrase ();
486 myHead.info = nextPhrase ();
489 myHead.year = atoi (nextWord ());
490 myHead.month = atoi (nextWord ());
491 myHead.day = atoi (nextWord ());
492 myHead.hour = atoi (nextWord ());
493 myHead.min = atoi (nextWord ());
495 case 3: /* dostype */
496 switch (b = findToken (hdrDOSTp, nextWord ())) {
498 AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
501 if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
506 switch (b = findToken (hdrModes, nextWord ())) {
508 AbEnd ("Unknown mode in header '%s'", myHead.dosname);
510 if (apple == 0) myHead.mode = 0x40;
513 if (apple == 0) myHead.mode = 0x00;
516 if (apple == 0) myHead.mode = 0xc0;
519 if (apple == 0) myHead.mode = 0x80;
523 case 5: /* structure */
524 switch (b = findToken (hdrStructTp, nextWord ())) {
526 AbEnd ("unknown structure type in header '%s'", myHead.dosname);
529 myHead.structure = 0;
533 myHead.structure = 1;
538 myHead.icon = nextPhrase ();
542 } while (strcmp (token, "}") != 0);
544 /* OK, all information is gathered, do flushout */
546 fprintf (outputSFile,
548 "\t\t.segment \"DIRENTRY\"\n\n");
552 fprintf (outputSFile,
553 "\t.import __VLIR0_START__, __VLIR0_LAST__, __BSS_SIZE__\n\n"
554 "\t.byte %i << 4 | %u\n",
555 myHead.structure + 2, (unsigned)strlen (myHead.dosname));
557 fillOut (myHead.dosname, 15, "0");
559 fprintf (outputSFile,
563 "\t.word __VLIR0_LAST__ - __VLIR0_START__ - __BSS_SIZE__\n"
565 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
570 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
573 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min,
574 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
578 fprintf (outputSFile,
583 fillOut (myHead.dosname, 16, "$a0");
585 fprintf (outputSFile,
589 "\t.byte %i, %i, %i, %i, %i\n\n"
591 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
592 myHead.structure, myHead.geostype,
593 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
596 fprintf (outputSFile,
598 "\t\t.segment \"FILEINFO\"\n\n"
599 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
600 "\t.byte 3, 21, 63 | $80\n");
602 if (myHead.icon != NULL) {
603 fprintf (outputSFile,
604 "\t.incbin \"%s\", 0, 63\n",
607 for (a = 0; a != 63; a = a + 3) {
608 fprintf (outputSFile,
609 "\t.byte %%%s, %%%s, %%%s\n",
610 bintos (icon1[a], i1), bintos (icon1[a+1], i2), bintos (icon1[a+2], i3));
614 fprintf (outputSFile,
615 "\t.byte %i, %i, %i\n"
616 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
617 myHead.dostype, myHead.geostype, myHead.structure);
619 fillOut (myHead.classname, 12, "$20");
621 fillOut (myHead.version, 4, "0");
623 fprintf (outputSFile,
628 setLen (myHead.author, 62);
629 fprintf (outputSFile,
632 "\t.res (63 - %i)\n\n",
633 myHead.author, (int)(strlen (myHead.author) + 1));
635 setLen (myHead.info, 95);
636 fprintf (outputSFile,
641 if (fclose (outputSFile) != 0)
642 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
646 static void DoVLIR (void)
649 int record, lastrecord;
650 int vlirsize, vlirtable[127];
654 vlirsize = strtol (nextWord (), NULL, 0);
656 if (strcmp(nextWord (), "{") != 0) {
657 AbEnd ("VLIR description has no opening bracket!");
661 memset (vlirtable, 0, sizeof(vlirtable));
665 if (strcmp (token, "}") == 0) break;
667 record = atoi (token);
668 if (record < 0 || record > 126) {
669 AbEnd ("VLIR record %i is out of range 0-126.", record);
671 if (vlirtable[record] == 1) {
672 AbEnd ("VLIR record %i is defined twice.", record);
675 vlirtable[record] = 1;
676 if (record > lastrecord) lastrecord = record;
677 } while (strcmp (token, "}") != 0);
679 if (lastrecord == -1) {
680 AbEnd ("There must be at least one VLIR record.");
683 /* always include record 0 */
686 /* OK, all information is gathered, do flushout */
688 fprintf (outputSFile,
690 "\t\t.segment \"RECORDS\"\n\n"
691 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
694 for (record = 0; record <= lastrecord; record++) {
695 if (vlirtable[record] == 1) {
696 fprintf (outputSFile,
697 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
701 fprintf (outputSFile,
704 for (record = 0; record <= lastrecord; record++) {
705 if (vlirtable[record] == 1) {
706 fprintf (outputSFile,
707 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
708 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
709 record, record, record, record);
711 fprintf (outputSFile,
716 fprintf (outputSFile,
719 if (fclose (outputSFile) != 0)
720 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
724 fprintf (outputCFile,
725 "extern void _OVERLAYADDR__;\n"
726 "extern void _OVERLAYSIZE__;\n\n"
727 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
728 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
730 if (fclose (outputCFile) != 0)
731 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
735 static char *filterInput (FILE *F, char *tbl)
737 /* loads file into buffer filtering it out */
738 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
742 if ((a == '\n') || (a == '\015')) a = ' ';
743 if (a == ',' && quote) a = ' ';
744 if (a == '\042') quote =! quote;
746 if ((a == '{') || (a == '(')) bracket++;
747 if ((a == '}') || (a == ')')) bracket--;
751 xrealloc (tbl, i + 1);
755 if ((prevchar != ' ') && (prevchar != -1)) {
760 if (a == ';' && quote) {
764 fseek (F, -1, SEEK_CUR);
772 if (bracket != 0) AbEnd ("There are unclosed brackets!");
778 static void processFile (const char *filename)
785 int head = 0; /* number of processed HEADER sections */
786 int vlir = 0; /* number of processed VLIR sections */
788 if ((F = fopen (filename, "r")) == 0) {
789 AbEnd ("Can't open file %s for reading: %s", filename, strerror (errno));
792 str = filterInput (F, xmalloc (BLOODY_BIG_BUFFER));
794 token = strtok (str, " ");
798 switch (findToken (mainToken, token)) {
804 AbEnd ("More than one HEADER section, aborting.");
809 case 2: break; /* icon not implemented yet */
810 case 3: break; /* dialog not implemented yet */
813 AbEnd ("More than one VLIR section, aborting.");
819 AbEnd ("Unknown section %s.", token);
824 } while (token != NULL);
828 int main (int argc, char *argv[])
830 /* Program long options */
831 static const LongOpt OptTab[] = {
832 { "--help", 0, OptHelp},
833 { "--target", 1, OptTarget},
834 { "--version", 0, OptVersion},
840 /* Initialize the cmdline module */
841 InitCmdLine (&argc, &argv, "grc65");
843 /* Check the parameters */
845 while (I < ArgCount) {
847 /* Get the argument */
848 const char* Arg = ArgVec [I];
850 /* Check for an option */
855 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
859 outputCName = GetArg (&I, 2);
863 outputSName = GetArg (&I, 2);
867 OptTarget (Arg, GetArg (&I, 2));
886 if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
887 if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
896 if (ffile == 0) AbEnd ("No input file");