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[BLOODY_BIG_BUFFER];
39 unsigned char vlirtabt[127];
40 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 bytes = fread(buffer,1,BLOODY_BIG_BUFFER,input);
66 AbEnd("%s is not a cvt header\n",argv[i]);
68 fwrite(buffer,1,bytes,outCVT);
70 /* now put 254 bytes of VLIR table, to update later */
72 /* clear out things */
74 fwrite(buffer,1,254,outCVT);
75 for (j=0;j!=126;j++) {
80 /* now read all VLIR chains, align to 254 bytes */
85 if (strcmp(argv[i],"blank")==0) {
86 vlirtabt[j]=0; vlirtabs[j]=0; }
87 else if (strcmp(argv[i],"noexist")==0) {
88 vlirtabt[j]=0; vlirtabs[j]=0xff; }
90 memset(buffer,0,BLOODY_BIG_BUFFER);
91 input = fopen(argv[i],"rb");
93 AbEnd("couldn't open %s:%s\n",argv[i],strerror(errno));
94 bytes = fread(buffer,1,BLOODY_BIG_BUFFER,input);
97 AbEnd("couldn't read %s:%s\n",argv[i],strerror(errno));
99 rest = bytes % 254 + 2;
100 if (rest>255) rest=255;
101 vlirtabt[j]=blocks+1; vlirtabs[j]=rest;
102 fwrite(buffer,1,(blocks+1)*254,outCVT);
108 /* now rewind and update VLIR table */
111 fseek(outCVT,508,SEEK_SET);
112 for (i=0;i!=127;i++) {
113 fputc(vlirtabt[i],outCVT);
114 fputc(vlirtabs[i],outCVT);
120 void printCHeader (void) {
122 fprintf(outputCFile, "\n/*\n\tThis file was generated by GEOS Resource Compiler\n"
123 "\n\tDO NOT EDIT! Any changes will be lost!\n"
124 "\n\tEdit proper resource file instead\n"
128 void printSHeader (void) {
130 fprintf(outputSFile, "\n;\n;\tThis file was generated by GEOS Resource Compiler\n;"
131 "\n;\tDO NOT EDIT! Any changes will be lost!\n;"
132 "\n;\tEdit proper resource file instead\n;"
136 void printVHeader (void) {
137 fprintf(outputVFile, "\n#\n#\tThis file was generated by GEOS Resource Compiler\n#"
138 "\n#\tDO NOT EDIT! Any changes will be lost!\n#"
139 "\n#\tEdit proper resource file instead\n#"
140 "\n#\tLook at end of this file to find commandline that must be used\n"
141 "#\tto invoke ld65 and grc (as VLIR linker)\n#"
145 void openCFile (void) {
146 if ((CFnum==0) && (forceFlag==0)) {
147 /* test if file exists already and no forcing*/
148 if ((outputCFile = fopen (outputCName,"r"))!=0)
149 AbEnd("file %s already exists, aborting\n", outputCName);
151 if ((outputCFile = fopen (outputCName,outputCMode))==0)
152 AbEnd("can't open file %s for writing: %s\n",outputCName,strerror (errno));
160 void openSFile (void) {
161 if ((SFnum==0) && (forceFlag==0)) {
162 /* test if file exists already and no forcing*/
163 if ((outputSFile = fopen (outputSName,"r"))!=0)
164 AbEnd("file %s already exists, aborting\n", outputSName);
166 if ((outputSFile = fopen (outputSName,outputSMode))==0)
167 AbEnd("can't open file %s for writing: %s\n",outputSName,strerror (errno));
175 void openVFile (void) {
176 if ((VFnum==0) && (forceFlag==0)) {
177 /* test if file exists already and no forcing*/
178 if ((outputVFile = fopen (outputVName,"r"))!=0)
179 AbEnd("file %s already exists, aborting\n", outputVName);
181 if ((outputVFile = fopen (outputVName,outputVMode))==0)
182 AbEnd("can't open file %s for writting: %s\n",outputVName,strerror (errno));
190 void printUsage (void) {
191 fprintf(stderr, "Usage: %s [options] file\n"
193 "\t-h, -?\t\tthis help\n"
194 "\t-f\t\tforce writting files\n"
195 "\t-o name\t\tname C output file\n"
196 "\t-s name\t\tname asm output file\n"
197 "\t-l name\t\tname ld65 config output file (for vlir)\n"
198 "Or as VLIR linker: %s -vlir output.cvt header [vlir0] ... [blank] ... [vlir_n]\n",
202 int findToken (const char **tokenTbl, const char *token) {
203 /* takes as input table of tokens and token, returns position in table or -1 if not found */
206 while (strlen(tokenTbl[a])!=0) {
207 if (strcmp(tokenTbl[a],token)==0) break;
210 if (strlen(tokenTbl[a])==0) a=-1;
215 return strtok(NULL, "\"");
219 return strtok(NULL, " ");
222 void setLen (char *name, unsigned len) {
223 if (strlen(name)>len)
227 void fillOut (char *name, int len, char *filler) {
230 fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
231 a = len - strlen(name);
233 fprintf(outputSFile, ".byte %s", filler);
234 while (--a!=0) fprintf(outputSFile, ", %s", filler);
235 fprintf(outputSFile, "\n\t\t");
239 //char *bintos(unsigned char a, char *out) {
240 char *bintos(unsigned char a, char out[7]) {
243 out[7-i] = ((a & 1)==0) ? '0' : '1';
249 int getNameSize (const char *word) {
250 /* count length of a word using BSW 9 font table */
253 while (word[i]!='\0') {
254 a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
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=malloc(sizeof(struct menuitem));
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 = malloc(strlen(namebuff));
290 strcpy (token, namebuff);
293 curItem->type=nextWord();
294 curItem->target=nextWord();
295 newItem=malloc(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);
305 /* Count menu sizes */
309 if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
310 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
311 myMenu.bot=myMenu.top+15;
312 for (a=0;a!=item;a++) {
313 size+=getNameSize(curItem->name);
314 curItem=curItem->next;
317 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
318 myMenu.bot=myMenu.top+(14*item)-1;
319 for (a=0;a!=item;a++) {
320 tmpsize=getNameSize(curItem->name);
321 size = (size > tmpsize) ? size : tmpsize;
322 curItem=curItem->next;
325 myMenu.right=myMenu.left+size-1;
328 for (a=0;a!=item;a++) {
329 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
330 if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
331 fprintf(outputCFile, "void %s (void);\n", curItem->target);
332 curItem=curItem->next;
335 fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
336 "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
337 myMenu.right, item, myMenu.type);
340 for (a=0;a!=item;a++) {
341 fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
342 if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
343 fprintf(outputCFile, "&");
344 fprintf(outputCFile, "%s,\n", curItem->target);
345 curItem=curItem->next;
348 fprintf(outputCFile, "\t};\n\n");
350 if (fclose (outputCFile)!=0)
351 AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
354 void DoHeader (void) {
359 struct appheader myHead;
361 char i1[9], i2[9], i3[9];
368 a = findToken (hdrFTypes, token);
371 AbEnd("filetype '%s' is not supported yet\n", token);
374 case 0: myHead.geostype = 6; break;
375 case 1: myHead.geostype = 14; break;
378 myHead.dosname = nextPhrase();
380 myHead.classname = nextPhrase();
382 myHead.version = nextPhrase();
384 /* put default values into myHead here */
385 myHead.author = "cc65";
386 myHead.info = "Program compiled with cc65 and GEOSLib.";
387 myHead.dostype = 128+3;
388 myHead.structure = 0;
392 my_tm = localtime (&t);
394 myHead.year = my_tm->tm_year;
395 myHead.month = my_tm->tm_mon+1;
396 myHead.day = my_tm->tm_mday;
397 myHead.hour = my_tm->tm_hour;
398 myHead.min = my_tm->tm_min;
400 if (strcmp(nextWord(),"{")!=0)
401 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
405 if (strcmp(token, "}")==0) break;
406 switch (a = findToken (hdrFields, token)) {
408 AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
411 myHead.author = nextPhrase(); break;
413 myHead.info = nextPhrase(); break;
415 myHead.year = atoi(nextWord());
416 myHead.month = atoi(nextWord());
417 myHead.day = atoi(nextWord());
418 myHead.hour = atoi(nextWord());
419 myHead.min = atoi(nextWord());
421 case 3: /* dostype */
422 switch (b = findToken (hdrDOSTp, nextWord())) {
424 AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
427 myHead.dostype = b/2 + 128 + 1;
432 switch (b = findToken (hdrModes, nextWord())) {
434 AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
436 myHead.mode = 0x40; break;
438 myHead.mode = 0x00; break;
440 myHead.mode = 0xc0; break;
442 myHead.mode = 0x80; break;
445 case 5: /* structure */
446 switch (b = findToken(hdrStructTp, nextWord())) {
448 AbEnd ("unknown structure type in header '%s'\n", myHead.dosname);
451 myHead.structure = 0; break;
454 myHead.structure = 1; break;
459 } while (strcmp(token, "}")!=0);
461 /* OK, all information is gathered, do flushout */
464 "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
466 fillOut(myHead.dosname,16,"$a0");
469 ".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"
470 ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
471 ".byte 3, 21, 63 | $80\n\t\t",
472 myHead.structure, myHead.geostype, myHead.year, myHead.month, myHead.day,
473 myHead.hour, myHead.min);
475 for (a=0;a!=63;a=a+3) {
477 ".byte %%%s, %%%s, %%%s\n\t\t",
478 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
481 "\n\t\t.byte %i, %i, %i\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
482 myHead.dostype, myHead.geostype, myHead.structure);
484 fillOut(myHead.classname,12,"$20");
486 fillOut(myHead.version,4,"0");
489 ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
491 setLen(myHead.author,62);
493 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
494 myHead.author, (int) (strlen(myHead.author)+1));
496 setLen(myHead.info, 95);
498 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
499 myHead.info, (int) (strlen(myHead.info)+1));
501 if (fclose (outputSFile)!=0)
502 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
510 int i,numchains,vlirbase;
516 struct vlirentry vlirtable[127];
520 headname = nextWord();
522 vlirbase = strtol(nextWord(),NULL,0);
524 if (strcmp(nextWord(),"{")!=0)
525 AbEnd ("VLIR description has no opening bracket!\n");
531 if (strcmp(token, "}")==0) break;
534 AbEnd("Too many VLIR chains!\n");
536 vlirtable[numchains].chainname=token;
538 /* for first chain - name header */
540 fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
541 "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
543 /* for all other - segment */
544 /* ignore non-existing segments */
545 vlirtable[numchains].exist=1;
546 if ( (strcmp(token,"blank")==0) || (strcmp(token,"noexist")==0) ) {
547 vlirtable[numchains].exist=0;
548 fprintf(outputVFile,"#");
550 fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
551 numchains-1,vlirbase,0x5c00-vlirbase,token);
554 } while (strcmp(token, "}")!=0);
555 fprintf(outputVFile,"}\n\n");
558 AbEnd("There must be at least one VLIR chain.\n");
561 /* now put segments info */
562 fprintf(outputVFile,"SEGMENTS {\n\tHEADER: load = HEADER, type = ro;\n"
563 "\tCODE: load = VLIR0, type = ro;\n"
564 "\tRODATA: load = VLIR0, type = ro;\n"
565 "\tDATA: load = VLIR0, type = rw;\n"
566 "\tBSS: load = VLIR0, type = bss, define = yes;\n\n");
568 for (i=2;i<=numchains;i++) {
569 if (vlirtable[i].exist==0) {
570 fprintf(outputVFile,"#");
572 fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
574 fprintf(outputVFile,"}\n");
576 /* now put usage info */
577 fprintf(outputVFile,"\n# ld65 -o output.cvt -C %s file1.o file2.o ...",outputVName);
578 fprintf(outputVFile,"\n# grc -vlir outputname %s",headname);
579 for (i=1;i<=numchains;i++) {
580 fprintf(outputVFile," %s",vlirtable[i].chainname);
582 fprintf(outputVFile,"\n");
584 if (fclose (outputVFile)!=0)
585 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
588 char *filterInput (FILE *F, char *tbl) {
589 /* loads file into buffer filtering it out */
590 int a, prevchar=-1, i=0, bracket=0, quote=1;
594 if ((a=='\n')||(a=='\015')) a = ' ';
596 if (a=='\042') quote=!quote;
598 if ((a=='{')||(a=='(')) bracket++;
599 if ((a=='}')||(a==')')) bracket--;
601 if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
603 if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
605 if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
607 tbl[i++]=a; prevchar=a; }
611 if (bracket!=0) AbEnd("there are unclosed brackets!\n");
616 void processFile (const char *filename) {
623 int head=0; /* number of processed HEADER sections */
624 int vlir=0; /* number of processed VLIR sections */
626 if ((F = fopen (filename,"r"))==0)
627 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
629 str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
631 token = strtok (str," ");
635 switch (findToken (mainToken, token)) {
637 case 0: DoMenu(); break;
640 AbEnd ("more than one HEADER section, aborting.\n");
645 case 2: break; /* icon not implemented yet */
646 case 3: break; /* dialog not implemented yet */
649 AbEnd ("more than one VLIR section, aborting.\n");
654 default: AbEnd ("unknown section %s.\n",token); break;
658 } while (token!=NULL);
661 int main(int argc, char *argv[]) {
667 const char *arg = argv[i];
674 outputCName=argv[++i];
677 outputSName=argv[++i];
680 outputVName=argv[++i];
688 if (strcmp(arg,"-vlir")==0) {
689 VLIRLinker(argc,argv);
693 AbEnd("unknown option %s\n",arg);
696 default: AbEnd("unknown option %s\n",arg);
701 if (outputCName==NULL)
702 outputCName = MakeFilename(arg,".h");
703 if (outputSName==NULL)
704 outputSName = MakeFilename(arg,".s");
705 if (outputVName==NULL)
706 outputVName = MakeFilename(arg,".cfg");
713 if (ffile==0) AbEnd("no input file\n");