Do not use sync(2) after commit. The fsync on individual files suffices.
[idzebra-moved-to-github.git] / data1 / d1_absyn.c
1 /* $Id: d1_absyn.c,v 1.9.2.9 2006-09-28 18:38:41 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 #include <yaz/oid.h>
29 #include <yaz/log.h>
30 #include <data1.h>
31 #include <zebra_xpath.h>
32
33 #define D1_MAX_NESTING  128
34
35 struct data1_systag {
36     char *name;
37     char *value;
38     struct data1_systag *next;
39 };
40
41 struct data1_absyn_cache_info 
42 {
43     char *name;
44     data1_absyn *absyn;
45     data1_absyn_cache next;
46 };
47
48 struct data1_attset_cache_info 
49 {
50     char *name;
51     data1_attset *attset;
52     data1_attset_cache next;
53 };
54
55 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
56 {
57     data1_absyn_cache p = *data1_absyn_cache_get (dh);
58
59     while (p)
60     {
61         if (!strcmp (name, p->name))
62             return p->absyn;
63         p = p->next;
64     }
65     return NULL;
66 }
67 /* *ostrich*
68    We need to destroy DFAs, in xp_element (xelm) definitions 
69    pop, 2002-12-13
70 */
71
72 void data1_absyn_destroy (data1_handle dh)
73 {
74     data1_absyn_cache p = *data1_absyn_cache_get (dh);
75     
76     while (p)
77     {
78         data1_absyn *abs = p->absyn;
79         if (abs)
80         {
81             data1_xpelement *xpe = abs->xp_elements;
82             while (xpe) {
83                 logf (LOG_DEBUG,"Destroy xp element %s",xpe->xpath_expr);
84                 if (xpe->dfa) {  dfa_delete (&xpe->dfa); }
85                 xpe = xpe->next;
86             } 
87         }
88         p = p->next;
89     }
90 }
91
92
93 void data1_absyn_trav (data1_handle dh, void *handle,
94                        void (*fh)(data1_handle dh, void *h, data1_absyn *a))
95 {
96     data1_absyn_cache p = *data1_absyn_cache_get (dh);
97
98     while (p)
99     {
100         (*fh)(dh, handle, p->absyn);
101         p = p->next;
102     }
103 }
104
105 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
106 {
107     char fname[512];
108     NMEM mem = data1_nmem_get (dh);
109
110     data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
111     data1_absyn_cache *pp = data1_absyn_cache_get (dh);
112
113     sprintf(fname, "%s.abs", name);
114     p->absyn = data1_read_absyn (dh, fname, 0);
115     p->name = nmem_strdup (mem, name);
116     p->next = *pp;
117     *pp = p;
118     return p->absyn;
119 }
120
121 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
122 {
123     data1_absyn *absyn;
124
125     if (!(absyn = data1_absyn_search (dh, name)))
126         absyn = data1_absyn_add (dh, name);
127     return absyn;
128 }
129
130 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
131 {
132     data1_attset_cache p = *data1_attset_cache_get (dh);
133
134     while (p)
135     {
136         if (!strcmp (name, p->name))
137             return p->attset;
138         p = p->next;
139     }
140     return NULL;
141 }
142
143 data1_attset *data1_attset_search_id (data1_handle dh, int id)
144 {
145     data1_attset_cache p = *data1_attset_cache_get (dh);
146
147     while (p)
148     {
149         if (id == p->attset->reference)
150             return p->attset;
151         p = p->next;
152     }
153     return NULL;
154 }
155
156 data1_attset *data1_attset_add (data1_handle dh, const char *name)
157 {
158     char fname[512], aname[512];
159     NMEM mem = data1_nmem_get (dh);
160     data1_attset *attset;
161
162     strcpy (aname, name);
163     sprintf(fname, "%s.att", name);
164     attset = data1_read_attset (dh, fname);
165     if (!attset)
166     {
167         char *cp;
168         attset = data1_read_attset (dh, name);
169         if (attset && (cp = strrchr (aname, '.')))
170             *cp = '\0';
171     }
172     if (!attset)
173         yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
174     else
175     {
176         data1_attset_cache p = (data1_attset_cache)
177             nmem_malloc (mem, sizeof(*p));
178         data1_attset_cache *pp = data1_attset_cache_get (dh);
179         
180         attset->name = p->name = nmem_strdup (mem, aname);
181         p->attset = attset;
182         p->next = *pp;
183         *pp = p;
184     }
185     return attset;
186 }
187
188 data1_attset *data1_get_attset (data1_handle dh, const char *name)
189 {
190     data1_attset *attset;
191
192     if (!(attset = data1_attset_search_name (dh, name)))
193         attset = data1_attset_add (dh, name);
194     return attset;
195 }
196
197 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
198                                     const char *name)
199 {
200     data1_esetname *r;
201
202     for (r = a->esetnames; r; r = r->next)
203         if (!data1_matchstr(r->name, name))
204             return r;
205     return 0;
206 }
207
208 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
209                                           data1_element *parent,
210                                           const char *tagname)
211 {
212     data1_element *r;
213
214     /* It's now possible to have a data1 tree with no abstract syntax */
215     if ( !abs )
216         return 0;
217
218     if (!parent)
219         r = abs->main_elements;
220     else
221         r = parent->children;
222
223     for (; r; r = r->next)
224     {
225         data1_name *n;
226
227         for (n = r->tag->names; n; n = n->next)
228             if (!data1_matchstr(tagname, n->name))
229                 return r;
230     }
231     return 0;
232 }
233
234 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
235                                        const char *name)
236 {
237     data1_element *r;
238
239     /* It's now possible to have a data1 tree with no abstract syntax */
240     if ( !absyn )
241         return 0;
242     for (r = absyn->main_elements; r; r = r->next)
243         if (!data1_matchstr(r->name, name))
244             return r;
245     return 0;
246 }
247
248
249 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
250 {
251     /* It's now possible to have a data1 tree with no abstract syntax */
252     if ( !absyn )
253         return;
254
255     for (; e; e = e->next)
256     {
257         if (!e->sub_name)
258         {
259             if (e->children)
260                 fix_element_ref (dh, absyn, e->children);
261         }
262         else
263         {
264             data1_sub_elements *sub_e = absyn->sub_elements;
265             while (sub_e && strcmp (e->sub_name, sub_e->name))
266                 sub_e = sub_e->next;
267             if (sub_e)
268                 e->children = sub_e->elements;
269             else
270                 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
271                       e->sub_name);
272         }
273     }
274 }
275 /* *ostrich*
276
277    New function, a bit dummy now... I've seen it in zrpn.c... We should build
278    more clever regexps...
279
280
281       //a    ->    ^a/.*$
282       //a/b  ->    ^b/a/.*$
283       /a     ->    ^a/$
284       /a/b   ->    ^b/a/$
285
286       /      ->    none
287
288    pop, 2002-12-13
289
290    Now [] predicates are supported
291
292    pop, 2003-01-17
293
294  */
295
296 static const char * mk_xpath_regexp (data1_handle dh, const char *expr) 
297 {
298     const char *p = expr;
299     int abs = 1;
300     int e = 0;
301     char *stack[32];
302     char *res_p, *res = 0;
303     size_t res_size = 1;
304     
305     if (*p != '/')
306         return ("");
307     p++;
308     if (*p == '/') 
309     { 
310         abs =0;
311         p++;
312     }
313     while (*p)
314     {
315         int is_predicate = 0;
316         char *s;
317         int i, j;
318         for (i = 0; *p && !strchr("/",*p); i++, p++)
319             ;
320         res_size += (i+3); /* we'll add / between later .. */
321         stack[e] = (char *) nmem_malloc(data1_nmem_get(dh), i+1);
322         s = stack[e];
323         for (j = 0; j < i; j++)
324         {
325             const char *pp = p-i+j;
326             if (*pp == '[')
327                 is_predicate=1;
328             else if (*pp == ']')
329                 is_predicate=0;
330             else 
331             {
332                 if (!is_predicate) {
333                     if (*pp == '*') 
334                         *s++ = '.';
335                     *s++ = *pp;
336                 }
337             }
338         }
339         *s = 0;
340         e++;
341         if (*p)
342             p++;
343     }
344     res_p = res = nmem_malloc(data1_nmem_get(dh), res_size + 10);
345
346     *res_p = '\0';
347     if (stack[e-1][0] == '@')  /* path/@attr spec (leaf is attribute) */
348         strcpy(res_p, "/");
349     else
350         strcpy(res_p, "[^@]*/");  /* path .. (index all cdata below it) */
351     res_p = res_p + strlen(res_p);
352     while (--e >= 0) {
353         sprintf(res_p, "%s/", stack[e]);
354         res_p += strlen(stack[e]) + 1;
355     }
356     if (!abs)
357     {
358         sprintf(res_p, ".*"); 
359         res_p += 2;
360     }
361     sprintf (res_p, "$");
362     res_p++;
363     yaz_log(LOG_DEBUG, "Got regexp: %s", res);
364     return res;
365 }
366
367 /* *ostrich*
368
369    added arg xpelement... when called from xelm context, it's 1, saying
370    that ! means xpath, not element name as attribute name...
371
372    pop, 2002-12-13
373  */
374 static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
375                             char *cp, const char *file, int lineno,
376                             const char *element_name, data1_absyn *res,
377                             int xpelement)
378 {
379     data1_termlist **tp = *tpp;
380     while(1)
381     {
382         char attname[512], structure[512];
383         char *source;
384         int r, i;
385         int level = 0;
386         structure[0] = '\0';
387         for (i = 0; cp[i] && i<sizeof(attname)-1; i++)
388             if (strchr(":,", cp[i]))
389                 break;
390             else
391                 attname[i] = cp[i];
392         if (i == 0)
393         {
394             if (*cp)
395                 yaz_log(LOG_WARN,
396                         "%s:%d: Syntax error in termlistspec '%s'",
397                         file, lineno, cp);
398             break;
399         }
400         attname[i] = '\0';
401         r = 1;
402         cp += i;
403         if (*cp == ':')
404             cp++;
405
406         for (i = 0; cp[i] && i<sizeof(structure)-1; i++)
407             if (level == 0 && strchr(",", cp[i]))
408                 break;
409             else
410             {
411                 structure[i] = cp[i];
412                 if (cp[i] == '(')
413                     level++;
414                 else if (cp[i] == ')')
415                     level--;
416             }
417         structure[i] = '\0';
418         if (i)
419             r = 2;
420         cp += i;
421         if (*cp)
422             cp++;  /* skip , */
423
424         *tp = (data1_termlist *)
425             nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
426         (*tp)->next = 0;
427         
428         if (!xpelement) {
429             if (*attname == '!')
430                 strcpy(attname, element_name);
431         }
432         if (!((*tp)->att = data1_getattbyname(dh, res->attset,
433                                               attname))) {
434             if ((!xpelement) || (*attname != '!')) {
435                 yaz_log(LOG_WARN,
436                         "%s:%d: Couldn't find att '%s' in attset",
437                         file, lineno, attname);
438                 return -1;
439             } else {
440                 (*tp)->att = 0;
441             }
442         }
443         
444         if (r == 2 && (source = strchr(structure, ':')))
445             *source++ = '\0';   /* cut off structure .. */
446         else
447             source = "data";    /* ok: default is leaf data */
448         (*tp)->source = (char *)
449             nmem_strdup (data1_nmem_get (dh), source);
450         
451         if (r < 2) /* is the structure qualified? */
452             (*tp)->structure = "w";
453         else 
454             (*tp)->structure = (char *)
455                 nmem_strdup (data1_nmem_get (dh), structure);
456         tp = &(*tp)->next;
457     }
458
459     *tpp = tp;
460     return 0;
461 }
462
463 /* quinn
464  * Converts a 'melm' field[$subfield] pattern to a simple xpath
465  */
466 static int melm2xpath(char *melm, char *buf)
467 {
468     char *dollar;
469     char *field = melm;
470     char *subfield;
471     char *fieldtype;
472     if ((dollar = strchr(melm, '$'))) {
473         *dollar = '\0';
474         subfield = ++dollar;
475     } else
476         subfield = "";
477     if (field[0] == '0' && field[1] == '0')
478         fieldtype = "controlfield";
479     else
480         fieldtype = "datafield";
481     sprintf(buf, "/*/%s[@tag=\"%s\"]", fieldtype, field);
482     if (*subfield) 
483         sprintf(buf + strlen(buf), "/subfield[@code=\"%s\"]", subfield);
484     else if (field[0] != '0' || field[1] != '0')
485         strcat(buf, "/subfield");
486     yaz_log(LOG_DEBUG, "Created xpath: '%s'", buf);
487     return 0;
488 }
489
490 const char *data1_systag_lookup(data1_absyn *absyn, const char *tag,
491                                 const char *default_value)
492 {
493     struct data1_systag *p = absyn->systags;
494     for (; p; p = p->next)
495         if (!strcmp(p->name, tag))
496             return p->value;
497     return default_value;
498 }
499
500 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
501
502 int read_absyn_line(FILE *f, int *lineno, char *line, int len,
503                     char *argv[], int num)
504 {
505     char *p;
506     int argc;
507     int quoted = 0;
508     
509     while ((p = fgets(line, len, f)))
510     {
511         (*lineno)++;
512         while (*p && l_isspace(*p))
513             p++;
514         if (*p && *p != '#')
515             break;
516     }
517     if (!p)
518         return 0;
519     
520     for (argc = 0; *p ; argc++)
521     {
522         if (*p == '#')  /* trailing comment */
523             break;
524         argv[argc] = p;
525         while (*p && !(l_isspace(*p) && !quoted)) {
526           if (*p =='"') quoted = 1 - quoted;
527           if (*p =='[') quoted = 1;
528           if (*p ==']') quoted = 0;
529           p++;
530         }
531         if (*p)
532         {
533             *(p++) = '\0';
534             while (*p && l_isspace(*p))
535                 p++;
536         }
537     }
538     return argc;
539 }
540
541
542 data1_absyn *data1_read_absyn (data1_handle dh, const char *file,
543                                int file_must_exist)
544 {
545     data1_sub_elements *cur_elements = NULL;
546     data1_xpelement *cur_xpelement = NULL;
547
548     data1_absyn *res = 0;
549     FILE *f;
550     data1_element **ppl[D1_MAX_NESTING];
551     data1_esetname **esetpp;
552     data1_maptab **maptabp;
553     data1_marctab **marcp;
554     data1_termlist *all = 0;
555     data1_attset_child **attset_childp;
556     data1_tagset **tagset_childp;
557     struct data1_systag **systagsp;
558     int level = 0;
559     int lineno = 0;
560     int argc;
561     char *argv[50], line[512];
562
563     if (!(f = data1_path_fopen(dh, file, "r")))
564     {
565         yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
566         if (file_must_exist)
567             return 0;
568     }
569     
570     res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
571     res->name = 0;
572     res->reference = VAL_NONE;
573     res->tagset = 0;
574     res->encoding = 0;
575     res->enable_xpath_indexing = (f ? 0 : 1);
576     res->systags = 0;
577     systagsp = &res->systags;
578     tagset_childp = &res->tagset;
579
580     res->attset = data1_empty_attset (dh);
581     attset_childp =  &res->attset->children;
582
583     res->varset = 0;
584     res->esetnames = 0;
585     esetpp = &res->esetnames;
586     res->maptabs = 0;
587     maptabp = &res->maptabs;
588     res->marc = 0;
589     marcp = &res->marc;
590     res->sub_elements = NULL;
591     res->main_elements = NULL;
592     res->xp_elements = NULL;
593     
594     while (f && (argc = read_absyn_line(f, &lineno, line, 512, argv, 50)))
595     {
596         char *cmd = *argv;
597         if (!strcmp(cmd, "elm") || !strcmp(cmd, "element"))
598         {
599             data1_element *new_element;
600             int i;
601             char *p, *sub_p, *path, *name, *termlists;
602             int type, value;
603             data1_termlist **tp;
604
605             if (argc < 4)
606             {
607                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
608                 continue;
609             }
610             path = argv[1];
611             name = argv[2];
612             termlists = argv[3];
613
614             if (!cur_elements)
615             {
616                 cur_elements = (data1_sub_elements *)
617                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
618                 cur_elements->next = res->sub_elements;
619                 cur_elements->elements = NULL;
620                 cur_elements->name = "main";
621                 res->sub_elements = cur_elements;
622                 
623                 level = 0;
624                 ppl[level] = &cur_elements->elements;
625             }
626             p = path;
627             for (i = 1;; i++)
628             {
629                 char *e;
630
631                 if ((e = strchr(p, '/')))
632                     p = e+1;
633                 else
634                     break;
635             }
636             if (i > level+1)
637             {
638                 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
639                 fclose(f);
640                 return 0;
641             }
642             level = i;
643             new_element = *ppl[level-1] = (data1_element *)
644                 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
645             new_element->next = new_element->children = 0;
646             new_element->tag = 0;
647             new_element->termlists = 0;
648             new_element->sub_name = 0;
649             
650             tp = &new_element->termlists;
651             ppl[level-1] = &new_element->next;
652             ppl[level] = &new_element->children;
653             
654             /* consider subtree (if any) ... */
655             if ((sub_p = strchr (p, ':')) && sub_p[1])
656             {
657                 *sub_p++ = '\0';
658                 new_element->sub_name =
659                     nmem_strdup (data1_nmem_get(dh), sub_p);            
660             }
661             /* well-defined tag */
662             if (sscanf(p, "(%d,%d)", &type, &value) == 2)
663             {
664                 if (!res->tagset)
665                 {
666                     yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
667                     fclose(f);
668                     return 0;
669                 }
670                 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
671                                                             type, value)))
672                 {
673                     yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
674                          file, lineno, p);
675                     fclose(f);
676                     return 0;
677                 }
678             }
679             /* private tag */
680             else if (*p)
681             {
682                 data1_tag *nt =
683                     new_element->tag = (data1_tag *)
684                     nmem_malloc(data1_nmem_get (dh),
685                                 sizeof(*new_element->tag));
686                 nt->which = DATA1T_string;
687                 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
688                 nt->names = (data1_name *)
689                     nmem_malloc(data1_nmem_get(dh), 
690                                 sizeof(*new_element->tag->names));
691                 nt->names->name = nt->value.string;
692                 nt->names->next = 0;
693                 nt->kind = DATA1K_string;
694                 nt->next = 0;
695                 nt->tagset = 0;
696             }
697             else
698             {
699                 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
700                 fclose(f);
701                 return 0;
702             }
703             /* parse termList definitions */
704             p = termlists;
705             if (*p != '-')
706             {
707                 assert (res->attset);
708                 
709                 if (parse_termlists (dh, &tp, p, file, lineno, name, res, 0))
710                 {
711                     fclose (f);
712                     return 0;
713                 }
714                 *tp = all; /* append any ALL entries to the list */
715             }
716             new_element->name = nmem_strdup(data1_nmem_get (dh), name);
717         }
718         /* *ostrich*
719            New code to support xelm directive
720            for each xelm a dfa is built. xelms are stored in res->xp_elements
721            
722            maybe we should use a simple sscanf instead of dfa?
723            
724            pop, 2002-12-13
725
726            Now [] predicates are supported. regexps and xpath structure is
727            a bit redundant, however it's comfortable later...
728
729            pop, 2003-01-17
730         */
731
732         else if (!strcmp(cmd, "xelm") || !strcmp(cmd, "melm")) {
733
734             int i;
735             char *p, *xpath_expr, *termlists;
736             const char *regexp = 0;
737             struct DFA *dfa = 0;
738             data1_termlist **tp;
739             char melm_xpath[128];
740             data1_xpelement *xp_old = 0;
741             
742             if (argc < 3)
743             {
744                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to xelm", file, lineno);
745                 continue;
746             }
747             
748             if (!strcmp(cmd, "melm")) {
749                 if (melm2xpath(argv[1], melm_xpath) < 0)
750                     continue;
751                 xpath_expr = melm_xpath;
752             } else {
753                 xpath_expr = argv[1];
754             }
755             termlists = argv[2];
756             regexp = mk_xpath_regexp(dh, xpath_expr);
757 #if OPTIMIZE_MELM
758             for (xp_old = res->xp_elements; xp_old; xp_old = xp_old->next)
759                 if (!strcmp(xp_old->regexp, regexp))
760                     break;
761 #endif
762             if (!xp_old)
763             {
764                 const char *regexp_ptr = regexp;
765                 dfa = dfa_init();
766
767                 i = dfa_parse (dfa, &regexp_ptr);
768                 if (i || *regexp_ptr) {
769                     yaz_log(YLOG_WARN, "%s:%d: Bad xpath to xelm", file, lineno);
770                     dfa_delete (&dfa);
771                     continue;
772                 }
773             }           
774             if (!cur_xpelement)
775             {
776                 cur_xpelement = (data1_xpelement *)
777                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
778                 res->xp_elements = cur_xpelement;
779             } else {
780                 cur_xpelement->next = (data1_xpelement *)
781                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
782                 cur_xpelement = cur_xpelement->next;
783             }
784 #if OPTIMIZE_MELM
785             cur_xpelement->regexp = regexp;
786 #endif
787             cur_xpelement->next = NULL;
788             cur_xpelement->xpath_expr = nmem_strdup(data1_nmem_get (dh), 
789                                                     xpath_expr); 
790             if (dfa)
791                 dfa_mkstate (dfa);
792             cur_xpelement->dfa = dfa;
793 #ifdef ENHANCED_XELM 
794             cur_xpelement->xpath_len =
795                 zebra_parse_xpath_str(xpath_expr, 
796                                       cur_xpelement->xpath, XPATH_STEP_COUNT,
797                                       data1_nmem_get(dh));
798             
799             /*
800             dump_xp_steps(cur_xpelement->xpath,cur_xpelement->xpath_len);
801             */
802 #endif
803             cur_xpelement->termlists = 0;
804             tp = &cur_xpelement->termlists;
805             
806             /* parse termList definitions */
807             p = termlists;
808             if (*p != '-')
809             {
810                 assert (res->attset);
811                 
812                 if (parse_termlists (dh, &tp, p, file, lineno,
813                                      xpath_expr, res, 1))
814                 {
815                     fclose (f);
816                     return 0;
817                 }
818                 *tp = all; /* append any ALL entries to the list */
819             }
820         }
821         else if (!strcmp(cmd, "section"))
822         {
823             char *name;
824             
825             if (argc < 2)
826             {
827                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
828                         file, lineno);
829                 continue;
830             }
831             name = argv[1];
832             
833             cur_elements = (data1_sub_elements *)
834                 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
835             cur_elements->next = res->sub_elements;
836             cur_elements->elements = NULL;
837             cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
838             res->sub_elements = cur_elements;
839             
840             level = 0;
841             ppl[level] = &cur_elements->elements;
842         }
843         else if (!strcmp(cmd, "xpath"))
844         {
845             if (argc != 2)
846             {
847                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'xpath' directive",
848                      file, lineno);
849                 continue;
850             }
851             if (!strcmp(argv[1], "enable"))
852                 res->enable_xpath_indexing = 1;
853             else if (!strcmp (argv[1], "disable"))
854                 res->enable_xpath_indexing = 0;
855             else
856             {
857                 yaz_log(LOG_WARN, "%s:%d: Expecting disable/enable "
858                         "after 'xpath' directive", file, lineno);
859             }
860         }
861         else if (!strcmp(cmd, "all"))
862         {
863             data1_termlist **tp = &all;
864             if (all)
865             {
866                 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
867                      file, lineno);
868                 continue;
869             }
870             if (argc != 2)
871             {
872                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
873                      file, lineno);
874                 continue;
875             }
876             if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res, 0))
877             {
878                 fclose (f);
879                 return 0;
880             }
881         }
882         else if (!strcmp(cmd, "name"))
883         {
884             if (argc != 2)
885             {
886                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
887                      file, lineno);
888                 continue;
889             }
890             res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
891         }
892         else if (!strcmp(cmd, "reference"))
893         {
894             char *name;
895             
896             if (argc != 2)
897             {
898                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
899                      file, lineno);
900                 continue;
901             }
902             name = argv[1];
903             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
904             {
905                 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'", 
906                      file, lineno, name);
907                 continue;
908             }
909         }
910         else if (!strcmp(cmd, "attset"))
911         {
912             char *name;
913             data1_attset *attset;
914             
915             if (argc != 2)
916             {
917                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
918                      file, lineno);
919                 continue;
920             }
921             name = argv[1];
922             if (!(attset = data1_get_attset (dh, name)))
923             {
924                 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset  %s",
925                      file, lineno, name);
926                 continue;
927             }
928             *attset_childp = (data1_attset_child *)
929                 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
930             (*attset_childp)->child = attset;
931             (*attset_childp)->next = 0;
932             attset_childp = &(*attset_childp)->next;
933         }
934         else if (!strcmp(cmd, "tagset"))
935         {
936             char *name;
937             int type = 0;
938             if (argc < 2)
939             {
940                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
941                      file, lineno);
942                 continue;
943             }
944             name = argv[1];
945             if (argc == 3)
946                 type = atoi(argv[2]);
947             *tagset_childp = data1_read_tagset (dh, name, type);
948             if (!(*tagset_childp))
949             {
950                 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
951                      file, lineno, name);
952                 continue;
953             }
954             tagset_childp = &(*tagset_childp)->next;
955         }
956         else if (!strcmp(cmd, "varset"))
957         {
958             char *name;
959
960             if (argc != 2)
961             {
962                 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
963                      file, lineno);
964                 continue;
965             }
966             name = argv[1];
967             if (!(res->varset = data1_read_varset (dh, name)))
968             {
969                 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
970                      file, lineno, name);
971                 continue;
972             }
973         }
974         else if (!strcmp(cmd, "esetname"))
975         {
976             char *name, *fname;
977
978             if (argc != 3)
979             {
980                 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
981                      file, lineno);
982                 continue;
983             }
984             name = argv[1];
985             fname = argv[2];
986             
987             *esetpp = (data1_esetname *)
988                 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
989             (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
990             (*esetpp)->next = 0;
991             if (*fname == '@')
992                 (*esetpp)->spec = 0;
993             else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
994             {
995                 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
996                      file, lineno, fname);
997                 continue;
998             }
999             esetpp = &(*esetpp)->next;
1000         }
1001         else if (!strcmp(cmd, "maptab"))
1002         {
1003             char *name;
1004             
1005             if (argc != 2)
1006             {
1007                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
1008                      file, lineno);
1009                 continue;
1010             }
1011             name = argv[1];
1012             if (!(*maptabp = data1_read_maptab (dh, name)))
1013             {
1014                 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
1015                      file, lineno, name);
1016                 continue;
1017             }
1018             maptabp = &(*maptabp)->next;
1019         }
1020         else if (!strcmp(cmd, "marc"))
1021         {
1022             char *name;
1023             
1024             if (argc != 2)
1025             {
1026                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
1027                      file, lineno);
1028                 continue;
1029             }
1030             name = argv[1];
1031             if (!(*marcp = data1_read_marctab (dh, name)))
1032             {
1033                 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
1034                      file, lineno, name);
1035                 continue;
1036             }
1037             marcp = &(*marcp)->next;
1038         }
1039         else if (!strcmp(cmd, "encoding"))
1040         {
1041             if (argc != 2)
1042             {
1043                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for encoding",
1044                      file, lineno);
1045                 continue;
1046             }
1047             res->encoding = nmem_strdup (data1_nmem_get(dh), argv[1]);
1048         }
1049         else if (!strcmp(cmd, "systag"))
1050         {
1051             if (argc != 3)
1052             {
1053                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for systag",
1054                      file, lineno);
1055                 continue;
1056             }
1057             *systagsp = nmem_malloc (data1_nmem_get(dh), sizeof(**systagsp));
1058
1059             (*systagsp)->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
1060             (*systagsp)->value = nmem_strdup(data1_nmem_get(dh), argv[2]);
1061             systagsp = &(*systagsp)->next;
1062         }
1063         else
1064         {
1065             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, 
1066                     lineno, cmd);
1067             continue;
1068         }
1069     }
1070     if (f)
1071         fclose(f);
1072     
1073     for (cur_elements = res->sub_elements; cur_elements;
1074          cur_elements = cur_elements->next)
1075     {
1076         if (!strcmp (cur_elements->name, "main"))
1077             res->main_elements = cur_elements->elements;
1078         fix_element_ref (dh, res, cur_elements->elements);
1079     }
1080     *systagsp = 0;
1081     yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);
1082     return res;
1083 }