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 typedef Vector<const char *> ArgsVector;
77 ArgsVector frontendArgs;
78 ArgsVector backendArgs;
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"
91 " -n Do not perform minimization\n"
92 " -m Minimize at the end of the compilation\n"
93 " -l Minimize after most operations (default)\n"
94 " -e Minimize after every operation\n"
96 " -x Run the frontend only: emit XML intermediate format\n"
97 " -V Generate a dot file for Graphviz\n"
98 " -p Display printable characters on labels\n"
99 " -S <spec> FSM specification to output (for rlgen-dot)\n"
100 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
102 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
103 " -D The host language is D\n"
104 " -J The host language is Java\n"
105 " -R The host language is Ruby\n"
106 "line direcives: (C/D only)\n"
107 " -L Inhibit writing of #line directives\n"
108 "code style: (C/Ruby only)\n"
109 " -T0 Table driven FSM (default)\n"
110 " -T1 Faster table driven FSM\n"
111 " -F0 Flat table driven FSM\n"
112 " -F1 Faster flat table-driven FSM\n"
113 "code style: (C only)\n"
114 " -G0 Goto-driven FSM\n"
115 " -G1 Faster goto-driven FSM\n"
116 " -G2 Really fast goto-driven FSM\n"
117 " -P<N> N-Way Split really fast goto-driven FSM\n"
121 /* Print version information. */
124 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
125 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
128 /* Total error count. */
129 int gblErrorCount = 0;
131 /* Print the opening to a warning in the input, then return the error ostream. */
132 ostream &warning( const InputLoc &loc )
134 assert( loc.fileName != 0 );
135 cerr << loc.fileName << ":" << loc.line << ":" <<
136 loc.col << ": warning: ";
140 /* Print the opening to a program error, then return the error stream. */
144 cerr << PROGNAME ": ";
148 ostream &error( const InputLoc &loc )
151 assert( loc.fileName != 0 );
152 cerr << loc.fileName << ":" << loc.line << ": ";
156 void escapeLineDirectivePath( std::ostream &out, char *path )
158 for ( char *pc = path; *pc != 0; pc++ ) {
166 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
168 ParamCheck pc("xo:dnmleabjkS:M:CDJRvHh?-:sT:F:G:P:LpV", argc, argv);
170 while ( pc.check() ) {
171 switch ( pc.state ) {
172 case ParamCheck::match:
173 switch ( pc.parameter ) {
184 if ( *pc.parameterArg == 0 )
185 error() << "a zero length output file name was given" << endl;
186 else if ( outputFileName != 0 )
187 error() << "more than one output file name was given" << endl;
189 /* Ok, remember the output file name. */
190 outputFileName = pc.parameterArg;
194 /* Minimization, mostly hidden options. */
196 wantDupsRemoved = false;
197 frontendArgs.append( "-d" );
200 /* Minimization, mostly hidden options. */
202 minimizeOpt = MinimizeNone;
203 frontendArgs.append( "-n" );
206 minimizeOpt = MinimizeEnd;
207 frontendArgs.append( "-m" );
210 minimizeOpt = MinimizeMostOps;
211 frontendArgs.append( "-l" );
214 minimizeOpt = MinimizeEveryOp;
215 frontendArgs.append( "-e" );
218 minimizeLevel = MinimizeApprox;
219 frontendArgs.append( "-a" );
222 minimizeLevel = MinimizeStable;
223 frontendArgs.append( "-b" );
226 minimizeLevel = MinimizePartition1;
227 frontendArgs.append( "-j" );
230 minimizeLevel = MinimizePartition2;
231 frontendArgs.append( "-k" );
236 if ( *pc.parameterArg == 0 )
237 error() << "please specify an argument to -S" << endl;
238 else if ( machineSpec != 0 )
239 error() << "more than one -S argument was given" << endl;
241 /* Ok, remember the path to the machine to generate. */
242 machineSpec = pc.parameterArg;
243 frontendArgs.append( "-S" );
244 frontendArgs.append( pc.parameterArg );
250 if ( *pc.parameterArg == 0 )
251 error() << "please specify an argument to -M" << endl;
252 else if ( machineName != 0 )
253 error() << "more than one -M argument was given" << endl;
255 /* Ok, remember the machine name to generate. */
256 machineName = pc.parameterArg;
257 frontendArgs.append( "-M" );
258 frontendArgs.append( pc.parameterArg );
262 /* Host language types. */
264 hostLang = &hostLangC;
265 frontendArgs.append( "-C" );
268 hostLang = &hostLangD;
269 frontendArgs.append( "-D" );
272 hostLang = &hostLangJava;
273 frontendArgs.append( "-J" );
276 hostLang = &hostLangRuby;
277 frontendArgs.append( "-R" );
280 /* Version and help. */
284 case 'H': case 'h': case '?':
288 printStatistics = true;
289 frontendArgs.append( "-s" );
292 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
296 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
301 error() << "--" << pc.parameterArg <<
302 " is an invalid argument" << endl;
305 /* Passthrough args. */
307 backendArgs.append( "-T" );
308 backendArgs.append( pc.parameterArg );
311 backendArgs.append( "-F" );
312 backendArgs.append( pc.parameterArg );
315 backendArgs.append( "-G" );
316 backendArgs.append( pc.parameterArg );
319 backendArgs.append( "-P" );
320 backendArgs.append( pc.parameterArg );
323 backendArgs.append( "-p" );
326 backendArgs.append( "-L" );
331 case ParamCheck::invalid:
332 error() << "-" << pc.parameter << " is an invalid argument" << endl;
335 case ParamCheck::noparam:
336 /* It is interpreted as an input file. */
337 if ( *pc.curArg == 0 )
338 error() << "a zero length input file name was given" << endl;
339 else if ( inputFileName != 0 )
340 error() << "more than one input file name was given" << endl;
342 /* OK, Remember the filename. */
343 inputFileName = pc.curArg;
350 int frontend( char *inputFileName, char *outputFileName )
352 /* Open the input file for reading. */
353 assert( inputFileName != 0 );
354 ifstream *inFile = new ifstream( inputFileName );
355 istream *inStream = inFile;
356 if ( ! inFile->is_open() )
357 error() << "could not open " << inputFileName << " for reading" << endp;
359 /* Used for just a few things. */
360 std::ostringstream hostData;
362 if ( machineSpec == 0 && machineName == 0 )
363 hostData << "<host line=\"1\" col=\"1\">";
365 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
368 /* Finished, final check for errors.. */
369 if ( gblErrorCount > 0 )
372 /* Now send EOF to all parsers. */
373 terminateAllParsers();
375 /* Finished, final check for errors.. */
376 if ( gblErrorCount > 0 )
379 if ( machineSpec == 0 && machineName == 0 )
380 hostData << "</host>\n";
382 if ( gblErrorCount > 0 )
385 ostream *outputFile = 0;
386 if ( outputFileName != 0 )
387 outputFile = new ofstream( outputFileName );
391 /* Write the machines, then the surrounding code. */
392 writeMachines( *outputFile, hostData.str(), inputFileName );
394 /* Close the intermediate file. */
395 if ( outputFileName != 0 )
398 return gblErrorCount > 0;
401 char *makeIntermedTemplate( char *baseFileName )
404 const char *templ = "ragel-XXXXXX.xml";
405 char *lastSlash = strrchr( baseFileName, '/' );
406 if ( lastSlash == 0 ) {
407 result = new char[strlen(templ)+1];
408 strcpy( result, templ );
411 int baseLen = lastSlash - baseFileName + 1;
412 result = new char[baseLen + strlen(templ) + 1];
413 memcpy( result, baseFileName, baseLen );
414 strcpy( result+baseLen, templ );
419 char *openIntermed( char *inputFileName, char *outputFileName )
424 /* Which filename do we use as the base? */
425 char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
427 /* The template for the intermediate file name. */
428 char *intermedFileName = makeIntermedTemplate( baseFileName );
430 /* Randomize the name and try to open. */
431 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
432 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
433 for ( int tries = 0; tries < 20; tries++ ) {
434 /* Choose a random name. */
435 for ( int x = 0; x < 6; x++ )
436 firstX[x] = fnChars[rand() % 52];
438 /* Try to open the file. */
439 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
442 /* Success. Close the file immediately and return the name for use
443 * by the child processes. */
445 result = intermedFileName;
449 if ( errno == EACCES ) {
450 error() << "failed to open temp file " << intermedFileName <<
451 ", access denied" << endp;
456 error() << "abnormal error: cannot find unique name for temp file" << endp;
462 void cleanExit( char *intermed, int status )
470 /* If any forward slash is found in argv0 then it is assumed that the path is
471 * explicit and the path to the backend executable should be derived from
472 * that. Whe check that location and also go up one then inside a directory of
473 * the same name in case we are executing from the source tree. If no forward
474 * slash is found it is assumed the file is being run from the installed
475 * location. The PREFIX supplied during configuration is used. */
476 char **makePathChecksUnix( const char *argv0, const char *progName )
478 char **result = new char*[3];
479 const char *lastSlash = strrchr( argv0, '/' );
482 if ( lastSlash != 0 ) {
483 char *path = strdup( argv0 );
484 int givenPathLen = (lastSlash - argv0) + 1;
485 path[givenPathLen] = 0;
487 int progNameLen = strlen(progName);
488 int length = givenPathLen + progNameLen + 1;
489 char *check = new char[length];
490 sprintf( check, "%s%s", path, progName );
491 result[numChecks++] = check;
493 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
494 check = new char[length];
495 sprintf( check, "%s../%s/%s", path, progName, progName );
496 result[numChecks++] = check;
499 int prefixLen = strlen(PREFIX);
500 int progNameLen = strlen(progName);
501 int length = prefixLen + 5 + progNameLen + 1;
502 char *check = new char[length];
504 sprintf( check, PREFIX "/bin/%s", progName );
505 result[numChecks++] = check;
508 result[numChecks] = 0;
513 void forkAndExec( const char *progName, char **pathChecks,
514 ArgsVector &args, char *intermed )
518 /* Error, no child created. */
519 error() << "failed to fork for " << progName << endl;
520 cleanExit( intermed, 1 );
522 else if ( pid == 0 ) {
524 while ( *pathChecks != 0 ) {
525 /* Execv does not modify argv, it just uses the const form that is
526 * compatible with the most code. Ours not included. */
527 execv( *pathChecks, (char *const*) args.data );
530 error() << "failed to exec " << progName << endl;
531 cleanExit( intermed, 1 );
534 /* Parent process, wait for the child. */
538 /* What happened with the child. */
539 if ( ! WIFEXITED( status ) ) {
540 error() << progName << " did not exit normally" << endl;
541 cleanExit( intermed, 1 );
544 if ( WEXITSTATUS(status) != 0 )
545 cleanExit( intermed, WEXITSTATUS(status) );
550 /* GetModuleFileNameEx is used to find out where the the current process's
551 * binary is. That location is searched first. If that fails then we go up one
552 * directory and look for the executable inside a directory of the same name
553 * in case we are executing from the source tree.
555 char **makePathChecksWin( const char *progName )
558 char *imageFileName = new char[len];
559 HANDLE h = GetCurrentProcess();
560 len = GetModuleFileNameEx( h, NULL, imageFileName, len );
561 imageFileName[len] = 0;
563 char **result = new char*[3];
564 const char *lastSlash = strrchr( imageFileName, '\\' );
567 assert( lastSlash != 0 );
568 char *path = strdup( imageFileName );
569 int givenPathLen = (lastSlash - imageFileName) + 1;
570 path[givenPathLen] = 0;
572 int progNameLen = strlen(progName);
573 int length = givenPathLen + progNameLen + 1;
574 char *check = new char[length];
575 sprintf( check, "%s%s", path, progName );
576 result[numChecks++] = check;
578 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
579 check = new char[length];
580 sprintf( check, "%s..\\%s\\%s", path, progName, progName );
581 result[numChecks++] = check;
583 result[numChecks] = 0;
587 void spawn( const char *progName, char **pathChecks,
588 ArgsVector &args, char *intermed )
591 while ( *pathChecks != 0 ) {
592 //cerr << "trying to execute " << *pathChecks << endl;
593 result = _spawnv( _P_WAIT, *pathChecks, args.data );
594 if ( result >= 0 || errno != ENOENT )
600 error() << "failed to spawn " << progName << endl;
601 cleanExit( intermed, 1 );
605 cleanExit( intermed, 1 );
610 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
612 /* The frontend program name. */
613 const char *progName = "ragel";
615 frontendArgs.insert( 0, progName );
616 frontendArgs.insert( 1, "-x" );
617 frontendArgs.append( "-o" );
618 frontendArgs.append( intermed );
619 frontendArgs.append( inputFileName );
620 frontendArgs.append( 0 );
623 char **pathChecks = makePathChecksUnix( argv0, progName );
624 forkAndExec( progName, pathChecks, frontendArgs, intermed );
626 char **pathChecks = makePathChecksWin( progName );
627 spawn( progName, pathChecks, frontendArgs, intermed );
631 void execBackend( const char *argv0, char *intermed, char *outputFileName )
633 /* Locate the backend program */
634 const char *progName = 0;
636 progName = "rlgen-dot";
638 switch ( hostLang->lang ) {
641 progName = "rlgen-cd";
644 progName = "rlgen-java";
647 progName = "rlgen-ruby";
652 backendArgs.insert( 0, progName );
653 if ( outputFileName != 0 ) {
654 backendArgs.append( "-o" );
655 backendArgs.append( outputFileName );
657 backendArgs.append( intermed );
658 backendArgs.append( 0 );
661 char **pathChecks = makePathChecksUnix( argv0, progName );
662 forkAndExec( progName, pathChecks, backendArgs, intermed );
664 char **pathChecks = makePathChecksWin( progName );
665 spawn( progName, pathChecks, backendArgs, intermed );
669 /* Main, process args and call yyparse to start scanning input. */
670 int main(int argc, char **argv)
672 char *inputFileName = 0;
673 char *outputFileName = 0;
675 processArgs( argc, argv, inputFileName, outputFileName );
677 /* If -M or -S are given and we're not generating a dot file then invoke
678 * the frontend. These options are not useful with code generators. */
679 if ( machineName != 0 || machineSpec != 0 ) {
684 /* Require an input file. If we use standard in then we won't have a file
685 * name on which to base the output. */
686 if ( inputFileName == 0 )
687 error() << "no input file given" << endl;
689 /* Bail on argument processing errors. */
690 if ( gblErrorCount > 0 )
693 /* Make sure we are not writing to the same file as the input file. */
694 if ( inputFileName != 0 && outputFileName != 0 &&
695 strcmp( inputFileName, outputFileName ) == 0 )
697 error() << "output file \"" << outputFileName <<
698 "\" is the same as the input file" << endp;
702 return frontend( inputFileName, outputFileName );
704 char *intermed = openIntermed( inputFileName, outputFileName );
706 /* From here on in the cleanExit function should be used to exit. */
708 /* Run the frontend, then the backend processes. */
709 execFrontend( argv[0], inputFileName, intermed );
710 execBackend( argv[0], intermed, outputFileName );
712 /* Clean up the intermediate. */
713 cleanExit( intermed, 0 );