chiark / gitweb /
util: unify getenv() logic for other PID
[elogind.git] / src / shared / conf-parser.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
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 Lesser General Public License as published by
13   the Free Software Foundation; either version 2.1 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   Lesser General Public License for more details.
20
21   You should have received a copy of the GNU Lesser 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 /* Prototype for a parser for a specific configuration setting */
32 typedef int (*ConfigParserCallback)(
33                 const char *filename,
34                 unsigned line,
35                 const char *section,
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  * 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 */
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                 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(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(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
81
82 int config_parse(
83                 const char *filename,
84                 FILE *f,
85                 const char *sections,  /* nulstr */
86                 ConfigItemLookup lookup,
87                 void *table,
88                 bool relaxed,
89                 void *userdata);
90
91 /* Generic parsers */
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_bytes_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_bytes_off(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
98 int config_parse_bool(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
99 int config_parse_tristate(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
100 int config_parse_string(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(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
102 int config_parse_strv(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
103 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);
104 int config_parse_usec(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105 int config_parse_mode(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
106
107 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg)                \
108         int function(                                                   \
109                         const char *filename,                           \
110                         unsigned line,                                  \
111                         const char *section,                            \
112                         const char *lvalue,                             \
113                         int ltype,                                      \
114                         const char *rvalue,                             \
115                         void *data,                                     \
116                         void *userdata) {                               \
117                                                                         \
118                 type *i = data, x;                                      \
119                                                                         \
120                 assert(filename);                                       \
121                 assert(lvalue);                                         \
122                 assert(rvalue);                                         \
123                 assert(data);                                           \
124                                                                         \
125                 if ((x = name##_from_string(rvalue)) < 0) {             \
126                         log_error("[%s:%u] " msg ", ignoring: %s", filename, line, rvalue); \
127                         return 0;                                       \
128                 }                                                       \
129                                                                         \
130                 *i = x;                                                 \
131                                                                         \
132                 return 0;                                               \
133         }
134
135 #endif