2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.27 1999-12-21 14:16:19 ian
8 * Changed retrieval module to allow data1 trees with no associated absyn.
9 * Also added a simple interface for extracting values from data1 trees using
10 * a string based tagpath.
12 * Revision 1.26 1999/11/30 13:47:12 adam
13 * Improved installation. Moved header files to include/yaz.
15 * Revision 1.25 1999/10/21 12:06:29 adam
16 * Retrieval module no longer uses ctype.h - functions.
18 * Revision 1.24 1999/08/27 09:40:32 adam
19 * Renamed logf function to yaz_log. Removed VC++ project files.
21 * Revision 1.23 1998/10/15 08:29:16 adam
22 * Tag set type may be specified in reference to it using "tagset"
23 * directive in .abs-files and "include" directive in .tag-files.
25 * Revision 1.22 1998/10/13 16:09:47 adam
26 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
27 * Added support for multiple attribute set references and tagset references
28 * from an abstract syntax file.
29 * Fixed many bad logs-calls in routines that read the various
30 * specifications regarding data1 (*.abs,*.att,...) and made the messages
31 * consistent whenever possible.
32 * Added extra 'lineno' argument to function readconf_line.
34 * Revision 1.21 1998/06/09 13:55:07 adam
37 * Revision 1.20 1998/05/18 13:07:02 adam
38 * Changed the way attribute sets are handled by the retriaval module.
39 * Extended Explain conversion / schema.
40 * Modified server and client to work with ASN.1 compiled protocol handlers.
42 * Revision 1.19 1998/03/05 08:15:32 adam
43 * Implemented data1_add_insert_taggeddata utility which is more flexible
44 * than data1_insert_taggeddata.
46 * Revision 1.18 1998/02/27 14:08:04 adam
47 * Added const to some char pointer arguments.
48 * Reworked data1_read_node so that it doesn't create a tree with
49 * pointers to original "SGML"-buffer.
51 * Revision 1.17 1998/02/11 11:53:34 adam
52 * Changed code so that it compiles as C++.
54 * Revision 1.16 1997/12/18 10:51:30 adam
55 * Implemented sub-trees feature for schemas - including forward
58 * Revision 1.15 1997/12/09 16:18:16 adam
59 * Work on EXPLAIN schema. First implementation of sub-schema facility
62 * Revision 1.14 1997/10/31 12:20:09 adam
63 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
64 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
65 * Bug fix: missing fclose in data1_read_espec1.
67 * Revision 1.13 1997/10/27 13:54:18 adam
68 * Changed structure field in data1 node to be simple string which
69 * is "unknown" to the retrieval system itself.
71 * Revision 1.12 1997/09/17 12:10:34 adam
74 * Revision 1.11 1997/09/05 09:50:55 adam
75 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
77 * Revision 1.10 1997/05/14 06:54:01 adam
80 * Revision 1.9 1997/02/19 14:46:15 adam
81 * The "all" specifier only affects elements that are indexed (and not
84 * Revision 1.8 1997/01/02 10:47:59 quinn
85 * Added optional, physical ANY
87 * Revision 1.7 1996/06/10 08:56:01 quinn
90 * Revision 1.6 1996/05/31 13:52:21 quinn
91 * Fixed uninitialized variable for local tags in abstract syntax.
93 * Revision 1.5 1996/05/09 07:27:43 quinn
94 * Multiple local attributes values supported.
96 * Revision 1.4 1996/05/01 12:45:28 quinn
97 * Support use of local tag names in abs file.
99 * Revision 1.3 1995/11/01 16:34:55 quinn
100 * Making data1 look for tables in data1_tabpath
102 * Revision 1.2 1995/11/01 13:54:44 quinn
105 * Revision 1.1 1995/11/01 11:56:06 quinn
106 * Added Retrieval (data management) functions en masse.
117 #include <yaz/data1.h>
119 #define D1_MAX_NESTING 128
121 struct data1_absyn_cache_info
125 data1_absyn_cache next;
128 struct data1_attset_cache_info
131 data1_attset *attset;
132 data1_attset_cache next;
135 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
137 data1_absyn_cache p = *data1_absyn_cache_get (dh);
141 if (!strcmp (name, p->name))
148 void data1_absyn_trav (data1_handle dh, void *handle,
149 void (*fh)(data1_handle dh, void *h, data1_absyn *a))
151 data1_absyn_cache p = *data1_absyn_cache_get (dh);
155 (*fh)(dh, handle, p->absyn);
160 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
163 NMEM mem = data1_nmem_get (dh);
165 data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
166 data1_absyn_cache *pp = data1_absyn_cache_get (dh);
168 sprintf(fname, "%s.abs", name);
169 p->absyn = data1_read_absyn (dh, fname);
170 p->name = nmem_strdup (mem, name);
176 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
180 if (!(absyn = data1_absyn_search (dh, name)))
181 absyn = data1_absyn_add (dh, name);
185 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
187 data1_attset_cache p = *data1_attset_cache_get (dh);
191 if (!strcmp (name, p->name))
198 data1_attset *data1_attset_search_id (data1_handle dh, int id)
200 data1_attset_cache p = *data1_attset_cache_get (dh);
204 if (id == p->attset->reference)
211 data1_attset *data1_attset_add (data1_handle dh, const char *name)
213 char fname[512], aname[512];
214 NMEM mem = data1_nmem_get (dh);
215 data1_attset *attset;
217 strcpy (aname, name);
218 sprintf(fname, "%s.att", name);
219 attset = data1_read_attset (dh, fname);
223 attset = data1_read_attset (dh, name);
224 if (attset && (cp = strrchr (aname, '.')))
228 yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
231 data1_attset_cache p = (data1_attset_cache)
232 nmem_malloc (mem, sizeof(*p));
233 data1_attset_cache *pp = data1_attset_cache_get (dh);
235 attset->name = p->name = nmem_strdup (mem, aname);
243 data1_attset *data1_get_attset (data1_handle dh, const char *name)
245 data1_attset *attset;
247 if (!(attset = data1_attset_search_name (dh, name)))
248 attset = data1_attset_add (dh, name);
252 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
257 for (r = a->esetnames; r; r = r->next)
258 if (!data1_matchstr(r->name, name))
263 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
264 data1_element *parent,
269 /* It's now possible to have a data1 tree with no abstract syntax */
274 r = abs->main_elements;
276 r = parent->children;
277 assert (abs->main_elements);
278 for (; r; r = r->next)
282 for (n = r->tag->names; n; n = n->next)
283 if (!data1_matchstr(tagname, n->name))
289 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
294 /* It's now possible to have a data1 tree with no abstract syntax */
298 assert (absyn->main_elements);
299 for (r = absyn->main_elements; r; r = r->next)
300 if (!data1_matchstr(r->name, name))
306 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
308 /* It's now possible to have a data1 tree with no abstract syntax */
312 for (; e; e = e->next)
317 fix_element_ref (dh, absyn, e->children);
321 data1_sub_elements *sub_e = absyn->sub_elements;
322 while (sub_e && strcmp (e->sub_name, sub_e->name))
325 e->children = sub_e->elements;
327 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
333 data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
335 data1_sub_elements *cur_elements = NULL;
336 data1_absyn *res = 0;
338 data1_element **ppl[D1_MAX_NESTING];
339 data1_esetname **esetpp;
340 data1_maptab **maptabp;
341 data1_marctab **marcp;
342 data1_termlist *all = 0;
343 data1_attset_child **attset_childp;
344 data1_tagset **tagset_childp;
348 char *argv[50], line[512];
350 if (!(f = yaz_path_fopen(data1_get_tabpath (dh), file, "r")))
352 yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
356 res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
358 res->reference = VAL_NONE;
360 tagset_childp = &res->tagset;
362 res->attset = data1_empty_attset (dh);
363 attset_childp = &res->attset->children;
367 esetpp = &res->esetnames;
369 maptabp = &res->maptabs;
373 res->sub_elements = NULL;
374 res->main_elements = NULL;
376 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
379 if (!strcmp(cmd, "elm"))
381 data1_element *new_element;
383 char *p, *sub_p, *path, *name, *termlists;
389 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
398 cur_elements = (data1_sub_elements *)
399 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
400 cur_elements->next = res->sub_elements;
401 cur_elements->elements = NULL;
402 cur_elements->name = "main";
403 res->sub_elements = cur_elements;
406 ppl[level] = &cur_elements->elements;
413 if ((e = strchr(p, '/')))
420 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
425 new_element = *ppl[level] = (data1_element *)
426 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
427 new_element->next = new_element->children = 0;
428 new_element->tag = 0;
429 new_element->termlists = 0;
430 new_element->sub_name = 0;
432 tp = &new_element->termlists;
433 ppl[level] = &new_element->next;
434 ppl[level+1] = &new_element->children;
436 /* consider subtree (if any) ... */
437 if ((sub_p = strchr (p, ':')) && sub_p[1])
440 new_element->sub_name =
441 nmem_strdup (data1_nmem_get(dh), sub_p);
443 /* well-defined tag */
444 if (sscanf(p, "(%d,%d)", &type, &value) == 2)
448 yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
452 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
455 yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
465 new_element->tag = (data1_tag *)
466 nmem_malloc(data1_nmem_get (dh),
467 sizeof(*new_element->tag));
468 nt->which = DATA1T_string;
469 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
470 nt->names = (data1_name *)
471 nmem_malloc(data1_nmem_get(dh),
472 sizeof(*new_element->tag->names));
473 nt->names->name = nt->value.string;
475 nt->kind = DATA1K_string;
481 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
485 /* parse termList definitions */
488 new_element->termlists = 0;
491 assert (res->attset);
494 char attname[512], structure[512];
497 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
501 "%s:%d: Syntax error in termlistspec '%s'",
507 strcpy(attname, name);
508 *tp = (data1_termlist *)
509 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
511 if (!((*tp)->att = data1_getattbyname(dh, res->attset,
515 "%s:%d: Couldn't find att '%s' in attset",
516 file, lineno, attname);
520 if (r < 2) /* is the structure qualified? */
521 (*tp)->structure = "w";
524 (*tp)->structure = (char *)
525 nmem_malloc (data1_nmem_get (dh),
526 strlen(structure)+1);
527 strcpy ((*tp)->structure, structure);
531 while ((p = strchr(p, ',')) && *(++p));
532 *tp = all; /* append any ALL entries to the list */
534 new_element->name = nmem_strdup(data1_nmem_get (dh), name);
536 else if (!strcmp(cmd, "section"))
542 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
548 cur_elements = (data1_sub_elements *)
549 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
550 cur_elements->next = res->sub_elements;
551 cur_elements->elements = NULL;
552 cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
553 res->sub_elements = cur_elements;
556 ppl[level] = &cur_elements->elements;
558 else if (!strcmp(cmd, "all"))
561 data1_termlist **tp = &all;
565 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
572 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
577 assert (res->attset);
580 char attname[512], structure[512];
583 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
586 yaz_log(LOG_WARN, "%s:%d: Syntax error in termlistspec",
591 *tp = (data1_termlist *)
592 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
594 data1_getattbyname (dh, res->attset, attname)))
596 yaz_log(LOG_WARN, "%s:%d: Couldn't find att '%s' in attset",
597 file, lineno, attname);
601 if (r < 2) /* is the structure qualified? */
602 (*tp)->structure = "w";
606 (char *)nmem_malloc (data1_nmem_get (dh),
607 strlen(structure)+1);
608 strcpy ((*tp)->structure, structure);
613 while ((p = strchr(p, ',')) && *(++p));
615 else if (!strcmp(cmd, "name"))
619 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
623 res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
625 else if (!strcmp(cmd, "reference"))
631 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
636 if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
638 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
643 else if (!strcmp(cmd, "attset"))
646 data1_attset *attset;
650 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
655 if (!(attset = data1_get_attset (dh, name)))
657 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset %s",
661 *attset_childp = (data1_attset_child *)
662 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
663 (*attset_childp)->child = attset;
664 (*attset_childp)->next = 0;
665 attset_childp = &(*attset_childp)->next;
667 else if (!strcmp(cmd, "tagset"))
673 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
679 type = atoi(argv[2]);
680 *tagset_childp = data1_read_tagset (dh, name, type);
681 if (!(*tagset_childp))
683 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
687 tagset_childp = &(*tagset_childp)->next;
689 else if (!strcmp(cmd, "varset"))
695 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
700 if (!(res->varset = data1_read_varset (dh, name)))
702 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
707 else if (!strcmp(cmd, "esetname"))
713 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
720 *esetpp = (data1_esetname *)
721 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
722 (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
726 else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
728 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
729 file, lineno, fname);
732 esetpp = &(*esetpp)->next;
734 else if (!strcmp(cmd, "maptab"))
740 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
745 if (!(*maptabp = data1_read_maptab (dh, name)))
747 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
751 maptabp = &(*maptabp)->next;
753 else if (!strcmp(cmd, "marc"))
759 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
764 if (!(*marcp = data1_read_marctab (dh, name)))
766 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
770 marcp = &(*marcp)->next;
774 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
780 for (cur_elements = res->sub_elements; cur_elements;
781 cur_elements = cur_elements->next)
783 if (!strcmp (cur_elements->name, "main"))
784 res->main_elements = cur_elements->elements;
785 fix_element_ref (dh, res, cur_elements->elements);
787 yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);