2008-08-29 Chris Allegretta <chrisa@asty.org>
        * 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
diff --git a/ChangeLog b/ChangeLog
index 17ad486..12d2fa1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2008-08-29 Chris Allegretta <chrisa@asty.org>
+	* 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 <chrisa@asty.org>
 	* 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 d9f4667..0850a7d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -144,6 +144,32 @@
     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 <sys/types.h>
+#endif
+#include <regex.h>
+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 a998551..bbe46bd 100644
--- a/src/color.c
+++ b/src/color.c
@@ -153,7 +153,7 @@
 		 * 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 @@
 	 * 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 2a40dda..a2c8930 100644
--- a/src/proto.h
+++ b/src/proto.h
@@ -685,6 +685,7 @@
 #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 ee3759f..85fb991 100644
--- a/src/rcfile.c
+++ b/src/rcfile.c
@@ -217,19 +217,19 @@
 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 81f1329..339b4d7 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -253,6 +253,42 @@
 	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