root/lib/common/output.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. pcmk__output_free
  2. pcmk__output_new
  3. pcmk__register_format
  4. pcmk__register_formats
  5. pcmk__unregister_formats
  6. pcmk__call_message
  7. pcmk__register_message
  8. pcmk__register_messages

   1 /*
   2  * Copyright 2019-2020 the Pacemaker project contributors
   3  *
   4  * The version control history for this file may have further details.
   5  *
   6  * This source code is licensed under the GNU Lesser General Public License
   7  * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
   8  */
   9 
  10 #include <libxml/tree.h>
  11 
  12 #include <crm/common/util.h>
  13 #include <crm/common/xml.h>
  14 #include <crm/common/internal.h>
  15 #include <crm/common/output_internal.h>
  16 #include <crm/common/strings_internal.h>
  17 #include <libxml/tree.h>
  18 
  19 static GHashTable *formatters = NULL;
  20 
  21 void
  22 pcmk__output_free(pcmk__output_t *out) {
     /* [previous][next][first][last][top][bottom][index][help] */
  23     out->free_priv(out);
  24 
  25     if (out->messages != NULL) {
  26         g_hash_table_destroy(out->messages);
  27     }
  28 
  29     g_free(out->request);
  30     free(out);
  31 }
  32 
  33 int
  34 pcmk__output_new(pcmk__output_t **out, const char *fmt_name, const char *filename,
     /* [previous][next][first][last][top][bottom][index][help] */
  35                  char **argv) {
  36     pcmk__output_factory_t create = NULL;;
  37 
  38     if (formatters == NULL) {
  39         return EINVAL;
  40     }
  41 
  42     /* If no name was given, just try "text".  It's up to each tool to register
  43      * what it supports so this also may not be valid.
  44      */
  45     if (fmt_name == NULL) {
  46         create = g_hash_table_lookup(formatters, "text");
  47     } else {
  48         create = g_hash_table_lookup(formatters, fmt_name);
  49     }
  50 
  51     if (create == NULL) {
  52         return pcmk_rc_unknown_format;
  53     }
  54 
  55     *out = create(argv);
  56     if (*out == NULL) {
  57         return ENOMEM;
  58     }
  59 
  60     if (pcmk__str_eq(filename, "-", pcmk__str_null_matches)) {
  61         (*out)->dest = stdout;
  62     } else {
  63         (*out)->dest = fopen(filename, "w");
  64         if ((*out)->dest == NULL) {
  65             return errno;
  66         }
  67     }
  68 
  69     (*out)->quiet = false;
  70 
  71     (*out)->messages = g_hash_table_new_full(crm_str_hash, g_str_equal, free, NULL);
  72 
  73     if ((*out)->init(*out) == false) {
  74         pcmk__output_free(*out);
  75         return ENOMEM;
  76     }
  77 
  78     return pcmk_rc_ok;
  79 }
  80 
  81 int
  82 pcmk__register_format(GOptionGroup *group, const char *name,
     /* [previous][next][first][last][top][bottom][index][help] */
  83                       pcmk__output_factory_t create, GOptionEntry *options) {
  84     if (create == NULL) {
  85         return -EINVAL;
  86     }
  87 
  88     if (formatters == NULL) {
  89         formatters = g_hash_table_new_full(crm_str_hash, g_str_equal, free, NULL);
  90     }
  91 
  92     if (options != NULL && group != NULL) {
  93         g_option_group_add_entries(group, options);
  94     }
  95 
  96     g_hash_table_insert(formatters, strdup(name), create);
  97     return 0;
  98 }
  99 
 100 void
 101 pcmk__register_formats(GOptionGroup *group, pcmk__supported_format_t *formats) {
     /* [previous][next][first][last][top][bottom][index][help] */
 102     pcmk__supported_format_t *entry = NULL;
 103 
 104     if (formats == NULL) {
 105         return;
 106     }
 107 
 108     for (entry = formats; entry->name != NULL; entry++) {
 109         pcmk__register_format(group, entry->name, entry->create, entry->options);
 110     }
 111 }
 112 
 113 void
 114 pcmk__unregister_formats() {
     /* [previous][next][first][last][top][bottom][index][help] */
 115     if (formatters != NULL) {
 116         g_hash_table_destroy(formatters);
 117     }
 118 }
 119 
 120 int
 121 pcmk__call_message(pcmk__output_t *out, const char *message_id, ...) {
     /* [previous][next][first][last][top][bottom][index][help] */
 122     va_list args;
 123     int rc = pcmk_rc_ok;
 124     pcmk__message_fn_t fn;
 125 
 126     fn = g_hash_table_lookup(out->messages, message_id);
 127     if (fn == NULL) {
 128         return EINVAL;
 129     }
 130 
 131     va_start(args, message_id);
 132     rc = fn(out, args);
 133     va_end(args);
 134 
 135     return rc;
 136 }
 137 
 138 void
 139 pcmk__register_message(pcmk__output_t *out, const char *message_id,
     /* [previous][next][first][last][top][bottom][index][help] */
 140                        pcmk__message_fn_t fn) {
 141     g_hash_table_replace(out->messages, strdup(message_id), fn);
 142 }
 143 
 144 void
 145 pcmk__register_messages(pcmk__output_t *out, pcmk__message_entry_t *table) {
     /* [previous][next][first][last][top][bottom][index][help] */
 146     pcmk__message_entry_t *entry;
 147 
 148     for (entry = table; entry->message_id != NULL; entry++) {
 149         if (pcmk__strcase_any_of(entry->fmt_name, "default", out->fmt_name, NULL)) {
 150             pcmk__register_message(out, entry->message_id, entry->fn);
 151         }
 152     }
 153 }

/* [previous][next][first][last][top][bottom][index][help] */