]>
Commit | Line | Data |
---|---|---|
03467c88 | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
ed5bcfbe LP |
2 | |
3 | #ifndef fooconfparserhfoo | |
4 | #define fooconfparserhfoo | |
5 | ||
a7334b09 LP |
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 | ||
ed5bcfbe | 25 | #include <stdio.h> |
10e87ee7 | 26 | #include <stdbool.h> |
ed5bcfbe LP |
27 | |
28 | /* An abstract parser for simple, line based, shallow configuration | |
29 | * files consisting of variable assignments only. */ | |
30 | ||
f975e971 LP |
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); | |
ed5bcfbe LP |
90 | |
91 | /* Generic parsers */ | |
2b583ce6 KS |
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); | |
916abb21 | 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); |
2b583ce6 KS |
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); | |
f975e971 LP |
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); | |
e6960940 | 104 | int config_parse_bytes(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata); |
ed5bcfbe | 105 | |
487393e9 LP |
106 | #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \ |
107 | int function( \ | |
108 | const char *filename, \ | |
109 | unsigned line, \ | |
110 | const char *section, \ | |
111 | const char *lvalue, \ | |
2b583ce6 | 112 | int ltype, \ |
487393e9 LP |
113 | const char *rvalue, \ |
114 | void *data, \ | |
115 | void *userdata) { \ | |
116 | \ | |
117 | type *i = data, x; \ | |
118 | \ | |
119 | assert(filename); \ | |
120 | assert(lvalue); \ | |
121 | assert(rvalue); \ | |
122 | assert(data); \ | |
123 | \ | |
124 | if ((x = name##_from_string(rvalue)) < 0) { \ | |
c0b34696 LP |
125 | log_error("[%s:%u] " msg ", ignoring: %s", filename, line, rvalue); \ |
126 | return 0; \ | |
487393e9 LP |
127 | } \ |
128 | \ | |
129 | *i = x; \ | |
130 | \ | |
131 | return 0; \ | |
132 | } | |
133 | ||
ed5bcfbe | 134 | #endif |