crex
/
src
/
main.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
#include "parg.hh"
using Parg = OB::Parg;
#include "crex.hh"
using Crex = OB::Crex;
#include "ansi_escape_codes.hh"
namespace aec = OB::ANSI_Escape_Codes;
#include <string>
#include <sstream>
#include <iostream>
#include <regex>
#include <unistd.h>
int program_options(Parg& pg);
std::regex_constants::syntax_option_type regex_options(Parg& pg);
std::regex_constants::match_flag_type regex_flags(Parg& pg);
bool is_tty();
void regex_print(std::string const& regex, std::string const& text,
Crex::Matches const& matches, bool color);
void regex_print_color(std::string const& regex, std::string const& text,
Crex::Matches const& matches);
void regex_print_no_color(std::string const& regex, std::string const& text,
Crex::Matches const& matches);
void regex_print_json(std::string& regex, std::string& text,
Crex::Matches const& matches);
std::string replace(std::string str, std::string key, std::string val);
int program_options(Parg& pg)
{
pg.name("crex").version("0.2.5 (07.10.2018)");
pg.description("explore, test, and check regular expressions");
pg.usage("[flags] [options] [--] [arguments]");
pg.usage("[-e|-b|-x|-a|-g|-E] [-io] [-c|-j] [-r regex] [-s string]");
pg.usage("[-v|--version]");
pg.usage("[-h|--help]");
pg.info("Examples", {
"crex -r '(hello)' -s 'hello world!'",
"printf 'Hello World!' | crex -r '(hello)' -ioec",
"crex --help",
"crex --version",
});
pg.info("Exit Codes", {"0 -> normal", "1 -> error"});
pg.info("Repository", {
"https://github.com/octobanana/crex.git",
});
pg.info("Homepage", {
"https://octobanana.com/software/crex",
});
pg.author("Brett Robinson (octobanana) <octobanana.dev@gmail.com>");
// enable piped stdin
pg.set_stdin();
// single flags
pg.set("help,h", "print the help output");
pg.set("version,v", "print the program version");
// combinable flags
pg.set("color,c", "print out results in color");
pg.set("json,j", "print out results in json");
// options
pg.set("string,s", "", "str", "the string to search");
pg.set("regex,r", "", "str", "the regular expression to use");
pg.set("icase,i", "character matching should be performed without regard to case");
pg.set("optimize,o", "instructs the regular expression engine to make matching faster, with the potential cost of making construction slower");
pg.set("ecmascript,e", "use the modified ECMAScript regular expression grammar");
pg.set("basic,b", "use the basic POSIX regular expression grammar");
pg.set("extended,x", "use the extended POSIX regular expression grammar");
pg.set("awk,a", "use the regular expression grammar used by the awk utility in POSIX");
pg.set("grep,g", "use the regular expression grammar used by the grep utility in POSIX");
pg.set("egrep,E", "use the regular expression grammar used by the grep utility, with the -E option, in POSIX");
int status {pg.parse()};
if (status < 0)
{
std::cout << pg.help() << "\n";
std::cout << "Error: " << pg.error() << "\n";
return -1;
}
if (pg.get<bool>("help"))
{
std::cout << pg.help();
return 1;
}
if (pg.get<bool>("version"))
{
std::cout << pg.name() << " v" << pg.version() << "\n";
return 1;
}
if (! pg.find("regex") || (! pg.find("string") && pg.get_stdin().empty()))
{
std::cout << pg.help() << "\n";
std::cout << "Error: " << "expected '-r' and '-s' options" << "\n";
return -1;
}
return 0;
}
std::regex_constants::syntax_option_type regex_options(Parg& pg)
{
std::regex_constants::syntax_option_type opts {};
if (pg.get<bool>("icase"))
{
opts |= std::regex::icase;
}
if (pg.get<bool>("optimize"))
{
opts |= std::regex::optimize;
}
if (pg.get<bool>("ecmascript"))
{
opts |= std::regex::ECMAScript;
}
else if (pg.get<bool>("basic"))
{
opts |= std::regex::basic;
}
else if (pg.get<bool>("extended"))
{
opts |= std::regex::extended;
}
else if (pg.get<bool>("awk"))
{
opts |= std::regex::awk;
}
else if (pg.get<bool>("grep"))
{
opts |= std::regex::grep;
}
else if (pg.get<bool>("egrep"))
{
opts |= std::regex::egrep;
}
else
{
opts |= std::regex::ECMAScript;
}
return opts;
}
std::regex_constants::match_flag_type regex_flags(Parg& pg)
{
std::regex_constants::match_flag_type flgs {std::regex_constants::match_not_null};
return flgs;
}
bool is_tty()
{
return isatty(STDOUT_FILENO);
}
std::string replace(std::string str, std::string key, std::string val)
{
size_t pos {0};
for (;;)
{
pos = str.find(key, pos);
if (pos == std::string::npos) break;
str.replace(pos, key.size(), val);
pos += val.size();
}
return str;
}
class Color_Ring
{
public:
Color_Ring(std::vector<std::string> colors):
_colors {colors}
{
}
std::string next()
{
auto res = get();
if (++_pos > _colors.size() - 1)
{
_pos = 0;
}
return res;
}
std::string get() const
{
return _colors.at(_pos);
}
Color_Ring& reset()
{
_pos = 0;
return *this;
}
private:
std::vector<std::string> const _colors;
size_t _pos {0};
};
struct Style
{
std::vector<std::string> h1 {aec::fg_white, aec::bold, aec::underline};
std::vector<std::string> h2 {aec::fg_green, aec::bold};
std::vector<std::string> h3 {aec::fg_magenta, aec::bold};
std::vector<std::string> num {aec::fg_cyan, aec::bold};
std::string match {aec::fg_green};
std::string plain {};
std::vector<std::string> special {aec::fg_white, aec::bold};
Color_Ring ring {{
aec::fg_magenta,
aec::fg_blue,
aec::fg_cyan,
aec::fg_green,
aec::fg_yellow,
aec::fg_red
}};
};
void regex_print(std::string const& regex, std::string const& text,
Crex::Matches const& matches, bool color)
{
if (color)
{
regex_print_color(regex, text, matches);
}
else
{
regex_print_no_color(regex, text, matches);
}
}
void regex_print_color(std::string const& regex, std::string const& text,
Crex::Matches const& matches)
{
Style style;
std::stringstream ss_head;
std::stringstream ss;
ss_head
<< aec::wrap("Regex", style.h1) << "\n"
<< regex
<< "\n\n"
<< aec::wrap("Text", style.h1) << "\n";
ss
<< "\n\n"
<< aec::wrap("Matches", style.h1) << "[" << aec::wrap(matches.size(), style.num) << "]\n";
size_t pos {0};
for (size_t i = 0; i < matches.size(); ++i)
{
style.ring.reset();
auto const& match = matches.at(i);
ss
<< aec::wrap("Match", style.h2) << "[" << aec::wrap(i + 1, style.num) << "] "
<< aec::wrap(match.at(0).second.first, style.num)
<< "-"
<< aec::wrap(match.at(0).second.second, style.num)
<< aec::wrap(" |", style.special);
std::string const& tmatch {match.at(0).first};
size_t const offset {match.at(0).second.first};
size_t prev {0};
size_t begin {0};
size_t end {0};
std::stringstream ss_text;
std::stringstream ss_match;
for (size_t j = 1; j < match.size(); ++j)
{
auto ftext = aec::wrap(match.at(j).first, style.ring.next());
begin = match.at(j).second.first - offset;
end = match.at(j).second.second - offset;
if (prev <= begin)
{
ss_text
<< aec::wrap(tmatch.substr(prev, begin - prev), style.plain);
}
ss_text
<< ftext;
prev = end + 1;
ss_match
<< aec::wrap("Group", style.h3) << "[" << aec::wrap(j, style.num) << "] "
<< aec::wrap(begin + offset, style.num)
<< "-"
<< aec::wrap(end + offset, style.num)
<< aec::wrap(" |", style.special)
<< ftext
<< aec::wrap("|\n", style.special);
}
if (match.size() < 2)
{
ss_text
<< aec::wrap(tmatch, style.plain);
}
else if (prev < tmatch.size())
{
ss_text
<< aec::wrap(tmatch.substr(prev, begin - prev), style.plain);
}
ss_match
<< "\n";
ss
<< ss_text.str()
<< aec::wrap("|\n", style.special)
<< ss_match.str();
if (pos < offset)
{
ss_head
<< aec::wrap(text.substr(pos, offset - pos), style.plain);
}
pos = offset + match.at(0).first.size() ;
ss_head
<< aec::wrap("(", style.special)
<< ss_text.str()
<< aec::wrap(")[", style.special)
<< aec::wrap(i + 1, style.num)
<< aec::wrap("]", style.special);
}
if (pos < text.size())
{
ss_head
<< aec::wrap(text.substr(pos, text.size() - pos), style.plain);
}
std::cout << ss_head.str() << ss.str();
}
void regex_print_no_color(std::string const& regex, std::string const& text,
Crex::Matches const& matches)
{
std::stringstream ss;
ss
<< "Regex\n"
<< regex
<< "\n\n"
<< "Text\n"
<< text
<< "\n\n"
<< "Matches[" << matches.size() << "]\n";
for (size_t i = 0; i < matches.size(); ++i)
{
auto const& match = matches.at(i);
ss
<< "Match[" << i + 1 << "] "
<< match.at(0).second.first << "-" << match.at(0).second.second
<< " |" << match.at(0).first << "|\n";
for (size_t j = 1; j < match.size(); ++j)
{
ss
<< "Group[" << j << "] "
<< match.at(j).second.first << "-" << match.at(j).second.second
<< " |" << match.at(j).first << "|\n";
}
ss
<< "\n";
}
std::cout << ss.str();
}
void regex_print_json(std::string& regex, std::string& text,
Crex::Matches const& matches)
{
auto const get_matches = [&]() {
std::stringstream ss;
for (size_t i = 0; i < matches.size(); ++i)
{
auto const& match = matches.at(i);
ss
<< "[";
for (size_t j = 0; j < match.size(); ++j)
{
auto mtext = match.at(j).first;
mtext = replace(mtext, "\"", "\\\"");
mtext = replace(mtext, "\\", "\\\\");
ss
<< "{"
<< "\"mtext\":\"" << mtext << "\","
<< "\"begin\":" << match.at(j).second.first << ","
<< "\"end\":" << match.at(j).second.second
<< "}";
if (j != match.size() - 1)
{
ss
<< ",";
}
}
ss
<< "]";
if (i != matches.size() - 1)
{
ss
<< ",";
}
}
return ss.str();
};
std::stringstream ss;
regex = replace(regex, "\"", "\\\"");
regex = replace(regex, "\\", "\\\\");
text = replace(text, "\"", "\\\"");
text = replace(text, "\\", "\\\\");
ss
<< "{"
<< "\"regex\":\"" << regex << "\","
<< "\"text\":\"" << text << "\","
<< "\"matches\":"
<< "["
<< get_matches()
<< "]}";
std::cout << ss.str();
}
int main(int argc, char *argv[])
{
Parg pg {argc, argv};
int pstatus {program_options(pg)};
if (pstatus > 0) return 0;
if (pstatus < 0) return 1;
try
{
std::string regex {pg.get("regex")};
std::string text {pg.get_stdin() + pg.get("string")};
if (regex.empty() || text.empty())
{
if (regex.empty())
{
throw std::runtime_error(
"regex is empty, expected '-r' option"
"\nView the help output with '-h'"
);
}
if (text.empty())
{
throw std::runtime_error(
"text is empty, expected '-s' option or piped stdin"
"\nView the help output with '-h'"
);
}
}
OB::Crex crex {};
crex.regex(regex);
crex.text(text);
crex.options(regex_options(pg));
crex.flags(regex_flags(pg));
if (! crex.run())
{
throw std::runtime_error("no matches found");
}
if (pg.get<bool>("json"))
{
regex_print_json(regex, text, crex.matches());
}
else
{
regex_print(regex, text, crex.matches(), pg.get<bool>("color"));
}
}
catch (std::exception const& e)
{
std::cerr << "Error: " << e.what() << "\n";
return 1;
}
catch (...)
{
std::cerr << "Error: an unexpected error occurred\n";
return 1;
}
return 0;
}