1 // $Id: settings.c,v 1.3 2007-03-29 13:44:19 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.
14 #include <libxml/parser.h>
15 #include <libxml/tree.h>
26 // Used for initializing setting_dictionary with pazpar2-specific settings
27 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 char *settings_name(int offset)
55 return dictionary->dict[offset];
58 static int isdir(const char *path)
62 if (stat(path, &st) < 0)
64 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
67 return st.st_mode & S_IFDIR;
70 // Read settings from an XML file, calling handler function for each setting
71 static void read_settings_file(const char *path,
72 void (*fun)(struct setting *set))
74 xmlDoc *doc = xmlParseFile(path);
76 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
80 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
83 n = xmlDocGetRootElement(doc);
84 namea = xmlGetProp(n, (xmlChar *) "name");
85 targeta = xmlGetProp(n, (xmlChar *) "target");
86 valuea = xmlGetProp(n, (xmlChar *) "value");
87 usera = xmlGetProp(n, (xmlChar *) "user");
88 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
89 for (n = n->children; n; n = n->next)
91 if (n->type != XML_ELEMENT_NODE)
93 if (!strcmp(n->name, (xmlChar *) "set"))
95 char *name, *target, *value, *user, *precedence;
97 name = xmlGetProp(n, (xmlChar *) "name");
98 target = xmlGetProp(n, (xmlChar *) "target");
99 value = xmlGetProp(n, (xmlChar *) "value");
100 user = xmlGetProp(n, (xmlChar *) "user");
101 precedence = xmlGetProp(n, (xmlChar *) "precedence");
103 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
105 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
116 // Copy everything into a temporary buffer -- we decide
117 // later if we are keeping it.
119 set.precedence = atoi((char *) precedence);
120 else if (precedencea)
121 set.precedence = atoi((char *) precedencea);
128 strcpy(userb, usera);
132 strcpy(targetb, target);
134 strcpy(targetb, targeta);
135 set.target = targetb;
139 strcpy(nameb, namea);
142 strcpy(valueb, value);
144 strcpy(valueb, valuea);
157 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
162 xmlFree(precedencea);
168 // Recursively read files in a directory structure, calling
169 // callback for each one
170 static void read_settings(const char *path,
171 void (*fun)(struct setting *set))
176 if (!(d = opendir(path)))
178 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
181 while ((de = readdir(d)))
184 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
186 sprintf(tmp, "%s/%s", path, de->d_name);
188 read_settings(tmp, fun);
192 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
193 read_settings_file(tmp, fun);
199 // Callback. Adds a new entry to the dictionary if necessary
200 // This is used in pass 1 to determine layout of dictionary
201 static void prepare_dictionary(struct setting *set)
205 for (i = 0; i < dictionary->num; i++)
206 if (!strcmp(dictionary->dict[i], set->name))
208 // Create a new dictionary entry
209 // Grow dictionary if necessary
210 if (!dictionary->size)
211 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
212 else if (dictionary->num + 1 > dictionary->size)
214 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
215 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
216 dictionary->dict = tmp;
217 dictionary->size *= 2;
219 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
222 // This is called from grep_databases -- adds/overrides setting for a target
223 // This is also where the rules for precedence of settings are implemented
224 static void update_database(void *context, struct database *db)
226 struct setting *set = (struct setting *) context;
227 struct setting *s, **sp;
232 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
233 memset(db->settings, sizeof(struct settings*) * dictionary->num, 0);
235 if ((offset = settings_offset(set->name)) < 0)
236 abort(); // Should never get here
238 // First we determine if this setting is overriding any existing settings
239 // with the same name.
240 for (s = db->settings[offset], sp = &db->settings[offset]; s;
241 sp = &s->next, s = s->next)
242 if (!strcmp(s->user, set->user))
244 if (s->precedence < set->precedence)
245 // We discard the value (nmem keeps track of the space)
247 else if (s->precedence > set->precedence)
248 // Db contains a higher-priority setting. Abort
250 if (*s->target == '*' && *set->target != '*')
251 // target-specific value trumps wildcard. Delete.
253 else if (*s->target != '*' && *set->target == '*')
254 // Db already contains higher-priority setting. Abort
257 if (!s) // s will be null when there are no higher-priority settings -- we add one
259 struct setting *new = nmem_malloc(nmem, sizeof(*new));
261 memset(new, sizeof(*new), 0);
262 new->precedence = set->precedence;
263 new->target = nmem_strdup(nmem, set->target);
264 new->name = nmem_strdup(nmem, set->name);
265 new->value = nmem_strdup(nmem, set->value);
266 new->user = nmem_strdup(nmem, set->user);
267 new->next = db->settings[offset];
268 db->settings[offset] = new;
272 // Callback -- updates database records with dictionary entries as appropriate
273 // This is used in pass 2 to assign name/value pairs to databases
274 static void update_databases(struct setting *set)
276 struct database_criterion crit;
277 struct database_criterion_value val;
279 // Update all databases which match pattern in set->target
283 val.value = set->target;
285 grep_databases(set, &crit, update_database);
288 // This simply copies the 'hard' (application-specific) settings
289 // to the settings dictionary.
290 static void initialize_hard_settings(struct setting_dictionary *dict)
292 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
293 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
294 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
295 dict->num = dict->size;
298 // If we ever decide we need to be able to specify multiple settings directories,
299 // the two calls to read_settings must be split -- so the dictionary is prepared
300 // for the contents of every directory before the databases are updated.
301 void settings_read(const char *path)
303 struct setting_dictionary *new;
305 nmem = nmem_create();
308 new = nmem_malloc(nmem, sizeof(*new));
309 initialize_hard_settings(new);
310 memset(new, sizeof(*new), 0);
312 read_settings(path, prepare_dictionary);
313 read_settings(path, update_databases);
319 * indent-tabs-mode: nil
321 * vim: shiftwidth=4 tabstop=8 expandtab