Changed string chop right; problem is that a pointer could point to
[pazpar2-moved-to-github.git] / src / config.c
1 /* $Id: config.c,v 1.33 2007-04-27 10:27:35 marc Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22 /* $Id: config.c,v 1.33 2007-04-27 10:27:35 marc Exp $ */
23
24 #include <string.h>
25
26 #include <libxml/parser.h>
27 #include <libxml/tree.h>
28 #include <libxslt/xslt.h>
29 #include <libxslt/transform.h>
30 #include <libxslt/xsltutils.h>
31
32 #if HAVE_CONFIG_H
33 #include <cconfig.h>
34 #endif
35
36 #include <yaz/yaz-util.h>
37 #include <yaz/nmem.h>
38
39 #define CONFIG_NOEXTERNS
40 #include "config.h"
41
42 static NMEM nmem = 0;
43 static char confdir[256] = ".";
44
45 struct conf_config *config = 0;
46
47
48 struct conf_metadata * conf_metadata_assign(NMEM nmem, 
49                                             struct conf_metadata * metadata,
50                                             const char *name,
51                                             enum conf_metadata_type type,
52                                             enum conf_metadata_merge merge,
53                                             int brief,
54                                             int termlist,
55                                             int rank,
56                                             int sortkey_offset)
57 {
58     if (!nmem || !metadata || !name)
59         return 0;
60     
61     metadata->name = nmem_strdup(nmem, name);
62
63     // enforcing that merge_range is always type_year 
64     if (merge == Metadata_merge_range)
65         metadata->type = Metadata_type_year;
66     else
67         metadata->type = type;
68
69     // enforcing that type_year is always range_merge
70     if (metadata->type == Metadata_type_year)
71         metadata->merge = Metadata_merge_range;
72     else
73         metadata->merge = merge;    
74
75     metadata->brief = brief;   
76     metadata->termlist = termlist;
77     metadata->rank = rank;    
78     metadata->sortkey_offset = sortkey_offset;
79     return metadata;
80 }
81
82
83 struct conf_sortkey * conf_sortkey_assign(NMEM nmem, 
84                                           struct conf_sortkey * sortkey,
85                                           const char *name,
86                                           enum conf_sortkey_type type)
87 {
88     if (!nmem || !sortkey || !name)
89         return 0;
90     
91     sortkey->name = nmem_strdup(nmem, name);
92     sortkey->type = type;
93
94     return sortkey;
95 }
96
97
98 struct conf_service * conf_service_create(NMEM nmem,
99                                           int num_metadata, int num_sortkeys)
100 {
101     struct conf_service * service = 0;
102
103     //assert(nmem);
104     
105     service = nmem_malloc(nmem, sizeof(struct conf_service));
106
107     service->num_metadata = num_metadata;
108     service->metadata = 0;
109     if (service->num_metadata)
110       service->metadata 
111           = nmem_malloc(nmem, 
112                         sizeof(struct conf_metadata) * service->num_metadata);
113     service->num_sortkeys = num_sortkeys;
114     service->sortkeys = 0;
115     if (service->num_sortkeys)
116         service->sortkeys 
117             = nmem_malloc(nmem, 
118                           sizeof(struct conf_sortkey) * service->num_sortkeys);
119     return service; 
120 }
121
122 struct conf_metadata* conf_service_add_metadata(NMEM nmem, 
123                                                 struct conf_service *service,
124                                                 int field_id,
125                                                 const char *name,
126                                                 enum conf_metadata_type type,
127                                                 enum conf_metadata_merge merge,
128                                                 int brief,
129                                                 int termlist,
130                                                 int rank,
131                                                 int sortkey_offset)
132 {
133     struct conf_metadata * md = 0;
134
135     if (!service || !service->metadata || !service->num_metadata
136         || field_id < 0  || !(field_id < service->num_metadata))
137         return 0;
138
139     //md = &((service->metadata)[field_id]);
140     md = service->metadata + field_id;
141     md = conf_metadata_assign(nmem, md, name, type, merge, 
142                              brief, termlist, rank, sortkey_offset);
143     return md;
144 }
145
146
147 struct conf_sortkey * conf_service_add_sortkey(NMEM nmem,
148                                                struct conf_service *service,
149                                                int field_id,
150                                                const char *name,
151                                                enum conf_sortkey_type type)
152 {
153     struct conf_sortkey * sk = 0;
154
155     if (!service || !service->sortkeys || !service->num_sortkeys
156         || field_id < 0  || !(field_id < service->num_sortkeys))
157         return 0;
158
159     //sk = &((service->sortkeys)[field_id]);
160     sk = service->sortkeys + field_id;
161     sk = conf_sortkey_assign(nmem, sk, name, type);
162
163     return sk;
164 }
165
166
167 int conf_service_metadata_field_id(struct conf_service *service,
168                                    const char * name)
169 {
170     int i = 0;
171
172     if (!service || !service->metadata || !service->num_metadata)
173         return -1;
174
175     for(i = 0; i < service->num_metadata; i++) {
176         if (!strcmp(name, (service->metadata[i]).name))
177             return i;
178     }
179    
180     return -1;
181 };
182
183
184 int conf_service_sortkey_field_id(struct conf_service *service,
185                                   const char * name)
186 {
187     int i = 0;
188
189     if (!service || !service->sortkeys || !service->num_sortkeys)
190         return -1;
191
192     for(i = 0; i < service->num_sortkeys; i++) {
193         if (!strcmp(name, (service->sortkeys[i]).name))
194             return i;
195     }
196    
197     return -1;
198 };
199
200
201
202 /* Code to parse configuration file */
203 /* ==================================================== */
204
205 static struct conf_service *parse_service(xmlNode *node)
206 {
207     xmlNode *n;
208     int md_node = 0;
209     int sk_node = 0;
210
211     struct conf_service *service = 0;
212     int num_metadata = 0;
213     int num_sortkeys = 0;
214     
215     // count num_metadata and num_sortkeys
216     for (n = node->children; n; n = n->next)
217         if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
218                                                    n->name, "metadata"))
219         {
220             xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
221             num_metadata++;
222             if (sortkey && strcmp((const char *) sortkey, "no"))
223                 num_sortkeys++;
224             xmlFree(sortkey);
225         }
226
227     service = conf_service_create(nmem, num_metadata, num_sortkeys);    
228
229     for (n = node->children; n; n = n->next)
230     {
231         if (n->type != XML_ELEMENT_NODE)
232             continue;
233         if (!strcmp((const char *) n->name, (const char *) "metadata"))
234         {
235             xmlChar *xml_name = xmlGetProp(n, (xmlChar *) "name");
236             xmlChar *xml_brief = xmlGetProp(n, (xmlChar *) "brief");
237             xmlChar *xml_sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
238             xmlChar *xml_merge = xmlGetProp(n, (xmlChar *) "merge");
239             xmlChar *xml_type = xmlGetProp(n, (xmlChar *) "type");
240             xmlChar *xml_termlist = xmlGetProp(n, (xmlChar *) "termlist");
241             xmlChar *xml_rank = xmlGetProp(n, (xmlChar *) "rank");
242
243             enum conf_metadata_type type = Metadata_type_generic;
244             enum conf_metadata_merge merge = Metadata_merge_no;
245             int brief = 0;
246             int termlist = 0;
247             int rank = 0;
248             int sortkey_offset = 0;
249             enum conf_sortkey_type sk_type = Metadata_sortkey_relevance;
250             
251             // now do the parsing logic
252             if (!xml_name)
253             {
254                 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
255                 return 0;
256             }
257             if (xml_brief)
258             {
259                 if (!strcmp((const char *) xml_brief, "yes"))
260                     brief = 1;
261                  else if (strcmp((const char *) xml_brief, "no"))
262                 {
263                     yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
264                     return 0;
265                 }
266             }
267             else
268                 brief = 0;
269
270             if (xml_termlist)
271             {
272                 if (!strcmp((const char *) xml_termlist, "yes"))
273                     termlist = 1;
274                 else if (strcmp((const char *) xml_termlist, "no"))
275                 {
276                     yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
277                     return 0;
278                 }
279             }
280             else
281                 termlist = 0;
282
283             if (xml_rank)
284                 rank = atoi((const char *) xml_rank);
285             else
286                 rank = 0;
287
288             if (xml_type)
289             {
290                 if (!strcmp((const char *) xml_type, "generic"))
291                     type = Metadata_type_generic;
292                 else if (!strcmp((const char *) xml_type, "year"))
293                     type = Metadata_type_year;
294                 else
295                 {
296                     yaz_log(YLOG_FATAL, 
297                             "Unknown value for metadata/type: %s", xml_type);
298                     return 0;
299                 }
300             }
301             else
302                 type = Metadata_type_generic;
303
304             if (xml_merge)
305             {
306                 if (!strcmp((const char *) xml_merge, "no"))
307                     merge = Metadata_merge_no;
308                 else if (!strcmp((const char *) xml_merge, "unique"))
309                     merge = Metadata_merge_unique;
310                 else if (!strcmp((const char *) xml_merge, "longest"))
311                     merge = Metadata_merge_longest;
312                 else if (!strcmp((const char *) xml_merge, "range"))
313                     merge = Metadata_merge_range;
314                 else if (!strcmp((const char *) xml_merge, "all"))
315                     merge = Metadata_merge_all;
316                 else
317                 {
318                     yaz_log(YLOG_FATAL, 
319                             "Unknown value for metadata/merge: %s", xml_merge);
320                     return 0;
321                 }
322             }
323             else
324                 merge = Metadata_merge_no;
325
326             // add a sortkey if so specified
327             if (xml_sortkey && strcmp((const char *) xml_sortkey, "no"))
328             {
329                 if (merge == Metadata_merge_no)
330                 {
331                     yaz_log(YLOG_FATAL, 
332                             "Can't specify sortkey on a non-merged field");
333                     return 0;
334                 }
335                 if (!strcmp((const char *) xml_sortkey, "numeric"))
336                     sk_type = Metadata_sortkey_numeric;
337                 else if (!strcmp((const char *) xml_sortkey, "skiparticle"))
338                     sk_type = Metadata_sortkey_skiparticle;
339                 else
340                 {
341                     yaz_log(YLOG_FATAL,
342                             "Unknown sortkey in metadata element: %s", 
343                             xml_sortkey);
344                     return 0;
345                 }
346                 sortkey_offset = sk_node;
347
348                 conf_service_add_sortkey(nmem, service, sk_node,
349                                          (const char *) xml_name, sk_type);
350                 
351                 sk_node++;
352             }
353             else
354                 sortkey_offset = -1;
355
356             // metadata known, assign values
357             conf_service_add_metadata(nmem, service, md_node,
358                                       (const char *) xml_name,
359                                       type, merge,
360                                       brief, termlist, rank, sortkey_offset);
361
362             xmlFree(xml_name);
363             xmlFree(xml_brief);
364             xmlFree(xml_sortkey);
365             xmlFree(xml_merge);
366             xmlFree(xml_type);
367             xmlFree(xml_termlist);
368             xmlFree(xml_rank);
369             md_node++;
370         }
371         else
372         {
373             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
374             return 0;
375         }
376     }
377     return service;
378 }
379
380 static char *parse_settings(xmlNode *node)
381 {
382     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
383     char *r;
384
385     if (src)
386         r = nmem_strdup(nmem, (const char *) src);
387     else
388     {
389         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
390         return 0;
391     }
392     xmlFree(src);
393     return r;
394 }
395
396 static struct conf_server *parse_server(xmlNode *node)
397 {
398     xmlNode *n;
399     struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
400
401     r->host = 0;
402     r->port = 0;
403     r->proxy_host = 0;
404     r->proxy_port = 0;
405     r->myurl = 0;
406     r->zproxy_host = 0;
407     r->zproxy_port = 0;
408     r->service = 0;
409     r->next = 0;
410     r->settings = 0;
411
412     for (n = node->children; n; n = n->next)
413     {
414         if (n->type != XML_ELEMENT_NODE)
415             continue;
416         if (!strcmp((const char *) n->name, "listen"))
417         {
418             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
419             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
420             if (port)
421                 r->port = atoi((const char *) port);
422             if (host)
423                 r->host = nmem_strdup(nmem, (const char *) host);
424             xmlFree(port);
425             xmlFree(host);
426         }
427         else if (!strcmp((const char *) n->name, "proxy"))
428         {
429             xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
430             xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
431             xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
432             if (port)
433                 r->proxy_port = atoi((const char *) port);
434             if (host)
435                 r->proxy_host = nmem_strdup(nmem, (const char *) host);
436             if (myurl)
437                 r->myurl = nmem_strdup(nmem, (const char *) myurl);
438 #ifdef GAGA
439             else
440             {
441                 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
442                 return 0;
443             }
444 #endif
445             xmlFree(port);
446             xmlFree(host);
447             xmlFree(myurl);
448         }
449         else if (!strcmp((const char *) n->name, "zproxy"))
450         {
451             xmlChar *port = 0;
452             xmlChar *host = 0;
453
454             port = xmlGetProp(n, (xmlChar *) "port");
455             host = xmlGetProp(n, (xmlChar *) "host");
456
457             if (port)
458                 r->zproxy_port = atoi((const char *) port);
459             if (host)
460                 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
461
462             xmlFree(port);
463             xmlFree(host);
464         }
465         else if (!strcmp((const char *) n->name, "settings"))
466         {
467             if (r->settings)
468             {
469                 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
470                 return 0;
471             }
472             if (!(r->settings = parse_settings(n)))
473                 return 0;
474         }
475         else if (!strcmp((const char *) n->name, "service"))
476         {
477             struct conf_service *s = parse_service(n);
478             if (!s)
479                 return 0;
480             r->service = s;
481         }
482         else
483         {
484             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
485             return 0;
486         }
487     }
488     return r;
489 }
490
491 xsltStylesheet *conf_load_stylesheet(const char *fname)
492 {
493     char path[256];
494     sprintf(path, "%s/%s", confdir, fname);
495     return xsltParseStylesheetFile((xmlChar *) path);
496 }
497
498 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
499 {
500     struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
501     xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
502     xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
503
504     memset(r, 0, sizeof(*r));
505
506     if (type)
507     {
508         if (!strcmp((const char *) type, "local"))
509             r->type = Targetprofiles_local;
510         else
511         {
512             yaz_log(YLOG_FATAL, "Unknown targetprofile type");
513             return 0;
514         }
515     }
516     else
517     {
518         yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
519         return 0;
520     }
521
522     if (src)
523         r->src = nmem_strdup(nmem, (const char *) src);
524     else
525     {
526         yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
527         return 0;
528     }
529     xmlFree(type);
530     xmlFree(src);
531     return r;
532 }
533
534 static struct conf_config *parse_config(xmlNode *root)
535 {
536     xmlNode *n;
537     struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
538
539     r->servers = 0;
540     r->targetprofiles = 0;
541
542     for (n = root->children; n; n = n->next)
543     {
544         if (n->type != XML_ELEMENT_NODE)
545             continue;
546         if (!strcmp((const char *) n->name, "server"))
547         {
548             struct conf_server *tmp = parse_server(n);
549             if (!tmp)
550                 return 0;
551             tmp->next = r->servers;
552             r->servers = tmp;
553         }
554         else if (!strcmp((const char *) n->name, "targetprofiles"))
555         {
556             // It would be fun to be able to fix this sometime
557             if (r->targetprofiles)
558             {
559                 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
560                 return 0;
561             }
562             if (!(r->targetprofiles = parse_targetprofiles(n)))
563                 return 0;
564         }
565         else
566         {
567             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
568             return 0;
569         }
570     }
571     return r;
572 }
573
574 int read_config(const char *fname)
575 {
576     xmlDoc *doc = xmlParseFile(fname);
577     const char *p;
578
579     if (!nmem)  // Initialize
580     {
581         nmem = nmem_create();
582         xmlSubstituteEntitiesDefault(1);
583         xmlLoadExtDtdDefaultValue = 1;
584     }
585     if (!doc)
586     {
587         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
588         exit(1);
589     }
590     if ((p = strrchr(fname, '/')))
591     {
592         int len = p - fname;
593         strncpy(confdir, fname, len);
594         confdir[len] = '\0';
595     }
596     config = parse_config(xmlDocGetRootElement(doc));
597     xmlFreeDoc(doc);
598
599     if (config)
600         return 1;
601     else
602         return 0;
603 }
604
605
606 /*
607  * Local variables:
608  * c-basic-offset: 4
609  * indent-tabs-mode: nil
610  * End:
611  * vim: shiftwidth=4 tabstop=8 expandtab
612  */