/[gentoo-projects]/portage-utils/qsearch.c
Gentoo

Contents of /portage-utils/qsearch.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.42 - (show annotations) (download) (as text)
Sun Oct 28 04:31:20 2012 UTC (20 months, 3 weeks ago) by vapier
Branch: MAIN
Changes since 1.41: +19 -14 lines
File MIME type: text/x-csrc
show matched pkgs even when DESCRIPTION could not be found #435636 by slawomir.nizio

1 /*
2 * Copyright 2005-2010 Gentoo Foundation
3 * Distributed under the terms of the GNU General Public License v2
4 * $Header: /var/cvsroot/gentoo-projects/portage-utils/qsearch.c,v 1.41 2012/10/28 04:16:19 vapier Exp $
5 *
6 * Copyright 2005-2010 Ned Ludd - <solar@gentoo.org>
7 * Copyright 2005-2010 Mike Frysinger - <vapier@gentoo.org>
8 */
9
10 #ifdef APPLET_qsearch
11
12 #define QSEARCH_FLAGS "acesSNH" COMMON_FLAGS
13 static struct option const qsearch_long_opts[] = {
14 {"all", no_argument, NULL, 'a'},
15 {"cache", no_argument, NULL, 'c'},
16 {"ebuilds", no_argument, NULL, 'e'},
17 {"search", no_argument, NULL, 's'},
18 {"desc", a_argument, NULL, 'S'},
19 {"name-only", no_argument, NULL, 'N'},
20 {"homepage", no_argument, NULL, 'H'},
21 COMMON_LONG_OPTS
22 };
23 static const char * const qsearch_opts_help[] = {
24 "List the descriptions of every package in the cache",
25 "Use the portage cache (default)",
26 "Use the portage ebuild tree",
27 "Regex search package basenames",
28 "Regex search package descriptions",
29 "Only show package name",
30 "Show homepage info",
31 COMMON_OPTS_HELP
32 };
33 static const char qsearch_rcsid[] = "$Id: qsearch.c,v 1.41 2012/10/28 04:16:19 vapier Exp $";
34 #define qsearch_usage(ret) usage(ret, QSEARCH_FLAGS, qsearch_long_opts, qsearch_opts_help, lookup_applet_idx("qsearch"))
35
36 int qsearch_main(int argc, char **argv)
37 {
38 FILE *fp;
39 char buf[_Q_PATH_MAX];
40 char ebuild[_Q_PATH_MAX];
41 char last[126] = "";
42 char *p, *q, *str;
43 char *search_me = NULL;
44 char show_homepage = 0, show_name_only = 0;
45 char search_desc = 0, search_all = 0, search_name = 1, search_cache = CACHE_EBUILD;
46 const char *search_vars[] = { "DESCRIPTION=", "HOMEPAGE=" };
47 size_t search_len;
48 int i, idx=0;
49
50 DBG("argc=%d argv[0]=%s argv[1]=%s",
51 argc, argv[0], argc > 1 ? argv[1] : "NULL?");
52
53 while ((i = GETOPT_LONG(QSEARCH, qsearch, "")) != -1) {
54 switch (i) {
55 COMMON_GETOPTS_CASES(qsearch)
56 case 'a': search_all = 1; break;
57 case 'c': search_cache = CACHE_METADATA; break;
58 case 'e': search_cache = CACHE_EBUILD; break;
59 case 's': search_desc = 0; search_name = 1; break;
60 case 'S': search_desc = 1; search_name = 0; break;
61 case 'N': show_name_only = 1; break;
62 case 'H': show_homepage = 1, idx = 1; break;
63 }
64 }
65
66 if (search_all) {
67 search_desc = 1;
68 search_name = 0;
69 } else {
70 if (argc == optind)
71 qsearch_usage(EXIT_FAILURE);
72 search_me = argv[optind];
73 }
74 #ifdef TESTING
75 /* FIXME: hardcoded */
76 if ((search_cache == CACHE_EBUILD) && (access("/usr/portage/.qsearch.x", R_OK) == 0)) {
77 if ((fp = fopen("/usr/portage/.qsearch.x", "r")) != NULL) {
78 search_len = strlen(search_me);
79 while (fgets(buf, sizeof(buf), fp) != NULL) {
80 if (strlen(buf) <= search_len)
81 continue;
82 /* add regexp, color highlighting and basename checks */
83 if (strncmp(buf, search_me, search_len) == 0) {
84 fputs(buf, stdout);
85 }
86 }
87 fclose(fp);
88 return 0;
89 }
90 }
91 #endif
92 last[0] = 0;
93 fp = fopen(initialize_flat(search_cache), "r");
94 if (!fp)
95 return 1;
96
97 search_len = strlen(search_vars[idx]);
98
99 while (fgets(ebuild, sizeof(ebuild), fp) != NULL) {
100 if ((p = strchr(ebuild, '\n')) != NULL)
101 *p = 0;
102 if (!ebuild[0])
103 continue;
104
105 switch (search_cache) {
106
107 case CACHE_METADATA: {
108 portage_cache *pcache;
109 if ((pcache = cache_read_file(ebuild)) != NULL) {
110 if (strcmp(pcache->atom->PN, last) != 0) {
111 strncpy(last, pcache->atom->PN, sizeof(last));
112 if ((rematch(search_me, (search_desc ? pcache->DESCRIPTION : ebuild), REG_EXTENDED | REG_ICASE) == 0) || search_all)
113 printf("%s%s/%s%s%s %s\n", BOLD, pcache->atom->CATEGORY, BLUE,
114 pcache->atom->PN, NORM,
115 (show_name_only ? "" :
116 (show_homepage ? pcache->HOMEPAGE : pcache->DESCRIPTION)));
117 }
118 cache_free(pcache);
119 } else {
120 if (!reinitialize)
121 warnf("(cache update pending) %s", ebuild);
122 reinitialize = 1;
123 }
124 break;
125 }
126
127 case CACHE_EBUILD: {
128 FILE *ebuildfp;
129 str = xstrdup(ebuild);
130 p = dirname(str);
131
132 if (strcmp(p, last) != 0) {
133 bool show_it = false;
134 strncpy(last, p, sizeof(last));
135 if (search_name) {
136 if (rematch(search_me, basename(last), REG_EXTENDED | REG_ICASE) != 0) {
137 goto no_cache_ebuild_match;
138 } else {
139 q = NULL;
140 show_it = true;
141 }
142 }
143
144 if ((ebuildfp = fopen(ebuild, "r")) != NULL) {
145 while (fgets(buf, sizeof(buf), ebuildfp) != NULL) {
146 if (strlen(buf) <= search_len)
147 continue;
148 if (strncmp(buf, search_vars[idx], search_len) == 0) {
149 if ((q = strrchr(buf, '"')) != NULL)
150 *q = 0;
151 if (strlen(buf) <= search_len)
152 break;
153 q = buf + search_len + 1;
154 if (!search_all && !search_name && rematch(search_me, q, REG_EXTENDED | REG_ICASE) != 0)
155 break;
156 show_it = true;
157 break;
158 }
159 }
160 fclose(ebuildfp);
161 } else {
162 if (!reinitialize)
163 warnfp("(cache update pending) %s", ebuild);
164 reinitialize = 1;
165 }
166
167 if (show_it) {
168 const char *b = basename(p);
169 printf("%s%s/%s%s%s %s\n",
170 BOLD, dirname(p), BLUE, b, NORM,
171 (show_name_only ? "" : q ? : "<no DESCRIPTION found>"));
172 }
173 }
174 no_cache_ebuild_match:
175 free(str);
176
177 break;
178 } /* case CACHE_EBUILD */
179 } /* switch (search_cache) */
180 }
181 fclose(fp);
182 return EXIT_SUCCESS;
183 }
184
185 #else
186 DEFINE_APPLET_STUB(qsearch)
187 #endif

  ViewVC Help
Powered by ViewVC 1.1.20