1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright 2018, Olivier MATZ <zer0@droids-corp.org>
15 * The type identifier for a config value.
18 EC_CONFIG_TYPE_NONE = 0,
21 EC_CONFIG_TYPE_UINT64,
22 EC_CONFIG_TYPE_STRING,
29 * Structure describing the format of a configuration value.
31 * This structure is used in a const array which is referenced by a
32 * struct ec_config. Each entry of the array represents a key/value
33 * storage of the configuration dictionary.
35 struct ec_config_schema {
36 const char *key; /**< The key string (NULL for list elts). */
37 const char *desc; /**< A description of the value. */
38 enum ec_config_type type; /**< Type of the value */
40 /** If type is dict or list, the schema of the dict or list
41 * elements. Else must be NULL. */
42 const struct ec_config_schema *subschema;
44 /** The subschema array len in case of dict (> 0) or list (set
45 * to 1). Else must be 0. */
50 TAILQ_HEAD(ec_config_list, ec_config);
53 * Structure storing the configuration data.
56 /** type of value stored in the union */
57 enum ec_config_type type;
60 bool boolean; /** Boolean value */
61 int64_t i64; /** Signed integer value */
62 uint64_t u64; /** Unsigned integer value */
63 char *string; /** String value */
64 struct ec_node *node; /** Node value */
65 struct ec_keyval *dict; /** Hash table value */
66 struct ec_config_list list; /** List value */
70 * Next in list, only valid if type is list.
72 TAILQ_ENTRY(ec_config) next;
74 /** Associated schema. Only valid if type is dict. */
75 const struct ec_config_schema *schema;
76 size_t schema_len; /**< Schema length. */
82 * Validate a configuration schema array.
85 * Pointer to the first element of the schema array.
87 * Length of the schema array.
89 * 0 if the schema is valid, or -1 on error (errno is set).
91 int ec_config_schema_validate(const struct ec_config_schema *schema,
95 * Dump a configuration schema array.
98 * Output stream on which the dump will be sent.
100 * Pointer to the first element of the schema array.
102 * Length of the schema array.
104 void ec_config_schema_dump(FILE *out, const struct ec_config_schema *schema,
111 * Create a boolean configuration value.
114 * The boolean value to be set.
116 * The configuration object, or NULL on error (errno is set).
118 struct ec_config *ec_config_bool(bool boolean);
121 * Create a signed integer configuration value.
124 * The signed integer value to be set.
126 * The configuration object, or NULL on error (errno is set).
128 struct ec_config *ec_config_i64(int64_t i64);
131 * Create an unsigned configuration value.
134 * The unsigned integer value to be set.
136 * The configuration object, or NULL on error (errno is set).
138 struct ec_config *ec_config_u64(uint64_t u64);
141 * Create a string configuration value.
144 * The string value to be set. The string is copied into the
145 * configuration object.
147 * The configuration object, or NULL on error (errno is set).
149 struct ec_config *ec_config_string(const char *string);
152 * Create a node configuration value.
155 * The node pointer to be set. The node is "consumed" by
156 * the function and should not be used by the caller, even
157 * on error. The caller can use ec_node_clone() to keep a
158 * reference on the node.
160 * The configuration object, or NULL on error (errno is set).
162 struct ec_config *ec_config_node(struct ec_node *node);
165 * Create a hash table configuration value.
168 * Optional pointer to the first element of the schema array. Set
169 * it to NULL if no schema should be attached.
171 * Length of the schema array. Set to 0 if no schema.
173 * The configuration object containing an empty hash table, or NULL on
174 * error (errno is set).
176 struct ec_config *ec_config_dict(const struct ec_config_schema *schema,
180 * Create a list configuration value.
183 * The configuration object containing an empty list, or NULL on
184 * error (errno is set).
186 struct ec_config *ec_config_list(void);
189 * Add a config object into a list.
192 * The list configuration in which the value will be added.
194 * The value configuration to add in the list. The value object
195 * will be freed when freeing the list object. On error, the
196 * value object is freed.
198 * 0 on success, else -1 (errno is set).
200 int ec_config_add(struct ec_config *list, struct ec_config *value);
202 void ec_config_free(struct ec_config *config);
204 int ec_config_validate(const struct ec_config *dict);
206 /* value is consumed */
207 int ec_config_set(struct ec_config *dict, const char *key,
208 struct ec_config *value);
211 * Compare two configurations.
213 int ec_config_cmp(const struct ec_config *config1,
214 const struct ec_config *config2);
217 * Get configuration value.
219 struct ec_config *ec_config_get(struct ec_config *config,
223 * Get the first element of a list.
226 * for (config = ec_config_list_iter(list);
228 * config = ec_config_list_next(list, config)) {
233 * The list configuration to iterate.
235 * The first configuration element, or NULL on error (errno is set).
237 struct ec_config *ec_config_list_first(struct ec_config *list);
240 * Get next element in list.
243 * The list configuration beeing iterated.
245 * The current configuration element.
247 * The next configuration element, or NULL if there is no more element.
250 ec_config_list_next(struct ec_config *list, struct ec_config *config);
253 * Remove an element from a list.
255 * The element is freed and should not be accessed.
258 * The list configuration.
260 * The element to remove from the list.
262 void ec_config_del(struct ec_config *list, struct ec_config *config);
265 * Free a configuration.
268 * The element to free.
270 void ec_config_free(struct ec_config *config);
273 * Compare two configurations.
276 * 0 if the configurations are equal, else -1.
278 int ec_config_cmp(const struct ec_config *value1,
279 const struct ec_config *value2);
282 * Dump a configuration.
285 * Output stream on which the dump will be sent.
287 * The configuration to dump.
289 void ec_config_dump(FILE *out, const struct ec_config *config);