1 /* $Id: settings.c,v 1.25 2007-07-03 11:21:48 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
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[] = {
67 struct setting_dictionary
74 static struct setting_dictionary *dictionary = 0;
76 // Returns size of settings directory
77 int settings_num(void)
79 return dictionary->num;
82 int settings_offset(const char *name)
88 for (i = 0; i < dictionary->num; i++)
89 if (!strcmp(name, dictionary->dict[i]))
94 // Ignores everything after second colon, if present
95 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
96 int settings_offset_cprefix(const char *name)
102 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
103 maxlen = (p - name) + 1;
104 for (i = 0; i < dictionary->num; i++)
105 if (!strncmp(name, dictionary->dict[i], maxlen))
110 char *settings_name(int offset)
112 return dictionary->dict[offset];
115 static int isdir(const char *path)
119 if (stat(path, &st) < 0)
121 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
124 return st.st_mode & S_IFDIR;
127 // Read settings from an XML file, calling handler function for each setting
128 static void read_settings_file(const char *path,
129 void (*fun)(struct setting *set))
131 xmlDoc *doc = xmlParseFile(path);
133 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
137 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
140 n = xmlDocGetRootElement(doc);
141 namea = xmlGetProp(n, (xmlChar *) "name");
142 targeta = xmlGetProp(n, (xmlChar *) "target");
143 valuea = xmlGetProp(n, (xmlChar *) "value");
144 usera = xmlGetProp(n, (xmlChar *) "user");
145 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
146 for (n = n->children; n; n = n->next)
148 if (n->type != XML_ELEMENT_NODE)
150 if (!strcmp((const char *) n->name, "set"))
152 char *name, *target, *value, *user, *precedence;
154 name = (char *) xmlGetProp(n, (xmlChar *) "name");
155 target = (char *) xmlGetProp(n, (xmlChar *) "target");
156 value = (char *) xmlGetProp(n, (xmlChar *) "value");
157 user = (char *) xmlGetProp(n, (xmlChar *) "user");
158 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
160 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
162 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
172 // Copy everything into a temporary buffer -- we decide
173 // later if we are keeping it.
175 set.precedence = atoi((char *) precedence);
176 else if (precedencea)
177 set.precedence = atoi((char *) precedencea);
181 strcpy(targetb, target);
183 strcpy(targetb, (const char *) targeta);
184 set.target = targetb;
188 strcpy(nameb, (const char *) namea);
191 strcpy(valueb, value);
193 strcpy(valueb, (const char *) valuea);
206 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
211 xmlFree(precedencea);
219 // Recursively read files or directories, invoking a
220 // callback for each one
221 static void read_settings(const char *path,
222 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);
241 read_settings(tmp, fun);
245 else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
246 read_settings_file(path, fun);
249 // Callback. Adds a new entry to the dictionary if necessary
250 // This is used in pass 1 to determine layout of dictionary
251 // and to load any databases mentioned
252 static void prepare_dictionary(struct setting *set)
257 // If target address is not wildcard, add the database
258 if (*set->target && set->target[strlen(set->target) - 1] != '*')
259 find_database(set->target, 0);
261 // Determine if we already have a dictionary entry
262 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
264 for (i = 0; i < dictionary->num; i++)
265 if (!strcmp(dictionary->dict[i], set->name))
268 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
270 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
274 // Create a new dictionary entry
275 // Grow dictionary if necessary
276 if (!dictionary->size)
277 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
278 else if (dictionary->num + 1 > dictionary->size)
280 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
281 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
282 dictionary->dict = tmp;
283 dictionary->size *= 2;
285 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
288 // This is called from grep_databases -- adds/overrides setting for a target
289 // This is also where the rules for precedence of settings are implemented
290 static void update_database(void *context, struct database *db)
292 struct setting *set = (struct setting *) context;
293 struct setting *s, **sp;
296 // Is this the right database?
297 if (!match_zurl(db->url, set->target))
301 // Initialize settings array if it doesn't exist.
302 // If so, also set the 'id' automatic setting
305 struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
307 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
308 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
311 id->target = id->value = db->url;
313 db->settings[PZ_ID] = id;
316 if ((offset = settings_offset_cprefix(set->name)) < 0)
317 abort(); // Should never get here
319 // First we determine if this setting is overriding any existing settings
320 // with the same name.
321 for (s = db->settings[offset], sp = &db->settings[offset]; s;
322 sp = &s->next, s = s->next)
323 if (!strcmp(s->name, set->name))
325 if (s->precedence < set->precedence)
326 // We discard the value (nmem keeps track of the space)
328 else if (s->precedence > set->precedence)
329 // Db contains a higher-priority setting. Abort
331 if (*s->target == '*' && *set->target != '*')
332 // target-specific value trumps wildcard. Delete.
334 else if (*s->target != '*' && *set->target == '*')
335 // Db already contains higher-priority setting. Abort
338 if (!s) // s will be null when there are no higher-priority settings -- we add one
340 struct setting *new = nmem_malloc(nmem, sizeof(*new));
342 memset(new, 0, sizeof(*new));
343 new->precedence = set->precedence;
344 new->target = nmem_strdup(nmem, set->target);
345 new->name = nmem_strdup(nmem, set->name);
346 new->value = nmem_strdup(nmem, set->value);
347 new->next = db->settings[offset];
348 db->settings[offset] = new;
352 // Callback -- updates database records with dictionary entries as appropriate
353 // This is used in pass 2 to assign name/value pairs to databases
354 static void update_databases(struct setting *set)
356 predef_grep_databases(set, 0, update_database);
359 // This simply copies the 'hard' (application-specific) settings
360 // to the settings dictionary.
361 static void initialize_hard_settings(struct setting_dictionary *dict)
363 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
364 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
365 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
366 dict->num = dict->size;
369 // If we ever decide we need to be able to specify multiple settings directories,
370 // the two calls to read_settings must be split -- so the dictionary is prepared
371 // for the contents of every directory before the databases are updated.
372 void settings_read(const char *path)
374 read_settings(path, prepare_dictionary);
375 read_settings(path, update_databases);
378 void init_settings(void)
380 struct setting_dictionary *new;
382 nmem = nmem_create();
385 new = nmem_malloc(nmem, sizeof(*new));
386 memset(new, 0, sizeof(*new));
387 initialize_hard_settings(new);
394 * indent-tabs-mode: nil
396 * vim: shiftwidth=4 tabstop=8 expandtab