confparse.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /* Copyright (c) 2001 Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2019, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file confparse.h
  8. *
  9. * \brief Header for confparse.c.
  10. */
  11. #ifndef TOR_CONFPARSE_H
  12. #define TOR_CONFPARSE_H
  13. #include "lib/conf/conftypes.h"
  14. #include "lib/conf/confmacros.h"
  15. #include "lib/testsupport/testsupport.h"
  16. /** An abbreviation for a configuration option allowed on the command line. */
  17. typedef struct config_abbrev_t {
  18. const char *abbreviated;
  19. const char *full;
  20. int commandline_only;
  21. int warn;
  22. } config_abbrev_t;
  23. typedef struct config_deprecation_t {
  24. const char *name;
  25. const char *why_deprecated;
  26. } config_deprecation_t;
  27. /* Handy macro for declaring "In the config file or on the command line,
  28. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  29. #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
  30. /** Type of a callback to validate whether a given configuration is
  31. * well-formed and consistent. See options_trial_assign() for documentation
  32. * of arguments. */
  33. typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
  34. struct config_mgr_t;
  35. /** Callback to clear all non-managed fields of a configuration object. */
  36. typedef void (*clear_cfg_fn_t)(const struct config_mgr_t *mgr, void*);
  37. /** Information on the keys, value types, key-to-struct-member mappings,
  38. * variable descriptions, validation functions, and abbreviations for a
  39. * configuration or storage format. */
  40. typedef struct config_format_t {
  41. size_t size; /**< Size of the struct that everything gets parsed into. */
  42. struct_magic_decl_t magic; /**< Magic number info for this struct. */
  43. const config_abbrev_t *abbrevs; /**< List of abbreviations that we expand
  44. * when parsing this format. */
  45. const config_deprecation_t *deprecations; /** List of deprecated options */
  46. const config_var_t *vars; /**< List of variables we recognize, their default
  47. * values, and where we stick them in the
  48. * structure. */
  49. validate_fn_t validate_fn; /**< Function to validate config. */
  50. clear_cfg_fn_t clear_fn; /**< Function to clear the configuration. */
  51. /** If present, extra denotes a LINELIST variable for unrecognized
  52. * lines. Otherwise, unrecognized lines are an error. */
  53. const struct_member_t *extra;
  54. } config_format_t;
  55. /**
  56. * A collection of config_format_t objects to describe several objects
  57. * that are all configured with the same configuration file.
  58. *
  59. * (NOTE: for now, this only handles a single config_format_t.)
  60. **/
  61. typedef struct config_mgr_t config_mgr_t;
  62. config_mgr_t *config_mgr_new(const config_format_t *toplevel_fmt);
  63. void config_mgr_free_(config_mgr_t *mgr);
  64. void config_mgr_freeze(config_mgr_t *mgr);
  65. #define config_mgr_free(mgr) \
  66. FREE_AND_NULL(config_mgr_t, config_mgr_free_, (mgr))
  67. struct smartlist_t *config_mgr_list_vars(const config_mgr_t *mgr);
  68. struct smartlist_t *config_mgr_list_deprecated_vars(const config_mgr_t *mgr);
  69. #define CAL_USE_DEFAULTS (1u<<0)
  70. #define CAL_CLEAR_FIRST (1u<<1)
  71. #define CAL_WARN_DEPRECATIONS (1u<<2)
  72. void *config_new(const config_mgr_t *fmt);
  73. void config_free_(const config_mgr_t *fmt, void *options);
  74. #define config_free(mgr, options) do { \
  75. config_free_((mgr), (options)); \
  76. (options) = NULL; \
  77. } while (0)
  78. struct config_line_t *config_get_assigned_option(const config_mgr_t *mgr,
  79. const void *options, const char *key,
  80. int escape_val);
  81. int config_is_same(const config_mgr_t *fmt,
  82. const void *o1, const void *o2,
  83. const char *name);
  84. struct config_line_t *config_get_changes(const config_mgr_t *mgr,
  85. const void *options1, const void *options2);
  86. void config_init(const config_mgr_t *mgr, void *options);
  87. void *config_dup(const config_mgr_t *mgr, const void *old);
  88. char *config_dump(const config_mgr_t *mgr, const void *default_options,
  89. const void *options, int minimal,
  90. int comment_defaults);
  91. bool config_check_ok(const config_mgr_t *mgr, const void *options,
  92. int severity);
  93. int config_assign(const config_mgr_t *mgr, void *options,
  94. struct config_line_t *list,
  95. unsigned flags, char **msg);
  96. const char *config_find_deprecation(const config_mgr_t *mgr,
  97. const char *key);
  98. const char *config_find_option_name(const config_mgr_t *mgr,
  99. const char *key);
  100. const char *config_expand_abbrev(const config_mgr_t *mgr,
  101. const char *option,
  102. int command_line, int warn_obsolete);
  103. void warn_deprecated_option(const char *what, const char *why);
  104. bool config_var_is_cumulative(const config_var_t *var);
  105. bool config_var_is_settable(const config_var_t *var);
  106. bool config_var_is_contained(const config_var_t *var);
  107. /* Helper macros to compare an option across two configuration objects */
  108. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  109. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  110. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  111. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  112. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  113. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  114. #ifdef CONFPARSE_PRIVATE
  115. STATIC void config_reset_line(const config_mgr_t *mgr, void *options,
  116. const char *key, int use_defaults);
  117. #endif
  118. #endif /* !defined(TOR_CONFPARSE_H) */