WRBUF updates.
[pazpar2-moved-to-github.git] / src / config.c
index 2de8479..d862443 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: config.c,v 1.6 2007-01-08 19:39:12 quinn Exp $ */
+/* $Id: config.c,v 1.17 2007-03-20 07:27:51 adam Exp $ */
 
 #include <string.h>
 
@@ -30,22 +30,35 @@ static struct conf_service *parse_service(xmlNode *node)
 {
     xmlNode *n;
     struct conf_service *r = nmem_malloc(nmem, sizeof(struct conf_service));
-    int num_metadata = 0;
     int md_node = 0;
+    int sk_node = 0;
 
-    // Allocate array of conf metadata structs, if necessary
+    r->num_sortkeys = r->num_metadata = 0;
+    // Allocate array of conf metadata and sortkey tructs, if necessary
     for (n = node->children; n; n = n->next)
-        if (n->type == XML_ELEMENT_NODE && !strcmp(n->name, "metadata"))
-            num_metadata++;
-    if (num_metadata)
-        r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * num_metadata);
-    r->num_metadata = num_metadata;
+        if (n->type == XML_ELEMENT_NODE && !strcmp((const char *)
+                                                   n->name, "metadata"))
+        {
+            xmlChar *sortkey = xmlGetProp(n, (xmlChar *) "sortkey");
+            r->num_metadata++;
+            if (sortkey && strcmp((const char *) sortkey, "no"))
+                r->num_sortkeys++;
+            xmlFree(sortkey);
+        }
+    if (r->num_metadata)
+        r->metadata = nmem_malloc(nmem, sizeof(struct conf_metadata) * r->num_metadata);
+    else
+        r->metadata = 0;
+    if (r->num_sortkeys)
+        r->sortkeys = nmem_malloc(nmem, sizeof(struct conf_sortkey) * r->num_sortkeys);
+    else
+        r->sortkeys = 0;
 
     for (n = node->children; n; n = n->next)
     {
         if (n->type != XML_ELEMENT_NODE)
             continue;
-        if (!strcmp(n->name, "metadata"))
+        if (!strcmp(n->name, (const char *) "metadata"))
         {
             struct conf_metadata *md = &r->metadata[md_node];
             xmlChar *name = xmlGetProp(n, "name");
@@ -91,14 +104,12 @@ static struct conf_service *parse_service(xmlNode *node)
             if (rank)
                 md->rank = atoi(rank);
             else
-                md->rank = 1;
+                md->rank = 0;
 
             if (type)
             {
                 if (!strcmp(type, "generic"))
                     md->type = Metadata_type_generic;
-                else if (!strcmp(type, "integer"))
-                    md->type = Metadata_type_integer;
                 else if (!strcmp(type, "year"))
                     md->type = Metadata_type_year;
                 else
@@ -107,26 +118,8 @@ static struct conf_service *parse_service(xmlNode *node)
                     return 0;
                 }
             }
-            md->type = Metadata_type_generic;
-
-            if (sortkey)
-            {
-                if (!strcmp(sortkey, "no"))
-                    md->sortkey = Metadata_sortkey_no;
-                else if (!strcmp(sortkey, "numeric"))
-                    md->sortkey = Metadata_sortkey_numeric;
-                else if (!strcmp(sortkey, "range"))
-                    md->sortkey = Metadata_sortkey_range;
-                else if (!strcmp(sortkey, "skiparticle"))
-                    md->sortkey = Metadata_sortkey_skiparticle;
-                else
-                {
-                    yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
-                    return 0;
-                }
-            }
             else
-                md->sortkey = Metadata_sortkey_no;
+                md->type = Metadata_type_generic;
 
             if (merge)
             {
@@ -149,6 +142,30 @@ static struct conf_service *parse_service(xmlNode *node)
             else
                 md->merge = Metadata_merge_no;
 
+            if (sortkey && strcmp(sortkey, "no"))
+            {
+                struct conf_sortkey *sk = &r->sortkeys[sk_node];
+                if (md->merge == Metadata_merge_no)
+                {
+                    yaz_log(YLOG_FATAL, "Can't specify sortkey on a non-merged field");
+                    return 0;
+                }
+                if (!strcmp(sortkey, "numeric"))
+                    sk->type = Metadata_sortkey_numeric;
+                else if (!strcmp(sortkey, "skiparticle"))
+                    sk->type = Metadata_sortkey_skiparticle;
+                else
+                {
+                    yaz_log(YLOG_FATAL, "Unknown sortkey in metadata element: %s", sortkey);
+                    return 0;
+                }
+                sk->name = md->name;
+                md->sortkey_offset = sk_node;
+                sk_node++;
+            }
+            else
+                md->sortkey_offset = -1;
+
             xmlFree(name);
             xmlFree(brief);
             xmlFree(sortkey);
@@ -175,6 +192,7 @@ static struct conf_server *parse_server(xmlNode *node)
     r->port = 0;
     r->proxy_host = 0;
     r->proxy_port = 0;
+    r->myurl = 0;
     r->service = 0;
     r->next = 0;
 
@@ -197,12 +215,21 @@ static struct conf_server *parse_server(xmlNode *node)
         {
             xmlChar *port = xmlGetProp(n, "port");
             xmlChar *host = xmlGetProp(n, "host");
+            xmlChar *myurl = xmlGetProp(n, "myurl");
             if (port)
                 r->proxy_port = atoi(port);
             if (host)
                 r->proxy_host = nmem_strdup(nmem, host);
+            if (myurl)
+                r->myurl = nmem_strdup(nmem, myurl);
+            else
+            {
+                yaz_log(YLOG_FATAL, "Must specify @myurl for proxy");
+                return 0;
+            }
             xmlFree(port);
             xmlFree(host);
+            xmlFree(myurl);
         }
         else if (!strcmp(n->name, "service"))
         {
@@ -275,13 +302,16 @@ static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
                 yaz_log(YLOG_WARN, "Missing name in 'nativesyntax' element");
                 return 0;
             }
+            if (encoding)
+                r->native_encoding = encoding;
             if (!strcmp(name, "iso2709"))
             {
                 r->native_syntax = Nativesyn_iso2709;
                 // Set a few defaults, too
                 r->native_format = Nativeform_marc21;
                 r->native_mapto = Nativemapto_marcxml;
-                r->native_encoding = "marc-8";
+                if (!r->native_encoding)
+                    r->native_encoding = "marc-8";
                 setup_marc(r);
             }
             else if (!strcmp(name, "xml"))
@@ -301,8 +331,6 @@ static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
                     return 0;
                 }
             }
