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