fnmatch.c 8.78 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
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 6 7 8 9 10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * 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
Javier Jardon's avatar
Javier Jardon committed
14
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
15
 */
Elliot Lee's avatar
Elliot Lee committed
16

17
/*
18
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
19 20 21 22 23
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

24 25 26 27 28
/*
 * Stripped down, converted to UTF-8 and test cases added
 *
 *                    Owen Taylor, 13 December 2002;
 */
Elliot Lee's avatar
Elliot Lee committed
29

30
#include "config.h"
31 32 33

#include "gtkprivate.h"

34
#include <string.h>
35

36 37 38 39 40 41 42
#include <glib.h>

static gunichar
get_char (const char **str)
{
  gunichar c = g_utf8_get_char (*str);
  *str = g_utf8_next_char (*str);
Elliot Lee's avatar
Elliot Lee committed
43

Tor Lillqvist's avatar
Tor Lillqvist committed
44 45
#ifdef G_PLATFORM_WIN32
  c = g_unichar_tolower (c);
46
#endif
Elliot Lee's avatar
Elliot Lee committed
47

48 49
  return c;
}
Elliot Lee's avatar
Elliot Lee committed
50

51 52 53 54 55
#if defined(G_OS_WIN32) || defined(G_WITH_CYGWIN)
#define DO_ESCAPE 0
#else  
#define DO_ESCAPE 1
#endif  
Elliot Lee's avatar
Elliot Lee committed
56

57 58 59 60 61 62 63 64 65 66 67 68
static gunichar
get_unescaped_char (const char **str,
		    gboolean    *was_escaped)
{
  gunichar c = get_char (str);

  *was_escaped = DO_ESCAPE && c == '\\';
  if (*was_escaped)
    c = get_char (str);
  
  return c;
}
Elliot Lee's avatar
Elliot Lee committed
69 70 71 72

/* Match STRING against the filename pattern PATTERN, returning zero if
   it matches, nonzero if not.  */

73 74 75
static gboolean
gtk_fnmatch_intern (const char *pattern,
		    const char *string,
Federico Mena Quintero's avatar
Federico Mena Quintero committed
76 77
		    gboolean    component_start,
		    gboolean    no_leading_period)
