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)
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.
11 490f28ff 2002-12-12 alex * Wildcard pattern matching
15 8907c8dd 2002-06-26 alex #include "portab.h"
17 61966a60 2006-10-06 fw static char UNUSED id[] = "$Id: match.c,v 1.5 2006/10/06 21:23:47 fw Exp $";
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>
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"
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>
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 ));
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 */
50 61966a60 2006-10-06 fw Match( const char *Pattern, const char *String )
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 */
59 d4eb55c7 2008-07-26 alex MatchCaseInsensitive(const char *pattern, const char *searchme)
61 d4eb55c7 2008-07-26 alex char haystack[COMMAND_LEN];
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 */
69 61966a60 2006-10-06 fw Matche( const char *p, const char *t )
71 8adff592 2005-03-19 fw register char range_start, range_end;
73 8adff592 2005-03-19 fw bool member_match;
76 8907c8dd 2002-06-26 alex for( ; *p; p++, t++ )
78 8907c8dd 2002-06-26 alex /* if this is the end of the text then this is the end of the match */
81 8907c8dd 2002-06-26 alex return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
84 8907c8dd 2002-06-26 alex /* determine and react to pattern type */
85 8907c8dd 2002-06-26 alex switch( *p )
87 8907c8dd 2002-06-26 alex case '?': /* single any character match */
90 8907c8dd 2002-06-26 alex case '*': /* multiple any character match */
91 8907c8dd 2002-06-26 alex return Matche_After_Star( p, t );
93 8907c8dd 2002-06-26 alex case '[': /* [..] construct, single member/exclusion character match */
94 8907c8dd 2002-06-26 alex /* move to beginning of range */
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 == '^' )
101 8adff592 2005-03-19 fw invert = true;
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;
108 8adff592 2005-03-19 fw member_match = false;
111 8907c8dd 2002-06-26 alex while( loop )
113 8907c8dd 2002-06-26 alex /* if end of construct then loop is done */
114 8907c8dd 2002-06-26 alex if( *p == ']' )
116 8adff592 2005-03-19 fw loop = false;
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;
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;
127 8907c8dd 2002-06-26 alex /* check for range bar */
128 8907c8dd 2002-06-26 alex if( *++p == '-' )
130 8907c8dd 2002-06-26 alex /* get the range end */
131 8907c8dd 2002-06-26 alex range_end = *++p;
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;
136 8907c8dd 2002-06-26 alex /* special character range end */
137 8907c8dd 2002-06-26 alex if( range_end == '\\' )
139 8907c8dd 2002-06-26 alex range_end = *++p;
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;
145 8907c8dd 2002-06-26 alex /* move just beyond this range */
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 )
153 8907c8dd 2002-06-26 alex if( *t >= range_start && *t <= range_end )
155 8adff592 2005-03-19 fw member_match = true;
156 8adff592 2005-03-19 fw loop = false;
161 8907c8dd 2002-06-26 alex if( *t >= range_end && *t <= range_start )
163 8adff592 2005-03-19 fw member_match = true;
164 8adff592 2005-03-19 fw loop = false;
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;
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 )
177 8907c8dd 2002-06-26 alex while( *p != ']' )
179 8907c8dd 2002-06-26 alex /* bad pattern (Missing ']') */
180 8907c8dd 2002-06-26 alex if( ! *p ) return MATCH_PATTERN;
182 8907c8dd 2002-06-26 alex /* skip exact match */
183 8907c8dd 2002-06-26 alex if( *p == '\\' )
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;
191 8907c8dd 2002-06-26 alex /* move to next pattern char */
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 */
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;
203 8907c8dd 2002-06-26 alex /* must match this character exactly */
205 8907c8dd 2002-06-26 alex if( *p != *t ) return MATCH_LITERAL;
208 8907c8dd 2002-06-26 alex /* if end of text not reached then the pattern fails */
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 */
216 61966a60 2006-10-06 fw Matche_After_Star( const char *p, const char *t )
218 8adff592 2005-03-19 fw register int nextp, match = 0;
220 8907c8dd 2002-06-26 alex /* pass over existing ? and * in pattern */
221 8907c8dd 2002-06-26 alex while( *p == '?' || *p == '*' )
223 8907c8dd 2002-06-26 alex /* take one char for each ? and + */
224 8907c8dd 2002-06-26 alex if (*p == '?')
226 8907c8dd 2002-06-26 alex /* if end of text then no match */
227 8907c8dd 2002-06-26 alex if( ! *t++ ) return MATCH_ABORT;
230 8907c8dd 2002-06-26 alex /* move to next char in pattern */
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;
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 == '\\' )
241 8907c8dd 2002-06-26 alex nextp = p[1];
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;
247 8907c8dd 2002-06-26 alex /* Continue until we run out of text or definite result seen */
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 );
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 );
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 */
265 8907c8dd 2002-06-26 alex /* -eof- */