xref: /freebsd/contrib/libucl/include/ucl.h (revision 0572ccaa4543b0abef8ef81e384c1d04de9f3da1)
1 /* Copyright (c) 2013, Vsevolod Stakhov
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *       * Redistributions of source code must retain the above copyright
7  *         notice, this list of conditions and the following disclaimer.
8  *       * Redistributions in binary form must reproduce the above copyright
9  *         notice, this list of conditions and the following disclaimer in the
10  *         documentation and/or other materials provided with the distribution.
11  *
12  * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
13  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15  * DISCLAIMED. IN NO EVENT SHALL AUTHOR BE LIABLE FOR ANY
16  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
17  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
18  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
19  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
20  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
21  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
22  */
23 
24 #ifndef UCL_H_
25 #define UCL_H_
26 
27 #include <string.h>
28 #include <stddef.h>
29 #include <stdlib.h>
30 #include <stdint.h>
31 #include <stdbool.h>
32 #include <stdarg.h>
33 #include <stdio.h>
34 
35 #ifdef _WIN32
36 # define UCL_EXTERN __declspec(dllexport)
37 #else
38 # define UCL_EXTERN
39 #endif
40 
41 /**
42  * @mainpage
43  * This is a reference manual for UCL API. You may find the description of UCL format by following this
44  * [github repository](https://github.com/vstakhov/libucl).
45  *
46  * This manual has several main sections:
47  *  - @ref structures
48  *  - @ref utils
49  *  - @ref parser
50  *  - @ref emitter
51  */
52 
53 /**
54  * @file ucl.h
55  * @brief UCL parsing and emitting functions
56  *
57  * UCL is universal configuration language, which is a form of
58  * JSON with less strict rules that make it more comfortable for
59  * using as a configuration language
60  */
61 #ifdef  __cplusplus
62 extern "C" {
63 #endif
64 /*
65  * Memory allocation utilities
66  * UCL_ALLOC(size) - allocate memory for UCL
67  * UCL_FREE(size, ptr) - free memory of specified size at ptr
68  * Default: malloc and free
69  */
70 #ifndef UCL_ALLOC
71 #define UCL_ALLOC(size) malloc(size)
72 #endif
73 #ifndef UCL_FREE
74 #define UCL_FREE(size, ptr) free(ptr)
75 #endif
76 
77 #if    __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4)
78 #define UCL_WARN_UNUSED_RESULT               \
79   __attribute__((warn_unused_result))
80 #else
81 #define UCL_WARN_UNUSED_RESULT
82 #endif
83 
84 #ifdef __GNUC__
85 #define UCL_DEPRECATED(func) func __attribute__ ((deprecated))
86 #elif defined(_MSC_VER)
87 #define UCL_DEPRECATED(func) __declspec(deprecated) func
88 #else
89 #define UCL_DEPRECATED(func) func
90 #endif
91 
92 /**
93  * @defgroup structures Structures and types
94  * UCL defines several enumeration types used for error reporting or specifying flags and attributes.
95  *
96  * @{
97  */
98 
99 /**
100  * The common error codes returned by ucl parser
101  */
102 typedef enum ucl_error {
103 	UCL_EOK = 0, /**< No error */
104 	UCL_ESYNTAX, /**< Syntax error occurred during parsing */
105 	UCL_EIO, /**< IO error occurred during parsing */
106 	UCL_ESTATE, /**< Invalid state machine state */
107 	UCL_ENESTED, /**< Input has too many recursion levels */
108 	UCL_EMACRO, /**< Error processing a macro */
109 	UCL_EINTERNAL, /**< Internal unclassified error */
110 	UCL_ESSL /**< SSL error */
111 } ucl_error_t;
112 
113 /**
114  * #ucl_object_t may have one of specified types, some types are compatible with each other and some are not.
115  * For example, you can always convert #UCL_TIME to #UCL_FLOAT. Also you can convert #UCL_FLOAT to #UCL_INTEGER
116  * by loosing floating point. Every object may be converted to a string by #ucl_object_tostring_forced() function.
117  *
118  */
119 typedef enum ucl_type {
120 	UCL_OBJECT = 0, /**< UCL object - key/value pairs */
121 	UCL_ARRAY, /**< UCL array */
122 	UCL_INT, /**< Integer number */
123 	UCL_FLOAT, /**< Floating point number */
124 	UCL_STRING, /**< Null terminated string */
125 	UCL_BOOLEAN, /**< Boolean value */
126 	UCL_TIME, /**< Time value (floating point number of seconds) */
127 	UCL_USERDATA, /**< Opaque userdata pointer (may be used in macros) */
128 	UCL_NULL /**< Null value */
129 } ucl_type_t;
130 
131 /**
132  * You can use one of these types to serialise #ucl_object_t by using ucl_object_emit().
133  */
134 typedef enum ucl_emitter {
135 	UCL_EMIT_JSON = 0, /**< Emit fine formatted JSON */
136 	UCL_EMIT_JSON_COMPACT, /**< Emit compacted JSON */
137 	UCL_EMIT_CONFIG, /**< Emit human readable config format */
138 	UCL_EMIT_YAML /**< Emit embedded YAML format */
139 } ucl_emitter_t;
140 
141 /**
142  * These flags defines parser behaviour. If you specify #UCL_PARSER_ZEROCOPY you must ensure
143  * that the input memory is not freed if an object is in use. Moreover, if you want to use
144  * zero-terminated keys and string values then you should not use zero-copy mode, as in this case
145  * UCL still has to perform copying implicitly.
146  */
147 typedef enum ucl_parser_flags {
148 	UCL_PARSER_KEY_LOWERCASE = 0x1, /**< Convert all keys to lower case */
149 	UCL_PARSER_ZEROCOPY = 0x2, /**< Parse input in zero-copy mode if possible */
150 	UCL_PARSER_NO_TIME = 0x4 /**< Do not parse time and treat time values as strings */
151 } ucl_parser_flags_t;
152 
153 /**
154  * String conversion flags, that are used in #ucl_object_fromstring_common function.
155  */
156 typedef enum ucl_string_flags {
157 	UCL_STRING_ESCAPE = 0x1,  /**< Perform JSON escape */
158 	UCL_STRING_TRIM = 0x2,    /**< Trim leading and trailing whitespaces */
159 	UCL_STRING_PARSE_BOOLEAN = 0x4,    /**< Parse passed string and detect boolean */
160 	UCL_STRING_PARSE_INT = 0x8,    /**< Parse passed string and detect integer number */
161 	UCL_STRING_PARSE_DOUBLE = 0x10,    /**< Parse passed string and detect integer or float number */
162 	UCL_STRING_PARSE_TIME = 0x20, /**< Parse time strings */
163 	UCL_STRING_PARSE_NUMBER =  UCL_STRING_PARSE_INT|UCL_STRING_PARSE_DOUBLE|UCL_STRING_PARSE_TIME,  /**<
164 									Parse passed string and detect number */
165 	UCL_STRING_PARSE =  UCL_STRING_PARSE_BOOLEAN|UCL_STRING_PARSE_NUMBER,   /**<
166 									Parse passed string (and detect booleans and numbers) */
167 	UCL_STRING_PARSE_BYTES = 0x40  /**< Treat numbers as bytes */
168 } ucl_string_flags_t;
169 
170 /**
171  * Basic flags for an object
172  */
173 typedef enum ucl_object_flags {
174 	UCL_OBJECT_ALLOCATED_KEY = 1, /**< An object has key allocated internally */
175 	UCL_OBJECT_ALLOCATED_VALUE = 2, /**< An object has a string value allocated internally */
176 	UCL_OBJECT_NEED_KEY_ESCAPE = 4 /**< The key of an object need to be escaped on output */
177 } ucl_object_flags_t;
178 
179 /**
180  * UCL object structure. Please mention that the most of fields should not be touched by
181  * UCL users. In future, this structure may be converted to private one.
182  */
183 typedef struct ucl_object_s {
184 	/**
185 	 * Variant value type
186 	 */
187 	union {
188 		int64_t iv;							/**< Int value of an object */
189 		const char *sv;					/**< String value of an object */
190 		double dv;							/**< Double value of an object */
191 		struct ucl_object_s *av;			/**< Array					*/
192 		void *ov;							/**< Object					*/
193 		void* ud;							/**< Opaque user data		*/
194 	} value;
195 	const char *key;						/**< Key of an object		*/
196 	struct ucl_object_s *next;				/**< Array handle			*/
197 	struct ucl_object_s *prev;				/**< Array handle			*/
198 	unsigned char* trash_stack[2];			/**< Pointer to allocated chunks */
199 	unsigned keylen;						/**< Lenght of a key		*/
200 	unsigned len;							/**< Size of an object		*/
201 	enum ucl_type type;						/**< Real type				*/
202 	uint16_t ref;							/**< Reference count		*/
203 	uint16_t flags;							/**< Object flags			*/
204 } ucl_object_t;
205 
206 /** @} */
207 
208 /**
209  * @defgroup utils Utility functions
210  * A number of utility functions simplify handling of UCL objects
211  *
212  * @{
213  */
214 /**
215  * Copy and return a key of an object, returned key is zero-terminated
216  * @param obj CL object
217  * @return zero terminated key
218  */
219 UCL_EXTERN char* ucl_copy_key_trash (const ucl_object_t *obj);
220 
221 /**
222  * Copy and return a string value of an object, returned key is zero-terminated
223  * @param obj CL object
224  * @return zero terminated string representation of object value
225  */
226 UCL_EXTERN char* ucl_copy_value_trash (const ucl_object_t *obj);
227 
228 /**
229  * Creates a new object
230  * @return new object
231  */
232 UCL_EXTERN ucl_object_t* ucl_object_new (void) UCL_WARN_UNUSED_RESULT;
233 
234 /**
235  * Create new object with type specified
236  * @param type type of a new object
237  * @return new object
238  */
239 UCL_EXTERN ucl_object_t* ucl_object_typed_new (unsigned int type) UCL_WARN_UNUSED_RESULT;
240 
241 /**
242  * Convert any string to an ucl object making the specified transformations
243  * @param str fixed size or NULL terminated string
244  * @param len length (if len is zero, than str is treated as NULL terminated)
245  * @param flags conversion flags
246  * @return new object
247  */
248 UCL_EXTERN ucl_object_t * ucl_object_fromstring_common (const char *str, size_t len,
249 		enum ucl_string_flags flags) UCL_WARN_UNUSED_RESULT;
250 
251 /**
252  * Create a UCL object from the specified string
253  * @param str NULL terminated string, will be json escaped
254  * @return new object
255  */
256 UCL_EXTERN ucl_object_t *ucl_object_fromstring (const char *str) UCL_WARN_UNUSED_RESULT;
257 
258 /**
259  * Create a UCL object from the specified string
260  * @param str fixed size string, will be json escaped
261  * @param len length of a string
262  * @return new object
263  */
264 UCL_EXTERN ucl_object_t *ucl_object_fromlstring (const char *str,
265 		size_t len) UCL_WARN_UNUSED_RESULT;
266 
267 /**
268  * Create an object from an integer number
269  * @param iv number
270  * @return new object
271  */
272 UCL_EXTERN ucl_object_t* ucl_object_fromint (int64_t iv) UCL_WARN_UNUSED_RESULT;
273 
274 /**
275  * Create an object from a float number
276  * @param dv number
277  * @return new object
278  */
279 UCL_EXTERN ucl_object_t* ucl_object_fromdouble (double dv) UCL_WARN_UNUSED_RESULT;
280 
281 /**
282  * Create an object from a boolean
283  * @param bv bool value
284  * @return new object
285  */
286 UCL_EXTERN ucl_object_t* ucl_object_frombool (bool bv) UCL_WARN_UNUSED_RESULT;
287 
288 /**
289  * Insert a object 'elt' to the hash 'top' and associate it with key 'key'
290  * @param top destination object (will be created automatically if top is NULL)
291  * @param elt element to insert (must NOT be NULL)
292  * @param key key to associate with this object (either const or preallocated)
293  * @param keylen length of the key (or 0 for NULL terminated keys)
294  * @param copy_key make an internal copy of key
295  * @return true if key has been inserted
296  */
297 UCL_EXTERN bool ucl_object_insert_key (ucl_object_t *top, ucl_object_t *elt,
298 		const char *key, size_t keylen, bool copy_key);
299 
300 /**
301  * Replace a object 'elt' to the hash 'top' and associate it with key 'key', old object will be unrefed,
302  * if no object has been found this function works like ucl_object_insert_key()
303  * @param top destination object (will be created automatically if top is NULL)
304  * @param elt element to insert (must NOT be NULL)
305  * @param key key to associate with this object (either const or preallocated)
306  * @param keylen length of the key (or 0 for NULL terminated keys)
307  * @param copy_key make an internal copy of key
308  * @return true if key has been inserted
309  */
310 UCL_EXTERN bool ucl_object_replace_key (ucl_object_t *top, ucl_object_t *elt,
311 		const char *key, size_t keylen, bool copy_key);
312 
313 /**
314  * Delete a object associated with key 'key', old object will be unrefered,
315  * @param top object
316  * @param key key associated to the object to remove
317  * @param keylen length of the key (or 0 for NULL terminated keys)
318  */
319 UCL_EXTERN bool ucl_object_delete_keyl (ucl_object_t *top,
320 		const char *key, size_t keylen);
321 
322 /**
323  * Delete a object associated with key 'key', old object will be unrefered,
324  * @param top object
325  * @param key key associated to the object to remove
326  */
327 UCL_EXTERN bool ucl_object_delete_key (ucl_object_t *top,
328 		const char *key);
329 
330 
331 /**
332  * Delete key from `top` object returning the object deleted. This object is not
333  * released
334  * @param top object
335  * @param key key to remove
336  * @param keylen length of the key (or 0 for NULL terminated keys)
337  * @return removed object or NULL if object has not been found
338  */
339 UCL_EXTERN ucl_object_t* ucl_object_pop_keyl (ucl_object_t *top, const char *key,
340 		size_t keylen) UCL_WARN_UNUSED_RESULT;
341 
342 /**
343  * Delete key from `top` object returning the object deleted. This object is not
344  * released
345  * @param top object
346  * @param key key to remove
347  * @return removed object or NULL if object has not been found
348  */
349 UCL_EXTERN ucl_object_t* ucl_object_pop_key (ucl_object_t *top, const char *key)
350 	UCL_WARN_UNUSED_RESULT;
351 
352 /**
353  * Insert a object 'elt' to the hash 'top' and associate it with key 'key', if the specified key exist,
354  * try to merge its content
355  * @param top destination object (will be created automatically if top is NULL)
356  * @param elt element to insert (must NOT be NULL)
357  * @param key key to associate with this object (either const or preallocated)
358  * @param keylen length of the key (or 0 for NULL terminated keys)
359  * @param copy_key make an internal copy of key
360  * @return true if key has been inserted
361  */
362 UCL_EXTERN bool ucl_object_insert_key_merged (ucl_object_t *top, ucl_object_t *elt,
363 		const char *key, size_t keylen, bool copy_key);
364 
365 /**
366  * Append an element to the front of array object
367  * @param top destination object (will be created automatically if top is NULL)
368  * @param elt element to append (must NOT be NULL)
369  * @return true if value has been inserted
370  */
371 UCL_EXTERN bool ucl_array_append (ucl_object_t *top,
372 		ucl_object_t *elt);
373 
374 /**
375  * Append an element to the start of array object
376  * @param top destination object (will be created automatically if top is NULL)
377  * @param elt element to append (must NOT be NULL)
378  * @return true if value has been inserted
379  */
380 UCL_EXTERN bool ucl_array_prepend (ucl_object_t *top,
381 		ucl_object_t *elt);
382 
383 /**
384  * Removes an element `elt` from the array `top`. Caller must unref the returned object when it is not
385  * needed.
386  * @param top array ucl object
387  * @param elt element to remove
388  * @return removed element or NULL if `top` is NULL or not an array
389  */
390 UCL_EXTERN ucl_object_t* ucl_array_delete (ucl_object_t *top,
391 		ucl_object_t *elt);
392 
393 /**
394  * Returns the first element of the array `top`
395  * @param top array ucl object
396  * @return element or NULL if `top` is NULL or not an array
397  */
398 UCL_EXTERN const ucl_object_t* ucl_array_head (const ucl_object_t *top);
399 
400 /**
401  * Returns the last element of the array `top`
402  * @param top array ucl object
403  * @return element or NULL if `top` is NULL or not an array
404  */
405 UCL_EXTERN const ucl_object_t* ucl_array_tail (const ucl_object_t *top);
406 
407 /**
408  * Removes the last element from the array `top`. Caller must unref the returned object when it is not
409  * needed.
410  * @param top array ucl object
411  * @return removed element or NULL if `top` is NULL or not an array
412  */
413 UCL_EXTERN ucl_object_t* ucl_array_pop_last (ucl_object_t *top);
414 
415 /**
416  * Removes the first element from the array `top`. Caller must unref the returned object when it is not
417  * needed.
418  * @param top array ucl object
419  * @return removed element or NULL if `top` is NULL or not an array
420  */
421 UCL_EXTERN ucl_object_t* ucl_array_pop_first (ucl_object_t *top);
422 
423 /**
424  * Append a element to another element forming an implicit array
425  * @param head head to append (may be NULL)
426  * @param elt new element
427  * @return true if element has been inserted
428  */
429 UCL_EXTERN ucl_object_t * ucl_elt_append (ucl_object_t *head,
430 		ucl_object_t *elt);
431 
432 /**
433  * Converts an object to double value
434  * @param obj CL object
435  * @param target target double variable
436  * @return true if conversion was successful
437  */
438 UCL_EXTERN bool ucl_object_todouble_safe (const ucl_object_t *obj, double *target);
439 
440 /**
441  * Unsafe version of \ref ucl_obj_todouble_safe
442  * @param obj CL object
443  * @return double value
444  */
445 UCL_EXTERN double ucl_object_todouble (const ucl_object_t *obj);
446 
447 /**
448  * Converts an object to integer value
449  * @param obj CL object
450  * @param target target integer variable
451  * @return true if conversion was successful
452  */
453 UCL_EXTERN bool ucl_object_toint_safe (const ucl_object_t *obj, int64_t *target);
454 
455 /**
456  * Unsafe version of \ref ucl_obj_toint_safe
457  * @param obj CL object
458  * @return int value
459  */
460 UCL_EXTERN int64_t ucl_object_toint (const ucl_object_t *obj);
461 
462 /**
463  * Converts an object to boolean value
464  * @param obj CL object
465  * @param target target boolean variable
466  * @return true if conversion was successful
467  */
468 UCL_EXTERN bool ucl_object_toboolean_safe (const ucl_object_t *obj, bool *target);
469 
470 /**
471  * Unsafe version of \ref ucl_obj_toboolean_safe
472  * @param obj CL object
473  * @return boolean value
474  */
475 UCL_EXTERN bool ucl_object_toboolean (const ucl_object_t *obj);
476 
477 /**
478  * Converts an object to string value
479  * @param obj CL object
480  * @param target target string variable, no need to free value
481  * @return true if conversion was successful
482  */
483 UCL_EXTERN bool ucl_object_tostring_safe (const ucl_object_t *obj, const char **target);
484 
485 /**
486  * Unsafe version of \ref ucl_obj_tostring_safe
487  * @param obj CL object
488  * @return string value
489  */
490 UCL_EXTERN const char* ucl_object_tostring (const ucl_object_t *obj);
491 
492 /**
493  * Convert any object to a string in JSON notation if needed
494  * @param obj CL object
495  * @return string value
496  */
497 UCL_EXTERN const char* ucl_object_tostring_forced (const ucl_object_t *obj);
498 
499 /**
500  * Return string as char * and len, string may be not zero terminated, more efficient that \ref ucl_obj_tostring as it
501  * allows zero-copy (if #UCL_PARSER_ZEROCOPY has been used during parsing)
502  * @param obj CL object
503  * @param target target string variable, no need to free value
504  * @param tlen target length
505  * @return true if conversion was successful
506  */
507 UCL_EXTERN bool ucl_object_tolstring_safe (const ucl_object_t *obj,
508 		const char **target, size_t *tlen);
509 
510 /**
511  * Unsafe version of \ref ucl_obj_tolstring_safe
512  * @param obj CL object
513  * @return string value
514  */
515 UCL_EXTERN const char* ucl_object_tolstring (const ucl_object_t *obj, size_t *tlen);
516 
517 /**
518  * Return object identified by a key in the specified object
519  * @param obj object to get a key from (must be of type UCL_OBJECT)
520  * @param key key to search
521  * @return object matched the specified key or NULL if key is not found
522  */
523 UCL_EXTERN const ucl_object_t* ucl_object_find_key (const ucl_object_t *obj,
524 		const char *key);
525 
526 /**
527  * Return object identified by a fixed size key in the specified object
528  * @param obj object to get a key from (must be of type UCL_OBJECT)
529  * @param key key to search
530  * @param klen length of a key
531  * @return object matched the specified key or NULL if key is not found
532  */
533 UCL_EXTERN const ucl_object_t* ucl_object_find_keyl (const ucl_object_t *obj,
534 		const char *key, size_t klen);
535 
536 /**
537  * Returns a key of an object as a NULL terminated string
538  * @param obj CL object
539  * @return key or NULL if there is no key
540  */
541 UCL_EXTERN const char* ucl_object_key (const ucl_object_t *obj);
542 
543 /**
544  * Returns a key of an object as a fixed size string (may be more efficient)
545  * @param obj CL object
546  * @param len target key length
547  * @return key pointer
548  */
549 UCL_EXTERN const char* ucl_object_keyl (const ucl_object_t *obj, size_t *len);
550 
551 /**
552  * Increase reference count for an object
553  * @param obj object to ref
554  */
555 UCL_EXTERN ucl_object_t* ucl_object_ref (const ucl_object_t *obj);
556 
557 /**
558  * Free ucl object
559  * @param obj ucl object to free
560  */
561 UCL_DEPRECATED(UCL_EXTERN void ucl_object_free (ucl_object_t *obj));
562 
563 /**
564  * Decrease reference count for an object
565  * @param obj object to unref
566  */
567 UCL_EXTERN void ucl_object_unref (ucl_object_t *obj);
568 
569 /**
570  * Compare objects `o1` and `o2`
571  * @param o1 the first object
572  * @param o2 the second object
573  * @return values >0, 0 and <0 if `o1` is more than, equal and less than `o2`.
574  * The order of comparison:
575  * 1) Type of objects
576  * 2) Size of objects
577  * 3) Content of objects
578  */
579 UCL_EXTERN int ucl_object_compare (const ucl_object_t *o1,
580 		const ucl_object_t *o2);
581 
582 /**
583  * Sort UCL array using `cmp` compare function
584  * @param ar
585  * @param cmp
586  */
587 UCL_EXTERN void ucl_object_array_sort (ucl_object_t *ar,
588 		int (*cmp)(const ucl_object_t *o1, const ucl_object_t *o2));
589 
590 /**
591  * Opaque iterator object
592  */
593 typedef void* ucl_object_iter_t;
594 
595 /**
596  * Get next key from an object
597  * @param obj object to iterate
598  * @param iter opaque iterator, must be set to NULL on the first call:
599  * ucl_object_iter_t it = NULL;
600  * while ((cur = ucl_iterate_object (obj, &it)) != NULL) ...
601  * @return the next object or NULL
602  */
603 UCL_EXTERN const ucl_object_t* ucl_iterate_object (const ucl_object_t *obj,
604 		ucl_object_iter_t *iter, bool expand_values);
605 /** @} */
606 
607 
608 /**
609  * @defgroup parser Parsing functions
610  * These functions are used to parse UCL objects
611  *
612  * @{
613  */
614 
615 /**
616  * Macro handler for a parser
617  * @param data the content of macro
618  * @param len the length of content
619  * @param ud opaque user data
620  * @param err error pointer
621  * @return true if macro has been parsed
622  */
623 typedef bool (*ucl_macro_handler) (const unsigned char *data, size_t len, void* ud);
624 
625 /* Opaque parser */
626 struct ucl_parser;
627 
628 /**
629  * Creates new parser object
630  * @param pool pool to allocate memory from
631  * @return new parser object
632  */
633 UCL_EXTERN struct ucl_parser* ucl_parser_new (int flags);
634 
635 /**
636  * Register new handler for a macro
637  * @param parser parser object
638  * @param macro macro name (without leading dot)
639  * @param handler handler (it is called immediately after macro is parsed)
640  * @param ud opaque user data for a handler
641  */
642 UCL_EXTERN void ucl_parser_register_macro (struct ucl_parser *parser, const char *macro,
643 		ucl_macro_handler handler, void* ud);
644 
645 /**
646  * Register new parser variable
647  * @param parser parser object
648  * @param var variable name
649  * @param value variable value
650  */
651 UCL_EXTERN void ucl_parser_register_variable (struct ucl_parser *parser, const char *var,
652 		const char *value);
653 
654 /**
655  * Load new chunk to a parser
656  * @param parser parser structure
657  * @param data the pointer to the beginning of a chunk
658  * @param len the length of a chunk
659  * @param err if *err is NULL it is set to parser error
660  * @return true if chunk has been added and false in case of error
661  */
662 UCL_EXTERN bool ucl_parser_add_chunk (struct ucl_parser *parser,
663 		const unsigned char *data, size_t len);
664 
665 /**
666  * Load ucl object from a string
667  * @param parser parser structure
668  * @param data the pointer to the string
669  * @param len the length of the string, if `len` is 0 then `data` must be zero-terminated string
670  * @return true if string has been added and false in case of error
671  */
672 UCL_EXTERN bool ucl_parser_add_string (struct ucl_parser *parser,
673 		const char *data,size_t len);
674 
675 /**
676  * Load and add data from a file
677  * @param parser parser structure
678  * @param filename the name of file
679  * @param err if *err is NULL it is set to parser error
680  * @return true if chunk has been added and false in case of error
681  */
682 UCL_EXTERN bool ucl_parser_add_file (struct ucl_parser *parser,
683 		const char *filename);
684 
685 /**
686  * Load and add data from a file descriptor
687  * @param parser parser structure
688  * @param filename the name of file
689  * @param err if *err is NULL it is set to parser error
690  * @return true if chunk has been added and false in case of error
691  */
692 UCL_EXTERN bool ucl_parser_add_fd (struct ucl_parser *parser,
693 		int fd);
694 
695 /**
696  * Get a top object for a parser (refcount is increased)
697  * @param parser parser structure
698  * @param err if *err is NULL it is set to parser error
699  * @return top parser object or NULL
700  */
701 UCL_EXTERN ucl_object_t* ucl_parser_get_object (struct ucl_parser *parser);
702 
703 /**
704  * Get the error string if failing
705  * @param parser parser object
706  */
707 UCL_EXTERN const char *ucl_parser_get_error(struct ucl_parser *parser);
708 /**
709  * Free ucl parser object
710  * @param parser parser object
711  */
712 UCL_EXTERN void ucl_parser_free (struct ucl_parser *parser);
713 
714 /**
715  * Add new public key to parser for signatures check
716  * @param parser parser object
717  * @param key PEM representation of a key
718  * @param len length of the key
719  * @param err if *err is NULL it is set to parser error
720  * @return true if a key has been successfully added
721  */
722 UCL_EXTERN bool ucl_pubkey_add (struct ucl_parser *parser, const unsigned char *key, size_t len);
723 
724 /**
725  * Set FILENAME and CURDIR variables in parser
726  * @param parser parser object
727  * @param filename filename to set or NULL to set FILENAME to "undef" and CURDIR to getcwd()
728  * @param need_expand perform realpath() if this variable is true and filename is not NULL
729  * @return true if variables has been set
730  */
731 UCL_EXTERN bool ucl_parser_set_filevars (struct ucl_parser *parser, const char *filename,
732 		bool need_expand);
733 
734 /** @} */
735 
736 /**
737  * @defgroup emitter Emitting functions
738  * These functions are used to serialise UCL objects to some string representation.
739  *
740  * @{
741  */
742 
743 /**
744  * Structure using for emitter callbacks
745  */
746 struct ucl_emitter_functions {
747 	/** Append a single character */
748 	int (*ucl_emitter_append_character) (unsigned char c, size_t nchars, void *ud);
749 	/** Append a string of a specified length */
750 	int (*ucl_emitter_append_len) (unsigned const char *str, size_t len, void *ud);
751 	/** Append a 64 bit integer */
752 	int (*ucl_emitter_append_int) (int64_t elt, void *ud);
753 	/** Append floating point element */
754 	int (*ucl_emitter_append_double) (double elt, void *ud);
755 	/** Opaque userdata pointer */
756 	void *ud;
757 };
758 
759 /**
760  * Emit object to a string
761  * @param obj object
762  * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
763  * #UCL_EMIT_CONFIG then emit config like object
764  * @return dump of an object (must be freed after using) or NULL in case of error
765  */
766 UCL_EXTERN unsigned char *ucl_object_emit (const ucl_object_t *obj,
767 		enum ucl_emitter emit_type);
768 
769 /**
770  * Emit object to a string
771  * @param obj object
772  * @param emit_type if type is #UCL_EMIT_JSON then emit json, if type is
773  * #UCL_EMIT_CONFIG then emit config like object
774  * @return dump of an object (must be freed after using) or NULL in case of error
775  */
776 UCL_EXTERN bool ucl_object_emit_full (const ucl_object_t *obj,
777 		enum ucl_emitter emit_type,
778 		struct ucl_emitter_functions *emitter);
779 /** @} */
780 
781 /**
782  * @defgroup schema Schema functions
783  * These functions are used to validate UCL objects using json schema format
784  *
785  * @{
786  */
787 
788 /**
789  * Used to define UCL schema error
790  */
791 enum ucl_schema_error_code {
792 	UCL_SCHEMA_OK = 0,          /**< no error */
793 	UCL_SCHEMA_TYPE_MISMATCH,   /**< type of object is incorrect */
794 	UCL_SCHEMA_INVALID_SCHEMA,  /**< schema is invalid */
795 	UCL_SCHEMA_MISSING_PROPERTY,/**< one or more missing properties */
796 	UCL_SCHEMA_CONSTRAINT,      /**< constraint found */
797 	UCL_SCHEMA_MISSING_DEPENDENCY, /**< missing dependency */
798 	UCL_SCHEMA_UNKNOWN          /**< generic error */
799 };
800 
801 /**
802  * Generic ucl schema error
803  */
804 struct ucl_schema_error {
805 	enum ucl_schema_error_code code;	/**< error code */
806 	char msg[128];						/**< error message */
807 	const ucl_object_t *obj;			/**< object where error occured */
808 };
809 
810 /**
811  * Validate object `obj` using schema object `schema`.
812  * @param schema schema object
813  * @param obj object to validate
814  * @param err error pointer, if this parameter is not NULL and error has been
815  * occured, then `err` is filled with the exact error definition.
816  * @return true if `obj` is valid using `schema`
817  */
818 UCL_EXTERN bool ucl_object_validate (const ucl_object_t *schema,
819 		const ucl_object_t *obj, struct ucl_schema_error *err);
820 
821 /** @} */
822 
823 #ifdef  __cplusplus
824 }
825 #endif
826 /*
827  * XXX: Poorly named API functions, need to replace them with the appropriate
828  * named function. All API functions *must* use naming ucl_object_*. Usage of
829  * ucl_obj* should be avoided.
830  */
831 #define ucl_obj_todouble_safe ucl_object_todouble_safe
832 #define ucl_obj_todouble ucl_object_todouble
833 #define ucl_obj_tostring ucl_object_tostring
834 #define ucl_obj_tostring_safe ucl_object_tostring_safe
835 #define ucl_obj_tolstring ucl_object_tolstring
836 #define ucl_obj_tolstring_safe ucl_object_tolstring_safe
837 #define ucl_obj_toint ucl_object_toint
838 #define ucl_obj_toint_safe ucl_object_toint_safe
839 #define ucl_obj_toboolean ucl_object_toboolean
840 #define ucl_obj_toboolean_safe ucl_object_toboolean_safe
841 #define ucl_obj_get_key ucl_object_find_key
842 #define ucl_obj_get_keyl ucl_object_find_keyl
843 #define ucl_obj_unref ucl_object_unref
844 #define ucl_obj_ref ucl_object_ref
845 #define ucl_obj_free ucl_object_free
846 
847 #endif /* UCL_H_ */
848