1edcfe984b8605d55a4014da6765f50ab765f68e
[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 #endif
41
42 /* Parsing. */
43 #include "ragel.h"
44 #include "rlscan.h"
45
46 /* Parameters and output. */
47 #include "pcheck.h"
48 #include "vector.h"
49 #include "version.h"
50 #include "common.h"
51
52 using std::istream;
53 using std::ostream;
54 using std::ifstream;
55 using std::ofstream;
56 using std::cin;
57 using std::cout;
58 using std::cerr;
59 using std::endl;
60 using std::ios;
61 using std::streamsize;
62
63 /* Controls minimization. */
64 MinimizeLevel minimizeLevel = MinimizePartition2;
65 MinimizeOpt minimizeOpt = MinimizeMostOps;
66
67 /* Graphviz dot file generation. */
68 char *machineSpec = 0, *machineName = 0;
69 bool machineSpecFound = false;
70
71 bool printStatistics = false;
72 bool frontendOnly = false;
73
74 typedef Vector<char*> ArgsVector;
75 ArgsVector frontendArgs;
76 ArgsVector backendArgs;
77
78 /* Print a summary of the options. */
79 void usage()
80 {
81         cout <<
82 "usage: ragel [options] file\n"
83 "general:\n"
84 "   -h, -H, -?, --help   Print this usage and exit\n"
85 "   -v, --version        Print version information and exit\n"
86 "   -o <file>            Write output to <file>\n"
87 "   -s                   Print some statistics on stderr\n"
88 "fsm minimization:\n"
89 "   -n                   Do not perform minimization\n"
90 "   -m                   Minimize at the end of the compilation\n"
91 "   -l                   Minimize after most operations (default)\n"
92 "   -e                   Minimize after every operation\n"
93 "machine selection:\n"
94 "   -S <spec>            FSM specification to output (for rlgen-dot)\n"
95 "   -M <machine>         Machine definition/instantiation to output (for rlgen-dot)\n"
96 "host language:\n"
97 "   -C                   The host language is C, C++, Obj-C or Obj-C++ (default)\n"
98 "   -D                   The host language is D\n"
99 "   -J                   The host language is Java\n"
100 "   -R                   The host language is Ruby\n"
101         ;       
102 }
103
104 /* Print version information. */
105 void version()
106 {
107         cout << "Ragel State Machine Compiler version " VERSION << " " PUBDATE << endl <<
108                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
109 }
110
111 /* Total error count. */
112 int gblErrorCount = 0;
113
114 /* Print the opening to a warning in the input, then return the error ostream. */
115 ostream &warning( const InputLoc &loc )
116 {
117         assert( loc.fileName != 0 );
118         cerr << loc.fileName << ":" << loc.line << ":" << 
119                         loc.col << ": warning: ";
120         return cerr;
121 }
122
123 /* Print the opening to a program error, then return the error stream. */
124 ostream &error()
125 {
126         gblErrorCount += 1;
127         cerr << PROGNAME ": ";
128         return cerr;
129 }
130
131 ostream &error( const InputLoc &loc )
132 {
133         gblErrorCount += 1;
134         assert( loc.fileName != 0 );
135         cerr << loc.fileName << ":" << loc.line << ": ";
136         return cerr;
137 }
138
139 void escapeLineDirectivePath( std::ostream &out, char *path )
140 {
141         for ( char *pc = path; *pc != 0; pc++ ) {
142                 if ( *pc == '\\' )
143                         out << "\\\\";
144                 else
145                         out << *pc;
146         }
147 }
148
149 void processArgs( int argc, char **argv, char *&inputFileName, char *&outputFileName )
150 {
151         ParamCheck pc("fo:nmleabjkS:M:CDJRvHh?-:sT:F:G:P:Lp", argc, argv);
152
153         while ( pc.check() ) {
154                 switch ( pc.state ) {
155                 case ParamCheck::match:
156                         switch ( pc.parameter ) {
157                         case 'f':
158                                 frontendOnly = true;
159                                 break;
160
161                         /* Output. */
162                         case 'o':
163                                 if ( *pc.parameterArg == 0 )
164                                         error() << "a zero length output file name was given" << endl;
165                                 else if ( outputFileName != 0 )
166                                         error() << "more than one output file name was given" << endl;
167                                 else {
168                                         /* Ok, remember the output file name. */
169                                         outputFileName = pc.parameterArg;
170                                 }
171                                 break;
172
173                         /* Minimization, mostly hidden options. */
174                         case 'n':
175                                 minimizeOpt = MinimizeNone;
176                                 frontendArgs.append( "-n" );
177                                 break;
178                         case 'm':
179                                 minimizeOpt = MinimizeEnd;
180                                 frontendArgs.append( "-m" );
181                                 break;
182                         case 'l':
183                                 minimizeOpt = MinimizeMostOps;
184                                 frontendArgs.append( "-l" );
185                                 break;
186                         case 'e':
187                                 minimizeOpt = MinimizeEveryOp;
188                                 frontendArgs.append( "-e" );
189                                 break;
190                         case 'a':
191                                 minimizeLevel = MinimizeApprox;
192                                 frontendArgs.append( "-a" );
193                                 break;
194                         case 'b':
195                                 minimizeLevel = MinimizeStable;
196                                 frontendArgs.append( "-b" );
197                                 break;
198                         case 'j':
199                                 minimizeLevel = MinimizePartition1;
200                                 frontendArgs.append( "-j" );
201                                 break;
202                         case 'k':
203                                 minimizeLevel = MinimizePartition2;
204                                 frontendArgs.append( "-k" );
205                                 break;
206
207                         /* Machine spec. */
208                         case 'S':
209                                 if ( *pc.parameterArg == 0 )
210                                         error() << "please specify an argument to -S" << endl;
211                                 else if ( machineSpec != 0 )
212                                         error() << "more than one -S argument was given" << endl;
213                                 else {
214                                         /* Ok, remember the path to the machine to generate. */
215                                         machineSpec = pc.parameterArg;
216                                         frontendArgs.append( "-S" );
217                                         frontendArgs.append( pc.parameterArg );
218                                 }
219                                 break;
220
221                         /* Machine path. */
222                         case 'M':
223                                 if ( *pc.parameterArg == 0 )
224                                         error() << "please specify an argument to -M" << endl;
225                                 else if ( machineName != 0 )
226                                         error() << "more than one -M argument was given" << endl;
227                                 else {
228                                         /* Ok, remember the machine name to generate. */
229                                         machineName = pc.parameterArg;
230                                         frontendArgs.append( "-M" );
231                                         frontendArgs.append( pc.parameterArg );
232                                 }
233                                 break;
234
235                         /* Host language types. */
236                         case 'C':
237                                 hostLang = &hostLangC;
238                                 frontendArgs.append( "-C" );
239                                 break;
240                         case 'D':
241                                 hostLang = &hostLangD;
242                                 frontendArgs.append( "-D" );
243                                 break;
244                         case 'J':
245                                 hostLang = &hostLangJava;
246                                 frontendArgs.append( "-J" );
247                                 break;
248                         case 'R':
249                                 hostLang = &hostLangRuby;
250                                 frontendArgs.append( "-R" );
251                                 break;
252
253                         /* Version and help. */
254                         case 'v':
255                                 version();
256                                 exit(0);
257                         case 'H': case 'h': case '?':
258                                 usage();
259                                 exit(0);
260                         case 's':
261                                 printStatistics = true;
262                                 frontendArgs.append( "-s" );
263                                 break;
264                         case '-':
265                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
266                                         usage();
267                                         exit(0);
268                                 }
269                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
270                                         version();
271                                         exit(0);
272                                 }
273                                 else {
274                                         error() << "--" << pc.parameterArg << 
275                                                         " is an invalid argument" << endl;
276                                 }
277
278                         /* Passthrough args. */
279                         case 'T': 
280                                 backendArgs.append( "-T" );
281                                 backendArgs.append( pc.parameterArg );
282                                 break;
283                         case 'F': 
284                                 backendArgs.append( "-F" );
285                                 backendArgs.append( pc.parameterArg );
286                                 break;
287                         case 'G': 
288                                 backendArgs.append( "-G" );
289                                 backendArgs.append( pc.parameterArg );
290                                 break;
291                         case 'P':
292                                 backendArgs.append( "-P" );
293                                 backendArgs.append( pc.parameterArg );
294                                 break;
295                         case 'p':
296                                 backendArgs.append( "-p" );
297                                 break;
298                         case 'L':
299                                 backendArgs.append( "-L" );
300                                 break;
301                         }
302                         break;
303
304                 case ParamCheck::invalid:
305                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
306                         break;
307
308                 case ParamCheck::noparam:
309                         /* It is interpreted as an input file. */
310                         if ( *pc.curArg == 0 )
311                                 error() << "a zero length input file name was given" << endl;
312                         else if ( inputFileName != 0 )
313                                 error() << "more than one input file name was given" << endl;
314                         else {
315                                 /* OK, Remember the filename. */
316                                 inputFileName = pc.curArg;
317                         }
318                         break;
319                 }
320         }
321 }
322
323 int frontend( char *inputFileName, char *outputFileName )
324 {
325         /* Open the input file for reading. */
326         istream *inStream;
327         if ( inputFileName != 0 ) {
328                 /* Open the input file for reading. */
329                 ifstream *inFile = new ifstream( inputFileName );
330                 inStream = inFile;
331                 if ( ! inFile->is_open() )
332                         error() << "could not open " << inputFileName << " for reading" << endp;
333         }
334         else {
335                 inputFileName = "<stdin>";
336                 inStream = &cin;
337         }
338
339         /* Used for just a few things. */
340         std::ostringstream hostData;
341
342         if ( machineSpec == 0 && machineName == 0 )
343                 hostData << "<host line=\"1\" col=\"1\">";
344
345         Scanner scanner( inputFileName, *inStream, hostData, 0, 0, 0, false );
346         scanner.do_scan();
347
348         /* Finished, final check for errors.. */
349         if ( gblErrorCount > 0 )
350                 return 1;
351         
352         /* Now send EOF to all parsers. */
353         terminateAllParsers();
354
355         /* Finished, final check for errors.. */
356         if ( gblErrorCount > 0 )
357                 return 1;
358
359         if ( machineSpec == 0 && machineName == 0 )
360                 hostData << "</host>\n";
361
362         if ( gblErrorCount > 0 )
363                 return 1;
364         
365         ostream *outputFile = 0;
366         if ( outputFileName != 0 )
367                 outputFile = new ofstream( outputFileName );
368         else
369                 outputFile = &cout;
370
371         /* Write the machines, then the surrounding code. */
372         writeMachines( *outputFile, hostData.str(), inputFileName );
373
374         /* Close the intermediate file. */
375         if ( outputFileName != 0 )
376                 delete outputFile;
377
378         return gblErrorCount > 0;
379 }
380
381 char *makeIntermedTemplate( char *baseFileName )
382 {
383         char *result = 0, *templ = "ragel-XXXXXX.xml";
384         char *lastSlash = strrchr( baseFileName, '/' );
385         if ( lastSlash == 0 ) {
386                 result = new char[strlen(templ)+1];
387                 strcpy( result, templ );
388         }
389         else {
390                 int baseLen = lastSlash - baseFileName + 1;
391                 result = new char[baseLen + strlen(templ) + 1];
392                 memcpy( result, baseFileName, baseLen );
393                 strcpy( result+baseLen, templ );
394         }
395         return result;
396 };
397
398 char *openIntermed( char *inputFileName, char *outputFileName )
399 {
400         srand(time(0));
401         char *result = 0;
402
403         /* Which filename do we use as the base? */
404         char *baseFileName = outputFileName != 0 ? outputFileName : inputFileName;
405
406         /* The template for the intermediate file name. */
407         char *intermedFileName = makeIntermedTemplate( baseFileName );
408
409         /* Randomize the name and try to open. */
410         char fnChars[] = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
411         char *firstX = strrchr( intermedFileName, 'X' ) - 5;
412         for ( int tries = 0; tries < 20; tries++ ) {
413                 /* Choose a random name. */
414                 for ( int x = 0; x < 6; x++ )
415                         firstX[x] = fnChars[rand() % 52];
416
417                 /* Try to open the file. */
418                 int fd = ::open( intermedFileName, O_WRONLY|O_EXCL|O_CREAT, S_IRUSR|S_IWUSR );
419
420                 if ( fd > 0 ) {
421                         /* Success. Close the file immediately and return the name for use
422                          * by the child processes. */
423                         ::close( fd );
424                         result = intermedFileName;
425                         break;
426                 }
427
428                 if ( errno == EACCES ) {
429                         error() << "failed to open temp file " << intermedFileName << 
430                                         ", access denied" << endp;
431                 }
432         }
433
434         if ( result == 0 )
435                 error() << "abnormal error: cannot find unique name for temp file" << endp;
436
437         return result;
438 }
439
440
441 void cleanExit( char *intermed, int status )
442 {
443         unlink( intermed );
444         exit( status );
445 }
446
447 #ifndef WIN32
448
449 /* If any forward slash is found in argv0 then it is assumed that the path is
450  * explicit and the path to the backend executable should be derived from
451  * that. If no forward slash is found it is assumed the file is being run from
452  * the installed location. The PREFIX supplied during configuration is used.
453  * */
454 char **makePathChecksUnix( const char *argv0, const char *progName )
455 {
456         char **result = new char*[3];
457         const char *lastSlash = strrchr( argv0, '/' );
458         int numChecks = 0;
459
460         if ( lastSlash != 0 ) {
461                 char *path = strdup( argv0 );
462                 int givenPathLen = (lastSlash - argv0) + 1;
463                 path[givenPathLen] = 0;
464
465                 int progNameLen = strlen(progName);
466                 int length = givenPathLen + progNameLen + 1;
467                 char *check = new char[length];
468                 sprintf( check, "%s%s", path, progName );
469                 result[numChecks++] = check;
470
471                 length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
472                 check = new char[length];
473                 sprintf( check, "%s../%s/%s", path, progName, progName );
474                 result[numChecks++] = check;
475         }
476         else {
477                 int prefixLen = strlen(PREFIX);
478                 int progNameLen = strlen(progName);
479                 int length = prefixLen + 5 + progNameLen + 1;
480                 char *check = new char[length];
481
482                 sprintf( check, PREFIX "/bin/%s", progName );
483                 result[numChecks++] = check;
484         }
485
486         result[numChecks] = 0;
487         return result;
488 }
489
490
491 int forkAndExec( char *progName, char **pathChecks, 
492                 ArgsVector &args, char *intermed )
493 {
494         pid_t pid = fork();
495         if ( pid < 0 ) {
496                 /* Error, no child created. */
497                 error() << "failed to fork for " << progName << endl;
498                 cleanExit( intermed, 1 );
499         }
500         else if ( pid == 0 ) {
501                 /* child */
502                 while ( *pathChecks != 0 ) {
503                         execv( *pathChecks, args.data );
504                         pathChecks += 1;
505                 }
506                 error() << "failed to exec " << progName << endl;
507                 cleanExit( intermed, 1 );
508         }
509
510         /* Parent process, wait for the child. */
511         int status;
512         wait( &status );
513
514         /* What happened with the child. */
515         if ( ! WIFEXITED( status ) ) {
516                 error() << progName << " did not exit normally" << endl;
517                 cleanExit( intermed, 1 );
518         }
519         
520         if ( WEXITSTATUS(status) != 0 )
521                 cleanExit( intermed, WEXITSTATUS(status) );
522
523         return status;
524 }
525
526 #else
527
528 /* If any forward slash is found in argv0 then it is assumed that the path is
529  * explicit and the path to the backend executable should be derived from
530  * that. If no forward slash is found it is assumed the file is being run from
531  * the installed location. The PREFIX supplied during configuration is used.
532  * */
533 char **makePathChecksWin( const char *progName )
534 {
535         int len = 1024;
536         char *imageFileName = new char[len];
537         HANDLE h = GetCurrentProcess();
538         len = GetModuleFileNameEx( h, NULL, imageFileName, len );
539         imageFileName[len] = 0;
540
541         char **result = new char*[3];
542         const char *lastSlash = strrchr( imageFileName, '\\' );
543         int numChecks = 0;
544
545         assert( lastSlash != 0 );
546         char *path = strdup( imageFileName );
547         int givenPathLen = (lastSlash - imageFileName) + 1;
548         path[givenPathLen] = 0;
549
550         int progNameLen = strlen(progName);
551         int length = givenPathLen + progNameLen + 1;
552         char *check = new char[length];
553         sprintf( check, "%s%s", path, progName );
554         result[numChecks++] = check;
555
556         length = givenPathLen + 3 + progNameLen + 1 + progNameLen + 1;
557         check = new char[length];
558         sprintf( check, "%s..\\%s\\%s", path, progName, progName );
559         result[numChecks++] = check;
560
561         result[numChecks] = 0;
562         return result;
563 }
564
565 #endif
566
567 void execFrontend( const char *argv0, char *inputFileName, char *intermed )
568 {
569         /* The frontend program name. */
570         char *progName = "ragel";
571
572         frontendArgs.insert( 0, progName );
573         frontendArgs.insert( 1, "-f" );
574         frontendArgs.append( "-o" );
575         frontendArgs.append( intermed );
576         frontendArgs.append( inputFileName );
577         frontendArgs.append( 0 );
578
579 #ifndef WIN32
580         char **pathChecks = makePathChecksUnix( argv0, progName );
581         forkAndExec( progName, pathChecks, frontendArgs, intermed );
582 #else
583         char **pathChecks = makePathChecksWin( progName );
584         while ( *pathChecks != 0 )
585                 cerr << *pathChecks++ << endl;
586 #endif
587 }
588
589 void execBackend( const char *argv0, char *intermed, char *outputFileName )
590 {
591         /* Locate the backend program */
592         char *progName = 0;
593         switch ( hostLang->lang ) {
594                 case HostLang::C:
595                 case HostLang::D:
596                         progName = "rlgen-cd";
597                         break;
598                 case HostLang::Java:
599                         progName = "rlgen-java";
600                         break;
601                 case HostLang::Ruby:
602                         progName = "rlgen-ruby";
603                         break;
604         }
605
606         backendArgs.insert( 0, progName );
607         if ( outputFileName != 0 ) {
608                 backendArgs.append( "-o" );
609                 backendArgs.append( outputFileName );
610         }
611         backendArgs.append( intermed );
612         backendArgs.append( 0 );
613
614 #ifndef WIN32
615         char **pathChecks = makePathChecksUnix( argv0, progName );
616         forkAndExec( progName, pathChecks, backendArgs, intermed );
617 #else
618         char **pathChecks = makePathChecksWin( progName );
619         while ( *pathChecks != 0 )
620                 cerr << *pathChecks++ << endl;
621 #endif
622 }
623
624 /* Main, process args and call yyparse to start scanning input. */
625 int main(int argc, char **argv)
626 {
627         char *inputFileName = 0;
628         char *outputFileName = 0;
629
630         processArgs( argc, argv, inputFileName, outputFileName );
631
632         /* Bail on above errors. */
633         if ( gblErrorCount > 0 )
634                 exit(1);
635
636         /* Make sure we are not writing to the same file as the input file. */
637         if ( inputFileName != 0 && outputFileName != 0 && 
638                         strcmp( inputFileName, outputFileName  ) == 0 )
639         {
640                 error() << "output file \"" << outputFileName  << 
641                                 "\" is the same as the input file" << endp;
642         }
643
644         if ( frontendOnly )
645                 return frontend( inputFileName, outputFileName );
646
647         char *intermed = openIntermed( inputFileName, outputFileName );
648
649         /* Run the frontend, then the backend processes. */
650         execFrontend( argv[0], inputFileName, intermed );
651         execBackend( argv[0], intermed, outputFileName );
652
653         /* Clean up the intermediate. */
654         cleanExit( intermed, 0 );
655
656         return 0;
657 }