Gentoo Websites Logo
Go to: Gentoo Home Documentation Forums Lists Bugs Planet Store Wiki Get Gentoo!
View | Details | Raw Unified | Return to bug 173005 | Differences between
and this patch

Collapse All | Expand All

(-)qgrep.c (-15 / +63 lines)
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 (!regexec(preg, p, 1, &match, regexec_flags)) {
156
			if (match.rm_so > 0)
157
				printf("%.*s", match.rm_so, p);
158
			printf("%s%.*s%s", RED, match.rm_eo - match.rm_so, p + match.rm_so, NORM);
159
			p += match.rm_eo;
160
			regexec_flags = REG_NOTBOL;
161
		}
162
	} else if (searchfunc != NULL && searchstr != NULL) {
163
		/* Iteration over substring matches, for color output. */
164
		char *q;
165
		int searchlen = strlen(searchstr);
166
		while ((q = ((searchfunc) (p, searchstr))) != NULL) {
167
			if (p < q)
168
				printf("%.*s", (q - p), p);
169
			printf("%s%.*s%s", RED, searchlen, q, NORM);
170
			p = q + searchlen;
171
		}
172
	}
173
	/* No color output (for context lines, or trailing portion
174
	 * of matching lines). */
175
	printf("%s\n", p);
176
	/* Once a line has been displayed, it is not valid anymore */
143
	current->valid = 0;
177
	current->valid = 0;
144
}
178
}
145
#define qgrep_print_context_line(buf, label, lineno) \
179
#define qgrep_print_context_line(buf, label, lineno) \
146
	qgrep_print_line(buf, label, lineno, '-')
180
	qgrep_print_line(buf, label, lineno, '-', NULL, NULL, NULL)
147
#define qgrep_print_matching_line(buf, label, lineno) \
181
#define qgrep_print_matching_line_nocolor(buf, label, lineno) \
148
	qgrep_print_line(buf, label, lineno, ':')
182
	qgrep_print_line(buf, label, lineno, ':', NULL, NULL, NULL)
183
#define qgrep_print_matching_line_regcolor(buf, label, lineno, preg) \
184
	qgrep_print_line(buf, label, lineno, ':', preg, NULL, NULL)
185
#define qgrep_print_matching_line_strcolor(buf, label, lineno, searchfunc, searchstr) \
186
	qgrep_print_line(buf, label, lineno, ':', NULL, searchfunc, searchstr)
149
187
150
/* Display a leading context (valid lines of the buffers list, but the matching one). */
188
/* 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);
189
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];
214
	char ebuild[_Q_PATH_MAX];
177
	char name[_Q_PATH_MAX];
215
	char name[_Q_PATH_MAX];
178
	char *label;
216
	char *label;
179
	int reflags = REG_NOSUB;
217
	int reflags = 0;
180
	char invert_match = 0;
218
	char invert_match = 0;
181
	regex_t preg, skip_preg;
219
	regex_t preg, skip_preg;
182
	char *skip_pattern = NULL;
220
	char *skip_pattern = NULL;
Lines 188-195 Link Here
188
	int need_separator = 0;
226
	int need_separator = 0;
189
	char status = 1;
227
	char status = 1;
190
228
191
	typedef char *(*FUNC) (char *, char *);
229
	QGREP_STR_FUNC strfunc = (QGREP_STR_FUNC) strstr;
192
	FUNC strfunc = (FUNC) strstr;
193
230
194
	DBG("argc=%d argv[0]=%s argv[1]=%s",
231
	DBG("argc=%d argv[0]=%s argv[1]=%s",
195
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
232
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
Lines 201-207 Link Here
201
		switch (i) {
238
		switch (i) {
202
		case 'I': invert_match = 1; break;
239
		case 'I': invert_match = 1; break;
203
		case 'i':
240
		case 'i':
204
			strfunc = (FUNC) strcasestr;
241
			strfunc = (QGREP_STR_FUNC) strcasestr;
205
			reflags |= REG_ICASE;
242
			reflags |= REG_ICASE;
206
			break;
243
			break;
207
		case 'c': do_count = 1; break;
244
		case 'c': do_count = 1; break;
Lines 287-298 Link Here
287
	if (do_regex) {
324
	if (do_regex) {
288
		int ret;
325
		int ret;
289
		char err[256];
326
		char err[256];
327
		if (invert_match || *RED == '\0')
328
			reflags |= REG_NOSUB;
290
		if ((ret = regcomp(&preg, argv[optind], reflags))) {
329
		if ((ret = regcomp(&preg, argv[optind], reflags))) {
291
			if (regerror(ret, &preg, err, sizeof(err)))
330
			if (regerror(ret, &preg, err, sizeof(err)))
292
				err("regcomp failed: %s", err);
331
				err("regcomp failed: %s", err);
293
			else
332
			else
294
				err("regcomp failed");
333
				err("regcomp failed");
295
		}
334
		}
335
		reflags |= REG_NOSUB;
296
		if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) {
336
		if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) {
297
			if (regerror(ret, &skip_preg, err, sizeof(err)))
337
			if (regerror(ret, &skip_preg, err, sizeof(err)))
298
				err("regcomp failed for --skip pattern: %s", err);
338
				err("regcomp failed for --skip pattern: %s", err);
Lines 398-404 Link Here
398
				if (skip_pattern) {
438
				if (skip_pattern) {
399
					/* reject some other lines which match an optional pattern */
