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

Collapse All | Expand All

(-)qgrep.c.cvs1.17 (-16 / +164 lines)
Lines 10-53 Link Here
10
10
11
#ifdef APPLET_qgrep
11
#ifdef APPLET_qgrep
12
12
13
#define QGREP_FLAGS "IiHceEs" COMMON_FLAGS
13
#define QGREP_FLAGS "IiHNclLexEsS:" COMMON_FLAGS
14
static struct option const qgrep_long_opts[] = {
14
static struct option const qgrep_long_opts[] = {
15
	{"invert-match",  no_argument, NULL, 'I'},
15
	{"invert-match",  no_argument, NULL, 'I'},
16
	{"ignore-case",   no_argument, NULL, 'i'},
16
	{"ignore-case",   no_argument, NULL, 'i'},
17
	{"with-filename", no_argument, NULL, 'H'},
17
	{"with-filename", no_argument, NULL, 'H'},
18
	{"with-name",     no_argument, NULL, 'N'},
18
	{"count",         no_argument, NULL, 'c'},
19
	{"count",         no_argument, NULL, 'c'},
20
	{"list",          no_argument, NULL, 'l'},
21
	{"invert-list",   no_argument, NULL, 'L'},
19
	{"regexp",        no_argument, NULL, 'e'},
22
	{"regexp",        no_argument, NULL, 'e'},
23
	{"extended",      no_argument, NULL, 'x'},
20
	{"eclass",        no_argument, NULL, 'E'},
24
	{"eclass",        no_argument, NULL, 'E'},
21
	{"skip-comments", no_argument, NULL, 's'},
25
	{"skip-comments", no_argument, NULL, 's'},
26
	{"skip",           a_argument, NULL, 'S'},
22
	COMMON_LONG_OPTS
27
	COMMON_LONG_OPTS
23
};
28
};
24
static const char *qgrep_opts_help[] = {
29
static const char *qgrep_opts_help[] = {
25
	"Select non-matching lines",
30
	"Select non-matching lines",
26
	"Ignore case distinctions",
31
	"Ignore case distinctions",
27
	"Print the filename for each match",
32
	"Print the filename for each match",
33
	"Print the package or eclass name for each match",
28
	"Only print a count of matching lines per FILE",
34
	"Only print a count of matching lines per FILE",
35
	"Only print FILE names containing matches",
36
	"Only print FILE names containing no match",
29
	"Use PATTERN as a regular expression",
37
	"Use PATTERN as a regular expression",
38
	"Use PATTERN as an extended regular expression",
30
	"Search in eclasses instead of ebuilds",
39
	"Search in eclasses instead of ebuilds",
31
	"Skip comments lines",
40
	"Skip comments lines",
41
	"Skip lines matching <arg>",
32
	COMMON_OPTS_HELP
42
	COMMON_OPTS_HELP
33
};
43
};
34
static const char qgrep_rcsid[] = "$Id: qgrep.c,v 1.17 2007/03/17 20:53:23 solar Exp $";
44
static const char qgrep_rcsid[] = "$Id: qgrep.c,v 1.17 2007/03/17 20:53:23 solar Exp $";
35
#define qgrep_usage(ret) usage(ret, QGREP_FLAGS, qgrep_long_opts, qgrep_opts_help, lookup_applet_idx("qgrep"))
45
#define qgrep_usage(ret) usage(ret, QGREP_FLAGS, qgrep_long_opts, qgrep_opts_help, lookup_applet_idx("qgrep"))
36
46
47
char qgrep_name_match(const char*, const int, depend_atom**);
48
char qgrep_name_match(const char* name, const int argc, depend_atom** argv)
49
{
50
	depend_atom* atom;
51
	int i;
52
53
	if ((atom = atom_explode(name)) == NULL)
54
		return 0;
55
56
	for (i = 0; i < argc; i++) {
57
		if (argv[i] == NULL)
58
			continue;
59
		if (atom->CATEGORY && argv[i]->CATEGORY && *(argv[i]->CATEGORY)
60
				&& strcmp(atom->CATEGORY, argv[i]->CATEGORY))
61
			continue;
62
		if (atom->PN && argv[i]->PN && *(argv[i]->PN)
63
				&& strcmp(atom->PN, argv[i]->PN))
64
			continue;
65
		if (atom->PVR && argv[i]->PVR && *(argv[i]->PVR)
66
				&& strcmp(atom->PVR, argv[i]->PVR))
67
			continue;
68
		atom_implode(atom);
69
		return 1;
70
	}
71
72
	atom_implode(atom);
73
	return 0;
74
}
75
37
int qgrep_main(int argc, char **argv)
76
int qgrep_main(int argc, char **argv)
38
{
77
{
39
	int i;
78
	int i;
40
	int count = 0;
79
	int count = 0;
41
	char *p;
80
	char *p;
42
	char do_count, do_regex, do_eclass;
81
	char do_count, do_regex, do_eclass, do_list;
43
	char show_filename, skip_comments;
82
	char show_filename, skip_comments, invert_list, show_name;
83
	char per_file_output;
44
	FILE *fp = NULL;
84
	FILE *fp = NULL;
45
	DIR *eclass_dir = NULL;
85
	DIR *eclass_dir = NULL;
46
	struct dirent *dentry;
86
	struct dirent *dentry;
47
	char ebuild[_Q_PATH_MAX];
87
	char ebuild[_Q_PATH_MAX];
88
	char name[_Q_PATH_MAX];
48
	char buf0[BUFSIZ];
89
	char buf0[BUFSIZ];
49
	int reflags = REG_NOSUB;
90
	int reflags = REG_NOSUB;
50
	char invert_match = 0;
91
	char invert_match = 0;
92
	regex_t preg, skip_preg;
93
	char *skip_pattern = NULL;
94
	depend_atom** include_atoms = NULL;
51
95
52
	typedef char *(*FUNC) (char *, char *);
96
	typedef char *(*FUNC) (char *, char *);
53
	FUNC strfunc = (FUNC) strstr;
97
	FUNC strfunc = (FUNC) strstr;
Lines 55-61 Link Here
55
	DBG("argc=%d argv[0]=%s argv[1]=%s",
99
	DBG("argc=%d argv[0]=%s argv[1]=%s",
56
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
100
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
57
101
58
	do_count = do_regex = do_eclass = show_filename = skip_comments = 0;
102
	do_count = do_regex = do_eclass = do_list = 0;
103
	show_filename = skip_comments = invert_list = show_name = 0;
59
104
60
	while ((i = GETOPT_LONG(QGREP, qgrep, "")) != -1) {
105
	while ((i = GETOPT_LONG(QGREP, qgrep, "")) != -1) {
61
		switch (i) {
106
		switch (i) {
Lines 65-80 Link Here
65
			reflags |= REG_ICASE;
110
			reflags |= REG_ICASE;
66
			break;
111
			break;
67
		case 'c': do_count = 1; break;
112
		case 'c': do_count = 1; break;
113
		case 'l': do_list = 1; break;
114
		case 'L': do_list = invert_list = 1; break;
68
		case 'e': do_regex = 1; break;
115
		case 'e': do_regex = 1; break;
116
		case 'x':
117
			do_regex = 1;
118
			reflags |= REG_EXTENDED;
119
			break;
69
		case 'E': do_eclass = 1; break;
120
		case 'E': do_eclass = 1; break;
70
		case 'H': show_filename = 1; break;
121
		case 'H': show_filename = 1; break;
122
		case 'N': show_name = 1; break;
71
		case 's': skip_comments = 1; break;
123
		case 's': skip_comments = 1; break;
124
		case 'S': skip_pattern = optarg; break;
72
		COMMON_GETOPTS_CASES(qgrep)
125
		COMMON_GETOPTS_CASES(qgrep)
73
		}
126
		}
74
	}
127
	}
75
	if (argc == optind)
128
	if (argc == optind)
76
		qgrep_usage(EXIT_FAILURE);
129
		qgrep_usage(EXIT_FAILURE);
77
130
131
	if (do_list && do_count) {
132
		warnf("%s and --count are incompatible options. The former wins.",
133
				(invert_list ? "--invert-list" : "--list"));
134
		do_count = 0;
135
	}
136
137
	if (show_name && show_filename) {
138
		warn("--with-name and --with-filename are incompatible options. The former wins.");
139
		show_filename = 0;
140
	}
141
142
	/* do we report results once per file or per line ? */
143
	per_file_output = do_count || (do_list && (!verbose || invert_list));
144
145
	if (argc > (optind + 1)) {
146
		include_atoms = xcalloc(sizeof(depend_atom*), (argc - optind - 1));
147
		for (i = (optind + 1); i < argc; i++)
148
			if ((include_atoms[i - optind - 1] = atom_explode(argv[i])) == NULL)
149
				warnf("%s: invalid atom, will be ignored", argv[i]);
150
	}
151
152
	if (do_regex) {
153
		int ret;
154
		char err[256];
155
		if ((ret = regcomp(&preg, argv[optind], reflags))) {
156
			if (regerror(ret, &preg, err, sizeof(err)))
157
				errf("regcomp failed: %s", err);
158
			else
159
				err("regcomp failed");
160
		}
161
		if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) {
162
			if (regerror(ret, &skip_preg, err, sizeof(err)))
163
				errf("regcomp failed for --skip pattern: %s", err);
164
			else
165
				err("regcomp failed for --skip pattern");
166
		}
167
	}
168
78
	if (!do_eclass) {
169
	if (!do_eclass) {
79
		initialize_ebuild_flat();	/* sets our pwd to $PORTDIR */
170
		initialize_ebuild_flat();	/* sets our pwd to $PORTDIR */
80
		if ((fp = fopen(CACHE_EBUILD_FILE, "r")) == NULL)
171
		if ((fp = fopen(CACHE_EBUILD_FILE, "r")) == NULL)
Lines 96-106 Link Here
96
				continue;
187
				continue;
97
			if (strcmp(p, ".eclass"))
188
			if (strcmp(p, ".eclass"))
98
				continue;
189
				continue;
99
		} else if ((p = strchr(ebuild, '\n')) != NULL)
190
			if (show_name || (include_atoms != NULL)) {
100
				*p = 0;
191
				/* cut ".eclass" */
192
				*p = '\0';
193
				/* and skip "eclass/" */
194
				snprintf(name, sizeof(name), "%s", ebuild + 7);
195
				/* restore the filepath */
196
				*p = '.';
197
			}
198
		} else {
199
			if ((p = strchr(ebuild, '\n')) != NULL)
200
				*p = '\0';
201
			if (show_name || (include_atoms != NULL)) {
202
				/* cut ".ebuild" */
203
				if (p == NULL)
204
					p = ebuild + strlen(ebuild);
205
				*(p-7) = '\0';
206
				/* cut "/foo/" from "cat/foo/foo-x.y" */
207
				if ((p = strchr(ebuild, '/')) == NULL)
208
					continue;
209
				*(p++) = '\0';
210
				/* find head of the ebuild basename */
211
				if ((p = strchr(p, '/')) == NULL)
212
					continue;
213
				/* find	start of the pkg name */
214
				snprintf(name, sizeof(name), "%s/%s", ebuild, (p+1));
215
				/* restore the filepath */
216
				*p = '/';
217
				*(p + strlen(p)) = '.';
218
				ebuild[strlen(ebuild)] = '/';
219
			}
220
		}
221
222
		if (include_atoms != NULL)
223
			if (!qgrep_name_match(name, (argc - optind - 1), include_atoms))
224
				continue;
225
101
		if ((newfp = fopen(ebuild, "r")) != NULL) {
226
		if ((newfp = fopen(ebuild, "r")) != NULL) {
102
			unsigned int lineno = 0;
227
			unsigned int lineno = 0;
103
			count = 0;
228
			count = 0;
229
104
			while ((fgets(buf0, sizeof(buf0), newfp)) != NULL) {
230
			while ((fgets(buf0, sizeof(buf0), newfp)) != NULL) {
105
				lineno++;
231
				lineno++;
106
				if ((p = strrchr(buf0, '\n')) != NULL)
232
				if ((p = strrchr(buf0, '\n')) != NULL)
Lines 114-154 Link Here
114
					if (*p == '#') continue;
240
					if (*p == '#') continue;
115
				}
241
				}
116
242
243
				if (skip_pattern) {
244
					if (!do_regex) {
245
						if (( (FUNC *) (strfunc) (buf0, skip_pattern)) != NULL) continue;
246
					} else {
247
						if (regexec(&skip_preg, buf0, 0, NULL, 0) == 0) continue;
248
					}
249
				}
250
117
				if (!invert_match) {
251
				if (!invert_match) {
118
					if (do_regex == 0) {
252
					if (do_regex == 0) {
119
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) == NULL) continue;
253
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) == NULL) continue;
120
					} else {
254
					} else {
121
						if ((rematch(argv[optind], buf0, reflags)) != 0) continue;
255
						if (regexec(&preg, buf0, 0, NULL, 0) != 0) continue;
122
					}
256
					}
123
				} else {
257
				} else {
124
					if (do_regex == 0) {
258
					if (do_regex == 0) {
125
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) != NULL) continue;
259
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) != NULL) continue;
126
					} else {
260
					} else {
127
						if ((rematch(argv[optind], buf0, reflags)) == 0) continue;
261
						if (regexec(&preg, buf0, 0, NULL, 0) == 0) continue;
128
					}
