1 /* $Id: settings.c,v 1.21 2007-05-16 17:16:21 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[] = {
65 struct setting_dictionary
72 static struct setting_dictionary *dictionary = 0;
74 // Returns size of settings directory
75 int settings_num(void)
77 return dictionary->num;
80 int settings_offset(const char *name)
86 for (i = 0; i < dictionary->num; i++)
87 if (!strcmp(name, dictionary->dict[i]))
92 // Ignores everything after second colon, if present
93 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
94 int settings_offset_cprefix(const char *name)
100 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
101 maxlen = (p - name) + 1;
102 for (i = 0; i < dictionary->num; i++)
103 if (!strncmp(name, dictionary->dict[i], maxlen))
108 char *settings_name(int offset)
110 return dictionary->dict[offset];
113 static int isdir(const char *path)
117 if (stat(path, &st) < 0)
119 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
122 return st.st_mode & S_IFDIR;
125 // Read settings from an XML file, calling handler function for each setting
126 static void read_settings_file(const char *path,
127 void (*fun)(struct setting *set))
129 xmlDoc *doc = xmlParseFile(path);
131 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
135 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
138 n = xmlDocGetRootElement(doc);
139 namea = xmlGetProp(n, (xmlChar *) "name");
140 targeta = xmlGetProp(n, (xmlChar *) "target");
141 valuea = xmlGetProp(n, (xmlChar *) "value");
142 usera = xmlGetProp(n, (xmlChar *) "user");
143 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
144 for (n = n->children; n; n = n->next)
146 if (n->type != XML_ELEMENT_NODE)
148 if (!strcmp((const char *) n->name, "set"))
150 char *name, *target, *value, *user, *precedence;
152 name = (char *) xmlGetProp(n, (xmlChar *) "name");
153 target = (char *) xmlGetProp(n, (xmlChar *) "target");
154 value = (char *) xmlGetProp(n, (xmlChar *) "value");
155 user = (char *) xmlGetProp(n, (xmlChar *) "user");
156 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
158 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
160 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
170 // Copy everything into a temporary buffer -- we decide
171 // later if we are keeping it.
173 set.precedence = atoi((char *) precedence);
174 else if (precedencea)
175 set.precedence = atoi((char *) precedencea);
179 strcpy(targetb, target);
181 strcpy(targetb, (const char *) targeta);
182 set.target = targetb;
186 strcpy(nameb, (const char *) namea);
189 strcpy(valueb, value);
191 strcpy(valueb, (const char *) valuea);
204 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
209 xmlFree(precedencea);
217 // Recursively read files or directories, invoking a
218 // callback for each one
219 static void read_settings(const char *path,
220 void (*fun)(struct setting *set))
228 if (!(d = opendir(path)))
230 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
233 while ((de = readdir(d)))
236 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
238 sprintf(tmp, "%s/%s", path, de->d_name);
239 read_settings(tmp, fun);
243 else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
244 read_settings_file(path, fun);
247 // Callback. Adds a new entry to the dictionary if necessary
248 // This is used in pass 1 to determine layout of dictionary
249 // and to load any databases mentioned
250 static void prepare_dictionary(struct setting *set)
255 // If target address is not wildcard, add the database
256 if (*set->target && set->target[strlen(set->target) - 1] != '*')
257 find_database(set->target, 0);
259 // Determine if we already have a dictionary entry
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))
266 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
268 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
272 // Create a new dictionary entry
273 // Grow dictionary if necessary
274 if (!dictionary->size)
275 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
276 else if (dictionary->num + 1 > dictionary->size)
278 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
279 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
280 dictionary->dict = tmp;
281 dictionary->size *= 2;
283 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
286 // This is called from grep_databases -- adds/overrides setting for a target
287 // This is also where the rules for precedence of settings are implemented
288 static void update_database(void *context, struct database *db)
290 struct setting *set = (struct setting *) context;
291 struct setting *s, **sp;
294 // Is this the right database?
295 if (!match_zurl(db->url, set->target))
298 // Initialize settings array if it doesn't exist.
299 // If so, also set the 'id' automatic setting
302 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
304 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
305 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
308 id->target = id->value = db->url;
310 db->settings[PZ_ID] = id;
312 if ((offset = settings_offset_cprefix(set->name)) < 0)
313 abort(); // Should never get here
315 // First we determine if this setting is overriding any existing settings
316 // with the same name.
317 for (s = db->settings[offset], sp = &db->settings[offset]; s;
318 sp = &s->next, s = s->next)
319 if (!strcmp(s->name, set->name))
321 if (s->precedence < set->precedence)
322 // We discard the value (nmem keeps track of the space)
324 else if (s->precedence > set->precedence)
325 // Db contains a higher-priority setting. Abort
327 if (*s->target == '*' && *set->target != '*')
328 // target-specific value trumps wildcard. Delete.
330 else if (*s->target != '*' && *set->target == '*')
331 // Db already contains higher-priority setting. Abort
334 if (!s) // s will be null when there are no higher-priority settings -- we add one
336 struct setting *new = nmem_malloc(nmem, sizeof(*new));
338 memset(new, 0, sizeof(*new));
339 new->precedence = set->precedence;
340 new->target = nmem_strdup(nmem, set->target);
341 new->name = nmem_strdup(nmem, set->name);
342 new->value = nmem_strdup(nmem, set->value);
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 read_settings(path, prepare_dictionary);
371 read_settings(path, update_databases);
374 void init_settings(void)
376 struct setting_dictionary *new;
378 nmem = nmem_create();
381 new = nmem_malloc(nmem, sizeof(*new));
382 memset(new, 0, sizeof(*new));
383 initialize_hard_settings(new);
390 * indent-tabs-mode: nil
392 * vim: shiftwidth=4 tabstop=8 expandtab