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;
104 char outputCMode[2] = "w";
105 char outputSMode[2] = "w";
108 void printUsage(void) {
110 printf("Usage: %s [options] file\n"
112 "\t-h, -?\t\tthis help\n"
113 "\t-f\t\tforce writing files\n"
114 "\t-o name\t\tname C output file\n"
115 "\t-s name\t\tname asm output file\n",
120 void printCHeader(void) {
124 "//\tThis file was generated by the GEOS Resource Compiler\n"
126 "//\tDO NOT EDIT! Any changes will be lost!\n"
128 "//\tEdit proper resource file instead.\n"
133 void printSHeader(void) {
137 ";\tThis file was generated by the GEOS Resource Compiler\n"
139 ";\tDO NOT EDIT! Any changes will be lost!\n"
141 ";\tEdit proper resource file instead.\n"
146 void openCFile(void) {
148 if ((CFnum == 0) && (forceFlag == 0)) {
149 /* test if file exists already and no forcing*/
150 if ((outputCFile = fopen(outputCName,"r")) != 0)
151 AbEnd("file %s already exists, aborting\n", outputCName);
154 if ((outputCFile = fopen(outputCName,outputCMode)) == 0) {
155 AbEnd("can't open file %s for writing: %s\n", outputCName, strerror(errno));
159 outputCMode[0] = 'a';
166 void openSFile(void) {
168 if ((SFnum == 0) && (forceFlag == 0)) {
169 /* test if file exists already and no forcing*/
170 if ((outputSFile = fopen(outputSName,"r")) != 0)
171 AbEnd("file %s already exists, aborting\n", outputSName);
174 if ((outputSFile = fopen(outputSName, outputSMode)) == 0) {
175 AbEnd("can't open file %s for writing: %s\n", outputSName, strerror(errno));
179 outputSMode[0] = 'a';
186 int findToken(const char **tokenTbl, const char *token) {
188 /* takes as input table of tokens and token, returns position in table or -1 if not found */
191 while (strlen(tokenTbl[a]) != 0) {
192 if (strcmp(tokenTbl[a], token) == 0) break;
196 if (strlen(tokenTbl[a]) == 0) a = -1;
202 return strtok(NULL, "\"");
207 return strtok(NULL, " ");
211 void setLen(char *name, unsigned len) {
212 if (strlen(name) > len)
217 void fillOut(char *name, int len, char *filler) {
222 fprintf(outputSFile, "\t.byte \"%s\"\n", name);
226 fprintf(outputSFile, "\t.res (%i - %i), %s\n", len, a, filler);
231 char *bintos(unsigned char a, char out[7]) {
236 out[7 - i] = ((a & 1) == 0) ? '0' : '1';
245 int getNameSize(const char *word) {
247 /* count length of a word using BSW 9 font table */
250 while (word[i] != '\0') {
251 a += (BSWTab[word[i] - 31] - BSWTab[word[i] - 32]);
261 int a, size, tmpsize, item = 0;
263 char namebuff[255] = "";
265 struct menuitem *curItem, *newItem;
269 myMenu.name = nextWord();
270 myMenu.left = atoi(nextWord());
271 myMenu.top = atoi(nextWord());
272 myMenu.type = nextWord();
274 if (strcmp(nextWord(), "{") != 0) {
275 AbEnd("menu '%s' description has no opening bracket!\n", myMenu.name);
277 curItem = xmalloc(sizeof(struct menuitem));
278 myMenu.item = curItem;
281 if (strcmp(token, "}") == 0) break;
282 if (token[strlen(token) - 1] != '"') {
283 strcpy(namebuff, token);
286 strcat(namebuff, " ");
287 strcat(namebuff, token);
288 } while (token[strlen(token) - 1] != '"');
289 token = xmalloc(strlen(namebuff));
290 strcpy(token, namebuff);
292 curItem->name = token;
293 curItem->type = nextWord();
294 curItem->target = nextWord();
295 newItem = xmalloc(sizeof(struct menuitem));
296 curItem->next = newItem;
299 } while (strcmp(token, "}") != 0);
300 if (item == 0) AbEnd("menu '%s' has 0 items!\n", myMenu.name);
301 if (item > 31) AbEnd("menu '%s' has too many items!\n", myMenu.name);
303 curItem->next = NULL;
305 /* count menu sizes */
307 curItem = myMenu.item;
308 if (strstr(myMenu.type, "HORIZONTAL") != NULL) {
309 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
310 myMenu.bot = myMenu.top + 15;
311 for (a = 0; a != item; a++) {
312 size += getNameSize(curItem->name);
313 curItem = curItem->next;
316 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
317 myMenu.bot = myMenu.top + (14 * item);
318 for (a = 0; a != item; a++) {
319 tmpsize = getNameSize(curItem->name);
320 size = (size > tmpsize) ? size : tmpsize;
321 curItem = curItem->next;
324 myMenu.right = myMenu.left + size - 1;
326 curItem = myMenu.item;
327 for (a = 0; a != item; a++) {
328 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
329 if ((strstr(curItem->type, "MENU_ACTION") != NULL) || (strstr(curItem->type, "DYN_SUB_MENU") != NULL)) {
334 curItem=curItem->next;
339 "const void %s = {\n"
340 "\t(char)%i, (char)%i,\n"
341 "\t(int)%i, (int)%i,\n"
342 "\t(char)(%i | %s),\n",
343 myMenu.name, myMenu.top, myMenu.bot, myMenu.left, myMenu.right, item, myMenu.type);
345 curItem = myMenu.item;
346 for (a = 0; a != item; a++) {
348 "\t%s, (char)%s, (int)",
349 curItem->name, curItem->type);
350 if ((strstr(curItem->type, "SUB_MENU") != NULL) && (strstr(curItem->type, "DYN_SUB_MENU") == NULL))
356 curItem = curItem->next;
362 if (fclose(outputCFile) != 0)
363 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
367 void DoHeader(void) {
372 struct appheader myHead;
374 char i1[9], i2[9], i3[9];
381 a = findToken(hdrFTypes, token);
388 myHead.geostype = 14;
391 AbEnd("filetype '%s' is not supported yet\n", token);
394 myHead.dosname = nextPhrase();
396 myHead.classname = nextPhrase();
398 myHead.version = nextPhrase();
400 /* put default values into myHead here */
401 myHead.author = "cc65";
402 myHead.info = "Program compiled with cc65 and GEOSLib.";
403 myHead.dostype = 128 + 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 myHead.dostype = b / 2 + 128 + 1;
452 switch (b = findToken(hdrModes, nextWord())) {
454 AbEnd("unknown mode in header '%s'\n", myHead.dosname);
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"
496 "\t.word 0\n", myHead.dostype);
498 fillOut(myHead.dosname, 16, "$a0");
504 "\t.byte %i, %i, %i, %i, %i\n\n"
506 "\t.byte \"PRG formatted GEOS file V1.0\"\n\n",
507 myHead.structure, myHead.geostype,
508 myHead.year, myHead.month, myHead.day, myHead.hour, myHead.min);
512 "\t\t.segment \"FILEINFO\"\n\n"
513 "\t.import __VLIR0_START__, __STARTUP_RUN__\n\n"
514 "\t.byte 3, 21, 63 | $80\n");
516 if (myHead.icon != NULL) {
518 "\t.incbin \"%s\", 0, 63\n",
521 for (a = 0; a != 63; a = a + 3) {
523 "\t.byte %%%s, %%%s, %%%s\n",
524 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3));
529 "\t.byte %i, %i, %i\n"
530 "\t.word __VLIR0_START__, __VLIR0_START__ - 1, __STARTUP_RUN__\n\n",
531 myHead.dostype, myHead.geostype, myHead.structure);
533 fillOut(myHead.classname, 12, "$20");
535 fillOut(myHead.version, 4, "0");
539 "\t.byte %i\n\n", myHead.mode);
541 setLen(myHead.author, 62);
545 "\t.res (63 - %i)\n\n",
546 myHead.author, (int)(strlen(myHead.author) + 1));
548 setLen(myHead.info, 95);
551 "\t.byte 0\n\n", myHead.info);
553 if (fclose (outputSFile) != 0)
554 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
561 int record, lastrecord;
562 int vlirsize, vlirtable[127];
566 vlirsize = strtol(nextWord(), NULL, 0);
568 if (strcmp(nextWord(), "{") != 0) {
569 AbEnd ("VLIR description has no opening bracket!\n");
573 memset(vlirtable, 0, sizeof(vlirtable));
577 if (strcmp(token, "}") == 0) break;
579 record = atoi(token);
580 if (record < 0 || record > 126) {
581 AbEnd("VLIR record %i is out of range 0-126.\n", record);
583 if (vlirtable[record] == 1) {
584 AbEnd("VLIR record %i is defined twice.\n", record);
587 vlirtable[record] = 1;
588 if (record > lastrecord) lastrecord = record;
589 } while (strcmp(token, "}") != 0);
591 if (lastrecord == -1) {
592 AbEnd("There must be at least one VLIR record.\n");
595 /* always include record 0 */
598 /* OK, all information is gathered, do flushout */
602 "\t\t.segment \"RECORDS\"\n\n"
603 "\t.export __OVERLAYSIZE__ : absolute = $%04x\n\n",
606 for (record = 0; record <= lastrecord; record++) {
607 if (vlirtable[record] == 1) {
609 "\t.import __VLIR%i_START__, __VLIR%i_LAST__\n",
616 for (record = 0; record <= lastrecord; record++) {
617 if (vlirtable[record] == 1) {
619 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) / 254) + 1\n"
620 "\t.byte .lobyte ((__VLIR%i_LAST__ - __VLIR%i_START__ - 1) .MOD 254) + 2\n",
621 record, record, record, record);
631 if (fclose(outputSFile) != 0)
632 AbEnd("error closing %s: %s\n", outputSName, strerror(errno));
637 "extern void _OVERLAYADDR__;\n"
638 "extern void _OVERLAYSIZE__;\n\n"
639 "#define OVERLAY_ADDR (char*) &_OVERLAYADDR__\n"
640 "#define OVERLAY_SIZE (unsigned)&_OVERLAYSIZE__\n\n");
642 if (fclose(outputCFile) != 0)
643 AbEnd("error closing %s: %s\n", outputCName, strerror(errno));
647 char *filterInput(FILE *F, char *tbl) {
649 /* loads file into buffer filtering it out */
650 int a, prevchar = -1, i = 0, bracket = 0, quote = 1;
654 if ((a == '\n') || (a == '\015')) a = ' ';
655 if (a == ',' && quote) a = ' ';
656 if (a == '\042') quote =! quote;
658 if ((a == '{') || (a == '(')) bracket++;
659 if ((a == '}') || (a == ')')) bracket--;
663 xrealloc(tbl, i + 1);
667 if ((prevchar != ' ') && (prevchar != -1)) {
672 if (a == ';' && quote) {
676 fseek(F, -1, SEEK_CUR);
684 if (bracket != 0) AbEnd("there are unclosed brackets!\n");
690 void processFile(const char *filename) {
697 int head = 0; /* number of processed HEADER sections */
698 int vlir = 0; /* number of processed VLIR sections */
700 if ((F = fopen(filename, "r")) == 0) {
701 AbEnd("can't open file %s for reading: %s\n", filename, strerror(errno));
704 str = filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
706 token = strtok(str, " ");
710 switch (findToken(mainToken, token)) {
716 AbEnd("more than one HEADER section, aborting.\n");
721 case 2: break; /* icon not implemented yet */
722 case 3: break; /* dialog not implemented yet */
725 AbEnd("more than one VLIR section, aborting.\n");
731 AbEnd("unknown section %s.\n",token);
736 } while (token != NULL);
740 int main(int argc, char *argv[]) {
742 int ffile = 0, i = 1;
747 const char *arg = argv[i];
755 outputCName = argv[++i];
758 outputSName = argv[++i];
766 AbEnd("unknown option %s\n", arg);
771 if (outputCName == NULL) outputCName = MakeFilename(arg, ".h");
772 if (outputSName == NULL) outputSName = MakeFilename(arg, ".s");
780 if (ffile == 0) AbEnd("no input file\n");