PERFORCE change 143021 for review

Gabor Kovesdan gabor at FreeBSD.org
Fri Jun 6 11:42:29 UTC 2008


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

Change 143021 by gabor at gabor_server on 2008/06/06 11:41:45

	- Add some work-in-progress code about --color / --colour

Affected files ...

.. //depot/projects/soc2008/gabor_textproc/grep/grep.1#6 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.c#13 edit
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#11 edit
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#13 edit

Differences ...

==== //depot/projects/soc2008/gabor_textproc/grep/grep.1#6 (text+ko) ====

@@ -29,7 +29,7 @@
 .\"
 .\"	@(#)grep.1	8.3 (Berkeley) 4/18/94
 .\"
-.Dd 5 Jun, 2008
+.Dd 6 Jun, 2008
 .Dt GREP 1
 .Os
 .Sh NAME
@@ -46,6 +46,8 @@
 .Op Fl e Ar pattern
 .Op Fl f Ar file
 .Op Fl Fl binary-files Ns = Ns Ar value
+.Op Fl Fl color Ns Op = Ns Ar when
+.Op Fl Fl colour Ns Op = Ns Ar when
 .Op Fl Fl context Ns Op = Ns Ar num
 .Op Fl Fl label
 .Op Fl Fl line-buffered
@@ -146,11 +148,11 @@
 no whitespace may be given between the option and its argument.
 .It Fl c , Fl Fl count
 Only a count of selected lines is written to standard output.
-.\"XXX: UNIMPLEMENTED .It Fl Fl colour Ns = Ns Op Ar when , Fl Fl color Ns = Ns Op Ar when
-.\"Mark up the matching text with the expression stored in
-.\".Ev GREP_COLOR
-.\"environment variable.
-.\"The possible values of when can be `never', `always' or `auto'.
+.It Fl Fl colour Ns = Ns Op Ar when , Fl Fl color Ns = Ns Op Ar when
+Mark up the matching text with the expression stored in
+.Ev GREP_COLOR
+environment variable.
+The possible values of when can be `never', `always' or `auto'.
 .\".It Fl D Ar action , Fl Fl devices Ns = Ns Ar action
 .\"XXX: Undocumented.
 .\".It Fl d Ar action , Fl Fl directories Ns = Ns Ar action

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

@@ -93,11 +93,13 @@
 int	 lbflag;	/* --line-buffered */
 int	 nullflag;	/* --null */
 char	*label;		/* --label */
+char	*color;		/* --color */
 
 int binbehave = BIN_FILE_BIN;
 
 enum {
 	BIN_OPT = CHAR_MAX + 1,
+	COLOR_OPT,
 	HELP_OPT,
 	MMAP_OPT,
 	LINEBUF_OPT,
@@ -122,7 +124,7 @@
 {
 	fprintf(stderr,
 	    "usage: %s [-abcDEFGHhIiLlnOoPqRSsUVvwxZ] [-A num] [-B num] [-C[num]]\n"
-	    "\t[-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n"
+	    "\t[--color=when] [-e pattern] [-f file] [--binary-files=value] [--context[=num]]\n"
 	    "\t[--label] [--line-buffered] [--null] [pattern] [file ...]\n"
 	    , __progname);
 	exit(2);
@@ -138,9 +140,8 @@
 	{"line-buffered",	no_argument,		NULL, LINEBUF_OPT},
 	{"label",		required_argument,	NULL, LABEL_OPT},
 	{"null",		no_argument,		NULL, NULL_OPT},
-/* XXX: UNIMPLEMENTED
 	{"color",		optional_argument,	NULL, COLOR_OPT},
-	{"colour",		optional_argument,	NULL, COLOR_OPT}, */
+	{"colour",		optional_argument,	NULL, COLOR_OPT},
 	{"after-context",	required_argument,	NULL, 'A'},
 	{"text",		no_argument,		NULL, 'a'},
 	{"before-context",	required_argument,	NULL, 'B'},
@@ -436,6 +437,18 @@
 			else
 				errx(2, "Unknown binary-files option");
 			break;
+		case COLOR_OPT:
+			if (optarg == NULL)
+				optarg = "auto";
+			if (strcmp("always", optarg) == 0) {
+				color = getenv("GREP_COLOR");
+				if (color == NULL)
+					color = "01;31";
+			} else if ((strcmp("auto", optarg) == 0))
+				color = getenv("GREP_COLOR");
+			else if (strcmp("never", optarg) == 0)
+				color = NULL;
+			break;
 		case LABEL_OPT:
 			label = optarg;
 			break;

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

@@ -65,7 +65,7 @@
 		 bflag, cflag, hflag, iflag, lflag, nflag, Oflag, oflag, qflag, sflag,
 		 vflag, wflag, xflag,
 		 nullflag;
-extern char	*label;
+extern char	*color, *label;
 extern int	 binbehave;
 
 extern int	 first, matchall, patterns, tail;

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

@@ -215,7 +215,31 @@
 				if (pmatch.rm_so != 0 || pmatch.rm_eo != l->len)
 					r = REG_NOMATCH;
 			}
-			if (r == 0 && oflag) {
+			if ((r == 0) && (color != NULL)) {
+/* XXX: this color stuff does not work yet :( */
+				if (oflag) {
+					char *matched, *tmp;
+					matched = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char));
+					strncpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
+					tmp = malloc((strlen(matched) + 50) * sizeof(char));
+					sprintf(tmp, "\33[%sm%s\33[00m", color, matched);
+				} else {
+					char	*tmp, *begin, *matched, *end;
+					begin = malloc(strlen(l->dat) - pmatch.rm_so + 1);
+					matched = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char));
+					end = malloc(strlen(l->dat) - pmatch.rm_eo);
+					strncpy(begin, l->dat, pmatch.rm_so);
+					strncpy(matched, &(l->dat[pmatch.rm_so]), pmatch.rm_eo - pmatch.rm_so + 1);
+					strncpy(end, &(l->dat[pmatch.rm_eo]), strlen(l->dat) - strlen(begin) - strlen(matched));
+					tmp = malloc((strlen(l->dat) + 50) * sizeof(char));
+					sprintf(tmp, "%s\33[%sm%s\33[00m%s", begin, color, matched, end);
+					free(begin);
+					free(matched);
+					free(end);
+					l->dat = tmp;
+				}
+			}
+			if (r == 0 && oflag && (color == NULL)) {
 				char *tmp;
 				if ((tmp = malloc((pmatch.rm_eo - pmatch.rm_so + 2) * sizeof(char))) == NULL)
 					errx(2, NULL);


More information about the p4-projects mailing list