4 * Copyright (c) 2006-2013 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 /* special handling of package version for GIT */
22 #if defined(GIT_VERSION)
23 #undef PACKAGE_VERSION
24 #define PACKAGE_VERSION GIT_VERSION
27 #include <stdlib.h> /* atoi */
29 #include <ctype.h> /* isspace */
35 #include <sys/types.h>
36 #include <sys/utsname.h> /* uname */
37 #include <locale.h> /* setlocale */
42 #include <alpm_list.h>
49 /* list of targets specified on command line */
50 static alpm_list_t
*pm_targets
;
52 /* Used to sort the options in --help */
53 static int options_cmp(const void *p1
, const void *p2
)
58 if(s1
== s2
) return 0;
61 /* First skip all spaces in both strings */
62 while(isspace((unsigned char)*s1
)) {
65 while(isspace((unsigned char)*s2
)) {
68 /* If we compare a long option (--abcd) and a short one (-a),
69 * the short one always wins */
70 if(*s1
== '-' && *s2
== '-') {
73 if(*s1
== '-' && *s2
== '-') {
74 /* two long -> strcmp */
77 } else if(*s2
== '-') {
78 /* s1 short, s2 long */
80 } else if(*s1
== '-') {
81 /* s1 long, s2 short */
84 /* two short -> strcmp */
87 return strcmp(s1
, s2
);
90 /** Display usage/syntax for the specified operation.
91 * @param op the operation code requested
92 * @param myname basename(argv[0])
94 static void usage(int op
, const char * const myname
)
96 #define addlist(s) (list = alpm_list_add(list, s))
97 alpm_list_t
*list
= NULL
, *i
;
98 /* prefetch some strings for usage below, which moves a lot of calls
100 char const * const str_opt
= _("options");
101 char const * const str_file
= _("file(s)");
102 char const * const str_pkg
= _("package(s)");
103 char const * const str_usg
= _("usage");
104 char const * const str_opr
= _("operation");
106 /* please limit your strings to 80 characters in width */
107 if(op
== PM_OP_MAIN
) {
108 printf("%s: %s <%s> [...]\n", str_usg
, myname
, str_opr
);
109 printf(_("operations:\n"));
110 printf(" %s {-h --help}\n", myname
);
111 printf(" %s {-V --version}\n", myname
);
112 printf(" %s {-D --database} <%s> <%s>\n", myname
, str_opt
, str_pkg
);
113 printf(" %s {-Q --query} [%s] [%s]\n", myname
, str_opt
, str_pkg
);
114 printf(" %s {-R --remove} [%s] <%s>\n", myname
, str_opt
, str_pkg
);
115 printf(" %s {-S --sync} [%s] [%s]\n", myname
, str_opt
, str_pkg
);
116 printf(" %s {-T --deptest} [%s] [%s]\n", myname
, str_opt
, str_pkg
);
117 printf(" %s {-U --upgrade} [%s] <%s>\n", myname
, str_opt
, str_file
);
118 printf(_("\nuse '%s {-h --help}' with an operation for available options\n"),
121 if(op
== PM_OP_REMOVE
) {
122 printf("%s: %s {-R --remove} [%s] <%s>\n", str_usg
, myname
, str_opt
, str_pkg
);
123 printf("%s:\n", str_opt
);
124 addlist(_(" -c, --cascade remove packages and all packages that depend on them\n"));
125 addlist(_(" -n, --nosave remove configuration files\n"));
126 addlist(_(" -s, --recursive remove unnecessary dependencies\n"
127 " (-ss includes explicitly installed dependencies)\n"));
128 addlist(_(" -u, --unneeded remove unneeded packages\n"));
129 } else if(op
== PM_OP_UPGRADE
) {
130 printf("%s: %s {-U --upgrade} [%s] <%s>\n", str_usg
, myname
, str_opt
, str_file
);
131 addlist(_(" --needed do not reinstall up to date packages\n"));
132 printf("%s:\n", str_opt
);
133 } else if(op
== PM_OP_QUERY
) {
134 printf("%s: %s {-Q --query} [%s] [%s]\n", str_usg
, myname
, str_opt
, str_pkg
);
135 printf("%s:\n", str_opt
);
136 addlist(_(" -c, --changelog view the changelog of a package\n"));
137 addlist(_(" -d, --deps list packages installed as dependencies [filter]\n"));
138 addlist(_(" -e, --explicit list packages explicitly installed [filter]\n"));
139 addlist(_(" -g, --groups view all members of a package group\n"));
140 addlist(_(" -i, --info view package information (-ii for backup files)\n"));
141 addlist(_(" -k, --check check that package files exist (-kk for file properties)\n"));
142 addlist(_(" -l, --list list the contents of the queried package\n"));
143 addlist(_(" -m, --foreign list installed packages not found in sync db(s) [filter]\n"));
144 addlist(_(" -n, --native list installed packages only found in sync db(s) [filter]\n"));
145 addlist(_(" -o, --owns <file> query the package that owns <file>\n"));
146 addlist(_(" -p, --file <package> query a package file instead of the database\n"));
147 addlist(_(" -q, --quiet show less information for query and search\n"));
148 addlist(_(" -s, --search <regex> search locally-installed packages for matching strings\n"));
149 addlist(_(" -t, --unrequired list packages not required by any package [filter]\n"));
150 addlist(_(" -u, --upgrades list outdated packages [filter]\n"));
151 } else if(op
== PM_OP_SYNC
) {
152 printf("%s: %s {-S --sync} [%s] [%s]\n", str_usg
, myname
, str_opt
, str_pkg
);
153 printf("%s:\n", str_opt
);
154 addlist(_(" -c, --clean remove old packages from cache directory (-cc for all)\n"));
155 addlist(_(" -g, --groups view all members of a package group\n"));
156 addlist(_(" -i, --info view package information\n"));
157 addlist(_(" -l, --list <repo> view a list of packages in a repo\n"));
158 addlist(_(" -q, --quiet show less information for query and search\n"));
159 addlist(_(" -s, --search <regex> search remote repositories for matching strings\n"));
160 addlist(_(" -u, --sysupgrade upgrade installed packages (-uu allows downgrade)\n"));
161 addlist(_(" -w, --downloadonly download packages but do not install/upgrade anything\n"));
162 addlist(_(" -y, --refresh download fresh package databases from the server\n"));
163 addlist(_(" --needed do not reinstall up to date packages\n"));
164 } else if(op
== PM_OP_DATABASE
) {
165 printf("%s: %s {-D --database} <%s> <%s>\n", str_usg
, myname
, str_opt
, str_pkg
);
166 printf("%s:\n", str_opt
);
167 addlist(_(" --asdeps mark packages as non-explicitly installed\n"));
168 addlist(_(" --asexplicit mark packages as explicitly installed\n"));
169 } else if(op
== PM_OP_DEPTEST
) {
170 printf("%s: %s {-T --deptest} [%s] [%s]\n", str_usg
, myname
, str_opt
, str_pkg
);
171 printf("%s:\n", str_opt
);
176 addlist(_(" --force force install, overwrite conflicting files\n"));
177 addlist(_(" --asdeps install packages as non-explicitly installed\n"));
178 addlist(_(" --asexplicit install packages as explicitly installed\n"));
179 addlist(_(" --ignore <pkg> ignore a package upgrade (can be used more than once)\n"));
180 addlist(_(" --ignoregroup <grp>\n"
181 " ignore a group upgrade (can be used more than once)\n"));
184 addlist(_(" -d, --nodeps skip dependency version checks (-dd to skip all checks)\n"));
185 addlist(_(" --dbonly only modify database entries, not package files\n"));
186 addlist(_(" --noprogressbar do not show a progress bar when downloading files\n"));
187 addlist(_(" --noscriptlet do not execute the install scriptlet if one exists\n"));
188 addlist(_(" -p, --print print the targets instead of performing the operation\n"));
189 addlist(_(" --print-format <string>\n"
190 " specify how the targets should be printed\n"));
194 addlist(_(" -b, --dbpath <path> set an alternate database location\n"));
195 addlist(_(" -r, --root <path> set an alternate installation root\n"));
196 addlist(_(" -v, --verbose be verbose\n"));
197 addlist(_(" --arch <arch> set an alternate architecture\n"));
198 addlist(_(" --cachedir <dir> set an alternate package cache location\n"));
199 addlist(_(" --color <when> colorize the output\n"));
200 addlist(_(" --config <path> set an alternate configuration file\n"));
201 addlist(_(" --debug display debug messages\n"));
202 addlist(_(" --gpgdir <path> set an alternate home directory for GnuPG\n"));
203 addlist(_(" --logfile <path> set an alternate log file\n"));
204 addlist(_(" --noconfirm do not ask for any confirmation\n"));
206 list
= alpm_list_msort(list
, alpm_list_count(list
), options_cmp
);
207 for(i
= list
; i
; i
= alpm_list_next(i
)) {
208 fputs((const char *)i
->data
, stdout
);
210 alpm_list_free(list
);
214 /** Output pacman version and copyright.
216 static void version(void)
219 printf(" .--. Pacman v%s - libalpm v%s\n", PACKAGE_VERSION
, alpm_version());
220 printf("/ _.-' .-. .-. .-. Copyright (C) 2006-2013 Pacman Development Team\n");
221 printf("\\ '-. '-' '-' '-' Copyright (C) 2002-2006 Judd Vinet\n");
223 printf(_(" This program may be freely redistributed under\n"
224 " the terms of the GNU General Public License.\n"));
228 /** Sets up gettext localization. Safe to call multiple times.
230 /* Inspired by the monotone function localize_monotone. */
231 #if defined(ENABLE_NLS)
232 static void localize(void)
236 setlocale(LC_ALL
, "");
237 bindtextdomain(PACKAGE
, LOCALEDIR
);
244 /** Set user agent environment variable.
246 static void setuseragent(void)
252 snprintf(agent
, 100, "pacman/%s (%s %s) libalpm/%s",
253 PACKAGE_VERSION
, un
.sysname
, un
.machine
, alpm_version());
254 setenv("HTTP_USER_AGENT", agent
, 0);
257 /** Free the resources.
259 * @param ret the return value
261 static void cleanup(int ret
)
263 /* free alpm library resources */
264 if(config
->handle
&& alpm_release(config
->handle
) == -1) {
265 pm_printf(ALPM_LOG_ERROR
, "error releasing alpm library\n");
269 FREELIST(pm_targets
);
278 /** Write function that correctly handles EINTR.
280 static ssize_t
xwrite(int fd
, const void *buf
, size_t count
)
284 ret
= write(fd
, buf
, count
);
285 } while(ret
== -1 && errno
== EINTR
);
289 /** Catches thrown signals. Performs necessary cleanup to ensure database is
290 * in a consistent state.
291 * @param signum the thrown signal
293 static void handler(int signum
)
295 int out
= fileno(stdout
);
296 int err
= fileno(stderr
);
298 if(signum
== SIGSEGV
) {
299 msg
= "\nerror: segmentation fault\n"
300 "Please submit a full bug report with --debug if appropriate.\n";
301 xwrite(err
, msg
, strlen(msg
));
303 } else if(signum
== SIGINT
|| signum
== SIGHUP
) {
304 if(signum
== SIGINT
) {
305 msg
= "\nInterrupt signal received\n";
307 msg
= "\nHangup 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. */
315 /* SIGINT: no committing transaction, release it now and then exit pacman
316 * SIGHUP, SIGTERM: release no matter what */
317 alpm_trans_release(config
->handle
);
318 /* output a newline to be sure we clear any line we may be on */
319 xwrite(out
, "\n", 1);
320 cleanup(128 + signum
);
323 #define check_optarg() if(!optarg) { return 1; }
325 static int parsearg_util_addlist(alpm_list_t
**list
)
327 alpm_list_t
*split
, *item
;
330 split
= strsplit(optarg
, ',');
331 for(item
= split
; item
; item
= alpm_list_next(item
)) {
332 *list
= alpm_list_add(*list
, item
->data
);
334 alpm_list_free(split
);
338 /** Helper function for parsing operation from command-line arguments.
339 * @param opt Keycode returned by getopt_long
340 * @param dryrun If nonzero, application state is NOT changed
341 * @return 0 if opt was handled, 1 if it was not handled
343 static int parsearg_op(int opt
, int dryrun
)
349 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_DATABASE
); break;
352 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_QUERY
); break;
355 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_REMOVE
); break;
358 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_SYNC
); break;
361 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_DEPTEST
); break;
364 config
->op
= (config
->op
!= PM_OP_MAIN
? 0 : PM_OP_UPGRADE
); break;
367 config
->version
= 1; break;
370 config
->help
= 1; break;
377 /** Helper functions for parsing command-line arguments.
378 * @param opt Keycode returned by getopt_long
379 * @return 0 on success, 1 on failure
381 static int parsearg_global(int opt
)
386 config_set_arch(optarg
);
391 config
->ask
= (unsigned int)atoi(optarg
);
395 config
->cachedirs
= alpm_list_add(config
->cachedirs
, strdup(optarg
));
398 if(strcmp("never", optarg
) == 0) {
399 config
->color
= PM_COLOR_OFF
;
400 } else if(strcmp("auto", optarg
) == 0) {
401 config
->color
= isatty(fileno(stdout
)) ? PM_COLOR_ON
: PM_COLOR_OFF
;
402 } else if(strcmp("always", optarg
) == 0) {
403 config
->color
= PM_COLOR_ON
;
405 pm_printf(ALPM_LOG_ERROR
, _("invalid argument '%s' for %s\n"),
409 enable_colors(config
->color
);
413 if(config
->configfile
) {
414 free(config
->configfile
);
416 config
->configfile
= strndup(optarg
, PATH_MAX
);
419 /* debug levels are made more 'human readable' than using a raw logmask
420 * here, error and warning are set in config_new, though perhaps a
421 * --quiet option will remove these later */
423 unsigned short debug
= (unsigned short)atoi(optarg
);
426 config
->logmask
|= ALPM_LOG_FUNCTION
; /* fall through */
428 config
->logmask
|= ALPM_LOG_DEBUG
;
431 pm_printf(ALPM_LOG_ERROR
, _("'%s' is not a valid debug level\n"),
436 config
->logmask
|= ALPM_LOG_DEBUG
;
438 /* progress bars get wonky with debug on, shut them off */
439 config
->noprogressbar
= 1;
442 config
->gpgdir
= strdup(optarg
);
446 config
->logfile
= strndup(optarg
, PATH_MAX
);
448 case OP_NOCONFIRM
: config
->noconfirm
= 1; break;
451 config
->dbpath
= strdup(optarg
);
453 case 'r': check_optarg(); config
->rootdir
= strdup(optarg
); break;
454 case 'v': (config
->verbose
)++; break;
460 static int parsearg_database(int opt
)
463 case OP_ASDEPS
: config
->flags
|= ALPM_TRANS_FLAG_ALLDEPS
; break;
464 case OP_ASEXPLICIT
: config
->flags
|= ALPM_TRANS_FLAG_ALLEXPLICIT
; break;
470 static int parsearg_query(int opt
)
473 case 'c': config
->op_q_changelog
= 1; break;
474 case 'd': config
->op_q_deps
= 1; break;
475 case 'e': config
->op_q_explicit
= 1; break;
476 case 'g': (config
->group
)++; break;
477 case 'i': (config
->op_q_info
)++; break;
478 case 'k': (config
->op_q_check
)++; break;
479 case 'l': config
->op_q_list
= 1; break;
480 case 'm': config
->op_q_locality
|= PKG_LOCALITY_LOCAL
; break;
481 case 'n': config
->op_q_locality
|= PKG_LOCALITY_FOREIGN
; break;
482 case 'o': config
->op_q_owns
= 1; break;
483 case 'p': config
->op_q_isfile
= 1; break;
484 case 'q': config
->quiet
= 1; break;
485 case 's': config
->op_q_search
= 1; break;
486 case 't': config
->op_q_unrequired
= 1; break;
487 case 'u': config
->op_q_upgrade
= 1; break;
493 /* options common to -S -R -U */
494 static int parsearg_trans(int opt
)
498 if(config
->flags
& ALPM_TRANS_FLAG_NODEPVERSION
) {
499 config
->flags
|= ALPM_TRANS_FLAG_NODEPS
;
501 config
->flags
|= ALPM_TRANS_FLAG_NODEPVERSION
;
504 case OP_DBONLY
: config
->flags
|= ALPM_TRANS_FLAG_DBONLY
; break;
505 case OP_NOPROGRESSBAR
: config
->noprogressbar
= 1; break;
506 case OP_NOSCRIPTLET
: config
->flags
|= ALPM_TRANS_FLAG_NOSCRIPTLET
; break;
507 case 'p': config
->print
= 1; break;
510 config
->print_format
= strdup(optarg
);
517 static int parsearg_remove(int opt
)
519 if(parsearg_trans(opt
) == 0)
522 case 'c': config
->flags
|= ALPM_TRANS_FLAG_CASCADE
; break;
523 case 'n': config
->flags
|= ALPM_TRANS_FLAG_NOSAVE
; break;
525 if(config
->flags
& ALPM_TRANS_FLAG_RECURSE
) {
526 config
->flags
|= ALPM_TRANS_FLAG_RECURSEALL
;
528 config
->flags
|= ALPM_TRANS_FLAG_RECURSE
;
531 case 'u': config
->flags
|= ALPM_TRANS_FLAG_UNNEEDED
; break;
537 /* options common to -S -U */
538 static int parsearg_upgrade(int opt
)
540 if(parsearg_trans(opt
) == 0)
543 case OP_FORCE
: config
->flags
|= ALPM_TRANS_FLAG_FORCE
; break;
544 case OP_ASDEPS
: config
->flags
|= ALPM_TRANS_FLAG_ALLDEPS
; break;
545 case OP_ASEXPLICIT
: config
->flags
|= ALPM_TRANS_FLAG_ALLEXPLICIT
; break;
546 case OP_NEEDED
: config
->flags
|= ALPM_TRANS_FLAG_NEEDED
; break;
548 parsearg_util_addlist(&(config
->ignorepkg
));
551 parsearg_util_addlist(&(config
->ignoregrp
));
558 static int parsearg_sync(int opt
)
560 if(parsearg_upgrade(opt
) == 0)
563 case 'c': (config
->op_s_clean
)++; break;
564 case 'g': (config
->group
)++; break;
565 case 'i': (config
->op_s_info
)++; break;
566 case 'l': config
->op_q_list
= 1; break;
567 case 'q': config
->quiet
= 1; break;
568 case 's': config
->op_s_search
= 1; break;
569 case 'u': (config
->op_s_upgrade
)++; break;
571 config
->op_s_downloadonly
= 1;
572 config
->flags
|= ALPM_TRANS_FLAG_DOWNLOADONLY
;
573 config
->flags
|= ALPM_TRANS_FLAG_NOCONFLICTS
;
575 case 'y': (config
->op_s_sync
)++; break;
581 /** Parse command-line arguments for each operation.
584 * @return 0 on success, 1 on error
586 static int parseargs(int argc
, char *argv
[])
589 int option_index
= 0;
591 const char *optstring
= "DQRSTUVb:cdefghiklmnopqr:stuvwy";
592 static const struct option opts
[] =
594 {"database", no_argument
, 0, 'D'},
595 {"query", no_argument
, 0, 'Q'},
596 {"remove", no_argument
, 0, 'R'},
597 {"sync", no_argument
, 0, 'S'},
598 {"deptest", no_argument
, 0, 'T'}, /* used by makepkg */
599 {"upgrade", no_argument
, 0, 'U'},
600 {"version", no_argument
, 0, 'V'},
601 {"dbpath", required_argument
, 0, 'b'},
602 {"cascade", no_argument
, 0, 'c'},
603 {"changelog", no_argument
, 0, 'c'},
604 {"clean", no_argument
, 0, 'c'},
605 {"nodeps", no_argument
, 0, 'd'},
606 {"deps", no_argument
, 0, 'd'},
607 {"explicit", no_argument
, 0, 'e'},
608 {"groups", no_argument
, 0, 'g'},
609 {"help", no_argument
, 0, 'h'},
610 {"info", no_argument
, 0, 'i'},
611 {"check", no_argument
, 0, 'k'},
612 {"list", no_argument
, 0, 'l'},
613 {"foreign", no_argument
, 0, 'm'},
614 {"native", no_argument
, 0, 'n'},
615 {"nosave", no_argument
, 0, 'n'},
616 {"owns", no_argument
, 0, 'o'},
617 {"file", no_argument
, 0, 'p'},
618 {"print", no_argument
, 0, 'p'},
619 {"quiet", no_argument
, 0, 'q'},
620 {"root", required_argument
, 0, 'r'},
621 {"recursive", no_argument
, 0, 's'},
622 {"search", no_argument
, 0, 's'},
623 {"unrequired", no_argument
, 0, 't'},
624 {"upgrades", no_argument
, 0, 'u'},
625 {"sysupgrade", no_argument
, 0, 'u'},
626 {"unneeded", no_argument
, 0, 'u'},
627 {"verbose", no_argument
, 0, 'v'},
628 {"downloadonly", no_argument
, 0, 'w'},
629 {"refresh", no_argument
, 0, 'y'},
631 {"noconfirm", no_argument
, 0, OP_NOCONFIRM
},
632 {"config", required_argument
, 0, OP_CONFIG
},
633 {"ignore", required_argument
, 0, OP_IGNORE
},
634 {"debug", optional_argument
, 0, OP_DEBUG
},
635 {"force", no_argument
, 0, OP_FORCE
},
636 {"noprogressbar", no_argument
, 0, OP_NOPROGRESSBAR
},
637 {"noscriptlet", no_argument
, 0, OP_NOSCRIPTLET
},
638 {"ask", required_argument
, 0, OP_ASK
},
639 {"cachedir", required_argument
, 0, OP_CACHEDIR
},
640 {"asdeps", no_argument
, 0, OP_ASDEPS
},
641 {"logfile", required_argument
, 0, OP_LOGFILE
},
642 {"ignoregroup", required_argument
, 0, OP_IGNOREGROUP
},
643 {"needed", no_argument
, 0, OP_NEEDED
},
644 {"asexplicit", no_argument
, 0, OP_ASEXPLICIT
},
645 {"arch", required_argument
, 0, OP_ARCH
},
646 {"print-format", required_argument
, 0, OP_PRINTFORMAT
},
647 {"gpgdir", required_argument
, 0, OP_GPGDIR
},
648 {"dbonly", no_argument
, 0, OP_DBONLY
},
649 {"color", required_argument
, 0, OP_COLOR
},
653 /* parse operation */
654 while((opt
= getopt_long(argc
, argv
, optstring
, opts
, &option_index
))) {
657 } else if(opt
== 0) {
659 } else if(opt
== '?') {
660 /* unknown option, getopt printed an error */
666 if(config
->op
== 0) {
667 pm_printf(ALPM_LOG_ERROR
, _("only one operation may be used at a time\n"));
671 usage(config
->op
, mbasename(argv
[0]));
674 if(config
->version
) {
679 /* parse all other options */
681 while((opt
= getopt_long(argc
, argv
, optstring
, opts
, &option_index
))) {
684 } else if(opt
== 0) {
686 } else if(opt
== '?') {
687 /* this should have failed during first pass already */
689 } else if(parsearg_op(opt
, 1) == 0) {
690 /* opt is an operation */
696 result
= parsearg_database(opt
);
699 result
= parsearg_query(opt
);
702 result
= parsearg_remove(opt
);
705 result
= parsearg_sync(opt
);
708 result
= parsearg_upgrade(opt
);
719 /* fall back to global options */
720 result
= parsearg_global(opt
);
722 /* global option parsing failed, abort */
723 pm_printf(ALPM_LOG_ERROR
, _("invalid option\n"));
728 while(optind
< argc
) {
729 /* add the target to our target array */
730 pm_targets
= alpm_list_add(pm_targets
, strdup(argv
[optind
]));
737 /** Print command line to logfile.
741 static void cl_to_log(int argc
, char *argv
[])
745 for(i
= 0; i
< argc
; i
++) {
746 size
+= strlen(argv
[i
]) + 1;
751 char *cl_text
= malloc(size
);
756 for(i
= 0; i
< argc
- 1; i
++) {
758 p
+= strlen(argv
[i
]);
762 alpm_logaction(config
->handle
, PACMAN_CALLER_PREFIX
,
763 "Running '%s'\n", cl_text
);
770 * @return A return code indicating success, failure, etc.
772 int main(int argc
, char *argv
[])
776 struct sigaction new_action
, old_action
;
777 const int signals
[] = { SIGHUP
, SIGINT
, SIGTERM
, SIGSEGV
};
778 uid_t myuid
= geteuid();
780 /* Set signal handlers */
781 /* Set up the structure to specify the new action. */
782 new_action
.sa_handler
= handler
;
783 sigemptyset(&new_action
.sa_mask
);
784 new_action
.sa_flags
= 0;
786 /* assign our handler to any signals we care about */
787 for(i
= 0; i
< sizeof(signals
) / sizeof(signals
[0]); i
++) {
788 int signal
= signals
[i
];
789 sigaction(signal
, NULL
, &old_action
);
790 if(old_action
.sa_handler
!= SIG_IGN
) {
791 sigaction(signal
, &new_action
, NULL
);
796 #if defined(ENABLE_NLS)
800 /* set user agent for downloading */
803 /* init config data */
804 config
= config_new();
806 /* disable progressbar if the output is redirected */
807 if(!isatty(fileno(stdout
))) {
808 config
->noprogressbar
= 1;
811 /* Priority of options:
814 * 3. compiled-in defaults
815 * However, we have to parse the command line first because a config file
816 * location can be specified here, so we need to make sure we prefer these
817 * options over the config file coming second.
820 /* parse the command line */
821 ret
= parseargs(argc
, argv
);
826 /* we support reading targets from stdin if a cmdline parameter is '-' */
827 if(alpm_list_find_str(pm_targets
, "-")) {
828 if(!isatty(fileno(stdin
))) {
829 int target_found
= 0;
830 size_t current_size
= PATH_MAX
;
831 char *vdata
, *line
= malloc(current_size
);
833 /* remove the '-' from the list */
834 pm_targets
= alpm_list_remove_str(pm_targets
, "-", &vdata
);
838 while((line
[i
] = (char)fgetc(stdin
)) != EOF
) {
839 if(isspace((unsigned char)line
[i
])) {
840 /* avoid adding zero length arg when multiple spaces separate args */
843 pm_targets
= alpm_list_add(pm_targets
, strdup(line
));
849 /* we may be at the end of our allocated buffer now */
850 if(i
>= current_size
) {
851 char *new = realloc(line
, current_size
* 2);
864 /* check for memory exhaustion */
866 pm_printf(ALPM_LOG_ERROR
, _("memory exhausted in argument parsing\n"));
867 cleanup(EXIT_FAILURE
);
870 /* end of stream -- check for data still in line buffer */
873 pm_targets
= alpm_list_add(pm_targets
, strdup(line
));
877 if(!freopen(ctermid(NULL
), "r", stdin
)) {
878 pm_printf(ALPM_LOG_ERROR
, _("failed to reopen stdin for reading: (%s)\n"),
883 pm_printf(ALPM_LOG_ERROR
, _("argument '-' specified with empty stdin\n"));
887 /* do not read stdin from terminal */
888 pm_printf(ALPM_LOG_ERROR
, _("argument '-' specified without input on stdin\n"));
893 pm_printf(ALPM_LOG_DEBUG
, "pacman v%s - libalpm v%s\n", PACKAGE_VERSION
, alpm_version());
895 /* parse the config file */
896 ret
= parseconfig(config
->configfile
);
901 /* noask is meant to be non-interactive */
903 config
->noconfirm
= 1;
906 /* set up the print operations */
907 if(config
->print
&& !config
->op_s_clean
) {
908 config
->noconfirm
= 1;
909 config
->flags
|= ALPM_TRANS_FLAG_NOCONFLICTS
;
910 config
->flags
|= ALPM_TRANS_FLAG_NOLOCK
;
911 /* Display only errors */
912 config
->logmask
&= ~ALPM_LOG_WARNING
;
915 /* check if we have sufficient permission for the requested operation */
916 if(myuid
> 0 && needs_root()) {
917 pm_printf(ALPM_LOG_ERROR
, _("you cannot perform this operation unless you are root.\n"));
918 cleanup(EXIT_FAILURE
);
921 if(config
->verbose
> 0) {
923 printf("Root : %s\n", alpm_option_get_root(config
->handle
));
924 printf("Conf File : %s\n", config
->configfile
);
925 printf("DB Path : %s\n", alpm_option_get_dbpath(config
->handle
));
926 printf("Cache Dirs: ");
927 for(j
= alpm_option_get_cachedirs(config
->handle
); j
; j
= alpm_list_next(j
)) {
928 printf("%s ", (const char *)j
->data
);
931 printf("Lock File : %s\n", alpm_option_get_lockfile(config
->handle
));
932 printf("Log File : %s\n", alpm_option_get_logfile(config
->handle
));
933 printf("GPG Dir : %s\n", alpm_option_get_gpgdir(config
->handle
));
934 list_display("Targets :", pm_targets
, 0);
937 /* Log command line */
939 cl_to_log(argc
, argv
);
942 /* start the requested operation */
945 ret
= pacman_database(pm_targets
);
948 ret
= pacman_remove(pm_targets
);
951 ret
= pacman_upgrade(pm_targets
);
954 ret
= pacman_query(pm_targets
);
957 ret
= pacman_sync(pm_targets
);
960 ret
= pacman_deptest(pm_targets
);
963 pm_printf(ALPM_LOG_ERROR
, _("no operation specified (use -h for help)\n"));
972 /* vim: set ts=2 sw=2 noet: */