Moved CCL Map, record syntax, charset normalization, record syntax normalization...
[pazpar2-moved-to-github.git] / src / settings.c
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.
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:requestsyntax",
30     "pz:cclmap:",
31     "pz:encoding",
32     "pz:xslt",
33     "pz:nativesyntax",
34     0
35 };
36
37 struct setting_dictionary
38 {
39     char **dict;
40     int size;
41     int num;
42 };
43
44 static struct setting_dictionary *dictionary = 0;
45
46 int settings_offset(const char *name)
47 {
48     int i;
49
50     if (!name)
51         name = "";
52     for (i = 0; i < dictionary->num; i++)
53         if (!strcmp(name, dictionary->dict[i]))
54             return i;
55     return -1;
56 }
57
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)
61 {
62     const char *p;
63     int maxlen = 100;
64     int i;
65
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))
70             return i;
71     return -1;
72 }
73
74 char *settings_name(int offset)
75 {
76     return dictionary->dict[offset];
77 }
78
79 static int isdir(const char *path)
80 {
81     struct stat st;
82
83     if (stat(path, &st) < 0)
84     {
85         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
86         exit(1);
87     }
88     return st.st_mode & S_IFDIR;
89 }
90
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))
94 {
95     xmlDoc *doc = xmlParseFile(path);
96     xmlNode *n;
97     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
98
99     if (!doc)
100     {
101         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
102         exit(1);
103     }
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)
111     {
112         if (n->type != XML_ELEMENT_NODE)
113             continue;
114         if (!strcmp((const char *) n->name, "set"))
115         {
116             char *name, *target, *value, *user, *precedence;
117
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");
123
124             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
125             {
126                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
127                 exit(1);
128             }
129             else
130             {
131                 struct setting set;
132                 char nameb[1024];
133                 char targetb[1024];
134                 char userb[1024];
135                 char valueb[1024];
136
137                 // Copy everything into a temporary buffer -- we decide
138                 // later if we are keeping it.
139                 if (precedence)
140                     set.precedence = atoi((char *) precedence);
141                 else if (precedencea)
142                     set.precedence = atoi((char *) precedencea);
143                 else
144                     set.precedence = 0;
145                 set.user = userb;
146                 if (user)
147                     strcpy(userb, user);
148                 else if (usera)
149                     strcpy(userb, (const char *) usera);
150                 else
151                     set.user = "";
152                 if (target)
153                     strcpy(targetb, target);
154                 else
155                     strcpy(targetb, (const char *) targeta);
156                 set.target = targetb;
157                 if (name)
158                     strcpy(nameb, name);
159                 else
160                     strcpy(nameb, (const char *) namea);
161                 set.name = nameb;
162                 if (value)
163                     strcpy(valueb, value);
164                 else
165                     strcpy(valueb, (const char *) valuea);
166                 set.value = valueb;
167                 set.next = 0;
168                 (*fun)(&set);
169             }
170             xmlFree(name);
171             xmlFree(precedence);
172             xmlFree(value);
173             xmlFree(user);
174             xmlFree(target);
175         }
176         else
177         {
178             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
179             exit(1);
180         }
181     }
182     xmlFree(namea);
183     xmlFree(precedencea);
184     xmlFree(valuea);
185     xmlFree(usera);
186     xmlFree(targeta);
187 }
188  
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))
193 {
194     DIR *d;
195     struct dirent *de;
196
197     if (!(d = opendir(path)))
198     {
199         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
200         exit(1);
201     }
202     while ((de = readdir(d)))
203     {
204         char tmp[1024];
205         if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
206             continue;
207         sprintf(tmp, "%s/%s", path, de->d_name);
208         if (isdir(tmp))
209             read_settings(tmp, fun);
210         else
211         {
212             char *dot;
213             if ((dot = rindex(de->d_name, '.')) && !strcmp(dot + 1, "xml"))
214                 read_settings_file(tmp, fun);
215         }
216     }
217     closedir(d);
218 }
219
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)
223 {
224     int i;
225     char *p;
226
227     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
228         *(p + 1) = '\0';
229     for (i = 0; i < dictionary->num; i++)
230         if (!strcmp(dictionary->dict[i], set->name))
231             return;
232     if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
233     {
234         yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
235         exit(1);
236     }
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)
242     {
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;
247     }
248     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
249 }
250
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)
254 {
255     struct setting *set = (struct setting *) context;
256     struct setting *s, **sp;
257     int offset;
258
259     if (!db->settings)
260     {
261         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
262         memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
263     }
264     if ((offset = settings_offset_cprefix(set->name)) < 0)
265         abort(); // Should never get here
266
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))
272         {
273             if (s->precedence < set->precedence)
274                 // We discard the value (nmem keeps track of the space)
275                 *sp = (*sp)->next;
276             else if (s->precedence > set->precedence)
277                 // Db contains a higher-priority setting. Abort 
278                 break;
279             if (*s->target == '*' && *set->target != '*')
280                 // target-specific value trumps wildcard. Delete.
281                 *sp = (*sp)->next;
282             else if (*s->target != '*' && *set->target == '*')
283                 // Db already contains higher-priority setting. Abort
284                 break;
285         }
286     if (!s) // s will be null when there are no higher-priority settings -- we add one
287     {
288         struct setting *new = nmem_malloc(nmem, sizeof(*new));
289
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;
298     }
299 }
300
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)
304 {
305     struct database_criterion crit;
306     struct database_criterion_value val;
307
308     // Update all databases which match pattern in set->target
309     crit.name = "id";
310     crit.values = &val;
311     crit.next = 0;
312     val.value = set->target;
313     val.next = 0;
314     grep_databases(set, &crit, update_database);
315 }
316
317 // This simply copies the 'hard' (application-specific) settings
318 // to the settings dictionary.
319 static void initialize_hard_settings(struct setting_dictionary *dict)
320 {
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;
325 }
326
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)
331 {
332     struct setting_dictionary *new;
333     if (!nmem)
334         nmem = nmem_create();
335     else
336         nmem_reset(nmem);
337     new = nmem_malloc(nmem, sizeof(*new));
338     memset(new, 0, sizeof(*new));
339     initialize_hard_settings(new);
340     dictionary = new;
341     read_settings(path, prepare_dictionary);
342     read_settings(path, update_databases);
343 }
344
345 /*
346  * Local variables:
347  * c-basic-offset: 4
348  * indent-tabs-mode: nil
349  * End:
350  * vim: shiftwidth=4 tabstop=8 expandtab
351  */