Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCRE support #1143

Merged
merged 1 commit into from
Oct 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions INSTALL.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
23 changes: 22 additions & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions include/tig/tig.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,13 @@
#include <libgen.h>
#include <termios.h>

#if defined HAVE_PCRE2
#include <pcre2posix.h>
#elif defined HAVE_PCRE
#include <pcreposix.h>
#else
#include <regex.h>
#endif

#include <locale.h>
#include <langinfo.h>
Expand Down
10 changes: 6 additions & 4 deletions src/search.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it is more readable to decouple the allocation from regular housekeeping needed when performing a new search.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes and also, maybe I should have mentioned it, PCRE has a slight tendency to segfault when using regfree() on view->regex before regcomp().

}

regex_err = regcomp(view->regex, view->env->search, REG_EXTENDED | regex_flags);
if (regex_err != 0) {
char buf[SIZEOF_STR] = "unknown error";
Expand Down