1 // $Id: settings.c,v 1.6 2007-04-03 03:55:12 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[] = {
36 struct setting_dictionary
43 static struct setting_dictionary *dictionary = 0;
45 int settings_offset(const char *name)
51 for (i = 0; i < dictionary->num; i++)
52 if (!strcmp(name, dictionary->dict[i]))
57 // Ignores everything after second colon, if present
58 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
59 static int settings_offset_cprefix(const char *name)
65 if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
66 maxlen = (p - name) + 1;
67 for (i = 0; i < dictionary->num; i++)
68 if (!strncmp(name, dictionary->dict[i], maxlen))
73 char *settings_name(int offset)
75 return dictionary->dict[offset];
78 static int isdir(const char *path)
82 if (stat(path, &st) < 0)
84 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
87 return st.st_mode & S_IFDIR;
90 // Read settings from an XML file, calling handler function for each setting
91 static void read_settings_file(const char *path,
92 void (*fun)(struct setting *set))
94 xmlDoc *doc = xmlParseFile(path);
96 xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
100 yaz_log(YLOG_FATAL, "Failed to parse %s", path);
103 n = xmlDocGetRootElement(doc);
104 namea = xmlGetProp(n, (xmlChar *) "name");
105 targeta = xmlGetProp(n, (xmlChar *) "target");
106 valuea = xmlGetProp(n, (xmlChar *) "value");
107 usera = xmlGetProp(n, (xmlChar *) "user");
108 precedencea = xmlGetProp(n, (xmlChar *) "precedence");
109 for (n = n->children; n; n = n->next)
111 if (n->type != XML_ELEMENT_NODE)
113 if (!strcmp((const char *) n->name, "set"))
115 char *name, *target, *value, *user, *precedence;
117 name = (char *) xmlGetProp(n, (xmlChar *) "name");
118 target = (char *) xmlGetProp(n, (xmlChar *) "target");
119 value = (char *) xmlGetProp(n, (xmlChar *) "value");
120 user = (char *) xmlGetProp(n, (xmlChar *) "user");
121 precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
123 if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
125 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
136 // Copy everything into a temporary buffer -- we decide
137 // later if we are keeping it.
139 set.precedence = atoi((char *) precedence);
140 else if (precedencea)
141 set.precedence = atoi((char *) precedencea);
148 strcpy(userb, (const char *) usera);
152 strcpy(targetb, target);
154 strcpy(targetb, (const char *) targeta);
155 set.target = targetb;
159 strcpy(nameb, (const char *) namea);
162 strcpy(valueb, value);
164 strcpy(valueb, (const char *) valuea);
177 yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
182 xmlFree(precedencea);
188 // Recursively read files in a directory structure, calling
189 // callback for each one
190 static void read_settings(const char *path,
191 void (*fun)(struct setting *set))
196 if (!(d = opendir(path)))
198 yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
201 while ((de = readdir(d)))
204 if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
206 sprintf(tmp, "%s/%s", path, de->d_name);
208 read_settings(tmp, fun);
212 if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
213 read_settings_file(tmp, fun);
219 // Callback. Adds a new entry to the dictionary if necessary
220 // This is used in pass 1 to determine layout of dictionary
221 static void prepare_dictionary(struct setting *set)
226 if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
228 for (i = 0; i < dictionary->num; i++)
229 if (!strcmp(dictionary->dict[i], set->name))
231 // Create a new dictionary entry
232 // Grow dictionary if necessary
233 if (!dictionary->size)
234 dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
235 else if (dictionary->num + 1 > dictionary->size)
237 char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
238 memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
239 dictionary->dict = tmp;
240 dictionary->size *= 2;
242 dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
245 // This is called from grep_databases -- adds/overrides setting for a target
246 // This is also where the rules for precedence of settings are implemented
247 static void update_database(void *context, struct database *db)
249 struct setting *set = (struct setting *) context;
250 struct setting *s, **sp;
255 db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
256 memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
258 if ((offset = settings_offset_cprefix(set->name)) < 0)
259 abort(); // Should never get here
261 // First we determine if this setting is overriding any existing settings
262 // with the same name.
263 for (s = db->settings[offset], sp = &db->settings[offset]; s;
264 sp = &s->next, s = s->next)
265 if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
267 if (s->precedence < set->precedence)
268 // We discard the value (nmem keeps track of the space)
270 else if (s->precedence > set->precedence)
271 // Db contains a higher-priority setting. Abort
273 if (*s->target == '*' && *set->target != '*')
274 // target-specific value trumps wildcard. Delete.
276 else if (*s->target != '*' && *set->target == '*')
277 // Db already contains higher-priority setting. Abort
280 if (!s) // s will be null when there are no higher-priority settings -- we add one
282 struct setting *new = nmem_malloc(nmem, sizeof(*new));
284 memset(new, 0, sizeof(*new));
285 new->precedence = set->precedence;
286 new->target = nmem_strdup(nmem, set->target);
287 new->name = nmem_strdup(nmem, set->name);
288 new->value = nmem_strdup(nmem, set->value);
289 new->user = nmem_strdup(nmem, set->user);
290 new->next = db->settings[offset];
291 db->settings[offset] = new;
295 // Callback -- updates database records with dictionary entries as appropriate
296 // This is used in pass 2 to assign name/value pairs to databases
297 static void update_databases(struct setting *set)
299 struct database_criterion crit;
300 struct database_criterion_value val;
302 // Update all databases which match pattern in set->target
306 val.value = set->target;
308 grep_databases(set, &crit, update_database);
311 // This simply copies the 'hard' (application-specific) settings
312 // to the settings dictionary.
313 static void initialize_hard_settings(struct setting_dictionary *dict)
315 dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
316 dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
317 memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
318 dict->num = dict->size;
321 // If we ever decide we need to be able to specify multiple settings directories,
322 // the two calls to read_settings must be split -- so the dictionary is prepared
323 // for the contents of every directory before the databases are updated.
324 void settings_read(const char *path)
326 struct setting_dictionary *new;
328 nmem = nmem_create();
331 new = nmem_malloc(nmem, sizeof(*new));
332 memset(new, 0, sizeof(*new));
333 initialize_hard_settings(new);
335 read_settings(path, prepare_dictionary);
336 read_settings(path, update_databases);
342 * indent-tabs-mode: nil
344 * vim: shiftwidth=4 tabstop=8 expandtab