confparse.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. /** An abbreviation for a configuration option allowed on the command line. */
  15. typedef struct config_abbrev_t {
  16. const char *abbreviated;
  17. const char *full;
  18. int commandline_only;
  19. int warn;
  20. } config_abbrev_t;
  21. typedef struct config_deprecation_t {
  22. const char *name;
  23. const char *why_deprecated;
  24. } config_deprecation_t;
  25. /* Handy macro for declaring "In the config file or on the command line,
  26. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  27. #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
  28. /** A variable allowed in the configuration file or on the command line. */
  29. typedef struct config_var_t {
  30. const char *name; /**< The full keyword (case insensitive). */
  31. config_type_t type; /**< How to interpret the type and turn it into a
  32. * value. */
  33. off_t var_offset; /**< Offset of the corresponding member of or_options_t. */
  34. const char *initvalue; /**< String (or null) describing initial value. */
  35. #ifdef TOR_UNIT_TESTS
  36. /** Used for compiler-magic to typecheck the corresponding field in the
  37. * corresponding struct. Only used in unit test mode, at compile-time. */
  38. confparse_dummy_values_t var_ptr_dummy;
  39. #endif
  40. } config_var_t;
  41. /* Macros to define extra members inside config_var_t fields, and at the
  42. * end of a list of them.
  43. */
  44. #ifdef TOR_UNIT_TESTS
  45. /* This is a somewhat magic type-checking macro for users of confparse.c.
  46. * It initializes a union member "confparse_dummy_values_t.conftype" with
  47. * the address of a static member "tp_dummy.member". This
  48. * will give a compiler warning unless the member field is of the correct
  49. * type.
  50. *
  51. * (This warning is mandatory, because a type mismatch here violates the type
  52. * compatibility constraint for simple assignment, and requires a diagnostic,
  53. * according to the C spec.)
  54. *
  55. * For example, suppose you say:
  56. * "CONF_CHECK_VAR_TYPE(or_options_t, STRING, Address)".
  57. * Then this macro will evaluate to:
  58. * { .STRING = &or_options_t_dummy.Address }
  59. * And since confparse_dummy_values_t.STRING has type "char **", that
  60. * expression will create a warning unless or_options_t.Address also
  61. * has type "char *".
  62. */
  63. #define CONF_CHECK_VAR_TYPE(tp, conftype, member) \
  64. { . conftype = &tp ## _dummy . member }
  65. #define CONF_TEST_MEMBERS(tp, conftype, member) \
  66. , CONF_CHECK_VAR_TYPE(tp, conftype, member)
  67. #define END_OF_CONFIG_VARS \
  68. { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, { .INT=NULL } }
  69. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  70. static tp tp ## _dummy
  71. #else /* !(defined(TOR_UNIT_TESTS)) */
  72. #define CONF_TEST_MEMBERS(tp, conftype, member)
  73. #define END_OF_CONFIG_VARS { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
  74. /* Repeatedly declarable incomplete struct to absorb redundant semicolons */
  75. #define DUMMY_TYPECHECK_INSTANCE(tp) \
  76. struct tor_semicolon_eater
  77. #endif /* defined(TOR_UNIT_TESTS) */
  78. /** Type of a callback to validate whether a given configuration is
  79. * well-formed and consistent. See options_trial_assign() for documentation
  80. * of arguments. */
  81. typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
  82. /** Callback to free a configuration object. */
  83. typedef void (*free_cfg_fn_t)(void*);
  84. /** Information on the keys, value types, key-to-struct-member mappings,
  85. * variable descriptions, validation functions, and abbreviations for a
  86. * configuration or storage format. */
  87. typedef struct config_format_t {
  88. size_t size; /**< Size of the struct that everything gets parsed into. */
  89. uint32_t magic; /**< Required 'magic value' to make sure we have a struct
  90. * of the right type. */
  91. off_t magic_offset; /**< Offset of the magic value within the struct. */
  92. config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
  93. * parsing this format. */
  94. const config_deprecation_t *deprecations; /** List of deprecated options */
  95. config_var_t *vars; /**< List of variables we recognize, their default
  96. * values, and where we stick them in the structure. */
  97. validate_fn_t validate_fn; /**< Function to validate config. */
  98. free_cfg_fn_t free_fn; /**< Function to free the configuration. */
  99. /** If present, extra is a LINELIST variable for unrecognized
  100. * lines. Otherwise, unrecognized lines are an error. */
  101. config_var_t *extra;
  102. } config_format_t;
  103. /** Macro: assert that <b>cfg</b> has the right magic field for format
  104. * <b>fmt</b>. */
  105. #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
  106. tor_assert(fmt && cfg); \
  107. tor_assert((fmt)->magic == \
  108. *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
  109. STMT_END
  110. #define CAL_USE_DEFAULTS (1u<<0)
  111. #define CAL_CLEAR_FIRST (1u<<1)
  112. #define CAL_WARN_DEPRECATIONS (1u<<2)
  113. void *config_new(const config_format_t *fmt);
  114. void config_free_(const config_format_t *fmt, void *options);
  115. #define config_free(fmt, options) do { \
  116. config_free_((fmt), (options)); \
  117. (options) = NULL; \
  118. } while (0)
  119. struct config_line_t *config_get_assigned_option(const config_format_t *fmt,
  120. const void *options, const char *key,
  121. int escape_val);
  122. int config_is_same(const config_format_t *fmt,
  123. const void *o1, const void *o2,
  124. const char *name);
  125. void config_init(const config_format_t *fmt, void *options);
  126. void *config_dup(const config_format_t *fmt, const void *old);
  127. char *config_dump(const config_format_t *fmt, const void *default_options,
  128. const void *options, int minimal,
  129. int comment_defaults);
  130. int config_assign(const config_format_t *fmt, void *options,
  131. struct config_line_t *list,
  132. unsigned flags, char **msg);
  133. config_var_t *config_find_option_mutable(config_format_t *fmt,
  134. const char *key);
  135. const char *config_find_deprecation(const config_format_t *fmt,
  136. const char *key);
  137. const config_var_t *config_find_option(const config_format_t *fmt,
  138. const char *key);
  139. const char *config_expand_abbrev(const config_format_t *fmt,
  140. const char *option,
  141. int command_line, int warn_obsolete);
  142. void warn_deprecated_option(const char *what, const char *why);
  143. /* Helper macros to compare an option across two configuration objects */
  144. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  145. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  146. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  147. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  148. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  149. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  150. #ifdef CONFPARSE_PRIVATE
  151. STATIC void config_reset_line(const config_format_t *fmt, void *options,
  152. const char *key, int use_defaults);
  153. #endif
  154. #endif /* !defined(TOR_CONFPARSE_H) */