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