Applied patch for compiling Ragel using MSVC from Andrei Polushin.
[external/ragel.git] / ragel / main.cpp
1 /*
2  *  Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
3  */
4
5 /*  This file is part of Ragel.
6  *
7  *  Ragel is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  Ragel is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  * 
17  *  You should have received a copy of the GNU General Public License
18  *  along with Ragel; if not, write to the Free Software
19  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20  */
21
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <iostream>
26 #include <fstream>
27 #include <unistd.h>
28 #include <sstream>
29 #include <unistd.h>
30 #include <sys/types.h>
31 #include <sys/stat.h>
32 #include <fcntl.h>
33 #include <errno.h>
34
35 #ifndef _WIN32
36 #include <sys/wait.h>
37 #else
38 #include <windows.h>
39 #include <psapi.h>
40 #include <time.h>
41 #include <io.h>
42 #include <process.h>
43
44 #if _MSC_VER
45 #define S_IRUSR _S_IREAD
46 #define S_IWUSR _S_IWRITE
47 #endif
48 #endif
49
50 /* Parsing. */
51 #include "ragel.h"
52 #include "rlscan.h"
53
54 /* Parameters and output. */
55 #include "pcheck.h"
56 #include "vector.h"
57 #include "version.h"
58 #include "common.h"
59
60 using std::istream;
61 using std::ostream;
62 using std::ifstream;
63 using std::ofstream;
64 using std::cin;
65 using std::cout;
66 using std::cerr;
67 using std::endl;
68 using std::ios;
69 using std::streamsize;
70
71 /* Controls minimization. */
72 MinimizeLevel minimizeLevel = MinimizePartition2;
73 MinimizeOpt minimizeOpt = MinimizeMostOps;
74
75 /* Graphviz dot file generation. */
76 char *machineSpec = 0, *machineName = 0;
77 bool machineSpecFound = false;
78 bool wantDupsRemoved = true;
79
80 bool printStatistics = false;
81 bool frontendOnly = false;
82 bool generateDot = false;
83
84 ArgsVector frontendArgs;
85 ArgsVector backendArgs;
86 ArgsVector includePaths;
87
88 /* Print a summary of the options. */
89 void usage()
90 {
91         cout <<
92 "usage: ragel [options] file\n"
93 "general:\n"
94 "   -h, -H, -?, --help   Print this usage and exit\n"
95 "   -v, --version        Print version information and exit\n"
96 "   -o <file>            Write output to <file>\n"
97 "   -s                   Print some statistics on stderr\n"
98 "   -d                   Do not remove duplicates from action lists\n"
99 "   -I <dir>             Add <dir> to the list of directories to search\n"
100 "                        for included an imported files\n"
101 "error reporting format:\n"
102 "   --error-format=gnu   file:line:column: message (default)\n"
103 "   --error-format=msvc  file(line,column): message\n"
104 "fsm minimization:\n"
105 "   -n                   Do not perform minimization\n"
106 "   -m                   Minimize at the end of the compilation\n"
107 "   -l                   Minimize after most operations (default)\n"
108 "   -e                   Minimize after every operation\n"
109 "visualization:\n"
110 "   -x                   Run the frontend only: emit XML intermediate format\n"
111 "   -V                   Generate a dot file for Graphviz\n"
112 "   -p                   Display printable characters on labels\n"
113 "   -S <spec>            FSM specification to output (for rlgen-dot)\n"
114 "   -M <machine>         Machine definition/instantiation to output (for rlgen-dot)\n"
115 "host language:\n"
116 "   -C                   The host language is C, C++, Obj-C or Obj-C++ (default)\n"
117 "   -D                   The host language is D\n"
118 "   -J                   The host language is Java\n"
119 "   -R                   The host language is Ruby\n"
120 "   -A                   The host language is C#\n"
121 "line direcives: (C/D/C# only)\n"
122 "   -L                   Inhibit writing of #line directives\n"
123 "code style: (C/Ruby/C# only)\n"
124 "   -T0                  Table driven FSM (default)\n"
125 "   -T1                  Faster table driven FSM\n"
126 "   -F0                  Flat table driven FSM\n"
127 "   -F1                  Faster flat table-driven FSM\n"
128 "code style: (C/C# only)\n"
129 "   -G0                  Goto-driven FSM\n"
130 "   -G1                  Faster goto-driven FSM\n"
131 "code style: (C only)\n"
132 "   -G2                  Really fast goto-driven FSM\n"
133 "   -P<N>                N-Way Split really fast goto-driven FSM\n"
134         ;       
135
136         exit(0);
137 }
138
139 /* Print version information and exit. */
140 void version()
141 {
142         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
143                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
144         exit(0);
145 }
146
147 /* Error reporting format. */
148 ErrorFormat errorFormat = ErrorFormatGNU;
149
150 InputLoc makeInputLoc( const char *fileName, int line, int col)
151 {
152         InputLoc loc = { fileName, line, col };
153         return loc;
154 }
155
156 ostream &operator<<( ostream &out, const InputLoc &loc )
157 {
158         assert( loc.fileName != 0 );
159         switch ( errorFormat ) {
160         case ErrorFormatMSVC:
161                 out << loc.fileName << "(" << loc.line;
162                 if ( loc.col )
163                         out << "," << loc.col;
164                 out << ")";
165                 break;
166
167         default:
168                 out << loc.fileName << ":" << loc.line;
169                 if ( loc.col )
170                         out << ":" << loc.col;
171                 break;
172         }
173         return out;
174 }
175
176 /* Total error count. */
177 int gblErrorCount = 0;
178
179 /* Print the opening to a warning in the input, then return the error ostream. */
180 ostream &warning( const InputLoc &loc )
181 {
182         cerr << loc << ": warning: ";
183         return cerr;
184 }
185
186 /* Print the opening to a program error, then return the error stream. */
187 ostream &error()
188 {
189         gblErrorCount += 1;
190         cerr << PROGNAME ": ";
191         return cerr;
192 }
193
194 ostream &error( const InputLoc &loc )
195 {
196         gblErrorCount += 1;
197         cerr << loc << ": ";
198         return cerr;
199 }
200
201 void escapeLineDirectivePath( std::ostream &out, char *path )
202 {
203         for ( char *pc = path; *pc != 0; pc++ ) {
204                 if ( *pc == '\\' )
205                         out << "\\\\";
206                 else
207                         out << *pc;
208         }
209 }
210
211 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
212 {
213         ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
214
215         while ( pc.check() ) {
216                 switch ( pc.state ) {
217                 case ParamCheck::match:
218                         switch ( pc.parameter ) {
219                         case 'V':
220                                 generateDot = true;
221                                 break;
222
223                         case 'x':
224                                 frontendOnly = true;
225                                 break;
226
227                         /* Output. */
228                         case 'o':
229                                 if ( *pc.paramArg == 0 )
230                                         error() << "a zero length output file name was given" << endl;
231                                 else if ( outputFileName != 0 )
232                                         error() << "more than one output file name was given" << endl;
233                                 else {
234                                         /* Ok, remember the output file name. */
235                                         outputFileName = pc.paramArg;
236                                 }
237                                 break;
238
239                         /* Minimization, mostly hidden options. */
240                         case 'd':
241                                 wantDupsRemoved = false;
242                                 frontendArgs.append( "-d" );
243                                 break;
244
245                         /* Minimization, mostly hidden options. */
246                         case 'n':
247                                 minimizeOpt = MinimizeNone;
248                                 frontendArgs.append( "-n" );
249                                 break;
250                         case 'm':
251                                 minimizeOpt = MinimizeEnd;
252                                 frontendArgs.append( "-m" );
253                                 break;
254                         case 'l':
255                                 minimizeOpt = MinimizeMostOps;
256                                 frontendArgs.append( "-l" );
257                                 break;
258                         case 'e':
259                                 minimizeOpt = MinimizeEveryOp;
260                                 frontendArgs.append( "-e" );
261                                 break;
262                         case 'a':
263                                 minimizeLevel = MinimizeApprox;
264                                 frontendArgs.append( "-a" );
265                                 break;
266                         case 'b':
267                                 minimizeLevel = MinimizeStable;
268                                 frontendArgs.append( "-b" );
269                                 break;
270                         case 'j':
271                                 minimizeLevel = MinimizePartition1;
272                                 frontendArgs.append( "-j" );
273                                 break;
274                         case 'k':
275                                 minimizeLevel = MinimizePartition2;
276                                 frontendArgs.append( "-k" );
277                                 break;
278
279                         /* Machine spec. */
280                         case 'S':
281                                 if ( *pc.paramArg == 0 )
282                                         error() << "please specify an argument to -S" << endl;
283                                 else if ( machineSpec != 0 )
284                                         error() << "more than one -S argument was given" << endl;
285                                 else {
286                                         /* Ok, remember the path to the machine to generate. */
287                                         machineSpec = pc.paramArg;
288                                         frontendArgs.append( "-S" );
289                                         frontendArgs.append( pc.paramArg );
290                                 }
291                                 break;
292
293                         /* Machine path. */
294                         case 'M':
295                                 if ( *pc.paramArg == 0 )
296                                         error() << "please specify an argument to -M" << endl;
297                                 else if ( machineName != 0 )
298                                         error() << "more than one -M argument was given" << endl;
299                                 else {
300                                         /* Ok, remember the machine name to generate. */
301                                         machineName = pc.paramArg;
302                                         frontendArgs.append( "-M" );
303                                         frontendArgs.append( pc.paramArg );
304                                 }
305                                 break;
306
307                         case 'I':
308                                 if ( *pc.paramArg == 0 )
309                                         error() << "please specify an argument to -I" << endl;
310                                 else {
311                                         includePaths.append( pc.paramArg );
312                                         frontendArgs.append( "-I" );
313                                         frontendArgs.append( pc.paramArg );
314                                 }
315                                 break;
316
317                         /* Error reporting format. */
318                         case 'E':
319                                 if ( pc.paramArg[0] == '0' )
320                                         errorFormat = ErrorFormatGNU;
321                                 else if ( pc.paramArg[0] == '1' )
322                                         errorFormat = ErrorFormatMSVC;
323                                 else {
324                                         error() << "-E" << pc.paramArg[0] << 
325                                                         " is an invalid argument" << endl;
326                                 }
327                                 frontendArgs.append( "-E" );
328                                 frontendArgs.append( pc.paramArg );
329                                 break;
330
331                         /* Host language types. */
332                         case 'C':
333                                 hostLang = &hostLangC;
334                                 frontendArgs.append( "-C" );
335                                 break;
336                         case 'D':
337                                 hostLang = &hostLangD;
338                                 frontendArgs.append( "-D" );
339                                 break;
340                         case 'J':
341                                 hostLang = &hostLangJava;
342                                 frontendArgs.append( "-J" );
343                                 break;
344                         case 'R':
345                                 hostLang = &hostLangRuby;
346                                 frontendArgs.append( "-R" );
347                                 break;
348                         case 'A':
349                                 hostLang = &hostLangCSharp;
350                                 frontendArgs.append( "-A" );
351                                 break;
352
353                         /* Version and help. */
354                         case 'v':
355                                 version();
356                                 break;
357                         case 'H': case 'h': case '?':
358                                 usage();
359                                 break;
360                         case 's':
361                                 printStatistics = true;
362                                 frontendArgs.append( "-s" );
363                                 break;
364                         case '-': {
365                                 char *eq = strchr( pc.paramArg, '=' );
366
367                                 if ( eq != 0 )
368                                         *eq++ = 0;
369
370                                 if ( strcmp( pc.paramArg, "help" ) == 0 )
371                                         usage();
372                                 else if ( strcmp( pc.paramArg, "version" ) == 0 )
373                                         version();
374                                 else if ( strcmp( pc.paramArg, "error-format" ) == 0 ) {
375                                         if ( eq == 0 )
376                                                 error() << "expecting '=value' for error-format" << endl;
377                                         else if ( strcmp( eq, "gnu" ) == 0 ) {
378                                                 errorFormat = ErrorFormatGNU;
379                                                 frontendArgs.append( "--error-format=gnu" );
380                                         }
381                                         else if ( strcmp( eq, "msvc" ) == 0 ) {
382                                                 errorFormat = ErrorFormatMSVC;
383                                                 frontendArgs.append( "--error-format=msvc" );
384                                         }
385                                         else {
386                                                 error() << "invalid value for error-format" << endl;
387                                         }
388                                 }
389                                 else {
390                                         error() << "--" << pc.paramArg << 
391                                                         " is an invalid argument" << endl;
392                                 }
393                         }
394
395                         /* Passthrough args. */
396                         case 'T': 
397                                 backendArgs.append( "-T" );
398                                 backendArgs.append( pc.paramArg );
399                                 break;
400                         case 'F': 
401                                 backendArgs.append( "-F" );
402                                 backendArgs.append( pc.paramArg );
403                                 break;
404                         case 'G': 
405                                 backendArgs.append( "-G" );
406                                 backendArgs.append( pc.paramArg );
407                                 break;
408                         case 'P':
409                                 backendArgs.append( "-P" );
410                                 backendArgs.append( pc.paramArg );
411                                 break;
412                         case 'p':
413                                 backendArgs.append( "-p" );
414                                 break;
415                         case 'L':
416                                 backendArgs.append( "-L" );
417                                 break;
418                         }
419                         break;
420
421                 case ParamCheck::invalid:
422                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
423                         break;
424
425                 case ParamCheck::noparam:
426                         /* It is interpreted as an input file. */
427                         if ( *pc.curArg == 0 )
428                                 error() << "a zero length input file name was given" << endl;
429                         else if ( inputFileName != 0 )
430                                 error() << "more than one input file name was given" << endl;
431                         else {
432                                 /* OK, Remember the filename. */
433                                 inputFileName = pc.curArg;
434                         }
435                         break;
436                 }
437         }
438 }
439
440 int frontend( char *inputFileName, char *outputFileName )
441 {
442         /* Open the input file for reading. */
443         assert( inputFileName != 0 );
444         ifstream *inFile = new ifstream( inputFileName );
445         istream *inStream = inFile;
446         if ( ! inFile->is_open() )
447                 error() << "could not open " << inputFileName << " for reading" << endp;
448
449         /* Used for just a few things. */
450         std::ostringstream hostData;
451
452         if ( machineSpec == 0 && machineName == 0 )
453                 hostData << "<host line=\"1\" col=\"1\">";
454
455         Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
456         scanner.do_scan();
457
458         /* Finished, final check for errors.. */
459         if ( gblErrorCount > 0 )
460                 return 1;
461         
462         /* Now send EOF to all parsers. */
463         terminateAllParsers();
464
465         /* Finished, final check for errors.. */
466         if ( gblErrorCount > 0 )
467                 return 1;
468
469         if ( machineSpec == 0 && machineName == 0 )
470                 hostData << "</host>\n";
471
472         if ( gblErrorCount > 0 )
473                 return 1;
474         
475         ostream *outputFile = 0;
476         if ( outputFileName != 0 )
477                 outputFile = new ofstream( outputFileName );
478         else
479                 outputFile = &cout;
480
481         /* Write the machines, then the surrounding code. */
482         writeMachines( *outputFile, hostData.str(), inputFileName );
483
484         /* Close the intermediate file. */
485         if ( outputFileName != 0 )
486                 delete outputFile;
487
488         return gblErrorCount > 0;
489 }
490
491 char *makeIntermedTemplate( char *baseFileName )
492 {
493         char *result = 0;
494         const char *templ = "ragel-XXXXXX.xml";
495         char *lastSlash = strrchr( baseFileName, '/' );
496         if ( lastSlash == 0 ) {
497                 result = new char[strlen(templ)+1];
498                 strcpy( result, templ );
499         }
500         else {
501                 int baseLen = lastSlash - baseFileName + 1;
502                 result = new char[baseLen + strlen(templ) + 1];
503                 memcpy( result, baseFileName, baseLen );
504                 strcpy( result+baseLen, templ );
505         }
506         return result;
507 };
508
509 char *openIntermed( char *inputFileName, char *outputFileName )
510 {
511         srand(time(0));
512         char *result = 0;
513
514         /* Which filename do we use as the base? */
515         char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
516
517         /* The template for the intermediate file name. */
518         char *intermedFileName = makeIntermedTemplate( baseFileName );
519
520         /* Randomize the name and try to open. */
521         char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
522         char *firstX = strrchr( intermedFileName, 'X' ) - 5;
523         for ( int tries = 0; tries < 20; tries++ ) {
524                 /* Choose a random name. */
525                 for ( int x = 0; x < 6; x++ )
526                         firstX[x] = fnChars[rand() % 52];
527
528                 /* Try to open the file. */
529                 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
530
531                 if ( fd > 0 ) {
532                         /* Success. Close the file immediately and return the name for use
533                          * by the child processes. */
534                         ::close( fd );
535                         result = intermedFileName;
536                         break;
537                 }
538
539                 if ( errno == EACCES ) {
540                         error() << "failed to open temp file " << intermedFileName << 
541                                         ", access denied" << endp;
542                 }
543         }
544
545         if ( result == 0 )
546                 error() << "abnormal error: cannot find unique name for temp file" << endp;
547
548         return result;
549 }
550
551
552 void cleanExit( char *intermed, int status )
553 {
554         unlink( intermed );
555         exit( status );
556 }
557
558 #ifndef _WIN32
559
560 /* If any forward slash is found in argv0 then it is assumed that the path is
561  * explicit and the path to the backend executable should be derived from
562  * that. Whe check that location and also go up one then inside a directory of
563  * the same name in case we are executing from the source tree. If no forward
564  * slash is found it is assumed the file is being run from the installed
565  * location. The PREFIX supplied during configuration is used. */
566 char **makePathChecksUnix( const char *argv0, const char *progName )
567 {
568         char **result = new char*[3];
569         const char *lastSlash = strrchr( argv0, '/' );
570         int numChecks = 0;
571
572         if ( lastSlash != 0 ) {
573                 char *path = strdup( argv0 );
574                 int givenPathLen = (lastSlash - argv0) + 1;
575                 path[givenPathLen] = 0;
576
577                 int progNameLen = strlen(progName);
578                 int length = givenPathLen + progNameLen + 1;
579                 char *check = new char[length];
580                 sprintf( check, "%s%s", path, progName );
581                 result[numChecks++] = check;
582
583                 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
584                 check = new char[length];
585                 sprintf( check, "%s../%s/%s", path, progName, progName );
586                 result[numChecks++] = check;
587         }
588         else {
589                 int prefixLen = strlen(PREFIX);
590                 int progNameLen = strlen(progName);
591                 int length = prefixLen + 5 + progNameLen + 1;
592                 char *check = new char[length];
593
594                 sprintf( check, PREFIX "/bin/%s", progName );
595                 result[numChecks++] = check;
596         }
597
598         result[numChecks] = 0;
599         return result;
600 }
601
602
603 void forkAndExec( const char *progName, char **pathChecks, 
604                 ArgsVector &args, char *intermed )
605 {
606         pid_t pid = fork();
607         if ( pid < 0 ) {
608                 /* Error, no child created. */
609                 error() << "failed to fork for " << progName << endl;
610                 cleanExit( intermed, 1 );
611         }
612         else if ( pid == 0 ) {
613                 /* child */
614                 while ( *pathChecks != 0 ) {
615                         /* Execv does not modify argv, it just uses the const form that is
616                          * compatible with the most code. Ours not included. */
617                         execv( *pathChecks, (char *const*) args.data );
618                         pathChecks += 1;
619                 }
620                 error() << "failed to exec " << progName << endl;
621                 cleanExit( intermed, 1 );
622         }
623
624         /* Parent process, wait for the child. */
625         int status;
626         wait( &status );
627
628         /* What happened with the child. */
629         if ( ! WIFEXITED( status ) ) {
630                 error() << progName << " did not exit normally" << endl;
631                 cleanExit( intermed, 1 );
632         }
633         
634         if ( WEXITSTATUS(status) != 0 )
635                 cleanExit( intermed, WEXITSTATUS(status) );
636 }
637
638 #else
639
640 /* GetModuleFileNameEx is used to find out where the the current process's
641  * binary is. That location is searched first. If that fails then we go up one
642  * directory and look for the executable inside a directory of the same name
643  * in case we are executing from the source tree.
644  * */
645 char **makePathChecksWin( const char *progName )
646 {
647         int len = 1024;
648         char *imageFileName = new char[len];
649         HANDLE h = GetCurrentProcess();
650         len = GetModuleFileNameEx( h, NULL, imageFileName, len );
651         imageFileName[len] = 0;
652
653         char **result = new char*[3];
654         const char *lastSlash = strrchr( imageFileName, '\\' );
655         int numChecks = 0;
656
657         assert( lastSlash != 0 );
658         char *path = strdup( imageFileName );
659         int givenPathLen = (lastSlash - imageFileName) + 1;
660         path[givenPathLen] = 0;
661
662         int progNameLen = strlen(progName);
663         int length = givenPathLen + progNameLen + 1;
664         char *check = new char[length];
665         sprintf( check, "%s%s", path, progName );
666         result[numChecks++] = check;
667
668         length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
669         check = new char[length];
670         sprintf( check, "%s..\\%s\\%s", path, progName, progName );
671         result[numChecks++] = check;
672
673         result[numChecks] = 0;
674         return result;
675 }
676
677 void spawn( const char *progName, char **pathChecks, 
678                 ArgsVector &args, char *intermed )
679 {
680         int result = 0;
681         while ( *pathChecks != 0 ) {
682                 //cerr << "trying to execute " << *pathChecks << endl;
683                 result = _spawnv( _P_WAIT, *pathChecks, args.data );
684                 if ( result >= 0 || errno != ENOENT )
685                         break;
686                 pathChecks += 1;
687         }
688
689         if ( result < 0 ) {
690                 error() << "failed to spawn " << progName << endl;
691                 cleanExit( intermed, 1 );
692         }
693
694         if ( result > 0 )
695                 cleanExit( intermed, 1 );
696 }
697
698 #endif
699
700 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
701 {
702         /* The frontend program name. */
703         const char *progName = "ragel";
704
705         frontendArgs.insert( 0, progName );
706         frontendArgs.insert( 1, "-x" );
707         frontendArgs.append( "-o" );
708         frontendArgs.append( intermed );
709         frontendArgs.append( inputFileName );
710         frontendArgs.append( 0 );
711
712 #ifndef _WIN32
713         char **pathChecks = makePathChecksUnix( argv0, progName );
714         forkAndExec( progName, pathChecks, frontendArgs, intermed );
715 #else
716         char **pathChecks = makePathChecksWin( progName );
717         spawn( progName, pathChecks, frontendArgs, intermed );
718 #endif
719 }
720
721 void execBackend( const char *argv0, char *intermed, char *outputFileName )
722 {
723         /* Locate the backend program */
724         const char *progName = 0;
725         if ( generateDot )
726                 progName = "rlgen-dot";
727         else {
728                 switch ( hostLang->lang ) {
729                         case HostLang::C:
730                         case HostLang::D:
731                                 progName = "rlgen-cd";
732                                 break;
733                         case HostLang::Java:
734                                 progName = "rlgen-java";
735                                 break;
736                         case HostLang::Ruby:
737                                 progName = "rlgen-ruby";
738                                 break;
739                         case HostLang::CSharp:
740                                 progName = "rlgen-csharp";
741                 }
742         }
743
744         backendArgs.insert( 0, progName );
745         if ( outputFileName != 0 ) {
746                 backendArgs.append( "-o" );
747                 backendArgs.append( outputFileName );
748         }
749         backendArgs.append( intermed );
750         backendArgs.append( 0 );
751
752 #ifndef _WIN32
753         char **pathChecks = makePathChecksUnix( argv0, progName );
754         forkAndExec( progName, pathChecks, backendArgs, intermed );
755 #else
756         char **pathChecks = makePathChecksWin( progName );
757         spawn( progName, pathChecks, backendArgs, intermed );
758 #endif
759 }
760
761 /* Main, process args and call yyparse to start scanning input. */
762 int main(int argc, char **argv)
763 {
764         char *inputFileName = 0;
765         char *outputFileName = 0;
766
767         processArgs( argc, argv, inputFileName, outputFileName );
768
769         /* If -M or -S are given and we're not generating a dot file then invoke
770          * the frontend. These options are not useful with code generators. */
771         if ( machineName != 0 || machineSpec != 0 ) {
772                 if ( !generateDot )
773                         frontendOnly = true;
774         }
775
776         /* Require an input file. If we use standard in then we won't have a file
777          * name on which to base the output. */
778         if ( inputFileName == 0 )
779                 error() << "no input file given" << endl;
780
781         /* Bail on argument processing errors. */
782         if ( gblErrorCount > 0 )
783                 exit(1);
784
785         /* Make sure we are not writing to the same file as the input file. */
786         if ( inputFileName != 0 && outputFileName != 0 && 
787                         strcmp( inputFileName, outputFileName  ) == 0 )
788         {
789                 error() << "output file \"" << outputFileName  << 
790                                 "\" is the same as the input file" << endp;
791         }
792
793         if ( frontendOnly )
794                 return frontend( inputFileName, outputFileName );
795
796         char *intermed = openIntermed( inputFileName, outputFileName );
797
798         /* From here on in the cleanExit function should be used to exit. */
799
800         /* Run the frontend, then the backend processes. */
801         execFrontend( argv[0], inputFileName, intermed );
802         execBackend( argv[0], intermed, outputFileName );
803
804         /* Clean up the intermediate. */
805         cleanExit( intermed, 0 );
806
807         return 0;
808 }