1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* exclude.c -- exclude file names
5 Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
6 2005, 2006, 2007, 2009, 2010 Free Software Foundation, Inc.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 /* Written by Paul Eggert <eggert@twinsun.com>
22 and Sergey Poznyakoff <gray@gnu.org>.
23 Thanks to Phil Proudman <phil@proudman51.freeserve.co.uk>
24 for improvement suggestions. */
46 # include "unlocked-io.h"
49 /* Non-GNU systems lack these options, so we don't need to check them. */
51 # define FNM_CASEFOLD 0
54 # define FNM_EXTMATCH 0
56 #ifndef FNM_LEADING_DIR
57 # define FNM_LEADING_DIR 0
60 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
61 & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
62 | FNM_CASEFOLD | FNM_EXTMATCH))
66 /* Exclusion patterns are grouped into a singly-linked list of
67 "exclusion segments". Each segment represents a set of patterns
68 that can be matches using the same algorithm. Non-wildcard
69 patterns are kept in hash tables, to speed up searches. Wildcard
70 patterns are stored as arrays of patterns. */
73 /* An exclude pattern-options pair. The options are fnmatch options
74 ORed with EXCLUDE_* options. */
82 /* An array of pattern-options pairs. */
84 struct exclude_pattern
86 struct patopts *exclude;
93 exclude_hash, /* a hash table of excluded names */
94 exclude_pattern /* an array of exclude patterns */
97 struct exclude_segment
99 struct exclude_segment *next; /* next segment in list */
100 enum exclude_type type; /* type of this segment */
101 int options; /* common options for this segment */
104 Hash_table *table; /* for type == exclude_hash */
105 struct exclude_pattern pat; /* for type == exclude_pattern */
109 /* The exclude structure keeps a singly-linked list of exclude segments */
112 struct exclude_segment *head, *tail;
115 /* Return true if str has wildcard characters */
117 fnmatch_pattern_has_wildcards (const char *str, int options)
119 const char *cset = "\\?*[]";
120 if (options & FNM_NOESCAPE)
124 size_t n = strcspn (str, cset);
127 else if (str[n] == '\\')
139 /* Return a newly allocated and empty exclude list. */
144 return xzalloc (sizeof *new_exclude ());
147 /* Calculate the hash of string. */
149 string_hasher (void const *data, size_t n_buckets)
151 char const *p = data;
152 return hash_string (p, n_buckets);
155 /* Ditto, for case-insensitive hashes */
157 string_hasher_ci (void const *data, size_t n_buckets)
159 char const *p = data;
160 mbui_iterator_t iter;
163 for (mbui_init (iter, p); mbui_avail (iter); mbui_advance (iter))
165 mbchar_t m = mbui_cur (iter);
169 wc = towlower (m.wc);
173 value = (value * 31 + wc) % n_buckets;
179 /* compare two strings for equality */
181 string_compare (void const *data1, void const *data2)
183 char const *p1 = data1;
184 char const *p2 = data2;
185 return strcmp (p1, p2) == 0;
188 /* compare two strings for equality, case-insensitive */
190 string_compare_ci (void const *data1, void const *data2)
192 char const *p1 = data1;
193 char const *p2 = data2;
194 return mbscasecmp (p1, p2) == 0;
198 string_free (void *data)
203 /* Create new exclude segment of given TYPE and OPTIONS, and attach it
204 to the tail of list in EX */
205 static struct exclude_segment *
206 new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
208 struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
210 sp->options = options;
213 case exclude_pattern:
217 sp->v.table = hash_initialize (0, NULL,
218 (options & FNM_CASEFOLD) ?
221 (options & FNM_CASEFOLD) ?
235 /* Free a single exclude segment */
237 free_exclude_segment (struct exclude_segment *seg)
241 case exclude_pattern:
242 free (seg->v.pat.exclude);
246 hash_free (seg->v.table);
252 /* Free the storage associated with an exclude list. */
254 free_exclude (struct exclude *ex)
256 struct exclude_segment *seg;
257 for (seg = ex->head; seg; )
259 struct exclude_segment *next = seg->next;
260 free_exclude_segment (seg);
266 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
267 (unlike fnmatch) wildcards are disabled in PATTERN. */
270 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
272 if (! (options & FNM_LEADING_DIR))
273 return ((options & FNM_CASEFOLD)
274 ? mbscasecmp (pattern, f)
275 : strcmp (pattern, f));
276 else if (! (options & FNM_CASEFOLD))
278 size_t patlen = strlen (pattern);
279 int r = strncmp (pattern, f, patlen);
290 /* Walk through a copy of F, seeing whether P matches any prefix
293 FIXME: This is an O(N**2) algorithm; it should be O(N).
294 Also, the copy should not be necessary. However, fixing this
295 will probably involve a change to the mbs* API. */
297 char *fcopy = xstrdup (f);
300 for (p = fcopy; ; *p++ = '/')
305 r = mbscasecmp (pattern, fcopy);
315 exclude_fnmatch (char const *pattern, char const *f, int options)
317 int (*matcher) (char const *, char const *, int) =
318 (options & EXCLUDE_WILDCARDS
320 : fnmatch_no_wildcards);
321 bool matched = ((*matcher) (pattern, f, options) == 0);
324 if (! (options & EXCLUDE_ANCHORED))
325 for (p = f; *p && ! matched; p++)
326 if (*p == '/' && p[1] != '/')
327 matched = ((*matcher) (pattern, p + 1, options) == 0);
332 /* Return true if the exclude_pattern segment SEG excludes F. */
335 excluded_file_pattern_p (struct exclude_segment const *seg, char const *f)
337 size_t exclude_count = seg->v.pat.exclude_count;
338 struct patopts const *exclude = seg->v.pat.exclude;
340 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
342 /* Scan through the options, until they change excluded */
343 for (i = 0; i < exclude_count; i++)
345 char const *pattern = exclude[i].pattern;
346 int options = exclude[i].options;
347 if (excluded != exclude_fnmatch (pattern, f, options))
353 /* Return true if the exclude_hash segment SEG excludes F.
354 BUFFER is an auxiliary storage of the same length as F (with nul
355 terminator included) */
357 excluded_file_name_p (struct exclude_segment const *seg, char const *f,
360 int options = seg->options;
361 bool excluded = !! (options & EXCLUDE_INCLUDE);
362 Hash_table *table = seg->v.table;
366 /* initialize the pattern */
371 if (hash_lookup (table, buffer))
373 if (options & FNM_LEADING_DIR)
375 char *p = strrchr (buffer, '/');
385 if (!(options & EXCLUDE_ANCHORED))
398 /* Return true if EX excludes F. */
401 excluded_file_name (struct exclude const *ex, char const *f)
403 struct exclude_segment *seg;
405 char *filename = NULL;
407 /* If no patterns are given, the default is to include. */
411 /* Otherwise, the default is the opposite of the first option. */
412 excluded = !! (ex->head->options & EXCLUDE_INCLUDE);
413 /* Scan through the segments, seeing whether they change status from
414 excluded to included or vice versa. */
415 for (seg = ex->head; seg; seg = seg->next)
421 case exclude_pattern:
422 rc = excluded_file_pattern_p (seg, f);
427 filename = xmalloc (strlen (f) + 1);
428 rc = excluded_file_name_p (seg, f, filename);
444 /* Append to EX the exclusion PATTERN with OPTIONS. */
447 add_exclude (struct exclude *ex, char const *pattern, int options)
449 struct exclude_segment *seg;
451 if ((options & EXCLUDE_WILDCARDS)
452 && fnmatch_pattern_has_wildcards (pattern, options))
454 struct exclude_pattern *pat;
455 struct patopts *patopts;
457 if (ex->tail && ex->tail->type == exclude_pattern
458 && ((ex->tail->options & EXCLUDE_INCLUDE) ==
459 (options & EXCLUDE_INCLUDE)))
462 seg = new_exclude_segment (ex, exclude_pattern, options);
465 if (pat->exclude_count == pat->exclude_alloc)
466 pat->exclude = x2nrealloc (pat->exclude, &pat->exclude_alloc,
467 sizeof *pat->exclude);
468 patopts = &pat->exclude[pat->exclude_count++];
469 patopts->pattern = pattern;
470 patopts->options = options;
475 #define EXCLUDE_HASH_FLAGS (EXCLUDE_INCLUDE|EXCLUDE_ANCHORED|\
476 FNM_LEADING_DIR|FNM_CASEFOLD)
477 if (ex->tail && ex->tail->type == exclude_hash
478 && ((ex->tail->options & EXCLUDE_HASH_FLAGS) ==
479 (options & EXCLUDE_HASH_FLAGS)))
482 seg = new_exclude_segment (ex, exclude_hash, options);
484 str = xstrdup (pattern);
485 p = hash_insert (seg->v.table, str);
491 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
492 OPTIONS. LINE_END terminates each pattern in the file. If
493 LINE_END is a space character, ignore trailing spaces and empty
494 lines in FILE. Return -1 on failure, 0 on success. */
497 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
498 struct exclude *ex, char const *file_name, int options,
501 bool use_stdin = file_name[0] == '-' && !file_name[1];
507 size_t buf_alloc = 0;
508 size_t buf_count = 0;
514 else if (! (in = fopen (file_name, "r")))
517 while ((c = getc (in)) != EOF)
519 if (buf_count == buf_alloc)
520 buf = x2realloc (buf, &buf_alloc);
521 buf[buf_count++] = c;
527 if (!use_stdin && fclose (in) != 0)
530 buf = xrealloc (buf, buf_count + 1);
531 buf[buf_count] = line_end;
532 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
535 for (p = buf; p < lim; p++)
538 char *pattern_end = p;
540 if (isspace ((unsigned char) line_end))
542 for (; ; pattern_end--)
543 if (pattern_end == pattern)
545 else if (! isspace ((unsigned char) pattern_end[-1]))
550 (*add_func) (ex, pattern, options);