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>
40 /* Parameters and output. */
55 using std::streamsize;
57 /* Controls minimization. */
58 MinimizeLevel minimizeLevel = MinimizePartition2;
59 MinimizeOpt minimizeOpt = MinimizeMostOps;
61 /* Graphviz dot file generation. */
62 char *machineSpec = 0, *machineName = 0;
63 bool machineSpecFound = false;
65 bool printStatistics = false;
66 bool frontendOnly = false;
68 typedef Vector<char*> ArgsVector;
69 ArgsVector frontendArgs;
70 ArgsVector backendArgs;
72 /* Print a summary of the options. */
76 "usage: ragel [options] file\n"
78 " -h, -H, -?, --help Print this usage and exit\n"
79 " -v, --version Print version information and exit\n"
80 " -o <file> Write output to <file>\n"
81 " -s Print some statistics on stderr\n"
83 " -n Do not perform minimization\n"
84 " -m Minimize at the end of the compilation\n"
85 " -l Minimize after most operations (default)\n"
86 " -e Minimize after every operation\n"
87 "machine selection:\n"
88 " -S <spec> FSM specification to output (for rlgen-dot)\n"
89 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
91 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
92 " -D The host language is D\n"
93 " -J The host language is Java\n"
94 " -R The host language is Ruby\n"
98 /* Print version information. */
101 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
102 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
105 /* Total error count. */
106 int gblErrorCount = 0;
108 /* Print the opening to a warning in the input, then return the error ostream. */
109 ostream &warning( const InputLoc &loc )
111 assert( loc.fileName != 0 );
112 cerr << loc.fileName << ":" << loc.line << ":" <<
113 loc.col << ": warning: ";
117 /* Print the opening to a program error, then return the error stream. */
121 cerr << PROGNAME ": ";
125 ostream &error( const InputLoc &loc )
128 assert( loc.fileName != 0 );
129 cerr << loc.fileName << ":" << loc.line << ": ";
133 void escapeLineDirectivePath( std::ostream &out, char *path )
135 for ( char *pc = path; *pc != 0; pc++ ) {
143 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
145 ParamCheck pc("fo:nmleabjkS:M:CDJRvHh?-:sT:F:G:P:Lp", argc, argv);
147 while ( pc.check() ) {
148 switch ( pc.state ) {
149 case ParamCheck::match:
150 switch ( pc.parameter ) {
157 if ( *pc.parameterArg == 0 )
158 error() << "a zero length output file name was given" << endl;
159 else if ( outputFileName != 0 )
160 error() << "more than one output file name was given" << endl;
162 /* Ok, remember the output file name. */
163 outputFileName = pc.parameterArg;
167 /* Minimization, mostly hidden options. */
169 minimizeOpt = MinimizeNone;
170 frontendArgs.append( "-n" );
173 minimizeOpt = MinimizeEnd;
174 frontendArgs.append( "-m" );
177 minimizeOpt = MinimizeMostOps;
178 frontendArgs.append( "-l" );
181 minimizeOpt = MinimizeEveryOp;
182 frontendArgs.append( "-e" );
185 minimizeLevel = MinimizeApprox;
186 frontendArgs.append( "-a" );
189 minimizeLevel = MinimizeStable;
190 frontendArgs.append( "-b" );
193 minimizeLevel = MinimizePartition1;
194 frontendArgs.append( "-j" );
197 minimizeLevel = MinimizePartition2;
198 frontendArgs.append( "-k" );
203 if ( *pc.parameterArg == 0 )
204 error() << "please specify an argument to -S" << endl;
205 else if ( machineSpec != 0 )
206 error() << "more than one -S argument was given" << endl;
208 /* Ok, remember the path to the machine to generate. */
209 machineSpec = pc.parameterArg;
210 frontendArgs.append( "-S" );
211 frontendArgs.append( pc.parameterArg );
217 if ( *pc.parameterArg == 0 )
218 error() << "please specify an argument to -M" << endl;
219 else if ( machineName != 0 )
220 error() << "more than one -M argument was given" << endl;
222 /* Ok, remember the machine name to generate. */
223 machineName = pc.parameterArg;
224 frontendArgs.append( "-M" );
225 frontendArgs.append( pc.parameterArg );
229 /* Host language types. */
231 hostLang = &hostLangC;
232 frontendArgs.append( "-C" );
235 hostLang = &hostLangD;
236 frontendArgs.append( "-D" );
239 hostLang = &hostLangJava;
240 frontendArgs.append( "-J" );
243 hostLang = &hostLangRuby;
244 frontendArgs.append( "-R" );
247 /* Version and help. */
251 case 'H': case 'h': case '?':
255 printStatistics = true;
256 frontendArgs.append( "-s" );
259 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
263 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
268 error() << "--" << pc.parameterArg <<
269 " is an invalid argument" << endl;
272 /* Passthrough args. */
274 backendArgs.append( "-T" );
275 backendArgs.append( pc.parameterArg );
278 backendArgs.append( "-F" );
279 backendArgs.append( pc.parameterArg );
282 backendArgs.append( "-G" );
283 backendArgs.append( pc.parameterArg );
286 backendArgs.append( "-P" );
287 backendArgs.append( pc.parameterArg );
290 backendArgs.append( "-p" );
293 backendArgs.append( "-L" );
298 case ParamCheck::invalid:
299 error() << "-" << pc.parameter << " is an invalid argument" << endl;
302 case ParamCheck::noparam:
303 /* It is interpreted as an input file. */
304 if ( *pc.curArg == 0 )
305 error() << "a zero length input file name was given" << endl;
306 else if ( inputFileName != 0 )
307 error() << "more than one input file name was given" << endl;
309 /* OK, Remember the filename. */
310 inputFileName = pc.curArg;
317 int frontend( char *inputFileName, char *outputFileName )
319 /* Open the input file for reading. */
321 if ( inputFileName != 0 ) {
322 /* Open the input file for reading. */
323 ifstream *inFile = new ifstream( inputFileName );
325 if ( ! inFile->is_open() )
326 error() << "could not open " << inputFileName << " for reading" << endp;
329 inputFileName = "<stdin>";
333 /* Used for just a few things. */
334 std::ostringstream hostData;
336 if ( machineSpec == 0 && machineName == 0 )
337 hostData << "<host line=\"1\" col=\"1\">";
339 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
342 /* Finished, final check for errors.. */
343 if ( gblErrorCount > 0 )
346 /* Now send EOF to all parsers. */
347 terminateAllParsers();
349 /* Finished, final check for errors.. */
350 if ( gblErrorCount > 0 )
353 if ( machineSpec == 0 && machineName == 0 )
354 hostData << "</host>\n";
356 if ( gblErrorCount > 0 )
359 ostream *outputFile = 0;
360 if ( outputFileName != 0 )
361 outputFile = new ofstream( outputFileName );
365 /* Write the machines, then the surrounding code. */
366 writeMachines( *outputFile, hostData.str(), inputFileName );
368 /* Close the intermediate file. */
369 if ( outputFileName != 0 )
372 return gblErrorCount > 0;
375 char *makeIntermedTemplate( char *baseFileName )
377 char *result = 0, *templ = "ragel-XXXXXX.xml";
378 char *lastSlash = strrchr( baseFileName, '/' );
379 if ( lastSlash == 0 ) {
380 result = new char[strlen(templ)+1];
381 strcpy( result, templ );
384 int baseLen = lastSlash - baseFileName + 1;
385 result = new char[baseLen + strlen(templ) + 1];
386 memcpy( result, baseFileName, baseLen );
387 strcpy( result+baseLen, templ );
392 char *openIntermed( char *inputFileName, char *outputFileName )
397 /* Which filename do we use as the base? */
398 char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
400 /* The template for the intermediate file name. */
401 char *intermedFileName = makeIntermedTemplate( baseFileName );
403 /* Randomize the name and try to open. */
404 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
405 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
406 for ( int tries = 0; tries < 20; tries++ ) {
407 /* Choose a random name. */
408 for ( int x = 0; x < 6; x++ )
409 firstX[x] = fnChars[random() % 52];
411 /* Try to open the file. */
412 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
415 /* Success. Close the file immediately and return the name for use
416 * by the child processes. */
418 result = intermedFileName;
422 if ( errno == EACCES ) {
423 error() << "failed to open temp file " << intermedFileName <<
424 ", access denied" << endp;
429 error() << "abnormal error: cannot find unique name for temp file" << endp;
434 /* If any forward slash is found in argv0 then it is assumed that the path is
435 * explicit and the path to the backend executable should be derived from
436 * that. If no forward slash is found it is assumed the file is being run from
437 * the installed location. The PREFIX supplied during configuration is used.
439 char **makePathChecks( const char *argv0, const char *progName )
441 char **result = new char*[3];
442 const char *lastSlash = strrchr( argv0, '/' );
445 if ( lastSlash != 0 ) {
446 char *path = strdup( argv0 );
447 int givenPathLen = (lastSlash - argv0) + 1;
448 path[givenPathLen] = 0;
450 int progNameLen = strlen(progName);
451 int length = givenPathLen + progNameLen + 1;
452 char *check = new char[length];
453 sprintf( check, "%s%s", path, progName );
454 result[numChecks++] = check;
456 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
457 check = new char[length];
458 sprintf( check, "%s../%s/%s", path, progName, progName );
459 result[numChecks++] = check;
462 int prefixLen = strlen(PREFIX);
463 int progNameLen = strlen(progName);
464 int length = prefixLen + 5 + progNameLen + 1;
465 char *check = new char[length];
467 sprintf( check, PREFIX "/bin/%s", progName );
468 result[numChecks++] = check;
471 result[numChecks] = 0;
475 void cleanExit( char *intermed, int status )
481 int execFrontend( const char *argv0, char *inputFileName, char *intermed )
483 /* The frontend program name. */
484 char *progName = "ragel";
486 char **pathChecks = makePathChecks( argv0, progName );
488 frontendArgs.insert( 0, progName );
489 frontendArgs.insert( 1, "-f" );
490 frontendArgs.append( "-o" );
491 frontendArgs.append( intermed );
492 frontendArgs.append( inputFileName );
493 frontendArgs.append( 0 );
497 /* Error, no child created. */
498 error() << "failed to fork frontend" << endl;
499 cleanExit( intermed, 1 );
501 else if ( pid == 0 ) {
503 while ( *pathChecks != 0 ) {
504 execv( *pathChecks, frontendArgs.data );
507 error() << "failed to exec frontend" << endl;
508 cleanExit( intermed, 1 );
511 /* Parent process, wait for the child. */
515 /* What happened with the child. */
516 if ( ! WIFEXITED( status ) ) {
517 error() << "frontend did not exit normally" << endp;
518 cleanExit( intermed, 1 );
521 if ( WEXITSTATUS(status) != 0 )
522 cleanExit( intermed, WEXITSTATUS(status) );
527 int execBackend( const char *argv0, char *intermed, char *outputFileName )
529 /* Locate the backend program */
531 switch ( hostLang->lang ) {
534 progName = "rlgen-cd";
537 progName = "rlgen-java";
540 progName = "rlgen-ruby";
544 char **pathChecks = makePathChecks( argv0, progName );
546 backendArgs.insert( 0, progName );
547 if ( outputFileName != 0 ) {
548 backendArgs.append( "-o" );
549 backendArgs.append( outputFileName );
551 backendArgs.append( intermed );
552 backendArgs.append( 0 );
556 /* Error, no child created. */
557 error() << "failed to fork backend" << endl;
558 cleanExit( intermed, 1 );
560 else if ( pid == 0 ) {
562 while ( *pathChecks != 0 ) {
563 execv( *pathChecks, backendArgs.data );
566 error() << "failed to exec backend" << endl;
567 cleanExit( intermed, 1 );
570 /* Parent process, wait for the child. */
574 /* What happened with the child. */
575 if ( ! WIFEXITED( status ) ) {
576 error() << "backend did not exit normally" << endl;
577 cleanExit( intermed, 1 );
580 if ( WEXITSTATUS(status) != 0 )
581 cleanExit( intermed, WEXITSTATUS(status) );
586 /* Main, process args and call yyparse to start scanning input. */
587 int main(int argc, char **argv)
589 char *inputFileName = 0;
590 char *outputFileName = 0;
592 processArgs( argc, argv, inputFileName, outputFileName );
594 /* Bail on above errors. */
595 if ( gblErrorCount > 0 )
598 /* Make sure we are not writing to the same file as the input file. */
599 if ( inputFileName != 0 && outputFileName != 0 &&
600 strcmp( inputFileName, outputFileName ) == 0 )
602 error() << "output file \"" << outputFileName <<
603 "\" is the same as the input file" << endp;
607 return frontend( inputFileName, outputFileName );
609 char *intermed = openIntermed( inputFileName, outputFileName );
611 /* Run the frontend, then the backend processes. */
612 execFrontend( argv[0], inputFileName, intermed );
613 execBackend( argv[0], intermed, outputFileName );
615 /* Clean up the intermediate. */
616 cleanExit( intermed, 0 );