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 (-13 / +147 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;
44
	FILE *fp = NULL;
83
	FILE *fp = NULL;
45
	DIR *eclass_dir = NULL;
84
	DIR *eclass_dir = NULL;
46
	struct dirent *dentry;
85
	struct dirent *dentry;
47
	char ebuild[_Q_PATH_MAX];
86
	char ebuild[_Q_PATH_MAX];
87
	char name[_Q_PATH_MAX];
48
	char buf0[BUFSIZ];
88
	char buf0[BUFSIZ];
49
	int reflags = REG_NOSUB;
89
	int reflags = REG_NOSUB;
50
	char invert_match = 0;
90
	char invert_match = 0;
91
	regex_t preg, skip_preg;
92
	char *skip_pattern = NULL;
93
	depend_atom** include_atoms = NULL;
51
94
52
	typedef char *(*FUNC) (char *, char *);
95
	typedef char *(*FUNC) (char *, char *);
53
	FUNC strfunc = (FUNC) strstr;
96
	FUNC strfunc = (FUNC) strstr;
Lines 55-61 Link Here
55
	DBG("argc=%d argv[0]=%s argv[1]=%s",
98
	DBG("argc=%d argv[0]=%s argv[1]=%s",
56
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
99
	    argc, argv[0], argc > 1 ? argv[1] : "NULL?");
57
100
58
	do_count = do_regex = do_eclass = show_filename = skip_comments = 0;
101
	do_count = do_regex = do_eclass = do_list = 0;
102
	show_filename = skip_comments = invert_list = show_name = 0;
59
103
60
	while ((i = GETOPT_LONG(QGREP, qgrep, "")) != -1) {
104
	while ((i = GETOPT_LONG(QGREP, qgrep, "")) != -1) {
61
		switch (i) {
105
		switch (i) {
Lines 65-80 Link Here
65
			reflags |= REG_ICASE;
109
			reflags |= REG_ICASE;
66
			break;
110
			break;
67
		case 'c': do_count = 1; break;
111
		case 'c': do_count = 1; break;
112
		case 'l': do_list = 1; break;
113
		case 'L': do_list = invert_list = 1; break;
68
		case 'e': do_regex = 1; break;
114
		case 'e': do_regex = 1; break;
115
		case 'x':
116
			do_regex = 1;
117
			reflags |= REG_EXTENDED;
118
			break;
69
		case 'E': do_eclass = 1; break;
119
		case 'E': do_eclass = 1; break;
70
		case 'H': show_filename = 1; break;
120
		case 'H': show_filename = 1; break;
121
		case 'N': show_name = 1; break;
71
		case 's': skip_comments = 1; break;
122
		case 's': skip_comments = 1; break;
123
		case 'S': skip_pattern = optarg; break;
72
		COMMON_GETOPTS_CASES(qgrep)
124
		COMMON_GETOPTS_CASES(qgrep)
73
		}
125
		}
74
	}
126
	}
75
	if (argc == optind)
127
	if (argc == optind)
76
		qgrep_usage(EXIT_FAILURE);
128
		qgrep_usage(EXIT_FAILURE);
77
129
130
	if (do_list)
131
		do_count = 0;
132
133
	if (argc > (optind + 1)) {
134
		include_atoms = xcalloc(sizeof(depend_atom*), (argc - optind - 1));
135
		for (i = (optind + 1); i < argc; i++)
136
			if ((include_atoms[i - optind - 1] = atom_explode(argv[i])) == NULL)
137
				warnf("%s: invalid atom, will be ignored", argv[i]);
138
	}
139
140
	if (do_regex) {
141
		int ret;
142
		char err[256];
143
		if ((ret = regcomp(&preg, argv[optind], reflags))) {
144
			if (regerror(ret, &preg, err, sizeof(err)))
145
				errf("regcomp failed: %s", err);
146
			else
147
				err("regcomp failed");
148
		}
149
		if (skip_pattern && (ret = regcomp(&skip_preg, skip_pattern, reflags))) {
150
			if (regerror(ret, &skip_preg, err, sizeof(err)))
151
				errf("regcomp failed for --skip pattern: %s", err);
152
			else
153
				err("regcomp failed for --skip pattern");
154
		}
155
	}
156
78
	if (!do_eclass) {
157
	if (!do_eclass) {
79
		initialize_ebuild_flat();	/* sets our pwd to $PORTDIR */
158
		initialize_ebuild_flat();	/* sets our pwd to $PORTDIR */
80
		if ((fp = fopen(CACHE_EBUILD_FILE, "r")) == NULL)
159
		if ((fp = fopen(CACHE_EBUILD_FILE, "r")) == NULL)
Lines 96-106 Link Here
96
				continue;
175
				continue;
97
			if (strcmp(p, ".eclass"))
176
			if (strcmp(p, ".eclass"))
98
				continue;
177
				continue;
99
		} else if ((p = strchr(ebuild, '\n')) != NULL)
178
			if (show_name || (include_atoms != NULL)) {
100
				*p = 0;
179
				/* cut ".eclass" */
180
				*p = '\0';
181
				/* and skip "eclass/" */
182
				snprintf(name, sizeof(name), "%s", ebuild + 7);
183
				/* restore the filepath */
184
				*p = '.';
185
			}
186
		} else {
187
			if ((p = strchr(ebuild, '\n')) != NULL)
188
				*p = '\0';
189
			if (show_name || (include_atoms != NULL)) {
190
				/* cut ".ebuild" */
191
				if (p == NULL)
192
					p = ebuild + strlen(ebuild);
193
				*(p-7) = '\0';
194
				/* cut "/foo/" from "cat/foo/foo-x.y" */
195
				if ((p = strchr(ebuild, '/')) == NULL)
196
					continue;
197
				*(p++) = '\0';
198
				/* find head of the ebuild basename */
199
				if ((p = strchr(p, '/')) == NULL)
200
					continue;
201
				/* find	start of the pkg name */
202
				snprintf(name, sizeof(name), "%s/%s", ebuild, (p+1));
203
				/* restore the filepath */
204
				*p = '/';
205
				*(p + strlen(p)) = '.';
206
				ebuild[strlen(ebuild)] = '/';
207
			}
208
		}
209
210
		if (include_atoms != NULL)
211
			if (!qgrep_name_match(name, (argc - optind - 1), include_atoms))
212
				continue;
213
101
		if ((newfp = fopen(ebuild, "r")) != NULL) {
214
		if ((newfp = fopen(ebuild, "r")) != NULL) {
102
			unsigned int lineno = 0;
215
			unsigned int lineno = 0;
103
			count = 0;
216
			count = 0;
217
104
			while ((fgets(buf0, sizeof(buf0), newfp)) != NULL) {
218
			while ((fgets(buf0, sizeof(buf0), newfp)) != NULL) {
105
				lineno++;
219
				lineno++;
106
				if ((p = strrchr(buf0, '\n')) != NULL)
220
				if ((p = strrchr(buf0, '\n')) != NULL)
Lines 114-137 Link Here
114
					if (*p == '#') continue;
228
					if (*p == '#') continue;
115
				}
229
				}
116
230
231
				if (skip_pattern) {
232
					if (!do_regex) {
233
						if (( (FUNC *) (strfunc) (buf0, skip_pattern)) != NULL) continue;
234
					} else {
235
						if (regexec(&skip_preg, buf0, 0, NULL, 0) == 0) continue;
236
					}
237
				}
238
117
				if (!invert_match) {
239
				if (!invert_match) {
118
					if (do_regex == 0) {
240
					if (do_regex == 0) {
119
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) == NULL) continue;
241
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) == NULL) continue;
120
					} else {
242
					} else {
121
						if ((rematch(argv[optind], buf0, reflags)) != 0) continue;
243
						if (regexec(&preg, buf0, 0, NULL, 0) != 0) continue;
122
					}
244
					}
123
				} else {
245
				} else {
124
					if (do_regex == 0) {
246
					if (do_regex == 0) {
125
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) != NULL) continue;
247
						if (( (FUNC *) (strfunc) (buf0, argv[optind])) != NULL) continue;
126
					} else {
248
					} else {
127
						if ((rematch(argv[optind], buf0, reflags)) == 0) continue;
249
						if (regexec(&preg, buf0, 0, NULL, 0) == 0) continue;
128
					}
250
					}
