PERFORCE change 144096 for review

Gabor Kovesdan gabor at FreeBSD.org
Wed Jun 25 13:58:21 UTC 2008


http://perforce.freebsd.org/chv.cgi?CH=144096

Change 144096 by gabor at gabor_server on 2008/06/25 13:57:53

	- Change the procline() layout to count the matches and store the
	  matching offsets. This will lead as a working --color support.
	  The change is incomplete now, thus --color and -o won't work
	  yet.
	
	Obtained from:	NetBSD Project

Affected files ...

.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#28 edit
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#42 edit

Differences ...

==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#28 (text+ko) ====

@@ -60,6 +60,8 @@
 #define LINK_EXPLICIT	1
 #define LINK_SKIP	2
 
+#define MAX_LINE_MATCHES	32
+
 struct file {
 	int		 noseek;
 	FILE		*f;

==== //depot/projects/soc2008/gabor_textproc/grep/util.c#42 (text+ko) ====

@@ -202,55 +202,42 @@
 procline(struct str *l, int nottext)
 {
 	regmatch_t	 pmatch;
-	int		 c, i, r;
+	regmatch_t	 matches[MAX_LINE_MATCHES];
+	regoff_t	 st = 0;
+	int		 c = 0, i, r, m = 0, t;
 
 	if (!matchall) {
-		for (c = i = 0; i < patterns; i++) {
-			pmatch.rm_so = 0;
+		t = vflag ? REG_NOMATCH : 0;
+
+		while (st <= l->len) {
+			pmatch.rm_so = st;
 			pmatch.rm_eo = l->len;
-			r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
-			if (r == 0 && xflag) {
-				if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
-					r = REG_NOMATCH;
-			}
-			if ((r == 0) && (color != NULL) && !oflag && !nottext) {
-				char	*tmp, *begin, *matched, *end;
 
-				begin = grep_malloc(strlen(l->dat) - pmatch.rm_so + 1);
-				matched = grep_malloc((pmatch.rm_eo - pmatch.rm_so + 1) * sizeof(char));
-				end = grep_malloc(strlen(l->dat) - pmatch.rm_eo + 1);
-
-				strlcpy(begin, l->dat, pmatch.rm_so + 1);
-				strlcpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
-				strlcpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - pmatch.rm_eo + 1);
-
-				asprintf(&tmp, "%s\33[%sm\33[K%s\33[m\33[K%s", begin, color, matched, end);
-
-				free(begin);
-				free(matched);
-				free(end);
-
-				l->len += strlen(color) + 12;
-				l->dat = tmp;
+			for (i = 0; i < patterns; i++) {
+				pmatch.rm_so = 0;
+				pmatch.rm_eo = l->len;
+				r = regexec(&r_pattern[i], l->dat, 1, &pmatch, eflags);
+				if (r == REG_NOMATCH && t == 0)
+					continue;
+				if (r == 0 && xflag)
+					if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
+						r = REG_NOMATCH;
+				if (r == t) {
+					if (m == 0)
+						c++;
+					if (m < MAX_LINE_MATCHES)
+						matches[m] = pmatch;
+						m++;
+				}
+					st = pmatch.rm_eo;
+					break;
 			}
-			if (r == 0 && oflag && !nottext) {
-				char	*tmp, *matched;
+			/* One pass if we are not recording matches */
+			if (!oflag && !color)
+				break;
 
-				matched = grep_malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char));
-				strlcpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
-
-				if (color != NULL)
-					asprintf(&tmp, "\33[%sm%s\33[m", color, matched);
-				else
-					tmp = matched;
-
-				l->dat = tmp;
-				l->len = strlen(l->dat);
-			}
-			if (r == 0) {
-				c++;
-				break;
-			}
+			if (st == pmatch.rm_so)
+				break; 	/* No matches */
 		}
 	} else
 		c = !vflag;


More information about the p4-projects mailing list