1 // $Id: settings.c,v 1.8 2007-04-08 21:51:58 quinn 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[] = {
38 struct setting_dictionary
45 static struct setting_dictionary *dictionary = 0;
47 int settings_offset(const char *name)
53 for (i = 0; i < dictionary->num; i++)
54 if (!strcmp(name, dictionary->dict[i]))
59 // Ignores everything after second colon, if present
60 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
61 static int settings_offset_cprefix(const char *name)
67 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
68 maxlen = (p - name) + 1;
69 for (i = 0; i < dictionary->num; i++)
70 if (!strncmp(name, dictionary->dict[i], maxlen))
75 char *settings_name(int offset)
77 return dictionary->dict[offset];
80 static int isdir(const char *path)
84 if (stat(path, &st) < 0)
86 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
89 return st.st_mode & S_IFDIR;
92 // Read settings from an XML file, calling handler function for each setting
93 static void read_settings_file(const char *path,
94 void (*fun)(struct setting *set))
96 xmlDoc *doc = xmlParseFile(path);
98 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
102 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
105 n = xmlDocGetRootElement(doc);
106 namea = xmlGetProp(n, (xmlChar *) "name");
107 targeta = xmlGetProp(n, (xmlChar *) "target");
108 valuea = xmlGetProp(n, (xmlChar *) "value");
109 usera = xmlGetProp(n, (xmlChar *) "user");
110 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
111 for (n = n->children; n; n = n->next)
113 if (n->type != XML_ELEMENT_NODE)
115 if (!strcmp((const char *) n->name, "set"))
117 char *name, *target, *value, *user, *precedence;
119 name = (char *) xmlGetProp(n, (xmlChar *) "name");
120 target = (char *) xmlGetProp(n, (xmlChar *) "target");
121 value = (char *) xmlGetProp(n, (xmlChar *) "value");
122 user = (char *) xmlGetProp(n, (xmlChar *) "user");
123 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
125 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
127 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
138 // Copy everything into a temporary buffer -- we decide
139 // later if we are keeping it.
141 set.precedence = atoi((char *) precedence);
142 else if (precedencea)
143 set.precedence = atoi((char *) precedencea);
150 strcpy(userb, (const char *) usera);
154 strcpy(targetb, target);
156 strcpy(targetb, (const char *) targeta);
157 set.target = targetb;
161 strcpy(nameb, (const char *) namea);
164 strcpy(valueb, value);
166 strcpy(valueb, (const char *) valuea);
179 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
184 xmlFree(precedencea);
190 // Recursively read files in a directory structure, calling
191 // callback for each one
192 static void read_settings(const char *path,
193 void (*fun)(struct setting *set))
198 if (!(d = opendir(path)))
200 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
203 while ((de = readdir(d)))
206 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
208 sprintf(tmp, "%s/%s", path, de->d_name);
210 read_settings(tmp, fun);
214 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
215 read_settings_file(tmp, fun);
221 // Callback. Adds a new entry to the dictionary if necessary
222 // This is used in pass 1 to determine layout of dictionary
223 static void prepare_dictionary(struct setting *set)
228 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
230 for (i = 0; i < dictionary->num; i++)
231 if (!strcmp(dictionary->dict[i], set->name))
233 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
235 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
238 // Create a new dictionary entry
239 // Grow dictionary if necessary
240 if (!dictionary->size)
241 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
242 else if (dictionary->num + 1 > dictionary->size)
244 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
245 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
246 dictionary->dict = tmp;
247 dictionary->size *= 2;
249 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
252 // This is called from grep_databases -- adds/overrides setting for a target
253 // This is also where the rules for precedence of settings are implemented
254 static void update_database(void *context, struct database *db)
256 struct setting *set = (struct setting *) context;
257 struct setting *s, **sp;
262 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
263 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
265 if ((offset = settings_offset_cprefix(set->name)) < 0)
266 abort(); // Should never get here
268 // First we determine if this setting is overriding any existing settings
269 // with the same name.
270 for (s = db->settings[offset], sp = &db->settings[offset]; s;
271 sp = &s->next, s = s->next)
272 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
274 if (s->precedence < set->precedence)
275 // We discard the value (nmem keeps track of the space)
277 else if (s->precedence > set->precedence)
278 // Db contains a higher-priority setting. Abort
280 if (*s->target == '*' && *set->target != '*')
281 // target-specific value trumps wildcard. Delete.
283 else if (*s->target != '*' && *set->target == '*')
284 // Db already contains higher-priority setting. Abort
287 if (!s) // s will be null when there are no higher-priority settings -- we add one
289 struct setting *new = nmem_malloc(nmem, sizeof(*new));
291 memset(new, 0, sizeof(*new));
292 new->precedence = set->precedence;
293 new->target = nmem_strdup(nmem, set->target);
294 new->name = nmem_strdup(nmem, set->name);
295 new->value = nmem_strdup(nmem, set->value);
296 new->user = nmem_strdup(nmem, set->user);
297 new->next = db->settings[offset];
298 db->settings[offset] = new;
302 // Callback -- updates database records with dictionary entries as appropriate
303 // This is used in pass 2 to assign name/value pairs to databases
304 static void update_databases(struct setting *set)
306 struct database_criterion crit;
307 struct database_criterion_value val;
309 // Update all databases which match pattern in set->target
313 val.value = set->target;
315 grep_databases(set, &crit, update_database);
318 // This simply copies the 'hard' (application-specific) settings
319 // to the settings dictionary.
320 static void initialize_hard_settings(struct setting_dictionary *dict)
322 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
323 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
324 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
325 dict->num = dict->size;
328 // If we ever decide we need to be able to specify multiple settings directories,
329 // the two calls to read_settings must be split -- so the dictionary is prepared
330 // for the contents of every directory before the databases are updated.
331 void settings_read(const char *path)
333 struct setting_dictionary *new;
335 nmem = nmem_create();
338 new = nmem_malloc(nmem, sizeof(*new));
339 memset(new, 0, sizeof(*new));
340 initialize_hard_settings(new);
342 read_settings(path, prepare_dictionary);
343 read_settings(path, update_databases);
349 * indent-tabs-mode: nil
351 * vim: shiftwidth=4 tabstop=8 expandtab