5 by Maciej 'YTM/Elysium' Witkowiak
7 see GEOSLib documentation for license info
12 - make it work, then do it better
13 - more or less comments? it was hard to code, should be even harder to
15 - add loadable icons feature (binary - 63 bytes)
18 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
36 void VLIRLinker(int argc, char *argv[]) {
38 unsigned char *buffer;
39 unsigned char vlirtabt[127];
40 unsigned char vlirtabs[127];
48 /* check if we know enough */
51 AbEnd("too few arguments, required [out] [cvthead] [vlir0] ...\n");
53 /* first open and copy CVT header */
55 outCVT = fopen(argv[i],"wb+");
57 AbEnd("can't open output:%s\n",strerror(errno));
60 input = fopen(argv[i],"rb");
62 AbEnd("can't open input:%s\n",strerror(errno));
64 buffer = xmalloc(THIS_BUFFER_IS_SOOO_HUGE);
65 memset(buffer,0,THIS_BUFFER_IS_SOOO_HUGE);
67 bytes = fread(buffer,1,1024,input);
70 AbEnd("%s is not a cvt header\n",argv[i]);
72 fwrite(buffer,1,bytes,outCVT);
74 /* now put 254 bytes of VLIR table, to update later */
76 /* clear out things */
78 fwrite(buffer,1,254,outCVT);
79 for (l=0;l<sizeof(vlirtabt)/sizeof(vlirtabt[0]);l++) {
84 /* scan arguments for the last one that is not blank or nonexistant */
89 if ((strcmp(argv[j],"blank")==0)||(strcmp(argv[j],"noexist")==0))
93 /* now read all VLIR chains, align to 254 bytes */
98 if (strcmp(argv[i],"blank")==0) {
99 vlirtabt[j]=0; vlirtabs[j]=0; }
100 else if (strcmp(argv[i],"noexist")==0) {
101 vlirtabt[j]=0; vlirtabs[j]=0xff; }
103 memset(buffer,0,bytes+512);
104 input = fopen(argv[i],"rb");
106 AbEnd("couldn't open %s:%s\n",argv[i],strerror(errno));
107 bytes = fread(buffer,1,THIS_BUFFER_IS_SOOO_HUGE,input);
110 AbEnd("couldn't read %s:%s\n",argv[i],strerror(errno));
111 blocks = bytes / 254;
119 vlirtabt[j]=blocks+1; vlirtabs[j]=rest;
120 /* do not pad the last chain - it doesn't change or break anything
121 but filesize in bytes of .cvt will be identical to native cvt */
123 fwrite(buffer,1,bytes,outCVT);
125 fwrite(buffer,1,(blocks+1)*254,outCVT);
133 /* now rewind and update VLIR table */
136 fseek(outCVT,508,SEEK_SET);
137 for (l=0;l<sizeof(vlirtabt)/sizeof(vlirtabt[0]);l++) {
138 fputc(vlirtabt[l],outCVT);
139 fputc(vlirtabs[l],outCVT);
145 void printCHeader (void) {
147 fprintf(outputCFile, "\n/*\n\tThis file was generated by GEOS Resource Compiler\n"
148 "\n\tDO NOT EDIT! Any changes will be lost!\n"
149 "\n\tEdit proper resource file instead\n"
153 void printSHeader (void) {
155 fprintf(outputSFile, "\n;\n;\tThis file was generated by GEOS Resource Compiler\n;"
156 "\n;\tDO NOT EDIT! Any changes will be lost!\n;"
157 "\n;\tEdit proper resource file instead\n;"
161 void printVHeader (void) {
162 fprintf(outputVFile, "\n#\n#\tThis file was generated by GEOS Resource Compiler\n#"
163 "\n#\tDO NOT EDIT! Any changes will be lost!\n#"
164 "\n#\tEdit proper resource file instead\n#"
165 "\n#\tLook at end of this file to find commandline that must be used\n"
166 "#\tto invoke ld65 and grc (as VLIR linker)\n#"
170 void openCFile (void) {
171 if ((CFnum==0) && (forceFlag==0)) {
172 /* test if file exists already and no forcing*/
173 if ((outputCFile = fopen (outputCName,"r"))!=0)
174 AbEnd("file %s already exists, aborting\n", outputCName);
176 if ((outputCFile = fopen (outputCName,outputCMode))==0)
177 AbEnd("can't open file %s for writing: %s\n",outputCName,strerror (errno));
185 void openSFile (void) {
186 if ((SFnum==0) && (forceFlag==0)) {
187 /* test if file exists already and no forcing*/
188 if ((outputSFile = fopen (outputSName,"r"))!=0)
189 AbEnd("file %s already exists, aborting\n", outputSName);
191 if ((outputSFile = fopen (outputSName,outputSMode))==0)
192 AbEnd("can't open file %s for writing: %s\n",outputSName,strerror (errno));
200 void openVFile (void) {
201 if ((VFnum==0) && (forceFlag==0)) {
202 /* test if file exists already and no forcing*/
203 if ((outputVFile = fopen (outputVName,"r"))!=0)
204 AbEnd("file %s already exists, aborting\n", outputVName);
206 if ((outputVFile = fopen (outputVName,outputVMode))==0)
207 AbEnd("can't open file %s for writting: %s\n",outputVName,strerror (errno));
215 void printUsage (void) {
216 printf("Usage: %s [options] file\n"
218 "\t-h, -?\t\tthis help\n"
219 "\t-f\t\tforce writting files\n"
220 "\t-o name\t\tname C output file\n"
221 "\t-s name\t\tname asm output file\n"
222 "\t-l name\t\tname ld65 config output file (for vlir)\n"
223 "Or as VLIR linker: %s -vlir output.cvt header [vlir0] ... [blank] ... [vlir_n]\n",
227 int findToken (const char **tokenTbl, const char *token) {
228 /* takes as input table of tokens and token, returns position in table or -1 if not found */
231 while (strlen(tokenTbl[a])!=0) {
232 if (strcmp(tokenTbl[a],token)==0) break;
235 if (strlen(tokenTbl[a])==0) a=-1;
240 return strtok(NULL, "\"");
244 return strtok(NULL, " ");
247 void setLen (char *name, unsigned len) {
248 if (strlen(name)>len)
252 void fillOut (char *name, int len, char *filler) {
255 fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
256 a = len - strlen(name);
258 fprintf(outputSFile, ".byte %s", filler);
259 while (--a!=0) fprintf(outputSFile, ", %s", filler);
260 fprintf(outputSFile, "\n\t\t");
264 char *bintos(unsigned char a, char out[7]) {
267 out[7-i] = ((a & 1)==0) ? '0' : '1';
273 int getNameSize (const char *word) {
274 /* count length of a word using BSW 9 font table */
277 while (word[i]!='\0') {
278 a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
285 int a, size, tmpsize, item=0;
287 char namebuff[255]="";
289 struct menuitem *curItem, *newItem;
293 myMenu.name=nextWord();
294 myMenu.left=atoi(nextWord());
295 myMenu.top=atoi(nextWord());
296 myMenu.type=nextWord();
298 if (strcmp(nextWord(),"{")!=0) {
299 AbEnd ("menu '%s' description has no opening bracket!\n", myMenu.name);
301 curItem = xmalloc(sizeof(struct menuitem));
305 if (strcmp(token,"}")==0) break;
306 if (token[strlen(token)-1]!='"') {
307 strcpy (namebuff, token);
310 strcat (namebuff, " ");
311 strcat (namebuff, token);
312 } while (token[strlen(token)-1]!='"');
313 token = xmalloc(strlen(namebuff));
314 strcpy (token, namebuff);
317 curItem->type=nextWord();
318 curItem->target=nextWord();
319 newItem=xmalloc(sizeof(struct menuitem));
320 curItem->next=newItem;
323 } while (strcmp(token,"}")!=0);
324 if (item==0) AbEnd ("menu '%s' has 0 items!\n", myMenu.name);
325 if (item>31) AbEnd ("menu '%s' has too many items!\n", myMenu.name);
329 /* Count menu sizes */
333 if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
334 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
335 myMenu.bot=myMenu.top+15;
336 for (a=0;a!=item;a++) {
337 size+=getNameSize(curItem->name);
338 curItem=curItem->next;
341 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
342 myMenu.bot=myMenu.top+(14*item);
343 for (a=0;a!=item;a++) {
344 tmpsize=getNameSize(curItem->name);
345 size = (size > tmpsize) ? size : tmpsize;
346 curItem=curItem->next;
349 myMenu.right=myMenu.left+size-1;
352 for (a=0;a!=item;a++) {
353 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
354 if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
355 fprintf(outputCFile, "void %s (void);\n", curItem->target);
356 curItem=curItem->next;
359 fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
360 "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
361 myMenu.right, item, myMenu.type);
364 for (a=0;a!=item;a++) {
365 fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
366 if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
367 fprintf(outputCFile, "&");
368 fprintf(outputCFile, "%s,\n", curItem->target);
369 curItem=curItem->next;
372 fprintf(outputCFile, "\t};\n\n");
374 if (fclose (outputCFile)!=0)
375 AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
378 void DoHeader (void) {
383 struct appheader myHead;
385 char i1[9], i2[9], i3[9];
392 a = findToken (hdrFTypes, token);
395 case 0: myHead.geostype = 6; break;
396 case 1: myHead.geostype = 14; break;
397 default: AbEnd("filetype '%s' is not supported yet\n", token);
400 myHead.dosname = nextPhrase();
402 myHead.classname = nextPhrase();
404 myHead.version = nextPhrase();
406 /* put default values into myHead here */
407 myHead.author = "cc65";
408 myHead.info = "Program compiled with cc65 and GEOSLib.";
409 myHead.dostype = 128+3;
410 myHead.structure = 0;
414 my_tm = localtime (&t);
416 myHead.year = my_tm->tm_year;
417 myHead.month = my_tm->tm_mon+1;
418 myHead.day = my_tm->tm_mday;
419 myHead.hour = my_tm->tm_hour;
420 myHead.min = my_tm->tm_min;
422 if (strcmp(nextWord(),"{")!=0)
423 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
427 if (strcmp(token, "}")==0) break;
428 switch (a = findToken (hdrFields, token)) {
430 AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
433 myHead.author = nextPhrase(); break;
435 myHead.info = nextPhrase(); break;
437 myHead.year = atoi(nextWord());
438 myHead.month = atoi(nextWord());
439 myHead.day = atoi(nextWord());
440 myHead.hour = atoi(nextWord());
441 myHead.min = atoi(nextWord());
443 case 3: /* dostype */
444 switch (b = findToken (hdrDOSTp, nextWord())) {
446 AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
449 myHead.dostype = b/2 + 128 + 1;
454 switch (b = findToken (hdrModes, nextWord())) {
456 AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
458 myHead.mode = 0x40; break;
460 myHead.mode = 0x00; break;
462 myHead.mode = 0xc0; break;
464 myHead.mode = 0x80; break;
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; break;
476 myHead.structure = 1; break;
481 } while (strcmp(token, "}")!=0);
483 /* OK, all information is gathered, do flushout */
486 "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
488 fillOut(myHead.dosname,16,"$a0");
491 ".word 0\n\t\t.byte %i\n\t\t.byte %i\n\t\t.byte %i, %i, %i, %i, %i\n\n\t\t"
492 ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
493 ".byte 3, 21, 63 | $80\n\t\t",
494 myHead.structure, myHead.geostype, myHead.year, myHead.month, myHead.day,
495 myHead.hour, myHead.min);
497 for (a=0;a!=63;a=a+3) {
499 ".byte %%%s, %%%s, %%%s\n\t\t",
500 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
503 "\n\t\t.byte %i, %i, %i\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
504 myHead.dostype, myHead.geostype, myHead.structure);
506 fillOut(myHead.classname,12,"$20");
508 fillOut(myHead.version,4,"0");
511 ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
513 setLen(myHead.author,62);
515 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
516 myHead.author, (int) (strlen(myHead.author)+1));
518 setLen(myHead.info, 95);
520 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
521 myHead.info, (int) (strlen(myHead.info)+1));
523 if (fclose (outputSFile)!=0)
524 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
532 int i,numchains,vlirbase;
538 struct vlirentry vlirtable[127];
542 headname = nextWord();
544 vlirbase = strtol(nextWord(),NULL,0);
546 if (strcmp(nextWord(),"{")!=0)
547 AbEnd ("VLIR description has no opening bracket!\n");
553 if (strcmp(token, "}")==0) break;
556 AbEnd("Too many VLIR chains!\n");
558 vlirtable[numchains].chainname=token;
560 /* for first chain - name header */
562 fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
563 "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
565 /* for all other - segment */
566 /* ignore non-existing segments */
567 vlirtable[numchains].exist=1;
568 if ( (strcmp(token,"blank")==0) || (strcmp(token,"noexist")==0) ) {
569 vlirtable[numchains].exist=0;
570 fprintf(outputVFile,"#");
572 fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
573 numchains-1,vlirbase,0x5c00-vlirbase,token);
576 } while (strcmp(token, "}")!=0);
577 fprintf(outputVFile,"}\n\n");
580 AbEnd("There must be at least one VLIR chain.\n");
583 /* now put segments info */
584 fprintf(outputVFile,"SEGMENTS {\n\tHEADER: load = HEADER, type = ro;\n"
585 "\tCODE: load = VLIR0, type = ro;\n"
586 "\tRODATA: load = VLIR0, type = ro;\n"
587 "\tDATA: load = VLIR0, type = rw;\n"
588 "\tBSS: load = VLIR0, type = bss, define = yes;\n\n");
590 for (i=2;i<=numchains;i++) {
591 if (vlirtable[i].exist==0) {
592 fprintf(outputVFile,"#");
594 fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
596 fprintf(outputVFile,"}\n");
598 /* now put usage info */
599 fprintf(outputVFile,"\n# ld65 -o output.cvt -C %s file1.o file2.o ...",outputVName);
600 fprintf(outputVFile,"\n# grc -vlir outputname %s",headname);
601 for (i=1;i<=numchains;i++) {
602 fprintf(outputVFile," %s",vlirtable[i].chainname);
604 fprintf(outputVFile,"\n");
606 if (fclose (outputVFile)!=0)
607 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
610 char *filterInput (FILE *F, char *tbl) {
611 /* loads file into buffer filtering it out */
612 int a, prevchar=-1, i=0, bracket=0, quote=1;
616 if ((a=='\n')||(a=='\015')) a = ' ';
617 if (a==',' && quote) a = ' ';
618 if (a=='\042') quote=!quote;
620 if ((a=='{')||(a=='(')) bracket++;
621 if ((a=='}')||(a==')')) bracket--;
623 if (a==EOF) { tbl[i]='\0'; xrealloc(tbl, i+1); break; };
625 if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
627 if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
629 tbl[i++]=a; prevchar=a; }
633 if (bracket!=0) AbEnd("there are unclosed brackets!\n");
638 void processFile (const char *filename) {
645 int head=0; /* number of processed HEADER sections */
646 int vlir=0; /* number of processed VLIR sections */
648 if ((F = fopen (filename,"r"))==0)
649 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
651 str=filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
653 token = strtok (str," ");
657 switch (findToken (mainToken, token)) {
659 case 0: DoMenu(); break;
662 AbEnd ("more than one HEADER section, aborting.\n");
667 case 2: break; /* icon not implemented yet */
668 case 3: break; /* dialog not implemented yet */
671 AbEnd ("more than one VLIR section, aborting.\n");
676 default: AbEnd ("unknown section %s.\n",token); break;
680 } while (token!=NULL);
683 int main(int argc, char *argv[]) {
689 const char *arg = argv[i];
696 outputCName=argv[++i];
699 outputSName=argv[++i];
702 outputVName=argv[++i];
710 if (strcmp(arg,"-vlir")==0) {
711 VLIRLinker(argc,argv);
715 AbEnd("unknown option %s\n",arg);
718 default: AbEnd("unknown option %s\n",arg);
723 if (outputCName==NULL)
724 outputCName = MakeFilename(arg,".h");
725 if (outputSName==NULL)
726 outputSName = MakeFilename(arg,".s");
727 if (outputVName==NULL)
728 outputVName = MakeFilename(arg,".cfg");
735 if (ffile==0) AbEnd("no input file\n");