2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 2000-12-05 12:21:45 adam
8 * Added termlist source for data1 system.
10 * Revision 1.27 1999/12/21 14:16:19 ian
11 * Changed retrieval module to allow data1 trees with no associated absyn.
12 * Also added a simple interface for extracting values from data1 trees using
13 * a string based tagpath.
15 * Revision 1.26 1999/11/30 13:47:12 adam
16 * Improved installation. Moved header files to include/yaz.
18 * Revision 1.25 1999/10/21 12:06:29 adam
19 * Retrieval module no longer uses ctype.h - functions.
21 * Revision 1.24 1999/08/27 09:40:32 adam
22 * Renamed logf function to yaz_log. Removed VC++ project files.
24 * Revision 1.23 1998/10/15 08:29:16 adam
25 * Tag set type may be specified in reference to it using "tagset"
26 * directive in .abs-files and "include" directive in .tag-files.
28 * Revision 1.22 1998/10/13 16:09:47 adam
29 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
30 * Added support for multiple attribute set references and tagset references
31 * from an abstract syntax file.
32 * Fixed many bad logs-calls in routines that read the various
33 * specifications regarding data1 (*.abs,*.att,...) and made the messages
34 * consistent whenever possible.
35 * Added extra 'lineno' argument to function readconf_line.
37 * Revision 1.21 1998/06/09 13:55:07 adam
40 * Revision 1.20 1998/05/18 13:07:02 adam
41 * Changed the way attribute sets are handled by the retriaval module.
42 * Extended Explain conversion / schema.
43 * Modified server and client to work with ASN.1 compiled protocol handlers.
45 * Revision 1.19 1998/03/05 08:15:32 adam
46 * Implemented data1_add_insert_taggeddata utility which is more flexible
47 * than data1_insert_taggeddata.
49 * Revision 1.18 1998/02/27 14:08:04 adam
50 * Added const to some char pointer arguments.
51 * Reworked data1_read_node so that it doesn't create a tree with
52 * pointers to original "SGML"-buffer.
54 * Revision 1.17 1998/02/11 11:53:34 adam
55 * Changed code so that it compiles as C++.
57 * Revision 1.16 1997/12/18 10:51:30 adam
58 * Implemented sub-trees feature for schemas - including forward
61 * Revision 1.15 1997/12/09 16:18:16 adam
62 * Work on EXPLAIN schema. First implementation of sub-schema facility
65 * Revision 1.14 1997/10/31 12:20:09 adam
66 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
67 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
68 * Bug fix: missing fclose in data1_read_espec1.
70 * Revision 1.13 1997/10/27 13:54:18 adam
71 * Changed structure field in data1 node to be simple string which
72 * is "unknown" to the retrieval system itself.
74 * Revision 1.12 1997/09/17 12:10:34 adam
77 * Revision 1.11 1997/09/05 09:50:55 adam
78 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
80 * Revision 1.10 1997/05/14 06:54:01 adam
83 * Revision 1.9 1997/02/19 14:46:15 adam
84 * The "all" specifier only affects elements that are indexed (and not
87 * Revision 1.8 1997/01/02 10:47:59 quinn
88 * Added optional, physical ANY
90 * Revision 1.7 1996/06/10 08:56:01 quinn
93 * Revision 1.6 1996/05/31 13:52:21 quinn
94 * Fixed uninitialized variable for local tags in abstract syntax.
96 * Revision 1.5 1996/05/09 07:27:43 quinn
97 * Multiple local attributes values supported.
99 * Revision 1.4 1996/05/01 12:45:28 quinn
100 * Support use of local tag names in abs file.
102 * Revision 1.3 1995/11/01 16:34:55 quinn
103 * Making data1 look for tables in data1_tabpath
105 * Revision 1.2 1995/11/01 13:54:44 quinn
108 * Revision 1.1 1995/11/01 11:56:06 quinn
109 * Added Retrieval (data management) functions en masse.
120 #include <yaz/data1.h>
122 #define D1_MAX_NESTING 128
124 struct data1_absyn_cache_info
128 data1_absyn_cache next;
131 struct data1_attset_cache_info
134 data1_attset *attset;
135 data1_attset_cache next;
138 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
140 data1_absyn_cache p = *data1_absyn_cache_get (dh);
144 if (!strcmp (name, p->name))
151 void data1_absyn_trav (data1_handle dh, void *handle,
152 void (*fh)(data1_handle dh, void *h, data1_absyn *a))
154 data1_absyn_cache p = *data1_absyn_cache_get (dh);
158 (*fh)(dh, handle, p->absyn);
163 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
166 NMEM mem = data1_nmem_get (dh);
168 data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
169 data1_absyn_cache *pp = data1_absyn_cache_get (dh);
171 sprintf(fname, "%s.abs", name);
172 p->absyn = data1_read_absyn (dh, fname);
173 p->name = nmem_strdup (mem, name);
179 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
183 if (!(absyn = data1_absyn_search (dh, name)))
184 absyn = data1_absyn_add (dh, name);
188 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
190 data1_attset_cache p = *data1_attset_cache_get (dh);
194 if (!strcmp (name, p->name))
201 data1_attset *data1_attset_search_id (data1_handle dh, int id)
203 data1_attset_cache p = *data1_attset_cache_get (dh);
207 if (id == p->attset->reference)
214 data1_attset *data1_attset_add (data1_handle dh, const char *name)
216 char fname[512], aname[512];
217 NMEM mem = data1_nmem_get (dh);
218 data1_attset *attset;
220 strcpy (aname, name);
221 sprintf(fname, "%s.att", name);
222 attset = data1_read_attset (dh, fname);
226 attset = data1_read_attset (dh, name);
227 if (attset && (cp = strrchr (aname, '.')))
231 yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
234 data1_attset_cache p = (data1_attset_cache)
235 nmem_malloc (mem, sizeof(*p));
236 data1_attset_cache *pp = data1_attset_cache_get (dh);
238 attset->name = p->name = nmem_strdup (mem, aname);
246 data1_attset *data1_get_attset (data1_handle dh, const char *name)
248 data1_attset *attset;
250 if (!(attset = data1_attset_search_name (dh, name)))
251 attset = data1_attset_add (dh, name);
255 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
260 for (r = a->esetnames; r; r = r->next)
261 if (!data1_matchstr(r->name, name))
266 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
267 data1_element *parent,
272 /* It's now possible to have a data1 tree with no abstract syntax */
277 r = abs->main_elements;
279 r = parent->children;
280 assert (abs->main_elements);
281 for (; r; r = r->next)
285 for (n = r->tag->names; n; n = n->next)
286 if (!data1_matchstr(tagname, n->name))
292 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
297 /* It's now possible to have a data1 tree with no abstract syntax */
301 assert (absyn->main_elements);
302 for (r = absyn->main_elements; r; r = r->next)
303 if (!data1_matchstr(r->name, name))
309 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
311 /* It's now possible to have a data1 tree with no abstract syntax */
315 for (; e; e = e->next)
320 fix_element_ref (dh, absyn, e->children);
324 data1_sub_elements *sub_e = absyn->sub_elements;
325 while (sub_e && strcmp (e->sub_name, sub_e->name))
328 e->children = sub_e->elements;
330 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
336 data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
338 data1_sub_elements *cur_elements = NULL;
339 data1_absyn *res = 0;
341 data1_element **ppl[D1_MAX_NESTING];
342 data1_esetname **esetpp;
343 data1_maptab **maptabp;
344 data1_marctab **marcp;
345 data1_termlist *all = 0;
346 data1_attset_child **attset_childp;
347 data1_tagset **tagset_childp;
351 char *argv[50], line[512];
353 if (!(f = yaz_path_fopen(data1_get_tabpath (dh), file, "r")))
355 yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
359 res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
361 res->reference = VAL_NONE;
363 tagset_childp = &res->tagset;
365 res->attset = data1_empty_attset (dh);
366 attset_childp = &res->attset->children;
370 esetpp = &res->esetnames;
372 maptabp = &res->maptabs;
376 res->sub_elements = NULL;
377 res->main_elements = NULL;
379 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
382 if (!strcmp(cmd, "elm"))
384 data1_element *new_element;
386 char *p, *sub_p, *path, *name, *termlists;
392 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
401 cur_elements = (data1_sub_elements *)
402 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
403 cur_elements->next = res->sub_elements;
404 cur_elements->elements = NULL;
405 cur_elements->name = "main";
406 res->sub_elements = cur_elements;
409 ppl[level] = &cur_elements->elements;
416 if ((e = strchr(p, '/')))
423 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
428 new_element = *ppl[level] = (data1_element *)
429 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
430 new_element->next = new_element->children = 0;
431 new_element->tag = 0;
432 new_element->termlists = 0;
433 new_element->sub_name = 0;
435 tp = &new_element->termlists;
436 ppl[level] = &new_element->next;
437 ppl[level+1] = &new_element->children;
439 /* consider subtree (if any) ... */
440 if ((sub_p = strchr (p, ':')) && sub_p[1])
443 new_element->sub_name =
444 nmem_strdup (data1_nmem_get(dh), sub_p);
446 /* well-defined tag */
447 if (sscanf(p, "(%d,%d)", &type, &value) == 2)
451 yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
455 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
458 yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
468 new_element->tag = (data1_tag *)
469 nmem_malloc(data1_nmem_get (dh),
470 sizeof(*new_element->tag));
471 nt->which = DATA1T_string;
472 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
473 nt->names = (data1_name *)
474 nmem_malloc(data1_nmem_get(dh),
475 sizeof(*new_element->tag->names));
476 nt->names->name = nt->value.string;
478 nt->kind = DATA1K_string;
484 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
488 /* parse termList definitions */
491 new_element->termlists = 0;
494 assert (res->attset);
497 char attname[512], structure[512];
501 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
505 "%s:%d: Syntax error in termlistspec '%s'",
511 strcpy(attname, name);
512 *tp = (data1_termlist *)
513 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
515 if (!((*tp)->att = data1_getattbyname(dh, res->attset,
519 "%s:%d: Couldn't find att '%s' in attset",
520 file, lineno, attname);
524 if (r == 2 && (source = strchr(structure, ':')))
525 *source++ = '\0'; /* cut off structure .. */
527 source = "data"; /* ok: default is leaf data */
528 (*tp)->source = (char *)
529 nmem_strdup (data1_nmem_get (dh), source);
531 if (r < 2) /* is the structure qualified? */
532 (*tp)->structure = "w";
534 (*tp)->structure = (char *)
535 nmem_strdup (data1_nmem_get (dh), structure);
538 while ((p = strchr(p, ',')) && *(++p));
539 *tp = all; /* append any ALL entries to the list */
541 new_element->name = nmem_strdup(data1_nmem_get (dh), name);
543 else if (!strcmp(cmd, "section"))
549 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
555 cur_elements = (data1_sub_elements *)
556 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
557 cur_elements->next = res->sub_elements;
558 cur_elements->elements = NULL;
559 cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
560 res->sub_elements = cur_elements;
563 ppl[level] = &cur_elements->elements;
565 else if (!strcmp(cmd, "all"))
568 data1_termlist **tp = &all;
572 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
579 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
584 assert (res->attset);
587 char attname[512], structure[512];
590 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
593 yaz_log(LOG_WARN, "%s:%d: Syntax error in termlistspec",
598 *tp = (data1_termlist *)
599 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
601 data1_getattbyname (dh, res->attset, attname)))
603 yaz_log(LOG_WARN, "%s:%d: Couldn't find att '%s' in attset",
604 file, lineno, attname);
608 if (r < 2) /* is the structure qualified? */
609 (*tp)->structure = "w";
613 (char *)nmem_malloc (data1_nmem_get (dh),
614 strlen(structure)+1);
615 strcpy ((*tp)->structure, structure);
620 while ((p = strchr(p, ',')) && *(++p));
622 else if (!strcmp(cmd, "name"))
626 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
630 res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
632 else if (!strcmp(cmd, "reference"))
638 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
643 if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
645 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
650 else if (!strcmp(cmd, "attset"))
653 data1_attset *attset;
657 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
662 if (!(attset = data1_get_attset (dh, name)))
664 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset %s",
668 *attset_childp = (data1_attset_child *)
669 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
670 (*attset_childp)->child = attset;
671 (*attset_childp)->next = 0;
672 attset_childp = &(*attset_childp)->next;
674 else if (!strcmp(cmd, "tagset"))
680 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
686 type = atoi(argv[2]);
687 *tagset_childp = data1_read_tagset (dh, name, type);
688 if (!(*tagset_childp))
690 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
694 tagset_childp = &(*tagset_childp)->next;
696 else if (!strcmp(cmd, "varset"))
702 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
707 if (!(res->varset = data1_read_varset (dh, name)))
709 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
714 else if (!strcmp(cmd, "esetname"))
720 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
727 *esetpp = (data1_esetname *)
728 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
729 (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
733 else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
735 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
736 file, lineno, fname);
739 esetpp = &(*esetpp)->next;
741 else if (!strcmp(cmd, "maptab"))
747 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
752 if (!(*maptabp = data1_read_maptab (dh, name)))
754 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
758 maptabp = &(*maptabp)->next;
760 else if (!strcmp(cmd, "marc"))
766 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
771 if (!(*marcp = data1_read_marctab (dh, name)))
773 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
777 marcp = &(*marcp)->next;
781 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
787 for (cur_elements = res->sub_elements; cur_elements;
788 cur_elements = cur_elements->next)
790 if (!strcmp (cur_elements->name, "main"))
791 res->main_elements = cur_elements->elements;
792 fix_element_ref (dh, res, cur_elements->elements);
794 yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);