reverting to exit call, as it might be dangerous to have a dictionary which is not...
[pazpar2-moved-to-github.git] / src / settings.c
1 /* $Id: settings.c,v 1.20 2007-04-23 09:06:20 marc 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     "pz:ip",
62     0
63 };
64
65 struct setting_dictionary
66 {
67     char **dict;
68     int size;
69     int num;
70 };
71
72 static struct setting_dictionary *dictionary = 0;
73
74 // Returns size of settings directory
75 int settings_num(void)
76 {
77     return dictionary->num;
78 }
79
80 int settings_offset(const char *name)
81 {
82     int i;
83
84     if (!name)
85         name = "";
86     for (i = 0; i < dictionary->num; i++)
87         if (!strcmp(name, dictionary->dict[i]))
88             return i;
89     return -1;
90 }
91
92 // Ignores everything after second colon, if present
93 // A bit of a hack to support the pz:cclmap: scheme (and more to come?)
94 static int settings_offset_cprefix(const char *name)
95 {
96     const char *p;
97     int maxlen = 100;
98     int i;
99
100     if (!strncmp("pz:", name, 3) && (p = strchr(name + 3, ':')))
101         maxlen = (p - name) + 1;
102     for (i = 0; i < dictionary->num; i++)
103         if (!strncmp(name, dictionary->dict[i], maxlen))
104             return i;
105     return -1;
106 }
107
108 char *settings_name(int offset)
109 {
110     return dictionary->dict[offset];
111 }
112
113 static int isdir(const char *path)
114 {
115     struct stat st;
116
117     if (stat(path, &st) < 0)
118     {
119         yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
120         exit(1);
121     }
122     return st.st_mode & S_IFDIR;
123 }
124
125 // Read settings from an XML file, calling handler function for each setting
126 static void read_settings_file(const char *path,
127         void (*fun)(struct setting *set))
128 {
129     xmlDoc *doc = xmlParseFile(path);
130     xmlNode *n;
131     xmlChar *namea, *targeta, *valuea, *usera, *precedencea;
132
133     if (!doc)
134     {
135         yaz_log(YLOG_FATAL, "Failed to parse %s", path);
136         exit(1);
137     }
138     n = xmlDocGetRootElement(doc);
139     namea = xmlGetProp(n, (xmlChar *) "name");
140     targeta = xmlGetProp(n, (xmlChar *) "target");
141     valuea = xmlGetProp(n, (xmlChar *) "value");
142     usera = xmlGetProp(n, (xmlChar *) "user");
143     precedencea = xmlGetProp(n, (xmlChar *) "precedence");
144     for (n = n->children; n; n = n->next)
145     {
146         if (n->type != XML_ELEMENT_NODE)
147             continue;
148         if (!strcmp((const char *) n->name, "set"))
149         {
150             char *name, *target, *value, *user, *precedence;
151
152             name = (char *) xmlGetProp(n, (xmlChar *) "name");
153             target = (char *) xmlGetProp(n, (xmlChar *) "target");
154             value = (char *) xmlGetProp(n, (xmlChar *) "value");
155             user = (char *) xmlGetProp(n, (xmlChar *) "user");
156             precedence = (char *) xmlGetProp(n, (xmlChar *) "precedence");
157
158             if ((!name && !namea) || (!value && !valuea) || (!target && !targeta))
159             {
160                 yaz_log(YLOG_FATAL, "set must specify name, value, and target");
161                 exit(1);
162             }
163             else
164             {
165                 struct setting set;
166                 char nameb[1024];
167                 char targetb[1024];
168                 char valueb[1024];
169
170                 // Copy everything into a temporary buffer -- we decide
171                 // later if we are keeping it.
172                 if (precedence)
173                     set.precedence = atoi((char *) precedence);
174                 else if (precedencea)
175                     set.precedence = atoi((char *) precedencea);
176                 else
177                     set.precedence = 0;
178                 if (target)
179                     strcpy(targetb, target);
180                 else
181                     strcpy(targetb, (const char *) targeta);
182                 set.target = targetb;
183                 if (name)
184                     strcpy(nameb, name);
185                 else
186                     strcpy(nameb, (const char *) namea);
187                 set.name = nameb;
188                 if (value)
189                     strcpy(valueb, value);
190                 else
191                     strcpy(valueb, (const char *) valuea);
192                 set.value = valueb;
193                 set.next = 0;
194                 (*fun)(&set);
195             }
196             xmlFree(name);
197             xmlFree(precedence);
198             xmlFree(value);
199             xmlFree(user);
200             xmlFree(target);
201         }
202         else
203         {
204             yaz_log(YLOG_FATAL, "Unknown element %s in settings file", (char*) n->name);
205             exit(1);
206         }
207     }
208     xmlFree(namea);
209     xmlFree(precedencea);
210     xmlFree(valuea);
211     xmlFree(usera);
212     xmlFree(targeta);
213
214     xmlFreeDoc(doc);
215 }
216  
217 // Recursively read files or directories, invoking a 
218 // callback for each one
219 static void read_settings(const char *path,
220                 void (*fun)(struct setting *set))
221 {
222     DIR *d;
223     struct dirent *de;
224     char *dot;
225
226     if (isdir(path))
227     {
228         if (!(d = opendir(path)))
229         {
230             yaz_log(YLOG_FATAL|YLOG_ERRNO, "%s", path);
231             exit(1);
232         }
233         while ((de = readdir(d)))
234         {
235             char tmp[1024];
236             if (*de->d_name == '.' || !strcmp(de->d_name, "CVS"))
237                 continue;
238             sprintf(tmp, "%s/%s", path, de->d_name);
239             read_settings(tmp, fun);
240         }
241         closedir(d);
242     }
243     else if ((dot = rindex(path, '.')) && !strcmp(dot + 1, "xml"))
244         read_settings_file(path, fun);
245 }
246
247 // Callback. Adds a new entry to the dictionary if necessary
248 // This is used in pass 1 to determine layout of dictionary
249 // and to load any databases mentioned
250 static void prepare_dictionary(struct setting *set)
251 {
252     int i;
253     char *p;
254
255     // If target address is not wildcard, add the database
256     if (*set->target && set->target[strlen(set->target) - 1] != '*')
257         find_database(set->target, 0);
258
259     // Determine if we already have a dictionary entry
260     if (!strncmp(set->name, "pz:", 3) && (p = strchr(set->name + 3, ':')))
261         *(p + 1) = '\0';
262     for (i = 0; i < dictionary->num; i++)
263         if (!strcmp(dictionary->dict[i], set->name))
264             return;
265
266     if (!strncmp(set->name, "pz:", 3)) // Probably a typo in config file
267         {
268             yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
269             exit(1);
270         }
271
272     // Create a new dictionary entry
273     // Grow dictionary if necessary
274     if (!dictionary->size)
275         dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
276     else if (dictionary->num + 1 > dictionary->size)
277     {
278         char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
279         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
280         dictionary->dict = tmp;
281         dictionary->size *= 2;
282     }
283     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
284 }
285
286 // This is called from grep_databases -- adds/overrides setting for a target
287 // This is also where the rules for precedence of settings are implemented
288 static void update_database(void *context, struct database *db)
289 {
290     struct setting *set = (struct setting *) context;
291     struct setting *s, **sp;
292     int offset;
293
294     // Is this the right database?
295     if (!match_zurl(db->url, set->target))
296         return;
297
298     // Initialize settings array if it doesn't exist.
299     // If so, also set the 'id' automatic setting
300     if (!db->settings)
301     {
302         struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
303
304         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
305         memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
306         id->precedence = 0;
307         id->name = "pz:id";
308         id->target = id->value = db->url;
309         id->next = 0;
310         db->settings[PZ_ID] = id;
311     }
312     if ((offset = settings_offset_cprefix(set->name)) < 0)
313         abort(); // Should never get here
314
315     // First we determine if this setting is overriding  any existing settings
316     // with the same name.
317     for (s = db->settings[offset], sp = &db->settings[offset]; s;
318             sp = &s->next, s = s->next)
319         if (!strcmp(s->name, set->name))
320         {
321             if (s->precedence < set->precedence)
322                 // We discard the value (nmem keeps track of the space)
323                 *sp = (*sp)->next;
324             else if (s->precedence > set->precedence)
325                 // Db contains a higher-priority setting. Abort 
326                 break;
327             if (*s->target == '*' && *set->target != '*')
328                 // target-specific value trumps wildcard. Delete.
329                 *sp = (*sp)->next;
330             else if (*s->target != '*' && *set->target == '*')
331                 // Db already contains higher-priority setting. Abort
332                 break;
333         }
334     if (!s) // s will be null when there are no higher-priority settings -- we add one
335     {
336         struct setting *new = nmem_malloc(nmem, sizeof(*new));
337
338         memset(new, 0, sizeof(*new));
339         new->precedence = set->precedence;
340         new->target = nmem_strdup(nmem, set->target);
341         new->name = nmem_strdup(nmem, set->name);
342         new->value = nmem_strdup(nmem, set->value);
343         new->next = db->settings[offset];
344         db->settings[offset] = new;
345     }
346 }
347
348 // Callback -- updates database records with dictionary entries as appropriate
349 // This is used in pass 2 to assign name/value pairs to databases
350 static void update_databases(struct setting *set)
351 {
352     grep_databases(set, 0, update_database);
353 }
354
355 // This simply copies the 'hard' (application-specific) settings
356 // to the settings dictionary.
357 static void initialize_hard_settings(struct setting_dictionary *dict)
358 {
359     dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
360     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
361     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
362     dict->num = dict->size;
363 }
364
365 // If we ever decide we need to be able to specify multiple settings directories,
366 // the two calls to read_settings must be split -- so the dictionary is prepared
367 // for the contents of every directory before the databases are updated.
368 void settings_read(const char *path)
369 {
370     read_settings(path, prepare_dictionary);
371     read_settings(path, update_databases);
372 }
373
374 void init_settings(void)
375 {
376     struct setting_dictionary *new;
377     if (!nmem)
378         nmem = nmem_create();
379     else
380         nmem_reset(nmem);
381     new = nmem_malloc(nmem, sizeof(*new));
382     memset(new, 0, sizeof(*new));
383     initialize_hard_settings(new);
384     dictionary = new;
385 }
386
387 /*
388  * Local variables:
389  * c-basic-offset: 4
390  * indent-tabs-mode: nil
391  * End:
392  * vim: shiftwidth=4 tabstop=8 expandtab
393  */