2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.37 2000-12-05 12:22:53 adam
8 * Termlist source implemented (so that we can index values of XML/SGML
11 * Revision 1.36 2000/12/05 10:01:44 adam
12 * Fixed bug regarding user-defined attribute sets.
14 * Revision 1.35 2000/11/29 15:21:31 adam
15 * Fixed problem with passwd db.
17 * Revision 1.34 2000/02/25 13:24:49 adam
18 * Fixed bug regarding pointer conversion that showed up on OSF V5.
20 * Revision 1.33 1999/11/30 13:48:04 adam
21 * Improved installation. Updated for inclusion of YAZ header files.
23 * Revision 1.32 1999/09/07 07:19:21 adam
24 * Work on character mapping. Implemented replace rules.
26 * Revision 1.31 1999/07/14 10:56:43 adam
27 * Fixed potential memory leak.
29 * Revision 1.30 1999/07/06 12:26:41 adam
30 * Retrieval handler obeys schema and handles XML transfer syntax.
32 * Revision 1.29 1999/05/26 07:49:14 adam
35 * Revision 1.28 1999/05/21 12:00:17 adam
36 * Better diagnostics for extraction process.
38 * Revision 1.27 1999/05/20 12:57:18 adam
39 * Implemented TCL filter. Updated recctrl system.
41 * Revision 1.26 1999/03/02 16:15:44 quinn
42 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
44 * Revision 1.25 1999/02/18 15:01:26 adam
47 * Revision 1.24 1999/02/02 14:51:28 adam
48 * Updated WIN32 code specific sections. Changed header.
50 * Revision 1.23 1998/10/18 07:51:10 adam
51 * Changed one logf call.
53 * Revision 1.22 1998/10/16 08:14:37 adam
54 * Updated record control system.
56 * Revision 1.21 1998/07/01 09:16:10 adam
57 * Element localno only added when it's greater than 0.
59 * Revision 1.20 1998/05/20 10:12:26 adam
60 * Implemented automatic EXPLAIN database maintenance.
61 * Modified Zebra to work with ASN.1 compiled version of YAZ.
63 * Revision 1.19 1998/03/11 11:19:05 adam
64 * Changed the way sequence numbers are generated.
66 * Revision 1.18 1998/03/05 08:41:31 adam
69 * Revision 1.17 1998/02/10 12:03:06 adam
72 * Revision 1.16 1998/01/29 13:38:17 adam
73 * Fixed problem with mapping to record with unknown schema.
75 * Revision 1.15 1998/01/26 10:37:57 adam
78 * Revision 1.14 1997/11/06 11:41:01 adam
79 * Implemented "begin variant" for the sgml.regx filter.
81 * Revision 1.13 1997/10/31 12:35:44 adam
82 * Added a few log statements.
84 * Revision 1.12 1997/10/29 12:02:22 adam
85 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
87 * Revision 1.11 1997/10/27 14:34:00 adam
88 * Work on generic character mapping depending on "structure" field
89 * in abstract syntax file.
91 * Revision 1.10 1997/09/18 08:59:21 adam
92 * Extra generic handle for the character mapping routines.
94 * Revision 1.9 1997/09/17 12:19:21 adam
95 * Zebra version corresponds to YAZ version 1.4.
96 * Changed Zebra server so that it doesn't depend on global common_resource.
98 * Revision 1.8 1997/09/09 13:38:14 adam
99 * Partial port to WIN95/NT.
101 * Revision 1.7 1997/09/05 15:30:10 adam
102 * Changed prototype for chr_map_input - added const.
103 * Added support for C++, headers uses extern "C" for public definitions.
105 * Revision 1.6 1997/09/04 13:54:40 adam
106 * Added MARC filter - type grs.marc.<syntax> where syntax refers
107 * to abstract syntax. New method tellf in retrieve/extract method.
109 * Revision 1.5 1997/07/15 16:29:03 adam
110 * Initialized dummy variable to keep checker gcc happy.
112 * Revision 1.4 1997/04/30 08:56:08 quinn
115 * Revision 1.2 1996/10/11 16:06:43 quinn
116 * Revision 1.3 1997/02/24 10:41:50 adam
117 * Cleanup of code and commented out the "end element-end-record" code.
119 * Revision 1.2 1996/10/11 16:06:43 quinn
120 * Fixed arguments to nodetogr
122 * Revision 1.1 1996/10/11 10:57:25 adam
123 * New module recctrl. Used to manage records (extract/retrieval).
125 * Revision 1.29 1996/10/08 10:30:21 quinn
126 * Fixed type mismatch
128 * Revision 1.28 1996/10/07 16:06:40 quinn
131 * Revision 1.27 1996/06/11 10:54:12 quinn
134 * Revision 1.26 1996/06/06 12:08:45 quinn
135 * Added showRecord function
137 * Revision 1.25 1996/06/04 14:18:53 quinn
140 * Revision 1.24 1996/06/04 13:27:54 quinn
141 * More work on charmapping
143 * Revision 1.23 1996/06/04 10:19:01 adam
144 * Minor changes - removed include of ctype.h.
146 * Revision 1.22 1996/06/03 10:15:27 quinn
147 * Various character-mapping.
149 * Revision 1.21 1996/05/31 13:27:24 quinn
150 * Character-conversion in phrases, too.
152 * Revision 1.19 1996/05/16 15:31:14 quinn
155 * Revision 1.18 1996/05/09 07:28:56 quinn
156 * Work towards phrases and multiple registers
158 * Revision 1.17 1996/05/01 13:46:37 adam
159 * First work on multiple records in one file.
160 * New option, -offset, to the "unread" command in the filter module.
162 * Revision 1.16 1996/01/17 14:57:54 adam
163 * Prototype changed for reader functions in extract/retrieve. File
164 * is identified by 'void *' instead of 'int.
166 * Revision 1.15 1996/01/08 19:15:47 adam
167 * New input filter that works!
169 * Revision 1.14 1995/12/15 12:36:11 adam
170 * Retrieval calls data1_read_regx when subType is specified.
172 * Revision 1.13 1995/12/15 12:24:43 quinn
173 * *** empty log message ***
175 * Revision 1.12 1995/12/15 12:20:28 quinn
176 * *** empty log message ***
178 * Revision 1.11 1995/12/15 12:07:57 quinn
179 * Changed extraction strategy.
181 * Revision 1.10 1995/12/14 11:10:48 quinn
184 * Revision 1.9 1995/12/13 17:14:05 quinn
185 * *** empty log message ***
187 * Revision 1.8 1995/12/13 15:33:18 quinn
188 * *** empty log message ***
190 * Revision 1.7 1995/12/13 13:45:39 quinn
191 * Changed data1 to use nmem.
193 * Revision 1.6 1995/12/04 14:22:30 adam
194 * Extra arg to recType_byName.
195 * Started work on new regular expression parsed input to
196 * structured records.
198 * Revision 1.5 1995/11/28 14:18:37 quinn
201 * Revision 1.4 1995/11/21 13:14:49 quinn
202 * Fixed end-of-data-field problem (maybe).
204 * Revision 1.3 1995/11/15 19:13:09 adam
205 * Work on record management.
211 #include <sys/types.h>
222 #define GRS_MAX_WORD 512
228 struct grs_handler *next;
231 struct grs_handlers {
232 struct grs_handler *handlers;
235 static int read_grs_type (struct grs_handlers *h,
236 struct grs_read_info *p, const char *type,
239 struct grs_handler *gh = h->handlers;
240 const char *cp = strchr (type, '.');
242 if (cp == NULL || cp == type)
244 cp = strlen(type) + type;
248 strcpy (p->type, cp+1);
249 for (gh = h->handlers; gh; gh = gh->next)
251 if (!memcmp (type, gh->type->type, cp-type))
256 gh->clientData = (*gh->type->init)();
258 p->clientData = gh->clientData;
259 *root = (gh->type->read)(p);
260 gh->clientData = p->clientData;
267 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
269 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
270 gh->next = h->handlers;
277 static void *grs_init(RecType recType)
279 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
282 grs_add_handler (h, recTypeGrs_sgml);
283 grs_add_handler (h, recTypeGrs_regx);
285 grs_add_handler (h, recTypeGrs_tcl);
287 grs_add_handler (h, recTypeGrs_marc);
291 static void grs_destroy(void *clientData)
293 struct grs_handlers *h = (struct grs_handlers *) clientData;
294 struct grs_handler *gh = h->handlers, *gh_next;
299 (*gh->type->destroy)(gh->clientData);
306 static void index_tag (data1_node *par, data1_node *n,
307 struct recExtractCtrl *p, int level, RecWord *wrd)
309 data1_termlist *tlist = 0;
310 data1_datatype dtype = DATA1K_string;
312 * cycle up towards the root until we find a tag with an att..
313 * this has the effect of indexing locally defined tags with
314 * the attribute of their ancestor in the record.
317 while (!par->u.tag.element)
318 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
320 if (!par || !(tlist = par->u.tag.element->termlists))
322 if (par->u.tag.element->tag)
323 dtype = par->u.tag.element->tag->kind;
325 for (; tlist; tlist = tlist->next)
328 /* consider source */
331 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
333 wrd->string = n->u.data.data;
334 wrd->length = n->u.data.len;
336 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
337 n->which == DATA1N_tag)
339 data1_xattr *p = n->u.tag.attributes;
340 while (p && strcmp (p->name, xattr))
344 wrd->string = p->value;
345 wrd->length = strlen(p->value);
350 if (p->flagShowRecords)
353 printf("%*sIdx: [%s]", (level + 1) * 4, "",
355 printf("%s:%s [%d] %s",
356 tlist->att->parent->name,
357 tlist->att->name, tlist->att->value,
360 for (i = 0; i<wrd->length && i < 8; i++)
361 fputc (wrd->string[i], stdout);
365 fputc ('\n', stdout);
369 wrd->reg_type = *tlist->structure;
370 wrd->attrSet = (int) (tlist->att->parent->reference);
371 wrd->attrUse = tlist->att->locals->local;
378 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
381 (*p->init)(p, &wrd); /* set defaults */
382 for (; n; n = n->next)
384 if (p->flagShowRecords) /* display element description to user */
386 if (n->which == DATA1N_root)
388 printf("%*s", level * 4, "");
389 printf("Record type: '%s'\n", n->u.root.absyn->name);
391 else if (n->which == DATA1N_tag)
395 printf("%*s", level * 4, "");
396 if (!(e = n->u.tag.element))
397 printf("Local tag: '%s'\n", n->u.tag.tag);
400 printf("Elm: '%s' ", e->name);
403 data1_tag *t = e->tag;
405 printf("TagNam: '%s' ", t->names->name);
408 printf("%s[%d],", t->tagset->name, t->tagset->type);
411 if (t->which == DATA1T_numeric)
412 printf("%d)", t->value.numeric);
414 printf("'%s')", t->value.string);
422 if (dumpkeys(n->child, p, level + 1) < 0)
425 if (n->which == DATA1N_tag)
427 index_tag (n, n, p, level, &wrd);
430 if (n->which == DATA1N_data)
432 data1_node *par = get_parent_tag(p->dh, n);
434 if (p->flagShowRecords)
436 printf("%*s", level * 4, "");
438 if (n->u.data.len > 32)
439 printf("'%.24s ... %.6s'\n", n->u.data.data,
440 n->u.data.data + n->u.data.len-6);
441 else if (n->u.data.len > 0)
442 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
449 index_tag (par, n, p, level, &wrd);
451 if (p->flagShowRecords && n->which == DATA1N_root)
453 printf("%*s-------------\n\n", level * 4, "");
459 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
462 int oidtmp[OID_SIZE];
464 oe.proto = PROTO_Z3950;
465 oe.oclass = CLASS_SCHEMA;
466 oe.value = n->u.root.absyn->reference;
468 if ((oid_ent_to_oid (&oe, oidtmp)))
469 (*p->schemaAdd)(p, oidtmp);
471 return dumpkeys(n, p, 0);
474 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
478 struct grs_read_info gri;
480 int oidtmp[OID_SIZE];
482 gri.readf = p->readf;
483 gri.seekf = p->seekf;
484 gri.tellf = p->tellf;
487 gri.offset = p->offset;
491 if (read_grs_type (h, &gri, p->subType, &n))
492 return RECCTRL_EXTRACT_ERROR;
494 return RECCTRL_EXTRACT_EOF;
495 oe.proto = PROTO_Z3950;
496 oe.oclass = CLASS_SCHEMA;
497 oe.value = n->u.root.absyn->reference;
498 if ((oid_ent_to_oid (&oe, oidtmp)))
499 (*p->schemaAdd)(p, oidtmp);
501 if (dumpkeys(n, p, 0) < 0)
503 data1_free_tree(p->dh, n);
504 return RECCTRL_EXTRACT_ERROR;
506 data1_free_tree(p->dh, n);
507 return RECCTRL_EXTRACT_OK;
510 static int grs_extract(void *clientData, struct recExtractCtrl *p)
513 NMEM mem = nmem_create ();
514 struct grs_handlers *h = (struct grs_handlers *) clientData;
516 ret = grs_extract_sub(h, p, mem);
522 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
524 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
526 data1_esetname *eset;
532 case Z_RecordComp_simple:
533 if (c->u.simple->which != Z_ElementSetNames_generic)
534 return 26; /* only generic form supported. Fix this later */
535 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
536 c->u.simple->u.generic)))
538 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
539 return 25; /* invalid esetname */
541 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
542 c->u.simple->u.generic);
545 case Z_RecordComp_complex:
546 if (c->u.complex->generic)
548 /* insert check for schema */
549 if ((p = c->u.complex->generic->elementSpec))
553 case Z_ElementSpec_elementSetName:
555 data1_getesetbyname(dh, n->u.root.absyn,
556 p->u.elementSetName)))
558 logf(LOG_LOG, "Unknown esetname '%s'",
559 p->u.elementSetName);
560 return 25; /* invalid esetname */
562 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
563 p->u.elementSetName);
566 case Z_ElementSpec_externalSpec:
567 if (p->u.externalSpec->which == Z_External_espec1)
569 logf(LOG_DEBUG, "Got Espec-1");
570 espec = p->u.externalSpec-> u.espec1;
574 logf(LOG_LOG, "Unknown external espec.");
575 return 25; /* bad. what is proper diagnostic? */
586 logf (LOG_DEBUG, "Element: Espec-1 match");
587 return data1_doespec1(dh, n, espec);
591 logf (LOG_DEBUG, "Element: all match");
596 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
598 data1_node *node = 0, *onode = 0;
601 int res, selected = 0;
603 struct grs_read_info gri;
605 struct grs_handlers *h = (struct grs_handlers *) clientData;
606 int requested_schema = VAL_NONE;
609 gri.readf = p->readf;
610 gri.seekf = p->seekf;
611 gri.tellf = p->tellf;
618 logf (LOG_DEBUG, "grs_retrieve");
619 if (read_grs_type (h, &gri, p->subType, &node))
632 data1_pr_tree (p->dh, node, stdout);
634 logf (LOG_DEBUG, "grs_retrieve: size");
635 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
638 dnew->u.data.what = DATA1I_text;
639 dnew->u.data.data = dnew->lbuf;
640 sprintf(dnew->u.data.data, "%d", p->recordSize);
641 dnew->u.data.len = strlen(dnew->u.data.data);
644 tagname = res_get_def(p->res, "tagrank", "rank");
645 if (strcmp(tagname, "0") && p->score >= 0 &&
646 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
648 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
649 dnew->u.data.what = DATA1I_num;
650 dnew->u.data.data = dnew->lbuf;
651 sprintf(dnew->u.data.data, "%d", p->score);
652 dnew->u.data.len = strlen(dnew->u.data.data);
655 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
656 if (strcmp(tagname, "0") && p->localno > 0 &&
657 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
659 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
660 dnew->u.data.what = DATA1I_text;
661 dnew->u.data.data = dnew->lbuf;
662 sprintf(dnew->u.data.data, "%d", p->localno);
663 dnew->u.data.len = strlen(dnew->u.data.data);
666 if (p->comp && p->comp->which == Z_RecordComp_complex &&
667 p->comp->u.complex->generic &&
668 p->comp->u.complex->generic->schema)
670 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
672 requested_schema = oe->value;
675 /* If schema has been specified, map if possible, then check that
676 * we got the right one
678 if (requested_schema != VAL_NONE)
680 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
681 for (map = node->u.root.absyn->maptabs; map; map = map->next)
683 if (map->target_absyn_ref == requested_schema)
686 if (!(node = data1_map_record(p->dh, onode, map, mem)))
695 if (node->u.root.absyn &&
696 requested_schema != node->u.root.absyn->reference)
704 * Does the requested format match a known syntax-mapping? (this reflects
705 * the overlap of schema and formatting which is inherent in the MARC
708 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
709 for (map = node->u.root.absyn->maptabs; map; map = map->next)
711 if (map->target_absyn_ref == p->input_format)
714 if (!(node = data1_map_record(p->dh, onode, map, mem)))
723 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
724 if (node->u.root.absyn &&
725 node->u.root.absyn->reference != VAL_NONE &&
726 p->input_format == VAL_GRS1)
730 int oidtmp[OID_SIZE];
732 oe.proto = PROTO_Z3950;
733 oe.oclass = CLASS_SCHEMA;
734 oe.value = node->u.root.absyn->reference;
736 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
739 data1_handle dh = p->dh;
743 for (ii = oid; *ii >= 0; ii++)
747 sprintf(p, "%d", *ii);
752 if ((dnew = data1_insert_taggeddata(dh, node, node,
753 "schemaIdentifier", mem)))
755 dnew->u.data.what = DATA1I_oid;
756 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
757 memcpy(dnew->u.data.data, tmp, p - tmp);
758 dnew->u.data.len = p - tmp;
763 logf (LOG_DEBUG, "grs_retrieve: element spec");
764 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
768 data1_free_tree(p->dh, onode);
769 data1_free_tree(p->dh, node);
773 else if (p->comp && !res)
776 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
777 switch (p->output_format = (p->input_format != VAL_NONE ?
778 p->input_format : VAL_SUTRS))
780 data1_marctab *marctab;
784 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
789 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
790 memcpy (new_buf, p->rec_buf, p->rec_len);
791 p->rec_buf = new_buf;
796 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
798 p->diagnostic = 238; /* not available in requested syntax */
800 p->rec_len = (size_t) (-1);
803 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
807 p->rec_len = (size_t) (-1);
810 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
814 p->rec_len = (size_t) (-1);
817 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
822 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
823 memcpy (new_buf, p->rec_buf, p->rec_len);
824 p->rec_buf = new_buf;
828 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
833 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
834 memcpy (new_buf, p->rec_buf, p->rec_len);
835 p->rec_buf = new_buf;
839 if (!node->u.root.absyn)
844 for (marctab = node->u.root.absyn->marc; marctab;
845 marctab = marctab->next)
846 if (marctab->reference == p->input_format)
853 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
854 selected, &p->rec_len)))
858 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
859 memcpy (new_buf, p->rec_buf, p->rec_len);
860 p->rec_buf = new_buf;
864 data1_free_tree(p->dh, node);
866 data1_free_tree(p->dh, onode);
871 static struct recType grs_type =
880 RecType recTypeGrs = &grs_type;