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
40 struct menuitem *next;
48 struct menuitem *item;
52 int year, month, day, hour, min;
65 const char *mainToken[] = {"MENU", "HEADER", "ICON", "DIALOG", "VLIR", ""};
67 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
68 "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
70 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
72 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
74 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
76 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
78 const int BSWTab[] = {0, 0x005, 0x007, 0x00b, 0x011, 0x017, 0x01d, 0x023,
79 0x025, 0x029, 0x02d, 0x033, 0x039, 0x03c, 0x041, 0x043, 0x04a, 0x04f,
80 0x052, 0x056, 0x05a, 0x05f, 0x063, 0x068, 0x06d, 0x072, 0x077, 0x079,
81 0x07c, 0x080, 0x084, 0x088, 0x08e, 0x094, 0x09a, 0x09f, 0x0a4, 0x0a9,
82 0x0ad, 0x0b1, 0x0b6, 0x0bc, 0x0be, 0x0c2, 0x0c8, 0x0cc, 0x0d4, 0x0da,
83 0x0e0, 0x0e5, 0x0eb, 0x0f0, 0x0f5, 0x0f9, 0x0fe, 0x104, 0x10c, 0x112,
84 0x118, 0x11e, 0x121, 0x129, 0x12c, 0x132, 0x13a, 0x13e, 0x143, 0x148,
85 0x14d, 0x152, 0x157, 0x15a, 0x15f, 0x164, 0x166, 0x168, 0x16d, 0x16f,
86 0x177, 0x17c, 0x182, 0x187, 0x18c, 0x18f, 0x193, 0x196, 0x19b, 0x1a1,
87 0x1a9, 0x1af, 0x1b4, 0x1ba, 0x1be, 0x1c0, 0x1c4, 0x1ca, 0x1d2, 0x1dd};
89 const unsigned char icon1[] = {255, 255, 255, 128, 0, 1, 128, 0, 1,
90 128, 0, 1, 128, 0, 1, 128, 0, 1,
91 128, 0, 1, 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, 255, 255, 255};
97 char *ProgName; /* for AbEnd, later remove and use common/cmdline.h */
99 char *outputCName = NULL, *outputSName = NULL;
100 FILE *outputCFile, *outputSFile;
101 int CFnum = 0, SFnum = 0;
103 char outputCMode[2] = "w";
104 char outputSMode[2] = "w";
107 void printUsage(void) {
109 printf("Usage: %s [options] file\n"
111 "\t-h, -?\t\tthis help\n"
112 "\t-o name\t\tname C output file\n"
113 "\t-s name\t\tname asm output file\n"
114 "\t-t sys\t\tset target system\n",
119 void printCHeader(void) {
123 "//\tThis file was generated by the GEOS Resource Compiler\n"
125 "//\tDO NOT EDIT! Any changes will be lost!\n"
127 "//\tEdit proper resource file instead.\n"
132 void printSHeader(void) {
136 ";\tThis file was generated by the GEOS Resource Compiler\n"
138 ";\tDO NOT EDIT! Any changes will be lost!\n"
140 ";\tEdit proper resource file instead.\n"
145 void openCFile(void) {
147 if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
148 AbEnd("can't open file %s for writing: %s\n", outputCName, strerror(errno));
152 outputCMode[0] = 'a';
159 void openSFile(void) {
161 if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
162 AbEnd("can't open file %s for writing: %s\n", outputSName, strerror(errno));
166 outputSMode[0] = 'a';
173 int findToken(const char **tokenTbl, const char *token) {
175 /* takes as input table of tokens and token, returns position in table or -1 if not found */
178 while (strlen(tokenTbl[a]) != 0) {
179 if (strcmp(tokenTbl[a], token) == 0) break;
183 if (strlen(tokenTbl[a]) == 0) a = -1;
189 return strtok(NULL, "\"");
194 return strtok(NULL, " ");
198 void setLen(char *name, unsigned len) {
199 if (strlen(name) > len)
204 void fillOut(char *name, int len, char *filler) {
209 fprintf(outputSFile, "\t.byte \"%s\"\n", name);
213 fprintf(outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
218 char *bintos(unsigned char a, char out[7]) {
223 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
232 int getNameSize(const char *word) {
234 /* count length of a word using BSW 9 font table */
237 while (word[i] != '\0') {
238 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
248 int a, size, tmpsize, item = 0;
250 char namebuff[255] = "";
252 struct menuitem *curItem, *newItem;
256 myMenu.name = nextWord();
257 myMenu.left = atoi(nextWord());
258 myMenu.top = atoi(nextWord());
259 myMenu.type = nextWord();
261 if (strcmp(nextWord(), "{") != 0) {
262 AbEnd("menu '%s' description has no opening bracket!\n", myMenu.name);
264 curItem = xmalloc(sizeof(struct menuitem));
265 myMenu.item = curItem;
268 if (strcmp(token, "}") == 0) break;
269 if (token[strlen(token) - 1] != '"') {
270 strcpy(namebuff, token);
273 strcat(namebuff, " ");
274 strcat(namebuff, token);
275 } while (token[strlen(token) - 1] != '"');
276 token = xmalloc(strlen(namebuff));
277 strcpy(token, namebuff);
279 curItem->name = token;
280 curItem->type = nextWord();
281 curItem->target = nextWord();
282 newItem = xmalloc(sizeof(struct menuitem));
283 curItem->next = newItem;
286 } while (strcmp(token, "}") != 0);
287 if (item == 0) AbEnd("menu '%s' has 0 items!\n", myMenu.name);
288 if (item > 31) AbEnd("menu '%s' has too many items!\n", myMenu.name);
290 curItem->next = NULL;
292 /* count menu sizes */
294 curItem = myMenu.item;
295 if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
296 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
297 myMenu.bot = myMenu.top + 15;
298 for (a = 0; a != item; a++) {
299 size += getNameSize(curItem->name);
300 curItem = curItem->next;
303 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
304 myMenu.bot = myMenu.top + (14 * item);
305 for (a = 0; a != item; a++) {
306 tmpsize = getNameSize(curItem->name);
307 size = (size > tmpsize) ? size : tmpsize;
308 curItem = curItem->next;
311 myMenu.right = myMenu.left + size - 1;
313 curItem = myMenu.item;
314 for (a = 0; a != item; a++) {
315 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
316 if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
321 curItem=curItem->next;
326 "const void %s = {\n"
327 "\t(char)%i, (char)%i,\n"
328 "\t(int)%i, (int)%i,\n"
329 "\t(char)(%i | %s),\n",
330 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
332 curItem = myMenu.item;
333 for (a = 0; a != item; a++) {
335 "\t%s, (char)%s, (int)",
336 curItem->name, curItem->type);
337 if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
343 curItem = curItem->next;
349 if (fclose(outputCFile) != 0)
350 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
354 void DoHeader(void) {
359 struct appheader myHead;
361 char i1[9], i2[9], i3[9];
368 a = findToken(hdrFTypes, token);
373 myHead.geostype = 0x82;
376 AbEnd("filetype '%s' is not supported yet\n", token);
384 myHead.geostype = 14;
387 AbEnd("filetype '%s' is not supported yet\n", token);
391 myHead.dosname = nextPhrase();
393 myHead.classname = nextPhrase();
395 myHead.version = nextPhrase();
397 /* put default values into myHead here */
398 myHead.author = "cc65";
399 myHead.info = "Program compiled with cc65 and GEOSLib.";
400 myHead.dostype = 128;
401 if (apple == 0) myHead.dostype += 3;
402 myHead.structure = 0;
407 my_tm = localtime(&t);
409 myHead.year = my_tm->tm_year;
410 myHead.month = my_tm->tm_mon+1;
411 myHead.day = my_tm->tm_mday;
412 myHead.hour = my_tm->tm_hour;
413 myHead.min = my_tm->tm_min;
415 if (strcmp(nextWord(), "{") != 0) {
416 AbEnd("header '%s' has no opening bracket!\n", myHead.dosname);
421 if (strcmp(token, "}") == 0) break;
422 switch (a = findToken(hdrFields, token)) {
424 AbEnd("unknown field '%s' in header '%s'\n", token, myHead.dosname);
427 myHead.author = nextPhrase();
430 myHead.info = nextPhrase();
433 myHead.year = atoi(nextWord());
434 myHead.month = atoi(nextWord());
435 myHead.day = atoi(nextWord());
436 myHead.hour = atoi(nextWord());
437 myHead.min = atoi(nextWord());
439 case 3: /* dostype */
440 switch (b = findToken(hdrDOSTp, nextWord())) {
442 AbEnd("unknown dostype in header '%s'\n", myHead.dosname);
445 if (apple == 0) myHead.dostype = b / 2 + 128 + 1;
450 switch (b = findToken(hdrModes, nextWord())) {
452 AbEnd("unknown mode in header '%s'\n", myHead.dosname);
454 if (apple == 0) myHead.mode = 0x40;
457 if (apple == 0) myHead.mode = 0x00;
460 if (apple == 0) myHead.mode = 0xc0;
463 if (apple == 0) myHead.mode = 0x80;
467 case 5: /* structure */
468 switch (b = findToken(hdrStructTp, nextWord())) {
470 AbEnd("unknown structure type in header '%s'\n", myHead.dosname);
473 myHead.structure = 0;
477 myHead.structure = 1;
482 myHead.icon = nextPhrase();
486 } while (strcmp(token, "}") != 0);
488 /* OK, all information is gathered, do flushout */
492 "\t\t.segment \"DIRENTRY\"\n\n");
497 "\t.byte %i << 4 | %u\n",
498 myHead.structure + 2, (unsigned) strlen(myHead.dosname));
500 fillOut(myHead.dosname, 15, "0");
507 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
512 "\t.word %i << 9 | %i << 5 | %i, %i << 8 | %i\n"
515 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min,
516 myHead.year % 100, myHead.month, myHead.day, myHead.hour, myHead.min);
525 fillOut(myHead.dosname, 16, "$a0");
531 "\t.byte %i, %i, %i, %i, %i\n\n"
533 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
534 myHead.structure, myHead.geostype,
535 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
540 "\t\t.segment \"FILEINFO\"\n\n"
541 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
542 "\t.byte 3, 21, 63 | $80\n");
544 if (myHead.icon != NULL) {
546 "\t.incbin \"%s\", 0, 63\n",
549 for (a = 0; a != 63; a = a + 3) {
551 "\t.byte %%%s, %%%s, %%%s\n",
552 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
557 "\t.byte %i, %i, %i\n"
558 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
559 myHead.dostype, myHead.geostype, myHead.structure);
561 fillOut(myHead.classname, 12, "$20");
563 fillOut(myHead.version, 4, "0");
570 setLen(myHead.author, 62);
574 "\t.res (63 - %i)\n\n",
575 myHead.author, (int)(strlen(myHead.author) + 1));
577 setLen(myHead.info, 95);
583 if (fclose (outputSFile) != 0)
584 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
591 int record, lastrecord;
592 int vlirsize, vlirtable[127];
596 vlirsize = strtol(nextWord(), NULL, 0);
598 if (strcmp(nextWord(), "{") != 0) {
599 AbEnd ("VLIR description has no opening bracket!\n");
603 memset(vlirtable, 0, sizeof(vlirtable));
607 if (strcmp(token, "}") == 0) break;
609 record = atoi(token);
610 if (record < 0 || record > 126) {
611 AbEnd("VLIR record %i is out of range 0-126.\n", record);
613 if (vlirtable[record] == 1) {
614 AbEnd("VLIR record %i is defined twice.\n", record);
617 vlirtable[record] = 1;
618 if (record > lastrecord) lastrecord = record;
619 } while (strcmp(token, "}") != 0);
621 if (lastrecord == -1) {
622 AbEnd("There must be at least one VLIR record.\n");
625 /* always include record 0 */
628 /* OK, all information is gathered, do flushout */
632 "\t\t.segment \"RECORDS\"\n\n"
633 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
636 for (record = 0; record <= lastrecord; record++) {
637 if (vlirtable[record] == 1) {
639 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
646 for (record = 0; record <= lastrecord; record++) {
647 if (vlirtable[record] == 1) {
649 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
650 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
651 record, record, record, record);
661 if (fclose(outputSFile) != 0)
662 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
667 "extern void _OVERLAYADDR__;\n"
668 "extern void _OVERLAYSIZE__;\n\n"
669 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
670 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
672 if (fclose(outputCFile) != 0)
673 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
677 char *filterInput(FILE *F, char *tbl) {
679 /* loads file into buffer filtering it out */
680 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
684 if ((a == '\n') || (a == '\015')) a = ' ';
685 if (a == ',' && quote) a = ' ';
686 if (a == '\042') quote =! quote;
688 if ((a == '{') || (a == '(')) bracket++;
689 if ((a == '}') || (a == ')')) bracket--;
693 xrealloc(tbl, i + 1);
697 if ((prevchar != ' ') && (prevchar != -1)) {
702 if (a == ';' && quote) {
706 fseek(F, -1, SEEK_CUR);
714 if (bracket != 0) AbEnd("there are unclosed brackets!\n");
720 void processFile(const char *filename) {
727 int head = 0; /* number of processed HEADER sections */
728 int vlir = 0; /* number of processed VLIR sections */
730 if ((F = fopen(filename, "r")) == 0) {
731 AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
734 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
736 token = strtok(str, " ");
740 switch (findToken(mainToken, token)) {
746 AbEnd("more than one HEADER section, aborting.\n");
751 case 2: break; /* icon not implemented yet */
752 case 3: break; /* dialog not implemented yet */
755 AbEnd("more than one VLIR section, aborting.\n");
761 AbEnd("unknown section %s.\n",token);
766 } while (token != NULL);
770 int main(int argc, char *argv[]) {
772 int ffile = 0, i = 1;
777 const char *arg = argv[i];
782 outputCName = argv[++i];
785 outputSName = argv[++i];
788 switch (FindTarget(argv[++i])) {
796 AbEnd("unknown target system type %s\n", argv[i]);
805 AbEnd("unknown option %s\n", arg);
810 if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
811 if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
819 if (ffile == 0) AbEnd("no input file\n");