From: Mark Wooding Date: Fri, 9 Aug 2019 10:55:20 +0000 (+0100) Subject: configure.ac, Makefile.am, pcre.c, regexp.c: Overhaul conditional building. X-Git-Url: https://git.distorted.org.uk/~mdw/anag/commitdiff_plain/7c2b74abc8bbc3b009a233b65d4f8aa23219892d configure.ac, Makefile.am, pcre.c, regexp.c: Overhaul conditional building. Use Automake conditions rather than `#ifdef'. Use `pkgconfig' to find PCRE. Tidy up the code a little. --- diff --git a/Makefile.am b/Makefile.am index 6374697..9425a68 100644 --- a/Makefile.am +++ b/Makefile.am @@ -30,6 +30,11 @@ SUFFIXES = bin_PROGRAMS = bin_SCRIPTS = +LDADD = +if HAVE_PCRE + LDADD += $(PCRE_LIBS) +endif + ###-------------------------------------------------------------------------- ### Making substitutions. @@ -54,8 +59,12 @@ anag_SOURCES = anag.c anag.h util.c anag_SOURCES += anagram.c anag_SOURCES += longest.c anag_SOURCES += mono.c -anag_SOURCES += pcre.c -anag_SOURCES += regexp.c +if HAVE_PCRE + anag_SOURCES += pcre.c +endif +if HAVE_REGCOMP + anag_SOURCES += regexp.c +endif anag_SOURCES += trackword.c anag_SOURCES += wildcard.c diff --git a/anag.h b/anag.h index 0223c54..6763d0f 100644 --- a/anag.h +++ b/anag.h @@ -95,8 +95,12 @@ extern node *subgram(const char *const */*av*/); extern node *wildcard(const char *const */*av*/); extern node *trackword(const char *const */*av*/); extern node *mono(const char *const */*av*/); -extern node *regexp(const char *const */*av*/); -extern node *pcrenode(const char *const */*av*/); +#ifdef HAVE_REGCOMP + extern node *regexp(const char *const */*av*/); +#endif +#ifdef HAVE_PCRE + extern node *pcrenode(const char *const */*av*/); +#endif extern node *longest(const char *const */*av*/); extern node *shortest(const char *const */*av*/); diff --git a/configure.ac b/configure.ac index f1cac47..272fe9c 100644 --- a/configure.ac +++ b/configure.ac @@ -40,9 +40,15 @@ dnl-------------------------------------------------------------------------- dnl C programming environment. AC_CHECK_FUNCS([regcomp]) - -AC_SEARCH_LIBS([pcre_fullinfo], [pcre], - [AC_DEFINE([HAVE_PCRE], [1], [Define if you have libpcre available.])]) +AM_CONDITIONAL([HAVE_REGCOMP], [test $ac_cv_func_regcomp = yes]) + +mdw_have_pcre2=nil mdw_have_pcre=nil +PKG_CHECK_MODULES([PCRE], [libpcre], + [mdw_have_pcre=t AM_CFLAGS="$AM_CFLAGS $PCRE_CFLAGS"], []) +AM_CONDITIONAL([HAVE_PCRE], [test $mdw_have_pcre = t]) +case $mdw_have_pcre in + t) AC_DEFINE([HAVE_PCRE], [1], [PCRE library is available.]) ;; +esac dnl-------------------------------------------------------------------------- dnl Java. diff --git a/pcre.c b/pcre.c index f15e37a..5c5994c 100644 --- a/pcre.c +++ b/pcre.c @@ -26,15 +26,8 @@ /*----- Header files ------------------------------------------------------*/ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#ifndef HAVE_PCRE - extern int dummy; -#else - #include "anag.h" + #include /*----- Data structures ---------------------------------------------------*/ @@ -58,12 +51,9 @@ static int n_pcre(node *nn, const char *p, size_t sz) int e; e = pcre_exec(n->rx, n->rx_study, p, sz, 0, 0, n->ovec, n->ovecsz); - if (e >= 0) - return (1); - if (e == PCRE_ERROR_NOMATCH) - return (0); + if (e >= 0) return (1); + if (e == PCRE_ERROR_NOMATCH) return (0); die("unexpected PCRE error code %d", e); - return (-1); } /* --- Node creation --- */ @@ -76,17 +66,16 @@ node *pcrenode(const char *const *av) int c; n->n.func = n_pcre; - if ((n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0)) == 0) - die("bad regular expression `%s': %s", av[0], e); + + n->rx = pcre_compile(av[0], PCRE_CASELESS, &e, &eo, 0); + if (!n->rx) die("bad regular expression `%s': %s", av[0], e); n->rx_study = pcre_study(n->rx, 0, &e); - if (e) - die("error studying pattern `%s': %s", av[0], e); - pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_BACKREFMAX, &c); - n->ovecsz = c * 2; - n->ovec = xmalloc(n->ovecsz * sizeof(*n->ovec)); + if (e) die("error studying pattern `%s': %s", av[0], e); + pcre_fullinfo(n->rx, n->rx_study, PCRE_INFO_CAPTURECOUNT, &c); + n->ovecsz = 2*c; + n->ovec = xmalloc(n->ovecsz*sizeof(*n->ovec)); + return (&n->n); } /*----- That's all, folks -------------------------------------------------*/ - -#endif diff --git a/regexp.c b/regexp.c index c930ea5..cd12f15 100644 --- a/regexp.c +++ b/regexp.c @@ -26,15 +26,8 @@ /*----- Header files ------------------------------------------------------*/ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#ifndef HAVE_REGCOMP - extern int dummy; -#else - #include "anag.h" + #include /*----- Data structures ---------------------------------------------------*/ @@ -52,19 +45,17 @@ typedef struct node_regexp { static int n_regexp(node *nn, const char *p, size_t sz) { node_regexp *n = (node_regexp *)nn; + char buf[256]; int e; switch (e = regexec(&n->rx, p, 0, 0, 0)) { - case 0: - return 1; - case REG_NOMATCH: - return 0; - default: { - char buf[256]; + case 0: return 1; + case REG_NOMATCH: return 0; + default: regerror(e, &n->rx, buf, sizeof(buf)); die("error matching regexp `%s' against `%s': %s", n->s, p, buf); - } break; + break; } return (0); } @@ -74,11 +65,12 @@ static int n_regexp(node *nn, const char *p, size_t sz) node *regexp(const char *const *av) { node_regexp *n = xmalloc(sizeof(*n)); + char buf[256]; int e; + n->n.func = n_regexp; if ((e = regcomp(&n->rx, av[0], REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { - char buf[256]; regerror(e, &n->rx, buf, sizeof(buf)); die("bad regular expression `%s': %s", av[0], buf); } @@ -86,5 +78,3 @@ node *regexp(const char *const *av) } /*----- That's all, folks -------------------------------------------------*/ - -#endif