2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.29 2000-12-05 14:34:49 adam
8 * Fixed bug with termlists (introduced by previous commit).
10 * Revision 1.28 2000/12/05 12:21:45 adam
11 * Added termlist source for data1 system.
13 * Revision 1.27 1999/12/21 14:16:19 ian
14 * Changed retrieval module to allow data1 trees with no associated absyn.
15 * Also added a simple interface for extracting values from data1 trees using
16 * a string based tagpath.
18 * Revision 1.26 1999/11/30 13:47:12 adam
19 * Improved installation. Moved header files to include/yaz.
21 * Revision 1.25 1999/10/21 12:06:29 adam
22 * Retrieval module no longer uses ctype.h - functions.
24 * Revision 1.24 1999/08/27 09:40:32 adam
25 * Renamed logf function to yaz_log. Removed VC++ project files.
27 * Revision 1.23 1998/10/15 08:29:16 adam
28 * Tag set type may be specified in reference to it using "tagset"
29 * directive in .abs-files and "include" directive in .tag-files.
31 * Revision 1.22 1998/10/13 16:09:47 adam
32 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
33 * Added support for multiple attribute set references and tagset references
34 * from an abstract syntax file.
35 * Fixed many bad logs-calls in routines that read the various
36 * specifications regarding data1 (*.abs,*.att,...) and made the messages
37 * consistent whenever possible.
38 * Added extra 'lineno' argument to function readconf_line.
40 * Revision 1.21 1998/06/09 13:55:07 adam
43 * Revision 1.20 1998/05/18 13:07:02 adam
44 * Changed the way attribute sets are handled by the retriaval module.
45 * Extended Explain conversion / schema.
46 * Modified server and client to work with ASN.1 compiled protocol handlers.
48 * Revision 1.19 1998/03/05 08:15:32 adam
49 * Implemented data1_add_insert_taggeddata utility which is more flexible
50 * than data1_insert_taggeddata.
52 * Revision 1.18 1998/02/27 14:08:04 adam
53 * Added const to some char pointer arguments.
54 * Reworked data1_read_node so that it doesn't create a tree with
55 * pointers to original "SGML"-buffer.
57 * Revision 1.17 1998/02/11 11:53:34 adam
58 * Changed code so that it compiles as C++.
60 * Revision 1.16 1997/12/18 10:51:30 adam
61 * Implemented sub-trees feature for schemas - including forward
64 * Revision 1.15 1997/12/09 16:18:16 adam
65 * Work on EXPLAIN schema. First implementation of sub-schema facility
68 * Revision 1.14 1997/10/31 12:20:09 adam
69 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
70 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
71 * Bug fix: missing fclose in data1_read_espec1.
73 * Revision 1.13 1997/10/27 13:54:18 adam
74 * Changed structure field in data1 node to be simple string which
75 * is "unknown" to the retrieval system itself.
77 * Revision 1.12 1997/09/17 12:10:34 adam
80 * Revision 1.11 1997/09/05 09:50:55 adam
81 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
83 * Revision 1.10 1997/05/14 06:54:01 adam
86 * Revision 1.9 1997/02/19 14:46:15 adam
87 * The "all" specifier only affects elements that are indexed (and not
90 * Revision 1.8 1997/01/02 10:47:59 quinn
91 * Added optional, physical ANY
93 * Revision 1.7 1996/06/10 08:56:01 quinn
96 * Revision 1.6 1996/05/31 13:52:21 quinn
97 * Fixed uninitialized variable for local tags in abstract syntax.
99 * Revision 1.5 1996/05/09 07:27:43 quinn
100 * Multiple local attributes values supported.
102 * Revision 1.4 1996/05/01 12:45:28 quinn
103 * Support use of local tag names in abs file.
105 * Revision 1.3 1995/11/01 16:34:55 quinn
106 * Making data1 look for tables in data1_tabpath
108 * Revision 1.2 1995/11/01 13:54:44 quinn
111 * Revision 1.1 1995/11/01 11:56:06 quinn
112 * Added Retrieval (data management) functions en masse.
123 #include <yaz/data1.h>
125 #define D1_MAX_NESTING 128
127 struct data1_absyn_cache_info
131 data1_absyn_cache next;
134 struct data1_attset_cache_info
137 data1_attset *attset;
138 data1_attset_cache next;
141 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
143 data1_absyn_cache p = *data1_absyn_cache_get (dh);
147 if (!strcmp (name, p->name))
154 void data1_absyn_trav (data1_handle dh, void *handle,
155 void (*fh)(data1_handle dh, void *h, data1_absyn *a))
157 data1_absyn_cache p = *data1_absyn_cache_get (dh);
161 (*fh)(dh, handle, p->absyn);
166 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
169 NMEM mem = data1_nmem_get (dh);
171 data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
172 data1_absyn_cache *pp = data1_absyn_cache_get (dh);
174 sprintf(fname, "%s.abs", name);
175 p->absyn = data1_read_absyn (dh, fname);
176 p->name = nmem_strdup (mem, name);
182 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
186 if (!(absyn = data1_absyn_search (dh, name)))
187 absyn = data1_absyn_add (dh, name);
191 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
193 data1_attset_cache p = *data1_attset_cache_get (dh);
197 if (!strcmp (name, p->name))
204 data1_attset *data1_attset_search_id (data1_handle dh, int id)
206 data1_attset_cache p = *data1_attset_cache_get (dh);
210 if (id == p->attset->reference)
217 data1_attset *data1_attset_add (data1_handle dh, const char *name)
219 char fname[512], aname[512];
220 NMEM mem = data1_nmem_get (dh);
221 data1_attset *attset;
223 strcpy (aname, name);
224 sprintf(fname, "%s.att", name);
225 attset = data1_read_attset (dh, fname);
229 attset = data1_read_attset (dh, name);
230 if (attset && (cp = strrchr (aname, '.')))
234 yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
237 data1_attset_cache p = (data1_attset_cache)
238 nmem_malloc (mem, sizeof(*p));
239 data1_attset_cache *pp = data1_attset_cache_get (dh);
241 attset->name = p->name = nmem_strdup (mem, aname);
249 data1_attset *data1_get_attset (data1_handle dh, const char *name)
251 data1_attset *attset;
253 if (!(attset = data1_attset_search_name (dh, name)))
254 attset = data1_attset_add (dh, name);
258 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
263 for (r = a->esetnames; r; r = r->next)
264 if (!data1_matchstr(r->name, name))
269 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
270 data1_element *parent,
275 /* It's now possible to have a data1 tree with no abstract syntax */
280 r = abs->main_elements;
282 r = parent->children;
283 assert (abs->main_elements);
284 for (; r; r = r->next)
288 for (n = r->tag->names; n; n = n->next)
289 if (!data1_matchstr(tagname, n->name))
295 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
300 /* It's now possible to have a data1 tree with no abstract syntax */
304 assert (absyn->main_elements);
305 for (r = absyn->main_elements; r; r = r->next)
306 if (!data1_matchstr(r->name, name))
312 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
314 /* It's now possible to have a data1 tree with no abstract syntax */
318 for (; e; e = e->next)
323 fix_element_ref (dh, absyn, e->children);
327 data1_sub_elements *sub_e = absyn->sub_elements;
328 while (sub_e && strcmp (e->sub_name, sub_e->name))
331 e->children = sub_e->elements;
333 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
340 static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
341 char *p, const char *file, int lineno,
342 const char *element_name, data1_absyn *res)
344 data1_termlist **tp = *tpp;
347 char attname[512], structure[512];
351 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
355 "%s:%d: Syntax error in termlistspec '%s'",
364 strcpy(attname, element_name);
365 *tp = (data1_termlist *)
366 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
368 if (!((*tp)->att = data1_getattbyname(dh, res->attset,
372 "%s:%d: Couldn't find att '%s' in attset",
373 file, lineno, attname);
380 if (r == 2 && (source = strchr(structure, ':')))
381 *source++ = '\0'; /* cut off structure .. */
383 source = "data"; /* ok: default is leaf data */
384 (*tp)->source = (char *)
385 nmem_strdup (data1_nmem_get (dh), source);
387 if (r < 2) /* is the structure qualified? */
388 (*tp)->structure = "w";
390 (*tp)->structure = (char *)
391 nmem_strdup (data1_nmem_get (dh), structure);
394 while ((p = strchr(p, ',')) && *(++p));
399 data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
401 data1_sub_elements *cur_elements = NULL;
402 data1_absyn *res = 0;
404 data1_element **ppl[D1_MAX_NESTING];
405 data1_esetname **esetpp;
406 data1_maptab **maptabp;
407 data1_marctab **marcp;
408 data1_termlist *all = 0;
409 data1_attset_child **attset_childp;
410 data1_tagset **tagset_childp;
414 char *argv[50], line[512];
416 if (!(f = yaz_path_fopen(data1_get_tabpath (dh), file, "r")))
418 yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
422 res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
424 res->reference = VAL_NONE;
426 tagset_childp = &res->tagset;
428 res->attset = data1_empty_attset (dh);
429 attset_childp = &res->attset->children;
433 esetpp = &res->esetnames;
435 maptabp = &res->maptabs;
439 res->sub_elements = NULL;
440 res->main_elements = NULL;
442 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
445 if (!strcmp(cmd, "elm"))
447 data1_element *new_element;
449 char *p, *sub_p, *path, *name, *termlists;
455 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
464 cur_elements = (data1_sub_elements *)
465 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
466 cur_elements->next = res->sub_elements;
467 cur_elements->elements = NULL;
468 cur_elements->name = "main";
469 res->sub_elements = cur_elements;
472 ppl[level] = &cur_elements->elements;
479 if ((e = strchr(p, '/')))
486 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
491 new_element = *ppl[level] = (data1_element *)
492 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
493 new_element->next = new_element->children = 0;
494 new_element->tag = 0;
495 new_element->termlists = 0;
496 new_element->sub_name = 0;
498 tp = &new_element->termlists;
499 ppl[level] = &new_element->next;
500 ppl[level+1] = &new_element->children;
502 /* consider subtree (if any) ... */
503 if ((sub_p = strchr (p, ':')) && sub_p[1])
506 new_element->sub_name =
507 nmem_strdup (data1_nmem_get(dh), sub_p);
509 /* well-defined tag */
510 if (sscanf(p, "(%d,%d)", &type, &value) == 2)
514 yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
518 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
521 yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
531 new_element->tag = (data1_tag *)
532 nmem_malloc(data1_nmem_get (dh),
533 sizeof(*new_element->tag));
534 nt->which = DATA1T_string;
535 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
536 nt->names = (data1_name *)
537 nmem_malloc(data1_nmem_get(dh),
538 sizeof(*new_element->tag->names));
539 nt->names->name = nt->value.string;
541 nt->kind = DATA1K_string;
547 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
551 /* parse termList definitions */
555 assert (res->attset);
557 if (parse_termlists (dh, &tp, p, file, lineno, name, res))
562 *tp = all; /* append any ALL entries to the list */
564 new_element->name = nmem_strdup(data1_nmem_get (dh), name);
566 else if (!strcmp(cmd, "section"))
572 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
578 cur_elements = (data1_sub_elements *)
579 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
580 cur_elements->next = res->sub_elements;
581 cur_elements->elements = NULL;
582 cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
583 res->sub_elements = cur_elements;
586 ppl[level] = &cur_elements->elements;
588 else if (!strcmp(cmd, "all"))
590 data1_termlist **tp = &all;
593 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
599 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
603 if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res))
609 else if (!strcmp(cmd, "name"))
613 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
617 res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
619 else if (!strcmp(cmd, "reference"))
625 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
630 if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
632 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
637 else if (!strcmp(cmd, "attset"))
640 data1_attset *attset;
644 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
649 if (!(attset = data1_get_attset (dh, name)))
651 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset %s",
655 *attset_childp = (data1_attset_child *)
656 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
657 (*attset_childp)->child = attset;
658 (*attset_childp)->next = 0;
659 attset_childp = &(*attset_childp)->next;
661 else if (!strcmp(cmd, "tagset"))
667 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
673 type = atoi(argv[2]);
674 *tagset_childp = data1_read_tagset (dh, name, type);
675 if (!(*tagset_childp))
677 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
681 tagset_childp = &(*tagset_childp)->next;
683 else if (!strcmp(cmd, "varset"))
689 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
694 if (!(res->varset = data1_read_varset (dh, name)))
696 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
701 else if (!strcmp(cmd, "esetname"))
707 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
714 *esetpp = (data1_esetname *)
715 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
716 (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
720 else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
722 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
723 file, lineno, fname);
726 esetpp = &(*esetpp)->next;
728 else if (!strcmp(cmd, "maptab"))
734 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
739 if (!(*maptabp = data1_read_maptab (dh, name)))
741 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
745 maptabp = &(*maptabp)->next;
747 else if (!strcmp(cmd, "marc"))
753 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
758 if (!(*marcp = data1_read_marctab (dh, name)))
760 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
764 marcp = &(*marcp)->next;
768 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
774 for (cur_elements = res->sub_elements; cur_elements;
775 cur_elements = cur_elements->next)
777 if (!strcmp (cur_elements->name, "main"))
778 res->main_elements = cur_elements->elements;
779 fix_element_ref (dh, res, cur_elements->elements);
781 yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);