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