Blame


1 8907c8dd 2002-06-26 alex /*
2 8907c8dd 2002-06-26 alex * ngIRCd -- The Next Generation IRC Daemon
3 8907c8dd 2002-06-26 alex * Copyright (c)2001,2002 by Alexander Barton (alex@barton.de)
4 8907c8dd 2002-06-26 alex *
5 490f28ff 2002-12-12 alex * This program is free software; you can redistribute it and/or modify
6 490f28ff 2002-12-12 alex * it under the terms of the GNU General Public License as published by
7 490f28ff 2002-12-12 alex * the Free Software Foundation; either version 2 of the License, or
8 490f28ff 2002-12-12 alex * (at your option) any later version.
9 490f28ff 2002-12-12 alex * Please read the file COPYING, README and AUTHORS for more information.
10 8907c8dd 2002-06-26 alex *
11 490f28ff 2002-12-12 alex * Wildcard pattern matching
12 8907c8dd 2002-06-26 alex */
13 8907c8dd 2002-06-26 alex
14 8907c8dd 2002-06-26 alex
15 8907c8dd 2002-06-26 alex #include "portab.h"
16 8907c8dd 2002-06-26 alex
17 490f28ff 2002-12-12 alex static char UNUSED id[] = "$Id: match.c,v 1.2 2002/12/12 12:24:18 alex Exp $";
18 490f28ff 2002-12-12 alex
19 8907c8dd 2002-06-26 alex #include "imp.h"
20 8907c8dd 2002-06-26 alex #include <assert.h>
21 8907c8dd 2002-06-26 alex #include <string.h>
22 8907c8dd 2002-06-26 alex
23 8907c8dd 2002-06-26 alex #include "exp.h"
24 8907c8dd 2002-06-26 alex #include "match.h"
25 8907c8dd 2002-06-26 alex
26 8907c8dd 2002-06-26 alex
27 8907c8dd 2002-06-26 alex /*
28 8907c8dd 2002-06-26 alex * Die Pattern-Matching-Funkionen [Matche(), Matche_After_Star()] basieren
29 8907c8dd 2002-06-26 alex * auf Versionen von J. Kercheval. Die Version 1.1 wurde am 12.03.1991 als
30 8907c8dd 2002-06-26 alex * "public domain" freigegeben:
31 8907c8dd 2002-06-26 alex * <http://www.snippets.org/snippets/portable/MATCH+C.php3>
32 8907c8dd 2002-06-26 alex */
33 8907c8dd 2002-06-26 alex
34 8907c8dd 2002-06-26 alex
35 8907c8dd 2002-06-26 alex LOCAL INT Matche PARAMS(( REGISTER CHAR *p, REGISTER CHAR *t ));
36 8907c8dd 2002-06-26 alex LOCAL INT Matche_After_Star PARAMS(( REGISTER CHAR *p, REGISTER CHAR *t ));
37 8907c8dd 2002-06-26 alex
38 8907c8dd 2002-06-26 alex
39 8907c8dd 2002-06-26 alex #define MATCH_PATTERN 6 /* bad pattern */
40 8907c8dd 2002-06-26 alex #define MATCH_LITERAL 5 /* match failure on literal match */
41 8907c8dd 2002-06-26 alex #define MATCH_RANGE 4 /* match failure on [..] construct */
42 8907c8dd 2002-06-26 alex #define MATCH_ABORT 3 /* premature end of text string */
43 8907c8dd 2002-06-26 alex #define MATCH_END 2 /* premature end of pattern string */
44 8907c8dd 2002-06-26 alex #define MATCH_VALID 1 /* valid match */
45 8907c8dd 2002-06-26 alex
46 8907c8dd 2002-06-26 alex
47 8907c8dd 2002-06-26 alex GLOBAL BOOLEAN
48 8907c8dd 2002-06-26 alex Match( CHAR *Pattern, CHAR *String )
49 8907c8dd 2002-06-26 alex {
50 8907c8dd 2002-06-26 alex /* Pattern mit String vergleichen */
51 8907c8dd 2002-06-26 alex if( Matche( Pattern, String ) == MATCH_VALID ) return TRUE;
52 8907c8dd 2002-06-26 alex else return FALSE;
53 8907c8dd 2002-06-26 alex } /* Match */
54 8907c8dd 2002-06-26 alex
55 8907c8dd 2002-06-26 alex
56 8907c8dd 2002-06-26 alex LOCAL INT
57 8907c8dd 2002-06-26 alex Matche( REGISTER CHAR *p, REGISTER CHAR *t )
58 8907c8dd 2002-06-26 alex {
59 8907c8dd 2002-06-26 alex REGISTER CHAR range_start, range_end;
60 8907c8dd 2002-06-26 alex BOOLEAN invert;
61 8907c8dd 2002-06-26 alex BOOLEAN member_match;
62 8907c8dd 2002-06-26 alex BOOLEAN loop;
63 8907c8dd 2002-06-26 alex
64 8907c8dd 2002-06-26 alex for( ; *p; p++, t++ )
65 8907c8dd 2002-06-26 alex {
66 8907c8dd 2002-06-26 alex /* if this is the end of the text then this is the end of the match */
67 8907c8dd 2002-06-26 alex if( ! *t )
68 8907c8dd 2002-06-26 alex {
69 8907c8dd 2002-06-26 alex return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
70 8907c8dd 2002-06-26 alex }
71 8907c8dd 2002-06-26 alex
72 8907c8dd 2002-06-26 alex /* determine and react to pattern type */
73 8907c8dd 2002-06-26 alex switch( *p )
74 8907c8dd 2002-06-26 alex {
75 8907c8dd 2002-06-26 alex case '?': /* single any character match */
76 8907c8dd 2002-06-26 alex break;
77 8907c8dd 2002-06-26 alex
78 8907c8dd 2002-06-26 alex case '*': /* multiple any character match */
79 8907c8dd 2002-06-26 alex return Matche_After_Star( p, t );
80 8907c8dd 2002-06-26 alex
81 8907c8dd 2002-06-26 alex case '[': /* [..] construct, single member/exclusion character match */
82 8907c8dd 2002-06-26 alex /* move to beginning of range */
83 8907c8dd 2002-06-26 alex p++;
84 8907c8dd 2002-06-26 alex
85 8907c8dd 2002-06-26 alex /* check if this is a member match or exclusion match */
86 8907c8dd 2002-06-26 alex invert = FALSE;
87 8907c8dd 2002-06-26 alex if( *p == '!' || *p == '^' )
88 8907c8dd 2002-06-26 alex {
89 8907c8dd 2002-06-26 alex invert = TRUE;
90 8907c8dd 2002-06-26 alex p++;
91 8907c8dd 2002-06-26 alex }
92 8907c8dd 2002-06-26 alex
93 8907c8dd 2002-06-26 alex /* if closing bracket here or at range start then we have a malformed pattern */
94 8907c8dd 2002-06-26 alex if ( *p == ']' ) return MATCH_PATTERN;
95 8907c8dd 2002-06-26 alex
96 8907c8dd 2002-06-26 alex member_match = FALSE;
97 8907c8dd 2002-06-26 alex loop = TRUE;
98 8907c8dd 2002-06-26 alex
99 8907c8dd 2002-06-26 alex while( loop )
100 8907c8dd 2002-06-26 alex {
101 8907c8dd 2002-06-26 alex /* if end of construct then loop is done */
102 8907c8dd 2002-06-26 alex if( *p == ']' )
103 8907c8dd 2002-06-26 alex {
104 8907c8dd 2002-06-26 alex loop = FALSE;
105 8907c8dd 2002-06-26 alex continue;
106 8907c8dd 2002-06-26 alex }
107 8907c8dd 2002-06-26 alex
108 8907c8dd 2002-06-26 alex /* matching a '!', '^', '-', '\' or a ']' */
109 8907c8dd 2002-06-26 alex if( *p == '\\' ) range_start = range_end = *++p;
110 8907c8dd 2002-06-26 alex else range_start = range_end = *p;
111 8907c8dd 2002-06-26 alex
112 8907c8dd 2002-06-26 alex /* if end of pattern then bad pattern (Missing ']') */
113 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
114 8907c8dd 2002-06-26 alex
115 8907c8dd 2002-06-26 alex /* check for range bar */
116 8907c8dd 2002-06-26 alex if( *++p == '-' )
117 8907c8dd 2002-06-26 alex {
118 8907c8dd 2002-06-26 alex /* get the range end */
119 8907c8dd 2002-06-26 alex range_end = *++p;
120 8907c8dd 2002-06-26 alex
121 8907c8dd 2002-06-26 alex /* if end of pattern or construct then bad pattern */
122 8907c8dd 2002-06-26 alex if( range_end == '\0' || range_end == ']' ) return MATCH_PATTERN;
123 8907c8dd 2002-06-26 alex
124 8907c8dd 2002-06-26 alex /* special character range end */
125 8907c8dd 2002-06-26 alex if( range_end == '\\' )
126 8907c8dd 2002-06-26 alex {
127 8907c8dd 2002-06-26 alex range_end = *++p;
128 8907c8dd 2002-06-26 alex
129 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
130 8907c8dd 2002-06-26 alex if ( ! range_end ) return MATCH_PATTERN;
131 8907c8dd 2002-06-26 alex }
132 8907c8dd 2002-06-26 alex
133 8907c8dd 2002-06-26 alex /* move just beyond this range */
134 8907c8dd 2002-06-26 alex p++;
135 8907c8dd 2002-06-26 alex }
136 8907c8dd 2002-06-26 alex
137 8907c8dd 2002-06-26 alex /* if the text character is in range then match found. make sure the range
138 8907c8dd 2002-06-26 alex * letters have the proper relationship to one another before comparison */
139 8907c8dd 2002-06-26 alex if( range_start < range_end )
140 8907c8dd 2002-06-26 alex {
141 8907c8dd 2002-06-26 alex if( *t >= range_start && *t <= range_end )
142 8907c8dd 2002-06-26 alex {
143 8907c8dd 2002-06-26 alex member_match = TRUE;
144 8907c8dd 2002-06-26 alex loop = FALSE;
145 8907c8dd 2002-06-26 alex }
146 8907c8dd 2002-06-26 alex }
147 8907c8dd 2002-06-26 alex else
148 8907c8dd 2002-06-26 alex {
149 8907c8dd 2002-06-26 alex if( *t >= range_end && *t <= range_start )
150 8907c8dd 2002-06-26 alex {
151 8907c8dd 2002-06-26 alex member_match = TRUE;
152 8907c8dd 2002-06-26 alex loop = FALSE;
153 8907c8dd 2002-06-26 alex }
154 8907c8dd 2002-06-26 alex }
155 8907c8dd 2002-06-26 alex }
156 8907c8dd 2002-06-26 alex
157 8907c8dd 2002-06-26 alex /* if there was a match in an exclusion set then no match */
158 8907c8dd 2002-06-26 alex /* if there was no match in a member set then no match */
159 8907c8dd 2002-06-26 alex if(( invert && member_match ) || ! ( invert || member_match )) return MATCH_RANGE;
160 8907c8dd 2002-06-26 alex
161 8907c8dd 2002-06-26 alex /* if this is not an exclusion then skip the rest of the [...]
162 8907c8dd 2002-06-26 alex * construct that already matched. */
163 8907c8dd 2002-06-26 alex if( member_match )
164 8907c8dd 2002-06-26 alex {
165 8907c8dd 2002-06-26 alex while( *p != ']' )
166 8907c8dd 2002-06-26 alex {
167 8907c8dd 2002-06-26 alex /* bad pattern (Missing ']') */
168 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
169 8907c8dd 2002-06-26 alex
170 8907c8dd 2002-06-26 alex /* skip exact match */
171 8907c8dd 2002-06-26 alex if( *p == '\\' )
172 8907c8dd 2002-06-26 alex {
173 8907c8dd 2002-06-26 alex p++;
174 8907c8dd 2002-06-26 alex
175 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
176 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
177 8907c8dd 2002-06-26 alex }
178 8907c8dd 2002-06-26 alex
179 8907c8dd 2002-06-26 alex /* move to next pattern char */
180 8907c8dd 2002-06-26 alex p++;
181 8907c8dd 2002-06-26 alex }
182 8907c8dd 2002-06-26 alex }
183 8907c8dd 2002-06-26 alex break;
184 8907c8dd 2002-06-26 alex case '\\': /* next character is quoted and must match exactly */
185 8907c8dd 2002-06-26 alex /* move pattern pointer to quoted char and fall through */
186 8907c8dd 2002-06-26 alex p++;
187 8907c8dd 2002-06-26 alex
188 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
189 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
190 8907c8dd 2002-06-26 alex
191 8907c8dd 2002-06-26 alex /* must match this character exactly */
192 8907c8dd 2002-06-26 alex default:
193 8907c8dd 2002-06-26 alex if( *p != *t ) return MATCH_LITERAL;
194 8907c8dd 2002-06-26 alex }
195 8907c8dd 2002-06-26 alex }
196 8907c8dd 2002-06-26 alex /* if end of text not reached then the pattern fails */
197 8907c8dd 2002-06-26 alex
198 8907c8dd 2002-06-26 alex if( *t ) return MATCH_END;
199 8907c8dd 2002-06-26 alex else return MATCH_VALID;
200 8907c8dd 2002-06-26 alex } /* Matche */
201 8907c8dd 2002-06-26 alex
202 8907c8dd 2002-06-26 alex
203 8907c8dd 2002-06-26 alex LOCAL INT
204 8907c8dd 2002-06-26 alex Matche_After_Star( REGISTER CHAR *p, REGISTER CHAR *t )
205 8907c8dd 2002-06-26 alex {
206 8907c8dd 2002-06-26 alex REGISTER INT nextp, match = 0;
207 8907c8dd 2002-06-26 alex
208 8907c8dd 2002-06-26 alex /* pass over existing ? and * in pattern */
209 8907c8dd 2002-06-26 alex while( *p == '?' || *p == '*' )
210 8907c8dd 2002-06-26 alex {
211 8907c8dd 2002-06-26 alex /* take one char for each ? and + */
212 8907c8dd 2002-06-26 alex if (*p == '?')
213 8907c8dd 2002-06-26 alex {
214 8907c8dd 2002-06-26 alex /* if end of text then no match */
215 8907c8dd 2002-06-26 alex if( ! *t++ ) return MATCH_ABORT;
216 8907c8dd 2002-06-26 alex }
217 8907c8dd 2002-06-26 alex
218 8907c8dd 2002-06-26 alex /* move to next char in pattern */
219 8907c8dd 2002-06-26 alex p++;
220 8907c8dd 2002-06-26 alex }
221 8907c8dd 2002-06-26 alex
222 8907c8dd 2002-06-26 alex /* if end of pattern we have matched regardless of text left */
223 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_VALID;
224 8907c8dd 2002-06-26 alex
225 8907c8dd 2002-06-26 alex /* get the next character to match which must be a literal or '[' */
226 8907c8dd 2002-06-26 alex nextp = *p;
227 8907c8dd 2002-06-26 alex if( nextp == '\\' )
228 8907c8dd 2002-06-26 alex {
229 8907c8dd 2002-06-26 alex nextp = p[1];
230 8907c8dd 2002-06-26 alex
231 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
232 8907c8dd 2002-06-26 alex if( ! nextp ) return MATCH_PATTERN;
233 8907c8dd 2002-06-26 alex }
234 8907c8dd 2002-06-26 alex
235 8907c8dd 2002-06-26 alex /* Continue until we run out of text or definite result seen */
236 8907c8dd 2002-06-26 alex do
237 8907c8dd 2002-06-26 alex {
238 8907c8dd 2002-06-26 alex /* a precondition for matching is that the next character
239 8907c8dd 2002-06-26 alex * in the pattern match the next character in the text or that
240 8907c8dd 2002-06-26 alex * the next pattern char is the beginning of a range. Increment
241 8907c8dd 2002-06-26 alex * text pointer as we go here */
242 8907c8dd 2002-06-26 alex if( nextp == *t || nextp == '[' ) match = Matche( p, t );
243 8907c8dd 2002-06-26 alex
244 8907c8dd 2002-06-26 alex /* if the end of text is reached then no match */
245 8907c8dd 2002-06-26 alex if( ! *t++ ) match = MATCH_ABORT;
246 8907c8dd 2002-06-26 alex } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
247 8907c8dd 2002-06-26 alex
248 8907c8dd 2002-06-26 alex /* return result */
249 8907c8dd 2002-06-26 alex return match;
250 8907c8dd 2002-06-26 alex } /* Matche_After_Star */
251 8907c8dd 2002-06-26 alex
252 8907c8dd 2002-06-26 alex
253 8907c8dd 2002-06-26 alex /* -eof- */