262
					}
129
				}
263
				}
130
264
131
				count++;
265
				count++;
132
				if (do_count) continue;
266
				if (per_file_output) continue;
133
				if (verbose || show_filename) {
267
				if (verbose || show_filename || show_name) {
134
					printf("%s:", ebuild);
268
					printf("%s", (show_name ? name : ebuild));
135
					if (verbose > 1) printf("%d:", lineno);
269
					if (verbose > 1) printf(":%d", lineno);
136
					printf(" ");
270
					if (!do_list)
271
						printf(": ");
137
				}
272
				}
138
				printf("%s\n",  buf0);
273
				printf("%s\n",  (do_list ? "" : buf0));
139
			}
274
			}
140
			fclose(newfp);
275
			fclose(newfp);
276
			if (!per_file_output) continue;
141
			if (do_count && count) {
277
			if (do_count && count) {
142
				if (verbose || show_filename) printf("%s:", ebuild);
278
				if (verbose || show_filename || show_name)
279
					printf("%s:", (show_name ? name : ebuild));
143
				printf("%d", count);
280
				printf("%d", count);
144
				puts("");
281
				puts("");
145
			}
282
			} else if (do_list && ((count && !invert_list) || (!count && invert_list)))
283
				printf("%s\n", (show_name ? name : ebuild));
146
		}
284
		}
147
	}
285
	}
148
	if (do_eclass)
286
	if (do_eclass)
149
		closedir(eclass_dir);
287
		closedir(eclass_dir);
150
	else
288
	else
151
		fclose(fp);
289
		fclose(fp);
290
	if (do_regex)
291
		regfree(&preg);
292
	if (do_regex && skip_pattern)
293
		regfree(&skip_preg);
294
	if (include_atoms != NULL) {
295
		for (i = 0; i < (argc - optind - 1); i++)
296
			if (include_atoms[i] != NULL)
297
				atom_implode(include_atoms[i]);
298
		free(include_atoms);
299
	}
152
	return EXIT_SUCCESS;
300
	return EXIT_SUCCESS;
153
}
301
}
154
302

Return to bug 171374