1 /* gmisc.c -- miscellaneous pattern matching utility functions for Bash.
3 Copyright (C) 2010 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne-Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #include "bashtypes.h"
25 #if defined (HAVE_UNISTD_H)
41 #if defined (HANDLE_MULTIBYTE)
45 extern char *glob_patscan __P((char *, char *, int));
47 /* Return 1 of the first character of WSTRING could match the first
48 character of pattern WPAT. Wide character version. */
50 match_pattern_wchar (wpat, wstring)
51 wchar_t *wpat, *wstring;
61 return (*wstring == wc);
63 return (*wstring == *wpat);
65 return (*wpat == WLPAREN ? 1 : (*wstring != L'\0'));
71 return (*wpat == WLPAREN ? 1 : (*wstring == wc));
73 return (*wstring != L'\0');
78 wmatchlen (wpat, wmax)
83 int matlen, bracklen, t, in_cclass, in_collsym, in_equiv;
88 matlen = in_cclass = in_collsym = in_equiv = 0;
106 if (*wpat == WLPAREN)
107 return (matlen = -1); /* XXX for now */
112 return (matlen = -1);
116 if (*wpat == WLPAREN)
117 return (matlen = -1); /* XXX for now */
122 /* scan for ending `]', skipping over embedded [:...:] */
129 wpat--; /* back up to NUL */
133 else if (wc == L'\\')
135 /* *wpat == backslash-escaped character */
137 /* If the backslash or backslash-escape ends the string,
138 bail. The ++wpat skips over the backslash escape */
139 if (*wpat == 0 || *++wpat == 0)
145 else if (wc == L'[' && *wpat == L':') /* character class */
151 else if (in_cclass && wc == L':' && *wpat == L']')
157 else if (wc == L'[' && *wpat == L'.') /* collating symbol */
161 if (*wpat == L']') /* right bracket can appear as collating symbol */
168 else if (in_collsym && wc == L'.' && *wpat == L']')
174 else if (wc == L'[' && *wpat == L'=') /* equivalence class */
178 if (*wpat == L']') /* right bracket can appear as equivalence class */
185 else if (in_equiv && wc == L'=' && *wpat == L']')
194 while ((wc = *wpat++) != L']');
195 matlen++; /* bracket expression can only match one char */
206 extglob_pattern_p (pat)
216 return (pat[1] == LPAREN);
224 /* Return 1 of the first character of STRING could match the first
225 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
227 match_pattern_char (pat, string)
238 return (*string == c);
240 return (*string == *pat);
242 return (*pat == LPAREN ? 1 : (*string != '\0'));
248 return (*pat == LPAREN ? 1 : (*string == c));
250 return (*string != '\0');
260 int matlen, bracklen, t, in_cclass, in_collsym, in_equiv;
265 matlen = in_cclass = in_collsym = in_equiv = 0;
284 return (matlen = -1); /* XXX for now */
289 return (matlen = -1);
294 return (matlen = -1); /* XXX for now */
299 /* scan for ending `]', skipping over embedded [:...:] */
306 pat--; /* back up to NUL */
312 /* *pat == backslash-escaped character */
314 /* If the backslash or backslash-escape ends the string,
315 bail. The ++pat skips over the backslash escape */
316 if (*pat == 0 || *++pat == 0)
322 else if (c == '[' && *pat == ':') /* character class */
328 else if (in_cclass && c == ':' && *pat == ']')
334 else if (c == '[' && *pat == '.') /* collating symbol */
338 if (*pat == ']') /* right bracket can appear as collating symbol */
345 else if (in_collsym && c == '.' && *pat == ']')
351 else if (c == '[' && *pat == '=') /* equivalence class */
355 if (*pat == ']') /* right bracket can appear as equivalence class */
362 else if (in_equiv && c == '=' && *pat == ']')
371 while ((c = *pat++) != ']');
372 matlen++; /* bracket expression can only match one char */
381 /* Skip characters in PAT and return the final occurrence of DIRSEP. This
382 is only called when extended_glob is set, so we have to skip over extglob
385 glob_dirscan (pat, dirsep)
389 char *p, *d, *pe, *se;
392 for (p = pat; p && *p; p++)
394 if (extglob_pattern_p (p))
397 se = p + strlen (p) - 1;
398 pe = glob_patscan (p + 2, se, 0);
403 p = pe - 1; /* will do increment above */