src/server/config/serverconfig.c

Sun, 06 Nov 2022 16:59:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 06 Nov 2022 16:59:39 +0100
changeset 416
e2093ca0ef90
parent 415
d938228c382e
child 417
90805bb9fbd6
permissions
-rw-r--r--

update ucx hash functions

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>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
38 #include <cx/buffer.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
39 #include <cx/utils.h>
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 ServerConfig* serverconfig_load(const char *file) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 FILE *in = fopen(file, "r");
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 if(in == NULL) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
47 CxBuffer buf;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
48 cxBufferInit(&buf, NULL, 16384, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
49
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
50 //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
51 char readbuf[2048];
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
52 size_t r;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
53 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
54 cxBufferWrite(readbuf, 1, r, &buf);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 fclose(in);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
58 ServerConfig *scfg = serverconfig_parse(cx_strn(buf.space, buf.size));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
60 cxBufferDestroy(&buf);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return scfg;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
65 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
66 CFGToken token = { {NULL, 0}, CFG_NO_TOKEN };
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 CFGTokenType type = CFG_TOKEN;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 int start = *pos;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 int token_begin = -1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 int token_end = content.length-1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 int quote = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 int comment = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 int i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 char prev = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 for(i=start;i<content.length;i++) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 char c = content.ptr[i];
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 *pos = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return token; // error
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 } else if(start == i) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 // single newline char token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 type = CFG_TOKEN_NEWLINE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 token_end = i+1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 break;
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 // only space/comment token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 token_begin = start;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 type = comment ? CFG_TOKEN_COMMENT : CFG_TOKEN_SPACE;
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 // 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
100 i--;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 } else if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 if(c == '"' && prev != '\\') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 quote = 0;
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 } else if(comment) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 // ignore
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 comment = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 } else if(c == '#') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 comment = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 } else if(isspace(c)) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 if(token_begin >= 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 } else if(c == '"') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 quote = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 } else if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 prev = c;
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 *pos = i + 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 return token; // error
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 token.type = type;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
136 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
137 return token;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
140 /*
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 static void test_print_config(ConfigNode *parent) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 UCX_FOREACH(elm, parent->children) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 ConfigNode *node = elm->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 if(node->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 printf("sp: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 } else if(node->type == CONFIG_NODE_COMMENT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 printf("cm: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 } else if(node->type == CONFIG_NODE_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 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
151 test_print_config(node);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 printf("o}: %s", node->text_end.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 } else if(node->type == CONFIG_NODE_DIRECTIVE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 printf("di: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 printf("fk: %s", node->text_begin.ptr);
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 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 }
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
160 */
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
162 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
163 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
164 if(!nv.ptr) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
165 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
166 } else {
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
167 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
168 cxstring name = token.content;
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
169 name.length = (size_t)eq;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
170
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
171 cxstring value = nv;
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
172 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
173 value.length--;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
174 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
175 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
176 value.length -= 2; // remove quote
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
177 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
178
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
179 arg->name = cx_strdup_a(a, name);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
180 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
181 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
182 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
183
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
184 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
185 ConfigNodeStack *elm = cxMalloc(a, sizeof(ConfigNodeStack));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
186 if(!elm) return 1;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
187 elm->node = node;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
188 elm->next = NULL;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
189 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
190 return 0;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
191 }
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
192
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
193 ServerConfig* serverconfig_parse(cxstring content) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
194 CxMempool *mp = cxBasicMempoolCreate(512);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 if(!mp) return NULL;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
196 CxAllocator *a = (CxAllocator*)mp->allocator;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
198 ServerConfig *config = cxMalloc(a, sizeof(ServerConfig));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 if(!config) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
200 cxMempoolDestroy(mp);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 config->mp = mp;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 // PARSE:
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 // first non space/comment token is directive/object name
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 // following tokens are arguments
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 // newline starts new directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 // '{' converts directive to object and following directives will
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 // be placed into the object
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 int pos = 0; // needed for tokenizer
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 CFGToken token;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
214 ConfigNode *root_obj = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 root_obj->type = CONFIG_NODE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
217 ConfigNodeStack *node_stack = cxMalloc(a, sizeof(ConfigNodeStack));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
218 node_stack->node = root_obj;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
219 node_stack->next = NULL;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
221 ConfigNode *current = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 ConfigNode *obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 int obj_closed = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 int text_start = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 int err = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 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
229 //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
230
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 switch(token.type) {
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
232 case CFG_NO_TOKEN: break;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 case CFG_TOKEN_COMMENT: {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 if(current->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 current->type = CONFIG_NODE_COMMENT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 case CFG_TOKEN_SPACE: break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 case CFG_TOKEN_NEWLINE: {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
241 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
242 text_start = pos;
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 cxmutstr line_cp = cx_strdup_a(a, line);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
246 ConfigNode *parent = node_stack->node;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 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
248 // 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
249 // 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
250 parent->text_end = line_cp;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
251 // 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
252 ConfigNodeStack *remove_item = node_stack;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
253 node_stack = node_stack->next;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
254 cxFree(a, remove_item);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 } 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
256 // newline after a object is opened with '{'
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
257 // append '{' to the object text
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
258 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
259 cxFree(a, obj->text_begin.ptr);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
260 cxFree(a, line_cp.ptr);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 obj->text_begin = new_textbegin;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
263 // normal line containing a directive, space or comment
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
264 // add it to parent node
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 current->text_begin = line_cp;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
266 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
267 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
269 // 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
270 // 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
271 // a '{' character
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 if(obj && obj->type == CONFIG_NODE_OBJECT) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
273 // new object started, add it to the stack
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
274 nodestack_prepend(a, &node_stack, obj);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
278 current = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 obj_closed = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 case CFG_TOKEN: {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
285 // normal text token
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
286 // either a directive/obj name, parameter or { }
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
287
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
288 if(!cx_strcmp(token.content, cx_str("{"))) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
289 // 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
290 // a directive
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 if(!obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 obj->type = CONFIG_NODE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 if(current != obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 current->type = CONFIG_NODE_OPEN_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
299 } else if(!cx_strcmp(token.content, cx_str("}"))) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 obj_closed = 1; // force newline before next directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 current->type = CONFIG_NODE_CLOSE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 if(obj_closed) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 break;
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 if(!current->name.ptr) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
310 // 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
311 current->name = cx_strdup_a(a, token.content);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 current->type = CONFIG_NODE_DIRECTIVE;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
313 obj = current; // potential object
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
315 // name already set, therefore this token must
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
316 // be a parameter
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
317 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
318 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
319 CFG_PARAM_ADD(&current->args, NULL, arg);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 break;
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 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 if(err) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 }
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 if(pos < content.length || err) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 // 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
333 cxMempoolDestroy(mp);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 //test_print_config(&root_obj);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 config->root = root_obj;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
339 config->tab = cx_strdup_a(a, cx_str("\t"));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 return config;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 void serverconfig_free(ServerConfig *cfg) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
345 cxMempoolDestroy(cfg->mp);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
348 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
349 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
350 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
351 return node;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
357 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
358 CxList *nodes = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_ptr);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
360 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
361 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
362 cxListAdd(nodes, node);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 return nodes;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
369 cxstring serverconfig_directive_value(ConfigNode *obj, cxstring name) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 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
371 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
372 ConfigParam *arg = node->args;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
373 return (cxstring){ arg->value.ptr, arg->value.length };
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
375 return (cxstring){ NULL, 0 };
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 }
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
377
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
378 cxmutstr serverconfig_arg_name_value(CxAllocator *a, cxstring str, cxstring *name) {
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
379 int valstart = 0;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
380 for(int i=0;i<str.length;i++) {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
381 if(str.ptr[i] == '=') {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
382 if(name) {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
383 name->ptr = str.ptr;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
384 name->length = i;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
385 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
386 valstart = i + 1;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
387 break;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
388 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
389 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
390
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
391 cxmutstr ret;
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
392 return ret;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
393 }

mercurial