]> Pileus Git - ~andy/gtk/blob - gtk/fnmatch.c
Change FSF Address
[~andy/gtk] / gtk / fnmatch.c
1 /* Copyright (C) 1991, 1992, 1993 Free Software Foundation, Inc.
2  *
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15  */
16
17 /*
18  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
19  * file for a list of people on the GTK+ Team.  See the ChangeLog
20  * files for a list of changes.  These files are distributed with
21  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
22  */
23
24 /*
25  * Stripped down, converted to UTF-8 and test cases added
26  *
27  *                    Owen Taylor, 13 December 2002;
28  */
29
30 #include "config.h"
31 #include <string.h>
32
33 #include <glib.h>
34
35 /* We need to make sure that all constants are defined
36  * to properly compile this file
37  */
38 #ifndef _GNU_SOURCE
39 #define _GNU_SOURCE
40 #endif
41
42 static gunichar
43 get_char (const char **str)
44 {
45   gunichar c = g_utf8_get_char (*str);
46   *str = g_utf8_next_char (*str);
47
48 #ifdef G_PLATFORM_WIN32
49   c = g_unichar_tolower (c);
50 #endif
51
52   return c;
53 }
54
55 #if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
56 #define DO_ESCAPE 0
57 #else  
58 #define DO_ESCAPE 1
59 #endif  
60
61 static gunichar
62 get_unescaped_char (const char **str,
63                     gboolean    *was_escaped)
64 {
65   gunichar c = get_char (str);
66
67   *was_escaped = DO_ESCAPE && c == '\\';
68   if (*was_escaped)
69     c = get_char (str);
70   
71   return c;
72 }
73
74 /* Match STRING against the filename pattern PATTERN, returning zero if
75    it matches, nonzero if not.  */
76
77 static gboolean
78 gtk_fnmatch_intern (const char *pattern,
79                     const char *string,
80                     gboolean    component_start,
81                     gboolean    no_leading_period)
82 {
83   const char *p = pattern, *n = string;
84   
85   while (*p)
86     {
87       const char *last_n = n;
88       
89       gunichar c = get_char (&p);
90       gunichar nc = get_char (&n);
91       
92       switch (c)
93         {
94         case '?':
95           if (nc == '\0')
96             return FALSE;
97           else if (nc == G_DIR_SEPARATOR)
98             return FALSE;
99           else if (nc == '.' && component_start && no_leading_period)
100             return FALSE;
101           break;
102         case '\\':
103           if (DO_ESCAPE)
104             c = get_char (&p);
105           if (nc != c)
106             return FALSE;
107           break;
108         case '*':
109           if (nc == '.' && component_start && no_leading_period)
110             return FALSE;
111
112           {
113             const char *last_p = p;
114
115             for (last_p = p, c = get_char (&p);
116                  c == '?' || c == '*';
117                  last_p = p, c = get_char (&p))
118               {
119                 if (c == '?')
120                   {
121                     if (nc == '\0')
122                       return FALSE;
123                     else if (nc == G_DIR_SEPARATOR)
124                       return FALSE;
125                     else
126                       {
127                         last_n = n; nc = get_char (&n);
128                       }
129                   }
130               }
131
132             /* If the pattern ends with wildcards, we have a
133              * guaranteed match unless there is a dir separator
134              * in the remainder of the string.
135              */
136             if (c == '\0')
137               {
138                 if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
139                   return FALSE;
140                 else
141                   return TRUE;
142               }
143
144             if (DO_ESCAPE && c == '\\')
145               c = get_char (&p);
146
147             for (p = last_p; nc != '\0';)
148               {
149                 if ((c == '[' || nc == c) &&
150                     gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
151                   return TRUE;
152                 
153                 component_start = (nc == G_DIR_SEPARATOR);
154                 last_n = n;
155                 nc = get_char (&n);
156               }
157                   
158             return FALSE;
159           }
160
161         case '[':
162           {
163             /* Nonzero if the sense of the character class is inverted.  */
164             gboolean not;
165             gboolean was_escaped;
166
167             if (nc == '\0' || nc == G_DIR_SEPARATOR)
168               return FALSE;
169
170             if (nc == '.' && component_start && no_leading_period)
171               return FALSE;
172
173             not = (*p == '!' || *p == '^');
174             if (not)
175               ++p;
176
177             c = get_unescaped_char (&p, &was_escaped);
178             for (;;)
179               {
180                 register gunichar cstart = c, cend = c;
181                 if (c == '\0')
182                   /* [ (unterminated) loses.  */
183                   return FALSE;
184
185                 c = get_unescaped_char (&p, &was_escaped);
186                 
187                 if (!was_escaped && c == '-' && *p != ']')
188                   {
189                     cend = get_unescaped_char (&p, &was_escaped);
190                     if (cend == '\0')
191                       return FALSE;
192
193                     c = get_char (&p);
194                   }
195
196                 if (nc >= cstart && nc <= cend)
197                   goto matched;
198
199                 if (!was_escaped && c == ']')
200                   break;
201               }
202             if (!not)
203               return FALSE;
204             break;
205
206           matched:;
207             /* Skip the rest of the [...] that already matched.  */
208             /* XXX 1003.2d11 is unclear if was_escaped is right.  */
209             while (was_escaped || c != ']')
210               {
211                 if (c == '\0')
212                   /* [... (unterminated) loses.  */
213                   return FALSE;
214
215                 c = get_unescaped_char (&p, &was_escaped);
216               }
217             if (not)
218               return FALSE;
219           }
220           break;
221
222         default:
223           if (c != nc)
224             return FALSE;
225         }
226
227       component_start = (nc == G_DIR_SEPARATOR);
228     }
229
230   if (*n == '\0')
231     return TRUE;
232
233   return FALSE;
234 }
235
236 /* Match STRING against the filename pattern PATTERN, returning zero if
237  *  it matches, nonzero if not.
238  *
239  * GTK+ used to use a old version of GNU fnmatch() that was buggy
240  * in various ways and didn't handle UTF-8. The following is
241  * converted to UTF-8. To simplify the process of making it
242  * correct, this is special-cased to the combinations of flags
243  * that gtkfilesel.c uses.
244  *
245  *   FNM_FILE_NAME   - always set
246  *   FNM_LEADING_DIR - never set
247  *   FNM_NOESCAPE    - set only on windows
248  *   FNM_CASEFOLD    - set only on windows
249  */
250 gboolean
251 _gtk_fnmatch (const char *pattern,
252               const char *string,
253               gboolean no_leading_period)
254 {
255   return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
256 }
257
258 #undef FNMATCH_TEST_CASES
259 #ifdef FNMATCH_TEST_CASES
260
261 #define TEST(pat, str, no_leading_period, result) \
262   g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
263
264 int main (int argc, char **argv)
265 {
266   TEST ("[a-]", "-", TRUE, TRUE);
267   
268   TEST ("a", "a", TRUE, TRUE);
269   TEST ("a", "b", TRUE, FALSE);
270
271   /* Test what ? matches */
272   TEST ("?", "a", TRUE, TRUE);
273   TEST ("?", ".", TRUE, FALSE);
274   TEST ("a?", "a.", TRUE, TRUE);
275   TEST ("a/?", "a/b", TRUE, TRUE);
276   TEST ("a/?", "a/.", TRUE, FALSE);
277   TEST ("?", "/", TRUE, FALSE);
278
279   /* Test what * matches */
280   TEST ("*", "a", TRUE, TRUE);
281   TEST ("*", ".", TRUE, FALSE);
282   TEST ("a*", "a.", TRUE, TRUE);
283   TEST ("a/*", "a/b", TRUE, TRUE);
284   TEST ("a/*", "a/.", TRUE, FALSE);
285   TEST ("*", "/", TRUE, FALSE);
286
287   /* Range tests */
288   TEST ("[ab]", "a", TRUE, TRUE);
289   TEST ("[ab]", "c", TRUE, FALSE);
290   TEST ("[^ab]", "a", TRUE, FALSE);
291   TEST ("[!ab]", "a", TRUE, FALSE);
292   TEST ("[^ab]", "c", TRUE, TRUE);
293   TEST ("[!ab]", "c", TRUE, TRUE);
294   TEST ("[a-c]", "b", TRUE, TRUE);
295   TEST ("[a-c]", "d", TRUE, FALSE);
296   TEST ("[a-]", "-", TRUE, TRUE);
297   TEST ("[]]", "]", TRUE, TRUE);
298   TEST ("[^]]", "a", TRUE, TRUE);
299   TEST ("[!]]", "a", TRUE, TRUE);
300
301   /* Various unclosed ranges */
302   TEST ("[ab", "a", TRUE, FALSE);
303   TEST ("[a-", "a", TRUE, FALSE);
304   TEST ("[ab", "c", TRUE, FALSE);
305   TEST ("[a-", "c", TRUE, FALSE);
306   TEST ("[^]", "a", TRUE, FALSE);
307
308   /* Ranges and special no-wildcard matches */
309   TEST ("[.]", ".", TRUE, FALSE);
310   TEST ("a[.]", "a.", TRUE, TRUE);
311   TEST ("a/[.]", "a/.", TRUE, FALSE);
312   TEST ("[/]", "/", TRUE, FALSE);
313   TEST ("[^/]", "a", TRUE, TRUE);
314   
315   /* Basic tests of * (and combinations of * and ?) */
316   TEST ("a*b", "ab", TRUE, TRUE);
317   TEST ("a*b", "axb", TRUE, TRUE);
318   TEST ("a*b", "axxb", TRUE, TRUE);
319   TEST ("a**b", "ab", TRUE, TRUE);
320   TEST ("a**b", "axb", TRUE, TRUE);
321   TEST ("a**b", "axxb", TRUE, TRUE);
322   TEST ("a*?*b", "ab", TRUE, FALSE);
323   TEST ("a*?*b", "axb", TRUE, TRUE);
324   TEST ("a*?*b", "axxb", TRUE, TRUE);
325
326   /* Test of  *[range] */
327   TEST ("a*[cd]", "ac", TRUE, TRUE);
328   TEST ("a*[cd]", "axc", TRUE, TRUE);
329   TEST ("a*[cd]", "axx", TRUE, FALSE);
330
331   TEST ("a/[.]", "a/.", TRUE, FALSE);
332   TEST ("a*[.]", "a/.", TRUE, FALSE);
333
334   /* Test of UTF-8 */
335
336   TEST ("ä", "ä", TRUE, TRUE);      /* TEST ("ä", "ä", TRUE); */
337   TEST ("?", "ä", TRUE, TRUE);       /* TEST ("?", "ä", TRUE); */
338   TEST ("*ö", "äö", TRUE, TRUE);   /* TEST ("*ö", "äö", TRUE); */
339   TEST ("*ö", "ääö", TRUE, TRUE); /* TEST ("*ö", "ääö", TRUE); */
340   TEST ("[ä]", "ä", TRUE, TRUE);    /* TEST ("[ä]", "ä", TRUE); */
341   TEST ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
342   TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
343
344 #ifdef DO_ESCAPE
345   /* Tests of escaping */
346   TEST ("\\\\", "\\", TRUE, TRUE);
347   TEST ("\\?", "?", TRUE, TRUE);
348   TEST ("\\?", "a", TRUE, FALSE);
349   TEST ("\\*", "*", TRUE, TRUE);
350   TEST ("\\*", "a", TRUE, FALSE);
351   TEST ("\\[a-b]", "[a-b]", TRUE, TRUE);
352   TEST ("[\\\\]", "\\", TRUE, TRUE);
353   TEST ("[\\^a]", "a", TRUE, TRUE);
354   TEST ("[a\\-c]", "b", TRUE, FALSE);
355   TEST ("[a\\-c]", "-", TRUE, TRUE);
356   TEST ("[a\\]", "a", TRUE, FALSE);
357 #endif /* DO_ESCAPE */
358   
359   return 0;
360 }
361
362 #endif /* FNMATCH_TEST_CASES */