1 // $Id: settings.c,v 1.9 2007-04-10 00:53:24 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[] = {
40 struct setting_dictionary
47 static struct setting_dictionary *dictionary = 0;
49 int settings_offset(const char *name)
55 for (i = 0; i < dictionary->num; i++)
56 if (!strcmp(name, dictionary->dict[i]))
61 // Ignores everything after second colon, if present
62 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
63 static int settings_offset_cprefix(const char *name)
69 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
70 maxlen = (p - name) + 1;
71 for (i = 0; i < dictionary->num; i++)
72 if (!strncmp(name, dictionary->dict[i], maxlen))
77 char *settings_name(int offset)
79 return dictionary->dict[offset];
82 static int isdir(const char *path)
86 if (stat(path, &st) < 0)
88 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
91 return st.st_mode & S_IFDIR;
94 // Read settings from an XML file, calling handler function for each setting
95 static void read_settings_file(const char *path,
96 void (*fun)(struct setting *set))
98 xmlDoc *doc = xmlParseFile(path);
100 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
104 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
107 n = xmlDocGetRootElement(doc);
108 namea = xmlGetProp(n, (xmlChar *) "name");
109 targeta = xmlGetProp(n, (xmlChar *) "target");
110 valuea = xmlGetProp(n, (xmlChar *) "value");
111 usera = xmlGetProp(n, (xmlChar *) "user");
112 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
113 for (n = n->children; n; n = n->next)
115 if (n->type != XML_ELEMENT_NODE)
117 if (!strcmp((const char *) n->name, "set"))
119 char *name, *target, *value, *user, *precedence;
121 name = (char *) xmlGetProp(n, (xmlChar *) "name");
122 target = (char *) xmlGetProp(n, (xmlChar *) "target");
123 value = (char *) xmlGetProp(n, (xmlChar *) "value");
124 user = (char *) xmlGetProp(n, (xmlChar *) "user");
125 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
127 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
129 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
140 // Copy everything into a temporary buffer -- we decide
141 // later if we are keeping it.
143 set.precedence = atoi((char *) precedence);
144 else if (precedencea)
145 set.precedence = atoi((char *) precedencea);
152 strcpy(userb, (const char *) usera);
156 strcpy(targetb, target);
158 strcpy(targetb, (const char *) targeta);
159 set.target = targetb;
163 strcpy(nameb, (const char *) namea);
166 strcpy(valueb, value);
168 strcpy(valueb, (const char *) valuea);
181 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
186 xmlFree(precedencea);
192 // Recursively read files in a directory structure, calling
193 // callback for each one
194 static void read_settings(const char *path,
195 void (*fun)(struct setting *set))
200 if (!(d = opendir(path)))
202 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
205 while ((de = readdir(d)))
208 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
210 sprintf(tmp, "%s/%s", path, de->d_name);
212 read_settings(tmp, fun);
216 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
217 read_settings_file(tmp, fun);
223 // Callback. Adds a new entry to the dictionary if necessary
224 // This is used in pass 1 to determine layout of dictionary
225 static void prepare_dictionary(struct setting *set)
230 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
232 for (i = 0; i < dictionary->num; i++)
233 if (!strcmp(dictionary->dict[i], set->name))
235 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
237 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
240 // Create a new dictionary entry
241 // Grow dictionary if necessary
242 if (!dictionary->size)
243 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
244 else if (dictionary->num + 1 > dictionary->size)
246 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
247 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
248 dictionary->dict = tmp;
249 dictionary->size *= 2;
251 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
254 // This is called from grep_databases -- adds/overrides setting for a target
255 // This is also where the rules for precedence of settings are implemented
256 static void update_database(void *context, struct database *db)
258 struct setting *set = (struct setting *) context;
259 struct setting *s, **sp;
264 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
265 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
267 if ((offset = settings_offset_cprefix(set->name)) < 0)
268 abort(); // Should never get here
270 // First we determine if this setting is overriding any existing settings
271 // with the same name.
272 for (s = db->settings[offset], sp = &db->settings[offset]; s;
273 sp = &s->next, s = s->next)
274 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
276 if (s->precedence < set->precedence)
277 // We discard the value (nmem keeps track of the space)
279 else if (s->precedence > set->precedence)
280 // Db contains a higher-priority setting. Abort
282 if (*s->target == '*' && *set->target != '*')
283 // target-specific value trumps wildcard. Delete.
285 else if (*s->target != '*' && *set->target == '*')
286 // Db already contains higher-priority setting. Abort
289 if (!s) // s will be null when there are no higher-priority settings -- we add one
291 struct setting *new = nmem_malloc(nmem, sizeof(*new));
293 memset(new, 0, sizeof(*new));
294 new->precedence = set->precedence;
295 new->target = nmem_strdup(nmem, set->target);
296 new->name = nmem_strdup(nmem, set->name);
297 new->value = nmem_strdup(nmem, set->value);
298 new->user = nmem_strdup(nmem, set->user);
299 new->next = db->settings[offset];
300 db->settings[offset] = new;
304 // Callback -- updates database records with dictionary entries as appropriate
305 // This is used in pass 2 to assign name/value pairs to databases
306 static void update_databases(struct setting *set)
308 struct database_criterion crit;
309 struct database_criterion_value val;
311 // Update all databases which match pattern in set->target
315 val.value = set->target;
317 grep_databases(set, &crit, update_database);
320 // This simply copies the 'hard' (application-specific) settings
321 // to the settings dictionary.
322 static void initialize_hard_settings(struct setting_dictionary *dict)
324 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
325 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
326 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
327 dict->num = dict->size;
330 // If we ever decide we need to be able to specify multiple settings directories,
331 // the two calls to read_settings must be split -- so the dictionary is prepared
332 // for the contents of every directory before the databases are updated.
333 void settings_read(const char *path)
335 struct setting_dictionary *new;
337 nmem = nmem_create();
340 new = nmem_malloc(nmem, sizeof(*new));
341 memset(new, 0, sizeof(*new));
342 initialize_hard_settings(new);
344 read_settings(path, prepare_dictionary);
345 read_settings(path, update_databases);
351 * indent-tabs-mode: nil
353 * vim: shiftwidth=4 tabstop=8 expandtab