2 * Copyright (C) 1994-2001, Index Data
6 * Revision 1.42 2002-02-20 17:30:01 adam
7 * Work on new API. Locking system re-implemented
9 * Revision 1.41 2001/05/22 21:01:47 adam
10 * Removed print of data1 tree on stdout so that inetd works again.
12 * Revision 1.40 2001/03/29 21:31:31 adam
13 * Fixed "record begin" for Tcl filter.
15 * Revision 1.39 2000/12/05 19:09:15 adam
16 * Fixed problem where indexer could crash if abstract syntax was undefined.
18 * Revision 1.38 2000/12/05 14:44:58 adam
19 * Fixed minor bug that could cause zmbol to break it data were emitted
20 * with not parent tags.
22 * Revision 1.37 2000/12/05 12:22:53 adam
23 * Termlist source implemented (so that we can index values of XML/SGML
26 * Revision 1.36 2000/12/05 10:01:44 adam
27 * Fixed bug regarding user-defined attribute sets.
29 * Revision 1.35 2000/11/29 15:21:31 adam
30 * Fixed problem with passwd db.
32 * Revision 1.34 2000/02/25 13:24:49 adam
33 * Fixed bug regarding pointer conversion that showed up on OSF V5.
35 * Revision 1.33 1999/11/30 13:48:04 adam
36 * Improved installation. Updated for inclusion of YAZ header files.
38 * Revision 1.32 1999/09/07 07:19:21 adam
39 * Work on character mapping. Implemented replace rules.
41 * Revision 1.31 1999/07/14 10:56:43 adam
42 * Fixed potential memory leak.
44 * Revision 1.30 1999/07/06 12:26:41 adam
45 * Retrieval handler obeys schema and handles XML transfer syntax.
47 * Revision 1.29 1999/05/26 07:49:14 adam
50 * Revision 1.28 1999/05/21 12:00:17 adam
51 * Better diagnostics for extraction process.
53 * Revision 1.27 1999/05/20 12:57:18 adam
54 * Implemented TCL filter. Updated recctrl system.
56 * Revision 1.26 1999/03/02 16:15:44 quinn
57 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
59 * Revision 1.25 1999/02/18 15:01:26 adam
62 * Revision 1.24 1999/02/02 14:51:28 adam
63 * Updated WIN32 code specific sections. Changed header.
65 * Revision 1.23 1998/10/18 07:51:10 adam
66 * Changed one logf call.
68 * Revision 1.22 1998/10/16 08:14:37 adam
69 * Updated record control system.
71 * Revision 1.21 1998/07/01 09:16:10 adam
72 * Element localno only added when it's greater than 0.
74 * Revision 1.20 1998/05/20 10:12:26 adam
75 * Implemented automatic EXPLAIN database maintenance.
76 * Modified Zebra to work with ASN.1 compiled version of YAZ.
78 * Revision 1.19 1998/03/11 11:19:05 adam
79 * Changed the way sequence numbers are generated.
81 * Revision 1.18 1998/03/05 08:41:31 adam
84 * Revision 1.17 1998/02/10 12:03:06 adam
87 * Revision 1.16 1998/01/29 13:38:17 adam
88 * Fixed problem with mapping to record with unknown schema.
90 * Revision 1.15 1998/01/26 10:37:57 adam
93 * Revision 1.14 1997/11/06 11:41:01 adam
94 * Implemented "begin variant" for the sgml.regx filter.
96 * Revision 1.13 1997/10/31 12:35:44 adam
97 * Added a few log statements.
99 * Revision 1.12 1997/10/29 12:02:22 adam
100 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
102 * Revision 1.11 1997/10/27 14:34:00 adam
103 * Work on generic character mapping depending on "structure" field
104 * in abstract syntax file.
106 * Revision 1.10 1997/09/18 08:59:21 adam
107 * Extra generic handle for the character mapping routines.
109 * Revision 1.9 1997/09/17 12:19:21 adam
110 * Zebra version corresponds to YAZ version 1.4.
111 * Changed Zebra server so that it doesn't depend on global common_resource.
113 * Revision 1.8 1997/09/09 13:38:14 adam
114 * Partial port to WIN95/NT.
116 * Revision 1.7 1997/09/05 15:30:10 adam
117 * Changed prototype for chr_map_input - added const.
118 * Added support for C++, headers uses extern "C" for public definitions.
120 * Revision 1.6 1997/09/04 13:54:40 adam
121 * Added MARC filter - type grs.marc.<syntax> where syntax refers
122 * to abstract syntax. New method tellf in retrieve/extract method.
124 * Revision 1.5 1997/07/15 16:29:03 adam
125 * Initialized dummy variable to keep checker gcc happy.
127 * Revision 1.4 1997/04/30 08:56:08 quinn
130 * Revision 1.2 1996/10/11 16:06:43 quinn
131 * Revision 1.3 1997/02/24 10:41:50 adam
132 * Cleanup of code and commented out the "end element-end-record" code.
134 * Revision 1.2 1996/10/11 16:06:43 quinn
135 * Fixed arguments to nodetogr
137 * Revision 1.1 1996/10/11 10:57:25 adam
138 * New module recctrl. Used to manage records (extract/retrieval).
140 * Revision 1.29 1996/10/08 10:30:21 quinn
141 * Fixed type mismatch
143 * Revision 1.28 1996/10/07 16:06:40 quinn
146 * Revision 1.27 1996/06/11 10:54:12 quinn
149 * Revision 1.26 1996/06/06 12:08:45 quinn
150 * Added showRecord function
152 * Revision 1.25 1996/06/04 14:18:53 quinn
155 * Revision 1.24 1996/06/04 13:27:54 quinn
156 * More work on charmapping
158 * Revision 1.23 1996/06/04 10:19:01 adam
159 * Minor changes - removed include of ctype.h.
161 * Revision 1.22 1996/06/03 10:15:27 quinn
162 * Various character-mapping.
164 * Revision 1.21 1996/05/31 13:27:24 quinn
165 * Character-conversion in phrases, too.
167 * Revision 1.19 1996/05/16 15:31:14 quinn
170 * Revision 1.18 1996/05/09 07:28:56 quinn
171 * Work towards phrases and multiple registers
173 * Revision 1.17 1996/05/01 13:46:37 adam
174 * First work on multiple records in one file.
175 * New option, -offset, to the "unread" command in the filter module.
177 * Revision 1.16 1996/01/17 14:57:54 adam
178 * Prototype changed for reader functions in extract/retrieve. File
179 * is identified by 'void *' instead of 'int.
181 * Revision 1.15 1996/01/08 19:15:47 adam
182 * New input filter that works!
184 * Revision 1.14 1995/12/15 12:36:11 adam
185 * Retrieval calls data1_read_regx when subType is specified.
187 * Revision 1.13 1995/12/15 12:24:43 quinn
188 * *** empty log message ***
190 * Revision 1.12 1995/12/15 12:20:28 quinn
191 * *** empty log message ***
193 * Revision 1.11 1995/12/15 12:07:57 quinn
194 * Changed extraction strategy.
196 * Revision 1.10 1995/12/14 11:10:48 quinn
199 * Revision 1.9 1995/12/13 17:14:05 quinn
200 * *** empty log message ***
202 * Revision 1.8 1995/12/13 15:33:18 quinn
203 * *** empty log message ***
205 * Revision 1.7 1995/12/13 13:45:39 quinn
206 * Changed data1 to use nmem.
208 * Revision 1.6 1995/12/04 14:22:30 adam
209 * Extra arg to recType_byName.
210 * Started work on new regular expression parsed input to
211 * structured records.
213 * Revision 1.5 1995/11/28 14:18:37 quinn
216 * Revision 1.4 1995/11/21 13:14:49 quinn
217 * Fixed end-of-data-field problem (maybe).
219 * Revision 1.3 1995/11/15 19:13:09 adam
220 * Work on record management.
226 #include <sys/types.h>
237 #define GRS_MAX_WORD 512
243 struct grs_handler *next;
246 struct grs_handlers {
247 struct grs_handler *handlers;
250 static int read_grs_type (struct grs_handlers *h,
251 struct grs_read_info *p, const char *type,
254 struct grs_handler *gh = h->handlers;
255 const char *cp = strchr (type, '.');
257 if (cp == NULL || cp == type)
259 cp = strlen(type) + type;
263 strcpy (p->type, cp+1);
264 for (gh = h->handlers; gh; gh = gh->next)
266 if (!memcmp (type, gh->type->type, cp-type))
271 gh->clientData = (*gh->type->init)();
273 p->clientData = gh->clientData;
274 *root = (gh->type->read)(p);
275 gh->clientData = p->clientData;
282 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
284 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
285 gh->next = h->handlers;
292 static void *grs_init(RecType recType)
294 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
297 grs_add_handler (h, recTypeGrs_sgml);
298 grs_add_handler (h, recTypeGrs_regx);
300 grs_add_handler (h, recTypeGrs_tcl);
302 grs_add_handler (h, recTypeGrs_marc);
306 static void grs_destroy(void *clientData)
308 struct grs_handlers *h = (struct grs_handlers *) clientData;
309 struct grs_handler *gh = h->handlers, *gh_next;
314 (*gh->type->destroy)(gh->clientData);
321 static void index_tag (data1_node *par, data1_node *n,
322 struct recExtractCtrl *p, int level, RecWord *wrd)
324 data1_termlist *tlist = 0;
325 data1_datatype dtype = DATA1K_string;
327 * cycle up towards the root until we find a tag with an att..
328 * this has the effect of indexing locally defined tags with
329 * the attribute of their ancestor in the record.
332 while (!par->u.tag.element)
333 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
335 if (!par || !(tlist = par->u.tag.element->termlists))
337 if (par->u.tag.element->tag)
338 dtype = par->u.tag.element->tag->kind;
340 for (; tlist; tlist = tlist->next)
343 /* consider source */
346 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
348 wrd->string = n->u.data.data;
349 wrd->length = n->u.data.len;
351 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
352 n->which == DATA1N_tag)
354 data1_xattr *p = n->u.tag.attributes;
355 while (p && strcmp (p->name, xattr))
359 wrd->string = p->value;
360 wrd->length = strlen(p->value);
365 if (p->flagShowRecords)
368 printf("%*sIdx: [%s]", (level + 1) * 4, "",
370 printf("%s:%s [%d] %s",
371 tlist->att->parent->name,
372 tlist->att->name, tlist->att->value,
375 for (i = 0; i<wrd->length && i < 8; i++)
376 fputc (wrd->string[i], stdout);
380 fputc ('\n', stdout);
384 wrd->reg_type = *tlist->structure;
385 wrd->attrSet = (int) (tlist->att->parent->reference);
386 wrd->attrUse = tlist->att->locals->local;
393 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
396 (*p->init)(p, &wrd); /* set defaults */
397 for (; n; n = n->next)
399 if (p->flagShowRecords) /* display element description to user */
401 if (n->which == DATA1N_root)
403 printf("%*s", level * 4, "");
404 printf("Record type: '%s'\n", n->u.root.absyn->name);
406 else if (n->which == DATA1N_tag)
410 printf("%*s", level * 4, "");
411 if (!(e = n->u.tag.element))
412 printf("Local tag: '%s'\n", n->u.tag.tag);
415 printf("Elm: '%s' ", e->name);
418 data1_tag *t = e->tag;
420 printf("TagNam: '%s' ", t->names->name);
423 printf("%s[%d],", t->tagset->name, t->tagset->type);
426 if (t->which == DATA1T_numeric)
427 printf("%d)", t->value.numeric);
429 printf("'%s')", t->value.string);
437 if (dumpkeys(n->child, p, level + 1) < 0)
440 if (n->which == DATA1N_tag)
442 index_tag (n, n, p, level, &wrd);
445 if (n->which == DATA1N_data)
447 data1_node *par = get_parent_tag(p->dh, n);
449 if (p->flagShowRecords)
451 printf("%*s", level * 4, "");
453 if (n->u.data.len > 32)
454 printf("'%.24s ... %.6s'\n", n->u.data.data,
455 n->u.data.data + n->u.data.len-6);
456 else if (n->u.data.len > 0)
457 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
463 index_tag (par, n, p, level, &wrd);
465 if (p->flagShowRecords && n->which == DATA1N_root)
467 printf("%*s-------------\n\n", level * 4, "");
473 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
476 int oidtmp[OID_SIZE];
478 oe.proto = PROTO_Z3950;
479 oe.oclass = CLASS_SCHEMA;
480 oe.value = n->u.root.absyn->reference;
482 if ((oid_ent_to_oid (&oe, oidtmp)))
483 (*p->schemaAdd)(p, oidtmp);
485 return dumpkeys(n, p, 0);
488 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
492 struct grs_read_info gri;
494 int oidtmp[OID_SIZE];
496 gri.readf = p->readf;
497 gri.seekf = p->seekf;
498 gri.tellf = p->tellf;
501 gri.offset = p->offset;
505 if (read_grs_type (h, &gri, p->subType, &n))
506 return RECCTRL_EXTRACT_ERROR;
508 return RECCTRL_EXTRACT_EOF;
509 oe.proto = PROTO_Z3950;
510 oe.oclass = CLASS_SCHEMA;
511 if (!n->u.root.absyn)
512 return RECCTRL_EXTRACT_ERROR;
513 oe.value = n->u.root.absyn->reference;
514 if ((oid_ent_to_oid (&oe, oidtmp)))
515 (*p->schemaAdd)(p, oidtmp);
518 data1_pr_tree (p->dh, n, stdout);
520 if (dumpkeys(n, p, 0) < 0)
522 data1_free_tree(p->dh, n);
523 return RECCTRL_EXTRACT_ERROR;
525 data1_free_tree(p->dh, n);
526 return RECCTRL_EXTRACT_OK;
529 static int grs_extract(void *clientData, struct recExtractCtrl *p)
532 NMEM mem = nmem_create ();
533 struct grs_handlers *h = (struct grs_handlers *) clientData;
535 ret = grs_extract_sub(h, p, mem);
541 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
543 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
545 data1_esetname *eset;
551 case Z_RecordComp_simple:
552 if (c->u.simple->which != Z_ElementSetNames_generic)
553 return 26; /* only generic form supported. Fix this later */
554 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
555 c->u.simple->u.generic)))
557 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
558 return 25; /* invalid esetname */
560 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
561 c->u.simple->u.generic);
564 case Z_RecordComp_complex:
565 if (c->u.complex->generic)
567 /* insert check for schema */
568 if ((p = c->u.complex->generic->elementSpec))
572 case Z_ElementSpec_elementSetName:
574 data1_getesetbyname(dh, n->u.root.absyn,
575 p->u.elementSetName)))
577 logf(LOG_LOG, "Unknown esetname '%s'",
578 p->u.elementSetName);
579 return 25; /* invalid esetname */
581 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
582 p->u.elementSetName);
585 case Z_ElementSpec_externalSpec:
586 if (p->u.externalSpec->which == Z_External_espec1)
588 logf(LOG_DEBUG, "Got Espec-1");
589 espec = p->u.externalSpec-> u.espec1;
593 logf(LOG_LOG, "Unknown external espec.");
594 return 25; /* bad. what is proper diagnostic? */
605 logf (LOG_DEBUG, "Element: Espec-1 match");
606 return data1_doespec1(dh, n, espec);
610 logf (LOG_DEBUG, "Element: all match");
615 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
617 data1_node *node = 0, *onode = 0;
620 int res, selected = 0;
622 struct grs_read_info gri;
624 struct grs_handlers *h = (struct grs_handlers *) clientData;
625 int requested_schema = VAL_NONE;
628 gri.readf = p->readf;
629 gri.seekf = p->seekf;
630 gri.tellf = p->tellf;
637 logf (LOG_DEBUG, "grs_retrieve");
638 if (read_grs_type (h, &gri, p->subType, &node))
651 data1_pr_tree (p->dh, node, stdout);
653 logf (LOG_DEBUG, "grs_retrieve: size");
654 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
657 dnew->u.data.what = DATA1I_text;
658 dnew->u.data.data = dnew->lbuf;
659 sprintf(dnew->u.data.data, "%d", p->recordSize);
660 dnew->u.data.len = strlen(dnew->u.data.data);
663 tagname = res_get_def(p->res, "tagrank", "rank");
664 if (strcmp(tagname, "0") && p->score >= 0 &&
665 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
667 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
668 dnew->u.data.what = DATA1I_num;
669 dnew->u.data.data = dnew->lbuf;
670 sprintf(dnew->u.data.data, "%d", p->score);
671 dnew->u.data.len = strlen(dnew->u.data.data);
674 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
675 if (strcmp(tagname, "0") && p->localno > 0 &&
676 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
678 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
679 dnew->u.data.what = DATA1I_text;
680 dnew->u.data.data = dnew->lbuf;
681 sprintf(dnew->u.data.data, "%d", p->localno);
682 dnew->u.data.len = strlen(dnew->u.data.data);
685 if (p->comp && p->comp->which == Z_RecordComp_complex &&
686 p->comp->u.complex->generic &&
687 p->comp->u.complex->generic->schema)
689 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
691 requested_schema = oe->value;
694 /* If schema has been specified, map if possible, then check that
695 * we got the right one
697 if (requested_schema != VAL_NONE)
699 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
700 for (map = node->u.root.absyn->maptabs; map; map = map->next)
702 if (map->target_absyn_ref == requested_schema)
705 if (!(node = data1_map_record(p->dh, onode, map, mem)))
714 if (node->u.root.absyn &&
715 requested_schema != node->u.root.absyn->reference)
723 * Does the requested format match a known syntax-mapping? (this reflects
724 * the overlap of schema and formatting which is inherent in the MARC
727 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
728 for (map = node->u.root.absyn->maptabs; map; map = map->next)
730 if (map->target_absyn_ref == p->input_format)
733 if (!(node = data1_map_record(p->dh, onode, map, mem)))
742 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
743 if (node->u.root.absyn &&
744 node->u.root.absyn->reference != VAL_NONE &&
745 p->input_format == VAL_GRS1)
749 int oidtmp[OID_SIZE];
751 oe.proto = PROTO_Z3950;
752 oe.oclass = CLASS_SCHEMA;
753 oe.value = node->u.root.absyn->reference;
755 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
758 data1_handle dh = p->dh;
762 for (ii = oid; *ii >= 0; ii++)
766 sprintf(p, "%d", *ii);
771 if ((dnew = data1_insert_taggeddata(dh, node, node,
772 "schemaIdentifier", mem)))
774 dnew->u.data.what = DATA1I_oid;
775 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
776 memcpy(dnew->u.data.data, tmp, p - tmp);
777 dnew->u.data.len = p - tmp;
782 logf (LOG_DEBUG, "grs_retrieve: element spec");
783 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
787 data1_free_tree(p->dh, onode);
788 data1_free_tree(p->dh, node);
792 else if (p->comp && !res)
796 data1_pr_tree (p->dh, node, stdout);
798 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
799 switch (p->output_format = (p->input_format != VAL_NONE ?
800 p->input_format : VAL_SUTRS))
802 data1_marctab *marctab;
806 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
811 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
812 memcpy (new_buf, p->rec_buf, p->rec_len);
813 p->rec_buf = new_buf;
818 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
820 p->diagnostic = 238; /* not available in requested syntax */
822 p->rec_len = (size_t) (-1);
825 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
829 p->rec_len = (size_t) (-1);
832 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
836 p->rec_len = (size_t) (-1);
839 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
844 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
845 memcpy (new_buf, p->rec_buf, p->rec_len);
846 p->rec_buf = new_buf;
850 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
855 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
856 memcpy (new_buf, p->rec_buf, p->rec_len);
857 p->rec_buf = new_buf;
861 if (!node->u.root.absyn)
866 for (marctab = node->u.root.absyn->marc; marctab;
867 marctab = marctab->next)
868 if (marctab->reference == p->input_format)
875 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
876 selected, &p->rec_len)))
880 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
881 memcpy (new_buf, p->rec_buf, p->rec_len);
882 p->rec_buf = new_buf;
886 data1_free_tree(p->dh, node);
888 data1_free_tree(p->dh, onode);
893 static struct recType grs_type =
902 RecType recTypeGrs = &grs_type;