init version.
[bush.git] / lib / glob / glob_loop.c
blob2e57ffecfc168590b82ab971e11afccff9badc2f
1 /* Copyright (C) 1991-2020 Free Software Foundation, Inc.
3 This file is part of GNU Bush, the Bourne Again SHell.
5 Bush 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.
10 Bush 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.
15 You should have received a copy of the GNU General Public License
16 along with Bush. If not, see <http://www.gnu.org/licenses/>.
19 static int INTERNAL_GLOB_PATTERN_P PARAMS((const GCHAR *));
21 /* Return nonzero if PATTERN has any special globbing chars in it.
22 Compiled twice, once each for single-byte and multibyte characters. */
23 static int
24 INTERNAL_GLOB_PATTERN_P (pattern)
25 const GCHAR *pattern;
27 register const GCHAR *p;
28 register GCHAR c;
29 int bopen, bsquote;
31 p = pattern;
32 bopen = bsquote = 0;
34 while ((c = *p++) != L('\0'))
35 switch (c)
37 case L('?'):
38 case L('*'):
39 return 1;
41 case L('['): /* Only accept an open brace if there is a close */
42 bopen++; /* brace to match it. Bracket expressions must be */
43 continue; /* complete, according to Posix.2 */
44 case L(']'):
45 if (bopen)
46 return 1;
47 continue;
49 case L('+'): /* extended matching operators */
50 case L('@'):
51 case L('!'):
52 if (*p == L('(')) /*) */
53 return 1;
54 continue;
56 case L('\\'):
57 /* Don't let the pattern end in a backslash (GMATCH returns no match
58 if the pattern ends in a backslash anyway), but otherwise note that
59 we have seen this, since the matching engine uses backslash as an
60 escape character and it can be removed. We return 2 later if we
61 have seen only backslash-escaped characters, so interested callers
62 know they can shortcut and just dequote the pathname. */
63 if (*p != L('\0'))
65 p++;
66 bsquote = 1;
67 continue;
69 else /* (*p == L('\0')) */
70 return 0;
73 #if 0
74 return bsquote ? 2 : 0;
75 #else
76 return (0);
77 #endif
80 #undef INTERNAL_GLOB_PATTERN_P
81 #undef L
82 #undef INT
83 #undef CHAR
84 #undef GCHAR