The manual
ginnOS
The reference: libc, the libraries and the kernel
Loading...
Searching...
No Matches
conf.h
Go to the documentation of this file.
1#pragma once
2
3/**
4 * @file conf.h
5 * @brief settings files: sections of "key = value" lines.
6 *
7 * a settings file looks like this:
8 *
9 * # the look of the desktop
10 * [theme]
11 * mode = dark
12 * accent = #3b82f6
13 * font_size = 13
14 *
15 * - a line that starts with '#' or ';' is a comment. there are no comments
16 * after a value, so "#3b82f6" is a value.
17 * - keys before the first [section] are in the section "".
18 * - spaces around the key and the value are removed. a value in double
19 * quotes keeps its spaces (" x "); the quotes are removed.
20 * - if a key is in a section two times, the last one is used.
21 * - a bad line (no '=', no key, a [section without ']') is kept as it is
22 * when the file is written again, and gives a warning on stderr.
23 *
24 * a conf_t has two layers: the system defaults (/etc/ginnos/<name>.conf,
25 * read only) and the user's values (~/.config/ginnos/<name>.conf). a get
26 * looks at the user's values first. a set changes only the user's values,
27 * and conf_save writes only the user's file. comments, blank lines and the
28 * order of the lines stay as they were.
29 */
30
31#include <stdbool.h>
32#include <stdint.h>
33
34#define CONF_SYSTEM_DIR "/etc/ginnos"
35#define CONF_USER_DIR ".config/ginnos" // in the home directory
36
37typedef struct conf conf_t;
38
39/**
40 * open the settings called name: /etc/ginnos/<name>.conf and
41 * ~/.config/ginnos/<name>.conf. a file that is not there is empty.
42 * @param name letters, digits, '-' and '_' only.
43 * @return the settings, or NULL (a bad name, or no memory).
44 */
45conf_t *conf_open(const char *name);
46
47/**
48 * open two files. user_path is the file conf_save writes.
49 * @param system_path may be NULL (no defaults).
50 * @param user_path may be NULL (conf_save then fails).
51 */
52conf_t *conf_load(const char *system_path, const char *user_path);
53
54/**
55 * settings from text in memory (for tests). conf_save fails on them.
56 * @param system_text the defaults, may be NULL.
57 * @param user_text the user's values, may be NULL.
58 */
59conf_t *conf_from_text(const char *system_text, const char *user_text);
60
61void conf_free(conf_t *conf);
62
63/**
64 * a value.
65 * @return the user's value, else the system value, else fallback. the
66 * string is valid until the next set, unset or free.
67 */
68const char *conf_get(const conf_t *conf, const char *section, const char *key,
69 const char *fallback);
70
71/**
72 * the name of a section, to go through all of them: index 0, 1, ... until
73 * NULL. the sections of the system file come first, then the sections
74 * that only the user's file has, each in file order and each name one
75 * time. only a [name] line makes a section; keys before the first
76 * [section] are not one.
77 * @return the name, or NULL when index is past the last section. the
78 * string is valid until the next set, unset or free.
79 */
80const char *conf_section(const conf_t *conf, int index);
81
82/**
83 * a whole number: decimal, or hexadecimal with 0x.
84 * @return fallback if the key is not there or is not a number.
85 */
86long conf_get_int(const conf_t *conf, const char *section, const char *key, long fallback);
87
88/**
89 * true, yes, on, 1 / false, no, off, 0 (any letter case).
90 * @return fallback if the key is not there or is not one of these.
91 */
92bool conf_get_bool(const conf_t *conf, const char *section, const char *key, bool fallback);
93
94/**
95 * a colour: #rrggbb or #rrggbbaa.
96 * @return 0xAARRGGBB, not premultiplied (gfx_rgba makes a gfx colour of
97 * it); alpha is 0xFF for #rrggbb. fallback if the key is not there
98 * or is not a colour.
99 */
100uint32_t conf_get_color(const conf_t *conf, const char *section, const char *key,
101 uint32_t fallback);
102
103/**
104 * set a user value. the line is changed where it is; a new key goes after
105 * the last line of its section, and a new section goes at the end.
106 * @param section a name without '[', ']' or a line break.
107 * @param key a name without '=', a line break, or a '#' or ';' or '['
108 * at its start.
109 * @param value text without a line break.
110 * @return 0, or -1 (a bad name or value, or no memory).
111 */
112int conf_set(conf_t *conf, const char *section, const char *key, const char *value);
113int conf_set_int(conf_t *conf, const char *section, const char *key, long value);
114int conf_set_bool(conf_t *conf, const char *section, const char *key, bool value);
115int conf_set_color(conf_t *conf, const char *section, const char *key, uint32_t argb);
116
117/**
118 * remove the user's value, so the system value is used again.
119 * @return 0 (also when there was no user value), or -1 (bad arguments).
120 */
121int conf_unset(conf_t *conf, const char *section, const char *key);
122
123/**
124 * write the user's file: to a new file, fsync, then rename over the old
125 * one, so a crash leaves the old file or the new one. the directory is
126 * made if it is not there.
127 * @return 0, or -1 with errno set.
128 */
129int conf_save(const conf_t *conf);
130
131/**
132 * the user's file as text, as conf_save writes it (for tests).
133 * @return a string to free, or NULL (no memory).
134 */
135char *conf_user_text(const conf_t *conf);
136
137/**
138 * how many bad lines the two files had.
139 */
140int conf_bad_lines(const conf_t *conf);
int conf_set_bool(conf_t *conf, const char *section, const char *key, bool value)
Definition conf.c:683
const char * conf_section(const conf_t *conf, int index)
the name of a section, to go through all of them: index 0, 1, ... until NULL.
Definition conf.c:473
int conf_save(const conf_t *conf)
write the user's file: to a new file, fsync, then rename over the old one, so a crash leaves the old ...
Definition conf.c:770
const char * conf_get(const conf_t *conf, const char *section, const char *key, const char *fallback)
a value.
Definition conf.c:446
bool conf_get_bool(const conf_t *conf, const char *section, const char *key, bool fallback)
true, yes, on, 1 / false, no, off, 0 (any letter case).
Definition conf.c:517
char * conf_user_text(const conf_t *conf)
the user's file as text, as conf_save writes it (for tests).
Definition conf.c:709
int conf_bad_lines(const conf_t *conf)
how many bad lines the two files had.
Definition conf.c:817
conf_t * conf_from_text(const char *system_text, const char *user_text)
settings from text in memory (for tests).
Definition conf.c:352
uint32_t conf_get_color(const conf_t *conf, const char *section, const char *key, uint32_t fallback)
a colour: rrggbb or rrggbbaa.
Definition conf.c:545
int conf_set_int(conf_t *conf, const char *section, const char *key, long value)
Definition conf.c:676
void conf_free(conf_t *conf)
Definition conf.c:436
int conf_set_color(conf_t *conf, const char *section, const char *key, uint32_t argb)
Definition conf.c:688
conf_t * conf_open(const char *name)
open the settings called name: /etc/ginnos/<name>.conf and ~/.config/ginnos/<name>....
Definition conf.c:421
conf_t * conf_load(const char *system_path, const char *user_path)
open two files.
Definition conf.c:366
int conf_set(conf_t *conf, const char *section, const char *key, const char *value)
set a user value.
Definition conf.c:602
int conf_unset(conf_t *conf, const char *section, const char *key)
remove the user's value, so the system value is used again.
Definition conf.c:699
long conf_get_int(const conf_t *conf, const char *section, const char *key, long fallback)
a whole number: decimal, or hexadecimal with 0x.
Definition conf.c:500
struct conf conf_t
Definition conf.h:37
unsigned int uint32_t
Definition fb.h:17
integer types of fixed width.
bool, true and false.