-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.c
150 lines (138 loc) · 3.05 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
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
// Copyright 2007-2009 Russ Cox. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include "re1.5.h"
struct {
char *name;
int (*fn)(ByteProg*, Subject*, const char**, int, int);
} tab[] = {
{"recursive", re1_5_recursiveprog},
{"recursiveloop", re1_5_recursiveloopprog},
{"backtrack", re1_5_backtrack},
{"thompson", re1_5_thompsonvm},
{"pike", re1_5_pikevm},
};
#ifdef DEBUG
int debug;
#endif
const char *re_engine;
void
usage(void)
{
fprintf(stderr, "Usage: re [-hmd] [-e ENGINE] <regexp> <string>...\n"
"-h: Print help message and exit\n"
"-m: String is anchored\n"
"-e ENGINE: Specify one of: recursive recursiveloop backtrack thompson pike\n");
#ifdef DEBUG
fprintf(stderr,
"-d: Print debug messages\n");
#endif
exit(2);
}
int
main(int argc, char **argv)
{
int i, j, k, l;
int is_anchored = 0;
argv++;
argc--;
while (argc > 0 && argv[0][0] == '-') {
char *arg;
for (arg = &argv[0][1]; *arg; arg++) {
switch (*arg) {
case 'h':
usage();
break;
case 'm':
is_anchored = 1;
break;
#ifdef DEBUG
case 'd':
debug = 1;
break;
#endif
case 'e':
if (argv[1] == NULL)
re1_5_fatal("-e: Missing Regex engine argument");
if (re_engine)
re1_5_fatal("-e: Regex engine already specified");
re_engine = argv[1];
argv++;
argc--;
break;
default:
re1_5_fatal("Unknown flag");
}
}
argv++;
argc--;
}
if(argc < 2)
usage();
#ifdef ODEBUG
// Old and unmaintained code
Regexp *re = parse(argv[0]);
printre(re);
printf("\n");
Prog *prog = compile(re);
printprog(prog);
printf("=============\n");
#endif
int sz = re1_5_sizecode(argv[0]);
#ifdef DEBUG
if (debug) printf("Precalculated size: %d\n", sz);
#endif
if (sz == -1) {
re1_5_fatal("Error in regexp");
}
ByteProg *code = malloc(sizeof(ByteProg) + sz);
int ret = re1_5_compilecode(code, argv[0]);
if (ret != 0) {
re1_5_fatal("Error in regexp");
}
int sub_els = (code->sub + 1) * 2;
#ifdef DEBUG
if (debug) re1_5_dumpcode(code);
#endif
const char *sub[sub_els];
int engine_found = 0;
for(i=1; i<argc; i++) {
printf("#%d %s\n", i, argv[i]);
for(j=0; j<nelem(tab); j++) {
Subject subj = {argv[i], argv[i] + strlen(argv[i])};
if (re_engine) {
if (0 != strcmp(re_engine, tab[j].name))
continue;
engine_found = 1;
}
printf("%s ", tab[j].name);
memset(sub, 0, sub_els * sizeof sub[0]);
if(!tab[j].fn(code, &subj, sub, sub_els, is_anchored)) {
printf("-no match-\n");
continue;
}
printf("match");
for(k=sub_els; k>0; k--)
if(sub[k-1])
break;
for(l=0; l<k; l+=2) {
printf(" (");
if(sub[l] == nil)
printf("?");
else
printf("%d", (int)(sub[l] - argv[i]));
printf(",");
if(sub[l+1] == nil)
printf("?");
else
printf("%d", (int)(sub[l+1] - argv[i]));
printf(")");
}
printf("\n");
}
if (re_engine && !engine_found)
re1_5_fatal("-e: Unknown engine name");
}
free(code);
return 0;
}