Moved CCL Map, record syntax, charset normalization, record syntax normalization...
[pazpar2-moved-to-github.git] / src / settings.c
index 11215c9..c5f1b2e 100644 (file)
@@ -1,8 +1,7 @@
-// $Id: settings.c,v 1.3 2007-03-29 13:44:19 quinn Exp $
+// $Id: settings.c,v 1.7 2007-04-08 20:52:09 quinn Exp $
 // This module implements a generic system of settings (attribute-value) that can 
 // be associated with search targets. The system supports both default values,
 // per-target overrides, and per-user settings.
-//
 
 #include <string.h>
 #include <stdio.h>
@@ -27,7 +26,11 @@ static NMEM nmem = 0;
 static char *hard_settings[] = {
     "pz:piggyback",
     "pz:elements",
-    "pz::syntax",
+    "pz:requestsyntax",
+    "pz:cclmap:",
+    "pz:encoding",
+    "pz:xslt",
+    "pz:nativesyntax",
     0
 };
 
@@ -44,12 +47,30 @@ int settings_offset(const char *name)
 {
     int i;
 
+    if (!name)
+        name = "";
     for (i = 0; i < dictionary->num; i++)
         if (!strcmp(name, dictionary->dict[i]))
             return i;
     return -1;
 }
 
+// Ignores everything after second colon, if present
+// A bit of a hack to support the pz:cclmap: scheme (and more to come?)
+static int settings_offset_cprefix(const char *name)
+{
+    const char *p;
+    int maxlen = 100;
+    int i;
+
+    if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
+        maxlen = (p - name) + 1;
+    for (i = 0; i < dictionary->num; i++)
+        if (!strncmp(name, dictionary->dict[i], maxlen))
+            return i;
+    return -1;
+}
+
 char *settings_name(int offset)
 {
     return dictionary->dict[offset];
@@ -90,15 +111,15 @@ static void read_settings_file(const char *path,
     {
         if (n->type != XML_ELEMENT_NODE)
             continue;
-        if (!strcmp(n->name, (xmlChar *) "set"))
+        if (!strcmp((const char *) n->name, "set"))
         {
             char *name, *target, *value, *user, *precedence;
 
-            name = xmlGetProp(n, (xmlChar *) "name");
-            target = xmlGetProp(n, (xmlChar *) "target");
-            value = xmlGetProp(n, (xmlChar *) "value");
-            user = xmlGetProp(n, (xmlChar *) "user");
-            precedence = xmlGetProp(n, (xmlChar *) "precedence");
+            name = (char *) xmlGetProp(n, (xmlChar *) "name");
+            target = (char *) xmlGetProp(n, (xmlChar *) "target");
+            value = (char *) xmlGetProp(n, (xmlChar *) "value");
+            user = (char *) xmlGetProp(n, (xmlChar *) "user");
+            precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
 
             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
             {
@@ -125,23 +146,23 @@ static void read_settings_file(const char *path,
                 if (user)
                     strcpy(userb, user);
                 else if (usera)
-                    strcpy(userb, usera);
+                    strcpy(userb, (const char *) usera);
                 else
                     set.user = "";
                 if (target)
                     strcpy(targetb, target);
                 else
-                    strcpy(targetb, targeta);
+                    strcpy(targetb, (const char *) targeta);
                 set.target = targetb;
                 if (name)
                     strcpy(nameb, name);
                 else
-                    strcpy(nameb, namea);
+                    strcpy(nameb, (const char *) namea);
                 set.name = nameb;
                 if (value)
                     strcpy(valueb, value);
                 else
-                    strcpy(valueb, valuea);
+                    strcpy(valueb, (const char *) valuea);
                 set.value = valueb;
                 set.next = 0;
                 (*fun)(&set);
@@ -201,10 +222,18 @@ static void read_settings(const char *path,
 static void prepare_dictionary(struct setting *set)
 {
     int i;
+    char *p;
 
+    if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
+        *(p + 1) = '\0';
     for (i = 0; i < dictionary->num; i++)
         if (!strcmp(dictionary->dict[i], set->name))
             return;
+    if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
+    {
+        yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
+        exit(1);
+    }
     // Create a new dictionary entry
     // Grow dictionary if necessary
     if (!dictionary->size)
@@ -230,16 +259,16 @@ static void update_database(void *context, struct database *db)
     if (!db->settings)
     {
         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
-        memset(db->settings, sizeof(struct settings*) * dictionary->num, 0);
+        memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
     }
-    if ((offset = settings_offset(set->name)) < 0)
+    if ((offset = settings_offset_cprefix(set->name)) < 0)
         abort(); // Should never get here
 
     // First we determine if this setting is overriding  any existing settings
     // with the same name.
     for (s = db->settings[offset], sp = &db->settings[offset]; s;
             sp = &s->next, s = s->next)
-        if (!strcmp(s->user, set->user))
+        if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
         {
             if (s->precedence < set->precedence)
                 // We discard the value (nmem keeps track of the space)
@@ -258,7 +287,7 @@ static void update_database(void *context, struct database *db)
     {
         struct setting *new = nmem_malloc(nmem, sizeof(*new));
 
-        memset(new, sizeof(*new), 0);
+        memset(new, 0, sizeof(*new));
         new->precedence = set->precedence;
         new->target = nmem_strdup(nmem, set->target);
         new->name = nmem_strdup(nmem, set->name);
@@ -306,8 +335,8 @@ void settings_read(const char *path)
     else
         nmem_reset(nmem);
     new = nmem_malloc(nmem, sizeof(*new));
+    memset(new, 0, sizeof(*new));
     initialize_hard_settings(new);
-    memset(new, sizeof(*new), 0);
     dictionary = new;
     read_settings(path, prepare_dictionary);
     read_settings(path, update_databases);