Fix bash completion for *.pkg.tar
[pacman-ng.git] / src / pacman / pacman.c
blob326664dd7794d2826cb87962d6487e5adb36d38b
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 "conf.h"
55 /* list of targets specified on command line */
56 static alpm_list_t *pm_targets;
58 /* Used to sort the options in --help */
59 static int options_cmp(const void *p1, const void *p2)
61 const char *s1 = p1;
62 const char *s2 = p2;
64 if(s1 == s2) return 0;
65 if(!s1) return -1;
66 if(!s2) return 1;
67 /* First skip all spaces in both strings */
68 while(isspace((unsigned char)*s1)) {
69 s1++;
71 while(isspace((unsigned char)*s2)) {
72 s2++;
74 /* If we compare a long option (--abcd) and a short one (-a),
75 * the short one always wins */
76 if(*s1 == '-' && *s2 == '-') {
77 s1++;
78 s2++;
79 if(*s1 == '-' && *s2 == '-') {
80 /* two long -> strcmp */
81 s1++;
82 s2++;
83 } else if(*s2 == '-') {
84 /* s1 short, s2 long */
85 return -1;
86 } else if(*s1 == '-') {
87 /* s1 long, s2 short */
88 return 1;
90 /* two short -> strcmp */
93 return strcmp(s1, s2);
96 /** Display usage/syntax for the specified operation.
97 * @param op the operation code requested
98 * @param myname basename(argv[0])
100 static void usage(int op, const char * const myname)
102 #define addlist(s) (list = alpm_list_add(list, s))
103 alpm_list_t *list = NULL, *i;
104 /* prefetch some strings for usage below, which moves a lot of calls
105 * out of gettext. */
106 char const * const str_opt = _("options");
107 char const * const str_file = _("file(s)");
108 char const * const str_pkg = _("package(s)");
109 char const * const str_usg = _("usage");
110 char const * const str_opr = _("operation");
112 /* please limit your strings to 80 characters in width */
113 if(op == PM_OP_MAIN) {
114 printf("%s: %s <%s> [...]\n", str_usg, myname, str_opr);
115 printf(_("operations:\n"));
116 printf(" %s {-h --help}\n", myname);
117 printf(" %s {-V --version}\n", myname);
118 printf(" %s {-D --database} <%s> <%s>\n", myname, str_opt, str_pkg);
119 printf(" %s {-Q --query} [%s] [%s]\n", myname, str_opt, str_pkg);
120 printf(" %s {-R --remove} [%s] <%s>\n", myname, str_opt, str_pkg);
121 printf(" %s {-S --sync} [%s] [%s]\n", myname, str_opt, str_pkg);
122 printf(" %s {-T --deptest} [%s] [%s]\n", myname, str_opt, str_pkg);
123 printf(" %s {-U --upgrade} [%s] <%s>\n", myname, str_opt, str_file);
124 printf(_("\nuse '%s {-h --help}' with an operation for available options\n"),
125 myname);
126 } else {
127 if(op == PM_OP_REMOVE) {
128 printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg, myname, str_opt, str_pkg);
129 printf("%s:\n", str_opt);
130 addlist(_(" -c, --cascade remove packages and all packages that depend on them\n"));
131 addlist(_(" -n, --nosave remove configuration files\n"));
132 addlist(_(" -s, --recursive remove unnecessary dependencies\n"
133 " (-ss includes explicitly installed dependencies)\n"));
134 addlist(_(" -u, --unneeded remove unneeded packages\n"));
135 } else if(op == PM_OP_UPGRADE) {
136 printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg, myname, str_opt, str_file);
137 addlist(_(" --needed do not reinstall up to date packages\n"));
138 addlist(_(" --recursive reinstall all dependencies of target packages\n"));
139 printf("%s:\n", str_opt);
140 } else if(op == PM_OP_QUERY) {
141 printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
142 printf("%s:\n", str_opt);
143 addlist(_(" -c, --changelog view the changelog of a package\n"));
144 addlist(_(" -d, --deps list packages installed as dependencies [filter]\n"));
145 addlist(_(" -e, --explicit list packages explicitly installed [filter]\n"));
146 addlist(_(" -g, --groups view all members of a package group\n"));
147 addlist(_(" -i, --info view package information (-ii for backup files)\n"));
148 addlist(_(" -k, --check check that the files owned by the package(s) are present\n"));
149 addlist(_(" -l, --list list the contents of the queried package\n"));
150 addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
151 addlist(_(" -o, --owns <file> query the package that owns <file>\n"));
152 addlist(_(" -p, --file <package> query a package file instead of the database\n"));
153 addlist(_(" -q, --quiet show less information for query and search\n"));
154 addlist(_(" -s, --search <regex> search locally-installed packages for matching strings\n"));
155 addlist(_(" -t, --unrequired list packages not required by any package [filter]\n"));
156 addlist(_(" -u, --upgrades list outdated packages [filter]\n"));
157 } else if(op == PM_OP_SYNC) {
158 printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
159 printf("%s:\n", str_opt);
160 addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n"));
161 addlist(_(" -g, --groups view all members of a package group\n"));
162 addlist(_(" -i, --info view package information\n"));
163 addlist(_(" -l, --list <repo> view a list of packages in a repo\n"));
164 addlist(_(" -q, --quiet show less information for query and search\n"));
165 addlist(_(" -s, --search <regex> search remote repositories for matching strings\n"));
166 addlist(_(" -u, --sysupgrade upgrade installed packages (-uu allows downgrade)\n"));
167 addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
168 addlist(_(" -y, --refresh download fresh package databases from the server\n"));
169 addlist(_(" --needed do not reinstall up to date packages\n"));
170 addlist(_(" --recursive reinstall all dependencies of target packages\n"));
171 } else if(op == PM_OP_DATABASE) {
172 printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg);
173 printf("%s:\n", str_opt);
174 addlist(_(" --asdeps mark packages as non-explicitly installed\n"));
175 addlist(_(" --asexplicit mark packages as explicitly installed\n"));
176 } else if(op == PM_OP_DEPTEST) {
177 printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
178 printf("%s:\n", str_opt);
180 switch(op) {
181 case PM_OP_SYNC:
182 case PM_OP_UPGRADE:
183 addlist(_(" -f, --force force install, overwrite conflicting files\n"));
184 addlist(_(" --asdeps install packages as non-explicitly installed\n"));
185 addlist(_(" --asexplicit install packages as explicitly installed\n"));
186 addlist(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
187 addlist(_(" --ignoregroup <grp>\n"
188 " ignore a group upgrade (can be used more than once)\n"));
189 /* pass through */
190 case PM_OP_REMOVE:
191 addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n"));
192 addlist(_(" --dbonly only modify database entries, not package files\n"));
193 addlist(_(" --noprogressbar do not show a progress bar when downloading files\n"));
194 addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
195 addlist(_(" -p, --print print the targets instead of performing the operation\n"));
196 addlist(_(" --print-format <string>\n"
197 " specify how the targets should be printed\n"));
198 break;
201 addlist(_(" -b, --dbpath <path> set an alternate database location\n"));
202 addlist(_(" -r, --root <path> set an alternate installation root\n"));
203 addlist(_(" -v, --verbose be verbose\n"));
204 addlist(_(" --arch <arch> set an alternate architecture\n"));
205 addlist(_(" --cachedir <dir> set an alternate package cache location\n"));
206 addlist(_(" --config <path> set an alternate configuration file\n"));
207 addlist(_(" --debug display debug messages\n"));
208 addlist(_(" --gpgdir <path> set an alternate home directory for GnuPG\n"));
209 addlist(_(" --logfile <path> set an alternate log file\n"));
210 addlist(_(" --noconfirm do not ask for any confirmation\n"));
212 list = alpm_list_msort(list, alpm_list_count(list), options_cmp);
213 for (i = list; i; i = alpm_list_next(i)) {
214 printf("%s", (char *)alpm_list_getdata(i));
216 alpm_list_free(list);
217 #undef addlist
220 /** Output pacman version and copyright.
222 static void version(void)
224 printf("\n");
225 printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version());
226 printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2011 Pacman Development Team\n");
227 printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n");
228 printf(" '--'\n");
229 printf(_(" This program may be freely redistributed under\n"
230 " the terms of the GNU General Public License.\n"));
231 printf("\n");
234 /** Sets up gettext localization. Safe to call multiple times.
236 /* Inspired by the monotone function localize_monotone. */
237 #if defined(ENABLE_NLS)
238 static void localize(void)
240 static int init = 0;
241 if(!init) {
242 setlocale(LC_ALL, "");
243 bindtextdomain(PACKAGE, LOCALEDIR);
244 textdomain(PACKAGE);
245 init = 1;
248 #endif
250 /** Set user agent environment variable.
252 static void setuseragent(void)
254 char agent[101];
255 struct utsname un;
257 uname(&un);
258 snprintf(agent, 100, "pacman/%s (%s %s) libalpm/%s",
259 PACKAGE_VERSION, un.sysname, un.machine, alpm_version());
260 setenv("HTTP_USER_AGENT", agent, 0);
263 /** Free the resources.
265 * @param ret the return value
267 static void cleanup(int ret) {
268 /* free alpm library resources */
269 if(config->handle && alpm_release(config->handle) == -1) {
270 pm_printf(ALPM_LOG_ERROR, "error releasing alpm library\n");
273 /* free memory */
274 FREELIST(pm_targets);
275 if(config) {
276 config_free(config);
277 config = NULL;
280 exit(ret);
283 /** Write function that correctly handles EINTR.
285 static ssize_t xwrite(int fd, const void *buf, size_t count)
287 ssize_t ret;
288 do {
289 ret = write(fd, buf, count);
290 } while(ret == -1 && errno == EINTR);
291 return ret;
294 /** Catches thrown signals. Performs necessary cleanup to ensure database is
295 * in a consistant state.
296 * @param signum the thrown signal
298 static void handler(int signum)
300 int out = fileno(stdout);
301 int err = fileno(stderr);
302 const char *msg;
303 if(signum == SIGSEGV) {
304 msg = "\nerror: segmentation fault\n"
305 "Please submit a full bug report with --debug if appropriate.\n";
306 xwrite(err, msg, strlen(msg));
307 exit(signum);
308 } else if(signum == SIGINT || signum == SIGHUP) {
309 if(signum == SIGINT) {
310 msg = "\nInterrupt signal received\n";
311 } else {
312 msg = "\nHangup signal received\n";
314 xwrite(err, msg, strlen(msg));
315 if(alpm_trans_interrupt(config->handle) == 0) {
316 /* a transaction is being interrupted, don't exit pacman yet. */
317 return;
320 /* SIGINT: no commiting transaction, release it now and then exit pacman
321 * SIGHUP, SIGTERM: release no matter what */
322 alpm_trans_release(config->handle);
323 /* output a newline to be sure we clear any line we may be on */
324 xwrite(out, "\n", 1);
325 cleanup(128 + signum);
328 #define check_optarg() if(!optarg) { return 1; }
330 static int parsearg_util_addlist(alpm_list_t **list)
332 alpm_list_t *split, *item;
334 check_optarg();
335 split = strsplit(optarg, ',');
336 for(item = split; item; item = alpm_list_next(item)) {
337 *list = alpm_list_add(*list, item->data);
339 alpm_list_free(split);
340 return 0;
343 /** Helper function for parsing operation from command-line arguments.
344 * @param opt Keycode returned by getopt_long
345 * @param dryrun If nonzero, application state is NOT changed
346 * @return 0 if opt was handled, 1 if it was not handled
348 static int parsearg_op(int opt, int dryrun)
350 switch(opt) {
351 /* operations */
352 case 'D':
353 if(dryrun) break;
354 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DATABASE); break;
355 case 'Q':
356 if(dryrun) break;
357 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
358 case 'R':
359 if(dryrun) break;
360 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
361 case 'S':
362 if(dryrun) break;
363 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
364 case 'T':
365 if(dryrun) break;
366 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
367 case 'U':
368 if(dryrun) break;
369 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
370 case 'V':
371 if(dryrun) break;
372 config->version = 1; break;
373 case 'h':
374 if(dryrun) break;
375 config->help = 1; break;
376 default:
377 return 1;
379 return 0;
382 /** Helper functions for parsing command-line arguments.
383 * @param opt Keycode returned by getopt_long
384 * @return 0 on success, 1 on failure
386 static int parsearg_global(int opt)
388 switch(opt) {
389 case OP_ARCH:
390 check_optarg();
391 config_set_arch(optarg);
392 break;
393 case OP_ASK:
394 check_optarg();
395 config->noask = 1;
396 config->ask = (unsigned int)atoi(optarg);
397 break;
398 case OP_CACHEDIR:
399 check_optarg();
400 config->cachedirs = alpm_list_add(config->cachedirs, strdup(optarg));
401 break;
402 case OP_CONFIG:
403 check_optarg();
404 if(config->configfile) {
405 free(config->configfile);
407 config->configfile = strndup(optarg, PATH_MAX);
408 break;
409 case OP_DEBUG:
410 /* debug levels are made more 'human readable' than using a raw logmask
411 * here, error and warning are set in config_new, though perhaps a
412 * --quiet option will remove these later */
413 if(optarg) {
414 unsigned short debug = (unsigned short)atoi(optarg);
415 switch(debug) {
416 case 2:
417 config->logmask |= ALPM_LOG_FUNCTION; /* fall through */
418 case 1:
419 config->logmask |= ALPM_LOG_DEBUG;
420 break;
421 default:
422 pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
423 optarg);
424 return 1;
426 } else {
427 config->logmask |= ALPM_LOG_DEBUG;
429 /* progress bars get wonky with debug on, shut them off */
430 config->noprogressbar = 1;
431 break;
432 case OP_GPGDIR:
433 config->gpgdir = strdup(optarg);
434 break;
435 case OP_LOGFILE:
436 check_optarg();
437 config->logfile = strndup(optarg, PATH_MAX);
438 break;
439 case OP_NOCONFIRM: config->noconfirm = 1; break;
440 case 'b':
441 check_optarg();
442 config->dbpath = strdup(optarg);
443 break;
444 case 'r': check_optarg(); config->rootdir = strdup(optarg); break;
445 case 'v': (config->verbose)++; break;
446 default: return 1;
448 return 0;
451 static int parsearg_database(int opt)
453 switch(opt) {
454 case OP_ASDEPS: config->flags |= ALPM_TRANS_FLAG_ALLDEPS; break;
455 case OP_ASEXPLICIT: config->flags |= ALPM_TRANS_FLAG_ALLEXPLICIT; break;
456 default: return 1;
458 return 0;
461 static int parsearg_query(int opt)
463 switch(opt) {
464 case 'c': config->op_q_changelog = 1; break;
465 case 'd': config->op_q_deps = 1; break;
466 case 'e': config->op_q_explicit = 1; break;
467 case 'g': (config->group)++; break;
468 case 'i': (config->op_q_info)++; break;
469 case 'k': config->op_q_check = 1; break;
470 case 'l': config->op_q_list = 1; break;
471 case 'm': config->op_q_foreign = 1; break;
472 case 'o': config->op_q_owns = 1; break;
473 case 'p': config->op_q_isfile = 1; break;
474 case 'q': config->quiet = 1; break;
475 case 's': config->op_q_search = 1; break;
476 case 't': config->op_q_unrequired = 1; break;
477 case 'u': config->op_q_upgrade = 1; break;
478 default: return 1;
480 return 0;
483 /* options common to -S -R -U */
484 static int parsearg_trans(int opt)
486 switch(opt) {
487 case 'd':
488 if(config->flags & ALPM_TRANS_FLAG_NODEPVERSION) {
489 config->flags |= ALPM_TRANS_FLAG_NODEPS;
490 } else {
491 config->flags |= ALPM_TRANS_FLAG_NODEPVERSION;
493 break;
494 case OP_DBONLY: config->flags |= ALPM_TRANS_FLAG_DBONLY; break;
495 case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
496 case OP_NOSCRIPTLET: config->flags |= ALPM_TRANS_FLAG_NOSCRIPTLET; break;
497 case 'p': config->print = 1; break;
498 case OP_PRINTFORMAT:
499 check_optarg();
500 config->print_format = strdup(optarg);
501 break;
502 default: return 1;
504 return 0;
507 static int parsearg_remove(int opt)
509 if(parsearg_trans(opt) == 0)
510 return 0;
511 switch(opt) {
512 case 'c': config->flags |= ALPM_TRANS_FLAG_CASCADE; break;
513 case 'n': config->flags |= ALPM_TRANS_FLAG_NOSAVE; break;
514 case 's':
515 case OP_RECURSIVE:
516 /* 's' is the legacy flag here, but since recursive is used in -S without
517 * a shortopt, we need to do funky tricks */
518 if(config->flags & ALPM_TRANS_FLAG_RECURSE) {
519 config->flags |= ALPM_TRANS_FLAG_RECURSEALL;
520 } else {
521 config->flags |= ALPM_TRANS_FLAG_RECURSE;
523 break;
524 case 'u': config->flags |= ALPM_TRANS_FLAG_UNNEEDED; break;
525 default: return 1;
527 return 0;
530 /* options common to -S -U */
531 static int parsearg_upgrade(int opt)
533 if(parsearg_trans(opt) == 0)
534 return 0;
535 switch(opt) {
536 case 'f': config->flags |= ALPM_TRANS_FLAG_FORCE; break;
537 case OP_ASDEPS: config->flags |= ALPM_TRANS_FLAG_ALLDEPS; break;
538 case OP_ASEXPLICIT: config->flags |= ALPM_TRANS_FLAG_ALLEXPLICIT; break;
539 case OP_NEEDED: config->flags |= ALPM_TRANS_FLAG_NEEDED; break;
540 case OP_RECURSIVE: config->flags |= ALPM_TRANS_FLAG_RECURSE; break;
541 case OP_IGNORE:
542 parsearg_util_addlist(&(config->ignorepkg));
543 break;
544 case OP_IGNOREGROUP:
545 parsearg_util_addlist(&(config->ignoregrp));
546 break;
547 default: return 1;
549 return 0;
552 static int parsearg_sync(int opt)
554 if(parsearg_upgrade(opt) == 0)
555 return 0;
556 switch(opt) {
557 case 'c': (config->op_s_clean)++; break;
558 case 'g': (config->group)++; break;
559 case 'i': (config->op_s_info)++; break;
560 case 'l': config->op_q_list = 1; break;
561 case 'q': config->quiet = 1; break;
562 case 's': config->op_s_search = 1; break;
563 case 'u': (config->op_s_upgrade)++; break;
564 case 'w':
565 config->op_s_downloadonly = 1;
566 config->flags |= ALPM_TRANS_FLAG_DOWNLOADONLY;
567 config->flags |= ALPM_TRANS_FLAG_NOCONFLICTS;
568 break;
569 case 'y': (config->op_s_sync)++; break;
570 default: return 1;
572 return 0;
575 /** Parse command-line arguments for each operation.
576 * @param argc argc
577 * @param argv argv
578 * @return 0 on success, 1 on error
580 static int parseargs(int argc, char *argv[])
582 int opt;
583 int option_index = 0;
584 int result;
585 const char *optstring = "DQRSTUVb:cdefghiklmnopqr:stuvwy";
586 static const struct option opts[] =
588 {"database", no_argument, 0, 'D'},
589 {"query", no_argument, 0, 'Q'},
590 {"remove", no_argument, 0, 'R'},
591 {"sync", no_argument, 0, 'S'},
592 {"deptest", no_argument, 0, 'T'}, /* used by makepkg */
593 {"upgrade", no_argument, 0, 'U'},
594 {"version", no_argument, 0, 'V'},
595 {"dbpath", required_argument, 0, 'b'},
596 {"cascade", no_argument, 0, 'c'},
597 {"changelog", no_argument, 0, 'c'},
598 {"clean", no_argument, 0, 'c'},
599 {"nodeps", no_argument, 0, 'd'},
600 {"deps", no_argument, 0, 'd'},
601 {"explicit", no_argument, 0, 'e'},
602 {"force", no_argument, 0, 'f'},
603 {"groups", no_argument, 0, 'g'},
604 {"help", no_argument, 0, 'h'},
605 {"info", no_argument, 0, 'i'},
606 {"check", no_argument, 0, 'k'},
607 {"list", no_argument, 0, 'l'},
608 {"foreign", no_argument, 0, 'm'},
609 {"nosave", no_argument, 0, 'n'},
610 {"owns", no_argument, 0, 'o'},
611 {"file", no_argument, 0, 'p'},
612 {"print", no_argument, 0, 'p'},
613 {"quiet", no_argument, 0, 'q'},
614 {"root", required_argument, 0, 'r'},
615 {"search", no_argument, 0, 's'},
616 {"unrequired", no_argument, 0, 't'},
617 {"upgrades", no_argument, 0, 'u'},
618 {"sysupgrade", no_argument, 0, 'u'},
619 {"unneeded", no_argument, 0, 'u'},
620 {"verbose", no_argument, 0, 'v'},
621 {"downloadonly", no_argument, 0, 'w'},
622 {"refresh", no_argument, 0, 'y'},
624 {"noconfirm", no_argument, 0, OP_NOCONFIRM},
625 {"config", required_argument, 0, OP_CONFIG},
626 {"ignore", required_argument, 0, OP_IGNORE},
627 {"debug", optional_argument, 0, OP_DEBUG},
628 {"noprogressbar", no_argument, 0, OP_NOPROGRESSBAR},
629 {"noscriptlet", no_argument, 0, OP_NOSCRIPTLET},
630 {"ask", required_argument, 0, OP_ASK},
631 {"cachedir", required_argument, 0, OP_CACHEDIR},
632 {"asdeps", no_argument, 0, OP_ASDEPS},
633 {"logfile", required_argument, 0, OP_LOGFILE},
634 {"ignoregroup", required_argument, 0, OP_IGNOREGROUP},
635 {"needed", no_argument, 0, OP_NEEDED},
636 {"asexplicit", no_argument, 0, OP_ASEXPLICIT},
637 {"arch", required_argument, 0, OP_ARCH},
638 {"print-format", required_argument, 0, OP_PRINTFORMAT},
639 {"gpgdir", required_argument, 0, OP_GPGDIR},
640 {"recursive", no_argument, 0, OP_RECURSIVE},
641 {"dbonly", no_argument, 0, OP_DBONLY},
642 {0, 0, 0, 0}
645 /* parse operation */
646 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
647 if(opt < 0) {
648 break;
649 } else if(opt == 0) {
650 continue;
651 } else if(opt == '?') {
652 /* unknown option, getopt printed an error */
653 return 1;
655 parsearg_op(opt, 0);
658 if(config->op == 0) {
659 pm_printf(ALPM_LOG_ERROR, _("only one operation may be used at a time\n"));
660 return 1;
662 if(config->help) {
663 usage(config->op, mbasename(argv[0]));
664 return 2;
666 if(config->version) {
667 version();
668 return 2;
671 /* parse all other options */
672 optind = 1;
673 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
674 if(opt < 0) {
675 break;
676 } else if(opt == 0) {
677 continue;
678 } else if(opt == '?') {
679 /* this should have failed during first pass already */
680 return 1;
681 } else if(parsearg_op(opt, 1) == 0) {
682 /* opt is an operation */
683 continue;
686 switch(config->op) {
687 case PM_OP_DATABASE:
688 result = parsearg_database(opt);
689 break;
690 case PM_OP_QUERY:
691 result = parsearg_query(opt);
692 break;
693 case PM_OP_REMOVE:
694 result = parsearg_remove(opt);
695 break;
696 case PM_OP_SYNC:
697 result = parsearg_sync(opt);
698 break;
699 case PM_OP_UPGRADE:
700 result = parsearg_upgrade(opt);
701 break;
702 case PM_OP_DEPTEST:
703 default:
704 result = 1;
705 break;
707 if(result == 0) {
708 continue;
711 /* fall back to global options */
712 result = parsearg_global(opt);
713 if(result != 0) {
714 /* global option parsing failed, abort */
715 pm_printf(ALPM_LOG_ERROR, _("invalid option\n"));
716 return result;
720 while(optind < argc) {
721 /* add the target to our target array */
722 pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
723 optind++;
726 return 0;
729 /** print commandline to logfile
731 static void cl_to_log(int argc, char* argv[])
733 size_t size = 0;
734 int i;
735 for(i = 0; i < argc; i++) {
736 size += strlen(argv[i]) + 1;
738 if(!size) {
739 return;
741 char *cl_text = malloc(size);
742 if(!cl_text) {
743 return;
745 char *p = cl_text;
746 for(i = 0; i < argc - 1; i++) {
747 strcpy(p, argv[i]);
748 p += strlen(argv[i]);
749 *p++ = ' ';
751 strcpy(p, argv[i]);
752 alpm_logaction(config->handle, "Running '%s'\n", cl_text);
753 free(cl_text);
756 /** Main function.
757 * @param argc argc
758 * @param argv argv
759 * @return A return code indicating success, failure, etc.
761 int main(int argc, char *argv[])
763 int ret = 0;
764 size_t i;
765 struct sigaction new_action, old_action;
766 const int signals[] = { SIGHUP, SIGINT, SIGTERM, SIGSEGV };
767 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
768 /* geteuid undefined in CYGWIN */
769 uid_t myuid = geteuid();
770 #endif
772 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
773 /*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
774 mtrace();
775 #endif
777 /* Set signal handlers */
778 /* Set up the structure to specify the new action. */
779 new_action.sa_handler = handler;
780 sigemptyset(&new_action.sa_mask);
781 new_action.sa_flags = 0;
783 /* assign our handler to any signals we care about */
784 for(i = 0; i < sizeof(signals) / sizeof(signals[0]); i++) {
785 int signal = signals[i];
786 sigaction(signal, NULL, &old_action);
787 if(old_action.sa_handler != SIG_IGN) {
788 sigaction(signal, &new_action, NULL);
792 /* i18n init */
793 #if defined(ENABLE_NLS)
794 localize();
795 #endif
797 /* set user agent for downloading */
798 setuseragent();
800 /* init config data */
801 config = config_new();
803 /* disable progressbar if the output is redirected */
804 if(!isatty(1)) {
805 config->noprogressbar = 1;
808 /* Priority of options:
809 * 1. command line
810 * 2. config file
811 * 3. compiled-in defaults
812 * However, we have to parse the command line first because a config file
813 * location can be specified here, so we need to make sure we prefer these
814 * options over the config file coming second.
817 /* parse the command line */
818 ret = parseargs(argc, argv);
819 if(ret != 0) {
820 cleanup(ret);
823 /* we support reading targets from stdin if a cmdline parameter is '-' */
824 if(!isatty(fileno(stdin)) && alpm_list_find_str(pm_targets, "-")) {
825 size_t current_size = PATH_MAX, i = 0;
826 char *line = malloc(current_size);
828 /* remove the '-' from the list */
829 pm_targets = alpm_list_remove_str(pm_targets, "-", NULL);
831 while((line[i] = (char)fgetc(stdin)) != EOF) {
832 if(isspace((unsigned char)line[i])) {
833 /* avoid adding zero length arg when multiple spaces separate args */
834 if(i > 0) {
835 line[i] = '\0';
836 pm_targets = alpm_list_add(pm_targets, strdup(line));
837 i = 0;
839 } else {
840 i++;
841 /* we may be at the end of our allocated buffer now */
842 if(i >= current_size) {
843 char *new = realloc(line, current_size * 2);
844 if(new) {
845 line = new;
846 current_size *= 2;
847 } else {
848 free(line);
849 line = NULL;
850 break;
855 /* check for memory exhaustion */
856 if(!line) {
857 pm_printf(ALPM_LOG_ERROR, _("memory exhausted in argument parsing\n"));
858 cleanup(EXIT_FAILURE);
861 /* end of stream -- check for data still in line buffer */
862 if(i > 0) {
863 line[i] = '\0';
864 pm_targets = alpm_list_add(pm_targets, strdup(line));
866 free(line);
867 if(!freopen(ctermid(NULL), "r", stdin)) {
868 pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
869 strerror(errno));
873 /* parse the config file */
874 ret = parseconfig(config->configfile);
875 if(ret != 0) {
876 cleanup(ret);
879 /* noask is meant to be non-interactive */
880 if(config->noask) {
881 config->noconfirm = 1;
884 /* set up the print operations */
885 if(config->print && !config->op_s_clean) {
886 config->noconfirm = 1;
887 config->flags |= ALPM_TRANS_FLAG_NOCONFLICTS;
888 config->flags |= ALPM_TRANS_FLAG_NOLOCK;
889 /* Display only errors */
890 config->logmask &= ~ALPM_LOG_WARNING;
893 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
894 /* check if we have sufficient permission for the requested operation */
895 if(myuid > 0 && needs_root()) {
896 pm_printf(ALPM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
897 cleanup(EXIT_FAILURE);
899 #endif
901 if(config->verbose > 0) {
902 alpm_list_t *i;
903 printf("Root : %s\n", alpm_option_get_root(config->handle));
904 printf("Conf File : %s\n", config->configfile);
905 printf("DB Path : %s\n", alpm_option_get_dbpath(config->handle));
906 printf("Cache Dirs: ");
907 for(i = alpm_option_get_cachedirs(config->handle); i; i = alpm_list_next(i)) {
908 printf("%s ", (char *)alpm_list_getdata(i));
910 printf("\n");
911 printf("Lock File : %s\n", alpm_option_get_lockfile(config->handle));
912 printf("Log File : %s\n", alpm_option_get_logfile(config->handle));
913 printf("GPG Dir : %s\n", alpm_option_get_gpgdir(config->handle));
914 list_display("Targets :", pm_targets);
917 /* Log commandline */
918 if(needs_root()) {
919 cl_to_log(argc, argv);
922 /* start the requested operation */
923 switch(config->op) {
924 case PM_OP_DATABASE:
925 ret = pacman_database(pm_targets);
926 break;
927 case PM_OP_REMOVE:
928 ret = pacman_remove(pm_targets);
929 break;
930 case PM_OP_UPGRADE:
931 ret = pacman_upgrade(pm_targets);
932 break;
933 case PM_OP_QUERY:
934 ret = pacman_query(pm_targets);
935 break;
936 case PM_OP_SYNC:
937 ret = pacman_sync(pm_targets);
938 break;
939 case PM_OP_DEPTEST:
940 ret = pacman_deptest(pm_targets);
941 break;
942 default:
943 pm_printf(ALPM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
944 ret = EXIT_FAILURE;
947 cleanup(ret);
948 /* not reached */
949 return EXIT_SUCCESS;
952 /* vim: set ts=2 sw=2 noet: */