summaryrefslogtreecommitdiffstats
path: root/lib/glob/gm_loop.c
blob: ac516f82c785766eff1274b8b0c08851516bf0fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/* Copyright (C) 1991-2017 Free Software Foundation, Inc.

   This file is part of GNU Bash, the Bourne Again SHell.
   
   Bash is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Bash is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
*/

#if EXTENDED_GLOB
int
EXTGLOB_PATTERN_P (pat)
     const CHAR *pat;
{
  switch (pat[0])
    {
    case L('*'):
    case L('+'):
    case L('!'):
    case L('@'):
    case L('?'):
      return (pat[1] == L('('));	/* ) */
    default:
      return 0;
    }
    
  return 0;
}
#endif

/* Return 1 of the first character of STRING could match the first
   character of pattern PAT.  Compiled to both single and wiide character
   versions.  FLAGS is a subset of strmatch flags; used to do case-insensitive
   matching for now. */
int
MATCH_PATTERN_CHAR (pat, string, flags)
     CHAR *pat, *string;
     int flags;
{
  CHAR c;

  if (*string == 0)
    return (*pat == L('*'));	/* XXX  - allow only * to match empty string */

  switch (c = *pat++)
    {
    default:
      return (FOLD(*string) == FOLD(c));
    case L('\\'):
      return (FOLD(*string) == FOLD(*pat));
    case L('?'):
      return (*pat == L('(') ? 1 : (*string != L'\0'));
    case L('*'):
      return (1);
    case L('+'):
    case L('!'):
    case L('@'):
      return (*pat ==  L('(') ? 1 : (FOLD(*string) == FOLD(c)));
    case L('['):
      return (*string != L('\0'));
    }
}

int
MATCHLEN (pat, max)
     CHAR *pat;
     size_t max;
{
  CHAR c;
  int matlen, bracklen, t, in_cclass, in_collsym, in_equiv;

  if (*pat == 0)
    return (0);

  matlen = in_cclass = in_collsym = in_equiv = 0;
  while (c = *pat++)
    {
      switch (c)
	{
	default:
	  matlen++;
	  break;
	case L('\\'):
	  if (*pat == 0)
	    return ++matlen;
	  else
	    {
	      matlen++;
	      pat++;
	    }
	  break;
	case L('?'):
	  if (*pat == LPAREN)
	    return (matlen = -1);		/* XXX for now */
	  else
	    matlen++;
	  break;
	case L('*'):
	  return (matlen = -1);
	case L('+'):
	case L('!'):
	case L('@'):
	  if (*pat == LPAREN)
	    return (matlen = -1);		/* XXX for now */
	  else
	    matlen++;
	  break;
	case L('['):
	  /* scan for ending `]', skipping over embedded [:...:] */
	  bracklen = 1;
	  c = *pat++;
	  do
	    {
	      if (c == 0)
		{
		  pat--;			/* back up to NUL */
	          matlen += bracklen;
	          goto bad_bracket;
	        }
	      else if (c == L('\\'))
		{
		  /* *pat == backslash-escaped character */
		  bracklen++;
		  /* If the backslash or backslash-escape ends the string,
		     bail.  The ++pat skips over the backslash escape */
		  if (*pat == 0 || *++pat == 0)
		    {
		      matlen += bracklen;
		      goto bad_bracket;
		    }
		}
	      else if (c == L('[') && *pat == L(':'))	/* character class */
		{
		  pat++;
		  bracklen++;
		  in_cclass = 1;
		}
	      else if (in_cclass && c == L(':') && *pat == L(']'))
		{
		  pat++;
		  bracklen++;
		  in_cclass = 0;
		}
	      else if (c == L('[') && *pat == L('.'))	/* collating symbol */
		{
		  pat++;
		  bracklen++;
		  if (*pat == L(']'))	/* right bracket can appear as collating symbol */
		    {
		      pat++;
		      bracklen++;
		    }
		  in_collsym = 1;
		}
	      else if (in_collsym && c == L('.') && *pat == L(']'))
		{
		  pat++;
		  bracklen++;
		  in_collsym = 0;
		}
	      else if (c == L('[') && *pat == L('='))	/* equivalence class */
		{
		  pat++;
		  bracklen++;
		  if (*pat == L(']'))	/* right bracket can appear as equivalence class */
		    {
		      pat++;
		      bracklen++;
		    }
		  in_equiv = 1;
		}
	      else if (in_equiv && c == L('=') && *pat == L(']'))
		{
		  pat++;
		  bracklen++;
		  in_equiv = 0;
		}
	      else
		bracklen++;
	    }
	  while ((c = *pat++) != L(']'));
	  matlen++;		/* bracket expression can only match one char */
bad_bracket:
	  break;
	}
    }

  return matlen;
}

#undef EXTGLOB_PATTERN_P
#undef MATCH_PATTERN_CHAR
#undef MATCHLEN
#undef FOLD
#undef L
#undef LPAREN
#undef RPAREN
#undef INT
#undef CHAR