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 /* Return 1 of the first character of WSTRING could match the first
46 character of pattern WPAT. Wide character version. */
48 match_pattern_wchar (wpat, wstring)
49 wchar_t *wpat, *wstring;
59 return (*wstring == wc);
61 return (*wstring == *wpat);
63 return (*wpat == WLPAREN ? 1 : (*wstring != L'\0'));
69 return (*wpat == WLPAREN ? 1 : (*wstring == wc));
71 return (*wstring != L'\0');
76 wmatchlen (wpat, wmax)
81 int matlen, t, in_cclass, in_collsym, in_equiv;
86 matlen = in_cclass = in_collsym = in_equiv = 0;
104 if (*wpat == WLPAREN)
105 return (matlen = -1); /* XXX for now */
110 return (matlen = -1);
114 if (*wpat == WLPAREN)
115 return (matlen = -1); /* XXX for now */
120 /* scan for ending `]', skipping over embedded [:...:] */
127 matlen += wpat - wbrack - 1; /* incremented below */
130 else if (wc == L'\\')
136 else if (wc == L'[' && *wpat == L':') /* character class */
141 else if (in_cclass && wc == L':' && *wpat == L']')
146 else if (wc == L'[' && *wpat == L'.') /* collating symbol */
149 if (*wpat == L']') /* right bracket can appear as collating symbol */
153 else if (in_collsym && wc == L'.' && *wpat == L']')
158 else if (wc == L'[' && *wpat == L'=') /* equivalence class */
161 if (*wpat == L']') /* right bracket can appear as equivalence class */
165 else if (in_equiv && wc == L'=' && *wpat == L']')
171 while ((wc = *wpat++) != L']');
172 matlen++; /* bracket expression can only match one char */
181 /* Return 1 of the first character of STRING could match the first
182 character of pattern PAT. Used to avoid n2 calls to strmatch(). */
184 match_pattern_char (pat, string)
195 return (*string == c);
197 return (*string == *pat);
199 return (*pat == LPAREN ? 1 : (*string != '\0'));
205 return (*pat == LPAREN ? 1 : (*string == c));
207 return (*string != '\0');
217 int matlen, t, in_cclass, in_collsym, in_equiv;
222 matlen = in_cclass = in_collsym = in_equiv = 0;
241 return (matlen = -1); /* XXX for now */
246 return (matlen = -1);
251 return (matlen = -1); /* XXX for now */
256 /* scan for ending `]', skipping over embedded [:...:] */
263 matlen += pat - brack - 1; /* incremented below */
272 else if (c == '[' && *pat == ':') /* character class */
277 else if (in_cclass && c == ':' && *pat == ']')
282 else if (c == '[' && *pat == '.') /* collating symbol */
285 if (*pat == ']') /* right bracket can appear as collating symbol */
289 else if (in_collsym && c == '.' && *pat == ']')
294 else if (c == '[' && *pat == '=') /* equivalence class */
297 if (*pat == ']') /* right bracket can appear as equivalence class */
301 else if (in_equiv && c == '=' && *pat == ']')
307 while ((c = *pat++) != ']');
308 matlen++; /* bracket expression can only match one char */