ee9c3b2fbc411c90a2ef914fbefd716e84b4128f
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.22 2007-04-08 20:52:09 quinn Exp $ */
2
3 #include <string.h>
4
5 #include <libxml/parser.h>
6 #include <libxml/tree.h>
7 #include <libxslt/xslt.h>
8 #include <libxslt/transform.h>
9 #include <libxslt/xsltutils.h>
10
11 #if HAVE_CONFIG_H
12 #include <cconfig.h>
13 #endif
14
15 #include <yaz/yaz-util.h>
16 #include <yaz/nmem.h>
17
18 #define CONFIG_NOEXTERNS
19 #include "config.h"
20
21 static NMEM nmem = 0;
22 static char confdir[256] = ".";
23
24 struct conf_config *config = 0;
25
26 /* Code to parse configuration file */
27 /* ==================================================== */
28
29 static struct conf_service *parse_service(xmlNode *node)
30 {
31     xmlNode *n;
32     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
33     int md_node = 0;
34     int sk_node = 0;
35
36     r->num_sortkeys = r->num_metadata = 0;
37     // Allocate array of conf metadata and sortkey tructs, if necessary
38     for (n = node->children; n; n = n->next)
39         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
40                                                    n->name, "metadata"))
41         {
42             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
43             r->num_metadata++;
44             if (sortkey && strcmp((const char *) sortkey, "no"))
45                 r->num_sortkeys++;
46             xmlFree(sortkey);
47         }
48     if (r->num_metadata)
49         r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
50     else
51         r->metadata = 0;
52     if (r->num_sortkeys)
53         r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
54     else
55         r->sortkeys = 0;
56
57     for (n = node->children; n; n = n->next)
58     {
59         if (n->type != XML_ELEMENT_NODE)
60             continue;
61         if (!strcmp((const char *) n->name, (const char *) "metadata"))
62         {
63             struct conf_metadata *md = &r->metadata[md_node];
64             xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
65             xmlChar *brief = xmlGetProp(n, (xmlChar *) "brief");
66             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
67             xmlChar *merge = xmlGetProp(n, (xmlChar *) "merge");
68             xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
69             xmlChar *termlist = xmlGetProp(n, (xmlChar *) "termlist");
70             xmlChar *rank = xmlGetProp(n, (xmlChar *) "rank");
71
72             if (!name)
73             {
74                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
75                 return 0;
76             }
77             md->name = nmem_strdup(nmem, (const char *) name);
78             if (brief)
79             {
80                 if (!strcmp((const char *) brief, "yes"))
81                     md->brief = 1;
82                 else if (strcmp((const char *) brief, "no"))
83                 {
84                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
85                     return 0;
86                 }
87             }
88             else
89                 md->brief = 0;
90
91             if (termlist)
92             {
93                 if (!strcmp((const char *) termlist, "yes"))
94                     md->termlist = 1;
95                 else if (strcmp((const char *) termlist, "no"))
96                 {
97                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
98                     return 0;
99                 }
100             }
101             else
102                 md->termlist = 0;
103
104             if (rank)
105                 md->rank = atoi((const char *) rank);
106             else
107                 md->rank = 0;
108
109             if (type)
110             {
111                 if (!strcmp((const char *) type, "generic"))
112                     md->type = Metadata_type_generic;
113                 else if (!strcmp((const char *) type, "year"))
114                     md->type = Metadata_type_year;
115                 else
116                 {
117                     yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
118                     return 0;
119                 }
120             }
121             else
122                 md->type = Metadata_type_generic;
123
124             if (merge)
125             {
126                 if (!strcmp((const char *) merge, "no"))
127                     md->merge = Metadata_merge_no;
128                 else if (!strcmp((const char *) merge, "unique"))
129                     md->merge = Metadata_merge_unique;
130                 else if (!strcmp((const char *) merge, "longest"))
131                     md->merge = Metadata_merge_longest;
132                 else if (!strcmp((const char *) merge, "range"))
133                     md->merge = Metadata_merge_range;
134                 else if (!strcmp((const char *) merge, "all"))
135                     md->merge = Metadata_merge_all;
136                 else
137                 {
138                     yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
139                     return 0;
140                 }
141             }
142             else
143                 md->merge = Metadata_merge_no;
144
145             if (sortkey && strcmp((const char *) sortkey, "no"))
146             {
147                 struct conf_sortkey *sk = &r->sortkeys[sk_node];
148                 if (md->merge == Metadata_merge_no)
149                 {
150                     yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
151                     return 0;
152                 }
153                 if (!strcmp((const char *) sortkey, "numeric"))
154                     sk->type = Metadata_sortkey_numeric;
155                 else if (!strcmp((const char *) sortkey, "skiparticle"))
156                     sk->type = Metadata_sortkey_skiparticle;
157                 else
158                 {
159                     yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
160                     return 0;
161                 }
162                 sk->name = md->name;
163                 md->sortkey_offset = sk_node;
164                 sk_node++;
165             }
166             else
167                 md->sortkey_offset = -1;
168
169             xmlFree(name);
170             xmlFree(brief);
171             xmlFree(sortkey);
172             xmlFree(merge);
173             xmlFree(termlist);
174             xmlFree(rank);
175             md_node++;
176         }
177         else
178         {
179             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
180             return 0;
181         }
182     }
183     return r;
184 }
185
186 static char *parse_settings(xmlNode *node)
187 {
188     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
189     char *r;
190
191     if (src)
192         r = nmem_strdup(nmem, (const char *) src);
193     else
194     {
195         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
196         return 0;
197     }
198     xmlFree(src);
199     return r;
200 }
201
202 static struct conf_server *parse_server(xmlNode *node)
203 {
204     xmlNode *n;
205     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
206
207     r->host = 0;
208     r->port = 0;
209     r->proxy_host = 0;
210     r->proxy_port = 0;
211     r->myurl = 0;
212     r->zproxy_host = 0;
213     r->zproxy_port = 0;
214     r->service = 0;
215     r->next = 0;
216     r->settings = 0;
217
218     for (n = node->children; n; n = n->next)
219     {
220         if (n->type != XML_ELEMENT_NODE)
221             continue;
222         if (!strcmp((const char *) n->name, "listen"))
223         {
224             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
225             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
226             if (port)
227                 r->port = atoi((const char *) port);
228             if (host)
229                 r->host = nmem_strdup(nmem, (const char *) host);
230             xmlFree(port);
231             xmlFree(host);
232         }
233         else if (!strcmp((const char *) n->name, "proxy"))
234         {
235             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
236             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
237             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
238             if (port)
239                 r->proxy_port = atoi((const char *) port);
240             if (host)
241                 r->proxy_host = nmem_strdup(nmem, (const char *) host);
242             if (myurl)
243                 r->myurl = nmem_strdup(nmem, (const char *) myurl);
244 #ifdef GAGA
245             else
246             {
247                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
248                 return 0;
249             }
250 #endif
251             xmlFree(port);
252             xmlFree(host);
253             xmlFree(myurl);
254         }
255         else if (!strcmp((const char *) n->name, "zproxy"))
256         {
257             xmlChar *port = 0;
258             xmlChar *host = 0;
259
260             port = xmlGetProp(n, (xmlChar *) "port");
261             host = xmlGetProp(n, (xmlChar *) "host");
262
263             if (port)
264                 r->zproxy_port = atoi((const char *) port);
265             if (host)
266                 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
267
268             xmlFree(port);
269             xmlFree(host);
270         }
271         else if (!strcmp((const char *) n->name, "settings"))
272         {
273             if (r->settings)
274             {
275                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
276                 return 0;
277             }
278             if (!(r->settings = parse_settings(n)))
279                 return 0;
280         }
281         else if (!strcmp((const char *) n->name, "service"))
282         {
283             struct conf_service *s = parse_service(n);
284             if (!s)
285                 return 0;
286             r->service = s;
287         }
288         else
289         {
290             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
291             return 0;
292         }
293     }
294     return r;
295 }
296
297 xsltStylesheet *conf_load_stylesheet(const char *fname)
298 {
299     char path[256];
300     sprintf(path, "%s/%s", confdir, fname);
301     return xsltParseStylesheetFile((xmlChar *) path);
302 }
303
304 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
305 {
306     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
307     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
308     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
309
310     memset(r, 0, sizeof(*r));
311
312     if (type)
313     {
314         if (!strcmp((const char *) type, "local"))
315             r->type = Targetprofiles_local;
316         else
317         {
318             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
319             return 0;
320         }
321     }
322     else
323     {
324         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
325         return 0;
326     }
327
328     if (src)
329         r->src = nmem_strdup(nmem, (const char *) src);
330     else
331     {
332         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
333         return 0;
334     }
335     xmlFree(type);
336     xmlFree(src);
337     return r;
338 }
339
340 static struct conf_config *parse_config(xmlNode *root)
341 {
342     xmlNode *n;
343     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
344
345     r->servers = 0;
346     r->targetprofiles = 0;
347
348     for (n = root->children; n; n = n->next)
349     {
350         if (n->type != XML_ELEMENT_NODE)
351             continue;
352         if (!strcmp((const char *) n->name, "server"))
353         {
354             struct conf_server *tmp = parse_server(n);
355             if (!tmp)
356                 return 0;
357             tmp->next = r->servers;
358             r->servers = tmp;
359         }
360         else if (!strcmp((const char *) n->name, "targetprofiles"))
361         {
362             // It would be fun to be able to fix this sometime
363             if (r->targetprofiles)
364             {
365                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
366                 return 0;
367             }
368             if (!(r->targetprofiles = parse_targetprofiles(n)))
369                 return 0;
370         }
371         else
372         {
373             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
374             return 0;
375         }
376     }
377     return r;
378 }
379
380 int read_config(const char *fname)
381 {
382     xmlDoc *doc = xmlParseFile(fname);
383     const char *p;
384
385     if (!nmem)  // Initialize
386     {
387         nmem = nmem_create();
388         xmlSubstituteEntitiesDefault(1);
389         xmlLoadExtDtdDefaultValue = 1;
390     }
391     if (!doc)
392     {
393         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
394         exit(1);
395     }
396     if ((p = strrchr(fname, '/')))
397     {
398         int len = p - fname;
399         strncpy(confdir, fname, len);
400         confdir[len] = '\0';
401     }
402     config = parse_config(xmlDocGetRootElement(doc));
403     xmlFreeDoc(doc);
404
405     if (config)
406         return 1;
407     else
408         return 0;
409 }
410
411
412 /*
413  * Local variables:
414  * c-basic-offset: 4
415  * indent-tabs-mode: nil
416  * End:
417  * vim: shiftwidth=4 tabstop=8 expandtab
418  */