1 // $Id: settings.c,v 1.7 2007-04-08 20:52:09 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[] = {
37 struct setting_dictionary
44 static struct setting_dictionary *dictionary = 0;
46 int settings_offset(const char *name)
52 for (i = 0; i < dictionary->num; i++)
53 if (!strcmp(name, dictionary->dict[i]))
58 // Ignores everything after second colon, if present
59 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
60 static int settings_offset_cprefix(const char *name)
66 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
67 maxlen = (p - name) + 1;
68 for (i = 0; i < dictionary->num; i++)
69 if (!strncmp(name, dictionary->dict[i], maxlen))
74 char *settings_name(int offset)
76 return dictionary->dict[offset];
79 static int isdir(const char *path)
83 if (stat(path, &st) < 0)
85 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
88 return st.st_mode & S_IFDIR;
91 // Read settings from an XML file, calling handler function for each setting
92 static void read_settings_file(const char *path,
93 void (*fun)(struct setting *set))
95 xmlDoc *doc = xmlParseFile(path);
97 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
101 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
104 n = xmlDocGetRootElement(doc);
105 namea = xmlGetProp(n, (xmlChar *) "name");
106 targeta = xmlGetProp(n, (xmlChar *) "target");
107 valuea = xmlGetProp(n, (xmlChar *) "value");
108 usera = xmlGetProp(n, (xmlChar *) "user");
109 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
110 for (n = n->children; n; n = n->next)
112 if (n->type != XML_ELEMENT_NODE)
114 if (!strcmp((const char *) n->name, "set"))
116 char *name, *target, *value, *user, *precedence;
118 name = (char *) xmlGetProp(n, (xmlChar *) "name");
119 target = (char *) xmlGetProp(n, (xmlChar *) "target");
120 value = (char *) xmlGetProp(n, (xmlChar *) "value");
121 user = (char *) xmlGetProp(n, (xmlChar *) "user");
122 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
124 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
126 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
137 // Copy everything into a temporary buffer -- we decide
138 // later if we are keeping it.
140 set.precedence = atoi((char *) precedence);
141 else if (precedencea)
142 set.precedence = atoi((char *) precedencea);
149 strcpy(userb, (const char *) usera);
153 strcpy(targetb, target);
155 strcpy(targetb, (const char *) targeta);
156 set.target = targetb;
160 strcpy(nameb, (const char *) namea);
163 strcpy(valueb, value);
165 strcpy(valueb, (const char *) valuea);
178 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
183 xmlFree(precedencea);
189 // Recursively read files in a directory structure, calling
190 // callback for each one
191 static void read_settings(const char *path,
192 void (*fun)(struct setting *set))
197 if (!(d = opendir(path)))
199 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
202 while ((de = readdir(d)))
205 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
207 sprintf(tmp, "%s/%s", path, de->d_name);
209 read_settings(tmp, fun);
213 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
214 read_settings_file(tmp, fun);
220 // Callback. Adds a new entry to the dictionary if necessary
221 // This is used in pass 1 to determine layout of dictionary
222 static void prepare_dictionary(struct setting *set)
227 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
229 for (i = 0; i < dictionary->num; i++)
230 if (!strcmp(dictionary->dict[i], set->name))
232 if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
234 yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
237 // Create a new dictionary entry
238 // Grow dictionary if necessary
239 if (!dictionary->size)
240 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
241 else if (dictionary->num + 1 > dictionary->size)
243 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
244 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
245 dictionary->dict = tmp;
246 dictionary->size *= 2;
248 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
251 // This is called from grep_databases -- adds/overrides setting for a target
252 // This is also where the rules for precedence of settings are implemented
253 static void update_database(void *context, struct database *db)
255 struct setting *set = (struct setting *) context;
256 struct setting *s, **sp;
261 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
262 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
264 if ((offset = settings_offset_cprefix(set->name)) < 0)
265 abort(); // Should never get here
267 // First we determine if this setting is overriding any existing settings
268 // with the same name.
269 for (s = db->settings[offset], sp = &db->settings[offset]; s;
270 sp = &s->next, s = s->next)
271 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
273 if (s->precedence < set->precedence)
274 // We discard the value (nmem keeps track of the space)
276 else if (s->precedence > set->precedence)
277 // Db contains a higher-priority setting. Abort
279 if (*s->target == '*' && *set->target != '*')
280 // target-specific value trumps wildcard. Delete.
282 else if (*s->target != '*' && *set->target == '*')
283 // Db already contains higher-priority setting. Abort
286 if (!s) // s will be null when there are no higher-priority settings -- we add one
288 struct setting *new = nmem_malloc(nmem, sizeof(*new));
290 memset(new, 0, sizeof(*new));
291 new->precedence = set->precedence;
292 new->target = nmem_strdup(nmem, set->target);
293 new->name = nmem_strdup(nmem, set->name);
294 new->value = nmem_strdup(nmem, set->value);
295 new->user = nmem_strdup(nmem, set->user);
296 new->next = db->settings[offset];
297 db->settings[offset] = new;
301 // Callback -- updates database records with dictionary entries as appropriate
302 // This is used in pass 2 to assign name/value pairs to databases
303 static void update_databases(struct setting *set)
305 struct database_criterion crit;
306 struct database_criterion_value val;
308 // Update all databases which match pattern in set->target
312 val.value = set->target;
314 grep_databases(set, &crit, update_database);
317 // This simply copies the 'hard' (application-specific) settings
318 // to the settings dictionary.
319 static void initialize_hard_settings(struct setting_dictionary *dict)
321 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
322 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
323 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
324 dict->num = dict->size;
327 // If we ever decide we need to be able to specify multiple settings directories,
328 // the two calls to read_settings must be split -- so the dictionary is prepared
329 // for the contents of every directory before the databases are updated.
330 void settings_read(const char *path)
332 struct setting_dictionary *new;
334 nmem = nmem_create();
337 new = nmem_malloc(nmem, sizeof(*new));
338 memset(new, 0, sizeof(*new));
339 initialize_hard_settings(new);
341 read_settings(path, prepare_dictionary);
342 read_settings(path, update_databases);
348 * indent-tabs-mode: nil
350 * vim: shiftwidth=4 tabstop=8 expandtab