|
Lines 128-151
Link Here
|
| 128 |
} while (current != head); |
128 |
} while (current != head); |
| 129 |
} |
129 |
} |
| 130 |
|
130 |
|
|
|
131 |
/* Type for the str(case)str search functions */ |
| 132 |
typedef char *(*QGREP_STR_FUNC) (const char *, const char *); |
| 133 |
|
| 131 |
/* Display a buffer, with an optionnal prefix. */ |
134 |
/* Display a buffer, with an optionnal prefix. */ |
| 132 |
void qgrep_print_line(qgrep_buf_t *, const char *, const int, const char); |
135 |
void qgrep_print_line(qgrep_buf_t *, const char *, const int, const char, |
|
|
136 |
const regex_t*, const QGREP_STR_FUNC, const char*); |
| 133 |
void qgrep_print_line(qgrep_buf_t *current, const char *label, |
137 |
void qgrep_print_line(qgrep_buf_t *current, const char *label, |
| 134 |
const int line_number, const char zig) |
138 |
const int line_number, const char zig, const regex_t* preg, |
|
|
139 |
const QGREP_STR_FUNC searchfunc, const char* searchstr) |
| 135 |
{ |
140 |
{ |
|
|
141 |
char *p = current->buf; |
| 142 |
/* Print line prefix, when in verbose mode */ |
| 136 |
if (label != NULL) { |
143 |
if (label != NULL) { |
| 137 |
printf("%s", label); |
144 |
printf("%s", label); |
| 138 |
if (line_number > 0) |
145 |
if (line_number > 0) |
| 139 |
printf(":%d", line_number); |
146 |
printf(":%d", line_number); |
| 140 |
putchar(zig); |
147 |
putchar(zig); |
| 141 |
} |
148 |
} |
| 142 |
printf("%s\n", current->buf); |
149 |
if (preg != NULL) { |
|
|
150 |
/* Iteration over regexp matches, for color output. |
| 151 |
* First regexec is a normal one, and then loop with |
| 152 |
* REG_NOTBOL to not match "^pattern" anymore. */ |
| 153 |
regmatch_t match; |
| 154 |
int regexec_flags = 0; |
| 155 |
while ((*p != '\0') && !regexec(preg, p, 1, &match, regexec_flags)) { |
| 156 |
if (match.rm_so > 0) |
| 157 |
printf("%.*s", match.rm_so, p); |
| 158 |
if (match.rm_eo > match.rm_so) { |
| 159 |
printf("%s%.*s%s", RED, match.rm_eo - match.rm_so, p + match.rm_so, NORM); |
| 160 |
p += match.rm_eo; |
| 161 |
} else { |
| 162 |
p += match.rm_eo; |
| 163 |
putchar(*p++); |
| 164 |
} |
| 165 |
regexec_flags = REG_NOTBOL; |
| 166 |
} |
| 167 |
} else if (searchfunc != NULL && searchstr != NULL) { |
| 168 |
/* Iteration over substring matches, for color output. */ |
| 169 |
char *q; |
| 170 |
int searchlen = strlen(searchstr); |
| 171 |
while (searchlen && ((q = ((searchfunc) (p, searchstr))) != NULL)) { |
| 172 |
if (p < q) |
| 173 |
printf("%.*s", (q - p), p); |
| 174 |
printf("%s%.*s%s", RED, searchlen, q, NORM); |
| 175 |
p = q + searchlen; |
| 176 |
} |
| 177 |
} |
| 178 |
/* No color output (for context lines, or trailing portion |
| 179 |
* of matching lines). */ |
| 180 |
printf("%s\n", p); |
| 181 |
/* Once a line has been displayed, it is not valid anymore */ |
| 143 |
current->valid = 0; |
182 |
current->valid = 0; |
| 144 |
} |
183 |
} |
| 145 |
#define qgrep_print_context_line(buf, label, lineno) \ |
184 |
#define qgrep_print_context_line(buf, label, lineno) \ |
| 146 |
qgrep_print_line(buf, label, lineno, '-') |
185 |
qgrep_print_line(buf, label, lineno, '-', NULL, NULL, NULL) |
| 147 |
#define qgrep_print_matching_line(buf, label, lineno) \ |
186 |
#define qgrep_print_matching_line_nocolor(buf, label, lineno) \ |
| 148 |
qgrep_print_line(buf, label, lineno, ':') |
187 |
qgrep_print_line(buf, label, lineno, ':', NULL, NULL, NULL) |
|
|
188 |
#define qgrep_print_matching_line_regcolor(buf, label, lineno, preg) \ |
| 189 |
qgrep_print_line(buf, label, lineno, ':', preg, NULL, NULL) |
| 190 |
#define qgrep_print_matching_line_strcolor(buf, label, lineno, searchfunc, searchstr) \ |
| 191 |
qgrep_print_line(buf, label, lineno, ':', NULL, searchfunc, searchstr) |
| 149 |
|
192 |
|
| 150 |
/* Display a leading context (valid lines of the buffers list, but the matching one). */ |
193 |
/* Display a leading context (valid lines of the buffers list, but the matching one). */ |
| 151 |
void qgrep_print_before_context(qgrep_buf_t *, const char, const char *, const int); |
194 |
void qgrep_print_before_context(qgrep_buf_t *, const char, const char *, const int); |
|
Lines 176-182
Link Here
|
| 176 |
char ebuild[_Q_PATH_MAX]; |
219 |
char ebuild[_Q_PATH_MAX]; |
| 177 |
char name[_Q_PATH_MAX]; |
220 |
char name[_Q_PATH_MAX]; |
| 178 |
char *label; |
221 |
char *label; |
| 179 |
int reflags = REG_NOSUB; |
222 |
int reflags = 0; |
| 180 |
char invert_match = 0; |
223 |
char invert_match = 0; |
| 181 |
regex_t preg, skip_preg; |
224 |
regex_t preg, skip_preg; |
| 182 |
char *skip_pattern = NULL; |
225 |
char *skip_pattern = NULL; |
|
Lines 188-195
Link Here
|
| 188 |
int need_separator = 0; |
231 |
int need_separator = 0; |
| 189 |
char status = 1; |
232 |
char status = 1; |
| 190 |
|
233 |
|
| 191 |
typedef char *(*FUNC) (char *, char *); |
234 |
QGREP_STR_FUNC strfunc = (QGREP_STR_FUNC) strstr; |
| 192 |
FUNC strfunc = (FUNC) strstr; |
|
|
| 193 |
|
235 |
|
| 194 |
DBG("argc=%d argv[0]=%s argv[1]=%s", |
236 |
DBG("argc=%d argv[0]=%s argv[1]=%s", |
| 195 |
argc, argv[0], argc > 1 ? argv[1] : "NULL?"); |
237 |
argc, argv[0], argc > 1 ? argv[1] : "NULL?"); |
|
Lines 201-207
Link Here
|
| 201 |
switch (i) { |
243 |
switch (i) { |
| 202 |
case 'I': invert_match = 1; break; |
244 |
case 'I': invert_match = 1; break; |
| 203 |
case 'i': |
245 |
case 'i': |
| 204 |
strfunc = (FUNC) strcasestr; |
246 |
strfunc = (QGREP_STR_FUNC) strcasestr; |
| 205 |
reflags |= REG_ICASE; |
247 |
reflags |= REG_ICASE; |
| 206 |
break; |
248 |
break; |
| 207 |
case 'c': do_count = 1; break; |
249 |
case 'c': do_count = 1; break; |
|
Lines 287-298
Link Here
|
| 287 |
if (do_regex) { |
329 |
if (do_regex) { |
| 288 |
int ret; |
330 |
int ret; |
| 289 |
char err[256]; |
331 |
char err[256]; |
|
|
332 |
if (invert_match || *RED == '\0') |
| 333 |
reflags |= REG_NOSUB; |
| 290 |
if ((ret = regcomp(&preg, argv[optind], reflags))) { |
334 |
if ((ret = regcomp(&preg, argv[optind], reflags))) { |
| 291 |
if (regerror(ret, &preg, err, sizeof(err))) |
335 |
if (regerror(ret, &preg, err, sizeof(err))) |
| 292 |
err("regcomp failed: %s", err); |
336 |
err("regcomp failed: %s", err); |
| 293 |
else |
337 |
else |
| 294 |
err("regcomp failed"); |
338 |
err("regcomp failed"); |
| 295 |
} |
339 |
} |
|
|
340 |
reflags |= REG_NOSUB; |
| 296 |
if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) { |
341 |
if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) { |
| 297 |
if (regerror(ret, &skip_preg, err, sizeof(err))) |
342 |
if (regerror(ret, &skip_preg, err, sizeof(err))) |
| 298 |
err("regcomp failed for --skip pattern: %s", err); |
343 |
err("regcomp failed for --skip pattern: %s", err); |
|
Lines 398-404
Link Here
|
| 398 |
if (skip_pattern) { |
443 |
if (skip_pattern) { |
| 399 |
/* reject some other lines which match an optional pattern */ |
444 |
/* reject some other lines which match an optional pattern */ |
| 400 |
if (!do_regex) { |
445 |
if (!do_regex) { |
| 401 |
if (( (FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL) |
446 |
if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL) |
| 402 |
goto print_after_context; |
447 |
goto print_after_context; |
| 403 |
} else { |
448 |
} else { |
| 404 |
if (regexec(&skip_preg, buf_list->buf, 0, NULL, 0) == 0) |
449 |
if (regexec(&skip_preg, buf_list->buf, 0, NULL, 0) == 0) |
|
Lines 409-415
Link Here
|
| 409 |
/* four ways to match a line (with/without inversion and regexp) */ |
454 |
/* four ways to match a line (with/without inversion and regexp) */ |
| 410 |
if (!invert_match) { |
455 |
if (!invert_match) { |
| 411 |
if (do_regex == 0) { |
456 |
if (do_regex == 0) { |
| 412 |
if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL) |
457 |
if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL) |
| 413 |
goto print_after_context; |
458 |
goto print_after_context; |
| 414 |
} else { |
459 |
} else { |
| 415 |
if (regexec(&preg, buf_list->buf, 0, NULL, 0) != 0) |
460 |
if (regexec(&preg, buf_list->buf, 0, NULL, 0) != 0) |
|
Lines 417-423
Link Here
|
| 417 |
} |
462 |
} |
| 418 |
} else { |
463 |
} else { |
| 419 |
if (do_regex == 0) { |
464 |
if (do_regex == 0) { |
| 420 |
if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL) |
465 |
if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL) |
| 421 |
goto print_after_context; |
466 |
goto print_after_context; |
| 422 |
} else { |
467 |
} else { |
| 423 |
if (regexec(&preg, buf_list->buf, 0, NULL, 0) == 0) |
468 |
if (regexec(&preg, buf_list->buf, 0, NULL, 0) == 0) |
|
Lines 437-447
Link Here
|
| 437 |
* adjacent contextes are not separated */ |
482 |
* adjacent contextes are not separated */ |
| 438 |
need_separator = 0 - num_lines_before; |
483 |
need_separator = 0 - num_lines_before; |
| 439 |
if (!do_list) { |
484 |
if (!do_list) { |
| 440 |
/* print the leading context, and matching line */ |
485 |
/* print the leading context */ |
| 441 |
qgrep_print_before_context(buf_list, num_lines_before, label, |
486 |
qgrep_print_before_context(buf_list, num_lines_before, label, |
| 442 |
((verbose > 1) ? lineno : -1)); |
487 |
((verbose > 1) ? lineno : -1)); |
| 443 |
qgrep_print_matching_line(buf_list, label, |
488 |
/* print matching line */ |
|
|
489 |
if (invert_match || *RED == '\0') |
| 490 |
qgrep_print_matching_line_nocolor(buf_list, label, |
| 444 |
((verbose > 1) ? lineno : -1)); |
491 |
((verbose > 1) ? lineno : -1)); |
|
|
492 |
else if (do_regex) |
| 493 |
qgrep_print_matching_line_regcolor(buf_list, label, |
| 494 |
((verbose > 1) ? lineno : -1), &preg); |
| 495 |
else |
| 496 |
qgrep_print_matching_line_strcolor(buf_list, label, |
| 497 |
((verbose > 1) ? lineno : -1), strfunc, argv[optind]); |
| 445 |
} else { |
498 |
} else { |
| 446 |
/* in verbose do_list mode, list the file once per match */ |
499 |
/* in verbose do_list mode, list the file once per match */ |
| 447 |
printf("%s", label); |
500 |
printf("%s", label); |