-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsandbox.c
48 lines (39 loc) · 1.01 KB
/
sandbox.c
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
#include <stdio.h>
#include <stdlib.h>
#include "mregexp.h"
char *readline(const char *prompt)
{
printf("%s", prompt);
char *ret = NULL;
size_t retsz = 0;
do {
if (retsz > 0 && ret[retsz - 1] == '\n') {
ret[retsz - 1] = 0;
break;
}
ret = (char *) realloc(ret, (++retsz));
} while (fread(ret + retsz - 1, 1, 1, stdin));
return ret;
}
int main(void)
{
char *raw_re = readline("Enter regular expression > ");
char *text = readline("Enter text > ");
MRegexp *re = mregexp_compile(raw_re);
if (mregexp_error() || re == NULL) {
printf("Invalid regular expression: Compile failed with error %d\n",
mregexp_error());
return EXIT_FAILURE;
}
MRegexpMatch m;
if (mregexp_match(re, text, &m)) {
fwrite(text, 1, m.match_begin, stdout);
printf("%c[31;1m", 27);
fwrite(text + m.match_begin, 1, m.match_end - m.match_begin, stdout);
printf("%c[0m", 27);
printf("%s\n", text + m.match_end);
} else {
puts("No match :c");
}
return 0;
}