439
					/* reject some other lines which match an optional pattern */
400
					if (!do_regex) {
440
					if (!do_regex) {
401
						if (( (FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL)
441
						if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, skip_pattern)) != NULL)
402
							goto print_after_context;
442
							goto print_after_context;
403
					} else {
443
					} else {
404
						if (regexec(&skip_preg, buf_list->buf, 0, NULL, 0) == 0)
444
						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) */
449
				/* four ways to match a line (with/without inversion and regexp) */
410
				if (!invert_match) {
450
				if (!invert_match) {
411
					if (do_regex == 0) {
451
					if (do_regex == 0) {
412
						if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL)
452
						if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) == NULL)
413
							goto print_after_context;
453
							goto print_after_context;
414
					} else {
454
					} else {
415
						if (regexec(&preg, buf_list->buf, 0, NULL, 0) != 0)
455
						if (regexec(&preg, buf_list->buf, 0, NULL, 0) != 0)
Lines 417-423 Link Here
417
					}
457
					}
418
				} else {
458
				} else {
419
					if (do_regex == 0) {
459
					if (do_regex == 0) {
420
						if (( (FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL)
460
						if (( (QGREP_STR_FUNC *) (strfunc) (buf_list->buf, argv[optind])) != NULL)
421
							goto print_after_context;
461
							goto print_after_context;
422
					} else {
462
					} else {
423
						if (regexec(&preg, buf_list->buf, 0, NULL, 0) == 0)
463
						if (regexec(&preg, buf_list->buf, 0, NULL, 0) == 0)
Lines 437-447 Link Here
437
				 * adjacent contextes are not separated */
477
				 * adjacent contextes are not separated */
438
				need_separator = 0 - num_lines_before;
478
				need_separator = 0 - num_lines_before;
439
				if (!do_list) {
479
				if (!do_list) {
440
					/* print the leading context, and matching line */
480
					/* print the leading context */
441
					qgrep_print_before_context(buf_list, num_lines_before, label,
481
					qgrep_print_before_context(buf_list, num_lines_before, label,
442
							((verbose > 1) ? lineno : -1));
482
							((verbose > 1) ? lineno : -1));
443
					qgrep_print_matching_line(buf_list, label,
483
					/* print matching line */
484
					if (invert_match || *RED == '\0')
485
						qgrep_print_matching_line_nocolor(buf_list, label,
444
							((verbose > 1) ? lineno : -1));
486
							((verbose > 1) ? lineno : -1));
487
					else if (do_regex)
488
						qgrep_print_matching_line_regcolor(buf_list, label,
489
							((verbose > 1) ? lineno : -1), &preg);
490
					else
491
						qgrep_print_matching_line_strcolor(buf_list, label,
492
							((verbose > 1) ? lineno : -1), strfunc, argv[optind]);
445
				} else {
493
				} else {
446
					/* in verbose do_list mode, list the file once per match */
494
					/* in verbose do_list mode, list the file once per match */
447
					printf("%s", label);
495
					printf("%s", label);

Return to bug 173005