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

Message ID 20190910183203.204087-1-wsdmatty@gmail.com
State New
Headers show
Series
  • [pacman-dev] pacman/pacman-conf: -i/--is-set switch, returns 0 if option set
Related show

Commit Message

Matthew Sexton Sept. 10, 2019, 6:32 p.m. UTC
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@gmail.com>
---
 src/pacman/pacman-conf.c | 44 ++++++++++++++++++++++++++++++++++++++--
 1 file changed, 42 insertions(+), 2 deletions(-)

Comments

Matthew Sexton Sept. 10, 2019, 11:29 p.m. UTC | #1
On Tuesday, September 10, 2019 2:32:03 PM EDT you wrote:
> 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@gmail.com>
> ---
>  src/pacman/pacman-conf.c | 44 ++++++++++++++++++++++++++++++++++++++--
>  1 file changed, 42 insertions(+), 2 deletions(-)

Note: This patch is based off of my previous patch adding gettext localisation 
to pacman-conf. If attempting to apply this patch without that one first, the 
patch will most likely not apply.
Eli Schwartz Sept. 11, 2019, 12:29 a.m. UTC | #2
On 9/10/19 2:32 PM, Matthew Sexton wrote:
> Returns 1 if option not set. Returns 1 with error to stderr if
> flag does not exist. Case insensitive.

As discussed on IRC, it seems like this may be easier and more reliable
to achieve by using exit statuses. Instead of exiting 1 on a missing
flag, and returning 0 otherwise while printing out the key (or not)...

Maybe emulate e.g. grep. Return 2 if an error occurred, i.e. missing
option, and then even without -i, we can return 1 if the option is not set.

Patch

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();
 	}