removed some of the most obvious exit() statements, which are now with the dynamic...
[pazpar2-moved-to-github.git] / src / settings.c
1 /* $Id: settings.c,v 1.19 2007-04-23 08:15:22 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 fle
267         yaz_log(YLOG_FATAL, "Unknown pz: setting '%s'", set->name);
268
269     // Create a new dictionary entry
270     // Grow dictionary if necessary
271     if (!dictionary->size)
272         dictionary->dict = nmem_malloc(nmem, (dictionary->size = 50) * sizeof(char*));
273     else if (dictionary->num + 1 > dictionary->size)
274     {
275         char **tmp = nmem_malloc(nmem, dictionary->size * 2 * sizeof(char*));
276         memcpy(tmp, dictionary->dict, dictionary->size * sizeof(char*));
277         dictionary->dict = tmp;
278         dictionary->size *= 2;
279     }
280     dictionary->dict[dictionary->num++] = nmem_strdup(nmem, set->name);
281 }
282
283 // This is called from grep_databases -- adds/overrides setting for a target
284 // This is also where the rules for precedence of settings are implemented
285 static void update_database(void *context, struct database *db)
286 {
287     struct setting *set = (struct setting *) context;
288     struct setting *s, **sp;
289     int offset;
290
291     // Is this the right database?
292     if (!match_zurl(db->url, set->target))
293         return;
294
295     // Initialize settings array if it doesn't exist.
296     // If so, also set the 'id' automatic setting
297     if (!db->settings)
298     {
299         struct setting *id = nmem_malloc(nmem, sizeof(struct setting));
300
301         db->settings = nmem_malloc(nmem, sizeof(struct settings*) * dictionary->num);
302         memset(db->settings, 0, sizeof(struct settings*) * dictionary->num);
303         id->precedence = 0;
304         id->name = "pz:id";
305         id->target = id->value = db->url;
306         id->next = 0;
307         db->settings[PZ_ID] = id;
308     }
309     if ((offset = settings_offset_cprefix(set->name)) < 0)
310         abort(); // Should never get here
311
312     // First we determine if this setting is overriding  any existing settings
313     // with the same name.
314     for (s = db->settings[offset], sp = &db->settings[offset]; s;
315             sp = &s->next, s = s->next)
316         if (!strcmp(s->name, set->name))
317         {
318             if (s->precedence < set->precedence)
319                 // We discard the value (nmem keeps track of the space)
320                 *sp = (*sp)->next;
321             else if (s->precedence > set->precedence)
322                 // Db contains a higher-priority setting. Abort 
323                 break;
324             if (*s->target == '*' && *set->target != '*')
325                 // target-specific value trumps wildcard. Delete.
326                 *sp = (*sp)->next;
327             else if (*s->target != '*' && *set->target == '*')
328                 // Db already contains higher-priority setting. Abort
329                 break;
330         }
331     if (!s) // s will be null when there are no higher-priority settings -- we add one
332     {
333         struct setting *new = nmem_malloc(nmem, sizeof(*new));
334
335         memset(new, 0, sizeof(*new));
336         new->precedence = set->precedence;
337         new->target = nmem_strdup(nmem, set->target);
338         new->name = nmem_strdup(nmem, set->name);
339         new->value = nmem_strdup(nmem, set->value);
340         new->next = db->settings[offset];
341         db->settings[offset] = new;
342     }
343 }
344
345 // Callback -- updates database records with dictionary entries as appropriate
346 // This is used in pass 2 to assign name/value pairs to databases
347 static void update_databases(struct setting *set)
348 {
349     grep_databases(set, 0, update_database);
350 }
351
352 // This simply copies the 'hard' (application-specific) settings
353 // to the settings dictionary.
354 static void initialize_hard_settings(struct setting_dictionary *dict)
355 {
356     dict->dict = nmem_malloc(nmem, sizeof(hard_settings) - sizeof(char*));
357     dict->size = (sizeof(hard_settings) - sizeof(char*)) / sizeof(char*);
358     memcpy(dict->dict, hard_settings, dict->size * sizeof(char*));
359     dict->num = dict->size;
360 }
361
362 // If we ever decide we need to be able to specify multiple settings directories,
363 // the two calls to read_settings must be split -- so the dictionary is prepared
364 // for the contents of every directory before the databases are updated.
365 void settings_read(const char *path)
366 {
367     read_settings(path, prepare_dictionary);
368     read_settings(path, update_databases);
369 }
370
371 void init_settings(void)
372 {
373     struct setting_dictionary *new;
374     if (!nmem)
375         nmem = nmem_create();
376     else
377         nmem_reset(nmem);
378     new = nmem_malloc(nmem, sizeof(*new));
379     memset(new, 0, sizeof(*new));
380     initialize_hard_settings(new);
381     dictionary = new;
382 }
383
384 /*
385  * Local variables:
386  * c-basic-offset: 4
387  * indent-tabs-mode: nil
388  * End:
389  * vim: shiftwidth=4 tabstop=8 expandtab
390  */