2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.30 1999-07-06 12:26:41 adam
8 * Retrieval handler obeys schema and handles XML transfer syntax.
10 * Revision 1.29 1999/05/26 07:49:14 adam
13 * Revision 1.28 1999/05/21 12:00:17 adam
14 * Better diagnostics for extraction process.
16 * Revision 1.27 1999/05/20 12:57:18 adam
17 * Implemented TCL filter. Updated recctrl system.
19 * Revision 1.26 1999/03/02 16:15:44 quinn
20 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
22 * Revision 1.25 1999/02/18 15:01:26 adam
25 * Revision 1.24 1999/02/02 14:51:28 adam
26 * Updated WIN32 code specific sections. Changed header.
28 * Revision 1.23 1998/10/18 07:51:10 adam
29 * Changed one logf call.
31 * Revision 1.22 1998/10/16 08:14:37 adam
32 * Updated record control system.
34 * Revision 1.21 1998/07/01 09:16:10 adam
35 * Element localno only added when it's greater than 0.
37 * Revision 1.20 1998/05/20 10:12:26 adam
38 * Implemented automatic EXPLAIN database maintenance.
39 * Modified Zebra to work with ASN.1 compiled version of YAZ.
41 * Revision 1.19 1998/03/11 11:19:05 adam
42 * Changed the way sequence numbers are generated.
44 * Revision 1.18 1998/03/05 08:41:31 adam
47 * Revision 1.17 1998/02/10 12:03:06 adam
50 * Revision 1.16 1998/01/29 13:38:17 adam
51 * Fixed problem with mapping to record with unknown schema.
53 * Revision 1.15 1998/01/26 10:37:57 adam
56 * Revision 1.14 1997/11/06 11:41:01 adam
57 * Implemented "begin variant" for the sgml.regx filter.
59 * Revision 1.13 1997/10/31 12:35:44 adam
60 * Added a few log statements.
62 * Revision 1.12 1997/10/29 12:02:22 adam
63 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
65 * Revision 1.11 1997/10/27 14:34:00 adam
66 * Work on generic character mapping depending on "structure" field
67 * in abstract syntax file.
69 * Revision 1.10 1997/09/18 08:59:21 adam
70 * Extra generic handle for the character mapping routines.
72 * Revision 1.9 1997/09/17 12:19:21 adam
73 * Zebra version corresponds to YAZ version 1.4.
74 * Changed Zebra server so that it doesn't depend on global common_resource.
76 * Revision 1.8 1997/09/09 13:38:14 adam
77 * Partial port to WIN95/NT.
79 * Revision 1.7 1997/09/05 15:30:10 adam
80 * Changed prototype for chr_map_input - added const.
81 * Added support for C++, headers uses extern "C" for public definitions.
83 * Revision 1.6 1997/09/04 13:54:40 adam
84 * Added MARC filter - type grs.marc.<syntax> where syntax refers
85 * to abstract syntax. New method tellf in retrieve/extract method.
87 * Revision 1.5 1997/07/15 16:29:03 adam
88 * Initialized dummy variable to keep checker gcc happy.
90 * Revision 1.4 1997/04/30 08:56:08 quinn
93 * Revision 1.2 1996/10/11 16:06:43 quinn
94 * Revision 1.3 1997/02/24 10:41:50 adam
95 * Cleanup of code and commented out the "end element-end-record" code.
97 * Revision 1.2 1996/10/11 16:06:43 quinn
98 * Fixed arguments to nodetogr
100 * Revision 1.1 1996/10/11 10:57:25 adam
101 * New module recctrl. Used to manage records (extract/retrieval).
103 * Revision 1.29 1996/10/08 10:30:21 quinn
104 * Fixed type mismatch
106 * Revision 1.28 1996/10/07 16:06:40 quinn
109 * Revision 1.27 1996/06/11 10:54:12 quinn
112 * Revision 1.26 1996/06/06 12:08:45 quinn
113 * Added showRecord function
115 * Revision 1.25 1996/06/04 14:18:53 quinn
118 * Revision 1.24 1996/06/04 13:27:54 quinn
119 * More work on charmapping
121 * Revision 1.23 1996/06/04 10:19:01 adam
122 * Minor changes - removed include of ctype.h.
124 * Revision 1.22 1996/06/03 10:15:27 quinn
125 * Various character-mapping.
127 * Revision 1.21 1996/05/31 13:27:24 quinn
128 * Character-conversion in phrases, too.
130 * Revision 1.19 1996/05/16 15:31:14 quinn
133 * Revision 1.18 1996/05/09 07:28:56 quinn
134 * Work towards phrases and multiple registers
136 * Revision 1.17 1996/05/01 13:46:37 adam
137 * First work on multiple records in one file.
138 * New option, -offset, to the "unread" command in the filter module.
140 * Revision 1.16 1996/01/17 14:57:54 adam
141 * Prototype changed for reader functions in extract/retrieve. File
142 * is identified by 'void *' instead of 'int.
144 * Revision 1.15 1996/01/08 19:15:47 adam
145 * New input filter that works!
147 * Revision 1.14 1995/12/15 12:36:11 adam
148 * Retrieval calls data1_read_regx when subType is specified.
150 * Revision 1.13 1995/12/15 12:24:43 quinn
151 * *** empty log message ***
153 * Revision 1.12 1995/12/15 12:20:28 quinn
154 * *** empty log message ***
156 * Revision 1.11 1995/12/15 12:07:57 quinn
157 * Changed extraction strategy.
159 * Revision 1.10 1995/12/14 11:10:48 quinn
162 * Revision 1.9 1995/12/13 17:14:05 quinn
163 * *** empty log message ***
165 * Revision 1.8 1995/12/13 15:33:18 quinn
166 * *** empty log message ***
168 * Revision 1.7 1995/12/13 13:45:39 quinn
169 * Changed data1 to use nmem.
171 * Revision 1.6 1995/12/04 14:22:30 adam
172 * Extra arg to recType_byName.
173 * Started work on new regular expression parsed input to
174 * structured records.
176 * Revision 1.5 1995/11/28 14:18:37 quinn
179 * Revision 1.4 1995/11/21 13:14:49 quinn
180 * Fixed end-of-data-field problem (maybe).
182 * Revision 1.3 1995/11/15 19:13:09 adam
183 * Work on record management.
189 #include <sys/types.h>
200 #define GRS_MAX_WORD 512
206 struct grs_handler *next;
209 struct grs_handlers {
210 struct grs_handler *handlers;
213 static int read_grs_type (struct grs_handlers *h,
214 struct grs_read_info *p, const char *type,
217 struct grs_handler *gh = h->handlers;
218 const char *cp = strchr (type, '.');
220 if (cp == NULL || cp == type)
222 cp = strlen(type) + type;
226 strcpy (p->type, cp+1);
227 for (gh = h->handlers; gh; gh = gh->next)
229 if (!memcmp (type, gh->type->type, cp-type))
234 gh->clientData = (*gh->type->init)();
236 p->clientData = gh->clientData;
237 *root = (gh->type->read)(p);
238 gh->clientData = p->clientData;
245 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
247 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
248 gh->next = h->handlers;
255 static void *grs_init(RecType recType)
257 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
260 grs_add_handler (h, recTypeGrs_sgml);
261 grs_add_handler (h, recTypeGrs_regx);
263 grs_add_handler (h, recTypeGrs_tcl);
265 grs_add_handler (h, recTypeGrs_marc);
269 static void grs_destroy(void *clientData)
271 struct grs_handlers *h = (struct grs_handlers *) clientData;
272 struct grs_handler *gh = h->handlers, *gh_next;
277 (*gh->type->destroy)(gh->clientData);
284 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
287 (*p->init)(p, &wrd); /* set defaults */
288 for (; n; n = n->next)
290 if (p->flagShowRecords) /* display element description to user */
292 if (n->which == DATA1N_root)
294 printf("%*s", level * 4, "");
295 printf("Record type: '%s'\n", n->u.root.absyn->name);
297 else if (n->which == DATA1N_tag)
301 printf("%*s", level * 4, "");
302 if (!(e = n->u.tag.element))
303 printf("Local tag: '%s'\n", n->u.tag.tag);
306 printf("Elm: '%s' ", e->name);
309 data1_tag *t = e->tag;
311 printf("TagNam: '%s' ", t->names->name);
314 printf("%s[%d],", t->tagset->name, t->tagset->type);
317 if (t->which == DATA1T_numeric)
318 printf("%d)", t->value.numeric);
320 printf("'%s')", t->value.string);
328 if (dumpkeys(n->child, p, level + 1) < 0)
331 if (n->which == DATA1N_data)
333 data1_node *par = get_parent_tag(p->dh, n);
334 data1_termlist *tlist = 0;
335 data1_datatype dtype = DATA1K_string;
337 if (p->flagShowRecords)
339 printf("%*s", level * 4, "");
341 if (n->u.data.len > 32)
342 printf("'%.24s ... %.6s'\n", n->u.data.data,
343 n->u.data.data + n->u.data.len-6);
344 else if (n->u.data.len > 0)
345 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
353 * cycle up towards the root until we find a tag with an att..
354 * this has the effect of indexing locally defined tags with
355 * the attribute of their ancestor in the record.
358 while (!par->u.tag.element)
359 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
361 if (!par || !(tlist = par->u.tag.element->termlists))
363 if (par->u.tag.element->tag)
364 dtype = par->u.tag.element->tag->kind;
365 for (; tlist; tlist = tlist->next)
367 if (p->flagShowRecords)
369 printf("%*sIdx: [%s]", (level + 1) * 4, "",
371 printf("%s:%s [%d]\n",
372 tlist->att->parent->name,
373 tlist->att->name, tlist->att->value);
377 wrd.reg_type = *tlist->structure;
378 wrd.string = n->u.data.data;
379 wrd.length = n->u.data.len;
380 wrd.attrSet = (int) (tlist->att->parent->reference);
381 wrd.attrUse = tlist->att->locals->local;
386 if (p->flagShowRecords && n->which == DATA1N_root)
388 printf("%*s-------------\n\n", level * 4, "");
394 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
397 int oidtmp[OID_SIZE];
399 oe.proto = PROTO_Z3950;
400 oe.oclass = CLASS_SCHEMA;
401 oe.value = n->u.root.absyn->reference;
403 if ((oid_ent_to_oid (&oe, oidtmp)))
404 (*p->addSchema)(p, oidtmp);
406 return dumpkeys(n, p, 0);
409 static int grs_extract(void *clientData, struct recExtractCtrl *p)
413 struct grs_read_info gri;
415 int oidtmp[OID_SIZE];
416 struct grs_handlers *h = (struct grs_handlers *) clientData;
418 mem = nmem_create ();
419 gri.readf = p->readf;
420 gri.seekf = p->seekf;
421 gri.tellf = p->tellf;
424 gri.offset = p->offset;
428 if (read_grs_type (h, &gri, p->subType, &n))
429 return RECCTRL_EXTRACT_ERROR;
431 return RECCTRL_EXTRACT_EOF;
433 oe.proto = PROTO_Z3950;
434 oe.oclass = CLASS_SCHEMA;
435 oe.value = n->u.root.absyn->reference;
436 if ((oid_ent_to_oid (&oe, oidtmp)))
437 (*p->addSchema)(p, oidtmp);
439 if (dumpkeys(n, p, 0) < 0)
441 data1_free_tree(p->dh, n);
442 return RECCTRL_EXTRACT_ERROR;
444 data1_free_tree(p->dh, n);
446 return RECCTRL_EXTRACT_OK;
450 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
452 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
454 data1_esetname *eset;
460 case Z_RecordComp_simple:
461 if (c->u.simple->which != Z_ElementSetNames_generic)
462 return 26; /* only generic form supported. Fix this later */
463 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
464 c->u.simple->u.generic)))
466 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
467 return 25; /* invalid esetname */
469 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
470 c->u.simple->u.generic);
473 case Z_RecordComp_complex:
474 if (c->u.complex->generic)
476 /* insert check for schema */
477 if ((p = c->u.complex->generic->elementSpec))
481 case Z_ElementSpec_elementSetName:
483 data1_getesetbyname(dh, n->u.root.absyn,
484 p->u.elementSetName)))
486 logf(LOG_LOG, "Unknown esetname '%s'",
487 p->u.elementSetName);
488 return 25; /* invalid esetname */
490 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
491 p->u.elementSetName);
494 case Z_ElementSpec_externalSpec:
495 if (p->u.externalSpec->which == Z_External_espec1)
497 logf(LOG_DEBUG, "Got Espec-1");
498 espec = p->u.externalSpec-> u.espec1;
502 logf(LOG_LOG, "Unknown external espec.");
503 return 25; /* bad. what is proper diagnostic? */
514 logf (LOG_DEBUG, "Element: Espec-1 match");
515 return data1_doespec1(dh, n, espec);
519 logf (LOG_DEBUG, "Element: all match");
524 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
526 data1_node *node = 0, *onode = 0;
529 int res, selected = 0;
531 struct grs_read_info gri;
533 struct grs_handlers *h = (struct grs_handlers *) clientData;
534 int requested_schema = VAL_NONE;
537 gri.readf = p->readf;
538 gri.seekf = p->seekf;
539 gri.tellf = p->tellf;
546 logf (LOG_DEBUG, "grs_retrieve");
547 if (read_grs_type (h, &gri, p->subType, &node))
559 logf (LOG_DEBUG, "grs_retrieve: size");
560 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
563 dnew->u.data.what = DATA1I_text;
564 dnew->u.data.data = dnew->lbuf;
565 sprintf(dnew->u.data.data, "%d", p->recordSize);
566 dnew->u.data.len = strlen(dnew->u.data.data);
569 tagname = res_get_def(p->res, "tagrank", "rank");
570 if (strcmp(tagname, "0") && p->score >= 0 &&
571 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
573 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
574 dnew->u.data.what = DATA1I_num;
575 dnew->u.data.data = dnew->lbuf;
576 sprintf(dnew->u.data.data, "%d", p->score);
577 dnew->u.data.len = strlen(dnew->u.data.data);
580 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
581 if (strcmp(tagname, "0") && p->localno > 0 &&
582 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
584 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
585 dnew->u.data.what = DATA1I_text;
586 dnew->u.data.data = dnew->lbuf;
587 sprintf(dnew->u.data.data, "%d", p->localno);
588 dnew->u.data.len = strlen(dnew->u.data.data);
591 if (p->comp && p->comp->which == Z_RecordComp_complex &&
592 p->comp->u.complex->generic &&
593 p->comp->u.complex->generic->schema)
595 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
597 requested_schema = oe->value;
600 /* If schema has been specified, map if possible, then check that
601 * we got the right one
603 if (requested_schema != VAL_NONE)
605 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
606 for (map = node->u.root.absyn->maptabs; map; map = map->next)
608 if (map->target_absyn_ref == requested_schema)
611 if (!(node = data1_map_record(p->dh, onode, map, mem)))
620 if (node->u.root.absyn &&
621 requested_schema != node->u.root.absyn->reference)
629 * Does the requested format match a known syntax-mapping? (this reflects
630 * the overlap of schema and formatting which is inherent in the MARC
633 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
634 for (map = node->u.root.absyn->maptabs; map; map = map->next)
636 if (map->target_absyn_ref == p->input_format)
639 if (!(node = data1_map_record(p->dh, onode, map, mem)))
648 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
649 if (node->u.root.absyn &&
650 node->u.root.absyn->reference != VAL_NONE &&
651 p->input_format == VAL_GRS1)
655 int oidtmp[OID_SIZE];
657 oe.proto = PROTO_Z3950;
658 oe.oclass = CLASS_SCHEMA;
659 oe.value = node->u.root.absyn->reference;
661 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
664 data1_handle dh = p->dh;
668 for (ii = oid; *ii >= 0; ii++)
672 sprintf(p, "%d", *ii);
677 if ((dnew = data1_insert_taggeddata(dh, node, node,
678 "schemaIdentifier", mem)))
680 dnew->u.data.what = DATA1I_oid;
681 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
682 memcpy(dnew->u.data.data, tmp, p - tmp);
683 dnew->u.data.len = p - tmp;
688 logf (LOG_DEBUG, "grs_retrieve: element spec");
689 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
693 data1_free_tree(p->dh, onode);
694 data1_free_tree(p->dh, node);
698 else if (p->comp && !res)
701 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
702 switch (p->output_format = (p->input_format != VAL_NONE ?
703 p->input_format : VAL_SUTRS))
705 data1_marctab *marctab;
709 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
714 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
715 memcpy (new_buf, p->rec_buf, p->rec_len);
716 p->rec_buf = new_buf;
721 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
723 p->diagnostic = 238; /* not available in requested syntax */
725 p->rec_len = (size_t) (-1);
728 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
732 p->rec_len = (size_t) (-1);
735 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
739 p->rec_len = (size_t) (-1);
742 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
747 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
748 memcpy (new_buf, p->rec_buf, p->rec_len);
749 p->rec_buf = new_buf;
753 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
758 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
759 memcpy (new_buf, p->rec_buf, p->rec_len);
760 p->rec_buf = new_buf;
764 if (!node->u.root.absyn)
769 for (marctab = node->u.root.absyn->marc; marctab;
770 marctab = marctab->next)
771 if (marctab->reference == p->input_format)
778 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
784 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
785 memcpy (new_buf, p->rec_buf, p->rec_len);
786 p->rec_buf = new_buf;
790 data1_free_tree(p->dh, node);
792 data1_free_tree(p->dh, onode);
797 static struct recType grs_type =
806 RecType recTypeGrs = &grs_type;