1 /* $Id: settings.c,v 1.10 2007-04-10 08:48:56 adam 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[] = {
61 struct setting_dictionary
68 static struct setting_dictionary *dictionary = 0;
70 int settings_offset(const char *name)
76 for (i = 0; i < dictionary->num; i++)
77 if (!strcmp(name, dictionary->dict[i]))
82 // Ignores everything after second colon, if present
83 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
84 static int settings_offset_cprefix(const char *name)
90 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
91 maxlen = (p - name) + 1;
92 for (i = 0; i < dictionary->num; i++)
93 if (!strncmp(name, dictionary->dict[i], maxlen))
98 char *settings_name(int offset)
100 return dictionary->dict[offset];
103 static int isdir(const char *path)
107 if (stat(path, &st) < 0)
109 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
112 return st.st_mode & S_IFDIR;
115 // Read settings from an XML file, calling handler function for each setting
116 static void read_settings_file(const char *path,
117 void (*fun)(struct setting *set))
119 xmlDoc *doc = xmlParseFile(path);
121 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
125 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
128 n = xmlDocGetRootElement(doc);
129 namea = xmlGetProp(n, (xmlChar *) "name");
130 targeta = xmlGetProp(n, (xmlChar *) "target");
131 valuea = xmlGetProp(n, (xmlChar *) "value");
132 usera = xmlGetProp(n, (xmlChar *) "user");
133 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
134 for (n = n->children; n; n = n->next)
136 if (n->type != XML_ELEMENT_NODE)
138 if (!strcmp((const char *) n->name, "set"))
140 char *name, *target, *value, *user, *precedence;
142 name = (char *) xmlGetProp(n, (xmlChar *) "name");
143 target = (char *) xmlGetProp(n, (xmlChar *) "target");
144 value = (char *) xmlGetProp(n, (xmlChar *) "value");
145 user = (char *) xmlGetProp(n, (xmlChar *) "user");
146 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
148 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
150 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
161 // Copy everything into a temporary buffer -- we decide
162 // later if we are keeping it.
164 set.precedence = atoi((char *) precedence);
165 else if (precedencea)
166 set.precedence = atoi((char *) precedencea);
173 strcpy(userb, (const char *) usera);
177 strcpy(targetb, target);
179 strcpy(targetb, (const char *) targeta);
180 set.target = targetb;
184 strcpy(nameb, (const char *) namea);
187 strcpy(valueb, value);
189 strcpy(valueb, (const char *) valuea);
202 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
207 xmlFree(precedencea);
213 // Recursively read files in a directory structure, calling
214 // callback for each one
215 static void read_settings(const char *path,
216 void (*fun)(struct setting *set))
221 if (!(d = opendir(path)))
223 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
226 while ((de = readdir(d)))
229 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
231 sprintf(tmp, "%s/%s", path, de->d_name);
233 read_settings(tmp, fun);
237 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
238 read_settings_file(tmp, fun);
244 // Callback. Adds a new entry to the dictionary if necessary
245 // This is used in pass 1 to determine layout of dictionary
246 static void prepare_dictionary(struct setting *set)
251 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
253 for (i = 0; i < dictionary->num; i++)
254 if (!strcmp(dictionary->dict[i], set->name))
256 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
258 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
261 // Create a new dictionary entry
262 // Grow dictionary if necessary
263 if (!dictionary->size)
264 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
265 else if (dictionary->num + 1 > dictionary->size)
267 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
268 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
269 dictionary->dict = tmp;
270 dictionary->size *= 2;
272 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
275 // This is called from grep_databases -- adds/overrides setting for a target
276 // This is also where the rules for precedence of settings are implemented
277 static void update_database(void *context, struct database *db)
279 struct setting *set = (struct setting *) context;
280 struct setting *s, **sp;
285 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
286 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
288 if ((offset = settings_offset_cprefix(set->name)) < 0)
289 abort(); // Should never get here
291 // First we determine if this setting is overriding any existing settings
292 // with the same name.
293 for (s = db->settings[offset], sp = &db->settings[offset]; s;
294 sp = &s->next, s = s->next)
295 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
297 if (s->precedence < set->precedence)
298 // We discard the value (nmem keeps track of the space)
300 else if (s->precedence > set->precedence)
301 // Db contains a higher-priority setting. Abort
303 if (*s->target == '*' && *set->target != '*')
304 // target-specific value trumps wildcard. Delete.
306 else if (*s->target != '*' && *set->target == '*')
307 // Db already contains higher-priority setting. Abort
310 if (!s) // s will be null when there are no higher-priority settings -- we add one
312 struct setting *new = nmem_malloc(nmem, sizeof(*new));
314 memset(new, 0, sizeof(*new));
315 new->precedence = set->precedence;
316 new->target = nmem_strdup(nmem, set->target);
317 new->name = nmem_strdup(nmem, set->name);
318 new->value = nmem_strdup(nmem, set->value);
319 new->user = nmem_strdup(nmem, set->user);
320 new->next = db->settings[offset];
321 db->settings[offset] = new;
325 // Callback -- updates database records with dictionary entries as appropriate
326 // This is used in pass 2 to assign name/value pairs to databases
327 static void update_databases(struct setting *set)
329 struct database_criterion crit;
330 struct database_criterion_value val;
332 // Update all databases which match pattern in set->target
336 val.value = set->target;
338 grep_databases(set, &crit, update_database);
341 // This simply copies the 'hard' (application-specific) settings
342 // to the settings dictionary.
343 static void initialize_hard_settings(struct setting_dictionary *dict)
345 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
346 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
347 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
348 dict->num = dict->size;
351 // If we ever decide we need to be able to specify multiple settings directories,
352 // the two calls to read_settings must be split -- so the dictionary is prepared
353 // for the contents of every directory before the databases are updated.
354 void settings_read(const char *path)
356 struct setting_dictionary *new;
358 nmem = nmem_create();
361 new = nmem_malloc(nmem, sizeof(*new));
362 memset(new, 0, sizeof(*new));
363 initialize_hard_settings(new);
365 read_settings(path, prepare_dictionary);
366 read_settings(path, update_databases);
372 * indent-tabs-mode: nil
374 * vim: shiftwidth=4 tabstop=8 expandtab