src/server/config/serverconfig.c

Sun, 25 Sep 2022 10:36:28 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 25 Sep 2022 10:36:28 +0200
changeset 388
30d29ef5b79a
parent 367
1592224f6059
child 415
d938228c382e
permissions
-rw-r--r--

change uid before most of the config is loaded

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"
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdio.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <stdlib.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <string.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <ctype.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <ucx/buffer.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <ucx/utils.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 ServerConfig* serverconfig_load(const char *file) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 FILE *in = fopen(file, "r");
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 if(in == NULL) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 }
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 UcxBuffer *buf = ucx_buffer_new(NULL, 4096, UCX_BUFFER_AUTOEXTEND);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 if(!buf) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 fclose(in);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 ucx_stream_copy(in, buf, (read_func)fread, (write_func)ucx_buffer_write);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 fclose(in);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 ServerConfig *scfg = serverconfig_parse(scstrn(buf->space, buf->size));
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 ucx_buffer_free(buf);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 return scfg;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 static CFGToken get_next_token(scstr_t content, int *pos) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 CFGToken token = { {NULL, 0}, CFG_NO_TOKEN };
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 CFGTokenType type = CFG_TOKEN;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 int start = *pos;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 int token_begin = -1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 int token_end = content.length-1;
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 quote = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 int comment = 0;
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 i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 char prev = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 for(i=start;i<content.length;i++) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 char c = content.ptr[i];
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 *pos = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 return token; // error
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 } else if(start == i) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 // single newline char token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 type = CFG_TOKEN_NEWLINE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86 token_end = i+1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 // only space/comment token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 token_begin = start;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 type = comment ? CFG_TOKEN_COMMENT : CFG_TOKEN_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 // 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
97 i--;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 } else if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 if(c == '"' && prev != '\\') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 quote = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 } else if(comment) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 // ignore
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 comment = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 } else if(c == '#') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 comment = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 } else if(isspace(c)) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 if(token_begin >= 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 } else if(c == '"') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 quote = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 } else 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 prev = c;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 }
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 *pos = i + 1;
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 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 return token; // error
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 token.type = type;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 token.content = scstrsubsl(content, token_begin, token_end - token_begin);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 return token;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
137 /*
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 static void test_print_config(ConfigNode *parent) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 UCX_FOREACH(elm, parent->children) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 ConfigNode *node = elm->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 if(node->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 printf("sp: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 } else if(node->type == CONFIG_NODE_COMMENT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 printf("cm: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 } else if(node->type == CONFIG_NODE_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 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
148 test_print_config(node);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 printf("o}: %s", node->text_end.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 } else if(node->type == CONFIG_NODE_DIRECTIVE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 printf("di: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 printf("fk: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
157 */
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
159 static void config_arg_set_value(UcxAllocator *a, ConfigArg *arg, CFGToken token) {
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
160 scstr_t nv = scstrchr(token.content, '=');
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
161 if(!nv.ptr) {
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
162 arg->value = sstrdup_a(a, token.content);
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
163 } else {
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
164 intptr_t eq = (intptr_t)(nv.ptr - token.content.ptr);
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
165 scstr_t name = token.content;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
166 name.length = (size_t)eq;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
167
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
168 scstr_t value = nv;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
169 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
170 value.length--;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
171 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
172 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
173 value.length -= 2; // remove quote
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
174 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
175
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
176 arg->name = sstrdup_a(a, name);
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
177 arg->value = sstrdup_a(a, value);
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
178 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
179 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
180
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 ServerConfig* serverconfig_parse(scstr_t content) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 UcxMempool *mp = ucx_mempool_new(512);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(!mp) return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 UcxAllocator *a = mp->allocator;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 ServerConfig *config = ucx_mempool_malloc(mp, sizeof(ServerConfig));
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 if(!config) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 ucx_mempool_destroy(mp);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 config->mp = mp;
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 // PARSE:
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 // first non space/comment token is directive/object name
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 // following tokens are arguments
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 // newline starts new directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 // '{' converts directive to object and following directives will
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 // be placed into the object
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 int pos = 0; // needed for tokenizer
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 CFGToken token;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 ConfigNode *root_obj = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode));
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 root_obj->type = CONFIG_NODE_OBJECT;
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 UcxList *node_stack = ucx_list_prepend(NULL, root_obj);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 ConfigNode *current = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode));
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 ConfigNode *obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 int obj_closed = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 int text_start = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 int err = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 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
215 //printf("%s [%.*s]\n", token_type_str(token.type), (int)token.content.length, token.content.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 switch(token.type) {
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
218 case CFG_NO_TOKEN: break;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 case CFG_TOKEN_COMMENT: {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 if(current->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 current->type = CONFIG_NODE_COMMENT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 case CFG_TOKEN_SPACE: break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 case CFG_TOKEN_NEWLINE: {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 scstr_t line = scstrsubsl(content, text_start, pos - text_start);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 text_start = pos;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 sstr_t line_cp = sstrdup_a(a, line);
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 ConfigNode *parent = node_stack->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 if(current->type == CONFIG_NODE_CLOSE_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 parent->text_end = line_cp;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 node_stack = ucx_list_remove_a(a, node_stack, node_stack);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 } else if(current->type == CONFIG_NODE_OPEN_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 sstr_t new_textbegin = sstrcat_a(a, 2, obj->text_begin, line_cp);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 alfree(a, obj->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 alfree(a, line_cp.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 obj->text_begin = new_textbegin;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 current->text_begin = line_cp;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 ConfigNode *parent = node_stack->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 parent->children = ucx_list_append_a(a, parent->children, current);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 if(obj && obj->type == CONFIG_NODE_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 node_stack = ucx_list_prepend_a(a, node_stack, obj);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 current = ucx_mempool_calloc(mp, 1, sizeof(ConfigNode));
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 obj_closed = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 case CFG_TOKEN: {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 if(!sstrcmp(token.content, S("{"))) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 if(!obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 break;
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 obj->type = CONFIG_NODE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 if(current != obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 current->type = CONFIG_NODE_OPEN_OBJECT;
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 } else if(!sstrcmp(token.content, S("}"))) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 obj_closed = 1; // force newline before next directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 current->type = CONFIG_NODE_CLOSE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 if(obj_closed) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 break;
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 if(!current->name.ptr) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 current->name = sstrdup_a(a, token.content);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 current->type = CONFIG_NODE_DIRECTIVE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 obj = current;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 ConfigArg *arg = ucx_mempool_calloc(mp, 1, sizeof(ConfigArg));
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
284 config_arg_set_value(a, arg, token);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 current->args = ucx_list_append_a(a, current->args, arg);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 if(err) {
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 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 if(pos < content.length || err) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 // content not fully parsed because of an error
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 ucx_mempool_destroy(mp);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 //test_print_config(&root_obj);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 config->root = root_obj;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 config->tab = sstrdup_a(a, SC("\t"));
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 return config;
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 void serverconfig_free(ServerConfig *cfg) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 ucx_mempool_destroy(cfg->mp);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 ConfigNode* serverconfig_get_node(ConfigNode *parent, ConfigNodeType type, scstr_t name) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 UCX_FOREACH(elm, parent->children) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 ConfigNode *node = elm->data;
366
47bc686fafe4 add new dav config element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 269
diff changeset
317 if(node->type == type && !sstrcasecmp(node->name, name)) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 return node;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 }
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 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 }
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 UcxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, scstr_t name) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 UcxList *nodes = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 UCX_FOREACH(elm, parent->children) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 ConfigNode *node = elm->data;
366
47bc686fafe4 add new dav config element
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 269
diff changeset
329 if(node->type == type && !sstrcasecmp(node->name, name)) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 nodes = ucx_list_append(nodes, node);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 return nodes;
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 scstr_t serverconfig_directive_value(ConfigNode *obj, scstr_t name) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 ConfigNode *node = serverconfig_get_node(obj, CONFIG_NODE_DIRECTIVE, name);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 if(node && ucx_list_size(node->args) == 1) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 ConfigArg *arg = node->args->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 return SCSTR(arg->value);
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 return scstrn(NULL, 0);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 }
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
345
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
346 sstr_t serverconfig_arg_name_value(UcxAllocator *a, scstr_t str, scstr_t *name) {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
347 int valstart = 0;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
348 for(int i=0;i<str.length;i++) {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
349 if(str.ptr[i] == '=') {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
350 if(name) {
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
351 name->ptr = str.ptr;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
352 name->length = i;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
353 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
354 valstart = i + 1;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
355 break;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
356 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
357 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
358
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
359 sstr_t ret;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
360 return ret;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
361 }

mercurial