aboutsummaryrefslogtreecommitdiff
path: root/pattern.c
diff options
context:
space:
mode:
authorXin LI <delphij@FreeBSD.org>2019-06-29 04:39:01 +0000
committerXin LI <delphij@FreeBSD.org>2019-06-29 04:39:01 +0000
commit6aa86b45318b321ba7e9cc3c221ab9062ddac4a0 (patch)
treeed38a1d35ad0e927ec1b5282deedc86014f9181d /pattern.c
parentb04073e3aef4b6c7e7d2755a7bbc098e5f2a03f8 (diff)
Diffstat (limited to 'pattern.c')
-rw-r--r--pattern.c85
1 files changed, 82 insertions, 3 deletions
diff --git a/pattern.c b/pattern.c
index 563bde0cc697..da27dc647adc 100644
--- a/pattern.c
+++ b/pattern.c
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 1984-2017 Mark Nudelman
+ * Copyright (C) 1984-2019 Mark Nudelman
*
* You may distribute under the terms of either the GNU General Public
* License or the Less License, as specified in the README file.
@@ -14,6 +14,7 @@
#include "less.h"
extern int caseless;
+extern int utf_mode;
/*
* Compile a search pattern, for future use by match_pattern.
@@ -63,11 +64,11 @@ compile_pattern2(pattern, search_type, comp_pattern, show_error)
*comp_pattern = comp;
#endif
#if HAVE_PCRE
- pcre *comp;
constant char *errstring;
int erroffset;
PARG parg;
- comp = pcre_compile(pattern, 0,
+ pcre *comp = pcre_compile(pattern,
+ (utf_mode) ? PCRE_UTF8 | PCRE_NO_UTF8_CHECK : 0,
&errstring, &erroffset, NULL);
if (comp == NULL)
{
@@ -78,6 +79,25 @@ compile_pattern2(pattern, search_type, comp_pattern, show_error)
}
*comp_pattern = comp;
#endif
+#if HAVE_PCRE2
+ int errcode;
+ PCRE2_SIZE erroffset;
+ PARG parg;
+ pcre2_code *comp = pcre2_compile((PCRE2_SPTR)pattern, strlen(pattern),
+ 0, &errcode, &erroffset, NULL);
+ if (comp == NULL)
+ {
+ if (show_error)
+ {
+ char msg[160];
+ pcre2_get_error_message(errcode, (PCRE2_UCHAR*)msg, sizeof(msg));
+ parg.p_string = msg;
+ error("%s", &parg);
+ }
+ return (-1);
+ }
+ *comp_pattern = comp;
+#endif
#if HAVE_RE_COMP
PARG parg;
if ((parg.p_string = re_comp(pattern)) != NULL)
@@ -174,6 +194,11 @@ uncompile_pattern(pattern)
pcre_free(*pattern);
*pattern = NULL;
#endif
+#if HAVE_PCRE2
+ if (*pattern != NULL)
+ pcre2_code_free(*pattern);
+ *pattern = NULL;
+#endif
#if HAVE_RE_COMP
*pattern = 0;
#endif
@@ -223,6 +248,9 @@ is_null_pattern(pattern)
#if HAVE_PCRE
return (pattern == NULL);
#endif
+#if HAVE_PCRE2
+ return (pattern == NULL);
+#endif
#if HAVE_RE_COMP
return (pattern == 0);
#endif
@@ -351,6 +379,21 @@ match_pattern(pattern, tpattern, line, line_len, sp, ep, notbol, search_type)
}
}
#endif
+#if HAVE_PCRE2
+ {
+ int flags = (notbol) ? PCRE2_NOTBOL : 0;
+ pcre2_match_data *md = pcre2_match_data_create(3, NULL);
+ matched = pcre2_match(pattern, (PCRE2_SPTR)line, line_len,
+ 0, flags, md, NULL) >= 0;
+ if (matched)
+ {
+ PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(md);
+ *sp = line + ovector[0];
+ *ep = line + ovector[1];
+ }
+ pcre2_match_data_free(md);
+ }
+#endif
#if HAVE_RE_COMP
matched = (re_exec(line) == 1);
/*
@@ -382,3 +425,39 @@ match_pattern(pattern, tpattern, line, line_len, sp, ep, notbol, search_type)
return (matched);
}
+/*
+ * Return the name of the pattern matching library.
+ */
+ public char *
+pattern_lib_name(VOID_PARAM)
+{
+#if HAVE_GNU_REGEX
+ return ("GNU");
+#else
+#if HAVE_POSIX_REGCOMP
+ return ("POSIX");
+#else
+#if HAVE_PCRE2
+ return ("PCRE2");
+#else
+#if HAVE_PCRE
+ return ("PCRE");
+#else
+#if HAVE_RE_COMP
+ return ("BSD");
+#else
+#if HAVE_REGCMP
+ return ("V8");
+#else
+#if HAVE_V8_REGCOMP
+ return ("Spencer V8");
+#else
+ return ("no");
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+}