-            if (encoding)
-                r->native_encoding = encoding;
             if (mapto)
             {
                 if (!strcmp(mapto, "marcxml"))
@@ -327,7 +355,7 @@ static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
             xmlChar *charset = xmlGetProp(n, "charset");
             xmlChar *format = xmlGetProp(n, "format");
             xmlChar *stylesheet = xmlGetProp(n, "stylesheet");
-            bzero(m, sizeof(*m));
+            memset(m, 0, sizeof(*m));
             if (type)
             {
                 if (!strcmp(type, "xslt"))
@@ -364,6 +392,42 @@ static struct conf_retrievalprofile *parse_retrievalprofile(xmlNode *node)
     return r;
 }
 
+static struct conf_targetprofiles *parse_targetprofiles(xmlNode *node)
+{
+    struct conf_targetprofiles *r = nmem_malloc(nmem, sizeof(*r));
+    xmlChar *type = xmlGetProp(node, "type");
+    xmlChar *src = xmlGetProp(node, "src");
+
+    memset(r, 0, sizeof(*r));
+
+    if (type)
+    {
+        if (!strcmp(type, "local"))
+            r->type = Targetprofiles_local;
+        else
+        {
+            yaz_log(YLOG_FATAL, "Unknown targetprofile type");
+            return 0;
+        }
+    }
+    else
+    {
+        yaz_log(YLOG_FATAL, "Must specify type for targetprofile");
+        return 0;
+    }
+
+    if (src)
+        r->src = nmem_strdup(nmem, src);
+    else
+    {
+        yaz_log(YLOG_FATAL, "Must specify src in targetprofile");
+        return 0;
+    }
+    xmlFree(type);
+    xmlFree(src);
+    return r;
+}
+
 static struct conf_config *parse_config(xmlNode *root)
 {
     xmlNode *n;
@@ -373,6 +437,7 @@ static struct conf_config *parse_config(xmlNode *root)
     r->servers = 0;
     r->queryprofiles = 0;
     r->retrievalprofiles = 0;
+    r->targetprofiles = 0;
 
     for (n = root->children; n; n = n->next)
     {
@@ -395,6 +460,17 @@ static struct conf_config *parse_config(xmlNode *root)
                 return 0;
             rp = &(*rp)->next;
         }
+        else if (!strcmp(n->name, "targetprofiles"))
+        {
+            // It would be fun to be able to fix this sometime
+            if (r->targetprofiles)
+            {
+                yaz_log(YLOG_FATAL, "Can't repeat targetprofiles");
+                return 0;
+            }
+            if (!(r->targetprofiles = parse_targetprofiles(n)))
+                return 0;
+        }
         else
         {
             yaz_log(YLOG_FATAL, "Bad element: %s", n->name);
@@ -406,7 +482,7 @@ static struct conf_config *parse_config(xmlNode *root)
 
 int read_config(const char *fname)
 {
-    xmlDoc *doc = xmlReadFile(fname, NULL, 0);
+    xmlDoc *doc = xmlParseFile(fname);
     const char *p;
 
     if (!nmem)  // Initialize
@@ -420,7 +496,7 @@ int read_config(const char *fname)
         yaz_log(YLOG_FATAL, "Failed to read %s", fname);
         exit(1);
     }
-    if ((p = rindex(fname, '/')))
+    if ((p = strrchr(fname, '/')))
     {
         int len = p - fname;
         strncpy(confdir, fname, len);