diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-03 13:39:29 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-06-03 13:39:29 +0000 |
commit | b41961d74fe7ff2d4d4abaca92454e87c561e49f (patch) | |
tree | b34e3826a7b649dafdbd05081140c990c96d736d /lib/common/output_text.c | |
parent | Releasing progress-linux version 2.1.7-1~progress7.99u1. (diff) | |
download | pacemaker-b41961d74fe7ff2d4d4abaca92454e87c561e49f.tar.xz pacemaker-b41961d74fe7ff2d4d4abaca92454e87c561e49f.zip |
Merging upstream version 2.1.8~rc1.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/common/output_text.c')
-rw-r--r-- | lib/common/output_text.c | 108 |
1 files changed, 91 insertions, 17 deletions
diff --git a/lib/common/output_text.c b/lib/common/output_text.c index 6bd362d..d43e29f 100644 --- a/lib/common/output_text.c +++ b/lib/common/output_text.c @@ -1,5 +1,5 @@ /* - * Copyright 2019-2022 the Pacemaker project contributors + * Copyright 2019-2024 the Pacemaker project contributors * * The version control history for this file may have further details. * @@ -15,10 +15,14 @@ #include <glib.h> #include <termios.h> +#include "crmcommon_private.h" + +// @COMPAT Drop at 3.0.0 static gboolean fancy = FALSE; +// @COMPAT Drop at 3.0.0 GOptionEntry pcmk__text_output_entries[] = { - { "text-fancy", 0, 0, G_OPTION_ARG_NONE, &fancy, + { "text-fancy", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &fancy, "Use more highly formatted output (requires --output-as=text)", NULL }, @@ -33,9 +37,18 @@ typedef struct text_list_data_s { typedef struct private_data_s { GQueue *parent_q; + bool fancy; } private_data_t; static void +free_list_data(gpointer data) { + text_list_data_t *list_data = data; + + free(list_data->singular_noun); + free(list_data->plural_noun); +} + +static void text_free_priv(pcmk__output_t *out) { private_data_t *priv = NULL; @@ -45,7 +58,7 @@ text_free_priv(pcmk__output_t *out) { priv = out->priv; - g_queue_free(priv->parent_q); + g_queue_free_full(priv->parent_q, free_list_data); free(priv); out->priv = NULL; } @@ -59,15 +72,14 @@ text_init(pcmk__output_t *out) { /* If text_init was previously called on this output struct, just return. */ if (out->priv != NULL) { return true; - } else { - out->priv = calloc(1, sizeof(private_data_t)); - if (out->priv == NULL) { - return false; - } + } - priv = out->priv; + out->priv = calloc(1, sizeof(private_data_t)); + if (out->priv == NULL) { + return false; } + priv = out->priv; priv->parent_q = g_queue_new(); return true; } @@ -80,6 +92,9 @@ text_finish(pcmk__output_t *out, crm_exit_t exit_status, bool print, void **copy static void text_reset(pcmk__output_t *out) { + private_data_t *priv = NULL; + bool old_fancy = false; + CRM_ASSERT(out != NULL); if (out->dest != stdout) { @@ -88,8 +103,15 @@ text_reset(pcmk__output_t *out) { CRM_ASSERT(out->dest != NULL); + // Save priv->fancy before free/init sequence overwrites it + priv = out->priv; + old_fancy = priv->fancy; + text_free_priv(out); text_init(out); + + priv = out->priv; + priv->fancy = old_fancy; } static void @@ -192,17 +214,17 @@ text_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plur va_start(ap, format); - if (fancy && format) { + if ((fancy || priv->fancy) && (format != NULL)) { pcmk__indented_vprintf(out, format, ap); fprintf(out->dest, ":\n"); } va_end(ap); - new_list = calloc(1, sizeof(text_list_data_t)); + new_list = pcmk__assert_alloc(1, sizeof(text_list_data_t)); new_list->len = 0; - pcmk__str_update(&new_list->singular_noun, singular_noun); - pcmk__str_update(&new_list->plural_noun, plural_noun); + new_list->singular_noun = pcmk__str_copy(singular_noun); + new_list->plural_noun = pcmk__str_copy(plural_noun); g_queue_push_tail(priv->parent_q, new_list); } @@ -210,13 +232,15 @@ text_begin_list(pcmk__output_t *out, const char *singular_noun, const char *plur G_GNUC_PRINTF(3, 4) static void text_list_item(pcmk__output_t *out, const char *id, const char *format, ...) { + private_data_t *priv = NULL; va_list ap; CRM_ASSERT(out != NULL); + priv = out->priv; va_start(ap, format); - if (fancy) { + if (fancy || priv->fancy) { if (id != NULL) { /* Not really a good way to do this all in one call, so make it two. * The first handles the indentation and list styling. The second @@ -269,7 +293,7 @@ text_end_list(pcmk__output_t *out) { } } - free(node); + free_list_data(node); } static bool @@ -336,6 +360,52 @@ pcmk__mk_text_output(char **argv) { return retval; } +/*! + * \internal + * \brief Check whether fancy output is enabled for a text output object + * + * This returns \c false if the output object is not of text format. + * + * \param[in] out Output object + * + * \return \c true if \p out has fancy output enabled, or \c false otherwise + */ +bool +pcmk__output_text_get_fancy(pcmk__output_t *out) +{ + CRM_ASSERT(out != NULL); + + if (pcmk__str_eq(out->fmt_name, "text", pcmk__str_none)) { + private_data_t *priv = out->priv; + + CRM_ASSERT(priv != NULL); + return priv->fancy; + } + return false; +} + +/*! + * \internal + * \brief Enable or disable fancy output for a text output object + * + * This does nothing if the output object is not of text format. + * + * \param[in,out] out Output object + * \param[in] enabled Whether fancy output should be enabled for \p out + */ +void +pcmk__output_text_set_fancy(pcmk__output_t *out, bool enabled) +{ + CRM_ASSERT(out != NULL); + + if (pcmk__str_eq(out->fmt_name, "text", pcmk__str_none)) { + private_data_t *priv = out->priv; + + CRM_ASSERT(priv != NULL); + priv->fancy = enabled; + } +} + G_GNUC_PRINTF(2, 0) void pcmk__formatted_vprintf(pcmk__output_t *out, const char *format, va_list args) { @@ -363,10 +433,14 @@ pcmk__formatted_printf(pcmk__output_t *out, const char *format, ...) { G_GNUC_PRINTF(2, 0) void pcmk__indented_vprintf(pcmk__output_t *out, const char *format, va_list args) { + private_data_t *priv = NULL; + CRM_ASSERT(out != NULL); CRM_CHECK(pcmk__str_eq(out->fmt_name, "text", pcmk__str_none), return); - if (fancy) { + priv = out->priv; + + if (fancy || priv->fancy) { int level = 0; private_data_t *priv = out->priv; @@ -428,7 +502,7 @@ pcmk__text_prompt(const char *prompt, bool echo, char **dest) #if HAVE_SSCANF_M rc = scanf("%ms", dest); #else - *dest = calloc(1, 1024); + *dest = pcmk__assert_alloc(1, 1024); rc = scanf("%1023s", *dest); #endif fprintf(stderr, "\n"); |