129
				}
251
				}
130
252
131
				count++;
253
				count++;
132
				if (do_count) continue;
254
				if (do_count || do_list) continue;
133
				if (verbose || show_filename) {
255
				if (verbose || show_filename || show_name) {
134
					printf("%s:", ebuild);
256
					printf("%s:", (show_name ? name : ebuild));
135
					if (verbose > 1) printf("%d:", lineno);
257
					if (verbose > 1) printf("%d:", lineno);
136
					printf(" ");
258
					printf(" ");
137
				}
259
				}
Lines 139-154 Link Here
139
			}
261
			}
140
			fclose(newfp);
262
			fclose(newfp);
141
			if (do_count && count) {
263
			if (do_count && count) {
142
				if (verbose || show_filename) printf("%s:", ebuild);
264
				if (verbose || show_filename || show_name)
265
					printf("%s:", (show_name ? name : ebuild));
143
				printf("%d", count);
266
				printf("%d", count);
144
				puts("");
267
				puts("");
145
			}
268
			} else if (do_list && ((count && !invert_list) || (!count && invert_list)))
269
				printf("%s\n", (show_name ? name : ebuild));
146
		}
270
		}
147
	}
271
	}
148
	if (do_eclass)
272
	if (do_eclass)
149
		closedir(eclass_dir);
273
		closedir(eclass_dir);
150
	else
274
	else
151
		fclose(fp);
275
		fclose(fp);
276
	if (do_regex)
277
		regfree(&preg);
278
	if (do_regex && skip_pattern)
279
		regfree(&skip_preg);
280
	if (include_atoms != NULL) {
281
		for (i = 0; i < (argc - optind - 1); i++)
282
			if (include_atoms[i] != NULL)
283
				atom_implode(include_atoms[i]);
284
		free(include_atoms);
285
	}
152
	return EXIT_SUCCESS;
286
	return EXIT_SUCCESS;
153
}
287
}
154
288

Return to bug 171374