Make use of @datadir@ and @mandir@ in doc/Makefile.in for specifying where
[external/ragel.git] / rlgen-dot / main.cpp
1 /*
2  *  Copyright 2001-2005 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 istream *inStream = 0;
48 ostream *outStream = 0;
49 output_filter *outFilter = 0;
50 char *outputFileName = 0;
51
52 /* Graphviz dot file generation. */
53 bool graphvizDone = false;
54
55 int numSplitPartitions = 0;
56 bool displayPrintables = false;
57
58 /* Print a summary of the options. */
59 void 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 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 /* Total error count. */
81 int gblErrorCount = 0;
82
83 ostream &error()
84 {
85         gblErrorCount += 1;
86         cerr << PROGNAME ": ";
87         return cerr;
88 }
89
90 /*
91  * Callbacks invoked by the XML data parser.
92  */
93
94 /* Invoked by the parser when the root element is opened. */
95 ostream *openOutput( char *inputFile )
96 {
97         /* Make sure we are not writing to the same file as the input file. */
98         if ( outputFileName != 0 && strcmp( inputFile, outputFileName  ) == 0 ) {
99                 error() << "output file \"" << outputFileName  << 
100                                 "\" is the same as the input file" << endl;
101         }
102
103         if ( outputFileName != 0 ) {
104                 /* Create the filter on the output and open it. */
105                 outFilter = new output_filter( outputFileName );
106                 outFilter->open( outputFileName, ios::out|ios::trunc );
107                 if ( !outFilter->is_open() ) {
108                         error() << "error opening " << outputFileName << " for writing" << endl;
109                         exit(1);
110                 }
111
112                 /* Open the output stream, attaching it to the filter. */
113                 outStream = new ostream( outFilter );
114         }
115         else {
116                 /* Writing out ot std out. */
117                 outStream = &cout;
118         }
119         return outStream;
120 }
121
122 /* Invoked by the parser when a ragel definition is opened. */
123 CodeGenData *makeCodeGen( char *sourceFileName, char *fsmName, 
124                 ostream &out, bool wantComplete )
125 {
126         CodeGenData *codeGen = new GraphvizDotGen(out);
127
128         codeGen->sourceFileName = sourceFileName;
129         codeGen->fsmName = fsmName;
130         codeGen->wantComplete = wantComplete;
131
132         return codeGen;
133 }
134
135
136 /* Main, process args and call yyparse to start scanning input. */
137 int main(int argc, char **argv)
138 {
139         ParamCheck pc("o:pvHh?-:", argc, argv);
140         char *xmlInputFileName = 0;
141
142         while ( pc.check() ) {
143                 switch ( pc.state ) {
144                 case ParamCheck::match:
145                         switch ( pc.parameter ) {
146                         /* Output. */
147                         case 'o':
148                                 if ( *pc.parameterArg == 0 )
149                                         error() << "a zero length output file name was given" << endl;
150                                 else if ( outputFileName != 0 )
151                                         error() << "more than one output file name was given" << endl;
152                                 else {
153                                         /* Ok, remember the output file name. */
154                                         outputFileName = pc.parameterArg;
155                                 }
156                                 break;
157
158                         case 'p':
159                                 displayPrintables = true;
160                                 break;
161
162                         /* Version and help. */
163                         case 'v':
164                                 version();
165                                 exit(0);
166                         case 'H': case 'h': case '?':
167                                 usage();
168                                 exit(0);
169                         case '-':
170                                 if ( strcasecmp(pc.parameterArg, "help") == 0 ) {
171                                         usage();
172                                         exit(0);
173                                 }
174                                 else if ( strcasecmp(pc.parameterArg, "version") == 0 ) {
175                                         version();
176                                         exit(0);
177                                 }
178                                 else {
179                                         error() << "--" << pc.parameterArg << 
180                                                         " is an invalid argument" << endl;
181                                         break;
182                                 }
183                         }
184                         break;
185
186                 case ParamCheck::invalid:
187                         error() << "-" << pc.parameter << " is an invalid argument" << endl;
188                         break;
189
190                 case ParamCheck::noparam:
191                         if ( *pc.curArg == 0 )
192                                 error() << "a zero length input file name was given" << endl;
193                         else if ( xmlInputFileName != 0 )
194                                 error() << "more than one input file name was given" << endl;
195                         else {
196                                 /* OK, Remember the filename. */
197                                 xmlInputFileName = pc.curArg;
198                         }
199                         break;
200                 }
201         }
202
203         /* Bail on above errors. */
204         if ( gblErrorCount > 0 )
205                 exit(1);
206
207         /* Open the input file for reading. */
208         if ( xmlInputFileName != 0 ) {
209                 /* Open the input file for reading. */
210                 ifstream *inFile = new ifstream( xmlInputFileName );
211                 inStream = inFile;
212                 if ( ! inFile->is_open() )
213                         error() << "could not open " << xmlInputFileName << " for reading" << endl;
214         }
215         else {
216                 xmlInputFileName = "<stdin>";
217                 inStream = &cin;
218         }
219
220         /* Bail on above errors. */
221         if ( gblErrorCount > 0 )
222                 exit(1);
223
224         bool wantComplete = false;
225         bool outputActive = false;
226
227         /* Parse the input! */
228         xml_parse( *inStream, xmlInputFileName, outputActive, wantComplete );
229
230         /* If writing to a file, delete the ostream, causing it to flush.
231          * Standard out is flushed automatically. */
232         if ( outputFileName != 0 ) {
233                 delete outStream;
234                 delete outFilter;
235         }
236
237         /* Finished, final check for errors.. */
238         if ( gblErrorCount > 0 ) {
239                 /* If we opened an output file, remove it. */
240                 if ( outputFileName != 0 )
241                         unlink( outputFileName );
242                 exit(1);
243         }
244         return 0;
245 }