chiark / gitweb /
23cdc7293046b56275101e356f8305ae228b1e20
[elogind.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                                     unsigned section_line,
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 */
46 typedef 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
55  * be stored in a gperf perfect hashtable */
56 typedef 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 */
64 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
65
66 /* Prototype for a generic high-level lookup function */
67 typedef int (*ConfigItemLookup)(
68                 const void *table,
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 */
78 int config_item_table_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
79
80 /* gperf implementation of ConfigItemLookup, based on gperf
81  * ConfigPerfItem tables */
82 int config_item_perf_lookup(const void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
83
84 int config_parse(const char *unit,
85                  const char *filename,
86                  FILE *f,
87                  const char *sections,  /* nulstr */
88                  ConfigItemLookup lookup,
89                  const void *table,
90                  bool relaxed,
91                  bool allow_include,
92                  bool warn,
93                  void *userdata);
94
95 int 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
103 /* Generic parsers */
104 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);
105 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);
106 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);
107 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);
108 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);
109 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);
110 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);
111 /// UNNEEDED by elogind
112 #if 0
113 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);
114 int 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);
115 #endif // 0
116 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);
117 /// UNNEEDED by elogind
118 #if 0
119 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);
120 #endif // 0
121 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);
122 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);
123 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);
124 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);
125 /// UNNEEDED by elogind
126 #if 0
127 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);
128 #endif // 0
129 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);
130 /// UNNEEDED by elogind
131 #if 0
132 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);
133 #endif // 0
134 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);
135 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);
136 /// UNNEEDED by elogind
137 #if 0
138 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);
139 #endif // 0
140
141 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
142         int function(const char *unit,                                  \
143                      const char *filename,                              \
144                      unsigned line,                                     \
145                      const char *section,                               \
146                      unsigned section_line,                             \
147                      const char *lvalue,                                \
148                      int ltype,                                         \
149                      const char *rvalue,                                \
150                      void *data,                                        \
151                      void *userdata) {                                  \
152                                                                         \
153                 type *i = data, x;                                      \
154                                                                         \
155                 assert(filename);                                       \
156                 assert(lvalue);                                         \
157                 assert(rvalue);                                         \
158                 assert(data);                                           \
159                                                                         \
160                 if ((x = name##_from_string(rvalue)) < 0) {             \
161                         log_syntax(unit, LOG_ERR, filename, line, -x,   \
162                                    msg ", ignoring: %s", rvalue);       \
163                         return 0;                                       \
164                 }                                                       \
165                                                                         \
166                 *i = x;                                                 \
167                 return 0;                                               \
168         }
169
170 #define DEFINE_CONFIG_PARSE_ENUMV(function,name,type,invalid,msg)              \
171         int function(const char *unit,                                         \
172                      const char *filename,                                     \
173                      unsigned line,                                            \
174                      const char *section,                                      \
175                      unsigned section_line,                                    \
176                      const char *lvalue,                                       \
177                      int ltype,                                                \
178                      const char *rvalue,                                       \
179                      void *data,                                               \
180                      void *userdata) {                                         \
181                                                                                \
182                 type **enums = data, x, *ys;                                   \
183                 _cleanup_free_ type *xs = NULL;                                \
184                 const char *word, *state;                                      \
185                 size_t l, i = 0;                                               \
186                                                                                \
187                 assert(filename);                                              \
188                 assert(lvalue);                                                \
189                 assert(rvalue);                                                \
190                 assert(data);                                                  \
191                                                                                \
192                 xs = new0(type, 1);                                            \
193                 if(!xs)                                                        \
194                         return -ENOMEM;                                        \
195                                                                                \
196                 *xs = invalid;                                                 \
197                                                                                \
198                 FOREACH_WORD(word, l, rvalue, state) {                         \
199                         _cleanup_free_ char *en = NULL;                        \
200                         type *new_xs;                                          \
201                                                                                \
202                         en = strndup(word, l);                                 \
203                         if (!en)                                               \
204                                 return -ENOMEM;                                \
205                                                                                \
206                         if ((x = name##_from_string(en)) < 0) {                \
207                                 log_syntax(unit, LOG_ERR, filename, line,      \
208                                        -x, msg ", ignoring: %s", en);          \
209                                 continue;                                      \
210                         }                                                      \
211                                                                                \
212                         for (ys = xs; x != invalid && *ys != invalid; ys++) {  \
213                                 if (*ys == x) {                                \
214                                         log_syntax(unit, LOG_ERR, filename,    \
215                                               line, -x,                        \
216                                               "Duplicate entry, ignoring: %s", \
217                                               en);                             \
218                                         x = invalid;                           \
219                                 }                                              \
220                         }                                                      \
221                                                                                \
222                         if (x == invalid)                                      \
223                                 continue;                                      \
224                                                                                \
225                         *(xs + i) = x;                                         \
226                         new_xs = realloc(xs, (++i + 1) * sizeof(type));        \
227                         if (new_xs)                                            \
228                                 xs = new_xs;                                   \
229                         else                                                   \
230                                 return -ENOMEM;                                \
231                                                                                \
232                         *(xs + i) = invalid;                                   \
233                 }                                                              \
234                                                                                \
235                 free(*enums);                                                  \
236                 *enums = xs;                                                   \
237                 xs = NULL;                                                     \
238                                                                                \
239                 return 0;                                                      \
240         }