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...)
31 //#include "cmdline.h"
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 = malloc(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 fprintf(stderr, "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) {
265 char *bintos(unsigned char a, char out[7]) {
268 out[7-i] = ((a & 1)==0) ? '0' : '1';
274 int getNameSize (const char *word) {
275 /* count length of a word using BSW 9 font table */
278 while (word[i]!='\0') {
279 a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
286 int a, size, tmpsize, item=0;
288 char namebuff[255]="";
290 struct menuitem *curItem, *newItem;
294 myMenu.name=nextWord();
295 myMenu.left=atoi(nextWord());
296 myMenu.top=atoi(nextWord());
297 myMenu.type=nextWord();
299 if (strcmp(nextWord(),"{")!=0) {
300 AbEnd ("menu '%s' description has no opening bracket!\n", myMenu.name);
302 curItem=malloc(sizeof(struct menuitem));
306 if (strcmp(token,"}")==0) break;
307 if (token[strlen(token)-1]!='"') {
308 strcpy (namebuff, token);
311 strcat (namebuff, " ");
312 strcat (namebuff, token);
313 } while (token[strlen(token)-1]!='"');
314 token = malloc(strlen(namebuff));
315 strcpy (token, namebuff);
318 curItem->type=nextWord();
319 curItem->target=nextWord();
320 newItem=malloc(sizeof(struct menuitem));
321 curItem->next=newItem;
324 } while (strcmp(token,"}")!=0);
325 if (item==0) AbEnd ("menu '%s' has 0 items!\n", myMenu.name);
326 if (item>31) AbEnd ("menu '%s' has too many items!\n", myMenu.name);
330 /* Count menu sizes */
334 if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
335 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
336 myMenu.bot=myMenu.top+15;
337 for (a=0;a!=item;a++) {
338 size+=getNameSize(curItem->name);
339 curItem=curItem->next;
342 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
343 myMenu.bot=myMenu.top+(14*item);
344 for (a=0;a!=item;a++) {
345 tmpsize=getNameSize(curItem->name);
346 size = (size > tmpsize) ? size : tmpsize;
347 curItem=curItem->next;
350 myMenu.right=myMenu.left+size-1;
353 for (a=0;a!=item;a++) {
354 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
355 if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
356 fprintf(outputCFile, "void %s (void);\n", curItem->target);
357 curItem=curItem->next;
360 fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
361 "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
362 myMenu.right, item, myMenu.type);
365 for (a=0;a!=item;a++) {
366 fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
367 if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
368 fprintf(outputCFile, "&");
369 fprintf(outputCFile, "%s,\n", curItem->target);
370 curItem=curItem->next;
373 fprintf(outputCFile, "\t};\n\n");
375 if (fclose (outputCFile)!=0)
376 AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
379 void DoHeader (void) {
384 struct appheader myHead;
386 char i1[9], i2[9], i3[9];
393 a = findToken (hdrFTypes, token);
396 AbEnd("filetype '%s' is not supported yet\n", token);
399 case 0: myHead.geostype = 6; break;
400 case 1: myHead.geostype = 14; break;
403 myHead.dosname = nextPhrase();
405 myHead.classname = nextPhrase();
407 myHead.version = nextPhrase();
409 /* put default values into myHead here */
410 myHead.author = "cc65";
411 myHead.info = "Program compiled with cc65 and GEOSLib.";
412 myHead.dostype = 128+3;
413 myHead.structure = 0;
417 my_tm = localtime (&t);
419 myHead.year = my_tm->tm_year;
420 myHead.month = my_tm->tm_mon+1;
421 myHead.day = my_tm->tm_mday;
422 myHead.hour = my_tm->tm_hour;
423 myHead.min = my_tm->tm_min;
425 if (strcmp(nextWord(),"{")!=0)
426 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
430 if (strcmp(token, "}")==0) break;
431 switch (a = findToken (hdrFields, token)) {
433 AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
436 myHead.author = nextPhrase(); break;
438 myHead.info = nextPhrase(); break;
440 myHead.year = atoi(nextWord());
441 myHead.month = atoi(nextWord());
442 myHead.day = atoi(nextWord());
443 myHead.hour = atoi(nextWord());
444 myHead.min = atoi(nextWord());
446 case 3: /* dostype */
447 switch (b = findToken (hdrDOSTp, nextWord())) {
449 AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
452 myHead.dostype = b/2 + 128 + 1;
457 switch (b = findToken (hdrModes, nextWord())) {
459 AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
461 myHead.mode = 0x40; break;
463 myHead.mode = 0x00; break;
465 myHead.mode = 0xc0; break;
467 myHead.mode = 0x80; break;
470 case 5: /* structure */
471 switch (b = findToken(hdrStructTp, nextWord())) {
473 AbEnd ("unknown structure type in header '%s'\n", myHead.dosname);
476 myHead.structure = 0; break;
479 myHead.structure = 1; break;
484 } while (strcmp(token, "}")!=0);
486 /* OK, all information is gathered, do flushout */
489 "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
491 fillOut(myHead.dosname,16,"$a0");
494 ".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"
495 ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
496 ".byte 3, 21, 63 | $80\n\t\t",
497 myHead.structure, myHead.geostype, myHead.year, myHead.month, myHead.day,
498 myHead.hour, myHead.min);
500 for (a=0;a!=63;a=a+3) {
502 ".byte %%%s, %%%s, %%%s\n\t\t",
503 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
506 "\n\t\t.byte %i, %i, %i\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
507 myHead.dostype, myHead.geostype, myHead.structure);
509 fillOut(myHead.classname,12,"$20");
511 fillOut(myHead.version,4,"0");
514 ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
516 setLen(myHead.author,62);
518 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
519 myHead.author, (int) (strlen(myHead.author)+1));
521 setLen(myHead.info, 95);
523 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
524 myHead.info, (int) (strlen(myHead.info)+1));
526 if (fclose (outputSFile)!=0)
527 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
535 int i,numchains,vlirbase;
541 struct vlirentry vlirtable[127];
545 headname = nextWord();
547 vlirbase = strtol(nextWord(),NULL,0);
549 if (strcmp(nextWord(),"{")!=0)
550 AbEnd ("VLIR description has no opening bracket!\n");
556 if (strcmp(token, "}")==0) break;
559 AbEnd("Too many VLIR chains!\n");
561 vlirtable[numchains].chainname=token;
563 /* for first chain - name header */
565 fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
566 "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
568 /* for all other - segment */
569 /* ignore non-existing segments */
570 vlirtable[numchains].exist=1;
571 if ( (strcmp(token,"blank")==0) || (strcmp(token,"noexist")==0) ) {
572 vlirtable[numchains].exist=0;
573 fprintf(outputVFile,"#");
575 fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
576 numchains-1,vlirbase,0x5c00-vlirbase,token);
579 } while (strcmp(token, "}")!=0);
580 fprintf(outputVFile,"}\n\n");
583 AbEnd("There must be at least one VLIR chain.\n");
586 /* now put segments info */
587 fprintf(outputVFile,"SEGMENTS {\n\tHEADER: load = HEADER, type = ro;\n"
588 "\tCODE: load = VLIR0, type = ro;\n"
589 "\tRODATA: load = VLIR0, type = ro;\n"
590 "\tDATA: load = VLIR0, type = rw;\n"
591 "\tBSS: load = VLIR0, type = bss, define = yes;\n\n");
593 for (i=2;i<=numchains;i++) {
594 if (vlirtable[i].exist==0) {
595 fprintf(outputVFile,"#");
597 fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
599 fprintf(outputVFile,"}\n");
601 /* now put usage info */
602 fprintf(outputVFile,"\n# ld65 -o output.cvt -C %s file1.o file2.o ...",outputVName);
603 fprintf(outputVFile,"\n# grc -vlir outputname %s",headname);
604 for (i=1;i<=numchains;i++) {
605 fprintf(outputVFile," %s",vlirtable[i].chainname);
607 fprintf(outputVFile,"\n");
609 if (fclose (outputVFile)!=0)
610 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
613 char *filterInput (FILE *F, char *tbl) {
614 /* loads file into buffer filtering it out */
615 int a, prevchar=-1, i=0, bracket=0, quote=1;
619 if ((a=='\n')||(a=='\015')) a = ' ';
621 if (a=='\042') quote=!quote;
623 if ((a=='{')||(a=='(')) bracket++;
624 if ((a=='}')||(a==')')) bracket--;
626 if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
628 if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
630 if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
632 tbl[i++]=a; prevchar=a; }
636 if (bracket!=0) AbEnd("there are unclosed brackets!\n");
641 void processFile (const char *filename) {
648 int head=0; /* number of processed HEADER sections */
649 int vlir=0; /* number of processed VLIR sections */
651 if ((F = fopen (filename,"r"))==0)
652 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
654 str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
656 token = strtok (str," ");
660 switch (findToken (mainToken, token)) {
662 case 0: DoMenu(); break;
665 AbEnd ("more than one HEADER section, aborting.\n");
670 case 2: break; /* icon not implemented yet */
671 case 3: break; /* dialog not implemented yet */
674 AbEnd ("more than one VLIR section, aborting.\n");
679 default: AbEnd ("unknown section %s.\n",token); break;
683 } while (token!=NULL);
686 int main(int argc, char *argv[]) {
692 const char *arg = argv[i];
699 outputCName=argv[++i];
702 outputSName=argv[++i];
705 outputVName=argv[++i];
713 if (strcmp(arg,"-vlir")==0) {
714 VLIRLinker(argc,argv);
718 AbEnd("unknown option %s\n",arg);
721 default: AbEnd("unknown option %s\n",arg);
726 if (outputCName==NULL)
727 outputCName = MakeFilename(arg,".h");
728 if (outputSName==NULL)
729 outputSName = MakeFilename(arg,".s");
730 if (outputVName==NULL)
731 outputVName = MakeFilename(arg,".cfg");
738 if (ffile==0) AbEnd("no input file\n");