1 |
/* |
2 |
* match.c |
3 |
* new wildcard matching functions updated for speed. |
4 |
* --Ian. |
5 |
*/ |
6 |
/* |
7 |
* Copyright (C) 2001, 2002, 2003, 2004 Eggheads Development Team |
8 |
* |
9 |
* This program is free software; you can redistribute it and/or |
10 |
* modify it under the terms of the GNU General Public License |
11 |
* as published by the Free Software Foundation; either version 2 |
12 |
* of the License, or (at your option) any later version. |
13 |
* |
14 |
* This program is distributed in the hope that it will be useful, |
15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
17 |
* GNU General Public License for more details. |
18 |
* |
19 |
* You should have received a copy of the GNU General Public License |
20 |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
22 |
*/ |
23 |
|
24 |
#define QUOTE '\\' /* quoting character... for matching "*" etc. */ |
25 |
#define WILDS '*' /* matches any number of characters */ |
26 |
#define WILDP '%' /* matches any nunber of non-space characters */ |
27 |
#define WILDQ '?' /* matches exactly one character */ |
28 |
#define WILDT '~' /* matches any number of spaces */ |
29 |
|
30 |
#include <ctype.h> |
31 |
#define irctoupper toupper |
32 |
|
33 |
/* matching for binds */ |
34 |
int wild_match_per(char *wild, char *matchtext) { |
35 |
char *fallback=0; |
36 |
int match = 0, saved = 0, count = 0, wildcard = 0; |
37 |
|
38 |
/* If either string is NULL, they can't match */ |
39 |
if (!matchtext || !wild) |
40 |
return 0; |
41 |
|
42 |
while(*matchtext && *wild) { |
43 |
switch (*wild) { |
44 |
case WILDP : |
45 |
fallback = wild++; /* setup fallback */ |
46 |
match = 1; |
47 |
while((*(wild++)==WILDP)); /* get rid of redundant %s ... %%%% */ |
48 |
if (*wild==QUOTE) /* is the match char quoted? */ |
49 |
wild++; |
50 |
while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext))) |
51 |
if (*matchtext==' ') { |
52 |
match=0; /* loop until we find the next char... or don't */ |
53 |
break; |
54 |
} else |
55 |
matchtext++; |
56 |
break; |
57 |
case WILDQ : /* inc both... let the while() check for errors */ |
58 |
wild++; |
59 |
matchtext++; |
60 |
break; |
61 |
case WILDT : |
62 |
wild++; |
63 |
if (*matchtext!=' '){ /* need at least one space */ |
64 |
count = saved; |
65 |
if (!fallback) /* no match and no fallback? die */ |
66 |
return 0; |
67 |
wild = fallback-1; /* fall back */ |
68 |
break; |
69 |
} |
70 |
while ((*(matchtext++)==' ')); /* loop through spaces */ |
71 |
count++; |
72 |
break; |
73 |
case WILDS : |
74 |
if (!*++wild) |
75 |
return (count+saved+1); |
76 |
wildcard = 1; |
77 |
break; |
78 |
default : |
79 |
if (*wild==QUOTE) /* is it quoted? */ |
80 |
wild++; |
81 |
if (wildcard) { |
82 |
saved = count; |
83 |
match = count = 0; |
84 |
fallback = wild; /* setup fallback in case we hit a false positive */ |
85 |
while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext))) |
86 |
matchtext++; |
87 |
if (!*matchtext) { |
88 |
match=1; |
89 |
break; |
90 |
} |
91 |
} |
92 |
if (irctoupper(*wild) != irctoupper(*matchtext)) { |
93 |
if (!fallback) /* no match and no fallback? die */ |
94 |
return 0; |
95 |
count = saved; |
96 |
wild = fallback-1; /* fall back */ |
97 |
break; |
98 |
} |
99 |
count++; |
100 |
wildcard = 0; |
101 |
match=1; |
102 |
if (*wild) |
103 |
wild++; |
104 |
if (*matchtext) |
105 |
matchtext++; |
106 |
} |
107 |
} |
108 |
return (!match || *wild || *matchtext)?0:(count+saved+1); |
109 |
} |
110 |
|
111 |
/* generic hostmask matching... */ |
112 |
int wild_match(char *wild, char *matchtext) { |
113 |
char *fallback=0; |
114 |
int match = 0, count = 0, saved = 0, wildcard = 0; |
115 |
|
116 |
/* If either string is NULL, they can't match */ |
117 |
if (!matchtext || !wild) |
118 |
return 0; |
119 |
|
120 |
while(*matchtext && *wild) { |
121 |
switch (*wild) { |
122 |
case WILDQ : /* inc both... let the while() check for errors */ |
123 |
wild++; |
124 |
matchtext++; |
125 |
break; |
126 |
while ((*(matchtext++)==' ')); /* loop through spaces */ |
127 |
break; |
128 |
case WILDS : |
129 |
if (!*++wild) |
130 |
return (count+saved+1); |
131 |
wildcard = 1; |
132 |
break; |
133 |
default : |
134 |
if (*wild==QUOTE) /* is it quoted? */ |
135 |
wild++; |
136 |
if (wildcard) { |
137 |
saved = count; |
138 |
match = count = 0; |
139 |
fallback = wild; /* setup fallback in case we hit a false positive */ |
140 |
while(*matchtext && (irctoupper(*wild) != irctoupper(*matchtext))) |
141 |
matchtext++; |
142 |
if (!*matchtext) { |
143 |
match=1; |
144 |
break; |
145 |
} |
146 |
} |
147 |
if (irctoupper(*wild) != irctoupper(*matchtext)) { |
148 |
if (!fallback) /* no match and no fallback? die */ |
149 |
return 0; |
150 |
count = saved; |
151 |
wild = fallback-1; /* fall back */ |
152 |
break; |
153 |
} |
154 |
wildcard = 0; |
155 |
count++; |
156 |
match=1; |
157 |
if (*wild) |
158 |
wild++; |
159 |
if (*matchtext) |
160 |
matchtext++; |
161 |
} |
162 |
} |
163 |
return (!match || *wild || *matchtext)?0:(count+saved+1); |
164 |
} |