maint: update copyright
[gnulib.git] / tests / test-exclude.c
1 /* Test suite for exclude.
2    Copyright (C) 2009-2014 Free Software Foundation, Inc.
3    This file is part of the GNUlib Library.
4
5    This program is free software: you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 3 of the License, or
8    (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
17
18 #include <config.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <errno.h>
22 #include <string.h>
23 #include <stdbool.h>
24 #include <fnmatch.h>
25
26 #include "exclude.h"
27 #include "progname.h"
28 #include "error.h"
29 #include "argmatch.h"
30
31 #ifndef FNM_CASEFOLD
32 # define FNM_CASEFOLD 0
33 #endif
34 #ifndef FNM_LEADING_DIR
35 # define FNM_LEADING_DIR 0
36 #endif
37
38 char const * const exclude_keywords[] = {
39   "noescape",
40   "pathname",
41   "period",
42   "leading_dir",
43   "casefold",
44   "anchored",
45   "include",
46   "wildcards",
47   NULL
48 };
49
50 int exclude_flags[] = {
51   FNM_NOESCAPE,
52   FNM_PATHNAME,
53   FNM_PERIOD,
54   FNM_LEADING_DIR,
55   FNM_CASEFOLD,
56   EXCLUDE_ANCHORED,
57   EXCLUDE_INCLUDE,
58   EXCLUDE_WILDCARDS
59 };
60
61 ARGMATCH_VERIFY (exclude_keywords, exclude_flags);
62
63 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
64    thus must link with a definition of that function.  Provide it here.  */
65 #ifdef ARGMATCH_DIE_DECL
66
67 _Noreturn ARGMATCH_DIE_DECL;
68 ARGMATCH_DIE_DECL { exit (1); }
69
70 #endif
71
72 int
73 main (int argc, char **argv)
74 {
75   int exclude_options = 0;
76   struct exclude *exclude = new_exclude ();
77
78   set_program_name (argv[0]);
79
80   if (argc == 1)
81     error (1, 0, "usage: %s file -- words...", argv[0]);
82
83   while (--argc)
84     {
85       char *opt = *++argv;
86       if (opt[0] == '-')
87         {
88           int neg = 0;
89           int flag;
90           char *s = opt + 1;
91
92           if (opt[1] == '-' && opt[2] == 0)
93             {
94               argc--;
95               break;
96             }
97           if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
98             {
99               neg = 1;
100               s += 3;
101             }
102           flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
103           if (neg)
104             exclude_options &= ~flag;
105           else
106             exclude_options |= flag;
107
108           /* Skip this test if invoked with -leading-dir on a system that
109              lacks support for FNM_LEADING_DIR. */
110           if (strcmp (s, "leading_dir") == 0 && FNM_LEADING_DIR == 0)
111             exit (77);
112
113           /* Likewise for -casefold and FNM_CASEFOLD.  */
114           if (strcmp (s, "casefold") == 0 && FNM_CASEFOLD == 0)
115             exit (77);
116         }
117       else if (add_exclude_file (add_exclude, exclude, opt,
118                                  exclude_options, '\n') != 0)
119         error (1, errno, "error loading %s", opt);
120     }
121
122   for (; argc; --argc)
123     {
124       char *word = *++argv;
125
126       printf ("%s: %d\n", word, excluded_file_name (exclude, word));
127     }
128   return 0;
129 }