d2f19d82e8c9d907f03425d527a25d517664343f
[external/ragel.git] / rlgen-dot / 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
29 #include "common.h"
30 #include "rlgen-dot.h"
31 #include "xmlparse.h"
32 #include "pcheck.h"
33 #include "vector.h"
34 #include "version.h"
35 #include "gvdotgen.h"
36
37 using std::istream;
38 using std::ifstream;
39 using std::ostream;
40 using std::ios;
41 using std::cin;
42 using std::cout;
43 using std::cerr;
44 using std::endl;
45
46 /* Io globals. */
47 extern istream *inStream;
48 extern ostream *outStream;
49 extern output_filter *outFilter;
50 extern const char *outputFileName;
51
52 /* Graphviz dot file generation. */
53 extern bool graphvizDone;
54 extern bool displayPrintables;
55
56 extern int numSplitPartitions;
57
58 /* Print a summary of the options. */
59 void dot_usage()
60 {
61         cout <<
62 "usage: " PROGNAME " [options] file\n"
63 "general:\n"
64 "   -h, -H, -?, --help    Print this usage and exit\n"
65 "   -v, --version         Print version information and exit\n"
66 "   -o <file>             Write output to <file>\n"
67 "output:\n"
68 "   -p                    Display printable characters on labels\n"
69         ;       
70 }
71
72 /* Print version information. */
73 void dot_version()
74 {
75         cout << "Ragel Code Generator for Graphviz" << endl <<
76                         "Version " VERSION << ", " PUBDATE << endl <<
77                         "Copyright (c) 2001-2007 by Adrian Thurston" << endl;
78 }
79
80 ostream &dot_error()
81 {
82         gblErrorCount += 1;
83         cerr << PROGNAME ": ";
84         return cerr;
85 }
86
87 /*
88  * Callbacks invoked by the XML data parser.
89  */
90
91 /* Invoked by the parser when the root element is opened. */
92 ostream *dotOpenOutput( char *inputFile )
93 {
94         /* Make sure we are not writing to the same file as the input file. */
95         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
96                 dot_error() << "output file \"" << outputFileName  << 
97                                 "\" is the same as the input file" << endl;
98         }
99
100         if ( outputFileName != 0 ) {
101                 /* Create the filter on the output and open it. */
102                 outFilter = new output_filter( outputFileName );
103                 outFilter->open( outputFileName, ios::out|ios::trunc );
104                 if ( !outFilter->is_open() ) {
105                         dot_error() << "error opening " << outputFileName << " for writing" << endl;
106                         exit(1);
107                 }
108
109                 /* Open the output stream, attaching it to the filter. */
110                 outStream = new ostream( outFilter );
111         }
112         else {
113                 /* Writing out ot std out. */
114                 outStream = &cout;
115         }
116         return outStream;
117 }
118
119 /* Invoked by the parser when a ragel definition is opened. */
120 CodeGenData *dotMakeCodeGen( char *sourceFileName, char *fsmName, 
121                 ostream &out, bool wantComplete )
122 {
123         CodeGenData *codeGen = new GraphvizDotGen(out);
124
125         codeGen->sourceFileName = sourceFileName;
126         codeGen->fsmName = fsmName;
127         codeGen->wantComplete = wantComplete;
128
129         return codeGen;
130 }
131
132
133 /* Main, process args and call yyparse to start scanning input. */
134 int dot_main( const char *xmlInputFileName )
135 {
136         /* Open the input file for reading. */
137         ifstream *inFile = new ifstream( xmlInputFileName );
138         inStream = inFile;
139         if ( ! inFile->is_open() )
140                 dot_error() << "could not open " << xmlInputFileName << " for reading" << endl;
141
142         /* Bail on above errors. */
143         if ( gblErrorCount > 0 )
144                 exit(1);
145
146         bool wantComplete = false;
147         bool outputActive = false;
148
149         /* Parse the input! */
150         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
151
152         /* If writing to a file, delete the ostream, causing it to flush.
153          * Standard out is flushed automatically. */
154         if ( outputFileName != 0 ) {
155                 delete outStream;
156                 delete outFilter;
157         }
158
159         /* Finished, final check for errors.. */
160         if ( gblErrorCount > 0 ) {
161                 /* If we opened an output file, remove it. */
162                 if ( outputFileName != 0 )
163                         unlink( outputFileName );
164                 exit(1);
165         }
166         return 0;
167 }