Mon, 07 Nov 2022 17:59:44 +0100
use server.conf parser for init.conf as well
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
1 | /* |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
3 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
4 | * Copyright 2020 Olaf Wintermann. All rights reserved. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
5 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
8 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
11 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
15 | * |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
27 | */ |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
28 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
29 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
30 | #include "serverconfig.h" |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
31 | #include "conf.h" |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
32 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
33 | #include <stdio.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
34 | #include <stdlib.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
35 | #include <string.h> |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
36 | #include <ctype.h> |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
37 | #include <errno.h> |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
38 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
39 | #include <cx/buffer.h> |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
40 | #include <cx/utils.h> |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
41 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
42 | ServerConfig* serverconfig_load(const char *file) { |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
43 | CxMempool *mp = cxBasicMempoolCreate(512); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
44 | if(!mp) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
45 | return NULL; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
46 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
47 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
48 | ConfigParser2 parser; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
49 | memset(&parser, 0, sizeof(ConfigParser2)); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
50 | parser.mp = mp; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
51 | parser.allow_hierarchy = true; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
52 | ConfigNode *root = serverconfig_load_file(&parser, file); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
53 | if(!root) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
54 | cxMempoolDestroy(mp); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
55 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
56 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
57 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
58 | ServerConfig *scfg = cxMalloc(mp->allocator, sizeof(ServerConfig)); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
59 | if(!scfg) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
60 | cxMempoolDestroy(mp); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
61 | return NULL; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
62 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
63 | scfg->root = root; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
64 | scfg->mp = mp; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
65 | scfg->tab = cx_str("\t"); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
66 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
67 | return scfg; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
68 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
69 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
70 | ConfigNode* serverconfig_load_file(ConfigParser2 *parser, const char *file) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
71 | FILE *in = fopen(file, "r"); |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
72 | if(in == NULL) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
73 | parser->error = CONFIG_PARSER_IO_ERROR; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
74 | parser->io_errno = errno; |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
75 | return NULL; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
76 | } |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
77 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
78 | // temporary buffer to store the file content |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
79 | CxBuffer buf; |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
80 | if(cxBufferInit(&buf, NULL, 16384, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) { |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
81 | fclose(in); |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
82 | parser->error = CONFIG_PARSER_OOM; |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
83 | return NULL; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
84 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
85 | |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
86 | //ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
87 | char readbuf[2048]; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
88 | size_t r; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
89 | while((r = fread(readbuf, 1, 2048, in)) > 0) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
90 | cxBufferWrite(readbuf, 1, r, &buf); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
91 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
92 | fclose(in); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
93 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
94 | ConfigNode *root = serverconfig_parse(parser, cx_strn(buf.space, buf.size)); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
95 | cxBufferDestroy(&buf); |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
96 | return root; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
97 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
98 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
99 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
100 | static CFGToken get_next_token(cxstring content, int *pos) { |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
101 | CFGToken token = { {NULL, 0}, CFG_NO_TOKEN }; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
102 | CFGTokenType type = CFG_TOKEN; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
103 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
104 | int start = *pos; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
105 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
106 | int token_begin = -1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
107 | int token_end = content.length-1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
108 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
109 | int quote = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
110 | int comment = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
111 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
112 | int i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
113 | char prev = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
114 | for(i=start;i<content.length;i++) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
115 | char c = content.ptr[i]; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
116 | if(c == '\n') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
117 | if(quote) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
118 | *pos = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
119 | return token; // error |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
120 | } else if(start == i) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
121 | // single newline char token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
122 | type = CFG_TOKEN_NEWLINE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
123 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
124 | token_end = i+1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
125 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
126 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
127 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
128 | token_end = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
129 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
130 | // only space/comment token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
131 | token_begin = start; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
132 | type = comment ? CFG_TOKEN_COMMENT : CFG_TOKEN_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
133 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
134 | // make sure next run will return current newline char as token |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
135 | i--; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
136 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
137 | } else if(quote) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
138 | if(c == '"' && prev != '\\') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
139 | quote = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
140 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
141 | } else if(comment) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
142 | // ignore |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
143 | if(c == '\n') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
144 | comment = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
145 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
146 | } else if(c == '#') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
147 | comment = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
148 | } else if(isspace(c)) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
149 | if(token_begin >= 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
150 | token_end = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
151 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
152 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
153 | } else if(c == '"') { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
154 | quote = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
155 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
156 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
157 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
158 | } else if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
159 | token_begin = i; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
160 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
161 | prev = c; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
162 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
163 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
164 | *pos = i + 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
165 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
166 | if(token_begin < 0) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
167 | return token; // error |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
168 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
169 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
170 | token.type = type; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
171 | token.content = cx_strsubsl(content, token_begin, token_end - token_begin); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
172 | return token; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
173 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
174 | |
258
134279e804b6
remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
175 | /* |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
176 | static void test_print_config(ConfigNode *parent) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
177 | UCX_FOREACH(elm, parent->children) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
178 | ConfigNode *node = elm->data; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
179 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
180 | if(node->type == CONFIG_NODE_SPACE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
181 | printf("sp: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
182 | } else if(node->type == CONFIG_NODE_COMMENT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
183 | printf("cm: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
184 | } else if(node->type == CONFIG_NODE_OBJECT) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
185 | printf("o{: %s : %s", node->name.ptr, node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
186 | test_print_config(node); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
187 | printf("o}: %s", node->text_end.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
188 | } else if(node->type == CONFIG_NODE_DIRECTIVE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
189 | printf("di: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
190 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
191 | printf("fk: %s", node->text_begin.ptr); |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
192 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
193 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
194 | } |
258
134279e804b6
remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
256
diff
changeset
|
195 | */ |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
196 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
197 | static void config_arg_set_value(CxAllocator *a, ConfigParam *arg, CFGToken token) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
198 | cxstring nv = cx_strchr(token.content, '='); |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
199 | if(!nv.ptr) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
200 | arg->value = cx_strdup_a(a, token.content); |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
201 | } else { |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
202 | intptr_t eq = (intptr_t)(nv.ptr - token.content.ptr); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
203 | cxstring name = token.content; |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
204 | name.length = (size_t)eq; |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
205 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
206 | cxstring value = nv; |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
207 | value.ptr++; |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
208 | value.length--; |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
209 | if(value.length > 1 && value.ptr[0] == '"' && value.ptr[value.length-1] == '"') { |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
210 | value.ptr++; |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
211 | value.length -= 2; // remove quote |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
212 | } |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
213 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
214 | arg->name = cx_strdup_a(a, name); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
215 | arg->value = cx_strdup_a(a, value); |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
216 | } |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
217 | } |
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
218 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
219 | static int nodestack_prepend(CxAllocator *a, ConfigNodeStack **stack, ConfigNode *node) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
220 | ConfigNodeStack *elm = cxMalloc(a, sizeof(ConfigNodeStack)); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
221 | if(!elm) return 1; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
222 | elm->node = node; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
223 | elm->next = NULL; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
224 | cx_linked_list_prepend((void**)stack, NULL, -1, offsetof(ConfigNodeStack, next), elm); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
225 | return 0; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
226 | } |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
227 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
228 | ConfigNode* serverconfig_parse(ConfigParser2 *parser, cxstring content) { |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
229 | CxMempool *mp = parser->mp; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
230 | CxAllocator *a = (CxAllocator*)mp->allocator; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
231 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
232 | // PARSE: |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
233 | // first non space/comment token is directive/object name |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
234 | // following tokens are arguments |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
235 | // newline starts new directive |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
236 | // '{' converts directive to object and following directives will |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
237 | // be placed into the object |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
238 | int pos = 0; // needed for tokenizer |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
239 | CFGToken token; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
240 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
241 | ConfigNode *root_obj = cxCalloc(a, 1, sizeof(ConfigNode)); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
242 | root_obj->type = CONFIG_NODE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
243 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
244 | ConfigNodeStack *node_stack = cxMalloc(a, sizeof(ConfigNodeStack)); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
245 | node_stack->node = root_obj; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
246 | node_stack->next = NULL; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
247 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
248 | ConfigNode *current = cxCalloc(a, 1, sizeof(ConfigNode)); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
249 | current->type = CONFIG_NODE_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
250 | ConfigNode *obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
251 | int obj_closed = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
252 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
253 | int text_start = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
254 | int err = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
255 | while((token = get_next_token(content, &pos)).type != CFG_NO_TOKEN) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
256 | //printf("%s [%.*s]\n", token_type_str(token.type), (int)token.content.length, token.content.ptr); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
257 | |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
258 | switch(token.type) { |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
259 | case CFG_NO_TOKEN: break; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
260 | case CFG_TOKEN_COMMENT: { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
261 | if(current->type == CONFIG_NODE_SPACE) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
262 | current->type = CONFIG_NODE_COMMENT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
263 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
264 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
265 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
266 | case CFG_TOKEN_SPACE: break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
267 | case CFG_TOKEN_NEWLINE: { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
268 | cxstring line = cx_strsubsl(content, text_start, pos - text_start); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
269 | text_start = pos; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
270 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
271 | cxmutstr line_cp = cx_strdup_a(a, line); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
272 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
273 | ConfigNode *parent = node_stack->node; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
274 | if(current->type == CONFIG_NODE_CLOSE_OBJECT) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
275 | // this is a newline after a object is closed with '}' |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
276 | // the line containing "}\n" should be added to the object |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
277 | parent->text_end = line_cp; |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
278 | // validate |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
279 | if(parser->validateObjEnd && parser->validateObjEnd(parser, parent)) { |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
280 | // validation failed |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
281 | err = 1; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
282 | break; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
283 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
284 | // done with this object, remove it from the stack |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
285 | ConfigNodeStack *remove_item = node_stack; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
286 | node_stack = node_stack->next; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
287 | cxFree(a, remove_item); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
288 | } else if(current->type == CONFIG_NODE_OPEN_OBJECT) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
289 | // newline after a object is opened with '{' |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
290 | // append '{' to the object text |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
291 | cxmutstr new_textbegin = cx_strcat_a(a, 2, obj->text_begin, line_cp); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
292 | cxFree(a, obj->text_begin.ptr); |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
293 | cxFree(a, line_cp.ptr); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
294 | obj->text_begin = new_textbegin; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
295 | } else { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
296 | // normal line containing a directive, space or comment |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
297 | if(current->type == CONFIG_NODE_DIRECTIVE && |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
298 | parser->validateDirective && |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
299 | parser->validateDirective(parser, current)) |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
300 | { |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
301 | err = 1; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
302 | break; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
303 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
304 | // add it to parent node |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
305 | current->text_begin = line_cp; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
306 | CFG_NODE_ADD(&parent->children_begin, &parent->children_end, current); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
307 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
308 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
309 | // obj points to the previous node that started as a directive |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
310 | // the type is set to CONFIG_NODE_OBECT if it was followed by |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
311 | // a '{' character |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
312 | if(obj && obj->type == CONFIG_NODE_OBJECT) { |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
313 | // new object started |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
314 | if(parser->validateObjBegin && parser->validateObjBegin(parser, obj)) { |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
315 | // validation callback failed |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
316 | err = 1; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
317 | break; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
318 | } |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
319 | |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
320 | // add it to the stack |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
321 | nodestack_prepend(a, &node_stack, obj); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
322 | obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
323 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
324 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
325 | current = cxCalloc(a, 1, sizeof(ConfigNode)); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
326 | current->type = CONFIG_NODE_SPACE; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
327 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
328 | obj_closed = 0; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
329 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
330 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
331 | case CFG_TOKEN: { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
332 | // normal text token |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
333 | // either a directive/obj name, parameter or { } |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
334 | |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
335 | if(!cx_strcmp(token.content, cx_str("{"))) { |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
336 | // check if the parser allows an object hierarchy |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
337 | if(!parser->allow_hierarchy) { |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
338 | parser->error = CONFIG_PARSER_SYNTAX_ERROR; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
339 | err = 1; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
340 | break; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
341 | } |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
342 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
343 | // obj is pointing to the previous node that started |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
344 | // a directive |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
345 | if(!obj) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
346 | err = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
347 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
348 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
349 | obj->type = CONFIG_NODE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
350 | if(current != obj) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
351 | current->type = CONFIG_NODE_OPEN_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
352 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
353 | } else if(!cx_strcmp(token.content, cx_str("}"))) { |
417
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
354 | // check if the parser allows an object hierarchy |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
355 | if(!parser->allow_hierarchy) { |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
356 | parser->error = CONFIG_PARSER_SYNTAX_ERROR; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
357 | err = 1; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
358 | break; |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
359 | } |
90805bb9fbd6
prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
415
diff
changeset
|
360 | |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
361 | obj_closed = 1; // force newline before next directive |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
362 | obj = NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
363 | current->type = CONFIG_NODE_CLOSE_OBJECT; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
364 | } else { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
365 | if(obj_closed) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
366 | err = 1; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
367 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
368 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
369 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
370 | if(!current->name.ptr) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
371 | // currently this could be a directive or object |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
372 | current->name = cx_strdup_a(a, token.content); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
373 | current->type = CONFIG_NODE_DIRECTIVE; |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
374 | obj = current; // potential object |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
375 | } else { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
376 | // name already set, therefore this token must |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
377 | // be a parameter |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
378 | ConfigParam *arg = cxCalloc(a, 1, sizeof(ConfigParam)); |
367
1592224f6059
add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
366
diff
changeset
|
379 | config_arg_set_value(a, arg, token); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
380 | CFG_PARAM_ADD(¤t->args, NULL, arg); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
381 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
382 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
383 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
384 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
385 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
386 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
387 | if(err) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
388 | break; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
389 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
390 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
391 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
392 | if(pos < content.length || err) { |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
393 | // content not fully parsed because of an error |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
394 | cxMempoolDestroy(mp); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
395 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
396 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
397 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
398 | //test_print_config(&root_obj); |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
399 | return root_obj; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
400 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
401 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
402 | void serverconfig_free(ServerConfig *cfg) { |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
403 | cxMempoolDestroy(cfg->mp); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
404 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
405 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
406 | ConfigNode* serverconfig_get_node(ConfigNode *parent, ConfigNodeType type, cxstring name) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
407 | for(ConfigNode *node=parent->children_begin;node;node=node->next) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
408 | if(node->type == type && !cx_strcasecmp(cx_strcast(node->name), name)) { |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
409 | return node; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
410 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
411 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
412 | return NULL; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
413 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
414 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
415 | CxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, cxstring name) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
416 | CxList *nodes = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_ptr); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
417 | |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
418 | for(ConfigNode *node=parent->children_begin;node;node=node->next) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
419 | if(node->type == type && !cx_strcasecmp(cx_strcast(node->name), name)) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
420 | cxListAdd(nodes, node); |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
421 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
422 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
423 | |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
424 | return nodes; |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
425 | } |
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
426 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
427 | cxstring serverconfig_object_directive_value(ConfigNode *obj, cxstring name) { |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
428 | ConfigNode *node = serverconfig_get_node(obj, CONFIG_NODE_DIRECTIVE, name); |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
429 | if(node && CFG_NUM_PARAMS(node->args) == 1) { |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
430 | ConfigParam *arg = node->args; |
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
431 | return (cxstring){ arg->value.ptr, arg->value.length }; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
432 | } |
415
d938228c382e
switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
367
diff
changeset
|
433 | return (cxstring){ NULL, 0 }; |
256
19259b6c5cf7
replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff
changeset
|
434 | } |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
435 | |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
436 | cxstring serverconfig_directive_get_arg(ConfigNode *directive, cxstring arg_name) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
437 | cxstring ret = (cxstring){ NULL, 0 }; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
438 | ConfigParam *arg = directive->args; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
439 | while(arg) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
440 | if(!cx_strcmp(arg_name, cx_strcast(arg->name))) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
441 | ret = cx_strcast(arg->value); |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
442 | break; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
443 | } |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
444 | arg = arg->next; |
269
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
445 | } |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
446 | |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
447 | return ret; |
3dfbd0b91950
add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
258
diff
changeset
|
448 | } |
418
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
449 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
450 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
451 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
452 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
453 | /* -------------------------- utility functions -------------------------- */ |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
454 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
455 | int serverconfig_validate_directive_name( |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
456 | ConfigNode *directive, |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
457 | const char *names[], |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
458 | size_t numnames, |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
459 | size_t *nameindex) |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
460 | { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
461 | for(size_t i=0;i<numnames;i++) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
462 | if(!cx_strcmp(cx_strcast(directive->name), cx_str(names[i]))) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
463 | *nameindex = i; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
464 | return 0; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
465 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
466 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
467 | return 1; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
468 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
469 | |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
470 | int serverconfig_check_param_names(ConfigNode *directive, ConfigParam **err) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
471 | ConfigParam *arg = directive->args; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
472 | while(arg) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
473 | if(arg->name.length == 0) { |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
474 | *err = arg; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
475 | return 1; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
476 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
477 | arg = arg->next; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
478 | } |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
479 | return 0; |
b7dcc9c4f270
use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
417
diff
changeset
|
480 | } |