pacman-key: fix quotation on several variable assignments
[pacman-ng.git] / src / pacman / pacman.c
blobe855203a6d8328b1e26077f9e8bbcd7c1c5de2de
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 printf("%s:\n", str_opt);
138 } else if(op == PM_OP_QUERY) {
139 printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
140 printf("%s:\n", str_opt);
141 addlist(_(" -c, --changelog view the changelog of a package\n"));
142 addlist(_(" -d, --deps list packages installed as dependencies [filter]\n"));
143 addlist(_(" -e, --explicit list packages explicitly installed [filter]\n"));
144 addlist(_(" -g, --groups view all members of a package group\n"));
145 addlist(_(" -i, --info view package information (-ii for backup files)\n"));
146 addlist(_(" -k, --check check that the files owned by the package(s) are present\n"));
147 addlist(_(" -l, --list list the contents of the queried package\n"));
148 addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
149 addlist(_(" -o, --owns <file> query the package that owns <file>\n"));
150 addlist(_(" -p, --file <package> query a package file instead of the database\n"));
151 addlist(_(" -q, --quiet show less information for query and search\n"));
152 addlist(_(" -s, --search <regex> search locally-installed packages for matching strings\n"));
153 addlist(_(" -t, --unrequired list packages not required by any package [filter]\n"));
154 addlist(_(" -u, --upgrades list outdated packages [filter]\n"));
155 } else if(op == PM_OP_SYNC) {
156 printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
157 printf("%s:\n", str_opt);
158 addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n"));
159 addlist(_(" -g, --groups view all members of a package group\n"));
160 addlist(_(" -i, --info view package information\n"));
161 addlist(_(" -l, --list <repo> view a list of packages in a repo\n"));
162 addlist(_(" -q, --quiet show less information for query and search\n"));
163 addlist(_(" -s, --search <regex> search remote repositories for matching strings\n"));
164 addlist(_(" -u, --sysupgrade upgrade installed packages (-uu allows downgrade)\n"));
165 addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
166 addlist(_(" -y, --refresh download fresh package databases from the server\n"));
167 addlist(_(" --needed don't reinstall up to date packages\n"));
168 } else if(op == PM_OP_DATABASE) {
169 printf("%s: %s {-D --database} <%s> <%s>\n", str_usg, myname, str_opt, str_pkg);
170 printf("%s:\n", str_opt);
171 addlist(_(" --asdeps mark packages as non-explicitly installed\n"));
172 addlist(_(" --asexplicit mark packages as explicitly installed\n"));
173 } else if(op == PM_OP_DEPTEST) {
174 printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg, myname, str_opt, str_pkg);
175 printf("%s:\n", str_opt);
177 switch(op) {
178 case PM_OP_SYNC:
179 case PM_OP_UPGRADE:
180 addlist(_(" -f, --force force install, overwrite conflicting files\n"));
181 addlist(_(" --asdeps install packages as non-explicitly installed\n"));
182 addlist(_(" --asexplicit install packages as explicitly installed\n"));
183 addlist(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
184 addlist(_(" --ignoregroup <grp>\n"
185 " ignore a group upgrade (can be used more than once)\n"));
186 /* pass through */
187 case PM_OP_REMOVE:
188 addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n"));
189 addlist(_(" -k, --dbonly only modify database entries, not package files\n"));
190 addlist(_(" --noprogressbar do not show a progress bar when downloading files\n"));
191 addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
192 addlist(_(" --print print the targets instead of performing the operation\n"));
193 addlist(_(" --print-format <string>\n"
194 " specify how the targets should be printed\n"));
195 break;
198 addlist(_(" -b, --dbpath <path> set an alternate database location\n"));
199 addlist(_(" -r, --root <path> set an alternate installation root\n"));
200 addlist(_(" -v, --verbose be verbose\n"));
201 addlist(_(" --arch <arch> set an alternate architecture\n"));
202 addlist(_(" --cachedir <dir> set an alternate package cache location\n"));
203 addlist(_(" --config <path> set an alternate configuration file\n"));
204 addlist(_(" --debug display debug messages\n"));
205 addlist(_(" --gpgdir <path> set an alternate home directory for GnuPG\n"));
206 addlist(_(" --logfile <path> set an alternate log file\n"));
207 addlist(_(" --noconfirm do not ask for any confirmation\n"));
209 list = alpm_list_msort(list, alpm_list_count(list), options_cmp);
210 for (i = list; i; i = alpm_list_next(i)) {
211 printf("%s", (char *)alpm_list_getdata(i));
213 alpm_list_free(list);
214 #undef addlist
217 /** Output pacman version and copyright.
219 static void version(void)
221 printf("\n");
222 printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION, alpm_version());
223 printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2011 Pacman Development Team\n");
224 printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n");
225 printf(" '--'\n");
226 printf(_(" This program may be freely redistributed under\n"
227 " the terms of the GNU General Public License.\n"));
228 printf("\n");
231 /** Sets up gettext localization. Safe to call multiple times.
233 /* Inspired by the monotone function localize_monotone. */
234 #if defined(ENABLE_NLS)
235 static void localize(void)
237 static int init = 0;
238 if(!init) {
239 setlocale(LC_ALL, "");
240 bindtextdomain(PACKAGE, LOCALEDIR);
241 textdomain(PACKAGE);
242 init = 1;
245 #endif
247 /** Set user agent environment variable.
249 static void setuseragent(void)
251 char agent[101];
252 struct utsname un;
254 uname(&un);
255 snprintf(agent, 100, "pacman/%s (%s %s) libalpm/%s",
256 PACKAGE_VERSION, un.sysname, un.machine, alpm_version());
257 setenv("HTTP_USER_AGENT", agent, 0);
260 /** Free the resources.
262 * @param ret the return value
264 static void cleanup(int ret) {
265 /* free alpm library resources */
266 if(config->handle && alpm_release(config->handle) == -1) {
267 pm_printf(ALPM_LOG_ERROR, "error releasing alpm library\n");
270 /* free memory */
271 FREELIST(pm_targets);
272 if(config) {
273 config_free(config);
274 config = NULL;
277 exit(ret);
280 /** Write function that correctly handles EINTR.
282 static ssize_t xwrite(int fd, const void *buf, size_t count)
284 ssize_t ret;
285 do {
286 ret = write(fd, buf, count);
287 } while(ret == -1 && errno == EINTR);
288 return ret;
291 /** Catches thrown signals. Performs necessary cleanup to ensure database is
292 * in a consistant state.
293 * @param signum the thrown signal
295 static void handler(int signum)
297 int out = fileno(stdout);
298 int err = fileno(stderr);
299 if(signum == SIGSEGV) {
300 const char *msg1 = "error: segmentation fault\n";
301 const char *msg2 = "Internal pacman error: Segmentation fault.\n"
302 "Please submit a full bug report with --debug if appropriate.\n";
303 /* write a error message to out, the rest to err */
304 xwrite(out, msg1, strlen(msg1));
305 xwrite(err, msg2, strlen(msg2));
306 exit(signum);
307 } else if(signum == SIGINT) {
308 const char *msg = "\nInterrupt signal received\n";
309 xwrite(err, msg, strlen(msg));
310 if(alpm_trans_interrupt(config->handle) == 0) {
311 /* a transaction is being interrupted, don't exit pacman yet. */
312 return;
314 /* no commiting transaction, we can release it now and then exit pacman */
315 alpm_trans_release(config->handle);
316 /* output a newline to be sure we clear any line we may be on */
317 xwrite(out, "\n", 1);
319 cleanup(128 + signum);
322 #define check_optarg() if(!optarg) { return 1; }
324 static int parsearg_util_addlist(alpm_list_t **list)
326 alpm_list_t *split, *item;
328 check_optarg();
329 split = strsplit(optarg, ',');
330 for(item = split; item; item = alpm_list_next(item)) {
331 *list = alpm_list_add(*list, item->data);
333 alpm_list_free(split);
334 return 0;
337 /** Helper function for parsing operation from command-line arguments.
338 * @param opt Keycode returned by getopt_long
339 * @param dryrun If nonzero, application state is NOT changed
340 * @return 0 if opt was handled, 1 if it was not handled
342 static int parsearg_op(int opt, int dryrun)
344 switch(opt) {
345 /* operations */
346 case 'D':
347 if(dryrun) break;
348 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DATABASE); break;
349 case 'Q':
350 if(dryrun) break;
351 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
352 case 'R':
353 if(dryrun) break;
354 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
355 case 'S':
356 if(dryrun) break;
357 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
358 case 'T':
359 if(dryrun) break;
360 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
361 case 'U':
362 if(dryrun) break;
363 config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
364 case 'V':
365 if(dryrun) break;
366 config->version = 1; break;
367 case 'h':
368 if(dryrun) break;
369 config->help = 1; break;
370 default:
371 return 1;
373 return 0;
376 /** Helper functions for parsing command-line arguments.
377 * @param opt Keycode returned by getopt_long
378 * @return 0 on success, 1 on failure
380 static int parsearg_global(int opt)
382 switch(opt) {
383 case OP_ARCH:
384 check_optarg();
385 config_set_arch(strdup(optarg));
386 break;
387 case OP_ASK:
388 check_optarg();
389 config->noask = 1;
390 config->ask = (unsigned int)atoi(optarg);
391 break;
392 case OP_CACHEDIR:
393 check_optarg();
394 config->cachedirs = alpm_list_add(config->cachedirs, strdup(optarg));
395 break;
396 case OP_CONFIG:
397 check_optarg();
398 if(config->configfile) {
399 free(config->configfile);
401 config->configfile = strndup(optarg, PATH_MAX);
402 break;
403 case OP_DEBUG:
404 /* debug levels are made more 'human readable' than using a raw logmask
405 * here, error and warning are set in config_new, though perhaps a
406 * --quiet option will remove these later */
407 if(optarg) {
408 unsigned short debug = (unsigned short)atoi(optarg);
409 switch(debug) {
410 case 2:
411 config->logmask |= ALPM_LOG_FUNCTION; /* fall through */
412 case 1:
413 config->logmask |= ALPM_LOG_DEBUG;
414 break;
415 default:
416 pm_printf(ALPM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
417 optarg);
418 return 1;
420 } else {
421 config->logmask |= ALPM_LOG_DEBUG;
423 /* progress bars get wonky with debug on, shut them off */
424 config->noprogressbar = 1;
425 break;
426 case OP_GPGDIR:
427 config->gpgdir = strdup(optarg);
428 break;
429 case OP_LOGFILE:
430 check_optarg();
431 config->logfile = strndup(optarg, PATH_MAX);
432 break;
433 case OP_NOCONFIRM: config->noconfirm = 1; break;
434 case 'b':
435 check_optarg();
436 config->dbpath = strdup(optarg);
437 break;
438 case 'r': check_optarg(); config->rootdir = strdup(optarg); break;
439 case 'v': (config->verbose)++; break;
440 default: return 1;
442 return 0;
445 static int parsearg_database(int opt)
447 switch(opt) {
448 case OP_ASDEPS: config->flags |= ALPM_TRANS_FLAG_ALLDEPS; break;
449 case OP_ASEXPLICIT: config->flags |= ALPM_TRANS_FLAG_ALLEXPLICIT; break;
450 default: return 1;
452 return 0;
455 static int parsearg_query(int opt)
457 switch(opt) {
458 case 'c': config->op_q_changelog = 1; break;
459 case 'd': config->op_q_deps = 1; break;
460 case 'e': config->op_q_explicit = 1; break;
461 case 'g': (config->group)++; break;
462 case 'i': (config->op_q_info)++; break;
463 case 'k': config->op_q_check = 1; break;
464 case 'l': config->op_q_list = 1; break;
465 case 'm': config->op_q_foreign = 1; break;
466 case 'o': config->op_q_owns = 1; break;
467 case 'p': config->op_q_isfile = 1; break;
468 case 'q': config->quiet = 1; break;
469 case 's': config->op_q_search = 1; break;
470 case 't': config->op_q_unrequired = 1; break;
471 case 'u': config->op_q_upgrade = 1; break;
472 default: return 1;
474 return 0;
477 /* options common to -S -R -U */
478 static int parsearg_trans(int opt)
480 switch(opt) {
481 case 'd':
482 if(config->flags & ALPM_TRANS_FLAG_NODEPVERSION) {
483 config->flags |= ALPM_TRANS_FLAG_NODEPS;
484 } else {
485 config->flags |= ALPM_TRANS_FLAG_NODEPVERSION;
487 break;
488 case 'k': config->flags |= ALPM_TRANS_FLAG_DBONLY; break;
489 case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
490 case OP_NOSCRIPTLET: config->flags |= ALPM_TRANS_FLAG_NOSCRIPTLET; break;
491 case 'p': config->print = 1; break;
492 case OP_PRINTFORMAT:
493 check_optarg();
494 config->print_format = strdup(optarg);
495 break;
496 default: return 1;
498 return 0;
501 static int parsearg_remove(int opt)
503 if(parsearg_trans(opt) == 0)
504 return 0;
505 switch(opt) {
506 case 'c': config->flags |= ALPM_TRANS_FLAG_CASCADE; break;
507 case 'n': config->flags |= ALPM_TRANS_FLAG_NOSAVE; break;
508 case 's':
509 if(config->flags & ALPM_TRANS_FLAG_RECURSE) {
510 config->flags |= ALPM_TRANS_FLAG_RECURSEALL;
511 } else {
512 config->flags |= ALPM_TRANS_FLAG_RECURSE;
514 break;
515 case 'u': config->flags |= ALPM_TRANS_FLAG_UNNEEDED; break;
516 default: return 1;
518 return 0;
521 /* options common to -S -U */
522 static int parsearg_upgrade(int opt)
524 if(parsearg_trans(opt) == 0)
525 return 0;
526 switch(opt) {
527 case 'f': config->flags |= ALPM_TRANS_FLAG_FORCE; break;
528 case OP_ASDEPS: config->flags |= ALPM_TRANS_FLAG_ALLDEPS; break;
529 case OP_ASEXPLICIT: config->flags |= ALPM_TRANS_FLAG_ALLEXPLICIT; break;
530 case OP_IGNORE:
531 parsearg_util_addlist(&(config->ignorepkg));
532 break;
533 case OP_IGNOREGROUP:
534 parsearg_util_addlist(&(config->ignoregrp));
535 break;
536 default: return 1;
538 return 0;
541 static int parsearg_sync(int opt)
543 if(parsearg_upgrade(opt) == 0)
544 return 0;
545 switch(opt) {
546 case OP_NEEDED: config->flags |= ALPM_TRANS_FLAG_NEEDED; break;
547 case 'c': (config->op_s_clean)++; break;
548 case 'g': (config->group)++; break;
549 case 'i': (config->op_s_info)++; break;
550 case 'l': config->op_q_list = 1; break;
551 case 'q': config->quiet = 1; break;
552 case 's': config->op_s_search = 1; break;
553 case 'u': (config->op_s_upgrade)++; break;
554 case 'w':
555 config->op_s_downloadonly = 1;
556 config->flags |= ALPM_TRANS_FLAG_DOWNLOADONLY;
557 config->flags |= ALPM_TRANS_FLAG_NOCONFLICTS;
558 break;
559 case 'y': (config->op_s_sync)++; break;
560 default: return 1;
562 return 0;
565 /** Parse command-line arguments for each operation.
566 * @param argc argc
567 * @param argv argv
568 * @return 0 on success, 1 on error
570 static int parseargs(int argc, char *argv[])
572 int opt;
573 int option_index = 0;
574 int result;
575 const char *optstring = "DQRSTUVb:cdefghiklmnopqr:stuvwy";
576 static struct option opts[] =
578 {"database", no_argument, 0, 'D'},
579 {"query", no_argument, 0, 'Q'},
580 {"remove", no_argument, 0, 'R'},
581 {"sync", no_argument, 0, 'S'},
582 {"deptest", no_argument, 0, 'T'}, /* used by makepkg */
583 {"upgrade", no_argument, 0, 'U'},
584 {"version", no_argument, 0, 'V'},
585 {"dbpath", required_argument, 0, 'b'},
586 {"cascade", no_argument, 0, 'c'},
587 {"changelog", no_argument, 0, 'c'},
588 {"clean", no_argument, 0, 'c'},
589 {"nodeps", no_argument, 0, 'd'},
590 {"deps", no_argument, 0, 'd'},
591 {"explicit", no_argument, 0, 'e'},
592 {"force", no_argument, 0, 'f'},
593 {"groups", no_argument, 0, 'g'},
594 {"help", no_argument, 0, 'h'},
595 {"info", no_argument, 0, 'i'},
596 {"dbonly", no_argument, 0, 'k'},
597 {"check", no_argument, 0, 'k'},
598 {"list", no_argument, 0, 'l'},
599 {"foreign", no_argument, 0, 'm'},
600 {"nosave", no_argument, 0, 'n'},
601 {"owns", no_argument, 0, 'o'},
602 {"file", no_argument, 0, 'p'},
603 {"print", no_argument, 0, 'p'},
604 {"quiet", no_argument, 0, 'q'},
605 {"root", required_argument, 0, 'r'},
606 {"recursive", no_argument, 0, 's'},
607 {"search", no_argument, 0, 's'},
608 {"unrequired", no_argument, 0, 't'},
609 {"upgrades", no_argument, 0, 'u'},
610 {"sysupgrade", no_argument, 0, 'u'},
611 {"unneeded", no_argument, 0, 'u'},
612 {"verbose", no_argument, 0, 'v'},
613 {"downloadonly", no_argument, 0, 'w'},
614 {"refresh", no_argument, 0, 'y'},
615 {"noconfirm", no_argument, 0, OP_NOCONFIRM},
616 {"config", required_argument, 0, OP_CONFIG},
617 {"ignore", required_argument, 0, OP_IGNORE},
618 {"debug", optional_argument, 0, OP_DEBUG},
619 {"noprogressbar", no_argument, 0, OP_NOPROGRESSBAR},
620 {"noscriptlet", no_argument, 0, OP_NOSCRIPTLET},
621 {"ask", required_argument, 0, OP_ASK},
622 {"cachedir", required_argument, 0, OP_CACHEDIR},
623 {"asdeps", no_argument, 0, OP_ASDEPS},
624 {"logfile", required_argument, 0, OP_LOGFILE},
625 {"ignoregroup", required_argument, 0, OP_IGNOREGROUP},
626 {"needed", no_argument, 0, OP_NEEDED},
627 {"asexplicit", no_argument, 0, OP_ASEXPLICIT},
628 {"arch", required_argument, 0, OP_ARCH},
629 {"print-format", required_argument, 0, OP_PRINTFORMAT},
630 {"gpgdir", required_argument, 0, OP_GPGDIR},
631 {0, 0, 0, 0}
634 /* parse operation */
635 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
636 if(opt < 0) {
637 break;
638 } else if(opt == 0) {
639 continue;
640 } else if(opt == '?') {
641 /* unknown option, getopt printed an error */
642 return 1;
644 parsearg_op(opt, 0);
647 if(config->op == 0) {
648 pm_printf(ALPM_LOG_ERROR, _("only one operation may be used at a time\n"));
649 return 1;
651 if(config->help) {
652 usage(config->op, mbasename(argv[0]));
653 return 2;
655 if(config->version) {
656 version();
657 return 2;
660 /* parse all other options */
661 optind = 1;
662 while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
663 if(opt < 0) {
664 break;
665 } else if(opt == 0) {
666 continue;
667 } else if(opt == '?') {
668 /* this should have failed during first pass already */
669 return 1;
670 } else if(parsearg_op(opt, 1) == 0) {
671 /* opt is an operation */
672 continue;
675 switch(config->op) {
676 case PM_OP_DATABASE:
677 result = parsearg_database(opt);
678 break;
679 case PM_OP_QUERY:
680 result = parsearg_query(opt);
681 break;
682 case PM_OP_REMOVE:
683 result = parsearg_remove(opt);
684 break;
685 case PM_OP_SYNC:
686 result = parsearg_sync(opt);
687 break;
688 case PM_OP_UPGRADE:
689 result = parsearg_upgrade(opt);
690 break;
691 case PM_OP_DEPTEST:
692 default:
693 result = 1;
694 break;
696 if(result == 0) {
697 continue;
700 /* fall back to global options */
701 result = parsearg_global(opt);
702 if(result != 0) {
703 /* global option parsing failed, abort */
704 pm_printf(ALPM_LOG_ERROR, _("invalid option\n"));
705 return result;
709 while(optind < argc) {
710 /* add the target to our target array */
711 pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
712 optind++;
715 return 0;
718 /** print commandline to logfile
720 static void cl_to_log(int argc, char* argv[])
722 size_t size = 0;
723 int i;
724 for(i = 0; i < argc; i++) {
725 size += strlen(argv[i]) + 1;
727 if(!size) {
728 return;
730 char *cl_text = malloc(size);
731 if(!cl_text) {
732 return;
734 char *p = cl_text;
735 for(i = 0; i < argc - 1; i++) {
736 strcpy(p, argv[i]);
737 p += strlen(argv[i]);
738 *p++ = ' ';
740 strcpy(p, argv[i]);
741 alpm_logaction(config->handle, "Running '%s'\n", cl_text);
742 free(cl_text);
745 /** Main function.
746 * @param argc argc
747 * @param argv argv
748 * @return A return code indicating success, failure, etc.
750 int main(int argc, char *argv[])
752 int ret = 0;
753 struct sigaction new_action, old_action;
754 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
755 /* geteuid undefined in CYGWIN */
756 uid_t myuid = geteuid();
757 #endif
759 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
760 /*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
761 mtrace();
762 #endif
764 /* Set signal handlers */
765 /* Set up the structure to specify the new action. */
766 new_action.sa_handler = handler;
767 sigemptyset(&new_action.sa_mask);
768 new_action.sa_flags = 0;
770 sigaction(SIGINT, NULL, &old_action);
771 if(old_action.sa_handler != SIG_IGN) {
772 sigaction(SIGINT, &new_action, NULL);
774 sigaction(SIGTERM, NULL, &old_action);
775 if(old_action.sa_handler != SIG_IGN) {
776 sigaction(SIGTERM, &new_action, NULL);
778 sigaction(SIGSEGV, NULL, &old_action);
779 if(old_action.sa_handler != SIG_IGN) {
780 sigaction(SIGSEGV, &new_action, NULL);
783 /* i18n init */
784 #if defined(ENABLE_NLS)
785 localize();
786 #endif
788 /* set user agent for downloading */
789 setuseragent();
791 /* init config data */
792 config = config_new();
794 /* disable progressbar if the output is redirected */
795 if(!isatty(1)) {
796 config->noprogressbar = 1;
799 /* Priority of options:
800 * 1. command line
801 * 2. config file
802 * 3. compiled-in defaults
803 * However, we have to parse the command line first because a config file
804 * location can be specified here, so we need to make sure we prefer these
805 * options over the config file coming second.
808 /* parse the command line */
809 ret = parseargs(argc, argv);
810 if(ret != 0) {
811 cleanup(ret);
814 /* we support reading targets from stdin if a cmdline parameter is '-' */
815 if(!isatty(fileno(stdin)) && alpm_list_find_str(pm_targets, "-")) {
816 char line[PATH_MAX];
817 int i = 0;
819 /* remove the '-' from the list */
820 pm_targets = alpm_list_remove_str(pm_targets, "-", NULL);
822 while(i < PATH_MAX && (line[i] = (char)fgetc(stdin)) != EOF) {
823 if(isspace((unsigned char)line[i])) {
824 /* avoid adding zero length arg when multiple spaces separate args */
825 if(i > 0) {
826 line[i] = '\0';
827 pm_targets = alpm_list_add(pm_targets, strdup(line));
828 i = 0;
830 } else {
831 i++;
834 /* check for buffer overflow */
835 if(i >= PATH_MAX) {
836 pm_printf(ALPM_LOG_ERROR, _("buffer overflow detected in arg parsing\n"));
837 cleanup(EXIT_FAILURE);
840 /* end of stream -- check for data still in line buffer */
841 if(i > 0) {
842 line[i] = '\0';
843 pm_targets = alpm_list_add(pm_targets, strdup(line));
845 if(!freopen(ctermid(NULL), "r", stdin)) {
846 pm_printf(ALPM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
847 strerror(errno));
851 /* parse the config file */
852 ret = parseconfig(config->configfile);
853 if(ret != 0) {
854 cleanup(ret);
857 /* noask is meant to be non-interactive */
858 if(config->noask) {
859 config->noconfirm = 1;
862 /* set up the print operations */
863 if(config->print && !config->op_s_clean) {
864 config->noconfirm = 1;
865 config->flags |= ALPM_TRANS_FLAG_NOCONFLICTS;
866 config->flags |= ALPM_TRANS_FLAG_NOLOCK;
867 /* Display only errors */
868 config->logmask &= ~ALPM_LOG_WARNING;
871 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
872 /* check if we have sufficient permission for the requested operation */
873 if(myuid > 0 && needs_root()) {
874 pm_printf(ALPM_LOG_ERROR, _("you cannot perform this operation unless you are root.\n"));
875 cleanup(EXIT_FAILURE);
877 #endif
879 if(config->verbose > 0) {
880 alpm_list_t *i;
881 printf("Root : %s\n", alpm_option_get_root(config->handle));
882 printf("Conf File : %s\n", config->configfile);
883 printf("DB Path : %s\n", alpm_option_get_dbpath(config->handle));
884 printf("Cache Dirs: ");
885 for(i = alpm_option_get_cachedirs(config->handle); i; i = alpm_list_next(i)) {
886 printf("%s ", (char *)alpm_list_getdata(i));
888 printf("\n");
889 printf("Lock File : %s\n", alpm_option_get_lockfile(config->handle));
890 printf("Log File : %s\n", alpm_option_get_logfile(config->handle));
891 printf("GPG Dir : %s\n", alpm_option_get_gpgdir(config->handle));
892 list_display("Targets :", pm_targets);
895 /* Log commandline */
896 if(needs_root()) {
897 cl_to_log(argc, argv);
900 /* start the requested operation */
901 switch(config->op) {
902 case PM_OP_DATABASE:
903 ret = pacman_database(pm_targets);
904 break;
905 case PM_OP_REMOVE:
906 ret = pacman_remove(pm_targets);
907 break;
908 case PM_OP_UPGRADE:
909 ret = pacman_upgrade(pm_targets);
910 break;
911 case PM_OP_QUERY:
912 ret = pacman_query(pm_targets);
913 break;
914 case PM_OP_SYNC:
915 ret = pacman_sync(pm_targets);
916 break;
917 case PM_OP_DEPTEST:
918 ret = pacman_deptest(pm_targets);
919 break;
920 default:
921 pm_printf(ALPM_LOG_ERROR, _("no operation specified (use -h for help)\n"));
922 ret = EXIT_FAILURE;
925 cleanup(ret);
926 /* not reached */
927 return EXIT_SUCCESS;
930 /* vim: set ts=2 sw=2 noet: */