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