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