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...)
31 /* I hope that no one will be able to create a .grc bigger than this... */
32 #define BLOODY_BIG_BUFFER 65000
34 /* there are no 6MB GEOS binaries... I hope! */
35 #define THIS_BUFFER_IS_SOOO_HUGE 6000000
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 char *ProgName; /* for AbEnd, later remove and use common/cmdline.h */
101 char *outputCName = NULL, *outputSName = NULL;
102 FILE *outputCFile, *outputSFile;
103 int CFnum = 0, SFnum = 0;
105 char outputCMode[2] = "w";
106 char outputSMode[2] = "w";
109 void printUsage(void) {
111 printf("Usage: %s [options] file\n"
113 "\t-h, -?\t\tthis help\n"
114 "\t-o name\t\tname C output file\n"
115 "\t-s name\t\tname asm output file\n"
116 "\t-t sys\t\tset target system\n",
121 void printCHeader(void) {
125 "//\tThis file was generated by the GEOS Resource Compiler\n"
127 "//\tDO NOT EDIT! Any changes will be lost!\n"
129 "//\tEdit proper resource file instead.\n"
134 void printSHeader(void) {
138 ";\tThis file was generated by the GEOS Resource Compiler\n"
140 ";\tDO NOT EDIT! Any changes will be lost!\n"
142 ";\tEdit proper resource file instead.\n"
147 void openCFile(void) {
149 if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
150 AbEnd("can't open file %s for writing: %s\n", outputCName, strerror(errno));
154 outputCMode[0] = 'a';
161 void openSFile(void) {
163 if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
164 AbEnd("can't open file %s for writing: %s\n", outputSName, strerror(errno));
168 outputSMode[0] = 'a';
175 int findToken(const char **tokenTbl, const char *token) {
177 /* takes as input table of tokens and token, returns position in table or -1 if not found */
180 while (strlen(tokenTbl[a]) != 0) {
181 if (strcmp(tokenTbl[a], token) == 0) break;
185 if (strlen(tokenTbl[a]) == 0) a = -1;
191 return strtok(NULL, "\"");
196 return strtok(NULL, " ");
200 void setLen(char *name, unsigned len) {
201 if (strlen(name) > len)
206 void fillOut(char *name, int len, char *filler) {
211 fprintf(outputSFile, "\t.byte \"%s\"\n", name);
215 fprintf(outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
220 char *bintos(unsigned char a, char out[7]) {
225 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
234 int getNameSize(const char *word) {
236 /* count length of a word using BSW 9 font table */
239 while (word[i] != '\0') {
240 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
250 int a, size, tmpsize, item = 0;
252 char namebuff[255] = "";
254 struct menuitem *curItem, *newItem;
258 myMenu.name = nextWord();
259 myMenu.left = atoi(nextWord());
260 myMenu.top = atoi(nextWord());
261 myMenu.type = nextWord();
263 if (strcmp(nextWord(), "{") != 0) {
264 AbEnd("menu '%s' description has no opening bracket!\n", myMenu.name);
266 curItem = xmalloc(sizeof(struct menuitem));
267 myMenu.item = curItem;
270 if (strcmp(token, "}") == 0) break;
271 if (token[strlen(token) - 1] != '"') {
272 strcpy(namebuff, token);
275 strcat(namebuff, " ");
276 strcat(namebuff, token);
277 } while (token[strlen(token) - 1] != '"');
278 token = xmalloc(strlen(namebuff));
279 strcpy(token, namebuff);
281 curItem->name = token;
282 curItem->type = nextWord();
283 curItem->target = nextWord();
284 newItem = xmalloc(sizeof(struct menuitem));
285 curItem->next = newItem;
288 } while (strcmp(token, "}") != 0);
289 if (item == 0) AbEnd("menu '%s' has 0 items!\n", myMenu.name);
290 if (item > 31) AbEnd("menu '%s' has too many items!\n", myMenu.name);
292 curItem->next = NULL;
294 /* count menu sizes */
296 curItem = myMenu.item;
297 if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
298 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
299 myMenu.bot = myMenu.top + 15;
300 for (a = 0; a != item; a++) {
301 size += getNameSize(curItem->name);
302 curItem = curItem->next;
305 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
306 myMenu.bot = myMenu.top + (14 * item);
307 for (a = 0; a != item; a++) {
308 tmpsize = getNameSize(curItem->name);
309 size = (size > tmpsize) ? size : tmpsize;
310 curItem = curItem->next;
313 myMenu.right = myMenu.left + size - 1;
315 curItem = myMenu.item;
316 for (a = 0; a != item; a++) {
317 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
318 if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
323 curItem=curItem->next;
328 "const void %s = {\n"
329 "\t(char)%i, (char)%i,\n"
330 "\t(int)%i, (int)%i,\n"
331 "\t(char)(%i | %s),\n",
332 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
334 curItem = myMenu.item;
335 for (a = 0; a != item; a++) {
337 "\t%s, (char)%s, (int)",
338 curItem->name, curItem->type);
339 if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
345 curItem = curItem->next;
351 if (fclose(outputCFile) != 0)
352 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
356 void DoHeader(void) {
361 struct appheader myHead;
363 char i1[9], i2[9], i3[9];
370 a = findToken(hdrFTypes, token);
375 myHead.geostype = 0x82;
378 AbEnd("filetype '%s' is not supported yet\n", token);
386 myHead.geostype = 14;
389 AbEnd("filetype '%s' is not supported yet\n", token);
393 myHead.dosname = nextPhrase();
395 myHead.classname = nextPhrase();
397 myHead.version = nextPhrase();
399 /* put default values into myHead here */
400 myHead.author = "cc65";
401 myHead.info = "Program compiled with cc65 and GEOSLib.";
402 myHead.dostype = 128;
403 if (apple == 0) myHead.dostype += 3;
404 myHead.structure = 0;
409 my_tm = localtime(&t);
411 myHead.year = my_tm->tm_year;
412 myHead.month = my_tm->tm_mon+1;
413 myHead.day = my_tm->tm_mday;
414 myHead.hour = my_tm->tm_hour;
415 myHead.min = my_tm->tm_min;
417 if (strcmp(nextWord(), "{") != 0) {
418 AbEnd("header '%s' has no opening bracket!\n", myHead.dosname);
423 if (strcmp(token, "}") == 0) break;
424 switch (a = findToken(hdrFields, token)) {
426 AbEnd("unknown field '%s' in header '%s'\n", token, myHead.dosname);
429 myHead.author = nextPhrase();
432 myHead.info = nextPhrase();
435 myHead.year = atoi(nextWord());
436 myHead.month = atoi(nextWord());
437 myHead.day = atoi(nextWord());
438 myHead.hour = atoi(nextWord());
439 myHead.min = atoi(nextWord());
441 case 3: /* dostype */
442 switch (b = findToken(hdrDOSTp, nextWord())) {
444 AbEnd("unknown dostype in header '%s'\n", myHead.dosname);
447 if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
452 switch (b = findToken(hdrModes, nextWord())) {
454 AbEnd("unknown mode in header '%s'\n", myHead.dosname);
456 if (apple == 0) myHead.mode = 0x40;
459 if (apple == 0) myHead.mode = 0x00;
462 if (apple == 0) myHead.mode = 0xc0;
465 if (apple == 0) myHead.mode = 0x80;
469 case 5: /* structure */
470 switch (b = findToken(hdrStructTp, nextWord())) {
472 AbEnd("unknown structure type in header '%s'\n", myHead.dosname);
475 myHead.structure = 0;
479 myHead.structure = 1;
484 myHead.icon = nextPhrase();
488 } while (strcmp(token, "}") != 0);
490 /* OK, all information is gathered, do flushout */
494 "\t\t.segment \"DIRENTRY\"\n\n");
499 "\t.byte %i << 4 | %i\n",
500 myHead.structure + 2, strlen(myHead.dosname));
502 fillOut(myHead.dosname, 15, "0");
509 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
514 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
517 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min,
518 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min);
527 fillOut(myHead.dosname, 16, "$a0");
533 "\t.byte %i, %i, %i, %i, %i\n\n"
535 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
536 myHead.structure, myHead.geostype,
537 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
542 "\t\t.segment \"FILEINFO\"\n\n"
543 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
544 "\t.byte 3, 21, 63 | $80\n");
546 if (myHead.icon != NULL) {
548 "\t.incbin \"%s\", 0, 63\n",
551 for (a = 0; a != 63; a = a + 3) {
553 "\t.byte %%%s, %%%s, %%%s\n",
554 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
559 "\t.byte %i, %i, %i\n"
560 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
561 myHead.dostype, myHead.geostype, myHead.structure);
563 fillOut(myHead.classname, 12, "$20");
565 fillOut(myHead.version, 4, "0");
572 setLen(myHead.author, 62);
576 "\t.res (63 - %i)\n\n",
577 myHead.author, (int)(strlen(myHead.author) + 1));
579 setLen(myHead.info, 95);
585 if (fclose (outputSFile) != 0)
586 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
593 int record, lastrecord;
594 int vlirsize, vlirtable[127];
598 vlirsize = strtol(nextWord(), NULL, 0);
600 if (strcmp(nextWord(), "{") != 0) {
601 AbEnd ("VLIR description has no opening bracket!\n");
605 memset(vlirtable, 0, sizeof(vlirtable));
609 if (strcmp(token, "}") == 0) break;
611 record = atoi(token);
612 if (record < 0 || record > 126) {
613 AbEnd("VLIR record %i is out of range 0-126.\n", record);
615 if (vlirtable[record] == 1) {
616 AbEnd("VLIR record %i is defined twice.\n", record);
619 vlirtable[record] = 1;
620 if (record > lastrecord) lastrecord = record;
621 } while (strcmp(token, "}") != 0);
623 if (lastrecord == -1) {
624 AbEnd("There must be at least one VLIR record.\n");
627 /* always include record 0 */
630 /* OK, all information is gathered, do flushout */
634 "\t\t.segment \"RECORDS\"\n\n"
635 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
638 for (record = 0; record <= lastrecord; record++) {
639 if (vlirtable[record] == 1) {
641 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
648 for (record = 0; record <= lastrecord; record++) {
649 if (vlirtable[record] == 1) {
651 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
652 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
653 record, record, record, record);
663 if (fclose(outputSFile) != 0)
664 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
669 "extern void _OVERLAYADDR__;\n"
670 "extern void _OVERLAYSIZE__;\n\n"
671 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
672 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
674 if (fclose(outputCFile) != 0)
675 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
679 char *filterInput(FILE *F, char *tbl) {
681 /* loads file into buffer filtering it out */
682 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
686 if ((a == '\n') || (a == '\015')) a = ' ';
687 if (a == ',' && quote) a = ' ';
688 if (a == '\042') quote =! quote;
690 if ((a == '{') || (a == '(')) bracket++;
691 if ((a == '}') || (a == ')')) bracket--;
695 xrealloc(tbl, i + 1);
699 if ((prevchar != ' ') && (prevchar != -1)) {
704 if (a == ';' && quote) {
708 fseek(F, -1, SEEK_CUR);
716 if (bracket != 0) AbEnd("there are unclosed brackets!\n");
722 void processFile(const char *filename) {
729 int head = 0; /* number of processed HEADER sections */
730 int vlir = 0; /* number of processed VLIR sections */
732 if ((F = fopen(filename, "r")) == 0) {
733 AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
736 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
738 token = strtok(str, " ");
742 switch (findToken(mainToken, token)) {
748 AbEnd("more than one HEADER section, aborting.\n");
753 case 2: break; /* icon not implemented yet */
754 case 3: break; /* dialog not implemented yet */
757 AbEnd("more than one VLIR section, aborting.\n");
763 AbEnd("unknown section %s.\n",token);
768 } while (token != NULL);
772 int main(int argc, char *argv[]) {
774 int ffile = 0, i = 1;
779 const char *arg = argv[i];
784 outputCName = argv[++i];
787 outputSName = argv[++i];
790 switch (FindTarget(argv[++i])) {
791 case TGT_GEOS: /* todo: TGT_GEOS-CBM */
794 case TGT_COUNT: /* todo: TGT_GEOS-APPLE */
798 AbEnd("unknown target system type %s\n", argv[i]);
807 AbEnd("unknown option %s\n", arg);
812 if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
813 if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
821 if (ffile == 0) AbEnd("no input file\n");