src/server/config/serverconf.c

changeset 18
73aacbf6e492
child 19
d680536f8c2f
equal deleted inserted replaced
17:d2a97bbeb57d 18:73aacbf6e492
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2011 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 ServerConfig *load_server_config(char *file) {
36 FILE *in = fopen(file, "r");
37 if(in == NULL) {
38 return NULL;
39 }
40
41 ServerConfig *conf = malloc(sizeof(ServerConfig));
42 conf->parser.parse = serverconf_parse;
43 conf->file = file;
44 conf->objects = ucx_map_new(8);
45
46 conf->obj = NULL;
47
48 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
49 if(r != 0) {
50 // TODO: free
51 return NULL;
52 }
53
54 return conf;
55 }
56
57 void free_server_config(ServerConfig *conf) {
58 // TODO
59 }
60
61 int serverconf_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line){
62 ServerConfig *conf = p;
63
64 begin->type = cfg_get_line_type(line);
65 switch(begin->type) {
66 case LINE_BEGIN_TAG: {
67 ConfigTag *tag = cfg_parse_begin_tag(line, conf->parser.mp);
68
69 // create server config object
70 ServerConfigObject *obj = OBJ_NEW(
71 conf->parser.mp,
72 ServerConfigObject);
73 obj->type = tag->name;
74 obj->begin = begin;
75 obj->end = end;
76 obj->directives = NULL;
77
78 // add object to server config
79 UcxList *list = ucx_map_sstr_get(conf->objects, obj->type);
80 list = ucx_list_append(list, obj);
81 ucx_map_sstr_put(conf->objects, obj->type, list);
82 conf->obj = obj;
83
84 break;
85 }
86 case LINE_END_TAG: {
87 sstr_t tag = cfg_get_end_tag_name(line);
88 if(sstrcmp(tag, conf->obj->type) != 0) {
89 fprintf(stderr, "syntax error: wrong close tag\n");
90 exit(-1);
91 }
92 conf->obj = NULL;
93
94 break;
95 }
96 case LINE_DIRECTIVE: {
97 if(conf->obj == NULL) {
98 fprintf(stderr, "syntax error: directive outside of object\n");
99 exit(-1);
100 }
101
102 ConfigDirective *d = cfg_parse_directive(line, conf->parser.mp);
103 d->begin = begin;
104 d->end = end;
105
106 //printf("%s.%s\n", conf->obj->type.ptr, d->directive_type.ptr);
107 conf->obj->directives = ucx_dlist_append(conf->obj->directives, d);
108 }
109 }
110 return 0;
111 }

mercurial