confparse.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. /** An abbreviation for a configuration option allowed on the command line. */
  16. typedef struct config_abbrev_t {
  17. const char *abbreviated;
  18. const char *full;
  19. int commandline_only;
  20. int warn;
  21. } config_abbrev_t;
  22. typedef struct config_deprecation_t {
  23. const char *name;
  24. const char *why_deprecated;
  25. } config_deprecation_t;
  26. /* Handy macro for declaring "In the config file or on the command line,
  27. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  28. #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
  29. /** Type of a callback to validate whether a given configuration is
  30. * well-formed and consistent. See options_trial_assign() for documentation
  31. * of arguments. */
  32. typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
  33. /** Callback to free a configuration object. */
  34. typedef void (*free_cfg_fn_t)(void*);
  35. /** Information on the keys, value types, key-to-struct-member mappings,
  36. * variable descriptions, validation functions, and abbreviations for a
  37. * configuration or storage format. */
  38. typedef struct config_format_t {
  39. size_t size; /**< Size of the struct that everything gets parsed into. */
  40. struct_magic_decl_t magic; /**< Magic number info for this struct. */
  41. config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
  42. * parsing this format. */
  43. const config_deprecation_t *deprecations; /** List of deprecated options */
  44. config_var_t *vars; /**< List of variables we recognize, their default
  45. * values, and where we stick them in the structure. */
  46. validate_fn_t validate_fn; /**< Function to validate config. */
  47. free_cfg_fn_t free_fn; /**< Function to free the configuration. */
  48. /** If present, extra denotes a LINELIST variable for unrecognized
  49. * lines. Otherwise, unrecognized lines are an error. */
  50. struct_member_t *extra;
  51. } config_format_t;
  52. /** Macro: assert that <b>cfg</b> has the right magic field for format
  53. * <b>fmt</b>. */
  54. #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
  55. tor_assert(fmt); \
  56. struct_check_magic((cfg), &fmt->magic); \
  57. STMT_END
  58. #define CAL_USE_DEFAULTS (1u<<0)
  59. #define CAL_CLEAR_FIRST (1u<<1)
  60. #define CAL_WARN_DEPRECATIONS (1u<<2)
  61. void *config_new(const config_format_t *fmt);
  62. void config_free_(const config_format_t *fmt, void *options);
  63. #define config_free(fmt, options) do { \
  64. config_free_((fmt), (options)); \
  65. (options) = NULL; \
  66. } while (0)
  67. struct config_line_t *config_get_assigned_option(const config_format_t *fmt,
  68. const void *options, const char *key,
  69. int escape_val);
  70. int config_is_same(const config_format_t *fmt,
  71. const void *o1, const void *o2,
  72. const char *name);
  73. void config_init(const config_format_t *fmt, void *options);
  74. void *config_dup(const config_format_t *fmt, const void *old);
  75. char *config_dump(const config_format_t *fmt, const void *default_options,
  76. const void *options, int minimal,
  77. int comment_defaults);
  78. bool config_check_ok(const config_format_t *fmt, const void *options,
  79. int severity);
  80. int config_assign(const config_format_t *fmt, void *options,
  81. struct config_line_t *list,
  82. unsigned flags, char **msg);
  83. config_var_t *config_find_option_mutable(config_format_t *fmt,
  84. const char *key);
  85. const char *config_find_deprecation(const config_format_t *fmt,
  86. const char *key);
  87. const config_var_t *config_find_option(const config_format_t *fmt,
  88. const char *key);
  89. const char *config_expand_abbrev(const config_format_t *fmt,
  90. const char *option,
  91. int command_line, int warn_obsolete);
  92. void warn_deprecated_option(const char *what, const char *why);
  93. /* Helper macros to compare an option across two configuration objects */
  94. #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
  95. #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
  96. #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
  97. #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
  98. #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
  99. #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
  100. #ifdef CONFPARSE_PRIVATE
  101. STATIC void config_reset_line(const config_format_t *fmt, void *options,
  102. const char *key, int use_defaults);
  103. #endif
  104. #endif /* !defined(TOR_CONFPARSE_H) */