Пример #1
0
static int _parse_options(const char *key, char *value,
		const char *file, int linenum)
{
	if(value == NULL) {
		/* options without settings */
		if(strcmp(key, "UseSyslog") == 0) {
			config->usesyslog = 1;
			pm_printf(ALPM_LOG_DEBUG, "config: usesyslog\n");
		} else if(strcmp(key, "ILoveCandy") == 0) {
			config->chomp = 1;
			pm_printf(ALPM_LOG_DEBUG, "config: chomp\n");
		} else if(strcmp(key, "VerbosePkgLists") == 0) {
			config->verbosepkglists = 1;
			pm_printf(ALPM_LOG_DEBUG, "config: verbosepkglists\n");
		} else if(strcmp(key, "UseDelta") == 0) {
			config->deltaratio = 0.7;
			pm_printf(ALPM_LOG_DEBUG, "config: usedelta (default 0.7)\n");
		} else if(strcmp(key, "TotalDownload") == 0) {
			config->totaldownload = 1;
			pm_printf(ALPM_LOG_DEBUG, "config: totaldownload\n");
		} else if(strcmp(key, "CheckSpace") == 0) {
			config->checkspace = 1;
		} else if(strcmp(key, "Color") == 0) {
			if(config->color == PM_COLOR_UNSET) {
				config->color = isatty(fileno(stdout)) ? PM_COLOR_ON : PM_COLOR_OFF;
				enable_colors(config->color);
			}
		} else {
			pm_printf(ALPM_LOG_WARNING,
					_("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
					file, linenum, key, "options");
		}
	} else {
		/* options with settings */
		if(strcmp(key, "NoUpgrade") == 0) {
			setrepeatingoption(value, "NoUpgrade", &(config->noupgrade));
		} else if(strcmp(key, "NoExtract") == 0) {
			setrepeatingoption(value, "NoExtract", &(config->noextract));
		} else if(strcmp(key, "IgnorePkg") == 0) {
			setrepeatingoption(value, "IgnorePkg", &(config->ignorepkg));
		} else if(strcmp(key, "IgnoreGroup") == 0) {
			setrepeatingoption(value, "IgnoreGroup", &(config->ignoregrp));
		} else if(strcmp(key, "HoldPkg") == 0) {
			setrepeatingoption(value, "HoldPkg", &(config->holdpkg));
		} else if(strcmp(key, "CacheDir") == 0) {
			setrepeatingoption(value, "CacheDir", &(config->cachedirs));
		} else if(strcmp(key, "Architecture") == 0) {
			if(!config->arch) {
				config_set_arch(value);
			}
		} else if(strcmp(key, "UseDelta") == 0) {
			double ratio;
			char *endptr;
			const char *oldlocale;

			/* set the locale to 'C' for consistent decimal parsing (0.7 and never
			 * 0,7) from config files, then restore old setting when we are done */
			oldlocale = setlocale(LC_NUMERIC, NULL);
			setlocale(LC_NUMERIC, "C");
			ratio = strtod(value, &endptr);
			setlocale(LC_NUMERIC, oldlocale);

			if(*endptr != '\0' || ratio < 0.0 || ratio > 2.0) {
				pm_printf(ALPM_LOG_ERROR,
						_("config file %s, line %d: invalid value for '%s' : '%s'\n"),
						file, linenum, "UseDelta", value);
				return 1;
			}
			config->deltaratio = ratio;
			pm_printf(ALPM_LOG_DEBUG, "config: usedelta = %f\n", ratio);
		} else if(strcmp(key, "DBPath") == 0) {
			/* don't overwrite a path specified on the command line */
			if(!config->dbpath) {
				config->dbpath = strdup(value);
				pm_printf(ALPM_LOG_DEBUG, "config: dbpath: %s\n", value);
			}
		} else if(strcmp(key, "RootDir") == 0) {
			/* don't overwrite a path specified on the command line */
			if(!config->rootdir) {
				config->rootdir = strdup(value);
				pm_printf(ALPM_LOG_DEBUG, "config: rootdir: %s\n", value);
			}
		} else if(strcmp(key, "GPGDir") == 0) {
			if(!config->gpgdir) {
				config->gpgdir = strdup(value);
				pm_printf(ALPM_LOG_DEBUG, "config: gpgdir: %s\n", value);
			}
		} else if(strcmp(key, "LogFile") == 0) {
			if(!config->logfile) {
				config->logfile = strdup(value);
				pm_printf(ALPM_LOG_DEBUG, "config: logfile: %s\n", value);
			}
		} else if(strcmp(key, "XferCommand") == 0) {
			config->xfercommand = strdup(value);
			pm_printf(ALPM_LOG_DEBUG, "config: xfercommand: %s\n", value);
		} else if(strcmp(key, "CleanMethod") == 0) {
			alpm_list_t *methods = NULL;
			setrepeatingoption(value, "CleanMethod", &methods);
			if(process_cleanmethods(methods, file, linenum)) {
				FREELIST(methods);
				return 1;
			}
			FREELIST(methods);
		} else if(strcmp(key, "SigLevel") == 0) {
			alpm_list_t *values = NULL;
			setrepeatingoption(value, "SigLevel", &values);
			if(process_siglevel(values, &config->siglevel,
						&config->siglevel_mask, file, linenum)) {
				FREELIST(values);
				return 1;
			}
			FREELIST(values);
		} else if(strcmp(key, "LocalFileSigLevel") == 0) {
			alpm_list_t *values = NULL;
			setrepeatingoption(value, "LocalFileSigLevel", &values);
			if(process_siglevel(values, &config->localfilesiglevel,
						&config->localfilesiglevel_mask, file, linenum)) {
				FREELIST(values);
				return 1;
			}
			FREELIST(values);
		} else if(strcmp(key, "RemoteFileSigLevel") == 0) {
			alpm_list_t *values = NULL;
			setrepeatingoption(value, "RemoteFileSigLevel", &values);
			if(process_siglevel(values, &config->remotefilesiglevel,
						&config->remotefilesiglevel_mask, file, linenum)) {
				FREELIST(values);
				return 1;
			}
			FREELIST(values);
		} else {
			pm_printf(ALPM_LOG_WARNING,
					_("config file %s, line %d: directive '%s' in section '%s' not recognized.\n"),
					file, linenum, key, "options");
		}

	}
	return 0;
}
Пример #2
0
int main (int argc, char **argv)
{
	int ret=0, i;
	int need=0, given=0, cycle_db=0, db_order=0;
	alpm_list_t *t;

	struct sigaction a;
	init_config (argv[0]);
	a.sa_handler = handler;
	sigemptyset(&a.sa_mask);
	a.sa_flags = 0;
	sigaction(SIGINT, &a, NULL);
	sigaction(SIGTERM, &a, NULL);

	int opt;
	int opt_index=0;
	static struct option opts[] =
	{
		{"query",      no_argument,       0, 'Q'},
		{"sync",       no_argument,       0, 'S'},
		{"dbpath",     required_argument, 0, 'b'},
		{"deps",       no_argument,       0, 'd'},
		{"explicit",   no_argument,       0, 'e'},
		{"groups",     no_argument,       0, 'g'},
		{"help",       no_argument,       0, 'h'},
		{"info",       no_argument,       0, 'i'},
		{"list",       no_argument,       0, 'l'},
		{"foreign",    no_argument,       0, 'm'},
		{"native",     no_argument,       0, 'n'},
		{"file",       no_argument,       0, 'p'},
		{"quiet",      no_argument,       0, 'q'},
		{"root",       required_argument, 0, 'r'},
		{"search",     no_argument,       0, 's'},
		{"unrequired", no_argument,       0, 't'},
		{"upgrades",   no_argument,       0, 'u'},
		{"config",     required_argument, 0, 'c'},
		{"just-one",   no_argument,       0, '1'},
		{"aur",        no_argument,       0, 'A'},
		{"escape",     no_argument,       0, 'x'},
		{"format",     required_argument, 0, 'f'},
		{"list-repo",  required_argument, 0, 'L'},
		{"query-type", required_argument, 0, 1000},
		{"csep",       required_argument, 0, 1001},
		{"delimiter",  required_argument, 0, 1001},
		{"sort",       required_argument, 0, 1002},
		{"nocolor",    no_argument,       0, 1003},
		{"number",     no_argument,       0, 1004},
		{"get-res",    no_argument,       0, 1005},
		{"show-size",  no_argument,       0, 1006},
		{"aur-url",    required_argument, 0, 1007},
		{"insecure",   no_argument,       0, 1008},
		{"qdepends",   no_argument,       0, 1009},
		{"qconflicts", no_argument,       0, 1010},
		{"qprovides",  no_argument,       0, 1011},
		{"qreplaces",  no_argument,       0, 1012},
		{"qrequires",  no_argument,       0, 1013},
		{"color",      no_argument,       0, 1014},
		{"rsort",      required_argument, 0, 1015},
		{"version",    no_argument,       0, 'v'},

		{0, 0, 0, 0}
	};

	
	while ((opt = getopt_long (argc, argv, "1Ac:b:def:ghiLlmnpQqr:Sstuvx", opts, &opt_index)) != -1)
	{
		switch (opt) 
		{
			case '1':
				config.just_one = 1;
				break;
			case 'A':
				if (config.aur) break;
				config.aur = ++db_order;
				given |= N_DB;
				break;
			case 'c':
				FREE (config.configfile);
				config.configfile = strndup (optarg, PATH_MAX);
				break;
			case 'b':
				FREE (config.dbpath);
				config.dbpath = strndup (optarg, PATH_MAX);
				break;
			case 'd':
				config.filter |= F_DEPS;
				break;
			case 'e':
				config.filter |= F_EXPLICIT;
				break;
			case 'x':
				config.escape = 1;
				break;
			case 'f':
				config.custom_out = 1;
				strncpy (config.format_out, optarg, PATH_MAX);
				format_str (config.format_out);
				break;
			case 'g':
				if (config.op) break;
				config.op = OP_LIST_GROUP;
				config.filter |= F_GROUP;
				cycle_db = 1;
				break;
			case 'i':
				if (config.op)
				{
					if (config.op == OP_INFO) config.op = OP_INFO_P;
					break;
				}
				config.op = OP_INFO;
				need |= N_TARGET | N_DB;
				break;
			case 'L':
				config.list = 1;
				break;
			case 'l':
				if (config.op) break;
				config.op = OP_LIST_REPO;
				need |= N_DB;
				cycle_db = 1;
				break;
			case 'm':
				config.filter |= F_FOREIGN;
				break;
			case 'n':
				config.filter |= F_NATIVE;
				break;
			case 'p':
				config.is_file = 1;
				need |= N_TARGET;
				break;
			case 'Q':
				if (config.db_local) break;
				config.db_local = ++db_order;
				given |= N_DB;
				break;
			case 'q':
				config.quiet = 1;
				break;
			case 'r':
				FREE (config.rootdir);
				config.rootdir = strndup (optarg, PATH_MAX);
				break;
			case 's':
				if (config.op) break;
				config.op = OP_SEARCH;
				need |= N_DB;
				cycle_db = 1;
				break;
			case 'S':
				if (config.db_sync) break;
				config.db_sync = ++db_order;
				given |= N_DB;
				break;
			case 't':
				config.filter |= F_UNREQUIRED;
				break;
			case 1000: /* --query-type */
				if (config.op) break;
				config.op = OP_QUERY;
				if (strcmp (optarg, "depends")==0)
					config.query = OP_Q_DEPENDS;
				else if (strcmp (optarg, "conflicts")==0)
					config.query = OP_Q_CONFLICTS;
				else if (strcmp (optarg, "provides")==0)
					config.query = OP_Q_PROVIDES;
				else if (strcmp (optarg, "replaces")==0)
					config.query = OP_Q_REPLACES;
				need |= N_TARGET | N_DB;
				break;
			case 1009: /* --qdepends */
				SETQUERY (OP_Q_DEPENDS); break;
			case 1010: /* --qconflicts */
				SETQUERY (OP_Q_CONFLICTS); break;
			case 1011: /* --qprovides */
				SETQUERY (OP_Q_PROVIDES); break;
			case 1012: /* --qreplaces */
				SETQUERY (OP_Q_REPLACES); break;
			case 1013: /* --qrequires */
				SETQUERY (OP_Q_REQUIRES); break;
			case 1001: /* --delimiter */
				strncpy (config.delimiter, optarg, SEP_LEN);
				format_str (config.delimiter);
				break;
			case 1015: /* --rsort */
				config.rsort = 1;
			case 1002: /* --sort */
				if (strlen (optarg) > 1) {
					if (strcmp (optarg, "name")==0)
						config.sort = S_NAME;
					else if (strcmp (optarg, "vote")==0)
						config.sort = S_VOTE;
					else if (strcmp (optarg, "date")==0)
						config.sort = S_IDATE;
					else if (strcmp (optarg, "size")==0)
						config.sort = S_ISIZE;
				} else
					config.sort = optarg[0];
				break;
			case 1003: /* --nocolor */
				config.colors=0;
				break;
			case 1004: /* --number */
				config.numbering = 1;
				break;
			case 1005: /* --get-res */
				if (dup2(FD_RES, FD_RES) == FD_RES)
					config.get_res = 1;
				break;
			case 1006: /* --show-size */
				config.show_size = 1;
				break;
			case 1007: /* --aur-url */
				FREE (config.aur_url);
				config.aur_url = strdup (optarg);
				break;
			case 1008: /* --insecure */
				config.insecure = 1;
				break;
			case 1014: /* --color */
				config.colors=1;
				break;
			case 'u':
				config.filter |= F_UPGRADES;
				break;
			case 'v':
				version(); break;
			case 'h': usage (0); break;
			default: /* '?' */
				usage (1);
		}
	}
	if (config.list)
	{
		/* -L displays respository list and exits. */
		alpm_list_t *dbs = get_db_sync ();
		if (dbs)
		{
			for(t = dbs; t; t = alpm_list_next(t))
				printf ("%s\n", (char *)t->data);
			FREELIST (dbs);
		}
		cleanup (0);
	}
	if (!config.custom_out)
	{
		if (config.colors)
			color_init();
#if defined(HAVE_GETTEXT) && defined(ENABLE_NLS)
		/* TODO: specific package-query locale ? */
		setlocale (LC_ALL, "");
		bindtextdomain ("yaourt", LOCALEDIR);
		textdomain ("yaourt");
#endif
	}
	if ((need & N_DB) && !(given & N_DB))
	{
		fprintf(stderr, "search or information must have database target (-{Q,S,A}).\n");
		exit(1);
	}
	for (i = optind; i < argc; i++)
	{
		if (!config.just_one ||
		    !alpm_list_find_str (targets, argv[i]))
			targets = alpm_list_add(targets, strdup(argv[i]));
	}
	if (i!=optind) 
	{
		given |= N_TARGET;
	}
	if ((need & N_TARGET) && !(given & N_TARGET))
	{
		fprintf(stderr, "no targets specified.\n");
		usage(1);
	}
	if (targets == NULL)
	{
		if (config.op == OP_SEARCH)	config.op = OP_LIST_REPO_S;
	}
	else if (!config.op && (given & N_DB)) /* Show info by default */
		config.op = OP_INFO;
	// init_db_sync initializes alpm after parsing [options]
	if (!init_db_sync ()) cleanup(1);
	if (config.is_file)
	{
		for(t = targets; t; t = alpm_list_next(t))
		{
			alpm_pkg_t *pkg=NULL;
			const char *filename = t->data;
			if (alpm_pkg_load (config.handle, filename, 0, ALPM_SIG_USE_DEFAULT, &pkg)!=0 || pkg==NULL)
			{
				fprintf(stderr, "unable to read %s.\n", filename);
				continue;
			}
			print_package (filename, pkg, alpm_pkg_get_str);
			ret++;
		}
		cleanup(!ret);
	}

	if  (cycle_db || targets)
	{
		for (i=1; i<=db_order && (cycle_db || targets); i++)
		{
			/*printf ("%d, aur %d, local %d, sync %d\n", i, config.aur, config.db_local, config.db_sync);*/
			if (config.db_sync == i)
			{
				for(t = alpm_get_syncdbs(config.handle); t; t = alpm_list_next(t))
					ret += deal_db (t->data);
				if (!ret && config.op == OP_INFO_P)
				{
					config.op = OP_QUERY;
					config.query = OP_Q_PROVIDES;
					for(t = alpm_get_syncdbs(config.handle); t; t = alpm_list_next(t))
						ret += deal_db (t->data);
					config.op = OP_INFO_P;
				}
			}
			else if (config.db_local == i)
				ret += deal_db (alpm_get_localdb(config.handle));
			else if (config.aur == i)
				switch (config.op)
				{
					case OP_INFO:
					case OP_INFO_P: ret += aur_info (&targets); break;
					case OP_SEARCH: ret += aur_search (targets); break;
					default: break;
				}
		}
	}
	else if (!config.aur && config.db_local)
		ret += alpm_search_local (config.filter, NULL, NULL);
	else if (config.aur && !(given & N_TARGET))
	{
		if (config.filter & F_FOREIGN)
		{
			/* -Am */
			config.aur_foreign = 1;
			config.just_one = 1;
			alpm_search_local (config.filter, "%n", &targets);
			ret += aur_info (&targets);
			if (config.db_local)
				/* -AQm */
				ret += search_pkg_by_name (alpm_get_localdb(config.handle), &targets);
		}
		else if (config.filter & F_UPGRADES)
		{
			/* -Au */
			config.aur_upgrades = 1;
			if (config.db_local)
				/* -AQu */
				ret += alpm_search_local (config.filter, NULL, NULL);
			alpm_search_local (F_FOREIGN | (config.filter & ~F_UPGRADES), "%n>%v", &targets);
			ret += aur_info (&targets);
		}
	}

	show_results();

	/* Some cleanups */
	cleanup(!ret);
	return 0;
}