Fix grs.sgml to use off_t rather than int for file offsets
[idzebra-moved-to-github.git] / data1 / d1_absyn.c
1 /* $Id: d1_absyn.c,v 1.9.2.4 2005-07-19 18:34:16 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 Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
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 i;
301     int e = 0;
302     char *stack[32];
303     char *res_p, *res = 0;
304     size_t res_size = 1;
305     
306     if (*p != '/')
307         return ("");
308     p++;
309     if (*p == '/') 
310     { 
311         abs =0;
312         p++;
313     }
314     while (*p)
315     {
316         int is_predicate = 0;
317         char *s;
318         int j;
319         for (i = 0; *p && !strchr("/",*p); i++, p++)
320             ;
321         res_size += (i+3); /* we'll add / between later .. */
322         stack[e] = (char *) nmem_malloc(data1_nmem_get(dh), i+1);
323         s = stack[e];
324         for (j = 0; j < i; j++)
325         {
326             const char *pp = p-i+j;
327             if (*pp == '[')
328                 is_predicate=1;
329             else if (*pp == ']')
330                 is_predicate=0;
331             else 
332             {
333                 if (!is_predicate) {
334                     if (*pp == '*') 
335                         *s++ = '.';
336                     *s++ = *pp;
337                 }
338             }
339         }
340         *s = 0;
341         e++;
342         if (*p)
343             p++;
344     }
345     res_p = res = nmem_malloc(data1_nmem_get(dh), res_size + 10);
346
347     i = 0;
348     sprintf(res_p, ".*/");
349     res_p = res_p + strlen(res_p);
350     while (--e >= 0) {
351         sprintf(res_p, "%s/", stack[e]);
352         res_p += strlen(stack[e]) + 1;
353     }
354     if (!abs)
355     {
356         sprintf(res_p, ".*"); 
357         res_p += 2;
358     }
359     sprintf (res_p, "$");
360     res_p++;
361     yaz_log(YLOG_DEBUG, "Got regexp: %s", res);
362     return res;
363 }
364
365 /* *ostrich*
366
367    added arg xpelement... when called from xelm context, it's 1, saying
368    that ! means xpath, not element name as attribute name...
369
370    pop, 2002-12-13
371  */
372 static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
373                             char *cp, const char *file, int lineno,
374                             const char *element_name, data1_absyn *res,
375                             int xpelement)
376 {
377     data1_termlist **tp = *tpp;
378     while(1)
379     {
380         char attname[512], structure[512];
381         char *source;
382         int r, i;
383         int level = 0;
384         structure[0] = '\0';
385         for (i = 0; cp[i] && i<sizeof(attname)-1; i++)
386             if (strchr(":,", cp[i]))
387                 break;
388             else
389                 attname[i] = cp[i];
390         if (i == 0)
391         {
392             if (*cp)
393                 yaz_log(LOG_WARN,
394                         "%s:%d: Syntax error in termlistspec '%s'",
395                         file, lineno, cp);
396             break;
397         }
398         attname[i] = '\0';
399         r = 1;
400         cp += i;
401         if (*cp == ':')
402             cp++;
403
404         for (i = 0; cp[i] && i<sizeof(structure)-1; i++)
405             if (level == 0 && strchr(",", cp[i]))
406                 break;
407             else
408             {
409                 structure[i] = cp[i];
410                 if (cp[i] == '(')
411                     level++;
412                 else if (cp[i] == ')')
413                     level--;
414             }
415         structure[i] = '\0';
416         if (i)
417             r = 2;
418         cp += i;
419         if (*cp)
420             cp++;  /* skip , */
421
422         *tp = (data1_termlist *)
423             nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
424         (*tp)->next = 0;
425         
426         if (!xpelement) {
427             if (*attname == '!')
428                 strcpy(attname, element_name);
429         }
430         if (!((*tp)->att = data1_getattbyname(dh, res->attset,
431                                               attname))) {
432             if ((!xpelement) || (*attname != '!')) {
433                 yaz_log(LOG_WARN,
434                         "%s:%d: Couldn't find att '%s' in attset",
435                         file, lineno, attname);
436                 return -1;
437             } else {
438                 (*tp)->att = 0;
439             }
440         }
441         
442         if (r == 2 && (source = strchr(structure, ':')))
443             *source++ = '\0';   /* cut off structure .. */
444         else
445             source = "data";    /* ok: default is leaf data */
446         (*tp)->source = (char *)
447             nmem_strdup (data1_nmem_get (dh), source);
448         
449         if (r < 2) /* is the structure qualified? */
450             (*tp)->structure = "w";
451         else 
452             (*tp)->structure = (char *)
453                 nmem_strdup (data1_nmem_get (dh), structure);
454         tp = &(*tp)->next;
455     }
456
457     *tpp = tp;
458     return 0;
459 }
460
461 /* quinn
462  * Converts a 'melm' field[$subfield] pattern to a simple xpath
463  */
464 static int melm2xpath(char *melm, char *buf)
465 {
466     char *dollar;
467     char *field = melm;
468     char *subfield;
469     char *fieldtype;
470     if ((dollar = strchr(melm, '$'))) {
471         *dollar = '\0';
472         subfield = ++dollar;
473     } else
474         subfield = "";
475     if (field[0] == '0' && field[1] == '0')
476         fieldtype = "controlfield";
477     else
478         fieldtype = "datafield";
479     sprintf(buf, "/*/%s[@tag=\"%s\"]", fieldtype, field);
480     if (*subfield) 
481         sprintf(buf + strlen(buf), "/subfield[@code=\"%s\"]", subfield);
482     else if (field[0] != '0' || field[1] != '0')
483         strcat(buf, "/subfield");
484     yaz_log(YLOG_DEBUG, "Created xpath: '%s'", buf);
485     return 0;
486 }
487
488 const char *data1_systag_lookup(data1_absyn *absyn, const char *tag,
489                                 const char *default_value)
490 {
491     struct data1_systag *p = absyn->systags;
492     for (; p; p = p->next)
493         if (!strcmp(p->name, tag))
494             return p->value;
495     return default_value;
496 }
497
498 #define l_isspace(c) ((c) == '\t' || (c) == ' ' || (c) == '\n' || (c) == '\r')
499
500 int read_absyn_line(FILE *f, int *lineno, char *line, int len,
501                     char *argv[], int num)
502 {
503     char *p;
504     int argc;
505     int quoted = 0;
506     
507     while ((p = fgets(line, len, f)))
508     {
509         (*lineno)++;
510         while (*p && l_isspace(*p))
511             p++;
512         if (*p && *p != '#')
513             break;
514     }
515     if (!p)
516         return 0;
517     
518     for (argc = 0; *p ; argc++)
519     {
520         if (*p == '#')  /* trailing comment */
521             break;
522         argv[argc] = p;
523         while (*p && !(l_isspace(*p) && !quoted)) {
524           if (*p =='"') quoted = 1 - quoted;
525           if (*p =='[') quoted = 1;
526           if (*p ==']') quoted = 0;
527           p++;
528         }
529         if (*p)
530         {
531             *(p++) = '\0';
532             while (*p && l_isspace(*p))
533                 p++;
534         }
535     }
536     return argc;
537 }
538
539
540 data1_absyn *data1_read_absyn (data1_handle dh, const char *file,
541                                int file_must_exist)
542 {
543     data1_sub_elements *cur_elements = NULL;
544     data1_xpelement *cur_xpelement = NULL;
545
546     data1_absyn *res = 0;
547     FILE *f;
548     data1_element **ppl[D1_MAX_NESTING];
549     data1_esetname **esetpp;
550     data1_maptab **maptabp;
551     data1_marctab **marcp;
552     data1_termlist *all = 0;
553     data1_attset_child **attset_childp;
554     data1_tagset **tagset_childp;
555     struct data1_systag **systagsp;
556     int level = 0;
557     int lineno = 0;
558     int argc;
559     char *argv[50], line[512];
560
561     if (!(f = data1_path_fopen(dh, file, "r")))
562     {
563         yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
564         if (file_must_exist)
565             return 0;
566     }
567     
568     res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
569     res->name = 0;
570     res->reference = VAL_NONE;
571     res->tagset = 0;
572     res->encoding = 0;
573     res->enable_xpath_indexing = (f ? 0 : 1);
574     res->systags = 0;
575     systagsp = &res->systags;
576     tagset_childp = &res->tagset;
577
578     res->attset = data1_empty_attset (dh);
579     attset_childp =  &res->attset->children;
580
581     res->varset = 0;
582     res->esetnames = 0;
583     esetpp = &res->esetnames;
584     res->maptabs = 0;
585     maptabp = &res->maptabs;
586     res->marc = 0;
587     marcp = &res->marc;
588     res->sub_elements = NULL;
589     res->main_elements = NULL;
590     res->xp_elements = NULL;
591     
592     while (f && (argc = read_absyn_line(f, &lineno, line, 512, argv, 50)))
593     {
594         char *cmd = *argv;
595         if (!strcmp(cmd, "elm") || !strcmp(cmd, "element"))
596         {
597             data1_element *new_element;
598             int i;
599             char *p, *sub_p, *path, *name, *termlists;
600             int type, value;
601             data1_termlist **tp;
602
603             if (argc < 4)
604             {
605                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
606                 continue;
607             }
608             path = argv[1];
609             name = argv[2];
610             termlists = argv[3];
611
612             if (!cur_elements)
613             {
614                 cur_elements = (data1_sub_elements *)
615                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
616                 cur_elements->next = res->sub_elements;
617                 cur_elements->elements = NULL;
618                 cur_elements->name = "main";
619                 res->sub_elements = cur_elements;
620                 
621                 level = 0;
622                 ppl[level] = &cur_elements->elements;
623             }
624             p = path;
625             for (i = 1;; i++)
626             {
627                 char *e;
628
629                 if ((e = strchr(p, '/')))
630                     p = e+1;
631                 else
632                     break;
633             }
634             if (i > level+1)
635             {
636                 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
637                 fclose(f);
638                 return 0;
639             }
640             level = i;
641             new_element = *ppl[level-1] = (data1_element *)
642                 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
643             new_element->next = new_element->children = 0;
644             new_element->tag = 0;
645             new_element->termlists = 0;
646             new_element->sub_name = 0;
647             
648             tp = &new_element->termlists;
649             ppl[level-1] = &new_element->next;
650             ppl[level] = &new_element->children;
651             
652             /* consider subtree (if any) ... */
653             if ((sub_p = strchr (p, ':')) && sub_p[1])
654             {
655                 *sub_p++ = '\0';
656                 new_element->sub_name =
657                     nmem_strdup (data1_nmem_get(dh), sub_p);            
658             }
659             /* well-defined tag */
660             if (sscanf(p, "(%d,%d)", &type, &value) == 2)
661             {
662                 if (!res->tagset)
663                 {
664                     yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
665                     fclose(f);
666                     return 0;
667                 }
668                 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
669                                                             type, value)))
670                 {
671                     yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
672                          file, lineno, p);
673                     fclose(f);
674                     return 0;
675                 }
676             }
677             /* private tag */
678             else if (*p)
679             {
680                 data1_tag *nt =
681                     new_element->tag = (data1_tag *)
682                     nmem_malloc(data1_nmem_get (dh),
683                                 sizeof(*new_element->tag));
684                 nt->which = DATA1T_string;
685                 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
686                 nt->names = (data1_name *)
687                     nmem_malloc(data1_nmem_get(dh), 
688                                 sizeof(*new_element->tag->names));
689                 nt->names->name = nt->value.string;
690                 nt->names->next = 0;
691                 nt->kind = DATA1K_string;
692                 nt->next = 0;
693                 nt->tagset = 0;
694             }
695             else
696             {
697                 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
698                 fclose(f);
699                 return 0;
700             }
701             /* parse termList definitions */
702             p = termlists;
703             if (*p != '-')
704             {
705                 assert (res->attset);
706                 
707                 if (parse_termlists (dh, &tp, p, file, lineno, name, res, 0))
708                 {
709                     fclose (f);
710                     return 0;
711                 }
712                 *tp = all; /* append any ALL entries to the list */
713             }
714             new_element->name = nmem_strdup(data1_nmem_get (dh), name);
715         }
716         /* *ostrich*
717            New code to support xelm directive
718            for each xelm a dfa is built. xelms are stored in res->xp_elements
719            
720            maybe we should use a simple sscanf instead of dfa?
721            
722            pop, 2002-12-13
723
724            Now [] predicates are supported. regexps and xpath structure is
725            a bit redundant, however it's comfortable later...
726
727            pop, 2003-01-17
728         */
729
730         else if (!strcmp(cmd, "xelm") || !strcmp(cmd, "melm")) {
731
732             int i;
733             char *p, *xpath_expr, *termlists;
734             const char *regexp;
735             struct DFA *dfa = dfa = dfa_init();
736             data1_termlist **tp;
737             char melm_xpath[128];
738             
739             if (argc < 3)
740             {
741                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to xelm", file, lineno);
742                 continue;
743             }
744             
745             if (!strcmp(cmd, "melm")) {
746                 if (melm2xpath(argv[1], melm_xpath) < 0)
747                     continue;
748                 xpath_expr = melm_xpath;
749             } else {
750                 xpath_expr = argv[1];
751             }
752             termlists = argv[2];
753             regexp = mk_xpath_regexp(dh, xpath_expr);
754             i = dfa_parse (dfa, &regexp);
755             if (i || *regexp) {
756                 yaz_log(LOG_WARN, "%s:%d: Bad xpath to xelm", file, lineno);
757                 dfa_delete (&dfa);
758                 continue;
759             }
760             
761             if (!cur_xpelement)
762             {
763                 cur_xpelement = (data1_xpelement *)
764                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
765                 res->xp_elements = cur_xpelement;
766             } else {
767                 cur_xpelement->next = (data1_xpelement *)
768                     nmem_malloc(data1_nmem_get(dh), sizeof(*cur_xpelement));
769                 cur_xpelement = cur_xpelement->next;
770             }
771             cur_xpelement->next = NULL;
772             cur_xpelement->xpath_expr = nmem_strdup(data1_nmem_get (dh), 
773                                                     xpath_expr); 
774             
775             dfa_mkstate (dfa);
776             cur_xpelement->dfa = dfa;
777
778 #ifdef ENHANCED_XELM 
779             cur_xpelement->xpath_len =
780                 zebra_parse_xpath_str(xpath_expr, 
781                                       cur_xpelement->xpath, XPATH_STEP_COUNT,
782                                       data1_nmem_get(dh));
783             
784             /*
785             dump_xp_steps(cur_xpelement->xpath,cur_xpelement->xpath_len);
786             */
787 #endif
788             cur_xpelement->termlists = 0;
789             tp = &cur_xpelement->termlists;
790             
791             /* parse termList definitions */
792             p = termlists;
793             if (*p != '-')
794             {
795                 assert (res->attset);
796                 
797                 if (parse_termlists (dh, &tp, p, file, lineno,
798                                      xpath_expr, res, 1))
799                 {
800                     fclose (f);
801                     return 0;
802                 }
803                 *tp = all; /* append any ALL entries to the list */
804             }
805         }
806         else if (!strcmp(cmd, "section"))
807         {
808             char *name;
809             
810             if (argc < 2)
811             {
812                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
813                         file, lineno);
814                 continue;
815             }
816             name = argv[1];
817             
818             cur_elements = (data1_sub_elements *)
819                 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
820             cur_elements->next = res->sub_elements;
821             cur_elements->elements = NULL;
822             cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
823             res->sub_elements = cur_elements;
824             
825             level = 0;
826             ppl[level] = &cur_elements->elements;
827         }
828         else if (!strcmp(cmd, "xpath"))
829         {
830             if (argc != 2)
831             {
832                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'xpath' directive",
833                      file, lineno);
834                 continue;
835             }
836             if (!strcmp(argv[1], "enable"))
837                 res->enable_xpath_indexing = 1;
838             else if (!strcmp (argv[1], "disable"))
839                 res->enable_xpath_indexing = 0;
840             else
841             {
842                 yaz_log(LOG_WARN, "%s:%d: Expecting disable/enable "
843                         "after 'xpath' directive", file, lineno);
844             }
845         }
846         else if (!strcmp(cmd, "all"))
847         {
848             data1_termlist **tp = &all;
849             if (all)
850             {
851                 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
852                      file, lineno);
853                 continue;
854             }
855             if (argc != 2)
856             {
857                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
858                      file, lineno);
859                 continue;
860             }
861             if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res, 0))
862             {
863                 fclose (f);
864                 return 0;
865             }
866         }
867         else if (!strcmp(cmd, "name"))
868         {
869             if (argc != 2)
870             {
871                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
872                      file, lineno);
873                 continue;
874             }
875             res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
876         }
877         else if (!strcmp(cmd, "reference"))
878         {
879             char *name;
880             
881             if (argc != 2)
882             {
883                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
884                      file, lineno);
885                 continue;
886             }
887             name = argv[1];
888             if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
889             {
890                 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'", 
891                      file, lineno, name);
892                 continue;
893             }
894         }
895         else if (!strcmp(cmd, "attset"))
896         {
897             char *name;
898             data1_attset *attset;
899             
900             if (argc != 2)
901             {
902                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
903                      file, lineno);
904                 continue;
905             }
906             name = argv[1];
907             if (!(attset = data1_get_attset (dh, name)))
908             {
909                 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset  %s",
910                      file, lineno, name);
911                 continue;
912             }
913             *attset_childp = (data1_attset_child *)
914                 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
915             (*attset_childp)->child = attset;
916             (*attset_childp)->next = 0;
917             attset_childp = &(*attset_childp)->next;
918         }
919         else if (!strcmp(cmd, "tagset"))
920         {
921             char *name;
922             int type = 0;
923             if (argc < 2)
924             {
925                 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
926                      file, lineno);
927                 continue;
928             }
929             name = argv[1];
930             if (argc == 3)
931                 type = atoi(argv[2]);
932             *tagset_childp = data1_read_tagset (dh, name, type);
933             if (!(*tagset_childp))
934             {
935                 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
936                      file, lineno, name);
937                 continue;
938             }
939             tagset_childp = &(*tagset_childp)->next;
940         }
941         else if (!strcmp(cmd, "varset"))
942         {
943             char *name;
944
945             if (argc != 2)
946             {
947                 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
948                      file, lineno);
949                 continue;
950             }
951             name = argv[1];
952             if (!(res->varset = data1_read_varset (dh, name)))
953             {
954                 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
955                      file, lineno, name);
956                 continue;
957             }
958         }
959         else if (!strcmp(cmd, "esetname"))
960         {
961             char *name, *fname;
962
963             if (argc != 3)
964             {
965                 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
966                      file, lineno);
967                 continue;
968             }
969             name = argv[1];
970             fname = argv[2];
971             
972             *esetpp = (data1_esetname *)
973                 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
974             (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
975             (*esetpp)->next = 0;
976             if (*fname == '@')
977                 (*esetpp)->spec = 0;
978             else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
979             {
980                 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
981                      file, lineno, fname);
982                 continue;
983             }
984             esetpp = &(*esetpp)->next;
985         }
986         else if (!strcmp(cmd, "maptab"))
987         {
988             char *name;
989             
990             if (argc != 2)
991             {
992                 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
993                      file, lineno);
994                 continue;
995             }
996             name = argv[1];
997             if (!(*maptabp = data1_read_maptab (dh, name)))
998             {
999                 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
1000                      file, lineno, name);
1001                 continue;
1002             }
1003             maptabp = &(*maptabp)->next;
1004         }
1005         else if (!strcmp(cmd, "marc"))
1006         {
1007             char *name;
1008             
1009             if (argc != 2)
1010             {
1011                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
1012                      file, lineno);
1013                 continue;
1014             }
1015             name = argv[1];
1016             if (!(*marcp = data1_read_marctab (dh, name)))
1017             {
1018                 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
1019                      file, lineno, name);
1020                 continue;
1021             }
1022             marcp = &(*marcp)->next;
1023         }
1024         else if (!strcmp(cmd, "encoding"))
1025         {
1026             if (argc != 2)
1027             {
1028                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for encoding",
1029                      file, lineno);
1030                 continue;
1031             }
1032             res->encoding = nmem_strdup (data1_nmem_get(dh), argv[1]);
1033         }
1034         else if (!strcmp(cmd, "systag"))
1035         {
1036             if (argc != 3)
1037             {
1038                 yaz_log(LOG_WARN, "%s:%d: Bad # or args for systag",
1039                      file, lineno);
1040                 continue;
1041             }
1042             *systagsp = nmem_malloc (data1_nmem_get(dh), sizeof(**systagsp));
1043
1044             (*systagsp)->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
1045             (*systagsp)->value = nmem_strdup(data1_nmem_get(dh), argv[2]);
1046             systagsp = &(*systagsp)->next;
1047         }
1048         else
1049         {
1050             yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, 
1051                     lineno, cmd);
1052             continue;
1053         }
1054     }
1055     if (f)
1056         fclose(f);
1057     
1058     for (cur_elements = res->sub_elements; cur_elements;
1059          cur_elements = cur_elements->next)
1060     {
1061         if (!strcmp (cur_elements->name, "main"))
1062             res->main_elements = cur_elements->elements;
1063         fix_element_ref (dh, res, cur_elements->elements);
1064     }
1065     *systagsp = 0;
1066     yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);
1067     return res;
1068 }