2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.29 1999-05-26 07:49:14 adam
10 * Revision 1.28 1999/05/21 12:00:17 adam
11 * Better diagnostics for extraction process.
13 * Revision 1.27 1999/05/20 12:57:18 adam
14 * Implemented TCL filter. Updated recctrl system.
16 * Revision 1.26 1999/03/02 16:15:44 quinn
17 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
19 * Revision 1.25 1999/02/18 15:01:26 adam
22 * Revision 1.24 1999/02/02 14:51:28 adam
23 * Updated WIN32 code specific sections. Changed header.
25 * Revision 1.23 1998/10/18 07:51:10 adam
26 * Changed one logf call.
28 * Revision 1.22 1998/10/16 08:14:37 adam
29 * Updated record control system.
31 * Revision 1.21 1998/07/01 09:16:10 adam
32 * Element localno only added when it's greater than 0.
34 * Revision 1.20 1998/05/20 10:12:26 adam
35 * Implemented automatic EXPLAIN database maintenance.
36 * Modified Zebra to work with ASN.1 compiled version of YAZ.
38 * Revision 1.19 1998/03/11 11:19:05 adam
39 * Changed the way sequence numbers are generated.
41 * Revision 1.18 1998/03/05 08:41:31 adam
44 * Revision 1.17 1998/02/10 12:03:06 adam
47 * Revision 1.16 1998/01/29 13:38:17 adam
48 * Fixed problem with mapping to record with unknown schema.
50 * Revision 1.15 1998/01/26 10:37:57 adam
53 * Revision 1.14 1997/11/06 11:41:01 adam
54 * Implemented "begin variant" for the sgml.regx filter.
56 * Revision 1.13 1997/10/31 12:35:44 adam
57 * Added a few log statements.
59 * Revision 1.12 1997/10/29 12:02:22 adam
60 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
62 * Revision 1.11 1997/10/27 14:34:00 adam
63 * Work on generic character mapping depending on "structure" field
64 * in abstract syntax file.
66 * Revision 1.10 1997/09/18 08:59:21 adam
67 * Extra generic handle for the character mapping routines.
69 * Revision 1.9 1997/09/17 12:19:21 adam
70 * Zebra version corresponds to YAZ version 1.4.
71 * Changed Zebra server so that it doesn't depend on global common_resource.
73 * Revision 1.8 1997/09/09 13:38:14 adam
74 * Partial port to WIN95/NT.
76 * Revision 1.7 1997/09/05 15:30:10 adam
77 * Changed prototype for chr_map_input - added const.
78 * Added support for C++, headers uses extern "C" for public definitions.
80 * Revision 1.6 1997/09/04 13:54:40 adam
81 * Added MARC filter - type grs.marc.<syntax> where syntax refers
82 * to abstract syntax. New method tellf in retrieve/extract method.
84 * Revision 1.5 1997/07/15 16:29:03 adam
85 * Initialized dummy variable to keep checker gcc happy.
87 * Revision 1.4 1997/04/30 08:56:08 quinn
90 * Revision 1.2 1996/10/11 16:06:43 quinn
91 * Revision 1.3 1997/02/24 10:41:50 adam
92 * Cleanup of code and commented out the "end element-end-record" code.
94 * Revision 1.2 1996/10/11 16:06:43 quinn
95 * Fixed arguments to nodetogr
97 * Revision 1.1 1996/10/11 10:57:25 adam
98 * New module recctrl. Used to manage records (extract/retrieval).
100 * Revision 1.29 1996/10/08 10:30:21 quinn
101 * Fixed type mismatch
103 * Revision 1.28 1996/10/07 16:06:40 quinn
106 * Revision 1.27 1996/06/11 10:54:12 quinn
109 * Revision 1.26 1996/06/06 12:08:45 quinn
110 * Added showRecord function
112 * Revision 1.25 1996/06/04 14:18:53 quinn
115 * Revision 1.24 1996/06/04 13:27:54 quinn
116 * More work on charmapping
118 * Revision 1.23 1996/06/04 10:19:01 adam
119 * Minor changes - removed include of ctype.h.
121 * Revision 1.22 1996/06/03 10:15:27 quinn
122 * Various character-mapping.
124 * Revision 1.21 1996/05/31 13:27:24 quinn
125 * Character-conversion in phrases, too.
127 * Revision 1.19 1996/05/16 15:31:14 quinn
130 * Revision 1.18 1996/05/09 07:28:56 quinn
131 * Work towards phrases and multiple registers
133 * Revision 1.17 1996/05/01 13:46:37 adam
134 * First work on multiple records in one file.
135 * New option, -offset, to the "unread" command in the filter module.
137 * Revision 1.16 1996/01/17 14:57:54 adam
138 * Prototype changed for reader functions in extract/retrieve. File
139 * is identified by 'void *' instead of 'int.
141 * Revision 1.15 1996/01/08 19:15:47 adam
142 * New input filter that works!
144 * Revision 1.14 1995/12/15 12:36:11 adam
145 * Retrieval calls data1_read_regx when subType is specified.
147 * Revision 1.13 1995/12/15 12:24:43 quinn
148 * *** empty log message ***
150 * Revision 1.12 1995/12/15 12:20:28 quinn
151 * *** empty log message ***
153 * Revision 1.11 1995/12/15 12:07:57 quinn
154 * Changed extraction strategy.
156 * Revision 1.10 1995/12/14 11:10:48 quinn
159 * Revision 1.9 1995/12/13 17:14:05 quinn
160 * *** empty log message ***
162 * Revision 1.8 1995/12/13 15:33:18 quinn
163 * *** empty log message ***
165 * Revision 1.7 1995/12/13 13:45:39 quinn
166 * Changed data1 to use nmem.
168 * Revision 1.6 1995/12/04 14:22:30 adam
169 * Extra arg to recType_byName.
170 * Started work on new regular expression parsed input to
171 * structured records.
173 * Revision 1.5 1995/11/28 14:18:37 quinn
176 * Revision 1.4 1995/11/21 13:14:49 quinn
177 * Fixed end-of-data-field problem (maybe).
179 * Revision 1.3 1995/11/15 19:13:09 adam
180 * Work on record management.
186 #include <sys/types.h>
197 #define GRS_MAX_WORD 512
203 struct grs_handler *next;
206 struct grs_handlers {
207 struct grs_handler *handlers;
210 static int read_grs_type (struct grs_handlers *h,
211 struct grs_read_info *p, const char *type,
214 struct grs_handler *gh = h->handlers;
215 const char *cp = strchr (type, '.');
217 if (cp == NULL || cp == type)
219 cp = strlen(type) + type;
223 strcpy (p->type, cp+1);
224 for (gh = h->handlers; gh; gh = gh->next)
226 if (!memcmp (type, gh->type->type, cp-type))
231 gh->clientData = (*gh->type->init)();
233 p->clientData = gh->clientData;
234 *root = (gh->type->read)(p);
235 gh->clientData = p->clientData;
242 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
244 struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
245 gh->next = h->handlers;
252 static void *grs_init(RecType recType)
254 struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
257 grs_add_handler (h, recTypeGrs_sgml);
258 grs_add_handler (h, recTypeGrs_regx);
260 grs_add_handler (h, recTypeGrs_tcl);
262 grs_add_handler (h, recTypeGrs_marc);
266 static void grs_destroy(void *clientData)
268 struct grs_handlers *h = (struct grs_handlers *) clientData;
269 struct grs_handler *gh = h->handlers, *gh_next;
274 (*gh->type->destroy)(gh->clientData);
281 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
284 (*p->init)(p, &wrd); /* set defaults */
285 for (; n; n = n->next)
287 if (p->flagShowRecords) /* display element description to user */
289 if (n->which == DATA1N_root)
291 printf("%*s", level * 4, "");
292 printf("Record type: '%s'\n", n->u.root.absyn->name);
294 else if (n->which == DATA1N_tag)
298 printf("%*s", level * 4, "");
299 if (!(e = n->u.tag.element))
300 printf("Local tag: '%s'\n", n->u.tag.tag);
303 printf("Elm: '%s' ", e->name);
306 data1_tag *t = e->tag;
308 printf("TagNam: '%s' ", t->names->name);
311 printf("%s[%d],", t->tagset->name, t->tagset->type);
314 if (t->which == DATA1T_numeric)
315 printf("%d)", t->value.numeric);
317 printf("'%s')", t->value.string);
325 if (dumpkeys(n->child, p, level + 1) < 0)
328 if (n->which == DATA1N_data)
330 data1_node *par = get_parent_tag(p->dh, n);
331 data1_termlist *tlist = 0;
332 data1_datatype dtype = DATA1K_string;
334 if (p->flagShowRecords)
336 printf("%*s", level * 4, "");
338 if (n->u.data.len > 32)
339 printf("'%.24s ... %.6s'\n", n->u.data.data,
340 n->u.data.data + n->u.data.len-6);
341 else if (n->u.data.len > 0)
342 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
350 * cycle up towards the root until we find a tag with an att..
351 * this has the effect of indexing locally defined tags with
352 * the attribute of their ancestor in the record.
355 while (!par->u.tag.element)
356 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
358 if (!par || !(tlist = par->u.tag.element->termlists))
360 if (par->u.tag.element->tag)
361 dtype = par->u.tag.element->tag->kind;
362 for (; tlist; tlist = tlist->next)
364 if (p->flagShowRecords)
366 printf("%*sIdx: [%s]", (level + 1) * 4, "",
368 printf("%s:%s [%d]\n",
369 tlist->att->parent->name,
370 tlist->att->name, tlist->att->value);
374 wrd.reg_type = *tlist->structure;
375 wrd.string = n->u.data.data;
376 wrd.length = n->u.data.len;
377 wrd.attrSet = (int) (tlist->att->parent->reference);
378 wrd.attrUse = tlist->att->locals->local;
383 if (p->flagShowRecords && n->which == DATA1N_root)
385 printf("%*s-------------\n\n", level * 4, "");
391 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
394 int oidtmp[OID_SIZE];
396 oe.proto = PROTO_Z3950;
397 oe.oclass = CLASS_SCHEMA;
398 oe.value = n->u.root.absyn->reference;
400 if ((oid_ent_to_oid (&oe, oidtmp)))
401 (*p->addSchema)(p, oidtmp);
403 return dumpkeys(n, p, 0);
406 static int grs_extract(void *clientData, struct recExtractCtrl *p)
410 struct grs_read_info gri;
412 int oidtmp[OID_SIZE];
413 struct grs_handlers *h = (struct grs_handlers *) clientData;
415 mem = nmem_create ();
416 gri.readf = p->readf;
417 gri.seekf = p->seekf;
418 gri.tellf = p->tellf;
421 gri.offset = p->offset;
425 if (read_grs_type (h, &gri, p->subType, &n))
426 return RECCTRL_EXTRACT_ERROR;
428 return RECCTRL_EXTRACT_EOF;
430 oe.proto = PROTO_Z3950;
431 oe.oclass = CLASS_SCHEMA;
432 oe.value = n->u.root.absyn->reference;
433 if ((oid_ent_to_oid (&oe, oidtmp)))
434 (*p->addSchema)(p, oidtmp);
436 if (dumpkeys(n, p, 0) < 0)
438 data1_free_tree(p->dh, n);
439 return RECCTRL_EXTRACT_ERROR;
441 data1_free_tree(p->dh, n);
443 return RECCTRL_EXTRACT_OK;
447 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
449 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
451 data1_esetname *eset;
457 case Z_RecordComp_simple:
458 if (c->u.simple->which != Z_ElementSetNames_generic)
459 return 26; /* only generic form supported. Fix this later */
460 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
461 c->u.simple->u.generic)))
463 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
464 return 25; /* invalid esetname */
466 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
467 c->u.simple->u.generic);
470 case Z_RecordComp_complex:
471 if (c->u.complex->generic)
473 /* insert check for schema */
474 if ((p = c->u.complex->generic->elementSpec))
477 case Z_ElementSpec_elementSetName:
479 data1_getesetbyname(dh,
481 p->u.elementSetName)))
483 logf(LOG_LOG, "Unknown esetname '%s'",
484 p->u.elementSetName);
485 return 25; /* invalid esetname */
487 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
488 p->u.elementSetName);
491 case Z_ElementSpec_externalSpec:
492 if (p->u.externalSpec->which == Z_External_espec1)
494 logf(LOG_DEBUG, "Got Espec-1");
495 espec = p->u.externalSpec-> u.espec1;
499 logf(LOG_LOG, "Unknown external espec.");
500 return 25; /* bad. what is proper diagnostic? */
510 logf (LOG_DEBUG, "Element: Espec-1 match");
511 return data1_doespec1(dh, n, espec);
515 logf (LOG_DEBUG, "Element: all match");
520 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
522 data1_node *node = 0, *onode = 0;
525 int res, selected = 0;
527 struct grs_read_info gri;
529 struct grs_handlers *h = (struct grs_handlers *) clientData;
532 gri.readf = p->readf;
533 gri.seekf = p->seekf;
534 gri.tellf = p->tellf;
541 logf (LOG_DEBUG, "grs_retrieve");
542 if (read_grs_type (h, &gri, p->subType, &node))
554 logf (LOG_DEBUG, "grs_retrieve: size");
555 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
558 dnew->u.data.what = DATA1I_text;
559 dnew->u.data.data = dnew->lbuf;
560 sprintf(dnew->u.data.data, "%d", p->recordSize);
561 dnew->u.data.len = strlen(dnew->u.data.data);
564 tagname = res_get_def(p->res, "tagrank", "rank");
565 if (strcmp(tagname, "0") && p->score >= 0 && (dnew =
566 data1_insert_taggeddata(p->dh, node,
570 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
571 dnew->u.data.what = DATA1I_num;
572 dnew->u.data.data = dnew->lbuf;
573 sprintf(dnew->u.data.data, "%d", p->score);
574 dnew->u.data.len = strlen(dnew->u.data.data);
577 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
578 if (strcmp(tagname, "0") && p->localno > 0 &&
579 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
581 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
582 dnew->u.data.what = DATA1I_text;
583 dnew->u.data.data = dnew->lbuf;
584 sprintf(dnew->u.data.data, "%d", p->localno);
585 dnew->u.data.len = strlen(dnew->u.data.data);
588 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
589 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
590 node->u.root.absyn->reference != VAL_NONE)
594 int oidtmp[OID_SIZE];
596 oe.proto = PROTO_Z3950;
597 oe.oclass = CLASS_SCHEMA;
598 oe.value = node->u.root.absyn->reference;
600 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
603 data1_handle dh = p->dh;
607 for (ii = oid; *ii >= 0; ii++)
611 sprintf(p, "%d", *ii);
616 if ((dnew = data1_insert_taggeddata(dh, node, node,
617 "schemaIdentifier", mem)))
619 dnew->u.data.what = DATA1I_oid;
620 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
621 memcpy(dnew->u.data.data, tmp, p - tmp);
622 dnew->u.data.len = p - tmp;
627 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
629 * Does the requested format match a known schema-mapping? (this reflects
630 * the overlap of schema and formatting which is inherent in the MARC
632 * NOTE: This should look at the schema-specification in the compspec
635 for (map = node->u.root.absyn->maptabs; map; map = map->next)
636 if (map->target_absyn_ref == p->input_format)
639 if (!(node = data1_map_record(p->dh, onode, map, mem)))
648 logf (LOG_DEBUG, "grs_retrieve: element spec");
649 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
653 data1_free_tree(p->dh, onode);
654 data1_free_tree(p->dh, node);
658 else if (p->comp && !res)
661 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
662 switch (p->output_format = (p->input_format != VAL_NONE ?
663 p->input_format : VAL_SUTRS))
665 data1_marctab *marctab;
670 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
672 p->diagnostic = 238; /* not available in requested syntax */
674 p->rec_len = (size_t) (-1);
677 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
681 p->rec_len = (size_t) (-1);
684 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
688 p->rec_len = (size_t) (-1);
691 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
696 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
697 memcpy (new_buf, p->rec_buf, p->rec_len);
698 p->rec_buf = new_buf;
702 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
707 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
708 memcpy (new_buf, p->rec_buf, p->rec_len);
709 p->rec_buf = new_buf;
713 if (!node->u.root.absyn)
718 for (marctab = node->u.root.absyn->marc; marctab;
719 marctab = marctab->next)
720 if (marctab->reference == p->input_format)
727 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
733 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
734 memcpy (new_buf, p->rec_buf, p->rec_len);
735 p->rec_buf = new_buf;
739 data1_free_tree(p->dh, node);
741 data1_free_tree(p->dh, onode);
746 static struct recType grs_type =
755 RecType recTypeGrs = &grs_type;