chiark / gitweb /
19257abedcf3a66a65c8a91610c3955dabf98b87
[elogind.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
33  * files consisting of variable assignments only. */
34
35 /* Prototype for a parser for a specific configuration setting */
36 typedef int (*ConfigParserCallback)(const char *unit,
37                                     const char *filename,
38                                     unsigned line,
39                                     const char *section,
40                                     unsigned section_line,
41                                     const char *lvalue,
42                                     int ltype,
43                                     const char *rvalue,
44                                     void *data,
45                                     void *userdata);
46
47 /* Wraps information for parsing a specific configuration variable, to
48  * be stored in a simple array */
49 typedef struct ConfigTableItem {
50         const char *section;            /* Section */
51         const char *lvalue;             /* Name of the variable */
52         ConfigParserCallback parse;     /* Function that is called to parse the variable's value */
53         int ltype;                      /* Distinguish different variables passed to the same callback */
54         void *data;                     /* Where to store the variable's data */
55 } ConfigTableItem;
56
57 /* Wraps information for parsing a specific configuration variable, to
58  * be stored in a gperf perfect hashtable */
59 typedef struct ConfigPerfItem {
60         const char *section_and_lvalue; /* Section + "." + name of the variable */
61         ConfigParserCallback parse;     /* Function that is called to parse the variable's value */
62         int ltype;                      /* Distinguish different variables passed to the same callback */
63         size_t offset;                  /* Offset where to store data, from the beginning of userdata */
64 } ConfigPerfItem;
65
66 /* Prototype for a low-level gperf lookup function */
67 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
68
69 /* Prototype for a generic high-level lookup function */
70 typedef int (*ConfigItemLookup)(
71                 const void *table,
72                 const char *section,
73                 const char *lvalue,
74                 ConfigParserCallback *func,
75                 int *ltype,
76                 void **data,
77                 void *userdata);
78
79 /* Linear table search implementation of ConfigItemLookup, based on
80  * ConfigTableItem arrays */
81 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
82
83 /* gperf implementation of ConfigItemLookup, based on gperf
84  * ConfigPerfItem tables */
85 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
86
87 int config_parse(
88                 const char *unit,
89                 const char *filename,
90                 FILE *f,
91                 const char *sections,  /* nulstr */
92                 ConfigItemLookup lookup,
93                 const void *table,
94                 bool relaxed,
95                 bool allow_include,
96                 bool warn,
97                 void *userdata);
98
99 int config_parse_many_nulstr(
100                 const char *conf_file,      /* possibly NULL */
101                 const char *conf_file_dirs, /* nulstr */
102                 const char *sections,       /* nulstr */
103                 ConfigItemLookup lookup,
104                 const void *table,
105                 bool relaxed,
106                 void *userdata);
107
108 /* Generic parsers */
109 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);
110 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);
111 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);
112 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);
113 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);
114 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);
115 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);
116 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);
117 #if 0 /// UNNEEDED by elogind
118 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);
119 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);
120 #endif // 0
121 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);
122 #if 0 /// UNNEEDED by elogind
123 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);
124 #endif // 0
125 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);
126 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);
127 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);
128 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);
129 #if 0 /// UNNEEDED by elogind
130 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);
131 #endif // 0
132 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);
133 #if 0 /// UNNEEDED by elogind
134 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);
135 #endif // 0
136 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);
137 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);
138 #if 0 /// UNNEEDED by elogind
139 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);
140 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);
141 #endif // 0
142
143 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
144         int function(const char *unit,                                  \
145                      const char *filename,                              \
146                      unsigned line,                                     \
147                      const char *section,                               \
148                      unsigned section_line,                             \
149                      const char *lvalue,                                \
150                      int ltype,                                         \
151                      const char *rvalue,                                \
152                      void *data,                                        \
153                      void *userdata) {                                  \
154                                                                         \
155                 type *i = data, x;                                      \
156                                                                         \
157                 assert(filename);                                       \
158                 assert(lvalue);                                         \
159                 assert(rvalue);                                         \
160                 assert(data);                                           \
161                                                                         \
162                 if ((x = name##_from_string(rvalue)) < 0) {             \
163                         log_syntax(unit, LOG_ERR, filename, line, -x,   \
164                                    msg ", ignoring: %s", rvalue);       \
165                         return 0;                                       \
166                 }                                                       \
167                                                                         \
168                 *i = x;                                                 \
169                 return 0;                                               \
170         }
171
172 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg)              \
173         int function(const char *unit,                                         \
174                      const char *filename,                                     \
175                      unsigned line,                                            \
176                      const char *section,                                      \
177                      unsigned section_line,                                    \
178                      const char *lvalue,                                       \
179                      int ltype,                                                \
180                      const char *rvalue,                                       \
181                      void *data,                                               \
182                      void *userdata) {                                         \
183                                                                                \
184                 type **enums = data, x, *ys;                                   \
185                 _cleanup_free_ type *xs = NULL;                                \
186                 const char *word, *state;                                      \
187                 size_t l, i = 0;                                               \
188                                                                                \
189                 assert(filename);                                              \
190                 assert(lvalue);                                                \
191                 assert(rvalue);                                                \
192                 assert(data);                                                  \
193                                                                                \
194                 xs = new0(type, 1);                                            \
195                 if (!xs)                                                       \
196                         return -ENOMEM;                                        \
197                                                                                \
198                 *xs = invalid;                                                 \
199                                                                                \
200                 FOREACH_WORD(word, l, rvalue, state) {                         \
201                         _cleanup_free_ char *en = NULL;                        \
202                         type *new_xs;                                          \
203                                                                                \
204                         en = strndup(word, l);                                 \
205                         if (!en)                                               \
206                                 return -ENOMEM;                                \
207                                                                                \
208                         if ((x = name##_from_string(en)) < 0) {                \
209                                 log_syntax(unit, LOG_ERR, filename, line,      \
210                                        -x, msg ", ignoring: %s", en);          \
211                                 continue;                                      \
212                         }                                                      \
213                                                                                \
214                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
215                                 if (*ys == x) {                                \
216                                         log_syntax(unit, LOG_ERR, filename,    \
217                                               line, -x,                        \
218                                               "Duplicate entry, ignoring: %s", \
219                                               en);                             \
220                                         x = invalid;                           \
221                                 }                                              \
222                         }                                                      \
223                                                                                \
224                         if (x == invalid)                                      \
225                                 continue;                                      \
226                                                                                \
227                         *(xs + i) = x;                                         \
228                         new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
229                         if (new_xs)                                            \
230                                 xs = new_xs;                                   \
231                         else                                                   \
232                                 return -ENOMEM;                                \
233                                                                                \
234                         *(xs + i) = invalid;                                   \
235                 }                                                              \
236                                                                                \
237                 free(*enums);                                                  \
238                 *enums = xs;                                                   \
239                 xs = NULL;                                                     \
240                                                                                \
241                 return 0;                                                      \
242         }