2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 * Please read the file COPYING, README and AUTHORS for more information.
16 * Wildcard pattern matching
30 * The pattern matching functions [Matche(), Matche_After_Star()] are based
31 * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as
32 * "public domain": <http://c.snippets.org/snip_lister.php?fname=match.c>
36 static int Matche PARAMS(( const char *p, const char *t ));
37 static int Matche_After_Star PARAMS(( const char *p, const char *t ));
40 #define MATCH_PATTERN 6 /**< bad pattern */
41 #define MATCH_LITERAL 5 /**< match failure on literal match */
42 #define MATCH_RANGE 4 /**< match failure on [..] construct */
43 #define MATCH_ABORT 3 /**< premature end of text string */
44 #define MATCH_END 2 /**< premature end of pattern string */
45 #define MATCH_VALID 1 /**< valid match */
49 * Match string with pattern.
51 * @param Pattern Pattern to match with
52 * @param String Input string
53 * @return true if pattern matches
56 Match( const char *Pattern, const char *String )
58 /* Pattern mit String vergleichen */
59 if( Matche( Pattern, String ) == MATCH_VALID ) return true;
65 * Match string with pattern case-insensitive.
67 * @param Pattern Pattern to match with
68 * @param String Input string, at most COMMAND_LEN-1 characters long
69 * @return true if pattern matches
72 MatchCaseInsensitive(const char *Pattern, const char *String)
74 char haystack[COMMAND_LEN];
76 strlcpy(haystack, String, sizeof(haystack));
77 return Match(Pattern, ngt_LowerStr(haystack));
78 } /* MatchCaseInsensitive */
82 * Match string with pattern case-insensitive.
84 * @param pattern Pattern to match with
85 * @param String Input string, at most COMMAND_LEN-1 characters long
86 * @param Separator Character separating the individual patterns in the list
87 * @return true if pattern matches
90 MatchCaseInsensitiveList(const char *Pattern, const char *String,
91 const char *Separator)
93 char tmp_pattern[COMMAND_LEN], haystack[COMMAND_LEN], *ptr;
95 strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern));
96 strlcpy(haystack, String, sizeof(haystack));
97 ngt_LowerStr(haystack);
99 ptr = strtok(tmp_pattern, Separator);
102 if (Match(ptr, haystack))
104 ptr = strtok(NULL, Separator);
107 } /* MatchCaseInsensitive */
111 Matche( const char *p, const char *t )
113 register char range_start, range_end;
118 for( ; *p; p++, t++ )
120 /* if this is the end of the text then this is the end of the match */
123 return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
126 /* determine and react to pattern type */
129 case '?': /* single any character match */
132 case '*': /* multiple any character match */
133 return Matche_After_Star( p, t );
135 case '[': /* [..] construct, single member/exclusion character match */
136 /* move to beginning of range */
139 /* check if this is a member match or exclusion match */
141 if( *p == '!' || *p == '^' )
147 /* if closing bracket here or at range start then we have a malformed pattern */
148 if ( *p == ']' ) return MATCH_PATTERN;
150 member_match = false;
155 /* if end of construct then loop is done */
162 /* matching a '!', '^', '-', '\' or a ']' */
163 if( *p == '\\' ) range_start = range_end = *++p;
164 else range_start = range_end = *p;
166 /* if end of pattern then bad pattern (Missing ']') */
167 if( ! *p ) return MATCH_PATTERN;
169 /* check for range bar */
172 /* get the range end */
175 /* if end of pattern or construct then bad pattern */
176 if( range_end == '\0' || range_end == ']' ) return MATCH_PATTERN;
178 /* special character range end */
179 if( range_end == '\\' )
183 /* if end of text then we have a bad pattern */
184 if ( ! range_end ) return MATCH_PATTERN;
187 /* move just beyond this range */
191 /* if the text character is in range then match found. make sure the range
192 * letters have the proper relationship to one another before comparison */
193 if( range_start < range_end )
195 if( *t >= range_start && *t <= range_end )
203 if( *t >= range_end && *t <= range_start )
211 /* if there was a match in an exclusion set then no match */
212 /* if there was no match in a member set then no match */
213 if(( invert && member_match ) || ! ( invert || member_match )) return MATCH_RANGE;
215 /* if this is not an exclusion then skip the rest of the [...]
216 * construct that already matched. */
221 /* bad pattern (Missing ']') */
222 if( ! *p ) return MATCH_PATTERN;
224 /* skip exact match */
229 /* if end of text then we have a bad pattern */
230 if( ! *p ) return MATCH_PATTERN;
233 /* move to next pattern char */
238 case '\\': /* next character is quoted and must match exactly */
239 /* move pattern pointer to quoted char and fall through */
242 /* if end of text then we have a bad pattern */
243 if( ! *p ) return MATCH_PATTERN;
245 /* must match this character exactly */
247 if( *p != *t ) return MATCH_LITERAL;
250 /* if end of text not reached then the pattern fails */
252 if( *t ) return MATCH_END;
253 else return MATCH_VALID;
258 Matche_After_Star( const char *p, const char *t )
260 register int nextp, match = 0;
262 /* pass over existing ? and * in pattern */
263 while( *p == '?' || *p == '*' )
265 /* take one char for each ? and + */
268 /* if end of text then no match */
269 if( ! *t++ ) return MATCH_ABORT;
272 /* move to next char in pattern */
276 /* if end of pattern we have matched regardless of text left */
277 if( ! *p ) return MATCH_VALID;
279 /* get the next character to match which must be a literal or '[' */
285 /* if end of text then we have a bad pattern */
286 if( ! nextp ) return MATCH_PATTERN;
289 /* Continue until we run out of text or definite result seen */
292 /* a precondition for matching is that the next character
293 * in the pattern match the next character in the text or that
294 * the next pattern char is the beginning of a range. Increment
295 * text pointer as we go here */
296 if( nextp == *t || nextp == '[' ) match = Matche( p, t );
298 /* if the end of text is reached then no match */
299 if( ! *t++ ) match = MATCH_ABORT;
300 } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
304 } /* Matche_After_Star */