]> git.proxmox.com Git - systemd.git/blame - src/shared/conf-parser.h
Imported Upstream version 226
[systemd.git] / src / shared / conf-parser.h
CommitLineData
663996b3
MS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3#pragma once
4
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
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
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
18 Lesser General Public License for more details.
19
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22***/
23
24#include <stdio.h>
25#include <stdbool.h>
26
27#include "macro.h"
28
29/* An abstract parser for simple, line based, shallow configuration
30 * files consisting of variable assignments only. */
31
32/* Prototype for a parser for a specific configuration setting */
33typedef int (*ConfigParserCallback)(const char *unit,
34 const char *filename,
35 unsigned line,
36 const char *section,
60f067b4 37 unsigned section_line,
663996b3
MS
38 const char *lvalue,
39 int ltype,
40 const char *rvalue,
41 void *data,
42 void *userdata);
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
60f067b4 55 * be stored in a gperf perfect hashtable */
663996b3
MS
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)(
5eef597e 68 const void *table,
663996b3
MS
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 */
5eef597e 78int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
663996b3
MS
79
80/* gperf implementation of ConfigItemLookup, based on gperf
81 * ConfigPerfItem tables */
5eef597e 82int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
663996b3
MS
83
84int config_parse(const char *unit,
85 const char *filename,
86 FILE *f,
87 const char *sections, /* nulstr */
88 ConfigItemLookup lookup,
5eef597e 89 const void *table,
663996b3
MS
90 bool relaxed,
91 bool allow_include,
5eef597e 92 bool warn,
663996b3
MS
93 void *userdata);
94
f47781d8
MP
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
663996b3 103/* Generic parsers */
60f067b4
JS
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);
d9dfd233 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);
60f067b4
JS
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);
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);
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);
d9dfd233 114int 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);
60f067b4
JS
115int 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);
116int 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);
117int 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);
118int 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);
119int 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);
120int 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);
121int 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);
122int 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);
d9dfd233
MP
123int 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);
124int 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);
663996b3 125
e735f4d4
MP
126#define log_invalid_utf8(unit, level, config_file, config_line, error, rvalue) \
127 do { \
128 _cleanup_free_ char *_p = utf8_escape_invalid(rvalue); \
129 log_syntax(unit, level, config_file, config_line, error, \
130 "String is not UTF-8 clean, ignoring assignment: %s", strna(_p)); \
131 } while(false)
60f067b4 132
663996b3
MS
133#define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
134 int function(const char *unit, \
135 const char *filename, \
136 unsigned line, \
137 const char *section, \
60f067b4 138 unsigned section_line, \
663996b3
MS
139 const char *lvalue, \
140 int ltype, \
141 const char *rvalue, \
142 void *data, \
143 void *userdata) { \
144 \
145 type *i = data, x; \
146 \
147 assert(filename); \
148 assert(lvalue); \
149 assert(rvalue); \
150 assert(data); \
151 \
152 if ((x = name##_from_string(rvalue)) < 0) { \
153 log_syntax(unit, LOG_ERR, filename, line, -x, \
154 msg ", ignoring: %s", rvalue); \
155 return 0; \
156 } \
157 \
158 *i = x; \
159 return 0; \
160 }
60f067b4
JS
161
162#define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
163 int function(const char *unit, \
164 const char *filename, \
165 unsigned line, \
166 const char *section, \
167 unsigned section_line, \
168 const char *lvalue, \
169 int ltype, \
170 const char *rvalue, \
171 void *data, \
172 void *userdata) { \
173 \
5eef597e
MP
174 type **enums = data, x, *ys; \
175 _cleanup_free_ type *xs = NULL; \
176 const char *word, *state; \
60f067b4
JS
177 size_t l, i = 0; \
178 \
179 assert(filename); \
180 assert(lvalue); \
181 assert(rvalue); \
182 assert(data); \
183 \
184 xs = new0(type, 1); \
5eef597e
MP
185 if(!xs) \
186 return -ENOMEM; \
187 \
60f067b4
JS
188 *xs = invalid; \
189 \
5eef597e 190 FOREACH_WORD(word, l, rvalue, state) { \
60f067b4 191 _cleanup_free_ char *en = NULL; \
5eef597e 192 type *new_xs; \
60f067b4 193 \
5eef597e 194 en = strndup(word, l); \
60f067b4
JS
195 if (!en) \
196 return -ENOMEM; \
197 \
198 if ((x = name##_from_string(en)) < 0) { \
199 log_syntax(unit, LOG_ERR, filename, line, \
200 -x, msg ", ignoring: %s", en); \
201 continue; \
202 } \
203 \
204 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
205 if (*ys == x) { \
206 log_syntax(unit, LOG_ERR, filename, \
207 line, -x, \
208 "Duplicate entry, ignoring: %s", \
209 en); \
210 x = invalid; \
211 } \
212 } \
213 \
214 if (x == invalid) \
215 continue; \
216 \
217 *(xs + i) = x; \
5eef597e
MP
218 new_xs = realloc(xs, (++i + 1) * sizeof(type)); \
219 if (new_xs) \
220 xs = new_xs; \
221 else \
60f067b4 222 return -ENOMEM; \
5eef597e 223 \
60f067b4
JS
224 *(xs + i) = invalid; \
225 } \
226 \
227 free(*enums); \
228 *enums = xs; \
5eef597e
MP
229 xs = NULL; \
230 \
60f067b4
JS
231 return 0; \
232 }