THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.0KB

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