build-aux/config.sub
[gnulib.git] / tests / test-exclude.c
1 /* Test suite for exclude.
2    Copyright (C) 2009-2011 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 # if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8)
68 #  define _GL_ATTRIBUTE_NORETURN __attribute__ ((__noreturn__))
69 # else
70 #  define _GL_ATTRIBUTE_NORETURN /* empty */
71 # endif
72
73 ARGMATCH_DIE_DECL _GL_ATTRIBUTE_NORETURN;
74 ARGMATCH_DIE_DECL { exit (1); }
75
76 #endif
77
78 int
79 main (int argc, char **argv)
80 {
81   int exclude_options = 0;
82   struct exclude *exclude = new_exclude ();
83
84   set_program_name (argv[0]);
85
86   if (argc == 1)
87     error (1, 0, "usage: %s file -- words...", argv[0]);
88
89   while (--argc)
90     {
91       char *opt = *++argv;
92       if (opt[0] == '-')
93         {
94           int neg = 0;
95           int flag;
96           char *s = opt + 1;
97
98           if (opt[1] == '-' && opt[2] == 0)
99             {
100               argc--;
101               break;
102             }
103           if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
104             {
105               neg = 1;
106               s += 3;
107             }
108           flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
109           if (neg)
110             exclude_options &= ~flag;
111           else
112             exclude_options |= flag;
113         }
114       else if (add_exclude_file (add_exclude, exclude, opt,
115                                  exclude_options, '\n') != 0)
116         error (1, errno, "error loading %s", opt);
117     }
118
119   for (; argc; --argc)
120     {
121       char *word = *++argv;
122
123       printf ("%s: %d\n", word, excluded_file_name (exclude, word));
124     }
125   return 0;
126 }