-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
400 lines (384 loc) · 12.1 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/**********************************************************
* This software is part of the graphviz toolset *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2005 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
* *
* This version available from *
* http://dynagraph.org *
**********************************************************/
#include <stdio.h>
#include <fstream>
#include <time.h>
#include "common/emitGraph.h"
#include "common/Transform.h"
#include "incrface/incrparse.h"
#include "incrface/IncrLangEvents.h"
#include "common/time-o-matic.h"
#include "DuplicateStream.h"
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#include "dynagraph.version.h"
using namespace std;
using namespace Dynagraph;
char *g_outdot=0;
int g_count=1;
bool g_xeptFatal=false;
int g_maxWait=-1;
bool g_randomizeWait = false;
namespace Dynagraph {
StrAttrs g_defaultGraphAttrs;
Transform *g_transform;
bool g_useDotDefaults;
}
struct CouldntOpen {};
#ifndef DYNAGRAPH_NO_THREADS
#define TEXT_OUTPUT_MUTEX
#endif
template<typename V>
struct switchval {
char c;
V v;
char *desc;
};
switchval<dgr::reportType> g_reports[] = {
{'i',dgr::input_raw,"(raw) input"},
{'k',dgr::input_cooked,"(cooked) input"},
{'R',dgr::incrface,"incrface output (defaults to stdout unless preempted)"},
{'T',dgr::dotout,"output complete dot graph each layout"},
{'c',dgr::crossopt,"crossing optimization stats"},
{'t',dgr::timing,"timing breakdown"},
{'d',dgr::dynadag,"dynadag tallies"},
{'a',dgr::ranks,"rank debugging"},
{'g',dgr::modelDump,"dump graph"},
{'q',dgr::dumpQueue,"dump input queue before each cycle"},
{'r',dgr::readability,"readability"},
{'s',dgr::stability,"stability"},
{'p',dgr::progress,"progress"},
{'b',dgr::bug,"bug of the day"},
{'n',dgr::inner_input,"inner engine input"},
{'u',dgr::inner_output,"inner engine output"}
};
int g_nreports = sizeof(g_reports)/sizeof(switchval<dgr::reportType>);
template<typename V>
pair<bool,V> findSwitchVal(switchval<V> *array,int n,char c) {
for(int i =0;i<n;++i)
if(array[i].c==c)
return make_pair(true,array[i].v);
return make_pair(false,V());
}
template<typename V>
pair<bool,char*> findDescription(switchval<V> *array,int n,V v) {
for(int i=0; i<n; ++i)
if(array[i].v==v)
return make_pair(true,array[i].desc);
return make_pair(false,(char*)0);
}
void print_version() {
reports[dgr::cmdline] << "Dynagraph version " << DYNAGRAPH_VERSION_DOTS_QUOTED << endl;
}
template<int i>
struct char1z3 {
BOOST_MPL_ASSERT_RELATION(i,>=,0);
BOOST_MPL_ASSERT_RELATION(i,<,10+26+26);
enum {result = i<10 ? i+'0' : i<36 ? i-10+'A' : i-10-26+'a'};
};
char *DYNAGRAPH_V3RS10N() {
static char buf[8];
buf[0] = char1z3<Dg_MAJOR>::result;
buf[2] = char1z3<Dg_MINOR>::result;
buf[4] = char1z3<Dg_MINIC>::result;
buf[6] = char1z3<Dg_MNEST>::result;
buf[1] = buf[3] = buf[5] = '.';
buf[7] = 0;
return buf;
}
void print_v3rs10n() {
reports[dgr::cmdline] << DYNAGRAPH_V3RS10N() << endl;
}
void print_help() {
reports[dgr::cmdline] <<
"dynagraph arguments:" << endl <<
" -v (--version) print version information only" << endl <<
" -h (-? --help) print this help" << endl <<
" -d use dot-compatible coordinates (position in points, node size in inches)" << endl <<
" -a attr=value set default graph attribute" << endl <<
" -i filename input .dot file (static layout)" << endl <<
" -s filename input .incr file (incrface dynamic layout)" << endl <<
" -oN filename write stream N to filename" << endl <<
" -ra{N} report on a to stream N, where N is [0-9] for -oN, + for stdout, or stderr if N unspecified" << endl;
for(int i = 0;i<g_nreports;++i) reports[dgr::cmdline] <<
" " << g_reports[i].c << ' ' << g_reports[i].desc << endl;
reports[dgr::cmdline] <<
" -w[r] N wait [randomly up to] N nanoseconds before processing each line" << endl <<
" -e specify random number seed (for wait time or fdp)" << endl <<
" -x break on any exception" << endl;
}
void print_report_syntax() {
reports[dgr::error] <<
"-o: specify output filename" << endl <<
" -oN filename, N is [0-9], a file # specified in -r" << endl;
}
void print_attribute_syntax() {
reports[dgr::error] <<
"-a: specify default graph attribute" << endl <<
" -a attr=value" << endl <<
" effectively attr=value is prepended onto every 'open graph' attribute list" << endl;
}
int main(int argc, char *args[]) {
timer.Start();
// enable basic dynagraph report streams
reports.enable(dgr::error,&cerr);
reports.enable(dgr::cmdline,&cout); // for help & anything that returns immediately
loops.sep = ',';
int random_seed = -1;
typedef map<dgr::reportType,int> report_dest_map;
report_dest_map reportDests;
char *dotfile = 0;
ostream *outfile[10];
FILE *input_file = stdin;
for(int i = 0;i<10;++i) outfile[i] = 0;
for(int i = 1; i<argc; ++i) {
if(args[i][0]!='-') {
reports[dgr::error] << "not a valid command: " << args[i] << endl;
return 1;
}
switch(args[i][1]) {
case 'v':
print_version();
return 0;
case 'h':
case '?':
print_version();
print_help();
return 0;
case 'd': // dot-compatible coords
g_transform = &g_dotRatios;
g_useDotDefaults = true;
break;
case 'a': // set default attribute
if(i==argc-1 || args[i][2]) {
print_attribute_syntax();
return 1;
}
else {
char *eq = strchr(args[++i],'=');
DString attr,value;
if(eq) {
attr.assign(args[i],eq-args[i]);
value.assign(eq+1);
}
else
attr.assign(args[i]);
g_defaultGraphAttrs[attr] = value;
}
break;
case 'i': // input dot file (for batch layout)
if(i==argc-1) {
reports[dgr::error] << "-i must be followed by filename" << endl;
return 1;
}
if(args[i][2]) {
reports[dgr::error] << "syntax: -i filename" << endl;
return 1;
}
dotfile = args[++i];
break;
case 's': // dynamic script
if(i==argc-1 || args[i][2]) {
reports[dgr::error] <<
"-s: specify input incrface script" << endl <<
" -s filename: use script in filename (instead of stdin)" << endl;
return 1;
}
if(!(input_file = fopen(args[++i],"r"))) {
reports[dgr::error] << "could not open input script " << args[i] << endl;
return 1;
}
//setvbuf(input_file,0,_IONBF,0);
break;
case 'o': // output files
if(i==argc-1 || !args[i][2]) {
print_report_syntax();
return 1;
}
else if(!isdigit(args[i][2])) {
print_report_syntax();
return 1;
} else {
int n = args[i][2]-'0';
outfile[n] = new fstream(args[++i],fstream::out);
if(outfile[n]->fail()) {
reports[dgr::error] << "-o error: couldn't open file " << args[i] << " for writing" << endl;
return 1;
}
}
break;
case 'r': {// reports
char last = args[i][strlen(args[i])-1];
int o;
if(isdigit(last))
o = last-'0';
else if(last=='+')
o = -1; // stdout
else if(isalpha(last))
o = -2; // stderr
else {
reports[dgr::error] << "-r: generate report" << endl <<
" report destination " << last << " not recognized, must be 0=9 or +" << endl;
return 1;
}
for(int j = 2; args[i][j] && isalpha(args[i][j]); ++j) {
pair<bool,dgr::reportType> val = findSwitchVal(g_reports,g_nreports,args[i][j]);
if(!val.first) {
reports[dgr::error] << "-r: generate report " << endl <<
" report code '" << args[i][j] << "' not recognized" << endl;
return 1;
}
reportDests[val.second] = o;
}
break;
}
case 'w':
if(i==argc-1) {
reports[dgr::error] << "-w wait time needs nanosecond arg" << endl;
return 1;
}
if(args[i][2]=='r')
g_randomizeWait = true;
g_maxWait = atoi(args[++i]);
break;
case 'e':
if(i==argc-1) {
reports[dgr::error] << "-e random number sEed needs integer arg" << endl;
return 1;
}
random_seed = atoi(args[++i]);
break;
case 'x':
g_xeptFatal = true;
break;
case '-':
if(!strcmp(args[i]+2,"version")) {
print_version();
return 0;
}
if(!strcmp(args[i]+2,"v3rs10n")) {
print_v3rs10n();
return 0;
}
else if(!strcmp(args[i]+2,"help")) {
print_version();
print_help();
return 0;
}
//else fallthru
default:
reports[dgr::error] << "command not recognized: " << args[i] << endl;
print_version();
print_help();
return 1;
}
}
if(random_seed<0)
random_seed = time(NULL);
srand(random_seed);
// do not buffer std c files - ? - but i think only c++ iostreams are used?
// and why would you set buffering on stdin?
setvbuf(stdin,0,_IONBF,0);
setvbuf(stdout,0,_IONBF,0);
setvbuf(stderr,0,_IONBF,0);
// open report output files
bool cout_was_grabbed=false;
for(report_dest_map::iterator ri = reportDests.begin(); ri!=reportDests.end(); ++ri) {
ostream *f;
int dest_stream_id = ri->second;
if(dest_stream_id==-1) {
cout_was_grabbed = true;
f = &cout;
}
else if(dest_stream_id==-2)
f = &cerr;
else {
dgcheck(dest_stream_id>=0 && dest_stream_id<=9);
f = outfile[dest_stream_id];
}
if(!f) {
char c = '0' + dest_stream_id;
char outfilename[120];
if(dotfile && strlen(dotfile)<100)
sprintf(outfilename,"%s.out.%c",dotfile,c);
else
sprintf(outfilename,"dynagraph.out.%c",c);
reports[dgr::error] << "outfile " << c << " not specified; opening " << outfilename << endl;
outfile[dest_stream_id] = new fstream(outfilename,fstream::out);
if(outfile[dest_stream_id]->fail()) {
reports[dgr::error] << "couldn't open " << outfilename << " for writing" << endl;
return 1;
}
}
reports.enable(ri->first,f);
}
// reporting may influence behavior..
// recording raw input and inserting delays both require this thread here
if(reports.enabled(dgr::input_raw) || g_maxWait>=0) {
DuplicateIn *din = new DuplicateIn(input_file,reports[dgr::input_raw]);
incr_yyin = din->getNewInput();
}
else
incr_yyin = input_file; // else read direct from std::cin or -s file
// send incrface to stdout if nothing else is going there
if(!cout_was_grabbed) {
// if incrface has output file, tee to cout
if(reports.enabled(dgr::incrface)) {
typedef boost::iostreams::tee_device<std::ostream,std::ostream> t_dev_t;
typedef boost::iostreams::stream<t_dev_t> t_stream_t;
t_dev_t *t_dev = new t_dev_t(reports[dgr::incrface],std::cout);
t_stream_t *t_stream = new t_stream_t(*t_dev);
reports.enable(dgr::incrface,t_stream);
}
else
reports.enable(dgr::incrface, &cout);
}
// default transform
if(!g_transform)
g_transform = new Transform(Coord(1,1),Coord(1,1));
while(1) {
try {
incr_yyparse();
break; // end of stream
}
catch(Assertion sert) {
LOCK_REPORT(dgr::incrface);
reports[dgr::incrface] << "message \"(exception) Assertion: " << sert.expr << "; " << sert.file << ", " << sert.line << '"' << endl;
if(g_xeptFatal||sert.fatal)
exit(23);
}
catch(DGException2 dgx) {
LOCK_REPORT(dgr::incrface);
reports[dgr::incrface] << "message \"(exception) " << dgx.exceptype << ": " << dgx.param << '"' << endl;
if(g_xeptFatal||dgx.fatal)
exit(23);
}
catch(DGException dgx) {
LOCK_REPORT(dgr::incrface);
reports[dgr::incrface] << "message \"(exception) " << dgx.exceptype << '"' << endl;
if(g_xeptFatal||dgx.fatal)
exit(23);
}
catch(...) {
LOCK_REPORT(dgr::incrface);
reports[dgr::incrface] << "message \"(exception) unknown exception\"" << endl;
exit(23);
}
}
incr_shutdown();
reports[dgr::incrface] << "message \"dynagraph closing\"" << endl;
return 0;
}