-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathdisplay.cc
212 lines (159 loc) · 4.43 KB
/
display.cc
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
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <iostream>
#include <boost/algorithm/string.hpp>
#include "storage/StorageImpl.h"
#include "storage/Environment.h"
#include "storage/Devicegraph.h"
#include "storage/Utils/SystemCmd.h"
#include "storage/Utils/Logger.h"
#include "storage/Utils/AppUtil.h"
#include "storage/Utils/StorageDefines.h"
#include "storage/Utils/Format.h"
#include "storage/Actiongraph.h"
using namespace std;
using namespace storage;
enum Mode { DEVICEGRAPH, ACTIONGRAPH };
Mode mode = DEVICEGRAPH;
bool keep_gv = false;
bool keep_svg = false;
View view = View::ALL;
void
helper(const string& filename_gv, const string& filename_svg)
{
system(string(DOT_BIN " -Tsvg < " + quote(filename_gv) + " > " + quote(filename_svg)).c_str());
if (!keep_gv)
unlink(filename_gv.c_str());
system(string(DISPLAY_BIN " " + quote(filename_svg)).c_str());
if (!keep_svg)
unlink(filename_svg.c_str());
}
void
doit_devicegraph(const string& filename)
{
Environment environment(true, ProbeMode::READ_DEVICEGRAPH, TargetMode::DIRECT);
environment.set_devicegraph_filename(filename);
Storage storage(environment);
storage.probe();
const Devicegraph* probed = storage.get_probed();
cout << *probed << '\n';
const TmpDir& tmp_dir = storage.get_impl().get_tmp_dir();
string name = basename(filename);
if (boost::ends_with(name, ".xml"))
name.erase(name.size() - 4);
string filename_gv = keep_gv ? name + ".gv" : tmp_dir.get_fullname() + "/" + name + ".gv";
string filename_svg = keep_svg ? name + ".svg" : tmp_dir.get_fullname() + "/" + name + ".svg";
probed->write_graphviz(filename_gv, get_debug_devicegraph_style_callbacks(), view);
helper(filename_gv, filename_svg);
}
void
doit_actiongraph(const string& filename_lhs, const string& filename_rhs)
{
Environment environment(true, ProbeMode::READ_DEVICEGRAPH, TargetMode::DIRECT);
environment.set_devicegraph_filename(filename_lhs);
Storage storage(environment);
storage.probe();
Devicegraph* system = storage.get_system();
cout << *system << '\n';
Devicegraph* staging = storage.get_staging();
staging->load(filename_rhs);
cout << *staging << '\n';
Actiongraph actiongraph(storage, system, staging);
const TmpDir& tmp_dir = storage.get_impl().get_tmp_dir();
string name = "actions";
string filename_gv = keep_gv ? name + ".gv" : tmp_dir.get_fullname() + "/" + name + ".gv";
string filename_svg = keep_svg ? name + ".svg" : tmp_dir.get_fullname() + "/" + name + ".svg";
actiongraph.write_graphviz(filename_gv, get_debug_actiongraph_style_callbacks());
helper(filename_gv, filename_svg);
}
void usage() __attribute__ ((__noreturn__));
void
usage()
{
cerr << "display [--devicegraph] [--keep-gv] [--keep-svg] [--view view] filename\n"
<< "display --actiongraph [--keep-gv] [--keep-svg] filename filename\n";
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
const struct option options[] = {
{ "devicegraph", no_argument, 0, 1 },
{ "actiongraph", no_argument, 0, 2 },
{ "keep-gv", no_argument, 0, 3 },
{ "keep-svg", no_argument, 0, 4 },
{ "view", required_argument, 0, 5 },
{ 0, 0, 0, 0 }
};
while (true)
{
int option_index = 0;
int c = getopt_long(argc, argv, "", options, &option_index);
if (c == -1)
break;
if (c == '?')
usage();
switch (c)
{
case 1:
mode = DEVICEGRAPH;
break;
case 2:
mode = ACTIONGRAPH;
break;
case 3:
keep_gv = true;
break;
case 4:
keep_svg = true;
break;
case 5:
if (strcmp(optarg, "all") == 0)
view = View::ALL;
else if (strcmp(optarg, "classic") == 0)
view = View::CLASSIC;
else if (strcmp(optarg, "remove") == 0)
view = View::REMOVE;
else
{
cerr << sformat("Unknown view '%s'.", optarg) << endl;
usage();
}
break;
default:
usage();
}
}
switch (mode)
{
case DEVICEGRAPH:
if (optind + 1 != argc)
usage();
break;
case ACTIONGRAPH:
if (optind + 2 != argc)
usage();
break;
}
try
{
set_logger(get_logfile_logger());
switch (mode)
{
case DEVICEGRAPH:
doit_devicegraph(argv[optind]);
break;
case ACTIONGRAPH:
doit_actiongraph(argv[optind], argv[optind + 1]);
break;
}
}
catch (const exception& e)
{
cerr << "exception occurred: " << e.what() << '\n';
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}