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 */
136 static void OptTarget (const char* Opt attribute ((unused)), const char* Arg)
137 /* Set the target system */
139 switch (FindTarget(Arg)) {
150 AbEnd ("Unknown target system `%s'", Arg);
154 /* Target is known but unsupported */
155 AbEnd ("Unsupported target system `%s'", Arg);
163 static void OptVersion (const char* Opt attribute ((unused)),
164 const char* Arg attribute ((unused)))
165 /* Print the assembler version */
168 "grc65 V%s - (C) Copyright, Maciej 'YTM/Elysium' Witkowiak\n",
169 GetVersionAsString ());
174 void printCHeader(void) {
178 "//\tThis file was generated by the GEOS Resource Compiler\n"
180 "//\tDO NOT EDIT! Any changes will be lost!\n"
182 "//\tEdit proper resource file instead.\n"
187 void printSHeader(void) {
191 ";\tThis file was generated by the GEOS Resource Compiler\n"
193 ";\tDO NOT EDIT! Any changes will be lost!\n"
195 ";\tEdit proper resource file instead.\n"
200 void openCFile(void) {
202 if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
203 AbEnd ("Can't open file %s for writing: %s", outputCName, strerror(errno));
207 outputCMode[0] = 'a';
214 void openSFile(void) {
216 if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
217 AbEnd ("Can't open file %s for writing: %s", outputSName, strerror(errno));
221 outputSMode[0] = 'a';
228 int findToken(const char **tokenTbl, const char *token) {
230 /* takes as input table of tokens and token, returns position in table or -1 if not found */
233 while (strlen(tokenTbl[a]) != 0) {
234 if (strcmp(tokenTbl[a], token) == 0) break;
238 if (strlen(tokenTbl[a]) == 0) a = -1;
244 return strtok(NULL, "\"");
249 return strtok(NULL, " ");
253 void setLen(char *name, unsigned len) {
254 if (strlen(name) > len)
259 void fillOut(char *name, int len, char *filler) {
264 fprintf(outputSFile, "\t.byte \"%s\"\n", name);
268 fprintf(outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
273 char *bintos(unsigned char a, char out[7]) {
278 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
287 int getNameSize(const char *word) {
289 /* count length of a word using BSW 9 font table */
292 while (word[i] != '\0') {
293 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
303 int a, size, tmpsize, item = 0;
305 char namebuff[255] = "";
307 struct menuitem *curItem, *newItem;
311 myMenu.name = nextWord();
312 myMenu.left = atoi(nextWord());
313 myMenu.top = atoi(nextWord());
314 myMenu.type = nextWord();
316 if (strcmp(nextWord(), "{") != 0) {
317 AbEnd ("Menu '%s' description has no opening bracket!", myMenu.name);
319 curItem = xmalloc(sizeof(struct menuitem));
320 myMenu.item = curItem;
323 if (strcmp(token, "}") == 0) break;
324 if (token[strlen(token) - 1] != '"') {
325 strcpy(namebuff, token);
328 strcat(namebuff, " ");
329 strcat(namebuff, token);
330 } while (token[strlen(token) - 1] != '"');
331 token = xmalloc(strlen(namebuff));
332 strcpy(token, namebuff);
334 curItem->name = token;
335 curItem->type = nextWord();
336 curItem->target = nextWord();
337 newItem = xmalloc(sizeof(struct menuitem));
338 curItem->next = newItem;
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);
345 curItem->next = NULL;
347 /* count menu sizes */
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;
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;
366 myMenu.right = myMenu.left + size - 1;
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)) {
376 curItem=curItem->next;
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);
387 curItem = myMenu.item;
388 for (a = 0; a != item; a++) {
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))
398 curItem = curItem->next;
404 if (fclose(outputCFile) != 0)
405 AbEnd ("Error closing %s: %s", outputCName, strerror(errno));
409 void DoHeader(void) {
414 struct appheader myHead;
416 char i1[9], i2[9], i3[9];
423 a = findToken(hdrFTypes, token);
428 myHead.geostype = 0x82;
431 AbEnd ("Filetype '%s' is not supported yet", token);
439 myHead.geostype = 14;
442 AbEnd ("Filetype '%s' is not supported yet", token);
446 myHead.dosname = nextPhrase();
448 myHead.classname = nextPhrase();
450 myHead.version = nextPhrase();
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;
462 my_tm = localtime(&t);
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;
470 if (strcmp(nextWord(), "{") != 0) {
471 AbEnd ("Header '%s' has no opening bracket!", myHead.dosname);
476 if (strcmp(token, "}") == 0) break;
477 switch (a = findToken(hdrFields, token)) {
479 AbEnd ("Unknown field '%s' in header '%s'", token, myHead.dosname);
482 myHead.author = nextPhrase();
485 myHead.info = nextPhrase();
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());
494 case 3: /* dostype */
495 switch (b = findToken(hdrDOSTp, nextWord())) {
497 AbEnd ("Unknown dostype in header '%s'", myHead.dosname);
500 if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
505 switch (b = findToken(hdrModes, nextWord())) {
507 AbEnd ("Unknown mode in header '%s'", myHead.dosname);
509 if (apple == 0) myHead.mode = 0x40;
512 if (apple == 0) myHead.mode = 0x00;
515 if (apple == 0) myHead.mode = 0xc0;
518 if (apple == 0) myHead.mode = 0x80;
522 case 5: /* structure */
523 switch (b = findToken(hdrStructTp, nextWord())) {
525 AbEnd ("unknown structure type in header '%s'", myHead.dosname);
528 myHead.structure = 0;
532 myHead.structure = 1;
537 myHead.icon = nextPhrase();
541 } while (strcmp(token, "}") != 0);
543 /* OK, all information is gathered, do flushout */
547 "\t\t.segment \"DIRENTRY\"\n\n");
552 "\t.byte %i << 4 | %u\n",
553 myHead.structure + 2, (unsigned) strlen(myHead.dosname));
555 fillOut(myHead.dosname, 15, "0");
562 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
567 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
570 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min,
571 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min);
580 fillOut(myHead.dosname, 16, "$a0");
586 "\t.byte %i, %i, %i, %i, %i\n\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);
595 "\t\t.segment \"FILEINFO\"\n\n"
596 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
597 "\t.byte 3, 21, 63 | $80\n");
599 if (myHead.icon != NULL) {
601 "\t.incbin \"%s\", 0, 63\n",
604 for (a = 0; a != 63; a = a + 3) {
606 "\t.byte %%%s, %%%s, %%%s\n",
607 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
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);
616 fillOut(myHead.classname, 12, "$20");
618 fillOut(myHead.version, 4, "0");
625 setLen(myHead.author, 62);
629 "\t.res (63 - %i)\n\n",
630 myHead.author, (int)(strlen(myHead.author) + 1));
632 setLen(myHead.info, 95);
638 if (fclose (outputSFile) != 0)
639 AbEnd ("Error closing %s: %s", outputSName, strerror(errno));
646 int record, lastrecord;
647 int vlirsize, vlirtable[127];
651 vlirsize = strtol(nextWord(), NULL, 0);
653 if (strcmp(nextWord(), "{") != 0) {
654 AbEnd ("VLIR description has no opening bracket!");
658 memset(vlirtable, 0, sizeof(vlirtable));
662 if (strcmp(token, "}") == 0) break;
664 record = atoi(token);
665 if (record < 0 || record > 126) {
666 AbEnd ("VLIR record %i is out of range 0-126.", record);
668 if (vlirtable[record] == 1) {
669 AbEnd ("VLIR record %i is defined twice.", record);
672 vlirtable[record] = 1;
673 if (record > lastrecord) lastrecord = record;
674 } while (strcmp(token, "}") != 0);
676 if (lastrecord == -1) {
677 AbEnd ("There must be at least one VLIR record.");
680 /* always include record 0 */
683 /* OK, all information is gathered, do flushout */
687 "\t\t.segment \"RECORDS\"\n\n"
688 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
691 for (record = 0; record <= lastrecord; record++) {
692 if (vlirtable[record] == 1) {
694 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
701 for (record = 0; record <= lastrecord; record++) {
702 if (vlirtable[record] == 1) {
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);
716 if (fclose(outputSFile) != 0)
717 AbEnd ("Error closing %s: %s", outputSName, strerror(errno));
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");
727 if (fclose(outputCFile) != 0)
728 AbEnd ("Error closing %s: %s", outputCName, strerror(errno));
732 char *filterInput(FILE *F, char *tbl) {
734 /* loads file into buffer filtering it out */
735 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
739 if ((a == '\n') || (a == '\015')) a = ' ';
740 if (a == ',' && quote) a = ' ';
741 if (a == '\042') quote =! quote;
743 if ((a == '{') || (a == '(')) bracket++;
744 if ((a == '}') || (a == ')')) bracket--;
748 xrealloc(tbl, i + 1);
752 if ((prevchar != ' ') && (prevchar != -1)) {
757 if (a == ';' && quote) {
761 fseek(F, -1, SEEK_CUR);
769 if (bracket != 0) AbEnd ("There are unclosed brackets!");
775 void processFile(const char *filename) {
782 int head = 0; /* number of processed HEADER sections */
783 int vlir = 0; /* number of processed VLIR sections */
785 if ((F = fopen(filename, "r")) == 0) {
786 AbEnd ("Can't open file %s for reading: %s", filename, strerror(errno));
789 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
791 token = strtok(str, " ");
795 switch (findToken(mainToken, token)) {
801 AbEnd ("More than one HEADER section, aborting.");
806 case 2: break; /* icon not implemented yet */
807 case 3: break; /* dialog not implemented yet */
810 AbEnd ("More than one VLIR section, aborting.");
816 AbEnd ("Unknown section %s.",token);
821 } while (token != NULL);
825 int main(int argc, char *argv[]) {
827 /* Program long options */
828 static const LongOpt OptTab[] = {
829 { "--help", 0, OptHelp },
830 { "--target", 1, OptTarget },
831 { "--version", 0, OptVersion },
838 /* Initialize the cmdline module */
839 InitCmdLine (&argc, &argv, "grc65");
841 /* Check the parameters */
843 while (I < ArgCount) {
845 /* Get the argument */
846 const char* Arg = ArgVec [I];
848 /* Check for an option */
853 LongOption (&I, OptTab, sizeof(OptTab)/sizeof(OptTab[0]));
857 outputCName = GetArg (&I, 2);
861 outputSName = GetArg (&I, 2);
865 OptTarget (Arg, GetArg (&I, 2));
884 if (outputCName == NULL) outputCName = MakeFilename (Arg, ".h");
885 if (outputSName == NULL) outputSName = MakeFilename (Arg, ".s");
894 if (ffile == 0) AbEnd ("No input file");