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);
377 myHead.geostype = 14;
380 AbEnd("filetype '%s' is not supported yet\n", token);
383 myHead.dosname = nextPhrase();
385 myHead.classname = nextPhrase();
387 myHead.version = nextPhrase();
389 /* put default values into myHead here */
390 myHead.author = "cc65";
391 myHead.info = "Program compiled with cc65 and GEOSLib.";
392 myHead.dostype = 128 + 3;
393 myHead.structure = 0;
398 my_tm = localtime(&t);
400 myHead.year = my_tm->tm_year;
401 myHead.month = my_tm->tm_mon+1;
402 myHead.day = my_tm->tm_mday;
403 myHead.hour = my_tm->tm_hour;
404 myHead.min = my_tm->tm_min;
406 if (strcmp(nextWord(), "{") != 0) {
407 AbEnd("header '%s' has no opening bracket!\n", myHead.dosname);
412 if (strcmp(token, "}") == 0) break;
413 switch (a = findToken(hdrFields, token)) {
415 AbEnd("unknown field '%s' in header '%s'\n", token, myHead.dosname);
418 myHead.author = nextPhrase();
421 myHead.info = nextPhrase();
424 myHead.year = atoi(nextWord());
425 myHead.month = atoi(nextWord());
426 myHead.day = atoi(nextWord());
427 myHead.hour = atoi(nextWord());
428 myHead.min = atoi(nextWord());
430 case 3: /* dostype */
431 switch (b = findToken(hdrDOSTp, nextWord())) {
433 AbEnd("unknown dostype in header '%s'\n", myHead.dosname);
436 myHead.dostype = b / 2 + 128 + 1;
441 switch (b = findToken(hdrModes, nextWord())) {
443 AbEnd("unknown mode in header '%s'\n", myHead.dosname);
458 case 5: /* structure */
459 switch (b = findToken(hdrStructTp, nextWord())) {
461 AbEnd("unknown structure type in header '%s'\n", myHead.dosname);
464 myHead.structure = 0;
468 myHead.structure = 1;
473 myHead.icon = nextPhrase();
477 } while (strcmp(token, "}") != 0);
479 /* OK, all information is gathered, do flushout */
483 "\t\t.segment \"DIRENTRY\"\n\n"
485 "\t.word 0\n", myHead.dostype);
487 fillOut(myHead.dosname, 16, "$a0");
493 "\t.byte %i, %i, %i, %i, %i\n\n"
495 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
496 myHead.structure, myHead.geostype,
497 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
501 "\t\t.segment \"FILEINFO\"\n\n"
502 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
503 "\t.byte 3, 21, 63 | $80\n");
505 if (myHead.icon != NULL) {
507 "\t.incbin \"%s\", 0, 63\n",
510 for (a = 0; a != 63; a = a + 3) {
512 "\t.byte %%%s, %%%s, %%%s\n",
513 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
518 "\t.byte %i, %i, %i\n"
519 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
520 myHead.dostype, myHead.geostype, myHead.structure);
522 fillOut(myHead.classname, 12, "$20");
524 fillOut(myHead.version, 4, "0");
528 "\t.byte %i\n\n", myHead.mode);
530 setLen(myHead.author, 62);
534 "\t.res (63 - %i)\n\n",
535 myHead.author, (int)(strlen(myHead.author) + 1));
537 setLen(myHead.info, 95);
540 "\t.byte 0\n\n", myHead.info);
542 if (fclose (outputSFile) != 0)
543 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
550 int record, lastrecord;
551 int vlirsize, vlirtable[127];
555 vlirsize = strtol(nextWord(), NULL, 0);
557 if (strcmp(nextWord(), "{") != 0) {
558 AbEnd ("VLIR description has no opening bracket!\n");
562 memset(vlirtable, 0, sizeof(vlirtable));
566 if (strcmp(token, "}") == 0) break;
568 record = atoi(token);
569 if (record < 0 || record > 126) {
570 AbEnd("VLIR record %i is out of range 0-126.\n", record);
572 if (vlirtable[record] == 1) {
573 AbEnd("VLIR record %i is defined twice.\n", record);
576 vlirtable[record] = 1;
577 if (record > lastrecord) lastrecord = record;
578 } while (strcmp(token, "}") != 0);
580 if (lastrecord == -1) {
581 AbEnd("There must be at least one VLIR record.\n");
584 /* always include record 0 */
587 /* OK, all information is gathered, do flushout */
591 "\t\t.segment \"RECORDS\"\n\n"
592 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
595 for (record = 0; record <= lastrecord; record++) {
596 if (vlirtable[record] == 1) {
598 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
605 for (record = 0; record <= lastrecord; record++) {
606 if (vlirtable[record] == 1) {
608 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
609 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
610 record, record, record, record);
620 if (fclose(outputSFile) != 0)
621 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
626 "extern void _OVERLAYADDR__;\n"
627 "extern void _OVERLAYSIZE__;\n\n"
628 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
629 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
631 if (fclose(outputCFile) != 0)
632 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
636 char *filterInput(FILE *F, char *tbl) {
638 /* loads file into buffer filtering it out */
639 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
643 if ((a == '\n') || (a == '\015')) a = ' ';
644 if (a == ',' && quote) a = ' ';
645 if (a == '\042') quote =! quote;
647 if ((a == '{') || (a == '(')) bracket++;
648 if ((a == '}') || (a == ')')) bracket--;
652 xrealloc(tbl, i + 1);
656 if ((prevchar != ' ') && (prevchar != -1)) {
661 if (a == ';' && quote) {
665 fseek(F, -1, SEEK_CUR);
673 if (bracket != 0) AbEnd("there are unclosed brackets!\n");
679 void processFile(const char *filename) {
686 int head = 0; /* number of processed HEADER sections */
687 int vlir = 0; /* number of processed VLIR sections */
689 if ((F = fopen(filename, "r")) == 0) {
690 AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
693 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
695 token = strtok(str, " ");
699 switch (findToken(mainToken, token)) {
705 AbEnd("more than one HEADER section, aborting.\n");
710 case 2: break; /* icon not implemented yet */
711 case 3: break; /* dialog not implemented yet */
714 AbEnd("more than one VLIR section, aborting.\n");
720 AbEnd("unknown section %s.\n",token);
725 } while (token != NULL);
729 int main(int argc, char *argv[]) {
731 int ffile = 0, i = 1;
736 const char *arg = argv[i];
741 outputCName = argv[++i];
744 outputSName = argv[++i];
747 switch (FindTarget(argv[++i])) {
748 case TGT_GEOS: // todo: TGT_GEOS-CBM
751 case TGT_COUNT: //todo: TGT_GEOS-APPLE
755 AbEnd("unknown target system type %s\n", argv[i]);
764 AbEnd("unknown option %s\n", arg);
769 if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
770 if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
778 if (ffile == 0) AbEnd("no input file\n");