Skip to content
Newer
Older
100644 603 lines (528 sloc) 15.4 KB
62dc3d8 @falconindy copyright 2011
authored Jan 2, 2011
1 /* Copyright (c) 2010-2011 Dave Reisner
da3d331 @falconindy add MIT license header
authored Nov 30, 2010
2 *
3 * expac.c
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following
12 * conditions:
13 *
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
25 */
26
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
27 #define _GNU_SOURCE
28 #include <alpm.h>
29 #include <ctype.h>
30 #include <getopt.h>
919fa1c @falconindy use proper suffixes for package sizes
authored Aug 7, 2011
31 /* #include <limits.h> */
e1cb35f @falconindy fix up commenting
authored Nov 30, 2010
32 #include <stdbool.h>
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
dd7d84c @falconindy don't hardcode defaults
authored Dec 1, 2010
37 #define DEFAULT_DELIM "\n"
38 #define DEFAULT_LISTDELIM " "
39 #define DEFAULT_TIMEFMT "%c"
a971ee4 @falconindy %k is sync only
authored Dec 11, 2010
40 #define FORMAT_TOKENS "BCDEGLNOPRSabdmnprsuvw%"
7e66cff @falconindy filter token availability based on query type
authored Nov 30, 2010
41 #define FORMAT_TOKENS_LOCAL "ilFw"
a971ee4 @falconindy %k is sync only
authored Dec 12, 2010
42 #define FORMAT_TOKENS_SYNC "fk"
7e66cff @falconindy filter token availability based on query type
authored Dec 1, 2010
43 #define ESCAPE_TOKENS "\"\\abefnrtv"
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
44
919fa1c @falconindy use proper suffixes for package sizes
authored Aug 8, 2011
45 #ifndef PATH_MAX
46 #define PATH_MAX 4096
47 #endif
48
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 10, 2010
49 static char const digits[] = "0123456789";
50 static char const printf_flags[] = "'-+ #0I";
51
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
52 alpm_db_t *db_local = NULL;
7807fc4 @falconindy declare globals on separate lines for uniformity
authored Dec 1, 2010
53 alpm_list_t *dblist = NULL;
54 alpm_list_t *targets = NULL;
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
55 bool readone = false;
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
56 bool verbose = false;
57 bool search = false;
7e66cff @falconindy filter token availability based on query type
authored Dec 1, 2010
58 bool local = false;
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
59 bool groups = false;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
60 const char *format = NULL;
61 const char *timefmt = NULL;
62 const char *listdelim = NULL;
63 const char *delim = NULL;
928f60e @falconindy add %! as an auto-increment record counter
authored Jul 17, 2011
64 int pkgcounter = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
65
66 typedef const char *(*extractfn)(void*);
67
68 static char *strtrim(char *str) {
69 char *pch = str;
70
71 if (!str || *str == '\0') {
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
72 return str;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
73 }
74
75 while (isspace((unsigned char)*pch)) {
76 pch++;
77 }
78 if (pch != str) {
79 memmove(str, pch, (strlen(pch) + 1));
80 }
81
82 if (*str == '\0') {
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
83 return str;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
84 }
85
86 pch = (str + (strlen(str) - 1));
87 while (isspace((unsigned char)*pch)) {
88 pch--;
89 }
90 *++pch = '\0';
91
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
92 return str;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
93 }
94
5f1c74c @falconindy add static qualifier to trim_optdep
authored Jul 23, 2011
95 static char *trim_optdep(char *optdep) {
da0fbab @falconindy add %o for optdepends without descriptions
authored Jul 23, 2011
96 char *colon;
97
98 colon = strchr(optdep, ':');
99 if (colon) {
100 *colon = '\0';
101 }
102
103 return optdep;
104 }
105
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
106 static alpm_handle_t *alpm_init(void) {
107 alpm_handle_t *handle = NULL;
108 enum _alpm_errno_t alpm_errno = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
109 FILE *fp;
110 char line[PATH_MAX];
111 char *ptr, *section = NULL;
112
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
113 handle = alpm_initialize("/", "/var/lib/pacman", &alpm_errno);
114 if (!handle) {
115 alpm_strerror(alpm_errno);
116 return NULL;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
117 }
118
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
119 db_local = alpm_option_get_localdb(handle);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
120
121 fp = fopen("/etc/pacman.conf", "r");
122 if (!fp) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
123 perror("fopen: /etc/pacman.conf");
124 return handle;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
125 }
126
127 while (fgets(line, PATH_MAX, fp)) {
128 strtrim(line);
129
130 if (strlen(line) == 0 || line[0] == '#') {
131 continue;
132 }
133 if ((ptr = strchr(line, '#'))) {
134 *ptr = '\0';
135 }
136
137 if (line[0] == '[' && line[strlen(line) - 1] == ']') {
138 ptr = &line[1];
139 if (section) {
140 free(section);
141 }
142
143 section = strdup(ptr);
144 section[strlen(section) - 1] = '\0';
145
146 if (strcmp(section, "options") != 0) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
147 alpm_db_register_sync(handle, section,
148 ALPM_SIG_DATABASE | ALPM_SIG_DATABASE_OPTIONAL);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
149 }
150 }
151 }
152
153 free(section);
154 fclose(fp);
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
155 return handle;
156 }
157
158 static const char *alpm_dep_get_name(void *dep) {
159 return ((alpm_depend_t*)dep)->name;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
160 }
161
162 static void usage(void) {
163 fprintf(stderr, "expac %s\n"
164 "Usage: expac [options] <format> target...\n\n", VERSION);
165 fprintf(stderr,
166 " Options:\n"
167 " -Q, --local search local DB (default)\n"
8fd6a00 @falconindy assorted cleanup. remove assertions and unused format token
authored Nov 30, 2010
168 " -S, --sync search sync DBs\n"
e85f19c @falconindy fix usage
authored Apr 5, 2011
169 " -s, --search search for matching regex\n"
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
170 " -g, --group return packages matching targets as groups\n"
171 " -1, --readone return only the first result of a sync search\n\n"
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
172 " -d, --delim <string> separator used between packages (default: \"\\n\")\n"
173 " -l, --listdelim <string> separator used between list elements (default: \" \")\n"
916cc07 @falconindy fix up usage func
authored Nov 30, 2010
174 " -t, --timefmt <fmt> date format passed to strftime (default: \"%%c\")\n\n"
175 " -v, --verbose be more verbose\n\n"
176 " -h, --help display this help and exit\n\n");
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
177 }
178
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
179 static int parse_options(int argc, char *argv[], alpm_handle_t *handle) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
180 int opt, option_index = 0;
181
182 static struct option opts[] = {
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
183 {"readone", no_argument, 0, '1'},
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
184 {"delim", required_argument, 0, 'd'},
185 {"listdelim", required_argument, 0, 'l'},
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
186 {"group", required_argument, 0, 'g'},
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
187 {"help", no_argument, 0, 'h'},
188 {"local", no_argument, 0, 'Q'},
189 {"sync", no_argument, 0, 'S'},
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
190 {"search", no_argument, 0, 's'},
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
191 {"timefmt", required_argument, 0, 't'},
192 {"verbose", no_argument, 0, 'v'},
193 {0, 0, 0, 0}
194 };
195
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
196 while (-1 != (opt = getopt_long(argc, argv, "1l:d:ghf:QSst:v", opts, &option_index))) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
197 switch (opt) {
198 case 'S':
199 if (dblist) {
200 fprintf(stderr, "error: can only select one repo option (use -h for help)\n");
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
201 return 1;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
202 }
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
203 dblist = alpm_list_copy(alpm_option_get_syncdbs(handle));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
204 break;
205 case 'Q':
206 if (dblist) {
207 fprintf(stderr, "error: can only select one repo option (use -h for help)\n");
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
208 return 1;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
209 }
210 dblist = alpm_list_add(dblist, db_local);
7e66cff @falconindy filter token availability based on query type
authored Dec 1, 2010
211 local = true;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
212 break;
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
213 case '1':
214 readone = true;
215 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
216 case 'd':
217 delim = optarg;
218 break;
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
219 case 'g':
220 groups = true;
221 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
222 case 'l':
223 listdelim = optarg;
224 break;
225 case 'h':
226 usage();
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
227 return 1;
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
228 case 's':
229 search = true;
230 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
231 case 't':
232 timefmt = optarg;
233 break;
234 case 'v':
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
235 verbose = true;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
236 break;
237
238 case '?':
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
239 return 1;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
240 default:
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
241 return 1;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
242 }
243 }
244
245 if (optind < argc) {
246 format = argv[optind++];
247 } else {
248 fprintf(stderr, "error: missing format string (use -h for help)\n");
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
249 return 1;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
250 }
251
252 while (optind < argc) {
253 targets = alpm_list_add(targets, argv[optind++]);
254 }
255
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
256 return 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
257 }
258
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Nov 30, 2010
259 static int print_escaped(const char *delim) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
260 const char *f;
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
261 int out = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
262
263 for (f = delim; *f != '\0'; f++) {
264 if (*f == '\\') {
265 switch (*++f) {
266 case '\\':
267 putchar('\\');
268 break;
269 case '"':
270 putchar('\"');
271 break;
272 case 'a':
273 putchar('\a');
274 break;
275 case 'b':
276 putchar('\b');
277 break;
278 case 'e': /* \e is nonstandard */
279 putchar('\033');
280 break;
281 case 'n':
282 putchar('\n');
283 break;
284 case 'r':
285 putchar('\r');
286 break;
287 case 't':
288 putchar('\t');
289 break;
290 case 'v':
291 putchar('\v');
292 break;
fb8bdc6 @falconindy allow null terminators in format string
authored Jul 30, 2011
293 case '0':
294 putchar('\0');
295 break;
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
296 ++out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
297 }
298 } else {
299 putchar(*f);
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
300 ++out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
301 }
302 }
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
303
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
304 return out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
305 }
306
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
307 static int print_list(alpm_list_t *list, extractfn fn, bool shortdeps) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
308 alpm_list_t *i;
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
309 int out = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
310
311 if (!list) {
312 if (verbose) {
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
313 out += printf("None");
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
314 }
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
315 return out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
316 }
317
318 i = list;
319 while (1) {
320 char *item;
321
e1cb35f @falconindy fix up commenting
authored Dec 1, 2010
322 item = (char*)(fn ? fn(alpm_list_getdata(i)) : alpm_list_getdata(i));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
323
324 if (shortdeps) {
325 *(item + strcspn(item, "<>=")) = '\0';
326 }
327
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
328 out += printf("%s", item);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
329
330 if ((i = alpm_list_next(i))) {
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
331 out += print_escaped(listdelim);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
332 } else {
333 break;
334 }
335 }
336
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
337 return out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
338 }
339
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
340 static int print_time(time_t timestamp) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
341 char buffer[64];
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
342 int out = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
343
344 if (!timestamp) {
345 if (verbose) {
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
346 out += printf("None");
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
347 }
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
348 return out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
349 }
350
e1cb35f @falconindy fix up commenting
authored Dec 1, 2010
351 /* no overflow here, strftime prints a max of 64 including null */
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
352 strftime(&buffer[0], 64, timefmt, localtime(&timestamp));
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
353 out += printf("%s", buffer);
354
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
355 return out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
356 }
357
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
358 static int print_pkg(alpm_pkg_t *pkg, const char *format) {
3d4bf93 @falconindy don't depend on format pointer reaching a NULL
authored Mar 23, 2011
359 const char *f, *end;
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
360 char fmt[32];
361 int len, out = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
362
3d4bf93 @falconindy don't depend on format pointer reaching a NULL
authored Mar 24, 2011
363 end = rawmemchr(format, '\0');
364
365 for (f = format; f < end; f++) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
366 bool shortdeps = false;
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
367 len = 0;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
368 if (*f == '%') {
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
369 len = strspn(f + 1 + len, printf_flags);
370 len += strspn(f + 1 + len, digits);
371 snprintf(fmt, len + 3, "%ss", f);
372 fmt[len + 1] = 's';
373 f += len + 1;
374 switch (*f) {
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
375 /* simple attributes */
376 case 'f': /* filename */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
377 out += printf(fmt, alpm_pkg_get_filename(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
378 break;
379 case 'n': /* package name */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
380 out += printf(fmt, alpm_pkg_get_name(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
381 break;
382 case 'v': /* version */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
383 out += printf(fmt, alpm_pkg_get_version(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
384 break;
385 case 'd': /* description */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
386 out += printf(fmt, alpm_pkg_get_desc(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
387 break;
388 case 'u': /* project url */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
389 out += printf(fmt, alpm_pkg_get_url(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
390 break;
391 case 'p': /* packager name */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
392 out += printf(fmt, alpm_pkg_get_packager(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
393 break;
394 case 's': /* md5sum */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
395 out += printf(fmt, alpm_pkg_get_md5sum(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
396 break;
397 case 'a': /* architecutre */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
398 out += printf(fmt, alpm_pkg_get_arch(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
399 break;
400 case 'i': /* has install scriptlet? */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
401 out += printf(fmt, alpm_pkg_has_scriptlet(pkg) ? "yes" : "no");
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
402 break;
403 case 'r': /* repo */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
404 out += printf(fmt, alpm_db_get_name(alpm_pkg_get_db(pkg)));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
405 break;
d35f8eb @falconindy add install reason as %w
authored Nov 30, 2010
406 case 'w': /* install reason */
e835af7 @falconindy allow real printf formatters for simple strings
authored Dec 11, 2010
407 out += printf(fmt, alpm_pkg_get_reason(pkg) ? "dependency" : "explicit");
d35f8eb @falconindy add install reason as %w
authored Dec 1, 2010
408 break;
928f60e @falconindy add %! as an auto-increment record counter
authored Jul 17, 2011
409 case '!': /* result number */
410 out += printf("%d", pkgcounter++);
411 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
412
413 /* times */
414 case 'b': /* build date */
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
415 out += print_time(alpm_pkg_get_builddate(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
416 break;
417 case 'l': /* install date */
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
418 out += print_time(alpm_pkg_get_installdate(pkg));
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
419 break;
420
421 /* sizes */
422 case 'k': /* download size */
919fa1c @falconindy use proper suffixes for package sizes
authored Aug 8, 2011
423 out += printf("%.2f KiB", (float)alpm_pkg_get_size(pkg) / 1024.0);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
424 break;
425 case 'm': /* install size */
919fa1c @falconindy use proper suffixes for package sizes
authored Aug 8, 2011
426 out += printf("%.2f KiB", (float)alpm_pkg_get_isize(pkg) / 1024.0);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
427 break;
428
429 /* lists */
430 case 'N': /* requiredby */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
431 out += print_list(alpm_pkg_compute_requiredby(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
432 break;
433 case 'L': /* licenses */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
434 out += print_list(alpm_pkg_get_licenses(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
435 break;
436 case 'G': /* groups */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
437 out += print_list(alpm_pkg_get_groups(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
438 break;
439 case 'E': /* depends (shortdeps) */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
440 out += print_list(alpm_pkg_get_depends(pkg), (extractfn)alpm_dep_get_name, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
441 break;
442 case 'D': /* depends */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
443 out += print_list(alpm_pkg_get_depends(pkg), (extractfn)alpm_dep_compute_string, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
444 break;
445 case 'O': /* optdepends */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
446 out += print_list(alpm_pkg_get_optdepends(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
447 break;
da0fbab @falconindy add %o for optdepends without descriptions
authored Jul 23, 2011
448 case 'o': /* optdepends (shortdeps) */
449 out += print_list(alpm_pkg_get_optdepends(pkg), (extractfn)trim_optdep, shortdeps);
450 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
451 case 'C': /* conflicts */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
452 out += print_list(alpm_pkg_get_conflicts(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
453 break;
454 case 'S': /* provides (shortdeps) */
455 shortdeps = true;
456 case 'P': /* provides */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
457 out += print_list(alpm_pkg_get_provides(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
458 break;
459 case 'R': /* replaces */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
460 out += print_list(alpm_pkg_get_replaces(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
461 break;
462 case 'F': /* files */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
463 out += print_list(alpm_pkg_get_files(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
464 break;
465 case 'B': /* backup */
0c15e7e @falconindy Revert "fix memory leak in print_pkg"
authored Dec 14, 2010
466 out += print_list(alpm_pkg_get_backup(pkg), NULL, shortdeps);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
467 break;
468 case '%':
469 putchar('%');
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
470 out++;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
471 break;
8e8bee9 @falconindy print ? for unknown tokens
authored Dec 12, 2010
472 default:
473 putchar('?');
474 out++;
475 break;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
476 }
477 } else if (*f == '\\') {
e1cb35f @falconindy fix up commenting
authored Dec 1, 2010
478 char buf[3]; /* its not safe to do this in a single sprintf */
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
479 buf[0] = *f;
480 buf[1] = *++f;
481 buf[2] = '\0';
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
482 out += print_escaped(buf);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
483 } else {
484 putchar(*f);
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
485 out++;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
486 }
487 }
488
4b61b31 @falconindy don't print a pkg delimeter if pkg has no output
authored Dec 1, 2010
489 /* only print a delimeter if any package data was outputted */
490 if (out > 0) {
491 print_escaped(delim);
492 }
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
493
2cabb7f @falconindy return 1 if nothing is printed by print_pkg
authored Jul 17, 2011
494 return !out;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
495 }
496
5fae8f2 @falconindy declare resolve_pkg as static
authored Mar 21, 2011
497 static alpm_list_t *resolve_pkg(alpm_list_t *targets) {
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
498 char *pkgname, *reponame;
499 alpm_list_t *t, *r, *ret = NULL;
500
d1a229a @falconindy mimic pacman behavior of dumping the entire DB with no targets
authored Mar 16, 2011
501 if (!targets) {
502 for (r = dblist; r; r = alpm_list_next(r)) {
503 /* joining causes corruption on alpm_release(), so we copy */
504 ret = alpm_list_join(ret, alpm_list_copy(alpm_db_get_pkgcache(alpm_list_getdata(r))));
505 }
506 } else if (search) {
507 for (r = dblist; r; r = alpm_list_next(r)) {
508 ret = alpm_list_join(ret, alpm_db_search(alpm_list_getdata(r), targets));
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
509 }
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
510 } else if (groups) {
511 for (t = targets; t; t = alpm_list_next(t)) {
512 for (r = dblist; r; r = alpm_list_next(r)) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
513 alpm_group_t *grp = alpm_db_readgroup(alpm_list_getdata(r), alpm_list_getdata(t));
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
514 if (grp) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
515 ret = alpm_list_join(ret, alpm_list_copy(grp->packages));
3664af8 @falconindy add support for searching by group
authored Apr 5, 2011
516 }
517 }
518 }
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
519 } else {
520 for (t = targets; t; t = alpm_list_next(t)) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
521 alpm_pkg_t *pkg = NULL;
d818184 @falconindy don't report not found on found packages
authored Jul 23, 2011
522 int found = 0;
20affa5 @falconindy fix verbose error on each repo when package isn't found
authored Jul 17, 2011
523
f4e8889 @falconindy refactor resolve_pkg
authored Dec 1, 2010
524 pkgname = reponame = alpm_list_getdata(t);
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
525 if (strchr(pkgname, '/')) {
526 strsep(&pkgname, "/");
527 } else {
528 reponame = NULL;
529 }
530
531 for (r = dblist; r; r = alpm_list_next(r)) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
532 alpm_db_t *repo = alpm_list_getdata(r);
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
533
f4e8889 @falconindy refactor resolve_pkg
authored Dec 1, 2010
534 if (reponame && strcmp(reponame, alpm_db_get_name(repo)) != 0) {
535 continue;
536 }
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
537
20affa5 @falconindy fix verbose error on each repo when package isn't found
authored Jul 18, 2011
538 if (!(pkg = alpm_db_get_pkg(repo, pkgname)) &&
539 !(pkg = alpm_find_satisfier(alpm_db_get_pkgcache(repo), pkgname))) {
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
540 continue;
541 }
542
d818184 @falconindy don't report not found on found packages
authored Jul 23, 2011
543 found = 1;
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
544 ret = alpm_list_add(ret, pkg);
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
545 if (readone) {
2751896 @falconindy return first of each key, not for all keys
authored May 29, 2011
546 break;
ccd22f9 @falconindy add -1 option for limited -S operations
authored May 29, 2011
547 }
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
548 }
d818184 @falconindy don't report not found on found packages
authored Jul 23, 2011
549 if (!found && verbose) {
20affa5 @falconindy fix verbose error on each repo when package isn't found
authored Jul 18, 2011
550 fprintf(stderr, "error: package `%s' not found\n", pkgname);
551 }
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
552 }
553 }
554
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
555 return ret;
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
556 }
557
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
558 int main(int argc, char *argv[]) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
559 int ret = 1;
560 alpm_handle_t *handle;
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
561 alpm_list_t *results, *i;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
562
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
563 handle = alpm_init();
564 if (!handle) {
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
565 return ret;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
566 }
567
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
568 ret = parse_options(argc, argv, handle);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
569 if (ret != 0) {
570 goto finish;
571 }
572
e1cb35f @falconindy fix up commenting
authored Dec 1, 2010
573 /* ensure sane defaults */
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
574 if (!dblist) {
7e66cff @falconindy filter token availability based on query type
authored Dec 1, 2010
575 local = true;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
576 dblist = alpm_list_add(dblist, db_local);
577 }
dd7d84c @falconindy don't hardcode defaults
authored Dec 1, 2010
578 delim = delim ? delim : DEFAULT_DELIM;
579 listdelim = listdelim ? listdelim : DEFAULT_LISTDELIM;
580 timefmt = timefmt ? timefmt : DEFAULT_TIMEFMT;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
581
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
582 results = resolve_pkg(targets);
583 if (!results) {
584 ret = 1;
585 goto finish;
586 }
587
588 for (i = results; i; i = alpm_list_next(i)) {
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
589 alpm_pkg_t *pkg = alpm_list_getdata(i);
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
590 ret += print_pkg(pkg, format);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
591 }
4e2438f @falconindy fix bug in verify_format
authored Nov 30, 2010
592 ret = !!ret; /* clamp to zero/one */
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
593
b185b05 @falconindy Implement searching. Why am I rewriting pacman?
authored Nov 30, 2010
594 alpm_list_free(results);
595
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
596 finish:
044c141 @falconindy fix leak by always allocating for the dblist
authored Nov 30, 2010
597 alpm_list_free(dblist);
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
598 alpm_list_free(targets);
c69f136 @falconindy fix up alpm API calls for alpm7
authored Jun 15, 2011
599 alpm_release(handle);
3a5d0f9 @falconindy return(val) => return val
authored Mar 21, 2011
600 return ret;
9cde8d2 @falconindy initial commit
authored Nov 28, 2010
601 }
602
Something went wrong with that request. Please try again.