]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/shared/conf-parser.h
networkd: dhcp-server - allow configuration of the pool
[thirdparty/systemd.git] / src / shared / conf-parser.h
CommitLineData
03467c88 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
ed5bcfbe 2
c2f1db8f 3#pragma once
ed5bcfbe 4
a7334b09
LP
5/***
6 This file is part of systemd.
7
8 Copyright 2010 Lennart Poettering
9
10 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
13 (at your option) any later version.
14
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 18 Lesser General Public License for more details.
a7334b09 19
5430f7f2 20 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
ed5bcfbe 24#include <stdio.h>
10e87ee7 25#include <stdbool.h>
ed5bcfbe 26
e8e581bf
ZJS
27#include "macro.h"
28
ed5bcfbe
LP
29/* An abstract parser for simple, line based, shallow configuration
30 * files consisting of variable assignments only. */
31
f975e971 32/* Prototype for a parser for a specific configuration setting */
e8e581bf
ZJS
33typedef int (*ConfigParserCallback)(const char *unit,
34 const char *filename,
35 unsigned line,
36 const char *section,
71a61510 37 unsigned section_line,
e8e581bf
ZJS
38 const char *lvalue,
39 int ltype,
40 const char *rvalue,
41 void *data,
42 void *userdata);
f975e971
LP
43
44/* Wraps information for parsing a specific configuration variable, to
45 * be stored in a simple array */
46typedef struct ConfigTableItem {
47 const char *section; /* Section */
48 const char *lvalue; /* Name of the variable */
49 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
50 int ltype; /* Distinguish different variables passed to the same callback */
51 void *data; /* Where to store the variable's data */
52} ConfigTableItem;
53
54/* Wraps information for parsing a specific configuration variable, to
e5a7f173 55 * be stored in a gperf perfect hashtable */
f975e971
LP
56typedef struct ConfigPerfItem {
57 const char *section_and_lvalue; /* Section + "." + name of the variable */
58 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
59 int ltype; /* Distinguish different variables passed to the same callback */
60 size_t offset; /* Offset where to store data, from the beginning of userdata */
61} ConfigPerfItem;
62
63/* Prototype for a low-level gperf lookup function */
64typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
65
66/* Prototype for a generic high-level lookup function */
67typedef int (*ConfigItemLookup)(
e9f3d2d5 68 const void *table,
f975e971
LP
69 const char *section,
70 const char *lvalue,
71 ConfigParserCallback *func,
72 int *ltype,
73 void **data,
74 void *userdata);
75
76/* Linear table search implementation of ConfigItemLookup, based on
77 * ConfigTableItem arrays */
e9f3d2d5 78int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
f975e971
LP
79
80/* gperf implementation of ConfigItemLookup, based on gperf
81 * ConfigPerfItem tables */
e9f3d2d5 82int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
f975e971 83
e8e581bf
ZJS
84int config_parse(const char *unit,
85 const char *filename,
86 FILE *f,
87 const char *sections, /* nulstr */
88 ConfigItemLookup lookup,
e9f3d2d5 89 const void *table,
e8e581bf 90 bool relaxed,
db5c0122 91 bool allow_include,
36f822c4 92 bool warn,
e8e581bf 93 void *userdata);
ed5bcfbe 94
e8461023
JT
95int config_parse_many(const char *conf_file, /* possibly NULL */
96 const char *conf_file_dirs, /* nulstr */
97 const char *sections, /* nulstr */
98 ConfigItemLookup lookup,
99 const void *table,
100 bool relaxed,
101 void *userdata);
102
ed5bcfbe 103/* Generic parsers */
71a61510
TG
104int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
106int config_parse_long(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
9b3a67c5 107int config_parse_uint32(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
71a61510
TG
108int config_parse_uint64(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
109int config_parse_double(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
5556b5fe
LP
110int config_parse_iec_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
111int config_parse_si_size(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
112int config_parse_iec_off(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
71a61510
TG
113int config_parse_bool(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
114int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
115int config_parse_path(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
116int config_parse_strv(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
71a61510
TG
117int config_parse_sec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
118int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
119int config_parse_mode(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
ca37242e
LP
120int config_parse_log_facility(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
121int config_parse_log_level(const char *unit, const char *filename, unsigned line, const char *section, unsigned section_line, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
e8e581bf 122
978553ce
LP
123#define log_invalid_utf8(unit, level, config_file, config_line, error, rvalue) \
124 do { \
125 _cleanup_free_ char *_p = utf8_escape_invalid(rvalue); \
126 log_syntax(unit, level, config_file, config_line, error, \
127 "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \
128 } while(false)
b5d74213 129
487393e9 130#define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
e8e581bf
ZJS
131 int function(const char *unit, \
132 const char *filename, \
133 unsigned line, \
134 const char *section, \
71a61510 135 unsigned section_line, \
e8e581bf
ZJS
136 const char *lvalue, \
137 int ltype, \
138 const char *rvalue, \
139 void *data, \
140 void *userdata) { \
487393e9
LP
141 \
142 type *i = data, x; \
143 \
144 assert(filename); \
145 assert(lvalue); \
146 assert(rvalue); \
147 assert(data); \
148 \
149 if ((x = name##_from_string(rvalue)) < 0) { \
e8e581bf
ZJS
150 log_syntax(unit, LOG_ERR, filename, line, -x, \
151 msg ", ignoring: %s", rvalue); \
c0b34696 152 return 0; \
487393e9
LP
153 } \
154 \
155 *i = x; \
487393e9
LP
156 return 0; \
157 }
916484f5
TG
158
159#define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
160 int function(const char *unit, \
161 const char *filename, \
162 unsigned line, \
163 const char *section, \
71a61510 164 unsigned section_line, \
916484f5
TG
165 const char *lvalue, \
166 int ltype, \
167 const char *rvalue, \
168 void *data, \
169 void *userdata) { \
170 \
77c10205
TG
171 type **enums = data, x, *ys; \
172 _cleanup_free_ type *xs = NULL; \
a2a5291b 173 const char *word, *state; \
916484f5
TG
174 size_t l, i = 0; \
175 \
176 assert(filename); \
177 assert(lvalue); \
178 assert(rvalue); \
179 assert(data); \
180 \
181 xs = new0(type, 1); \
83e341a6
TG
182 if(!xs) \
183 return -ENOMEM; \
184 \
916484f5
TG
185 *xs = invalid; \
186 \
a2a5291b 187 FOREACH_WORD(word, l, rvalue, state) { \
916484f5 188 _cleanup_free_ char *en = NULL; \
77c10205 189 type *new_xs; \
916484f5 190 \
a2a5291b 191 en = strndup(word, l); \
916484f5
TG
192 if (!en) \
193 return -ENOMEM; \
194 \
195 if ((x = name##_from_string(en)) < 0) { \
196 log_syntax(unit, LOG_ERR, filename, line, \
197 -x, msg ", ignoring: %s", en); \
198 continue; \
199 } \
200 \
201 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
202 if (*ys == x) { \
203 log_syntax(unit, LOG_ERR, filename, \
204 line, -x, \
205 "Duplicate entry, ignoring: %s", \
206 en); \
207 x = invalid; \
208 } \
209 } \
210 \
211 if (x == invalid) \
212 continue; \
213 \
214 *(xs + i) = x; \
77c10205
TG
215 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
216 if (new_xs) \
217 xs = new_xs; \
218 else \
916484f5 219 return -ENOMEM; \
83e341a6 220 \
916484f5
TG
221 *(xs + i) = invalid; \
222 } \
223 \
224 free(*enums); \
225 *enums = xs; \
77c10205
TG
226 xs = NULL; \
227 \
916484f5
TG
228 return 0; \
229 }