Use strrchr rather than rindex (obsolete)
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.9 2007-01-10 10:15:23 adam 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 num_metadata = 0;
34     int md_node = 0;
35
36     // Allocate array of conf metadata structs, if necessary
37     for (n = node->children; n; n = n->next)
38         if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, "metadata"))
39             num_metadata++;
40     if (num_metadata)
41         r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * num_metadata);
42     r->num_metadata = num_metadata;
43
44     for (n = node->children; n; n = n->next)
45     {
46         if (n->type != XML_ELEMENT_NODE)
47             continue;
48         if (!strcmp(n->name, "metadata"))
49         {
50             struct conf_metadata *md = &r->metadata[md_node];
51             xmlChar *name = xmlGetProp(n, "name");
52             xmlChar *brief = xmlGetProp(n, "brief");
53             xmlChar *sortkey = xmlGetProp(n, "sortkey");
54             xmlChar *merge = xmlGetProp(n, "merge");
55             xmlChar *type = xmlGetProp(n, "type");
56             xmlChar *termlist = xmlGetProp(n, "termlist");
57             xmlChar *rank = xmlGetProp(n, "rank");
58
59             if (!name)
60             {
61                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
62                 return 0;
63             }
64             md->name = nmem_strdup(nmem, name);
65             if (brief)
66             {
67                 if (!strcmp(brief, "yes"))
68                     md->brief = 1;
69                 else if (strcmp(brief, "no"))
70                 {
71                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
72                     return 0;
73                 }
74             }
75             else
76                 md->brief = 0;
77
78             if (termlist)
79             {
80                 if (!strcmp(termlist, "yes"))
81                     md->termlist = 1;
82                 else if (strcmp(termlist, "no"))
83                 {
84                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
85                     return 0;
86                 }
87             }
88             else
89                 md->termlist = 0;
90
91             if (rank)
92                 md->rank = atoi(rank);
93             else
94                 md->rank = 0;
95
96             if (type)
97             {
98                 if (!strcmp(type, "generic"))
99                     md->type = Metadata_type_generic;
100                 else if (!strcmp(type, "integer"))
101                     md->type = Metadata_type_integer;
102                 else if (!strcmp(type, "year"))
103                     md->type = Metadata_type_year;
104                 else
105                 {
106                     yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
107                     return 0;
108                 }
109             }
110             else
111                 md->type = Metadata_type_generic;
112
113             if (sortkey)
114             {
115                 if (!strcmp(sortkey, "no"))
116                     md->sortkey = Metadata_sortkey_no;
117                 else if (!strcmp(sortkey, "numeric"))
118                     md->sortkey = Metadata_sortkey_numeric;
119                 else if (!strcmp(sortkey, "range"))
120                     md->sortkey = Metadata_sortkey_range;
121                 else if (!strcmp(sortkey, "skiparticle"))
122                     md->sortkey = Metadata_sortkey_skiparticle;
123                 else
124                 {
125                     yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
126                     return 0;
127                 }
128             }
129             else
130                 md->sortkey = Metadata_sortkey_no;
131
132             if (merge)
133             {
134                 if (!strcmp(merge, "no"))
135                     md->merge = Metadata_merge_no;
136                 else if (!strcmp(merge, "unique"))
137                     md->merge = Metadata_merge_unique;
138                 else if (!strcmp(merge, "longest"))
139                     md->merge = Metadata_merge_longest;
140                 else if (!strcmp(merge, "range"))
141                     md->merge = Metadata_merge_range;
142                 else if (!strcmp(merge, "all"))
143                     md->merge = Metadata_merge_all;
144                 else
145                 {
146                     yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
147                     return 0;
148                 }
149             }
150             else
151                 md->merge = Metadata_merge_no;
152
153             xmlFree(name);
154             xmlFree(brief);
155             xmlFree(sortkey);
156             xmlFree(merge);
157             xmlFree(termlist);
158             xmlFree(rank);
159             md_node++;
160         }
161         else
162         {
163             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
164             return 0;
165         }
166     }
167     return r;
168 }
169
170 static struct conf_server *parse_server(xmlNode *node)
171 {
172     xmlNode *n;
173     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
174
175     r->host = 0;
176     r->port = 0;
177     r->proxy_host = 0;
178     r->proxy_port = 0;
179     r->service = 0;
180     r->next = 0;
181
182     for (n = node->children; n; n = n->next)
183     {
184         if (n->type != XML_ELEMENT_NODE)
185             continue;
186         if (!strcmp(n->name, "listen"))
187         {
188             xmlChar *port = xmlGetProp(n, "port");
189             xmlChar *host = xmlGetProp(n, "host");
190             if (port)
191                 r->port = atoi(port);
192             if (host)
193                 r->host = nmem_strdup(nmem, host);
194             xmlFree(port);
195             xmlFree(host);
196         }
197         else if (!strcmp(n->name, "proxy"))
198         {
199             xmlChar *port = xmlGetProp(n, "port");
200             xmlChar *host = xmlGetProp(n, "host");
201             if (port)
202                 r->proxy_port = atoi(port);
203             if (host)
204                 r->proxy_host = nmem_strdup(nmem, host);
205             xmlFree(port);
206             xmlFree(host);
207         }
208         else if (!strcmp(n->name, "service"))
209         {
210             struct conf_service *s = parse_service(n);
211             if (!s)
212                 return 0;
213             r->service = s;
214         }
215         else
216         {
217             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
218             return 0;
219         }
220     }
221     return r;
222 }
223
224 static xsltStylesheet *load_stylesheet(const char *fname)
225 {
226     char path[256];
227     sprintf(path, "%s/%s", confdir, fname);
228     return xsltParseStylesheetFile(path);
229 }
230
231 static void setup_marc(struct conf_retrievalprofile *r)
232 {
233     yaz_iconv_t cm;
234     r->yaz_marc = yaz_marc_create();
235     if (!(cm = yaz_iconv_open("utf-8", r->native_encoding)))
236     {
237         yaz_log(YLOG_WARN, "Unable to support mapping from %s", r->native_encoding);
238         return;
239     }
240     yaz_marc_iconv(r->yaz_marc, cm);
241 }
242
243 static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
244 {
245     struct conf_retrievalprofile *r = nmem_malloc(nmem, sizeof(struct conf_retrievalprofile));
246     xmlNode *n;
247     struct conf_retrievalmap **rm = &r->maplist;
248
249     r->requestsyntax = 0;
250     r->native_syntax = Nativesyn_xml;
251     r->native_format = Nativeform_na;
252     r->native_encoding = 0;
253     r->native_mapto = Nativemapto_na;
254     r->yaz_marc = 0;
255     r->maplist = 0;
256     r->next = 0;
257
258     for (n = node->children; n; n = n->next)
259     {
260         if (n->type != XML_ELEMENT_NODE)
261             continue;
262         if (!strcmp(n->name, "requestsyntax"))
263         {
264             xmlChar *content = xmlNodeGetContent(n);
265             if (content)
266                 r->requestsyntax = nmem_strdup(nmem, content);
267         }
268         else if (!strcmp(n->name, "nativesyntax"))
269         {
270             xmlChar *name = xmlGetProp(n, "name");
271             xmlChar *format = xmlGetProp(n, "format");
272             xmlChar *encoding = xmlGetProp(n, "encoding");
273             xmlChar *mapto = xmlGetProp(n, "mapto");
274             if (!name)
275             {
276                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
277                 return 0;
278             }
279             if (!strcmp(name, "iso2709"))
280             {
281                 r->native_syntax = Nativesyn_iso2709;
282                 // Set a few defaults, too
283                 r->native_format = Nativeform_marc21;
284                 r->native_mapto = Nativemapto_marcxml;
285                 r->native_encoding = "marc-8";
286                 setup_marc(r);
287             }
288             else if (!strcmp(name, "xml"))
289                 r->native_syntax = Nativesyn_xml;
290             else
291             {
292                 yaz_log(YLOG_WARN, "Unknown native syntax name %s", name);
293                 return 0;
294             }
295             if (format)
296             {
297                 if (!strcmp(format, "marc21") || !strcmp(format, "usmarc"))
298                     r->native_format = Nativeform_marc21;
299                 else
300                 {
301                     yaz_log(YLOG_WARN, "Unknown native format name %s", format);
302                     return 0;
303                 }
304             }
305             if (encoding)
306                 r->native_encoding = encoding;
307             if (mapto)
308             {
309                 if (!strcmp(mapto, "marcxml"))
310                     r->native_mapto = Nativemapto_marcxml;
311                 else if (!strcmp(mapto, "marcxchange"))
312                     r->native_mapto = Nativemapto_marcxchange;
313                 else
314                 {
315                     yaz_log(YLOG_WARN, "Unknown mapto target %s", format);
316                     return 0;
317                 }
318             }
319             xmlFree(name);
320             xmlFree(format);
321             xmlFree(encoding);
322             xmlFree(mapto);
323         }
324         else if (!strcmp(n->name, "map"))
325         {
326             struct conf_retrievalmap *m = nmem_malloc(nmem, sizeof(struct conf_retrievalmap));
327             xmlChar *type = xmlGetProp(n, "type");
328             xmlChar *charset = xmlGetProp(n, "charset");
329             xmlChar *format = xmlGetProp(n, "format");
330             xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
331             memset(m, 0, sizeof(*m));
332             if (type)
333             {
334                 if (!strcmp(type, "xslt"))
335                     m->type = Map_xslt;
336                 else
337                 {
338                     yaz_log(YLOG_WARN, "Unknown map type: %s", type);
339                     return 0;
340                 }
341             }
342             if (charset)
343                 m->charset = nmem_strdup(nmem, charset);
344             if (format)
345                 m->format = nmem_strdup(nmem, format);
346             if (stylesheet)
347             {
348                 if (!(m->stylesheet = load_stylesheet(stylesheet)))
349                     return 0;
350             }
351             *rm = m;
352             rm = &m->next;
353             xmlFree(type);
354             xmlFree(charset);
355             xmlFree(format);
356             xmlFree(stylesheet);
357         }
358         else
359         {
360             yaz_log(YLOG_FATAL, "Bad element in retrievalprofile: %s", n->name);
361             return 0;
362         }
363     }
364
365     return r;
366 }
367
368 static struct conf_config *parse_config(xmlNode *root)
369 {
370     xmlNode *n;
371     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
372     struct conf_retrievalprofile **rp = &r->retrievalprofiles;
373
374     r->servers = 0;
375     r->queryprofiles = 0;
376     r->retrievalprofiles = 0;
377
378     for (n = root->children; n; n = n->next)
379     {
380         if (n->type != XML_ELEMENT_NODE)
381             continue;
382         if (!strcmp(n->name, "server"))
383         {
384             struct conf_server *tmp = parse_server(n);
385             if (!tmp)
386                 return 0;
387             tmp->next = r->servers;
388             r->servers = tmp;
389         }
390         else if (!strcmp(n->name, "queryprofile"))
391         {
392         }
393         else if (!strcmp(n->name, "retrievalprofile"))
394         {
395             if (!(*rp = parse_retrievalprofile(n)))
396                 return 0;
397             rp = &(*rp)->next;
398         }
399         else
400         {
401             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
402             return 0;
403         }
404     }
405     return r;
406 }
407
408 int read_config(const char *fname)
409 {
410     xmlDoc *doc = xmlReadFile(fname, NULL, 0);
411     const char *p;
412
413     if (!nmem)  // Initialize
414     {
415         nmem = nmem_create();
416         xmlSubstituteEntitiesDefault(1);
417         xmlLoadExtDtdDefaultValue = 1;
418     }
419     if (!doc)
420     {
421         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
422         exit(1);
423     }
424     if ((p = strrchr(fname, '/')))
425     {
426         int len = p - fname;
427         strncpy(confdir, fname, len);
428         confdir[len] = '\0';
429     }
430     config = parse_config(xmlDocGetRootElement(doc));
431     xmlFreeDoc(doc);
432
433     if (config)
434         return 1;
435     else
436         return 0;
437 }
438
439
440 /*
441  * Local variables:
442  * c-basic-offset: 4
443  * indent-tabs-mode: nil
444  * End:
445  * vim: shiftwidth=4 tabstop=8 expandtab
446  */