tor  master
confparse.h
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-2017, The Tor Project, Inc. */
5 /* See LICENSE for licensing information */
6 
7 #ifndef TOR_CONFPARSE_H
8 #define TOR_CONFPARSE_H
9 
11 typedef enum config_type_t {
12  CONFIG_TYPE_STRING = 0,
13  CONFIG_TYPE_FILENAME,
14  CONFIG_TYPE_UINT,
15  CONFIG_TYPE_INT,
16  CONFIG_TYPE_PORT,
18  CONFIG_TYPE_INTERVAL,
19  CONFIG_TYPE_MSEC_INTERVAL,
21  CONFIG_TYPE_MEMUNIT,
22  CONFIG_TYPE_DOUBLE,
23  CONFIG_TYPE_BOOL,
24  CONFIG_TYPE_AUTOBOOL,
26  CONFIG_TYPE_ISOTIME,
27  CONFIG_TYPE_CSV,
29  CONFIG_TYPE_CSV_INTERVAL,
34  CONFIG_TYPE_LINELIST,
35  CONFIG_TYPE_LINELIST_S,
37  CONFIG_TYPE_LINELIST_V,
40  CONFIG_TYPE_ROUTERSET,
42  CONFIG_TYPE_OBSOLETE,
43 } config_type_t;
44 
45 #ifdef TOR_UNIT_TESTS
46 
50 typedef union {
51  char **STRING;
52  char **FILENAME;
53  int *UINT; /* yes, really: Even though the confparse type is called
54  * "UINT", it still uses the C int type -- it just enforces that
55  * the values are in range [0,INT_MAX].
56  */
57  int *INT;
58  int *PORT;
59  int *INTERVAL;
60  int *MSEC_INTERVAL;
61  uint64_t *MEMUNIT;
62  double *DOUBLE;
63  int *BOOL;
64  int *AUTOBOOL;
65  time_t *ISOTIME;
66  smartlist_t **CSV;
67  int *CSV_INTERVAL;
68  config_line_t **LINELIST;
69  config_line_t **LINELIST_S;
70  config_line_t **LINELIST_V;
71  routerset_t **ROUTERSET;
72 } confparse_dummy_values_t;
73 #endif /* defined(TOR_UNIT_TESTS) */
74 
76 typedef struct config_abbrev_t {
77  const char *abbreviated;
78  const char *full;
79  int commandline_only;
80  int warn;
82 
83 typedef struct config_deprecation_t {
84  const char *name;
85  const char *why_deprecated;
87 
88 /* Handy macro for declaring "In the config file or on the command line,
89  * you can abbreviate <b>tok</b>s as <b>tok</b>". */
90 #define PLURAL(tok) { #tok, #tok "s", 0, 0 }
91 
93 typedef struct config_var_t {
94  const char *name;
95  config_type_t type;
97  off_t var_offset;
98  const char *initvalue;
100 #ifdef TOR_UNIT_TESTS
101 
103  confparse_dummy_values_t var_ptr_dummy;
104 #endif
105 } config_var_t;
106 
107 /* Macros to define extra members inside config_var_t fields, and at the
108  * end of a list of them.
109  */
110 #ifdef TOR_UNIT_TESTS
111 /* This is a somewhat magic type-checking macro for users of confparse.c.
112  * It initializes a union member "confparse_dummy_values_t.conftype" with
113  * the address of a static member "tp_dummy.member". This
114  * will give a compiler warning unless the member field is of the correct
115  * type.
116  *
117  * (This warning is mandatory, because a type mismatch here violates the type
118  * compatibility constraint for simple assignment, and requires a diagnostic,
119  * according to the C spec.)
120  *
121  * For example, suppose you say:
122  * "CONF_CHECK_VAR_TYPE(or_options_t, STRING, Address)".
123  * Then this macro will evaluate to:
124  * { .STRING = &or_options_t_dummy.Address }
125  * And since confparse_dummy_values_t.STRING has type "char **", that
126  * expression will create a warning unless or_options_t.Address also
127  * has type "char *".
128  */
129 #define CONF_CHECK_VAR_TYPE(tp, conftype, member) \
130  { . conftype = &tp ## _dummy . member }
131 #define CONF_TEST_MEMBERS(tp, conftype, member) \
132  , CONF_CHECK_VAR_TYPE(tp, conftype, member)
133 #define END_OF_CONFIG_VARS \
134  { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL, { .INT=NULL } }
135 #define DUMMY_TYPECHECK_INSTANCE(tp) \
136  static tp tp ## _dummy
137 #else /* !(defined(TOR_UNIT_TESTS)) */
138 #define CONF_TEST_MEMBERS(tp, conftype, member)
139 #define END_OF_CONFIG_VARS { NULL, CONFIG_TYPE_OBSOLETE, 0, NULL }
140 /* Repeatedly declarable incomplete struct to absorb redundant semicolons */
141 #define DUMMY_TYPECHECK_INSTANCE(tp) \
142  struct tor_semicolon_eater
143 #endif /* defined(TOR_UNIT_TESTS) */
144 
148 typedef int (*validate_fn_t)(void*,void*,void*,int,char**);
149 
153 typedef struct config_format_t {
154  size_t size;
155  uint32_t magic;
157  off_t magic_offset;
160  const config_deprecation_t *deprecations;
163  validate_fn_t validate_fn;
168 
171 #define CONFIG_CHECK(fmt, cfg) STMT_BEGIN \
172  tor_assert(fmt && cfg); \
173  tor_assert((fmt)->magic == \
174  *(uint32_t*)STRUCT_VAR_P(cfg,fmt->magic_offset)); \
175  STMT_END
176 
177 #define CAL_USE_DEFAULTS (1u<<0)
178 #define CAL_CLEAR_FIRST (1u<<1)
179 #define CAL_WARN_DEPRECATIONS (1u<<2)
180 
181 void *config_new(const config_format_t *fmt);
182 void config_free_(const config_format_t *fmt, void *options);
183 #define config_free(fmt, options) do { \
184  config_free_((fmt), (options)); \
185  (options) = NULL; \
186  } while (0)
187 
189  const void *options, const char *key,
190  int escape_val);
191 int config_is_same(const config_format_t *fmt,
192  const void *o1, const void *o2,
193  const char *name);
194 void config_init(const config_format_t *fmt, void *options);
195 void *config_dup(const config_format_t *fmt, const void *old);
196 char *config_dump(const config_format_t *fmt, const void *default_options,
197  const void *options, int minimal,
198  int comment_defaults);
199 int config_assign(const config_format_t *fmt, void *options,
200  config_line_t *list,
201  unsigned flags, char **msg);
203  const char *key);
204 const char *config_find_deprecation(const config_format_t *fmt,
205  const char *key);
207  const char *key);
208 const char *config_expand_abbrev(const config_format_t *fmt,
209  const char *option,
210  int command_line, int warn_obsolete);
211 void warn_deprecated_option(const char *what, const char *why);
212 
213 /* Helper macros to compare an option across two configuration objects */
214 #define CFG_EQ_BOOL(a,b,opt) ((a)->opt == (b)->opt)
215 #define CFG_EQ_INT(a,b,opt) ((a)->opt == (b)->opt)
216 #define CFG_EQ_STRING(a,b,opt) (!strcmp_opt((a)->opt, (b)->opt))
217 #define CFG_EQ_SMARTLIST(a,b,opt) smartlist_strings_eq((a)->opt, (b)->opt)
218 #define CFG_EQ_LINELIST(a,b,opt) config_lines_eq((a)->opt, (b)->opt)
219 #define CFG_EQ_ROUTERSET(a,b,opt) routerset_equal((a)->opt, (b)->opt)
220 
221 #endif /* !defined(TOR_CONFPARSE_H) */
222 
void config_init(const config_format_t *fmt, void *options)
Definition: confparse.c:909
Definition: confline.h:23
Definition: confparse.h:93
validate_fn_t validate_fn
Definition: confparse.h:163
config_type_t type
Definition: confparse.h:95
Definition: confparse.h:153
const char * config_expand_abbrev(const config_format_t *fmt, const char *option, int command_line, int warn_obsolete)
Definition: confparse.c:55
Definition: container.h:18
char * config_dump(const config_format_t *fmt, const void *default_options, const void *options, int minimal, int comment_defaults)
Definition: confparse.c:928
config_line_t * config_get_assigned_option(const config_format_t *fmt, const void *options, const char *key, int escape_val)
Definition: confparse.c:517
const config_var_t * config_find_option(const config_format_t *fmt, const char *key)
Definition: confparse.c:134
uint32_t magic
Definition: confparse.h:155
size_t size
Definition: confparse.h:154
config_var_t * config_find_option_mutable(config_format_t *fmt, const char *key)
Definition: confparse.c:103
void * config_dup(const config_format_t *fmt, const void *old)
Definition: confparse.c:880
config_var_t * extra
Definition: confparse.h:166
void config_free_(const config_format_t *fmt, void *options)
Definition: confparse.c:838
off_t magic_offset
Definition: confparse.h:157
Definition: confparse.h:83
const char * config_find_deprecation(const config_format_t *fmt, const char *key)
Definition: confparse.c:85
const char * initvalue
Definition: confparse.h:98
int config_is_same(const config_format_t *fmt, const void *o1, const void *o2, const char *name)
Definition: confparse.c:861
off_t var_offset
Definition: confparse.h:97
int config_assign(const config_format_t *fmt, void *options, config_line_t *list, unsigned config_assign_flags, char **msg)
Definition: confparse.c:703
config_var_t * vars
Definition: confparse.h:161
const char * name
Definition: confparse.h:94
config_abbrev_t * abbrevs
Definition: confparse.h:158
Definition: confparse.h:76
void * config_new(const config_format_t *fmt)
Definition: confparse.c:37