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