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