-
Notifications
You must be signed in to change notification settings - Fork 47
/
match.c
195 lines (173 loc) · 3.53 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
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
#include <string.h>
#include <ctype.h>
#include "match.h"
int match(char *sorig, char *p)
{
return matchBody(sorig, p, 0);
}
int matchNoCase(char *sorig, char *p)
{
return matchBody(sorig, p, 1);
}
#define CASE(x) (nocase ? tolower(x) : (x))
int matchBody(char *sorig, char *p, int nocase)
{
static int dummy = 0;
/* Algorithm:
Word separator: *. End-of-string
is considered to be a word constituent.
? is similarly considered to be a specialized
word constituent.
Match the word to the current position in s.
Empty words automatically succeed.
If the word matches s, and the word
and s contain end-of-string at that
point, return success.
\ escapes the next character, including \ itself (6.0).
For each *:
Find the next occurrence of the next word
and advance beyond it in both p and s.
If the next word ends in end-of-string
and is found successfully, return success,
otherwise advance past the *.
If the word is not found, return failure.
If the next word is empty, advance past the *.
Behavior of ?: advance one character in s and p.
Addendum: consider the | character to be a logical OR
separating distinct patterns. */
char *s = sorig;
int escaped = 0;
if (strstr(p, "WS-0000")) {
if (strstr(s, "ws_ftp_pro.html")) {
dummy = 1;
}
}
while (1) {
char *word;
int wordLen;
int wordPos;
if (escaped) {
/* This is like the default case,
except that | doesn't end the pattern. */
escaped = 0;
if ((*s == '\0') && (*p == '\0')) {
return 1;
}
if (CASE(*p) != CASE(*s)) {
goto nextPattern;
}
p++;
s++;
continue;
}
switch(*p) {
case '\\':
/* Escape the next character. */
escaped = 1;
p++;
continue;
case '*':
/* Find the next occurrence of the next word
and advance beyond it in both p and s.
If the next word ends in end-of-string
and is found successfully, return success,
otherwise advance past the *.
If the word is not found, return failure.
If the next word is empty, advance. */
p++;
wordLen = 0;
word = p;
while (1) {
if ((*p) == '*') {
break;
}
wordLen++;
if ((*p == '\0') || (*p == '|')) {
break;
}
p++;
}
wordPos = 0;
while (1) {
if (wordPos == wordLen) {
if ((*p == '\0') || (*p == '|')) {
return 1;
}
break;
}
if ((((CASE(*s)) == CASE(word[wordPos])) ||
((*s == '\0') &&
(word[wordPos] == '|'))) ||
(((*s != '\0') && (*s != '|')) &&
(word[wordPos] == '?')))
{
wordPos++;
s++;
} else {
s -= wordPos;
if (!(*s)) {
goto nextPattern;
}
s++;
wordPos = 0;
}
}
break;
case '?':
p++;
s++;
break;
default:
if ((*s == '\0') && ((*p == '\0') ||
(*p == '|'))) {
return 1;
}
if (CASE(*p) != CASE(*s)) {
goto nextPattern;
}
p++;
s++;
break;
}
continue;
nextPattern:
while (1) {
if (*p == '\0') {
return 0;
}
if (*p == '|') {
p++;
s = sorig;
break;
}
p++;
}
}
}
#ifdef TEST_MATCH
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, char *argv[])
{
char s[1024];
if (argc != 2) {
fprintf(stderr, "Usage: match pattern\n");
return 1;
}
while (1) {
if (!fgets(s, sizeof(s), stdin)) {
break;
}
while (isspace(s[strlen(s) - 1])) {
s[strlen(s) - 1] = '\0';
}
printf("%s --> %s\n", s, argv[1]);
if (match(s, argv[1])) {
printf("Match\n");
} else {
printf("No Match\n");
}
}
}
#endif /* TEST_MATCH */