From 1e15a0ccafbc5b4ed8074c5b47c5247fa35bcab4 Mon Sep 17 00:00:00 2001 From: Sotiris Papatheodorou Date: Sun, 18 Aug 2024 00:18:35 +0300 Subject: [PATCH] Fix config file overriding environment variable The convention for most programs is that command line options override environment variables which in turn override config files. Removed the test for empty as the option should be overridden unconditionally if it exists in the environment. In the old implementation, the host was set from the environment variable in the first pass and then overridden by the config file in the second pass. --- NEWS | 1 + src/Main.cxx | 3 +++ src/Options.cxx | 6 +++++- src/Options.hxx | 2 ++ 4 files changed, 11 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 989ca9ab..741a38b6 100644 --- a/NEWS +++ b/NEWS @@ -2,6 +2,7 @@ ncmpc 0.50 - not yet released * build: require Meson 0.56 * lyrics/musixmatch: add new lyrics extension * lyrics/google: fix partial loading of lyrics +* fix config file overriding MPD_HOST environment variable ncmpc 0.49 - (2023-08-04) * fix UI freeze if lyrics plugin is stuck diff --git a/src/Main.cxx b/src/Main.cxx index 581e5f69..48aa2282 100644 --- a/src/Main.cxx +++ b/src/Main.cxx @@ -301,6 +301,9 @@ try { GetGlobalKeyBindings().Check(nullptr, 0); #endif + /* set options from environment variables */ + options_env(); + /* parse command line options - 2 pass */ options_parse(argc, argv); diff --git a/src/Options.cxx b/src/Options.cxx index 1671c7c3..5c276414 100644 --- a/src/Options.cxx +++ b/src/Options.cxx @@ -320,7 +320,11 @@ options_parse(int argc, const char *argv[]) handle_option(opt->shortopt, nullptr); else if (opt && opt->argument) option_error(ERROR_MISSING_ARGUMENT, opt->longopt, opt->argument); +} - if (options.host.empty() && getenv("MPD_HOST")) +void +options_env() +{ + if (getenv("MPD_HOST")) options.host = getenv("MPD_HOST"); } diff --git a/src/Options.hxx b/src/Options.hxx index 4df5977e..60d4265a 100644 --- a/src/Options.hxx +++ b/src/Options.hxx @@ -97,4 +97,6 @@ extern Options options; void options_parse(int argc, const char **argv); +void options_env(); + #endif