1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2008 Index Data
4 Pazpar2 is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 // This module implements a generic system of settings
22 // (attribute-value) that can be associated with search targets. The
23 // system supports both default values, per-target overrides, and
28 #include <sys/types.h>
33 #include <libxml/parser.h>
34 #include <libxml/tree.h>
45 // Used for initializing setting_dictionary with pazpar2-specific settings
46 static char *hard_settings[] = {
65 struct setting_dictionary
72 static struct setting_dictionary *dictionary = 0;
74 // This establishes the precedence of wildcard expressions
75 #define SETTING_WILDCARD_NO 0 // No wildcard
76 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
77 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
79 // Returns size of settings directory
80 int settings_num(void)
82 return dictionary->num;
85 int settings_offset(const char *name)
91 for (i = 0; i < dictionary->num; i++)
92 if (!strcmp(name, dictionary->dict[i]))
97 // Ignores everything after second colon, if present
98 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
99 int settings_offset_cprefix(const char *name)
105 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
106 maxlen = (p - name) + 1;
107 for (i = 0; i < dictionary->num; i++)
108 if (!strncmp(name, dictionary->dict[i], maxlen))
113 char *settings_name(int offset)
115 return dictionary->dict[offset];
118 static int isdir(const char *path)
122 if (stat(path, &st) < 0)
124 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
127 return st.st_mode & S_IFDIR;
130 // Read settings from an XML file, calling handler function for each setting
131 static void read_settings_file(const char *path,
132 void (*fun)(struct setting *set))
134 xmlDoc *doc = xmlParseFile(path);
136 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
140 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
143 n = xmlDocGetRootElement(doc);
144 namea = xmlGetProp(n, (xmlChar *) "name");
145 targeta = xmlGetProp(n, (xmlChar *) "target");
146 valuea = xmlGetProp(n, (xmlChar *) "value");
147 usera = xmlGetProp(n, (xmlChar *) "user");
148 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
149 for (n = n->children; n; n = n->next)
151 if (n->type != XML_ELEMENT_NODE)
153 if (!strcmp((const char *) n->name, "set"))
155 char *name, *target, *value, *user, *precedence;
157 name = (char *) xmlGetProp(n, (xmlChar *) "name");
158 target = (char *) xmlGetProp(n, (xmlChar *) "target");
159 value = (char *) xmlGetProp(n, (xmlChar *) "value");
160 user = (char *) xmlGetProp(n, (xmlChar *) "user");
161 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
163 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
165 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
175 // Copy everything into a temporary buffer -- we decide
176 // later if we are keeping it.
178 set.precedence = atoi((char *) precedence);
179 else if (precedencea)
180 set.precedence = atoi((char *) precedencea);
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 or directories, invoking a
223 // callback for each one
224 static void read_settings(const char *path,
225 void (*fun)(struct setting *set))
233 if (!(d = opendir(path)))
235 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
238 while ((de = readdir(d)))
241 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
243 sprintf(tmp, "%s/%s", path, de->d_name);
244 read_settings(tmp, fun);
248 else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
249 read_settings_file(path, fun);
252 // Determines if a ZURL is a wildcard, and what kind
253 static int zurl_wildcard(const char *zurl)
256 return SETTING_WILDCARD_NO;
258 return SETTING_WILDCARD_YES;
259 else if (*(zurl + strlen(zurl) - 1) == '*')
260 return SETTING_WILDCARD_DB;
262 return SETTING_WILDCARD_NO;
265 // Callback. Adds a new entry to the dictionary if necessary
266 // This is used in pass 1 to determine layout of dictionary
267 // and to load any databases mentioned
268 static void prepare_dictionary(struct setting *set)
273 // If target address is not wildcard, add the database
274 if (*set->target && !zurl_wildcard(set->target))
275 find_database(set->target, 0);
277 // Determine if we already have a dictionary entry
278 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
280 for (i = 0; i < dictionary->num; i++)
281 if (!strcmp(dictionary->dict[i], set->name))
284 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
286 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
290 // Create a new dictionary entry
291 // Grow dictionary if necessary
292 if (!dictionary->size)
293 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
294 else if (dictionary->num + 1 > dictionary->size)
296 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
297 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
298 dictionary->dict = tmp;
299 dictionary->size *= 2;
301 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
304 // This is called from grep_databases -- adds/overrides setting for a target
305 // This is also where the rules for precedence of settings are implemented
306 static void update_database(void *context, struct database *db)
308 struct setting *set = (struct setting *) context;
309 struct setting *s, **sp;
312 // Is this the right database?
313 if (!match_zurl(db->url, set->target))
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)
327 *sp = (*sp)->next; // unlink value from existing setting
328 else if (s->precedence > set->precedence)
329 // Db contains a higher-priority setting. Abort search
331 if (zurl_wildcard(s->target) > zurl_wildcard(set->target))
332 // target-specific value trumps wildcard. Delete.
333 *sp = (*sp)->next; // unlink.....
334 else if (!zurl_wildcard(s->target))
335 // Db already contains higher-priority setting. Abort search
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 // Read any settings names introduced in service definition (config) and add to dictionary
370 // This is done now to avoid errors if user settings are declared in session overrides
371 static void initialize_soft_settings()
373 struct conf_service *service = config->servers->service;
376 for (i = 0; i < service->num_metadata; i++)
379 struct conf_metadata *md = &service->metadata[i];
381 if (md->setting == Metadata_setting_no)
389 prepare_dictionary(&set);
393 // If we ever decide we need to be able to specify multiple settings directories,
394 // the two calls to read_settings must be split -- so the dictionary is prepared
395 // for the contents of every directory before the databases are updated.
396 void settings_read(const char *path)
398 read_settings(path, prepare_dictionary);
399 read_settings(path, update_databases);
402 void init_settings(void)
404 struct setting_dictionary *new;
406 nmem = nmem_create();
409 new = nmem_malloc(nmem, sizeof(*new));
410 memset(new, 0, sizeof(*new));
411 initialize_hard_settings(new);
413 initialize_soft_settings();
419 * indent-tabs-mode: nil
421 * vim: shiftwidth=4 tabstop=8 expandtab