Go to:
Gentoo Home
Documentation
Forums
Lists
Bugs
Planet
Store
Wiki
Get Gentoo!
Gentoo's Bugzilla – Attachment 115148 Details for
Bug 173005
app-portage/portage-utils - Colorized output for qgrep
Home
|
New
–
[Ex]
|
Browse
|
Search
|
Privacy Policy
|
[?]
|
Reports
|
Requests
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
qgrep-colorize_matches_in_output.patch
qgrep-colorize_matches_in_output.patch (text/plain), 6.25 KB, created by
TGL
on 2007-04-01 13:09:03 UTC
(
hide
)
Description:
qgrep-colorize_matches_in_output.patch
Filename:
MIME Type:
Creator:
TGL
Created:
2007-04-01 13:09:03 UTC
Size:
6.25 KB
patch
obsolete
>Index: qgrep.c >=================================================================== >RCS file: /var/cvsroot/gentoo-projects/portage-utils/qgrep.c,v >retrieving revision 1.20 >diff -u -r1.20 qgrep.c >--- qgrep.c 26 Mar 2007 21:11:50 -0000 1.20 >+++ qgrep.c 1 Apr 2007 12:22:38 -0000 >@@ -128,24 +128,62 @@ > } while (current != head); > } > >+/* Type for the str(case)str search functions */ >+typedef char *(*QGREP_STR_FUNC) (const char *, const char *); >+ > /* Display a buffer, with an optionnal prefix. */ >-void qgrep_print_line(qgrep_buf_t *, const char *, const int, const char); >+void qgrep_print_line(qgrep_buf_t *, const char *, const int, const char, >+ const regex_t*, const QGREP_STR_FUNC, const char*); > void qgrep_print_line(qgrep_buf_t *current, const char *label, >- const int line_number, const char zig) >+ const int line_number, const char zig, const regex_t* preg, >+ const QGREP_STR_FUNC searchfunc, const char* searchstr) > { >+ char *p = current->buf; >+ /* Print line prefix, when in verbose mode */ > if (label != NULL) { > printf("%s", label); > if (line_number > 0) > printf(":%d", line_number); > putchar(zig); > } >- printf("%s\n", current->buf); >+ if (preg != NULL) { >+ /* Iteration over regexp matches, for color output. >+ * First regexec is a normal one, and then loop with >+ * REG_NOTBOL to not match "^pattern" anymore. */ >+ regmatch_t match; >+ int regexec_flags = 0; >+ while (!regexec(preg, p, 1, &match, regexec_flags)) { >+ if (match.rm_so > 0) >+ printf("%.*s", match.rm_so, p); >+ printf("%s%.*s%s", RED, match.rm_eo - match.rm_so, p + match.rm_so, NORM); >+ p += match.rm_eo; >+ regexec_flags = REG_NOTBOL; >+ } >+ } else if (searchfunc != NULL && searchstr != NULL) { >+ /* Iteration over substring matches, for color output. */ >+ char *q; >+ int searchlen = strlen(searchstr); >+ while ((q = ((searchfunc) (p, searchstr))) != NULL) { >+ if (p < q) >+ printf("%.*s", (q - p), p); >+ printf("%s%.*s%s", RED, searchlen, q, NORM); >+ p = q + searchlen; >+ } >+ } >+ /* No color output (for context lines, or trailing portion >+ * of matching lines). */ >+ printf("%s\n", p); >+ /* Once a line has been displayed, it is not valid anymore */ > current->valid = 0; > } > #define qgrep_print_context_line(buf, label, lineno) \ >- qgrep_print_line(buf, label, lineno, '-') >-#define qgrep_print_matching_line(buf, label, lineno) \ >- qgrep_print_line(buf, label, lineno, ':') >+ qgrep_print_line(buf, label, lineno, '-', NULL, NULL, NULL) >+#define qgrep_print_matching_line_nocolor(buf, label, lineno) \ >+ qgrep_print_line(buf, label, lineno, ':', NULL, NULL, NULL) >+#define qgrep_print_matching_line_regcolor(buf, label, lineno, preg) \ >+ qgrep_print_line(buf, label, lineno, ':', preg, NULL, NULL) >+#define qgrep_print_matching_line_strcolor(buf, label, lineno, searchfunc, searchstr) \ >+ qgrep_print_line(buf, label, lineno, ':', NULL, searchfunc, searchstr) > > /* Display a leading context (valid lines of the buffers list, but the matching one). */ > void qgrep_print_before_context(qgrep_buf_t *, const char, const char *, const int); >@@ -176,7 +214,7 @@ > char ebuild[_Q_PATH_MAX]; > char name[_Q_PATH_MAX]; > char *label; >- int reflags = REG_NOSUB; >+ int reflags = 0; > char invert_match = 0; > regex_t preg, skip_preg; > char *skip_pattern = NULL; >@@ -188,8 +226,7 @@ > int need_separator = 0; > char status = 1; > >- typedef char *(*FUNC) (char *, char *); >- FUNC strfunc = (FUNC) strstr; >+ QGREP_STR_FUNC strfunc = (QGREP_STR_FUNC) strstr; > > DBG("argc=%d argv[0]=%s argv[1]=%s", > argc, argv[0], argc > 1 ? argv[1] : "NULL?"); >@@ -201,7 +238,7 @@ > switch (i) { > case 'I': invert_match = 1; break; > case 'i': >- strfunc = (FUNC) strcasestr; >+ strfunc = (QGREP_STR_FUNC) strcasestr; > reflags |= REG_ICASE; > break; > case 'c': do_count = 1; break; >@@ -287,12 +324,15 @@ > if (do_regex) { > int ret; > char err[256]; >+ if (invert_match || *RED == '\0') >+ reflags |= REG_NOSUB; > if ((ret = regcomp(&preg, argv[optind], reflags))) { > if (regerror(ret, &preg, err, sizeof(err))) > err("regcomp failed: %s", err); > else > err("regcomp failed"); > } >+ reflags |= REG_NOSUB; > if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) { > if (regerror(ret, &skip_preg, err, sizeof(err))) > err("regcomp failed for --skip pattern: %s", err); >@@ -398,7 +438,7 @@ > if (skip_pattern) { > /* reject some other lines which match an optional pattern */ > if (!do_regex) { >- if (( (FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL) >+ if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL) > goto print_after_context; > } else { > if (regexec(&skip_preg, buf_list->buf, 0, NULL, 0) == 0) >@@ -409,7 +449,7 @@ > /* four ways to match a line (with/without inversion and regexp) */ > if (!invert_match) { > if (do_regex == 0) { >- if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL) >+ if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL) > goto print_after_context; > } else { > if (regexec(&preg, buf_list->buf, 0, NULL, 0) != 0) >@@ -417,7 +457,7 @@ > } > } else { > if (do_regex == 0) { >- if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL) >+ if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL) > goto print_after_context; > } else { > if (regexec(&preg, buf_list->buf, 0, NULL, 0) == 0) >@@ -437,11 +477,19 @@ > * adjacent contextes are not separated */ > need_separator = 0 - num_lines_before; > if (!do_list) { >- /* print the leading context, and matching line */ >+ /* print the leading context */ > qgrep_print_before_context(buf_list, num_lines_before, label, > ((verbose > 1) ? lineno : -1)); >- qgrep_print_matching_line(buf_list, label, >+ /* print matching line */ >+ if (invert_match || *RED == '\0') >+ qgrep_print_matching_line_nocolor(buf_list, label, > ((verbose > 1) ? lineno : -1)); >+ else if (do_regex) >+ qgrep_print_matching_line_regcolor(buf_list, label, >+ ((verbose > 1) ? lineno : -1), &preg); >+ else >+ qgrep_print_matching_line_strcolor(buf_list, label, >+ ((verbose > 1) ? lineno : -1), strfunc, argv[optind]); > } else { > /* in verbose do_list mode, list the file once per match */ > printf("%s", label);
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 173005
:
115148
|
115163