-
Notifications
You must be signed in to change notification settings - Fork 6
/
match.c
331 lines (305 loc) · 6.88 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#define ASTERISK '*' /* The '*' metacharacter */
#define QUESTION '?' /* The '?' metacharacter */
#define LEFT_BRACKET '[' /* The '[' metacharacter */
#define RIGHT_BRACKET ']' /* The ']' metacharacter */
#define IS_OCTAL(ch) (ch >= '0' && ch <= '7')
/* Filenames in BACKUP savesets can contain only these uppercase
letters, I think. */
#define IS_UC(ch) (ch >= 'A' && ch <= 'Z')
typedef int BOOLEAN;
#define VOID void
#define TRUE 1
#define FALSE 0
#define EOS '\000'
static BOOLEAN do_list ();
static char nextch ();
static VOID list_parse ();
/*
* FUNCTION
*
* match test string for wildcard match
*
* SYNOPSIS
*
* BOOLEAN match (string, pattern)
* register char *string;
* register char *pattern;
*
* DESCRIPTION
*
* Test string for match using pattern. The pattern may
* contain the normal shell metacharacters for pattern
* matching. The '*' character matches any string,
* including the null string. The '?' character matches
* any single character. A list of characters enclosed
* in '[' and ']' matches any character in the list.
* If the first character following the beginning '['
* is a '!' then any character not in the list is matched.
*
*/
/*
* PSEUDO CODE
*
* Begin match
* Switch on type of pattern character
* Case ASTERISK:
* Attempt to match asterisk
* Break
* Case QUESTION MARK:
* Attempt to match question mark
* Break
* Case EOS:
* Match is result of EOS on string test
* Break
* Case default:
* If explicit match then
* Match is result of submatch
* Else
* Match is FALSE
* End if
* Break
* End switch
* Return result of match test
* End match
*
*/
BOOLEAN match (string, pattern)
register char *string;
register char *pattern;
{
register BOOLEAN ismatch;
ismatch = FALSE;
switch (*pattern) {
case ASTERISK:
pattern++;
do {
ismatch = match (string, pattern);
} while (!ismatch && *string++ != EOS);
break;
case QUESTION:
if (*string != EOS) {
ismatch = match (++string, ++pattern);
}
break;
case EOS:
if (*string == EOS) {
ismatch = TRUE;
}
break;
case LEFT_BRACKET:
if (*string != EOS) {
ismatch = do_list (string, pattern);
}
break;
default:
if (*string++ == *pattern++) {
ismatch = match (string, pattern);
} else {
ismatch = FALSE;
}
break;
}
return (ismatch);
}
/*
* FUNCTION
*
* do_list process a list and following substring
*
* SYNOPSIS
*
* static BOOLEAN do_list (string, pattern)
* register char *string;
* register char *pattern;
*
* DESCRIPTION
*
* Called when a list is found in the pattern. Returns
* TRUE if the current character matches the list and
* the remaining substring matches the remaining pattern.
*
* Returns FALSE if either the current character fails to
* match the list or the list matches but the remaining
* substring and subpattern's don't.
*
* RESTRICTIONS
*
* The mechanism used to match characters in an inclusive
* pair (I.E. [a-d]) may not be portable to machines
* in which the native character set is not ASCII.
*
* The rules implemented here are:
*
* (1) The backslash character may be
* used to quote any special character.
* I.E. "\]" and "\-" anywhere in list,
* or "\!" at start of list.
*
* (2) The sequence \nnn becomes the character
* given by nnn (in octal).
*
* (3) Any non-escaped ']' marks the end of list.
*
* (4) A list beginning with the special character
* '!' matches any character NOT in list.
* The '!' character is only special if it
* is the first character in the list.
*
*/
/*
* PSEUDO CODE
*
* Begin do_list
* Default result is no match
* Skip over the opening left bracket
* If the next pattern character is a '!' then
* List match gives FALSE
* Skip over the '!' character
* Else
* List match gives TRUE
* End if
* While not at closing bracket or EOS
* Get lower and upper bounds
* If character in bounds then
* Result is same as sense flag.
* Skip over rest of list
* End if
* End while
* If match found then
* If not at end of pattern then
* Call match with rest of pattern
* End if
* End if
* Return match result
* End do_list
*
*/
static BOOLEAN do_list (string, pattern)
register char *string;
char *pattern;
{
register BOOLEAN ismatch;
register BOOLEAN if_found;
register BOOLEAN if_not_found;
auto char lower;
auto char upper;
pattern++;
if (*pattern == '!') {
if_found = FALSE;
if_not_found = TRUE;
pattern++;
} else {
if_found = TRUE;
if_not_found = FALSE;
}
ismatch = if_not_found;
while (*pattern != ']' && *pattern != EOS) {
list_parse (&pattern, &lower, &upper);
if (*string >= lower && *string <= upper) {
ismatch = if_found;
while (*pattern != ']' && *pattern != EOS) {pattern++;}
}
}
if (*pattern++ != ']') {
fprintf (stderr, "warning - character class error\n");
} else {
if (ismatch) {
ismatch = match (++string, pattern);
}
}
return (ismatch);
}
/*
* FUNCTION
*
* list_parse parse part of list into lower and upper bounds
*
* SYNOPSIS
*
* static VOID list_parse (patp, lowp, highp)
* char **patp;
* char *lowp;
* char *highp;
*
* DESCRIPTION
*
* Given pointer to a pattern pointer (patp), pointer to
* a place to store lower bound (lowp), and pointer to a
* place to store upper bound (highp), parses part of
* the list, updating the pattern pointer in the process.
*
* For list characters which are not part of a range,
* the lower and upper bounds are set to that character.
*
*/
static VOID list_parse (patp, lowp, highp)
char **patp;
char *lowp;
char *highp;
{
*lowp = nextch (patp);
if (**patp == '-') {
(*patp)++;
*highp = nextch (patp);
} else {
*highp = *lowp;
}
}
/*
* FUNCTION
*
* nextch determine next character in a pattern
*
* SYNOPSIS
*
* static char nextch (patp)
* char **patp;
*
* DESCRIPTION
*
* Given pointer to a pointer to a pattern, uses the pattern
* pointer to determine the next character in the pattern,
* subject to translation of backslash-char and backslash-octal
* sequences.
*
* The character pointer is updated to point at the next pattern
* character to be processed.
*
*/
static char nextch (patp)
char **patp;
{
register char ch;
register char chsum;
register int count;
ch = *(*patp)++;
if (ch == '\\') {
ch = *(*patp)++;
if (IS_OCTAL (ch)) {
chsum = 0;
for (count = 0; count < 3 && IS_OCTAL (ch); count++) {
chsum *= 8;
chsum += ch - '0';
ch = *(*patp)++;
}
(*patp)--;
ch = chsum;
}
}
return (ch);
}
char *
strlocase(str)
char *str;
{
int i;
for (i = 0; i < strlen(str); i++) {
if (IS_UC (str[i])) {
str[i] = str[i] - 'A' + 'a';
}
}
return (str);
}