From 4c510e9451cc90b1f32aae8fbe3bb14f7575618f Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Mon, 5 Aug 2024 11:40:05 +0200 Subject: [PATCH] common/common.c, include/common.h: introduce a common method for NUT programs to print_banner_once() [#2573] Signed-off-by: Jim Klimov --- common/common.c | 20 ++++++++++++++++++++ include/common.h | 12 ++++++++++++ 2 files changed, 32 insertions(+) diff --git a/common/common.c b/common/common.c index 47ae99f620..daf32a7719 100644 --- a/common/common.c +++ b/common/common.c @@ -209,6 +209,26 @@ int banner_is_disabled(void) return value; } +int print_banner_once(const char *prog, int even_if_disabled) +{ + static int printed = 0; + static int ret = -1; + + if (printed) + return ret; + + if (!banner_is_disabled() || even_if_disabled) { + ret = printf("Network UPS Tools %s %s%s\n", + prog, UPS_VERSION, + even_if_disabled == 2 ? "\n" : ""); + fflush(stdout); + if (ret > 0) + printed = 1; + } + + return ret; +} + /* enable writing upslog_with_errno() and upslogx() type messages to the syslog */ void syslogbit_set(void) diff --git a/include/common.h b/include/common.h index 07d374d05d..e3b52f56eb 100644 --- a/include/common.h +++ b/include/common.h @@ -204,6 +204,18 @@ extern const char *UPS_VERSION; * hide the NUT tool name+version banners; show them by default */ int banner_is_disabled(void); +/* Some NUT programs have historically printed their banner at start-up + * always, and so did not print one in help()/usage() or handling `-V` + * like others did. Now that we have NUT_QUIET_INIT_BANNER, we need a + * way to print that banner (regardless of the flag in some cases). + * The "even_if_disabled" should be 0 for initial banner of those + * programs (so the envvar would hide it), 1 -V case and 2 in -h case + * (for a blank line after). As before, the banner is printed to stdout. + * Returns the result of printf() involved. Remembers to not print again + * if the earlier printf() was successful. + */ +int print_banner_once(const char *prog, int even_if_disabled); + /* Normally we can (attempt to) use the syslog or Event Log (WIN32), * but environment variable NUT_DEBUG_SYSLOG allows to bypass it, and * perhaps keep daemons logging to stderr (e.g. in NUT Integration Test