ecfc8fb0cbf81c3d851ac4166583ae34462d171c
[dragonfly.git] / contrib / grep / lib / fnmatch.c
1 /* -*- buffer-read-only: t -*- vi: set ro: */
2 /* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3 /* Copyright (C) 1991-1993, 1996-2007, 2009-2011 Free Software Foundation, Inc.
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, or (at your option)
8    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, write to the Free Software Foundation,
17    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
18
19 #ifndef _LIBC
20 # include <config.h>
21 #endif
22
23 /* Enable GNU extensions in fnmatch.h.  */
24 #ifndef _GNU_SOURCE
25 # define _GNU_SOURCE    1
26 #endif
27
28 #if ! defined __builtin_expect && __GNUC__ < 3
29 # define __builtin_expect(expr, expected) (expr)
30 #endif
31
32 #include <fnmatch.h>
33
34 #include <alloca.h>
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <stddef.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #define WIDE_CHAR_SUPPORT \
44   (HAVE_WCTYPE_H && HAVE_BTOWC && HAVE_ISWCTYPE \
45    && HAVE_WMEMCHR && (HAVE_WMEMCPY || HAVE_WMEMPCPY))
46
47 /* For platform which support the ISO C amendement 1 functionality we
48    support user defined character classes.  */
49 #if defined _LIBC || WIDE_CHAR_SUPPORT
50 # include <wctype.h>
51 # include <wchar.h>
52 #endif
53
54 /* We need some of the locale data (the collation sequence information)
55    but there is no interface to get this information in general.  Therefore
56    we support a correct implementation only in glibc.  */
57 #ifdef _LIBC
58 # include "../locale/localeinfo.h"
59 # include "../locale/elem-hash.h"
60 # include "../locale/coll-lookup.h"
61 # include <shlib-compat.h>
62
63 # define CONCAT(a,b) __CONCAT(a,b)
64 # define mbsrtowcs __mbsrtowcs
65 # define fnmatch __fnmatch
66 extern int fnmatch (const char *pattern, const char *string, int flags);
67 #endif
68
69 #ifndef SIZE_MAX
70 # define SIZE_MAX ((size_t) -1)
71 #endif
72
73 /* We often have to test for FNM_FILE_NAME and FNM_PERIOD being both set.  */
74 #define NO_LEADING_PERIOD(flags) \
75   ((flags & (FNM_FILE_NAME | FNM_PERIOD)) == (FNM_FILE_NAME | FNM_PERIOD))
76
77 /* Comment out all this code if we are using the GNU C Library, and are not
78    actually compiling the library itself, and have not detected a bug
79    in the library.  This code is part of the GNU C
80    Library, but also included in many other GNU distributions.  Compiling
81    and linking in this code is a waste when using the GNU C library
82    (especially if it is a shared library).  Rather than having every GNU
83    program understand `configure --with-gnu-libc' and omit the object files,
84    it is simpler to just do this in the source for each such file.  */
85
86 #if defined _LIBC || !defined __GNU_LIBRARY__ || !HAVE_FNMATCH_GNU
87
88
89 # if ! (defined isblank || (HAVE_ISBLANK && HAVE_DECL_ISBLANK))
90 #  define isblank(c) ((c) == ' ' || (c) == '\t')
91 # endif
92
93 # define STREQ(s1, s2) (strcmp (s1, s2) == 0)
94
95 # if defined _LIBC || WIDE_CHAR_SUPPORT
96 /* The GNU C library provides support for user-defined character classes
97    and the functions from ISO C amendement 1.  */
98 #  ifdef CHARCLASS_NAME_MAX
99 #   define CHAR_CLASS_MAX_LENGTH CHARCLASS_NAME_MAX
100 #  else
101 /* This shouldn't happen but some implementation might still have this
102    problem.  Use a reasonable default value.  */
103 #   define CHAR_CLASS_MAX_LENGTH 256
104 #  endif
105
106 #  ifdef _LIBC
107 #   define IS_CHAR_CLASS(string) __wctype (string)
108 #  else
109 #   define IS_CHAR_CLASS(string) wctype (string)
110 #  endif
111
112 #  ifdef _LIBC
113 #   define ISWCTYPE(WC, WT)     __iswctype (WC, WT)
114 #  else
115 #   define ISWCTYPE(WC, WT)     iswctype (WC, WT)
116 #  endif
117
118 #  if (HAVE_MBSTATE_T && HAVE_MBSRTOWCS) || _LIBC
119 /* In this case we are implementing the multibyte character handling.  */
120 #   define HANDLE_MULTIBYTE     1
121 #  endif
122
123 # else
124 #  define CHAR_CLASS_MAX_LENGTH  6 /* Namely, `xdigit'.  */
125
126 #  define IS_CHAR_CLASS(string)                                               \
127    (STREQ (string, "alpha") || STREQ (string, "upper")                        \
128     || STREQ (string, "lower") || STREQ (string, "digit")                     \
129     || STREQ (string, "alnum") || STREQ (string, "xdigit")                    \
130     || STREQ (string, "space") || STREQ (string, "print")                     \
131     || STREQ (string, "punct") || STREQ (string, "graph")                     \
132     || STREQ (string, "cntrl") || STREQ (string, "blank"))
133 # endif
134
135 /* Avoid depending on library functions or files
136    whose names are inconsistent.  */
137
138 /* Global variable.  */
139 static int posixly_correct;
140
141 # ifndef internal_function
142 /* Inside GNU libc we mark some function in a special way.  In other
143    environments simply ignore the marking.  */
144 #  define internal_function
145 # endif
146
147 /* Note that this evaluates C many times.  */
148 # define FOLD(c) ((flags & FNM_CASEFOLD) ? tolower (c) : (c))
149 # define CHAR   char
150 # define UCHAR  unsigned char
151 # define INT    int
152 # define FCT    internal_fnmatch
153 # define EXT    ext_match
154 # define END    end_pattern
155 # define L_(CS) CS
156 # ifdef _LIBC
157 #  define BTOWC(C)      __btowc (C)
158 # else
159 #  define BTOWC(C)      btowc (C)
160 # endif
161 # define STRLEN(S) strlen (S)
162 # define STRCAT(D, S) strcat (D, S)
163 # ifdef _LIBC
164 #  define MEMPCPY(D, S, N) __mempcpy (D, S, N)
165 # else
166 #  if HAVE_MEMPCPY
167 #   define MEMPCPY(D, S, N) mempcpy (D, S, N)
168 #  else
169 #   define MEMPCPY(D, S, N) ((void *) ((char *) memcpy (D, S, N) + (N)))
170 #  endif
171 # endif
172 # define MEMCHR(S, C, N) memchr (S, C, N)
173 # define STRCOLL(S1, S2) strcoll (S1, S2)
174 # include "fnmatch_loop.c"
175
176
177 # if HANDLE_MULTIBYTE
178 #  define FOLD(c) ((flags & FNM_CASEFOLD) ? towlower (c) : (c))
179 #  define CHAR  wchar_t
180 #  define UCHAR wint_t
181 #  define INT   wint_t
182 #  define FCT   internal_fnwmatch
183 #  define EXT   ext_wmatch
184 #  define END   end_wpattern
185 #  define L_(CS)        L##CS
186 #  define BTOWC(C)      (C)
187 #  ifdef _LIBC
188 #   define STRLEN(S) __wcslen (S)
189 #   define STRCAT(D, S) __wcscat (D, S)
190 #   define MEMPCPY(D, S, N) __wmempcpy (D, S, N)
191 #  else
192 #   define STRLEN(S) wcslen (S)
193 #   define STRCAT(D, S) wcscat (D, S)
194 #   if HAVE_WMEMPCPY
195 #    define MEMPCPY(D, S, N) wmempcpy (D, S, N)
196 #   else
197 #    define MEMPCPY(D, S, N) (wmemcpy (D, S, N) + (N))
198 #   endif
199 #  endif
200 #  define MEMCHR(S, C, N) wmemchr (S, C, N)
201 #  define STRCOLL(S1, S2) wcscoll (S1, S2)
202 #  define WIDE_CHAR_VERSION 1
203
204 #  undef IS_CHAR_CLASS
205 /* We have to convert the wide character string in a multibyte string.  But
206    we know that the character class names consist of alphanumeric characters
207    from the portable character set, and since the wide character encoding
208    for a member of the portable character set is the same code point as
209    its single-byte encoding, we can use a simplified method to convert the
210    string to a multibyte character string.  */
211 static wctype_t
212 is_char_class (const wchar_t *wcs)
213 {
214   char s[CHAR_CLASS_MAX_LENGTH + 1];
215   char *cp = s;
216
217   do
218     {
219       /* Test for a printable character from the portable character set.  */
220 #  ifdef _LIBC
221       if (*wcs < 0x20 || *wcs > 0x7e
222           || *wcs == 0x24 || *wcs == 0x40 || *wcs == 0x60)
223         return (wctype_t) 0;
224 #  else
225       switch (*wcs)
226         {
227         case L' ': case L'!': case L'"': case L'#': case L'%':
228         case L'&': case L'\'': case L'(': case L')': case L'*':
229         case L'+': case L',': case L'-': case L'.': case L'/':
230         case L'0': case L'1': case L'2': case L'3': case L'4':
231         case L'5': case L'6': case L'7': case L'8': case L'9':
232         case L':': case L';': case L'<': case L'=': case L'>':
233         case L'?':
234         case L'A': case L'B': case L'C': case L'D': case L'E':
235         case L'F': case L'G': case L'H': case L'I': case L'J':
236         case L'K': case L'L': case L'M': case L'N': case L'O':
237         case L'P': case L'Q': case L'R': case L'S': case L'T':
238         case L'U': case L'V': case L'W': case L'X': case L'Y':
239         case L'Z':
240         case L'[': case L'\\': case L']': case L'^': case L'_':
241         case L'a': case L'b': case L'c': case L'd': case L'e':
242         case L'f': case L'g': case L'h': case L'i': case L'j':
243         case L'k': case L'l': case L'm': case L'n': case L'o':
244         case L'p': case L'q': case L'r': case L's': case L't':
245         case L'u': case L'v': case L'w': case L'x': case L'y':
246         case L'z': case L'{': case L'|': case L'}': case L'~':
247           break;
248         default:
249           return (wctype_t) 0;
250         }
251 #  endif
252
253       /* Avoid overrunning the buffer.  */
254       if (cp == s + CHAR_CLASS_MAX_LENGTH)
255         return (wctype_t) 0;
256
257       *cp++ = (char) *wcs++;
258     }
259   while (*wcs != L'\0');
260
261   *cp = '\0';
262
263 #  ifdef _LIBC
264   return __wctype (s);
265 #  else
266   return wctype (s);
267 #  endif
268 }
269 #  define IS_CHAR_CLASS(string) is_char_class (string)
270
271 #  include "fnmatch_loop.c"
272 # endif
273
274
275 int
276 fnmatch (const char *pattern, const char *string, int flags)
277 {
278 # if HANDLE_MULTIBYTE
279 #  define ALLOCA_LIMIT 2000
280   if (__builtin_expect (MB_CUR_MAX, 1) != 1)
281     {
282       mbstate_t ps;
283       size_t patsize;
284       size_t strsize;
285       size_t totsize;
286       wchar_t *wpattern;
287       wchar_t *wstring;
288       int res;
289
290       /* Calculate the size needed to convert the strings to
291          wide characters.  */
292       memset (&ps, '\0', sizeof (ps));
293       patsize = mbsrtowcs (NULL, &pattern, 0, &ps) + 1;
294       if (__builtin_expect (patsize != 0, 1))
295         {
296           assert (mbsinit (&ps));
297           strsize = mbsrtowcs (NULL, &string, 0, &ps) + 1;
298           if (__builtin_expect (strsize != 0, 1))
299             {
300               assert (mbsinit (&ps));
301               totsize = patsize + strsize;
302               if (__builtin_expect (! (patsize <= totsize
303                                        && totsize <= SIZE_MAX / sizeof (wchar_t)),
304                                     0))
305                 {
306                   errno = ENOMEM;
307                   return -1;
308                 }
309
310               /* Allocate room for the wide characters.  */
311               if (__builtin_expect (totsize < ALLOCA_LIMIT, 1))
312                 wpattern = (wchar_t *) alloca (totsize * sizeof (wchar_t));
313               else
314                 {
315                   wpattern = malloc (totsize * sizeof (wchar_t));
316                   if (__builtin_expect (! wpattern, 0))
317                     {
318                       errno = ENOMEM;
319                       return -1;
320                     }
321                 }
322               wstring = wpattern + patsize;
323
324               /* Convert the strings into wide characters.  */
325               mbsrtowcs (wpattern, &pattern, patsize, &ps);
326               assert (mbsinit (&ps));
327               mbsrtowcs (wstring, &string, strsize, &ps);
328
329               res = internal_fnwmatch (wpattern, wstring, wstring + strsize - 1,
330                                        flags & FNM_PERIOD, flags);
331
332               if (__builtin_expect (! (totsize < ALLOCA_LIMIT), 0))
333                 free (wpattern);
334               return res;
335             }
336         }
337     }
338
339 # endif /* HANDLE_MULTIBYTE */
340
341   return internal_fnmatch (pattern, string, string + strlen (string),
342                            flags & FNM_PERIOD, flags);
343 }
344
345 # ifdef _LIBC
346 #  undef fnmatch
347 versioned_symbol (libc, __fnmatch, fnmatch, GLIBC_2_2_3);
348 #  if SHLIB_COMPAT(libc, GLIBC_2_0, GLIBC_2_2_3)
349 strong_alias (__fnmatch, __fnmatch_old)
350 compat_symbol (libc, __fnmatch_old, fnmatch, GLIBC_2_0);
351 #  endif
352 libc_hidden_ver (__fnmatch, fnmatch)
353 # endif
354
355 #endif  /* _LIBC or not __GNU_LIBRARY__.  */