* modules/exclude (Depends-on): Depend on verify.
[gnulib.git] / lib / exclude.c
1 /* exclude.c -- exclude file names
2
3    Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003,
4    2004, 2005 Free Software Foundation, Inc.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; see the file COPYING.
18    If not, write to the Free Software Foundation,
19    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
20
21 /* Written by Paul Eggert <eggert@twinsun.com>  */
22
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdbool.h>
28
29 #include <ctype.h>
30 #include <errno.h>
31 #include <stddef.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35
36 #include "exclude.h"
37 #include "fnmatch.h"
38 #include "strcase.h"
39 #include "xalloc.h"
40 #include "verify.h"
41
42 #if USE_UNLOCKED_IO
43 # include "unlocked-io.h"
44 #endif
45
46 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
47 # define IN_CTYPE_DOMAIN(c) true
48 #else
49 # define IN_CTYPE_DOMAIN(c) isascii (c)
50 #endif
51
52 static inline bool
53 is_space (unsigned char c)
54 {
55   return IN_CTYPE_DOMAIN (c) && isspace (c);
56 }
57
58 /* Non-GNU systems lack these options, so we don't need to check them.  */
59 #ifndef FNM_CASEFOLD
60 # define FNM_CASEFOLD 0
61 #endif
62 #ifndef FNM_LEADING_DIR
63 # define FNM_LEADING_DIR 0
64 #endif
65
66 verify (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
67          & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
68             | FNM_CASEFOLD))
69         == 0);
70
71 /* An exclude pattern-options pair.  The options are fnmatch options
72    ORed with EXCLUDE_* options.  */
73
74 struct patopts
75   {
76     char const *pattern;
77     int options;
78   };
79
80 /* An exclude list, of pattern-options pairs.  */
81
82 struct exclude
83   {
84     struct patopts *exclude;
85     size_t exclude_alloc;
86     size_t exclude_count;
87   };
88
89 /* Return a newly allocated and empty exclude list.  */
90
91 struct exclude *
92 new_exclude (void)
93 {
94   return xzalloc (sizeof *new_exclude ());
95 }
96
97 /* Free the storage associated with an exclude list.  */
98
99 void
100 free_exclude (struct exclude *ex)
101 {
102   free (ex->exclude);
103   free (ex);
104 }
105
106 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
107    (unlike fnmatch) wildcards are disabled in PATTERN.  */
108
109 static int
110 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
111 {
112   if (! (options & FNM_LEADING_DIR))
113     return ((options & FNM_CASEFOLD)
114             ? strcasecmp (pattern, f)
115             : strcmp (pattern, f));
116   else
117     {
118       size_t patlen = strlen (pattern);
119       int r = ((options & FNM_CASEFOLD)
120                 ? strncasecmp (pattern, f, patlen)
121                 : strncmp (pattern, f, patlen));
122       if (! r)
123         {
124           r = f[patlen];
125           if (r == '/')
126             r = 0;
127         }
128       return r;
129     }
130 }
131
132 /* Return true if EX excludes F.  */
133
134 bool
135 excluded_file_name (struct exclude const *ex, char const *f)
136 {
137   size_t exclude_count = ex->exclude_count;
138
139   /* If no options are given, the default is to include.  */
140   if (exclude_count == 0)
141     return false;
142   else
143     {
144       struct patopts const *exclude = ex->exclude;
145       size_t i;
146
147       /* Otherwise, the default is the opposite of the first option.  */
148       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
149
150       /* Scan through the options, seeing whether they change F from
151          excluded to included or vice versa.  */
152       for (i = 0;  i < exclude_count;  i++)
153         {
154           char const *pattern = exclude[i].pattern;
155           int options = exclude[i].options;
156           if (excluded == !! (options & EXCLUDE_INCLUDE))
157             {
158               int (*matcher) (char const *, char const *, int) =
159                 (options & EXCLUDE_WILDCARDS
160                  ? fnmatch
161                  : fnmatch_no_wildcards);
162               bool matched = ((*matcher) (pattern, f, options) == 0);
163               char const *p;
164
165               if (! (options & EXCLUDE_ANCHORED))
166                 for (p = f; *p && ! matched; p++)
167                   if (*p == '/' && p[1] != '/')
168                     matched = ((*matcher) (pattern, p + 1, options) == 0);
169
170               excluded ^= matched;
171             }
172         }
173
174       return excluded;
175     }
176 }
177
178 /* Append to EX the exclusion PATTERN with OPTIONS.  */
179
180 void
181 add_exclude (struct exclude *ex, char const *pattern, int options)
182 {
183   struct patopts *patopts;
184
185   if (ex->exclude_count == ex->exclude_alloc)
186     ex->exclude = x2nrealloc (ex->exclude, &ex->exclude_alloc,
187                               sizeof *ex->exclude);
188
189   patopts = &ex->exclude[ex->exclude_count++];
190   patopts->pattern = pattern;
191   patopts->options = options;
192 }
193
194 /* Use ADD_FUNC to append to EX the patterns in FILE_NAME, each with
195    OPTIONS.  LINE_END terminates each pattern in the file.  If
196    LINE_END is a space character, ignore trailing spaces and empty
197    lines in FILE.  Return -1 on failure, 0 on success.  */
198
199 int
200 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
201                   struct exclude *ex, char const *file_name, int options,
202                   char line_end)
203 {
204   bool use_stdin = file_name[0] == '-' && !file_name[1];
205   FILE *in;
206   char *buf = NULL;
207   char *p;
208   char const *pattern;
209   char const *lim;
210   size_t buf_alloc = 0;
211   size_t buf_count = 0;
212   int c;
213   int e = 0;
214
215   if (use_stdin)
216     in = stdin;
217   else if (! (in = fopen (file_name, "r")))
218     return -1;
219
220   while ((c = getc (in)) != EOF)
221     {
222       if (buf_count == buf_alloc)
223         buf = x2realloc (buf, &buf_alloc);
224       buf[buf_count++] = c;
225     }
226
227   if (ferror (in))
228     e = errno;
229
230   if (!use_stdin && fclose (in) != 0)
231     e = errno;
232
233   buf = xrealloc (buf, buf_count + 1);
234   buf[buf_count] = line_end;
235   lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
236   pattern = buf;
237
238   for (p = buf; p < lim; p++)
239     if (*p == line_end)
240       {
241         char *pattern_end = p;
242
243         if (is_space (line_end))
244           {
245             for (; ; pattern_end--)
246               if (pattern_end == pattern)
247                 goto next_pattern;
248               else if (! is_space (pattern_end[-1]))
249                 break;
250           }
251
252         *pattern_end = '\0';
253         (*add_func) (ex, pattern, options);
254
255       next_pattern:
256         pattern = p + 1;
257       }
258
259   errno = e;
260   return e ? -1 : 0;
261 }