1 /* Pattern Matchers for Regular Expressions.
2 Copyright (C) 1992, 1998, 2000, 2005-2006, 2010, 2015 Free Software
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 of the License, or
8 (at your option) any later version.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>. */
35 #if defined (STDC_HEADERS) || (!defined (isascii) && !defined (HAVE_ISASCII))
36 # define IN_CTYPE_DOMAIN(c) 1
38 # define IN_CTYPE_DOMAIN(c) isascii(c)
40 #define ISALNUM(C) (IN_CTYPE_DOMAIN (C) && isalnum (C))
41 #define IS_WORD_CONSTITUENT(C) (ISALNUM(C) || (C) == '_')
45 /* Regex compiled regexp. */
46 struct re_pattern_buffer regexbuf;
47 struct re_registers regs; /* This is here on account of a BRAIN-DEAD
48 Q@#%!# library interface in regex.c. */
51 struct compiled_regex {
56 /* The Regex compiled patterns. */
57 struct patterns *patterns;
62 compile (const char *pattern, size_t pattern_size,
63 bool match_icase, bool match_words, bool match_lines, char eolbyte,
66 struct compiled_regex *cregex;
68 cregex = XMALLOC (struct compiled_regex);
69 memset (cregex, '\0', sizeof (struct compiled_regex));
70 cregex->match_words = match_words;
71 cregex->match_lines = match_lines;
72 cregex->eolbyte = eolbyte;
73 cregex->patterns = NULL;
76 re_set_syntax (syntax);
78 /* For GNU regex compiler we have to pass the patterns separately to detect
79 errors like "[\nallo\n]\n". The patterns here are "[", "allo" and "]"
80 GNU regex should have raised a syntax error. The same for backref, where
81 the backref should have been local to each pattern. */
84 size_t total = pattern_size;
85 const char *motif = pattern;
92 sep = (const char *) memchr (motif, '\n', total);
105 cregex->patterns = xrealloc (cregex->patterns, (cregex->pcount + 1) * sizeof (struct patterns));
106 memset (&cregex->patterns[cregex->pcount], '\0', sizeof (struct patterns));
108 if ((err = re_compile_pattern (motif, len,
109 &cregex->patterns[cregex->pcount].regexbuf)) != NULL)
110 error (exit_failure, 0, "%s", err);
115 while (sep && total != 0);
122 Gcompile (const char *pattern, size_t pattern_size,
123 bool match_icase, bool match_words, bool match_lines, char eolbyte)
125 return compile (pattern, pattern_size,
126 match_icase, match_words, match_lines, eolbyte,
127 RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE);
131 Ecompile (const char *pattern, size_t pattern_size,
132 bool match_icase, bool match_words, bool match_lines, char eolbyte)
134 return compile (pattern, pattern_size,
135 match_icase, match_words, match_lines, eolbyte,
136 RE_SYNTAX_POSIX_EGREP);
140 AWKcompile (const char *pattern, size_t pattern_size,
141 bool match_icase, bool match_words, bool match_lines, char eolbyte)
143 return compile (pattern, pattern_size,
144 match_icase, match_words, match_lines, eolbyte,
149 EGexecute (const void *compiled_pattern,
150 const char *buf, size_t buf_size,
151 size_t *match_size, bool exact)
153 struct compiled_regex *cregex = (struct compiled_regex *) compiled_pattern;
154 char eol = cregex->eolbyte;
155 register const char *buflim = buf + buf_size;
156 register const char *beg;
157 register const char *end;
159 for (beg = buf; beg < buflim; beg = end)
163 end = (const char *) memchr (beg, eol, buflim - beg);
166 /* Here, either end < buflim && *end == eol, or end == buflim. */
168 for (i = 0; i < cregex->pcount; i++)
172 cregex->patterns[i].regexbuf.not_eol = 0;
173 if (0 <= (start = re_search (&cregex->patterns[i].regexbuf, beg,
175 end - beg, &cregex->patterns[i].regs)))
177 len = cregex->patterns[i].regs.end[0] - start;
183 if (cregex->match_lines)
185 if (len == end - beg) /* implies start == 0 */
188 else if (cregex->match_words)
190 /* If -w, check if the match aligns with word boundaries.
191 We do this iteratively because:
192 (a) the line may contain more than one occurence of the
194 (b) Several alternatives in the pattern might be valid at
195 a given point, and we may need to consider a shorter
196 one to find a word boundary. */
199 if ((start == 0 || !IS_WORD_CONSTITUENT ((unsigned char) beg[start - 1]))
200 && (start + len == end - beg
201 || !IS_WORD_CONSTITUENT ((unsigned char) beg[start + len])))
205 /* Try a shorter length anchored at the same place. */
207 cregex->patterns[i].regexbuf.not_eol = 1;
208 len = re_match (&cregex->patterns[i].regexbuf, beg,
210 &cregex->patterns[i].regs);
214 /* Try looking further on. */
215 if (start == end - beg)
218 cregex->patterns[i].regexbuf.not_eol = 0;
219 start = re_search (&cregex->patterns[i].regexbuf, beg,
221 start, end - beg - start,
222 &cregex->patterns[i].regs);
223 len = cregex->patterns[i].regs.end[0] - start;
238 *match_size = end - beg;
243 EGfree (void *compiled_pattern)
245 struct compiled_regex *cregex = (struct compiled_regex *) compiled_pattern;
247 free (cregex->patterns);
251 /* POSIX Basic Regular Expressions */
252 matcher_t matcher_grep =
259 /* POSIX Extended Regular Expressions */
260 matcher_t matcher_egrep =
267 /* AWK Regular Expressions */
268 matcher_t matcher_awk =