-
Notifications
You must be signed in to change notification settings - Fork 0
/
bracket.cpp
178 lines (170 loc) · 3.99 KB
/
bracket.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
#include <climits>
#include <cstdlib>
#include <cwchar>
#include <fcntl.h>
#include <iostream>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "Parser.h"
#include "boost/iostreams/device/file_descriptor.hpp"
#include "boost/iostreams/stream.hpp"
#include "config.h"
#include "help.h"
using namespace RabinParser;
namespace ios = boost::iostreams;
extern int optind;
extern char *optarg;
constexpr const char *run_head = "%%------------------------------------RUN-------------------------------------\n";
constexpr const char *aut_head = "%%---------------------------------AUTOMATON----------------------------------\n";
static Rabin_automaton *parse();
static int out_fd(const char *, const bool);
int
main(int argc, char *argv[])
{
for (int i = 0; i < argc; i++) {
if (nullptr == argv[i]) {
std::cout << usage;
return EXIT_FAILURE;
}
}
{
int op = 0;
while ((op = getopt(argc, argv, "i:o:L:t:wglhV")) != -1) {
switch (op) {
case 'i':
config.in = optarg;
break;
case 'o':
config.graphviz_out = optarg;
config.graphviz = true;
break;
case 'L':
config.lp_out = optarg;
config.lp = true;
break;
case 't':
errno = 0;
{
unsigned long tmp = strtoul(optarg, nullptr, 10);
if (errno || INT_MAX < tmp || 1 > tmp) {
std::cout << usage;
return EXIT_FAILURE;
}
config.max_threads = static_cast<int>(tmp);
}
break;
case 'w':
config.overwrite = true;
break;
case 'g':
config.graphviz = true;
break;
case 'l':
config.lp = true;
break;
case 'h':
config.help = true;
break;
case 'V':
config.version = true;
break;
default:
std::cout << usage;
return EXIT_FAILURE;
}
}
}
if (config.help) {
std::cout << usage;
return EXIT_SUCCESS;
}
if (config.version) {
std::cout << PROG_VERSION << std::endl << license;
return EXIT_SUCCESS;
}
if (optind < argc) {
config.in = argv[optind];
}
const Rabin_automaton *const automaton = parse();
if (nullptr == automaton) {
return EXIT_FAILURE;
}
ios::stream<ios::file_descriptor> os;
if (config.lp) {
const int fd = out_fd(config.lp_out, config.overwrite);
if (-1 < fd) {
os.open(ios::file_descriptor(fd, ios::close_handle));
os << aut_head;
automaton->print_logic_prog_rep(os);
os << std::endl;
}
}
std::cout << "Searching for an accepted regular run..." << std::endl;
const Run *run = nullptr;
if (nullptr != (run = automaton->find_run(config.max_threads))) {
std::cout << "NONEMPTY LANGUAGE" << std::endl;
if (os.is_open()) {
os << std::endl << run_head;
run->print_logic_prog_rep(os);
os << std::endl;
os.close();
}
if (config.graphviz) {
const int fd = out_fd(config.graphviz_out, config.overwrite);
if (-1 < fd) {
os.open(ios::file_descriptor(fd, ios::close_handle));
os << *run << std::endl;
os.close();
}
}
delete run;
} else {
std::cout << "EMPTY LANGUAGE" << std::endl;
if (os.is_open()) {
os.close();
}
}
delete automaton;
return EXIT_SUCCESS;
}
static Rabin_automaton *
parse()
{
Scanner *scanner = nullptr;
if (nullptr != config.in) {
wchar_t *fileName = coco_string_create(config.in);
scanner = new Scanner(fileName);
coco_string_delete(fileName);
} else {
scanner = new Scanner(stdin);
}
Parser *const parser = new Parser(scanner);
try {
parser->Parse();
} catch (const Illegal_state_set &e) {
}
Rabin_automaton *const res = parser->automaton;
delete parser;
delete scanner;
return res;
}
static int
out_fd(const char *path, const bool overwrite)
{
int fd = -1;
errno = 0;
if (overwrite) {
fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 00664);
} else {
fd = open(path, O_EXCL | O_WRONLY | O_CREAT, 00664);
}
if (-1 == fd) {
if (!overwrite && EEXIST == errno) {
std::cerr << "file " << path << " already exists, not overwriting" << std::endl;
} else {
std::cerr << "could not open file " << path << ": " << strerror(errno) << std::endl;
}
}
return fd;
}