+ char **dict;
+ int size;
+ int num;
+};
+
+// This establishes the precedence of wildcard expressions
+#define SETTING_WILDCARD_NO 0 // No wildcard
+#define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
+#define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
+
+// Returns size of settings directory
+int settings_num(struct conf_service *service)
+{
+ return service->dictionary->num;
+}
+
+static int settings_lookup(struct conf_service *service, const char *name,
+ int allow_create)
+{
+ size_t maxlen;
+ int i;
+ const char *p;
+ struct setting_dictionary *dictionary = service->dictionary;
+
+ assert(name);
+
+ if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
+ maxlen = (p - name) + 1;
+ else
+ maxlen = strlen(name) + 1;
+ for (i = 0; i < dictionary->num; i++)
+ if (!strncmp(name, dictionary->dict[i], maxlen))
+ return i;
+ if (!allow_create)
+ return -1;
+ if (!strncmp("pz:", name, 3))
+ yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
+ if (dictionary->num + 1 > dictionary->size)
+ {
+ char **tmp =
+ nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
+ memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
+ dictionary->dict = tmp;
+ dictionary->size *= 2;
+ }
+ dictionary->dict[dictionary->num] = nmem_strdup(service->nmem, name);
+ dictionary->dict[dictionary->num][maxlen-1] = '\0';
+ return dictionary->num++;
+}
+
+int settings_create_offset(struct conf_service *service, const char *name)
+{
+ return settings_lookup(service, name, 1);
+}
+
+int settings_lookup_offset(struct conf_service *service, const char *name)
+{
+ return settings_lookup(service, name, 0);
+}
+
+char *settings_name(struct conf_service *service, int offset)
+{
+ assert(offset < service->dictionary->num);
+ return service->dictionary->dict[offset];
+}
+
+static int isdir(const char *path)
+{
+ struct stat st;
+
+ if (stat(path, &st) < 0)
+ {
+ yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
+ exit(1);
+ }
+ return st.st_mode & S_IFDIR;
+}
+
+// Read settings from an XML file, calling handler function for each setting
+void settings_read_node_x(xmlNode *n,
+ void *client_data,
+ void (*fun)(void *client_data,
+ struct setting *set))
+{
+ xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
+
+ namea = xmlGetProp(n, (xmlChar *) "name");
+ targeta = xmlGetProp(n, (xmlChar *) "target");
+ valuea = xmlGetProp(n, (xmlChar *) "value");
+ usera = xmlGetProp(n, (xmlChar *) "user");
+ precedencea = xmlGetProp(n, (xmlChar *) "precedence");
+ for (n = n->children; n; n = n->next)
+ {
+ if (n->type != XML_ELEMENT_NODE)
+ continue;
+ if (!strcmp((const char *) n->name, "set"))
+ {
+ char *name, *target, *value, *user, *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))
+ {
+ yaz_log(YLOG_FATAL, "set must specify name, value, and target");
+ exit(1);
+ }
+ else
+ {
+ struct setting set;
+ char nameb[1024];
+ char targetb[1024];
+ char valueb[1024];
+
+ // Copy everything into a temporary buffer -- we decide
+ // later if we are keeping it.
+ if (precedence)
+ set.precedence = atoi((char *) precedence);
+ else if (precedencea)
+ set.precedence = atoi((char *) precedencea);
+ else
+ set.precedence = 0;
+ if (target)
+ strcpy(targetb, target);
+ else
+ strcpy(targetb, (const char *) targeta);
+ set.target = targetb;
+ if (name)
+ strcpy(nameb, name);
+ else
+ strcpy(nameb, (const char *) namea);
+ set.name = nameb;
+ if (value)
+ strcpy(valueb, value);
+ else
+ strcpy(valueb, (const char *) valuea);
+ set.value = valueb;
+ set.next = 0;
+ (*fun)(client_data, &set);
+ }
+ xmlFree(name);
+ xmlFree(precedence);
+ xmlFree(value);
+ xmlFree(user);
+ xmlFree(target);
+ }
+ else
+ {
+ yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
+ exit(1);
+ }
+ }
+ xmlFree(namea);
+ xmlFree(precedencea);
+ xmlFree(valuea);
+ xmlFree(usera);
+ xmlFree(targeta);
+}
+
+static void read_settings_file(const char *path,
+ void *client_data,
+ void (*fun)(void *client_data,
+ struct setting *set))
+{
+ xmlDoc *doc = xmlParseFile(path);
+ xmlNode *n;
+
+ if (!doc)
+ {
+ yaz_log(YLOG_FATAL, "Failed to parse %s", path);
+ exit(1);
+ }
+ n = xmlDocGetRootElement(doc);
+ settings_read_node_x(n, client_data, fun);
+
+ xmlFreeDoc(doc);
+}
+
+
+// Recursively read files or directories, invoking a
+// callback for each one
+static void read_settings(const char *path,
+ void *client_data,
+ void (*fun)(void *client_data,
+ struct setting *set))
+{
+ DIR *d;
+ struct dirent *de;
+ char *dot;
+
+ if (isdir(path))
+ {
+ 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);
+ read_settings(tmp, client_data, fun);
+ }
+ closedir(d);
+ }
+ else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
+ read_settings_file(path, client_data, fun);
+}
+
+// Determines if a ZURL is a wildcard, and what kind
+static int zurl_wildcard(const char *zurl)
+{
+ if (!zurl)
+ return SETTING_WILDCARD_NO;
+ if (*zurl == '*')
+ return SETTING_WILDCARD_YES;
+ else if (*(zurl + strlen(zurl) - 1) == '*')
+ return SETTING_WILDCARD_DB;
+ else
+ return SETTING_WILDCARD_NO;
+}
+
+struct update_database_context {
+ struct setting *set;
+ struct conf_service *service;