2 * Copyright (C) 1994-2001, Index Data
6 * Revision 1.44 2002-04-11 20:09:47 adam
7 * work on string tag indexing
9 * Revision 1.43 2002/03/21 23:06:36 adam
10 * Source 'tag' in abs-file
12 * Revision 1.42 2002/02/20 17:30:01 adam
13 * Work on new API. Locking system re-implemented
15 * Revision 1.41 2001/05/22 21:01:47 adam
16 * Removed print of data1 tree on stdout so that inetd works again.
18 * Revision 1.40 2001/03/29 21:31:31 adam
19 * Fixed "record begin" for Tcl filter.
21 * Revision 1.39 2000/12/05 19:09:15 adam
22 * Fixed problem where indexer could crash if abstract syntax was undefined.
24 * Revision 1.38 2000/12/05 14:44:58 adam
25 * Fixed minor bug that could cause zmbol to break it data were emitted
26 * with not parent tags.
28 * Revision 1.37 2000/12/05 12:22:53 adam
29 * Termlist source implemented (so that we can index values of XML/SGML
32 * Revision 1.36 2000/12/05 10:01:44 adam
33 * Fixed bug regarding user-defined attribute sets.
35 * Revision 1.35 2000/11/29 15:21:31 adam
36 * Fixed problem with passwd db.
38 * Revision 1.34 2000/02/25 13:24:49 adam
39 * Fixed bug regarding pointer conversion that showed up on OSF V5.
41 * Revision 1.33 1999/11/30 13:48:04 adam
42 * Improved installation. Updated for inclusion of YAZ header files.
44 * Revision 1.32 1999/09/07 07:19:21 adam
45 * Work on character mapping. Implemented replace rules.
47 * Revision 1.31 1999/07/14 10:56:43 adam
48 * Fixed potential memory leak.
50 * Revision 1.30 1999/07/06 12:26:41 adam
51 * Retrieval handler obeys schema and handles XML transfer syntax.
53 * Revision 1.29 1999/05/26 07:49:14 adam
56 * Revision 1.28 1999/05/21 12:00:17 adam
57 * Better diagnostics for extraction process.
59 * Revision 1.27 1999/05/20 12:57:18 adam
60 * Implemented TCL filter. Updated recctrl system.
62 * Revision 1.26 1999/03/02 16:15:44 quinn
63 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
65 * Revision 1.25 1999/02/18 15:01:26 adam
68 * Revision 1.24 1999/02/02 14:51:28 adam
69 * Updated WIN32 code specific sections. Changed header.
71 * Revision 1.23 1998/10/18 07:51:10 adam
72 * Changed one logf call.
74 * Revision 1.22 1998/10/16 08:14:37 adam
75 * Updated record control system.
77 * Revision 1.21 1998/07/01 09:16:10 adam
78 * Element localno only added when it's greater than 0.
80 * Revision 1.20 1998/05/20 10:12:26 adam
81 * Implemented automatic EXPLAIN database maintenance.
82 * Modified Zebra to work with ASN.1 compiled version of YAZ.
84 * Revision 1.19 1998/03/11 11:19:05 adam
85 * Changed the way sequence numbers are generated.
87 * Revision 1.18 1998/03/05 08:41:31 adam
90 * Revision 1.17 1998/02/10 12:03:06 adam
93 * Revision 1.16 1998/01/29 13:38:17 adam
94 * Fixed problem with mapping to record with unknown schema.
96 * Revision 1.15 1998/01/26 10:37:57 adam
99 * Revision 1.14 1997/11/06 11:41:01 adam
100 * Implemented "begin variant" for the sgml.regx filter.
102 * Revision 1.13 1997/10/31 12:35:44 adam
103 * Added a few log statements.
105 * Revision 1.12 1997/10/29 12:02:22 adam
106 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
108 * Revision 1.11 1997/10/27 14:34:00 adam
109 * Work on generic character mapping depending on "structure" field
110 * in abstract syntax file.
112 * Revision 1.10 1997/09/18 08:59:21 adam
113 * Extra generic handle for the character mapping routines.
115 * Revision 1.9 1997/09/17 12:19:21 adam
116 * Zebra version corresponds to YAZ version 1.4.
117 * Changed Zebra server so that it doesn't depend on global common_resource.
119 * Revision 1.8 1997/09/09 13:38:14 adam
120 * Partial port to WIN95/NT.
122 * Revision 1.7 1997/09/05 15:30:10 adam
123 * Changed prototype for chr_map_input - added const.
124 * Added support for C++, headers uses extern "C" for public definitions.
126 * Revision 1.6 1997/09/04 13:54:40 adam
127 * Added MARC filter - type grs.marc.<syntax> where syntax refers
128 * to abstract syntax. New method tellf in retrieve/extract method.
130 * Revision 1.5 1997/07/15 16:29:03 adam
131 * Initialized dummy variable to keep checker gcc happy.
133 * Revision 1.4 1997/04/30 08:56:08 quinn
136 * Revision 1.2 1996/10/11 16:06:43 quinn
137 * Revision 1.3 1997/02/24 10:41:50 adam
138 * Cleanup of code and commented out the "end element-end-record" code.
140 * Revision 1.2 1996/10/11 16:06:43 quinn
141 * Fixed arguments to nodetogr
143 * Revision 1.1 1996/10/11 10:57:25 adam
144 * New module recctrl. Used to manage records (extract/retrieval).
146 * Revision 1.29 1996/10/08 10:30:21 quinn
147 * Fixed type mismatch
149 * Revision 1.28 1996/10/07 16:06:40 quinn
152 * Revision 1.27 1996/06/11 10:54:12 quinn
155 * Revision 1.26 1996/06/06 12:08:45 quinn
156 * Added showRecord function
158 * Revision 1.25 1996/06/04 14:18:53 quinn
161 * Revision 1.24 1996/06/04 13:27:54 quinn
162 * More work on charmapping
164 * Revision 1.23 1996/06/04 10:19:01 adam
165 * Minor changes - removed include of ctype.h.
167 * Revision 1.22 1996/06/03 10:15:27 quinn
168 * Various character-mapping.
170 * Revision 1.21 1996/05/31 13:27:24 quinn
171 * Character-conversion in phrases, too.
173 * Revision 1.19 1996/05/16 15:31:14 quinn
176 * Revision 1.18 1996/05/09 07:28:56 quinn
177 * Work towards phrases and multiple registers
179 * Revision 1.17 1996/05/01 13:46:37 adam
180 * First work on multiple records in one file.
181 * New option, -offset, to the "unread" command in the filter module.
183 * Revision 1.16 1996/01/17 14:57:54 adam
184 * Prototype changed for reader functions in extract/retrieve. File
185 * is identified by 'void *' instead of 'int.
187 * Revision 1.15 1996/01/08 19:15:47 adam
188 * New input filter that works!
190 * Revision 1.14 1995/12/15 12:36:11 adam
191 * Retrieval calls data1_read_regx when subType is specified.
193 * Revision 1.13 1995/12/15 12:24:43 quinn
194 * *** empty log message ***
196 * Revision 1.12 1995/12/15 12:20:28 quinn
197 * *** empty log message ***
199 * Revision 1.11 1995/12/15 12:07:57 quinn
200 * Changed extraction strategy.
202 * Revision 1.10 1995/12/14 11:10:48 quinn
205 * Revision 1.9 1995/12/13 17:14:05 quinn
206 * *** empty log message ***
208 * Revision 1.8 1995/12/13 15:33:18 quinn
209 * *** empty log message ***
211 * Revision 1.7 1995/12/13 13:45:39 quinn
212 * Changed data1 to use nmem.
214 * Revision 1.6 1995/12/04 14:22:30 adam
215 * Extra arg to recType_byName.
216 * Started work on new regular expression parsed input to
217 * structured records.
219 * Revision 1.5 1995/11/28 14:18:37 quinn
222 * Revision 1.4 1995/11/21 13:14:49 quinn
223 * Fixed end-of-data-field problem (maybe).
225 * Revision 1.3 1995/11/15 19:13:09 adam
226 * Work on record management.
232 #include <sys/types.h>
243 #define GRS_MAX_WORD 512
249 struct grs_handler *next;
252 struct grs_handlers {
253 struct grs_handler *handlers;
256 static int read_grs_type (struct grs_handlers *h,
257 struct grs_read_info *p, const char *type,
260 struct grs_handler *gh = h->handlers;
261 const char *cp = strchr (type, '.');
263 if (cp == NULL || cp == type)
265 cp = strlen(type) + type;
269 strcpy (p->type, cp+1);
270 for (gh = h->handlers; gh; gh = gh->next)
272 if (!memcmp (type, gh->type->type, cp-type))
277 gh->clientData = (*gh->type->init)();
279 p->clientData = gh->clientData;
280 *root = (gh->type->read)(p);
281 gh->clientData = p->clientData;
288 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
290 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
291 gh->next = h->handlers;
298 static void *grs_init(RecType recType)
300 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
303 grs_add_handler (h, recTypeGrs_sgml);
304 grs_add_handler (h, recTypeGrs_regx);
306 grs_add_handler (h, recTypeGrs_tcl);
308 grs_add_handler (h, recTypeGrs_marc);
312 static void grs_destroy(void *clientData)
314 struct grs_handlers *h = (struct grs_handlers *) clientData;
315 struct grs_handler *gh = h->handlers, *gh_next;
320 (*gh->type->destroy)(gh->clientData);
327 static void index_string_tag (data1_node *n,
328 struct recExtractCtrl *p,
329 int level, RecWord *wrd,
337 wrd->string = n->u.data.data;
338 wrd->length = n->u.data.len;
339 wrd->attrSet = VAL_BIB1;
341 if (p->flagShowRecords)
343 printf("%*s data=", (level + 1) * 4, "");
344 for (i = 0; i<wrd->length && i < 8; i++)
345 fputc (wrd->string[i], stdout);
355 wrd->string = n->u.tag.tag;
356 wrd->length = strlen(n->u.tag.tag);
357 wrd->attrSet = VAL_BIB1;
359 if (p->flagShowRecords)
361 printf("%*s tag=", (level + 1) * 4, "");
362 for (i = 0; i<wrd->length && i < 8; i++)
363 fputc (wrd->string[i], stdout);
374 static void index_termlist (data1_node *par, data1_node *n,
375 struct recExtractCtrl *p, int level, RecWord *wrd)
377 data1_termlist *tlist = 0;
378 data1_datatype dtype = DATA1K_string;
380 * cycle up towards the root until we find a tag with an att..
381 * this has the effect of indexing locally defined tags with
382 * the attribute of their ancestor in the record.
385 while (!par->u.tag.element)
386 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
388 if (!par || !(tlist = par->u.tag.element->termlists))
390 if (par->u.tag.element->tag)
391 dtype = par->u.tag.element->tag->kind;
393 for (; tlist; tlist = tlist->next)
396 /* consider source */
399 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
401 wrd->string = n->u.data.data;
402 wrd->length = n->u.data.len;
404 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
406 wrd->string = n->u.tag.tag;
407 wrd->length = strlen(n->u.tag.tag);
409 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
410 n->which == DATA1N_tag)
412 data1_xattr *p = n->u.tag.attributes;
413 while (p && strcmp (p->name, xattr))
417 wrd->string = p->value;
418 wrd->length = strlen(p->value);
423 if (p->flagShowRecords)
426 printf("%*sIdx: [%s]", (level + 1) * 4, "",
428 printf("%s:%s [%d] %s",
429 tlist->att->parent->name,
430 tlist->att->name, tlist->att->value,
433 for (i = 0; i<wrd->length && i < 8; i++)
434 fputc (wrd->string[i], stdout);
438 fputc ('\n', stdout);
442 wrd->reg_type = *tlist->structure;
443 wrd->attrSet = (int) (tlist->att->parent->reference);
444 wrd->attrUse = tlist->att->locals->local;
451 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
454 (*p->init)(p, &wrd); /* set defaults */
455 for (; n; n = n->next)
457 if (p->flagShowRecords) /* display element description to user */
459 if (n->which == DATA1N_root)
461 printf("%*s", level * 4, "");
462 printf("Record type: '%s'\n", n->u.root.type);
464 else if (n->which == DATA1N_tag)
468 printf("%*s", level * 4, "");
469 if (!(e = n->u.tag.element))
470 printf("Local tag: '%s'\n", n->u.tag.tag);
473 printf("Elm: '%s' ", e->name);
476 data1_tag *t = e->tag;
478 printf("TagNam: '%s' ", t->names->name);
481 printf("%s[%d],", t->tagset->name, t->tagset->type);
484 if (t->which == DATA1T_numeric)
485 printf("%d)", t->value.numeric);
487 printf("'%s')", t->value.string);
494 if (n->which == DATA1N_tag)
496 index_termlist (n, n, p, level, &wrd);
497 /* index start tag */
498 if (!n->root->u.root.absyn)
499 index_string_tag (n, p, level, &wrd, 1);
503 if (dumpkeys(n->child, p, level + 1) < 0)
507 if (n->which == DATA1N_data)
509 data1_node *par = get_parent_tag(p->dh, n);
511 if (p->flagShowRecords)
513 printf("%*s", level * 4, "");
515 if (n->u.data.len > 32)
516 printf("'%.24s ... %.6s'\n", n->u.data.data,
517 n->u.data.data + n->u.data.len-6);
518 else if (n->u.data.len > 0)
519 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
525 index_termlist (par, n, p, level, &wrd);
526 if (!n->root->u.root.absyn)
527 index_string_tag (n, p, level, &wrd, 1016);
531 if (n->which == DATA1N_tag)
534 if (!n->root->u.root.absyn)
535 index_string_tag (n, p, level, &wrd, 2);
539 if (p->flagShowRecords && n->which == DATA1N_root)
541 printf("%*s-------------\n\n", level * 4, "");
547 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
550 int oidtmp[OID_SIZE];
552 oe.proto = PROTO_Z3950;
553 oe.oclass = CLASS_SCHEMA;
556 oe.value = n->u.root.absyn->reference;
558 if ((oid_ent_to_oid (&oe, oidtmp)))
559 (*p->schemaAdd)(p, oidtmp);
561 return dumpkeys(n, p, 0);
564 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
568 struct grs_read_info gri;
570 int oidtmp[OID_SIZE];
572 gri.readf = p->readf;
573 gri.seekf = p->seekf;
574 gri.tellf = p->tellf;
577 gri.offset = p->offset;
581 if (read_grs_type (h, &gri, p->subType, &n))
582 return RECCTRL_EXTRACT_ERROR;
584 return RECCTRL_EXTRACT_EOF;
585 oe.proto = PROTO_Z3950;
586 oe.oclass = CLASS_SCHEMA;
588 if (!n->u.root.absyn)
589 return RECCTRL_EXTRACT_ERROR;
593 oe.value = n->u.root.absyn->reference;
594 if ((oid_ent_to_oid (&oe, oidtmp)))
595 (*p->schemaAdd)(p, oidtmp);
598 data1_pr_tree (p->dh, n, stdout);
600 if (dumpkeys(n, p, 0) < 0)
602 data1_free_tree(p->dh, n);
603 return RECCTRL_EXTRACT_ERROR;
605 data1_free_tree(p->dh, n);
606 return RECCTRL_EXTRACT_OK;
609 static int grs_extract(void *clientData, struct recExtractCtrl *p)
612 NMEM mem = nmem_create ();
613 struct grs_handlers *h = (struct grs_handlers *) clientData;
615 ret = grs_extract_sub(h, p, mem);
621 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
623 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
625 data1_esetname *eset;
631 case Z_RecordComp_simple:
632 if (c->u.simple->which != Z_ElementSetNames_generic)
633 return 26; /* only generic form supported. Fix this later */
634 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
635 c->u.simple->u.generic)))
637 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
638 return 25; /* invalid esetname */
640 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
641 c->u.simple->u.generic);
644 case Z_RecordComp_complex:
645 if (c->u.complex->generic)
647 /* insert check for schema */
648 if ((p = c->u.complex->generic->elementSpec))
652 case Z_ElementSpec_elementSetName:
654 data1_getesetbyname(dh, n->u.root.absyn,
655 p->u.elementSetName)))
657 logf(LOG_LOG, "Unknown esetname '%s'",
658 p->u.elementSetName);
659 return 25; /* invalid esetname */
661 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
662 p->u.elementSetName);
665 case Z_ElementSpec_externalSpec:
666 if (p->u.externalSpec->which == Z_External_espec1)
668 logf(LOG_DEBUG, "Got Espec-1");
669 espec = p->u.externalSpec-> u.espec1;
673 logf(LOG_LOG, "Unknown external espec.");
674 return 25; /* bad. what is proper diagnostic? */
685 logf (LOG_DEBUG, "Element: Espec-1 match");
686 return data1_doespec1(dh, n, espec);
690 logf (LOG_DEBUG, "Element: all match");
695 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
697 data1_node *node = 0, *onode = 0;
700 int res, selected = 0;
702 struct grs_read_info gri;
704 struct grs_handlers *h = (struct grs_handlers *) clientData;
705 int requested_schema = VAL_NONE;
708 gri.readf = p->readf;
709 gri.seekf = p->seekf;
710 gri.tellf = p->tellf;
717 logf (LOG_DEBUG, "grs_retrieve");
718 if (read_grs_type (h, &gri, p->subType, &node))
731 data1_pr_tree (p->dh, node, stdout);
733 logf (LOG_DEBUG, "grs_retrieve: size");
734 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
737 dnew->u.data.what = DATA1I_text;
738 dnew->u.data.data = dnew->lbuf;
739 sprintf(dnew->u.data.data, "%d", p->recordSize);
740 dnew->u.data.len = strlen(dnew->u.data.data);
743 tagname = res_get_def(p->res, "tagrank", "rank");
744 if (strcmp(tagname, "0") && p->score >= 0 &&
745 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
747 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
748 dnew->u.data.what = DATA1I_num;
749 dnew->u.data.data = dnew->lbuf;
750 sprintf(dnew->u.data.data, "%d", p->score);
751 dnew->u.data.len = strlen(dnew->u.data.data);
754 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
755 if (strcmp(tagname, "0") && p->localno > 0 &&
756 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
758 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
759 dnew->u.data.what = DATA1I_text;
760 dnew->u.data.data = dnew->lbuf;
761 sprintf(dnew->u.data.data, "%d", p->localno);
762 dnew->u.data.len = strlen(dnew->u.data.data);
765 if (p->comp && p->comp->which == Z_RecordComp_complex &&
766 p->comp->u.complex->generic &&
767 p->comp->u.complex->generic->schema)
769 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
771 requested_schema = oe->value;
774 /* If schema has been specified, map if possible, then check that
775 * we got the right one
777 if (requested_schema != VAL_NONE)
779 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
780 for (map = node->u.root.absyn->maptabs; map; map = map->next)
782 if (map->target_absyn_ref == requested_schema)
785 if (!(node = data1_map_record(p->dh, onode, map, mem)))
794 if (node->u.root.absyn &&
795 requested_schema != node->u.root.absyn->reference)
803 * Does the requested format match a known syntax-mapping? (this reflects
804 * the overlap of schema and formatting which is inherent in the MARC
807 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
808 if (node->u.root.absyn)
809 for (map = node->u.root.absyn->maptabs; map; map = map->next)
811 if (map->target_absyn_ref == p->input_format)
814 if (!(node = data1_map_record(p->dh, onode, map, mem)))
823 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
824 if (node->u.root.absyn &&
825 node->u.root.absyn->reference != VAL_NONE &&
826 p->input_format == VAL_GRS1)
830 int oidtmp[OID_SIZE];
832 oe.proto = PROTO_Z3950;
833 oe.oclass = CLASS_SCHEMA;
834 oe.value = node->u.root.absyn->reference;
836 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
839 data1_handle dh = p->dh;
843 for (ii = oid; *ii >= 0; ii++)
847 sprintf(p, "%d", *ii);
852 if ((dnew = data1_insert_taggeddata(dh, node, node,
853 "schemaIdentifier", mem)))
855 dnew->u.data.what = DATA1I_oid;
856 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
857 memcpy(dnew->u.data.data, tmp, p - tmp);
858 dnew->u.data.len = p - tmp;
863 logf (LOG_DEBUG, "grs_retrieve: element spec");
864 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
868 data1_free_tree(p->dh, onode);
869 data1_free_tree(p->dh, node);
873 else if (p->comp && !res)
877 data1_pr_tree (p->dh, node, stdout);
879 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
880 switch (p->output_format = (p->input_format != VAL_NONE ?
881 p->input_format : VAL_SUTRS))
883 data1_marctab *marctab;
887 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
892 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
893 memcpy (new_buf, p->rec_buf, p->rec_len);
894 p->rec_buf = new_buf;
899 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
901 p->diagnostic = 238; /* not available in requested syntax */
903 p->rec_len = (size_t) (-1);
906 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
910 p->rec_len = (size_t) (-1);
913 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
917 p->rec_len = (size_t) (-1);
920 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
925 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
926 memcpy (new_buf, p->rec_buf, p->rec_len);
927 p->rec_buf = new_buf;
931 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
936 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
937 memcpy (new_buf, p->rec_buf, p->rec_len);
938 p->rec_buf = new_buf;
942 if (!node->u.root.absyn)
947 for (marctab = node->u.root.absyn->marc; marctab;
948 marctab = marctab->next)
949 if (marctab->reference == p->input_format)
956 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
957 selected, &p->rec_len)))
961 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
962 memcpy (new_buf, p->rec_buf, p->rec_len);
963 p->rec_buf = new_buf;
967 data1_free_tree(p->dh, node);
969 data1_free_tree(p->dh, onode);
974 static struct recType grs_type =
983 RecType recTypeGrs = &grs_type;