78 79 80 81
{
  const char *p = pattern, *n = string;
  
  while (*p)
Elliot Lee's avatar
Elliot Lee committed
82
    {
83 84 85 86 87
      const char *last_n = n;
      
      gunichar c = get_char (&p);
      gunichar nc = get_char (&n);
      
Elliot Lee's avatar
Elliot Lee committed
88 89
      switch (c)
	{
90 91 92 93 94
   	case '?':
	  if (nc == '\0')
	    return FALSE;
	  else if (nc == G_DIR_SEPARATOR)
	    return FALSE;
Federico Mena Quintero's avatar
Federico Mena Quintero committed
95
	  else if (nc == '.' && component_start && no_leading_period)
96
	    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
97 98
	  break;
	case '\\':
99 100 101 102
	  if (DO_ESCAPE)
	    c = get_char (&p);
	  if (nc != c)
	    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
103 104
	  break;
	case '*':
Federico Mena Quintero's avatar
Federico Mena Quintero committed
105
	  if (nc == '.' && component_start && no_leading_period)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
	    return FALSE;

	  {
	    const char *last_p = p;

	    for (last_p = p, c = get_char (&p);
		 c == '?' || c == '*';
		 last_p = p, c = get_char (&p))
	      {
		if (c == '?')
		  {
		    if (nc == '\0')
		      return FALSE;
		    else if (nc == G_DIR_SEPARATOR)
		      return FALSE;
		    else
		      {
			last_n = n; nc = get_char (&n);
		      }
		  }
	      }
Elliot Lee's avatar
Elliot Lee committed
127

128 129 130 131 132 133 134 135 136 137 138
	    /* If the pattern ends with wildcards, we have a
	     * guaranteed match unless there is a dir separator
	     * in the remainder of the string.
	     */
	    if (c == '\0')
	      {
		if (strchr (last_n, G_DIR_SEPARATOR) != NULL)
		  return FALSE;
		else
		  return TRUE;
	      }
Elliot Lee's avatar
Elliot Lee committed
139

140 141
	    if (DO_ESCAPE && c == '\\')
	      c = get_char (&p);
Elliot Lee's avatar
Elliot Lee committed
142

143 144 145
	    for (p = last_p; nc != '\0';)
	      {
		if ((c == '[' || nc == c) &&
Federico Mena Quintero's avatar
Federico Mena Quintero committed
146
		    gtk_fnmatch_intern (p, last_n, component_start, no_leading_period))
147 148 149 150 151 152 153 154
		  return TRUE;
		
		component_start = (nc == G_DIR_SEPARATOR);
		last_n = n;
		nc = get_char (&n);
	      }
		  
	    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
155 156 157 158 159
	  }

	case '[':
	  {
	    /* Nonzero if the sense of the character class is inverted.  */
160 161
	    gboolean not;
	    gboolean was_escaped;
Elliot Lee's avatar
Elliot Lee committed
162

163 164
	    if (nc == '\0' || nc == G_DIR_SEPARATOR)
	      return FALSE;
Elliot Lee's avatar
Elliot Lee committed
165

Federico Mena Quintero's avatar
Federico Mena Quintero committed
166
	    if (nc == '.' && component_start && no_leading_period)
167
	      return FALSE;
Elliot Lee's avatar
Elliot Lee committed
168 169 170 171 172

	    not = (*p == '!' || *p == '^');
	    if (not)
	      ++p;

173
	    c = get_unescaped_char (&p, &was_escaped);
Elliot Lee's avatar
Elliot Lee committed
174 175
	    for (;;)
	      {
176
		register gunichar cstart = c, cend = c;
Elliot Lee's avatar
Elliot Lee committed
177 178
		if (c == '\0')
		  /* [ (unterminated) loses.  */
179
		  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
180

181 182 183
		c = get_unescaped_char (&p, &was_escaped);
		
		if (!was_escaped && c == '-' && *p != ']')
Elliot Lee's avatar
Elliot Lee committed
184
		  {
185
		    cend = get_unescaped_char (&p, &was_escaped);
Elliot Lee's avatar
Elliot Lee committed
186
		    if (cend == '\0')
187
		      return FALSE;
Elliot Lee's avatar
Elliot Lee committed
188

189
		    c = get_char (&p);
Elliot Lee's avatar
Elliot Lee committed
190 191
		  }

192
		if (nc >= cstart && nc <= cend)
Elliot Lee's avatar
Elliot Lee committed
193 194
		  goto matched;

195
		if (!was_escaped && c == ']')
Elliot Lee's avatar
Elliot Lee committed
196 197 198
		  break;
	      }
	    if (!not)
199
	      return FALSE;
Elliot Lee's avatar
Elliot Lee committed
200 201 202 203
	    break;

	  matched:;
	    /* Skip the rest of the [...] that already matched.  */
204 205
	    /* XXX 1003.2d11 is unclear if was_escaped is right.  */
	    while (was_escaped || c != ']')
Elliot Lee's avatar
Elliot Lee committed
206 207 208
	      {
		if (c == '\0')
		  /* [... (unterminated) loses.  */
209
		  return FALSE;
Elliot Lee's avatar
Elliot Lee committed
210

211
		c = get_unescaped_char (&p, &was_escaped);
Elliot Lee's avatar
Elliot Lee committed
212 213
	      }
	    if (not)
214
	      return FALSE;
Elliot Lee's avatar
Elliot Lee committed
215 216 217 218
	  }
	  break;

	default:
219 220
	  if (c != nc)
	    return FALSE;
Elliot Lee's avatar
Elliot Lee committed
221 222
	}

223
      component_start = (nc == G_DIR_SEPARATOR);
Elliot Lee's avatar
Elliot Lee committed
224 225 226
    }

  if (*n == '\0')
227 228 229 230 231 232 233 234 235
    return TRUE;

  return FALSE;
}

