added optional <zproxy> element to config file, containing any of these attribute...
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.18 2007-03-27 11:25:57 marc 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 struct conf_server *parse_server(xmlNode *node)
187 {
188     xmlNode *n;
189     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
190
191     r->host = 0;
192     r->port = 0;
193     r->proxy_host = 0;
194     r->proxy_port = 0;
195     r->myurl = 0;
196     r->zproxy_host = 0;
197     r->zproxy_port = 0;
198     r->service = 0;
199     r->next = 0;
200
201     for (n = node->children; n; n = n->next)
202     {
203         if (n->type != XML_ELEMENT_NODE)
204             continue;
205         if (!strcmp((const char *) n->name, "listen"))
206         {
207             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
208             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
209             if (port)
210                 r->port = atoi((const char *) port);
211             if (host)
212                 r->host = nmem_strdup(nmem, (const char *) host);
213             xmlFree(port);
214             xmlFree(host);
215         }
216         else if (!strcmp((const char *) n->name, "proxy"))
217         {
218             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
219             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
220             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
221             if (port)
222                 r->proxy_port = atoi((const char *) port);
223             if (host)
224                 r->proxy_host = nmem_strdup(nmem, (const char *) host);
225             if (myurl)
226                 r->myurl = nmem_strdup(nmem, (const char *) myurl);
227             else
228             {
229                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
230                 return 0;
231             }
232             xmlFree(port);
233             xmlFree(host);
234             xmlFree(myurl);
235         }
236         else if (!strcmp((const char *) n->name, "zproxy"))
237         {
238             xmlChar *port = 0;
239             xmlChar *host = 0;
240
241             port = xmlGetProp(n, (xmlChar *) "port");
242             host = xmlGetProp(n, (xmlChar *) "host");
243
244             if (port)
245                 r->zproxy_port = atoi((const char *) port);
246             if (host)
247                 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
248
249             xmlFree(port);
250             xmlFree(host);
251         }
252         else if (!strcmp((const char *) n->name, "service"))
253         {
254             struct conf_service *s = parse_service(n);
255             if (!s)
256                 return 0;
257             r->service = s;
258         }
259         else
260         {
261             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
262             return 0;
263         }
264     }
265     return r;
266 }
267
268 static xsltStylesheet *load_stylesheet(const char *fname)
269 {
270     char path[256];
271     sprintf(path, "%s/%s", confdir, fname);
272     return xsltParseStylesheetFile((xmlChar *) path);
273 }
274
275 static void setup_marc(struct conf_retrievalprofile *r)
276 {
277     yaz_iconv_t cm;
278     r->yaz_marc = yaz_marc_create();
279     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
280     {
281         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
282         return;
283     }
284     yaz_marc_iconv(r->yaz_marc, cm);
285 }
286
287 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
288 {
289     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
290     xmlNode *n;
291     struct conf_retrievalmap **rm = &r->maplist;
292
293     r->requestsyntax = 0;
294     r->native_syntax = Nativesyn_xml;
295     r->native_format = Nativeform_na;
296     r->native_encoding = 0;
297     r->native_mapto = Nativemapto_na;
298     r->yaz_marc = 0;
299     r->maplist = 0;
300     r->next = 0;
301
302     for (n = node->children; n; n = n->next)
303     {
304         if (n->type != XML_ELEMENT_NODE)
305             continue;
306         if (!strcmp((const char *) n->name, "requestsyntax"))
307         {
308             xmlChar *content = xmlNodeGetContent(n);
309             if (content)
310                 r->requestsyntax = nmem_strdup(nmem, (const char *) content);
311         }
312         else if (!strcmp((const char *) n->name, "nativesyntax"))
313         {
314             xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
315             xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
316             xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
317             xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
318             if (!name)
319             {
320                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
321                 return 0;
322             }
323             if (encoding)
324                 r->native_encoding = (char *) encoding;
325             if (!strcmp((const char *) name, "iso2709"))
326             {
327                 r->native_syntax = Nativesyn_iso2709;
328                 // Set a few defaults, too
329                 r->native_format = Nativeform_marc21;
330                 r->native_mapto = Nativemapto_marcxml;
331                 if (!r->native_encoding)
332                     r->native_encoding = "marc-8";
333                 setup_marc(r);
334             }
335             else if (!strcmp((const char *) name, "xml"))
336                 r->native_syntax = Nativesyn_xml;
337             else
338             {
339                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
340                 return 0;
341             }
342             if (format)
343             {
344                 if (!strcmp((const char *) format, "marc21") 
345                     || !strcmp((const char *) format, "usmarc"))
346                     r->native_format = Nativeform_marc21;
347                 else
348                 {
349                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
350                     return 0;
351                 }
352             }
353             if (mapto)
354             {
355                 if (!strcmp((const char *) mapto, "marcxml"))
356                     r->native_mapto = Nativemapto_marcxml;
357                 else if (!strcmp((const char *)mapto, "marcxchange"))
358                     r->native_mapto = Nativemapto_marcxchange;
359                 else
360                 {
361                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
362                     return 0;
363                 }
364             }
365             xmlFree(name);
366             xmlFree(format);
367             xmlFree(encoding);
368             xmlFree(mapto);
369         }
370         else if (!strcmp((const char *) n->name, "map"))
371         {
372             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
373             xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
374             xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
375             xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
376             xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
377             memset(m, 0, sizeof(*m));
378             if (type)
379             {
380                 if (!strcmp((const char *) type, "xslt"))
381                     m->type = Map_xslt;
382                 else
383                 {
384                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
385                     return 0;
386                 }
387             }
388             if (charset)
389                 m->charset = nmem_strdup(nmem, (const char *) charset);
390             if (format)
391                 m->format = nmem_strdup(nmem, (const char *) format);
392             if (stylesheet)
393             {
394                 if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
395                     return 0;
396             }
397             *rm = m;
398             rm = &m->next;
399             xmlFree(type);
400             xmlFree(charset);
401             xmlFree(format);
402             xmlFree(stylesheet);
403         }
404         else
405         {
406             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
407             return 0;
408         }
409     }
410
411     return r;
412 }
413
414 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
415 {
416     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
417     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
418     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
419
420     memset(r, 0, sizeof(*r));
421
422     if (type)
423     {
424         if (!strcmp((const char *) type, "local"))
425             r->type = Targetprofiles_local;
426         else
427         {
428             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
429             return 0;
430         }
431     }
432     else
433     {
434         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
435         return 0;
436     }
437
438     if (src)
439         r->src = nmem_strdup(nmem, (const char *) src);
440     else
441     {
442         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
443         return 0;
444     }
445     xmlFree(type);
446     xmlFree(src);
447     return r;
448 }
449
450 static struct conf_config *parse_config(xmlNode *root)
451 {
452     xmlNode *n;
453     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
454     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
455
456     r->servers = 0;
457     r->queryprofiles = 0;
458     r->retrievalprofiles = 0;
459     r->targetprofiles = 0;
460
461     for (n = root->children; n; n = n->next)
462     {
463         if (n->type != XML_ELEMENT_NODE)
464             continue;
465         if (!strcmp((const char *) n->name, "server"))
466         {
467             struct conf_server *tmp = parse_server(n);
468             if (!tmp)
469                 return 0;
470             tmp->next = r->servers;
471             r->servers = tmp;
472         }
473         else if (!strcmp((const char *) n->name, "queryprofile"))
474         {
475         }
476         else if (!strcmp((const char *) n->name, "retrievalprofile"))
477         {
478             if (!(*rp = parse_retrievalprofile(n)))
479                 return 0;
480             rp = &(*rp)->next;
481         }
482         else if (!strcmp((const char *) n->name, "targetprofiles"))
483         {
484             // It would be fun to be able to fix this sometime
485             if (r->targetprofiles)
486             {
487                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
488                 return 0;
489             }
490             if (!(r->targetprofiles = parse_targetprofiles(n)))
491                 return 0;
492         }
493         else
494         {
495             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
496             return 0;
497         }
498     }
499     return r;
500 }
501
502 int read_config(const char *fname)
503 {
504     xmlDoc *doc = xmlParseFile(fname);
505     const char *p;
506
507     if (!nmem)  // Initialize
508     {
509         nmem = nmem_create();
510         xmlSubstituteEntitiesDefault(1);
511         xmlLoadExtDtdDefaultValue = 1;
512     }
513     if (!doc)
514     {
515         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
516         exit(1);
517     }
518     if ((p = strrchr(fname, '/')))
519     {
520         int len = p - fname;
521         strncpy(confdir, fname, len);
522         confdir[len] = '\0';
523     }
524     config = parse_config(xmlDocGetRootElement(doc));
525     xmlFreeDoc(doc);
526
527     if (config)
528         return 1;
529     else
530         return 0;
531 }
532
533
534 /*
535  * Local variables:
536  * c-basic-offset: 4
537  * indent-tabs-mode: nil
538  * End:
539  * vim: shiftwidth=4 tabstop=8 expandtab
540  */