2 * Copyright (C) 1994-2001, Index Data
6 * Revision 1.43 2002-03-21 23:06:36 adam
7 * Source 'tag' in abs-file
9 * Revision 1.42 2002/02/20 17:30:01 adam
10 * Work on new API. Locking system re-implemented
12 * Revision 1.41 2001/05/22 21:01:47 adam
13 * Removed print of data1 tree on stdout so that inetd works again.
15 * Revision 1.40 2001/03/29 21:31:31 adam
16 * Fixed "record begin" for Tcl filter.
18 * Revision 1.39 2000/12/05 19:09:15 adam
19 * Fixed problem where indexer could crash if abstract syntax was undefined.
21 * Revision 1.38 2000/12/05 14:44:58 adam
22 * Fixed minor bug that could cause zmbol to break it data were emitted
23 * with not parent tags.
25 * Revision 1.37 2000/12/05 12:22:53 adam
26 * Termlist source implemented (so that we can index values of XML/SGML
29 * Revision 1.36 2000/12/05 10:01:44 adam
30 * Fixed bug regarding user-defined attribute sets.
32 * Revision 1.35 2000/11/29 15:21:31 adam
33 * Fixed problem with passwd db.
35 * Revision 1.34 2000/02/25 13:24:49 adam
36 * Fixed bug regarding pointer conversion that showed up on OSF V5.
38 * Revision 1.33 1999/11/30 13:48:04 adam
39 * Improved installation. Updated for inclusion of YAZ header files.
41 * Revision 1.32 1999/09/07 07:19:21 adam
42 * Work on character mapping. Implemented replace rules.
44 * Revision 1.31 1999/07/14 10:56:43 adam
45 * Fixed potential memory leak.
47 * Revision 1.30 1999/07/06 12:26:41 adam
48 * Retrieval handler obeys schema and handles XML transfer syntax.
50 * Revision 1.29 1999/05/26 07:49:14 adam
53 * Revision 1.28 1999/05/21 12:00:17 adam
54 * Better diagnostics for extraction process.
56 * Revision 1.27 1999/05/20 12:57:18 adam
57 * Implemented TCL filter. Updated recctrl system.
59 * Revision 1.26 1999/03/02 16:15:44 quinn
60 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
62 * Revision 1.25 1999/02/18 15:01:26 adam
65 * Revision 1.24 1999/02/02 14:51:28 adam
66 * Updated WIN32 code specific sections. Changed header.
68 * Revision 1.23 1998/10/18 07:51:10 adam
69 * Changed one logf call.
71 * Revision 1.22 1998/10/16 08:14:37 adam
72 * Updated record control system.
74 * Revision 1.21 1998/07/01 09:16:10 adam
75 * Element localno only added when it's greater than 0.
77 * Revision 1.20 1998/05/20 10:12:26 adam
78 * Implemented automatic EXPLAIN database maintenance.
79 * Modified Zebra to work with ASN.1 compiled version of YAZ.
81 * Revision 1.19 1998/03/11 11:19:05 adam
82 * Changed the way sequence numbers are generated.
84 * Revision 1.18 1998/03/05 08:41:31 adam
87 * Revision 1.17 1998/02/10 12:03:06 adam
90 * Revision 1.16 1998/01/29 13:38:17 adam
91 * Fixed problem with mapping to record with unknown schema.
93 * Revision 1.15 1998/01/26 10:37:57 adam
96 * Revision 1.14 1997/11/06 11:41:01 adam
97 * Implemented "begin variant" for the sgml.regx filter.
99 * Revision 1.13 1997/10/31 12:35:44 adam
100 * Added a few log statements.
102 * Revision 1.12 1997/10/29 12:02:22 adam
103 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
105 * Revision 1.11 1997/10/27 14:34:00 adam
106 * Work on generic character mapping depending on "structure" field
107 * in abstract syntax file.
109 * Revision 1.10 1997/09/18 08:59:21 adam
110 * Extra generic handle for the character mapping routines.
112 * Revision 1.9 1997/09/17 12:19:21 adam
113 * Zebra version corresponds to YAZ version 1.4.
114 * Changed Zebra server so that it doesn't depend on global common_resource.
116 * Revision 1.8 1997/09/09 13:38:14 adam
117 * Partial port to WIN95/NT.
119 * Revision 1.7 1997/09/05 15:30:10 adam
120 * Changed prototype for chr_map_input - added const.
121 * Added support for C++, headers uses extern "C" for public definitions.
123 * Revision 1.6 1997/09/04 13:54:40 adam
124 * Added MARC filter - type grs.marc.<syntax> where syntax refers
125 * to abstract syntax. New method tellf in retrieve/extract method.
127 * Revision 1.5 1997/07/15 16:29:03 adam
128 * Initialized dummy variable to keep checker gcc happy.
130 * Revision 1.4 1997/04/30 08:56:08 quinn
133 * Revision 1.2 1996/10/11 16:06:43 quinn
134 * Revision 1.3 1997/02/24 10:41:50 adam
135 * Cleanup of code and commented out the "end element-end-record" code.
137 * Revision 1.2 1996/10/11 16:06:43 quinn
138 * Fixed arguments to nodetogr
140 * Revision 1.1 1996/10/11 10:57:25 adam
141 * New module recctrl. Used to manage records (extract/retrieval).
143 * Revision 1.29 1996/10/08 10:30:21 quinn
144 * Fixed type mismatch
146 * Revision 1.28 1996/10/07 16:06:40 quinn
149 * Revision 1.27 1996/06/11 10:54:12 quinn
152 * Revision 1.26 1996/06/06 12:08:45 quinn
153 * Added showRecord function
155 * Revision 1.25 1996/06/04 14:18:53 quinn
158 * Revision 1.24 1996/06/04 13:27:54 quinn
159 * More work on charmapping
161 * Revision 1.23 1996/06/04 10:19:01 adam
162 * Minor changes - removed include of ctype.h.
164 * Revision 1.22 1996/06/03 10:15:27 quinn
165 * Various character-mapping.
167 * Revision 1.21 1996/05/31 13:27:24 quinn
168 * Character-conversion in phrases, too.
170 * Revision 1.19 1996/05/16 15:31:14 quinn
173 * Revision 1.18 1996/05/09 07:28:56 quinn
174 * Work towards phrases and multiple registers
176 * Revision 1.17 1996/05/01 13:46:37 adam
177 * First work on multiple records in one file.
178 * New option, -offset, to the "unread" command in the filter module.
180 * Revision 1.16 1996/01/17 14:57:54 adam
181 * Prototype changed for reader functions in extract/retrieve. File
182 * is identified by 'void *' instead of 'int.
184 * Revision 1.15 1996/01/08 19:15:47 adam
185 * New input filter that works!
187 * Revision 1.14 1995/12/15 12:36:11 adam
188 * Retrieval calls data1_read_regx when subType is specified.
190 * Revision 1.13 1995/12/15 12:24:43 quinn
191 * *** empty log message ***
193 * Revision 1.12 1995/12/15 12:20:28 quinn
194 * *** empty log message ***
196 * Revision 1.11 1995/12/15 12:07:57 quinn
197 * Changed extraction strategy.
199 * Revision 1.10 1995/12/14 11:10:48 quinn
202 * Revision 1.9 1995/12/13 17:14:05 quinn
203 * *** empty log message ***
205 * Revision 1.8 1995/12/13 15:33:18 quinn
206 * *** empty log message ***
208 * Revision 1.7 1995/12/13 13:45:39 quinn
209 * Changed data1 to use nmem.
211 * Revision 1.6 1995/12/04 14:22:30 adam
212 * Extra arg to recType_byName.
213 * Started work on new regular expression parsed input to
214 * structured records.
216 * Revision 1.5 1995/11/28 14:18:37 quinn
219 * Revision 1.4 1995/11/21 13:14:49 quinn
220 * Fixed end-of-data-field problem (maybe).
222 * Revision 1.3 1995/11/15 19:13:09 adam
223 * Work on record management.
229 #include <sys/types.h>
240 #define GRS_MAX_WORD 512
246 struct grs_handler *next;
249 struct grs_handlers {
250 struct grs_handler *handlers;
253 static int read_grs_type (struct grs_handlers *h,
254 struct grs_read_info *p, const char *type,
257 struct grs_handler *gh = h->handlers;
258 const char *cp = strchr (type, '.');
260 if (cp == NULL || cp == type)
262 cp = strlen(type) + type;
266 strcpy (p->type, cp+1);
267 for (gh = h->handlers; gh; gh = gh->next)
269 if (!memcmp (type, gh->type->type, cp-type))
274 gh->clientData = (*gh->type->init)();
276 p->clientData = gh->clientData;
277 *root = (gh->type->read)(p);
278 gh->clientData = p->clientData;
285 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
287 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
288 gh->next = h->handlers;
295 static void *grs_init(RecType recType)
297 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
300 grs_add_handler (h, recTypeGrs_sgml);
301 grs_add_handler (h, recTypeGrs_regx);
303 grs_add_handler (h, recTypeGrs_tcl);
305 grs_add_handler (h, recTypeGrs_marc);
309 static void grs_destroy(void *clientData)
311 struct grs_handlers *h = (struct grs_handlers *) clientData;
312 struct grs_handler *gh = h->handlers, *gh_next;
317 (*gh->type->destroy)(gh->clientData);
324 static void index_tag (data1_node *par, data1_node *n,
325 struct recExtractCtrl *p, int level, RecWord *wrd)
327 data1_termlist *tlist = 0;
328 data1_datatype dtype = DATA1K_string;
330 * cycle up towards the root until we find a tag with an att..
331 * this has the effect of indexing locally defined tags with
332 * the attribute of their ancestor in the record.
335 while (!par->u.tag.element)
336 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
338 if (!par || !(tlist = par->u.tag.element->termlists))
340 if (par->u.tag.element->tag)
341 dtype = par->u.tag.element->tag->kind;
343 for (; tlist; tlist = tlist->next)
346 /* consider source */
349 if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
351 wrd->string = n->u.data.data;
352 wrd->length = n->u.data.len;
354 else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
356 wrd->string = n->u.tag.tag;
357 wrd->length = strlen(n->u.tag.tag);
359 else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
360 n->which == DATA1N_tag)
362 data1_xattr *p = n->u.tag.attributes;
363 while (p && strcmp (p->name, xattr))
367 wrd->string = p->value;
368 wrd->length = strlen(p->value);
373 if (p->flagShowRecords)
376 printf("%*sIdx: [%s]", (level + 1) * 4, "",
378 printf("%s:%s [%d] %s",
379 tlist->att->parent->name,
380 tlist->att->name, tlist->att->value,
383 for (i = 0; i<wrd->length && i < 8; i++)
384 fputc (wrd->string[i], stdout);
388 fputc ('\n', stdout);
392 wrd->reg_type = *tlist->structure;
393 wrd->attrSet = (int) (tlist->att->parent->reference);
394 wrd->attrUse = tlist->att->locals->local;
401 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
404 (*p->init)(p, &wrd); /* set defaults */
405 for (; n; n = n->next)
407 if (p->flagShowRecords) /* display element description to user */
409 if (n->which == DATA1N_root)
411 printf("%*s", level * 4, "");
412 printf("Record type: '%s'\n", n->u.root.absyn->name);
414 else if (n->which == DATA1N_tag)
418 printf("%*s", level * 4, "");
419 if (!(e = n->u.tag.element))
420 printf("Local tag: '%s'\n", n->u.tag.tag);
423 printf("Elm: '%s' ", e->name);
426 data1_tag *t = e->tag;
428 printf("TagNam: '%s' ", t->names->name);
431 printf("%s[%d],", t->tagset->name, t->tagset->type);
434 if (t->which == DATA1T_numeric)
435 printf("%d)", t->value.numeric);
437 printf("'%s')", t->value.string);
445 if (dumpkeys(n->child, p, level + 1) < 0)
448 if (n->which == DATA1N_tag)
450 index_tag (n, n, p, level, &wrd);
453 if (n->which == DATA1N_data)
455 data1_node *par = get_parent_tag(p->dh, n);
457 if (p->flagShowRecords)
459 printf("%*s", level * 4, "");
461 if (n->u.data.len > 32)
462 printf("'%.24s ... %.6s'\n", n->u.data.data,
463 n->u.data.data + n->u.data.len-6);
464 else if (n->u.data.len > 0)
465 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
471 index_tag (par, n, p, level, &wrd);
473 if (p->flagShowRecords && n->which == DATA1N_root)
475 printf("%*s-------------\n\n", level * 4, "");
481 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
484 int oidtmp[OID_SIZE];
486 oe.proto = PROTO_Z3950;
487 oe.oclass = CLASS_SCHEMA;
488 oe.value = n->u.root.absyn->reference;
490 if ((oid_ent_to_oid (&oe, oidtmp)))
491 (*p->schemaAdd)(p, oidtmp);
493 return dumpkeys(n, p, 0);
496 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
500 struct grs_read_info gri;
502 int oidtmp[OID_SIZE];
504 gri.readf = p->readf;
505 gri.seekf = p->seekf;
506 gri.tellf = p->tellf;
509 gri.offset = p->offset;
513 if (read_grs_type (h, &gri, p->subType, &n))
514 return RECCTRL_EXTRACT_ERROR;
516 return RECCTRL_EXTRACT_EOF;
517 oe.proto = PROTO_Z3950;
518 oe.oclass = CLASS_SCHEMA;
519 if (!n->u.root.absyn)
520 return RECCTRL_EXTRACT_ERROR;
521 oe.value = n->u.root.absyn->reference;
522 if ((oid_ent_to_oid (&oe, oidtmp)))
523 (*p->schemaAdd)(p, oidtmp);
526 data1_pr_tree (p->dh, n, stdout);
528 if (dumpkeys(n, p, 0) < 0)
530 data1_free_tree(p->dh, n);
531 return RECCTRL_EXTRACT_ERROR;
533 data1_free_tree(p->dh, n);
534 return RECCTRL_EXTRACT_OK;
537 static int grs_extract(void *clientData, struct recExtractCtrl *p)
540 NMEM mem = nmem_create ();
541 struct grs_handlers *h = (struct grs_handlers *) clientData;
543 ret = grs_extract_sub(h, p, mem);
549 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
551 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
553 data1_esetname *eset;
559 case Z_RecordComp_simple:
560 if (c->u.simple->which != Z_ElementSetNames_generic)
561 return 26; /* only generic form supported. Fix this later */
562 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
563 c->u.simple->u.generic)))
565 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
566 return 25; /* invalid esetname */
568 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
569 c->u.simple->u.generic);
572 case Z_RecordComp_complex:
573 if (c->u.complex->generic)
575 /* insert check for schema */
576 if ((p = c->u.complex->generic->elementSpec))
580 case Z_ElementSpec_elementSetName:
582 data1_getesetbyname(dh, n->u.root.absyn,
583 p->u.elementSetName)))
585 logf(LOG_LOG, "Unknown esetname '%s'",
586 p->u.elementSetName);
587 return 25; /* invalid esetname */
589 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
590 p->u.elementSetName);
593 case Z_ElementSpec_externalSpec:
594 if (p->u.externalSpec->which == Z_External_espec1)
596 logf(LOG_DEBUG, "Got Espec-1");
597 espec = p->u.externalSpec-> u.espec1;
601 logf(LOG_LOG, "Unknown external espec.");
602 return 25; /* bad. what is proper diagnostic? */
613 logf (LOG_DEBUG, "Element: Espec-1 match");
614 return data1_doespec1(dh, n, espec);
618 logf (LOG_DEBUG, "Element: all match");
623 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
625 data1_node *node = 0, *onode = 0;
628 int res, selected = 0;
630 struct grs_read_info gri;
632 struct grs_handlers *h = (struct grs_handlers *) clientData;
633 int requested_schema = VAL_NONE;
636 gri.readf = p->readf;
637 gri.seekf = p->seekf;
638 gri.tellf = p->tellf;
645 logf (LOG_DEBUG, "grs_retrieve");
646 if (read_grs_type (h, &gri, p->subType, &node))
659 data1_pr_tree (p->dh, node, stdout);
661 logf (LOG_DEBUG, "grs_retrieve: size");
662 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
665 dnew->u.data.what = DATA1I_text;
666 dnew->u.data.data = dnew->lbuf;
667 sprintf(dnew->u.data.data, "%d", p->recordSize);
668 dnew->u.data.len = strlen(dnew->u.data.data);
671 tagname = res_get_def(p->res, "tagrank", "rank");
672 if (strcmp(tagname, "0") && p->score >= 0 &&
673 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
675 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
676 dnew->u.data.what = DATA1I_num;
677 dnew->u.data.data = dnew->lbuf;
678 sprintf(dnew->u.data.data, "%d", p->score);
679 dnew->u.data.len = strlen(dnew->u.data.data);
682 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
683 if (strcmp(tagname, "0") && p->localno > 0 &&
684 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
686 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
687 dnew->u.data.what = DATA1I_text;
688 dnew->u.data.data = dnew->lbuf;
689 sprintf(dnew->u.data.data, "%d", p->localno);
690 dnew->u.data.len = strlen(dnew->u.data.data);
693 if (p->comp && p->comp->which == Z_RecordComp_complex &&
694 p->comp->u.complex->generic &&
695 p->comp->u.complex->generic->schema)
697 oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
699 requested_schema = oe->value;
702 /* If schema has been specified, map if possible, then check that
703 * we got the right one
705 if (requested_schema != VAL_NONE)
707 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
708 for (map = node->u.root.absyn->maptabs; map; map = map->next)
710 if (map->target_absyn_ref == requested_schema)
713 if (!(node = data1_map_record(p->dh, onode, map, mem)))
722 if (node->u.root.absyn &&
723 requested_schema != node->u.root.absyn->reference)
731 * Does the requested format match a known syntax-mapping? (this reflects
732 * the overlap of schema and formatting which is inherent in the MARC
735 logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
736 for (map = node->u.root.absyn->maptabs; map; map = map->next)
738 if (map->target_absyn_ref == p->input_format)
741 if (!(node = data1_map_record(p->dh, onode, map, mem)))
750 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
751 if (node->u.root.absyn &&
752 node->u.root.absyn->reference != VAL_NONE &&
753 p->input_format == VAL_GRS1)
757 int oidtmp[OID_SIZE];
759 oe.proto = PROTO_Z3950;
760 oe.oclass = CLASS_SCHEMA;
761 oe.value = node->u.root.absyn->reference;
763 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
766 data1_handle dh = p->dh;
770 for (ii = oid; *ii >= 0; ii++)
774 sprintf(p, "%d", *ii);
779 if ((dnew = data1_insert_taggeddata(dh, node, node,
780 "schemaIdentifier", mem)))
782 dnew->u.data.what = DATA1I_oid;
783 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
784 memcpy(dnew->u.data.data, tmp, p - tmp);
785 dnew->u.data.len = p - tmp;
790 logf (LOG_DEBUG, "grs_retrieve: element spec");
791 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
795 data1_free_tree(p->dh, onode);
796 data1_free_tree(p->dh, node);
800 else if (p->comp && !res)
804 data1_pr_tree (p->dh, node, stdout);
806 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
807 switch (p->output_format = (p->input_format != VAL_NONE ?
808 p->input_format : VAL_SUTRS))
810 data1_marctab *marctab;
814 if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
819 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
820 memcpy (new_buf, p->rec_buf, p->rec_len);
821 p->rec_buf = new_buf;
826 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
828 p->diagnostic = 238; /* not available in requested syntax */
830 p->rec_len = (size_t) (-1);
833 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
837 p->rec_len = (size_t) (-1);
840 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
844 p->rec_len = (size_t) (-1);
847 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
852 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
853 memcpy (new_buf, p->rec_buf, p->rec_len);
854 p->rec_buf = new_buf;
858 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
863 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
864 memcpy (new_buf, p->rec_buf, p->rec_len);
865 p->rec_buf = new_buf;
869 if (!node->u.root.absyn)
874 for (marctab = node->u.root.absyn->marc; marctab;
875 marctab = marctab->next)
876 if (marctab->reference == p->input_format)
883 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
884 selected, &p->rec_len)))
888 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
889 memcpy (new_buf, p->rec_buf, p->rec_len);
890 p->rec_buf = new_buf;
894 data1_free_tree(p->dh, node);
896 data1_free_tree(p->dh, onode);
901 static struct recType grs_type =
910 RecType recTypeGrs = &grs_type;