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/>.
23 /* special handling of package version for GIT */
24 #if defined(GIT_VERSION)
25 #undef PACKAGE_VERSION
26 #define PACKAGE_VERSION GIT_VERSION
29 #include <ctype.h> /* isspace */
30 #include <stdlib.h> /* atoi */
32 #include <ctype.h> /* isspace */
38 #include <sys/types.h>
39 #include <sys/utsname.h> /* uname */
40 #include <locale.h> /* setlocale */
42 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
43 #include <mcheck.h> /* debug tracing (mtrace) */
48 #include <alpm_list.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
)
65 if(s1
== s2
) return 0;
68 /* First skip all spaces in both strings */
69 while(isspace((unsigned char)*s1
)) {
72 while(isspace((unsigned char)*s2
)) {
75 /* If we compare a long option (--abcd) and a short one (-a),
76 * the short one always wins */
77 if(*s1
== '-' && *s2
== '-') {
80 if(*s1
== '-' && *s2
== '-') {
81 /* two long -> strcmp */
84 } else if(*s2
== '-') {
85 /* s1 short, s2 long */
87 } else if(*s1
== '-') {
88 /* s1 long, s2 short */
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
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"),
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
);
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"));
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"));
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
);
218 /** Output pacman version and copyright.
220 static void version(void)
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");
227 printf(_(" This program may be freely redistributed under\n"
228 " the terms of the GNU General Public License.\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)
240 setlocale(LC_ALL
, "");
241 bindtextdomain(PACKAGE
, LOCALEDIR
);
248 /** Set user agent environment variable.
250 static void setuseragent(void)
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());
272 FREELIST(pm_targets
);
281 /** Write function that correctly handles EINTR.
283 static ssize_t
xwrite(int fd
, const void *buf
, size_t count
)
287 ret
= write(fd
, buf
, count
);
288 } while(ret
== -1 && errno
== EINTR
);
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
));
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. */
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);
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 */
332 list
= strsplit(optarg
, ',');
333 for(item
= list
; item
; item
= alpm_list_next(item
)) {
334 fn((char *)alpm_list_getdata(item
));
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
)
351 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_DATABASE
); break;
354 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_QUERY
); break;
357 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_REMOVE
); break;
360 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_SYNC
); break;
363 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_DEPTEST
); break;
366 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_UPGRADE
); break;
369 config
->version
= 1; break;
372 config
->help
= 1; break;
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
)
388 config_set_arch(optarg
);
393 config
->ask
= (unsigned int)atoi(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());
405 if(config
->configfile
) {
406 free(config
->configfile
);
408 config
->configfile
= strndup(optarg
, PATH_MAX
);
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 */
415 unsigned short debug
= (unsigned short)atoi(optarg
);
418 config
->logmask
|= PM_LOG_FUNCTION
; /* fall through */
420 config
->logmask
|= PM_LOG_DEBUG
;
423 pm_printf(PM_LOG_ERROR
, _("'%s' is not a valid debug level\n"),
428 config
->logmask
|= PM_LOG_DEBUG
;
430 /* progress bars get wonky with debug on, shut them off */
431 config
->noprogressbar
= 1;
434 config
->gpgdir
= strdup(optarg
);
438 config
->logfile
= strndup(optarg
, PATH_MAX
);
440 case OP_NOCONFIRM
: config
->noconfirm
= 1; break;
443 config
->dbpath
= strdup(optarg
);
445 case 'r': check_optarg(); config
->rootdir
= strdup(optarg
); break;
446 case 'v': (config
->verbose
)++; break;
452 static int parsearg_database(int opt
)
455 case OP_ASDEPS
: config
->flags
|= PM_TRANS_FLAG_ALLDEPS
; break;
456 case OP_ASEXPLICIT
: config
->flags
|= PM_TRANS_FLAG_ALLEXPLICIT
; break;
462 static int parsearg_query(int 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;
484 /* options common to -S -R -U */
485 static int parsearg_trans(int opt
)
489 if(config
->flags
& PM_TRANS_FLAG_NODEPVERSION
) {
490 config
->flags
|= PM_TRANS_FLAG_NODEPS
;
492 config
->flags
|= PM_TRANS_FLAG_NODEPVERSION
;
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;
501 config
->print_format
= strdup(optarg
);
508 static int parsearg_remove(int opt
)
510 if(parsearg_trans(opt
) == 0)
513 case 'c': config
->flags
|= PM_TRANS_FLAG_CASCADE
; break;
514 case 'n': config
->flags
|= PM_TRANS_FLAG_NOSAVE
; break;
516 if(config
->flags
& PM_TRANS_FLAG_RECURSE
) {
517 config
->flags
|= PM_TRANS_FLAG_RECURSEALL
;
519 config
->flags
|= PM_TRANS_FLAG_RECURSE
;
522 case 'u': config
->flags
|= PM_TRANS_FLAG_UNNEEDED
; break;
528 /* options common to -S -U */
529 static int parsearg_upgrade(int opt
)
531 if(parsearg_trans(opt
) == 0)
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;
538 parsearg_util_addlist(alpm_option_add_ignorepkg
);
541 parsearg_util_addlist(alpm_option_add_ignoregrp
);
548 static int parsearg_sync(int opt
)
550 if(parsearg_upgrade(opt
) == 0)
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;
562 config
->op_s_downloadonly
= 1;
563 config
->flags
|= PM_TRANS_FLAG_DOWNLOADONLY
;
564 config
->flags
|= PM_TRANS_FLAG_NOCONFLICTS
;
566 case 'y': (config
->op_s_sync
)++; break;
572 /** Parse command-line arguments for each operation.
575 * @return 0 on success, 1 on error
577 static int parseargs(int argc
, char *argv
[])
580 int option_index
= 0;
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
},
641 /* parse operation */
642 while((opt
= getopt_long(argc
, argv
, optstring
, opts
, &option_index
))) {
645 } else if(opt
== 0) {
647 } else if(opt
== '?') {
648 /* unknown option, getopt printed an error */
654 if(config
->op
== 0) {
655 pm_printf(PM_LOG_ERROR
, _("only one operation may be used at a time\n"));
659 usage(config
->op
, mbasename(argv
[0]));
662 if(config
->version
) {
667 /* parse all other options */
669 while((opt
= getopt_long(argc
, argv
, optstring
, opts
, &option_index
))) {
672 } else if(opt
== 0) {
674 } else if(opt
== '?') {
675 /* this should have failed during first pass already */
677 } else if(parsearg_op(opt
, 1) == 0) {
678 /* opt is an operation */
684 result
= parsearg_database(opt
);
687 result
= parsearg_query(opt
);
690 result
= parsearg_remove(opt
);
693 result
= parsearg_sync(opt
);
696 result
= parsearg_upgrade(opt
);
707 /* fall back to global options */
708 result
= parsearg_global(opt
);
710 /* global option parsing failed, abort */
711 pm_printf(PM_LOG_ERROR
, _("invalid option\n"));
716 while(optind
< argc
) {
717 /* add the target to our target array */
718 pm_targets
= alpm_list_add(pm_targets
, strdup(argv
[optind
]));
725 /** print commandline to logfile
727 static void cl_to_log(int argc
, char* argv
[])
731 for(i
= 0; i
<argc
; i
++) {
732 size
+= strlen(argv
[i
]) + 1;
734 char *cl_text
= malloc(size
);
738 for(i
= 0; i
<argc
-1; i
++) {
740 p
+= strlen(argv
[i
]);
744 alpm_logaction("Running '%s'\n", cl_text
);
751 * @return A return code indicating success, failure, etc.
753 int main(int argc
, char *argv
[])
756 struct sigaction new_action
, old_action
;
757 #if defined(HAVE_GETEUID) && !defined(CYGWIN)
758 /* geteuid undefined in CYGWIN */
759 uid_t myuid
= geteuid();
762 #if defined(PACMAN_DEBUG) && defined(HAVE_MCHECK_H)
763 /*setenv("MALLOC_TRACE","pacman.mtrace", 0);*/
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
);
787 #if defined(ENABLE_NLS)
791 /* set user agent for downloading */
794 /* init config data */
795 config
= config_new();
797 /* disable progressbar if the output is redirected */
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:
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
);
833 /* we support reading targets from stdin if a cmdline parameter is '-' */
834 if(!isatty(fileno(stdin
)) && alpm_list_find_str(pm_targets
, "-")) {
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 */
846 pm_targets
= alpm_list_add(pm_targets
, strdup(line
));
853 /* check for buffer overflow */
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 */
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"),
870 /* parse the config file */
871 ret
= parseconfig(config
->configfile
);
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 */
883 config
->noconfirm
= 1;
886 /* set up the print operations */
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
);
903 if(config
->verbose
> 0) {
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
));
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 */
920 cl_to_log(argc
, argv
);
923 /* start the requested operation */
926 ret
= pacman_database(pm_targets
);
929 ret
= pacman_remove(pm_targets
);
932 ret
= pacman_upgrade(pm_targets
);
935 ret
= pacman_query(pm_targets
);
938 ret
= pacman_sync(pm_targets
);
941 ret
= pacman_deptest(pm_targets
);
944 pm_printf(PM_LOG_ERROR
, _("no operation specified (use -h for help)\n"));
953 /* vim: set ts=2 sw=2 noet: */