-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.c
executable file
·75 lines (69 loc) · 1.68 KB
/
main.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
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
/* regex tester program
*
* Use the command "NEW" to enter a new regular expression.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <unistd.h>
#include "shre.h"
#include "shre_errno.h"
#define BUFFER 12000
char buf[BUFFER];
pattern_t* shre;
match_t* match;
void trim(char* str) {
if (*str != '\0') {
while (*(++str) != '\0');
if (*(str-1) == '\n') {
*(str-1) = '\0';
}
}
}
void do_test() {
Start:
if (isatty(fileno(stdin)))
printf("Enter a regular expression:\n\n");
if (fgets(buf, BUFFER, stdin) == NULL)
return;
trim(buf);
if ((shre = shre_compile(buf)) == NULL) {
printf("error: %s\n", shre_strerror(shre_er));
goto Start;
}
if (isatty(fileno(stdin)))
printf("\nEnter text to test the regular expression:\n\n");
while (fgets(buf, BUFFER, stdin) != NULL) {
trim(buf);
if (strcmp(buf, "NEW") == 0) {
goto Start;
}
printf("Pattern: '%s'\n", shre_expression(shre));
printf("String: '%s'\n", buf);
printf("Match: ");
if ((match = shre_search(shre, buf)) == NULL) {
printf(" None\n\n");
} else {
printf("'%s'\n", match_get(match));
for (int i = 1; i < match_num_groups(match); ++i) {
printf("Group %2d: ", i);
char* str = match_group(match, i);
if (!str) {
printf(" NULL\n");
} else {
printf("'%s'\n", str);
free(str);
}
}
printf("\n");
match_free(match);
}
}
}
int main() {
start_regex_engine();
do_test();
cleanup_regex_engine();
return 0;
}