From 6b83e5290c6c6e99f150185e964243b703574f45 Mon Sep 17 00:00:00 2001 From: Chris Allegretta Date: Sat, 30 Aug 2008 05:16:20 +0000 Subject: [PATCH] 2008-08-29 Chris Allegretta * configure.ac, color.c, rcfile.c, utils.c: 1st attempt at supporting systems which don't support GNU-style word boundaries. New function fixbounds() to translate from GNU-style to BSD-style, autoconf option GNU_WORDBOUNDS. git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@4315 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- ChangeLog | 5 +++++ configure.ac | 26 ++++++++++++++++++++++++++ src/color.c | 6 +++--- src/proto.h | 1 + src/rcfile.c | 6 +++--- src/utils.c | 36 ++++++++++++++++++++++++++++++++++++ 6 files changed, 74 insertions(+), 6 deletions(-) diff --git a/ChangeLog b/ChangeLog index 17ad4863..12d2fa14 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2008-08-29 Chris Allegretta + * configure.ac, color.c, rcfile.c, utils.c: 1st attempt at supporting systems which don't support + GNU-style word boundaries. New function fixbounds() to translate from GNU-style to + BSD-style, autoconf option GNU_WORDBOUNDS. + 2008-08-28 Chris Allegretta * configure.ac, rcfile.c: Add support for an alternate rcfilename at configure time. Maybe this should become a command line option some day, but I don't see the need currently. Start of diff --git a/configure.ac b/configure.ac index d9f46678..0850a7d3 100644 --- a/configure.ac +++ b/configure.ac @@ -144,6 +144,32 @@ AC_ARG_ENABLE(color, if test x$ac_cv_header_regex_h = xyes; then AC_DEFINE(ENABLE_NANORC, 1, [Define this to use .nanorc files.]) nanorc_support=yes AC_DEFINE(ENABLE_COLOR, 1, [Define this to have syntax highlighting, requires regex.h and ENABLE_NANORC too!]) color_support=yes + + # now check for the end of word boundary support (/< and />) + AC_MSG_CHECKING([for GNU-style word boundary regex support]) + AC_TRY_RUN([ +#ifdef HAVE_SYS_TYPES_H +#include +#endif +#include +int main(void) +{ + regex_t r; + size_t nmatch; + regmatch_t pmatch; + + if (regcomp(&r, "\\\\>", REG_EXTENDED|REG_NOSUB)) + return 1; + if (regexec(&r, "word boundary", nmatch, &pmatch, 0)) + return 1; + return 0; +}], + AC_MSG_RESULT(yes) + AC_DEFINE(GNU_WORDBOUNDS, 1, [Define if the system supports GNU-style word boundaries in regexes.]) gnu_wordbounds=yes, + AC_MSG_RESULT(no), + AC_MSG_WARN([*** Can't check for gnu word boundary support when cross-compiling]) +) + else AC_MSG_ERROR([ *** The header file regex.h was not found. If you wish to use color diff --git a/src/color.c b/src/color.c index a998551b..bbe46bd9 100644 --- a/src/color.c +++ b/src/color.c @@ -153,7 +153,7 @@ void color_update(void) * already. */ if (not_compiled) { e->ext = (regex_t *)nmalloc(sizeof(regex_t)); - regcomp(e->ext, e->ext_regex, REG_EXTENDED); + regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED); } /* Set colorstrings if we matched the extension @@ -188,13 +188,13 @@ void color_update(void) * regexes if we haven't already. */ if (tmpcolor->start == NULL) { tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t)); - regcomp(tmpcolor->start, tmpcolor->start_regex, + regcomp(tmpcolor->start, fixbounds(tmpcolor->start_regex), REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); } if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) { tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t)); - regcomp(tmpcolor->end, tmpcolor->end_regex, + regcomp(tmpcolor->end, fixbounds(tmpcolor->end_regex), REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); } } diff --git a/src/proto.h b/src/proto.h index 2a40dda2..a2c89309 100644 --- a/src/proto.h +++ b/src/proto.h @@ -685,6 +685,7 @@ ssize_t ngetdelim(char **lineptr, size_t *n, int delim, FILE *stream); #endif #ifdef HAVE_REGEX_H bool regexp_bol_or_eol(const regex_t *preg, const char *string); +const char *fixbounds(const char *r); #endif #ifndef DISABLE_SPELLER bool is_whole_word(size_t pos, const char *buf, const char *word); diff --git a/src/rcfile.c b/src/rcfile.c index ee3759f3..85fb9913 100644 --- a/src/rcfile.c +++ b/src/rcfile.c @@ -217,19 +217,19 @@ char *parse_next_regex(char *ptr) bool nregcomp(const char *regex, int eflags) { regex_t preg; - int rc = regcomp(&preg, regex, REG_EXTENDED | eflags); + const char *r = fixbounds(regex); + int rc = regcomp(&preg, r, REG_EXTENDED | eflags); if (rc != 0) { size_t len = regerror(rc, &preg, NULL, 0); char *str = charalloc(len); regerror(rc, &preg, str, len); - rcfile_error(N_("Bad regex \"%s\": %s"), regex, str); + rcfile_error(N_("Bad regex \"%s\": %s"), r, str); free(str); } regfree(&preg); - return (rc == 0); } diff --git a/src/utils.c b/src/utils.c index 81f1329e..339b4d74 100644 --- a/src/utils.c +++ b/src/utils.c @@ -253,6 +253,42 @@ bool regexp_bol_or_eol(const regex_t *preg, const char *string) regexec(preg, string, 0, NULL, REG_NOTBOL | REG_NOTEOL) == REG_NOMATCH); } + +/* Fix the regex if we're on platforms which requires an adjustment + * from GNU-style to BSD-style word boundaries. */ +const char *fixbounds(const char *r) { +#ifndef GNU_WORDBOUNDS + int i, j = 0; + char *r2 = charalloc(strlen(r) * 5); + char *r3; + +#ifdef DEBUG + fprintf(stderr, "fixbounds(): Start string = \"%s\"\n", r); +#endif + + for (i = 0; i < strlen(r); i++) { + if (r[i] != '\0' && r[i] == '\\' && (r[i+1] == '>' || r[i+1] == '<')) { + strcpy(&r2[j], "[[:"); + r2[j+3] = r[i+1]; + strcpy(&r2[j+4], ":]]"); + i++; + j += 6; + } else + r2[j] = r[i]; + j++; + } + r2[j] = '\0'; + r3 = mallocstrcpy(NULL, r2); + free(r2); +#ifdef DEBUG + fprintf(stderr, "fixbounds(): Ending string = \"%s\"\n", r3); +#endif + return (const char *) r3; +#endif + + return r; +} + #endif #ifndef DISABLE_SPELLER -- 2.39.5