1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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->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
51 return NULL;
52 }
53
54 fclose(in);
55
56 return conf;
57 }
58
59 void free_server_config(ServerConfig *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 ServerConfig *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
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
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
112 conf->obj->directives = ucx_list_append_a(mp, conf->obj->directives, d);
113 }
114 }
115 return 0;
116 }
117
118
119 UcxList* srvcfg_get_listeners(ServerConfig *cfg, UcxAllocator *mp,
int *error) {
120 mp = mp ? mp : cfg->parser.mp;
121
122 UcxList *list = ucx_map_sstr_get(cfg->objects, sstrn(
"Listener",
8));
123 UcxList *lslist =
NULL;
124 UCX_FOREACH(elm, list) {
125 ServerConfigObject *ls = elm->data;
126 sstr_t name = cfg_directivelist_get_str(ls->directives, sstr(
"Name"));
127 sstr_t port = cfg_directivelist_get_str(ls->directives, sstr(
"Port"));
128 sstr_t vs = cfg_directivelist_get_str(
129 ls->directives,
130 sstr(
"DefaultVS"));
131 sstr_t threadpool = cfg_directivelist_get_str(
132 ls->directives,
133 sstr(
"Threadpool"));
134
135 CfgListener *listener =
OBJ_NEW_N(mp, CfgListener);
136
137 if(!name.ptr || !port.ptr || !vs.ptr) {
138
139 *error =
1;
140 listener->cfg_correct =
0;
141 }
else {
142 listener->cfg_correct =
1;
143 }
144
145 if(name.ptr) {
146 listener->name = sstrdup_a(mp, name);
147 }
148 if(port.ptr) {
149
150 sstr_t portdp = sstrdup(port);
151 listener->port = atoi(portdp.ptr);
152 free(portdp.ptr);
153 }
154 if(vs.ptr) {
155 listener->vs = sstrdup_a(mp, vs);
156 }
157 if(threadpool.ptr) {
158 listener->threadpool = sstrdup_a(mp, threadpool);
159 }
160
161 lslist = ucx_list_append_a(mp, lslist, listener);
162 }
163
164 return lslist;
165 }
166