]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
Remove dead code and unexport some calls
[thirdparty/systemd.git] / src / shared / conf-parser.h
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 */
33 typedef int (*ConfigParserCallback)(const char *unit,
34 const char *filename,
35 unsigned line,
36 const char *section,
37 const char *lvalue,
38 int ltype,
39 const char *rvalue,
40 void *data,
41 void *userdata);
42
43 /* Wraps information for parsing a specific configuration variable, to
44 * be stored in a simple array */
45 typedef struct ConfigTableItem {
46 const char *section; /* Section */
47 const char *lvalue; /* Name of the variable */
48 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
49 int ltype; /* Distinguish different variables passed to the same callback */
50 void *data; /* Where to store the variable's data */
51 } ConfigTableItem;
52
53 /* Wraps information for parsing a specific configuration variable, to
54 * ve srored in a gperf perfect hashtable */
55 typedef struct ConfigPerfItem {
56 const char *section_and_lvalue; /* Section + "." + name of the variable */
57 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
58 int ltype; /* Distinguish different variables passed to the same callback */
59 size_t offset; /* Offset where to store data, from the beginning of userdata */
60 } ConfigPerfItem;
61
62 /* Prototype for a low-level gperf lookup function */
63 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
64
65 /* Prototype for a generic high-level lookup function */
66 typedef int (*ConfigItemLookup)(
67 void *table,
68 const char *section,
69 const char *lvalue,
70 ConfigParserCallback *func,
71 int *ltype,
72 void **data,
73 void *userdata);
74
75 /* Linear table search implementation of ConfigItemLookup, based on
76 * ConfigTableItem arrays */
77 int config_item_table_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
78
79 /* gperf implementation of ConfigItemLookup, based on gperf
80 * ConfigPerfItem tables */
81 int config_item_perf_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
82
83 int config_parse(const char *unit,
84 const char *filename,
85 FILE *f,
86 const char *sections, /* nulstr */
87 ConfigItemLookup lookup,
88 void *table,
89 bool relaxed,
90 bool allow_include,
91 void *userdata);
92
93 /* Generic parsers */
94 int config_parse_int(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
95 int config_parse_unsigned(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
96 int config_parse_long(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
97 int config_parse_uint64(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
98 int config_parse_double(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
99 int config_parse_bytes_size(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
100 int config_parse_bytes_off(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
101 int config_parse_bool(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
102 int config_parse_string(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
103 int config_parse_path(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
104 int config_parse_strv(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105 int config_parse_path_strv(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
106 int config_parse_sec(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
107 int config_parse_nsec(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
108 int config_parse_mode(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
109 int config_parse_facility(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
110 int config_parse_level(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
111 int config_parse_set_status(const char *unit, const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
112
113 int log_syntax_internal(const char *unit, int level,
114 const char *file, unsigned line, const char *func,
115 const char *config_file, unsigned config_line,
116 int error, const char *format, ...) _printf_(9, 10);
117
118 #define log_syntax(unit, level, config_file, config_line, error, ...) \
119 log_syntax_internal(unit, level, \
120 __FILE__, __LINE__, __func__, \
121 config_file, config_line, \
122 error, __VA_ARGS__)
123
124 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
125 int function(const char *unit, \
126 const char *filename, \
127 unsigned line, \
128 const char *section, \
129 const char *lvalue, \
130 int ltype, \
131 const char *rvalue, \
132 void *data, \
133 void *userdata) { \
134 \
135 type *i = data, x; \
136 \
137 assert(filename); \
138 assert(lvalue); \
139 assert(rvalue); \
140 assert(data); \
141 \
142 if ((x = name##_from_string(rvalue)) < 0) { \
143 log_syntax(unit, LOG_ERR, filename, line, -x, \
144 msg ", ignoring: %s", rvalue); \
145 return 0; \
146 } \
147 \
148 *i = x; \
149 return 0; \
150 }
151
152 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg) \
153 int function(const char *unit, \
154 const char *filename, \
155 unsigned line, \
156 const char *section, \
157 const char *lvalue, \
158 int ltype, \
159 const char *rvalue, \
160 void *data, \
161 void *userdata) { \
162 \
163 type **enums = data, *xs, x, *ys; \
164 char *w, *state; \
165 size_t l, i = 0; \
166 \
167 assert(filename); \
168 assert(lvalue); \
169 assert(rvalue); \
170 assert(data); \
171 \
172 xs = new0(type, 1); \
173 *xs = invalid; \
174 \
175 FOREACH_WORD(w, l, rvalue, state) { \
176 _cleanup_free_ char *en = NULL; \
177 \
178 en = strndup(w, l); \
179 if (!en) \
180 return -ENOMEM; \
181 \
182 if ((x = name##_from_string(en)) < 0) { \
183 log_syntax(unit, LOG_ERR, filename, line, \
184 -x, msg ", ignoring: %s", en); \
185 continue; \
186 } \
187 \
188 for (ys = xs; x != invalid && *ys != invalid; ys++) { \
189 if (*ys == x) { \
190 log_syntax(unit, LOG_ERR, filename, \
191 line, -x, \
192 "Duplicate entry, ignoring: %s", \
193 en); \
194 x = invalid; \
195 } \
196 } \
197 \
198 if (x == invalid) \
199 continue; \
200 \
201 *(xs + i) = x; \
202 xs = realloc(xs, ++i + 1); \
203 if (!xs) \
204 return -ENOMEM; \
205 *(xs + i) = invalid; \
206 } \
207 \
208 free(*enums); \
209 *enums = xs; \
210 return 0; \
211 }