-
Notifications
You must be signed in to change notification settings - Fork 4
/
match.c
158 lines (150 loc) · 2.82 KB
/
match.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
151
152
153
154
155
156
157
158
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <regex.h>
#include "match.h"
struct matcher {
regex_t re;
};
void pat2ere(FILE *f, const char *s)
{
size_t i, j;
char *z = strchr(s, '/');
if (!z || !z[1]) {
// If no / or just final / to force directory-only matches,
// match in any directory (same as "**/" prefix).
fputs("/(.*/)?", f);
} else if (s[0]!='/') {
putc('/', f);
}
for (i=0; s[i]; i++) {
switch (s[i]) {
case '\\':
switch(s[++i]) {
case 0:
fputs("\\\\", f);
return;
case '*':
case '[':
case '?':
putc('\\', f);
default:
putc(s[i], f);
}
break;
case '/':
putc('/', f);
if (s[i+1]!='*' || s[i+2]!='*')
break;
i++;
case '*':
if (s[i+1]=='*' && (!s[i+2] || s[i+2]=='/') && (!i || s[i-1]=='/')) {
if (!i) putc('/', f);
fputs("(.*", f);
if (s[i+2]) {
fputs("/)?", f);
i++;
} else {
fputs(")?.", f);
}
i++;
} else {
fputs("[^/]*", f);
}
break;
case '?':
fputs("[^/]", f);
break;
case '[':
j=i+1;
if (s[j]=='^' || s[j]=='!') j++;
if (s[j]==']') j++;
for (; s[j] && s[j]!=']'; j++) {
if (s[j]=='[' && (s[j+1]==':' || s[j+1]=='.' || s[j+1]=='=')) {
int c = s[++j];
while (s[j+1] && s[j+2] && (s[j+1]!=c || s[j+2]!=']')) j++;
}
}
if (s[j]) {
putc('[', f);
if (s[i+1]=='!') putc('^', f);
else putc(s[i+1], f);
for (i=i+2; i<j; i++)
putc(s[i], f);
putc(']', f);
break;
}
case '.':
case '(':
case '+':
case '{':
case '|':
case '^':
case '$':
putc('\\', f);
default:
putc(s[i], f);
break;
}
}
}
struct matcher *matcher_from_file(FILE *f)
{
struct matcher *m = calloc(1, sizeof *m);
if (!m) return 0;
char *re_buf;
size_t re_len;
FILE *re_f = open_memstream(&re_buf, &re_len);
char *in_buf = 0;
size_t in_len = 0;
int first = 1;
ssize_t l;
while ((l=getline(&in_buf, &in_len, f)) > 0) {
if (in_buf[l-1]=='\n') {
in_buf[--l] = 0;
}
if (!l) continue;
if (first) {
fputs("^(", re_f);
first = 0;
} else {
putc('|', re_f);
}
pat2ere(re_f, in_buf);
// Directory names for matching will be passed with a final
// slash. Adding "/?" to end of regex allows patterns not
// ending in / to match either file or directory.
fputs("/?", re_f);
}
if (first) {
// If there were no patterns,
fputs("$ ^", re_f);
} else {
fputs(")$", re_f);
}
fflush(re_f);
if (ferror(f) || ferror(re_f)) {
fclose(re_f);
return 0;
}
fclose(re_f);
//fprintf(stderr, "%s\n", re_buf);
if (regcomp(&m->re, re_buf, REG_EXTENDED|REG_NOSUB)) {
return 0;
}
return m;
}
int matcher_matches(struct matcher *m, const char *s)
{
if (!m) return 0;
int r = regexec(&m->re, s, 0, 0, 0);
if (r==0) return 1;
if (r==REG_NOMATCH) return 0;
return -1;
}
#if 0
int main()
{
matcher_from_file(stdin);
}
#endif