/* Match STRING against the filename pattern PATTERN, returning zero if
 *  it matches, nonzero if not.
 *
 * GTK+ used to use a old version of GNU fnmatch() that was buggy
236
 * in various ways and didn’t handle UTF-8. The following is
237 238 239 240 241 242 243 244 245 246 247
 * converted to UTF-8. To simplify the process of making it
 * correct, this is special-cased to the combinations of flags
 * that gtkfilesel.c uses.
 *
 *   FNM_FILE_NAME   - always set
 *   FNM_LEADING_DIR - never set
 *   FNM_NOESCAPE    - set only on windows
 *   FNM_CASEFOLD    - set only on windows
 */
gboolean
_gtk_fnmatch (const char *pattern,
Federico Mena Quintero's avatar
Federico Mena Quintero committed
248 249
	      const char *string,
	      gboolean no_leading_period)
250
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
251
  return gtk_fnmatch_intern (pattern, string, TRUE, no_leading_period);
252 253
}

Matthias Clasen's avatar
Matthias Clasen committed
254
#undef FNMATCH_TEST_CASES
255
#ifdef FNMATCH_TEST_CASES
Elliot Lee's avatar
Elliot Lee committed
256

Federico Mena Quintero's avatar
Federico Mena Quintero committed
257 258
#define TEST(pat, str, no_leading_period, result) \
  g_assert (_gtk_fnmatch ((pat), (str), (no_leading_period)) == result)
Elliot Lee's avatar
Elliot Lee committed
259

