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