[pacman-dev] [PATCH] pacman/pacman-conf: -i/--is-set switch, returns 0 if option set

Matthew Sexton wsdmatty at gmail.com
Tue Sep 10 18:32:03 UTC 2019


Returns 1 if option not set. Returns 1 with error to stderr if
flag does not exist. Case insensitive.

Signed-off-by: Matthew Sexton <wsdmatty at gmail.com>
---
 src/pacman/pacman-conf.c | 44 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)

diff --git a/src/pacman/pacman-conf.c b/src/pacman/pacman-conf.c
index efc62cdd..ada4bd9f 100644
--- a/src/pacman/pacman-conf.c
+++ b/src/pacman/pacman-conf.c
@@ -25,7 +25,7 @@
 const char *myname = "pacman-conf", *myver = "1.0.0";
 
 alpm_list_t *directives = NULL;
-char sep = '\n', *repo_name = NULL;
+char sep = '\n', *repo_name = NULL, *check_name = NULL;
 const char *config_file = NULL;
 int repo_list = 0, verbose = 0;
 
@@ -47,6 +47,7 @@ static void usage(int ret)
 	fputs(_("  -r, --repo=<remote>  query options for a specific repo\n"), stream);
 	fputs(_("  -v, --verbose        always show directive names\n"), stream);
 	fputs(_("  -l, --repo-list      list configured repositories\n"), stream);
+	fputs(_("  -i, --is-set         returns 0 (successful) if config flag is set\n"), stream);
 	fputs(_("  -h, --help           display this help information\n"), stream);
 	fputs(_("  -V, --version        display version information\n"), stream);
 	cleanup();
@@ -58,7 +59,7 @@ static void parse_opts(int argc, char **argv)
 	int c;
 	config_file = CONFFILE;
 
-	const char *short_opts = "c:hlR:r:Vv";
+	const char *short_opts = "c:h:i:lR:r:Vv";
 	struct option long_opts[] = {
 		{ "config"    , required_argument , NULL , 'c' },
 		{ "rootdir"   , required_argument , NULL , 'R' },
@@ -67,6 +68,7 @@ static void parse_opts(int argc, char **argv)
 		{ "verbose"   , no_argument       , NULL , 'v' },
 		{ "help"      , no_argument       , NULL , 'h' },
 		{ "version"   , no_argument       , NULL , 'V' },
+		{ "is-set"    , required_argument , NULL , 'i' },
 		{ 0, 0, 0, 0 },
 	};
 
@@ -99,6 +101,9 @@ static void parse_opts(int argc, char **argv)
 				cleanup();
 				exit(0);
 				break;
+			case 'i':
+				check_name = optarg;
+				break;
 			case '?':
 			default:
 				usage(1);
@@ -391,6 +396,39 @@ static int list_directives(void)
 	return ret;
 }
 
+static int check_config_flags( void )
+{
+	/* 1 = flag-not-set. Regardless of whether flag exists or not */
+	int ret = 1;
+	short value;
+
+	if (strcasecmp(check_name, "Color") == 0) {
+		value = config->color;
+	} else if (strcasecmp(check_name, "UseSyslog") == 0) {
+		value = config->usesyslog;
+	} else if (strcasecmp(check_name, "TotalDownload") == 0) {
+		value = config->totaldownload;
+	} else if (strcasecmp(check_name, "CheckSpace") == 0) {
+		value = config->checkspace;
+	} else if (strcasecmp(check_name, "VerbosePkgLists") == 0) {
+		value = config->verbosepkglists;
+	} else if (strcasecmp(check_name, "DisableDownloadTimeout") == 0) {
+		value = config->disable_dl_timeout;
+	} else if (strcasecmp(check_name, "ILoveCandy") == 0) {
+		value = config->chomp;
+	} else {
+		value = -1;
+	}
+
+	if (value == -1) {
+		fprintf(stderr, _("error: unknown config option '%s'\n"), check_name);
+		ret = 1;
+	} else {
+		ret = !value;
+	}
+	return ret;
+}
+
 int main(int argc, char **argv)
 {
 	int ret = 0;
@@ -427,6 +465,8 @@ int main(int argc, char **argv)
 		list_repos();
 	} else if(repo_name) {
 		ret = list_repo_directives();
+	} else if (check_name) {
+		ret = check_config_flags();
 	} else {
 		ret = list_directives();
 	}
-- 
2.23.0


More information about the pacman-dev mailing list