2 * Copyright (C) 1994-2001, Index Data
6 * Revision 1.41 2001-05-22 21:01:47 adam
7 * Removed print of data1 tree on stdout so that inetd works again.
9 * Revision 1.40 2001/03/29 21:31:31 adam
10 * Fixed "record begin" for Tcl filter.
12 * Revision 1.39 2000/12/05 19:09:15 adam
13 * Fixed problem where indexer could crash if abstract syntax was undefined.
15 * Revision 1.38 2000/12/05 14:44:58 adam
16 * Fixed minor bug that could cause zmbol to break it data were emitted
17 * with not parent tags.
19 * Revision 1.37 2000/12/05 12:22:53 adam
20 * Termlist source implemented (so that we can index values of XML/SGML
23 * Revision 1.36 2000/12/05 10:01:44 adam
24 * Fixed bug regarding user-defined attribute sets.
26 * Revision 1.35 2000/11/29 15:21:31 adam
27 * Fixed problem with passwd db.
29 * Revision 1.34 2000/02/25 13:24:49 adam
30 * Fixed bug regarding pointer conversion that showed up on OSF V5.
32 * Revision 1.33 1999/11/30 13:48:04 adam
33 * Improved installation. Updated for inclusion of YAZ header files.
35 * Revision 1.32 1999/09/07 07:19:21 adam
36 * Work on character mapping. Implemented replace rules.
38 * Revision 1.31 1999/07/14 10:56:43 adam
39 * Fixed potential memory leak.
41 * Revision 1.30 1999/07/06 12:26:41 adam
42 * Retrieval handler obeys schema and handles XML transfer syntax.
44 * Revision 1.29 1999/05/26 07:49:14 adam
47 * Revision 1.28 1999/05/21 12:00:17 adam
48 * Better diagnostics for extraction process.
50 * Revision 1.27 1999/05/20 12:57:18 adam
51 * Implemented TCL filter. Updated recctrl system.
53 * Revision 1.26 1999/03/02 16:15:44 quinn
54 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
56 * Revision 1.25 1999/02/18 15:01:26 adam
59 * Revision 1.24 1999/02/02 14:51:28 adam
60 * Updated WIN32 code specific sections. Changed header.
62 * Revision 1.23 1998/10/18 07:51:10 adam
63 * Changed one logf call.
65 * Revision 1.22 1998/10/16 08:14:37 adam
66 * Updated record control system.
68 * Revision 1.21 1998/07/01 09:16:10 adam
69 * Element localno only added when it's greater than 0.
71 * Revision 1.20 1998/05/20 10:12:26 adam
72 * Implemented automatic EXPLAIN database maintenance.
73 * Modified Zebra to work with ASN.1 compiled version of YAZ.
75 * Revision 1.19 1998/03/11 11:19:05 adam
76 * Changed the way sequence numbers are generated.
78 * Revision 1.18 1998/03/05 08:41:31 adam
81 * Revision 1.17 1998/02/10 12:03:06 adam
84 * Revision 1.16 1998/01/29 13:38:17 adam
85 * Fixed problem with mapping to record with unknown schema.
87 * Revision 1.15 1998/01/26 10:37:57 adam
90 * Revision 1.14 1997/11/06 11:41:01 adam
91 * Implemented "begin variant" for the sgml.regx filter.
93 * Revision 1.13 1997/10/31 12:35:44 adam
94 * Added a few log statements.
96 * Revision 1.12 1997/10/29 12:02:22 adam
97 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
99 * Revision 1.11 1997/10/27 14:34:00 adam
100 * Work on generic character mapping depending on "structure" field
101 * in abstract syntax file.
103 * Revision 1.10 1997/09/18 08:59:21 adam
104 * Extra generic handle for the character mapping routines.
106 * Revision 1.9 1997/09/17 12:19:21 adam
107 * Zebra version corresponds to YAZ version 1.4.
108 * Changed Zebra server so that it doesn't depend on global common_resource.
110 * Revision 1.8 1997/09/09 13:38:14 adam
111 * Partial port to WIN95/NT.
113 * Revision 1.7 1997/09/05 15:30:10 adam
114 * Changed prototype for chr_map_input - added const.
115 * Added support for C++, headers uses extern "C" for public definitions.
117 * Revision 1.6 1997/09/04 13:54:40 adam
118 * Added MARC filter - type grs.marc.<syntax> where syntax refers
119 * to abstract syntax. New method tellf in retrieve/extract method.
121 * Revision 1.5 1997/07/15 16:29:03 adam
122 * Initialized dummy variable to keep checker gcc happy.
124 * Revision 1.4 1997/04/30 08:56:08 quinn
127 * Revision 1.2 1996/10/11 16:06:43 quinn
128 * Revision 1.3 1997/02/24 10:41:50 adam
129 * Cleanup of code and commented out the "end element-end-record" code.
131 * Revision 1.2 1996/10/11 16:06:43 quinn
132 * Fixed arguments to nodetogr
134 * Revision 1.1 1996/10/11 10:57:25 adam
135 * New module recctrl. Used to manage records (extract/retrieval).
137 * Revision 1.29 1996/10/08 10:30:21 quinn
138 * Fixed type mismatch
140 * Revision 1.28 1996/10/07 16:06:40 quinn
143 * Revision 1.27 1996/06/11 10:54:12 quinn
146 * Revision 1.26 1996/06/06 12:08:45 quinn
147 * Added showRecord function
149 * Revision 1.25 1996/06/04 14:18:53 quinn
152 * Revision 1.24 1996/06/04 13:27:54 quinn
153 * More work on charmapping
155 * Revision 1.23 1996/06/04 10:19:01 adam
156 * Minor changes - removed include of ctype.h.
158 * Revision 1.22 1996/06/03 10:15:27 quinn
159 * Various character-mapping.
161 * Revision 1.21 1996/05/31 13:27:24 quinn
162 * Character-conversion in phrases, too.
164 * Revision 1.19 1996/05/16 15:31:14 quinn
167 * Revision 1.18 1996/05/09 07:28:56 quinn
168 * Work towards phrases and multiple registers
170 * Revision 1.17 1996/05/01 13:46:37 adam
171 * First work on multiple records in one file.
172 * New option, -offset, to the "unread" command in the filter module.
174 * Revision 1.16 1996/01/17 14:57:54 adam
175 * Prototype changed for reader functions in extract/retrieve. File
176 * is identified by 'void *' instead of 'int.
178 * Revision 1.15 1996/01/08 19:15:47 adam
179 * New input filter that works!
181 * Revision 1.14 1995/12/15 12:36:11 adam
182 * Retrieval calls data1_read_regx when subType is specified.
184 * Revision 1.13 1995/12/15 12:24:43 quinn
185 * *** empty log message ***
187 * Revision 1.12 1995/12/15 12:20:28 quinn
188 * *** empty log message ***
190 * Revision 1.11 1995/12/15 12:07:57 quinn
191 * Changed extraction strategy.
193 * Revision 1.10 1995/12/14 11:10:48 quinn
196 * Revision 1.9 1995/12/13 17:14:05 quinn
197 * *** empty log message ***
199 * Revision 1.8 1995/12/13 15:33:18 quinn
200 * *** empty log message ***
202 * Revision 1.7 1995/12/13 13:45:39 quinn
203 * Changed data1 to use nmem.
205 * Revision 1.6 1995/12/04 14:22:30 adam
206 * Extra arg to recType_byName.
207 * Started work on new regular expression parsed input to
208 * structured records.
210 * Revision 1.5 1995/11/28 14:18:37 quinn
213 * Revision 1.4 1995/11/21 13:14:49 quinn
214 * Fixed end-of-data-field problem (maybe).
216 * Revision 1.3 1995/11/15 19:13:09 adam
217 * Work on record management.
223 #include <sys/types.h>
234 #define GRS_MAX_WORD 512
240 struct grs_handler *next;
243 struct grs_handlers {
244 struct grs_handler *handlers;
247 static int read_grs_type (struct grs_handlers *h,
248 struct grs_read_info *p, const char *type,
251 struct grs_handler *gh = h->handlers;
252 const char *cp = strchr (type, '.');
254 if (cp == NULL || cp == type)
256 cp = strlen(type) + type;
260 strcpy (p->type, cp+1);
261 for (gh = h->handlers; gh; gh = gh->next)
263 if (!memcmp (type, gh->type->type, cp-type))
268 gh->clientData = (*gh->type->init)();
270 p->clientData = gh->clientData;
271 *root = (gh->type->read)(p);
272 gh->clientData = p->clientData;
279 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
281 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
282 gh->next = h->handlers;
289 static void *grs_init(RecType recType)
291 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
294 grs_add_handler (h, recTypeGrs_sgml);
295 grs_add_handler (h, recTypeGrs_regx);
297 grs_add_handler (h, recTypeGrs_tcl);
299 grs_add_handler (h, recTypeGrs_marc);
303 static void grs_destroy(void *clientData)
305 struct grs_handlers *h = (struct grs_handlers *) clientData;
306 struct grs_handler *gh = h->handlers, *gh_next;
311 (*gh->type->destroy)(gh->clientData);
318 static void index_tag (data1_node *par, data1_node *n,
319 struct recExtractCtrl *p, int level, RecWord *wrd)
321 data1_termlist *tlist = 0;
322 data1_datatype dtype = DATA1K_string;
324 * cycle up towards the root until we find a tag with an att..
325 * this has the effect of indexing locally defined tags with
326 * the attribute of their ancestor in the record.
329 while (!par->u.tag.element)
330 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
332 if (!par || !(tlist = par->u.tag.element->termlists))
334 if (par->u.tag.element->tag)
335 dtype = par->u.tag.element->tag->kind;
337 for (; tlist; tlist = tlist->next)
340 /* consider source */
343 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
345 wrd->string = n->u.data.data;
346 wrd->length = n->u.data.len;
348 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
349 n->which == DATA1N_tag)
351 data1_xattr *p = n->u.tag.attributes;
352 while (p && strcmp (p->name, xattr))
356 wrd->string = p->value;
357 wrd->length = strlen(p->value);
362 if (p->flagShowRecords)
365 printf("%*sIdx: [%s]", (level + 1) * 4, "",
367 printf("%s:%s [%d] %s",
368 tlist->att->parent->name,
369 tlist->att->name, tlist->att->value,
372 for (i = 0; i<wrd->length && i < 8; i++)
373 fputc (wrd->string[i], stdout);
377 fputc ('\n', stdout);
381 wrd->reg_type = *tlist->structure;
382 wrd->attrSet = (int) (tlist->att->parent->reference);
383 wrd->attrUse = tlist->att->locals->local;
390 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
393 (*p->init)(p, &wrd); /* set defaults */
394 for (; n; n = n->next)
396 if (p->flagShowRecords) /* display element description to user */
398 if (n->which == DATA1N_root)
400 printf("%*s", level * 4, "");
401 printf("Record type: '%s'\n", n->u.root.absyn->name);
403 else if (n->which == DATA1N_tag)
407 printf("%*s", level * 4, "");
408 if (!(e = n->u.tag.element))
409 printf("Local tag: '%s'\n", n->u.tag.tag);
412 printf("Elm: '%s' ", e->name);
415 data1_tag *t = e->tag;
417 printf("TagNam: '%s' ", t->names->name);
420 printf("%s[%d],", t->tagset->name, t->tagset->type);
423 if (t->which == DATA1T_numeric)
424 printf("%d)", t->value.numeric);
426 printf("'%s')", t->value.string);
434 if (dumpkeys(n->child, p, level + 1) < 0)
437 if (n->which == DATA1N_tag)
439 index_tag (n, n, p, level, &wrd);
442 if (n->which == DATA1N_data)
444 data1_node *par = get_parent_tag(p->dh, n);
446 if (p->flagShowRecords)
448 printf("%*s", level * 4, "");
450 if (n->u.data.len > 32)
451 printf("'%.24s ... %.6s'\n", n->u.data.data,
452 n->u.data.data + n->u.data.len-6);
453 else if (n->u.data.len > 0)
454 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
460 index_tag (par, n, p, level, &wrd);
462 if (p->flagShowRecords && n->which == DATA1N_root)
464 printf("%*s-------------\n\n", level * 4, "");
470 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
473 int oidtmp[OID_SIZE];
475 oe.proto = PROTO_Z3950;
476 oe.oclass = CLASS_SCHEMA;
477 oe.value = n->u.root.absyn->reference;
479 if ((oid_ent_to_oid (&oe, oidtmp)))
480 (*p->schemaAdd)(p, oidtmp);
482 return dumpkeys(n, p, 0);
485 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
489 struct grs_read_info gri;
491 int oidtmp[OID_SIZE];
493 gri.readf = p->readf;
494 gri.seekf = p->seekf;
495 gri.tellf = p->tellf;
498 gri.offset = p->offset;
502 if (read_grs_type (h, &gri, p->subType, &n))
503 return RECCTRL_EXTRACT_ERROR;
505 return RECCTRL_EXTRACT_EOF;
506 oe.proto = PROTO_Z3950;
507 oe.oclass = CLASS_SCHEMA;
508 if (!n->u.root.absyn)
509 return RECCTRL_EXTRACT_ERROR;
510 oe.value = n->u.root.absyn->reference;
511 if ((oid_ent_to_oid (&oe, oidtmp)))
512 (*p->schemaAdd)(p, oidtmp);
514 if (dumpkeys(n, p, 0) < 0)
516 data1_free_tree(p->dh, n);
517 return RECCTRL_EXTRACT_ERROR;
519 data1_free_tree(p->dh, n);
520 return RECCTRL_EXTRACT_OK;
523 static int grs_extract(void *clientData, struct recExtractCtrl *p)
526 NMEM mem = nmem_create ();
527 struct grs_handlers *h = (struct grs_handlers *) clientData;
529 ret = grs_extract_sub(h, p, mem);
535 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
537 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
539 data1_esetname *eset;
545 case Z_RecordComp_simple:
546 if (c->u.simple->which != Z_ElementSetNames_generic)
547 return 26; /* only generic form supported. Fix this later */
548 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
549 c->u.simple->u.generic)))
551 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
552 return 25; /* invalid esetname */
554 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
555 c->u.simple->u.generic);
558 case Z_RecordComp_complex:
559 if (c->u.complex->generic)
561 /* insert check for schema */
562 if ((p = c->u.complex->generic->elementSpec))
566 case Z_ElementSpec_elementSetName:
568 data1_getesetbyname(dh, n->u.root.absyn,
569 p->u.elementSetName)))
571 logf(LOG_LOG, "Unknown esetname '%s'",
572 p->u.elementSetName);
573 return 25; /* invalid esetname */
575 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
576 p->u.elementSetName);
579 case Z_ElementSpec_externalSpec:
580 if (p->u.externalSpec->which == Z_External_espec1)
582 logf(LOG_DEBUG, "Got Espec-1");
583 espec = p->u.externalSpec-> u.espec1;
587 logf(LOG_LOG, "Unknown external espec.");
588 return 25; /* bad. what is proper diagnostic? */
599 logf (LOG_DEBUG, "Element: Espec-1 match");
600 return data1_doespec1(dh, n, espec);
604 logf (LOG_DEBUG, "Element: all match");
609 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
611 data1_node *node = 0, *onode = 0;
614 int res, selected = 0;
616 struct grs_read_info gri;
618 struct grs_handlers *h = (struct grs_handlers *) clientData;
619 int requested_schema = VAL_NONE;
622 gri.readf = p->readf;
623 gri.seekf = p->seekf;
624 gri.tellf = p->tellf;
631 logf (LOG_DEBUG, "grs_retrieve");
632 if (read_grs_type (h, &gri, p->subType, &node))
645 data1_pr_tree (p->dh, node, stdout);
647 logf (LOG_DEBUG, "grs_retrieve: size");
648 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
651 dnew->u.data.what = DATA1I_text;
652 dnew->u.data.data = dnew->lbuf;
653 sprintf(dnew->u.data.data, "%d", p->recordSize);
654 dnew->u.data.len = strlen(dnew->u.data.data);
657 tagname = res_get_def(p->res, "tagrank", "rank");
658 if (strcmp(tagname, "0") && p->score >= 0 &&
659 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
661 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
662 dnew->u.data.what = DATA1I_num;
663 dnew->u.data.data = dnew->lbuf;
664 sprintf(dnew->u.data.data, "%d", p->score);
665 dnew->u.data.len = strlen(dnew->u.data.data);
668 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
669 if (strcmp(tagname, "0") && p->localno > 0 &&
670 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
672 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
673 dnew->u.data.what = DATA1I_text;
674 dnew->u.data.data = dnew->lbuf;
675 sprintf(dnew->u.data.data, "%d", p->localno);
676 dnew->u.data.len = strlen(dnew->u.data.data);
679 if (p->comp && p->comp->which == Z_RecordComp_complex &&
680 p->comp->u.complex->generic &&
681 p->comp->u.complex->generic->schema)
683 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
685 requested_schema = oe->value;
688 /* If schema has been specified, map if possible, then check that
689 * we got the right one
691 if (requested_schema != VAL_NONE)
693 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
694 for (map = node->u.root.absyn->maptabs; map; map = map->next)
696 if (map->target_absyn_ref == requested_schema)
699 if (!(node = data1_map_record(p->dh, onode, map, mem)))
708 if (node->u.root.absyn &&
709 requested_schema != node->u.root.absyn->reference)
717 * Does the requested format match a known syntax-mapping? (this reflects
718 * the overlap of schema and formatting which is inherent in the MARC
721 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
722 for (map = node->u.root.absyn->maptabs; map; map = map->next)
724 if (map->target_absyn_ref == p->input_format)
727 if (!(node = data1_map_record(p->dh, onode, map, mem)))
736 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
737 if (node->u.root.absyn &&
738 node->u.root.absyn->reference != VAL_NONE &&
739 p->input_format == VAL_GRS1)
743 int oidtmp[OID_SIZE];
745 oe.proto = PROTO_Z3950;
746 oe.oclass = CLASS_SCHEMA;
747 oe.value = node->u.root.absyn->reference;
749 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
752 data1_handle dh = p->dh;
756 for (ii = oid; *ii >= 0; ii++)
760 sprintf(p, "%d", *ii);
765 if ((dnew = data1_insert_taggeddata(dh, node, node,
766 "schemaIdentifier", mem)))
768 dnew->u.data.what = DATA1I_oid;
769 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
770 memcpy(dnew->u.data.data, tmp, p - tmp);
771 dnew->u.data.len = p - tmp;
776 logf (LOG_DEBUG, "grs_retrieve: element spec");
777 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
781 data1_free_tree(p->dh, onode);
782 data1_free_tree(p->dh, node);
786 else if (p->comp && !res)
790 data1_pr_tree (p->dh, node, stdout);
792 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
793 switch (p->output_format = (p->input_format != VAL_NONE ?
794 p->input_format : VAL_SUTRS))
796 data1_marctab *marctab;
800 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
805 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
806 memcpy (new_buf, p->rec_buf, p->rec_len);
807 p->rec_buf = new_buf;
812 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
814 p->diagnostic = 238; /* not available in requested syntax */
816 p->rec_len = (size_t) (-1);
819 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
823 p->rec_len = (size_t) (-1);
826 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
830 p->rec_len = (size_t) (-1);
833 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
838 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
839 memcpy (new_buf, p->rec_buf, p->rec_len);
840 p->rec_buf = new_buf;
844 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
849 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
850 memcpy (new_buf, p->rec_buf, p->rec_len);
851 p->rec_buf = new_buf;
855 if (!node->u.root.absyn)
860 for (marctab = node->u.root.absyn->marc; marctab;
861 marctab = marctab->next)
862 if (marctab->reference == p->input_format)
869 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
870 selected, &p->rec_len)))
874 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
875 memcpy (new_buf, p->rec_buf, p->rec_len);
876 p->rec_buf = new_buf;
880 data1_free_tree(p->dh, node);
882 data1_free_tree(p->dh, onode);
887 static struct recType grs_type =
896 RecType recTypeGrs = &grs_type;