src/server/config/objconf.c

Sat, 18 Mar 2023 11:44:37 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 18 Mar 2023 11:44:37 +0100
changeset 477
39ebd50cfc12
parent 459
f21b4ff81c01
child 490
d218607f5a7e
permissions
-rw-r--r--

fix nsapi_error_request() could send empty error messages with http status 200, if the request status code wasn't set

16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
44
3da1f7b6847f added some error messages
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 25
diff changeset
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
a9bbd82d2dce New configuration file parser
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
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "objconf.h"
453
4586d534f9b5 fix build on macos
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
30 #include "logging.h"
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <string.h>
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
34 #include <cx/utils.h>
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
35
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 /* dev notes:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 *
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 * to free ObjectConfig, free:
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 * line dlist
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 * mempool
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 * object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 */
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 ObjectConfig *load_object_config(char *file) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 FILE *in = fopen(file, "r");
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 if(in == NULL) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 return NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 ObjectConfig *conf = malloc(sizeof(ObjectConfig));
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 conf->parser.parse = objconf_parse;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 conf->file = file;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
53 //conf->conditions = NULL;
23
a2c8fc23c90e Added basic authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 17
diff changeset
54 conf->levels = NULL;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
55 conf->objects = cxPointerLinkedListCreate(cxDefaultAllocator, cx_cmp_ptr);
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
56 //conf->lines = NULL;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 if(r != 0) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 // TODO: free
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 return NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 }
62
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
63
c47e081b6c0f added keyfile based authentication
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 44
diff changeset
64 fclose(in);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 return conf;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
419
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
69 ObjectConfig2* objectconf_load(const char *file) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
70 CxMempool *mp = cxBasicMempoolCreate(512);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
71 if(!mp) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
72 return NULL;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
73 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
74
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
75 // setup parser
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
76 ConfigParser2 parser;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
77 memset(&parser, 0, sizeof(ConfigParser2));
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
78 parser.mp = mp;
459
f21b4ff81c01 log error in get_next_token()
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 453
diff changeset
79 parser.filename = file;
425
6440171c257f make config parser delimiters configurable
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 419
diff changeset
80 parser.delim = "()/*%";
419
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
81 parser.validateDirective = objectconf_validate_directive;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
82 parser.validateObjBegin = objectconf_validate_objbegin;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
83 parser.validateObjEnd = objectconf_validate_objend;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
84 parser.allow_hierarchy = 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
85
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
86 ConfigNode *obj_config = serverconfig_load_file(&parser, file);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
87 if(!obj_config) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
88 cxMempoolDestroy(mp);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
89 return NULL;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
90 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
91
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
92 ObjectConfig2 *conf = cxMalloc(mp->allocator, sizeof(ObjectConfig2));
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
93 if(!conf) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
94 cxMempoolDestroy(mp);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
95 return NULL;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
96 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
97
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
98 conf->mp = mp;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
99 conf->root = obj_config;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
100
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
101 return conf;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
102 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
103
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
104 void objectconf_free(ObjectConfig2 *objconf) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
105 cxMempoolDestroy(objconf->mp);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
106 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
107
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
108 int objectconf_validate_directive(ConfigParser2 *parser, ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
109 // It is possible that this function is called for Objects
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
110 // if { is on the next line
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
111 // It may be possible to fix the parser to not do that, but it is fine
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
112 // to just deal with that here
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
113
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
114 // ignore if node->name is an Object name like
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
115 // Object, Client, If, Else IfElse
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
116 const char *objnames[] = { "Object", "Client", "If", "ElseIf", "Else" };
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
117 size_t typeindex;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
118 if(!serverconfig_validate_directive_name(node, objnames, 5, &typeindex)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
119 return 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
120 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
121
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
122 // now check directive type
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
123 const char *dirtypes[] = { "AuthTrans", "NameTrans", "PathCheck", "ObjectType", "Service", "AddLog", "Error" };
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
124 if(serverconfig_validate_directive_name(node, dirtypes, 7, &typeindex)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
125 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
126 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
127
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
128 // directives must have parameter names
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
129 ConfigParam *param_err;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
130 if(serverconfig_check_param_names(node, &param_err)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
131 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
132 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
133
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
134 // check if the fn parameter exists
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
135 cxstring fn = serverconfig_directive_get_arg(node, cx_str("fn"));
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
136 if(fn.length == 0) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
137 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
138 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
139
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
140 return 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
141 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
142
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
143 static int validate_else_node(ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
144 // previous node must be "If" or "ElseIf"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
145 ConfigNode *prev = serverconfig_previous_dir_or_obj(node);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
146 const char *allowed_prev_types[] = { "If", "ElseIf" };
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
147 size_t typeindex;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
148 if(serverconfig_validate_directive_name(prev, allowed_prev_types, 2, &typeindex)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
149 return 1; // prevous node (not counting space, comments) is not "If" or "Else"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
150 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
151
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
152 // "Else" must have no args
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
153 return node->args ? 1 : 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
154 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
155
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
156 static int validate_elseif_node(ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
157 // the previous node must be "If" or "IfElse"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
158 // in case if "IfElse", we also have to check all previous nodes, until
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
159 // we find the starting "If" node
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
160 const char *allowed_prev_types[] = { "If", "ElseIf" };
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
161 size_t typeindex;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
162 ConfigNode *prev = serverconfig_previous_dir_or_obj(node);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
163 while(prev) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
164 if(serverconfig_validate_directive_name(prev, allowed_prev_types, 2, &typeindex)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
165 return 1; // previous node was not "If" or "ElseIf"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
166 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
167 // we found a valid "If" node, the whole construct is valid
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
168 if(typeindex == 0) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
169 return 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
170 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
171
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
172 prev = serverconfig_previous_dir_or_obj(node);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
173 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
174 // at this point we found only "IfElse" nodes, but no "If" node
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
175 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
176 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
177
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
178 int objectconf_validate_objbegin(ConfigParser2 *parser, ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
179 // get object type
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
180 const char *objnames[] = { "Object", "Client", "If", "ElseIf", "Else" };
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
181 size_t typeindex;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
182 if(serverconfig_validate_directive_name(node, objnames, 5, &typeindex)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
183 return 1; // unknown object type
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
184 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
185
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
186 // some special requirements for "Else" node
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
187 if(typeindex == 4) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
188 // type is "Else"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
189 return validate_else_node(node);
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
190 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
191
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
192 // check "ElseIf" was used after "If" or "IfElse"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
193 if(typeindex == 3 && validate_elseif_node(node)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
194 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
195 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
196
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
197 // only Object must have a name/value arg, other types can have more
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
198 // complex parameters
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
199 if(typeindex == 0) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
200 // type is "Object"
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
201 ConfigParam *err;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
202 if(serverconfig_check_param_names(node, &err)) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
203 return 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
204 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
205 return 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
206 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
207
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
208 // all remaining object types must have arguments, but don't check more
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
209 return node->args ? 0 : 1;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
210 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
211
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
212 int objectconf_validate_objend(ConfigParser2 *parser, ConfigNode *node) {
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
213 return 0;
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
214 }
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
215
f1d29785ad2d new obj.conf parser based on serverconfig parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 415
diff changeset
216
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
217 void free_object_config(ObjectConfig *conf) {
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
218 // free other lists
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
219 if(conf->levels) {
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
220 //ucx_list_free(conf->levels);
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
221 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
222
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
223 // free mempool
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
224 //ucx_mempool_destroy(conf->parser.mp->pool);
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
225 free(conf);
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
226 }
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
227
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228
a9bbd82d2dce New configuration file parser
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: 115
diff changeset
230 int objconf_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 ObjectConfig *conf = p;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
233 begin->type = cfg_get_line_type(line);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 switch(begin->type) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 case LINE_BEGIN_TAG: {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
236 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 if(tag == NULL) {
453
4586d534f9b5 fix build on macos
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
238 ws_cfg_log(LOG_FAILURE, "Parse error in %s", conf->file);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 exit(-1); // TODO: better error handling
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 tag->begin = begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 tag->end = end;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
243 tag->type_num = cfg_get_tag_type(cx_strcast(tag->name));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
244 //printf("line {%s}\n", cx_strdub(ll).ptr);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 if(objconf_on_begin_tag(conf, tag) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
246 fprintf(stderr, "1error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 case LINE_END_TAG: {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
252 cxmutstr tag = cfg_get_end_tag_name(line);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 if(objconf_on_end_tag(conf, tag) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
254 fprintf(stderr, "2error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 case LINE_DIRECTIVE: {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
261 ConfigDirective *dir = cfg_parse_directive(
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 line,
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 conf->parser.mp);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 dir->begin = begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 dir->end = end;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 if(objconf_on_directive(conf, dir) != 0) {
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
267 fprintf(stderr, "3error\n");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 exit(-1);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 int objconf_on_begin_tag(ObjectConfig *conf, ConfigTag *tag) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
276 CxAllocator *mp = conf->parser.mp;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 if(tag->type_num != TAG_OBJECT) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
278 ConfigParserLevel *l = conf->levels;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 if(l->tag->type_num != TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 tag->parent = l->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 switch(tag->type_num) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 case TAG_OBJECT: {
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
287 ConfigObject *obj = OBJ_NEW_N(mp, ConfigObject);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 obj->begin = tag->begin;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 obj->end = tag->end;
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
290
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
291 obj->name = cfg_param_get(tag->param, cx_str("name"));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
292 obj->ppath = cfg_param_get(tag->param, cx_str("ppath"));
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 conf->obj = obj;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
295 //conf->objects = ucx_list_append_a(mp, conf->objects, obj);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
296 cxListAdd(conf->objects, obj);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 // create tree level object
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
299 ConfigParserLevel *lvl = OBJ_NEW(mp, ConfigParserLevel);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 lvl->iftag = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 lvl->levelnum = 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 lvl->tag = tag;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
303 lvl->next = NULL;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
304 //conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
305 CFG_LEVEL_PREPEND(&conf->levels, lvl);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 case TAG_IF: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 // create tree level object
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
311 ConfigParserLevel *last_lvl = conf->levels;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312
83
28433f06d5ee added minimal nsapi conditions
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 79
diff changeset
313 ConfigParserLevel *lvl = OBJ_NEW(mp, ConfigParserLevel);
17
d2a97bbeb57d Added init.conf parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 16
diff changeset
314
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 lvl->iftag = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 lvl->levelnum = last_lvl->levelnum + 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 lvl->tag = tag;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
318 //conf->levels = ucx_list_prepend_a(mp, conf->levels, lvl);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
319 CFG_LEVEL_PREPEND(&conf->levels, lvl);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 last_lvl->iftag = tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 case TAG_ELSEIF: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 case TAG_ELSE: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 // create tree level object
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
328 ConfigParserLevel *last_lvl = conf->levels;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 tag->iftag = last_lvl->iftag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 ConfigParserLevel *lvl = OBJ_NEW(
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 conf->parser.mp,
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 ConfigParserLevel);
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 lvl->iftag = last_lvl->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 lvl->levelnum = last_lvl->levelnum + 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 lvl->tag = tag;
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
338 //conf->levels = ucx_list_prepend(conf->levels, lvl);
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
339 CFG_LEVEL_PREPEND(&conf->levels, lvl);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 case TAG_CLIENT: {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 // create tree level object
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 // TODO
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 break;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 default: {
453
4586d534f9b5 fix build on macos
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
351 ws_cfg_log(LOG_FAILURE, "objconf: unknown tag");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 return 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
359 int objconf_on_end_tag(ObjectConfig *conf, cxmutstr tagname) {
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
360 int type = cfg_get_tag_type(cx_strcast(tagname));
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 if(type == -1) {
453
4586d534f9b5 fix build on macos
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 425
diff changeset
362 ws_cfg_log(LOG_FAILURE, "objconf: unknown tag");
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 return 1;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 } else {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 if(type == TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 conf->obj = NULL;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 // remove level
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
370 /*
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
371 conf->levels = ucx_list_remove_a(
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
372 conf->parser.mp,
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
373 conf->levels,
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
374 conf->levels);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
375 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
376 conf->levels = conf->levels->next;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 int objconf_on_directive(ObjectConfig *conf, ConfigDirective *dir) {
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
383 ConfigParserLevel *lvl = conf->levels;
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 // check if we have a condition for the directive
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 // if the level tag is not an object tag, use it as condition
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 if(lvl->tag->type_num != TAG_OBJECT) {
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 dir->condition = lvl->tag;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 // add directive to current object
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
392 /*
91
fac51f87def0 ucx update
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 88
diff changeset
393 conf->obj->directives[dir->type_num] = ucx_list_append_a(
79
f48cea237ec3 fixed some memory leaks
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 62
diff changeset
394 conf->parser.mp,
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 conf->obj->directives[dir->type_num],
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 dir);
415
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
397 */
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
398
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
399 ConfigDirectiveList *dir_entry = cxMalloc(conf->parser.mp, sizeof(ConfigDirectiveList));
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
400 dir_entry->directive = dir;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
401 dir_entry->next = NULL;
d938228c382e switch from ucx 2 to 3
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 115
diff changeset
402 CFG_DIRECTIVES_ADD(&conf->obj->directives[dir->type_num], dir_entry);
16
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 return 0;
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 }
a9bbd82d2dce New configuration file parser
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406

mercurial