confparse.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #ifndef TOR_CONFPARSE_H
  7. #define TOR_CONFPARSE_H
  8. /** Enumeration of types which option values can take */
  9. typedef enum config_type_t {
  10. CONFIG_TYPE_STRING = 0, /**< An arbitrary string. */
  11. CONFIG_TYPE_FILENAME, /**< A filename: some prefixes get expanded. */
  12. CONFIG_TYPE_UINT, /**< A non-negative integer less than MAX_INT */
  13. CONFIG_TYPE_INT, /**< Any integer. */
  14. CONFIG_TYPE_PORT, /**< A port from 1...65535, 0 for "not set", or
  15. * "auto". */
  16. CONFIG_TYPE_INTERVAL, /**< A number of seconds, with optional units*/
  17. CONFIG_TYPE_MSEC_INTERVAL,/**< A number of milliseconds, with optional
  18. * units */
  19. CONFIG_TYPE_MEMUNIT, /**< A number of bytes, with optional units*/
  20. CONFIG_TYPE_DOUBLE, /**< A floating-point value */
  21. CONFIG_TYPE_BOOL, /**< A boolean value, expressed as 0 or 1. */
  22. CONFIG_TYPE_AUTOBOOL, /**< A boolean+auto value, expressed 0 for false,
  23. * 1 for true, and -1 for auto */
  24. CONFIG_TYPE_ISOTIME, /**< An ISO-formatted time relative to UTC. */
  25. CONFIG_TYPE_CSV, /**< A list of strings, separated by commas and
  26. * optional whitespace. */
  27. CONFIG_TYPE_CSV_INTERVAL, /**< A list of strings, separated by commas and
  28. * optional whitespace, representing intervals in
  29. * seconds, with optional units */
  30. CONFIG_TYPE_LINELIST, /**< Uninterpreted config lines */
  31. CONFIG_TYPE_LINELIST_S, /**< Uninterpreted, context-sensitive config lines,
  32. * mixed with other keywords. */
  33. CONFIG_TYPE_LINELIST_V, /**< Catch-all "virtual" option to summarize
  34. * context-sensitive config lines when fetching.
  35. */
  36. CONFIG_TYPE_ROUTERSET, /**< A list of router names, addrs, and fps,
  37. * parsed into a routerset_t. */
  38. CONFIG_TYPE_OBSOLETE, /**< Obsolete (ignored) option. */
  39. } config_type_t;
  40. /** An abbreviation for a configuration option allowed on the command line. */
  41. typedef struct config_abbrev_t {
  42. const char *abbreviated;
  43. const char *full;
  44. int commandline_only;
  45. int warn;
  46. } config_abbrev_t;
  47. typedef struct config_deprecation_t {
  48. const char *name;
  49. const char *why_deprecated;
  50. } config_deprecation_t;
  51. /* Handy macro for declaring "In the config file or on the command line,
  52. * you can abbreviate <b>tok</b>s as <b>tok</b>". */
  53. #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
  54. /** A variable allowed in the configuration file or on the command line. */
  55. typedef struct config_var_t {
  56. const char *name; /**< The full keyword (case insensitive). */
  57. config_type_t type; /**< How to interpret the type and turn it into a
  58. * value. */
  59. off_t var_offset; /**< Offset of the corresponding member of or_options_t. */
  60. const char *initvalue; /**< String (or null) describing initial value. */
  61. } config_var_t;
  62. /** Type of a callback to validate whether a given configuration is
  63. * well-formed and consistent. See options_trial_assign() for documentation
  64. * of arguments. */
  65. typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
  66. /** Information on the keys, value types, key-to-struct-member mappings,
  67. * variable descriptions, validation functions, and abbreviations for a
  68. * configuration or storage format. */
  69. typedef struct config_format_t {
  70. size_t size; /**< Size of the struct that everything gets parsed into. */
  71. uint32_t magic; /**< Required 'magic value' to make sure we have a struct
  72. * of the right type. */
  73. off_t magic_offset; /**< Offset of the magic value within the struct. */
  74. config_abbrev_t *abbrevs; /**< List of abbreviations that we expand when
  75. * parsing this format. */
  76. config_deprecation_t *deprecations; /** List of deprecated options */
  77. config_var_t *vars; /**< List of variables we recognize, their default
  78. * values, and where we stick them in the structure. */
  79. validate_fn_t validate_fn; /**< Function to validate config. */
  80. /** If present, extra is a LINELIST variable for unrecognized
  81. * lines. Otherwise, unrecognized lines are an error. */
  82. config_var_t *extra;
  83. } config_format_t;
  84. /** Macro: assert that <b>cfg</b> has the right magic field for format
  85. * <b>fmt</b>. */
  86. #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
  87. tor_assert(fmt && cfg); \
  88. tor_assert((fmt)->magic == \
  89. *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
  90. STMT_END
  91. void *config_new(const config_format_t *fmt);
  92. void config_line_append(config_line_t **lst,
  93. const char *key, const char *val);
  94. config_line_t *config_lines_dup(const config_line_t *inp);
  95. const config_line_t *config_line_find(const config_line_t *lines,
  96. const char *key);
  97. void config_free(const config_format_t *fmt, void *options);
  98. int config_lines_eq(config_line_t *a, config_line_t *b);
  99. int config_count_key(const config_line_t *a, const char *key);
  100. config_line_t *config_get_assigned_option(const config_format_t *fmt,
  101. const void *options, const char *key,
  102. int escape_val);
  103. int config_is_same(const config_format_t *fmt,
  104. const void *o1, const void *o2,
  105. const char *name);
  106. void config_init(const config_format_t *fmt, void *options);
  107. void *config_dup(const config_format_t *fmt, const void *old);
  108. char *config_dump(const config_format_t *fmt, const void *default_options,
  109. const void *options, int minimal,
  110. int comment_defaults);
  111. int config_assign(const config_format_t *fmt, void *options,
  112. config_line_t *list,
  113. int use_defaults, int clear_first, char **msg);
  114. config_var_t *config_find_option_mutable(config_format_t *fmt,
  115. const char *key);
  116. const config_var_t *config_find_option(const config_format_t *fmt,
  117. const char *key);
  118. int config_get_lines(const char *string, config_line_t **result, int extended);
  119. void config_free_lines(config_line_t *front);
  120. const char *config_expand_abbrev(const config_format_t *fmt,
  121. const char *option,
  122. int command_line, int warn_obsolete);
  123. void warn_deprecated_option(const char *what, const char *why);
  124. #endif