*** empty log message ***
[pazpar2-moved-to-github.git] / src / settings.c
1 /* $Id: settings.c,v 1.17 2007-04-20 15:36:48 quinn Exp $
2    Copyright (c) 2006-2007, Index Data.
3
4 This file is part of Pazpar2.
5
6 Pazpar2 is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
10
11 Pazpar2 is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Pazpar2; see the file LICENSE.  If not, write to the
18 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA.
20  */
21
22
23 // This module implements a generic system of settings
24 // (attribute-value) that can be associated with search targets. The
25 // system supports both default values, per-target overrides, and
26 // per-user settings.
27
28 #include <string.h>
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <dirent.h>
32 #include <stdlib.h>
33 #include <sys/stat.h>
34
35 #include <libxml/parser.h>
36 #include <libxml/tree.h>
37
38 #include <yaz/nmem.h>
39 #include <yaz/log.h>
40
41 #include "pazpar2.h"
42 #include "database.h"
43 #include "settings.h"
44
45 static NMEM nmem = 0;
46
47 // Used for initializing setting_dictionary with pazpar2-specific settings
48 static char *hard_settings[] = {
49     "pz:piggyback",
50     "pz:elements",
51     "pz:requestsyntax",
52     "pz:cclmap:",
53     "pz:xslt",
54     "pz:nativesyntax",
55     "pz:authentication",
56     "pz:allow",
57     "pz:maxrecs",
58     "pz:id",
59     "pz:name",
60     "pz:queryencoding",
61     0
62 };
63
64 struct setting_dictionary
65 {
66     char **dict;
67     int size;
68     int num;
69 };
70
71 static struct setting_dictionary *dictionary = 0;
72
73 // Returns size of settings directory
74 int settings_num(void)
75 {
76     return dictionary->num;
77 }
78
79 int settings_offset(const char *name)
80 {
81     int i;
82
83     if (!name)
84         name = "";
85     for (i = 0; i < dictionary->num; i++)
86         if (!strcmp(name, dictionary->dict[i]))
87             return i;
88     return -1;
89 }
90
91 // Ignores everything after second colon, if present
92 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
93 static int settings_offset_cprefix(const char *name)
94 {
95     const char *p;
96     int maxlen = 100;
97     int i;
98
99     if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
100         maxlen = (p - name) + 1;
101     for (i = 0; i < dictionary->num; i++)
102         if (!strncmp(name, dictionary->dict[i], maxlen))
103             return i;
104     return -1;
105 }
106
107 char *settings_name(int offset)
108 {
109     return dictionary->dict[offset];
110 }
111
112 static int isdir(const char *path)
113 {
114     struct stat st;
115
116     if (stat(path, &st) < 0)
117     {
118         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
119         exit(1);
120     }
121     return st.st_mode & S_IFDIR;
122 }
123
124 // Read settings from an XML file, calling handler function for each setting
125 static void read_settings_file(const char *path,
126         void (*fun)(struct setting *set))
127 {
128     xmlDoc *doc = xmlParseFile(path);
129     xmlNode *n;
130     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
131
132     if (!doc)
133     {
134         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
135         exit(1);
136     }
137     n = xmlDocGetRootElement(doc);
138     namea = xmlGetProp(n, (xmlChar *) "name");
139     targeta = xmlGetProp(n, (xmlChar *) "target");
140     valuea = xmlGetProp(n, (xmlChar *) "value");
141     usera = xmlGetProp(n, (xmlChar *) "user");
142     precedencea = xmlGetProp(n, (xmlChar *) "precedence");
143     for (n = n->children; n; n = n->next)
144     {
145         if (n->type != XML_ELEMENT_NODE)
146             continue;
147         if (!strcmp((const char *) n->name, "set"))
148         {
149             char *name, *target, *value, *user, *precedence;
150
151             name = (char *) xmlGetProp(n, (xmlChar *) "name");
152             target = (char *) xmlGetProp(n, (xmlChar *) "target");
153             value = (char *) xmlGetProp(n, (xmlChar *) "value");
154             user = (char *) xmlGetProp(n, (xmlChar *) "user");
155             precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
156
157             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
158             {
159                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
160                 exit(1);
161             }
162             else
163             {
164                 struct setting set;
165                 char nameb[1024];
166                 char targetb[1024];
167                 char valueb[1024];
168
169                 // Copy everything into a temporary buffer -- we decide
170                 // later if we are keeping it.
171                 if (precedence)
172                     set.precedence = atoi((char *) precedence);
173                 else if (precedencea)
174                     set.precedence = atoi((char *) precedencea);
175                 else
176                     set.precedence = 0;
177                 if (target)
178                     strcpy(targetb, target);
179                 else
180                     strcpy(targetb, (const char *) targeta);
181                 set.target = targetb;
182                 if (name)
183                     strcpy(nameb, name);
184                 else
185                     strcpy(nameb, (const char *) namea);
186                 set.name = nameb;
187                 if (value)
188                     strcpy(valueb, value);
189                 else
190                     strcpy(valueb, (const char *) valuea);
191                 set.value = valueb;
192                 set.next = 0;
193                 (*fun)(&set);
194             }
195             xmlFree(name);
196             xmlFree(precedence);
197             xmlFree(value);
198             xmlFree(user);
199             xmlFree(target);
200         }
201         else
202         {
203             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
204             exit(1);
205         }
206     }
207     xmlFree(namea);
208     xmlFree(precedencea);
209     xmlFree(valuea);
210     xmlFree(usera);
211     xmlFree(targeta);
212
213     xmlFreeDoc(doc);
214 }
215  
216 // Recursively read files or directories, invoking a 
217 // callback for each one
218 static void read_settings(const char *path,
219                 void (*fun)(struct setting *set))
220 {
221     DIR *d;
222     struct dirent *de;
223     char *dot;
224
225     if (isdir(path))
226     {
227         if (!(d = opendir(path)))
228         {
229             yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
230             exit(1);
231         }
232         while ((de = readdir(d)))
233         {
234             char tmp[1024];
235             if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
236                 continue;
237             sprintf(tmp, "%s/%s", path, de->d_name);
238             read_settings(tmp, fun);
239         }
240         closedir(d);
241     }
242     else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
243         read_settings_file(path, fun);
244 }
245
246 // Callback. Adds a new entry to the dictionary if necessary
247 // This is used in pass 1 to determine layout of dictionary
248 // and to load any databases mentioned
249 static void prepare_dictionary(struct setting *set)
250 {
251     int i;
252     char *p;
253
254     // If target address is not wildcard, add the database
255     if (*set->target && set->target[strlen(set->target) - 1] != '*')
256         find_database(set->target, 0);
257
258     // Determine if we already have a dictionary entry
259     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
260         *(p + 1) = '\0';
261     for (i = 0; i < dictionary->num; i++)
262         if (!strcmp(dictionary->dict[i], set->name))
263             return;
264
265     if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config fle
266     {
267         yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
268         exit(1);
269     }
270     // Create a new dictionary entry
271     // Grow dictionary if necessary
272     if (!dictionary->size)
273         dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
274     else if (dictionary->num + 1 > dictionary->size)
275     {
276         char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
277         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
278         dictionary->dict = tmp;
279         dictionary->size *= 2;
280     }
281     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
282 }
283
284 // This is called from grep_databases -- adds/overrides setting for a target
285 // This is also where the rules for precedence of settings are implemented
286 static void update_database(void *context, struct database *db)
287 {
288     struct setting *set = (struct setting *) context;
289     struct setting *s, **sp;
290     int offset;
291
292     // Is this the right database?
293     if (!match_zurl(db->url, set->target))
294         return;
295
296     // Initialize settings array if it doesn't exist.
297     // If so, also set the 'id' automatic setting
298     if (!db->settings)
299     {
300         struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
301
302         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
303         memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
304         id->precedence = 0;
305         id->name = "pz:id";
306         id->target = id->value = db->url;
307         id->next = 0;
308         db->settings[PZ_ID] = id;
309     }
310     if ((offset = settings_offset_cprefix(set->name)) < 0)
311         abort(); // Should never get here
312
313     // First we determine if this setting is overriding  any existing settings
314     // with the same name.
315     for (s = db->settings[offset], sp = &db->settings[offset]; s;
316             sp = &s->next, s = s->next)
317         if (!strcmp(s->name, set->name))
318         {
319             if (s->precedence < set->precedence)
320                 // We discard the value (nmem keeps track of the space)
321                 *sp = (*sp)->next;
322             else if (s->precedence > set->precedence)
323                 // Db contains a higher-priority setting. Abort 
324                 break;
325             if (*s->target == '*' && *set->target != '*')
326                 // target-specific value trumps wildcard. Delete.
327                 *sp = (*sp)->next;
328             else if (*s->target != '*' && *set->target == '*')
329                 // Db already contains higher-priority setting. Abort
330                 break;
331         }
332     if (!s) // s will be null when there are no higher-priority settings -- we add one
333     {
334         struct setting *new = nmem_malloc(nmem, sizeof(*new));
335
336         memset(new, 0, sizeof(*new));
337         new->precedence = set->precedence;
338         new->target = nmem_strdup(nmem, set->target);
339         new->name = nmem_strdup(nmem, set->name);
340         new->value = nmem_strdup(nmem, set->value);
341         new->next = db->settings[offset];
342         db->settings[offset] = new;
343     }
344 }
345
346 // Callback -- updates database records with dictionary entries as appropriate
347 // This is used in pass 2 to assign name/value pairs to databases
348 static void update_databases(struct setting *set)
349 {
350     grep_databases(set, 0, update_database);
351 }
352
353 // This simply copies the 'hard' (application-specific) settings
354 // to the settings dictionary.
355 static void initialize_hard_settings(struct setting_dictionary *dict)
356 {
357     dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
358     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
359     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
360     dict->num = dict->size;
361 }
362
363 // If we ever decide we need to be able to specify multiple settings directories,
364 // the two calls to read_settings must be split -- so the dictionary is prepared
365 // for the contents of every directory before the databases are updated.
366 void settings_read(const char *path)
367 {
368     struct setting_dictionary *new;
369     if (!nmem)
370         nmem = nmem_create();
371     else
372         nmem_reset(nmem);
373     new = nmem_malloc(nmem, sizeof(*new));
374     memset(new, 0, sizeof(*new));
375     initialize_hard_settings(new);
376     dictionary = new;
377     read_settings(path, prepare_dictionary);
378     read_settings(path, update_databases);
379 }
380
381 /*
382  * Local variables:
383  * c-basic-offset: 4
384  * indent-tabs-mode: nil
385  * End:
386  * vim: shiftwidth=4 tabstop=8 expandtab
387  */