1 /* $Id: settings.c,v 1.13 2007-04-11 18:42:25 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 (attribute-value) that can
24 // be associated with search targets. The system supports both default values,
25 // per-target overrides, and per-user settings.
29 #include <sys/types.h>
34 #include <libxml/parser.h>
35 #include <libxml/tree.h>
46 // Used for initializing setting_dictionary with pazpar2-specific settings
47 static char *hard_settings[] = {
63 struct setting_dictionary
70 static struct setting_dictionary *dictionary = 0;
72 // Returns size of settings directory
73 int settings_num(void)
75 return dictionary->num;
78 int settings_offset(const char *name)
84 for (i = 0; i < dictionary->num; i++)
85 if (!strcmp(name, dictionary->dict[i]))
90 // Ignores everything after second colon, if present
91 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
92 static int settings_offset_cprefix(const char *name)
98 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
99 maxlen = (p - name) + 1;
100 for (i = 0; i < dictionary->num; i++)
101 if (!strncmp(name, dictionary->dict[i], maxlen))
106 char *settings_name(int offset)
108 return dictionary->dict[offset];
111 static int isdir(const char *path)
115 if (stat(path, &st) < 0)
117 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
120 return st.st_mode & S_IFDIR;
123 // Read settings from an XML file, calling handler function for each setting
124 static void read_settings_file(const char *path,
125 void (*fun)(struct setting *set))
127 xmlDoc *doc = xmlParseFile(path);
129 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
133 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
136 n = xmlDocGetRootElement(doc);
137 namea = xmlGetProp(n, (xmlChar *) "name");
138 targeta = xmlGetProp(n, (xmlChar *) "target");
139 valuea = xmlGetProp(n, (xmlChar *) "value");
140 usera = xmlGetProp(n, (xmlChar *) "user");
141 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
142 for (n = n->children; n; n = n->next)
144 if (n->type != XML_ELEMENT_NODE)
146 if (!strcmp((const char *) n->name, "set"))
148 char *name, *target, *value, *user, *precedence;
150 name = (char *) xmlGetProp(n, (xmlChar *) "name");
151 target = (char *) xmlGetProp(n, (xmlChar *) "target");
152 value = (char *) xmlGetProp(n, (xmlChar *) "value");
153 user = (char *) xmlGetProp(n, (xmlChar *) "user");
154 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
156 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
158 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
169 // Copy everything into a temporary buffer -- we decide
170 // later if we are keeping it.
172 set.precedence = atoi((char *) precedence);
173 else if (precedencea)
174 set.precedence = atoi((char *) precedencea);
181 strcpy(userb, (const char *) usera);
185 strcpy(targetb, target);
187 strcpy(targetb, (const char *) targeta);
188 set.target = targetb;
192 strcpy(nameb, (const char *) namea);
195 strcpy(valueb, value);
197 strcpy(valueb, (const char *) valuea);
210 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
215 xmlFree(precedencea);
223 // Recursively read files in a directory structure, calling
224 // callback for each one
225 static void read_settings(const char *path,
226 void (*fun)(struct setting *set))
231 if (!(d = opendir(path)))
233 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
236 while ((de = readdir(d)))
239 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
241 sprintf(tmp, "%s/%s", path, de->d_name);
243 read_settings(tmp, fun);
247 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
248 read_settings_file(tmp, fun);
254 // Callback. Adds a new entry to the dictionary if necessary
255 // This is used in pass 1 to determine layout of dictionary
256 // and to load any databases mentioned
257 static void prepare_dictionary(struct setting *set)
262 // If target address is not wildcard, add the database
263 if (*set->target && set->target[strlen(set->target) - 1] != '*')
264 find_database(set->target, 0);
266 // Determine if we already have a dictionary entry
267 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
269 for (i = 0; i < dictionary->num; i++)
270 if (!strcmp(dictionary->dict[i], set->name))
273 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
275 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
278 // Create a new dictionary entry
279 // Grow dictionary if necessary
280 if (!dictionary->size)
281 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
282 else if (dictionary->num + 1 > dictionary->size)
284 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
285 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
286 dictionary->dict = tmp;
287 dictionary->size *= 2;
289 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
292 // This is called from grep_databases -- adds/overrides setting for a target
293 // This is also where the rules for precedence of settings are implemented
294 static void update_database(void *context, struct database *db)
296 struct setting *set = (struct setting *) context;
297 struct setting *s, **sp;
300 // Is this the right database?
301 if (!match_zurl(db->url, set->target))
304 // Initialize settings array if it doesn't exist.
305 // If so, also set the 'id' automatic setting
308 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
310 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
311 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
314 id->target = id->value = db->url;
317 db->settings[PZ_ID] = id;
319 if ((offset = settings_offset_cprefix(set->name)) < 0)
320 abort(); // Should never get here
322 // First we determine if this setting is overriding any existing settings
323 // with the same name.
324 for (s = db->settings[offset], sp = &db->settings[offset]; s;
325 sp = &s->next, s = s->next)
326 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
328 if (s->precedence < set->precedence)
329 // We discard the value (nmem keeps track of the space)
331 else if (s->precedence > set->precedence)
332 // Db contains a higher-priority setting. Abort
334 if (*s->target == '*' && *set->target != '*')
335 // target-specific value trumps wildcard. Delete.
337 else if (*s->target != '*' && *set->target == '*')
338 // Db already contains higher-priority setting. Abort
341 if (!s) // s will be null when there are no higher-priority settings -- we add one
343 struct setting *new = nmem_malloc(nmem, sizeof(*new));
345 memset(new, 0, sizeof(*new));
346 new->precedence = set->precedence;
347 new->target = nmem_strdup(nmem, set->target);
348 new->name = nmem_strdup(nmem, set->name);
349 new->value = nmem_strdup(nmem, set->value);
350 new->user = nmem_strdup(nmem, set->user);
351 new->next = db->settings[offset];
352 db->settings[offset] = new;
356 // Callback -- updates database records with dictionary entries as appropriate
357 // This is used in pass 2 to assign name/value pairs to databases
358 static void update_databases(struct setting *set)
360 grep_databases(set, 0, update_database);
363 // This simply copies the 'hard' (application-specific) settings
364 // to the settings dictionary.
365 static void initialize_hard_settings(struct setting_dictionary *dict)
367 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
368 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
369 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
370 dict->num = dict->size;
373 // If we ever decide we need to be able to specify multiple settings directories,
374 // the two calls to read_settings must be split -- so the dictionary is prepared
375 // for the contents of every directory before the databases are updated.
376 void settings_read(const char *path)
378 struct setting_dictionary *new;
380 nmem = nmem_create();
383 new = nmem_malloc(nmem, sizeof(*new));
384 memset(new, 0, sizeof(*new));
385 initialize_hard_settings(new);
387 read_settings(path, prepare_dictionary);
388 read_settings(path, update_databases);
394 * indent-tabs-mode: nil
396 * vim: shiftwidth=4 tabstop=8 expandtab