Functional settings system. At this point, they control the CCL map only
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.19 2007-03-30 02:45:07 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 static xsltStylesheet *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 void setup_marc(struct conf_retrievalprofile *r)
305 {
306     yaz_iconv_t cm;
307     r->yaz_marc = yaz_marc_create();
308     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
309     {
310         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
311         return;
312     }
313     yaz_marc_iconv(r->yaz_marc, cm);
314 }
315
316 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
317 {
318     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
319     xmlNode *n;
320     struct conf_retrievalmap **rm = &r->maplist;
321
322     r->requestsyntax = 0;
323     r->native_syntax = Nativesyn_xml;
324     r->native_format = Nativeform_na;
325     r->native_encoding = 0;
326     r->native_mapto = Nativemapto_na;
327     r->yaz_marc = 0;
328     r->maplist = 0;
329     r->next = 0;
330
331     for (n = node->children; n; n = n->next)
332     {
333         if (n->type != XML_ELEMENT_NODE)
334             continue;
335         if (!strcmp((const char *) n->name, "requestsyntax"))
336         {
337             xmlChar *content = xmlNodeGetContent(n);
338             if (content)
339                 r->requestsyntax = nmem_strdup(nmem, (const char *) content);
340         }
341         else if (!strcmp((const char *) n->name, "nativesyntax"))
342         {
343             xmlChar *name = xmlGetProp(n, (xmlChar *) "name");
344             xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
345             xmlChar *encoding = xmlGetProp(n, (xmlChar *) "encoding");
346             xmlChar *mapto = xmlGetProp(n, (xmlChar *) "mapto");
347             if (!name)
348             {
349                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
350                 return 0;
351             }
352             if (encoding)
353                 r->native_encoding = (char *) encoding;
354             if (!strcmp((const char *) name, "iso2709"))
355             {
356                 r->native_syntax = Nativesyn_iso2709;
357                 // Set a few defaults, too
358                 r->native_format = Nativeform_marc21;
359                 r->native_mapto = Nativemapto_marcxml;
360                 if (!r->native_encoding)
361                     r->native_encoding = "marc-8";
362                 setup_marc(r);
363             }
364             else if (!strcmp((const char *) name, "xml"))
365                 r->native_syntax = Nativesyn_xml;
366             else
367             {
368                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
369                 return 0;
370             }
371             if (format)
372             {
373                 if (!strcmp((const char *) format, "marc21") 
374                     || !strcmp((const char *) format, "usmarc"))
375                     r->native_format = Nativeform_marc21;
376                 else
377                 {
378                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
379                     return 0;
380                 }
381             }
382             if (mapto)
383             {
384                 if (!strcmp((const char *) mapto, "marcxml"))
385                     r->native_mapto = Nativemapto_marcxml;
386                 else if (!strcmp((const char *)mapto, "marcxchange"))
387                     r->native_mapto = Nativemapto_marcxchange;
388                 else
389                 {
390                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
391                     return 0;
392                 }
393             }
394             xmlFree(name);
395             xmlFree(format);
396             xmlFree(encoding);
397             xmlFree(mapto);
398         }
399         else if (!strcmp((const char *) n->name, "map"))
400         {
401             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
402             xmlChar *type = xmlGetProp(n, (xmlChar *) "type");
403             xmlChar *charset = xmlGetProp(n, (xmlChar *) "charset");
404             xmlChar *format = xmlGetProp(n, (xmlChar *) "format");
405             xmlChar *stylesheet = xmlGetProp(n, (xmlChar *) "stylesheet");
406             memset(m, 0, sizeof(*m));
407             if (type)
408             {
409                 if (!strcmp((const char *) type, "xslt"))
410                     m->type = Map_xslt;
411                 else
412                 {
413                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
414                     return 0;
415                 }
416             }
417             if (charset)
418                 m->charset = nmem_strdup(nmem, (const char *) charset);
419             if (format)
420                 m->format = nmem_strdup(nmem, (const char *) format);
421             if (stylesheet)
422             {
423                 if (!(m->stylesheet = load_stylesheet((char *) stylesheet)))
424                     return 0;
425             }
426             *rm = m;
427             rm = &m->next;
428             xmlFree(type);
429             xmlFree(charset);
430             xmlFree(format);
431             xmlFree(stylesheet);
432         }
433         else
434         {
435             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
436             return 0;
437         }
438     }
439
440     return r;
441 }
442
443 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
444 {
445     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
446     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
447     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
448
449     memset(r, 0, sizeof(*r));
450
451     if (type)
452     {
453         if (!strcmp((const char *) type, "local"))
454             r->type = Targetprofiles_local;
455         else
456         {
457             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
458             return 0;
459         }
460     }
461     else
462     {
463         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
464         return 0;
465     }
466
467     if (src)
468         r->src = nmem_strdup(nmem, (const char *) src);
469     else
470     {
471         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
472         return 0;
473     }
474     xmlFree(type);
475     xmlFree(src);
476     return r;
477 }
478
479 static struct conf_config *parse_config(xmlNode *root)
480 {
481     xmlNode *n;
482     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
483     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
484
485     r->servers = 0;
486     r->retrievalprofiles = 0;
487     r->targetprofiles = 0;
488
489     for (n = root->children; n; n = n->next)
490     {
491         if (n->type != XML_ELEMENT_NODE)
492             continue;
493         if (!strcmp((const char *) n->name, "server"))
494         {
495             struct conf_server *tmp = parse_server(n);
496             if (!tmp)
497                 return 0;
498             tmp->next = r->servers;
499             r->servers = tmp;
500         }
501         else if (!strcmp((const char *) n->name, "retrievalprofile"))
502         {
503             if (!(*rp = parse_retrievalprofile(n)))
504                 return 0;
505             rp = &(*rp)->next;
506         }
507         else if (!strcmp((const char *) n->name, "targetprofiles"))
508         {
509             // It would be fun to be able to fix this sometime
510             if (r->targetprofiles)
511             {
512                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
513                 return 0;
514             }
515             if (!(r->targetprofiles = parse_targetprofiles(n)))
516                 return 0;
517         }
518         else
519         {
520             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
521             return 0;
522         }
523     }
524     return r;
525 }
526
527 int read_config(const char *fname)
528 {
529     xmlDoc *doc = xmlParseFile(fname);
530     const char *p;
531
532     if (!nmem)  // Initialize
533     {
534         nmem = nmem_create();
535         xmlSubstituteEntitiesDefault(1);
536         xmlLoadExtDtdDefaultValue = 1;
537     }
538     if (!doc)
539     {
540         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
541         exit(1);
542     }
543     if ((p = strrchr(fname, '/')))
544     {
545         int len = p - fname;
546         strncpy(confdir, fname, len);
547         confdir[len] = '\0';
548     }
549     config = parse_config(xmlDocGetRootElement(doc));
550     xmlFreeDoc(doc);
551
552     if (config)
553         return 1;
554     else
555         return 0;
556 }
557
558
559 /*
560  * Local variables:
561  * c-basic-offset: 4
562  * indent-tabs-mode: nil
563  * End:
564  * vim: shiftwidth=4 tabstop=8 expandtab
565  */