removed warnings coming from missing casts (const char *) and (xmlChar *)
[pazpar2-moved-to-github.git] / src / settings.c
1 // $Id: settings.c,v 1.5 2007-03-31 19:55:25 marc 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.
5
6 #include <string.h>
7 #include <stdio.h>
8 #include <sys/types.h>
9 #include <dirent.h>
10 #include <stdlib.h>
11 #include <sys/stat.h>
12
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15
16 #include <yaz/nmem.h>
17 #include <yaz/log.h>
18
19 #include "pazpar2.h"
20 #include "database.h"
21 #include "settings.h"
22
23 static NMEM nmem = 0;
24
25 // Used for initializing setting_dictionary with pazpar2-specific settings
26 static char *hard_settings[] = {
27     "pz:piggyback",
28     "pz:elements",
29     "pz:syntax",
30     "pz:cclmap:",
31     0
32 };
33
34 struct setting_dictionary
35 {
36     char **dict;
37     int size;
38     int num;
39 };
40
41 static struct setting_dictionary *dictionary = 0;
42
43 int settings_offset(const char *name)
44 {
45     int i;
46
47     for (i = 0; i < dictionary->num; i++)
48         if (!strcmp(name, dictionary->dict[i]))
49             return i;
50     return -1;
51 }
52
53 // Ignores everything after second colon, if present
54 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
55 static int settings_offset_cprefix(const char *name)
56 {
57     const char *p;
58     int maxlen = 100;
59     int i;
60
61     if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
62         maxlen = (p - name) + 1;
63     for (i = 0; i < dictionary->num; i++)
64         if (!strncmp(name, dictionary->dict[i], maxlen))
65             return i;
66     return -1;
67 }
68
69 char *settings_name(int offset)
70 {
71     return dictionary->dict[offset];
72 }
73
74 static int isdir(const char *path)
75 {
76     struct stat st;
77
78     if (stat(path, &st) < 0)
79     {
80         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
81         exit(1);
82     }
83     return st.st_mode & S_IFDIR;
84 }
85
86 // Read settings from an XML file, calling handler function for each setting
87 static void read_settings_file(const char *path,
88         void (*fun)(struct setting *set))
89 {
90     xmlDoc *doc = xmlParseFile(path);
91     xmlNode *n;
92     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
93
94     if (!doc)
95     {
96         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
97         exit(1);
98     }
99     n = xmlDocGetRootElement(doc);
100     namea = xmlGetProp(n, (xmlChar *) "name");
101     targeta = xmlGetProp(n, (xmlChar *) "target");
102     valuea = xmlGetProp(n, (xmlChar *) "value");
103     usera = xmlGetProp(n, (xmlChar *) "user");
104     precedencea = xmlGetProp(n, (xmlChar *) "precedence");
105     for (n = n->children; n; n = n->next)
106     {
107         if (n->type != XML_ELEMENT_NODE)
108             continue;
109         if (!strcmp((const char *) n->name, "set"))
110         {
111             char *name, *target, *value, *user, *precedence;
112
113             name = (char *) xmlGetProp(n, (xmlChar *) "name");
114             target = (char *) xmlGetProp(n, (xmlChar *) "target");
115             value = (char *) xmlGetProp(n, (xmlChar *) "value");
116             user = (char *) xmlGetProp(n, (xmlChar *) "user");
117             precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
118
119             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
120             {
121                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
122                 exit(1);
123             }
124             else
125             {
126                 struct setting set;
127                 char nameb[1024];
128                 char targetb[1024];
129                 char userb[1024];
130                 char valueb[1024];
131
132                 // Copy everything into a temporary buffer -- we decide
133                 // later if we are keeping it.
134                 if (precedence)
135                     set.precedence = atoi((char *) precedence);
136                 else if (precedencea)
137                     set.precedence = atoi((char *) precedencea);
138                 else
139                     set.precedence = 0;
140                 set.user = userb;
141                 if (user)
142                     strcpy(userb, user);
143                 else if (usera)
144                     strcpy(userb, (const char *) usera);
145                 else
146                     set.user = "";
147                 if (target)
148                     strcpy(targetb, target);
149                 else
150                     strcpy(targetb, (const char *) targeta);
151                 set.target = targetb;
152                 if (name)
153                     strcpy(nameb, name);
154                 else
155                     strcpy(nameb, (const char *) namea);
156                 set.name = nameb;
157                 if (value)
158                     strcpy(valueb, value);
159                 else
160                     strcpy(valueb, (const char *) valuea);
161                 set.value = valueb;
162                 set.next = 0;
163                 (*fun)(&set);
164             }
165             xmlFree(name);
166             xmlFree(precedence);
167             xmlFree(value);
168             xmlFree(user);
169             xmlFree(target);
170         }
171         else
172         {
173             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
174             exit(1);
175         }
176     }
177     xmlFree(namea);
178     xmlFree(precedencea);
179     xmlFree(valuea);
180     xmlFree(usera);
181     xmlFree(targeta);
182 }
183  
184 // Recursively read files in a directory structure, calling 
185 // callback for each one
186 static void read_settings(const char *path,
187                 void (*fun)(struct setting *set))
188 {
189     DIR *d;
190     struct dirent *de;
191
192     if (!(d = opendir(path)))
193     {
194         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
195         exit(1);
196     }
197     while ((de = readdir(d)))
198     {
199         char tmp[1024];
200         if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
201             continue;
202         sprintf(tmp, "%s/%s", path, de->d_name);
203         if (isdir(tmp))
204             read_settings(tmp, fun);
205         else
206         {
207             char *dot;
208             if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
209                 read_settings_file(tmp, fun);
210         }
211     }
212     closedir(d);
213 }
214
215 // Callback. Adds a new entry to the dictionary if necessary
216 // This is used in pass 1 to determine layout of dictionary
217 static void prepare_dictionary(struct setting *set)
218 {
219     int i;
220     char *p;
221
222     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
223         *(p + 1) = '\0';
224     for (i = 0; i < dictionary->num; i++)
225         if (!strcmp(dictionary->dict[i], set->name))
226             return;
227     // Create a new dictionary entry
228     // Grow dictionary if necessary
229     if (!dictionary->size)
230         dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
231     else if (dictionary->num + 1 > dictionary->size)
232     {
233         char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
234         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
235         dictionary->dict = tmp;
236         dictionary->size *= 2;
237     }
238     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
239 }
240
241 // This is called from grep_databases -- adds/overrides setting for a target
242 // This is also where the rules for precedence of settings are implemented
243 static void update_database(void *context, struct database *db)
244 {
245     struct setting *set = (struct setting *) context;
246     struct setting *s, **sp;
247     int offset;
248
249     if (!db->settings)
250     {
251         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
252         memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
253     }
254     if ((offset = settings_offset_cprefix(set->name)) < 0)
255         abort(); // Should never get here
256
257     // First we determine if this setting is overriding  any existing settings
258     // with the same name.
259     for (s = db->settings[offset], sp = &db->settings[offset]; s;
260             sp = &s->next, s = s->next)
261         if (!strcmp(s->user, set->user) && !strcmp(s->name, set->name))
262         {
263             if (s->precedence < set->precedence)
264                 // We discard the value (nmem keeps track of the space)
265                 *sp = (*sp)->next;
266             else if (s->precedence > set->precedence)
267                 // Db contains a higher-priority setting. Abort 
268                 break;
269             if (*s->target == '*' && *set->target != '*')
270                 // target-specific value trumps wildcard. Delete.
271                 *sp = (*sp)->next;
272             else if (*s->target != '*' && *set->target == '*')
273                 // Db already contains higher-priority setting. Abort
274                 break;
275         }
276     if (!s) // s will be null when there are no higher-priority settings -- we add one
277     {
278         struct setting *new = nmem_malloc(nmem, sizeof(*new));
279
280         memset(new, 0, sizeof(*new));
281         new->precedence = set->precedence;
282         new->target = nmem_strdup(nmem, set->target);
283         new->name = nmem_strdup(nmem, set->name);
284         new->value = nmem_strdup(nmem, set->value);
285         new->user = nmem_strdup(nmem, set->user);
286         new->next = db->settings[offset];
287         db->settings[offset] = new;
288     }
289 }
290
291 // Callback -- updates database records with dictionary entries as appropriate
292 // This is used in pass 2 to assign name/value pairs to databases
293 static void update_databases(struct setting *set)
294 {
295     struct database_criterion crit;
296     struct database_criterion_value val;
297
298     // Update all databases which match pattern in set->target
299     crit.name = "id";
300     crit.values = &val;
301     crit.next = 0;
302     val.value = set->target;
303     val.next = 0;
304     grep_databases(set, &crit, update_database);
305 }
306
307 // This simply copies the 'hard' (application-specific) settings
308 // to the settings dictionary.
309 static void initialize_hard_settings(struct setting_dictionary *dict)
310 {
311     dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
312     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
313     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
314     dict->num = dict->size;
315 }
316
317 // If we ever decide we need to be able to specify multiple settings directories,
318 // the two calls to read_settings must be split -- so the dictionary is prepared
319 // for the contents of every directory before the databases are updated.
320 void settings_read(const char *path)
321 {
322     struct setting_dictionary *new;
323     if (!nmem)
324         nmem = nmem_create();
325     else
326         nmem_reset(nmem);
327     new = nmem_malloc(nmem, sizeof(*new));
328     memset(new, 0, sizeof(*new));
329     initialize_hard_settings(new);
330     dictionary = new;
331     read_settings(path, prepare_dictionary);
332     read_settings(path, update_databases);
333 }
334
335 /*
336  * Local variables:
337  * c-basic-offset: 4
338  * indent-tabs-mode: nil
339  * End:
340  * vim: shiftwidth=4 tabstop=8 expandtab
341  */