2 * Copyright (C) 1984-2019 Mark Nudelman
4 * You may distribute under the terms of either the GNU General Public
5 * License or the Less License, as specified in the README file.
7 * For more information, see the README file.
11 * Routines to do pattern matching.
20 * Compile a search pattern, for future use by match_pattern.
23 compile_pattern2(pattern, search_type, comp_pattern, show_error)
26 PATTERN_TYPE *comp_pattern;
29 if (search_type & SRCH_NO_REGEX)
33 struct re_pattern_buffer *comp = (struct re_pattern_buffer *)
34 ecalloc(1, sizeof(struct re_pattern_buffer));
35 re_set_syntax(RE_SYNTAX_POSIX_EXTENDED);
36 if (re_compile_pattern(pattern, strlen(pattern), comp))
40 error("Invalid pattern", NULL_PARG);
43 if (*comp_pattern != NULL)
45 regfree(*comp_pattern);
50 #if HAVE_POSIX_REGCOMP
51 regex_t *comp = (regex_t *) ecalloc(1, sizeof(regex_t));
52 if (regcomp(comp, pattern, REGCOMP_FLAG))
56 error("Invalid pattern", NULL_PARG);
59 if (*comp_pattern != NULL)
61 regfree(*comp_pattern);
67 constant char *errstring;
70 pcre *comp = pcre_compile(pattern,
71 (utf_mode) ? PCRE_UTF8 | PCRE_NO_UTF8_CHECK : 0,
72 &errstring, &erroffset, NULL);
75 parg.p_string = (char *) errstring;
86 pcre2_code *comp = pcre2_compile((PCRE2_SPTR)pattern, strlen(pattern),
87 0, &errcode, &erroffset, NULL);
93 pcre2_get_error_message(errcode, (PCRE2_UCHAR*)msg, sizeof(msg));
103 if ((parg.p_string = re_comp(pattern)) != NULL)
113 if ((comp = regcmp(pattern, 0)) == NULL)
116 error("Invalid pattern", NULL_PARG);
119 if (comp_pattern != NULL)
121 *comp_pattern = comp;
125 reg_show_error = show_error;
126 comp = regcomp(pattern);
131 * regcomp has already printed an error message
136 if (*comp_pattern != NULL)
138 *comp_pattern = comp;
145 * Like compile_pattern2, but convert the pattern to lowercase if necessary.
148 compile_pattern(pattern, search_type, comp_pattern)
151 PATTERN_TYPE *comp_pattern;
156 if (caseless != OPT_ONPLUS)
157 cvt_pattern = pattern;
160 cvt_pattern = (char*) ecalloc(1, cvt_length(strlen(pattern), CVT_TO_LC));
161 cvt_text(cvt_pattern, pattern, (int *)NULL, (int *)NULL, CVT_TO_LC);
163 result = compile_pattern2(cvt_pattern, search_type, comp_pattern, 1);
164 if (cvt_pattern != pattern)
170 * Forget that we have a compiled pattern.
173 uncompile_pattern(pattern)
174 PATTERN_TYPE *pattern;
177 if (*pattern != NULL)
184 #if HAVE_POSIX_REGCOMP
185 if (*pattern != NULL)
193 if (*pattern != NULL)
198 if (*pattern != NULL)
199 pcre2_code_free(*pattern);
206 if (*pattern != NULL)
211 if (*pattern != NULL)
218 * Can a pattern be successfully compiled?
221 valid_pattern(pattern)
224 PATTERN_TYPE comp_pattern;
227 CLEAR_PATTERN(comp_pattern);
228 result = compile_pattern2(pattern, 0, &comp_pattern, 0);
231 uncompile_pattern(&comp_pattern);
236 * Is a compiled pattern null?
239 is_null_pattern(pattern)
240 PATTERN_TYPE pattern;
243 return (pattern == NULL);
245 #if HAVE_POSIX_REGCOMP
246 return (pattern == NULL);
249 return (pattern == NULL);
252 return (pattern == NULL);
255 return (pattern == 0);
258 return (pattern == NULL);
261 return (pattern == NULL);
264 return (pattern == NULL);
269 * Simple pattern matching function.
270 * It supports no metacharacters like *, etc.
273 match(pattern, pattern_len, buf, buf_len, pfound, pend)
278 char **pfound, **pend;
281 char *pattern_end = pattern + pattern_len;
282 char *buf_end = buf + buf_len;
284 for ( ; buf < buf_end; buf++)
286 for (pp = pattern, lp = buf; ; pp++, lp++)
290 if (caseless == OPT_ONPLUS && ASCII_IS_UPPER(cp))
291 cp = ASCII_TO_LOWER(cp);
294 if (pp == pattern_end || lp == buf_end)
297 if (pp == pattern_end)
310 * Perform a pattern match with the previously compiled pattern.
311 * Set sp and ep to the start and end of the matched string.
314 match_pattern(pattern, tpattern, line, line_len, sp, ep, notbol, search_type)
315 PATTERN_TYPE pattern;
328 search_type |= SRCH_NO_REGEX;
330 if (search_type & SRCH_NO_REGEX)
331 matched = match(tpattern, strlen(tpattern), line, line_len, sp, ep);
336 struct re_registers search_regs;
337 pattern->not_bol = notbol;
338 pattern->regs_allocated = REGS_UNALLOCATED;
339 matched = re_search(pattern, line, line_len, 0, line_len, &search_regs) >= 0;
342 *sp = line + search_regs.start[0];
343 *ep = line + search_regs.end[0];
347 #if HAVE_POSIX_REGCOMP
350 int flags = (notbol) ? REG_NOTBOL : 0;
352 flags |= REG_STARTEND;
356 matched = !regexec(pattern, line, 1, &rm, flags);
360 *sp = line + rm.rm_so;
361 *ep = line + rm.rm_eo;
371 int flags = (notbol) ? PCRE_NOTBOL : 0;
373 matched = pcre_exec(pattern, NULL, line, line_len,
374 0, flags, ovector, 3) >= 0;
377 *sp = line + ovector[0];
378 *ep = line + ovector[1];
384 int flags = (notbol) ? PCRE2_NOTBOL : 0;
385 pcre2_match_data *md = pcre2_match_data_create(3, NULL);
386 matched = pcre2_match(pattern, (PCRE2_SPTR)line, line_len,
387 0, flags, md, NULL) >= 0;
390 PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md);
391 *sp = line + ovector[0];
392 *ep = line + ovector[1];
394 pcre2_match_data_free(md);
398 matched = (re_exec(line) == 1);
400 * re_exec doesn't seem to provide a way to get the matched string.
405 *ep = regex(pattern, line);
406 matched = (*ep != NULL);
412 matched = regexec2(pattern, line, notbol);
414 matched = regexec(pattern, line);
418 *sp = pattern->startp[0];
419 *ep = pattern->endp[0];
423 matched = (!(search_type & SRCH_NO_MATCH) && matched) ||
424 ((search_type & SRCH_NO_MATCH) && !matched);
429 * Return the name of the pattern matching library.
432 pattern_lib_name(VOID_PARAM)
437 #if HAVE_POSIX_REGCOMP
453 return ("Spencer V8");