/
main.cpp
224 lines (189 loc) · 5.02 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
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <string>
#include <set>
#include <bigmc.h>
void print_usage(char **argv) {
fprintf(stderr,
"Usage: %s [options] <modelfile>\n"
" Options:\n"
"\t-h\tDisplay this help and exit\n"
"\t-V\tPrint verbose output\n"
"\t-VV\tPrint even more verbose output\n"
"\t-t n\tStart n concurrent checker threads (default: 2)\n"
"\t-G x\tOutput graphviz dot file to x\n"
"\t-m x\tSpecify x maximum steps of graph unfolding (default: 1000)\n"
"\t-p\tPrint new states as they are discovered.\n"
"\t-l\tLocal check mode - do not build the reaction graph.\n"
"\t-r x\tOutput statistics and graphs every x steps (default: 500)\n"
"\t-v\tPrint version information and exit\n",
argv[0]);
}
void print_version() {
cout << "BigMC version " << PACKAGE_VERSION << " (http://bigraph.org/bigmc)\n";
cout << "Copyright (c) 2011 Gian Perrone <gdpe at itu.dk>\n";
}
void config_read() {
// We try to locate bigmc.conf in this order:
// If BIGMC_HOME is set:
// $BIGMC_HOME/conf/bigmc.conf
// otherwise:
// /usr/local/bigmc/conf/bigmc.conf
// ./bigmc.conf
// ./conf/bigmc.conf
// fail
FILE *fp = NULL;
char *configfile = NULL;
char *bigmc_home = getenv("BIGMC_HOME");
if(bigmc_home == NULL) {
cerr << "Warning: BIGMC_HOME environment variable is not set." << endl;
fp = fopen("/usr/local/bigmc/conf/bigmc.conf", "r");
configfile = "/usr/local/bigmc/conf/bigmc.conf";
if(fp == NULL) {
fp = fopen("./bigmc.conf", "r");
configfile = "./bigmc.conf";
}
if(fp == NULL) {
fp = fopen("./conf/bigmc.conf","r");
configfile = "./conf/bigmc.conf";
}
if(fp == NULL) {
cerr << "Error: failed to locate bigmc.conf. Try setting BIGMC_HOME\n";
exit(1);
}
global_cfg.bigmc_home = ".";
} else {
char buf[4096] = { 0 };
sprintf(buf,"%s/conf/bigmc.conf", bigmc_home);
fp = fopen(buf, "r");
configfile = strdup(buf);
if(fp == NULL) {
cerr << "Error: failed to locate " << buf << endl;
exit(1);
}
global_cfg.bigmc_home = bigmc_home;
}
int clineno = 0;
char line[2048] = {0};
while(fgets(line, 2048, fp) != NULL) {
clineno++;
if(line[0] == '#' || line[0] == '\n' || line[0] == '\r') continue;
if(strncmp(line, "predicate", 9) == 0) {
char *c = line+9;
while(*c == ' ' || *c == '\t') c++;
// First we get the symbol name
char sname[256];
int i = 0;
while(*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r' && *c != '\0') {
sname[i++] = *(c++);
}
sname[i] = '\0';
// Skip any whitespace
while(*c == ' ' || *c == '\t') c++;
// Then we get the filename
char fname[768];
i = 0;
while(*c != ' ' && *c != '\t' && *c != '\n' && *c != '\r' &&
*c != '\0' && *c != '#') {
fname[i++] = *(c++);
}
fname[i] = '\0';
if(strlen(sname) == 0 || strlen(fname) == 0) {
cout << "Error: " << configfile << ":" << clineno <<
": invalid predicate declaration" << endl;
exit(1);
}
string psname = string(sname);
query_predicate::register_predicate(psname,
new predicate(psname, fname));
} else {
cout << "Error: " << configfile << ":" << clineno <<
": invalid configuration option" << endl;
exit(1);
}
}
free(configfile);
}
int main(int argc, char**argv) {
int c;
// Set config defaults
global_cfg.debug = false;
global_cfg.verbose_level = 0;
global_cfg.max_steps = 1000;
global_cfg.report_interval = 500;
global_cfg.graph_out = NULL;
global_cfg.check_local = false;
global_cfg.threads = 2;
global_cfg.print_mode = false;
while ((c = getopt (argc, argv, "hvVlpdm:G:r:t:")) != -1)
switch (c) {
case 'h':
print_usage(argv);
return 0;
case 'V':
global_cfg.verbose_level++;
break;
case 'v':
print_version();
return 0;
case 'd':
global_cfg.debug = true;
break;
case 'G':
global_cfg.graph_out = strdup(optarg);
break;
case 'm':
global_cfg.max_steps = (unsigned long)atol(optarg);
break;
case 'p':
global_cfg.print_mode = true;
break;
case 't':
global_cfg.threads = atoi(optarg);
break;
case 'r':
global_cfg.report_interval = (unsigned long)atol(optarg);
break;
case 'l':
global_cfg.check_local = true;
break;
case '?':
if (isprint (optopt))
fprintf (stderr, "Unknown option `-%c'.\n", optopt);
else
fprintf (stderr, "Unknown option character `\\x%x'.\n",
optopt);
default:
print_usage(argv);
abort ();
}
config_read();
int index;
char *modelfile = NULL;
for (index = optind; index < argc; index++) {
modelfile = argv[index];
}
if(!modelfile) {
parser::init(NULL);
} else {
FILE *fp = fopen(modelfile, "r");
if(fp == NULL) {
perror("Cannot read model file");
return 1;
} else
fclose(fp);
global_cfg.model_file = modelfile;
parser::init(modelfile);
}
global_cfg.model_file = modelfile;
try {
parser::parse();
} catch (const std::bad_alloc& ) {
rerror("top-level") << "Oh dear. We hit a bad_alloc exception. This usually means you ran out of memory." << endl;
}
parser::cleanup();
query_predicate::cleanup();
return 0;
}