2 * Copyright (c) 1995-2000, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 2000-12-05 19:07:24 adam
8 * Fixed problem with element level in reading of abstract syntax.
10 * Revision 1.29 2000/12/05 14:34:49 adam
11 * Fixed bug with termlists (introduced by previous commit).
13 * Revision 1.28 2000/12/05 12:21:45 adam
14 * Added termlist source for data1 system.
16 * Revision 1.27 1999/12/21 14:16:19 ian
17 * Changed retrieval module to allow data1 trees with no associated absyn.
18 * Also added a simple interface for extracting values from data1 trees using
19 * a string based tagpath.
21 * Revision 1.26 1999/11/30 13:47:12 adam
22 * Improved installation. Moved header files to include/yaz.
24 * Revision 1.25 1999/10/21 12:06:29 adam
25 * Retrieval module no longer uses ctype.h - functions.
27 * Revision 1.24 1999/08/27 09:40:32 adam
28 * Renamed logf function to yaz_log. Removed VC++ project files.
30 * Revision 1.23 1998/10/15 08:29:16 adam
31 * Tag set type may be specified in reference to it using "tagset"
32 * directive in .abs-files and "include" directive in .tag-files.
34 * Revision 1.22 1998/10/13 16:09:47 adam
35 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
36 * Added support for multiple attribute set references and tagset references
37 * from an abstract syntax file.
38 * Fixed many bad logs-calls in routines that read the various
39 * specifications regarding data1 (*.abs,*.att,...) and made the messages
40 * consistent whenever possible.
41 * Added extra 'lineno' argument to function readconf_line.
43 * Revision 1.21 1998/06/09 13:55:07 adam
46 * Revision 1.20 1998/05/18 13:07:02 adam
47 * Changed the way attribute sets are handled by the retriaval module.
48 * Extended Explain conversion / schema.
49 * Modified server and client to work with ASN.1 compiled protocol handlers.
51 * Revision 1.19 1998/03/05 08:15:32 adam
52 * Implemented data1_add_insert_taggeddata utility which is more flexible
53 * than data1_insert_taggeddata.
55 * Revision 1.18 1998/02/27 14:08:04 adam
56 * Added const to some char pointer arguments.
57 * Reworked data1_read_node so that it doesn't create a tree with
58 * pointers to original "SGML"-buffer.
60 * Revision 1.17 1998/02/11 11:53:34 adam
61 * Changed code so that it compiles as C++.
63 * Revision 1.16 1997/12/18 10:51:30 adam
64 * Implemented sub-trees feature for schemas - including forward
67 * Revision 1.15 1997/12/09 16:18:16 adam
68 * Work on EXPLAIN schema. First implementation of sub-schema facility
71 * Revision 1.14 1997/10/31 12:20:09 adam
72 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
73 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
74 * Bug fix: missing fclose in data1_read_espec1.
76 * Revision 1.13 1997/10/27 13:54:18 adam
77 * Changed structure field in data1 node to be simple string which
78 * is "unknown" to the retrieval system itself.
80 * Revision 1.12 1997/09/17 12:10:34 adam
83 * Revision 1.11 1997/09/05 09:50:55 adam
84 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
86 * Revision 1.10 1997/05/14 06:54:01 adam
89 * Revision 1.9 1997/02/19 14:46:15 adam
90 * The "all" specifier only affects elements that are indexed (and not
93 * Revision 1.8 1997/01/02 10:47:59 quinn
94 * Added optional, physical ANY
96 * Revision 1.7 1996/06/10 08:56:01 quinn
99 * Revision 1.6 1996/05/31 13:52:21 quinn
100 * Fixed uninitialized variable for local tags in abstract syntax.
102 * Revision 1.5 1996/05/09 07:27:43 quinn
103 * Multiple local attributes values supported.
105 * Revision 1.4 1996/05/01 12:45:28 quinn
106 * Support use of local tag names in abs file.
108 * Revision 1.3 1995/11/01 16:34:55 quinn
109 * Making data1 look for tables in data1_tabpath
111 * Revision 1.2 1995/11/01 13:54:44 quinn
114 * Revision 1.1 1995/11/01 11:56:06 quinn
115 * Added Retrieval (data management) functions en masse.
126 #include <yaz/data1.h>
128 #define D1_MAX_NESTING 128
130 struct data1_absyn_cache_info
134 data1_absyn_cache next;
137 struct data1_attset_cache_info
140 data1_attset *attset;
141 data1_attset_cache next;
144 data1_absyn *data1_absyn_search (data1_handle dh, const char *name)
146 data1_absyn_cache p = *data1_absyn_cache_get (dh);
150 if (!strcmp (name, p->name))
157 void data1_absyn_trav (data1_handle dh, void *handle,
158 void (*fh)(data1_handle dh, void *h, data1_absyn *a))
160 data1_absyn_cache p = *data1_absyn_cache_get (dh);
164 (*fh)(dh, handle, p->absyn);
169 data1_absyn *data1_absyn_add (data1_handle dh, const char *name)
172 NMEM mem = data1_nmem_get (dh);
174 data1_absyn_cache p = (data1_absyn_cache)nmem_malloc (mem, sizeof(*p));
175 data1_absyn_cache *pp = data1_absyn_cache_get (dh);
177 sprintf(fname, "%s.abs", name);
178 p->absyn = data1_read_absyn (dh, fname);
179 p->name = nmem_strdup (mem, name);
185 data1_absyn *data1_get_absyn (data1_handle dh, const char *name)
189 if (!(absyn = data1_absyn_search (dh, name)))
190 absyn = data1_absyn_add (dh, name);
194 data1_attset *data1_attset_search_name (data1_handle dh, const char *name)
196 data1_attset_cache p = *data1_attset_cache_get (dh);
200 if (!strcmp (name, p->name))
207 data1_attset *data1_attset_search_id (data1_handle dh, int id)
209 data1_attset_cache p = *data1_attset_cache_get (dh);
213 if (id == p->attset->reference)
220 data1_attset *data1_attset_add (data1_handle dh, const char *name)
222 char fname[512], aname[512];
223 NMEM mem = data1_nmem_get (dh);
224 data1_attset *attset;
226 strcpy (aname, name);
227 sprintf(fname, "%s.att", name);
228 attset = data1_read_attset (dh, fname);
232 attset = data1_read_attset (dh, name);
233 if (attset && (cp = strrchr (aname, '.')))
237 yaz_log (LOG_WARN|LOG_ERRNO, "Couldn't load attribute set %s", name);
240 data1_attset_cache p = (data1_attset_cache)
241 nmem_malloc (mem, sizeof(*p));
242 data1_attset_cache *pp = data1_attset_cache_get (dh);
244 attset->name = p->name = nmem_strdup (mem, aname);
252 data1_attset *data1_get_attset (data1_handle dh, const char *name)
254 data1_attset *attset;
256 if (!(attset = data1_attset_search_name (dh, name)))
257 attset = data1_attset_add (dh, name);
261 data1_esetname *data1_getesetbyname(data1_handle dh, data1_absyn *a,
266 for (r = a->esetnames; r; r = r->next)
267 if (!data1_matchstr(r->name, name))
272 data1_element *data1_getelementbytagname (data1_handle dh, data1_absyn *abs,
273 data1_element *parent,
278 /* It's now possible to have a data1 tree with no abstract syntax */
283 r = abs->main_elements;
285 r = parent->children;
286 assert (abs->main_elements);
287 for (; r; r = r->next)
291 for (n = r->tag->names; n; n = n->next)
292 if (!data1_matchstr(tagname, n->name))
298 data1_element *data1_getelementbyname (data1_handle dh, data1_absyn *absyn,
303 /* It's now possible to have a data1 tree with no abstract syntax */
307 assert (absyn->main_elements);
308 for (r = absyn->main_elements; r; r = r->next)
309 if (!data1_matchstr(r->name, name))
315 void fix_element_ref (data1_handle dh, data1_absyn *absyn, data1_element *e)
317 /* It's now possible to have a data1 tree with no abstract syntax */
321 for (; e; e = e->next)
326 fix_element_ref (dh, absyn, e->children);
330 data1_sub_elements *sub_e = absyn->sub_elements;
331 while (sub_e && strcmp (e->sub_name, sub_e->name))
334 e->children = sub_e->elements;
336 yaz_log (LOG_WARN, "Unresolved reference to sub-elements %s",
343 static int parse_termlists (data1_handle dh, data1_termlist ***tpp,
344 char *p, const char *file, int lineno,
345 const char *element_name, data1_absyn *res)
347 data1_termlist **tp = *tpp;
350 char attname[512], structure[512];
354 if (!(r = sscanf(p, "%511[^:,]:%511[^,]", attname,
358 "%s:%d: Syntax error in termlistspec '%s'",
367 strcpy(attname, element_name);
368 *tp = (data1_termlist *)
369 nmem_malloc(data1_nmem_get(dh), sizeof(**tp));
371 if (!((*tp)->att = data1_getattbyname(dh, res->attset,
375 "%s:%d: Couldn't find att '%s' in attset",
376 file, lineno, attname);
383 if (r == 2 && (source = strchr(structure, ':')))
384 *source++ = '\0'; /* cut off structure .. */
386 source = "data"; /* ok: default is leaf data */
387 (*tp)->source = (char *)
388 nmem_strdup (data1_nmem_get (dh), source);
390 if (r < 2) /* is the structure qualified? */
391 (*tp)->structure = "w";
393 (*tp)->structure = (char *)
394 nmem_strdup (data1_nmem_get (dh), structure);
397 while ((p = strchr(p, ',')) && *(++p));
402 data1_absyn *data1_read_absyn (data1_handle dh, const char *file)
404 data1_sub_elements *cur_elements = NULL;
405 data1_absyn *res = 0;
407 data1_element **ppl[D1_MAX_NESTING];
408 data1_esetname **esetpp;
409 data1_maptab **maptabp;
410 data1_marctab **marcp;
411 data1_termlist *all = 0;
412 data1_attset_child **attset_childp;
413 data1_tagset **tagset_childp;
417 char *argv[50], line[512];
419 if (!(f = yaz_path_fopen(data1_get_tabpath (dh), file, "r")))
421 yaz_log(LOG_WARN|LOG_ERRNO, "Couldn't open %s", file);
425 res = (data1_absyn *) nmem_malloc(data1_nmem_get(dh), sizeof(*res));
427 res->reference = VAL_NONE;
429 tagset_childp = &res->tagset;
431 res->attset = data1_empty_attset (dh);
432 attset_childp = &res->attset->children;
436 esetpp = &res->esetnames;
438 maptabp = &res->maptabs;
442 res->sub_elements = NULL;
443 res->main_elements = NULL;
445 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
448 if (!strcmp(cmd, "elm"))
450 data1_element *new_element;
452 char *p, *sub_p, *path, *name, *termlists;
458 yaz_log(LOG_WARN, "%s:%d: Bad # of args to elm", file, lineno);
467 cur_elements = (data1_sub_elements *)
468 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
469 cur_elements->next = res->sub_elements;
470 cur_elements->elements = NULL;
471 cur_elements->name = "main";
472 res->sub_elements = cur_elements;
475 ppl[level] = &cur_elements->elements;
482 if ((e = strchr(p, '/')))
489 yaz_log(LOG_WARN, "%s:%d: Bad level increase", file, lineno);
494 new_element = *ppl[level-1] = (data1_element *)
495 nmem_malloc(data1_nmem_get(dh), sizeof(*new_element));
496 new_element->next = new_element->children = 0;
497 new_element->tag = 0;
498 new_element->termlists = 0;
499 new_element->sub_name = 0;
501 tp = &new_element->termlists;
502 ppl[level-1] = &new_element->next;
503 ppl[level] = &new_element->children;
505 /* consider subtree (if any) ... */
506 if ((sub_p = strchr (p, ':')) && sub_p[1])
509 new_element->sub_name =
510 nmem_strdup (data1_nmem_get(dh), sub_p);
512 /* well-defined tag */
513 if (sscanf(p, "(%d,%d)", &type, &value) == 2)
517 yaz_log(LOG_WARN, "%s:%d: No tagset loaded", file, lineno);
521 if (!(new_element->tag = data1_gettagbynum (dh, res->tagset,
524 yaz_log(LOG_WARN, "%s:%d: Couldn't find tag %s in tagset",
534 new_element->tag = (data1_tag *)
535 nmem_malloc(data1_nmem_get (dh),
536 sizeof(*new_element->tag));
537 nt->which = DATA1T_string;
538 nt->value.string = nmem_strdup(data1_nmem_get (dh), p);
539 nt->names = (data1_name *)
540 nmem_malloc(data1_nmem_get(dh),
541 sizeof(*new_element->tag->names));
542 nt->names->name = nt->value.string;
544 nt->kind = DATA1K_string;
550 yaz_log(LOG_WARN, "%s:%d: Bad element", file, lineno);
554 /* parse termList definitions */
558 assert (res->attset);
560 if (parse_termlists (dh, &tp, p, file, lineno, name, res))
565 *tp = all; /* append any ALL entries to the list */
567 new_element->name = nmem_strdup(data1_nmem_get (dh), name);
569 else if (!strcmp(cmd, "section"))
575 yaz_log(LOG_WARN, "%s:%d: Bad # of args to section",
581 cur_elements = (data1_sub_elements *)
582 nmem_malloc(data1_nmem_get(dh), sizeof(*cur_elements));
583 cur_elements->next = res->sub_elements;
584 cur_elements->elements = NULL;
585 cur_elements->name = nmem_strdup (data1_nmem_get(dh), name);
586 res->sub_elements = cur_elements;
589 ppl[level] = &cur_elements->elements;
591 else if (!strcmp(cmd, "all"))
593 data1_termlist **tp = &all;
596 yaz_log(LOG_WARN, "%s:%d: Too many 'all' directives - ignored",
602 yaz_log(LOG_WARN, "%s:%d: Bad # of args to 'all' directive",
606 if (parse_termlists (dh, &tp, argv[1], file, lineno, 0, res))
612 else if (!strcmp(cmd, "name"))
616 yaz_log(LOG_WARN, "%s:%d: Bad # of args to name directive",
620 res->name = nmem_strdup(data1_nmem_get(dh), argv[1]);
622 else if (!strcmp(cmd, "reference"))
628 yaz_log(LOG_WARN, "%s:%d: Bad # of args to reference",
633 if ((res->reference = oid_getvalbyname(name)) == VAL_NONE)
635 yaz_log(LOG_WARN, "%s:%d: Unknown tagset ref '%s'",
640 else if (!strcmp(cmd, "attset"))
643 data1_attset *attset;
647 yaz_log(LOG_WARN, "%s:%d: Bad # of args to attset",
652 if (!(attset = data1_get_attset (dh, name)))
654 yaz_log(LOG_WARN, "%s:%d: Couldn't find attset %s",
658 *attset_childp = (data1_attset_child *)
659 nmem_malloc (data1_nmem_get(dh), sizeof(**attset_childp));
660 (*attset_childp)->child = attset;
661 (*attset_childp)->next = 0;
662 attset_childp = &(*attset_childp)->next;
664 else if (!strcmp(cmd, "tagset"))
670 yaz_log(LOG_WARN, "%s:%d: Bad # of args to tagset",
676 type = atoi(argv[2]);
677 *tagset_childp = data1_read_tagset (dh, name, type);
678 if (!(*tagset_childp))
680 yaz_log(LOG_WARN, "%s:%d: Couldn't load tagset %s",
684 tagset_childp = &(*tagset_childp)->next;
686 else if (!strcmp(cmd, "varset"))
692 yaz_log(LOG_WARN, "%s:%d: Bad # of args in varset",
697 if (!(res->varset = data1_read_varset (dh, name)))
699 yaz_log(LOG_WARN, "%s:%d: Couldn't load Varset %s",
704 else if (!strcmp(cmd, "esetname"))
710 yaz_log(LOG_WARN, "%s:%d: Bad # of args in esetname",
717 *esetpp = (data1_esetname *)
718 nmem_malloc(data1_nmem_get(dh), sizeof(**esetpp));
719 (*esetpp)->name = nmem_strdup(data1_nmem_get(dh), name);
723 else if (!((*esetpp)->spec = data1_read_espec1 (dh, fname)))
725 yaz_log(LOG_WARN, "%s:%d: Espec-1 read failed for %s",
726 file, lineno, fname);
729 esetpp = &(*esetpp)->next;
731 else if (!strcmp(cmd, "maptab"))
737 yaz_log(LOG_WARN, "%s:%d: Bad # of args for maptab",
742 if (!(*maptabp = data1_read_maptab (dh, name)))
744 yaz_log(LOG_WARN, "%s:%d: Couldn't load maptab %s",
748 maptabp = &(*maptabp)->next;
750 else if (!strcmp(cmd, "marc"))
756 yaz_log(LOG_WARN, "%s:%d: Bad # or args for marc",
761 if (!(*marcp = data1_read_marctab (dh, name)))
763 yaz_log(LOG_WARN, "%s:%d: Couldn't read marctab %s",
767 marcp = &(*marcp)->next;
771 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno, cmd);
777 for (cur_elements = res->sub_elements; cur_elements;
778 cur_elements = cur_elements->next)
780 if (!strcmp (cur_elements->name, "main"))
781 res->main_elements = cur_elements->elements;
782 fix_element_ref (dh, res, cur_elements->elements);
784 yaz_log (LOG_DEBUG, "%s: data1_read_absyn end", file);