Same fname scheme for mmap+xslt
[pazpar2-moved-to-github.git] / src / pazpar2_config.c
1 /* This file is part of Pazpar2.
2    Copyright (C) 2006-2009 Index Data
3
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <string.h>
25 #include <assert.h>
26
27 #include <libxml/parser.h>
28 #include <libxml/tree.h>
29
30 #include <yaz/yaz-util.h>
31 #include <yaz/nmem.h>
32 #include <yaz/snprintf.h>
33 #include <yaz/tpath.h>
34
35 #if HAVE_GLOB_H
36 #define USE_POSIX_GLOB 1
37 #else
38 #define USE_POSIX_GLOB 0
39 #endif
40
41
42 #if USE_POSIX_GLOB
43 #include <glob.h>
44 #endif
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #if HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #include "pazpar2_config.h"
51 #include "settings.h"
52 #include "eventl.h"
53 #include "http.h"
54
55 struct conf_config
56 {
57     NMEM nmem; /* for conf_config and servers memory */
58     struct conf_server *servers;
59     WRBUF confdir;
60 };
61
62
63 static char *parse_settings(struct conf_config *config,
64                             NMEM nmem, xmlNode *node);
65
66 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
67                                                         xmlNode *node);
68
69 static void conf_metadata_assign(NMEM nmem, 
70                                  struct conf_metadata * metadata,
71                                  const char *name,
72                                  enum conf_metadata_type type,
73                                  enum conf_metadata_merge merge,
74                                  enum conf_setting_type setting,
75                                  int brief,
76                                  int termlist,
77                                  int rank,
78                                  int sortkey_offset,
79                                  enum conf_metadata_mergekey mt)
80 {
81     assert(nmem && metadata && name);
82     
83     metadata->name = nmem_strdup(nmem, name);
84
85     metadata->type = type;
86
87     // enforcing that type_year is always range_merge
88     if (metadata->type == Metadata_type_year)
89         metadata->merge = Metadata_merge_range;
90     else
91         metadata->merge = merge;    
92
93     metadata->setting = setting;
94     metadata->brief = brief;   
95     metadata->termlist = termlist;
96     metadata->rank = rank;    
97     metadata->sortkey_offset = sortkey_offset;
98     metadata->mergekey = mt;
99 }
100
101
102 static void conf_sortkey_assign(NMEM nmem, 
103                                 struct conf_sortkey * sortkey,
104                                 const char *name,
105                                 enum conf_sortkey_type type)
106 {
107     assert(nmem && sortkey && name);
108     
109     sortkey->name = nmem_strdup(nmem, name);
110     sortkey->type = type;
111 }
112
113
114 static struct conf_service *service_init(struct conf_server *server,
115                                          int num_metadata, int num_sortkeys,
116                                          const char *service_id)
117 {
118     struct conf_service * service = 0;
119     NMEM nmem = nmem_create();
120
121     service = nmem_malloc(nmem, sizeof(struct conf_service));
122     service->ref_count = 1;
123     service->nmem = nmem;
124     service->next = 0;
125     service->settings = 0;
126     service->databases = 0;
127     service->targetprofiles = 0;
128     service->server = server;
129     service->session_timeout = 60; /* default session timeout */
130     service->z3950_session_timeout = 180;
131     service->z3950_operation_timeout = 30;
132
133     service->relevance_pct = 0;
134     service->sort_pct = 0;
135     service->mergekey_pct = 0;
136
137     service->id = service_id ? nmem_strdup(nmem, service_id) : 0;
138     service->num_metadata = num_metadata;
139     service->metadata = 0;
140     if (service->num_metadata)
141       service->metadata 
142           = nmem_malloc(nmem, 
143                         sizeof(struct conf_metadata) * service->num_metadata);
144     service->num_sortkeys = num_sortkeys;
145     service->sortkeys = 0;
146     if (service->num_sortkeys)
147         service->sortkeys 
148             = nmem_malloc(nmem, 
149                           sizeof(struct conf_sortkey) * service->num_sortkeys);
150     service->dictionary = 0;
151     return service; 
152 }
153
154 static struct conf_metadata* conf_service_add_metadata(
155     struct conf_service *service,
156     int field_id,
157     const char *name,
158     enum conf_metadata_type type,
159     enum conf_metadata_merge merge,
160     enum conf_setting_type setting,
161     int brief,
162     int termlist,
163     int rank,
164     int sortkey_offset,
165     enum conf_metadata_mergekey mt)
166 {
167     struct conf_metadata * md = 0;
168
169     if (!service || !service->metadata || !service->num_metadata
170         || field_id < 0  || !(field_id < service->num_metadata))
171         return 0;
172
173     md = service->metadata + field_id;
174     conf_metadata_assign(service->nmem, md, name, type, merge, setting,
175                          brief, termlist, rank, sortkey_offset,
176                          mt);
177     return md;
178 }
179
180
181 static struct conf_sortkey * conf_service_add_sortkey(
182     struct conf_service *service,
183     int field_id,
184     const char *name,
185     enum conf_sortkey_type type)
186 {
187     struct conf_sortkey * sk = 0;
188
189     if (!service || !service->sortkeys || !service->num_sortkeys
190         || field_id < 0  || !(field_id < service->num_sortkeys))
191         return 0;
192
193     //sk = &((service->sortkeys)[field_id]);
194     sk = service->sortkeys + field_id;
195     conf_sortkey_assign(service->nmem, sk, name, type);
196
197     return sk;
198 }
199
200
201 int conf_service_metadata_field_id(struct conf_service *service,
202                                    const char * name)
203 {
204     int i = 0;
205
206     if (!service || !service->metadata || !service->num_metadata)
207         return -1;
208
209     for(i = 0; i < service->num_metadata; i++) {
210         if (!strcmp(name, (service->metadata[i]).name))
211             return i;
212     }
213    
214     return -1;
215 }
216
217
218 int conf_service_sortkey_field_id(struct conf_service *service,
219                                   const char * name)
220 {
221     int i = 0;
222
223     if (!service || !service->sortkeys || !service->num_sortkeys)
224         return -1;
225
226     for(i = 0; i < service->num_sortkeys; i++) {
227         if (!strcmp(name, (service->sortkeys[i]).name))
228             return i;
229     }
230    
231     return -1;
232 }
233
234 static void conf_dir_path(struct conf_config *config, WRBUF w, const char *src)
235 {
236     if (config->confdir && wrbuf_len(config->confdir) > 0 &&
237         !yaz_is_abspath(src))
238     {
239         wrbuf_printf(w, "%s/%s", wrbuf_cstr(config->confdir), src);
240     }
241     else
242         wrbuf_puts(w, src);
243 }
244
245 void service_destroy(struct conf_service *service)
246 {
247     if (service)
248     {
249         assert(service->ref_count > 0);
250         service->ref_count--;
251         if (service->ref_count == 0)
252         {
253             pp2_charset_destroy(service->relevance_pct);
254             pp2_charset_destroy(service->sort_pct);
255             pp2_charset_destroy(service->mergekey_pct);
256             nmem_destroy(service->nmem);
257         }
258     }
259 }
260
261 void service_incref(struct conf_service *service)
262 {
263     service->ref_count++;
264 }
265
266 static int parse_metadata(struct conf_service *service, xmlNode *n,
267                           int *md_node, int *sk_node)
268 {
269     xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
270     xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
271     xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
272     xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
273     xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
274     xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
275     xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
276     xmlChar *xml_setting = xmlGetProp(n, (xmlChar *) "setting");
277     xmlChar *xml_mergekey = xmlGetProp(n, (xmlChar *) "mergekey");
278     
279     enum conf_metadata_type type = Metadata_type_generic;
280     enum conf_metadata_merge merge = Metadata_merge_no;
281     enum conf_setting_type setting = Metadata_setting_no;
282     enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
283     enum conf_metadata_mergekey mergekey_type = Metadata_mergekey_no;
284     int brief = 0;
285     int termlist = 0;
286     int rank = 0;
287     int sortkey_offset = 0;
288     
289     // now do the parsing logic
290     if (!xml_name)
291     {
292         yaz_log(YLOG_FATAL, "Must specify name in metadata element");
293         return -1;
294     }
295     if (xml_brief)
296     {
297         if (!strcmp((const char *) xml_brief, "yes"))
298             brief = 1;
299         else if (strcmp((const char *) xml_brief, "no"))
300         {
301             yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
302             return -1;
303         }
304     }
305     else
306         brief = 0;
307     
308     if (xml_termlist)
309     {
310         if (!strcmp((const char *) xml_termlist, "yes"))
311             termlist = 1;
312         else if (strcmp((const char *) xml_termlist, "no"))
313         {
314             yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
315             return -1;
316         }
317     }
318     else
319         termlist = 0;
320     
321     if (xml_rank)
322         rank = atoi((const char *) xml_rank);
323     else
324         rank = 0;
325     
326     if (xml_type)
327     {
328         if (!strcmp((const char *) xml_type, "generic"))
329             type = Metadata_type_generic;
330         else if (!strcmp((const char *) xml_type, "year"))
331             type = Metadata_type_year;
332         else if (!strcmp((const char *) xml_type, "date"))
333             type = Metadata_type_date;
334         else
335         {
336             yaz_log(YLOG_FATAL, 
337                     "Unknown value for metadata/type: %s", xml_type);
338             return -1;
339         }
340     }
341     else
342         type = Metadata_type_generic;
343     
344     if (xml_merge)
345     {
346         if (!strcmp((const char *) xml_merge, "no"))
347             merge = Metadata_merge_no;
348         else if (!strcmp((const char *) xml_merge, "unique"))
349             merge = Metadata_merge_unique;
350         else if (!strcmp((const char *) xml_merge, "longest"))
351             merge = Metadata_merge_longest;
352         else if (!strcmp((const char *) xml_merge, "range"))
353             merge = Metadata_merge_range;
354         else if (!strcmp((const char *) xml_merge, "all"))
355             merge = Metadata_merge_all;
356         else
357         {
358             yaz_log(YLOG_FATAL, 
359                     "Unknown value for metadata/merge: %s", xml_merge);
360             return -1;
361         }
362     }
363     else
364         merge = Metadata_merge_no;
365     
366     if (xml_setting)
367     {
368         if (!strcmp((const char *) xml_setting, "no"))
369             setting = Metadata_setting_no;
370         else if (!strcmp((const char *) xml_setting, "postproc"))
371             setting = Metadata_setting_postproc;
372         else if (!strcmp((const char *) xml_setting, "parameter"))
373             setting = Metadata_setting_parameter;
374         else
375         {
376             yaz_log(YLOG_FATAL,
377                     "Unknown value for medadata/setting: %s", xml_setting);
378             return -1;
379         }
380     }
381     
382     // add a sortkey if so specified
383     if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
384     {
385         if (merge == Metadata_merge_no)
386         {
387             yaz_log(YLOG_FATAL, 
388                     "Can't specify sortkey on a non-merged field");
389             return -1;
390         }
391         if (!strcmp((const char *) xml_sortkey, "numeric"))
392             sk_type = Metadata_sortkey_numeric;
393         else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
394             sk_type = Metadata_sortkey_skiparticle;
395         else
396         {
397             yaz_log(YLOG_FATAL,
398                     "Unknown sortkey in metadata element: %s", 
399                     xml_sortkey);
400             return -1;
401         }
402         sortkey_offset = *sk_node;
403         
404         conf_service_add_sortkey(service, *sk_node,
405                                  (const char *) xml_name, sk_type);
406         
407         (*sk_node)++;
408     }
409     else
410         sortkey_offset = -1;
411     
412     if (xml_mergekey && strcmp((const char *) xml_mergekey, "no"))
413     {
414         mergekey_type = Metadata_mergekey_yes;
415     }
416     
417     
418     // metadata known, assign values
419     conf_service_add_metadata(service, *md_node,
420                               (const char *) xml_name,
421                               type, merge, setting,
422                               brief, termlist, rank, sortkey_offset,
423                               mergekey_type);
424     
425     xmlFree(xml_name);
426     xmlFree(xml_brief);
427     xmlFree(xml_sortkey);
428     xmlFree(xml_merge);
429     xmlFree(xml_type);
430     xmlFree(xml_termlist);
431     xmlFree(xml_rank);
432     xmlFree(xml_setting);
433     (*md_node)++;
434     return 0;
435 }
436
437 static struct conf_service *service_create_static(struct conf_server *server,
438                                                   xmlNode *node,
439                                                   const char *service_id)
440 {
441     xmlNode *n;
442     int md_node = 0;
443     int sk_node = 0;
444
445     struct conf_service *service = 0;
446     int num_metadata = 0;
447     int num_sortkeys = 0;
448     int got_settings = 0;
449     
450     // count num_metadata and num_sortkeys
451     for (n = node->children; n; n = n->next)
452         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
453                                                    n->name, "metadata"))
454         {
455             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
456             num_metadata++;
457             if (sortkey && strcmp((const char *) sortkey, "no"))
458                 num_sortkeys++;
459             xmlFree(sortkey);
460         }
461
462     service = service_init(server, num_metadata, num_sortkeys, service_id);
463
464     for (n = node->children; n; n = n->next)
465     {
466         if (n->type != XML_ELEMENT_NODE)
467             continue;
468         if (!strcmp((const char *) n->name, "timeout"))
469         {
470             xmlChar *src = xmlGetProp(n, (xmlChar *) "session");
471             if (src)
472             {
473                 service->session_timeout = atoi((const char *) src);
474                 xmlFree(src);
475                 if (service->session_timeout < 9)
476                 {
477                     yaz_log(YLOG_FATAL, "session timeout out of range");
478                     return 0;
479                 }
480             }
481             src = xmlGetProp(n, (xmlChar *) "z3950_operation");
482             if (src)
483             {
484                 service->z3950_operation_timeout = atoi((const char *) src);
485                 xmlFree(src);
486                 if (service->z3950_session_timeout < 9)
487                 {
488                     yaz_log(YLOG_FATAL, "Z39.50 operation timeout out of range");
489                     return 0;
490                 }
491             }
492             src = xmlGetProp(n, (xmlChar *) "z3950_session");
493             if (src)
494             {
495                 service->z3950_session_timeout = atoi((const char *) src);
496                 xmlFree(src);
497                 if (service->z3950_session_timeout < 9)
498                 {
499                     yaz_log(YLOG_FATAL, "Z39.50 session timeout out of range");
500                     return 0;
501                 }
502             }
503         }
504         else if (!strcmp((const char *) n->name, "settings"))
505             got_settings++;
506         else if (!strcmp((const char *) n->name, (const char *) "targetprofiles"))
507         {
508             if (service->targetprofiles)
509             {
510                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
511                 return 0;
512             }
513             if (!(service->targetprofiles = 
514                   parse_targetprofiles(service->nmem, n)))
515                 return 0;
516         }
517         else if (!strcmp((const char *) n->name, "relevance"))
518         {
519             if (service->relevance_pct)
520             {
521                 yaz_log(YLOG_LOG, "relevance may not repeat in service");
522                 return 0;
523             }
524             else
525             {
526                 service->relevance_pct = pp2_charset_create_xml(n);
527                 if (!service->relevance_pct)
528                     return 0;
529             }
530         }
531         else if (!strcmp((const char *) n->name, "sort"))
532         {
533             if (service->sort_pct)
534             {
535                 yaz_log(YLOG_LOG, "sort may not repeat in service");
536                 return 0;
537             }
538             else
539             {
540                 service->sort_pct = pp2_charset_create_xml(n);
541                 if (!service->sort_pct)
542                     return 0;
543             }
544         }
545         else if (!strcmp((const char *) n->name, "mergekey"))
546         {
547             if (service->mergekey_pct)
548             {
549                 yaz_log(YLOG_LOG, "mergekey may not repeat in service");
550                 return 0;
551             }
552             else
553             {
554                 service->mergekey_pct = pp2_charset_create_xml(n);
555                 if (!service->mergekey_pct)
556                     return 0;
557             }
558         }
559         else if (!strcmp((const char *) n->name, (const char *) "metadata"))
560         {
561             if (parse_metadata(service, n, &md_node, &sk_node))
562                 return 0;
563         }
564         else
565         {
566             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
567             return 0;
568         }
569     }
570     if (got_settings)
571     {
572         int pass;
573         /* metadata has been read.. Consider now settings */
574         init_settings(service);
575         for (pass = 1; pass <= 2; pass++)
576         {
577             for (n = node->children; n; n = n->next)
578             {
579                 if (n->type != XML_ELEMENT_NODE)
580                     continue;
581                 if (!strcmp((const char *) n->name, "settings"))
582                 {
583                     xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
584                     if (src)
585                     {
586                         WRBUF w = wrbuf_alloc();
587                         conf_dir_path(server->config, w, (const char *) src);
588                         settings_read_file(service, wrbuf_cstr(w), pass);
589                         wrbuf_destroy(w);
590                         xmlFree(src);
591                     }
592                     else
593                     {
594                         settings_read_node(service, n, pass);
595                     }
596                 }
597             }
598         }
599     }
600     return service;
601 }
602
603 static char *parse_settings(struct conf_config *config,
604                             NMEM nmem, xmlNode *node)
605 {
606     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
607     char *r;
608
609     if (src)
610     {
611         WRBUF w = wrbuf_alloc();
612         conf_dir_path(config, w, (const char *) src);
613         r = nmem_strdup(nmem, wrbuf_cstr(w));
614         wrbuf_destroy(w);
615     }
616     else
617     {
618         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
619         return 0;
620     }
621     xmlFree(src);
622     return r;
623 }
624
625 static void inherit_server_settings(struct conf_service *s)
626 {
627     struct conf_server *server = s->server;
628     if (!s->dictionary) /* service has no config settings ? */
629     {
630         if (server->server_settings)
631         {
632             /* inherit settings from server */
633             init_settings(s);
634             settings_read_file(s, server->server_settings, 1);
635             settings_read_file(s, server->server_settings, 2);
636         }
637         else
638         {
639             yaz_log(YLOG_WARN, "service '%s' has no settings",
640                     s->id ? s->id : "unnamed");
641             init_settings(s);
642         }
643     }
644     
645     /* use relevance/sort/mergekey from server if not defined
646        for this service.. */
647     if (!s->relevance_pct)
648     {
649         if (server->relevance_pct)
650         {
651             s->relevance_pct = server->relevance_pct;
652             pp2_charset_incref(s->relevance_pct);
653         }
654         else
655             s->relevance_pct = pp2_charset_create(0);
656     }
657     
658     if (!s->sort_pct)
659     {
660         if (server->sort_pct)
661         {
662             s->sort_pct = server->sort_pct;
663             pp2_charset_incref(s->sort_pct);
664         }
665         else
666             s->sort_pct = pp2_charset_create(0);
667     }
668     
669     if (!s->mergekey_pct)
670     {
671         if (server->mergekey_pct)
672         {
673             s->mergekey_pct = server->mergekey_pct;
674             pp2_charset_incref(s->mergekey_pct);
675         }
676         else
677             s->mergekey_pct = pp2_charset_create(0);
678     }
679 }
680
681 struct conf_service *service_create(struct conf_server *server,
682                                     xmlNode *node)
683 {
684     struct conf_service *service = service_create_static(server,
685                                                          node, 0);
686     if (service)
687     {
688         inherit_server_settings(service);
689         resolve_databases(service);
690     }
691     return service;
692 }
693
694 static struct conf_server *server_create(struct conf_config *config,
695                                          NMEM nmem, xmlNode *node)
696 {
697     xmlNode *n;
698     struct conf_server *server = nmem_malloc(nmem, sizeof(struct conf_server));
699
700     server->host = 0;
701     server->port = 0;
702     server->proxy_host = 0;
703     server->proxy_port = 0;
704     server->myurl = 0;
705     server->proxy_addr = 0;
706     server->service = 0;
707     server->config = config;
708     server->next = 0;
709     server->relevance_pct = 0;
710     server->sort_pct = 0;
711     server->mergekey_pct = 0;
712     server->server_settings = 0;
713
714     for (n = node->children; n; n = n->next)
715     {
716         if (n->type != XML_ELEMENT_NODE)
717             continue;
718         if (!strcmp((const char *) n->name, "listen"))
719         {
720             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
721             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
722             if (port)
723                 server->port = atoi((const char *) port);
724             if (host)
725                 server->host = nmem_strdup(nmem, (const char *) host);
726             xmlFree(port);
727             xmlFree(host);
728         }
729         else if (!strcmp((const char *) n->name, "proxy"))
730         {
731             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
732             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
733             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
734             if (port)
735                 server->proxy_port = atoi((const char *) port);
736             if (host)
737                 server->proxy_host = nmem_strdup(nmem, (const char *) host);
738             if (myurl)
739                 server->myurl = nmem_strdup(nmem, (const char *) myurl);
740             xmlFree(port);
741             xmlFree(host);
742             xmlFree(myurl);
743         }
744         else if (!strcmp((const char *) n->name, "settings"))
745         {
746             if (server->server_settings)
747             {
748                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
749                 return 0;
750             }
751             if (!(server->server_settings = parse_settings(config, nmem, n)))
752                 return 0;
753         }
754         else if (!strcmp((const char *) n->name, "relevance"))
755         {
756             server->relevance_pct = pp2_charset_create_xml(n);
757             if (!server->relevance_pct)
758                 return 0;
759         }
760         else if (!strcmp((const char *) n->name, "sort"))
761         {
762             server->sort_pct = pp2_charset_create_xml(n);
763             if (!server->sort_pct)
764                 return 0;
765         }
766         else if (!strcmp((const char *) n->name, "mergekey"))
767         {
768             server->mergekey_pct = pp2_charset_create_xml(n);
769             if (!server->mergekey_pct)
770                 return 0;
771         }
772         else if (!strcmp((const char *) n->name, "service"))
773         {
774             char *service_id = (char *)
775                 xmlGetProp(n, (xmlChar *) "id");
776
777             struct conf_service **sp = &server->service;
778             for (; *sp; sp = &(*sp)->next)
779                 if ((*sp)->id && service_id &&
780                     0 == strcmp((*sp)->id, service_id))
781                 {
782                     yaz_log(YLOG_FATAL, "Duplicate service: %s", service_id);
783                     break;
784                 }
785                 else if (!(*sp)->id && !service_id)
786                 {
787                     yaz_log(YLOG_FATAL, "Duplicate unnamed service '%s'",
788                         service_id);
789                     break;
790                 }
791
792             if (*sp)  /* service already exist */
793             {
794                 xmlFree(service_id);
795                 return 0;
796             }
797             else
798             {
799                 struct conf_service *s = service_create_static(server, n,
800                                                                service_id);
801                 xmlFree(service_id);
802                 if (!s)
803                     return 0;
804                 *sp = s;
805             }
806         }
807         else
808         {
809             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
810             return 0;
811         }
812     }
813     if (server->service)
814     {
815         struct conf_service *s;
816         for (s = server->service; s; s = s->next)
817             inherit_server_settings(s);
818     }
819     return server;
820 }
821
822 WRBUF conf_get_fname(struct conf_service *service, const char *fname)
823 {
824     struct conf_config *config = service->server->config;
825     WRBUF w = wrbuf_alloc();
826
827     conf_dir_path(config, w, fname);
828     return w;
829 }
830
831 static struct conf_targetprofiles *parse_targetprofiles(NMEM nmem,
832                                                         xmlNode *node)
833 {
834     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
835     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
836     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
837
838     memset(r, 0, sizeof(*r));
839
840     if (type)
841     {
842         if (!strcmp((const char *) type, "local"))
843             r->type = Targetprofiles_local;
844         else
845         {
846             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
847             return 0;
848         }
849     }
850     else
851     {
852         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
853         return 0;
854     }
855
856     if (src)
857         r->src = nmem_strdup(nmem, (const char *) src);
858     else
859     {
860         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
861         return 0;
862     }
863     xmlFree(type);
864     xmlFree(src);
865     return r;
866 }
867
868 struct conf_service *locate_service(struct conf_server *server,
869                                     const char *service_id)
870 {
871     struct conf_service *s = server->service;
872     for (; s; s = s->next)
873         if (s->id && service_id && 0 == strcmp(s->id, service_id))
874             return s;
875         else if (!s->id && !service_id)
876             return s;
877     return 0;
878 }
879
880
881 static int parse_config(struct conf_config *config, xmlNode *root)
882 {
883     xmlNode *n;
884
885     for (n = root->children; n; n = n->next)
886     {
887         if (n->type != XML_ELEMENT_NODE)
888             continue;
889         if (!strcmp((const char *) n->name, "server"))
890         {
891             struct conf_server *tmp = server_create(config, config->nmem, n);
892             if (!tmp)
893                 return -1;
894             tmp->next = config->servers;
895             config->servers = tmp;
896         }
897         else if (!strcmp((const char *) n->name, "targetprofiles"))
898         {
899             yaz_log(YLOG_FATAL, "targetprofiles unsupported here. Must be part of service");
900             return -1;
901
902         }
903         else
904         {
905             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
906             return -1;
907         }
908     }
909     return 0;
910 }
911
912 static int process_config_includes(struct conf_config *config, xmlNode *n);
913
914 static int config_include_one(struct conf_config *config, xmlNode **sib,
915     const char *path)
916 {
917     struct stat st;
918     if (stat(path, &st) < 0)
919     {
920         yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
921         return -1;
922     }
923     else
924     {
925         if ((st.st_mode & S_IFMT) == S_IFREG)
926         {
927             xmlDoc *doc = xmlParseFile(path);
928             if (doc)
929             {
930                 xmlNodePtr t = xmlDocGetRootElement(doc);
931                 int ret = process_config_includes(config, t);
932                 *sib = xmlAddNextSibling(*sib, xmlCopyNode(t, 1));
933                 xmlFreeDoc(doc);
934                 if (ret)
935                     return -1;
936             }
937             else
938             {
939                 yaz_log(YLOG_FATAL, "Could not parse %s", path);
940                 return -1;
941             }
942         }
943     }
944     return 0;
945 }
946
947 static int config_include_src(struct conf_config *config, xmlNode **np,
948                               const char *src)
949 {
950     int ret = 0; /* return code. OK so far */
951     WRBUF w = wrbuf_alloc();
952     xmlNodePtr sib; /* our sibling that we append */
953     xmlNodePtr c; /* tmp node */
954
955     wrbuf_printf(w, " begin include src=\"%s\" ", src);
956
957     /* replace include element with a 'begin' comment */
958     sib = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
959     xmlReplaceNode(*np, sib);
960
961     xmlFreeNode(*np);
962
963     wrbuf_rewind(w);
964     conf_dir_path(config, w, src);
965 #if USE_POSIX_GLOB
966     {
967         size_t i;
968         glob_t glob_res;
969         glob(wrbuf_cstr(w), 0 /* flags */, 0 /* errfunc */, &glob_res);
970         
971         for (i = 0; ret == 0 && i < glob_res.gl_pathc; i++)
972         {
973             const char *path = glob_res.gl_pathv[i];
974             ret = config_include_one(config, &sib, path);
975         }
976         globfree(&glob_res);
977     }
978 #else
979     ret = config_include_one(config, &sib, wrbuf_cstr(w));
980 #endif
981     wrbuf_rewind(w);
982     wrbuf_printf(w, " end include src=\"%s\" ", src);
983     c = xmlNewComment((const xmlChar *) wrbuf_cstr(w));
984     sib = xmlAddNextSibling(sib, c);
985     
986     *np = sib;
987     wrbuf_destroy(w);
988     return ret;
989 }
990
991 static int process_config_includes(struct conf_config *config, xmlNode *n)
992 {
993     for (; n; n = n->next)
994     {
995         if (n->type == XML_ELEMENT_NODE)
996         {
997             if (!strcmp((const char *) n->name, "include"))
998             {
999                 xmlChar *src = xmlGetProp(n, (xmlChar *) "src");
1000                 if (src)
1001                 {
1002                     int ret = config_include_src(config, &n,
1003                                                  (const char *) src);
1004                     xmlFree(src);
1005                     if (ret)
1006                         return ret;
1007                         
1008                 }
1009             }
1010             else
1011             {
1012                 if (process_config_includes(config, n->children))
1013                     return -1;
1014             }
1015         }
1016     }
1017     return 0;
1018 }
1019
1020 struct conf_config *config_create(const char *fname, int verbose)
1021 {
1022     xmlDoc *doc = xmlParseFile(fname);
1023     xmlNode *n;
1024     const char *p;
1025     int r;
1026     NMEM nmem = nmem_create();
1027     struct conf_config *config = nmem_malloc(nmem, sizeof(struct conf_config));
1028
1029     xmlSubstituteEntitiesDefault(1);
1030     xmlLoadExtDtdDefaultValue = 1;
1031     if (!doc)
1032     {
1033         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
1034         nmem_destroy(nmem);
1035         return 0;
1036     }
1037
1038     config->nmem = nmem;
1039     config->servers = 0;
1040
1041     config->confdir = wrbuf_alloc();
1042     if ((p = strrchr(fname, 
1043 #ifdef WIN32
1044                      '\\'
1045 #else
1046                      '/'
1047 #endif
1048              )))
1049     {
1050         int len = p - fname;
1051         wrbuf_write(config->confdir, fname, len);
1052     }
1053     wrbuf_puts(config->confdir, "");
1054     
1055     n = xmlDocGetRootElement(doc);
1056     r = process_config_includes(config, n);
1057     if (r == 0) /* OK */
1058     {
1059         if (verbose)
1060         {
1061             yaz_log(YLOG_LOG, "Configuration %s after include processing",
1062                     fname);
1063             xmlDocFormatDump(yaz_log_file(), doc, 0);
1064         }
1065         r = parse_config(config, n);
1066     }
1067     xmlFreeDoc(doc);
1068
1069     if (r)
1070     {
1071         config_destroy(config);
1072         return 0;
1073     }
1074     return config;
1075 }
1076
1077 void server_destroy(struct conf_server *server)
1078 {
1079     struct conf_service *s = server->service;
1080     while (s)
1081     {
1082         struct conf_service *s_next = s->next;
1083         service_destroy(s);
1084         s = s_next;
1085     }
1086     pp2_charset_destroy(server->relevance_pct);
1087     pp2_charset_destroy(server->sort_pct);
1088     pp2_charset_destroy(server->mergekey_pct);
1089 }
1090
1091 void config_destroy(struct conf_config *config)
1092 {
1093     if (config)
1094     {
1095         struct conf_server *server = config->servers;
1096         while (server)
1097         {
1098             struct conf_server *s_next = server->next;
1099             server_destroy(server);
1100             server = s_next;
1101         }
1102         wrbuf_destroy(config->confdir);
1103         nmem_destroy(config->nmem);
1104     }
1105 }
1106
1107 void config_stop_listeners(struct conf_config *conf)
1108 {
1109     struct conf_server *ser;
1110     for (ser = conf->servers; ser; ser = ser->next)
1111         http_close_server(ser);
1112 }
1113
1114 void config_start_databases(struct conf_config *conf)
1115 {
1116     struct conf_server *ser;
1117     for (ser = conf->servers; ser; ser = ser->next)
1118     {
1119         struct conf_service *s = ser->service;
1120         for (;s ; s = s->next)
1121             resolve_databases(s);
1122     }
1123 }
1124
1125 int config_start_listeners(struct conf_config *conf,
1126                            const char *listener_override)
1127 {
1128     struct conf_server *ser;
1129     for (ser = conf->servers; ser; ser = ser->next)
1130     {
1131         WRBUF w = wrbuf_alloc();
1132         int r;
1133         if (listener_override)
1134         {
1135             wrbuf_puts(w, listener_override);
1136             listener_override = 0; /* only first server is overriden */
1137         }
1138         else
1139         {
1140             if (ser->host)
1141                 wrbuf_puts(w, ser->host);
1142             if (ser->port)
1143             {
1144                 if (wrbuf_len(w))
1145                     wrbuf_puts(w, ":");
1146                 wrbuf_printf(w, "%d", ser->port);
1147             }
1148         }
1149         r = http_init(wrbuf_cstr(w), ser);
1150         wrbuf_destroy(w);
1151         if (r)
1152             return -1;
1153
1154         w = wrbuf_alloc();
1155         if (ser->proxy_host || ser->proxy_port)
1156         {
1157             if (ser->proxy_host)
1158                 wrbuf_puts(w, ser->proxy_host);
1159             if (ser->proxy_port)
1160             {
1161                 if (wrbuf_len(w))
1162                     wrbuf_puts(w, ":");
1163                 wrbuf_printf(w, "%d", ser->proxy_port);
1164             }
1165         }
1166         if (wrbuf_len(w))
1167             http_set_proxyaddr(wrbuf_cstr(w), ser);
1168         wrbuf_destroy(w);
1169     }
1170     return 0;
1171 }
1172
1173 /*
1174  * Local variables:
1175  * c-basic-offset: 4
1176  * c-file-style: "Stroustrup"
1177  * indent-tabs-mode: nil
1178  * End:
1179  * vim: shiftwidth=4 tabstop=8 expandtab
1180  */
1181