src/server/config/serverconf.c

branch
config
changeset 258
134279e804b6
parent 257
bfeb015c98a4
child 259
0b8692959d37
equal deleted inserted replaced
257:bfeb015c98a4 258:134279e804b6
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "serverconf.h"
34
35 ServerConfig2 *load_server_config(char *file) {
36 FILE *in = fopen(file, "r");
37 if(in == NULL) {
38 return NULL;
39 }
40
41 ServerConfig2 *conf = malloc(sizeof(ServerConfig2));
42 conf->parser.parse = serverconf_parse;
43 conf->file = file;
44 conf->obj = NULL;
45
46 conf->objects = ucx_map_new(16);
47 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
48 cfg_map_destr(conf->parser.mp->pool, conf->objects);
49 if(r != 0) {
50 // TODO: free
51 return NULL;
52 }
53
54 fclose(in);
55
56 return conf;
57 }
58
59 void free_server_config(ServerConfig2 *conf) {
60 ucx_mempool_destroy(conf->parser.mp->pool);
61 free(conf);
62 }
63
64 int serverconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line){
65 ServerConfig2 *conf = p;
66 UcxAllocator *mp = conf->parser.mp;
67
68 begin->type = cfg_get_line_type(line);
69 switch(begin->type) {
70 case LINE_BEGIN_TAG: {
71 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
72
73 // create server config object
74 ServerConfigObject *obj = OBJ_NEW(
75 conf->parser.mp,
76 ServerConfigObject);
77 obj->type = tag->name;
78 obj->begin = begin;
79 obj->end = end;
80 obj->directives = NULL;
81
82 // add object to server config
83 UcxList *list = ucx_map_sstr_get(conf->objects, obj->type);
84 list = ucx_list_append_a(mp, list, obj);
85 ucx_map_sstr_put(conf->objects, obj->type, list);
86 conf->obj = obj;
87
88 break;
89 }
90 case LINE_END_TAG: {
91 sstr_t tag = cfg_get_end_tag_name(line);
92 if(sstrcmp(tag, conf->obj->type) != 0) {
93 log_ereport(LOG_FAILURE, "server.conf: syntax error: wrong close tag");
94 log_ereport(LOG_FAILURE, "open tag: %s close tag: %s", sstrdup(tag).ptr, sstrdup(conf->obj->type).ptr);
95 exit(-1);
96 }
97 conf->obj = NULL;
98
99 break;
100 }
101 case LINE_DIRECTIVE: {
102 if(conf->obj == NULL) {
103 log_ereport(LOG_FAILURE, "server.conf: directive outside of object");
104 exit(-1);
105 }
106
107 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
108 d->begin = begin;
109 d->end = end;
110
111 //printf("%s.%s\n", conf->obj->type.ptr, d->directive_type.ptr);
112 conf->obj->directives = ucx_list_append_a(mp, conf->obj->directives, d);
113 }
114 }
115 return 0;
116 }
117
118

mercurial