]> git.sur5r.net Git - cc65/blob - src/grc/grc.c
Updated PETSCII mappings
[cc65] / src / grc / grc.c
1
2 /*
3     GEOS resource compiler
4
5     by Maciej 'YTM/Alliance' Witkowiak
6
7     Error function by Uz
8
9     see GEOSLib documentation for license info
10
11 */
12
13 /*
14  - make it work, then do it better
15  - more or less comments? it was hard to code, should be even harder to
16    understand =D
17  - add loadable icons feature (binary - 63 bytes)
18 */
19
20 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
21 */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <errno.h>
29 #include <time.h>
30
31 #include "grc.h"
32
33
34 void Error (const char* Format, ...)
35 /* borrowed from cl65/error.c     */
36 /* Print an error message and die */
37 {
38     va_list ap;
39     va_start (ap, Format);
40     fprintf (stderr, "%s: ", progName);
41     vfprintf (stderr, Format, ap);
42     va_end (ap);
43     exit (EXIT_FAILURE);
44 }
45
46 void printCHeader (void) {
47
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"
51             "\n*/\n\n");
52 }
53
54 void printSHeader (void) {
55
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;"
59             "\n;\n\n");
60 }
61
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);
67         }
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++; }
71 }
72
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);
78         }
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++; }
82 }
83
84 void printUsage (void) {
85     fprintf(stderr, "Usage: %s [options] file\n"
86             "Options:\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",
91             progName);
92
93 }
94
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 */
97 int a=0;
98
99     while (strlen(tokenTbl[a])!=0) {
100         if (strcmp(tokenTbl[a],token)==0) break;
101         a++;
102     }
103     if (strlen(tokenTbl[a])==0) a=-1;
104     return a;
105 }
106
107 char *nextPhrase() {
108     return strtok(NULL, "\"");
109     }
110
111 char *nextWord() {
112     return strtok(NULL, " ");
113     }
114
115 void setLen (char *name, unsigned len) {
116     if (strlen(name)>len)
117         name[len]='\0';
118 }
119
120 void fillOut (char *name, int len, char *filler) {
121 int a;
122     setLen (name, len);
123     fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
124     a = len - strlen(name);
125     if (a!=0) {
126         fprintf(outputSFile, ".byte %s", filler);
127         while (--a!=0) fprintf(outputSFile, ", %s", filler);
128         fprintf(outputSFile, "\n\t\t");
129         }
130 }
131
132 char *bintos(unsigned char a, char *out) {
133 int i=0;
134     for (;i<8;i++) {
135     out[7-i] = ((a & 1)==0) ? '0' : '1';
136     a = a >> 1; };
137     out[i]='\0';
138 return out;
139 }
140
141 int getNameSize (const char *word) {
142 /* count length of a word using BSW 9 font table */
143 int a=0, i=0;
144
145     while (word[i]!='\0') {
146         a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
147
148     return a;
149 }
150
151 void DoMenu (void) {
152
153 int a, size, tmpsize, item=0;
154 char *token;
155 char namebuff[255]="";
156 struct menu myMenu;
157 struct menuitem *curItem, *newItem;
158
159     openCFile();
160
161     myMenu.name=nextWord();
162     myMenu.left=atoi(nextWord());
163     myMenu.top=atoi(nextWord());
164     myMenu.type=nextWord();
165
166     if (strcmp(nextWord(),"{")!=0) {
167         Error ("menu %s description has no opening bracket!\n", myMenu.name);
168         };
169     curItem=malloc(sizeof(struct menuitem));
170     myMenu.item=curItem;
171     do {
172         token = nextWord();
173         if (strcmp(token,"}")==0) break;
174         if (token[strlen(token)-1]!='"') {
175             strcpy (namebuff, token);
176             do {
177                 token = nextWord();
178                 strcat (namebuff, " ");
179                 strcat (namebuff, token);
180                 } while (token[strlen(token)-1]!='"');
181             token = malloc(strlen(namebuff));
182             strcpy (token, namebuff);
183         }
184         curItem->name=token;
185         curItem->type=nextWord();
186         curItem->target=nextWord();
187         newItem=malloc(sizeof(struct menuitem));
188         curItem->next=newItem;
189         curItem=newItem;
190         item++;
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);
194
195     curItem->next=NULL;
196
197     /* Count menu sizes */
198
199     size=0;
200     curItem=myMenu.item;
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;
207                 };
208         } else {
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;
215                 };
216         };
217     myMenu.right=myMenu.left+size-1;
218
219     curItem=myMenu.item;
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;
225         }
226
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);
230
231     curItem=myMenu.item;
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;
238         }
239
240     fprintf(outputCFile, "\t};\n\n");
241
242     if (fclose (outputCFile)!=0)
243         Error("error closing %s: %s\n",outputCName,strerror (errno));
244 }
245
246 void DoHeader (void) {
247
248 time_t t;
249 struct tm *my_tm;
250
251 struct appheader myHead;
252 char *token;
253 char i1[9], i2[9], i3[9];
254 int a, b;
255
256     openSFile();
257
258     token = nextWord();
259
260     a = findToken (hdrFTypes, token);
261
262     if (a>1)
263         Error("filetype %s isn't supported yet\n", token);
264
265     switch (a) {
266         case 0: myHead.geostype = 6; break;
267         case 1: myHead.geostype = 14; break;
268         }
269
270     myHead.dosname = nextPhrase();
271     nextPhrase();
272     myHead.classname = nextPhrase();
273     nextPhrase();
274     myHead.version = nextPhrase();
275
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;
280     myHead.mode = 0;
281
282     t = time(NULL);
283     my_tm = localtime (&t);
284
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;
290
291     if (strcmp(nextWord(),"{")!=0) {
292         Error ("header %s has no opening bracket!\n", myHead.dosname);
293         };
294
295     do {
296         token=nextWord();
297         if (strcmp(token, "}")==0) break;
298         switch (a = findToken (hdrFields, token)) {
299                 case -1:
300                     Error ("unknown field %s in header %s\n", token, myHead.dosname);
301                     break;
302                 case 0: /* author */
303                     myHead.author = nextPhrase(); break;
304                 case 1: /* info */
305                     myHead.info = nextPhrase(); break;
306                 case 2: /* date */
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());
312                     break;
313                 case 3: /* dostype */
314                     switch (b = findToken (hdrDOSTp, nextWord())) {
315                         case -1:
316                             Error ("unknown dostype in header %s\n", myHead.dosname);
317                             break;
318                         default:
319                             myHead.dostype = b/2 + 128 + 1;
320                             break;
321                     }
322                     break;
323                 case 4: /* mode */
324                     switch (b = findToken (hdrModes, nextWord())) {
325                         case -1:
326                             Error ("unknown mode in header %s\n", myHead.dosname);
327                         case 0:
328                             myHead.mode = 0x40; break;
329                         case 1:
330                             myHead.mode = 0x00; break;
331                         case 2:
332                             myHead.mode = 0xc0; break;
333                         case 3:
334                             myHead.mode = 0x80; break;
335                     }
336                     break;
337         }
338
339     } while (strcmp(token, "}")!=0);
340
341     /* OK, all information is gathered, do flushout */
342
343     fprintf(outputSFile,
344          "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
345
346     fillOut(myHead.dosname,16,"$a0");
347
348     fprintf(outputSFile,
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,
353         myHead.min);
354
355     for (a=0;a!=63;a=a+3) {
356         fprintf(outputSFile,
357              ".byte %%%s, %%%s, %%%s\n\t\t",
358              bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
359
360     fprintf(outputSFile,
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);
363
364     fillOut(myHead.classname,12,"$20");
365
366     fillOut(myHead.version,4,"0");
367
368     fprintf(outputSFile,
369             ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
370
371     setLen(myHead.author,62);
372     fprintf(outputSFile,
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));
375
376     setLen(myHead.info, 95);
377     fprintf(outputSFile,
378             ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
379             myHead.info, (int) (strlen(myHead.info)+1));
380
381     if (fclose (outputSFile)!=0)
382         Error("error closing %s: %s\n",outputSName,strerror (errno));
383
384
385 }
386
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;
390
391     while (1) {
392         a = getc(F);
393         if ((a=='\n')||(a=='\015')) a = ' ';
394         if (a==',') a = ' ';
395         if (a=='\042') quote=!quote;
396         if (quote) {
397             if ((a=='{')||(a=='(')) bracket++;
398             if ((a=='}')||(a==')')) bracket--;
399         }
400         if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
401         if (isspace(a)) {
402             if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
403         } else {
404             if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
405                 else {
406                     tbl[i++]=a; prevchar=a; }
407         }
408     }
409
410     if (bracket!=0) Error("there are unclosed brackets!\n");
411
412     return tbl;
413 }
414
415 void processFile (const char *filename) {
416
417 FILE *F;
418
419 char *str;
420 char *token;
421
422 int head=0;
423
424     if ((F = fopen (filename,"r"))==0)
425         Error("can't open file %s for reading: %s\n",filename,strerror (errno));
426
427     str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
428
429     token = strtok (str," ");
430
431     do {
432         if (str!=NULL) {
433             switch (findToken (mainToken, token)) {
434
435             case 0: DoMenu(); break;
436             case 1:
437                 if (++head!=1) {
438                         Error ("more than one HEADER section, aborting.\n");
439                     } else {
440                         DoHeader();
441                     }
442                 break;
443             case 2: break;
444             case 3: break;
445             default: Error ("unknown section %s.\n",token); break;
446             }
447         }
448         token = nextWord();
449     } while (token!=NULL);
450 }
451
452 int main(int argc, char *argv[]) {
453
454 int ffile=0, i=1;
455 char *p, *tmp;
456
457     progName = argv[0];
458     while (i < argc) {
459         const char *arg = argv[i];
460         if (arg[0] == '-') {
461             switch (arg[1]) {
462                 case 'f':
463                     forceFlag=1;
464                     break;
465                 case 'o':
466                     outputCName=argv[++i];
467                     break;
468                 case 's':
469                     outputSName=argv[++i];
470                     break;
471                 case 'h':
472                 case '?':
473                     printUsage();
474                     exit (EXIT_SUCCESS);
475                     break;
476                 default: Error("unknown option %s\n",arg);
477             }
478         } else {
479             ffile++;
480
481             tmp = malloc(strlen(arg)+4);
482             strcpy (tmp, arg);
483             if ((p = strrchr (tmp, '.')))
484                 *p = '\0';
485
486
487             if (outputCName==NULL) {
488                 outputCName = malloc(strlen(arg));
489                 strcpy (outputCName, tmp);
490                 strcat (outputCName, ".h");
491             }
492
493
494             if (outputSName==NULL) {
495                 outputSName = malloc(strlen(arg));
496                 strcpy (outputSName, tmp);
497                 strcat (outputSName, ".s");
498             }
499
500
501             processFile(arg);
502
503             }
504         i++;
505         }
506     if (ffile==0) Error("no input file\n");
507
508     return EXIT_SUCCESS;
509 }