]> git.sur5r.net Git - cc65/blob - src/grc/grc.c
Optimization for __bzero.
[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 "cmdline.h"
32 #include "fname.h"
33 #include "abend.h"
34 #include "chartype.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 = malloc(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     fprintf(stderr, "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) {
265 char *bintos(unsigned char a, char out[7]) {
266 int i=0;
267     for (;i<8;i++) {
268     out[7-i] = ((a & 1)==0) ? '0' : '1';
269     a = a >> 1; };
270     out[i]='\0';
271 return out;
272 }
273
274 int getNameSize (const char *word) {
275 /* count length of a word using BSW 9 font table */
276 int a=0, i=0;
277
278     while (word[i]!='\0') {
279         a+=(BSWTab[word[i]-31] - BSWTab[word[i]-32]); i++; }
280
281     return a;
282 }
283
284 void DoMenu (void) {
285
286 int a, size, tmpsize, item=0;
287 char *token;
288 char namebuff[255]="";
289 struct menu myMenu;
290 struct menuitem *curItem, *newItem;
291
292     openCFile();
293
294     myMenu.name=nextWord();
295     myMenu.left=atoi(nextWord());
296     myMenu.top=atoi(nextWord());
297     myMenu.type=nextWord();
298
299     if (strcmp(nextWord(),"{")!=0) {
300         AbEnd ("menu '%s' description has no opening bracket!\n", myMenu.name);
301         };
302     curItem=malloc(sizeof(struct menuitem));
303     myMenu.item=curItem;
304     do {
305         token = nextWord();
306         if (strcmp(token,"}")==0) break;
307         if (token[strlen(token)-1]!='"') {
308             strcpy (namebuff, token);
309             do {
310                 token = nextWord();
311                 strcat (namebuff, " ");
312                 strcat (namebuff, token);
313                 } while (token[strlen(token)-1]!='"');
314             token = malloc(strlen(namebuff));
315             strcpy (token, namebuff);
316         }
317         curItem->name=token;
318         curItem->type=nextWord();
319         curItem->target=nextWord();
320         newItem=malloc(sizeof(struct menuitem));
321         curItem->next=newItem;
322         curItem=newItem;
323         item++;
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);
327
328     curItem->next=NULL;
329
330     /* Count menu sizes */
331
332     size=0;
333     curItem=myMenu.item;
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;
340                 };
341         } else {
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;
348                 };
349         };
350     myMenu.right=myMenu.left+size-1;
351
352     curItem=myMenu.item;
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;
358         }
359
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);
363
364     curItem=myMenu.item;
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;
371         }
372
373     fprintf(outputCFile, "\t};\n\n");
374
375     if (fclose (outputCFile)!=0)
376         AbEnd("error closing %s: %s\n",outputCName,strerror (errno));
377 }
378
379 void DoHeader (void) {
380
381 time_t t;
382 struct tm *my_tm;
383
384 struct appheader myHead;
385 char *token;
386 char i1[9], i2[9], i3[9];
387 int a, b;
388
389     openSFile();
390
391     token = nextWord();
392
393     a = findToken (hdrFTypes, token);
394
395     if (a>1)
396            AbEnd("filetype '%s' is not supported yet\n", token);
397
398     switch (a) {
399         case 0: myHead.geostype = 6; break;
400         case 1: myHead.geostype = 14; break;
401         }
402
403     myHead.dosname = nextPhrase();
404     nextPhrase();
405     myHead.classname = nextPhrase();
406     nextPhrase();
407     myHead.version = nextPhrase();
408
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;
414     myHead.mode = 0;
415
416     t = time(NULL);
417     my_tm = localtime (&t);
418
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;
424
425     if (strcmp(nextWord(),"{")!=0)
426                 AbEnd ("header '%s' has no opening bracket!\n", myHead.dosname);
427
428     do {
429         token=nextWord();
430         if (strcmp(token, "}")==0) break;
431         switch (a = findToken (hdrFields, token)) {
432                 case -1:
433                     AbEnd ("unknown field '%s' in header '%s'\n", token, myHead.dosname);
434                     break;
435                 case 0: /* author */
436                     myHead.author = nextPhrase(); break;
437                 case 1: /* info */
438                     myHead.info = nextPhrase(); break;
439                 case 2: /* date */
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());
445                     break;
446                 case 3: /* dostype */
447                     switch (b = findToken (hdrDOSTp, nextWord())) {
448                         case -1:
449                             AbEnd ("unknown dostype in header '%s'\n", myHead.dosname);
450                             break;
451                         default:
452                             myHead.dostype = b/2 + 128 + 1;
453                             break;
454                     }
455                     break;
456                 case 4: /* mode */
457                     switch (b = findToken (hdrModes, nextWord())) {
458                         case -1:
459                             AbEnd ("unknown mode in header '%s'\n", myHead.dosname);
460                         case 0:
461                             myHead.mode = 0x40; break;
462                         case 1:
463                             myHead.mode = 0x00; break;
464                         case 2:
465                             myHead.mode = 0xc0; break;
466                         case 3:
467                             myHead.mode = 0x80; break;
468                     }
469                     break;
470                 case 5: /* structure */
471                     switch (b = findToken(hdrStructTp, nextWord())) {
472                         case -1:
473                             AbEnd ("unknown structure type in header '%s'\n", myHead.dosname);
474                         case 0:
475                         case 1:
476                             myHead.structure = 0; break;
477                         case 2:
478                         case 3:
479                             myHead.structure = 1; break;
480                     }
481                     break;
482         }
483
484     } while (strcmp(token, "}")!=0);
485
486     /* OK, all information is gathered, do flushout */
487
488     fprintf(outputSFile,
489          "\t\t\t.segment \"HEADER\"\n\n\t\t.byte %i\n\t\t.word 0\n\t\t", myHead.dostype);
490
491     fillOut(myHead.dosname,16,"$a0");
492
493     fprintf(outputSFile,
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);
499
500     for (a=0;a!=63;a=a+3) {
501         fprintf(outputSFile,
502              ".byte %%%s, %%%s, %%%s\n\t\t",
503              bintos(icon1[a], i1), bintos(icon1[a+1], i2), bintos(icon1[a+2], i3)); };
504
505     fprintf(outputSFile,
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);
508
509     fillOut(myHead.classname,12,"$20");
510
511     fillOut(myHead.version,4,"0");
512
513     fprintf(outputSFile,
514             ".byte 0, 0, 0\n\t\t.byte %i\n\n\t\t", myHead.mode);
515
516     setLen(myHead.author,62);
517     fprintf(outputSFile,
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));
520
521     setLen(myHead.info, 95);
522     fprintf(outputSFile,
523             ".byte \"%s\"\n\t\t.byte 0\n\t\t.res (96-%i)\n\n",
524             myHead.info, (int) (strlen(myHead.info)+1));
525
526     if (fclose (outputSFile)!=0)
527                 AbEnd("error closing %s: %s\n",outputSName,strerror (errno));
528
529 }
530
531 void DoVLIR (void) {
532
533 char *token;
534 char *headname;
535 int i,numchains,vlirbase;
536 struct vlirentry {
537     char *chainname;
538     int exist;
539 };
540
541 struct vlirentry vlirtable[127];
542
543     openVFile();
544
545     headname = nextWord();
546
547     vlirbase = strtol(nextWord(),NULL,0);
548
549     if (strcmp(nextWord(),"{")!=0)
550                 AbEnd ("VLIR description has no opening bracket!\n");
551
552     numchains=0;
553
554     do {
555         token=nextWord();
556         if (strcmp(token, "}")==0) break;
557         numchains++;
558         if (numchains>127) {
559             AbEnd("Too many VLIR chains!\n");
560         }
561         vlirtable[numchains].chainname=token;
562
563         /* for first chain - name header */
564         if (numchains==1) {
565           fprintf(outputVFile,"MEMORY {\n\tHEADER: start = $204, size = 508, file = \"%s\";\n"
566             "\tVLIR0: start = $0400, size = $5C00, file = \"%s\";\n",headname,token);
567         } else {
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,"#");
574           }
575           fprintf(outputVFile,"\tVLIR%i: start = $%x, size = $%x, file = \"%s\";\n",
576             numchains-1,vlirbase,0x5c00-vlirbase,token);
577         }
578
579     } while (strcmp(token, "}")!=0);
580     fprintf(outputVFile,"}\n\n");
581
582     if (numchains==0) {
583         AbEnd("There must be at least one VLIR chain.\n");
584     };
585
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");
592
593     for (i=2;i<=numchains;i++) {
594         if (vlirtable[i].exist==0) {
595             fprintf(outputVFile,"#");
596         }
597         fprintf(outputVFile,"\tVLIR%i: load = VLIR%i, type = rw, define = yes;\n",i-1,i-1);
598     }
599     fprintf(outputVFile,"}\n");
600
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);
606     }
607     fprintf(outputVFile,"\n");
608
609     if (fclose (outputVFile)!=0)
610                 AbEnd("error closing %s: %s\n",outputVName,strerror (errno));
611 }
612
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;
616
617     while (1) {
618         a = getc(F);
619         if ((a=='\n')||(a=='\015')) a = ' ';
620         if (a==',') a = ' ';
621         if (a=='\042') quote=!quote;
622         if (quote) {
623             if ((a=='{')||(a=='(')) bracket++;
624             if ((a=='}')||(a==')')) bracket--;
625         }
626         if (a==EOF) { tbl[i]='\0'; realloc(tbl, i+1); break; };
627         if (IsSpace(a)) {
628             if ((prevchar!=' ') && (prevchar!=-1)) { tbl[i++]=' '; prevchar=' '; }
629         } else {
630             if (a==';' && quote) { do { a = getc (F); } while (a!='\n'); fseek(F,-1,SEEK_CUR); }
631                 else {
632                     tbl[i++]=a; prevchar=a; }
633         }
634     }
635
636     if (bracket!=0) AbEnd("there are unclosed brackets!\n");
637
638     return tbl;
639 }
640
641 void processFile (const char *filename) {
642
643 FILE *F;
644
645 char *str;
646 char *token;
647
648 int head=0;     /* number of processed HEADER sections */
649 int vlir=0;     /* number of processed VLIR sections */
650
651     if ((F = fopen (filename,"r"))==0)
652                 AbEnd("can't open file %s for reading: %s\n",filename,strerror (errno));
653
654     str=filterInput(F, malloc(BLOODY_BIG_BUFFER));
655
656     token = strtok (str," ");
657
658     do {
659         if (str!=NULL) {
660             switch (findToken (mainToken, token)) {
661
662             case 0: DoMenu(); break;
663             case 1:
664                 if (++head!=1) {
665                         AbEnd ("more than one HEADER section, aborting.\n");
666                     } else {
667                         DoHeader();
668                     }
669                 break;
670             case 2: break;      /* icon not implemented yet */
671             case 3: break;      /* dialog not implemented yet */
672             case 4:
673                 if (++vlir!=1) {
674                         AbEnd ("more than one VLIR section, aborting.\n");
675                 } else {
676                         DoVLIR();
677                 }
678                 break;
679             default: AbEnd ("unknown section %s.\n",token); break;
680             }
681         }
682         token = nextWord();
683     } while (token!=NULL);
684 }
685
686 int main(int argc, char *argv[]) {
687
688 int ffile=0, i=1;
689
690     ProgName = argv[0];
691     while (i < argc) {
692         const char *arg = argv[i];
693         if (arg[0] == '-') {
694             switch (arg[1]) {
695                 case 'f':
696                     forceFlag=1;
697                     break;
698                 case 'o':
699                     outputCName=argv[++i];
700                     break;
701                 case 's':
702                     outputSName=argv[++i];
703                     break;
704                 case 'l':
705                     outputVName=argv[++i];
706                     break;
707                 case 'h':
708                 case '?':
709                     printUsage();
710                     exit (EXIT_SUCCESS);
711                     break;
712                 case 'v':
713                     if (strcmp(arg,"-vlir")==0) {
714                         VLIRLinker(argc,argv);
715                         exit (EXIT_SUCCESS);
716                         break;
717                     } else {
718                         AbEnd("unknown option %s\n",arg);
719                         break;
720                     }
721                 default: AbEnd("unknown option %s\n",arg);
722             }
723         } else {
724             ffile++;
725
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");
732
733             processFile(arg);
734
735             }
736         i++;
737         }
738     if (ffile==0) AbEnd("no input file\n");
739
740     return EXIT_SUCCESS;
741 }