1 /* $Id: settings.c,v 1.12 2007-04-11 11:22:35 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 (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[] = {
62 struct setting_dictionary
69 static struct setting_dictionary *dictionary = 0;
71 // Returns size of settings directory
72 int settings_num(void)
74 return dictionary->num;
77 int settings_offset(const char *name)
83 for (i = 0; i < dictionary->num; i++)
84 if (!strcmp(name, dictionary->dict[i]))
89 // Ignores everything after second colon, if present
90 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
91 static int settings_offset_cprefix(const char *name)
97 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
98 maxlen = (p - name) + 1;
99 for (i = 0; i < dictionary->num; i++)
100 if (!strncmp(name, dictionary->dict[i], maxlen))
105 char *settings_name(int offset)
107 return dictionary->dict[offset];
110 static int isdir(const char *path)
114 if (stat(path, &st) < 0)
116 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
119 return st.st_mode & S_IFDIR;
122 // Read settings from an XML file, calling handler function for each setting
123 static void read_settings_file(const char *path,
124 void (*fun)(struct setting *set))
126 xmlDoc *doc = xmlParseFile(path);
128 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
132 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
135 n = xmlDocGetRootElement(doc);
136 namea = xmlGetProp(n, (xmlChar *) "name");
137 targeta = xmlGetProp(n, (xmlChar *) "target");
138 valuea = xmlGetProp(n, (xmlChar *) "value");
139 usera = xmlGetProp(n, (xmlChar *) "user");
140 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
141 for (n = n->children; n; n = n->next)
143 if (n->type != XML_ELEMENT_NODE)
145 if (!strcmp((const char *) n->name, "set"))
147 char *name, *target, *value, *user, *precedence;
149 name = (char *) xmlGetProp(n, (xmlChar *) "name");
150 target = (char *) xmlGetProp(n, (xmlChar *) "target");
151 value = (char *) xmlGetProp(n, (xmlChar *) "value");
152 user = (char *) xmlGetProp(n, (xmlChar *) "user");
153 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
155 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
157 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
168 // Copy everything into a temporary buffer -- we decide
169 // later if we are keeping it.
171 set.precedence = atoi((char *) precedence);
172 else if (precedencea)
173 set.precedence = atoi((char *) precedencea);
180 strcpy(userb, (const char *) usera);
184 strcpy(targetb, target);
186 strcpy(targetb, (const char *) targeta);
187 set.target = targetb;
191 strcpy(nameb, (const char *) namea);
194 strcpy(valueb, value);
196 strcpy(valueb, (const char *) valuea);
209 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
214 xmlFree(precedencea);
222 // Recursively read files in a directory structure, calling
223 // callback for each one
224 static void read_settings(const char *path,
225 void (*fun)(struct setting *set))
230 if (!(d = opendir(path)))
232 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
235 while ((de = readdir(d)))
238 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
240 sprintf(tmp, "%s/%s", path, de->d_name);
242 read_settings(tmp, fun);
246 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
247 read_settings_file(tmp, fun);
253 // Callback. Adds a new entry to the dictionary if necessary
254 // This is used in pass 1 to determine layout of dictionary
255 static void prepare_dictionary(struct setting *set)
260 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
262 for (i = 0; i < dictionary->num; i++)
263 if (!strcmp(dictionary->dict[i], set->name))
265 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
267 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
270 // Create a new dictionary entry
271 // Grow dictionary if necessary
272 if (!dictionary->size)
273 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
274 else if (dictionary->num + 1 > dictionary->size)
276 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
277 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
278 dictionary->dict = tmp;
279 dictionary->size *= 2;
281 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
284 // This is called from grep_databases -- adds/overrides setting for a target
285 // This is also where the rules for precedence of settings are implemented
286 static void update_database(void *context, struct database *db)
288 struct setting *set = (struct setting *) context;
289 struct setting *s, **sp;
292 // Is this the right database?
293 if (!match_zurl(db->url, set->target))
296 // Initialize settings array if it doesn't exist.
297 // If so, also set the 'id' automatic setting
300 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
302 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
303 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
306 id->target = id->value = db->url;
309 db->settings[PZ_ID] = id;
311 if ((offset = settings_offset_cprefix(set->name)) < 0)
312 abort(); // Should never get here
314 // First we determine if this setting is overriding any existing settings
315 // with the same name.
316 for (s = db->settings[offset], sp = &db->settings[offset]; s;
317 sp = &s->next, s = s->next)
318 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
320 if (s->precedence < set->precedence)
321 // We discard the value (nmem keeps track of the space)
323 else if (s->precedence > set->precedence)
324 // Db contains a higher-priority setting. Abort
326 if (*s->target == '*' && *set->target != '*')
327 // target-specific value trumps wildcard. Delete.
329 else if (*s->target != '*' && *set->target == '*')
330 // Db already contains higher-priority setting. Abort
333 if (!s) // s will be null when there are no higher-priority settings -- we add one
335 struct setting *new = nmem_malloc(nmem, sizeof(*new));
337 memset(new, 0, sizeof(*new));
338 new->precedence = set->precedence;
339 new->target = nmem_strdup(nmem, set->target);
340 new->name = nmem_strdup(nmem, set->name);
341 new->value = nmem_strdup(nmem, set->value);
342 new->user = nmem_strdup(nmem, set->user);
343 new->next = db->settings[offset];
344 db->settings[offset] = new;
348 // Callback -- updates database records with dictionary entries as appropriate
349 // This is used in pass 2 to assign name/value pairs to databases
350 static void update_databases(struct setting *set)
352 grep_databases(set, 0, update_database);
355 // This simply copies the 'hard' (application-specific) settings
356 // to the settings dictionary.
357 static void initialize_hard_settings(struct setting_dictionary *dict)
359 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
360 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
361 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
362 dict->num = dict->size;
365 // If we ever decide we need to be able to specify multiple settings directories,
366 // the two calls to read_settings must be split -- so the dictionary is prepared
367 // for the contents of every directory before the databases are updated.
368 void settings_read(const char *path)
370 struct setting_dictionary *new;
372 nmem = nmem_create();
375 new = nmem_malloc(nmem, sizeof(*new));
376 memset(new, 0, sizeof(*new));
377 initialize_hard_settings(new);
379 read_settings(path, prepare_dictionary);
380 read_settings(path, update_databases);
386 * indent-tabs-mode: nil
388 * vim: shiftwidth=4 tabstop=8 expandtab