1 /* $Id: settings.c,v 1.27 2008-02-20 06:22:32 quinn Exp $
2 Copyright (c) 2006-2007, Index Data.
4 This file is part of Pazpar2.
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE. If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
23 // This module implements a generic system of settings
24 // (attribute-value) that can be associated with search targets. The
25 // system supports both default values, per-target overrides, and
30 #include <sys/types.h>
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
47 // Used for initializing setting_dictionary with pazpar2-specific settings
48 static char *hard_settings[] = {
67 struct setting_dictionary
74 static struct setting_dictionary *dictionary = 0;
76 // This establishes the precedence of wildcard expressions
77 #define SETTING_WILDCARD_NO 0 // No wildcard
78 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
79 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
81 // Returns size of settings directory
82 int settings_num(void)
84 return dictionary->num;
87 int settings_offset(const char *name)
93 for (i = 0; i < dictionary->num; i++)
94 if (!strcmp(name, dictionary->dict[i]))
99 // Ignores everything after second colon, if present
100 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
101 int settings_offset_cprefix(const char *name)
107 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
108 maxlen = (p - name) + 1;
109 for (i = 0; i < dictionary->num; i++)
110 if (!strncmp(name, dictionary->dict[i], maxlen))
115 char *settings_name(int offset)
117 return dictionary->dict[offset];
120 static int isdir(const char *path)
124 if (stat(path, &st) < 0)
126 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
129 return st.st_mode & S_IFDIR;
132 // Read settings from an XML file, calling handler function for each setting
133 static void read_settings_file(const char *path,
134 void (*fun)(struct setting *set))
136 xmlDoc *doc = xmlParseFile(path);
138 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
142 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
145 n = xmlDocGetRootElement(doc);
146 namea = xmlGetProp(n, (xmlChar *) "name");
147 targeta = xmlGetProp(n, (xmlChar *) "target");
148 valuea = xmlGetProp(n, (xmlChar *) "value");
149 usera = xmlGetProp(n, (xmlChar *) "user");
150 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
151 for (n = n->children; n; n = n->next)
153 if (n->type != XML_ELEMENT_NODE)
155 if (!strcmp((const char *) n->name, "set"))
157 char *name, *target, *value, *user, *precedence;
159 name = (char *) xmlGetProp(n, (xmlChar *) "name");
160 target = (char *) xmlGetProp(n, (xmlChar *) "target");
161 value = (char *) xmlGetProp(n, (xmlChar *) "value");
162 user = (char *) xmlGetProp(n, (xmlChar *) "user");
163 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
165 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
167 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
177 // Copy everything into a temporary buffer -- we decide
178 // later if we are keeping it.
180 set.precedence = atoi((char *) precedence);
181 else if (precedencea)
182 set.precedence = atoi((char *) precedencea);
186 strcpy(targetb, target);
188 strcpy(targetb, (const char *) targeta);
189 set.target = targetb;
193 strcpy(nameb, (const char *) namea);
196 strcpy(valueb, value);
198 strcpy(valueb, (const char *) valuea);
211 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
216 xmlFree(precedencea);
224 // Recursively read files or directories, invoking a
225 // callback for each one
226 static void read_settings(const char *path,
227 void (*fun)(struct setting *set))
235 if (!(d = opendir(path)))
237 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
240 while ((de = readdir(d)))
243 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
245 sprintf(tmp, "%s/%s", path, de->d_name);
246 read_settings(tmp, fun);
250 else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
251 read_settings_file(path, fun);
254 // Determines if a ZURL is a wildcard, and what kind
255 static int zurl_wildcard(const char *zurl)
258 return SETTING_WILDCARD_NO;
260 return SETTING_WILDCARD_YES;
261 else if (*(zurl + strlen(zurl) - 1) == '*')
262 return SETTING_WILDCARD_DB;
264 return SETTING_WILDCARD_NO;
267 // Callback. Adds a new entry to the dictionary if necessary
268 // This is used in pass 1 to determine layout of dictionary
269 // and to load any databases mentioned
270 static void prepare_dictionary(struct setting *set)
275 // If target address is not wildcard, add the database
276 if (*set->target && !zurl_wildcard(set->target))
277 find_database(set->target, 0);
279 // Determine if we already have a dictionary entry
280 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
282 for (i = 0; i < dictionary->num; i++)
283 if (!strcmp(dictionary->dict[i], set->name))
286 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
288 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
292 // Create a new dictionary entry
293 // Grow dictionary if necessary
294 if (!dictionary->size)
295 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
296 else if (dictionary->num + 1 > dictionary->size)
298 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
299 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
300 dictionary->dict = tmp;
301 dictionary->size *= 2;
303 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
306 // This is called from grep_databases -- adds/overrides setting for a target
307 // This is also where the rules for precedence of settings are implemented
308 static void update_database(void *context, struct database *db)
310 struct setting *set = (struct setting *) context;
311 struct setting *s, **sp;
314 // Is this the right database?
315 if (!match_zurl(db->url, set->target))
319 // Initialize settings array if it doesn't exist.
320 // If so, also set the 'id' automatic setting
323 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
325 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
326 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
329 id->target = id->value = db->url;
331 db->settings[PZ_ID] = id;
334 if ((offset = settings_offset_cprefix(set->name)) < 0)
335 abort(); // Should never get here
337 // First we determine if this setting is overriding any existing settings
338 // with the same name.
339 for (s = db->settings[offset], sp = &db->settings[offset]; s;
340 sp = &s->next, s = s->next)
341 if (!strcmp(s->name, set->name))
343 if (s->precedence < set->precedence)
344 // We discard the value (nmem keeps track of the space)
345 *sp = (*sp)->next; // unlink value from existing setting
346 else if (s->precedence > set->precedence)
347 // Db contains a higher-priority setting. Abort search
349 if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
350 // target-specific value trumps wildcard. Delete.
351 *sp = (*sp)->next; // unlink.....
352 else if (!zurl_wildcard(s->target))
353 // Db already contains higher-priority setting. Abort search
356 if (!s) // s will be null when there are no higher-priority settings -- we add one
358 struct setting *new = nmem_malloc(nmem, sizeof(*new));
360 memset(new, 0, sizeof(*new));
361 new->precedence = set->precedence;
362 new->target = nmem_strdup(nmem, set->target);
363 new->name = nmem_strdup(nmem, set->name);
364 new->value = nmem_strdup(nmem, set->value);
365 new->next = db->settings[offset];
366 db->settings[offset] = new;
370 // Callback -- updates database records with dictionary entries as appropriate
371 // This is used in pass 2 to assign name/value pairs to databases
372 static void update_databases(struct setting *set)
374 predef_grep_databases(set, 0, update_database);
377 // This simply copies the 'hard' (application-specific) settings
378 // to the settings dictionary.
379 static void initialize_hard_settings(struct setting_dictionary *dict)
381 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
382 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
383 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
384 dict->num = dict->size;
387 // Read any settings names introduced in service definition (config) and add to dictionary
388 // This is done now to avoid errors if user settings are declared in session overrides
389 static void initialize_soft_settings()
391 struct conf_service *service = config->servers->service;
394 for (i = 0; i < service->num_metadata; i++)
397 struct conf_metadata *md = &service->metadata[i];
399 if (md->setting == Metadata_setting_no)
407 prepare_dictionary(&set);
411 // If we ever decide we need to be able to specify multiple settings directories,
412 // the two calls to read_settings must be split -- so the dictionary is prepared
413 // for the contents of every directory before the databases are updated.
414 void settings_read(const char *path)
416 read_settings(path, prepare_dictionary);
417 read_settings(path, update_databases);
420 void init_settings(void)
422 struct setting_dictionary *new;
424 nmem = nmem_create();
427 new = nmem_malloc(nmem, sizeof(*new));
428 memset(new, 0, sizeof(*new));
429 initialize_hard_settings(new);
431 initialize_soft_settings();
437 * indent-tabs-mode: nil
439 * vim: shiftwidth=4 tabstop=8 expandtab