2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.38 2000-12-05 14:44:58 adam
8 * Fixed minor bug that could cause zmbol to break it data were emitted
9 * with not parent tags.
11 * Revision 1.37 2000/12/05 12:22:53 adam
12 * Termlist source implemented (so that we can index values of XML/SGML
15 * Revision 1.36 2000/12/05 10:01:44 adam
16 * Fixed bug regarding user-defined attribute sets.
18 * Revision 1.35 2000/11/29 15:21:31 adam
19 * Fixed problem with passwd db.
21 * Revision 1.34 2000/02/25 13:24:49 adam
22 * Fixed bug regarding pointer conversion that showed up on OSF V5.
24 * Revision 1.33 1999/11/30 13:48:04 adam
25 * Improved installation. Updated for inclusion of YAZ header files.
27 * Revision 1.32 1999/09/07 07:19:21 adam
28 * Work on character mapping. Implemented replace rules.
30 * Revision 1.31 1999/07/14 10:56:43 adam
31 * Fixed potential memory leak.
33 * Revision 1.30 1999/07/06 12:26:41 adam
34 * Retrieval handler obeys schema and handles XML transfer syntax.
36 * Revision 1.29 1999/05/26 07:49:14 adam
39 * Revision 1.28 1999/05/21 12:00:17 adam
40 * Better diagnostics for extraction process.
42 * Revision 1.27 1999/05/20 12:57:18 adam
43 * Implemented TCL filter. Updated recctrl system.
45 * Revision 1.26 1999/03/02 16:15:44 quinn
46 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
48 * Revision 1.25 1999/02/18 15:01:26 adam
51 * Revision 1.24 1999/02/02 14:51:28 adam
52 * Updated WIN32 code specific sections. Changed header.
54 * Revision 1.23 1998/10/18 07:51:10 adam
55 * Changed one logf call.
57 * Revision 1.22 1998/10/16 08:14:37 adam
58 * Updated record control system.
60 * Revision 1.21 1998/07/01 09:16:10 adam
61 * Element localno only added when it's greater than 0.
63 * Revision 1.20 1998/05/20 10:12:26 adam
64 * Implemented automatic EXPLAIN database maintenance.
65 * Modified Zebra to work with ASN.1 compiled version of YAZ.
67 * Revision 1.19 1998/03/11 11:19:05 adam
68 * Changed the way sequence numbers are generated.
70 * Revision 1.18 1998/03/05 08:41:31 adam
73 * Revision 1.17 1998/02/10 12:03:06 adam
76 * Revision 1.16 1998/01/29 13:38:17 adam
77 * Fixed problem with mapping to record with unknown schema.
79 * Revision 1.15 1998/01/26 10:37:57 adam
82 * Revision 1.14 1997/11/06 11:41:01 adam
83 * Implemented "begin variant" for the sgml.regx filter.
85 * Revision 1.13 1997/10/31 12:35:44 adam
86 * Added a few log statements.
88 * Revision 1.12 1997/10/29 12:02:22 adam
89 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
91 * Revision 1.11 1997/10/27 14:34:00 adam
92 * Work on generic character mapping depending on "structure" field
93 * in abstract syntax file.
95 * Revision 1.10 1997/09/18 08:59:21 adam
96 * Extra generic handle for the character mapping routines.
98 * Revision 1.9 1997/09/17 12:19:21 adam
99 * Zebra version corresponds to YAZ version 1.4.
100 * Changed Zebra server so that it doesn't depend on global common_resource.
102 * Revision 1.8 1997/09/09 13:38:14 adam
103 * Partial port to WIN95/NT.
105 * Revision 1.7 1997/09/05 15:30:10 adam
106 * Changed prototype for chr_map_input - added const.
107 * Added support for C++, headers uses extern "C" for public definitions.
109 * Revision 1.6 1997/09/04 13:54:40 adam
110 * Added MARC filter - type grs.marc.<syntax> where syntax refers
111 * to abstract syntax. New method tellf in retrieve/extract method.
113 * Revision 1.5 1997/07/15 16:29:03 adam
114 * Initialized dummy variable to keep checker gcc happy.
116 * Revision 1.4 1997/04/30 08:56:08 quinn
119 * Revision 1.2 1996/10/11 16:06:43 quinn
120 * Revision 1.3 1997/02/24 10:41:50 adam
121 * Cleanup of code and commented out the "end element-end-record" code.
123 * Revision 1.2 1996/10/11 16:06:43 quinn
124 * Fixed arguments to nodetogr
126 * Revision 1.1 1996/10/11 10:57:25 adam
127 * New module recctrl. Used to manage records (extract/retrieval).
129 * Revision 1.29 1996/10/08 10:30:21 quinn
130 * Fixed type mismatch
132 * Revision 1.28 1996/10/07 16:06:40 quinn
135 * Revision 1.27 1996/06/11 10:54:12 quinn
138 * Revision 1.26 1996/06/06 12:08:45 quinn
139 * Added showRecord function
141 * Revision 1.25 1996/06/04 14:18:53 quinn
144 * Revision 1.24 1996/06/04 13:27:54 quinn
145 * More work on charmapping
147 * Revision 1.23 1996/06/04 10:19:01 adam
148 * Minor changes - removed include of ctype.h.
150 * Revision 1.22 1996/06/03 10:15:27 quinn
151 * Various character-mapping.
153 * Revision 1.21 1996/05/31 13:27:24 quinn
154 * Character-conversion in phrases, too.
156 * Revision 1.19 1996/05/16 15:31:14 quinn
159 * Revision 1.18 1996/05/09 07:28:56 quinn
160 * Work towards phrases and multiple registers
162 * Revision 1.17 1996/05/01 13:46:37 adam
163 * First work on multiple records in one file.
164 * New option, -offset, to the "unread" command in the filter module.
166 * Revision 1.16 1996/01/17 14:57:54 adam
167 * Prototype changed for reader functions in extract/retrieve. File
168 * is identified by 'void *' instead of 'int.
170 * Revision 1.15 1996/01/08 19:15:47 adam
171 * New input filter that works!
173 * Revision 1.14 1995/12/15 12:36:11 adam
174 * Retrieval calls data1_read_regx when subType is specified.
176 * Revision 1.13 1995/12/15 12:24:43 quinn
177 * *** empty log message ***
179 * Revision 1.12 1995/12/15 12:20:28 quinn
180 * *** empty log message ***
182 * Revision 1.11 1995/12/15 12:07:57 quinn
183 * Changed extraction strategy.
185 * Revision 1.10 1995/12/14 11:10:48 quinn
188 * Revision 1.9 1995/12/13 17:14:05 quinn
189 * *** empty log message ***
191 * Revision 1.8 1995/12/13 15:33:18 quinn
192 * *** empty log message ***
194 * Revision 1.7 1995/12/13 13:45:39 quinn
195 * Changed data1 to use nmem.
197 * Revision 1.6 1995/12/04 14:22:30 adam
198 * Extra arg to recType_byName.
199 * Started work on new regular expression parsed input to
200 * structured records.
202 * Revision 1.5 1995/11/28 14:18:37 quinn
205 * Revision 1.4 1995/11/21 13:14:49 quinn
206 * Fixed end-of-data-field problem (maybe).
208 * Revision 1.3 1995/11/15 19:13:09 adam
209 * Work on record management.
215 #include <sys/types.h>
226 #define GRS_MAX_WORD 512
232 struct grs_handler *next;
235 struct grs_handlers {
236 struct grs_handler *handlers;
239 static int read_grs_type (struct grs_handlers *h,
240 struct grs_read_info *p, const char *type,
243 struct grs_handler *gh = h->handlers;
244 const char *cp = strchr (type, '.');
246 if (cp == NULL || cp == type)
248 cp = strlen(type) + type;
252 strcpy (p->type, cp+1);
253 for (gh = h->handlers; gh; gh = gh->next)
255 if (!memcmp (type, gh->type->type, cp-type))
260 gh->clientData = (*gh->type->init)();
262 p->clientData = gh->clientData;
263 *root = (gh->type->read)(p);
264 gh->clientData = p->clientData;
271 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
273 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
274 gh->next = h->handlers;
281 static void *grs_init(RecType recType)
283 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
286 grs_add_handler (h, recTypeGrs_sgml);
287 grs_add_handler (h, recTypeGrs_regx);
289 grs_add_handler (h, recTypeGrs_tcl);
291 grs_add_handler (h, recTypeGrs_marc);
295 static void grs_destroy(void *clientData)
297 struct grs_handlers *h = (struct grs_handlers *) clientData;
298 struct grs_handler *gh = h->handlers, *gh_next;
303 (*gh->type->destroy)(gh->clientData);
310 static void index_tag (data1_node *par, data1_node *n,
311 struct recExtractCtrl *p, int level, RecWord *wrd)
313 data1_termlist *tlist = 0;
314 data1_datatype dtype = DATA1K_string;
316 * cycle up towards the root until we find a tag with an att..
317 * this has the effect of indexing locally defined tags with
318 * the attribute of their ancestor in the record.
321 while (!par->u.tag.element)
322 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
324 if (!par || !(tlist = par->u.tag.element->termlists))
326 if (par->u.tag.element->tag)
327 dtype = par->u.tag.element->tag->kind;
329 for (; tlist; tlist = tlist->next)
332 /* consider source */
335 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
337 wrd->string = n->u.data.data;
338 wrd->length = n->u.data.len;
340 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
341 n->which == DATA1N_tag)
343 data1_xattr *p = n->u.tag.attributes;
344 while (p && strcmp (p->name, xattr))
348 wrd->string = p->value;
349 wrd->length = strlen(p->value);
354 if (p->flagShowRecords)
357 printf("%*sIdx: [%s]", (level + 1) * 4, "",
359 printf("%s:%s [%d] %s",
360 tlist->att->parent->name,
361 tlist->att->name, tlist->att->value,
364 for (i = 0; i<wrd->length && i < 8; i++)
365 fputc (wrd->string[i], stdout);
369 fputc ('\n', stdout);
373 wrd->reg_type = *tlist->structure;
374 wrd->attrSet = (int) (tlist->att->parent->reference);
375 wrd->attrUse = tlist->att->locals->local;
382 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
385 (*p->init)(p, &wrd); /* set defaults */
386 for (; n; n = n->next)
388 if (p->flagShowRecords) /* display element description to user */
390 if (n->which == DATA1N_root)
392 printf("%*s", level * 4, "");
393 printf("Record type: '%s'\n", n->u.root.absyn->name);
395 else if (n->which == DATA1N_tag)
399 printf("%*s", level * 4, "");
400 if (!(e = n->u.tag.element))
401 printf("Local tag: '%s'\n", n->u.tag.tag);
404 printf("Elm: '%s' ", e->name);
407 data1_tag *t = e->tag;
409 printf("TagNam: '%s' ", t->names->name);
412 printf("%s[%d],", t->tagset->name, t->tagset->type);
415 if (t->which == DATA1T_numeric)
416 printf("%d)", t->value.numeric);
418 printf("'%s')", t->value.string);
426 if (dumpkeys(n->child, p, level + 1) < 0)
429 if (n->which == DATA1N_tag)
431 index_tag (n, n, p, level, &wrd);
434 if (n->which == DATA1N_data)
436 data1_node *par = get_parent_tag(p->dh, n);
438 if (p->flagShowRecords)
440 printf("%*s", level * 4, "");
442 if (n->u.data.len > 32)
443 printf("'%.24s ... %.6s'\n", n->u.data.data,
444 n->u.data.data + n->u.data.len-6);
445 else if (n->u.data.len > 0)
446 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
452 index_tag (par, n, p, level, &wrd);
454 if (p->flagShowRecords && n->which == DATA1N_root)
456 printf("%*s-------------\n\n", level * 4, "");
462 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
465 int oidtmp[OID_SIZE];
467 oe.proto = PROTO_Z3950;
468 oe.oclass = CLASS_SCHEMA;
469 oe.value = n->u.root.absyn->reference;
471 if ((oid_ent_to_oid (&oe, oidtmp)))
472 (*p->schemaAdd)(p, oidtmp);
474 return dumpkeys(n, p, 0);
477 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
481 struct grs_read_info gri;
483 int oidtmp[OID_SIZE];
485 gri.readf = p->readf;
486 gri.seekf = p->seekf;
487 gri.tellf = p->tellf;
490 gri.offset = p->offset;
494 if (read_grs_type (h, &gri, p->subType, &n))
495 return RECCTRL_EXTRACT_ERROR;
497 return RECCTRL_EXTRACT_EOF;
498 oe.proto = PROTO_Z3950;
499 oe.oclass = CLASS_SCHEMA;
500 oe.value = n->u.root.absyn->reference;
501 if ((oid_ent_to_oid (&oe, oidtmp)))
502 (*p->schemaAdd)(p, oidtmp);
504 if (dumpkeys(n, p, 0) < 0)
506 data1_free_tree(p->dh, n);
507 return RECCTRL_EXTRACT_ERROR;
509 data1_free_tree(p->dh, n);
510 return RECCTRL_EXTRACT_OK;
513 static int grs_extract(void *clientData, struct recExtractCtrl *p)
516 NMEM mem = nmem_create ();
517 struct grs_handlers *h = (struct grs_handlers *) clientData;
519 ret = grs_extract_sub(h, p, mem);
525 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
527 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
529 data1_esetname *eset;
535 case Z_RecordComp_simple:
536 if (c->u.simple->which != Z_ElementSetNames_generic)
537 return 26; /* only generic form supported. Fix this later */
538 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
539 c->u.simple->u.generic)))
541 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
542 return 25; /* invalid esetname */
544 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
545 c->u.simple->u.generic);
548 case Z_RecordComp_complex:
549 if (c->u.complex->generic)
551 /* insert check for schema */
552 if ((p = c->u.complex->generic->elementSpec))
556 case Z_ElementSpec_elementSetName:
558 data1_getesetbyname(dh, n->u.root.absyn,
559 p->u.elementSetName)))
561 logf(LOG_LOG, "Unknown esetname '%s'",
562 p->u.elementSetName);
563 return 25; /* invalid esetname */
565 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
566 p->u.elementSetName);
569 case Z_ElementSpec_externalSpec:
570 if (p->u.externalSpec->which == Z_External_espec1)
572 logf(LOG_DEBUG, "Got Espec-1");
573 espec = p->u.externalSpec-> u.espec1;
577 logf(LOG_LOG, "Unknown external espec.");
578 return 25; /* bad. what is proper diagnostic? */
589 logf (LOG_DEBUG, "Element: Espec-1 match");
590 return data1_doespec1(dh, n, espec);
594 logf (LOG_DEBUG, "Element: all match");
599 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
601 data1_node *node = 0, *onode = 0;
604 int res, selected = 0;
606 struct grs_read_info gri;
608 struct grs_handlers *h = (struct grs_handlers *) clientData;
609 int requested_schema = VAL_NONE;
612 gri.readf = p->readf;
613 gri.seekf = p->seekf;
614 gri.tellf = p->tellf;
621 logf (LOG_DEBUG, "grs_retrieve");
622 if (read_grs_type (h, &gri, p->subType, &node))
635 data1_pr_tree (p->dh, node, stdout);
637 logf (LOG_DEBUG, "grs_retrieve: size");
638 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
641 dnew->u.data.what = DATA1I_text;
642 dnew->u.data.data = dnew->lbuf;
643 sprintf(dnew->u.data.data, "%d", p->recordSize);
644 dnew->u.data.len = strlen(dnew->u.data.data);
647 tagname = res_get_def(p->res, "tagrank", "rank");
648 if (strcmp(tagname, "0") && p->score >= 0 &&
649 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
651 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
652 dnew->u.data.what = DATA1I_num;
653 dnew->u.data.data = dnew->lbuf;
654 sprintf(dnew->u.data.data, "%d", p->score);
655 dnew->u.data.len = strlen(dnew->u.data.data);
658 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
659 if (strcmp(tagname, "0") && p->localno > 0 &&
660 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
662 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
663 dnew->u.data.what = DATA1I_text;
664 dnew->u.data.data = dnew->lbuf;
665 sprintf(dnew->u.data.data, "%d", p->localno);
666 dnew->u.data.len = strlen(dnew->u.data.data);
669 if (p->comp && p->comp->which == Z_RecordComp_complex &&
670 p->comp->u.complex->generic &&
671 p->comp->u.complex->generic->schema)
673 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
675 requested_schema = oe->value;
678 /* If schema has been specified, map if possible, then check that
679 * we got the right one
681 if (requested_schema != VAL_NONE)
683 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
684 for (map = node->u.root.absyn->maptabs; map; map = map->next)
686 if (map->target_absyn_ref == requested_schema)
689 if (!(node = data1_map_record(p->dh, onode, map, mem)))
698 if (node->u.root.absyn &&
699 requested_schema != node->u.root.absyn->reference)
707 * Does the requested format match a known syntax-mapping? (this reflects
708 * the overlap of schema and formatting which is inherent in the MARC
711 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
712 for (map = node->u.root.absyn->maptabs; map; map = map->next)
714 if (map->target_absyn_ref == p->input_format)
717 if (!(node = data1_map_record(p->dh, onode, map, mem)))
726 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
727 if (node->u.root.absyn &&
728 node->u.root.absyn->reference != VAL_NONE &&
729 p->input_format == VAL_GRS1)
733 int oidtmp[OID_SIZE];
735 oe.proto = PROTO_Z3950;
736 oe.oclass = CLASS_SCHEMA;
737 oe.value = node->u.root.absyn->reference;
739 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
742 data1_handle dh = p->dh;
746 for (ii = oid; *ii >= 0; ii++)
750 sprintf(p, "%d", *ii);
755 if ((dnew = data1_insert_taggeddata(dh, node, node,
756 "schemaIdentifier", mem)))
758 dnew->u.data.what = DATA1I_oid;
759 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
760 memcpy(dnew->u.data.data, tmp, p - tmp);
761 dnew->u.data.len = p - tmp;
766 logf (LOG_DEBUG, "grs_retrieve: element spec");
767 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
771 data1_free_tree(p->dh, onode);
772 data1_free_tree(p->dh, node);
776 else if (p->comp && !res)
779 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
780 switch (p->output_format = (p->input_format != VAL_NONE ?
781 p->input_format : VAL_SUTRS))
783 data1_marctab *marctab;
787 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
792 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
793 memcpy (new_buf, p->rec_buf, p->rec_len);
794 p->rec_buf = new_buf;
799 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
801 p->diagnostic = 238; /* not available in requested syntax */
803 p->rec_len = (size_t) (-1);
806 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
810 p->rec_len = (size_t) (-1);
813 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
817 p->rec_len = (size_t) (-1);
820 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
825 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
826 memcpy (new_buf, p->rec_buf, p->rec_len);
827 p->rec_buf = new_buf;
831 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
836 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
837 memcpy (new_buf, p->rec_buf, p->rec_len);
838 p->rec_buf = new_buf;
842 if (!node->u.root.absyn)
847 for (marctab = node->u.root.absyn->marc; marctab;
848 marctab = marctab->next)
849 if (marctab->reference == p->input_format)
856 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
857 selected, &p->rec_len)))
861 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
862 memcpy (new_buf, p->rec_buf, p->rec_len);
863 p->rec_buf = new_buf;
867 data1_free_tree(p->dh, node);
869 data1_free_tree(p->dh, onode);
874 static struct recType grs_type =
883 RecType recTypeGrs = &grs_type;