1 /* $Id: config.c,v 1.22 2007-04-08 20:52:09 quinn Exp $ */
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>
15 #include <yaz/yaz-util.h>
18 #define CONFIG_NOEXTERNS
22 static char confdir[256] = ".";
24 struct conf_config *config = 0;
26 /* Code to parse configuration file */
27 /* ==================================================== */
29 static struct conf_service *parse_service(xmlNode *node)
32 struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
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 *)
42 xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
44 if (sortkey && strcmp((const char *) sortkey, "no"))
49 r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
53 r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
57 for (n = node->children; n; n = n->next)
59 if (n->type != XML_ELEMENT_NODE)
61 if (!strcmp((const char *) n->name, (const char *) "metadata"))
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");
74 yaz_log(YLOG_FATAL, "Must specify name in metadata element");
77 md->name = nmem_strdup(nmem, (const char *) name);
80 if (!strcmp((const char *) brief, "yes"))
82 else if (strcmp((const char *) brief, "no"))
84 yaz_log(YLOG_FATAL, "metadata/brief must be yes or no");
93 if (!strcmp((const char *) termlist, "yes"))
95 else if (strcmp((const char *) termlist, "no"))
97 yaz_log(YLOG_FATAL, "metadata/termlist must be yes or no");
105 md->rank = atoi((const char *) rank);
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;
117 yaz_log(YLOG_FATAL, "Unknown value for metadata/type: %s", type);
122 md->type = Metadata_type_generic;
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;
138 yaz_log(YLOG_FATAL, "Unknown value for metadata/merge: %s", merge);
143 md->merge = Metadata_merge_no;
145 if (sortkey && strcmp((const char *) sortkey, "no"))
147 struct conf_sortkey *sk = &r->sortkeys[sk_node];
148 if (md->merge == Metadata_merge_no)
150 yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
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;
159 yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
163 md->sortkey_offset = sk_node;
167 md->sortkey_offset = -1;
179 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
186 static char *parse_settings(xmlNode *node)
188 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
192 r = nmem_strdup(nmem, (const char *) src);
195 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
202 static struct conf_server *parse_server(xmlNode *node)
205 struct conf_server *r = nmem_malloc(nmem, sizeof(struct conf_server));
218 for (n = node->children; n; n = n->next)
220 if (n->type != XML_ELEMENT_NODE)
222 if (!strcmp((const char *) n->name, "listen"))
224 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
225 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
227 r->port = atoi((const char *) port);
229 r->host = nmem_strdup(nmem, (const char *) host);
233 else if (!strcmp((const char *) n->name, "proxy"))
235 xmlChar *port = xmlGetProp(n, (xmlChar *) "port");
236 xmlChar *host = xmlGetProp(n, (xmlChar *) "host");
237 xmlChar *myurl = xmlGetProp(n, (xmlChar *) "myurl");
239 r->proxy_port = atoi((const char *) port);
241 r->proxy_host = nmem_strdup(nmem, (const char *) host);
243 r->myurl = nmem_strdup(nmem, (const char *) myurl);
247 yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
255 else if (!strcmp((const char *) n->name, "zproxy"))
260 port = xmlGetProp(n, (xmlChar *) "port");
261 host = xmlGetProp(n, (xmlChar *) "host");
264 r->zproxy_port = atoi((const char *) port);
266 r->zproxy_host = nmem_strdup(nmem, (const char *) host);
271 else if (!strcmp((const char *) n->name, "settings"))
275 yaz_log(YLOG_FATAL, "Can't repeat 'settings'");
278 if (!(r->settings = parse_settings(n)))
281 else if (!strcmp((const char *) n->name, "service"))
283 struct conf_service *s = parse_service(n);
290 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
297 xsltStylesheet *conf_load_stylesheet(const char *fname)
300 sprintf(path, "%s/%s", confdir, fname);
301 return xsltParseStylesheetFile((xmlChar *) path);
304 static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
306 struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
307 xmlChar *type = xmlGetProp(node, (xmlChar *) "type");
308 xmlChar *src = xmlGetProp(node, (xmlChar *) "src");
310 memset(r, 0, sizeof(*r));
314 if (!strcmp((const char *) type, "local"))
315 r->type = Targetprofiles_local;
318 yaz_log(YLOG_FATAL, "Unknown targetprofile type");
324 yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
329 r->src = nmem_strdup(nmem, (const char *) src);
332 yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
340 static struct conf_config *parse_config(xmlNode *root)
343 struct conf_config *r = nmem_malloc(nmem, sizeof(struct conf_config));
346 r->targetprofiles = 0;
348 for (n = root->children; n; n = n->next)
350 if (n->type != XML_ELEMENT_NODE)
352 if (!strcmp((const char *) n->name, "server"))
354 struct conf_server *tmp = parse_server(n);
357 tmp->next = r->servers;
360 else if (!strcmp((const char *) n->name, "targetprofiles"))
362 // It would be fun to be able to fix this sometime
363 if (r->targetprofiles)
365 yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
368 if (!(r->targetprofiles = parse_targetprofiles(n)))
373 yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
380 int read_config(const char *fname)
382 xmlDoc *doc = xmlParseFile(fname);
385 if (!nmem) // Initialize
387 nmem = nmem_create();
388 xmlSubstituteEntitiesDefault(1);
389 xmlLoadExtDtdDefaultValue = 1;
393 yaz_log(YLOG_FATAL, "Failed to read %s", fname);
396 if ((p = strrchr(fname, '/')))
399 strncpy(confdir, fname, len);
402 config = parse_config(xmlDocGetRootElement(doc));
415 * indent-tabs-mode: nil
417 * vim: shiftwidth=4 tabstop=8 expandtab