tor  0.4.2.0-alpha-dev
confparse.h
Go to the documentation of this file.
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 
13 #ifndef TOR_CONFPARSE_H
14 #define TOR_CONFPARSE_H
15 
17 typedef enum config_type_t {
51 
52 #ifdef TOR_UNIT_TESTS
53 
57 typedef union {
58  char **STRING;
59  char **FILENAME;
60  int *UINT; /* yes, really: Even though the confparse type is called
61  * "UINT", it still uses the C int type -- it just enforces that
62  * the values are in range [0,INT_MAX].
63  */
64  uint64_t *UINT64;
65  int *INT;
66  int *PORT;
67  int *INTERVAL;
68  int *MSEC_INTERVAL;
69  uint64_t *MEMUNIT;
70  double *DOUBLE;
71  int *BOOL;
72  int *AUTOBOOL;
73  time_t *ISOTIME;
74  smartlist_t **CSV;
75  int *CSV_INTERVAL;
76  struct config_line_t **LINELIST;
77  struct config_line_t **LINELIST_S;
78  struct config_line_t **LINELIST_V;
79  routerset_t **ROUTERSET;
80 } confparse_dummy_values_t;
81 #endif /* defined(TOR_UNIT_TESTS) */
82 
84 typedef struct config_abbrev_t {
85  const char *abbreviated;
86  const char *full;
87  int commandline_only;
88  int warn;
90 
91 typedef struct config_deprecation_t {
92  const char *name;
93  const char *why_deprecated;
95 
96 /* Handy macro for declaring "In the config file or on the command line,
97  * you can abbreviate <b>tok</b>s as <b>tok</b>". */
98 #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
99 
101 typedef struct config_var_t {
102  const char *name;
105  off_t var_offset;
106  const char *initvalue;
108 #ifdef TOR_UNIT_TESTS
109 
111  confparse_dummy_values_t var_ptr_dummy;
112 #endif
113 } config_var_t;
114 
115 /* Macros to define extra members inside config_var_t fields, and at the
116  * end of a list of them.
117  */
118 #ifdef TOR_UNIT_TESTS
119 /* This is a somewhat magic type-checking macro for users of confparse.c.
120  * It initializes a union member "confparse_dummy_values_t.conftype" with
121  * the address of a static member "tp_dummy.member". This
122  * will give a compiler warning unless the member field is of the correct
123  * type.
124  *
125  * (This warning is mandatory, because a type mismatch here violates the type
126  * compatibility constraint for simple assignment, and requires a diagnostic,
127  * according to the C spec.)
128  *
129  * For example, suppose you say:
130  * "CONF_CHECK_VAR_TYPE(or_options_t, STRING, Address)".
131  * Then this macro will evaluate to:
132  * { .STRING = &or_options_t_dummy.Address }
133  * And since confparse_dummy_values_t.STRING has type "char **", that
134  * expression will create a warning unless or_options_t.Address also
135  * has type "char *".
136  */
137 #define CONF_CHECK_VAR_TYPE(tp, conftype, member) \
138  { . conftype = &tp ## _dummy . member }
139 #define CONF_TEST_MEMBERS(tp, conftype, member) \
140  , CONF_CHECK_VAR_TYPE(tp, conftype, member)
141 #define END_OF_CONFIG_VARS \
142  { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, { .INT=NULL } }
143 #define DUMMY_TYPECHECK_INSTANCE(tp) \
144  static tp tp ## _dummy
145 #else /* !(defined(TOR_UNIT_TESTS)) */
146 #define CONF_TEST_MEMBERS(tp, conftype, member)
147 #define END_OF_CONFIG_VARS { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
148 /* Repeatedly declarable incomplete struct to absorb redundant semicolons */
149 #define DUMMY_TYPECHECK_INSTANCE(tp) \
150  struct tor_semicolon_eater
151 #endif /* defined(TOR_UNIT_TESTS) */
152 
156 typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
157 
159 typedef void (*free_cfg_fn_t)(void*);
160 
164 typedef struct config_format_t {
165  size_t size;
166  uint32_t magic;
168  off_t magic_offset;
171  const config_deprecation_t *deprecations;
180 
183 #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
184  tor_assert(fmt && cfg); \
185  tor_assert((fmt)->magic == \
186  *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
187  STMT_END
188 
189 #define CAL_USE_DEFAULTS (1u<<0)
190 #define CAL_CLEAR_FIRST (1u<<1)
191 #define CAL_WARN_DEPRECATIONS (1u<<2)
192 
193 void *config_new(const config_format_t *fmt);
194 void config_free_(const config_format_t *fmt, void *options);
195 #define config_free(fmt, options) do { \
196  config_free_((fmt), (options)); \
197  (options) = NULL; \
198  } while (0)
199 
201  const void *options, const char *key,
202  int escape_val);
203 int config_is_same(const config_format_t *fmt,
204  const void *o1, const void *o2,
205  const char *name);
206 void config_init(const config_format_t *fmt, void *options);
207 void *config_dup(const config_format_t *fmt, const void *old);
208 char *config_dump(const config_format_t *fmt, const void *default_options,
209  const void *options, int minimal,
210  int comment_defaults);
211 int config_assign(const config_format_t *fmt, void *options,
212  struct config_line_t *list,
213  unsigned flags, char **msg);
215  const char *key);
216 const char *config_find_deprecation(const config_format_t *fmt,
217  const char *key);
219  const char *key);
220 const char *config_expand_abbrev(const config_format_t *fmt,
221  const char *option,
222  int command_line, int warn_obsolete);
223 void warn_deprecated_option(const char *what, const char *why);
224 
225 /* Helper macros to compare an option across two configuration objects */
226 #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
227 #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
228 #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
229 #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
230 #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
231 #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
232 
233 #endif /* !defined(TOR_CONFPARSE_H) */
off_t var_offset
Definition: confparse.h:105
void config_free_(const config_format_t *fmt, void *options)
Definition: confparse.c:855
config_abbrev_t * abbrevs
Definition: confparse.h:169
off_t magic_offset
Definition: confparse.h:168
const config_var_t * config_find_option(const config_format_t *fmt, const char *key)
Definition: confparse.c:136
config_var_t * extra
Definition: confparse.h:178
struct config_format_t config_format_t
config_type_t
Definition: confparse.h:17
void config_init(const config_format_t *fmt, void *options)
Definition: confparse.c:926
config_type_t type
Definition: confparse.h:103
int config_is_same(const config_format_t *fmt, const void *o1, const void *o2, const char *name)
Definition: confparse.c:878
int(* validate_fn_t)(void *, void *, void *, int, char **)
Definition: confparse.h:156
const char * config_find_deprecation(const config_format_t *fmt, const char *key)
Definition: confparse.c:87
free_cfg_fn_t free_fn
Definition: confparse.h:175
struct config_line_t * config_get_assigned_option(const config_format_t *fmt, const void *options, const char *key, int escape_val)
Definition: confparse.c:532
const char * name
Definition: confparse.h:102
char * config_dump(const config_format_t *fmt, const void *default_options, const void *options, int minimal, int comment_defaults)
Definition: confparse.c:945
void * config_new(const config_format_t *fmt)
Definition: confparse.c:39
struct config_var_t config_var_t
const char * initvalue
Definition: confparse.h:106
config_var_t * vars
Definition: confparse.h:172
const char * config_expand_abbrev(const config_format_t *fmt, const char *option, int command_line, int warn_obsolete)
Definition: confparse.c:57
struct config_abbrev_t config_abbrev_t
config_var_t * config_find_option_mutable(config_format_t *fmt, const char *key)
Definition: confparse.c:105
uint32_t magic
Definition: confparse.h:166
validate_fn_t validate_fn
Definition: confparse.h:174
void * config_dup(const config_format_t *fmt, const void *old)
Definition: confparse.c:897
int config_assign(const config_format_t *fmt, void *options, struct config_line_t *list, unsigned flags, char **msg)
Definition: confparse.c:719
void(* free_cfg_fn_t)(void *)
Definition: confparse.h:159