summaryrefslogtreecommitdiff
path: root/src/pacman/pacman.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/pacman/pacman.c')
-rw-r--r--src/pacman/pacman.c503
1 files changed, 340 insertions, 163 deletions
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index 78407d67..e078aaba 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -26,6 +26,7 @@
#define PACKAGE_VERSION GIT_VERSION
#endif
+#include <ctype.h> /* isspace */
#include <stdlib.h> /* atoi */
#include <stdio.h>
#include <limits.h>
@@ -352,6 +353,243 @@ static void setlibpaths(void)
#define check_optarg() if(!optarg) { return(1); }
+typedef void (*fn_add) (const char *s);
+
+static int parsearg_util_addlist(fn_add fn)
+{
+ alpm_list_t *list = NULL, *item = NULL; /* lists for splitting strings */
+
+ check_optarg();
+ list = strsplit(optarg, ',');
+ for(item = list; item; item = alpm_list_next(item)) {
+ fn((char *)alpm_list_getdata(item));
+ }
+ FREELIST(list);
+ return(0);
+}
+
+/** Helper function for parsing operation from command-line arguments.
+ * @param opt Keycode returned by getopt_long
+ * @param dryrun If nonzero, application state is NOT changed
+ * @return 0 if opt was handled, 1 if it was not handled
+ */
+static int parsearg_op(int opt, int dryrun)
+{
+ switch(opt) {
+ /* operations */
+ case 'D':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DATABASE); break;
+ case 'Q':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
+ case 'R':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
+ case 'S':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
+ case 'T':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
+ case 'U':
+ if(dryrun) break;
+ config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
+ case 'V':
+ if(dryrun) break;
+ config->version = 1; break;
+ case 'h':
+ if(dryrun) break;
+ config->help = 1; break;
+ default:
+ return(1);
+ }
+ return(0);
+}
+
+/** Helper functions for parsing command-line arguments.
+ * @param opt Keycode returned by getopt_long
+ * @return 0 on success, 1 on failure
+ */
+static int parsearg_global(int opt)
+{
+ switch(opt) {
+ case OP_ARCH: check_optarg(); setarch(optarg); break;
+ case OP_ASK:
+ check_optarg();
+ config->noask = 1;
+ config->ask = atoi(optarg);
+ break;
+ case OP_CACHEDIR:
+ check_optarg();
+ if(alpm_option_add_cachedir(optarg) != 0) {
+ pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
+ optarg, alpm_strerrorlast());
+ return(1);
+ }
+ break;
+ case OP_CONFIG:
+ check_optarg();
+ if(config->configfile) {
+ free(config->configfile);
+ }
+ config->configfile = strndup(optarg, PATH_MAX);
+ break;
+ case OP_DEBUG:
+ /* debug levels are made more 'human readable' than using a raw logmask
+ * here, error and warning are set in config_new, though perhaps a
+ * --quiet option will remove these later */
+ if(optarg) {
+ unsigned short debug = atoi(optarg);
+ switch(debug) {
+ case 2:
+ config->logmask |= PM_LOG_FUNCTION; /* fall through */
+ case 1:
+ config->logmask |= PM_LOG_DEBUG;
+ break;
+ default:
+ pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
+ optarg);
+ return(1);
+ }
+ } else {
+ config->logmask |= PM_LOG_DEBUG;
+ }
+ /* progress bars get wonky with debug on, shut them off */
+ config->noprogressbar = 1;
+ break;
+ case OP_LOGFILE:
+ check_optarg();
+ config->logfile = strndup(optarg, PATH_MAX);
+ break;
+ case OP_NOCONFIRM: config->noconfirm = 1; break;
+ case 'b':
+ check_optarg();
+ config->dbpath = strdup(optarg);
+ break;
+ case 'r': check_optarg(); config->rootdir = strdup(optarg); break;
+ case 'v': (config->verbose)++; break;
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_database(int opt)
+{
+ switch(opt) {
+ case OP_ASDEPS: config->flags |= PM_TRANS_FLAG_ALLDEPS; break;
+ case OP_ASEXPLICIT: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break;
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_query(int opt)
+{
+ switch(opt) {
+ case 'c': config->op_q_changelog = 1; break;
+ case 'd': config->op_q_deps = 1; break;
+ case 'e': config->op_q_explicit = 1; break;
+ case 'g': (config->group)++; break;
+ case 'i': (config->op_q_info)++; break;
+ case 'k': config->op_q_check = 1; break;
+ case 'l': config->op_q_list = 1; break;
+ case 'm': config->op_q_foreign = 1; break;
+ case 'o': config->op_q_owns = 1; break;
+ case 'p': config->op_q_isfile = 1; break;
+ case 'q': config->quiet = 1; break;
+ case 's': config->op_q_search = 1; break;
+ case 't': config->op_q_unrequired = 1; break;
+ case 'u': config->op_q_upgrade = 1; break;
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_remove(int opt)
+{
+ switch(opt) {
+ case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
+ case OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+ case OP_PRINTFORMAT: check_optarg(); config->print_format = strdup(optarg); break;
+ case 'c': config->flags |= PM_TRANS_FLAG_CASCADE; break;
+ case 'd': config->flags |= PM_TRANS_FLAG_NODEPS; break;
+ case 'k': config->flags |= PM_TRANS_FLAG_DBONLY; break;
+ case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
+ case 'p': config->print = 1; break;
+ case 's':
+ if(config->flags & PM_TRANS_FLAG_RECURSE) {
+ config->flags |= PM_TRANS_FLAG_RECURSEALL;
+ } else {
+ config->flags |= PM_TRANS_FLAG_RECURSE;
+ }
+ break;
+ case 'u': config->flags |= PM_TRANS_FLAG_UNNEEDED; break;
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_sync(int opt)
+{
+ switch(opt) {
+ case OP_ASDEPS: config->flags |= PM_TRANS_FLAG_ALLDEPS; break;
+ case OP_ASEXPLICIT: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break;
+ case OP_IGNORE: parsearg_util_addlist(alpm_option_add_ignorepkg); break;
+ case OP_IGNOREGROUP: parsearg_util_addlist(alpm_option_add_ignoregrp); break;
+ case OP_NEEDED: config->flags |= PM_TRANS_FLAG_NEEDED; break;
+ case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
+ case OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+ case OP_PRINTFORMAT: check_optarg(); config->print_format = strdup(optarg); break;
+ case 'c': (config->op_s_clean)++; break;
+ case 'd': config->flags |= PM_TRANS_FLAG_NODEPS; break;
+ case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
+ case 'g': (config->group)++; break;
+ case 'i': (config->op_s_info)++; break;
+ case 'l': config->op_q_list = 1; break;
+ case 'k': config->flags |= PM_TRANS_FLAG_DBONLY; break;
+ case 'p': config->print = 1; break;
+ case 'q': config->quiet = 1; break;
+ case 's': config->op_s_search = 1; break;
+ case 'u': (config->op_s_upgrade)++; break;
+ case 'w':
+ config->op_s_downloadonly = 1;
+ config->flags |= PM_TRANS_FLAG_DOWNLOADONLY;
+ config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
+ break;
+ case 'y': (config->op_s_sync)++; break;
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_deptest(int opt)
+{
+ switch(opt) {
+ default: return(1);
+ }
+ return(0);
+}
+
+static int parsearg_upgrade(int opt)
+{
+ switch(opt) {
+ case OP_ASDEPS: config->flags |= PM_TRANS_FLAG_ALLDEPS; break;
+ case OP_ASEXPLICIT: config->flags |= PM_TRANS_FLAG_ALLEXPLICIT; break;
+ case OP_IGNORE: parsearg_util_addlist(alpm_option_add_ignorepkg); break;
+ case OP_IGNOREGROUP: parsearg_util_addlist(alpm_option_add_ignoregrp); break;
+ case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
+ case OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
+ case OP_PRINTFORMAT: check_optarg(); config->print_format = strdup(optarg); break;
+ case 'd': config->flags |= PM_TRANS_FLAG_NODEPS; break;
+ case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
+ case 'k': config->flags |= PM_TRANS_FLAG_DBONLY; break;
+ case 'p': config->print = 1; break;
+ default: return(1);
+ }
+ return(0);
+}
+
/** Parse command-line arguments for each operation.
* @param argc argc
* @param argv argv
@@ -361,6 +599,8 @@ static int parseargs(int argc, char *argv[])
{
int opt;
int option_index = 0;
+ int result;
+ const char *optstring = "DQRSTUVb:cdefghiklmnopqr:stuvwy";
static struct option opts[] =
{
{"database", no_argument, 0, 'D'},
@@ -418,175 +658,22 @@ static int parseargs(int argc, char *argv[])
{0, 0, 0, 0}
};
- while((opt = getopt_long(argc, argv, "DQRSTUVb:cdefghiklmnopqr:stuvwy", opts, &option_index))) {
- alpm_list_t *list = NULL, *item = NULL; /* lists for splitting strings */
-
+ /* parse operation */
+ while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
if(opt < 0) {
break;
+ } else if(opt == 0) {
+ continue;
+ } else if(opt == '?') {
+ return(1);
}
- switch(opt) {
- case 0: break;
- case OP_NOCONFIRM: config->noconfirm = 1; break;
- case OP_CONFIG:
- check_optarg();
- if(config->configfile) {
- free(config->configfile);
- }
- config->configfile = strndup(optarg, PATH_MAX);
- break;
- case OP_IGNORE:
- check_optarg();
- list = strsplit(optarg, ',');
- for(item = list; item; item = alpm_list_next(item)) {
- alpm_option_add_ignorepkg((char *)alpm_list_getdata(item));
- }
- FREELIST(list);
- break;
- case OP_DEBUG:
- /* debug levels are made more 'human readable' than using a raw logmask
- * here, error and warning are set in config_new, though perhaps a
- * --quiet option will remove these later */
- if(optarg) {
- unsigned short debug = atoi(optarg);
- switch(debug) {
- case 2:
- config->logmask |= PM_LOG_FUNCTION; /* fall through */
- case 1:
- config->logmask |= PM_LOG_DEBUG;
- break;
- default:
- pm_printf(PM_LOG_ERROR, _("'%s' is not a valid debug level\n"),
- optarg);
- return(1);
- }
- } else {
- config->logmask |= PM_LOG_DEBUG;
- }
- /* progress bars get wonky with debug on, shut them off */
- config->noprogressbar = 1;
- break;
- case OP_NOPROGRESSBAR: config->noprogressbar = 1; break;
- case OP_NOSCRIPTLET: config->flags |= PM_TRANS_FLAG_NOSCRIPTLET; break;
- case OP_ASK:
- check_optarg();
- config->noask = 1;
- config->ask = atoi(optarg);
- break;
- case OP_CACHEDIR:
- check_optarg();
- if(alpm_option_add_cachedir(optarg) != 0) {
- pm_printf(PM_LOG_ERROR, _("problem adding cachedir '%s' (%s)\n"),
- optarg, alpm_strerrorlast());
- return(1);
- }
- break;
- case OP_ASDEPS:
- config->flags |= PM_TRANS_FLAG_ALLDEPS;
- break;
- case OP_LOGFILE:
- check_optarg();
- config->logfile = strndup(optarg, PATH_MAX);
- break;
- case OP_IGNOREGROUP:
- check_optarg();
- list = strsplit(optarg, ',');
- for(item = list; item; item = alpm_list_next(item)) {
- alpm_option_add_ignoregrp((char *)alpm_list_getdata(item));
- }
- FREELIST(list);
- break;
- case OP_NEEDED: config->flags |= PM_TRANS_FLAG_NEEDED; break;
- case OP_ASEXPLICIT:
- config->flags |= PM_TRANS_FLAG_ALLEXPLICIT;
- break;
- case OP_ARCH:
- check_optarg();
- setarch(optarg);
- break;
- case OP_PRINTFORMAT:
- check_optarg();
- config->print_format = strdup(optarg);
- break;
- case 'D': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DATABASE); break;
- case 'Q': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_QUERY); break;
- case 'R': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_REMOVE); break;
- case 'S': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_SYNC); break;
- case 'T': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_DEPTEST); break;
- case 'U': config->op = (config->op != PM_OP_MAIN ? 0 : PM_OP_UPGRADE); break;
- case 'V': config->version = 1; break;
- case 'b':
- check_optarg();
- config->dbpath = strdup(optarg);
- break;
- case 'c':
- (config->op_s_clean)++;
- config->flags |= PM_TRANS_FLAG_CASCADE;
- config->op_q_changelog = 1;
- break;
- case 'd':
- config->op_q_deps = 1;
- config->flags |= PM_TRANS_FLAG_NODEPS;
- break;
- case 'e':
- config->op_q_explicit = 1;
- break;
- case 'f': config->flags |= PM_TRANS_FLAG_FORCE; break;
- case 'g': (config->group)++; break;
- case 'h': config->help = 1; break;
- case 'i': (config->op_q_info)++; (config->op_s_info)++; break;
- case 'k':
- config->flags |= PM_TRANS_FLAG_DBONLY;
- config->op_q_check = 1;
- break;
- case 'l': config->op_q_list = 1; break;
- case 'm': config->op_q_foreign = 1; break;
- case 'n': config->flags |= PM_TRANS_FLAG_NOSAVE; break;
- case 'o': config->op_q_owns = 1; break;
- case 'p':
- config->op_q_isfile = 1;
- config->print = 1;
- break;
- case 'q':
- config->quiet = 1;
- break;
- case 'r':
- check_optarg();
- config->rootdir = strdup(optarg);
- break;
- case 's':
- config->op_s_search = 1;
- config->op_q_search = 1;
- if(config->flags & PM_TRANS_FLAG_RECURSE) {
- config->flags |= PM_TRANS_FLAG_RECURSEALL;
- } else {
- config->flags |= PM_TRANS_FLAG_RECURSE;
- }
- break;
- case 't':
- config->op_q_unrequired = 1;
- break;
- case 'u':
- (config->op_s_upgrade)++;
- config->op_q_upgrade = 1;
- config->flags |= PM_TRANS_FLAG_UNNEEDED;
- break;
- case 'v': (config->verbose)++; break;
- case 'w':
- config->op_s_downloadonly = 1;
- config->flags |= PM_TRANS_FLAG_DOWNLOADONLY;
- config->flags |= PM_TRANS_FLAG_NOCONFLICTS;
- break;
- case 'y': (config->op_s_sync)++; break;
- case '?': return(1);
- default: return(1);
- }
+ parsearg_op(opt, 0);
}
if(config->op == 0) {
pm_printf(PM_LOG_ERROR, _("only one operation may be used at a time\n"));
return(1);
}
-
if(config->help) {
usage(config->op, mbasename(argv[0]));
return(2);
@@ -596,6 +683,53 @@ static int parseargs(int argc, char *argv[])
return(2);
}
+ /* parse all other options */
+ optind = 1;
+ while((opt = getopt_long(argc, argv, optstring, opts, &option_index))) {
+
+ if(opt < 0) {
+ break;
+ } else if(opt == 0) {
+ continue;
+ } else if(opt == '?') {
+ return(1);
+ } else if(parsearg_op(opt, 1) == 0) { /* opt is an operation */
+ continue;
+ }
+
+ switch(config->op) {
+ case PM_OP_DATABASE:
+ result = parsearg_database(opt);
+ break;
+ case PM_OP_QUERY:
+ result = parsearg_query(opt);
+ break;
+ case PM_OP_REMOVE:
+ result = parsearg_remove(opt);
+ break;
+ case PM_OP_SYNC:
+ result = parsearg_sync(opt);
+ break;
+ case PM_OP_DEPTEST:
+ result = parsearg_deptest(opt);
+ break;
+ case PM_OP_UPGRADE:
+ result = parsearg_upgrade(opt);
+ break;
+ default:
+ return(1);
+ }
+ if(result != 0) {
+ /* operation-specific option parsing failed, fall back to global options */
+ result = parsearg_global(opt);
+ if(result != 0) {
+ /* global option parsing failed, abort */
+ pm_printf(PM_LOG_ERROR, _("invalid option\n"));
+ return(result);
+ }
+ }
+ }
+
while(optind < argc) {
/* add the target to our target array */
pm_targets = alpm_list_add(pm_targets, strdup(argv[optind]));
@@ -676,6 +810,7 @@ int download_with_xfercommand(const char *url, const char *localpath,
struct stat st;
char *parsedcmd,*tempcmd;
char cwd[PATH_MAX];
+ int restore_cwd = 0;
char *destfile, *tempfile, *filename;
if(!config->xfercommand) {
@@ -708,8 +843,14 @@ int download_with_xfercommand(const char *url, const char *localpath,
parsedcmd = strreplace(tempcmd, "%u", url);
free(tempcmd);
+ /* save the cwd so we can restore it later */
+ if(getcwd(cwd, PATH_MAX) == NULL) {
+ pm_printf(PM_LOG_ERROR, _("could not get current working directory\n"));
+ } else {
+ restore_cwd = 1;
+ }
+
/* cwd to the download directory */
- getcwd(cwd, PATH_MAX);
if(chdir(localpath)) {
pm_printf(PM_LOG_WARNING, _("could not chdir to download directory %s\n"), localpath);
ret = -1;
@@ -736,7 +877,11 @@ int download_with_xfercommand(const char *url, const char *localpath,
}
cleanup:
- chdir(cwd);
+ /* restore the old cwd if we have it */
+ if(restore_cwd && chdir(cwd) != 0) {
+ pm_printf(PM_LOG_ERROR, _("could not change directory to %s (%s)\n"), cwd, strerror(errno));
+ }
+
if(ret == -1) {
/* hack to let an user the time to cancel a download */
sleep(2);
@@ -989,7 +1134,7 @@ static int _parseconfig(const char *file, const char *givensection,
file, linenum, value);
break;
default:
- for(int gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
+ for(size_t gindex = 0; gindex < globbuf.gl_pathc; gindex++) {
pm_printf(PM_LOG_DEBUG, "config file %s, line %d: including %s\n",
file, linenum, globbuf.gl_pathv[gindex]);
_parseconfig(globbuf.gl_pathv[gindex], section, db);
@@ -1161,6 +1306,38 @@ int main(int argc, char *argv[])
cleanup(ret);
}
+ /* read package arguments from stdin if we have none yet */
+ if(!pm_targets && !isatty(fileno(stdin))) {
+ char line[PATH_MAX];
+ int i = 0;
+ while(i < PATH_MAX && (line[i] = fgetc(stdin)) != EOF) {
+ if(isspace((unsigned char)line[i])) {
+ line[i] = '\0';
+ /* avoid adding zero length arg when multiple spaces separate args */
+ if(i > 0) {
+ pm_targets = alpm_list_add(pm_targets, strdup(line));
+ i = 0;
+ }
+ } else {
+ i++;
+ }
+ }
+ /* check for buffer overflow */
+ if (i >= PATH_MAX) {
+ pm_printf(PM_LOG_ERROR, _("buffer overflow detected in arg parsing\n"));
+ cleanup(EXIT_FAILURE);
+ }
+
+ /* end of stream -- check for data still in line buffer */
+ if(i > 0) {
+ pm_targets = alpm_list_add(pm_targets, strdup(line));
+ }
+ if (!freopen(ctermid(NULL), "r", stdin)) {
+ pm_printf(PM_LOG_ERROR, _("failed to reopen stdin for reading: (%s)\n"),
+ strerror(errno));
+ }
+ }
+
/* parse the config file */
ret = parseconfig(config->configfile);
if(ret != 0) {