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...)
35 void VLIRLinker(int argc, char *argv[]) {
37 unsigned char *buffer;
38 unsigned char vlirtabt[127];
39 unsigned char vlirtabs[127];
47 /* check if we know enough */
50 AbEnd("too few arguments, required [out] [cvthead] [vlir0] ...\n");
52 /* first open and copy CVT header */
54 outCVT = fopen(argv[i],"wb+");
56 AbEnd("can't open output:%s\n",strerror(errno));
59 input = fopen(argv[i],"rb");
61 AbEnd("can't open input:%s\n",strerror(errno));
63 buffer = malloc(THIS_BUFFER_IS_SOOO_HUGE);
64 memset(buffer,0,THIS_BUFFER_IS_SOOO_HUGE);
66 bytes = fread(buffer,1,1024,input);
69 AbEnd("%s is not a cvt header\n",argv[i]);
71 fwrite(buffer,1,bytes,outCVT);
73 /* now put 254 bytes of VLIR table, to update later */
75 /* clear out things */
77 fwrite(buffer,1,254,outCVT);
78 for (l=0;l<sizeof(vlirtabt)/sizeof(vlirtabt[0]);l++) {
83 /* scan arguments for the last one that is not blank or nonexistant */
88 if ((strcmp(argv[j],"blank")==0)||(strcmp(argv[j],"noexist")==0))
92 /* now read all VLIR chains, align to 254 bytes */
97 if (strcmp(argv[i],"blank")==0) {
98 vlirtabt[j]=0; vlirtabs[j]=0; }
99 else if (strcmp(argv[i],"noexist")==0) {
100 vlirtabt[j]=0; vlirtabs[j]=0xff; }
102 memset(buffer,0,bytes+512);
103 input = fopen(argv[i],"rb");
105 AbEnd("couldn't open %s:%s\n",argv[i],strerror(errno));
106 bytes = fread(buffer,1,THIS_BUFFER_IS_SOOO_HUGE,input);
109 AbEnd("couldn't read %s:%s\n",argv[i],strerror(errno));
110 blocks = bytes / 254;
118 vlirtabt[j]=blocks+1; vlirtabs[j]=rest;
119 /* do not pad the last chain - it doesn't change or break anything
120 but filesize in bytes of .cvt will be identical to native cvt */
122 fwrite(buffer,1,bytes,outCVT);
124 fwrite(buffer,1,(blocks+1)*254,outCVT);
132 /* now rewind and update VLIR table */
135 fseek(outCVT,508,SEEK_SET);
136 for (l=0;l<sizeof(vlirtabt)/sizeof(vlirtabt[0]);l++) {
137 fputc(vlirtabt[l],outCVT);
138 fputc(vlirtabs[l],outCVT);
144 void printCHeader (void) {
146 fprintf(outputCFile, "\n/*\n\tThis file was generated by GEOS Resource Compiler\n"
147 "\n\tDO NOT EDIT! Any changes will be lost!\n"
148 "\n\tEdit proper resource file instead\n"
152 void printSHeader (void) {
154 fprintf(outputSFile, "\n;\n;\tThis file was generated by GEOS Resource Compiler\n;"
155 "\n;\tDO NOT EDIT! Any changes will be lost!\n;"
156 "\n;\tEdit proper resource file instead\n;"
160 void printVHeader (void) {
161 fprintf(outputVFile, "\n#\n#\tThis file was generated by GEOS Resource Compiler\n#"
162 "\n#\tDO NOT EDIT! Any changes will be lost!\n#"
163 "\n#\tEdit proper resource file instead\n#"
164 "\n#\tLook at end of this file to find commandline that must be used\n"
165 "#\tto invoke ld65 and grc (as VLIR linker)\n#"
169 void openCFile (void) {
170 if ((CFnum==0) && (forceFlag==0)) {
171 /* test if file exists already and no forcing*/
172 if ((outputCFile = fopen (outputCName,"r"))!=0)
173 AbEnd("file %s already exists, aborting\n", outputCName);
175 if ((outputCFile = fopen (outputCName,outputCMode))==0)
176 AbEnd("can't open file %s for writing: %s\n",outputCName,strerror (errno));
184 void openSFile (void) {
185 if ((SFnum==0) && (forceFlag==0)) {
186 /* test if file exists already and no forcing*/
187 if ((outputSFile = fopen (outputSName,"r"))!=0)
188 AbEnd("file %s already exists, aborting\n", outputSName);
190 if ((outputSFile = fopen (outputSName,outputSMode))==0)
191 AbEnd("can't open file %s for writing: %s\n",outputSName,strerror (errno));
199 void openVFile (void) {
200 if ((VFnum==0) && (forceFlag==0)) {
201 /* test if file exists already and no forcing*/
202 if ((outputVFile = fopen (outputVName,"r"))!=0)
203 AbEnd("file %s already exists, aborting\n", outputVName);
205 if ((outputVFile = fopen (outputVName,outputVMode))==0)
206 AbEnd("can't open file %s for writting: %s\n",outputVName,strerror (errno));
214 void printUsage (void) {
215 printf("Usage: %s [options] file\n"
217 "\t-h, -?\t\tthis help\n"
218 "\t-f\t\tforce writting files\n"
219 "\t-o name\t\tname C output file\n"
220 "\t-s name\t\tname asm output file\n"
221 "\t-l name\t\tname ld65 config output file (for vlir)\n"
222 "Or as VLIR linker: %s -vlir output.cvt header [vlir0] ... [blank] ... [vlir_n]\n",
226 int findToken (const char **tokenTbl, const char *token) {
227 /* takes as input table of tokens and token, returns position in table or -1 if not found */
230 while (strlen(tokenTbl[a])!=0) {
231 if (strcmp(tokenTbl[a],token)==0) break;
234 if (strlen(tokenTbl[a])==0) a=-1;
239 return strtok(NULL, "\"");
243 return strtok(NULL, " ");
246 void setLen (char *name, unsigned len) {
247 if (strlen(name)>len)
251 void fillOut (char *name, int len, char *filler) {
254 fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
255 a = len - strlen(name);
257 fprintf(outputSFile, ".byte %s", filler);
258 while (--a!=0) fprintf(outputSFile, ", %s", filler);
259 fprintf(outputSFile, "\n\t\t");
263 char *bintos(unsigned char a, char out[7]) {
266 out[7-i] = ((a & 1)==0) ? '0' : '1';
272 int getNameSize (const char *word) {
273 /* count length of a word using BSW 9 font table */
276 while (word[i]!='\0') {
277 a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
284 int a, size, tmpsize, item=0;
286 char namebuff[255]="";
288 struct menuitem *curItem, *newItem;
292 myMenu.name=nextWord();
293 myMenu.left=atoi(nextWord());
294 myMenu.top=atoi(nextWord());
295 myMenu.type=nextWord();
297 if (strcmp(nextWord(),"{")!=0) {
298 AbEnd ("menu '%s' description has no opening bracket!\n", myMenu.name);
300 curItem=malloc(sizeof(struct menuitem));
304 if (strcmp(token,"}")==0) break;
305 if (token[strlen(token)-1]!='"') {
306 strcpy (namebuff, token);
309 strcat (namebuff, " ");
310 strcat (namebuff, token);
311 } while (token[strlen(token)-1]!='"');
312 token = malloc(strlen(namebuff));
313 strcpy (token, namebuff);
316 curItem->type=nextWord();
317 curItem->target=nextWord();
318 newItem=malloc(sizeof(struct menuitem));
319 curItem->next=newItem;
322 } while (strcmp(token,"}")!=0);
323 if (item==0) AbEnd ("menu '%s' has 0 items!\n", myMenu.name);
324 if (item>31) AbEnd ("menu '%s' has too many items!\n", myMenu.name);
328 /* Count menu sizes */
332 if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
333 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
334 myMenu.bot=myMenu.top+15;
335 for (a=0;a!=item;a++) {
336 size+=getNameSize(curItem->name);
337 curItem=curItem->next;
340 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
341 myMenu.bot=myMenu.top+(14*item);
342 for (a=0;a!=item;a++) {
343 tmpsize=getNameSize(curItem->name);
344 size = (size > tmpsize) ? size : tmpsize;
345 curItem=curItem->next;
348 myMenu.right=myMenu.left+size-1;
351 for (a=0;a!=item;a++) {
352 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
353 if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
354 fprintf(outputCFile, "void %s (void);\n", curItem->target);
355 curItem=curItem->next;
358 fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
359 "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
360 myMenu.right, item, myMenu.type);
363 for (a=0;a!=item;a++) {
364 fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
365 if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
366 fprintf(outputCFile, "&");
367 fprintf(outputCFile, "%s,\n", curItem->target);
368 curItem=curItem->next;
371 fprintf(outputCFile, "\t};\n\n");
373 if (fclose (outputCFile)!=0)
374 AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
377 void DoHeader (void) {
382 struct appheader myHead;
384 char i1[9], i2[9], i3[9];
391 a = findToken (hdrFTypes, token);
394 AbEnd("filetype '%s' is not supported yet\n", token);
397 case 0: myHead.geostype = 6; break;
398 case 1: myHead.geostype = 14; break;
401 myHead.dosname = nextPhrase();
403 myHead.classname = nextPhrase();
405 myHead.version = nextPhrase();
407 /* put default values into myHead here */
408 myHead.author = "cc65";
409 myHead.info = "Program compiled with cc65 and GEOSLib.";
410 myHead.dostype = 128+3;
411 myHead.structure = 0;
415 my_tm = localtime (&t);
417 myHead.year = my_tm->tm_year;
418 myHead.month = my_tm->tm_mon+1;
419 myHead.day = my_tm->tm_mday;
420 myHead.hour = my_tm->tm_hour;
421 myHead.min = my_tm->tm_min;
423 if (strcmp(nextWord(),"{")!=0)
424 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
428 if (strcmp(token, "}")==0) break;
429 switch (a = findToken (hdrFields, token)) {
431 AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
434 myHead.author = nextPhrase(); break;
436 myHead.info = nextPhrase(); break;
438 myHead.year = atoi(nextWord());
439 myHead.month = atoi(nextWord());
440 myHead.day = atoi(nextWord());
441 myHead.hour = atoi(nextWord());
442 myHead.min = atoi(nextWord());
444 case 3: /* dostype */
445 switch (b = findToken (hdrDOSTp, nextWord())) {
447 AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
450 myHead.dostype = b/2 + 128 + 1;
455 switch (b = findToken (hdrModes, nextWord())) {
457 AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
459 myHead.mode = 0x40; break;
461 myHead.mode = 0x00; break;
463 myHead.mode = 0xc0; break;
465 myHead.mode = 0x80; break;
468 case 5: /* structure */
469 switch (b = findToken(hdrStructTp, nextWord())) {
471 AbEnd ("unknown structure type in header '%s'\n", myHead.dosname);
474 myHead.structure = 0; break;
477 myHead.structure = 1; break;
482 } while (strcmp(token, "}")!=0);
484 /* OK, all information is gathered, do flushout */
487 "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
489 fillOut(myHead.dosname,16,"$a0");
492 ".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"
493 ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
494 ".byte 3, 21, 63 | $80\n\t\t",
495 myHead.structure, myHead.geostype, myHead.year, myHead.month, myHead.day,
496 myHead.hour, myHead.min);
498 for (a=0;a!=63;a=a+3) {
500 ".byte %%%s, %%%s, %%%s\n\t\t",
501 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
504 "\n\t\t.byte %i, %i, %i\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
505 myHead.dostype, myHead.geostype, myHead.structure);
507 fillOut(myHead.classname,12,"$20");
509 fillOut(myHead.version,4,"0");
512 ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
514 setLen(myHead.author,62);
516 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
517 myHead.author, (int) (strlen(myHead.author)+1));
519 setLen(myHead.info, 95);
521 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
522 myHead.info, (int) (strlen(myHead.info)+1));
524 if (fclose (outputSFile)!=0)
525 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
533 int i,numchains,vlirbase;
539 struct vlirentry vlirtable[127];
543 headname = nextWord();
545 vlirbase = strtol(nextWord(),NULL,0);
547 if (strcmp(nextWord(),"{")!=0)
548 AbEnd ("VLIR description has no opening bracket!\n");
554 if (strcmp(token, "}")==0) break;
557 AbEnd("Too many VLIR chains!\n");
559 vlirtable[numchains].chainname=token;
561 /* for first chain - name header */
563 fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
564 "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
566 /* for all other - segment */
567 /* ignore non-existing segments */
568 vlirtable[numchains].exist=1;
569 if ( (strcmp(token,"blank")==0) || (strcmp(token,"noexist")==0) ) {
570 vlirtable[numchains].exist=0;
571 fprintf(outputVFile,"#");
573 fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
574 numchains-1,vlirbase,0x5c00-vlirbase,token);
577 } while (strcmp(token, "}")!=0);
578 fprintf(outputVFile,"}\n\n");
581 AbEnd("There must be at least one VLIR chain.\n");
584 /* now put segments info */
585 fprintf(outputVFile,"SEGMENTS {\n\tHEADER: load = HEADER, type = ro;\n"
586 "\tCODE: load = VLIR0, type = ro;\n"
587 "\tRODATA: load = VLIR0, type = ro;\n"
588 "\tDATA: load = VLIR0, type = rw;\n"
589 "\tBSS: load = VLIR0, type = bss, define = yes;\n\n");
591 for (i=2;i<=numchains;i++) {
592 if (vlirtable[i].exist==0) {
593 fprintf(outputVFile,"#");
595 fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
597 fprintf(outputVFile,"}\n");
599 /* now put usage info */
600 fprintf(outputVFile,"\n# ld65 -o output.cvt -C %s file1.o file2.o ...",outputVName);
601 fprintf(outputVFile,"\n# grc -vlir outputname %s",headname);
602 for (i=1;i<=numchains;i++) {
603 fprintf(outputVFile," %s",vlirtable[i].chainname);
605 fprintf(outputVFile,"\n");
607 if (fclose (outputVFile)!=0)
608 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
611 char *filterInput (FILE *F, char *tbl) {
612 /* loads file into buffer filtering it out */
613 int a, prevchar=-1, i=0, bracket=0, quote=1;
617 if ((a=='\n')||(a=='\015')) a = ' ';
618 if (a==',' && quote) a = ' ';
619 if (a=='\042') quote=!quote;
621 if ((a=='{')||(a=='(')) bracket++;
622 if ((a=='}')||(a==')')) bracket--;
624 if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
626 if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
628 if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
630 tbl[i++]=a; prevchar=a; }
634 if (bracket!=0) AbEnd("there are unclosed brackets!\n");
639 void processFile (const char *filename) {
646 int head=0; /* number of processed HEADER sections */
647 int vlir=0; /* number of processed VLIR sections */
649 if ((F = fopen (filename,"r"))==0)
650 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
652 str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
654 token = strtok (str," ");
658 switch (findToken (mainToken, token)) {
660 case 0: DoMenu(); break;
663 AbEnd ("more than one HEADER section, aborting.\n");
668 case 2: break; /* icon not implemented yet */
669 case 3: break; /* dialog not implemented yet */
672 AbEnd ("more than one VLIR section, aborting.\n");
677 default: AbEnd ("unknown section %s.\n",token); break;
681 } while (token!=NULL);
684 int main(int argc, char *argv[]) {
690 const char *arg = argv[i];
697 outputCName=argv[++i];
700 outputSName=argv[++i];
703 outputVName=argv[++i];
711 if (strcmp(arg,"-vlir")==0) {
712 VLIRLinker(argc,argv);
716 AbEnd("unknown option %s\n",arg);
719 default: AbEnd("unknown option %s\n",arg);
724 if (outputCName==NULL)
725 outputCName = MakeFilename(arg,".h");
726 if (outputSName==NULL)
727 outputSName = MakeFilename(arg,".s");
728 if (outputVName==NULL)
729 outputVName = MakeFilename(arg,".cfg");
736 if (ffile==0) AbEnd("no input file\n");