-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathprobe.cc
193 lines (149 loc) · 3.87 KB
/
probe.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
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <string.h>
#include <iostream>
#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/StorageDefines.h"
#include "storage/Utils/Format.h"
using namespace std;
using namespace storage;
bool display_devicegraph = false;
bool save_devicegraph = false;
bool save_mockup = false;
bool load_mockup = false;
bool ignore_probe_errors = false;
View view = View::ALL;
string rootprefix;
class MyProbeCallbacks : public ProbeCallbacks
{
public:
virtual void
message(const std::string& message) const override
{
cerr << message << endl;
}
virtual bool
error(const std::string& message, const std::string& what) const override
{
cerr << message << endl;
return ignore_probe_errors;
}
};
void
doit()
{
set_logger(get_logfile_logger());
ProbeMode probe_mode = ProbeMode::STANDARD;
if (save_mockup)
probe_mode = ProbeMode::STANDARD_WRITE_MOCKUP;
else if (load_mockup)
probe_mode = ProbeMode::READ_MOCKUP;
Environment environment(true, probe_mode, TargetMode::DIRECT);
environment.set_rootprefix(rootprefix);
environment.set_mockup_filename("mockup.xml");
MyProbeCallbacks my_probe_callbacks;
Storage storage(environment);
storage.probe(&my_probe_callbacks);
const Devicegraph* probed = storage.get_probed();
cout.setf(ios::boolalpha);
cout.setf(ios::showbase);
cout << *probed << '\n';
probed->check();
if (save_devicegraph)
probed->save("devicegraph.xml");
if (display_devicegraph)
{
const TmpDir& tmp_dir = storage.get_impl().get_tmp_dir();
probed->write_graphviz(tmp_dir.get_fullname() + "/probe.gv",
get_debug_devicegraph_style_callbacks(), view);
system(string(DOT_BIN " -Tsvg < " + quote(tmp_dir.get_fullname() + "/probe.gv") + " > " +
quote(tmp_dir.get_fullname() + "/probe.svg")).c_str());
unlink(string(tmp_dir.get_fullname() + "/probe.gv").c_str());
system(string(DISPLAY_BIN " " + quote(tmp_dir.get_fullname() + "/probe.svg")).c_str());
unlink(string(tmp_dir.get_fullname() + "/probe.svg").c_str());
}
}
void usage() __attribute__ ((__noreturn__));
void
usage()
{
cerr << "probe [--display-devicegraph] [--save-devicegraph] [--save-mockup] [--load-mockup] "
"[--ignore-probe-errors] [--view view] [--rootprefix rootprefix]\n";
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
const struct option options[] = {
{ "display-devicegraph", no_argument, 0, 1 },
{ "save-devicegraph", no_argument, 0, 2 },
{ "save-mockup", no_argument, 0, 3 },
{ "load-mockup", no_argument, 0, 4 },
{ "ignore-probe-errors", no_argument, 0, 5 },
{ "view", required_argument, 0, 6 },
{ "rootprefix", required_argument, 0, 7 },
{ 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:
display_devicegraph = true;
break;
case 2:
save_devicegraph = true;
break;
case 3:
save_mockup = true;
break;
case 4:
load_mockup = true;
break;
case 5:
ignore_probe_errors = true;
break;
case 6:
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;
case 7:
rootprefix = optarg;
break;
default:
usage();
}
}
if (optind < argc)
usage();
try
{
doit();
}
catch (const exception& e)
{
cerr << "exception occurred: " << e.what() << '\n';
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}