1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2009 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 // This module implements a generic system of settings
21 // (attribute-value) that can be associated with search targets. The
22 // system supports both default values, per-target overrides, and
33 #include <sys/types.h>
38 #include <libxml/parser.h>
39 #include <libxml/tree.h>
48 // Used for initializing setting_dictionary with pazpar2-specific settings
49 static char *hard_settings[] = {
71 struct setting_dictionary
78 // This establishes the precedence of wildcard expressions
79 #define SETTING_WILDCARD_NO 0 // No wildcard
80 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
81 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
83 // Returns size of settings directory
84 int settings_num(struct conf_service *service)
86 return service->dictionary->num;
89 int settings_offset(struct conf_service *service, const char *name)
95 for (i = 0; i < service->dictionary->num; i++)
96 if (!strcmp(name, service->dictionary->dict[i]))
101 // Ignores everything after second colon, if present
102 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
103 int settings_offset_cprefix(struct conf_service *service, const char *name)
109 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
110 maxlen = (p - name) + 1;
111 for (i = 0; i < service->dictionary->num; i++)
112 if (!strncmp(name, service->dictionary->dict[i], maxlen))
117 char *settings_name(struct conf_service *service, int offset)
119 return service->dictionary->dict[offset];
122 static int isdir(const char *path)
126 if (stat(path, &st) < 0)
128 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
131 return st.st_mode & S_IFDIR;
134 // Read settings from an XML file, calling handler function for each setting
135 static void read_settings_node(xmlNode *n,
136 struct conf_service *service,
137 void (*fun)(struct conf_service *service,
138 struct setting *set))
140 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
142 namea = xmlGetProp(n, (xmlChar *) "name");
143 targeta = xmlGetProp(n, (xmlChar *) "target");
144 valuea = xmlGetProp(n, (xmlChar *) "value");
145 usera = xmlGetProp(n, (xmlChar *) "user");
146 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
147 for (n = n->children; n; n = n->next)
149 if (n->type != XML_ELEMENT_NODE)
151 if (!strcmp((const char *) n->name, "set"))
153 char *name, *target, *value, *user, *precedence;
155 name = (char *) xmlGetProp(n, (xmlChar *) "name");
156 target = (char *) xmlGetProp(n, (xmlChar *) "target");
157 value = (char *) xmlGetProp(n, (xmlChar *) "value");
158 user = (char *) xmlGetProp(n, (xmlChar *) "user");
159 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
161 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
163 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
173 // Copy everything into a temporary buffer -- we decide
174 // later if we are keeping it.
176 set.precedence = atoi((char *) precedence);
177 else if (precedencea)
178 set.precedence = atoi((char *) precedencea);
182 strcpy(targetb, target);
184 strcpy(targetb, (const char *) targeta);
185 set.target = targetb;
189 strcpy(nameb, (const char *) namea);
192 strcpy(valueb, value);
194 strcpy(valueb, (const char *) valuea);
197 (*fun)(service, &set);
207 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
212 xmlFree(precedencea);
218 static void read_settings_file(const char *path,
219 struct conf_service *service,
220 void (*fun)(struct conf_service *service,
221 struct setting *set))
223 xmlDoc *doc = xmlParseFile(path);
228 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
231 n = xmlDocGetRootElement(doc);
232 read_settings_node(n, service, fun);
238 // Recursively read files or directories, invoking a
239 // callback for each one
240 static void read_settings(const char *path,
241 struct conf_service *service,
242 void (*fun)(struct conf_service *service,
243 struct setting *set))
251 if (!(d = opendir(path)))
253 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
256 while ((de = readdir(d)))
259 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
261 sprintf(tmp, "%s/%s", path, de->d_name);
262 read_settings(tmp, service, fun);
266 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
267 read_settings_file(path, service, fun);
270 // Determines if a ZURL is a wildcard, and what kind
271 static int zurl_wildcard(const char *zurl)
274 return SETTING_WILDCARD_NO;
276 return SETTING_WILDCARD_YES;
277 else if (*(zurl + strlen(zurl) - 1) == '*')
278 return SETTING_WILDCARD_DB;
280 return SETTING_WILDCARD_NO;
283 // Callback. Adds a new entry to the dictionary if necessary
284 // This is used in pass 1 to determine layout of dictionary
285 // and to load any databases mentioned
286 static void prepare_dictionary(struct conf_service *service,
289 struct setting_dictionary *dictionary = service->dictionary;
294 // Determine if we already have a dictionary entry
295 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
297 for (i = 0; i < dictionary->num; i++)
298 if (!strcmp(dictionary->dict[i], set->name))
301 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
303 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
307 // Create a new dictionary entry
308 // Grow dictionary if necessary
309 if (!dictionary->size)
311 nmem_malloc(service->nmem, (dictionary->size = 50) * sizeof(char*));
312 else if (dictionary->num + 1 > dictionary->size)
315 nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
316 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
317 dictionary->dict = tmp;
318 dictionary->size *= 2;
320 dictionary->dict[dictionary->num++] = nmem_strdup(service->nmem, set->name);
324 struct update_database_context {
326 struct conf_service *service;
329 // This is called from grep_databases -- adds/overrides setting for a target
330 // This is also where the rules for precedence of settings are implemented
331 static void update_database(void *context, struct database *db)
333 struct setting *set = ((struct update_database_context *)
335 struct conf_service *service = ((struct update_database_context *)
337 struct setting *s, **sp;
340 // Is this the right database?
341 if (!match_zurl(db->url, set->target))
344 if ((offset = settings_offset_cprefix(service, set->name)) < 0)
347 // First we determine if this setting is overriding any existing settings
348 // with the same name.
349 for (s = db->settings[offset], sp = &db->settings[offset]; s;
350 sp = &s->next, s = s->next)
351 if (!strcmp(s->name, set->name))
353 if (s->precedence < set->precedence)
354 // We discard the value (nmem keeps track of the space)
355 *sp = (*sp)->next; // unlink value from existing setting
356 else if (s->precedence > set->precedence)
357 // Db contains a higher-priority setting. Abort search
359 if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
360 // target-specific value trumps wildcard. Delete.
361 *sp = (*sp)->next; // unlink.....
362 else if (!zurl_wildcard(s->target))
363 // Db already contains higher-priority setting. Abort search
366 if (!s) // s will be null when there are no higher-priority settings -- we add one
368 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
370 memset(new, 0, sizeof(*new));
371 new->precedence = set->precedence;
372 new->target = nmem_strdup(service->nmem, set->target);
373 new->name = nmem_strdup(service->nmem, set->name);
374 new->value = nmem_strdup(service->nmem, set->value);
375 new->next = db->settings[offset];
376 db->settings[offset] = new;
380 // Callback -- updates database records with dictionary entries as appropriate
381 // This is used in pass 2 to assign name/value pairs to databases
382 static void update_databases(struct conf_service *service,
385 struct update_database_context context;
387 context.service = service;
388 predef_grep_databases(&context, service, 0, update_database);
391 // This simply copies the 'hard' (application-specific) settings
392 // to the settings dictionary.
393 static void initialize_hard_settings(struct conf_service *service)
395 struct setting_dictionary *dict = service->dictionary;
396 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
397 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
398 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
399 dict->num = dict->size;
402 // Read any settings names introduced in service definition (config) and add to dictionary
403 // This is done now to avoid errors if user settings are declared in session overrides
404 static void initialize_soft_settings(struct conf_service *service)
408 for (i = 0; i < service->num_metadata; i++)
411 struct conf_metadata *md = &service->metadata[i];
413 if (md->setting == Metadata_setting_no)
421 prepare_dictionary(service, &set);
425 static void prepare_target_dictionary(struct conf_service *service,
428 struct setting_dictionary *dictionary = service->dictionary;
433 // If target address is not wildcard, add the database
434 if (*set->target && !zurl_wildcard(set->target))
435 find_database(set->target, 0, service);
437 // Determine if we already have a dictionary entry
438 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
440 for (i = 0; i < dictionary->num; i++)
441 if (!strcmp(dictionary->dict[i], set->name))
443 yaz_log(YLOG_WARN, "Setting '%s' not configured as metadata", set->name);
446 void init_settings(struct conf_service *service)
448 struct setting_dictionary *new;
450 assert(service->nmem);
452 new = nmem_malloc(service->nmem, sizeof(*new));
453 memset(new, 0, sizeof(*new));
454 service->dictionary = new;
455 initialize_hard_settings(service);
456 initialize_soft_settings(service);
459 void settings_read_file(struct conf_service *service, const char *path,
463 read_settings(path, service, prepare_target_dictionary);
465 read_settings(path, service, update_databases);
468 void settings_read_node(struct conf_service *service, xmlNode *n,
472 read_settings_node(n, service, prepare_target_dictionary);
474 read_settings_node(n, service, update_databases);
480 * c-file-style: "Stroustrup"
481 * indent-tabs-mode: nil
483 * vim: shiftwidth=4 tabstop=8 expandtab