Universal configuration library parser
Table of Contents generated with DocToc
This repository provides the C library for parsing configurations written in UCL - universal configuration language. It also provides functions to operate with other formats:
JSON: read, write and pretty formatMessagepack: read and writeS-Expressions: read only (canonical form)Yaml: limited write support (mainly for compatibility)If you are looking for the libucl API documentation you can find it at this page.
Libucl with all features enabled (macros, .include directives, file I/O, URL includes) is designed for parsing trusted inputs such as configuration files controlled by the system administrator. It is NOT designed to handle untrusted or adversarial input safely.
Features like .include can read arbitrary files from the filesystem, macros can trigger external actions, and URL includes can make network requests. These are powerful capabilities for configuration management but are inherently unsafe when exposed to attacker-controlled data.
If you need to parse untrusted input, either use libucl in pure JSON mode without macros, or disable dangerous features (e.g. do not register macro handlers, disable URL includes) and enforce strict nesting depth limits via the parser API.
UCL is heavily infused by nginx configuration as the example of a convenient configuration
system. However, UCL is fully compatible with JSON format and is able to parse json files.
For example, you can write the same configuration in the following ways:
param = value;
section {
param = value;
param1 = value1;
flag = true;
number = 10k;
time = 0.2s;
string = "something";
subsection {
host = {
host = "hostname";
port = 900;
}
host = {
host = "hostname";
port = 901;
}
}
}
{
"param": "value",
"section": {
"param": "value",
"param1": "value1",
"flag": true,
"number": 10000,
"time": "0.2s",
"string": "something",
"subsection": {
"host": [
{
"host": "hostname",
"port": 900
},
{
"host": "hostname",
"port": 901
}
]
}
}
}
There are various things that make ucl configuration more convenient for editing than strict json:
"key": "value"
is equal to:
{"key": "value"}
: may be replaced = or even be skipped for objects:key = value;
section {
key = value;
}
is equal to:
{
"key": "value",
"section": {
"key": "value"
}
}
{
"key1": "value",
"key2": "value",
}
{
"key": "value1",
"key": "value2"
}
is converted to:
{
"key": ["value1", "value2"]
}
UCL accepts named keys and organize them into objects hierarchy internally. Here is an example of this process:
section "blah" {
key = value;
}
section foo {
key = value;
}
is converted to the following object:
section {
blah {
key = value;
}
foo {
key = value;
}
}
Plain definitions may be more complex and contain more than a single level of nested objects:
section "blah" "foo" {
key = value;
}
is presented as:
section {
blah {
foo {
key = value;
}
}
}
[kKmMgG] - standard 10 base multipliers (so 1k is translated to 1000)[kKmMgG]b - 2 power multipliers (so 1kb is translated to 1024)[s|min|d|w|y] - time multipliers, all time values are translated to float number of seconds, for example 10min is translated to 600.0 and 10ms is translated to 0.010x prefix, for example key = 0xff. However, floating point values can use decimal base only.true or yes or on and false or no or off.UCL supports different style of comments:
#/* ... */Multiline comments may be nested:
# Sample single line comment
/*
some comment
/* nested comment */
end of comment
*/
UCL supports external macros both multiline and single line ones:
.macro_name "sometext";
.macro_name {
Some long text
....
};
Moreover, each macro can accept an optional list of arguments in braces. These
arguments themselves are the UCL object that is parsed and passed to a macro as
options:
.macro_name(param=value) "something";
.macro_name(param={key=value}) "something";
.macro_name(.include "params.conf") "something";
.macro_name(#this is multiline macro
param = [value1, value2]) "something";
.macro_name(key="()") "something";
UCL also provide a convenient include macro to load content from another files
to the current UCL object. This macro accepts either path to file:
.include "/full/path.conf"
.include "./relative/path.conf"
.include "${CURDIR}/path.conf"
or URL (if ucl is built with url support provided by either libcurl or libfetch):
.include "http://example.com/file.conf"
.include macro supports a set of options:
try (default: false) - if this option is true than UCL treats errors on loading of
this file as non-fatal. For example, such a file can be absent but it won't stop the parsing
of the top-level document.sign (default: false) - if this option is true UCL loads and checks the signature for
a file from path named <FILEPATH>.sig. Trusted public keys should be provided for UCL API after
parser is created but before any configurations are parsed.glob (default: false) - if this option is true UCL treats the filename as GLOB pattern and load
all files that matches the specified pattern (normally the format of patterns is defined in glob manual page
for your operating system). This option is meaningless for URL includes.url (default: true) - allow URL includes.path (default: empty) - A UCL_ARRAY of directories to search for the include file.
Search ends after the first match, unless glob is true, then all matches are included.prefix (default false) - Put included contents inside an object, instead
of loading them into the root. If no key is provided, one is automatically generated based on each files basename()key (default: ) - Key to load contents of include into. If
the key already exists, it must be the correct typetarget (default: object) - Specify if the prefix key should be an
object or an array.priority (default: 0) - specify priority for the include (see below).duplicate (default: 'append') - specify policy of duplicates resolving:append - default strategy, if we have new object of higher priority then it replaces old one, if we have new object with less priority it is ignored completely, and if we have two duplicate objects with the same priority then we have a multi-value key (implicit array)merge - if we have object or array, then new keys are merged inside, if we have a plain object then an implicit array is formed (regardless of priorities)error - create error on duplicate keys and stop parsingrewrite - always rewrite an old value with new one (ignoring priorities)Priorities are used by UCL parser to manage the policy of objects rewriting during including other files as following:
By default, the priority of top-level object is set to zero (lowest priority). Currently,
you can define up to 16 priorities (from 0 to 15). Includes with bigger priorities will
rewrite keys from the objects with lower priorities as specified by the policy. The priority
of the top-level or any other object can be changed with the .priority macro, which has no
options and takes the new priority:
# Default priority: 0.
foo = 6
.priority 5
# The following will have priority 5.
bar = 6
baz = 7
# The following will be included with a priority of 3, 5, and 6 respectively.
.include(priority=3) "path.conf"
.include(priority=5) "equivalent-path.conf"
.include(priority=6) "highpriority-path.conf"
UCL supports variables in input. Variables are registered by a user of the UCL parser and can be presented in the following forms:
${VARIABLE}$VARIABLEUCL currently does not support nested variables. To escape variables one could use double dollar signs:
$${VARIABLE} is converted to ${VARIABLE}$$VARIABLE is converted to $VARIABLEHowever, if no valid variables are found in a string, no expansion will be performed (and $$ thus remains unchanged). This may be a subject
to change in future libucl releases.
UCL can handle multiline strings as well as single line ones. It uses shell/perl like notation for such objects:
key = <<EOD
some text
splitted to
lines
EOD
In this example key will be interpreted as the following string: some text\nsplitted to\nlines.
Here are some rules for this syntax:
<< symbols and it must consist of capital letters only (e.g. <<eof or << EOF won't work);key <<EOD
some
text
EOD
It is possible to use single quoted strings to simplify escaping rules. All values passed in single quoted strings are NOT escaped, with two exceptions: a single ' character just before \ character, and a newline character just after \ character that is ignored.
key = 'value'; # Read as v
No open issues yet, or sync has not completed.