PERFORCE change 144098 for review
Gabor Kovesdan
gabor at FreeBSD.org
Wed Jun 25 14:51:15 UTC 2008
http://perforce.freebsd.org/chv.cgi?CH=144098
Change 144098 by gabor at gabor_server on 2008/06/25 14:50:37
- Reimplement --color and -o
Obtained from: NetBSD Project
Affected files ...
.. //depot/projects/soc2008/gabor_textproc/grep/grep.h#29 edit
.. //depot/projects/soc2008/gabor_textproc/grep/queue.c#5 edit
.. //depot/projects/soc2008/gabor_textproc/grep/util.c#43 edit
Differences ...
==== //depot/projects/soc2008/gabor_textproc/grep/grep.h#29 (text+ko) ====
@@ -101,7 +101,7 @@
void *grep_malloc(size_t size);
void *grep_calloc(size_t nmemb, size_t size);
void *grep_realloc(void *ptr, size_t size);
-void printline(struct str *line, int sep);
+void printline(struct str *line, int sep, regmatch_t *matches, int m);
/* queue.c */
void initqueue(void);
==== //depot/projects/soc2008/gabor_textproc/grep/queue.c#5 (text+ko) ====
@@ -116,7 +116,7 @@
struct queue *item;
while ((item = dequeue()) != NULL) {
- printline(&item->data, '-');
+ printline(&item->data, '-', (regmatch_t *)NULL, 0);
free_item(item);
}
}
==== //depot/projects/soc2008/gabor_textproc/grep/util.c#43 (text+ko) ====
@@ -214,8 +214,6 @@
pmatch.rm_eo = l->len;
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;
@@ -259,9 +257,9 @@
printqueue();
}
linesqueued = 0;
- printline(l, ':');
+ printline(l, ':', matches, m);
} else {
- printline(l, '-');
+ printline(l, '-', matches, m);
tail--;
}
}
@@ -305,9 +303,10 @@
}
void
-printline(struct str *line, int sep)
+printline(struct str *line, int sep, regmatch_t *matches, int m)
{
- int n = 0;
+ int i, n = 0;
+ size_t a = 0;
if (!hflag) {
if (nullflag == 0)
@@ -332,6 +331,28 @@
}
if (n)
putchar(sep);
- fwrite(line->dat, line->len, 1, stdout);
- putchar('\n');
+ if ((oflag || color) && m > 0) {
+ for (i = 0; i < m; i++) {
+ if (!oflag)
+ fwrite(line->dat + a, matches[i].rm_so - a, 1, stdout);
+ if (color)
+ fprintf(stdout, "\33[%sm", color);
+ fwrite(line->dat + matches[i].rm_so,
+ matches[i].rm_eo - matches[i].rm_so, 1, stdout);
+ if (color)
+ fprintf(stdout, "\33[00m");
+ a = matches[i].rm_eo;
+ if (oflag)
+ putchar('\n');
+ }
+ if (!oflag) {
+ if (line->len - a > 0)
+ fwrite(line->dat + a, line->len - a, 1, stdout);
+ putchar('\n');
+ }
+ } else {
+ fwrite(line->dat, line->len, 1, stdout);
+ putchar('\n');
+ }
+
}
More information about the p4-projects
mailing list