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;
67 typedef Vector<char*> ArgsVector;
68 ArgsVector backendArgs;
70 /* Print a summary of the options. */
74 "usage: ragel [options] file\n"
76 " -h, -H, -?, --help Print this usage and exit\n"
77 " -v, --version Print version information and exit\n"
78 " -o <file> Write output to <file>\n"
79 " -s Print some statistics on stderr\n"
81 " -n Do not perform minimization\n"
82 " -m Minimize at the end of the compilation\n"
83 " -l Minimize after most operations (default)\n"
84 " -e Minimize after every operation\n"
85 "machine selection:\n"
86 " -S <spec> FSM specification to output (for rlgen-dot)\n"
87 " -M <machine> Machine definition/instantiation to output (for rlgen-dot)\n"
89 " -C The host language is C, C++, Obj-C or Obj-C++ (default)\n"
90 " -D The host language is D\n"
91 " -J The host language is Java\n"
92 " -R The host language is Ruby\n"
96 /* Print version information. */
99 cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
100 "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
103 /* Total error count. */
104 int gblErrorCount = 0;
106 /* Print the opening to a warning in the input, then return the error ostream. */
107 ostream &warning( const InputLoc &loc )
109 assert( loc.fileName != 0 );
110 cerr << loc.fileName << ":" << loc.line << ":" <<
111 loc.col << ": warning: ";
115 /* Print the opening to a program error, then return the error stream. */
119 cerr << PROGNAME ": ";
123 ostream &error( const InputLoc &loc )
126 assert( loc.fileName != 0 );
127 cerr << loc.fileName << ":" << loc.line << ": ";
131 void escapeLineDirectivePath( std::ostream &out, char *path )
133 for ( char *pc = path; *pc != 0; pc++ ) {
141 /* If any forward slash is found in argv0 then it is assumed that the path is
142 * explicit and the path to the backend executable should be derived from
143 * that. If no forward slash is found it is assumed the file is being run from
144 * the installed location. The PREFIX supplied during configuration is used.
146 char **makePathChecks( const char *argv0, const char *progName )
148 char **result = new char*[3];
149 const char *lastSlash = strrchr( argv0, '/' );
152 if ( lastSlash != 0 ) {
153 char *path = strdup( argv0 );
154 int givenPathLen = (lastSlash - argv0) + 1;
155 path[givenPathLen] = 0;
157 int progNameLen = strlen(progName);
158 int length = givenPathLen + progNameLen + 1;
159 char *check = new char[length];
160 sprintf( check, "%s%s", path, progName );
161 result[numChecks++] = check;
163 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
164 check = new char[length];
165 sprintf( check, "%s../%s/%s", path, progName, progName );
166 result[numChecks++] = check;
169 int prefixLen = strlen(PREFIX);
170 int progNameLen = strlen(progName);
171 int length = prefixLen + 5 + progNameLen + 1;
172 char *check = new char[length];
174 sprintf( check, PREFIX "/bin/%s", progName );
175 result[numChecks++] = check;
178 result[numChecks] = 0;
183 int execBackend( const char *argv0, costream *intermed )
185 /* Locate the backend program */
186 const char *progName = 0;
187 switch ( hostLang->lang ) {
190 progName = "rlgen-cd";
193 progName = "rlgen-java";
196 progName = "rlgen-ruby";
200 char **pathChecks = makePathChecks( argv0, progName );
202 backendArgs.insert( 0, "rlgen-ruby" );
203 backendArgs.append( intermed->b->fileName );
204 backendArgs.append( 0 );
208 /* Error, no child created. */
209 error() << "failed to fork backend" << endp;
211 else if ( pid == 0 ) {
213 while ( *pathChecks != 0 ) {
214 execv( *pathChecks, backendArgs.data );
217 error() << "failed to exec backend" << endp;
220 /* Parent process, wait for the child. */
224 /* Clean up the intermediate. */
225 unlink( intermed->b->fileName );
227 /* What happened with the child. */
228 if ( ! WIFEXITED( status ) )
229 error() << "backend did not exit normally" << endp;
231 if ( WEXITSTATUS(status) != 0 )
232 exit( WEXITSTATUS(status) );
237 char *makeIntermedTemplate( char *baseFileName )
240 char *lastSlash = strrchr( baseFileName, '/' );
241 if ( lastSlash == 0 ) {
242 result = new char[13];
243 strcpy( result, "ragel-XXXXXX.xml" );
246 int baseLen = lastSlash - baseFileName + 1;
247 result = new char[baseLen + 13];
248 memcpy( result, baseFileName, baseLen );
249 strcpy( result+baseLen, "ragel-XXXXXX.xml" );
254 char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
256 costream *openIntermed( char *inputFileName, char *outputFileName )
259 costream *result = 0;
261 /* Which filename do we use as the base? */
262 char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
264 /* The template for the intermediate file name. */
265 char *intermedFileName = makeIntermedTemplate( baseFileName );
267 /* Randomize the name and try to open. */
268 char *firstX = strrchr( intermedFileName, 'X' ) - 5;
269 for ( int tries = 0; tries < 20; tries++ ) {
270 /* Choose a random name. */
271 for ( int x = 0; x < 6; x++ )
272 firstX[x] = fnChars[random() % 52];
274 /* Try to open the file. */
275 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
279 FILE *file = fdopen( fd, "wt" );
281 error() << "fdopen(...) on intermediate file failed" << endp;
283 cfilebuf *b = new cfilebuf( intermedFileName, file );
284 result = new costream( b );
288 if ( errno == EACCES ) {
289 error() << "failed to open temp file " << intermedFileName <<
290 ", access denied" << endp;
295 error() << "abnormal error: cannot find unique name for temp file" << endp;
300 /* Main, process args and call yyparse to start scanning input. */
301 int main(int argc, char **argv)
303 ParamCheck pc("o:nmleabjkS:M:CDJRvHh?-:sT:F:G:P:Lp", argc, argv);
304 char *inputFileName = 0;
305 char *outputFileName = 0;
307 while ( pc.check() ) {
308 switch ( pc.state ) {
309 case ParamCheck::match:
310 switch ( pc.parameter ) {
313 if ( *pc.parameterArg == 0 )
314 error() << "a zero length output file name was given" << endl;
315 else if ( outputFileName != 0 )
316 error() << "more than one output file name was given" << endl;
318 /* Ok, remember the output file name. */
319 outputFileName = pc.parameterArg;
320 backendArgs.append( "-o" );
321 backendArgs.append( pc.parameterArg );
325 /* Minimization, mostly hidden options. */
327 minimizeOpt = MinimizeNone;
330 minimizeOpt = MinimizeEnd;
333 minimizeOpt = MinimizeMostOps;
336 minimizeOpt = MinimizeEveryOp;
339 minimizeLevel = MinimizeApprox;
342 minimizeLevel = MinimizeStable;
345 minimizeLevel = MinimizePartition1;
348 minimizeLevel = MinimizePartition2;
353 if ( *pc.parameterArg == 0 )
354 error() << "please specify an argument to -S" << endl;
355 else if ( machineSpec != 0 )
356 error() << "more than one -S argument was given" << endl;
358 /* Ok, remember the path to the machine to generate. */
359 machineSpec = pc.parameterArg;
365 if ( *pc.parameterArg == 0 )
366 error() << "please specify an argument to -M" << endl;
367 else if ( machineName != 0 )
368 error() << "more than one -M argument was given" << endl;
370 /* Ok, remember the machine name to generate. */
371 machineName = pc.parameterArg;
375 /* Host language types. */
377 hostLang = &hostLangC;
380 hostLang = &hostLangD;
383 hostLang = &hostLangJava;
386 hostLang = &hostLangRuby;
389 /* Version and help. */
393 case 'H': case 'h': case '?':
397 printStatistics = true;
400 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
404 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
409 error() << "--" << pc.parameterArg <<
410 " is an invalid argument" << endl;
413 /* Passthrough args. */
415 backendArgs.append( "-T" );
416 backendArgs.append( pc.parameterArg );
419 backendArgs.append( "-F" );
420 backendArgs.append( pc.parameterArg );
423 backendArgs.append( "-G" );
424 backendArgs.append( pc.parameterArg );
427 backendArgs.append( "-P" );
428 backendArgs.append( pc.parameterArg );
431 backendArgs.append( "-p" );
434 backendArgs.append( "-L" );
439 case ParamCheck::invalid:
440 error() << "-" << pc.parameter << " is an invalid argument" << endl;
443 case ParamCheck::noparam:
444 /* It is interpreted as an input file. */
445 if ( *pc.curArg == 0 )
446 error() << "a zero length input file name was given" << endl;
447 else if ( inputFileName != 0 )
448 error() << "more than one input file name was given" << endl;
450 /* OK, Remember the filename. */
451 inputFileName = pc.curArg;
457 /* Bail on above errors. */
458 if ( gblErrorCount > 0 )
461 /* Make sure we are not writing to the same file as the input file. */
462 if ( inputFileName != 0 && outputFileName != 0 &&
463 strcmp( inputFileName, outputFileName ) == 0 )
465 error() << "output file \"" << outputFileName <<
466 "\" is the same as the input file" << endp;
469 /* Open the input file for reading. */
471 if ( inputFileName != 0 ) {
472 /* Open the input file for reading. */
473 ifstream *inFile = new ifstream( inputFileName );
475 if ( ! inFile->is_open() )
476 error() << "could not open " << inputFileName << " for reading" << endp;
479 inputFileName = "<stdin>";
483 /* Used for just a few things. */
484 std::ostringstream hostData;
486 if ( machineSpec == 0 && machineName == 0 )
487 hostData << "<host line=\"1\" col=\"1\">";
489 Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
492 /* Finished, final check for errors.. */
493 if ( gblErrorCount > 0 )
496 /* Now send EOF to all parsers. */
497 terminateAllParsers();
499 /* Finished, final check for errors.. */
500 if ( gblErrorCount > 0 )
503 if ( machineSpec == 0 && machineName == 0 )
504 hostData << "</host>\n";
506 if ( gblErrorCount > 0 )
509 costream *intermed = openIntermed( inputFileName, outputFileName );
511 /* Write the machines, then the surrounding code. */
512 writeMachines( *intermed, hostData.str(), inputFileName );
514 /* Close the intermediate file. */
517 /* Run the backend process. */
518 execBackend( argv[0], intermed );