Subject: regex support for less-177
To: None <current-users@sun-lamp.cs.berkeley.edu>
From: None <Mark_Weaver@brown.edu>
List: current-users
Date: 12/16/1993 17:14:30
These are patches to less (found deep within othersrc) to allow it to
use regex.  There is also a sys_errlist problem fixed.

diff -u  othersrc/mh-6.8/miscellany/less-177/search.c.mhw1 othersrc/mh-6.8/miscellany/less-177/search.c
--- othersrc/mh-6.8/miscellany/less-177/search.c.mhw1	Sun Nov  7 06:04:40 1993
+++ othersrc/mh-6.8/miscellany/less-177/search.c	Thu Dec 16 11:41:16 1993
@@ -7,6 +7,10 @@
 #if REGCOMP
 #include "regexp.h"
 #endif
+#if REGEX
+#include <sys/types.h>
+#include <regex.h>
+#endif
 
 extern int sigs;
 extern int how_search;
@@ -41,19 +45,18 @@
 #if RECOMP
 	char *re_comp();
 	PARG parg;
-#else
-#if REGCMP
+#elif REGCMP
 	char *regcmp();
 	static char *cpattern = NULL;
-#else
-#if REGCOMP
+#elif REGCOMP
 	static struct regexp *regpattern = NULL;
+#elif REGEX
+	static regex_t regexpattern;
+	static int regexhavepat = 0;
 #else
 	static char lpbuf[100];
 	static char *last_pattern = NULL;
 #endif
-#endif
-#endif
 
 	/*
 	 * Extract flags and type of search.
@@ -85,8 +88,7 @@
 		error("%s", &parg);
 		return (-1);
 	}
-#else
-#if REGCMP
+#elif REGCMP
 	if (pattern == NULL || *pattern == '\0')
 	{
 		/*
@@ -113,8 +115,7 @@
 			free(cpattern);
 		cpattern = s;
 	}
-#else
-#if REGCOMP
+#elif REGCOMP
 	if (pattern == NULL || *pattern == '\0')
 	{
 		/*
@@ -142,6 +143,35 @@
 			free(regpattern);
 		regpattern = s;
 	}
+#elif REGEX
+	if (pattern == NULL || *pattern == '\0')
+	{
+		/*
+		 * A null pattern means use the previous pattern.
+		 * The compiled previous pattern is in regpattern, 
+		 * so just use it.
+		 */
+		if (!regexhavepat)
+		{
+			error("No previous regular expression", NULL_PARG);
+			return (-1);
+		}
+	} else
+	{
+		/*
+		 * Otherwise compile the given pattern.
+		 */
+		regex_t s;
+		if (regcomp(&s, pattern, REG_BASIC | REG_NOSUB))
+		{
+			error("Invalid pattern", NULL_PARG);
+			return (-1);
+		}
+		if (regexhavepat)
+			regfree(&regexpattern);
+		regexpattern = s;
+		regexhavepat = 1;
+	}
 #else
 	if (pattern == NULL || *pattern == '\0')
 	{
@@ -160,8 +190,6 @@
 		last_pattern = lpbuf;
 	}
 #endif
-#endif
-#endif
 
 	/*
 	 * Figure out where to start the search.
@@ -299,17 +327,15 @@
 		 */
 #if REGCMP
 		line_match = (regex(cpattern, line) != NULL);
-#else
-#if RECOMP
+#elif RECOMP
 		line_match = (re_exec(line) == 1);
-#else
-#if REGCOMP
+#elif REGCOMP
 		line_match = regexec(regpattern, line);
+#elif REGEX
+		line_match = (regexec(&regexpattern, line, 0, NULL, 0) == 0);
 #else
 		line_match = match(pattern, line);
 #endif
-#endif
-#endif
 		/*
 		 * We are successful if want_match and line_match are
 		 * both true (want a match and got it),
@@ -327,7 +353,7 @@
 	return (0);
 }
 
-#if (!REGCMP) && (!RECOMP) && (!REGCOMP)
+#if (!REGCMP) && (!RECOMP) && (!REGCOMP) && (!REGEX)
 /*
  * We have neither regcmp() nor re_comp().
  * We use this function to do simple pattern matching.
diff -u  othersrc/mh-6.8/miscellany/less-177/os.c.mhw1 othersrc/mh-6.8/miscellany/less-177/os.c
--- othersrc/mh-6.8/miscellany/less-177/os.c.mhw1	Sun Nov  7 06:04:30 1993
+++ othersrc/mh-6.8/miscellany/less-177/os.c	Thu Dec 16 11:41:17 1993
@@ -96,8 +96,10 @@
 
 #if PERROR
 
+#ifndef __NetBSD__
 extern char *sys_errlist[];
 extern int sys_nerr;
+#endif
 extern int errno;
 
 	public char *
diff -u  othersrc/mh-6.8/miscellany/less-177/linstall.mhw1 othersrc/mh-6.8/miscellany/less-177/linstall
--- othersrc/mh-6.8/miscellany/less-177/linstall.mhw1	Sun Nov  7 06:04:14 1993
+++ othersrc/mh-6.8/miscellany/less-177/linstall	Thu Dec 16 11:41:17 1993
@@ -424,19 +424,23 @@
 else
 	def=1; REGCMP=1;RECOMP=0
 fi
+REGEX=0
 if [ $alldefault = 0 ]
 then
 	$ECHO "Most System V systems have the regcmp() function."
 	$ECHO "Most Berkeley-derived systems have the re_comp() function."
+	$ECHO "POSIX systems have regex."
 	$ECHO "Does your system have:"
 	$ECHO "  1. regcmp"
 	$ECHO "  2. re_comp"
-	$ECHO "  3. neither   [$def] \c"
+	$ECHO "  3. POSIX regex"
+	$ECHO "  4. none of the above   [$def] \c"
 	read ans
 	case "X$ans" in
-	X1) REGCMP=1;RECOMP=0 ;;
-	X2) REGCMP=0;RECOMP=1 ;;
-	X3) REGCMP=0;RECOMP=0 ;;
+	X1) REGCMP=1;RECOMP=0;REGEX=0 ;;
+	X2) REGCMP=0;RECOMP=1;REGEX=0 ;;
+	X3) REGCMP=0;RECOMP=0;REGEX=1 ;;
+	X4) REGCMP=0;RECOMP=0;REGEX=0 ;;
 	esac
 	$ECHO ""
 fi
@@ -450,6 +454,7 @@
  */
 #define	REGCMP		$REGCMP
 #define	RECOMP		$RECOMP
+#define	REGEX		$REGEX
 
 EOF
 
--------------------------------------------------------------------
Email: Mark_Weaver@brown.edu           | Brown University
PGP Key: finger mhw@cs.brown.edu       | Dept of Computer Science

------------------------------------------------------------------------------