libeconf(3) | libeconf | libeconf(3) |
include/libeconf.h - Public API for the econf library.
include/libeconf_ext.h - Public extended API for the econf library.
libeconf is a highly flexible and configurable library to
parse and manage key=value configuration files.
It reads configuration file snippets from different directories and builds the
final configuration file for the application from it.
The first file is the vendor provided configuration file places in /usr/_vendordir_/_project_. Optionally, /run/_project_ is also supported for ephemeral overrides.
Defining _project_ sub directory is optional.
There are two methods of overriding this vendor settings: Copy the file from /usr/_vendordir_/_project_ to /etc/_project_ and modify the settings (see Example 1).
Alternatively, a directory named _file_._suffix_.d/ within /etc/_project_ can be created, with drop-in files in the form _name_._suffix_ (see Example 2).
These files contain only the changes of the specific settings the
user is interested in.
There can be several such drop-in files, they are processed in lexicographic
order of their filename.
The first method is useful to override the complete configuration file with an own one, the vendor supplied configuration is ignored.
So, if /etc/_project_/_example_._suffix_ exists,
/usr/_vendor_/_project_/_example_._suffix_ and
/run/_project_/_example_._suffix_ will not be read.
The disadvantage is, that changes of the vendor configuration file, due e.g.
an package update, are ignored and the user has to manually merge them.
The other method will continue to use
/usr/_vendor_/_project_/_example_._suffix_ as base configuration file and
merge all changes from /etc/_project_/_example_._suffix_.d/*._suffix_.
So the user will automatically get improvements of the vendor, with the
drawback, that they could be incompatible with the user made changes.
If there is a file with the same name in
/usr/_vendor_/_project_/_example_._suffix_.d/ and in
/etc/_project_/_example_._suffix_.d/*._suffix_., the file in
/usr/_project_/_vendor_/_example_._suffix_.d/ will completely ignored.
To disable a configuration file supplied by the vendor, the recommended way is
to place a symlink to /dev/null in the configuration directory in
/etc/_project_/, with the same filename as the vendor configuration
file.
Optionally, schemes with only drop-ins and without a ‘main’ configuration file will be supported too. In such schemes many drop-ins are loaded from a common directory in each hierarchy. For example, /usr/lib/<project>.d/*, /run/<project>.d/* and /etc/<project>.d/c.conf are all loaded and parsed in this scheme.
If a /etc/_example_._suffix_ file exists:
* /etc/_example_._suffix_
* /usr/_vendor_/_project_/_example_._suffix_.d/*._suffix_
* /run/_project_/_example_._suffix_.d/*._suffix_
* /etc/_project_/_example_._suffix_.d/*._suffix_
The list of files and directories read if **no** /etc/_example_._suffix_ file exists:
* /usr/_vendor_/_project_/_example_._suffix_ if no /run/_project_/_example_._suffix_ exist
* /usr/_vendor_/_project_/_example_._suffix_.d/*._suffix_
* /run/_project_/_example_._suffix_.d/*._suffix_
* /etc/_project_/_example_._suffix_.d/*._suffix_
The libeconf library fulfills all requirements defined by the
Linux Userspace API (UAPI) Group chapter "Configuration Files
Specification".
See:
https://uapi-group.org/specifications/specs/configuration_files_specification/
Python Documentation: https://github.com/openSUSE/libeconf/blob/master/bindings/python3/docs/python-libeconf.3
C# Documentation: https://github.com/openSUSE/libeconf/blob/master/bindings/csharp/docs/README.md
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include 'libeconf.h'
#include 'libeconf_ext.h'
typedef enum econf_err econf_err
typedef struct econf_file econf_file
typedef struct econf_ext_value econf_ext_value
enum econf_err { ECONF_SUCCESS = 0,
ECONF_ERROR = 1, ECONF_NOMEM = 2, ECONF_NOFILE = 3,
ECONF_NOGROUP = 4, ECONF_NOKEY = 5, ECONF_EMPTYKEY = 6,
ECONF_WRITEERROR = 7, ECONF_PARSE_ERROR = 8,
ECONF_MISSING_BRACKET = 9, ECONF_MISSING_DELIMITER = 10,
ECONF_EMPTY_SECTION_NAME = 11, ECONF_TEXT_AFTER_SECTION = 12,
ECONF_FILE_LIST_IS_NULL = 13, ECONF_WRONG_BOOLEAN_VALUE = 14,
ECONF_KEY_HAS_NULL_VALUE = 15, ECONF_WRONG_OWNER = 16,
ECONF_WRONG_GROUP = 17, ECONF_WRONG_FILE_PERMISSION = 18,
ECONF_WRONG_DIR_PERMISSION = 19, ECONF_ERROR_FILE_IS_SYM_LINK
= 20, ECONF_PARSING_CALLBACK_FAILED = 21 }
libeconf error codes
econf_err econf_readConfig (econf_file
**key_file, const char *project, const char *usr_subdir, const char
*config_name, const char *config_suffix, const char *delim, const char
*comment)
Evaluating key/values of a given configuration by reading and merging all
needed/available files in three different directories (normally in /usr/etc,
/run and /etc). econf_err econf_readConfigWithCallback
(econf_file **key_file, const char *project, const char *usr_subdir,
const char *config_name, const char *config_suffix, const char *delim, const
char *comment, bool (*callback)(const char *filename, const void *data),
const void *callback_data)
Has the same functionality like econf_readConfig. The user can
additionally define a callback in order e.g. to check all parsed file.
econf_err econf_readFile (econf_file **result, const
char *file_name, const char *delim, const char *comment)
Process the file of the given file_name and save its contents into key_file
object. econf_err econf_readFileWithCallback
(econf_file **result, const char *file_name, const char *delim, const
char *comment, bool (*callback)(const char *filename, const void *data),
const void *callback_data)
Has the same functionality like econf_readFile. The user can
additionally define a callback in order to check the parsed file.
econf_err econf_mergeFiles (econf_file **merged_file,
econf_file *usr_file, econf_file *etc_file)
Merge the contents of two key_files objects. econf_err
econf_newKeyFile (econf_file **result, char delimiter, char
comment)
Create a new econf_file object. econf_err econf_newIniFile
(econf_file **result)
Create a new econf_file object in IniFile format. econf_err
econf_writeFile (econf_file *key_file, const char
*save_to_dir, const char *file_name)
Write content of an econf_file struct to specified location. char *
econf_getPath (econf_file *kf)
Evaluating path name of the regarding configuration file. econf_err
econf_getGroups (econf_file *kf, size_t *length, char
***groups)
Evaluating all group entries. econf_err econf_getKeys
(econf_file *kf, const char *group, size_t *length, char ***keys)
Evaluating all keys. econf_err econf_getIntValue
(econf_file *kf, const char *group, const char *key, int32_t *result)
Evaluating int32 value for given group/key. econf_err
econf_getInt64Value (econf_file *kf, const char *group, const
char *key, int64_t *result)
Evaluating int64 value for given group/key. econf_err
econf_getUIntValue (econf_file *kf, const char *group, const
char *key, uint32_t *result)
Evaluating uint32 value for given group/key. econf_err
econf_getUInt64Value (econf_file *kf, const char *group, const
char *key, uint64_t *result)
Evaluating uint64 value for given group/key. econf_err
econf_getFloatValue (econf_file *kf, const char *group, const
char *key, float *result)
Evaluating float value for given group/key. econf_err
econf_getDoubleValue (econf_file *kf, const char *group, const
char *key, double *result)
Evaluating double value for given group/key. econf_err
econf_getStringValue (econf_file *kf, const char *group, const
char *key, char **result)
Evaluating string value for given group/key. econf_err
econf_getBoolValue (econf_file *kf, const char *group, const
char *key, bool *result)
Evaluating bool value for given group/key. econf_err
econf_getIntValueDef (econf_file *kf, const char *group, const
char *key, int32_t *result, int32_t def)
Evaluating int32 value for given group/key. econf_err
econf_getInt64ValueDef (econf_file *kf, const char *group,
const char *key, int64_t *result, int64_t def)
Evaluating int64 value for given group/key. econf_err
econf_getUIntValueDef (econf_file *kf, const char *group,
const char *key, uint32_t *result, uint32_t def)
Evaluating uint32 value for given group/key. econf_err
econf_getUInt64ValueDef (econf_file *kf, const char *group,
const char *key, uint64_t *result, uint64_t def)
Evaluating uint64 value for given group/key. econf_err
econf_getFloatValueDef (econf_file *kf, const char *group,
const char *key, float *result, float def)
Evaluating float value for given group/key. econf_err
econf_getDoubleValueDef (econf_file *kf, const char *group,
const char *key, double *result, double def)
Evaluating double value for given group/key. econf_err
econf_getStringValueDef (econf_file *kf, const char *group,
const char *key, char **result, char *def)
Evaluating string value for given group/key. econf_err
econf_getBoolValueDef (econf_file *kf, const char *group,
const char *key, bool *result, bool def)
Evaluating bool value for given group/key. econf_err
econf_setIntValue (econf_file *kf, const char *group, const
char *key, int32_t value)
Set int32 value for given group/key. econf_err
econf_setInt64Value (econf_file *kf, const char *group, const
char *key, int64_t value)
Set int64 value for given group/key. econf_err
econf_setUIntValue (econf_file *kf, const char *group, const
char *key, uint32_t value)
Set uint32 value for given group/key. econf_err
econf_setUInt64Value (econf_file *kf, const char *group, const
char *key, uint64_t value)
Set uint64 value for given group/key. econf_err
econf_setFloatValue (econf_file *kf, const char *group, const
char *key, float value)
Set float value for given group/key. econf_err
econf_setDoubleValue (econf_file *kf, const char *group, const
char *key, double value)
Set double value for given group/key. econf_err
econf_setStringValue (econf_file *kf, const char *group, const
char *key, const char *value)
Set string value for given group/key. econf_err
econf_setBoolValue (econf_file *kf, const char *group, const
char *key, const char *value)
Set bool value for given group/key. const char * econf_errString (const
econf_err error)
Convert an econf_err type to a string. void econf_errLocation (char
**filename, uint64_t *line_nr)
Info about where the error has happened. void econf_freeArray (char
**array)
Free an array of type char** created by econf_getGroups() or
econf_getKeys(). void econf_freeFile (econf_file
*key_file)
Free memory allocated by e.g. char econf_comment_tag (econf_file
*key_file)
Returns the comment character tag of the given econf_file object. char
econf_delimiter_tag (econf_file *key_file)
Returns the delimiter character of the given econf_file object. void
econf_set_comment_tag (econf_file *key_file, const char
comment)
Set the comment character tag of the given econf_file object. void
econf_set_delimiter_tag (econf_file *key_file, const char
delimiter)
Set the delimiter character of the given econf_file object.
econf_err econf_getExtValue (econf_file *kf, const char
*group, const char *key, econf_ext_value **result)
Evaluating more information for given group/key. void
econf_freeExtValue (econf_ext_value *to_free)
Free an complete econf_ext_value struct. econf_err
econf_set_conf_dirs (const char **dir_postfix_list)
Set a list of directory structures (with order) which describes the
directories in which the files have to be parsed.
Public API for the econf library.
Definition in file libeconf.h and libeconf_ext.h.
Container which includes all information about the configuration file(s).
char ** values
Values of a given key in form of an string array. char * file
Path of the configuration file where this value has been read. uint64_t
line_number
Line number of the configuration key/value. char * comment_before_key
Comment before the key/value entry. char * comment_after_value
Comment after the value entry.
libeconf error codes
Enumerator
Evaluating key/values of a given configuration by reading and merging all needed/available files from different directories. Order is:
/etc/$project/$config_name.$config_suffix does exist:
-- /etc/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
/etc/$project/$config_name.$config_suffix does NOT exist:
- /run/$project/$config_name.$config_suffix does exist:
-- /run/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
- /run/$project/$config_name.$config_suffix does NOT exist:
-- $usr_subdir/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
No main $config_name.$config_suffix file is defined or must not be
parsed:
-- $usr_subdir/$project.d/*.$config_suffix
-- /run/$project.d/*.$config_suffix
-- /etc/$project.d/*.$config_suffix
Parameters:
Returns:
Example: Reading content in different cases in following order:
/etc/foo/example.conf does exist:
- /etc/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
/etc/foo/example.conf does NOT exist:
/run/foo/example.conf does exist:
- /run/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
/run/foo/example.conf does NOT exist:
- /usr/lib/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
#include "libeconf.h" econf_file *key_file = NULL; econf_err error; error = econf_readConfig (&key_file, "foo", "/usr/lib", "example", "conf", "=", "#"); econf_free (key_file);
Evaluating key/values of a given configuration by reading and merging all needed/available files from different directories. Order is:
/etc/$project/$config_name.$config_suffix does exist:
-- /etc/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
/etc/$project/$config_name.$config_suffix does NOT exist:
- /run/$project/$config_name.$config_suffix does exist:
-- /run/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
- /run/$project/$config_name.$config_suffix does NOT exist:
-- $usr_subdir/$project/$config_name.$config_suffix
-- $usr_subdir/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /run/$project/$config_name.$config_suffix.d/ *.$config_suffix
-- /etc/$project/$config_name.$config_suffix.d/ *.$config_suffix
No main $config_name.$config_suffix file is defined or must not be
parsed:
-- $usr_subdir/$project.d/*.$config_suffix
-- /run/$project.d/*.$config_suffix
-- /etc/$project.d/*.$config_suffix
For each parsed file the user defined function will be called in order e.g. to check the correct file permissions.
Parameters:
Returns:
Example: Reading content in different cases in following order:
/etc/foo/example.conf does exist:
- /etc/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
/etc/foo/example.conf does NOT exist:
/run/foo/example.conf does exist:
- /run/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
/run/foo/example.conf does NOT exist:
- /usr/lib/foo/example.conf
- /usr/lib/foo/example.conf.d/ *.conf
- /run/foo/example.conf.d/ *.conf
- /etc/foo/example.conf.d/ *.conf
#include "libeconf.h" bool checkFile(const char *filename, const void *data) { /* checking code which returns true or false */ return true; } econf_file *key_file = NULL; econf_err error; error = econf_readConfigWithCallback (&key_file, "foo", "/usr/lib", "example", "conf", "=", "#", checkFile, NULL); econf_free (key_file);
Process the file of the given file_name and save its contents into key_file object.
Parameters:
Returns:
Usage:
#include "libeconf.h" econf_file *key_file = NULL; econf_err error; error = econf_readFile (&key_file, "/etc/test.conf", "=", "#"); econf_free (key_file);
Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with the same name will be joined to one single entry.
Process the file of the given file_name and save its contents into key_file object. The user defined function will be called in order e.g. to check the correct file permissions.
Parameters:
Returns:
Usage:
#include "libeconf.h" bool checkFile(const char *filename, const void *data) { /* checking code which returns true or false */ return true; } econf_file *key_file = NULL; econf_err error; error = econf_readFileWithCallback (&key_file, "/etc/test.conf", "=", "#", checkFile, NULL); econf_free (key_file);
Default behaviour if entries have the same name in one file: The first hit will be returned. Further entries will be ignored. This can be changed by setting the environment variable JOIN_SAME_ENTRIES (see econf_set_opt). In that case entries with the same name will be joined to one single entry.
Merge the contents of two key_files objects. Entries in etc_file will be preferred. Comment and delimiter tag will be taken from usr_file. This can be changed by calling the functions econf_set_comment_tag and econf_set_delimiter_tag.
Parameters:
Returns:
Usage:
#include "libeconf.h" econf_file *key_file_1 = NULL, *key_file_2 = NULL, *key_file_ret = NULL econf_err error; error = econf_readFile (&key_file1, "/usr/etc/test.conf", "=", "#"); error = econf_readFile (&key_file2, /etc/test.conf", "=", "#"); error = econf_mergeFiles (&key_file_ret, key_file_1, key_file_2); econf_free (key_file_ret); econf_free (key_file_1); econf_free (key_file_2);
Create a new econf_file object.
Parameters:
Returns:
Create a new econf_file object in IniFile format. So the delimiter will be '=' and comments are beginning with '#'.
Parameters:
Returns:
Write content of an econf_file struct to specified location.
Parameters:
Returns:
Evaluating path name.
Parameters:
Returns:
Evaluating all group entries.
Parameters:
Returns:
Evaluating all keys.
Parameters:
Returns:
Evaluating int32 value for given group/key.
Parameters:
Returns:
Evaluating int64 value for given group/key.
Parameters:
Returns:
Evaluating uint32 value for given group/key.
Parameters:
Returns:
Evaluating uint64 value for given group/key.
Parameters:
Returns:
Evaluating float value for given group/key.
Parameters:
Returns:
Evaluating double value for given group/key.
Parameters:
Returns:
Evaluating string value for given group/key.
Parameters:
Returns:
Evaluating bool value for given group/key.
Parameters:
Returns:
Evaluating int32 value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating int64 value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating uint32 value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating uint64 value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating float value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating double value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating string value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Evaluating bool value for given group/key. If key is not found, the default value is returned and error is ECONF_NOKEY.
Parameters:
Returns:
Set int32 value for given group/key.
Parameters:
Returns:
Set int64 value for given group/key.
Parameters:
Returns:
Set uint32 value for given group/key.
Parameters:
Returns:
Set uint64 value for given group/key.
Parameters:
Returns:
Set float value for given group/key.
Parameters:
Returns:
Set double value for given group/key.
Parameters:
Returns:
Set string value for given group/key.
Parameters:
Returns:
Set bool value for given group/key.
Parameters:
Returns:
Convert an econf_err type to a string.
Parameters:
Returns:
Info about where the error has happened.
Parameters:
Free an array of type char** created by econf_getGroups() or econf_getKeys().
Parameters:
Returns:
Free memory allocated by e.g. econf_readFile(), econf_readDirs(),...
Parameters:
Returns:
Returns the comment character tag of the given econf_file object. This tag will be taken while writing comments to file.
Parameters:
Returns:
Returns the delimiter character of the given econf_file object. This delimiter will be taken while writing the data to file.
Parameters:
Returns:
Set the comment character tag of the given econf_file object. This tag will be taken while writing comments to file.
Parameters:
Set the delimiter character of the given econf_file object. This delimiter will be taken while writing the data to file.
Parameters:
Evaluating more information for given group/key.
Parameters:
Returns:
Free an complete econf_ext_value struct.
Parameters:
Returns:
Set a list of directory structures (with order) which describes the directories in which the files have to be parsed.
Parameters:
Returns:
econftool
Thu Apr 8 2021 | Version 0.4.7 |