1 /* $Id: settings.c,v 1.23 2007-06-06 11:49:48 marc 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[] = {
66 struct setting_dictionary
73 static struct setting_dictionary *dictionary = 0;
75 // Returns size of settings directory
76 int settings_num(void)
78 return dictionary->num;
81 int settings_offset(const char *name)
87 for (i = 0; i < dictionary->num; i++)
88 if (!strcmp(name, dictionary->dict[i]))
93 // Ignores everything after second colon, if present
94 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
95 int settings_offset_cprefix(const char *name)
101 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
102 maxlen = (p - name) + 1;
103 for (i = 0; i < dictionary->num; i++)
104 if (!strncmp(name, dictionary->dict[i], maxlen))
109 char *settings_name(int offset)
111 return dictionary->dict[offset];
114 static int isdir(const char *path)
118 if (stat(path, &st) < 0)
120 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
123 return st.st_mode & S_IFDIR;
126 // Read settings from an XML file, calling handler function for each setting
127 static void read_settings_file(const char *path,
128 void (*fun)(struct setting *set))
130 xmlDoc *doc = xmlParseFile(path);
132 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
136 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
139 n = xmlDocGetRootElement(doc);
140 namea = xmlGetProp(n, (xmlChar *) "name");
141 targeta = xmlGetProp(n, (xmlChar *) "target");
142 valuea = xmlGetProp(n, (xmlChar *) "value");
143 usera = xmlGetProp(n, (xmlChar *) "user");
144 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
145 for (n = n->children; n; n = n->next)
147 if (n->type != XML_ELEMENT_NODE)
149 if (!strcmp((const char *) n->name, "set"))
151 char *name, *target, *value, *user, *precedence;
153 name = (char *) xmlGetProp(n, (xmlChar *) "name");
154 target = (char *) xmlGetProp(n, (xmlChar *) "target");
155 value = (char *) xmlGetProp(n, (xmlChar *) "value");
156 user = (char *) xmlGetProp(n, (xmlChar *) "user");
157 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
159 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
161 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
171 // Copy everything into a temporary buffer -- we decide
172 // later if we are keeping it.
174 set.precedence = atoi((char *) precedence);
175 else if (precedencea)
176 set.precedence = atoi((char *) precedencea);
180 strcpy(targetb, target);
182 strcpy(targetb, (const char *) targeta);
183 set.target = targetb;
187 strcpy(nameb, (const char *) namea);
190 strcpy(valueb, value);
192 strcpy(valueb, (const char *) valuea);
205 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
210 xmlFree(precedencea);
218 // Recursively read files or directories, invoking a
219 // callback for each one
220 static void read_settings(const char *path,
221 void (*fun)(struct setting *set))
229 if (!(d = opendir(path)))
231 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
234 while ((de = readdir(d)))
237 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
239 sprintf(tmp, "%s/%s", path, de->d_name);
240 read_settings(tmp, fun);
244 else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
245 read_settings_file(path, fun);
248 // Callback. Adds a new entry to the dictionary if necessary
249 // This is used in pass 1 to determine layout of dictionary
250 // and to load any databases mentioned
251 static void prepare_dictionary(struct setting *set)
256 // If target address is not wildcard, add the database
257 if (*set->target && set->target[strlen(set->target) - 1] != '*')
258 find_database(set->target, 0);
260 // Determine if we already have a dictionary entry
261 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
263 for (i = 0; i < dictionary->num; i++)
264 if (!strcmp(dictionary->dict[i], set->name))
267 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
269 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
273 // Create a new dictionary entry
274 // Grow dictionary if necessary
275 if (!dictionary->size)
276 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
277 else if (dictionary->num + 1 > dictionary->size)
279 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
280 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
281 dictionary->dict = tmp;
282 dictionary->size *= 2;
284 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
287 // This is called from grep_databases -- adds/overrides setting for a target
288 // This is also where the rules for precedence of settings are implemented
289 static void update_database(void *context, struct database *db)
291 struct setting *set = (struct setting *) context;
292 struct setting *s, **sp;
295 // Is this the right database?
296 if (!match_zurl(db->url, set->target))
300 // Initialize settings array if it doesn't exist.
301 // If so, also set the 'id' automatic setting
304 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
306 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
307 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
310 id->target = id->value = db->url;
312 db->settings[PZ_ID] = id;
315 if ((offset = settings_offset_cprefix(set->name)) < 0)
316 abort(); // Should never get here
318 // First we determine if this setting is overriding any existing settings
319 // with the same name.
320 for (s = db->settings[offset], sp = &db->settings[offset]; s;
321 sp = &s->next, s = s->next)
322 if (!strcmp(s->name, set->name))
324 if (s->precedence < set->precedence)
325 // We discard the value (nmem keeps track of the space)
327 else if (s->precedence > set->precedence)
328 // Db contains a higher-priority setting. Abort
330 if (*s->target == '*' && *set->target != '*')
331 // target-specific value trumps wildcard. Delete.
333 else if (*s->target != '*' && *set->target == '*')
334 // Db already contains higher-priority setting. Abort
337 if (!s) // s will be null when there are no higher-priority settings -- we add one
339 struct setting *new = nmem_malloc(nmem, sizeof(*new));
341 memset(new, 0, sizeof(*new));
342 new->precedence = set->precedence;
343 new->target = nmem_strdup(nmem, set->target);
344 new->name = nmem_strdup(nmem, set->name);
345 new->value = nmem_strdup(nmem, set->value);
346 new->next = db->settings[offset];
347 db->settings[offset] = new;
351 // Callback -- updates database records with dictionary entries as appropriate
352 // This is used in pass 2 to assign name/value pairs to databases
353 static void update_databases(struct setting *set)
355 grep_databases(set, 0, update_database);
358 // This simply copies the 'hard' (application-specific) settings
359 // to the settings dictionary.
360 static void initialize_hard_settings(struct setting_dictionary *dict)
362 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
363 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
364 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
365 dict->num = dict->size;
368 // If we ever decide we need to be able to specify multiple settings directories,
369 // the two calls to read_settings must be split -- so the dictionary is prepared
370 // for the contents of every directory before the databases are updated.
371 void settings_read(const char *path)
373 read_settings(path, prepare_dictionary);
374 read_settings(path, update_databases);
377 void init_settings(void)
379 struct setting_dictionary *new;
381 nmem = nmem_create();
384 new = nmem_malloc(nmem, sizeof(*new));
385 memset(new, 0, sizeof(*new));
386 initialize_hard_settings(new);
393 * indent-tabs-mode: nil
395 * vim: shiftwidth=4 tabstop=8 expandtab