2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.39 2000-12-05 19:09:15 adam
8 * Fixed problem where indexer could crash if abstract syntax was undefined.
10 * Revision 1.38 2000/12/05 14:44:58 adam
11 * Fixed minor bug that could cause zmbol to break it data were emitted
12 * with not parent tags.
14 * Revision 1.37 2000/12/05 12:22:53 adam
15 * Termlist source implemented (so that we can index values of XML/SGML
18 * Revision 1.36 2000/12/05 10:01:44 adam
19 * Fixed bug regarding user-defined attribute sets.
21 * Revision 1.35 2000/11/29 15:21:31 adam
22 * Fixed problem with passwd db.
24 * Revision 1.34 2000/02/25 13:24:49 adam
25 * Fixed bug regarding pointer conversion that showed up on OSF V5.
27 * Revision 1.33 1999/11/30 13:48:04 adam
28 * Improved installation. Updated for inclusion of YAZ header files.
30 * Revision 1.32 1999/09/07 07:19:21 adam
31 * Work on character mapping. Implemented replace rules.
33 * Revision 1.31 1999/07/14 10:56:43 adam
34 * Fixed potential memory leak.
36 * Revision 1.30 1999/07/06 12:26:41 adam
37 * Retrieval handler obeys schema and handles XML transfer syntax.
39 * Revision 1.29 1999/05/26 07:49:14 adam
42 * Revision 1.28 1999/05/21 12:00:17 adam
43 * Better diagnostics for extraction process.
45 * Revision 1.27 1999/05/20 12:57:18 adam
46 * Implemented TCL filter. Updated recctrl system.
48 * Revision 1.26 1999/03/02 16:15:44 quinn
49 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
51 * Revision 1.25 1999/02/18 15:01:26 adam
54 * Revision 1.24 1999/02/02 14:51:28 adam
55 * Updated WIN32 code specific sections. Changed header.
57 * Revision 1.23 1998/10/18 07:51:10 adam
58 * Changed one logf call.
60 * Revision 1.22 1998/10/16 08:14:37 adam
61 * Updated record control system.
63 * Revision 1.21 1998/07/01 09:16:10 adam
64 * Element localno only added when it's greater than 0.
66 * Revision 1.20 1998/05/20 10:12:26 adam
67 * Implemented automatic EXPLAIN database maintenance.
68 * Modified Zebra to work with ASN.1 compiled version of YAZ.
70 * Revision 1.19 1998/03/11 11:19:05 adam
71 * Changed the way sequence numbers are generated.
73 * Revision 1.18 1998/03/05 08:41:31 adam
76 * Revision 1.17 1998/02/10 12:03:06 adam
79 * Revision 1.16 1998/01/29 13:38:17 adam
80 * Fixed problem with mapping to record with unknown schema.
82 * Revision 1.15 1998/01/26 10:37:57 adam
85 * Revision 1.14 1997/11/06 11:41:01 adam
86 * Implemented "begin variant" for the sgml.regx filter.
88 * Revision 1.13 1997/10/31 12:35:44 adam
89 * Added a few log statements.
91 * Revision 1.12 1997/10/29 12:02:22 adam
92 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
94 * Revision 1.11 1997/10/27 14:34:00 adam
95 * Work on generic character mapping depending on "structure" field
96 * in abstract syntax file.
98 * Revision 1.10 1997/09/18 08:59:21 adam
99 * Extra generic handle for the character mapping routines.
101 * Revision 1.9 1997/09/17 12:19:21 adam
102 * Zebra version corresponds to YAZ version 1.4.
103 * Changed Zebra server so that it doesn't depend on global common_resource.
105 * Revision 1.8 1997/09/09 13:38:14 adam
106 * Partial port to WIN95/NT.
108 * Revision 1.7 1997/09/05 15:30:10 adam
109 * Changed prototype for chr_map_input - added const.
110 * Added support for C++, headers uses extern "C" for public definitions.
112 * Revision 1.6 1997/09/04 13:54:40 adam
113 * Added MARC filter - type grs.marc.<syntax> where syntax refers
114 * to abstract syntax. New method tellf in retrieve/extract method.
116 * Revision 1.5 1997/07/15 16:29:03 adam
117 * Initialized dummy variable to keep checker gcc happy.
119 * Revision 1.4 1997/04/30 08:56:08 quinn
122 * Revision 1.2 1996/10/11 16:06:43 quinn
123 * Revision 1.3 1997/02/24 10:41:50 adam
124 * Cleanup of code and commented out the "end element-end-record" code.
126 * Revision 1.2 1996/10/11 16:06:43 quinn
127 * Fixed arguments to nodetogr
129 * Revision 1.1 1996/10/11 10:57:25 adam
130 * New module recctrl. Used to manage records (extract/retrieval).
132 * Revision 1.29 1996/10/08 10:30:21 quinn
133 * Fixed type mismatch
135 * Revision 1.28 1996/10/07 16:06:40 quinn
138 * Revision 1.27 1996/06/11 10:54:12 quinn
141 * Revision 1.26 1996/06/06 12:08:45 quinn
142 * Added showRecord function
144 * Revision 1.25 1996/06/04 14:18:53 quinn
147 * Revision 1.24 1996/06/04 13:27:54 quinn
148 * More work on charmapping
150 * Revision 1.23 1996/06/04 10:19:01 adam
151 * Minor changes - removed include of ctype.h.
153 * Revision 1.22 1996/06/03 10:15:27 quinn
154 * Various character-mapping.
156 * Revision 1.21 1996/05/31 13:27:24 quinn
157 * Character-conversion in phrases, too.
159 * Revision 1.19 1996/05/16 15:31:14 quinn
162 * Revision 1.18 1996/05/09 07:28:56 quinn
163 * Work towards phrases and multiple registers
165 * Revision 1.17 1996/05/01 13:46:37 adam
166 * First work on multiple records in one file.
167 * New option, -offset, to the "unread" command in the filter module.
169 * Revision 1.16 1996/01/17 14:57:54 adam
170 * Prototype changed for reader functions in extract/retrieve. File
171 * is identified by 'void *' instead of 'int.
173 * Revision 1.15 1996/01/08 19:15:47 adam
174 * New input filter that works!
176 * Revision 1.14 1995/12/15 12:36:11 adam
177 * Retrieval calls data1_read_regx when subType is specified.
179 * Revision 1.13 1995/12/15 12:24:43 quinn
180 * *** empty log message ***
182 * Revision 1.12 1995/12/15 12:20:28 quinn
183 * *** empty log message ***
185 * Revision 1.11 1995/12/15 12:07:57 quinn
186 * Changed extraction strategy.
188 * Revision 1.10 1995/12/14 11:10:48 quinn
191 * Revision 1.9 1995/12/13 17:14:05 quinn
192 * *** empty log message ***
194 * Revision 1.8 1995/12/13 15:33:18 quinn
195 * *** empty log message ***
197 * Revision 1.7 1995/12/13 13:45:39 quinn
198 * Changed data1 to use nmem.
200 * Revision 1.6 1995/12/04 14:22:30 adam
201 * Extra arg to recType_byName.
202 * Started work on new regular expression parsed input to
203 * structured records.
205 * Revision 1.5 1995/11/28 14:18:37 quinn
208 * Revision 1.4 1995/11/21 13:14:49 quinn
209 * Fixed end-of-data-field problem (maybe).
211 * Revision 1.3 1995/11/15 19:13:09 adam
212 * Work on record management.
218 #include <sys/types.h>
229 #define GRS_MAX_WORD 512
235 struct grs_handler *next;
238 struct grs_handlers {
239 struct grs_handler *handlers;
242 static int read_grs_type (struct grs_handlers *h,
243 struct grs_read_info *p, const char *type,
246 struct grs_handler *gh = h->handlers;
247 const char *cp = strchr (type, '.');
249 if (cp == NULL || cp == type)
251 cp = strlen(type) + type;
255 strcpy (p->type, cp+1);
256 for (gh = h->handlers; gh; gh = gh->next)
258 if (!memcmp (type, gh->type->type, cp-type))
263 gh->clientData = (*gh->type->init)();
265 p->clientData = gh->clientData;
266 *root = (gh->type->read)(p);
267 gh->clientData = p->clientData;
274 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
276 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
277 gh->next = h->handlers;
284 static void *grs_init(RecType recType)
286 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
289 grs_add_handler (h, recTypeGrs_sgml);
290 grs_add_handler (h, recTypeGrs_regx);
292 grs_add_handler (h, recTypeGrs_tcl);
294 grs_add_handler (h, recTypeGrs_marc);
298 static void grs_destroy(void *clientData)
300 struct grs_handlers *h = (struct grs_handlers *) clientData;
301 struct grs_handler *gh = h->handlers, *gh_next;
306 (*gh->type->destroy)(gh->clientData);
313 static void index_tag (data1_node *par, data1_node *n,
314 struct recExtractCtrl *p, int level, RecWord *wrd)
316 data1_termlist *tlist = 0;
317 data1_datatype dtype = DATA1K_string;
319 * cycle up towards the root until we find a tag with an att..
320 * this has the effect of indexing locally defined tags with
321 * the attribute of their ancestor in the record.
324 while (!par->u.tag.element)
325 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
327 if (!par || !(tlist = par->u.tag.element->termlists))
329 if (par->u.tag.element->tag)
330 dtype = par->u.tag.element->tag->kind;
332 for (; tlist; tlist = tlist->next)
335 /* consider source */
338 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
340 wrd->string = n->u.data.data;
341 wrd->length = n->u.data.len;
343 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
344 n->which == DATA1N_tag)
346 data1_xattr *p = n->u.tag.attributes;
347 while (p && strcmp (p->name, xattr))
351 wrd->string = p->value;
352 wrd->length = strlen(p->value);
357 if (p->flagShowRecords)
360 printf("%*sIdx: [%s]", (level + 1) * 4, "",
362 printf("%s:%s [%d] %s",
363 tlist->att->parent->name,
364 tlist->att->name, tlist->att->value,
367 for (i = 0; i<wrd->length && i < 8; i++)
368 fputc (wrd->string[i], stdout);
372 fputc ('\n', stdout);
376 wrd->reg_type = *tlist->structure;
377 wrd->attrSet = (int) (tlist->att->parent->reference);
378 wrd->attrUse = tlist->att->locals->local;
385 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
388 (*p->init)(p, &wrd); /* set defaults */
389 for (; n; n = n->next)
391 if (p->flagShowRecords) /* display element description to user */
393 if (n->which == DATA1N_root)
395 printf("%*s", level * 4, "");
396 printf("Record type: '%s'\n", n->u.root.absyn->name);
398 else if (n->which == DATA1N_tag)
402 printf("%*s", level * 4, "");
403 if (!(e = n->u.tag.element))
404 printf("Local tag: '%s'\n", n->u.tag.tag);
407 printf("Elm: '%s' ", e->name);
410 data1_tag *t = e->tag;
412 printf("TagNam: '%s' ", t->names->name);
415 printf("%s[%d],", t->tagset->name, t->tagset->type);
418 if (t->which == DATA1T_numeric)
419 printf("%d)", t->value.numeric);
421 printf("'%s')", t->value.string);
429 if (dumpkeys(n->child, p, level + 1) < 0)
432 if (n->which == DATA1N_tag)
434 index_tag (n, n, p, level, &wrd);
437 if (n->which == DATA1N_data)
439 data1_node *par = get_parent_tag(p->dh, n);
441 if (p->flagShowRecords)
443 printf("%*s", level * 4, "");
445 if (n->u.data.len > 32)
446 printf("'%.24s ... %.6s'\n", n->u.data.data,
447 n->u.data.data + n->u.data.len-6);
448 else if (n->u.data.len > 0)
449 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
455 index_tag (par, n, p, level, &wrd);
457 if (p->flagShowRecords && n->which == DATA1N_root)
459 printf("%*s-------------\n\n", level * 4, "");
465 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
468 int oidtmp[OID_SIZE];
470 oe.proto = PROTO_Z3950;
471 oe.oclass = CLASS_SCHEMA;
472 oe.value = n->u.root.absyn->reference;
474 if ((oid_ent_to_oid (&oe, oidtmp)))
475 (*p->schemaAdd)(p, oidtmp);
477 return dumpkeys(n, p, 0);
480 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
484 struct grs_read_info gri;
486 int oidtmp[OID_SIZE];
488 gri.readf = p->readf;
489 gri.seekf = p->seekf;
490 gri.tellf = p->tellf;
493 gri.offset = p->offset;
497 if (read_grs_type (h, &gri, p->subType, &n))
498 return RECCTRL_EXTRACT_ERROR;
500 return RECCTRL_EXTRACT_EOF;
501 oe.proto = PROTO_Z3950;
502 oe.oclass = CLASS_SCHEMA;
503 if (!n->u.root.absyn)
504 return RECCTRL_EXTRACT_ERROR;
505 oe.value = n->u.root.absyn->reference;
506 if ((oid_ent_to_oid (&oe, oidtmp)))
507 (*p->schemaAdd)(p, oidtmp);
509 if (dumpkeys(n, p, 0) < 0)
511 data1_free_tree(p->dh, n);
512 return RECCTRL_EXTRACT_ERROR;
514 data1_free_tree(p->dh, n);
515 return RECCTRL_EXTRACT_OK;
518 static int grs_extract(void *clientData, struct recExtractCtrl *p)
521 NMEM mem = nmem_create ();
522 struct grs_handlers *h = (struct grs_handlers *) clientData;
524 ret = grs_extract_sub(h, p, mem);
530 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
532 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
534 data1_esetname *eset;
540 case Z_RecordComp_simple:
541 if (c->u.simple->which != Z_ElementSetNames_generic)
542 return 26; /* only generic form supported. Fix this later */
543 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
544 c->u.simple->u.generic)))
546 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
547 return 25; /* invalid esetname */
549 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
550 c->u.simple->u.generic);
553 case Z_RecordComp_complex:
554 if (c->u.complex->generic)
556 /* insert check for schema */
557 if ((p = c->u.complex->generic->elementSpec))
561 case Z_ElementSpec_elementSetName:
563 data1_getesetbyname(dh, n->u.root.absyn,
564 p->u.elementSetName)))
566 logf(LOG_LOG, "Unknown esetname '%s'",
567 p->u.elementSetName);
568 return 25; /* invalid esetname */
570 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
571 p->u.elementSetName);
574 case Z_ElementSpec_externalSpec:
575 if (p->u.externalSpec->which == Z_External_espec1)
577 logf(LOG_DEBUG, "Got Espec-1");
578 espec = p->u.externalSpec-> u.espec1;
582 logf(LOG_LOG, "Unknown external espec.");
583 return 25; /* bad. what is proper diagnostic? */
594 logf (LOG_DEBUG, "Element: Espec-1 match");
595 return data1_doespec1(dh, n, espec);
599 logf (LOG_DEBUG, "Element: all match");
604 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
606 data1_node *node = 0, *onode = 0;
609 int res, selected = 0;
611 struct grs_read_info gri;
613 struct grs_handlers *h = (struct grs_handlers *) clientData;
614 int requested_schema = VAL_NONE;
617 gri.readf = p->readf;
618 gri.seekf = p->seekf;
619 gri.tellf = p->tellf;
626 logf (LOG_DEBUG, "grs_retrieve");
627 if (read_grs_type (h, &gri, p->subType, &node))
640 data1_pr_tree (p->dh, node, stdout);
642 logf (LOG_DEBUG, "grs_retrieve: size");
643 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
646 dnew->u.data.what = DATA1I_text;
647 dnew->u.data.data = dnew->lbuf;
648 sprintf(dnew->u.data.data, "%d", p->recordSize);
649 dnew->u.data.len = strlen(dnew->u.data.data);
652 tagname = res_get_def(p->res, "tagrank", "rank");
653 if (strcmp(tagname, "0") && p->score >= 0 &&
654 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
656 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
657 dnew->u.data.what = DATA1I_num;
658 dnew->u.data.data = dnew->lbuf;
659 sprintf(dnew->u.data.data, "%d", p->score);
660 dnew->u.data.len = strlen(dnew->u.data.data);
663 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
664 if (strcmp(tagname, "0") && p->localno > 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_text;
669 dnew->u.data.data = dnew->lbuf;
670 sprintf(dnew->u.data.data, "%d", p->localno);
671 dnew->u.data.len = strlen(dnew->u.data.data);
674 if (p->comp && p->comp->which == Z_RecordComp_complex &&
675 p->comp->u.complex->generic &&
676 p->comp->u.complex->generic->schema)
678 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
680 requested_schema = oe->value;
683 /* If schema has been specified, map if possible, then check that
684 * we got the right one
686 if (requested_schema != VAL_NONE)
688 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
689 for (map = node->u.root.absyn->maptabs; map; map = map->next)
691 if (map->target_absyn_ref == requested_schema)
694 if (!(node = data1_map_record(p->dh, onode, map, mem)))
703 if (node->u.root.absyn &&
704 requested_schema != node->u.root.absyn->reference)
712 * Does the requested format match a known syntax-mapping? (this reflects
713 * the overlap of schema and formatting which is inherent in the MARC
716 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
717 for (map = node->u.root.absyn->maptabs; map; map = map->next)
719 if (map->target_absyn_ref == p->input_format)
722 if (!(node = data1_map_record(p->dh, onode, map, mem)))
731 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
732 if (node->u.root.absyn &&
733 node->u.root.absyn->reference != VAL_NONE &&
734 p->input_format == VAL_GRS1)
738 int oidtmp[OID_SIZE];
740 oe.proto = PROTO_Z3950;
741 oe.oclass = CLASS_SCHEMA;
742 oe.value = node->u.root.absyn->reference;
744 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
747 data1_handle dh = p->dh;
751 for (ii = oid; *ii >= 0; ii++)
755 sprintf(p, "%d", *ii);
760 if ((dnew = data1_insert_taggeddata(dh, node, node,
761 "schemaIdentifier", mem)))
763 dnew->u.data.what = DATA1I_oid;
764 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
765 memcpy(dnew->u.data.data, tmp, p - tmp);
766 dnew->u.data.len = p - tmp;
771 logf (LOG_DEBUG, "grs_retrieve: element spec");
772 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
776 data1_free_tree(p->dh, onode);
777 data1_free_tree(p->dh, node);
781 else if (p->comp && !res)
784 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
785 switch (p->output_format = (p->input_format != VAL_NONE ?
786 p->input_format : VAL_SUTRS))
788 data1_marctab *marctab;
792 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
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;
804 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
806 p->diagnostic = 238; /* not available in requested syntax */
808 p->rec_len = (size_t) (-1);
811 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
815 p->rec_len = (size_t) (-1);
818 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
822 p->rec_len = (size_t) (-1);
825 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
830 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
831 memcpy (new_buf, p->rec_buf, p->rec_len);
832 p->rec_buf = new_buf;
836 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
841 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
842 memcpy (new_buf, p->rec_buf, p->rec_len);
843 p->rec_buf = new_buf;
847 if (!node->u.root.absyn)
852 for (marctab = node->u.root.absyn->marc; marctab;
853 marctab = marctab->next)
854 if (marctab->reference == p->input_format)
861 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
862 selected, &p->rec_len)))
866 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
867 memcpy (new_buf, p->rec_buf, p->rec_len);
868 p->rec_buf = new_buf;
872 data1_free_tree(p->dh, node);
874 data1_free_tree(p->dh, onode);
879 static struct recType grs_type =
888 RecType recTypeGrs = &grs_type;