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 if( Matche( Pattern, String ) == MATCH_VALID ) return true;
64 * Match string with pattern case-insensitive.
66 * @param Pattern Pattern to match with
67 * @param String Input string, at most COMMAND_LEN-1 characters long
68 * @return true if pattern matches
71 MatchCaseInsensitive(const char *Pattern, const char *String)
73 char haystack[COMMAND_LEN];
75 strlcpy(haystack, String, sizeof(haystack));
76 return Match(Pattern, ngt_LowerStr(haystack));
77 } /* MatchCaseInsensitive */
81 * Match string with pattern case-insensitive.
83 * @param pattern Pattern to match with
84 * @param String Input string, at most COMMAND_LEN-1 characters long
85 * @param Separator Character separating the individual patterns in the list
86 * @return true if pattern matches
89 MatchCaseInsensitiveList(const char *Pattern, const char *String,
90 const char *Separator)
92 char tmp_pattern[COMMAND_LEN], haystack[COMMAND_LEN], *ptr;
94 strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern));
95 strlcpy(haystack, String, sizeof(haystack));
96 ngt_LowerStr(haystack);
98 ptr = strtok(tmp_pattern, Separator);
101 if (Match(ptr, haystack))
103 ptr = strtok(NULL, Separator);
106 } /* MatchCaseInsensitive */
110 Matche( const char *p, const char *t )
112 for( ; *p; p++, t++ )
114 /* if this is the end of the text then this is the end of the match */
117 return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
120 /* determine and react to pattern type */
123 case '?': /* single any character match */
126 case '*': /* multiple any character match */
127 return Matche_After_Star( p, t );
129 default: /* must match this character exactly */
130 if( *p != *t ) return MATCH_LITERAL;
133 /* if end of text not reached then the pattern fails */
135 if( *t ) return MATCH_END;
136 else return MATCH_VALID;
141 Matche_After_Star( const char *p, const char *t )
143 register int nextp, match = 0;
145 /* pass over existing ? and * in pattern */
146 while( *p == '?' || *p == '*' )
148 /* take one char for each ? and + */
151 /* if end of text then no match */
152 if( ! *t++ ) return MATCH_ABORT;
155 /* move to next char in pattern */
159 /* if end of pattern we have matched regardless of text left */
160 if( ! *p ) return MATCH_VALID;
162 /* get the next character to match which must be a literal or '[' */
168 /* if end of text then we have a bad pattern */
169 if( ! nextp ) return MATCH_PATTERN;
172 /* Continue until we run out of text or definite result seen */
175 /* a precondition for matching is that the next character
176 * in the pattern match the next character in the text or that
177 * the next pattern char is the beginning of a range. Increment
178 * text pointer as we go here */
179 if( nextp == *t || nextp == '[' ) match = Matche( p, t );
181 /* if the end of text is reached then no match */
182 if( ! *t++ ) match = MATCH_ABORT;
183 } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
187 } /* Matche_After_Star */