From 3d069a8a5f46a399fcab297d1c6d1d7735d2b185 Mon Sep 17 00:00:00 2001 From: Thomas Koutcher Date: Sat, 4 Sep 2021 20:16:37 +0200 Subject: [PATCH] Add PCRE support Closes #1137 --- INSTALL.adoc | 2 ++ configure.ac | 23 ++++++++++++++++++++++- include/tig/tig.h | 6 ++++++ src/search.c | 10 ++++++---- 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/INSTALL.adoc b/INSTALL.adoc index 2b5c97427..287f399c5 100644 --- a/INSTALL.adoc +++ b/INSTALL.adoc @@ -193,6 +193,8 @@ configure script and building documentation: |Tool |Description |readline |Adds support for completion and history in search and command prompts. +|PCRE |Adds support for Perl Compatible Regular + Expressions in searches. |autoconf |Contains autoreconf for generating configure from configure.ac. |asciidoc (>= 8.4) |Generates HTML and (DocBook) XML from text. diff --git a/configure.ac b/configure.ac index b4ed8576b..ba03e5028 100644 --- a/configure.ac +++ b/configure.ac @@ -36,12 +36,33 @@ AX_LIB_READLINE(6.3) AM_ICONV +dnl Check for PCRE (Perl Compatible Regular Expressions) library +AC_ARG_WITH(pcre, [AS_HELP_STRING([--without-pcre], [do not use PCRE (Perl Compatible Regular Expressions) library])]) +AS_IF([test "x$with_pcre" != xno], [ + AC_CHECK_HEADERS([pcre2posix.h]) + AS_IF([test "x$ac_cv_header_pcre2posix_h" = xyes], [ + AC_CHECK_LIB([pcre2-posix], [regexec], [ + AC_DEFINE([HAVE_PCRE2], [1], [Define if you have PCRE2]) + LIBS="$LIBS -lpcre2-posix -lpcre2-8" + ]) + ]) + AS_IF([test "x$ac_cv_lib_pcre2_posix_regexec" != xyes], [ + AC_CHECK_HEADERS([pcreposix.h]) + AS_IF([test "x$ac_cv_header_pcreposix_h" = xyes], [ + AC_CHECK_LIB([pcreposix], [regexec], [ + AC_DEFINE([HAVE_PCRE], [1], [Define if you have PCRE]) + LIBS="$LIBS -lpcreposix -lpcre" + ]) + ]) + ]) +]) + dnl OS-specific case $(uname -s 2>/dev/null || echo unknown) in "OS400") AC_CHECK_LIB(util, main, [LIBS="$LIBS -lutil"], AC_MSG_ERROR([Please install the libutil-devel package])) ;; esac - + AC_CHECK_PROGS(SED, [gsed], [sed]) AC_TDD_GCOV AC_SUBST(COVERAGE_CFLAGS) diff --git a/include/tig/tig.h b/include/tig/tig.h index a3d856e05..08f63a4a3 100644 --- a/include/tig/tig.h +++ b/include/tig/tig.h @@ -56,7 +56,13 @@ #include #include +#if defined HAVE_PCRE2 +#include +#elif defined HAVE_PCRE +#include +#else #include +#endif #include #include diff --git a/src/search.c b/src/search.c index aa0404657..4f2ade51c 100644 --- a/src/search.c +++ b/src/search.c @@ -67,15 +67,17 @@ setup_and_find_next(struct view *view, enum request request) && !utf8_string_contains_uppercase(view->env->search)) regex_flags |= REG_ICASE; - if (view->regex) { - regfree(view->regex); - *view->grep = 0; - } else { + if (!view->regex) { view->regex = calloc(1, sizeof(*view->regex)); if (!view->regex) return ERROR_OUT_OF_MEMORY; } + if (*view->grep) { + regfree(view->regex); + *view->grep = 0; + } + regex_err = regcomp(view->regex, view->env->search, REG_EXTENDED | regex_flags); if (regex_err != 0) { char buf[SIZEOF_STR] = "unknown error";