1 // $Id: settings.c,v 1.5 2007-03-31 19:55:25 marc Exp $
2 // This module implements a generic system of settings (attribute-value) that can
3 // be associated with search targets. The system supports both default values,
4 // per-target overrides, and per-user settings.
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
25 // Used for initializing setting_dictionary with pazpar2-specific settings
26 static char *hard_settings[] = {
34 struct setting_dictionary
41 static struct setting_dictionary *dictionary = 0;
43 int settings_offset(const char *name)
47 for (i = 0; i < dictionary->num; i++)
48 if (!strcmp(name, dictionary->dict[i]))
53 // Ignores everything after second colon, if present
54 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
55 static int settings_offset_cprefix(const char *name)
61 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
62 maxlen = (p - name) + 1;
63 for (i = 0; i < dictionary->num; i++)
64 if (!strncmp(name, dictionary->dict[i], maxlen))
69 char *settings_name(int offset)
71 return dictionary->dict[offset];
74 static int isdir(const char *path)
78 if (stat(path, &st) < 0)
80 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
83 return st.st_mode & S_IFDIR;
86 // Read settings from an XML file, calling handler function for each setting
87 static void read_settings_file(const char *path,
88 void (*fun)(struct setting *set))
90 xmlDoc *doc = xmlParseFile(path);
92 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
96 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
99 n = xmlDocGetRootElement(doc);
100 namea = xmlGetProp(n, (xmlChar *) "name");
101 targeta = xmlGetProp(n, (xmlChar *) "target");
102 valuea = xmlGetProp(n, (xmlChar *) "value");
103 usera = xmlGetProp(n, (xmlChar *) "user");
104 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
105 for (n = n->children; n; n = n->next)
107 if (n->type != XML_ELEMENT_NODE)
109 if (!strcmp((const char *) n->name, "set"))
111 char *name, *target, *value, *user, *precedence;
113 name = (char *) xmlGetProp(n, (xmlChar *) "name");
114 target = (char *) xmlGetProp(n, (xmlChar *) "target");
115 value = (char *) xmlGetProp(n, (xmlChar *) "value");
116 user = (char *) xmlGetProp(n, (xmlChar *) "user");
117 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
119 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
121 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
132 // Copy everything into a temporary buffer -- we decide
133 // later if we are keeping it.
135 set.precedence = atoi((char *) precedence);
136 else if (precedencea)
137 set.precedence = atoi((char *) precedencea);
144 strcpy(userb, (const char *) usera);
148 strcpy(targetb, target);
150 strcpy(targetb, (const char *) targeta);
151 set.target = targetb;
155 strcpy(nameb, (const char *) namea);
158 strcpy(valueb, value);
160 strcpy(valueb, (const char *) valuea);
173 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
178 xmlFree(precedencea);
184 // Recursively read files in a directory structure, calling
185 // callback for each one
186 static void read_settings(const char *path,
187 void (*fun)(struct setting *set))
192 if (!(d = opendir(path)))
194 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
197 while ((de = readdir(d)))
200 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
202 sprintf(tmp, "%s/%s", path, de->d_name);
204 read_settings(tmp, fun);
208 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
209 read_settings_file(tmp, fun);
215 // Callback. Adds a new entry to the dictionary if necessary
216 // This is used in pass 1 to determine layout of dictionary
217 static void prepare_dictionary(struct setting *set)
222 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
224 for (i = 0; i < dictionary->num; i++)
225 if (!strcmp(dictionary->dict[i], set->name))
227 // Create a new dictionary entry
228 // Grow dictionary if necessary
229 if (!dictionary->size)
230 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
231 else if (dictionary->num + 1 > dictionary->size)
233 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
234 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
235 dictionary->dict = tmp;
236 dictionary->size *= 2;
238 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
241 // This is called from grep_databases -- adds/overrides setting for a target
242 // This is also where the rules for precedence of settings are implemented
243 static void update_database(void *context, struct database *db)
245 struct setting *set = (struct setting *) context;
246 struct setting *s, **sp;
251 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
252 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
254 if ((offset = settings_offset_cprefix(set->name)) < 0)
255 abort(); // Should never get here
257 // First we determine if this setting is overriding any existing settings
258 // with the same name.
259 for (s = db->settings[offset], sp = &db->settings[offset]; s;
260 sp = &s->next, s = s->next)
261 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
263 if (s->precedence < set->precedence)
264 // We discard the value (nmem keeps track of the space)
266 else if (s->precedence > set->precedence)
267 // Db contains a higher-priority setting. Abort
269 if (*s->target == '*' && *set->target != '*')
270 // target-specific value trumps wildcard. Delete.
272 else if (*s->target != '*' && *set->target == '*')
273 // Db already contains higher-priority setting. Abort
276 if (!s) // s will be null when there are no higher-priority settings -- we add one
278 struct setting *new = nmem_malloc(nmem, sizeof(*new));
280 memset(new, 0, sizeof(*new));
281 new->precedence = set->precedence;
282 new->target = nmem_strdup(nmem, set->target);
283 new->name = nmem_strdup(nmem, set->name);
284 new->value = nmem_strdup(nmem, set->value);
285 new->user = nmem_strdup(nmem, set->user);
286 new->next = db->settings[offset];
287 db->settings[offset] = new;
291 // Callback -- updates database records with dictionary entries as appropriate
292 // This is used in pass 2 to assign name/value pairs to databases
293 static void update_databases(struct setting *set)
295 struct database_criterion crit;
296 struct database_criterion_value val;
298 // Update all databases which match pattern in set->target
302 val.value = set->target;
304 grep_databases(set, &crit, update_database);
307 // This simply copies the 'hard' (application-specific) settings
308 // to the settings dictionary.
309 static void initialize_hard_settings(struct setting_dictionary *dict)
311 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
312 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
313 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
314 dict->num = dict->size;
317 // If we ever decide we need to be able to specify multiple settings directories,
318 // the two calls to read_settings must be split -- so the dictionary is prepared
319 // for the contents of every directory before the databases are updated.
320 void settings_read(const char *path)
322 struct setting_dictionary *new;
324 nmem = nmem_create();
327 new = nmem_malloc(nmem, sizeof(*new));
328 memset(new, 0, sizeof(*new));
329 initialize_hard_settings(new);
331 read_settings(path, prepare_dictionary);
332 read_settings(path, update_databases);
338 * indent-tabs-mode: nil
340 * vim: shiftwidth=4 tabstop=8 expandtab