confparse.h 7.2 KB

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