1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29 #ifndef WS_CONFIG_SERVERCONFIG_H
30 #define WS_CONFIG_SERVERCONFIG_H
31
32 #include <cx/linked_list.h>
33 #include <cx/hash_map.h>
34 #include <cx/mempool.h>
35 #include <cx/string.h>
36
37 #include <stdbool.h>
38
39 #include "conf.h"
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 #define CFG_NODE_ADD(list_begin, list_end, elm) \
46 cx_linked_list_add((
void**)list_begin, (
void**)list_end, offsetof(ConfigNode, prev), offsetof(ConfigNode, next), elm)
47
48 typedef struct ServerConfig ServerConfig;
49 typedef struct ConfigNode ConfigNode;
50 typedef struct CFGToken CFGToken;
51 typedef struct ConfigParser2 ConfigParser2;
52
53 typedef enum ConfigNodeType ConfigNodeType;
54 typedef enum CFGTokenType CFGTokenType;
55
56 struct ServerConfig {
57 CxMempool *mp;
58 ConfigNode *root;
59 cxstring tab;
60 };
61
62 enum ConfigNodeType {
63 CONFIG_NODE_SPACE =
0,
64 CONFIG_NODE_COMMENT,
65 CONFIG_NODE_OBJECT,
66 CONFIG_NODE_DIRECTIVE,
67 CONFIG_NODE_OPEN_OBJECT,
68 CONFIG_NODE_CLOSE_OBJECT
69 };
70
71 struct ConfigNode {
72 cxmutstr text_begin;
73 cxmutstr text_end;
74 ConfigNodeType type;
75
76 cxmutstr name;
77 ConfigParam *args;
78 ConfigNode *children_begin;
79 ConfigNode *children_end;
80
81 ConfigNode *prev;
82 ConfigNode *next;
83 };
84
85 typedef struct ConfigNodeStack ConfigNodeStack;
86 struct ConfigNodeStack {
87 ConfigNode *node;
88 ConfigNodeStack *next;
89 };
90
91 enum CFGTokenType {
92 CFG_NO_TOKEN =
0,
93 CFG_TOKEN_COMMENT,
94 CFG_TOKEN_SPACE,
95 CFG_TOKEN_NEWLINE,
96 CFG_TOKEN
97 };
98
99 struct CFGToken {
100 cxstring content;
101 CFGTokenType type;
102 };
103
104 enum ConfigParserError {
105 CONFIG_PARSER_OOM =
0,
106 CONFIG_PARSER_IO_ERROR,
107 CONFIG_PARSER_SYNTAX_ERROR
108 };
109
110
111
112
113
114
115
116
117 typedef int(*CfgValidateNodeFunc)(ConfigParser2 *parser, ConfigNode *node);
118
119 struct ConfigParser2 {
120 CxMempool *mp;
121
122 const char *filename;
123
124 int linenum;
125 int linepos;
126
127 enum ConfigParserError error;
128
129 const char *delim;
130
131
132
133
134 int io_errno;
135
136
137
138
139 CfgValidateNodeFunc validateObjBegin;
140
141
142
143
144 CfgValidateNodeFunc validateObjEnd;
145
146
147
148
149 CfgValidateNodeFunc validateDirective;
150
151
152
153
154
155 bool allow_hierarchy;
156 };
157
158 ServerConfig* serverconfig_load(
const char *file);
159
160 ConfigNode* serverconfig_load_file(ConfigParser2 *parser,
const char *file);
161
162 ConfigNode* serverconfig_parse(ConfigParser2 *parser, cxstring content);
163
164 void serverconfig_free(ServerConfig *cfg);
165
166 ConfigNode* serverconfig_get_node(ConfigNode *parent, ConfigNodeType type, cxstring name);
167
168 CxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, cxstring name);
169
170
171
172
173
174
175
176 cxstring serverconfig_object_directive_value(ConfigNode *obj, cxstring directive_name);
177
178
179
180
181 cxstring serverconfig_directive_get_arg(ConfigNode *directive, cxstring param_name);
182
183
184
185
186
187
188
189
190 int serverconfig_validate_directive_name(
191 ConfigNode *directive,
192 const char *names[],
193 size_t numnames,
194 size_t *nameindex);
195
196
197
198
199
200
201
202
203
204
205 int serverconfig_check_param_names(ConfigNode *directive, ConfigParam **err);
206
207
208
209
210 ConfigNode* serverconfig_previous_dir_or_obj(ConfigNode *node);
211
212
213
214
215 size_t serverconfig_children_count(ConfigNode *node, ConfigNodeType type);
216
217 #ifdef __cplusplus
218 }
219 #endif
220
221 #endif
222
223