src/server/config/serverconfig.c

Fri, 29 Nov 2024 18:10:26 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 29 Nov 2024 18:10:26 +0100
changeset 564
f19fd264c69c
parent 512
afcdb57e329d
permissions
-rw-r--r--

fix wrong column name usage by pg prop extensions

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"
512
afcdb57e329d fix build on macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
32 #include "logging.h"
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <stdio.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <stdlib.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <string.h>
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <ctype.h>
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
38 #include <errno.h>
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
40 #include <cx/buffer.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
41 #include <cx/utils.h>
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 ServerConfig* serverconfig_load(const char *file) {
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
44 CxMempool *mp = cxBasicMempoolCreate(512);
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
45 if(!mp) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
46 return NULL;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
47 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
48
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
49 ConfigParser2 parser;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
50 memset(&parser, 0, sizeof(ConfigParser2));
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
51 parser.mp = mp;
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
52 parser.filename = file;
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
53 parser.allow_hierarchy = true;
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
54 parser.delim = "";
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
55 ConfigNode *root = serverconfig_load_file(&parser, file);
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
56 if(!root) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
57 cxMempoolDestroy(mp);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 return NULL;
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
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
61 ServerConfig *scfg = cxMalloc(mp->allocator, sizeof(ServerConfig));
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
62 if(!scfg) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
63 cxMempoolDestroy(mp);
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
64 return NULL;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
65 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
66 scfg->root = root;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
67 scfg->mp = mp;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
68 scfg->tab = cx_str("\t");
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
69
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
70 return scfg;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
71 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
72
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
73 ConfigNode* serverconfig_load_file(ConfigParser2 *parser, const char *file) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
74 FILE *in = fopen(file, "r");
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
75 if(in == NULL) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
76 parser->error = CONFIG_PARSER_IO_ERROR;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
77 parser->io_errno = errno;
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
78 return NULL;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
79 }
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
80
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
81 // temporary buffer to store the file content
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
82 CxBuffer buf;
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
83 if(cxBufferInit(&buf, NULL, 16384, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND|CX_BUFFER_FREE_CONTENTS)) {
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
84 fclose(in);
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
85 parser->error = CONFIG_PARSER_OOM;
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
86 return NULL;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
87 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
88
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
89 //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
90 char readbuf[2048];
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
91 size_t r;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
92 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
93 cxBufferWrite(readbuf, 1, r, &buf);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 fclose(in);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
97 ConfigNode *root = serverconfig_parse(parser, cx_strn(buf.space, buf.size));
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
98 cxBufferDestroy(&buf);
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
99 return root;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
102 static int scfg_char_is_delim(ConfigParser2 *parser, char c) {
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
103 size_t len = strlen(parser->delim);
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
104 for(int i=0;i<len;i++) {
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
105 if(c == parser->delim[i]) {
423
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 422
diff changeset
106 return 1;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 422
diff changeset
107 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 422
diff changeset
108 }
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 422
diff changeset
109 return 0;
bb7cff720dd0 add obj.conf expression parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 422
diff changeset
110 }
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
112 static CFGToken get_next_token(ConfigParser2 *parser, cxstring content, int *pos) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 CFGToken token = { {NULL, 0}, CFG_NO_TOKEN };
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 CFGTokenType type = CFG_TOKEN;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 int start = *pos;
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 int token_begin = -1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 int token_end = content.length-1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 int quote = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 int comment = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 int i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 char prev = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 for(i=start;i<content.length;i++) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 char c = content.ptr[i];
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 *pos = i;
512
afcdb57e329d fix build on macOS
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 490
diff changeset
131 ws_cfg_log(
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
132 LOG_FAILURE,
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
133 "cfgparser: file %s:%d:%d: error: %s",
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
134 parser->filename,
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
135 parser->linenum,
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
136 parser->linepos+2,
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
137 "missing '\"' character before end of line");
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 return token; // error
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 } else if(start == i) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 // single newline char token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 type = CFG_TOKEN_NEWLINE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 token_end = i+1;
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
144 parser->linenum++;
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
145 parser->linepos = 0;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 // only space/comment token
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 token_begin = start;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 type = comment ? CFG_TOKEN_COMMENT : CFG_TOKEN_SPACE;
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 // 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
156 i--;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158 } else if(quote) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 if(c == '"' && prev != '\\') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 quote = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 } else if(comment) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 // ignore
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 if(c == '\n') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 comment = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167 } else if(c == '#') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 comment = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 } else if(isspace(c)) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 if(token_begin >= 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 token_end = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 } else if(c == '"') {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 quote = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 }
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
179 } else if(scfg_char_is_delim(parser, c)) {
422
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
180 if(token_begin >= 0) {
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
181 token_end = i;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
182 i--;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
183 break;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
184 } else {
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
185 token_begin = i;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
186 token_end = i+1;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
187 break;
76f2f5d532d0 extend serverconfig tokenizer to create separate tokens for brackets
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
188 }
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 } else if(token_begin < 0) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 token_begin = i;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 prev = c;
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
193
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
194 parser->linepos++;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 *pos = i + 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 if(token_begin < 0) {
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
200 return token; // error or EOF
256
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
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 token.type = type;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
204 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
205 return token;
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
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
208 /*
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 static void test_print_config(ConfigNode *parent) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 UCX_FOREACH(elm, parent->children) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 ConfigNode *node = elm->data;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 if(node->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 printf("sp: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 } else if(node->type == CONFIG_NODE_COMMENT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 printf("cm: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 } else if(node->type == CONFIG_NODE_OBJECT) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 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
219 test_print_config(node);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 printf("o}: %s", node->text_end.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 } else if(node->type == CONFIG_NODE_DIRECTIVE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 printf("di: %s", node->text_begin.ptr);
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 printf("fk: %s", node->text_begin.ptr);
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 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 }
258
134279e804b6 remove old server.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 256
diff changeset
228 */
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
230 static void config_arg_set_value(CxAllocator *a, ConfigParam *arg, CFGToken token) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
231 cxstring nv = (cxstring){NULL,0};
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
232 WSBool quotes = token.content.ptr[0] == '\"';
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
233 if(!quotes) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
234 nv = cx_strchr(token.content, '=');
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
235 }
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
236 if(!nv.ptr) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
237 if(quotes) {
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
238 // remove quote
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
239 token.content.ptr++;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
240 token.content.length -= 2;
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
241 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
242 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
243 } else {
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
244 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
245 cxstring name = token.content;
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
246 name.length = (size_t)eq;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
247
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
248 cxstring value = nv;
367
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
249 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
250 value.length--;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
251 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
252 value.ptr++;
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
253 value.length -= 2; // remove quote
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
254 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
255
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
256 arg->name = cx_strdup_a(a, name);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
257 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
258 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
259 }
1592224f6059 add support for name/value config args
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 366
diff changeset
260
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
261 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
262 ConfigNodeStack *elm = cxMalloc(a, sizeof(ConfigNodeStack));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
263 if(!elm) return 1;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
264 elm->node = node;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
265 elm->next = NULL;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
266 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
267 return 0;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
268 }
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
269
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
270 ConfigNode* serverconfig_parse(ConfigParser2 *parser, cxstring content) {
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
271 CxMempool *mp = parser->mp;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
272 CxAllocator *a = (CxAllocator*)mp->allocator;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
274 parser->linenum = 1;
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
275 parser->linepos = 1;
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 455
diff changeset
276
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
277 if(!parser->delim) {
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
278 parser->delim = "";
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
279 }
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
280
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 // PARSE:
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 // first non space/comment token is directive/object name
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 // following tokens are arguments
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 // newline starts new directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 // '{' converts directive to object and following directives will
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 // be placed into the object
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 int pos = 0; // needed for tokenizer
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 CFGToken token;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
290 ConfigNode *root_obj = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 root_obj->type = CONFIG_NODE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
293 ConfigNodeStack *node_stack = cxMalloc(a, sizeof(ConfigNodeStack));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
294 node_stack->node = root_obj;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
295 node_stack->next = NULL;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
297 ConfigNode *current = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 ConfigNode *obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 int obj_closed = 0;
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 int text_start = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 int err = 0;
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 424
diff changeset
304 while((token = get_next_token(parser, content, &pos)).type != CFG_NO_TOKEN) {
464
0a29110b94ec add ldap-query saf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 459
diff changeset
305 //printf("[%.*s]\n", (int)token.content.length, token.content.ptr); fflush(stdout);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
306
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 switch(token.type) {
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
308 case CFG_NO_TOKEN: break;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 case CFG_TOKEN_COMMENT: {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 if(current->type == CONFIG_NODE_SPACE) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 current->type = CONFIG_NODE_COMMENT;
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 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 case CFG_TOKEN_SPACE: break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 case CFG_TOKEN_NEWLINE: {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
317 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
318 text_start = pos;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
320 cxmutstr line_cp = cx_strdup_a(a, line);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
322 ConfigNode *parent = node_stack->node;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 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
324 // 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
325 // 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
326 parent->text_end = line_cp;
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
327 // validate
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
328 if(parser->validateObjEnd && parser->validateObjEnd(parser, parent)) {
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
329 // validation failed
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
330 err = 1;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
331 break;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
332 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
333 // 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
334 ConfigNodeStack *remove_item = node_stack;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
335 node_stack = node_stack->next;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
336 cxFree(a, remove_item);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 } 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
338 // newline after a object is opened with '{'
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
339 // append '{' to the object text
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
340 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
341 cxFree(a, obj->text_begin.ptr);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
342 cxFree(a, line_cp.ptr);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 obj->text_begin = new_textbegin;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
345 // normal line containing a directive, space or comment
419
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
346
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
347 // add it to parent node
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
348 current->text_begin = line_cp;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
349 CFG_NODE_ADD(&parent->children_begin, &parent->children_end, current);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
350
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
351 // validate after CFG_NODE_ADD, because now current->prev is set
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
352 if(current->type == CONFIG_NODE_DIRECTIVE &&
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
353 parser->validateDirective &&
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
354 parser->validateDirective(parser, current))
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
355 {
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
356 err = 1;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
357 break;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
358 }
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
361 // 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
362 // 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
363 // a '{' character
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 if(obj && obj->type == CONFIG_NODE_OBJECT) {
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
365 // new object started
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
366 if(parser->validateObjBegin && parser->validateObjBegin(parser, obj)) {
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
367 // validation callback failed
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
368 err = 1;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
369 break;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
370 }
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
371
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
372 // add it to the stack
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
373 nodestack_prepend(a, &node_stack, obj);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
377 current = cxCalloc(a, 1, sizeof(ConfigNode));
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 current->type = CONFIG_NODE_SPACE;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 obj_closed = 0;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 case CFG_TOKEN: {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
384 // normal text token
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
385 // either a directive/obj name, parameter or { }
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
386
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
387 if(!cx_strcmp(token.content, cx_str("{"))) {
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
388 // check if the parser allows an object hierarchy
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
389 if(!parser->allow_hierarchy) {
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
390 parser->error = CONFIG_PARSER_SYNTAX_ERROR;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
391 err = 1;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
392 break;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
393 }
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
394
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
395 // 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
396 // a directive
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 if(!obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 obj->type = CONFIG_NODE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 if(current != obj) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 current->type = CONFIG_NODE_OPEN_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
405 } else if(!cx_strcmp(token.content, cx_str("}"))) {
417
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
406 // check if the parser allows an object hierarchy
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
407 if(!parser->allow_hierarchy) {
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
408 parser->error = CONFIG_PARSER_SYNTAX_ERROR;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
409 err = 1;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
410 break;
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
411 }
90805bb9fbd6 prepare serverconfig parser to be also used for obj.conf and init.conf
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
412
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 obj_closed = 1; // force newline before next directive
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 obj = NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 current->type = CONFIG_NODE_CLOSE_OBJECT;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 } else {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 if(obj_closed) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 err = 1;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 if(!current->name.ptr) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
423 // 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
424 current->name = cx_strdup_a(a, token.content);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 current->type = CONFIG_NODE_DIRECTIVE;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
426 obj = current; // potential object
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 } else {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
428 // name already set, therefore this token must
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
429 // be a parameter
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
430 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
431 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
432 CFG_PARAM_ADD(&current->args, NULL, arg);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 if(err) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 break;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 if(pos < content.length || err) {
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 // content not fully parsed because of an error
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 //test_print_config(&root_obj);
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
450 return root_obj;
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 void serverconfig_free(ServerConfig *cfg) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
454 cxMempoolDestroy(cfg->mp);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
457 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
458 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
459 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
460 return node;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 return NULL;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
466 CxList* serverconfig_get_node_list(ConfigNode *parent, ConfigNodeType type, cxstring name) {
490
d218607f5a7e update ucx
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 464
diff changeset
467 CxList *nodes = cxLinkedListCreate(cxDefaultAllocator, NULL, CX_STORE_POINTERS);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
469 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
470 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
471 cxListAdd(nodes, node);
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 return nodes;
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 }
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
478 cxstring serverconfig_object_directive_value(ConfigNode *obj, cxstring name) {
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 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
480 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
481 ConfigParam *arg = node->args;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
482 return (cxstring){ arg->value.ptr, arg->value.length };
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 }
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 367
diff changeset
484 return (cxstring){ NULL, 0 };
256
19259b6c5cf7 replace old server config loader
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 }
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
486
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
487 cxstring serverconfig_directive_get_arg(ConfigNode *directive, cxstring arg_name) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
488 cxstring ret = (cxstring){ NULL, 0 };
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
489 ConfigParam *arg = directive->args;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
490 while(arg) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
491 if(!cx_strcmp(arg_name, cx_strcast(arg->name))) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
492 ret = cx_strcast(arg->value);
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
493 break;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
494 }
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
495 arg = arg->next;
269
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
496 }
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
497
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
498 return ret;
3dfbd0b91950 add ResourcePool initialization
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 258
diff changeset
499 }
418
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
500
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
501
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
502
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
503
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
504 /* -------------------------- utility functions -------------------------- */
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
505
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
506 int serverconfig_validate_directive_name(
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
507 ConfigNode *directive,
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
508 const char *names[],
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
509 size_t numnames,
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
510 size_t *nameindex)
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
511 {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
512 for(size_t i=0;i<numnames;i++) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
513 if(!cx_strcmp(cx_strcast(directive->name), cx_str(names[i]))) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
514 *nameindex = i;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
515 return 0;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
516 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
517 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
518 return 1;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
519 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
520
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
521 int serverconfig_check_param_names(ConfigNode *directive, ConfigParam **err) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
522 ConfigParam *arg = directive->args;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
523 while(arg) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
524 if(arg->name.length == 0) {
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
525 *err = arg;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
526 return 1;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
527 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
528 arg = arg->next;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
529 }
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
530 return 0;
b7dcc9c4f270 use server.conf parser for init.conf as well
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 417
diff changeset
531 }
419
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
532
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
533 ConfigNode* serverconfig_previous_dir_or_obj(ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
534 node = node->prev;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
535 while(node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
536 if(node->type == CONFIG_NODE_DIRECTIVE || node->type == CONFIG_NODE_OBJECT) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
537 return node;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
538 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
539 node = node->prev;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
540 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
541 return NULL;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
542 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
543
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
544 size_t serverconfig_children_count(ConfigNode *node, ConfigNodeType type) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
545 size_t count = 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
546 node = node->children_begin;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
547 while(node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
548 if(node->type == type) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
549 count++;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
550 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
551 node = node->next;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
552 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
553 return count;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 418
diff changeset
554 }

mercurial