]> git.sur5r.net Git - cc65/blob - src/grc/grc.c
Use xmalloc.h and change the code slightly to get rid of two gcc warnings.
[cc65] / src / grc / grc.c
1
2 /*
3     GEOS resource compiler
4
5     by Maciej 'YTM/Elysium' Witkowiak
6
7     see GEOSLib documentation for license info
8
9 */
10
11 /*
12  - make it work, then do it better
13  - more or less comments? it was hard to code, should be even harder to
14    understand =D
15  - add loadable icons feature (binary - 63 bytes)
16 */
17
18 /* - err, maybe free allocated memory, huh? (who cares, it's just a little prog...)
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <time.h>
27
28 #include "grc.h"
29
30 /* common stuff */
31 #include "fname.h"
32 #include "abend.h"
33 #include "chartype.h"
34 #include "xmalloc.h"
35
36 void VLIRLinker(int argc, char *argv[]) {
37 FILE *outCVT, *input;
38 unsigned char *buffer;
39 unsigned char vlirtabt[127];
40 unsigned char vlirtabs[127];
41 int i,j,lastarg;
42 unsigned l;
43 int bytes;
44 int blocks,rest;
45
46         i=2;
47
48         /* check if we know enough */
49
50         if (argc<4)
51             AbEnd("too few arguments, required [out] [cvthead] [vlir0] ...\n");
52
53         /* first open and copy CVT header */
54
55         outCVT = fopen(argv[i],"wb+");
56         if (outCVT==NULL)
57             AbEnd("can't open output:%s\n",strerror(errno));
58
59         ++i;
60         input = fopen(argv[i],"rb");
61         if (input==NULL)
62             AbEnd("can't open input:%s\n",strerror(errno));
63
64         buffer = xmalloc(THIS_BUFFER_IS_SOOO_HUGE);
65         memset(buffer,0,THIS_BUFFER_IS_SOOO_HUGE);
66
67         bytes = fread(buffer,1,1024,input);
68         fclose(input);
69         if (bytes!=508)
70                 AbEnd("%s is not a cvt header\n",argv[i]);
71
72         fwrite(buffer,1,bytes,outCVT);
73
74         /* now put 254 bytes of VLIR table, to update later */
75
76         /* clear out things */
77         memset(buffer,0,512);
78         fwrite(buffer,1,254,outCVT);
79         for (l=0;l<sizeof(vlirtabt)/sizeof(vlirtabt[0]);l++) {
80                 vlirtabt[l]=0;
81                 vlirtabs[l]=0;
82         }
83
84         /* scan arguments for the last one that is not blank or nonexistant */
85         j=argc;
86         lastarg=argc-1;
87         while (j!=3) {
88                 --j;
89                 if ((strcmp(argv[j],"blank")==0)||(strcmp(argv[j],"noexist")==0))
90                         --lastarg;
91         }
92
93         /* now read all VLIR chains, align to 254 bytes */
94
95         ++i;
96         j=0;
97         while (i!=argc) {
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; }
102                 else {
103                         memset(buffer,0,bytes+512);
104                         input = fopen(argv[i],"rb");
105                         if (input==NULL)
106                                 AbEnd("couldn't open %s:%s\n",argv[i],strerror(errno));
107                         bytes = fread(buffer,1,THIS_BUFFER_IS_SOOO_HUGE,input);
108                         fclose(input);
109                         if (bytes==0)
110                                 AbEnd("couldn't read %s:%s\n",argv[i],strerror(errno));
111                         blocks = bytes / 254;
112                         rest = bytes % 254;
113                         if (rest==0) {
114                             rest = 255;
115                             --blocks;
116                         } else {
117                             ++rest;
118                         }
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 */
122                         if (i==lastarg)
123                                 fwrite(buffer,1,bytes,outCVT);
124                         else
125                                 fwrite(buffer,1,(blocks+1)*254,outCVT);
126                 }
127                 ++j;
128                 ++i;
129         }
130
131         free(buffer);
132
133         /* now rewind and update VLIR table */
134
135         fflush(outCVT);
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);
140         }
141         fclose(outCVT);
142         exit(EXIT_SUCCESS);
143 }
144
145 void printCHeader (void) {
146
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"
150             "\n*/\n\n");
151 }
152
153 void printSHeader (void) {
154
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;"
158             "\n;\n\n");
159 }
160
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#"
167             "\n#\n\n");
168 }
169
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);
175         }
176     if ((outputCFile = fopen (outputCName,outputCMode))==0)
177             AbEnd("can't open file %s for writing: %s\n",outputCName,strerror (errno));
178     if (CFnum==0) {
179                 outputCMode[0]='a';
180                 printCHeader();
181                 CFnum++;
182         }
183 }
184
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);
190         }
191     if ((outputSFile = fopen (outputSName,outputSMode))==0)
192              AbEnd("can't open file %s for writing: %s\n",outputSName,strerror (errno));
193     if (SFnum==0) {
194                 outputSMode[0]='a';
195                 printSHeader();
196                 SFnum++;
197         }
198 }
199
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);
205         }
206     if ((outputVFile = fopen (outputVName,outputVMode))==0)
207              AbEnd("can't open file %s for writting: %s\n",outputVName,strerror (errno));
208     if (VFnum==0) {
209                 outputVMode[0]='a';
210                 printVHeader();
211                 VFnum++;
212         }
213 }
214
215 void printUsage (void) {
216     printf("Usage: %s [options] file\n"
217            "Options:\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",
224            ProgName,ProgName);
225 }
226
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 */
229 int a=0;
230
231     while (strlen(tokenTbl[a])!=0) {
232                 if (strcmp(tokenTbl[a],token)==0) break;
233                 a++;
234     }
235     if (strlen(tokenTbl[a])==0) a=-1;
236     return a;
237 }
238
239 char *nextPhrase() {
240     return strtok(NULL, "\"");
241     }
242
243 char *nextWord() {
244     return strtok(NULL, " ");
245     }
246
247 void setLen (char *name, unsigned len) {
248     if (strlen(name)>len)
249         name[len]='\0';
250 }
251
252 void fillOut (char *name, int len, char *filler) {
253 int a;
254     setLen (name, len);
255     fprintf(outputSFile, ".byte \"%s\"\n\t\t", name);
256     a = len - strlen(name);
257     if (a!=0) {
258         fprintf(outputSFile, ".byte %s", filler);
259         while (--a!=0) fprintf(outputSFile, ", %s", filler);
260         fprintf(outputSFile, "\n\t\t");
261         }
262 }
263
264 char *bintos(unsigned char a, char out[7]) {
265 int i=0;
266     for (;i<8;i++) {
267     out[7-i] = ((a & 1)==0) ? '0' : '1';
268     a = a >> 1; };
269     out[i]='\0';
270 return out;
271 }
272
273 int getNameSize (const char *word) {
274 /* count length of a word using BSW 9 font table */
275 int a=0, i=0;
276
277     while (word[i]!='\0') {
278         a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
279
280     return a;
281 }
282
283 void DoMenu (void) {
284
285 int a, size, tmpsize, item=0;
286 char *token;
287 char namebuff[255]="";
288 struct menu myMenu;
289 struct menuitem *curItem, *newItem;
290
291     openCFile();
292
293     myMenu.name=nextWord();
294     myMenu.left=atoi(nextWord());
295     myMenu.top=atoi(nextWord());
296     myMenu.type=nextWord();
297
298     if (strcmp(nextWord(),"{")!=0) {
299         AbEnd ("menu '%s' description has no opening bracket!\n", myMenu.name);
300         }
301     curItem = xmalloc(sizeof(struct menuitem));
302     myMenu.item=curItem;
303     do {
304         token = nextWord();
305         if (strcmp(token,"}")==0) break;
306         if (token[strlen(token)-1]!='"') {
307             strcpy (namebuff, token);
308             do {
309                 token = nextWord();
310                 strcat (namebuff, " ");
311                 strcat (namebuff, token);
312                 } while (token[strlen(token)-1]!='"');
313             token = xmalloc(strlen(namebuff));
314             strcpy (token, namebuff);
315         }
316         curItem->name=token;
317         curItem->type=nextWord();
318         curItem->target=nextWord();
319         newItem=xmalloc(sizeof(struct menuitem));
320         curItem->next=newItem;
321         curItem=newItem;
322         item++;
323         } while (strcmp(token,"}")!=0);
324     if (item==0) AbEnd ("menu '%s' has 0 items!\n", myMenu.name);
325     if (item>31) AbEnd ("menu '%s' has too many items!\n", myMenu.name);
326
327     curItem->next=NULL;
328
329     /* Count menu sizes */
330
331     size=0;
332     curItem=myMenu.item;
333     if (strstr(myMenu.type,"HORIZONTAL")!=NULL) {
334         /* menu is HORIZONTAL, ysize=15, sum xsize of all items +~8?*/
335             myMenu.bot=myMenu.top+15;
336             for (a=0;a!=item;a++) {
337                 size+=getNameSize(curItem->name);
338                 curItem=curItem->next;
339                 };
340         } else {
341         /* menu is VERTICAL, ysize=item*15, count largest xsize of all items +~8? */
342             myMenu.bot=myMenu.top+(14*item);
343             for (a=0;a!=item;a++) {
344                 tmpsize=getNameSize(curItem->name);
345                 size = (size > tmpsize) ? size : tmpsize;
346                 curItem=curItem->next;
347                 };
348         };
349     myMenu.right=myMenu.left+size-1;
350
351     curItem=myMenu.item;
352     for (a=0;a!=item;a++) {
353         /* print prototype only if MENU_ACTION or DYN_SUB_MENU are present in type */
354         if ((strstr(curItem->type, "MENU_ACTION")!=NULL) || (strstr(curItem->type, "DYN_SUB_MENU")!=NULL))
355             fprintf(outputCFile, "void %s (void);\n", curItem->target);
356         curItem=curItem->next;
357         }
358
359     fprintf(outputCFile, "\nconst void %s = {\n\t(char)%i, (char)%i,\n\t(int)%i, (int)%i,\n\t"
360             "(char)(%i | %s),\n", myMenu.name, myMenu.top, myMenu.bot, myMenu.left,
361             myMenu.right, item, myMenu.type);
362
363     curItem=myMenu.item;
364     for (a=0;a!=item;a++) {
365         fprintf(outputCFile, "\t%s, (char)%s, (int)", curItem->name, curItem->type);
366         if ((strstr(curItem->type, "SUB_MENU")!=NULL) && (strstr(curItem->type, "DYN_SUB_MENU")==NULL))
367             fprintf(outputCFile, "&");
368         fprintf(outputCFile, "%s,\n", curItem->target);
369         curItem=curItem->next;
370         }
371
372     fprintf(outputCFile, "\t};\n\n");
373
374     if (fclose (outputCFile)!=0)
375         AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
376 }
377
378 void DoHeader (void) {
379
380 time_t t;
381 struct tm *my_tm;
382
383 struct appheader myHead;
384 char *token;
385 char i1[9], i2[9], i3[9];
386 int a, b;
387
388     openSFile();
389
390     token = nextWord();
391
392     a = findToken (hdrFTypes, token);
393
394     switch (a) {
395         case 0:  myHead.geostype = 6; break;
396         case 1:  myHead.geostype = 14; break;
397         default: AbEnd("filetype '%s' is not supported yet\n", token);
398         }
399
400     myHead.dosname = nextPhrase();
401     nextPhrase();
402     myHead.classname = nextPhrase();
403     nextPhrase();
404     myHead.version = nextPhrase();
405
406     /* put default values into myHead here */
407     myHead.author = "cc65";
408     myHead.info = "Program compiled with cc65 and GEOSLib.";
409     myHead.dostype = 128+3;
410     myHead.structure = 0;
411     myHead.mode = 0;
412
413     t = time(NULL);
414     my_tm = localtime (&t);
415
416     myHead.year = my_tm->tm_year;
417     myHead.month = my_tm->tm_mon+1;
418     myHead.day = my_tm->tm_mday;
419     myHead.hour = my_tm->tm_hour;
420     myHead.min = my_tm->tm_min;
421
422     if (strcmp(nextWord(),"{")!=0)
423                 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
424
425     do {
426         token=nextWord();
427         if (strcmp(token, "}")==0) break;
428         switch (a = findToken (hdrFields, token)) {
429                 case -1:
430                     AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
431                     break;
432                 case 0: /* author */
433                     myHead.author = nextPhrase(); break;
434                 case 1: /* info */
435                     myHead.info = nextPhrase(); break;
436                 case 2: /* date */
437                     myHead.year = atoi(nextWord());
438                     myHead.month = atoi(nextWord());
439                     myHead.day = atoi(nextWord());
440                     myHead.hour = atoi(nextWord());
441                     myHead.min = atoi(nextWord());
442                     break;
443                 case 3: /* dostype */
444                     switch (b = findToken (hdrDOSTp, nextWord())) {
445                         case -1:
446                             AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
447                             break;
448                         default:
449                             myHead.dostype = b/2 + 128 + 1;
450                             break;
451                     }
452                     break;
453                 case 4: /* mode */
454                     switch (b = findToken (hdrModes, nextWord())) {
455                         case -1:
456                             AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
457                         case 0:
458                             myHead.mode = 0x40; break;
459                         case 1:
460                             myHead.mode = 0x00; break;
461                         case 2:
462                             myHead.mode = 0xc0; break;
463                         case 3:
464                             myHead.mode = 0x80; break;
465                     }
466                     break;
467                 case 5: /* structure */
468                     switch (b = findToken(hdrStructTp, nextWord())) {
469                         case -1:
470                             AbEnd ("unknown structure type in header '%s'\n", myHead.dosname);
471                         case 0:
472                         case 1:
473                             myHead.structure = 0; break;
474                         case 2:
475                         case 3:
476                             myHead.structure = 1; break;
477                     }
478                     break;
479         }
480
481     } while (strcmp(token, "}")!=0);
482
483     /* OK, all information is gathered, do flushout */
484
485     fprintf(outputSFile,
486          "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
487
488     fillOut(myHead.dosname,16,"$a0");
489
490     fprintf(outputSFile,
491         ".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"
492         ".word 0\n\t\t.byte \"PRG formatted GEOS file V1.0\"\n\n\t\t.res $c4\n\n\t\t"
493         ".byte 3, 21, 63 | $80\n\t\t",
494         myHead.structure, myHead.geostype, myHead.year, myHead.month, myHead.day,
495         myHead.hour, myHead.min);
496
497     for (a=0;a!=63;a=a+3) {
498         fprintf(outputSFile,
499              ".byte %%%s, %%%s, %%%s\n\t\t",
500              bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
501
502     fprintf(outputSFile,
503             "\n\t\t.byte %i, %i, %i\n\t\t.word $0400, $0400-1, $0400\n\n\t\t",
504             myHead.dostype, myHead.geostype, myHead.structure);
505
506     fillOut(myHead.classname,12,"$20");
507
508     fillOut(myHead.version,4,"0");
509
510     fprintf(outputSFile,
511             ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
512
513     setLen(myHead.author,62);
514     fprintf(outputSFile,
515             ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (63-%i)\n\n\t\t",
516             myHead.author, (int) (strlen(myHead.author)+1));
517
518     setLen(myHead.info, 95);
519     fprintf(outputSFile,
520             ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
521             myHead.info, (int) (strlen(myHead.info)+1));
522
523     if (fclose (outputSFile)!=0)
524                 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
525
526 }
527
528 void DoVLIR (void) {
529
530 char *token;
531 char *headname;
532 int i,numchains,vlirbase;
533 struct vlirentry {
534     char *chainname;
535     int exist;
536 };
537
538 struct vlirentry vlirtable[127];
539
540     openVFile();
541
542     headname = nextWord();
543
544     vlirbase = strtol(nextWord(),NULL,0);
545
546     if (strcmp(nextWord(),"{")!=0)
547                 AbEnd ("VLIR description has no opening bracket!\n");
548
549     numchains=0;
550
551     do {
552         token=nextWord();
553         if (strcmp(token, "}")==0) break;
554         numchains++;
555         if (numchains>127) {
556             AbEnd("Too many VLIR chains!\n");
557         }
558         vlirtable[numchains].chainname=token;
559
560         /* for first chain - name header */
561         if (numchains==1) {
562           fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
563             "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
564         } else {
565         /* for all other - segment */
566           /* ignore non-existing segments */
567           vlirtable[numchains].exist=1;
568           if ( (strcmp(token,"blank")==0) || (strcmp(token,"noexist")==0) ) {
569             vlirtable[numchains].exist=0;
570             fprintf(outputVFile,"#");
571           }
572           fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
573             numchains-1,vlirbase,0x5c00-vlirbase,token);
574         }
575
576     } while (strcmp(token, "}")!=0);
577     fprintf(outputVFile,"}\n\n");
578
579     if (numchains==0) {
580         AbEnd("There must be at least one VLIR chain.\n");
581     };
582
583     /* now put segments info */
584     fprintf(outputVFile,"SEGMENTS {\n\tHEADER: load = HEADER, type = ro;\n"
585         "\tCODE: load = VLIR0, type = ro;\n"
586         "\tRODATA: load = VLIR0, type = ro;\n"
587         "\tDATA: load = VLIR0, type = rw;\n"
588         "\tBSS: load = VLIR0, type = bss, define = yes;\n\n");
589
590     for (i=2;i<=numchains;i++) {
591         if (vlirtable[i].exist==0) {
592             fprintf(outputVFile,"#");
593         }
594         fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
595     }
596     fprintf(outputVFile,"}\n");
597
598     /* now put usage info */
599     fprintf(outputVFile,"\n# ld65 -o output.cvt -C %s file1.o file2.o ...",outputVName);
600     fprintf(outputVFile,"\n# grc -vlir outputname %s",headname);
601     for (i=1;i<=numchains;i++) {
602         fprintf(outputVFile," %s",vlirtable[i].chainname);
603     }
604     fprintf(outputVFile,"\n");
605
606     if (fclose (outputVFile)!=0)
607                 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
608 }
609
610 char *filterInput (FILE *F, char *tbl) {
611 /* loads file into buffer filtering it out */
612 int a, prevchar=-1, i=0, bracket=0, quote=1;
613
614     while (1) {
615         a = getc(F);
616         if ((a=='\n')||(a=='\015')) a = ' ';
617         if (a==',' && quote) a = ' ';
618         if (a=='\042') quote=!quote;
619         if (quote) {
620             if ((a=='{')||(a=='(')) bracket++;
621             if ((a=='}')||(a==')')) bracket--;
622         }
623         if (a==EOF) { tbl[i]='\0'; xrealloc(tbl, i+1); break; };
624         if (IsSpace(a)) {
625             if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
626         } else {
627             if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
628                 else {
629                     tbl[i++]=a; prevchar=a; }
630         }
631     }
632
633     if (bracket!=0) AbEnd("there are unclosed brackets!\n");
634
635     return tbl;
636 }
637
638 void processFile (const char *filename) {
639
640 FILE *F;
641
642 char *str;
643 char *token;
644
645 int head=0;     /* number of processed HEADER sections */
646 int vlir=0;     /* number of processed VLIR sections */
647
648     if ((F = fopen (filename,"r"))==0)
649                 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
650
651     str=filterInput(F, xmalloc(BLOODY_BIG_BUFFER));
652
653     token = strtok (str," ");
654
655     do {
656         if (str!=NULL) {
657             switch (findToken (mainToken, token)) {
658
659             case 0: DoMenu(); break;
660             case 1:
661                 if (++head!=1) {
662                         AbEnd ("more than one HEADER section, aborting.\n");
663                     } else {
664                         DoHeader();
665                     }
666                 break;
667             case 2: break;      /* icon not implemented yet */
668             case 3: break;      /* dialog not implemented yet */
669             case 4:
670                 if (++vlir!=1) {
671                         AbEnd ("more than one VLIR section, aborting.\n");
672                 } else {
673                         DoVLIR();
674                 }
675                 break;
676             default: AbEnd ("unknown section %s.\n",token); break;
677             }
678         }
679         token = nextWord();
680     } while (token!=NULL);
681 }
682
683 int main(int argc, char *argv[]) {
684
685 int ffile=0, i=1;
686
687     ProgName = argv[0];
688     while (i < argc) {
689         const char *arg = argv[i];
690         if (arg[0] == '-') {
691             switch (arg[1]) {
692                 case 'f':
693                     forceFlag=1;
694                     break;
695                 case 'o':
696                     outputCName=argv[++i];
697                     break;
698                 case 's':
699                     outputSName=argv[++i];
700                     break;
701                 case 'l':
702                     outputVName=argv[++i];
703                     break;
704                 case 'h':
705                 case '?':
706                     printUsage();
707                     exit (EXIT_SUCCESS);
708                     break;
709                 case 'v':
710                     if (strcmp(arg,"-vlir")==0) {
711                         VLIRLinker(argc,argv);
712                         exit (EXIT_SUCCESS);
713                         break;
714                     } else {
715                         AbEnd("unknown option %s\n",arg);
716                         break;
717                     }
718                 default: AbEnd("unknown option %s\n",arg);
719             }
720         } else {
721             ffile++;
722
723                 if (outputCName==NULL)
724                         outputCName = MakeFilename(arg,".h");
725                 if (outputSName==NULL)
726                         outputSName = MakeFilename(arg,".s");
727                 if (outputVName==NULL)
728                         outputVName = MakeFilename(arg,".cfg");
729
730             processFile(arg);
731
732             }
733         i++;
734         }
735     if (ffile==0) AbEnd("no input file\n");
736
737     return EXIT_SUCCESS;
738 }