makepkg: add libprovides support
[pacman-ng.git] / src / pacman / pacman.c
blob1a6e3eb4a8d6dbfa2586bbf4527e14ca46b3af6c
1 /*
2 * pacman.c
4 * Copyright (c) 2006-2011 Pacman Development Team <pacman-dev@archlinux.org>
5 * Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "config.h"
23 /* special handling of package version for GIT */
24 #if defined(GIT_VERSION)
25 #undef PACKAGE_VERSION
26 #define PACKAGE_VERSION GIT_VERSION
27 #endif
29 #include <ctype.h> /* isspace */
30 #include <stdlib.h> /* atoi */
31 #include <stdio.h>
32 #include <ctype.h> /* isspace */
33 #include <limits.h>
34 #include <getopt.h>
35 #include <string.h>
36 #include <signal.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/utsname.h> /* uname */
40 #include <locale.h> /* setlocale */
41 #include <errno.h>
42 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
43 #include <mcheck.h> /* debug tracing (mtrace) */
44 #endif
46 /* alpm */
47 #include <alpm.h>
48 #include <alpm_list.h>
50 /* pacman */
51 #include "pacman.h"
52 #include "util.h"
53 #include "callback.h"
54 #include "conf.h"
56 /* list of targets specified on command line */
57 static alpm_list_t *pm_targets;
59 /* Used to sort the options in --help */
60 static int options_cmp(const void *p1, const void *p2)
62 const char *s1 = p1;
63 const char *s2 = p2;
65 if(s1 == s2) return 0;
66 if(!s1) return -1;
67 if(!s2) return 1;
68 /* First skip all spaces in both strings */
69 while(isspace((unsigned char)*s1)) {
70 s1++;
72 while(isspace((unsigned char)*s2)) {
73 s2++;
75 /* If we compare a long option (--abcd) and a short one (-a),
76 * the short one always wins */
77 if(*s1 == '-' && *s2 == '-') {
78 s1++;
79 s2++;
80 if(*s1 == '-' && *s2 == '-') {
81 /* two long -> strcmp */
82 s1++;
83 s2++;
84 } else if(*s2 == '-') {
85 /* s1 short, s2 long */
86 return -1;
87 } else if(*s1 == '-') {
88 /* s1 long, s2 short */
89 return 1;
91 /* two short -> strcmp */
94 return strcmp(s1, s2);
97 /** Display usage/syntax for the specified operation.
98 * @param op the operation code requested
99 * @param myname basename(argv[0])
101 static void usage(int op, const char * const myname)
103 #define addlist(s) (list = alpm_list_add(list, s))
104 alpm_list_t *list = NULL, *i;
105 /* prefetch some strings for usage below, which moves a lot of calls
106 * out of gettext. */
107 char const * const str_opt = _("options");
108 char const * const str_file = _("file(s)");
109 char const * const str_pkg = _("package(s)");
110 char const * const str_usg = _("usage");
111 char const * const str_opr = _("operation");
113 /* please limit your strings to 80 characters in width */
114 if(op == PM_OP_MAIN) {
115 printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr);
116 printf(_("operations:\n"));
117 printf(" %s {-h --help}\n", myname);
118 printf(" %s {-V --version}\n", myname);
119 printf(" %s {-D --database} <%s> <%s>\n", myname, str_opt, str_pkg);
120 printf(" %s {-Q --query} [%s] [%s]\n", myname, str_opt, str_pkg);
121 printf(" %s {-R --remove} [%s] <%s>\n", myname, str_opt, str_pkg);
122 printf(" %s {-S --sync} [%s] [%s]\n", myname, str_opt, str_pkg);
123 printf(" %s {-T --deptest} [%s] [%s]\n", myname, str_opt, str_pkg);
124 printf(" %s {-U --upgrade} [%s] <%s>\n", myname, str_opt, str_file);
125 printf(_("\nuse '%s {-h --help}' with an operation for available options\n"),
126 myname);
127 } else {
128 if(op == PM_OP_REMOVE) {
129 printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg);
130 printf("%s:\n", str_opt);
131 addlist(_(" -c, --cascade remove packages and all packages that depend on them\n"));
132 addlist(_(" -n, --nosave remove configuration files\n"));
133 addlist(_(" -s, --recursive remove unnecessary dependencies\n"
134 " (-ss includes explicitly installed dependencies)\n"));
135 addlist(_(" -u, --unneeded remove unneeded packages\n"));
136 } else if(op == PM_OP_UPGRADE) {
137 printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file);
138 printf("%s:\n", str_opt);
139 } else if(op == PM_OP_QUERY) {
140 printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
141 printf("%s:\n", str_opt);
142 addlist(_(" -c, --changelog view the changelog of a package\n"));
143 addlist(_(" -d, --deps list packages installed as dependencies [filter]\n"));
144 addlist(_(" -e, --explicit list packages explicitly installed [filter]\n"));
145 addlist(_(" -g, --groups view all members of a package group\n"));
146 addlist(_(" -i, --info view package information (-ii for backup files)\n"));
147 addlist(_(" -k, --check check that the files owned by the package(s) are present\n"));
148 addlist(_(" -l, --list list the contents of the queried package\n"));
149 addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
150 addlist(_(" -o, --owns <file> query the package that owns <file>\n"));
151 addlist(_(" -p, --file <package> query a package file instead of the database\n"));
152 addlist(_(" -q, --quiet show less information for query and search\n"));
153 addlist(_(" -s, --search <regex> search locally-installed packages for matching strings\n"));
154 addlist(_(" -t, --unrequired list packages not required by any package [filter]\n"));
155 addlist(_(" -u, --upgrades list outdated packages [filter]\n"));
156 } else if(op == PM_OP_SYNC) {
157 printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
158 printf("%s:\n", str_opt);
159 addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n"));
160 addlist(_(" -g, --groups view all members of a package group\n"));
161 addlist(_(" -i, --info view package information\n"));
162 addlist(_(" -l, --list <repo> view a list of packages in a repo\n"));
163 addlist(_(" -q, --quiet show less information for query and search\n"));
164 addlist(_(" -s, --search <regex> search remote repositories for matching strings\n"));
165 addlist(_(" -u, --sysupgrade upgrade installed packages (-uu allows downgrade)\n"));
166 addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
167 addlist(_(" -y, --refresh download fresh package databases from the server\n"));
168 addlist(_(" --needed don't reinstall up to date packages\n"));
169 } else if(op == PM_OP_DATABASE) {
170 printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg);
171 printf("%s:\n", str_opt);
172 addlist(_(" --asdeps mark packages as non-explicitly installed\n"));
173 addlist(_(" --asexplicit mark packages as explicitly installed\n"));
174 } else if(op == PM_OP_DEPTEST) {
175 printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
176 printf("%s:\n", str_opt);
178 switch(op) {
179 case PM_OP_SYNC:
180 case PM_OP_UPGRADE:
181 addlist(_(" -f, --force force install, overwrite conflicting files\n"));
182 addlist(_(" --asdeps install packages as non-explicitly installed\n"));
183 addlist(_(" --asexplicit install packages as explicitly installed\n"));
184 addlist(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
185 addlist(_(" --ignoregroup <grp>\n"
186 " ignore a group upgrade (can be used more than once)\n"));
187 /* pass through */
188 case PM_OP_REMOVE:
189 addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n"));
190 addlist(_(" -k, --dbonly only modify database entries, not package files\n"));
191 addlist(_(" --noprogressbar do not show a progress bar when downloading files\n"));
192 addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
193 addlist(_(" --print print the targets instead of performing the operation\n"));
194 addlist(_(" --print-format <string>\n"
195 " specify how the targets should be printed\n"));
196 break;
199 addlist(_(" -b, --dbpath <path> set an alternate database location\n"));
200 addlist(_(" -r, --root <path> set an alternate installation root\n"));
201 addlist(_(" -v, --verbose be verbose\n"));
202 addlist(_(" --arch <arch> set an alternate architecture\n"));
203 addlist(_(" --cachedir <dir> set an alternate package cache location\n"));
204 addlist(_(" --config <path> set an alternate configuration file\n"));
205 addlist(_(" --debug display debug messages\n"));
206 addlist(_(" --gpgdir <path> set an alternate home directory for GnuPG\n"));
207 addlist(_(" --logfile <path> set an alternate log file\n"));
208 addlist(_(" --noconfirm do not ask for any confirmation\n"));
210 list = alpm_list_msort(list, alpm_list_count(list), options_cmp);
211 for (i = list; i; i = alpm_list_next(i)) {
212 printf("%s", (char *)alpm_list_getdata(i));
214 alpm_list_free(list);
215 #undef addlist
218 /** Output pacman version and copyright.
220 static void version(void)
222 printf("\n");
223 printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version());
224 printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2011 Pacman Development Team\n");
225 printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n");
226 printf(" '--'\n");
227 printf(_(" This program may be freely redistributed under\n"
228 " the terms of the GNU General Public License.\n"));
229 printf("\n");
232 /** Sets up gettext localization. Safe to call multiple times.
234 /* Inspired by the monotone function localize_monotone. */
235 #if defined(ENABLE_NLS)
236 static void localize(void)
238 static int init = 0;
239 if(!init) {
240 setlocale(LC_ALL, "");
241 bindtextdomain(PACKAGE, LOCALEDIR);
242 textdomain(PACKAGE);
243 init = 1;
246 #endif
248 /** Set user agent environment variable.
250 static void setuseragent(void)
252 char agent[101];
253 struct utsname un;
255 uname(&un);
256 snprintf(agent, 100, "pacman/%s (%s %s) libalpm/%s",
257 PACKAGE_VERSION, un.sysname, un.machine, alpm_version());
258 setenv("HTTP_USER_AGENT", agent, 0);
261 /** Free the resources.
263 * @param ret the return value
265 static void cleanup(int ret) {
266 /* free alpm library resources */
267 if(alpm_release() == -1) {
268 pm_printf(PM_LOG_ERROR, "%s\n", alpm_strerrorlast());
271 /* free memory */
272 FREELIST(pm_targets);
273 if(config) {
274 config_free(config);
275 config = NULL;
278 exit(ret);
281 /** Write function that correctly handles EINTR.
283 static ssize_t xwrite(int fd, const void *buf, size_t count)
285 ssize_t ret;
286 do {
287 ret = write(fd, buf, count);
288 } while(ret == -1 && errno == EINTR);
289 return ret;
292 /** Catches thrown signals. Performs necessary cleanup to ensure database is
293 * in a consistant state.
294 * @param signum the thrown signal
296 static void handler(int signum)
298 int out = fileno(stdout);
299 int err = fileno(stderr);
300 if(signum == SIGSEGV) {
301 const char *msg1 = "error: segmentation fault\n";
302 const char *msg2 = "Internal pacman error: Segmentation fault.\n"
303 "Please submit a full bug report with --debug if appropriate.\n";
304 /* write a error message to out, the rest to err */
305 xwrite(out, msg1, strlen(msg1));
306 xwrite(err, msg2, strlen(msg2));
307 exit(signum);
308 } else if(signum == SIGINT) {
309 const char *msg = "\nInterrupt signal received\n";
310 xwrite(err, msg, strlen(msg));
311 if(alpm_trans_interrupt() == 0) {
312 /* a transaction is being interrupted, don't exit pacman yet. */
313 return;
315 /* no commiting transaction, we can release it now and then exit pacman */
316 alpm_trans_release();
317 /* output a newline to be sure we clear any line we may be on */
318 xwrite(out, "\n", 1);
320 cleanup(signum);
323 #define check_optarg() if(!optarg) { return 1; }
325 typedef int (*fn_add) (const char *s);
327 static int parsearg_util_addlist(fn_add fn)
329 alpm_list_t *list = NULL, *item = NULL; /* lists for splitting strings */
331 check_optarg();
332 list = strsplit(optarg, ',');
333 for(item = list; item; item = alpm_list_next(item)) {
334 fn((char *)alpm_list_getdata(item));
336 FREELIST(list);
337 return 0;
340 /** Helper function for parsing operation from command-line arguments.
341 * @param opt Keycode returned by getopt_long
342 * @param dryrun If nonzero, application state is NOT changed
343 * @return 0 if opt was handled, 1 if it was not handled
345 static int parsearg_op(int opt, int dryrun)
347 switch(opt) {
348 /* operations */
349 case 'D':
350 if(dryrun) break;
351 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DATABASE); break;
352 case 'Q':
353 if(dryrun) break;
354 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
355 case 'R':
356 if(dryrun) break;
357 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
358 case 'S':
359 if(dryrun) break;
360 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
361 case 'T':
362 if(dryrun) break;
363 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
364 case 'U':
365 if(dryrun) break;
366 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
367 case 'V':
368 if(dryrun) break;
369 config->version = 1; break;
370 case 'h':
371 if(dryrun) break;
372 config->help = 1; break;
373 default:
374 return 1;
376 return 0;
379 /** Helper functions for parsing command-line arguments.
380 * @param opt Keycode returned by getopt_long
381 * @return 0 on success, 1 on failure
383 static int parsearg_global(int opt)
385 switch(opt) {
386 case OP_ARCH:
387 check_optarg();
388 config_set_arch(optarg);
389 break;
390 case OP_ASK:
391 check_optarg();
392 config->noask = 1;
393 config->ask = (unsigned int)atoi(optarg);
394 break;
395 case OP_CACHEDIR:
396 check_optarg();
397 if(alpm_option_add_cachedir(optarg) != 0) {
398 pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
399 optarg, alpm_strerrorlast());
400 return 1;
402 break;
403 case OP_CONFIG:
404 check_optarg();
405 if(config->configfile) {
406 free(config->configfile);
408 config->configfile = strndup(optarg, PATH_MAX);
409 break;
410 case OP_DEBUG:
411 /* debug levels are made more 'human readable' than using a raw logmask
412 * here, error and warning are set in config_new, though perhaps a
413 * --quiet option will remove these later */
414 if(optarg) {
415 unsigned short debug = (unsigned short)atoi(optarg);
416 switch(debug) {
417 case 2:
418 config->logmask |= PM_LOG_FUNCTION; /* fall through */
419 case 1:
420 config->logmask |= PM_LOG_DEBUG;
421 break;
422 default:
423 pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
424 optarg);
425 return 1;
427 } else {
428 config->logmask |= PM_LOG_DEBUG;
430 /* progress bars get wonky with debug on, shut them off */
431 config->noprogressbar = 1;
432 break;
433 case OP_GPGDIR:
434 config->gpgdir = strdup(optarg);
435 break;
436 case OP_LOGFILE:
437 check_optarg();
438 config->logfile = strndup(optarg, PATH_MAX);
439 break;
440 case OP_NOCONFIRM: config->noconfirm = 1; break;
441 case 'b':
442 check_optarg();
443 config->dbpath = strdup(optarg);
444 break;
445 case 'r': check_optarg(); config->rootdir = strdup(optarg); break;
446 case 'v': (config->verbose)++; break;
447 default: return 1;
449 return 0;
452 static int parsearg_database(int opt)
454 switch(opt) {
455 case OP_ASDEPS: config->flags |= PM_TRANS_FLAG_ALLDEPS; break;
456 case OP_ASEXPLICIT: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break;
457 default: return 1;
459 return 0;
462 static int parsearg_query(int opt)
464 switch(opt) {
465 case 'c': config->op_q_changelog = 1; break;
466 case 'd': config->op_q_deps = 1; break;
467 case 'e': config->op_q_explicit = 1; break;
468 case 'g': (config->group)++; break;
469 case 'i': (config->op_q_info)++; break;
470 case 'k': config->op_q_check = 1; break;
471 case 'l': config->op_q_list = 1; break;
472 case 'm': config->op_q_foreign = 1; break;
473 case 'o': config->op_q_owns = 1; break;
474 case 'p': config->op_q_isfile = 1; break;
475 case 'q': config->quiet = 1; break;
476 case 's': config->op_q_search = 1; break;
477 case 't': config->op_q_unrequired = 1; break;
478 case 'u': config->op_q_upgrade = 1; break;
479 default: return 1;
481 return 0;
484 /* options common to -S -R -U */
485 static int parsearg_trans(int opt)
487 switch(opt) {
488 case 'd':
489 if(config->flags & PM_TRANS_FLAG_NODEPVERSION) {
490 config->flags |= PM_TRANS_FLAG_NODEPS;
491 } else {
492 config->flags |= PM_TRANS_FLAG_NODEPVERSION;
494 break;
495 case 'k': config->flags |= PM_TRANS_FLAG_DBONLY; break;
496 case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
497 case OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
498 case 'p': config->print = 1; break;
499 case OP_PRINTFORMAT:
500 check_optarg();
501 config->print_format = strdup(optarg);
502 break;
503 default: return 1;
505 return 0;
508 static int parsearg_remove(int opt)
510 if(parsearg_trans(opt) == 0)
511 return 0;
512 switch(opt) {
513 case 'c': config->flags |= PM_TRANS_FLAG_CASCADE; break;
514 case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
515 case 's':
516 if(config->flags & PM_TRANS_FLAG_RECURSE) {
517 config->flags |= PM_TRANS_FLAG_RECURSEALL;
518 } else {
519 config->flags |= PM_TRANS_FLAG_RECURSE;
521 break;
522 case 'u': config->flags |= PM_TRANS_FLAG_UNNEEDED; break;
523 default: return 1;
525 return 0;
528 /* options common to -S -U */
529 static int parsearg_upgrade(int opt)
531 if(parsearg_trans(opt) == 0)
532 return 0;
533 switch(opt) {
534 case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
535 case OP_ASDEPS: config->flags |= PM_TRANS_FLAG_ALLDEPS; break;
536 case OP_ASEXPLICIT: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break;
537 case OP_IGNORE:
538 parsearg_util_addlist(alpm_option_add_ignorepkg);
539 break;
540 case OP_IGNOREGROUP:
541 parsearg_util_addlist(alpm_option_add_ignoregrp);
542 break;
543 default: return 1;
545 return 0;
548 static int parsearg_sync(int opt)
550 if(parsearg_upgrade(opt) == 0)
551 return 0;
552 switch(opt) {
553 case OP_NEEDED: config->flags |= PM_TRANS_FLAG_NEEDED; break;
554 case 'c': (config->op_s_clean)++; break;
555 case 'g': (config->group)++; break;
556 case 'i': (config->op_s_info)++; break;
557 case 'l': config->op_q_list = 1; break;
558 case 'q': config->quiet = 1; break;
559 case 's': config->op_s_search = 1; break;
560 case 'u': (config->op_s_upgrade)++; break;
561 case 'w':
562 config->op_s_downloadonly = 1;
563 config->flags |= PM_TRANS_FLAG_DOWNLOADONLY;
564 config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
565 break;
566 case 'y': (config->op_s_sync)++; break;
567 default: return 1;
569 return 0;
572 /** Parse command-line arguments for each operation.
573 * @param argc argc
574 * @param argv argv
575 * @return 0 on success, 1 on error
577 static int parseargs(int argc, char *argv[])
579 int opt;
580 int option_index = 0;
581 int result;
582 const char *optstring = "DQRSTUVb:cdefghiklmnopqr:stuvwy";
583 static struct option opts[] =
585 {"database", no_argument, 0, 'D'},
586 {"query", no_argument, 0, 'Q'},
587 {"remove", no_argument, 0, 'R'},
588 {"sync", no_argument, 0, 'S'},
589 {"deptest", no_argument, 0, 'T'}, /* used by makepkg */
590 {"upgrade", no_argument, 0, 'U'},
591 {"version", no_argument, 0, 'V'},
592 {"dbpath", required_argument, 0, 'b'},
593 {"cascade", no_argument, 0, 'c'},
594 {"changelog", no_argument, 0, 'c'},
595 {"clean", no_argument, 0, 'c'},
596 {"nodeps", no_argument, 0, 'd'},
597 {"deps", no_argument, 0, 'd'},
598 {"explicit", no_argument, 0, 'e'},
599 {"force", no_argument, 0, 'f'},
600 {"groups", no_argument, 0, 'g'},
601 {"help", no_argument, 0, 'h'},
602 {"info", no_argument, 0, 'i'},
603 {"dbonly", no_argument, 0, 'k'},
604 {"check", no_argument, 0, 'k'},
605 {"list", no_argument, 0, 'l'},
606 {"foreign", no_argument, 0, 'm'},
607 {"nosave", no_argument, 0, 'n'},
608 {"owns", no_argument, 0, 'o'},
609 {"file", no_argument, 0, 'p'},
610 {"print", no_argument, 0, 'p'},
611 {"quiet", no_argument, 0, 'q'},
612 {"root", required_argument, 0, 'r'},
613 {"recursive", no_argument, 0, 's'},
614 {"search", no_argument, 0, 's'},
615 {"unrequired", no_argument, 0, 't'},
616 {"upgrades", no_argument, 0, 'u'},
617 {"sysupgrade", no_argument, 0, 'u'},
618 {"unneeded", no_argument, 0, 'u'},
619 {"verbose", no_argument, 0, 'v'},
620 {"downloadonly", no_argument, 0, 'w'},
621 {"refresh", no_argument, 0, 'y'},
622 {"noconfirm", no_argument, 0, OP_NOCONFIRM},
623 {"config", required_argument, 0, OP_CONFIG},
624 {"ignore", required_argument, 0, OP_IGNORE},
625 {"debug", optional_argument, 0, OP_DEBUG},
626 {"noprogressbar", no_argument, 0, OP_NOPROGRESSBAR},
627 {"noscriptlet", no_argument, 0, OP_NOSCRIPTLET},
628 {"ask", required_argument, 0, OP_ASK},
629 {"cachedir", required_argument, 0, OP_CACHEDIR},
630 {"asdeps", no_argument, 0, OP_ASDEPS},
631 {"logfile", required_argument, 0, OP_LOGFILE},
632 {"ignoregroup", required_argument, 0, OP_IGNOREGROUP},
633 {"needed", no_argument, 0, OP_NEEDED},
634 {"asexplicit", no_argument, 0, OP_ASEXPLICIT},
635 {"arch", required_argument, 0, OP_ARCH},
636 {"print-format", required_argument, 0, OP_PRINTFORMAT},
637 {"gpgdir", required_argument, 0, OP_GPGDIR},
638 {0, 0, 0, 0}
641 /* parse operation */
642 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
643 if(opt < 0) {
644 break;
645 } else if(opt == 0) {
646 continue;
647 } else if(opt == '?') {
648 /* unknown option, getopt printed an error */
649 return 1;
651 parsearg_op(opt, 0);
654 if(config->op == 0) {
655 pm_printf(PM_LOG_ERROR, _("only one operation may be used at a time\n"));
656 return 1;
658 if(config->help) {
659 usage(config->op, mbasename(argv[0]));
660 return 2;
662 if(config->version) {
663 version();
664 return 2;
667 /* parse all other options */
668 optind = 1;
669 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
670 if(opt < 0) {
671 break;
672 } else if(opt == 0) {
673 continue;
674 } else if(opt == '?') {
675 /* this should have failed during first pass already */
676 return 1;
677 } else if(parsearg_op(opt, 1) == 0) {
678 /* opt is an operation */
679 continue;
682 switch(config->op) {
683 case PM_OP_DATABASE:
684 result = parsearg_database(opt);
685 break;
686 case PM_OP_QUERY:
687 result = parsearg_query(opt);
688 break;
689 case PM_OP_REMOVE:
690 result = parsearg_remove(opt);
691 break;
692 case PM_OP_SYNC:
693 result = parsearg_sync(opt);
694 break;
695 case PM_OP_UPGRADE:
696 result = parsearg_upgrade(opt);
697 break;
698 case PM_OP_DEPTEST:
699 default:
700 result = 1;
701 break;
703 if(result == 0) {
704 continue;
707 /* fall back to global options */
708 result = parsearg_global(opt);
709 if(result != 0) {
710 /* global option parsing failed, abort */
711 pm_printf(PM_LOG_ERROR, _("invalid option\n"));
712 return result;
716 while(optind < argc) {
717 /* add the target to our target array */
718 pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
719 optind++;
722 return 0;
725 /** print commandline to logfile
727 static void cl_to_log(int argc, char* argv[])
729 size_t size = 0;
730 int i;
731 for(i = 0; i<argc; i++) {
732 size += strlen(argv[i]) + 1;
734 char *cl_text = malloc(size);
735 if(!cl_text)
736 return;
737 char *p = cl_text;
738 for(i = 0; i<argc-1; i++) {
739 strcpy(p, argv[i]);
740 p += strlen(argv[i]);
741 *p++ = ' ';
743 strcpy(p, argv[i]);
744 alpm_logaction("Running '%s'\n", cl_text);
745 free(cl_text);
748 /** Main function.
749 * @param argc argc
750 * @param argv argv
751 * @return A return code indicating success, failure, etc.
753 int main(int argc, char *argv[])
755 int ret = 0;
756 struct sigaction new_action, old_action;
757 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
758 /* geteuid undefined in CYGWIN */
759 uid_t myuid = geteuid();
760 #endif
762 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
763 /*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
764 mtrace();
765 #endif
767 /* Set signal handlers */
768 /* Set up the structure to specify the new action. */
769 new_action.sa_handler = handler;
770 sigemptyset(&new_action.sa_mask);
771 new_action.sa_flags = 0;
773 sigaction(SIGINT, NULL, &old_action);
774 if(old_action.sa_handler != SIG_IGN) {
775 sigaction(SIGINT, &new_action, NULL);
777 sigaction(SIGTERM, NULL, &old_action);
778 if(old_action.sa_handler != SIG_IGN) {
779 sigaction(SIGTERM, &new_action, NULL);
781 sigaction(SIGSEGV, NULL, &old_action);
782 if(old_action.sa_handler != SIG_IGN) {
783 sigaction(SIGSEGV, &new_action, NULL);
786 /* i18n init */
787 #if defined(ENABLE_NLS)
788 localize();
789 #endif
791 /* set user agent for downloading */
792 setuseragent();
794 /* init config data */
795 config = config_new();
797 /* disable progressbar if the output is redirected */
798 if(!isatty(1)) {
799 config->noprogressbar = 1;
802 /* initialize library */
803 if(alpm_initialize() == -1) {
804 pm_printf(PM_LOG_ERROR, _("failed to initialize alpm library (%s)\n"),
805 alpm_strerrorlast());
806 cleanup(EXIT_FAILURE);
809 /* Setup logging as soon as possible, to print out maximum debugging info */
810 alpm_option_set_logcb(cb_log);
811 alpm_option_set_dlcb(cb_dl_progress);
812 /* define paths to reasonable defaults */
813 alpm_option_set_root(ROOTDIR);
814 alpm_option_set_dbpath(DBPATH);
815 alpm_option_set_signaturedir(GPGDIR);
816 alpm_option_set_logfile(LOGFILE);
818 /* Priority of options:
819 * 1. command line
820 * 2. config file
821 * 3. compiled-in defaults
822 * However, we have to parse the command line first because a config file
823 * location can be specified here, so we need to make sure we prefer these
824 * options over the config file coming second.
827 /* parse the command line */
828 ret = parseargs(argc, argv);
829 if(ret != 0) {
830 cleanup(ret);
833 /* we support reading targets from stdin if a cmdline parameter is '-' */
834 if(!isatty(fileno(stdin)) && alpm_list_find_str(pm_targets, "-")) {
835 char line[PATH_MAX];
836 int i = 0;
838 /* remove the '-' from the list */
839 pm_targets = alpm_list_remove_str(pm_targets, "-", NULL);
841 while(i < PATH_MAX && (line[i] = (char)fgetc(stdin)) != EOF) {
842 if(isspace((unsigned char)line[i])) {
843 /* avoid adding zero length arg when multiple spaces separate args */
844 if(i > 0) {
845 line[i] = '\0';
846 pm_targets = alpm_list_add(pm_targets, strdup(line));
847 i = 0;
849 } else {
850 i++;
853 /* check for buffer overflow */
854 if(i >= PATH_MAX) {
855 pm_printf(PM_LOG_ERROR, _("buffer overflow detected in arg parsing\n"));
856 cleanup(EXIT_FAILURE);
859 /* end of stream -- check for data still in line buffer */
860 if(i > 0) {
861 line[i] = '\0';
862 pm_targets = alpm_list_add(pm_targets, strdup(line));
864 if(!freopen(ctermid(NULL), "r", stdin)) {
865 pm_printf(PM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
866 strerror(errno));
870 /* parse the config file */
871 ret = parseconfig(config->configfile);
872 if(ret != 0) {
873 cleanup(ret);
876 /* set TotalDownload callback if option enabled */
877 if(config->totaldownload) {
878 alpm_option_set_totaldlcb(cb_dl_total);
881 /* noask is meant to be non-interactive */
882 if(config->noask) {
883 config->noconfirm = 1;
886 /* set up the print operations */
887 if(config->print) {
888 config->noconfirm = 1;
889 config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
890 config->flags |= PM_TRANS_FLAG_NOLOCK;
891 /* Display only errors */
892 config->logmask &= ~PM_LOG_WARNING;
895 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
896 /* check if we have sufficient permission for the requested operation */
897 if(myuid > 0 && needs_root()) {
898 pm_printf(PM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
899 cleanup(EXIT_FAILURE);
901 #endif
903 if(config->verbose > 0) {
904 alpm_list_t *i;
905 printf("Root : %s\n", alpm_option_get_root());
906 printf("Conf File : %s\n", config->configfile);
907 printf("DB Path : %s\n", alpm_option_get_dbpath());
908 printf("Cache Dirs: ");
909 for(i = alpm_option_get_cachedirs(); i; i = alpm_list_next(i)) {
910 printf("%s ", (char *)alpm_list_getdata(i));
912 printf("\n");
913 printf("Lock File : %s\n", alpm_option_get_lockfile());
914 printf("Log File : %s\n", alpm_option_get_logfile());
915 list_display("Targets :", pm_targets);
918 /* Log commandline */
919 if(needs_root()) {
920 cl_to_log(argc, argv);
923 /* start the requested operation */
924 switch(config->op) {
925 case PM_OP_DATABASE:
926 ret = pacman_database(pm_targets);
927 break;
928 case PM_OP_REMOVE:
929 ret = pacman_remove(pm_targets);
930 break;
931 case PM_OP_UPGRADE:
932 ret = pacman_upgrade(pm_targets);
933 break;
934 case PM_OP_QUERY:
935 ret = pacman_query(pm_targets);
936 break;
937 case PM_OP_SYNC:
938 ret = pacman_sync(pm_targets);
939 break;
940 case PM_OP_DEPTEST:
941 ret = pacman_deptest(pm_targets);
942 break;
943 default:
944 pm_printf(PM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
945 ret = EXIT_FAILURE;
948 cleanup(ret);
949 /* not reached */
950 return EXIT_SUCCESS;
953 /* vim: set ts=2 sw=2 noet: */