xalloc.h now uses inline, so includers need to configure this.
[gnulib.git] / lib / exclude.c
1 /* exclude.c -- exclude file names
2
3    Copyright (C) 1992, 1993, 1994, 1997, 1999, 2000, 2001, 2002, 2003 Free
4    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    59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
20
21 /* Written by Paul Eggert <eggert@twinsun.com>  */
22
23 #if HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26
27 #include <stdbool.h>
28
29 #include <ctype.h>
30 #include <errno.h>
31 #ifndef errno
32 extern int errno;
33 #endif
34 #include <stddef.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "exclude.h"
40 #include "fnmatch.h"
41 #include "unlocked-io.h"
42 #include "xalloc.h"
43
44 #if STDC_HEADERS || (! defined isascii && ! HAVE_ISASCII)
45 # define IN_CTYPE_DOMAIN(c) true
46 #else
47 # define IN_CTYPE_DOMAIN(c) isascii (c)
48 #endif
49
50 static inline bool
51 is_space (unsigned char c)
52 {
53   return IN_CTYPE_DOMAIN (c) && isspace (c);
54 }
55
56 /* Verify a requirement at compile-time (unlike assert, which is runtime).  */
57 #define verify(name, assertion) struct name { char a[(assertion) ? 1 : -1]; }
58
59 /* Non-GNU systems lack these options, so we don't need to check them.  */
60 #ifndef FNM_CASEFOLD
61 # define FNM_CASEFOLD 0
62 #endif
63 #ifndef FNM_LEADING_DIR
64 # define FNM_LEADING_DIR 0
65 #endif
66
67 verify (EXCLUDE_macros_do_not_collide_with_FNM_macros,
68         (((EXCLUDE_ANCHORED | EXCLUDE_INCLUDE | EXCLUDE_WILDCARDS)
69           & (FNM_PATHNAME | FNM_NOESCAPE | FNM_PERIOD | FNM_LEADING_DIR
70              | FNM_CASEFOLD))
71          == 0));
72
73 /* An exclude pattern-options pair.  The options are fnmatch options
74    ORed with EXCLUDE_* options.  */
75
76 struct patopts
77   {
78     char const *pattern;
79     int options;
80   };
81
82 /* An exclude list, of pattern-options pairs.  */
83
84 struct exclude
85   {
86     struct patopts *exclude;
87     size_t exclude_alloc;
88     size_t exclude_count;
89   };
90
91 /* Return a newly allocated and empty exclude list.  */
92
93 struct exclude *
94 new_exclude (void)
95 {
96   struct exclude *ex = xmalloc (sizeof *ex);
97   ex->exclude_count = 0;
98   ex->exclude_alloc = 60;
99   ex->exclude = xmalloc (ex->exclude_alloc * sizeof ex->exclude[0]);
100   return ex;
101 }
102
103 /* Free the storage associated with an exclude list.  */
104
105 void
106 free_exclude (struct exclude *ex)
107 {
108   free (ex->exclude);
109   free (ex);
110 }
111
112 /* Return zero if PATTERN matches F, obeying OPTIONS, except that
113    (unlike fnmatch) wildcards are disabled in PATTERN.  */
114
115 static int
116 fnmatch_no_wildcards (char const *pattern, char const *f, int options)
117 {
118   if (! (options & FNM_LEADING_DIR))
119     return ((options & FNM_CASEFOLD)
120             ? strcasecmp (pattern, f)
121             : strcmp (pattern, f));
122   else
123     {
124       size_t patlen = strlen (pattern);
125       int r = ((options & FNM_CASEFOLD)
126                 ? strncasecmp (pattern, f, patlen)
127                 : strncmp (pattern, f, patlen));
128       if (! r)
129         {
130           r = f[patlen];
131           if (r == '/')
132             r = 0;
133         }
134       return r;
135     }
136 }
137
138 /* Return true if EX excludes F.  */
139
140 bool
141 excluded_filename (struct exclude const *ex, char const *f)
142 {
143   size_t exclude_count = ex->exclude_count;
144
145   /* If no options are given, the default is to include.  */
146   if (exclude_count == 0)
147     return false;
148   else
149     {
150       struct patopts const *exclude = ex->exclude;
151       size_t i;
152
153       /* Otherwise, the default is the opposite of the first option.  */
154       bool excluded = !! (exclude[0].options & EXCLUDE_INCLUDE);
155
156       /* Scan through the options, seeing whether they change F from
157          excluded to included or vice versa.  */
158       for (i = 0;  i < exclude_count;  i++)
159         {
160           char const *pattern = exclude[i].pattern;
161           int options = exclude[i].options;
162           if (excluded == !! (options & EXCLUDE_INCLUDE))
163             {
164               int (*matcher) (char const *, char const *, int) =
165                 (options & EXCLUDE_WILDCARDS
166                  ? fnmatch
167                  : fnmatch_no_wildcards);
168               bool matched = ((*matcher) (pattern, f, options) == 0);
169               char const *p;
170
171               if (! (options & EXCLUDE_ANCHORED))
172                 for (p = f; *p && ! matched; p++)
173                   if (*p == '/' && p[1] != '/')
174                     matched = ((*matcher) (pattern, p + 1, options) == 0);
175
176               excluded ^= matched;
177             }
178         }
179
180       return excluded;
181     }
182 }
183
184 /* Append to EX the exclusion PATTERN with OPTIONS.  */
185
186 void
187 add_exclude (struct exclude *ex, char const *pattern, int options)
188 {
189   struct patopts *patopts;
190
191   if (ex->exclude_alloc <= ex->exclude_count)
192     {
193       ex->exclude = xnrealloc (ex->exclude, ex->exclude_alloc,
194                                2 * sizeof *ex->exclude);
195       ex->exclude_alloc *= 2;
196     }
197
198   patopts = &ex->exclude[ex->exclude_count++];
199   patopts->pattern = pattern;
200   patopts->options = options;
201 }
202
203 /* Use ADD_FUNC to append to EX the patterns in FILENAME, each with
204    OPTIONS.  LINE_END terminates each pattern in the file.  If
205    LINE_END is a space character, ignore trailing spaces and empty
206    lines in FILE.  Return -1 on failure, 0 on success.  */
207
208 int
209 add_exclude_file (void (*add_func) (struct exclude *, char const *, int),
210                   struct exclude *ex, char const *filename, int options,
211                   char line_end)
212 {
213   bool use_stdin = filename[0] == '-' && !filename[1];
214   FILE *in;
215   char *buf;
216   char *p;
217   char const *pattern;
218   char const *lim;
219   size_t buf_alloc = 1000;
220   size_t buf_count = 0;
221   int c;
222   int e = 0;
223
224   if (use_stdin)
225     in = stdin;
226   else if (! (in = fopen (filename, "r")))
227     return -1;
228
229   buf = xmalloc (buf_alloc);
230
231   while ((c = getc (in)) != EOF)
232     {
233       buf[buf_count++] = c;
234       if (buf_count == buf_alloc)
235         {
236           buf = xnrealloc (buf, buf_alloc, 2);
237           buf_alloc *= 2;
238         }
239     }
240
241   if (ferror (in))
242     e = errno;
243
244   if (!use_stdin && fclose (in) != 0)
245     e = errno;
246
247   buf = xrealloc (buf, buf_count + 1);
248   buf[buf_count] = line_end;
249   lim = buf + buf_count + ! (buf_count == 0 || buf[buf_count - 1] == line_end);
250   pattern = buf;
251
252   for (p = buf; p < lim; p++)
253     if (*p == line_end)
254       {
255         char *pattern_end = p;
256
257         if (is_space (line_end))
258           {
259             for (; ; pattern_end--)
260               if (pattern_end == pattern)
261                 goto next_pattern;
262               else if (! is_space (pattern_end[-1]))
263                 break;
264           }
265
266         *pattern_end = '\0';
267         (*add_func) (ex, pattern, options);
268
269       next_pattern:
270         pattern = p + 1;
271       }
272
273   errno = e;
274   return e ? -1 : 0;
275 }