1 /* Test suite for exclude.
2 Copyright (C) 2009-2023 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 <https://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 <fnmatch.h>
24
25 #include "exclude.h"
26 #include "error.h"
27 #include "argmatch.h"
28
29 #ifndef FNM_CASEFOLD
30 # define FNM_CASEFOLD 0
31 #endif
32 #ifndef FNM_LEADING_DIR
33 # define FNM_LEADING_DIR 0
34 #endif
35
36 char const * const exclude_keywords[] = {
37 "noescape",
38 "pathname",
39 "period",
40 "leading_dir",
41 "casefold",
42 "anchored",
43 "include",
44 "wildcards",
45 NULL
46 };
47
48 int exclude_flags[] = {
49 FNM_NOESCAPE,
50 FNM_PATHNAME,
51 FNM_PERIOD,
52 FNM_LEADING_DIR,
53 FNM_CASEFOLD,
54 EXCLUDE_ANCHORED,
55 EXCLUDE_INCLUDE,
56 EXCLUDE_WILDCARDS
57 };
58
59 ARGMATCH_VERIFY (exclude_keywords, exclude_flags);
60
61 /* Some packages define ARGMATCH_DIE and ARGMATCH_DIE_DECL in <config.h>, and
62 thus must link with a definition of that function. Provide it here. */
63 #ifdef ARGMATCH_DIE_DECL
64
65 _Noreturn ARGMATCH_DIE_DECL;
66 ARGMATCH_DIE_DECL { exit (1); }
67
68 #endif
69
70 int
71 main (int argc, char **argv)
72 {
73 int exclude_options = 0;
74 struct exclude *exclude = new_exclude ();
75
76 if (argc == 1)
77 error (1, 0, "usage: %s file -- words...", argv[0]);
78
79 while (--argc)
80 {
81 char *opt = *++argv;
82 if (opt[0] == '-')
83 {
84 int neg = 0;
85 int flag;
86 char *s = opt + 1;
87
88 if (opt[1] == '-' && opt[2] == 0)
89 {
90 argc--;
91 break;
92 }
93 if (strlen (s) > 3 && memcmp (s, "no-", 3) == 0)
94 {
95 neg = 1;
96 s += 3;
97 }
98 flag = XARGMATCH (opt, s, exclude_keywords, exclude_flags);
99 if (neg)
100 exclude_options &= ~flag;
101 else
102 exclude_options |= flag;
103
104 /* Skip this test if invoked with -leading-dir on a system that
105 lacks support for FNM_LEADING_DIR. */
106 if (strcmp (s, "leading_dir") == 0 && FNM_LEADING_DIR == 0)
107 exit (77);
108
109 /* Likewise for -casefold and FNM_CASEFOLD. */
110 if (strcmp (s, "casefold") == 0 && FNM_CASEFOLD == 0)
111 exit (77);
112 }
113 else if (add_exclude_file (add_exclude, exclude, opt,
114 exclude_options, '\n') != 0)
115 error (1, errno, "error loading %s", opt);
116 }
117
118 for (; argc; --argc)
119 {
120 char *word = *++argv;
121
122 printf ("%s: %d\n", word, excluded_file_name (exclude, word));
123 }
124
125 free_exclude (exclude);
126 return 0;
127 }