5 by Maciej 'YTM/Alliance' Witkowiak
9 see GEOSLib documentation for license info
14 - make it work, then do it better
15 - more or less comments? it was hard to code, should be even harder to
17 - add loadable icons feature (binary - 63 bytes)
20 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
34 void Error (const char* Format, ...)
35 /* borrowed from cl65/error.c */
36 /* Print an error message and die */
39 va_start (ap, Format);
40 fprintf (stderr, "%s: ", progName);
41 vfprintf (stderr, Format, ap);
46 void printCHeader (void) {
48 fprintf(outputCFile, "\n/*\n\tThis file was generated by GEOS Resource Compiler\n"
49 "\n\tDO NOT EDIT! Any changes will be lost!\n"
50 "\n\tEdit proper resource file instead\n"
54 void printSHeader (void) {
56 fprintf(outputSFile, "\n;\n;\tThis file was generated by GEOS Resource Compiler\n;"
57 "\n;\tDO NOT EDIT! Any changes will be lost!\n;"
58 "\n;\tEdit proper resource file instead\n;"
62 void openCFile (void) {
63 if ((CFnum==0) && (forceFlag==0)) {
64 /* test if file exists already and no forcing*/
65 if ((outputCFile = fopen (outputCName,"r"))!=0)
66 Error("file %s already exists, no forcing, aborting\n", outputCName);
68 if ((outputCFile = fopen (outputCName,outputCMode))==0)
69 Error("can't open file %s for writting: %s\n",outputCName,strerror (errno));
70 if (CFnum==0) { outputCMode[0]='a'; printCHeader(); CFnum++; }
73 void openSFile (void) {
74 if ((SFnum==0) && (forceFlag==0)) {
75 /* test if file exists already and no forcing*/
76 if ((outputSFile = fopen (outputSName,"r"))!=0)
77 Error("file %s already exists, no forcing, aborting\n", outputSName);
79 if ((outputSFile = fopen (outputSName,outputSMode))==0)
80 Error("can't open file %s for writting: %s\n",outputSName,strerror (errno));
81 if (SFnum==0) { outputSMode[0]='a'; printSHeader(); SFnum++; }
84 void printUsage (void) {
85 fprintf(stderr, "Usage: %s [options] file\n"
87 "\t-h, -?\t\tthis help\n"
88 "\t-f\t\tforce writting files\n"
89 "\t-o name\t\tname C output file\n"
90 "\t-s name\t\tname asm output file\n",
95 int findToken (const char **tokenTbl, const char *token) {
96 /* takes as input table of tokens and token, returns position in table or -1 if not found */
99 while (strlen(tokenTbl[a])!=0) {
100 if (strcmp(tokenTbl[a],token)==0) break;
103 if (strlen(tokenTbl[a])==0) a=-1;
108 return strtok(NULL, "\"");
112 return strtok(NULL, " ");
115 void setLen (char *name, int len) {
116 if (strlen(name)>len)
120 void fillOut (char *name, int len, char *filler) {
123 fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
124 a = len - strlen(name);
126 fprintf(outputSFile, ".byte %s", filler);
127 while (--a!=0) fprintf(outputSFile, ", %s", filler);
128 fprintf(outputSFile, "\n\t\t");
132 char *bintos(unsigned char a, char *out) {
135 out[7-i] = ((a & 1)==0) ? '0' : '1';
141 int getNameSize (const char *word) {
142 /* count length of a word using BSW 9 font table */
145 while (word[i]!='\0') {
146 a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
153 int a, size, tmpsize, item=0;
155 char namebuff[255]="";
157 struct menuitem *curItem, *newItem;
161 myMenu.name=nextWord();
162 myMenu.left=atoi(nextWord());
163 myMenu.top=atoi(nextWord());
164 myMenu.type=nextWord();
166 if (strcmp(nextWord(),"{")!=0) {
167 Error ("menu %s description has no opening bracket!\n", myMenu.name);
169 curItem=malloc(sizeof(struct menuitem));
173 if (strcmp(token,"}")==0) break;
174 if (token[strlen(token)-1]!='"') {
175 strcpy (namebuff, token);
178 strcat (namebuff, " ");
179 strcat (namebuff, token);
180 } while (token[strlen(token)-1]!='"');
181 token = malloc(strlen(namebuff));
182 strcpy (token, namebuff);
185 curItem->type=nextWord();
186 curItem->target=nextWord();
187 newItem=malloc(sizeof(struct menuitem));
188 curItem->next=newItem;
191 } while (strcmp(token,"}")!=0);
192 if (item==0) Error ("menu %s has 0 items!\n", myMenu.name);
193 if (item>31) Error ("menu %s has too many items!\n", myMenu.name);
197 /* Count menu sizes */
201 if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
202 /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
203 myMenu.bot=myMenu.top+15;
204 for (a=0;a!=item;a++) {
205 size+=getNameSize(curItem->name);
206 curItem=curItem->next;
209 /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
210 myMenu.bot=myMenu.top+(14*item)-1;
211 for (a=0;a!=item;a++) {
212 tmpsize=getNameSize(curItem->name);
213 size = (size > tmpsize) ? size : tmpsize;
214 curItem=curItem->next;
217 myMenu.right=myMenu.left+size-1;
220 for (a=0;a!=item;a++) {
221 /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
222 if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
223 fprintf(outputCFile, "void %s (void);\n", curItem->target);
224 curItem=curItem->next;
227 fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
228 "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
229 myMenu.right, item, myMenu.type);
232 for (a=0;a!=item;a++) {
233 fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
234 if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
235 fprintf(outputCFile, "&");
236 fprintf(outputCFile, "%s,\n", curItem->target);
237 curItem=curItem->next;
240 fprintf(outputCFile, "\t};\n\n");
242 if (fclose (outputCFile)!=0)
243 Error("error closing %s: %s\n",outputCName,strerror (errno));
246 void DoHeader (void) {
251 struct appheader myHead;
253 char i1[9], i2[9], i3[9];
260 a = findToken (hdrFTypes, token);
263 Error("filetype %s isn't supported yet\n", token);
266 case 0: myHead.geostype = 6; break;
267 case 1: myHead.geostype = 14; break;
270 myHead.dosname = nextPhrase();
272 myHead.classname = nextPhrase();
274 myHead.version = nextPhrase();
276 /* put default values into myHead here */
277 myHead.author = "cc65";
278 myHead.info = "Program compiled with cc65 and GEOSLib.";
279 myHead.dostype = 128+3;
283 my_tm = localtime (&t);
285 myHead.year = my_tm->tm_year;
286 myHead.month = my_tm->tm_mon+1;
287 myHead.day = my_tm->tm_mday;
288 myHead.hour = my_tm->tm_hour;
289 myHead.min = my_tm->tm_min;
291 if (strcmp(nextWord(),"{")!=0) {
292 Error ("header %s has no opening bracket!\n", myHead.dosname);
297 if (strcmp(token, "}")==0) break;
298 switch (a = findToken (hdrFields, token)) {
300 Error ("unknown field %s in header %s\n", token, myHead.dosname);
303 myHead.author = nextPhrase(); break;
305 myHead.info = nextPhrase(); break;
307 myHead.year = atoi(nextWord());
308 myHead.month = atoi(nextWord());
309 myHead.day = atoi(nextWord());
310 myHead.hour = atoi(nextWord());
311 myHead.min = atoi(nextWord());
313 case 3: /* dostype */
314 switch (b = findToken (hdrDOSTp, nextWord())) {
316 Error ("unknown dostype in header %s\n", myHead.dosname);
319 myHead.dostype = b/2 + 128 + 1;
324 switch (b = findToken (hdrModes, nextWord())) {
326 Error ("unknown mode in header %s\n", myHead.dosname);
328 myHead.mode = 0x40; break;
330 myHead.mode = 0x00; break;
332 myHead.mode = 0xc0; break;
334 myHead.mode = 0x80; break;
339 } while (strcmp(token, "}")!=0);
341 /* OK, all information is gathered, do flushout */
344 "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
346 fillOut(myHead.dosname,16,"$a0");
349 ".word 0\n\t\t.byte 0\n\t\t.byte %i\n\t\t.byte %i, %i, %i, %i, %i\n\n\t\t"
350 ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
351 ".byte 3, 21, 63 | $80\n\t\t",
352 myHead.geostype, myHead.year, myHead.month, myHead.day, myHead.hour,
355 for (a=0;a!=63;a=a+3) {
357 ".byte %%%s, %%%s, %%%s\n\t\t",
358 bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
361 "\n\t\t.byte %i, %i, 0\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
362 myHead.dostype, myHead.geostype);
364 fillOut(myHead.classname,12,"$20");
366 fillOut(myHead.version,4,"0");
369 ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
371 setLen(myHead.author,62);
373 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
374 myHead.author, (int) (strlen(myHead.author)+1));
376 setLen(myHead.info, 95);
378 ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
379 myHead.info, (int) (strlen(myHead.info)+1));
381 if (fclose (outputSFile)!=0)
382 Error("error closing %s: %s\n",outputSName,strerror (errno));
387 char *filterInput (FILE *F, char *tbl) {
388 /* loads file into buffer filtering it out */
389 int a, prevchar=-1, i=0, bracket=0, quote=1;
393 if ((a=='\n')||(a=='\015')) a = ' ';
395 if (a=='\042') quote=!quote;
397 if ((a=='{')||(a=='(')) bracket++;
398 if ((a=='}')||(a==')')) bracket--;
400 if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
402 if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
404 if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
406 tbl[i++]=a; prevchar=a; }
410 if (bracket!=0) Error("there are unclosed brackets!\n");
415 void processFile (const char *filename) {
424 if ((F = fopen (filename,"r"))==0)
425 Error("can't open file %s for reading: %s\n",filename,strerror (errno));
427 str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
429 token = strtok (str," ");
433 switch (findToken (mainToken, token)) {
435 case 0: DoMenu(); break;
438 Error ("more than one HEADER section, aborting.\n");
445 default: Error ("unknown section %s.\n",token); break;
449 } while (token!=NULL);
452 int main(int argc, char *argv[]) {
459 const char *arg = argv[i];
466 outputCName=argv[++i];
469 outputSName=argv[++i];
476 default: Error("unknown option %s\n",arg);
481 tmp = malloc(strlen(arg)+4);
483 if ((p = strrchr (tmp, '.')))
487 if (outputCName==NULL) {
488 outputCName = malloc(strlen(arg));
489 strcpy (outputCName, tmp);
490 strcat (outputCName, ".h");
494 if (outputSName==NULL) {
495 outputSName = malloc(strlen(arg));
496 strcpy (outputSName, tmp);
497 strcat (outputSName, ".s");
506 if (ffile==0) Error("no input file\n");