trace2 / tr2_cfg.con commit Merge branch 'sg/overlong-progress-fix' (425e51e)
   1#include "cache.h"
   2#include "config.h"
   3#include "tr2_cfg.h"
   4
   5#define TR2_ENVVAR_CFG_PARAM "GIT_TR2_CONFIG_PARAMS"
   6
   7static struct strbuf **tr2_cfg_patterns;
   8static int tr2_cfg_count_patterns;
   9static int tr2_cfg_loaded;
  10
  11/*
  12 * Parse a string containing a comma-delimited list of config keys
  13 * or wildcard patterns into a list of strbufs.
  14 */
  15static int tr2_cfg_load_patterns(void)
  16{
  17        struct strbuf **s;
  18        const char *envvar;
  19
  20        if (tr2_cfg_loaded)
  21                return tr2_cfg_count_patterns;
  22        tr2_cfg_loaded = 1;
  23
  24        envvar = getenv(TR2_ENVVAR_CFG_PARAM);
  25        if (!envvar || !*envvar)
  26                return tr2_cfg_count_patterns;
  27
  28        tr2_cfg_patterns = strbuf_split_buf(envvar, strlen(envvar), ',', -1);
  29        for (s = tr2_cfg_patterns; *s; s++) {
  30                struct strbuf *buf = *s;
  31
  32                if (buf->len && buf->buf[buf->len - 1] == ',')
  33                        strbuf_setlen(buf, buf->len - 1);
  34                strbuf_trim_trailing_newline(*s);
  35                strbuf_trim(*s);
  36        }
  37
  38        tr2_cfg_count_patterns = s - tr2_cfg_patterns;
  39        return tr2_cfg_count_patterns;
  40}
  41
  42void tr2_cfg_free_patterns(void)
  43{
  44        if (tr2_cfg_patterns)
  45                strbuf_list_free(tr2_cfg_patterns);
  46        tr2_cfg_count_patterns = 0;
  47        tr2_cfg_loaded = 0;
  48}
  49
  50struct tr2_cfg_data {
  51        const char *file;
  52        int line;
  53};
  54
  55/*
  56 * See if the given config key matches any of our patterns of interest.
  57 */
  58static int tr2_cfg_cb(const char *key, const char *value, void *d)
  59{
  60        struct strbuf **s;
  61        struct tr2_cfg_data *data = (struct tr2_cfg_data *)d;
  62
  63        for (s = tr2_cfg_patterns; *s; s++) {
  64                struct strbuf *buf = *s;
  65                int wm = wildmatch(buf->buf, key, WM_CASEFOLD);
  66                if (wm == WM_MATCH) {
  67                        trace2_def_param_fl(data->file, data->line, key, value);
  68                        return 0;
  69                }
  70        }
  71
  72        return 0;
  73}
  74
  75void tr2_cfg_list_config_fl(const char *file, int line)
  76{
  77        struct tr2_cfg_data data = { file, line };
  78
  79        if (tr2_cfg_load_patterns() > 0)
  80                read_early_config(tr2_cfg_cb, &data);
  81}
  82
  83void tr2_cfg_set_fl(const char *file, int line, const char *key,
  84                    const char *value)
  85{
  86        struct tr2_cfg_data data = { file, line };
  87
  88        if (tr2_cfg_load_patterns() > 0)
  89                tr2_cfg_cb(key, value, &data);
  90}