projects
/
gnulib.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
strtoimax: port to HP-UX 11.11
[gnulib.git]
/
lib
/
exclude.c
diff --git
a/lib/exclude.c
b/lib/exclude.c
index
34b5636
..
a7dd9b3
100644
(file)
--- a/
lib/exclude.c
+++ b/
lib/exclude.c
@@
-1,7
+1,7
@@
/* exclude.c -- exclude file names
/* exclude.c -- exclude file names
- Copyright (C) 1992
, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003, 2004,
-
2005, 2006, 2007, 2009, 2010 Free Software
Foundation, Inc.
+ Copyright (C) 1992
-1994, 1997, 1999-2007, 2009-2013 Free Software
+ Foundation, Inc.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@
-104,34
+104,47
@@
struct exclude_segment
} v;
};
} v;
};
-/* The exclude structure keeps a singly-linked list of exclude segments */
+/* The exclude structure keeps a singly-linked list of exclude segments,
+ maintained in reverse order. */
struct exclude
{
struct exclude
{
- struct exclude_segment *head
, *tail
;
+ struct exclude_segment *head;
};
};
-/* Return true if str has wildcard characters */
+/* Return true if STR has or may have wildcards, when matched with OPTIONS.
+ Return false if STR definitely does not have wildcards. */
bool
fnmatch_pattern_has_wildcards (const char *str, int options)
{
bool
fnmatch_pattern_has_wildcards (const char *str, int options)
{
- const char *cset = "\\?*[]";
- if (options & FNM_NOESCAPE)
- cset++;
- while (*str)
+ while (1)
{
{
- size_t n = strcspn (str, cset);
- if (str[n] == 0)
- break;
- else if (str[n] == '\\')
+ switch (*str++)
{
{
- str += n + 1;
- if (*str)
- str++;
+ case '\\':
+ str += ! (options & FNM_NOESCAPE) && *str;
+ break;
+
+ case '+': case '@': case '!':
+ if (options & FNM_EXTMATCH && *str == '(')
+ return true;
+ break;
+
+ case '?': case '*': case '[':
+ return true;
+
+ case '\0':
+ return false;
}
}
- else
- return true;
}
}
- return false;
+}
+
+static void
+unescape_pattern (char *str)
+{
+ char const *q = str;
+ do
+ q += *q == '\\' && q[1];
+ while ((*str++ = *q++));
}
/* Return a newly allocated and empty exclude list. */
}
/* Return a newly allocated and empty exclude list. */
@@
-199,8
+212,8
@@
string_free (void *data)
}
/* Create new exclude segment of given TYPE and OPTIONS, and attach it
}
/* Create new exclude segment of given TYPE and OPTIONS, and attach it
- to the
tail of list in EX
*/
-static
struct exclude_segment *
+ to the
head of EX.
*/
+static
void
new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
{
struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
{
struct exclude_segment *sp = xzalloc (sizeof (struct exclude_segment));
@@
-222,12
+235,8
@@
new_exclude_segment (struct exclude *ex, enum exclude_type type, int options)
string_free);
break;
}
string_free);
break;
}
- if (ex->tail)
- ex->tail->next = sp;
- else
- ex->head = sp;
- ex->tail = sp;
- return sp;
+ sp->next = ex->head;
+ ex->head = sp;
}
/* Free a single exclude segment */
}
/* Free a single exclude segment */
@@
-327,36
+336,33
@@
exclude_fnmatch (char const *pattern, char const *f, int options)
return matched;
}
return matched;
}
-/* Return true if the exclude_pattern segment SEG
exclud
es F. */
+/* Return true if the exclude_pattern segment SEG
match
es F. */
static bool
static bool
-
excluded_file_pattern_p
(struct exclude_segment const *seg, char const *f)
+
file_pattern_matches
(struct exclude_segment const *seg, char const *f)
{
size_t exclude_count = seg->v.pat.exclude_count;
struct patopts const *exclude = seg->v.pat.exclude;
size_t i;
{
size_t exclude_count = seg->v.pat.exclude_count;
struct patopts const *exclude = seg->v.pat.exclude;
size_t i;
- bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
- /* Scan through the options, until they change excluded */
for (i = 0; i < exclude_count; i++)
{
char const *pattern = exclude[i].pattern;
int options = exclude[i].options;
if (exclude_fnmatch (pattern, f, options))
for (i = 0; i < exclude_count; i++)
{
char const *pattern = exclude[i].pattern;
int options = exclude[i].options;
if (exclude_fnmatch (pattern, f, options))
- return
!excluded
;
+ return
true
;
}
}
- return
excluded
;
+ return
false
;
}
}
-/* Return true if the exclude_hash segment SEG
exclud
es F.
+/* Return true if the exclude_hash segment SEG
match
es F.
BUFFER is an auxiliary storage of the same length as F (with nul
terminator included) */
static bool
BUFFER is an auxiliary storage of the same length as F (with nul
terminator included) */
static bool
-
excluded_file_name_p
(struct exclude_segment const *seg, char const *f,
-
char *buffer)
+
file_name_matches
(struct exclude_segment const *seg, char const *f,
+ char *buffer)
{
int options = seg->options;
{
int options = seg->options;
- bool excluded = !! (options & EXCLUDE_INCLUDE);
Hash_table *table = seg->v.table;
do
Hash_table *table = seg->v.table;
do
@@
-367,7
+373,7
@@
excluded_file_name_p (struct exclude_segment const *seg, char const *f,
while (1)
{
if (hash_lookup (table, buffer))
while (1)
{
if (hash_lookup (table, buffer))
- return
!excluded
;
+ return
true
;
if (options & FNM_LEADING_DIR)
{
char *p = strrchr (buffer, '/');
if (options & FNM_LEADING_DIR)
{
char *p = strrchr (buffer, '/');
@@
-390,7
+396,8
@@
excluded_file_name_p (struct exclude_segment const *seg, char const *f,
break;
}
while (f);
break;
}
while (f);
- return excluded;
+
+ return false;
}
/* Return true if EX excludes F. */
}
/* Return true if EX excludes F. */
@@
-399,44
+406,46
@@
bool
excluded_file_name (struct exclude const *ex, char const *f)
{
struct exclude_segment *seg;
excluded_file_name (struct exclude const *ex, char const *f)
{
struct exclude_segment *seg;
- bool
excluded
;
+ bool
invert = false
;
char *filename = NULL;
/* If no patterns are given, the default is to include. */
if (!ex->head)
return false;
char *filename = NULL;
/* If no patterns are given, the default is to include. */
if (!ex->head)
return false;
- /* Otherwise, the default is the opposite of the first option. */
- excluded = !! (ex->head->options & EXCLUDE_INCLUDE);
- /* Scan through the segments, seeing whether they change status from
- excluded to included or vice versa. */
- for (seg = ex->head; seg; seg = seg->next)
+ /* Scan through the segments, reporting the status of the first match.
+ The segments are in reverse order, so this reports the status of
+ the last match in the original option list. */
+ for (seg = ex->head; ; seg = seg->next)
{
{
- bool rc;
-
- switch (seg->type)
+ if (seg->type == exclude_hash)
{
{
- case exclude_pattern:
- rc = excluded_file_pattern_p (seg, f);
- break;
-
- case exclude_hash:
if (!filename)
filename = xmalloc (strlen (f) + 1);
if (!filename)
filename = xmalloc (strlen (f) + 1);
- rc = excluded_file_name_p (seg, f, filename);
- break;
-
- default:
- abort ();
+ if (file_name_matches (seg, f, filename))
+ break;
}
}
- if (rc != excluded)
+ else
+ {
+ if (file_pattern_matches (seg, f))
+ break;
+ }
+
+ if (! seg->next)
{
{
- excluded = rc;
+ /* If patterns are given but none match, the default is the
+ opposite of the last segment (i.e., the first in the
+ original option list). For example, in the command
+ 'grep -r --exclude="a*" --include="*b" pat dir', the
+ first option is --exclude so any file name matching
+ neither a* nor *b is included. */
+ invert = true;
break;
}
}
break;
}
}
+
free (filename);
free (filename);
- return
excluded
;
+ return
invert ^ ! (seg->options & EXCLUDE_INCLUDE)
;
}
/* Append to EX the exclusion PATTERN with OPTIONS. */
}
/* Append to EX the exclusion PATTERN with OPTIONS. */
@@
-452,12
+461,11
@@
add_exclude (struct exclude *ex, char const *pattern, int options)
struct exclude_pattern *pat;
struct patopts *patopts;
struct exclude_pattern *pat;
struct patopts *patopts;
- if (ex->tail && ex->tail->type == exclude_pattern
- && ((ex->tail->options & EXCLUDE_INCLUDE) ==
- (options & EXCLUDE_INCLUDE)))
- seg = ex->tail;
- else
- seg = new_exclude_segment (ex, exclude_pattern, options);
+ if (! (ex->head && ex->head->type == exclude_pattern
+ && ((ex->head->options & EXCLUDE_INCLUDE)
+ == (options & EXCLUDE_INCLUDE))))
+ new_exclude_segment (ex, exclude_pattern, options);
+ seg = ex->head;
pat = &seg->v.pat;
if (pat->exclude_count == pat->exclude_alloc)
pat = &seg->v.pat;
if (pat->exclude_count == pat->exclude_alloc)
@@
-470,16
+478,17
@@
add_exclude (struct exclude *ex, char const *pattern, int options)
else
{
char *str, *p;
else
{
char *str, *p;
-#define EXCLUDE_HASH_FLAGS (EXCLUDE_INCLUDE|EXCLUDE_ANCHORED|\
- FNM_LEADING_DIR|FNM_CASEFOLD)
- if (ex->tail && ex->tail->type == exclude_hash
- && ((ex->tail->options & EXCLUDE_HASH_FLAGS) ==
- (options & EXCLUDE_HASH_FLAGS)))
- seg = ex->tail;
- else
- seg = new_exclude_segment (ex, exclude_hash, options);
+ int exclude_hash_flags = (EXCLUDE_INCLUDE | EXCLUDE_ANCHORED
+ | FNM_LEADING_DIR | FNM_CASEFOLD);
+ if (! (ex->head && ex->head->type == exclude_hash
+ && ((ex->head->options & exclude_hash_flags)
+ == (options & exclude_hash_flags))))
+ new_exclude_segment (ex, exclude_hash, options);
+ seg = ex->head;
str = xstrdup (pattern);
str = xstrdup (pattern);
+ if ((options & (EXCLUDE_WILDCARDS | FNM_NOESCAPE)) == EXCLUDE_WILDCARDS)
+ unescape_pattern (str);
p = hash_insert (seg->v.table, str);
if (p != str)
free (str);
p = hash_insert (seg->v.table, str);
if (p != str)
free (str);