1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
3 #ifndef fooconfparserhfoo
4 #define fooconfparserhfoo
7 This file is part of systemd.
9 Copyright 2010 Lennart Poettering
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.
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.
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/>.
28 /* An abstract parser for simple, line based, shallow configuration
29 * files consisting of variable assignments only. */
31 /* Prototype for a parser for a specific configuration setting */
32 typedef int (*ConfigParserCallback)(
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 */
52 /* Wraps information for parsing a specific configuration variable, to
53 * ve srored 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 */
61 /* Prototype for a low-level gperf lookup function */
62 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
64 /* Prototype for a generic high-level lookup function */
65 typedef int (*ConfigItemLookup)(
69 ConfigParserCallback *func,
74 /* Linear table search implementation of ConfigItemLookup, based on
75 * ConfigTableItem arrays */
76 int config_item_table_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
78 /* gperf implementation of ConfigItemLookup, based on gperf
79 * ConfigPerfItem tables */
80 int config_item_perf_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
85 const char *sections, /* nulstr */
86 ConfigItemLookup lookup,
92 int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
93 int config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
94 int config_parse_long(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
95 int config_parse_uint64(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
96 int config_parse_size(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
97 int config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
98 int config_parse_string(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
99 int config_parse_path(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
100 int config_parse_strv(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
101 int config_parse_path_strv(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
102 int config_parse_usec(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
103 int config_parse_mode(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
107 const char *filename, \
109 const char *section, \
110 const char *lvalue, \
112 const char *rvalue, \
123 if ((x = name##_from_string(rvalue)) < 0) { \
124 log_error("[%s:%u] " msg ", ignoring: %s", filename, line, rvalue); \