2 * Copyright 2001-2007 Adrian Thurston <thurston@cs.queensu.ca>
5 /* This file is part of Ragel.
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.
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.
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
30 #include <sys/types.h>
46 /* Parameters and output. */
61 using std::streamsize;
63 /* Controls minimization. */
64 MinimizeLevel minimizeLevel = MinimizePartition2;
65 MinimizeOpt minimizeOpt = MinimizeMostOps;
67 /* Graphviz dot file generation. */
68 char *machineSpec = 0, *machineName = 0;
69 bool machineSpecFound = false;
70 bool wantDupsRemoved = true;
72 bool printStatistics = false;
73 bool frontendOnly = false;
74 bool generateDot = false;
76 ArgsVector frontendArgs;
77 ArgsVector backendArgs;
78 ArgsVector includePaths;
80 /* Print a summary of the options. */
84 "usage: ragel [options] file\n"
86 " -h, -H, -?, --help Print this usage and exit\n"
87 " -v, --version Print version information and exit\n"
88 " -o <file> Write output to <file>\n"
89 " -s Print some statistics on stderr\n"
90 " -d Do not remove duplicates from action lists\n"
92 " -n Do not perform minimization\n"
93 " -m Minimize at the end of the compilation\n"
94 " -l Minimize after most operations (default)\n"
95 " -e Minimize after every operation\n"
97 " -x Run the frontend only: emit XML intermediate format\n"
98 " -V Generate a dot file for Graphviz\n"
99 " -p Display printable characters on labels\n"
100 " -S <spec> FSM specification to output (for rlgen-dot)\n"
101 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
103 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
104 " -D The host language is D\n"
105 " -J The host language is Java\n"
106 " -R The host language is Ruby\n"
107 " -A The host language is C#\n"
108 "line direcives: (C/D/C# only)\n"
109 " -L Inhibit writing of #line directives\n"
110 "code style: (C/Ruby/C# only)\n"
111 " -T0 Table driven FSM (default)\n"
112 " -T1 Faster table driven FSM\n"
113 " -F0 Flat table driven FSM\n"
114 " -F1 Faster flat table-driven FSM\n"
115 "code style: (C/C# only)\n"
116 " -G0 Goto-driven FSM\n"
117 " -G1 Faster goto-driven FSM\n"
118 "code style: (C only)\n"
119 " -G2 Really fast goto-driven FSM\n"
120 " -P<N> N-Way Split really fast goto-driven FSM\n"
124 /* Print version information. */
127 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
128 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
131 /* Total error count. */
132 int gblErrorCount = 0;
134 /* Print the opening to a warning in the input, then return the error ostream. */
135 ostream &warning( const InputLoc &loc )
137 assert( loc.fileName != 0 );
138 cerr << loc.fileName << ":" << loc.line << ":" <<
139 loc.col << ": warning: ";
143 /* Print the opening to a program error, then return the error stream. */
147 cerr << PROGNAME ": ";
151 ostream &error( const InputLoc &loc )
154 assert( loc.fileName != 0 );
155 cerr << loc.fileName << ":" << loc.line << ": ";
159 void escapeLineDirectivePath( std::ostream &out, char *path )
161 for ( char *pc = path; *pc != 0; pc++ ) {
169 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
171 ParamCheck pc("xo:dnmleabjkS:M:I:CDJRAvHh?-:sT:F:G:P:LpV", argc, argv);
173 while ( pc.check() ) {
174 switch ( pc.state ) {
175 case ParamCheck::match:
176 switch ( pc.parameter ) {
187 if ( *pc.parameterArg == 0 )
188 error() << "a zero length output file name was given" << endl;
189 else if ( outputFileName != 0 )
190 error() << "more than one output file name was given" << endl;
192 /* Ok, remember the output file name. */
193 outputFileName = pc.parameterArg;
197 /* Minimization, mostly hidden options. */
199 wantDupsRemoved = false;
200 frontendArgs.append( "-d" );
203 /* Minimization, mostly hidden options. */
205 minimizeOpt = MinimizeNone;
206 frontendArgs.append( "-n" );
209 minimizeOpt = MinimizeEnd;
210 frontendArgs.append( "-m" );
213 minimizeOpt = MinimizeMostOps;
214 frontendArgs.append( "-l" );
217 minimizeOpt = MinimizeEveryOp;
218 frontendArgs.append( "-e" );
221 minimizeLevel = MinimizeApprox;
222 frontendArgs.append( "-a" );
225 minimizeLevel = MinimizeStable;
226 frontendArgs.append( "-b" );
229 minimizeLevel = MinimizePartition1;
230 frontendArgs.append( "-j" );
233 minimizeLevel = MinimizePartition2;
234 frontendArgs.append( "-k" );
239 if ( *pc.parameterArg == 0 )
240 error() << "please specify an argument to -S" << endl;
241 else if ( machineSpec != 0 )
242 error() << "more than one -S argument was given" << endl;
244 /* Ok, remember the path to the machine to generate. */
245 machineSpec = pc.parameterArg;
246 frontendArgs.append( "-S" );
247 frontendArgs.append( pc.parameterArg );
253 if ( *pc.parameterArg == 0 )
254 error() << "please specify an argument to -M" << endl;
255 else if ( machineName != 0 )
256 error() << "more than one -M argument was given" << endl;
258 /* Ok, remember the machine name to generate. */
259 machineName = pc.parameterArg;
260 frontendArgs.append( "-M" );
261 frontendArgs.append( pc.parameterArg );
266 if ( *pc.parameterArg == 0 )
267 error() << "please specify an argument to -I" << endl;
269 includePaths.append( pc.parameterArg );
270 frontendArgs.append( "-I" );
271 frontendArgs.append( pc.parameterArg );
275 /* Host language types. */
277 hostLang = &hostLangC;
278 frontendArgs.append( "-C" );
281 hostLang = &hostLangD;
282 frontendArgs.append( "-D" );
285 hostLang = &hostLangJava;
286 frontendArgs.append( "-J" );
289 hostLang = &hostLangRuby;
290 frontendArgs.append( "-R" );
293 hostLang = &hostLangCSharp;
294 frontendArgs.append( "-A" );
297 /* Version and help. */
301 case 'H': case 'h': case '?':
305 printStatistics = true;
306 frontendArgs.append( "-s" );
309 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
313 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
318 error() << "--" << pc.parameterArg <<
319 " is an invalid argument" << endl;
322 /* Passthrough args. */
324 backendArgs.append( "-T" );
325 backendArgs.append( pc.parameterArg );
328 backendArgs.append( "-F" );
329 backendArgs.append( pc.parameterArg );
332 backendArgs.append( "-G" );
333 backendArgs.append( pc.parameterArg );
336 backendArgs.append( "-P" );
337 backendArgs.append( pc.parameterArg );
340 backendArgs.append( "-p" );
343 backendArgs.append( "-L" );
348 case ParamCheck::invalid:
349 error() << "-" << pc.parameter << " is an invalid argument" << endl;
352 case ParamCheck::noparam:
353 /* It is interpreted as an input file. */
354 if ( *pc.curArg == 0 )
355 error() << "a zero length input file name was given" << endl;
356 else if ( inputFileName != 0 )
357 error() << "more than one input file name was given" << endl;
359 /* OK, Remember the filename. */
360 inputFileName = pc.curArg;
367 int frontend( char *inputFileName, char *outputFileName )
369 /* Open the input file for reading. */
370 assert( inputFileName != 0 );
371 ifstream *inFile = new ifstream( inputFileName );
372 istream *inStream = inFile;
373 if ( ! inFile->is_open() )
374 error() << "could not open " << inputFileName << " for reading" << endp;
376 /* Used for just a few things. */
377 std::ostringstream hostData;
379 if ( machineSpec == 0 && machineName == 0 )
380 hostData << "<host line=\"1\" col=\"1\">";
382 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
385 /* Finished, final check for errors.. */
386 if ( gblErrorCount > 0 )
389 /* Now send EOF to all parsers. */
390 terminateAllParsers();
392 /* Finished, final check for errors.. */
393 if ( gblErrorCount > 0 )
396 if ( machineSpec == 0 && machineName == 0 )
397 hostData << "</host>\n";
399 if ( gblErrorCount > 0 )
402 ostream *outputFile = 0;
403 if ( outputFileName != 0 )
404 outputFile = new ofstream( outputFileName );
408 /* Write the machines, then the surrounding code. */
409 writeMachines( *outputFile, hostData.str(), inputFileName );
411 /* Close the intermediate file. */
412 if ( outputFileName != 0 )
415 return gblErrorCount > 0;
418 char *makeIntermedTemplate( char *baseFileName )
421 const char *templ = "ragel-XXXXXX.xml";
422 char *lastSlash = strrchr( baseFileName, '/' );
423 if ( lastSlash == 0 ) {
424 result = new char[strlen(templ)+1];
425 strcpy( result, templ );
428 int baseLen = lastSlash - baseFileName + 1;
429 result = new char[baseLen + strlen(templ) + 1];
430 memcpy( result, baseFileName, baseLen );
431 strcpy( result+baseLen, templ );
436 char *openIntermed( char *inputFileName, char *outputFileName )
441 /* Which filename do we use as the base? */
442 char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
444 /* The template for the intermediate file name. */
445 char *intermedFileName = makeIntermedTemplate( baseFileName );
447 /* Randomize the name and try to open. */
448 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
449 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
450 for ( int tries = 0; tries < 20; tries++ ) {
451 /* Choose a random name. */
452 for ( int x = 0; x < 6; x++ )
453 firstX[x] = fnChars[rand() % 52];
455 /* Try to open the file. */
456 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
459 /* Success. Close the file immediately and return the name for use
460 * by the child processes. */
462 result = intermedFileName;
466 if ( errno == EACCES ) {
467 error() << "failed to open temp file " << intermedFileName <<
468 ", access denied" << endp;
473 error() << "abnormal error: cannot find unique name for temp file" << endp;
479 void cleanExit( char *intermed, int status )
487 /* If any forward slash is found in argv0 then it is assumed that the path is
488 * explicit and the path to the backend executable should be derived from
489 * that. Whe check that location and also go up one then inside a directory of
490 * the same name in case we are executing from the source tree. If no forward
491 * slash is found it is assumed the file is being run from the installed
492 * location. The PREFIX supplied during configuration is used. */
493 char **makePathChecksUnix( const char *argv0, const char *progName )
495 char **result = new char*[3];
496 const char *lastSlash = strrchr( argv0, '/' );
499 if ( lastSlash != 0 ) {
500 char *path = strdup( argv0 );
501 int givenPathLen = (lastSlash - argv0) + 1;
502 path[givenPathLen] = 0;
504 int progNameLen = strlen(progName);
505 int length = givenPathLen + progNameLen + 1;
506 char *check = new char[length];
507 sprintf( check, "%s%s", path, progName );
508 result[numChecks++] = check;
510 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
511 check = new char[length];
512 sprintf( check, "%s../%s/%s", path, progName, progName );
513 result[numChecks++] = check;
516 int prefixLen = strlen(PREFIX);
517 int progNameLen = strlen(progName);
518 int length = prefixLen + 5 + progNameLen + 1;
519 char *check = new char[length];
521 sprintf( check, PREFIX "/bin/%s", progName );
522 result[numChecks++] = check;
525 result[numChecks] = 0;
530 void forkAndExec( const char *progName, char **pathChecks,
531 ArgsVector &args, char *intermed )
535 /* Error, no child created. */
536 error() << "failed to fork for " << progName << endl;
537 cleanExit( intermed, 1 );
539 else if ( pid == 0 ) {
541 while ( *pathChecks != 0 ) {
542 /* Execv does not modify argv, it just uses the const form that is
543 * compatible with the most code. Ours not included. */
544 execv( *pathChecks, (char *const*) args.data );
547 error() << "failed to exec " << progName << endl;
548 cleanExit( intermed, 1 );
551 /* Parent process, wait for the child. */
555 /* What happened with the child. */
556 if ( ! WIFEXITED( status ) ) {
557 error() << progName << " did not exit normally" << endl;
558 cleanExit( intermed, 1 );
561 if ( WEXITSTATUS(status) != 0 )
562 cleanExit( intermed, WEXITSTATUS(status) );
567 /* GetModuleFileNameEx is used to find out where the the current process's
568 * binary is. That location is searched first. If that fails then we go up one
569 * directory and look for the executable inside a directory of the same name
570 * in case we are executing from the source tree.
572 char **makePathChecksWin( const char *progName )
575 char *imageFileName = new char[len];
576 HANDLE h = GetCurrentProcess();
577 len = GetModuleFileNameEx( h, NULL, imageFileName, len );
578 imageFileName[len] = 0;
580 char **result = new char*[3];
581 const char *lastSlash = strrchr( imageFileName, '\\' );
584 assert( lastSlash != 0 );
585 char *path = strdup( imageFileName );
586 int givenPathLen = (lastSlash - imageFileName) + 1;
587 path[givenPathLen] = 0;
589 int progNameLen = strlen(progName);
590 int length = givenPathLen + progNameLen + 1;
591 char *check = new char[length];
592 sprintf( check, "%s%s", path, progName );
593 result[numChecks++] = check;
595 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
596 check = new char[length];
597 sprintf( check, "%s..\\%s\\%s", path, progName, progName );
598 result[numChecks++] = check;
600 result[numChecks] = 0;
604 void spawn( const char *progName, char **pathChecks,
605 ArgsVector &args, char *intermed )
608 while ( *pathChecks != 0 ) {
609 //cerr << "trying to execute " << *pathChecks << endl;
610 result = _spawnv( _P_WAIT, *pathChecks, args.data );
611 if ( result >= 0 || errno != ENOENT )
617 error() << "failed to spawn " << progName << endl;
618 cleanExit( intermed, 1 );
622 cleanExit( intermed, 1 );
627 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
629 /* The frontend program name. */
630 const char *progName = "ragel";
632 frontendArgs.insert( 0, progName );
633 frontendArgs.insert( 1, "-x" );
634 frontendArgs.append( "-o" );
635 frontendArgs.append( intermed );
636 frontendArgs.append( inputFileName );
637 frontendArgs.append( 0 );
640 char **pathChecks = makePathChecksUnix( argv0, progName );
641 forkAndExec( progName, pathChecks, frontendArgs, intermed );
643 char **pathChecks = makePathChecksWin( progName );
644 spawn( progName, pathChecks, frontendArgs, intermed );
648 void execBackend( const char *argv0, char *intermed, char *outputFileName )
650 /* Locate the backend program */
651 const char *progName = 0;
653 progName = "rlgen-dot";
655 switch ( hostLang->lang ) {
658 progName = "rlgen-cd";
661 progName = "rlgen-java";
664 progName = "rlgen-ruby";
666 case HostLang::CSharp:
667 progName = "rlgen-csharp";
671 backendArgs.insert( 0, progName );
672 if ( outputFileName != 0 ) {
673 backendArgs.append( "-o" );
674 backendArgs.append( outputFileName );
676 backendArgs.append( intermed );
677 backendArgs.append( 0 );
680 char **pathChecks = makePathChecksUnix( argv0, progName );
681 forkAndExec( progName, pathChecks, backendArgs, intermed );
683 char **pathChecks = makePathChecksWin( progName );
684 spawn( progName, pathChecks, backendArgs, intermed );
688 /* Main, process args and call yyparse to start scanning input. */
689 int main(int argc, char **argv)
691 char *inputFileName = 0;
692 char *outputFileName = 0;
694 processArgs( argc, argv, inputFileName, outputFileName );
696 /* If -M or -S are given and we're not generating a dot file then invoke
697 * the frontend. These options are not useful with code generators. */
698 if ( machineName != 0 || machineSpec != 0 ) {
703 /* Require an input file. If we use standard in then we won't have a file
704 * name on which to base the output. */
705 if ( inputFileName == 0 )
706 error() << "no input file given" << endl;
708 /* Bail on argument processing errors. */
709 if ( gblErrorCount > 0 )
712 /* Make sure we are not writing to the same file as the input file. */
713 if ( inputFileName != 0 && outputFileName != 0 &&
714 strcmp( inputFileName, outputFileName ) == 0 )
716 error() << "output file \"" << outputFileName <<
717 "\" is the same as the input file" << endp;
721 return frontend( inputFileName, outputFileName );
723 char *intermed = openIntermed( inputFileName, outputFileName );
725 /* From here on in the cleanExit function should be used to exit. */
727 /* Run the frontend, then the backend processes. */
728 execFrontend( argv[0], inputFileName, intermed );
729 execBackend( argv[0], intermed, outputFileName );
731 /* Clean up the intermediate. */
732 cleanExit( intermed, 0 );