confparse.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  36. * Callback to clear all non-managed fields of a configuration object.
  37. *
  38. * <b>obj</b> is the configuration object whose non-managed fields should be
  39. * cleared.
  40. *
  41. * (Regular fields get cleared by config_reset(), but you might have fields
  42. * in the object that do not correspond to configuration variables. If those
  43. * fields need to be cleared or freed, this is where to do it.)
  44. */
  45. typedef void (*clear_cfg_fn_t)(const struct config_mgr_t *mgr, void *obj);
  46. /** Information on the keys, value types, key-to-struct-member mappings,
  47. * variable descriptions, validation functions, and abbreviations for a
  48. * configuration or storage format. */
  49. typedef struct config_format_t {
  50. size_t size; /**< Size of the struct that everything gets parsed into. */
  51. struct_magic_decl_t magic; /**< Magic number info for this struct. */
  52. const config_abbrev_t *abbrevs; /**< List of abbreviations that we expand
  53. * when parsing this format. */
  54. const config_deprecation_t *deprecations; /** List of deprecated options */
  55. const config_var_t *vars; /**< List of variables we recognize, their default
  56. * values, and where we stick them in the
  57. * structure. */
  58. validate_fn_t validate_fn; /**< Function to validate config. */
  59. clear_cfg_fn_t clear_fn; /**< Function to clear the configuration. */
  60. /** If present, extra denotes a LINELIST variable for unrecognized
  61. * lines. Otherwise, unrecognized lines are an error. */
  62. const struct_member_t *extra;
  63. /** The position of a config_suite_t pointer within the toplevel object,
  64. * or -1 if there is no such pointer. */
  65. int config_suite_offset;
  66. } config_format_t;
  67. /**
  68. * A collection of config_format_t objects to describe several objects
  69. * that are all configured with the same configuration file.
  70. *
  71. * (NOTE: for now, this only handles a single config_format_t.)
  72. **/
  73. typedef struct config_mgr_t config_mgr_t;
  74. config_mgr_t *config_mgr_new(const config_format_t *toplevel_fmt);
  75. void config_mgr_free_(config_mgr_t *mgr);
  76. int config_mgr_add_format(config_mgr_t *mgr,
  77. const config_format_t *fmt);
  78. void config_mgr_freeze(config_mgr_t *mgr);
  79. #define config_mgr_free(mgr) \
  80. FREE_AND_NULL(config_mgr_t, config_mgr_free_, (mgr))
  81. struct smartlist_t *config_mgr_list_vars(const config_mgr_t *mgr);
  82. struct smartlist_t *config_mgr_list_deprecated_vars(const config_mgr_t *mgr);
  83. /** A collection of managed configuration objects. */
  84. typedef struct config_suite_t config_suite_t;
  85. #define CAL_USE_DEFAULTS (1u<<0)
  86. #define CAL_CLEAR_FIRST (1u<<1)
  87. #define CAL_WARN_DEPRECATIONS (1u<<2)
  88. void *config_new(const config_mgr_t *fmt);
  89. void config_free_(const config_mgr_t *fmt, void *options);
  90. #define config_free(mgr, options) do { \
  91. config_free_((mgr), (options)); \
  92. (options) = NULL; \
  93. } while (0)
  94. struct config_line_t *config_get_assigned_option(const config_mgr_t *mgr,
  95. const void *options, const char *key,
  96. int escape_val);
  97. int config_is_same(const config_mgr_t *fmt,
  98. const void *o1, const void *o2,
  99. const char *name);
  100. struct config_line_t *config_get_changes(const config_mgr_t *mgr,
  101. const void *options1, const void *options2);
  102. void config_init(const config_mgr_t *mgr, void *options);
  103. void *config_dup(const config_mgr_t *mgr, const void *old);
  104. char *config_dump(const config_mgr_t *mgr, const void *default_options,
  105. const void *options, int minimal,
  106. int comment_defaults);
  107. bool config_check_ok(const config_mgr_t *mgr, const void *options,
  108. int severity);
  109. int config_assign(const config_mgr_t *mgr, void *options,
  110. struct config_line_t *list,
  111. unsigned flags, char **msg);
  112. const char *config_find_deprecation(const config_mgr_t *mgr,
  113. const char *key);
  114. const char *config_find_option_name(const config_mgr_t *mgr,
  115. const char *key);
  116. const char *config_expand_abbrev(const config_mgr_t *mgr,
  117. const char *option,
  118. int command_line, int warn_obsolete);
  119. void warn_deprecated_option(const char *what, const char *why);
  120. bool config_var_is_cumulative(const config_var_t *var);
  121. bool config_var_is_settable(const config_var_t *var);
  122. bool config_var_is_contained(const config_var_t *var);
  123. bool config_var_is_invisible(const config_var_t *var);
  124. bool config_var_is_dumpable(const config_var_t *var);
  125. /* Helper macros to compare an option across two configuration objects */
  126. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  127. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  128. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  129. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  130. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  131. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  132. #ifdef CONFPARSE_PRIVATE
  133. STATIC void config_reset_line(const config_mgr_t *mgr, void *options,
  134. const char *key, int use_defaults);
  135. STATIC void *config_mgr_get_obj_mutable(const config_mgr_t *mgr,
  136. void *toplevel, int idx);
  137. STATIC const void *config_mgr_get_obj(const config_mgr_t *mgr,
  138. const void *toplevel, int idx);
  139. #endif
  140. #endif /* !defined(TOR_CONFPARSE_H) */