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 61966a60 2006-10-06 fw static char UNUSED id[] = "$Id: match.c,v 1.5 2006/10/06 21:23:47 fw 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 d4eb55c7 2008-07-26 alex #include "defines.h"
26 d4eb55c7 2008-07-26 alex #include "tool.h"
27 8907c8dd 2002-06-26 alex
28 8907c8dd 2002-06-26 alex
29 8907c8dd 2002-06-26 alex /*
30 8907c8dd 2002-06-26 alex * Die Pattern-Matching-Funkionen [Matche(), Matche_After_Star()] basieren
31 8907c8dd 2002-06-26 alex * auf Versionen von J. Kercheval. Die Version 1.1 wurde am 12.03.1991 als
32 8907c8dd 2002-06-26 alex * "public domain" freigegeben:
33 8907c8dd 2002-06-26 alex * <http://www.snippets.org/snippets/portable/MATCH+C.php3>
34 8907c8dd 2002-06-26 alex */
35 8907c8dd 2002-06-26 alex
36 8907c8dd 2002-06-26 alex
37 61966a60 2006-10-06 fw static int Matche PARAMS(( const char *p, const char *t ));
38 61966a60 2006-10-06 fw static int Matche_After_Star PARAMS(( const char *p, const char *t ));
39 8907c8dd 2002-06-26 alex
40 8907c8dd 2002-06-26 alex
41 8907c8dd 2002-06-26 alex #define MATCH_PATTERN 6 /* bad pattern */
42 8907c8dd 2002-06-26 alex #define MATCH_LITERAL 5 /* match failure on literal match */
43 8907c8dd 2002-06-26 alex #define MATCH_RANGE 4 /* match failure on [..] construct */
44 8907c8dd 2002-06-26 alex #define MATCH_ABORT 3 /* premature end of text string */
45 8907c8dd 2002-06-26 alex #define MATCH_END 2 /* premature end of pattern string */
46 8907c8dd 2002-06-26 alex #define MATCH_VALID 1 /* valid match */
47 8907c8dd 2002-06-26 alex
48 8907c8dd 2002-06-26 alex
49 8adff592 2005-03-19 fw GLOBAL bool
50 61966a60 2006-10-06 fw Match( const char *Pattern, const char *String )
51 8907c8dd 2002-06-26 alex {
52 8907c8dd 2002-06-26 alex /* Pattern mit String vergleichen */
53 8adff592 2005-03-19 fw if( Matche( Pattern, String ) == MATCH_VALID ) return true;
54 8adff592 2005-03-19 fw else return false;
55 8907c8dd 2002-06-26 alex } /* Match */
56 8907c8dd 2002-06-26 alex
57 8907c8dd 2002-06-26 alex
58 d4eb55c7 2008-07-26 alex GLOBAL bool
59 d4eb55c7 2008-07-26 alex MatchCaseInsensitive(const char *pattern, const char *searchme)
60 d4eb55c7 2008-07-26 alex {
61 d4eb55c7 2008-07-26 alex char haystack[COMMAND_LEN];
62 d4eb55c7 2008-07-26 alex
63 d4eb55c7 2008-07-26 alex strlcpy(haystack, searchme, sizeof(haystack));
64 318c8b23 2008-07-27 alex return Match(pattern, ngt_LowerStr(haystack));
65 d4eb55c7 2008-07-26 alex } /* MatchCaseInsensitive */
66 d4eb55c7 2008-07-26 alex
67 d4eb55c7 2008-07-26 alex
68 77f54693 2005-07-31 alex static int
69 61966a60 2006-10-06 fw Matche( const char *p, const char *t )
70 8907c8dd 2002-06-26 alex {
71 8adff592 2005-03-19 fw register char range_start, range_end;
72 8adff592 2005-03-19 fw bool invert;
73 8adff592 2005-03-19 fw bool member_match;
74 8adff592 2005-03-19 fw bool loop;
75 8907c8dd 2002-06-26 alex
76 8907c8dd 2002-06-26 alex for( ; *p; p++, t++ )
77 8907c8dd 2002-06-26 alex {
78 8907c8dd 2002-06-26 alex /* if this is the end of the text then this is the end of the match */
79 8907c8dd 2002-06-26 alex if( ! *t )
80 8907c8dd 2002-06-26 alex {
81 8907c8dd 2002-06-26 alex return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
82 8907c8dd 2002-06-26 alex }
83 8907c8dd 2002-06-26 alex
84 8907c8dd 2002-06-26 alex /* determine and react to pattern type */
85 8907c8dd 2002-06-26 alex switch( *p )
86 8907c8dd 2002-06-26 alex {
87 8907c8dd 2002-06-26 alex case '?': /* single any character match */
88 8907c8dd 2002-06-26 alex break;
89 8907c8dd 2002-06-26 alex
90 8907c8dd 2002-06-26 alex case '*': /* multiple any character match */
91 8907c8dd 2002-06-26 alex return Matche_After_Star( p, t );
92 8907c8dd 2002-06-26 alex
93 8907c8dd 2002-06-26 alex case '[': /* [..] construct, single member/exclusion character match */
94 8907c8dd 2002-06-26 alex /* move to beginning of range */
95 8907c8dd 2002-06-26 alex p++;
96 8907c8dd 2002-06-26 alex
97 8907c8dd 2002-06-26 alex /* check if this is a member match or exclusion match */
98 8adff592 2005-03-19 fw invert = false;
99 8907c8dd 2002-06-26 alex if( *p == '!' || *p == '^' )
100 8907c8dd 2002-06-26 alex {
101 8adff592 2005-03-19 fw invert = true;
102 8907c8dd 2002-06-26 alex p++;
103 8907c8dd 2002-06-26 alex }
104 8907c8dd 2002-06-26 alex
105 8907c8dd 2002-06-26 alex /* if closing bracket here or at range start then we have a malformed pattern */
106 8907c8dd 2002-06-26 alex if ( *p == ']' ) return MATCH_PATTERN;
107 8907c8dd 2002-06-26 alex
108 8adff592 2005-03-19 fw member_match = false;
109 8adff592 2005-03-19 fw loop = true;
110 8907c8dd 2002-06-26 alex
111 8907c8dd 2002-06-26 alex while( loop )
112 8907c8dd 2002-06-26 alex {
113 8907c8dd 2002-06-26 alex /* if end of construct then loop is done */
114 8907c8dd 2002-06-26 alex if( *p == ']' )
115 8907c8dd 2002-06-26 alex {
116 8adff592 2005-03-19 fw loop = false;
117 8907c8dd 2002-06-26 alex continue;
118 8907c8dd 2002-06-26 alex }
119 8907c8dd 2002-06-26 alex
120 8907c8dd 2002-06-26 alex /* matching a '!', '^', '-', '\' or a ']' */
121 8907c8dd 2002-06-26 alex if( *p == '\\' ) range_start = range_end = *++p;
122 8907c8dd 2002-06-26 alex else range_start = range_end = *p;
123 8907c8dd 2002-06-26 alex
124 8907c8dd 2002-06-26 alex /* if end of pattern then bad pattern (Missing ']') */
125 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
126 8907c8dd 2002-06-26 alex
127 8907c8dd 2002-06-26 alex /* check for range bar */
128 8907c8dd 2002-06-26 alex if( *++p == '-' )
129 8907c8dd 2002-06-26 alex {
130 8907c8dd 2002-06-26 alex /* get the range end */
131 8907c8dd 2002-06-26 alex range_end = *++p;
132 8907c8dd 2002-06-26 alex
133 8907c8dd 2002-06-26 alex /* if end of pattern or construct then bad pattern */
134 8907c8dd 2002-06-26 alex if( range_end == '\0' || range_end == ']' ) return MATCH_PATTERN;
135 8907c8dd 2002-06-26 alex
136 8907c8dd 2002-06-26 alex /* special character range end */
137 8907c8dd 2002-06-26 alex if( range_end == '\\' )
138 8907c8dd 2002-06-26 alex {
139 8907c8dd 2002-06-26 alex range_end = *++p;
140 8907c8dd 2002-06-26 alex
141 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
142 8907c8dd 2002-06-26 alex if ( ! range_end ) return MATCH_PATTERN;
143 8907c8dd 2002-06-26 alex }
144 8907c8dd 2002-06-26 alex
145 8907c8dd 2002-06-26 alex /* move just beyond this range */
146 8907c8dd 2002-06-26 alex p++;
147 8907c8dd 2002-06-26 alex }
148 8907c8dd 2002-06-26 alex
149 8907c8dd 2002-06-26 alex /* if the text character is in range then match found. make sure the range
150 8907c8dd 2002-06-26 alex * letters have the proper relationship to one another before comparison */
151 8907c8dd 2002-06-26 alex if( range_start < range_end )
152 8907c8dd 2002-06-26 alex {
153 8907c8dd 2002-06-26 alex if( *t >= range_start && *t <= range_end )
154 8907c8dd 2002-06-26 alex {
155 8adff592 2005-03-19 fw member_match = true;
156 8adff592 2005-03-19 fw loop = false;
157 8907c8dd 2002-06-26 alex }
158 8907c8dd 2002-06-26 alex }
159 8907c8dd 2002-06-26 alex else
160 8907c8dd 2002-06-26 alex {
161 8907c8dd 2002-06-26 alex if( *t >= range_end && *t <= range_start )
162 8907c8dd 2002-06-26 alex {
163 8adff592 2005-03-19 fw member_match = true;
164 8adff592 2005-03-19 fw loop = false;
165 8907c8dd 2002-06-26 alex }
166 8907c8dd 2002-06-26 alex }
167 8907c8dd 2002-06-26 alex }
168 8907c8dd 2002-06-26 alex
169 8907c8dd 2002-06-26 alex /* if there was a match in an exclusion set then no match */
170 8907c8dd 2002-06-26 alex /* if there was no match in a member set then no match */
171 8907c8dd 2002-06-26 alex if(( invert && member_match ) || ! ( invert || member_match )) return MATCH_RANGE;
172 8907c8dd 2002-06-26 alex
173 8907c8dd 2002-06-26 alex /* if this is not an exclusion then skip the rest of the [...]
174 8907c8dd 2002-06-26 alex * construct that already matched. */
175 8907c8dd 2002-06-26 alex if( member_match )
176 8907c8dd 2002-06-26 alex {
177 8907c8dd 2002-06-26 alex while( *p != ']' )
178 8907c8dd 2002-06-26 alex {
179 8907c8dd 2002-06-26 alex /* bad pattern (Missing ']') */
180 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
181 8907c8dd 2002-06-26 alex
182 8907c8dd 2002-06-26 alex /* skip exact match */
183 8907c8dd 2002-06-26 alex if( *p == '\\' )
184 8907c8dd 2002-06-26 alex {
185 8907c8dd 2002-06-26 alex p++;
186 8907c8dd 2002-06-26 alex
187 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
188 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
189 8907c8dd 2002-06-26 alex }
190 8907c8dd 2002-06-26 alex
191 8907c8dd 2002-06-26 alex /* move to next pattern char */
192 8907c8dd 2002-06-26 alex p++;
193 8907c8dd 2002-06-26 alex }
194 8907c8dd 2002-06-26 alex }
195 8907c8dd 2002-06-26 alex break;
196 8907c8dd 2002-06-26 alex case '\\': /* next character is quoted and must match exactly */
197 8907c8dd 2002-06-26 alex /* move pattern pointer to quoted char and fall through */
198 8907c8dd 2002-06-26 alex p++;
199 8907c8dd 2002-06-26 alex
200 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
201 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
202 8907c8dd 2002-06-26 alex
203 8907c8dd 2002-06-26 alex /* must match this character exactly */
204 8907c8dd 2002-06-26 alex default:
205 8907c8dd 2002-06-26 alex if( *p != *t ) return MATCH_LITERAL;
206 8907c8dd 2002-06-26 alex }
207 8907c8dd 2002-06-26 alex }
208 8907c8dd 2002-06-26 alex /* if end of text not reached then the pattern fails */
209 8907c8dd 2002-06-26 alex
210 8907c8dd 2002-06-26 alex if( *t ) return MATCH_END;
211 8907c8dd 2002-06-26 alex else return MATCH_VALID;
212 8907c8dd 2002-06-26 alex } /* Matche */
213 8907c8dd 2002-06-26 alex
214 8907c8dd 2002-06-26 alex
215 77f54693 2005-07-31 alex static int
216 61966a60 2006-10-06 fw Matche_After_Star( const char *p, const char *t )
217 8907c8dd 2002-06-26 alex {
218 8adff592 2005-03-19 fw register int nextp, match = 0;
219 8907c8dd 2002-06-26 alex
220 8907c8dd 2002-06-26 alex /* pass over existing ? and * in pattern */
221 8907c8dd 2002-06-26 alex while( *p == '?' || *p == '*' )
222 8907c8dd 2002-06-26 alex {
223 8907c8dd 2002-06-26 alex /* take one char for each ? and + */
224 8907c8dd 2002-06-26 alex if (*p == '?')
225 8907c8dd 2002-06-26 alex {
226 8907c8dd 2002-06-26 alex /* if end of text then no match */
227 8907c8dd 2002-06-26 alex if( ! *t++ ) return MATCH_ABORT;
228 8907c8dd 2002-06-26 alex }
229 8907c8dd 2002-06-26 alex
230 8907c8dd 2002-06-26 alex /* move to next char in pattern */
231 8907c8dd 2002-06-26 alex p++;
232 8907c8dd 2002-06-26 alex }
233 8907c8dd 2002-06-26 alex
234 8907c8dd 2002-06-26 alex /* if end of pattern we have matched regardless of text left */
235 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_VALID;
236 8907c8dd 2002-06-26 alex
237 8907c8dd 2002-06-26 alex /* get the next character to match which must be a literal or '[' */
238 8907c8dd 2002-06-26 alex nextp = *p;
239 8907c8dd 2002-06-26 alex if( nextp == '\\' )
240 8907c8dd 2002-06-26 alex {
241 8907c8dd 2002-06-26 alex nextp = p[1];
242 8907c8dd 2002-06-26 alex
243 8907c8dd 2002-06-26 alex /* if end of text then we have a bad pattern */
244 8907c8dd 2002-06-26 alex if( ! nextp ) return MATCH_PATTERN;
245 8907c8dd 2002-06-26 alex }
246 8907c8dd 2002-06-26 alex
247 8907c8dd 2002-06-26 alex /* Continue until we run out of text or definite result seen */
248 8907c8dd 2002-06-26 alex do
249 8907c8dd 2002-06-26 alex {
250 8907c8dd 2002-06-26 alex /* a precondition for matching is that the next character
251 8907c8dd 2002-06-26 alex * in the pattern match the next character in the text or that
252 8907c8dd 2002-06-26 alex * the next pattern char is the beginning of a range. Increment
253 8907c8dd 2002-06-26 alex * text pointer as we go here */
254 8907c8dd 2002-06-26 alex if( nextp == *t || nextp == '[' ) match = Matche( p, t );
255 8907c8dd 2002-06-26 alex
256 8907c8dd 2002-06-26 alex /* if the end of text is reached then no match */
257 8907c8dd 2002-06-26 alex if( ! *t++ ) match = MATCH_ABORT;
258 8907c8dd 2002-06-26 alex } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
259 8907c8dd 2002-06-26 alex
260 8907c8dd 2002-06-26 alex /* return result */
261 8907c8dd 2002-06-26 alex return match;
262 8907c8dd 2002-06-26 alex } /* Matche_After_Star */
263 8907c8dd 2002-06-26 alex
264 8907c8dd 2002-06-26 alex
265 8907c8dd 2002-06-26 alex /* -eof- */