]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
conf-parser: turn three bool function params into a flags fields
[thirdparty/systemd.git] / src / shared / conf-parser.h
1 #pragma once
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <stdio.h>
26 #include <syslog.h>
27
28 #include "alloc-util.h"
29 #include "log.h"
30 #include "macro.h"
31
32 /* An abstract parser for simple, line based, shallow configuration files consisting of variable assignments only. */
33
34 typedef enum ConfigParseFlags {
35 CONFIG_PARSE_RELAXED = 1U << 0,
36 CONFIG_PARSE_ALLOW_INCLUDE = 1U << 1,
37 CONFIG_PARSE_WARN = 1U << 2,
38 CONFIG_PARSE_REFUSE_BOM = 1U << 3,
39 } ConfigParseFlags;
40
41 /* Prototype for a parser for a specific configuration setting */
42 typedef int (*ConfigParserCallback)(const char *unit,
43 const char *filename,
44 unsigned line,
45 const char *section,
46 unsigned section_line,
47 const char *lvalue,
48 int ltype,
49 const char *rvalue,
50 void *data,
51 void *userdata);
52
53 /* Wraps information for parsing a specific configuration variable, to
54 * be stored in a simple array */
55 typedef struct ConfigTableItem {
56 const char *section; /* Section */
57 const char *lvalue; /* 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 void *data; /* Where to store the variable's data */
61 } ConfigTableItem;
62
63 /* Wraps information for parsing a specific configuration variable, to
64 * be stored in a gperf perfect hashtable */
65 typedef struct ConfigPerfItem {
66 const char *section_and_lvalue; /* Section + "." + name of the variable */
67 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
68 int ltype; /* Distinguish different variables passed to the same callback */
69 size_t offset; /* Offset where to store data, from the beginning of userdata */
70 } ConfigPerfItem;
71
72 /* Prototype for a low-level gperf lookup function */
73 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
74
75 /* Prototype for a generic high-level lookup function */
76 typedef int (*ConfigItemLookup)(
77 const void *table,
78 const char *section,
79 const char *lvalue,
80 ConfigParserCallback *func,
81 int *ltype,
82 void **data,
83 void *userdata);
84
85 /* Linear table search implementation of ConfigItemLookup, based on
86 * ConfigTableItem arrays */
87 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
88
89 /* gperf implementation of ConfigItemLookup, based on gperf
90 * ConfigPerfItem tables */
91 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
92
93 int config_parse(
94 const char *unit,
95 const char *filename,
96 FILE *f,
97 const char *sections, /* nulstr */
98 ConfigItemLookup lookup,
99 const void *table,
100 ConfigParseFlags flags,
101 void *userdata);
102
103 int config_parse_many_nulstr(
104 const char *conf_file, /* possibly NULL */
105 const char *conf_file_dirs, /* nulstr */
106 const char *sections, /* nulstr */
107 ConfigItemLookup lookup,
108 const void *table,
109 ConfigParseFlags flags,
110 void *userdata);
111
112 int config_parse_many(
113 const char *conf_file, /* possibly NULL */
114 const char* const* conf_file_dirs,
115 const char *dropin_dirname,
116 const char *sections, /* nulstr */
117 ConfigItemLookup lookup,
118 const void *table,
119 ConfigParseFlags flags,
120 void *userdata);
121
122 /* Generic parsers */
123 int 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);
124 int 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);
125 int 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);
126 int config_parse_uint8(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);
127 int config_parse_uint16(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);
128 int 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);
129 int 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);
130 int 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);
131 int 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);
132 int 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);
133 int config_parse_iec_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);
134 int 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);
135 int config_parse_tristate(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);
136 int 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);
137 int 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);
138 int 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);
139 int 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);
140 int 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);
141 int 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);
142 int 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);
143 int 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);
144 int config_parse_signal(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);
145 int config_parse_personality(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);
146 int config_parse_ifname(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);
147 int config_parse_ip_port(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);
148
149 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
150 int function(const char *unit, \
151 const char *filename, \
152 unsigned line, \
153 const char *section, \
154 unsigned section_line, \
155 const char *lvalue, \
156 int ltype, \
157 const char *rvalue, \
158 void *data, \
159 void *userdata) { \
160 \
161 type *i = data, x; \
162 \
163 assert(filename); \
164 assert(lvalue); \
165 assert(rvalue); \
166 assert(data); \
167 \
168 if ((x = name##_from_string(rvalue)) < 0) { \
169 log_syntax(unit, LOG_ERR, filename, line, -x, \
170 msg ", ignoring: %s", rvalue); \
171 return 0; \
172 } \
173 \
174 *i = x; \
175 return 0; \
176 }
177
178 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
179 int function(const char *unit, \
180 const char *filename, \
181 unsigned line, \
182 const char *section, \
183 unsigned section_line, \
184 const char *lvalue, \
185 int ltype, \
186 const char *rvalue, \
187 void *data, \
188 void *userdata) { \
189 \
190 type **enums = data, x, *ys; \
191 _cleanup_free_ type *xs = NULL; \
192 const char *word, *state; \
193 size_t l, i = 0; \
194 \
195 assert(filename); \
196 assert(lvalue); \
197 assert(rvalue); \
198 assert(data); \
199 \
200 xs = new0(type, 1); \
201 if (!xs) \
202 return -ENOMEM; \
203 \
204 *xs = invalid; \
205 \
206 FOREACH_WORD(word, l, rvalue, state) { \
207 _cleanup_free_ char *en = NULL; \
208 type *new_xs; \
209 \
210 en = strndup(word, l); \
211 if (!en) \
212 return -ENOMEM; \
213 \
214 if ((x = name##_from_string(en)) < 0) { \
215 log_syntax(unit, LOG_ERR, filename, line, \
216 -x, msg ", ignoring: %s", en); \
217 continue; \
218 } \
219 \
220 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
221 if (*ys == x) { \
222 log_syntax(unit, LOG_ERR, filename, \
223 line, -x, \
224 "Duplicate entry, ignoring: %s", \
225 en); \
226 x = invalid; \
227 } \
228 } \
229 \
230 if (x == invalid) \
231 continue; \
232 \
233 *(xs + i) = x; \
234 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
235 if (new_xs) \
236 xs = new_xs; \
237 else \
238 return -ENOMEM; \
239 \
240 *(xs + i) = invalid; \
241 } \
242 \
243 free(*enums); \
244 *enums = xs; \
245 xs = NULL; \
246 \
247 return 0; \
248 }