260 261
int main (int argc, char **argv)
{
Federico Mena Quintero's avatar
Federico Mena Quintero committed
262
  TEST ("[a-]", "-", TRUE, TRUE);
263
  
Federico Mena Quintero's avatar
Federico Mena Quintero committed
264 265
  TEST ("a", "a", TRUE, TRUE);
  TEST ("a", "b", TRUE, FALSE);
266 267

  /* Test what ? matches */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
268 269 270 271 272 273
  TEST ("?", "a", TRUE, TRUE);
  TEST ("?", ".", TRUE, FALSE);
  TEST ("a?", "a.", TRUE, TRUE);
  TEST ("a/?", "a/b", TRUE, TRUE);
  TEST ("a/?", "a/.", TRUE, FALSE);
  TEST ("?", "/", TRUE, FALSE);
274 275

  /* Test what * matches */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
276 277 278 279 280 281
  TEST ("*", "a", TRUE, TRUE);
  TEST ("*", ".", TRUE, FALSE);
  TEST ("a*", "a.", TRUE, TRUE);
  TEST ("a/*", "a/b", TRUE, TRUE);
  TEST ("a/*", "a/.", TRUE, FALSE);
  TEST ("*", "/", TRUE, FALSE);
282 283

  /* Range tests */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
284 285 286 287 288 289 290 291 292 293 294 295
  TEST ("[ab]", "a", TRUE, TRUE);
  TEST ("[ab]", "c", TRUE, FALSE);
  TEST ("[^ab]", "a", TRUE, FALSE);
  TEST ("[!ab]", "a", TRUE, FALSE);
  TEST ("[^ab]", "c", TRUE, TRUE);
  TEST ("[!ab]", "c", TRUE, TRUE);
  TEST ("[a-c]", "b", TRUE, TRUE);
  TEST ("[a-c]", "d", TRUE, FALSE);
  TEST ("[a-]", "-", TRUE, TRUE);
  TEST ("[]]", "]", TRUE, TRUE);
  TEST ("[^]]", "a", TRUE, TRUE);
  TEST ("[!]]", "a", TRUE, TRUE);
296 297

  /* Various unclosed ranges */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
298 299 300 301 302
  TEST ("[ab", "a", TRUE, FALSE);
  TEST ("[a-", "a", TRUE, FALSE);
  TEST ("[ab", "c", TRUE, FALSE);
  TEST ("[a-", "c", TRUE, FALSE);
  TEST ("[^]", "a", TRUE, FALSE);
303 304

  /* Ranges and special no-wildcard matches */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
305 306 307 308 309
  TEST ("[.]", ".", TRUE, FALSE);
  TEST ("a[.]", "a.", TRUE, TRUE);
  TEST ("a/[.]", "a/.", TRUE, FALSE);
  TEST ("[/]", "/", TRUE, FALSE);
  TEST ("[^/]", "a", TRUE, TRUE);
310 311
  
  /* Basic tests of * (and combinations of * and ?) */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
312 313 314 315 316 317 318 319 320
  TEST ("a*b", "ab", TRUE, TRUE);
  TEST ("a*b", "axb", TRUE, TRUE);
  TEST ("a*b", "axxb", TRUE, TRUE);
  TEST ("a**b", "ab", TRUE, TRUE);
  TEST ("a**b", "axb", TRUE, TRUE);
  TEST ("a**b", "axxb", TRUE, TRUE);
  TEST ("a*?*b", "ab", TRUE, FALSE);
  TEST ("a*?*b", "axb", TRUE, TRUE);
  TEST ("a*?*b", "axxb", TRUE, TRUE);
321 322

  /* Test of  *[range] */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
323 324 325
  TEST ("a*[cd]", "ac", TRUE, TRUE);
  TEST ("a*[cd]", "axc", TRUE, TRUE);
  TEST ("a*[cd]", "axx", TRUE, FALSE);
326

Federico Mena Quintero's avatar
Federico Mena Quintero committed
327 328
  TEST ("a/[.]", "a/.", TRUE, FALSE);
  TEST ("a*[.]", "a/.", TRUE, FALSE);
329 330 331

  /* Test of UTF-8 */

Federico Mena Quintero's avatar
Federico Mena Quintero committed
332 333 334 335 336 337 338
  TEST ("ä", "ä", TRUE, TRUE);      /* TEST ("ä", "ä", TRUE); */
  TEST ("?", "ä", TRUE, TRUE);       /* TEST ("?", "ä", TRUE); */
  TEST ("*ö", "äö", TRUE, TRUE);   /* TEST ("*ö", "äö", TRUE); */
  TEST ("*ö", "ääö", TRUE, TRUE); /* TEST ("*ö", "ääö", TRUE); */
  TEST ("[ä]", "ä", TRUE, TRUE);    /* TEST ("[ä]", "ä", TRUE); */
  TEST ("[ä-ö]", "é", TRUE, TRUE); /* TEST ("[ä-ö]", "é", TRUE); */
  TEST ("[ä-ö]", "a", TRUE, FALSE); /* TEST ("[ä-ö]", "a", FALSE); */
339 340 341

#ifdef DO_ESCAPE
  /* Tests of escaping */
Federico Mena Quintero's avatar
Federico Mena Quintero committed
342 343 344 345 346 347 348 349 350 351 352
  TEST ("\\\\", "\\", TRUE, TRUE);
  TEST ("\\?", "?", TRUE, TRUE);
  TEST ("\\?", "a", TRUE, FALSE);
  TEST ("\\*", "*", TRUE, TRUE);
  TEST ("\\*", "a", TRUE, FALSE);
  TEST ("\\[a-b]", "[a-b]", TRUE, TRUE);
  TEST ("[\\\\]", "\\", TRUE, TRUE);
  TEST ("[\\^a]", "a", TRUE, TRUE);
  TEST ("[a\\-c]", "b", TRUE, FALSE);
  TEST ("[a\\-c]", "-", TRUE, TRUE);
  TEST ("[a\\]", "a", TRUE, FALSE);
353 354 355
#endif /* DO_ESCAPE */
  
  return 0;
Elliot Lee's avatar
Elliot Lee committed
356 357
}

358
#endif /* FNMATCH_TEST_CASES */