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", "MEMORY", ""};
69 const char *toggle[] = {"off", "no", "0", "on", "yes", "1", ""};
71 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
72 "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
74 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
76 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
78 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
80 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
82 const char *memFields[] = {"stacksize", "overlaysize", "overlaynums", "backbuffer", ""};
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};
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};
103 const char *outputCName = NULL;
104 const char *outputSName = NULL;
105 FILE *outputCFile, *outputSFile;
106 int CFnum = 0, SFnum = 0;
108 char outputCMode[2] = "w";
109 char outputSMode[2] = "w";
112 static void Usage (void)
115 "Usage: %s [options] file\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"
124 " --help\t\tHelp (this text)\n"
125 " --target sys\t\tSet the target system\n"
126 " --version\t\tPrint the version number\n",
131 static void OptHelp (const char* Opt attribute ((unused)),
132 const char* Arg attribute ((unused)))
133 /* Print usage information and exit */
140 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
141 /* Set the target system */
143 switch (FindTarget (Arg)) {
154 AbEnd ("Unknown target system `%s'", Arg);
158 /* Target is known but unsupported */
159 AbEnd ("Unsupported target system `%s'", Arg);
165 static void OptVersion (const char* Opt attribute ((unused)),
166 const char* Arg attribute ((unused)))
167 /* Print the program version */
169 fprintf (stderr, "%s V%s\n", ProgName, GetVersionAsString ());
175 static void printCHeader (void)
177 fprintf (outputCFile,
179 "//\tThis file was generated by the GEOS Resource Compiler\n"
181 "//\tDO NOT EDIT! Any changes will be lost!\n"
183 "//\tEdit proper resource file instead.\n"
188 static void printSHeader (void)
190 fprintf (outputSFile,
192 ";\tThis file was generated by the GEOS Resource Compiler\n"
194 ";\tDO NOT EDIT! Any changes will be lost!\n"
196 ";\tEdit proper resource file instead.\n"
201 static void openCFile (void)
203 if ((outputCFile = fopen (outputCName,outputCMode)) == 0) {
204 AbEnd ("Can't open file %s for writing: %s", outputCName, strerror (errno));
208 outputCMode[0] = 'a';
215 static void openSFile (void)
217 if ((outputSFile = fopen (outputSName, outputSMode)) == 0) {
218 AbEnd ("Can't open file %s for writing: %s", outputSName, strerror (errno));
222 outputSMode[0] = 'a';
229 static int findToken (const char * const *tokenTbl, const char *token)
231 /* takes as input table of tokens and token, returns position in table or -1 if not found */
234 for (i = 0; tokenTbl[i][0]; i++) {
235 if (strcmp (tokenTbl[i], token) == 0) {
244 static char *nextPhrase (void)
246 return strtok (NULL, "\"");
250 static char *nextWord (void)
252 return strtok (NULL, " ");
256 static void setLen (char *name, unsigned len)
258 if (strlen (name) > len) {
264 static void fillOut (char *name, int len, char *filler)
269 fprintf (outputSFile, "\t.byte \"%s\"\n", name);
273 fprintf (outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
278 static char *bintos (unsigned char a, char out[7])
283 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
292 static int getNameSize (const char *word)
294 /* count length of a word using BSW 9 font table */
297 while (word[i] != '\0') {
298 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
306 static void DoMenu (void)
308 int a, size, tmpsize, item = 0;
310 char namebuff[255] = "";
312 struct menuitem *curItem, *newItem;
316 myMenu.name = nextWord ();
317 myMenu.left = atoi (nextWord ());
318 myMenu.top = atoi (nextWord ());
319 myMenu.type = nextWord ();
321 if (strcmp (nextWord (), "{") != 0) {
322 AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
324 curItem = xmalloc (sizeof(struct menuitem));
325 myMenu.item = curItem;
329 if (strcmp (token, "}") == 0) break;
330 if (token[strlen (token) - 1] != '"') {
331 strcpy (namebuff, token);
334 strcat (namebuff, " ");
335 strcat (namebuff, token);
336 } while (token[strlen (token) - 1] != '"');
337 token = xmalloc (strlen (namebuff));
338 strcpy (token, namebuff);
340 curItem->name = token;
341 curItem->type = nextWord ();
342 curItem->target = nextWord ();
343 newItem = xmalloc (sizeof(struct menuitem));
344 curItem->next = newItem;
349 if (item == 0) AbEnd ("Menu '%s' has 0 items!", myMenu.name);
350 if (item > 31) AbEnd ("Menu '%s' has too many items!", myMenu.name);
352 curItem->next = NULL;
354 /* count menu sizes */
356 curItem = myMenu.item;
357 if (strstr (myMenu.type, "HORIZONTAL") != NULL) {
358 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
359 myMenu.bot = myMenu.top + 15;
360 for (a = 0; a != item; a++) {
361 size += getNameSize (curItem->name);
362 curItem = curItem->next;
365 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
366 myMenu.bot = myMenu.top + (14 * item);
367 for (a = 0; a != item; a++) {
368 tmpsize = getNameSize (curItem->name);
369 size = (size > tmpsize) ? size : tmpsize;
370 curItem = curItem->next;
373 myMenu.right = myMenu.left + size - 1;
375 curItem = myMenu.item;
376 for (a = 0; a != item; a++) {
377 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
378 if ((strstr (curItem->type, "MENU_ACTION") != NULL) || (strstr (curItem->type, "DYN_SUB_MENU") != NULL)) {
379 fprintf (outputCFile,
383 curItem=curItem->next;
386 fprintf (outputCFile,
388 "const void %s = {\n"
389 "\t(char)%i, (char)%i,\n"
390 "\t(int)%i, (int)%i,\n"
391 "\t(char)(%i | %s),\n",
392 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
394 curItem = myMenu.item;
395 for (a = 0; a != item; a++) {
396 fprintf (outputCFile,
397 "\t%s, (char)%s, (int)",
398 curItem->name, curItem->type);
399 if ((strstr (curItem->type, "SUB_MENU") != NULL) && (strstr (curItem->type, "DYN_SUB_MENU") == NULL)) {
400 fprintf (outputCFile,
403 fprintf (outputCFile,
406 curItem = curItem->next;
409 fprintf (outputCFile,
412 if (fclose (outputCFile) != 0) {
413 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
418 static void DoHeader (void)
423 struct appheader myHead;
425 char i1[9], i2[9], i3[9];
432 i = findToken (hdrFTypes, token);
437 myHead.geostype = 0x82;
440 AbEnd ("Filetype '%s' is not supported yet", token);
448 myHead.geostype = 14;
451 AbEnd ("Filetype '%s' is not supported yet", token);
455 myHead.dosname = nextPhrase ();
457 myHead.classname = nextPhrase ();
459 myHead.version = nextPhrase ();
461 /* put default values into myHead here */
462 myHead.author = "cc65";
463 myHead.info = "Program compiled with cc65 and GEOSLib.";
464 myHead.dostype = 128;
465 if (apple == 0) myHead.dostype += 3;
466 myHead.structure = 0;
471 my_tm = localtime (&t);
473 myHead.year = my_tm->tm_year % 100;
474 myHead.month = my_tm->tm_mon + 1;
475 myHead.day = my_tm->tm_mday;
476 myHead.hour = my_tm->tm_hour;
477 myHead.min = my_tm->tm_min;
479 if (strcmp (nextWord (), "{") != 0) {
480 AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
485 if (strcmp (token, "}") == 0) break;
486 switch (findToken (hdrFields, token)) {
488 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
492 myHead.author = nextPhrase ();
496 myHead.info = nextPhrase ();
500 myHead.year = atoi (nextWord ());
501 myHead.month = atoi (nextWord ());
502 myHead.day = atoi (nextWord ());
503 myHead.hour = atoi (nextWord ());
504 myHead.min = atoi (nextWord ());
507 case 3: /* dostype */
508 switch (i = findToken (hdrDOSTp, nextWord ())) {
510 AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
513 if (apple == 0) myHead.dostype = i / 2 + 128 + 1;
519 switch (findToken (hdrModes, nextWord ())) {
521 AbEnd ("Unknown mode in header '%s'", myHead.dosname);
523 if (apple == 0) myHead.mode = 0x40;
526 if (apple == 0) myHead.mode = 0x00;
529 if (apple == 0) myHead.mode = 0xc0;
532 if (apple == 0) myHead.mode = 0x80;
537 case 5: /* structure */
538 switch (findToken (hdrStructTp, nextWord ())) {
540 AbEnd ("unknown structure type in header '%s'", myHead.dosname);
543 myHead.structure = 0;
547 myHead.structure = 1;
553 myHead.icon = nextPhrase ();
558 /* OK, all information is gathered, do flushout */
560 fprintf (outputSFile,
561 "\t\t.segment \"DIRENTRY\"\n\n");
565 if (myHead.structure == 0) {
566 fprintf (outputSFile,
567 "\t.import __VLIR0_START__, __VLIR0_LAST__, __BSS_SIZE__\n\n");
569 fprintf (outputSFile,
570 "\t.byte %i << 4 | %u\n",
571 myHead.structure + 2, (unsigned)strlen (myHead.dosname));
573 fillOut (myHead.dosname, 15, "0");
575 fprintf (outputSFile,
581 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
586 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
589 myHead.structure == 0 ?
590 "__VLIR0_LAST__ - __VLIR0_START__ - __BSS_SIZE__" : "0",
591 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min,
592 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
596 fprintf (outputSFile,
601 fillOut (myHead.dosname, 16, "$a0");
603 fprintf (outputSFile,
607 "\t.byte %i, %i, %i, %i, %i\n\n"
609 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
610 myHead.structure, myHead.geostype,
611 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
614 fprintf (outputSFile,
615 "\t\t.segment \"FILEINFO\"\n\n"
616 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
617 "\t.byte 3, 21, 63 | $80\n");
619 if (myHead.icon != NULL) {
620 fprintf (outputSFile,
621 "\t.incbin \"%s\", 0, 63\n",
624 for (i = 0; i != 63; i = i + 3) {
625 fprintf (outputSFile,
626 "\t.byte %%%s, %%%s, %%%s\n",
627 bintos (icon1[i], i1), bintos (icon1[i+1], i2), bintos (icon1[i+2], i3));
631 fprintf (outputSFile,
632 "\t.byte %i, %i, %i\n"
633 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
634 myHead.dostype, myHead.geostype, myHead.structure);
636 fillOut (myHead.classname, 12, "$20");
638 fillOut (myHead.version, 4, "0");
640 fprintf (outputSFile,
645 setLen (myHead.author, 62);
646 fprintf (outputSFile,
649 "\t.res (63 - %i)\n\n",
650 myHead.author, (int)(strlen (myHead.author) + 1));
652 setLen (myHead.info, 95);
653 fprintf (outputSFile,
658 if (fclose (outputSFile) != 0) {
659 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
664 static void DoMemory (void)
667 int stacksize, overlaysize;
668 int overlaytable[127];
669 int number, lastnumber;
676 memset (overlaytable, 0, sizeof (overlaytable));
680 if (strcmp (nextWord (), "{") != 0) {
681 AbEnd ("MEMORY description has no opening bracket!");
686 if (token == NULL) token = nextWord ();
687 if (strcmp (token, "}") == 0) break;
688 switch (findToken (memFields, token)) {
690 AbEnd ("Unknown field '%s' in MEMORY description", token);
693 case 0: /* stacksize */
694 stacksize = strtol (nextWord (), NULL, 0);
698 case 1: /* overlaysize */
699 overlaysize = strtol (nextWord (), NULL, 0);
703 case 2: /* overlaynums */
706 if (IsDigit (token[0]) == 0) break;
708 number = atoi (token);
709 if (number < 0 || number > 126) {
710 AbEnd ("Overlay number %i is out of range 0-126", number);
712 if (overlaytable[number] == 1) {
713 AbEnd ("Overlay number %i is defined twice", number);
716 overlaytable[number] = 1;
717 if (number > lastnumber) lastnumber = number;
719 } while (IsDigit (token[0]) != 0);
721 if (lastnumber == -1) {
722 AbEnd ("There must be at least one overlay number");
725 /* always include number 0 */
729 case 3: /* backbuffer */
730 switch (findToken (toggle, nextWord ())) {
732 AbEnd ("Unknown value for 'backbuffer'");
748 /* OK, all information is gathered, do flushout */
750 if (lastnumber != -1) {
751 fprintf (outputSFile,
752 "\t\t.segment \"RECORDS\"\n\n");
756 for (number = 0; number <= lastnumber; number++) {
757 fprintf (outputSFile,
759 overlaytable[number] == 1 ? "$00" : "$FF");
761 fprintf (outputSFile,
764 for (number = 0; number <= lastnumber; number++) {
765 if (overlaytable[number] == 1) {
766 fprintf (outputSFile,
767 "\t\t.segment \"VLIRIDX%i\"\n\n"
768 "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n\n"
770 "\t.byte .lobyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n"
772 "\t.byte .hibyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n\n",
773 number, number, number,
774 number == 0 ? ", __BSS_SIZE__" : "",
776 number == 0 ? " - __BSS_SIZE__" : "",
778 number == 0 ? " - __BSS_SIZE__" : "");
784 for (number = 0; number <= lastnumber; number++) {
785 if (overlaytable[number] == 1) {
786 fprintf (outputSFile,
787 "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n",
788 number, number, number == 0 ? ", __BSS_SIZE__" : "");
791 fprintf (outputSFile,
794 for (number = 0; number <= lastnumber; number++) {
795 if (overlaytable[number] == 1) {
796 fprintf (outputSFile,
797 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) / 254) + 1\n"
798 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) .MOD 254) + 2\n",
799 number, number, number == 0 ? " - __BSS_SIZE__" : "",
800 number, number, number == 0 ? " - __BSS_SIZE__" : "");
802 fprintf (outputSFile,
807 fprintf (outputSFile,
813 fprintf (outputCFile,
814 "extern void _OVERLAYADDR__[];\n"
815 "extern void _OVERLAYSIZE__[];\n\n"
816 "#define OVERLAY_ADDR (char*) _OVERLAYADDR__\n"
817 "#define OVERLAY_SIZE (unsigned)_OVERLAYSIZE__\n\n");
819 if (fclose (outputCFile) != 0) {
820 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
824 if (stacksize != -1) {
825 fprintf (outputSFile,
826 "\t.export __STACKSIZE__ : absolute = $%04x\n\n",
830 if (overlaysize != -1) {
831 fprintf (outputSFile,
832 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
836 if (backbuffer != -1) {
837 fprintf (outputSFile,
838 "\t.export __BACKBUFSIZE__ : absolute = $%04x\n\n",
839 backbuffer ? 0x2000 : 0x0000);
842 if (fclose (outputSFile) != 0) {
843 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
848 static char *filterInput (FILE *F, char *tbl)
850 /* loads file into buffer filtering it out */
851 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
855 if ((a == '\n') || (a == '\015')) a = ' ';
856 if (a == ',' && quote) a = ' ';
857 if (a == '\042') quote =! quote;
859 if ((a == '{') || (a == '(')) bracket++;
860 if ((a == '}') || (a == ')')) bracket--;
864 xrealloc (tbl, i + 1);
868 if ((prevchar != ' ') && (prevchar != -1)) {
873 if (a == ';' && quote) {
877 fseek (F, -1, SEEK_CUR);
885 if (bracket != 0) AbEnd ("There are unclosed brackets!");
891 static void processFile (const char *filename)
898 int head = 0; /* number of processed HEADER sections */
899 int memory = 0; /* number of processed MEMORY sections */
901 if ((F = fopen (filename, "r")) == 0) {
902 AbEnd ("Can't open file %s for reading: %s", filename, strerror (errno));
905 str = filterInput (F, xmalloc (BLOODY_BIG_BUFFER));
907 token = strtok (str, " ");
911 switch (findToken (mainToken, token)) {
917 AbEnd ("More than one HEADER section, aborting");
922 case 2: break; /* icon not implemented yet */
923 case 3: break; /* dialog not implemented yet */
926 AbEnd ("More than one MEMORY section, aborting");
932 AbEnd ("Unknown section '%s'", token);
937 } while (token != NULL);
941 int main (int argc, char *argv[])
943 /* Program long options */
944 static const LongOpt OptTab[] = {
945 { "--help", 0, OptHelp},
946 { "--target", 1, OptTarget},
947 { "--version", 0, OptVersion},
953 /* Initialize the cmdline module */
954 InitCmdLine (&argc, &argv, "grc65");
956 /* Check the parameters */
958 while (I < ArgCount) {
960 /* Get the argument */
961 const char* Arg = ArgVec [I];
963 /* Check for an option */
968 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
972 outputCName = GetArg (&I, 2);
976 outputSName = GetArg (&I, 2);
980 OptTarget (Arg, GetArg (&I, 2));
999 if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
1000 if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
1009 if (ffile == 0) AbEnd ("No input file");
1011 return EXIT_SUCCESS;