]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/match.c
88872f0d9102353b83e79cc6274be451acc58f45
[ngircd-alex.git] / src / ngircd / match.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
4  *
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.
10  */
11
12 #include "portab.h"
13
14 /**
15  * @file
16  * Wildcard pattern matching
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <string.h>
22
23 #include "exp.h"
24 #include "match.h"
25 #include "defines.h"
26 #include "tool.h"
27
28 /*
29  * The pattern matching functions [Matche(), Matche_After_Star()] are based
30  * on code of J. Kercheval. Version 1.1 has been released on 1991-03-12 as
31  * "public domain": <http://c.snippets.org/snip_lister.php?fname=match.c>
32  */
33
34 static int Matche PARAMS(( const char *p, const char *t ));
35 static int Matche_After_Star PARAMS(( const char *p, const char *t ));
36
37 #define MATCH_PATTERN   6       /**< bad pattern */
38 #define MATCH_LITERAL   5       /**< match failure on literal match */
39 #define MATCH_RANGE     4       /**< match failure on [..] construct */
40 #define MATCH_ABORT     3       /**< premature end of text string */
41 #define MATCH_END       2       /**< premature end of pattern string */
42 #define MATCH_VALID     1       /**< valid match */
43
44 /**
45  * Match string with pattern.
46  *
47  * @param Pattern Pattern to match with
48  * @param String Input string
49  * @return true if pattern matches
50  */
51 GLOBAL bool
52 Match( const char *Pattern, const char *String )
53 {
54         if( Matche( Pattern, String ) == MATCH_VALID ) return true;
55         else return false;
56 } /* Match */
57
58 /**
59  * Match string with pattern case-insensitive.
60  *
61  * @param Pattern Pattern to match with
62  * @param String Input string, at most COMMAND_LEN-1 characters long
63  * @return true if pattern matches
64  */
65 GLOBAL bool
66 MatchCaseInsensitive(const char *Pattern, const char *String)
67 {
68         char haystack[COMMAND_LEN];
69
70         strlcpy(haystack, String, sizeof(haystack));
71         return Match(Pattern, ngt_LowerStr(haystack));
72 } /* MatchCaseInsensitive */
73
74 /**
75  * Match string with pattern case-insensitive.
76  *
77  * @param pattern Pattern to match with
78  * @param String Input string, at most COMMAND_LEN-1 characters long
79  * @param Separator Character separating the individual patterns in the list
80  * @return true if pattern matches
81  */
82 GLOBAL bool
83 MatchCaseInsensitiveList(const char *Pattern, const char *String,
84                      const char *Separator)
85 {
86         char tmp_pattern[COMMAND_LEN], haystack[COMMAND_LEN], *ptr;
87
88         strlcpy(tmp_pattern, Pattern, sizeof(tmp_pattern));
89         strlcpy(haystack, String, sizeof(haystack));
90         ngt_LowerStr(haystack);
91
92         ptr = strtok(tmp_pattern, Separator);
93         while (ptr) {
94                 ngt_TrimStr(ptr);
95                 if (Match(ptr, haystack))
96                         return true;
97                 ptr = strtok(NULL, Separator);
98         }
99         return false;
100 } /* MatchCaseInsensitive */
101
102 static int
103 Matche( const char *p, const char *t )
104 {
105         for( ; *p; p++, t++ )
106         {
107                 /* if this is the end of the text then this is the end of the match */
108                 if( ! *t )
109                 {
110                         return ( *p == '*' && *++p == '\0' ) ? MATCH_VALID : MATCH_ABORT;
111                 }
112
113                 /* determine and react to pattern type */
114                 switch( *p )
115                 {
116                         case '?':       /* single any character match */
117                                 break;
118
119                         case '*':       /* multiple any character match */
120                                 return Matche_After_Star( p, t );
121
122                         default:        /* must match this character exactly */
123                                 if( *p != *t ) return MATCH_LITERAL;
124                 }
125         }
126         /* if end of text not reached then the pattern fails */
127
128         if( *t ) return MATCH_END;
129         else return MATCH_VALID;
130 } /* Matche */
131
132 static int
133 Matche_After_Star( const char *p, const char *t )
134 {
135         register int nextp, match = 0;
136
137         /* pass over existing ? and * in pattern */
138         while( *p == '?' || *p == '*' )
139         {
140                 /* take one char for each ? and + */
141                 if (*p == '?')
142                 {
143                         /* if end of text then no match */
144                         if( ! *t++ ) return MATCH_ABORT;
145                 }
146
147                 /* move to next char in pattern */
148                 p++;
149         }
150
151         /* if end of pattern we have matched regardless of text left */
152         if( ! *p ) return MATCH_VALID;
153
154         /* get the next character to match which must be a literal or '[' */
155         nextp = *p;
156         if( nextp == '\\' )
157         {
158                 nextp = p[1];
159
160                 /* if end of text then we have a bad pattern */
161                 if( ! nextp ) return MATCH_PATTERN;
162         }
163
164         /* Continue until we run out of text or definite result seen */
165         do
166         {
167                 /* a precondition for matching is that the next character
168                  * in the pattern match the next character in the text or that
169                  * the next pattern char is the beginning of a range.  Increment
170                  * text pointer as we go here */
171                 if( nextp == *t || nextp == '[' ) match = Matche( p, t );
172
173                 /* if the end of text is reached then no match */
174                 if( ! *t++ ) match = MATCH_ABORT;
175         } while( match != MATCH_VALID && match != MATCH_ABORT && match != MATCH_PATTERN );
176
177         /* return result */
178         return match;
179 } /* Matche_After_Star */
180
181 /* -eof- */