1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* exclude.c -- exclude file names
5 Copyright (C) 1992-1994, 1997, 1999-2007, 2009-2011 Free Software
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] == '\\')
140 unescape_pattern (char *str)
156 while ((*str++ = *q++));
159 /* Return a newly allocated and empty exclude list. */
164 return xzalloc (sizeof *new_exclude ());
167 /* Calculate the hash of string. */
169 string_hasher (void const *data, size_t n_buckets)
171 char const *p = data;
172 return hash_string (p, n_buckets);
175 /* Ditto, for case-insensitive hashes */
177 string_hasher_ci (void const *data, size_t n_buckets)
179 char const *p = data;
180 mbui_iterator_t iter;
183 for (mbui_init (iter, p); mbui_avail (iter); mbui_advance (iter))
185 mbchar_t m = mbui_cur (iter);
189 wc = towlower (m.wc);
193 value = (value * 31 + wc) % n_buckets;
199 /* compare two strings for equality */
201 string_compare (void const *data1, void const *data2)
203 char const *p1 = data1;
204 char const *p2 = data2;
205 return strcmp (p1, p2) == 0;
208 /* compare two strings for equality, case-insensitive */
210 string_compare_ci (void const *data1, void const *data2)
212 char const *p1 = data1;
213 char const *p2 = data2;
214 return mbscasecmp (p1, p2) == 0;
218 string_free (void *data)
223 /* Create new exclude segment of given TYPE and OPTIONS, and attach it
224 to the tail of list in EX */
225 static struct exclude_segment *
226 new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
228 struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
230 sp->options = options;
233 case exclude_pattern:
237 sp->v.table = hash_initialize (0, NULL,
238 (options & FNM_CASEFOLD) ?
241 (options & FNM_CASEFOLD) ?
255 /* Free a single exclude segment */
257 free_exclude_segment (struct exclude_segment *seg)
261 case exclude_pattern:
262 free (seg->v.pat.exclude);
266 hash_free (seg->v.table);
272 /* Free the storage associated with an exclude list. */
274 free_exclude (struct exclude *ex)
276 struct exclude_segment *seg;
277 for (seg = ex->head; seg; )
279 struct exclude_segment *next = seg->next;
280 free_exclude_segment (seg);
286 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
287 (unlike fnmatch) wildcards are disabled in PATTERN. */
290 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
292 if (! (options & FNM_LEADING_DIR))
293 return ((options & FNM_CASEFOLD)
294 ? mbscasecmp (pattern, f)
295 : strcmp (pattern, f));
296 else if (! (options & FNM_CASEFOLD))
298 size_t patlen = strlen (pattern);
299 int r = strncmp (pattern, f, patlen);
310 /* Walk through a copy of F, seeing whether P matches any prefix
313 FIXME: This is an O(N**2) algorithm; it should be O(N).
314 Also, the copy should not be necessary. However, fixing this
315 will probably involve a change to the mbs* API. */
317 char *fcopy = xstrdup (f);
320 for (p = fcopy; ; *p++ = '/')
325 r = mbscasecmp (pattern, fcopy);
335 exclude_fnmatch (char const *pattern, char const *f, int options)
337 int (*matcher) (char const *, char const *, int) =
338 (options & EXCLUDE_WILDCARDS
340 : fnmatch_no_wildcards);
341 bool matched = ((*matcher) (pattern, f, options) == 0);
344 if (! (options & EXCLUDE_ANCHORED))
345 for (p = f; *p && ! matched; p++)
346 if (*p == '/' && p[1] != '/')
347 matched = ((*matcher) (pattern, p + 1, options) == 0);
352 /* Return true if the exclude_pattern segment SEG excludes F. */
355 excluded_file_pattern_p (struct exclude_segment const *seg, char const *f)
357 size_t exclude_count = seg->v.pat.exclude_count;
358 struct patopts const *exclude = seg->v.pat.exclude;
360 bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
362 /* Scan through the options, until they change excluded */
363 for (i = 0; i < exclude_count; i++)
365 char const *pattern = exclude[i].pattern;
366 int options = exclude[i].options;
367 if (exclude_fnmatch (pattern, f, options))
373 /* Return true if the exclude_hash segment SEG excludes F.
374 BUFFER is an auxiliary storage of the same length as F (with nul
375 terminator included) */
377 excluded_file_name_p (struct exclude_segment const *seg, char const *f,
380 int options = seg->options;
381 bool excluded = !! (options & EXCLUDE_INCLUDE);
382 Hash_table *table = seg->v.table;
386 /* initialize the pattern */
391 if (hash_lookup (table, buffer))
393 if (options & FNM_LEADING_DIR)
395 char *p = strrchr (buffer, '/');
405 if (!(options & EXCLUDE_ANCHORED))
418 /* Return true if EX excludes F. */
421 excluded_file_name (struct exclude const *ex, char const *f)
423 struct exclude_segment *seg;
425 char *filename = NULL;
427 /* If no patterns are given, the default is to include. */
431 /* Otherwise, the default is the opposite of the first option. */
432 excluded = !! (ex->head->options & EXCLUDE_INCLUDE);
433 /* Scan through the segments, seeing whether they change status from
434 excluded to included or vice versa. */
435 for (seg = ex->head; seg; seg = seg->next)
441 case exclude_pattern:
442 rc = excluded_file_pattern_p (seg, f);
447 filename = xmalloc (strlen (f) + 1);
448 rc = excluded_file_name_p (seg, f, filename);
464 /* Append to EX the exclusion PATTERN with OPTIONS. */
467 add_exclude (struct exclude *ex, char const *pattern, int options)
469 struct exclude_segment *seg;
471 if ((options & EXCLUDE_WILDCARDS)
472 && fnmatch_pattern_has_wildcards (pattern, options))
474 struct exclude_pattern *pat;
475 struct patopts *patopts;
477 if (ex->tail && ex->tail->type == exclude_pattern
478 && ((ex->tail->options & EXCLUDE_INCLUDE) ==
479 (options & EXCLUDE_INCLUDE)))
482 seg = new_exclude_segment (ex, exclude_pattern, options);
485 if (pat->exclude_count == pat->exclude_alloc)
486 pat->exclude = x2nrealloc (pat->exclude, &pat->exclude_alloc,
487 sizeof *pat->exclude);
488 patopts = &pat->exclude[pat->exclude_count++];
489 patopts->pattern = pattern;
490 patopts->options = options;
495 #define EXCLUDE_HASH_FLAGS (EXCLUDE_INCLUDE|EXCLUDE_ANCHORED|\
496 FNM_LEADING_DIR|FNM_CASEFOLD)
497 if (ex->tail && ex->tail->type == exclude_hash
498 && ((ex->tail->options & EXCLUDE_HASH_FLAGS) ==
499 (options & EXCLUDE_HASH_FLAGS)))
502 seg = new_exclude_segment (ex, exclude_hash, options);
504 str = xstrdup (pattern);
505 if (options & EXCLUDE_WILDCARDS)
506 unescape_pattern (str);
507 p = hash_insert (seg->v.table, str);
513 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
514 OPTIONS. LINE_END terminates each pattern in the file. If
515 LINE_END is a space character, ignore trailing spaces and empty
516 lines in FILE. Return -1 on failure, 0 on success. */
519 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
520 struct exclude *ex, char const *file_name, int options,
523 bool use_stdin = file_name[0] == '-' && !file_name[1];
529 size_t buf_alloc = 0;
530 size_t buf_count = 0;
536 else if (! (in = fopen (file_name, "r")))
539 while ((c = getc (in)) != EOF)
541 if (buf_count == buf_alloc)
542 buf = x2realloc (buf, &buf_alloc);
543 buf[buf_count++] = c;
549 if (!use_stdin && fclose (in) != 0)
552 buf = xrealloc (buf, buf_count + 1);
553 buf[buf_count] = line_end;
554 lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
557 for (p = buf; p < lim; p++)
560 char *pattern_end = p;
562 if (isspace ((unsigned char) line_end))
564 for (; ; pattern_end--)
565 if (pattern_end == pattern)
567 else if (! isspace ((unsigned char) pattern_end[-1]))
572 (*add_func) (ex, pattern, options);