]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/conf-parser.h
util: rename parse_usec() to parse_sec() sinds the default unit is seconds
[thirdparty/systemd.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 /* 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)(
32 const char *filename,
33 unsigned line,
34 const char *section,
35 const char *lvalue,
36 int ltype,
37 const char *rvalue,
38 void *data,
39 void *userdata);
40
41 /* Wraps information for parsing a specific configuration variable, to
42 * be stored in a simple array */
43 typedef struct ConfigTableItem {
44 const char *section; /* Section */
45 const char *lvalue; /* Name of the variable */
46 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
47 int ltype; /* Distinguish different variables passed to the same callback */
48 void *data; /* Where to store the variable's data */
49 } ConfigTableItem;
50
51 /* Wraps information for parsing a specific configuration variable, to
52 * ve srored in a gperf perfect hashtable */
53 typedef struct ConfigPerfItem {
54 const char *section_and_lvalue; /* Section + "." + name of the variable */
55 ConfigParserCallback parse; /* Function that is called to parse the variable's value */
56 int ltype; /* Distinguish different variables passed to the same callback */
57 size_t offset; /* Offset where to store data, from the beginning of userdata */
58 } ConfigPerfItem;
59
60 /* Prototype for a low-level gperf lookup function */
61 typedef const ConfigPerfItem* (*ConfigPerfItemLookup)(const char *section_and_lvalue, unsigned length);
62
63 /* Prototype for a generic high-level lookup function */
64 typedef int (*ConfigItemLookup)(
65 void *table,
66 const char *section,
67 const char *lvalue,
68 ConfigParserCallback *func,
69 int *ltype,
70 void **data,
71 void *userdata);
72
73 /* Linear table search implementation of ConfigItemLookup, based on
74 * ConfigTableItem arrays */
75 int config_item_table_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
76
77 /* gperf implementation of ConfigItemLookup, based on gperf
78 * ConfigPerfItem tables */
79 int config_item_perf_lookup(void *table, const char *section, const char *lvalue, ConfigParserCallback *func, int *ltype, void **data, void *userdata);
80
81 int config_parse(
82 const char *filename,
83 FILE *f,
84 const char *sections, /* nulstr */
85 ConfigItemLookup lookup,
86 void *table,
87 bool relaxed,
88 void *userdata);
89
90 /* Generic parsers */
91 int config_parse_int(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
92 int config_parse_unsigned(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
93 int config_parse_long(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
94 int config_parse_uint64(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
95 int config_parse_double(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_sec(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
105 int config_parse_nsec(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
106 int config_parse_mode(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
107 int config_parse_facility(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
108 int config_parse_level(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
109 int config_parse_set_status(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata);
110
111 #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \
112 int function( \
113 const char *filename, \
114 unsigned line, \
115 const char *section, \
116 const char *lvalue, \
117 int ltype, \
118 const char *rvalue, \
119 void *data, \
120 void *userdata) { \
121 \
122 type *i = data, x; \
123 \
124 assert(filename); \
125 assert(lvalue); \
126 assert(rvalue); \
127 assert(data); \
128 \
129 if ((x = name##_from_string(rvalue)) < 0) { \
130 log_error("[%s:%u] " msg ", ignoring: %s", filename, line, rvalue); \
131 return 0; \
132 } \
133 \
134 *i = x; \
135 \
136 return 0; \
137 }