1 /* This file is part of Pazpar2.
2 Copyright (C) 2006-2011 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
20 // This module implements a generic system of settings
21 // (attribute-value) that can be associated with search targets. The
22 // system supports both default values, per-target overrides, and
33 #include <sys/types.h>
34 #include <yaz/dirent.h>
38 #include <libxml/parser.h>
39 #include <libxml/tree.h>
48 // Used for initializing setting_dictionary with pazpar2-specific settings
49 static char *hard_settings[] = {
71 "pz:negotiation_charset",
73 "pz:reuse_connections", /* PZ_REUSE_CONNECTION */
74 "pz:termlist_term_sort", /* PZ_TERMLIST_TERM_SORT */
75 "pz:termlist_term_count", /* PZ_TERMLIST_TERM_COUNT */
76 "pz:termlist_term_factor", /* PZ_TERMLIST_TERM_FACTOR*/
77 "pz:preferred", /* PZ_PREFERRED */
78 "pz:extra_args", /* PZ_EXTRA_ARGS */
82 struct setting_dictionary
89 // This establishes the precedence of wildcard expressions
90 #define SETTING_WILDCARD_NO 0 // No wildcard
91 #define SETTING_WILDCARD_DB 1 // Database wildcard 'host:port/*'
92 #define SETTING_WILDCARD_YES 2 // Complete wildcard '*'
94 // Returns size of settings directory
95 int settings_num(struct conf_service *service)
97 return service->dictionary->num;
100 static int settings_lookup(struct conf_service *service, const char *name,
106 struct setting_dictionary *dictionary = service->dictionary;
110 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
111 maxlen = (p - name) + 1;
113 maxlen = strlen(name) + 1;
114 for (i = 0; i < dictionary->num; i++)
115 if (!strncmp(name, dictionary->dict[i], maxlen))
119 if (!strncmp("pz:", name, 3))
120 yaz_log(YLOG_WARN, "Adding pz-type setting name %s", name);
121 if (dictionary->num + 1 > dictionary->size)
124 nmem_malloc(service->nmem, dictionary->size * 2 * sizeof(char*));
125 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
126 dictionary->dict = tmp;
127 dictionary->size *= 2;
129 dictionary->dict[dictionary->num] = nmem_strdup(service->nmem, name);
130 dictionary->dict[dictionary->num][maxlen-1] = '\0';
131 return dictionary->num++;
134 int settings_create_offset(struct conf_service *service, const char *name)
136 return settings_lookup(service, name, 1);
139 int settings_lookup_offset(struct conf_service *service, const char *name)
141 return settings_lookup(service, name, 0);
144 char *settings_name(struct conf_service *service, int offset)
146 assert(offset < service->dictionary->num);
147 return service->dictionary->dict[offset];
150 static int isdir(const char *path)
154 if (stat(path, &st) < 0)
156 yaz_log(YLOG_FATAL|YLOG_ERRNO, "stat %s", path);
159 return st.st_mode & S_IFDIR;
162 // Read settings from an XML file, calling handler function for each setting
163 void settings_read_node_x(xmlNode *n,
165 void (*fun)(void *client_data,
166 struct setting *set))
168 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
170 namea = xmlGetProp(n, (xmlChar *) "name");
171 targeta = xmlGetProp(n, (xmlChar *) "target");
172 valuea = xmlGetProp(n, (xmlChar *) "value");
173 usera = xmlGetProp(n, (xmlChar *) "user");
174 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
175 for (n = n->children; n; n = n->next)
177 if (n->type != XML_ELEMENT_NODE)
179 if (!strcmp((const char *) n->name, "set"))
181 char *name, *target, *value, *user, *precedence;
183 name = (char *) xmlGetProp(n, (xmlChar *) "name");
184 target = (char *) xmlGetProp(n, (xmlChar *) "target");
185 value = (char *) xmlGetProp(n, (xmlChar *) "value");
186 user = (char *) xmlGetProp(n, (xmlChar *) "user");
187 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
189 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
191 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
201 // Copy everything into a temporary buffer -- we decide
202 // later if we are keeping it.
204 set.precedence = atoi((char *) precedence);
205 else if (precedencea)
206 set.precedence = atoi((char *) precedencea);
210 strcpy(targetb, target);
212 strcpy(targetb, (const char *) targeta);
213 set.target = targetb;
217 strcpy(nameb, (const char *) namea);
220 strcpy(valueb, value);
222 strcpy(valueb, (const char *) valuea);
225 (*fun)(client_data, &set);
235 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
240 xmlFree(precedencea);
246 static void read_settings_file(const char *path,
248 void (*fun)(void *client_data,
249 struct setting *set))
251 xmlDoc *doc = xmlParseFile(path);
256 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
259 n = xmlDocGetRootElement(doc);
260 settings_read_node_x(n, client_data, fun);
266 // Recursively read files or directories, invoking a
267 // callback for each one
268 static void read_settings(const char *path,
270 void (*fun)(void *client_data,
271 struct setting *set))
279 if (!(d = opendir(path)))
281 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
284 while ((de = readdir(d)))
287 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
289 sprintf(tmp, "%s/%s", path, de->d_name);
290 read_settings(tmp, client_data, fun);
294 else if ((dot = strrchr(path, '.')) && !strcmp(dot + 1, "xml"))
295 read_settings_file(path, client_data, fun);
298 // Determines if a ZURL is a wildcard, and what kind
299 static int zurl_wildcard(const char *zurl)
302 return SETTING_WILDCARD_NO;
304 return SETTING_WILDCARD_YES;
305 else if (*(zurl + strlen(zurl) - 1) == '*')
306 return SETTING_WILDCARD_DB;
308 return SETTING_WILDCARD_NO;
311 struct update_database_context {
313 struct conf_service *service;
316 void expand_settings_array(struct setting ***set_ar, int *num, int offset,
323 int i, n_num = offset + 10;
324 struct setting **n_ar = nmem_malloc(nmem, n_num * sizeof(*n_ar));
325 for (i = 0; i < *num; i++)
326 n_ar[i] = (*set_ar)[i];
327 for (; i < n_num; i++)
334 // This is called from grep_databases -- adds/overrides setting for a target
335 // This is also where the rules for precedence of settings are implemented
336 static void update_database(void *context, struct database *db)
338 struct setting *set = ((struct update_database_context *)
340 struct conf_service *service = ((struct update_database_context *)
345 // Is this the right database?
346 if (!match_zurl(db->url, set->target))
349 offset = settings_create_offset(service, set->name);
350 expand_settings_array(&db->settings, &db->num_settings, offset,
353 // First we determine if this setting is overriding any existing settings
354 // with the same name.
355 assert(offset < db->num_settings);
356 for (sp = &db->settings[offset]; *sp; )
357 if (!strcmp((*sp)->name, set->name))
359 if ((*sp)->precedence < set->precedence)
361 // We discard the value (nmem keeps track of the space)
362 *sp = (*sp)->next; // unlink value from existing setting
364 else if ((*sp)->precedence > set->precedence)
366 // Db contains a higher-priority setting. Abort search
369 else if (zurl_wildcard((*sp)->target) > zurl_wildcard(set->target))
371 // target-specific value trumps wildcard. Delete.
372 *sp = (*sp)->next; // unlink.....
374 else if (zurl_wildcard((*sp)->target) < zurl_wildcard(set->target))
375 // Db already contains higher-priority setting. Abort search
382 if (!*sp) // is null when there are no higher-priority settings, so we add one
384 struct setting *new = nmem_malloc(service->nmem, sizeof(*new));
386 memset(new, 0, sizeof(*new));
387 new->precedence = set->precedence;
388 new->target = nmem_strdup(service->nmem, set->target);
389 new->name = nmem_strdup(service->nmem, set->name);
390 new->value = nmem_strdup(service->nmem, set->value);
391 new->next = db->settings[offset];
392 db->settings[offset] = new;
396 // Callback -- updates database records with dictionary entries as appropriate
397 // This is used in pass 2 to assign name/value pairs to databases
398 static void update_databases(void *client_data, struct setting *set)
400 struct conf_service *service = (struct conf_service *) client_data;
401 struct update_database_context context;
403 context.service = service;
404 predef_grep_databases(&context, service, update_database);
407 // This simply copies the 'hard' (application-specific) settings
408 // to the settings dictionary.
409 static void initialize_hard_settings(struct conf_service *service)
411 struct setting_dictionary *dict = service->dictionary;
412 dict->dict = nmem_malloc(service->nmem, sizeof(hard_settings) - sizeof(char*));
413 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
414 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
415 dict->num = dict->size;
418 // Read any settings names introduced in service definition (config) and add to dictionary
419 // This is done now to avoid errors if user settings are declared in session overrides
420 static void initialize_soft_settings(struct conf_service *service)
424 for (i = 0; i < service->num_metadata; i++)
426 struct conf_metadata *md = &service->metadata[i];
428 if (md->setting == Metadata_setting_no)
431 settings_create_offset(service, md->name);
435 static void prepare_target_dictionary(void *client_data, struct setting *set)
437 struct conf_service *service = (struct conf_service *) client_data;
438 struct setting_dictionary *dictionary = service->dictionary;
443 // If target address is not wildcard, add the database
444 if (*set->target && !zurl_wildcard(set->target))
445 find_database(set->target, service);
447 // Determine if we already have a dictionary entry
448 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
450 for (i = 0; i < dictionary->num; i++)
451 if (!strcmp(dictionary->dict[i], set->name))
453 yaz_log(YLOG_WARN, "Setting '%s' not configured as metadata", set->name);
456 void init_settings(struct conf_service *service)
458 struct setting_dictionary *new;
460 assert(service->nmem);
462 new = nmem_malloc(service->nmem, sizeof(*new));
463 memset(new, 0, sizeof(*new));
464 service->dictionary = new;
465 initialize_hard_settings(service);
466 initialize_soft_settings(service);
469 void settings_read_file(struct conf_service *service, const char *path,
473 read_settings(path, service, prepare_target_dictionary);
475 read_settings(path, service, update_databases);
478 void settings_read_node(struct conf_service *service, xmlNode *n,
482 settings_read_node_x(n, service, prepare_target_dictionary);
484 settings_read_node_x(n, service, update_databases);
490 * c-file-style: "Stroustrup"
491 * indent-tabs-mode: nil
493 * vim: shiftwidth=4 tabstop=8 expandtab