confparse.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. /** The position of a config_suite_t pointer within the toplevel object,
  55. * or -1 if there is no such pointer. */
  56. int config_suite_offset;
  57. } config_format_t;
  58. /**
  59. * A collection of config_format_t objects to describe several objects
  60. * that are all configured with the same configuration file.
  61. *
  62. * (NOTE: for now, this only handles a single config_format_t.)
  63. **/
  64. typedef struct config_mgr_t config_mgr_t;
  65. config_mgr_t *config_mgr_new(const config_format_t *toplevel_fmt);
  66. void config_mgr_free_(config_mgr_t *mgr);
  67. int config_mgr_add_format(config_mgr_t *mgr,
  68. const config_format_t *fmt);
  69. void config_mgr_freeze(config_mgr_t *mgr);
  70. #define config_mgr_free(mgr) \
  71. FREE_AND_NULL(config_mgr_t, config_mgr_free_, (mgr))
  72. struct smartlist_t *config_mgr_list_vars(const config_mgr_t *mgr);
  73. struct smartlist_t *config_mgr_list_deprecated_vars(const config_mgr_t *mgr);
  74. /** A collection of managed configuration objects. */
  75. typedef struct config_suite_t config_suite_t;
  76. #define CAL_USE_DEFAULTS (1u<<0)
  77. #define CAL_CLEAR_FIRST (1u<<1)
  78. #define CAL_WARN_DEPRECATIONS (1u<<2)
  79. void *config_new(const config_mgr_t *fmt);
  80. void config_free_(const config_mgr_t *fmt, void *options);
  81. #define config_free(mgr, options) do { \
  82. config_free_((mgr), (options)); \
  83. (options) = NULL; \
  84. } while (0)
  85. struct config_line_t *config_get_assigned_option(const config_mgr_t *mgr,
  86. const void *options, const char *key,
  87. int escape_val);
  88. int config_is_same(const config_mgr_t *fmt,
  89. const void *o1, const void *o2,
  90. const char *name);
  91. struct config_line_t *config_get_changes(const config_mgr_t *mgr,
  92. const void *options1, const void *options2);
  93. void config_init(const config_mgr_t *mgr, void *options);
  94. void *config_dup(const config_mgr_t *mgr, const void *old);
  95. char *config_dump(const config_mgr_t *mgr, const void *default_options,
  96. const void *options, int minimal,
  97. int comment_defaults);
  98. bool config_check_ok(const config_mgr_t *mgr, const void *options,
  99. int severity);
  100. int config_assign(const config_mgr_t *mgr, void *options,
  101. struct config_line_t *list,
  102. unsigned flags, char **msg);
  103. const char *config_find_deprecation(const config_mgr_t *mgr,
  104. const char *key);
  105. const char *config_find_option_name(const config_mgr_t *mgr,
  106. const char *key);
  107. const char *config_expand_abbrev(const config_mgr_t *mgr,
  108. const char *option,
  109. int command_line, int warn_obsolete);
  110. void warn_deprecated_option(const char *what, const char *why);
  111. bool config_var_is_cumulative(const config_var_t *var);
  112. bool config_var_is_settable(const config_var_t *var);
  113. bool config_var_is_contained(const config_var_t *var);
  114. /* Helper macros to compare an option across two configuration objects */
  115. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  116. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  117. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  118. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  119. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  120. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  121. #ifdef CONFPARSE_PRIVATE
  122. STATIC void config_reset_line(const config_mgr_t *mgr, void *options,
  123. const char *key, int use_defaults);
  124. #endif
  125. #endif /* !defined(TOR_CONFPARSE_H) */