var_type_def_st.h 6.3 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 var_type_def_st.h
  8. * @brief Structure declarations for typedvar type definitions.
  9. *
  10. * This structure is used for defining new variable types. If you are not
  11. * defining a new variable type for use by the configuration management
  12. * system, you don't need this structure.
  13. *
  14. * For defining new variables, see the types in conftypes.h.
  15. *
  16. * For data-driven access to configuration variables, see the other members of
  17. * lib/confmgt/.
  18. *
  19. * STATUS NOTE: It is not yet possible to actually define new variables
  20. * outside of config.c, and many of the types that will eventually be used
  21. * to do so are not yet moved. This will change as more of #29211 is
  22. * completed.
  23. **/
  24. #ifndef TOR_LIB_CONFMGT_VAR_TYPE_DEF_ST_H
  25. #define TOR_LIB_CONFMGT_VAR_TYPE_DEF_ST_H
  26. #include <stdbool.h>
  27. struct config_line_t;
  28. /**
  29. * A structure full of functions pointers to implement a variable type.
  30. *
  31. * Every type MUST implement parse or kv_parse and encode or kv_encode;
  32. * the other functions pointers MAY be NULL.
  33. *
  34. * All functions here take a <b>params</b> argument, whose value
  35. * is determined by the type definition. Two types may have the
  36. * same functions, but differ only in parameters.
  37. *
  38. * Implementation considerations: If "" encodes a valid value for a type, try
  39. * to make sure that it encodes the same thing as the default value for the
  40. * type (that is, the value that is set by config_clear() or memset(0)). If
  41. * this is not the case, you need to make extra certain that your parse/encode
  42. * implementations preserve the NULL/"" distinction.
  43. **/
  44. struct var_type_fns_t {
  45. /**
  46. * Try to parse a string in <b>value</b> that encodes an object of this
  47. * type. On success, adjust the lvalue pointed to by <b>target</b> to hold
  48. * that value, and return 0. On failure, set *<b>errmsg</b> to a newly
  49. * allocated string holding an error message, and return -1.
  50. *
  51. * If not NULL, <b>key</b> is the name of the option, which may be used for
  52. * logging.
  53. **/
  54. int (*parse)(void *target, const char *value, char **errmsg,
  55. const void *params, const char *key);
  56. /**
  57. * Try to parse a single line from the head of<b>line</b> that encodes
  58. * an object of this type. On success and failure, behave as in the parse()
  59. * function.
  60. *
  61. * If this function is absent, it is implemented in terms of parse().
  62. *
  63. * All types for which keys are significant should use this method. For
  64. * example, a "linelist" type records the actual keys that are given
  65. * for each line, and so should use this method.
  66. *
  67. * Note that although multiple lines may be provided in <b>line</b>,
  68. * only the first one should be handled by this function.
  69. **/
  70. int (*kv_parse)(void *target, const struct config_line_t *line,
  71. char **errmsg, const void *params);
  72. /**
  73. * Encode a value pointed to by <b>value</b> and return its result
  74. * in a newly allocated string. The string may need to be escaped.
  75. *
  76. * If this function is absent, it is implemented in terms of kv_encode().
  77. *
  78. * Returns NULL if this option has a NULL value, or on internal error.
  79. *
  80. * Requirement: all strings generated by encode() should produce a
  81. * semantically equivalent value when given to parse().
  82. **/
  83. char *(*encode)(const void *value, const void *params);
  84. /**
  85. * As encode(), but returns a newly allocated config_line_t object. The
  86. * provided <b>key</b> is used as the key of the lines, unless the type is
  87. * one that encodes its own keys.
  88. *
  89. * Unlike kv_parse(), this function will return a list of multiple lines,
  90. * if <b>value</b> is such that it must be encoded by multiple lines.
  91. *
  92. * Returns NULL if there are no lines to encode, or on internal error.
  93. *
  94. * If this function is absent, it is implemented in terms of encode().
  95. **/
  96. struct config_line_t *(*kv_encode)(const char *key, const void *value,
  97. const void *params);
  98. /**
  99. * Free all storage held in <b>arg</b>, and set <b>arg</b> to a default
  100. * value -- usually zero or NULL.
  101. *
  102. * If this function is absent, the default implementation does nothing.
  103. **/
  104. void (*clear)(void *arg, const void *params);
  105. /**
  106. * Return true if <b>a</b> and <b>b</b> hold the same value, and false
  107. * otherwise.
  108. *
  109. * If this function is absent, it is implemented by encoding both a and
  110. * b and comparing their encoded strings for equality.
  111. **/
  112. bool (*eq)(const void *a, const void *b, const void *params);
  113. /**
  114. * Try to copy the value from <b>value</b> into <b>target</b>.
  115. * On success return 0; on failure return -1.
  116. *
  117. * If this function is absent, it is implemented by encoding the value
  118. * into a string, and then parsing it into the target.
  119. **/
  120. int (*copy)(void *target, const void *value, const void *params);
  121. /**
  122. * Check whether <b>value</b> holds a valid value according to the
  123. * rules of this type; return true if it does and false if it doesn't.
  124. *
  125. * The default implementation for this function assumes that all
  126. * values are valid.
  127. **/
  128. bool (*ok)(const void *value, const void *params);
  129. /**
  130. * Mark a value of this variable as "fragile", so that future attempts to
  131. * assign to this variable will replace rather than extending it.
  132. *
  133. * The default implementation for this function does nothing.
  134. *
  135. * Only meaningful for types with is_cumulative set.
  136. **/
  137. void (*mark_fragile)(void *value, const void *params);
  138. };
  139. /**
  140. * A structure describing a type that can be manipulated with the typedvar_*
  141. * functions.
  142. **/
  143. struct var_type_def_t {
  144. /**
  145. * The name of this type. Should not include spaces. Used for
  146. * debugging, log messages, and the controller API. */
  147. const char *name;
  148. /**
  149. * A function table for this type.
  150. */
  151. const struct var_type_fns_t *fns;
  152. /**
  153. * A pointer to a value that should be passed as the 'params' argument when
  154. * calling the functions in this type's function table.
  155. */
  156. const void *params;
  157. /**
  158. * A bitwise OR of one or more VTFLAG_* values, describing properties
  159. * for all values of this type.
  160. **/
  161. uint32_t flags;
  162. };
  163. #endif /* !defined(TOR_LIB_CONFMGT_VAR_TYPE_DEF_ST_H) */