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 */
170 "grc65 V%s - (C) Copyright, Maciej 'YTM/Elysium' Witkowiak\n",
171 GetVersionAsString ());
176 static void printCHeader (void)
178 fprintf (outputCFile,
180 "//\tThis file was generated by the GEOS Resource Compiler\n"
182 "//\tDO NOT EDIT! Any changes will be lost!\n"
184 "//\tEdit proper resource file instead.\n"
189 static void printSHeader (void)
191 fprintf (outputSFile,
193 ";\tThis file was generated by the GEOS Resource Compiler\n"
195 ";\tDO NOT EDIT! Any changes will be lost!\n"
197 ";\tEdit proper resource file instead.\n"
202 static void openCFile (void)
204 if ((outputCFile = fopen (outputCName,outputCMode)) == 0) {
205 AbEnd ("Can't open file %s for writing: %s", outputCName, strerror (errno));
209 outputCMode[0] = 'a';
216 static void openSFile (void)
218 if ((outputSFile = fopen (outputSName, outputSMode)) == 0) {
219 AbEnd ("Can't open file %s for writing: %s", outputSName, strerror (errno));
223 outputSMode[0] = 'a';
230 static int findToken (const char **tokenTbl, const char *token)
232 /* takes as input table of tokens and token, returns position in table or -1 if not found */
235 while (strlen (tokenTbl[a]) != 0) {
236 if (strcmp (tokenTbl[a], token) == 0) break;
240 if (strlen (tokenTbl[a]) == 0) a = -1;
245 static char *nextPhrase (void)
247 return strtok (NULL, "\"");
251 static char *nextWord (void)
253 return strtok (NULL, " ");
257 static void setLen (char *name, unsigned len)
259 if (strlen (name) > len) {
265 static void fillOut (char *name, int len, char *filler)
270 fprintf (outputSFile, "\t.byte \"%s\"\n", name);
274 fprintf (outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
279 static char *bintos (unsigned char a, char out[7])
284 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
293 static int getNameSize (const char *word)
295 /* count length of a word using BSW 9 font table */
298 while (word[i] != '\0') {
299 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
307 static void DoMenu (void)
309 int a, size, tmpsize, item = 0;
311 char namebuff[255] = "";
313 struct menuitem *curItem, *newItem;
317 myMenu.name = nextWord ();
318 myMenu.left = atoi (nextWord ());
319 myMenu.top = atoi (nextWord ());
320 myMenu.type = nextWord ();
322 if (strcmp (nextWord (), "{") != 0) {
323 AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
325 curItem = xmalloc (sizeof(struct menuitem));
326 myMenu.item = curItem;
330 if (strcmp (token, "}") == 0) break;
331 if (token[strlen (token) - 1] != '"') {
332 strcpy (namebuff, token);
335 strcat (namebuff, " ");
336 strcat (namebuff, token);
337 } while (token[strlen (token) - 1] != '"');
338 token = xmalloc (strlen (namebuff));
339 strcpy (token, namebuff);
341 curItem->name = token;
342 curItem->type = nextWord ();
343 curItem->target = nextWord ();
344 newItem = xmalloc (sizeof(struct menuitem));
345 curItem->next = newItem;
350 if (item == 0) AbEnd ("Menu '%s' has 0 items!", myMenu.name);
351 if (item > 31) AbEnd ("Menu '%s' has too many items!", myMenu.name);
353 curItem->next = NULL;
355 /* count menu sizes */
357 curItem = myMenu.item;
358 if (strstr (myMenu.type, "HORIZONTAL") != NULL) {
359 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
360 myMenu.bot = myMenu.top + 15;
361 for (a = 0; a != item; a++) {
362 size += getNameSize (curItem->name);
363 curItem = curItem->next;
366 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
367 myMenu.bot = myMenu.top + (14 * item);
368 for (a = 0; a != item; a++) {
369 tmpsize = getNameSize (curItem->name);
370 size = (size > tmpsize) ? size : tmpsize;
371 curItem = curItem->next;
374 myMenu.right = myMenu.left + size - 1;
376 curItem = myMenu.item;
377 for (a = 0; a != item; a++) {
378 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
379 if ((strstr (curItem->type, "MENU_ACTION") != NULL) || (strstr (curItem->type, "DYN_SUB_MENU") != NULL)) {
380 fprintf (outputCFile,
384 curItem=curItem->next;
387 fprintf (outputCFile,
389 "const void %s = {\n"
390 "\t(char)%i, (char)%i,\n"
391 "\t(int)%i, (int)%i,\n"
392 "\t(char)(%i | %s),\n",
393 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
395 curItem = myMenu.item;
396 for (a = 0; a != item; a++) {
397 fprintf (outputCFile,
398 "\t%s, (char)%s, (int)",
399 curItem->name, curItem->type);
400 if ((strstr (curItem->type, "SUB_MENU") != NULL) && (strstr (curItem->type, "DYN_SUB_MENU") == NULL)) {
401 fprintf (outputCFile,
404 fprintf (outputCFile,
407 curItem = curItem->next;
410 fprintf (outputCFile,
413 if (fclose (outputCFile) != 0) {
414 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
419 static void DoHeader (void)
424 struct appheader myHead;
426 char i1[9], i2[9], i3[9];
433 i = findToken (hdrFTypes, token);
438 myHead.geostype = 0x82;
441 AbEnd ("Filetype '%s' is not supported yet", token);
449 myHead.geostype = 14;
452 AbEnd ("Filetype '%s' is not supported yet", token);
456 myHead.dosname = nextPhrase ();
458 myHead.classname = nextPhrase ();
460 myHead.version = nextPhrase ();
462 /* put default values into myHead here */
463 myHead.author = "cc65";
464 myHead.info = "Program compiled with cc65 and GEOSLib.";
465 myHead.dostype = 128;
466 if (apple == 0) myHead.dostype += 3;
467 myHead.structure = 0;
472 my_tm = localtime (&t);
474 myHead.year = my_tm->tm_year % 100;
475 myHead.month = my_tm->tm_mon + 1;
476 myHead.day = my_tm->tm_mday;
477 myHead.hour = my_tm->tm_hour;
478 myHead.min = my_tm->tm_min;
480 if (strcmp (nextWord (), "{") != 0) {
481 AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
486 if (strcmp (token, "}") == 0) break;
487 switch (findToken (hdrFields, token)) {
489 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
493 myHead.author = nextPhrase ();
497 myHead.info = nextPhrase ();
501 myHead.year = atoi (nextWord ());
502 myHead.month = atoi (nextWord ());
503 myHead.day = atoi (nextWord ());
504 myHead.hour = atoi (nextWord ());
505 myHead.min = atoi (nextWord ());
508 case 3: /* dostype */
509 switch (i = findToken (hdrDOSTp, nextWord ())) {
511 AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
514 if (apple == 0) myHead.dostype = i / 2 + 128 + 1;
520 switch (findToken (hdrModes, nextWord ())) {
522 AbEnd ("Unknown mode in header '%s'", myHead.dosname);
524 if (apple == 0) myHead.mode = 0x40;
527 if (apple == 0) myHead.mode = 0x00;
530 if (apple == 0) myHead.mode = 0xc0;
533 if (apple == 0) myHead.mode = 0x80;
538 case 5: /* structure */
539 switch (findToken (hdrStructTp, nextWord ())) {
541 AbEnd ("unknown structure type in header '%s'", myHead.dosname);
544 myHead.structure = 0;
548 myHead.structure = 1;
554 myHead.icon = nextPhrase ();
559 /* OK, all information is gathered, do flushout */
561 fprintf (outputSFile,
562 "\t\t.segment \"DIRENTRY\"\n\n");
566 if (myHead.structure == 0) {
567 fprintf (outputSFile,
568 "\t.import __VLIR0_START__, __VLIR0_LAST__, __BSS_SIZE__\n\n");
570 fprintf (outputSFile,
571 "\t.byte %i << 4 | %u\n",
572 myHead.structure + 2, (unsigned)strlen (myHead.dosname));
574 fillOut (myHead.dosname, 15, "0");
576 fprintf (outputSFile,
582 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
587 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
590 myHead.structure == 0 ?
591 "__VLIR0_LAST__ - __VLIR0_START__ - __BSS_SIZE__" : "0",
592 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min,
593 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
597 fprintf (outputSFile,
602 fillOut (myHead.dosname, 16, "$a0");
604 fprintf (outputSFile,
608 "\t.byte %i, %i, %i, %i, %i\n\n"
610 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
611 myHead.structure, myHead.geostype,
612 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
615 fprintf (outputSFile,
616 "\t\t.segment \"FILEINFO\"\n\n"
617 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
618 "\t.byte 3, 21, 63 | $80\n");
620 if (myHead.icon != NULL) {
621 fprintf (outputSFile,
622 "\t.incbin \"%s\", 0, 63\n",
625 for (i = 0; i != 63; i = i + 3) {
626 fprintf (outputSFile,
627 "\t.byte %%%s, %%%s, %%%s\n",
628 bintos (icon1[i], i1), bintos (icon1[i+1], i2), bintos (icon1[i+2], i3));
632 fprintf (outputSFile,
633 "\t.byte %i, %i, %i\n"
634 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
635 myHead.dostype, myHead.geostype, myHead.structure);
637 fillOut (myHead.classname, 12, "$20");
639 fillOut (myHead.version, 4, "0");
641 fprintf (outputSFile,
646 setLen (myHead.author, 62);
647 fprintf (outputSFile,
650 "\t.res (63 - %i)\n\n",
651 myHead.author, (int)(strlen (myHead.author) + 1));
653 setLen (myHead.info, 95);
654 fprintf (outputSFile,
659 if (fclose (outputSFile) != 0) {
660 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
665 static void DoMemory (void)
668 int stacksize, overlaysize;
669 int overlaytable[127];
670 int number, lastnumber;
677 memset (overlaytable, 0, sizeof (overlaytable));
681 if (strcmp (nextWord (), "{") != 0) {
682 AbEnd ("MEMORY description has no opening bracket!");
687 if (token == NULL) token = nextWord ();
688 if (strcmp (token, "}") == 0) break;
689 switch (findToken (memFields, token)) {
691 AbEnd ("Unknown field '%s' in MEMORY description", token);
694 case 0: /* stacksize */
695 stacksize = strtol (nextWord (), NULL, 0);
699 case 1: /* overlaysize */
700 overlaysize = strtol (nextWord (), NULL, 0);
704 case 2: /* overlaynums */
707 if (IsDigit (token[0]) == 0) break;
709 number = atoi (token);
710 if (number < 0 || number > 126) {
711 AbEnd ("Overlay number %i is out of range 0-126", number);
713 if (overlaytable[number] == 1) {
714 AbEnd ("Overlay number %i is defined twice", number);
717 overlaytable[number] = 1;
718 if (number > lastnumber) lastnumber = number;
720 } while (IsDigit (token[0]) != 0);
722 if (lastnumber == -1) {
723 AbEnd ("There must be at least one overlay number");
726 /* always include number 0 */
730 case 3: /* backbuffer */
731 switch (findToken (toggle, nextWord ())) {
733 AbEnd ("Unknown value for 'backbuffer'");
749 /* OK, all information is gathered, do flushout */
751 if (lastnumber != -1) {
752 fprintf (outputSFile,
753 "\t\t.segment \"RECORDS\"\n\n");
757 for (number = 0; number <= lastnumber; number++) {
758 fprintf (outputSFile,
760 overlaytable[number] == 1 ? "$00" : "$FF");
762 fprintf (outputSFile,
765 for (number = 0; number <= lastnumber; number++) {
766 if (overlaytable[number] == 1) {
767 fprintf (outputSFile,
768 "\t\t.segment \"VLIRIDX%i\"\n\n"
769 "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n\n"
771 "\t.byte .lobyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n"
773 "\t.byte .hibyte (__VLIR%i_LAST__ - __VLIR%i_START__%s)\n\n",
774 number, number, number,
775 number == 0 ? ", __BSS_SIZE__" : "",
777 number == 0 ? " - __BSS_SIZE__" : "",
779 number == 0 ? " - __BSS_SIZE__" : "");
785 for (number = 0; number <= lastnumber; number++) {
786 if (overlaytable[number] == 1) {
787 fprintf (outputSFile,
788 "\t.import __VLIR%i_START__, __VLIR%i_LAST__%s\n",
789 number, number, number == 0 ? ", __BSS_SIZE__" : "");
792 fprintf (outputSFile,
795 for (number = 0; number <= lastnumber; number++) {
796 if (overlaytable[number] == 1) {
797 fprintf (outputSFile,
798 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) / 254) + 1\n"
799 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__%s - 1) .MOD 254) + 2\n",
800 number, number, number == 0 ? " - __BSS_SIZE__" : "",
801 number, number, number == 0 ? " - __BSS_SIZE__" : "");
803 fprintf (outputSFile,
808 fprintf (outputSFile,
814 fprintf (outputCFile,
815 "extern void _OVERLAYADDR__[];\n"
816 "extern void _OVERLAYSIZE__[];\n\n"
817 "#define OVERLAY_ADDR (char*) _OVERLAYADDR__\n"
818 "#define OVERLAY_SIZE (unsigned)_OVERLAYSIZE__\n\n");
820 if (fclose (outputCFile) != 0) {
821 AbEnd ("Error closing %s: %s", outputCName, strerror (errno));
825 if (stacksize != -1) {
826 fprintf (outputSFile,
827 "\t.export __STACKSIZE__ : absolute = $%04x\n\n",
831 if (overlaysize != -1) {
832 fprintf (outputSFile,
833 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
837 if (backbuffer != -1) {
838 fprintf (outputSFile,
839 "\t.export __BACKBUFSIZE__ : absolute = $%04x\n\n",
840 backbuffer ? 0x2000 : 0x0000);
843 if (fclose (outputSFile) != 0) {
844 AbEnd ("Error closing %s: %s", outputSName, strerror (errno));
849 static char *filterInput (FILE *F, char *tbl)
851 /* loads file into buffer filtering it out */
852 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
856 if ((a == '\n') || (a == '\015')) a = ' ';
857 if (a == ',' && quote) a = ' ';
858 if (a == '\042') quote =! quote;
860 if ((a == '{') || (a == '(')) bracket++;
861 if ((a == '}') || (a == ')')) bracket--;
865 xrealloc (tbl, i + 1);
869 if ((prevchar != ' ') && (prevchar != -1)) {
874 if (a == ';' && quote) {
878 fseek (F, -1, SEEK_CUR);
886 if (bracket != 0) AbEnd ("There are unclosed brackets!");
892 static void processFile (const char *filename)
899 int head = 0; /* number of processed HEADER sections */
900 int memory = 0; /* number of processed MEMORY sections */
902 if ((F = fopen (filename, "r")) == 0) {
903 AbEnd ("Can't open file %s for reading: %s", filename, strerror (errno));
906 str = filterInput (F, xmalloc (BLOODY_BIG_BUFFER));
908 token = strtok (str, " ");
912 switch (findToken (mainToken, token)) {
918 AbEnd ("More than one HEADER section, aborting");
923 case 2: break; /* icon not implemented yet */
924 case 3: break; /* dialog not implemented yet */
927 AbEnd ("More than one MEMORY section, aborting");
933 AbEnd ("Unknown section '%s'", token);
938 } while (token != NULL);
942 int main (int argc, char *argv[])
944 /* Program long options */
945 static const LongOpt OptTab[] = {
946 { "--help", 0, OptHelp},
947 { "--target", 1, OptTarget},
948 { "--version", 0, OptVersion},
954 /* Initialize the cmdline module */
955 InitCmdLine (&argc, &argv, "grc65");
957 /* Check the parameters */
959 while (I < ArgCount) {
961 /* Get the argument */
962 const char* Arg = ArgVec [I];
964 /* Check for an option */
969 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
973 outputCName = GetArg (&I, 2);
977 outputSName = GetArg (&I, 2);
981 OptTarget (Arg, GetArg (&I, 2));
1000 if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
1001 if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
1010 if (ffile == 0) AbEnd ("No input file");
1012 return EXIT_SUCCESS;