Skip to content

config.3

Manvendra Bhangui edited this page Feb 25, 2024 · 4 revisions

NAME

config - read optional configuration data

SYNTAX

#include <sconfig.h>

int config(&c);
stralloc *config_data(&c);

int config_default(&c,s);
int config_copy(&c,&d);
int config_env(&c,s);
int config_readline(&c,s);
int config_readfile(&c,s);

config_str c = CONFIG_STR;
config_str d = CONFIG_STR;

char *s;

DESCRIPTION

A config_str such as c holds an optional configuration string in dynamically allocated memory.

The configuration string is stored inside a stralloc. config_data returns a pointer to the stralloc.

config returns 1 if c has been configured, 0 otherwise.

Initially c is set to CONFIG_STR, which is unconfigured.

READING CONFIGURATION

The following functions attempt to configure c. They return 0 without changing c if c is already configured; otherwise they configure c and return 0 if configuration data is supplied; otherwise they leave c unconfigured and return 0. They return -1, leaving c unconfigured, if a temporary error (out of memory, I/O error, etc.) occurs.

config_default configures c as a copy of the \0-terminated string s, if s is not the zero pointer.

config_copy configures c as a copy of d if d is configured.

config_env configures c as a copy of the first environment variable named s, if the environment has such a variable.

config_readline configures c as the first line of the file named s, if that file exists. It removes any trailing spaces or tabs from the line. It also converts every \0 inside the line to \n, so that the string can be used as a \0-terminated string after stralloc_0.

config_readfile configures c as the entire contents of the file named s, if that file exists. For each line, it removes any trailing spaces or tabs, converts every \0 inside the line to \n, and puts a \0 at the end of the line (even if the line was originally a partial final line). It skips blank lines and lines beginning with #.

SEE ALSO

stralloc(3)

Clone this wiki locally