Basic configuration functionality in place (not yet used)
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.2 2006-12-27 21:11:10 quinn Exp $ */
2
3 #include <string.h>
4
5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
7
8 #include <yaz/yaz-util.h>
9 #include <yaz/nmem.h>
10
11 #define CONFIG_NOEXTERNS
12 #include "config.h"
13
14 static NMEM nmem = 0;
15
16 struct conf_config *config = 0;
17
18 static struct conf_service *parse_service(xmlNode *node)
19 {
20     xmlNode *n;
21     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
22
23     r->termlists = 0;
24
25     for (n = node->children; n; n = n->next)
26     {
27         if (n->type != XML_ELEMENT_NODE)
28             continue;
29         if (!strcmp(n->name, "termlist"))
30         {
31             struct conf_termlist *tl = nmem_malloc(nmem, sizeof(struct conf_termlist));
32             xmlChar *name = xmlGetProp(n, "name");
33             if (!name)
34             {
35                 yaz_log(YLOG_WARN, "Missing name attribute in termlist");
36                 continue;
37             }
38             tl->name = nmem_strdup(nmem, name);
39             tl->next = r->termlists;
40             r->termlists = tl;
41         }
42         else
43         {
44             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
45             return 0;
46         }
47     }
48     return r;
49 }
50
51 static struct conf_server *parse_server(xmlNode *node)
52 {
53     xmlNode *n;
54     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
55
56     r->host = 0;
57     r->port = 0;
58     r->proxy_host = 0;
59     r->proxy_port = 0;
60     r->service = 0;
61     r->next = 0;
62
63     for (n = node->children; n; n = n->next)
64     {
65         if (n->type != XML_ELEMENT_NODE)
66             continue;
67         if (!strcmp(n->name, "listen"))
68         {
69             xmlChar *port = xmlGetProp(n, "port");
70             xmlChar *host = xmlGetProp(n, "host");
71             if (port)
72                 r->port = atoi(port);
73             if (host)
74                 r->host = nmem_strdup(nmem, host);
75         }
76         else if (!strcmp(n->name, "proxy"))
77         {
78             xmlChar *port = xmlGetProp(n, "port");
79             xmlChar *host = xmlGetProp(n, "host");
80             if (port)
81                 r->proxy_port = atoi(port);
82             if (host)
83                 r->proxy_host = nmem_strdup(nmem, host);
84         }
85         else if (!strcmp(n->name, "service"))
86         {
87             struct conf_service *s = parse_service(n);
88             if (!s)
89                 return 0;
90             r->service = s;
91         }
92         else
93         {
94             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
95             return 0;
96         }
97     }
98     return r;
99 }
100
101 static struct conf_config *parse_config(xmlNode *root)
102 {
103     xmlNode *n;
104     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
105
106     r->servers = 0;
107     r->queryprofiles = 0;
108     r->retrievalprofiles = 0;
109
110     for (n = root->children; n; n = n->next)
111     {
112         if (n->type != XML_ELEMENT_NODE)
113             continue;
114         if (!strcmp(n->name, "server"))
115         {
116             struct conf_server *tmp = parse_server(n);
117             if (!tmp)
118                 return 0;
119             tmp->next = r->servers;
120             r->servers = tmp;
121         }
122         else if (!strcmp(n->name, "queryprofile"))
123         {
124         }
125         else if (!strcmp(n->name, "retrievalprofile"))
126         {
127         }
128         else
129         {
130             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
131             return 0;
132         }
133     }
134     return r;
135 }
136
137 int read_config(const char *fname)
138 {
139     xmlDoc *doc = xmlReadFile(fname, NULL, 0);
140     if (!nmem)
141         nmem = nmem_create();
142     if (!doc)
143     {
144         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
145         exit(1);
146     }
147     if ((config = parse_config(xmlDocGetRootElement(doc))))
148         return 1;
149     else
150         return 0;
151 }
152
153
154 /*
155  * Local variables:
156  * c-basic-offset: 4
157  * indent-tabs-mode: nil
158  * End:
159  * vim: shiftwidth=4 tabstop=8 expandtab
160  */