chiark / gitweb /
units: add missing target files
[elogind.git] / src / conf-parser.h
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #ifndef fooconfparserhfoo
4 #define fooconfparserhfoo
5
6 /***
7   This file is part of systemd.
8
9   Copyright 2010 Lennart Poettering
10
11   systemd is free software; you can redistribute it and/or modify it
12   under the terms of the GNU General Public License as published by
13   the Free Software Foundation; either version 2 of the License, or
14   (at your option) any later version.
15
16   systemd is distributed in the hope that it will be useful, but
17   WITHOUT ANY WARRANTY; without even the implied warranty of
18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19   General Public License for more details.
20
21   You should have received a copy of the GNU General Public License
22   along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 ***/
24
25 #include <stdio.h>
26 #include <stdbool.h>
27
28 /* An abstract parser for simple, line based, shallow configuration
29  * files consisting of variable assignments only. */
30
31 typedef int (*ConfigParserCallback)(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
32
33 /* Wraps info for parsing a specific configuration variable */
34 typedef struct ConfigItem {
35         const char *lvalue; /* name of the variable */
36         ConfigParserCallback parse; /* Function that is called to parse the variable's value */
37         void *data; /* Where to store the variable's data */
38         const char *section;
39 } ConfigItem;
40
41 /* The configuration file parsing routine. Expects a table of
42  * config_items in *t that is terminated by an item where lvalue is
43  * NULL */
44 int config_parse(const char *filename, FILE *f, const char* const *sections, const ConfigItem *t, bool relaxed, void *userdata);
45
46 /* Generic parsers */
47 int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
48 int config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
49 int config_parse_size(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
50 int config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
51 int config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
52 int config_parse_path(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
53 int config_parse_strv(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
54 int config_parse_path_strv(const char *filename, unsigned line, const char *section, const char *lvalue, const char *rvalue, void *data, void *userdata);
55
56 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
57         int function(                                                   \
58                         const char *filename,                           \
59                         unsigned line,                                  \
60                         const char *section,                            \
61                         const char *lvalue,                             \
62                         const char *rvalue,                             \
63                         void *data,                                     \
64                         void *userdata) {                               \
65                                                                         \
66                 type *i = data, x;                                      \
67                                                                         \
68                 assert(filename);                                       \
69                 assert(lvalue);                                         \
70                 assert(rvalue);                                         \
71                 assert(data);                                           \
72                                                                         \
73                 if ((x = name##_from_string(rvalue)) < 0) {             \
74                         log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \
75                         return -EBADMSG;                                \
76                 }                                                       \
77                                                                         \
78                 *i = x;                                                 \
79                                                                         \
80                 return 0;                                               \
81         }
82
83
84 #endif