1 .TH LIBUCL 5 "March 20, 2014" "Libucl manual"
4 \f[B]ucl_parser_new\f[], \f[B]ucl_parser_register_macro\f[],
5 \f[B]ucl_parser_register_variable\f[], \f[B]ucl_parser_add_chunk\f[],
6 \f[B]ucl_parser_add_string\f[], \f[B]ucl_parser_add_file\f[],
7 \f[B]ucl_parser_get_object\f[], \f[B]ucl_parser_get_error\f[],
8 \f[B]ucl_parser_free\f[], \f[B]ucl_pubkey_add\f[],
9 \f[B]ucl_parser_set_filevars\f[] \- universal configuration library
10 parser and utility functions
13 UCL library (libucl, \-lucl)
16 \f[C]#include\ <ucl.h>\f[]
19 Libucl is a parser and \f[C]C\f[] API to parse and generate \f[C]ucl\f[]
21 Libucl consist of several groups of functions:
24 Used to parse \f[C]ucl\f[] files and provide interface to extract
26 Currently, \f[C]libucl\f[] can parse only full \f[C]ucl\f[] documents,
27 for instance, it is impossible to parse a part of document and therefore
28 it is impossible to use \f[C]libucl\f[] as a streaming parser.
29 In future, this limitation can be removed.
30 .SS Emitting functions
32 Convert \f[C]ucl\f[] objects to some textual or binary representation.
33 Currently, libucl supports the following exports:
35 \f[C]JSON\f[] \- valid json format (can possibly loose some original
36 data, such as implicit arrays)
38 \f[C]Config\f[] \- human\-readable configuration format (losseless)
40 \f[C]YAML\f[] \- embedded yaml format (has the same limitations as
42 .SS Conversion functions
44 Help to convert \f[C]ucl\f[] objects to C types.
45 These functions are used to convert \f[C]ucl_object_t\f[] to C primitive
46 types, such as numbers, strings or boolean values.
47 .SS Generation functions
49 Allow creating of \f[C]ucl\f[] objects from C types and creating of
50 complex \f[C]ucl\f[] objects, such as hashes or arrays from primitive
51 \f[C]ucl\f[] objects, such as numbers or strings.
52 .SS Iteration functions
54 Iterate over \f[C]ucl\f[] complex objects or over a chain of values, for
55 example when a key in an object has multiple values (that can be treated
56 as implicit array or implicit consolidation).
57 .SS Validation functions
59 Validation functions are used to validate some object \f[C]obj\f[] using
60 json\-schema compatible object \f[C]schema\f[].
61 Both input and schema must be UCL objects to perform validation.
64 Provide basic utilities to manage \f[C]ucl\f[] objects: creating,
65 removing, retaining and releasing reference count and so on.
68 Parser functions operates with \f[C]struct\ ucl_parser\f[].
73 struct\ ucl_parser*\ ucl_parser_new\ (int\ flags);
77 Creates new parser with the specified flags:
79 \f[C]UCL_PARSER_KEY_LOWERCASE\f[] \- lowercase keys parsed
81 \f[C]UCL_PARSER_ZEROCOPY\f[] \- try to use zero\-copy mode when reading
82 files (in zero\-copy mode text chunk being parsed without copying
83 strings so it should exist till any object parsed is used)
85 \f[C]UCL_PARSER_NO_TIME\f[] \- treat time values as strings without
86 parsing them as floats
87 .SS ucl_parser_register_macro
91 void\ ucl_parser_register_macro\ (struct\ ucl_parser\ *parser,
92 \ \ \ \ const\ char\ *macro,\ ucl_macro_handler\ handler,\ void*\ ud);
96 Register new macro with name .\f[C]macro\f[] parsed by handler
97 \f[C]handler\f[] that accepts opaque data pointer \f[C]ud\f[].
98 Macro handler should be of the following type:
102 bool\ (*ucl_macro_handler)\ (const\ unsigned\ char\ *data,
103 \ \ \ \ size_t\ len,\ void*\ ud);`
107 Handler function accepts macro text \f[C]data\f[] of length \f[C]len\f[]
108 and the opaque pointer \f[C]ud\f[].
109 If macro is parsed successfully the handler should return \f[C]true\f[].
110 \f[C]false\f[] indicates parsing failure and the parser can be
112 .SS ucl_parser_register_variable
116 void\ ucl_parser_register_variable\ (struct\ ucl_parser\ *parser,
117 \ \ \ \ const\ char\ *var,\ const\ char\ *value);
121 Register new variable $\f[C]var\f[] that should be replaced by the
122 parser to the \f[C]value\f[] string.
123 .SS ucl_parser_add_chunk
127 bool\ ucl_parser_add_chunk\ (struct\ ucl_parser\ *parser,\
128 \ \ \ \ const\ unsigned\ char\ *data,\ size_t\ len);
132 Add new text chunk with \f[C]data\f[] of length \f[C]len\f[] to the
134 At the moment, \f[C]libucl\f[] parser is not a streamlined parser and
135 chunk \f[I]must\f[] contain the \f[I]valid\f[] ucl object.
136 For example, this object should be valid:
140 {\ "var":\ "value"\ }
144 while this one won\[aq]t be parsed correctly:
152 This limitation may possible be removed in future.
153 .SS ucl_parser_add_string
157 bool\ ucl_parser_add_string\ (struct\ ucl_parser\ *parser,\
158 \ \ \ \ const\ char\ *data,\ size_t\ len);
162 This function acts exactly like \f[C]ucl_parser_add_chunk\f[] does but
163 if \f[C]len\f[] argument is zero, then the string \f[C]data\f[] must be
164 zero\-terminated and the actual length is calculated up to \f[C]\\0\f[]
166 .SS ucl_parser_add_file
170 bool\ ucl_parser_add_file\ (struct\ ucl_parser\ *parser,\
171 \ \ \ \ const\ char\ *filename);
175 Load file \f[C]filename\f[] and parse it with the specified
177 This function uses \f[C]mmap\f[] call to load file, therefore, it should
178 not be \f[C]shrinked\f[] during parsing.
179 Otherwise, \f[C]libucl\f[] can cause memory corruption and terminate the
181 This function is also used by the internal handler of \f[C]include\f[]
182 macro, hence, this macro has the same limitation.
183 .SS ucl_parser_get_object
187 ucl_object_t*\ ucl_parser_get_object\ (struct\ ucl_parser\ *parser);
191 If the \f[C]ucl\f[] data has been parsed correctly this function returns
192 the top object for the parser.
193 Otherwise, this function returns the \f[C]NULL\f[] pointer.
194 The reference count for \f[C]ucl\f[] object returned is increased by
195 one, therefore, a caller should decrease reference by using
196 \f[C]ucl_object_unref\f[] to free object after usage.
197 .SS ucl_parser_get_error
201 const\ char\ *ucl_parser_get_error(struct\ ucl_parser\ *parser);
205 Returns the constant error string for the parser object.
206 If no error occurred during parsing a \f[C]NULL\f[] object is returned.
207 A caller should not try to free or modify this string.
212 void\ ucl_parser_free\ (struct\ ucl_parser\ *parser);
216 Frees memory occupied by the parser object.
217 The reference count for top object is decreased as well, however if the
218 function \f[C]ucl_parser_get_object\f[] was called previously then the
219 top object won\[aq]t be freed.
224 bool\ ucl_pubkey_add\ (struct\ ucl_parser\ *parser,\
225 \ \ \ \ const\ unsigned\ char\ *key,\ size_t\ len);
229 This function adds a public key from text blob \f[C]key\f[] of length
230 \f[C]len\f[] to the \f[C]parser\f[] object.
231 This public key should be in the \f[C]PEM\f[] format and can be used by
232 \f[C]\&.includes\f[] macro for checking signatures of files included.
233 \f[C]Openssl\f[] support should be enabled to make this function
235 If a key cannot be added (e.g.
236 due to format error) or \f[C]openssl\f[] was not linked to
237 \f[C]libucl\f[] then this function returns \f[C]false\f[].
238 .SS ucl_parser_set_filevars
242 bool\ ucl_parser_set_filevars\ (struct\ ucl_parser\ *parser,\
243 \ \ \ \ const\ char\ *filename,\ bool\ need_expand);
247 Add the standard file variables to the \f[C]parser\f[] based on the
248 \f[C]filename\f[] specified:
250 \f[C]$FILENAME\f[] \- a filename of \f[C]ucl\f[] input
252 \f[C]$CURDIR\f[] \- a current directory of the input
254 For example, if a \f[C]filename\f[] param is
255 \f[C]\&../something.conf\f[] then the variables will have the following
258 \f[C]$FILENAME\f[] \- "../something.conf"
260 \f[C]$CURDIR\f[] \- ".."
262 if \f[C]need_expand\f[] parameter is \f[C]true\f[] then all relative
263 paths are expanded using \f[C]realpath\f[] call.
264 In this example if \f[C]\&..\f[] is \f[C]/etc/dir\f[] then variables
265 will have these values:
267 \f[C]$FILENAME\f[] \- "/etc/something.conf"
269 \f[C]$CURDIR\f[] \- "/etc"
270 .SS Parser usage example
272 The following example loads, parses and extracts \f[C]ucl\f[] object
273 from stdin using \f[C]libucl\f[] parser functions (the length of input
279 struct\ ucl_parser\ *parser\ =\ NULL;
280 int\ ret\ =\ 0,\ r\ =\ 0;
281 ucl_object_t\ *obj\ =\ NULL;
285 parser\ =\ ucl_parser_new\ (0);
286 while\ (!feof\ (in)\ &&\ r\ <\ (int)sizeof\ (inbuf))\ {
287 \ \ \ \ r\ +=\ fread\ (inbuf\ +\ r,\ 1,\ sizeof\ (inbuf)\ \-\ r,\ in);
289 ucl_parser_add_chunk\ (parser,\ inbuf,\ r);
292 if\ (ucl_parser_get_error\ (parser))\ {
293 \ \ \ \ printf\ ("Error\ occured:\ %s\\n",\ ucl_parser_get_error\ (parser));
297 \ \ \ \ obj\ =\ ucl_parser_get_object\ (parser);
300 if\ (parser\ !=\ NULL)\ {
301 \ \ \ \ ucl_parser_free\ (parser);
303 if\ (obj\ !=\ NULL)\ {
304 \ \ \ \ ucl_object_unref\ (obj);
309 .SH EMITTING FUNCTIONS
311 Libucl can transform UCL objects to a number of tectual formats:
313 configuration (\f[C]UCL_EMIT_CONFIG\f[]) \- nginx like human readable
314 configuration file where implicit arrays are transformed to the
317 compact json: \f[C]UCL_EMIT_JSON_COMPACT\f[] \- single line valid json
320 formatted json: \f[C]UCL_EMIT_JSON\f[] \- pretty formatted JSON with
323 compact yaml: \f[C]UCL_EMIT_YAML\f[] \- compact YAML output
325 Moreover, libucl API allows to select a custom set of emitting functions
326 allowing efficent and zero\-copy output of libucl objects.
327 Libucl uses the following structure to support this feature:
331 struct\ ucl_emitter_functions\ {
332 \ \ \ \ /**\ Append\ a\ single\ character\ */
333 \ \ \ \ int\ (*ucl_emitter_append_character)\ (unsigned\ char\ c,\ size_t\ nchars,\ void\ *ud);
334 \ \ \ \ /**\ Append\ a\ string\ of\ a\ specified\ length\ */
335 \ \ \ \ int\ (*ucl_emitter_append_len)\ (unsigned\ const\ char\ *str,\ size_t\ len,\ void\ *ud);
336 \ \ \ \ /**\ Append\ a\ 64\ bit\ integer\ */
337 \ \ \ \ int\ (*ucl_emitter_append_int)\ (int64_t\ elt,\ void\ *ud);
338 \ \ \ \ /**\ Append\ floating\ point\ element\ */
339 \ \ \ \ int\ (*ucl_emitter_append_double)\ (double\ elt,\ void\ *ud);
340 \ \ \ \ /**\ Opaque\ userdata\ pointer\ */
346 This structure defines the following callbacks:
348 \f[C]ucl_emitter_append_character\f[] \- a function that is called to
349 append \f[C]nchars\f[] characters equal to \f[C]c\f[]
351 \f[C]ucl_emitter_append_len\f[] \- used to append a string of length
352 \f[C]len\f[] starting from pointer \f[C]str\f[]
354 \f[C]ucl_emitter_append_int\f[] \- this function applies to integer
357 \f[C]ucl_emitter_append_double\f[] \- this function is intended to
358 output floating point variable
360 The set of these functions could be used to output text formats of
361 \f[C]UCL\f[] objects to different structures or streams.
363 Libucl provides the following functions for emitting UCL objects:
368 unsigned\ char\ *ucl_object_emit\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type);
372 Allocate a string that is suitable to fit the underlying UCL object
373 \f[C]obj\f[] and fill it with the textual representation of the object
374 \f[C]obj\f[] according to style \f[C]emit_type\f[].
375 The caller should free the returned string after using.
376 .SS ucl_object_emit_full
380 bool\ ucl_object_emit_full\ (const\ ucl_object_t\ *obj,\ enum\ ucl_emitter\ emit_type,
381 \ \ \ \ \ \ \ \ struct\ ucl_emitter_functions\ *emitter);
385 This function is similar to the previous with the exception that it
386 accepts the additional argument \f[C]emitter\f[] that defines the
387 concrete set of output functions.
388 This emit function could be useful for custom structures or streams
389 emitters (including C++ ones, for example).
390 .SH CONVERSION FUNCTIONS
392 Conversion functions are used to convert UCL objects to primitive types,
393 such as strings, numbers or boolean values.
394 There are two types of conversion functions:
396 safe: try to convert an ucl object to a primitive type and fail if such
397 a conversion is not possible
399 unsafe: return primitive type without additional checks, if the object
400 cannot be converted then some reasonable default is returned (NULL for
401 strings and 0 for numbers)
403 Also there is a single \f[C]ucl_object_tostring_forced\f[] function that
404 converts any UCL object (including compound types \- arrays and objects)
405 to a string representation.
406 For compound and numeric types this function performs emitting to a
407 compact json format actually.
409 Here is a list of all conversion functions:
411 \f[C]ucl_object_toint\f[] \- returns \f[C]int64_t\f[] of UCL object
413 \f[C]ucl_object_todouble\f[] \- returns \f[C]double\f[] of UCL object
415 \f[C]ucl_object_toboolean\f[] \- returns \f[C]bool\f[] of UCL object
417 \f[C]ucl_object_tostring\f[] \- returns \f[C]const\ char\ *\f[] of UCL
418 object (this string is NULL terminated)
420 \f[C]ucl_object_tolstring\f[] \- returns \f[C]const\ char\ *\f[] and
421 \f[C]size_t\f[] len of UCL object (string can be not NULL terminated)
423 \f[C]ucl_object_tostring_forced\f[] \- returns string representation of
426 Strings returned by these pointers are associated with the UCL object
427 and exist over its lifetime.
428 A caller should not free this memory.
429 .SH GENERATION FUNCTIONS
431 It is possible to generate UCL objects from C primitive types.
432 Moreover, libucl permits to create and modify complex UCL objects, such
433 as arrays or associative objects.
438 ucl_object_t\ *\ ucl_object_new\ (void)
442 Creates new object of type \f[C]UCL_NULL\f[].
443 This object should be released by caller.
444 .SS ucl_object_typed_new
448 ucl_object_t\ *\ ucl_object_typed_new\ (unsigned\ int\ type)
452 Create an object of a specified type: \- \f[C]UCL_OBJECT\f[] \- UCL
453 object \- key/value pairs \- \f[C]UCL_ARRAY\f[] \- UCL array \-
454 \f[C]UCL_INT\f[] \- integer number \- \f[C]UCL_FLOAT\f[] \- floating
455 point number \- \f[C]UCL_STRING\f[] \- NULL terminated string \-
456 \f[C]UCL_BOOLEAN\f[] \- boolean value \- \f[C]UCL_TIME\f[] \- time value
457 (floating point number of seconds) \- \f[C]UCL_USERDATA\f[] \- opaque
458 userdata pointer (may be used in macros) \- \f[C]UCL_NULL\f[] \- null
461 This object should be released by caller.
462 .SS Primitive objects generation
464 Libucl provides the functions similar to inverse conversion functions
465 called with the specific C type: \- \f[C]ucl_object_fromint\f[] \-
466 converts \f[C]int64_t\f[] to UCL object \-
467 \f[C]ucl_object_fromdouble\f[] \- converts \f[C]double\f[] to UCL object
468 \- \f[C]ucl_object_fromboolean\f[] \- converts \f[C]bool\f[] to UCL
469 object \- \f[C]ucl_object_fromstring\f[] \- converts
470 \f[C]const\ char\ *\f[] to UCL object (this string is NULL terminated)
471 \- \f[C]ucl_object_fromlstring\f[] \- converts \f[C]const\ char\ *\f[]
472 and \f[C]size_t\f[] len to UCL object (string can be not NULL
475 Also there is a function to generate UCL object from a string performing
476 various parsing or conversion operations called
477 \f[C]ucl_object_fromstring_common\f[].
478 .SS ucl_object_fromstring_common
482 ucl_object_t\ *\ ucl_object_fromstring_common\ (const\ char\ *str,\
483 \ \ \ \ size_t\ len,\ enum\ ucl_string_flags\ flags)
487 This function is used to convert a string \f[C]str\f[] of size
488 \f[C]len\f[] to an UCL objects applying \f[C]flags\f[] conversions.
489 If \f[C]len\f[] is equal to zero then a \f[C]str\f[] is assumed as
491 This function supports the following flags (a set of flags can be
492 specified using logical \f[C]OR\f[] operation):
494 \f[C]UCL_STRING_ESCAPE\f[] \- perform JSON escape
496 \f[C]UCL_STRING_TRIM\f[] \- trim leading and trailing whitespaces
498 \f[C]UCL_STRING_PARSE_BOOLEAN\f[] \- parse passed string and detect
501 \f[C]UCL_STRING_PARSE_INT\f[] \- parse passed string and detect integer
504 \f[C]UCL_STRING_PARSE_DOUBLE\f[] \- parse passed string and detect
505 integer or float number
507 \f[C]UCL_STRING_PARSE_TIME\f[] \- parse time values as floating point
510 \f[C]UCL_STRING_PARSE_NUMBER\f[] \- parse passed string and detect
511 number (both float, integer and time types)
513 \f[C]UCL_STRING_PARSE\f[] \- parse passed string (and detect booleans,
514 numbers and time values)
516 \f[C]UCL_STRING_PARSE_BYTES\f[] \- assume that numeric multipliers are
517 in bytes notation, for example \f[C]10k\f[] means \f[C]10*1024\f[] and
518 not \f[C]10*1000\f[] as assumed without this flag
520 If parsing operations fail then the resulting UCL object will be a
522 A caller should always check the type of the returned object and release
524 .SH ITERATION FUNCTIONS
526 Iteration are used to iterate over UCL compound types: arrays and
528 Moreover, iterations could be performed over the keys with multiple
529 values (implicit arrays).
530 To iterate over an object, an array or a key with multiple values there
531 is a function \f[C]ucl_iterate_object\f[].
532 .SS ucl_iterate_object
536 const\ ucl_object_t*\ ucl_iterate_object\ (const\ ucl_object_t\ *obj,\
537 \ \ \ \ ucl_object_iter_t\ *iter,\ bool\ expand_values);
541 This function accept opaque iterator pointer \f[C]iter\f[].
542 In the first call this iterator \f[I]must\f[] be initialized to
544 Iterator is changed by this function call.
545 \f[C]ucl_iterate_object\f[] returns the next UCL object in the compound
546 object \f[C]obj\f[] or \f[C]NULL\f[] if all objects have been iterated.
547 The reference count of the object returned is not increased, so a caller
548 should not unref the object or modify its content (e.g.
549 by inserting to another compound object).
550 The object \f[C]obj\f[] should not be changed during the iteration
552 \f[C]expand_values\f[] flag speicifies whether
553 \f[C]ucl_iterate_object\f[] should expand keys with multiple values.
554 The general rule is that if you need to iterate throught the
555 \f[I]object\f[] or \f[I]explicit array\f[], then you always need to set
556 this flag to \f[C]true\f[].
557 However, if you get some key in the object and want to extract all its
558 values then you should set \f[C]expand_values\f[] to \f[C]false\f[].
559 Mixing of iteration types are not permitted since the iterator is set
560 according to the iteration type and cannot be reused.
561 Here is an example of iteration over the objects using libucl API
562 (assuming that \f[C]top\f[] is \f[C]UCL_OBJECT\f[] in this example):
566 ucl_object_iter_t\ it\ =\ NULL,\ it_obj\ =\ NULL;
567 const\ ucl_object_t\ *cur,\ *tmp;
569 /*\ Iterate\ over\ the\ object\ */
570 while\ ((obj\ =\ ucl_iterate_object\ (top,\ &it,\ true)))\ {
571 \ \ \ \ printf\ ("key:\ \\"%s\\"\\n",\ ucl_object_key\ (obj));
572 \ \ \ \ /*\ Iterate\ over\ the\ values\ of\ a\ key\ */
573 \ \ \ \ while\ ((cur\ =\ ucl_iterate_object\ (obj,\ &it_obj,\ false)))\ {
574 \ \ \ \ \ \ \ \ printf\ ("value:\ \\"%s\\"\\n",\
575 \ \ \ \ \ \ \ \ \ \ \ \ ucl_object_tostring_forced\ (cur));
580 .SH VALIDATION FUNCTIONS
582 Currently, there is only one validation function called
583 \f[C]ucl_object_validate\f[].
584 It performs validation of object using the specified schema.
585 This function is defined as following:
586 .SS ucl_object_validate
590 bool\ ucl_object_validate\ (const\ ucl_object_t\ *schema,
591 \ \ \ \ const\ ucl_object_t\ *obj,\ struct\ ucl_schema_error\ *err);
595 This function uses ucl object \f[C]schema\f[], that must be valid in
596 terms of \f[C]json\-schema\f[] draft v4, to validate input object
598 If this function returns \f[C]true\f[] then validation procedure has
600 Otherwise, \f[C]false\f[] is returned and \f[C]err\f[] is set to a
602 If caller set \f[C]err\f[] to NULL then this function does not set any
603 error just returning \f[C]false\f[].
604 Error is the structure defined as following:
608 struct\ ucl_schema_error\ {
609 \ \ \ \ enum\ ucl_schema_error_code\ code;\ \ \ \ /*\ error\ code\ */
610 \ \ \ \ char\ msg[128];\ \ \ \ \ \ \ \ \ \ \ \ \ \ /*\ error\ message\ */
611 \ \ \ \ ucl_object_t\ *obj;\ \ \ \ \ \ \ \ \ \ /*\ object\ where\ error\ occured\ */
616 Caller may use \f[C]code\f[] field to get a numeric error code:
620 enum\ ucl_schema_error_code\ {
621 \ \ \ \ UCL_SCHEMA_OK\ =\ 0,\ \ \ \ \ \ \ \ \ \ /*\ no\ error\ */
622 \ \ \ \ UCL_SCHEMA_TYPE_MISMATCH,\ \ \ /*\ type\ of\ object\ is\ incorrect\ */
623 \ \ \ \ UCL_SCHEMA_INVALID_SCHEMA,\ \ /*\ schema\ is\ invalid\ */
624 \ \ \ \ UCL_SCHEMA_MISSING_PROPERTY,/*\ missing\ properties\ */
625 \ \ \ \ UCL_SCHEMA_CONSTRAINT,\ \ \ \ \ \ /*\ constraint\ found\ */
626 \ \ \ \ UCL_SCHEMA_MISSING_DEPENDENCY,\ /*\ missing\ dependency\ */
627 \ \ \ \ UCL_SCHEMA_UNKNOWN\ \ \ \ \ \ \ \ \ \ /*\ generic\ error\ */
632 \f[C]msg\f[] is a stiring description of an error and \f[C]obj\f[] is an
633 object where error has been occurred.
634 Error object is not allocated by libucl, so there is no need to free it
635 after validation (a static object should thus be used).
637 Vsevolod Stakhov <vsevolod@highsecure.ru>.