2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.32 1999-09-07 07:19:21 adam
8 * Work on character mapping. Implemented replace rules.
10 * Revision 1.31 1999/07/14 10:56:43 adam
11 * Fixed potential memory leak.
13 * Revision 1.30 1999/07/06 12:26:41 adam
14 * Retrieval handler obeys schema and handles XML transfer syntax.
16 * Revision 1.29 1999/05/26 07:49:14 adam
19 * Revision 1.28 1999/05/21 12:00:17 adam
20 * Better diagnostics for extraction process.
22 * Revision 1.27 1999/05/20 12:57:18 adam
23 * Implemented TCL filter. Updated recctrl system.
25 * Revision 1.26 1999/03/02 16:15:44 quinn
26 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
28 * Revision 1.25 1999/02/18 15:01:26 adam
31 * Revision 1.24 1999/02/02 14:51:28 adam
32 * Updated WIN32 code specific sections. Changed header.
34 * Revision 1.23 1998/10/18 07:51:10 adam
35 * Changed one logf call.
37 * Revision 1.22 1998/10/16 08:14:37 adam
38 * Updated record control system.
40 * Revision 1.21 1998/07/01 09:16:10 adam
41 * Element localno only added when it's greater than 0.
43 * Revision 1.20 1998/05/20 10:12:26 adam
44 * Implemented automatic EXPLAIN database maintenance.
45 * Modified Zebra to work with ASN.1 compiled version of YAZ.
47 * Revision 1.19 1998/03/11 11:19:05 adam
48 * Changed the way sequence numbers are generated.
50 * Revision 1.18 1998/03/05 08:41:31 adam
53 * Revision 1.17 1998/02/10 12:03:06 adam
56 * Revision 1.16 1998/01/29 13:38:17 adam
57 * Fixed problem with mapping to record with unknown schema.
59 * Revision 1.15 1998/01/26 10:37:57 adam
62 * Revision 1.14 1997/11/06 11:41:01 adam
63 * Implemented "begin variant" for the sgml.regx filter.
65 * Revision 1.13 1997/10/31 12:35:44 adam
66 * Added a few log statements.
68 * Revision 1.12 1997/10/29 12:02:22 adam
69 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
71 * Revision 1.11 1997/10/27 14:34:00 adam
72 * Work on generic character mapping depending on "structure" field
73 * in abstract syntax file.
75 * Revision 1.10 1997/09/18 08:59:21 adam
76 * Extra generic handle for the character mapping routines.
78 * Revision 1.9 1997/09/17 12:19:21 adam
79 * Zebra version corresponds to YAZ version 1.4.
80 * Changed Zebra server so that it doesn't depend on global common_resource.
82 * Revision 1.8 1997/09/09 13:38:14 adam
83 * Partial port to WIN95/NT.
85 * Revision 1.7 1997/09/05 15:30:10 adam
86 * Changed prototype for chr_map_input - added const.
87 * Added support for C++, headers uses extern "C" for public definitions.
89 * Revision 1.6 1997/09/04 13:54:40 adam
90 * Added MARC filter - type grs.marc.<syntax> where syntax refers
91 * to abstract syntax. New method tellf in retrieve/extract method.
93 * Revision 1.5 1997/07/15 16:29:03 adam
94 * Initialized dummy variable to keep checker gcc happy.
96 * Revision 1.4 1997/04/30 08:56:08 quinn
99 * Revision 1.2 1996/10/11 16:06:43 quinn
100 * Revision 1.3 1997/02/24 10:41:50 adam
101 * Cleanup of code and commented out the "end element-end-record" code.
103 * Revision 1.2 1996/10/11 16:06:43 quinn
104 * Fixed arguments to nodetogr
106 * Revision 1.1 1996/10/11 10:57:25 adam
107 * New module recctrl. Used to manage records (extract/retrieval).
109 * Revision 1.29 1996/10/08 10:30:21 quinn
110 * Fixed type mismatch
112 * Revision 1.28 1996/10/07 16:06:40 quinn
115 * Revision 1.27 1996/06/11 10:54:12 quinn
118 * Revision 1.26 1996/06/06 12:08:45 quinn
119 * Added showRecord function
121 * Revision 1.25 1996/06/04 14:18:53 quinn
124 * Revision 1.24 1996/06/04 13:27:54 quinn
125 * More work on charmapping
127 * Revision 1.23 1996/06/04 10:19:01 adam
128 * Minor changes - removed include of ctype.h.
130 * Revision 1.22 1996/06/03 10:15:27 quinn
131 * Various character-mapping.
133 * Revision 1.21 1996/05/31 13:27:24 quinn
134 * Character-conversion in phrases, too.
136 * Revision 1.19 1996/05/16 15:31:14 quinn
139 * Revision 1.18 1996/05/09 07:28:56 quinn
140 * Work towards phrases and multiple registers
142 * Revision 1.17 1996/05/01 13:46:37 adam
143 * First work on multiple records in one file.
144 * New option, -offset, to the "unread" command in the filter module.
146 * Revision 1.16 1996/01/17 14:57:54 adam
147 * Prototype changed for reader functions in extract/retrieve. File
148 * is identified by 'void *' instead of 'int.
150 * Revision 1.15 1996/01/08 19:15:47 adam
151 * New input filter that works!
153 * Revision 1.14 1995/12/15 12:36:11 adam
154 * Retrieval calls data1_read_regx when subType is specified.
156 * Revision 1.13 1995/12/15 12:24:43 quinn
157 * *** empty log message ***
159 * Revision 1.12 1995/12/15 12:20:28 quinn
160 * *** empty log message ***
162 * Revision 1.11 1995/12/15 12:07:57 quinn
163 * Changed extraction strategy.
165 * Revision 1.10 1995/12/14 11:10:48 quinn
168 * Revision 1.9 1995/12/13 17:14:05 quinn
169 * *** empty log message ***
171 * Revision 1.8 1995/12/13 15:33:18 quinn
172 * *** empty log message ***
174 * Revision 1.7 1995/12/13 13:45:39 quinn
175 * Changed data1 to use nmem.
177 * Revision 1.6 1995/12/04 14:22:30 adam
178 * Extra arg to recType_byName.
179 * Started work on new regular expression parsed input to
180 * structured records.
182 * Revision 1.5 1995/11/28 14:18:37 quinn
185 * Revision 1.4 1995/11/21 13:14:49 quinn
186 * Fixed end-of-data-field problem (maybe).
188 * Revision 1.3 1995/11/15 19:13:09 adam
189 * Work on record management.
195 #include <sys/types.h>
206 #define GRS_MAX_WORD 512
212 struct grs_handler *next;
215 struct grs_handlers {
216 struct grs_handler *handlers;
219 static int read_grs_type (struct grs_handlers *h,
220 struct grs_read_info *p, const char *type,
223 struct grs_handler *gh = h->handlers;
224 const char *cp = strchr (type, '.');
226 if (cp == NULL || cp == type)
228 cp = strlen(type) + type;
232 strcpy (p->type, cp+1);
233 for (gh = h->handlers; gh; gh = gh->next)
235 if (!memcmp (type, gh->type->type, cp-type))
240 gh->clientData = (*gh->type->init)();
242 p->clientData = gh->clientData;
243 *root = (gh->type->read)(p);
244 gh->clientData = p->clientData;
251 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
253 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
254 gh->next = h->handlers;
261 static void *grs_init(RecType recType)
263 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
266 grs_add_handler (h, recTypeGrs_sgml);
267 grs_add_handler (h, recTypeGrs_regx);
269 grs_add_handler (h, recTypeGrs_tcl);
271 grs_add_handler (h, recTypeGrs_marc);
275 static void grs_destroy(void *clientData)
277 struct grs_handlers *h = (struct grs_handlers *) clientData;
278 struct grs_handler *gh = h->handlers, *gh_next;
283 (*gh->type->destroy)(gh->clientData);
290 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
293 (*p->init)(p, &wrd); /* set defaults */
294 for (; n; n = n->next)
296 if (p->flagShowRecords) /* display element description to user */
298 if (n->which == DATA1N_root)
300 printf("%*s", level * 4, "");
301 printf("Record type: '%s'\n", n->u.root.absyn->name);
303 else if (n->which == DATA1N_tag)
307 printf("%*s", level * 4, "");
308 if (!(e = n->u.tag.element))
309 printf("Local tag: '%s'\n", n->u.tag.tag);
312 printf("Elm: '%s' ", e->name);
315 data1_tag *t = e->tag;
317 printf("TagNam: '%s' ", t->names->name);
320 printf("%s[%d],", t->tagset->name, t->tagset->type);
323 if (t->which == DATA1T_numeric)
324 printf("%d)", t->value.numeric);
326 printf("'%s')", t->value.string);
334 if (dumpkeys(n->child, p, level + 1) < 0)
337 if (n->which == DATA1N_data)
339 data1_node *par = get_parent_tag(p->dh, n);
340 data1_termlist *tlist = 0;
341 data1_datatype dtype = DATA1K_string;
343 if (p->flagShowRecords)
345 printf("%*s", level * 4, "");
347 if (n->u.data.len > 32)
348 printf("'%.24s ... %.6s'\n", n->u.data.data,
349 n->u.data.data + n->u.data.len-6);
350 else if (n->u.data.len > 0)
351 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
359 * cycle up towards the root until we find a tag with an att..
360 * this has the effect of indexing locally defined tags with
361 * the attribute of their ancestor in the record.
364 while (!par->u.tag.element)
365 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
367 if (!par || !(tlist = par->u.tag.element->termlists))
369 if (par->u.tag.element->tag)
370 dtype = par->u.tag.element->tag->kind;
371 for (; tlist; tlist = tlist->next)
373 if (p->flagShowRecords)
375 printf("%*sIdx: [%s]", (level + 1) * 4, "",
377 printf("%s:%s [%d]\n",
378 tlist->att->parent->name,
379 tlist->att->name, tlist->att->value);
383 wrd.reg_type = *tlist->structure;
384 wrd.string = n->u.data.data;
385 wrd.length = n->u.data.len;
386 wrd.attrSet = (int) (tlist->att->parent->reference);
387 wrd.attrUse = tlist->att->locals->local;
388 (*p->tokenAdd)(&wrd);
392 if (p->flagShowRecords && n->which == DATA1N_root)
394 printf("%*s-------------\n\n", level * 4, "");
400 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
403 int oidtmp[OID_SIZE];
405 oe.proto = PROTO_Z3950;
406 oe.oclass = CLASS_SCHEMA;
407 oe.value = n->u.root.absyn->reference;
409 if ((oid_ent_to_oid (&oe, oidtmp)))
410 (*p->schemaAdd)(p, oidtmp);
412 return dumpkeys(n, p, 0);
415 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
419 struct grs_read_info gri;
421 int oidtmp[OID_SIZE];
423 gri.readf = p->readf;
424 gri.seekf = p->seekf;
425 gri.tellf = p->tellf;
428 gri.offset = p->offset;
432 if (read_grs_type (h, &gri, p->subType, &n))
433 return RECCTRL_EXTRACT_ERROR;
435 return RECCTRL_EXTRACT_EOF;
436 oe.proto = PROTO_Z3950;
437 oe.oclass = CLASS_SCHEMA;
438 oe.value = n->u.root.absyn->reference;
439 if ((oid_ent_to_oid (&oe, oidtmp)))
440 (*p->schemaAdd)(p, oidtmp);
442 if (dumpkeys(n, p, 0) < 0)
444 data1_free_tree(p->dh, n);
445 return RECCTRL_EXTRACT_ERROR;
447 data1_free_tree(p->dh, n);
448 return RECCTRL_EXTRACT_OK;
451 static int grs_extract(void *clientData, struct recExtractCtrl *p)
454 NMEM mem = nmem_create ();
455 struct grs_handlers *h = (struct grs_handlers *) clientData;
457 ret = grs_extract_sub(h, p, mem);
463 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
465 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
467 data1_esetname *eset;
473 case Z_RecordComp_simple:
474 if (c->u.simple->which != Z_ElementSetNames_generic)
475 return 26; /* only generic form supported. Fix this later */
476 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
477 c->u.simple->u.generic)))
479 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
480 return 25; /* invalid esetname */
482 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
483 c->u.simple->u.generic);
486 case Z_RecordComp_complex:
487 if (c->u.complex->generic)
489 /* insert check for schema */
490 if ((p = c->u.complex->generic->elementSpec))
494 case Z_ElementSpec_elementSetName:
496 data1_getesetbyname(dh, n->u.root.absyn,
497 p->u.elementSetName)))
499 logf(LOG_LOG, "Unknown esetname '%s'",
500 p->u.elementSetName);
501 return 25; /* invalid esetname */
503 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
504 p->u.elementSetName);
507 case Z_ElementSpec_externalSpec:
508 if (p->u.externalSpec->which == Z_External_espec1)
510 logf(LOG_DEBUG, "Got Espec-1");
511 espec = p->u.externalSpec-> u.espec1;
515 logf(LOG_LOG, "Unknown external espec.");
516 return 25; /* bad. what is proper diagnostic? */
527 logf (LOG_DEBUG, "Element: Espec-1 match");
528 return data1_doespec1(dh, n, espec);
532 logf (LOG_DEBUG, "Element: all match");
537 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
539 data1_node *node = 0, *onode = 0;
542 int res, selected = 0;
544 struct grs_read_info gri;
546 struct grs_handlers *h = (struct grs_handlers *) clientData;
547 int requested_schema = VAL_NONE;
550 gri.readf = p->readf;
551 gri.seekf = p->seekf;
552 gri.tellf = p->tellf;
559 logf (LOG_DEBUG, "grs_retrieve");
560 if (read_grs_type (h, &gri, p->subType, &node))
572 logf (LOG_DEBUG, "grs_retrieve: size");
573 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
576 dnew->u.data.what = DATA1I_text;
577 dnew->u.data.data = dnew->lbuf;
578 sprintf(dnew->u.data.data, "%d", p->recordSize);
579 dnew->u.data.len = strlen(dnew->u.data.data);
582 tagname = res_get_def(p->res, "tagrank", "rank");
583 if (strcmp(tagname, "0") && p->score >= 0 &&
584 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
586 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
587 dnew->u.data.what = DATA1I_num;
588 dnew->u.data.data = dnew->lbuf;
589 sprintf(dnew->u.data.data, "%d", p->score);
590 dnew->u.data.len = strlen(dnew->u.data.data);
593 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
594 if (strcmp(tagname, "0") && p->localno > 0 &&
595 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
597 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
598 dnew->u.data.what = DATA1I_text;
599 dnew->u.data.data = dnew->lbuf;
600 sprintf(dnew->u.data.data, "%d", p->localno);
601 dnew->u.data.len = strlen(dnew->u.data.data);
604 if (p->comp && p->comp->which == Z_RecordComp_complex &&
605 p->comp->u.complex->generic &&
606 p->comp->u.complex->generic->schema)
608 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
610 requested_schema = oe->value;
613 /* If schema has been specified, map if possible, then check that
614 * we got the right one
616 if (requested_schema != VAL_NONE)
618 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
619 for (map = node->u.root.absyn->maptabs; map; map = map->next)
621 if (map->target_absyn_ref == requested_schema)
624 if (!(node = data1_map_record(p->dh, onode, map, mem)))
633 if (node->u.root.absyn &&
634 requested_schema != node->u.root.absyn->reference)
642 * Does the requested format match a known syntax-mapping? (this reflects
643 * the overlap of schema and formatting which is inherent in the MARC
646 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
647 for (map = node->u.root.absyn->maptabs; map; map = map->next)
649 if (map->target_absyn_ref == p->input_format)
652 if (!(node = data1_map_record(p->dh, onode, map, mem)))
661 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
662 if (node->u.root.absyn &&
663 node->u.root.absyn->reference != VAL_NONE &&
664 p->input_format == VAL_GRS1)
668 int oidtmp[OID_SIZE];
670 oe.proto = PROTO_Z3950;
671 oe.oclass = CLASS_SCHEMA;
672 oe.value = node->u.root.absyn->reference;
674 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
677 data1_handle dh = p->dh;
681 for (ii = oid; *ii >= 0; ii++)
685 sprintf(p, "%d", *ii);
690 if ((dnew = data1_insert_taggeddata(dh, node, node,
691 "schemaIdentifier", mem)))
693 dnew->u.data.what = DATA1I_oid;
694 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
695 memcpy(dnew->u.data.data, tmp, p - tmp);
696 dnew->u.data.len = p - tmp;
701 logf (LOG_DEBUG, "grs_retrieve: element spec");
702 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
706 data1_free_tree(p->dh, onode);
707 data1_free_tree(p->dh, node);
711 else if (p->comp && !res)
714 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
715 switch (p->output_format = (p->input_format != VAL_NONE ?
716 p->input_format : VAL_SUTRS))
718 data1_marctab *marctab;
722 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
727 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
728 memcpy (new_buf, p->rec_buf, p->rec_len);
729 p->rec_buf = new_buf;
734 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
736 p->diagnostic = 238; /* not available in requested syntax */
738 p->rec_len = (size_t) (-1);
741 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
745 p->rec_len = (size_t) (-1);
748 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
752 p->rec_len = (size_t) (-1);
755 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
760 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
761 memcpy (new_buf, p->rec_buf, p->rec_len);
762 p->rec_buf = new_buf;
766 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
771 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
772 memcpy (new_buf, p->rec_buf, p->rec_len);
773 p->rec_buf = new_buf;
777 if (!node->u.root.absyn)
782 for (marctab = node->u.root.absyn->marc; marctab;
783 marctab = marctab->next)
784 if (marctab->reference == p->input_format)
791 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
797 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
798 memcpy (new_buf, p->rec_buf, p->rec_len);
799 p->rec_buf = new_buf;
803 data1_free_tree(p->dh, node);
805 data1_free_tree(p->dh, onode);
810 static struct recType grs_type =
819 RecType recTypeGrs = &grs_type;