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...)
30 /* I hope that no one will be able to create a .grc bigger than this... */
31 #define BLOODY_BIG_BUFFER 65000
33 /* there are no 6MB GEOS binaries... I hope! */
34 #define THIS_BUFFER_IS_SOOO_HUGE 6000000
41 struct menuitem *next;
49 struct menuitem *item;
53 int year, month, day, hour, min;
66 const char *mainToken[] = {"MENU", "HEADER", "ICON", "DIALOG", "VLIR", ""};
68 const char *hdrFTypes[] = {"APPLICATION", "AUTO_EXEC", "DESK_ACC", "ASSEMBLY",
69 "DISK_DEVICE", "PRINTER", "SYSTEM", ""};
71 const char *hdrFields[] = {"author", "info", "date", "dostype", "mode", "structure", "icon", ""};
73 const char *hdrDOSTp[] = {"seq", "SEQ", "prg", "PRG", "usr", "USR", ""};
75 const char *hdrStructTp[] = {"seq", "SEQ", "vlir", "VLIR", ""};
77 const char *hdrModes[] = {"any", "40only", "80only", "c64only", ""};
79 const int BSWTab[] = {0, 0x005, 0x007, 0x00b, 0x011, 0x017, 0x01d, 0x023,
80 0x025, 0x029, 0x02d, 0x033, 0x039, 0x03c, 0x041, 0x043, 0x04a, 0x04f,
81 0x052, 0x056, 0x05a, 0x05f, 0x063, 0x068, 0x06d, 0x072, 0x077, 0x079,
82 0x07c, 0x080, 0x084, 0x088, 0x08e, 0x094, 0x09a, 0x09f, 0x0a4, 0x0a9,
83 0x0ad, 0x0b1, 0x0b6, 0x0bc, 0x0be, 0x0c2, 0x0c8, 0x0cc, 0x0d4, 0x0da,
84 0x0e0, 0x0e5, 0x0eb, 0x0f0, 0x0f5, 0x0f9, 0x0fe, 0x104, 0x10c, 0x112,
85 0x118, 0x11e, 0x121, 0x129, 0x12c, 0x132, 0x13a, 0x13e, 0x143, 0x148,
86 0x14d, 0x152, 0x157, 0x15a, 0x15f, 0x164, 0x166, 0x168, 0x16d, 0x16f,
87 0x177, 0x17c, 0x182, 0x187, 0x18c, 0x18f, 0x193, 0x196, 0x19b, 0x1a1,
88 0x1a9, 0x1af, 0x1b4, 0x1ba, 0x1be, 0x1c0, 0x1c4, 0x1ca, 0x1d2, 0x1dd};
90 const unsigned char icon1[] = {255, 255, 255, 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, 128, 0, 1,
96 128, 0, 1, 128, 0, 1, 255, 255, 255};
98 char *ProgName; /* for AbEnd, later remove and use common/cmdline.h */
100 char *outputCName = NULL, *outputSName = NULL;
101 FILE *outputCFile, *outputSFile;
102 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",
118 void printCHeader(void) {
122 "//\tThis file was generated by the GEOS Resource Compiler\n"
124 "//\tDO NOT EDIT! Any changes will be lost!\n"
126 "//\tEdit proper resource file instead.\n"
131 void printSHeader(void) {
135 ";\tThis file was generated by the GEOS Resource Compiler\n"
137 ";\tDO NOT EDIT! Any changes will be lost!\n"
139 ";\tEdit proper resource file instead.\n"
144 void openCFile(void) {
146 if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
147 AbEnd("can't open file %s for writing: %s\n", outputCName, strerror(errno));
151 outputCMode[0] = 'a';
158 void openSFile(void) {
160 if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
161 AbEnd("can't open file %s for writing: %s\n", outputSName, strerror(errno));
165 outputSMode[0] = 'a';
172 int findToken(const char **tokenTbl, const char *token) {
174 /* takes as input table of tokens and token, returns position in table or -1 if not found */
177 while (strlen(tokenTbl[a]) != 0) {
178 if (strcmp(tokenTbl[a], token) == 0) break;
182 if (strlen(tokenTbl[a]) == 0) a = -1;
188 return strtok(NULL, "\"");
193 return strtok(NULL, " ");
197 void setLen(char *name, unsigned len) {
198 if (strlen(name) > len)
203 void fillOut(char *name, int len, char *filler) {
208 fprintf(outputSFile, "\t.byte \"%s\"\n", name);
212 fprintf(outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
217 char *bintos(unsigned char a, char out[7]) {
222 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
231 int getNameSize(const char *word) {
233 /* count length of a word using BSW 9 font table */
236 while (word[i] != '\0') {
237 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
247 int a, size, tmpsize, item = 0;
249 char namebuff[255] = "";
251 struct menuitem *curItem, *newItem;
255 myMenu.name = nextWord();
256 myMenu.left = atoi(nextWord());
257 myMenu.top = atoi(nextWord());
258 myMenu.type = nextWord();
260 if (strcmp(nextWord(), "{") != 0) {
261 AbEnd("menu '%s' description has no opening bracket!\n", myMenu.name);
263 curItem = xmalloc(sizeof(struct menuitem));
264 myMenu.item = curItem;
267 if (strcmp(token, "}") == 0) break;
268 if (token[strlen(token) - 1] != '"') {
269 strcpy(namebuff, token);
272 strcat(namebuff, " ");
273 strcat(namebuff, token);
274 } while (token[strlen(token) - 1] != '"');
275 token = xmalloc(strlen(namebuff));
276 strcpy(token, namebuff);
278 curItem->name = token;
279 curItem->type = nextWord();
280 curItem->target = nextWord();
281 newItem = xmalloc(sizeof(struct menuitem));
282 curItem->next = newItem;
285 } while (strcmp(token, "}") != 0);
286 if (item == 0) AbEnd("menu '%s' has 0 items!\n", myMenu.name);
287 if (item > 31) AbEnd("menu '%s' has too many items!\n", myMenu.name);
289 curItem->next = NULL;
291 /* count menu sizes */
293 curItem = myMenu.item;
294 if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
295 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
296 myMenu.bot = myMenu.top + 15;
297 for (a = 0; a != item; a++) {
298 size += getNameSize(curItem->name);
299 curItem = curItem->next;
302 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
303 myMenu.bot = myMenu.top + (14 * item);
304 for (a = 0; a != item; a++) {
305 tmpsize = getNameSize(curItem->name);
306 size = (size > tmpsize) ? size : tmpsize;
307 curItem = curItem->next;
310 myMenu.right = myMenu.left + size - 1;
312 curItem = myMenu.item;
313 for (a = 0; a != item; a++) {
314 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
315 if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
320 curItem=curItem->next;
325 "const void %s = {\n"
326 "\t(char)%i, (char)%i,\n"
327 "\t(int)%i, (int)%i,\n"
328 "\t(char)(%i | %s),\n",
329 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
331 curItem = myMenu.item;
332 for (a = 0; a != item; a++) {
334 "\t%s, (char)%s, (int)",
335 curItem->name, curItem->type);
336 if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
342 curItem = curItem->next;
348 if (fclose(outputCFile) != 0)
349 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
353 void DoHeader(void) {
358 struct appheader myHead;
360 char i1[9], i2[9], i3[9];
367 a = findToken(hdrFTypes, token);
374 myHead.geostype = 14;
377 AbEnd("filetype '%s' is not supported yet\n", token);
380 myHead.dosname = nextPhrase();
382 myHead.classname = nextPhrase();
384 myHead.version = nextPhrase();
386 /* put default values into myHead here */
387 myHead.author = "cc65";
388 myHead.info = "Program compiled with cc65 and GEOSLib.";
389 myHead.dostype = 128 + 3;
390 myHead.structure = 0;
395 my_tm = localtime(&t);
397 myHead.year = my_tm->tm_year;
398 myHead.month = my_tm->tm_mon+1;
399 myHead.day = my_tm->tm_mday;
400 myHead.hour = my_tm->tm_hour;
401 myHead.min = my_tm->tm_min;
403 if (strcmp(nextWord(), "{") != 0) {
404 AbEnd("header '%s' has no opening bracket!\n", myHead.dosname);
409 if (strcmp(token, "}") == 0) break;
410 switch (a = findToken(hdrFields, token)) {
412 AbEnd("unknown field '%s' in header '%s'\n", token, myHead.dosname);
415 myHead.author = nextPhrase();
418 myHead.info = nextPhrase();
421 myHead.year = atoi(nextWord());
422 myHead.month = atoi(nextWord());
423 myHead.day = atoi(nextWord());
424 myHead.hour = atoi(nextWord());
425 myHead.min = atoi(nextWord());
427 case 3: /* dostype */
428 switch (b = findToken(hdrDOSTp, nextWord())) {
430 AbEnd("unknown dostype in header '%s'\n", myHead.dosname);
433 myHead.dostype = b / 2 + 128 + 1;
438 switch (b = findToken(hdrModes, nextWord())) {
440 AbEnd("unknown mode in header '%s'\n", myHead.dosname);
455 case 5: /* structure */
456 switch (b = findToken(hdrStructTp, nextWord())) {
458 AbEnd("unknown structure type in header '%s'\n", myHead.dosname);
461 myHead.structure = 0;
465 myHead.structure = 1;
470 myHead.icon = nextPhrase();
474 } while (strcmp(token, "}") != 0);
476 /* OK, all information is gathered, do flushout */
480 "\t\t.segment \"DIRENTRY\"\n\n"
482 "\t.word 0\n", myHead.dostype);
484 fillOut(myHead.dosname, 16, "$a0");
490 "\t.byte %i, %i, %i, %i, %i\n\n"
492 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
493 myHead.structure, myHead.geostype,
494 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
498 "\t\t.segment \"FILEINFO\"\n\n"
499 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
500 "\t.byte 3, 21, 63 | $80\n");
502 if (myHead.icon != NULL) {
504 "\t.incbin \"%s\", 0, 63\n",
507 for (a = 0; a != 63; a = a + 3) {
509 "\t.byte %%%s, %%%s, %%%s\n",
510 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
515 "\t.byte %i, %i, %i\n"
516 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
517 myHead.dostype, myHead.geostype, myHead.structure);
519 fillOut(myHead.classname, 12, "$20");
521 fillOut(myHead.version, 4, "0");
525 "\t.byte %i\n\n", myHead.mode);
527 setLen(myHead.author, 62);
531 "\t.res (63 - %i)\n\n",
532 myHead.author, (int)(strlen(myHead.author) + 1));
534 setLen(myHead.info, 95);
537 "\t.byte 0\n\n", myHead.info);
539 if (fclose (outputSFile) != 0)
540 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
547 int record, lastrecord;
548 int vlirsize, vlirtable[127];
552 vlirsize = strtol(nextWord(), NULL, 0);
554 if (strcmp(nextWord(), "{") != 0) {
555 AbEnd ("VLIR description has no opening bracket!\n");
559 memset(vlirtable, 0, sizeof(vlirtable));
563 if (strcmp(token, "}") == 0) break;
565 record = atoi(token);
566 if (record < 0 || record > 126) {
567 AbEnd("VLIR record %i is out of range 0-126.\n", record);
569 if (vlirtable[record] == 1) {
570 AbEnd("VLIR record %i is defined twice.\n", record);
573 vlirtable[record] = 1;
574 if (record > lastrecord) lastrecord = record;
575 } while (strcmp(token, "}") != 0);
577 if (lastrecord == -1) {
578 AbEnd("There must be at least one VLIR record.\n");
581 /* always include record 0 */
584 /* OK, all information is gathered, do flushout */
588 "\t\t.segment \"RECORDS\"\n\n"
589 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
592 for (record = 0; record <= lastrecord; record++) {
593 if (vlirtable[record] == 1) {
595 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
602 for (record = 0; record <= lastrecord; record++) {
603 if (vlirtable[record] == 1) {
605 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
606 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
607 record, record, record, record);
617 if (fclose(outputSFile) != 0)
618 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
623 "extern void _OVERLAYADDR__;\n"
624 "extern void _OVERLAYSIZE__;\n\n"
625 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
626 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
628 if (fclose(outputCFile) != 0)
629 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
633 char *filterInput(FILE *F, char *tbl) {
635 /* loads file into buffer filtering it out */
636 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
640 if ((a == '\n') || (a == '\015')) a = ' ';
641 if (a == ',' && quote) a = ' ';
642 if (a == '\042') quote =! quote;
644 if ((a == '{') || (a == '(')) bracket++;
645 if ((a == '}') || (a == ')')) bracket--;
649 xrealloc(tbl, i + 1);
653 if ((prevchar != ' ') && (prevchar != -1)) {
658 if (a == ';' && quote) {
662 fseek(F, -1, SEEK_CUR);
670 if (bracket != 0) AbEnd("there are unclosed brackets!\n");
676 void processFile(const char *filename) {
683 int head = 0; /* number of processed HEADER sections */
684 int vlir = 0; /* number of processed VLIR sections */
686 if ((F = fopen(filename, "r")) == 0) {
687 AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
690 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
692 token = strtok(str, " ");
696 switch (findToken(mainToken, token)) {
702 AbEnd("more than one HEADER section, aborting.\n");
707 case 2: break; /* icon not implemented yet */
708 case 3: break; /* dialog not implemented yet */
711 AbEnd("more than one VLIR section, aborting.\n");
717 AbEnd("unknown section %s.\n",token);
722 } while (token != NULL);
726 int main(int argc, char *argv[]) {
728 int ffile = 0, i = 1;
733 const char *arg = argv[i];
738 outputCName = argv[++i];
741 outputSName = argv[++i];
749 AbEnd("unknown option %s\n", arg);
754 if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
755 if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
763 if (ffile == 0) AbEnd("no input file\n");