More work on settings system.. still not functional.
authorSebastian Hammer <quinn@indexdata.com>
Wed, 28 Mar 2007 04:33:41 +0000 (04:33 +0000)
committerSebastian Hammer <quinn@indexdata.com>
Wed, 28 Mar 2007 04:33:41 +0000 (04:33 +0000)
src/pazpar2.c
src/pazpar2.h
src/settings.c
src/settings.h

index 776b78c..1a8fdfa 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: pazpar2.c,v 1.54 2007-03-27 11:25:57 marc Exp $ */
+/* $Id: pazpar2.c,v 1.55 2007-03-28 04:33:41 quinn Exp $ */
 
 #include <stdlib.h>
 #include <stdio.h>
@@ -40,6 +40,7 @@
 #include "relevance.h"
 #include "config.h"
 #include "database.h"
+#include "settings.h"
 
 #define MAX_CHUNK 15
 
@@ -74,6 +75,7 @@ struct parameters global_parameters =
     "",
     "",
     "",
+    "",
     0,
     0,
     30,
@@ -1659,7 +1661,7 @@ int main(int argc, char **argv)
 
     yaz_log_init(YLOG_DEFAULT_LEVEL, "pazpar2", 0);
 
-    while ((ret = options("f:x:h:p:z:C:s:d", argv, argc, &arg)) != -2)
+    while ((ret = options("t:f:x:h:p:z:C:s:d", argv, argc, &arg)) != -2)
     {
        switch (ret) {
             case 'f':
@@ -1678,6 +1680,9 @@ int main(int argc, char **argv)
             case 'z':
                 strcpy(global_parameters.zproxy_override, arg);
                 break;
+            case 't':
+                strcpy(global_parameters.settings_path, arg);
+                break;
             case 's':
                 load_simpletargets(arg);
                 break;
@@ -1708,6 +1713,8 @@ int main(int argc, char **argv)
     start_proxy();
     start_zproxy();
 
+    if (*global_parameters.settings_path)
+        settings_read(global_parameters.settings_path);
     if (!global_parameters.ccl_filter)
         global_parameters.ccl_filter = load_cclfile("../etc/default.bib");
     global_parameters.yaz_marc = yaz_marc_create();
index a66956e..ee89ef0 100644 (file)
@@ -190,6 +190,7 @@ struct parameters {
     char proxy_override[128];
     char listener_override[128];
     char zproxy_override[128];
+    char settings_path[128];
     struct conf_server *server;
     int dump_records;
     int timeout;               /* operations timeout, in seconds */
index 6ec2d74..0593d41 100644 (file)
-// $Id: settings.c,v 1.1 2007-03-27 15:31:34 quinn Exp $
+// $Id: settings.c,v 1.2 2007-03-28 04:33:41 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>
+#include <sys/types.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+#include <libxml/parser.h>
+#include <libxml/tree.h>
 
 #include <yaz/nmem.h>
+#include <yaz/log.h>
 
 static NMEM nmem = 0;
 
+#define PZ_PIGGYBACK    0 
+#define PZ_ELEMENTS     1
+#define PZ_SYNTAX       2
+
+static char *hard_settings[] = {
+    "pz:piggyback",
+    "pz:elements",
+    "pz::syntax",
+    0
+};
+
 struct setting
 {
+    char *target;
+    char *name;
+    char *value;
+    char *user;
 };
 
 struct setting_dictionary
 {
+    char **dict;
+    int size;
+    int num;
 };
 
+static int isdir(const char *path)
+{
+    struct stat st;
+
+    if (stat(path, &st) < 0)
+    {
+        yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
+        exit(1);
+    }
+    return st.st_mode & S_IFDIR;
+}
+
+static void read_settings_file(const char *path, void *context,
+        void (*fun)(void *context, struct setting *set))
+{
+    xmlDoc *doc = xmlParseFile(path);
+    xmlNode *n;
+    //xmlChar *namea, *targeta, *valuea, *usera;
+
+    if (!doc)
+    {
+        yaz_log(YLOG_FATAL, "Failed to parse %s", path);
+        exit(1);
+    }
+    n = xmlDocGetRootElement(doc);
+
+    for (n = n->children; n; n = n->next)
+    {
+        fprintf(stderr, "Node name: %s\n", n->name);
+    }
+}
 // Recursively read files in a directory structure, calling 
 static void read_settings(const char *path, void *context,
                void (*fun)(void *context, struct setting *set))
 {
+    DIR *d;
+    struct dirent *de;
+
+    if (!(d = opendir(path)))
+    {
+        yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
+        exit(1);
+    }
+    while ((de = readdir(d)))
+    {
+        char tmp[1024];
+        if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
+            continue;
+        sprintf(tmp, "%s/%s", path, de->d_name);
+        if (isdir(tmp))
+            read_settings(tmp, context, fun);
+        else
+        {
+            char *dot;
+            if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
+                read_settings_file(tmp, context, fun);
+        }
+    }
+    closedir(d);
 }
 
+// Callback. Adds a new entry to the dictionary if necessary
+// This is used in pass 1 to determine layout of dictionary
 static void prepare_dictionary(void *context, struct setting *set)
 {
+    struct setting_dictionary *dict = (struct setting_dictionary *) context;
+    int i;
+
+    for (i = 0; i < dict->num; i++)
+        if (!strcmp(set->name, set->name))
+            return;
+    // Create a new dictionary entry
+    // Grow dictionary if necessary
+    if (!dict->size)
+        dict->dict = nmem_malloc(nmem, (dict->size = 50) * sizeof(char*));
+    else if (dict->num + 1 > dict->size)
+    {
+        char **tmp = nmem_malloc(nmem, dict->size * 2 * sizeof(char*));
+        memcpy(tmp, dict->dict, dict->size * sizeof(char*));
+        dict->dict = tmp;
+        dict->size *= 2;
+    }
+    dict->dict[dict->num++] = nmem_strdup(nmem, set->name);
 }
 
+#ifdef GAGA
+// Callback -- updates database records with dictionary entries as appropriate
 static void update_databases(void *context, struct setting *set)
 {
 }
+#endif
+
+// This simply copies the 'hard' (application-specific) settings
+// to the settings dictionary.
+static void initialize_hard_settings(struct setting_dictionary *dict)
+{
+    dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
+    dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
+    memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
+    dict->num = dict->size;
+}
 
 // If we ever decide we need to be able to specify multiple settings directories,
 // the two calls to read_settings must be split -- so the dictionary is prepared
@@ -43,9 +159,10 @@ void settings_read(const char *path)
     else
         nmem_reset(nmem);
     new = nmem_malloc(nmem, sizeof(*new));
+    initialize_hard_settings(new);
     memset(new, sizeof(*new), 0);
     read_settings(path, new, prepare_dictionary);
-    read_settings(path, new, update_databases);
+    //read_settings(path, new, update_databases);
 }
 
 /*
index 858b4af..ffe4af2 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef SETTINGS_H
 #define SETTINGS_H
 
+void settings_read(const char *path);
+
 #endif
 
 /*