2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.28 1999-05-21 12:00:17 adam
8 * Better diagnostics for extraction process.
10 * Revision 1.27 1999/05/20 12:57:18 adam
11 * Implemented TCL filter. Updated recctrl system.
13 * Revision 1.26 1999/03/02 16:15:44 quinn
14 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
16 * Revision 1.25 1999/02/18 15:01:26 adam
19 * Revision 1.24 1999/02/02 14:51:28 adam
20 * Updated WIN32 code specific sections. Changed header.
22 * Revision 1.23 1998/10/18 07:51:10 adam
23 * Changed one logf call.
25 * Revision 1.22 1998/10/16 08:14:37 adam
26 * Updated record control system.
28 * Revision 1.21 1998/07/01 09:16:10 adam
29 * Element localno only added when it's greater than 0.
31 * Revision 1.20 1998/05/20 10:12:26 adam
32 * Implemented automatic EXPLAIN database maintenance.
33 * Modified Zebra to work with ASN.1 compiled version of YAZ.
35 * Revision 1.19 1998/03/11 11:19:05 adam
36 * Changed the way sequence numbers are generated.
38 * Revision 1.18 1998/03/05 08:41:31 adam
41 * Revision 1.17 1998/02/10 12:03:06 adam
44 * Revision 1.16 1998/01/29 13:38:17 adam
45 * Fixed problem with mapping to record with unknown schema.
47 * Revision 1.15 1998/01/26 10:37:57 adam
50 * Revision 1.14 1997/11/06 11:41:01 adam
51 * Implemented "begin variant" for the sgml.regx filter.
53 * Revision 1.13 1997/10/31 12:35:44 adam
54 * Added a few log statements.
56 * Revision 1.12 1997/10/29 12:02:22 adam
57 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
59 * Revision 1.11 1997/10/27 14:34:00 adam
60 * Work on generic character mapping depending on "structure" field
61 * in abstract syntax file.
63 * Revision 1.10 1997/09/18 08:59:21 adam
64 * Extra generic handle for the character mapping routines.
66 * Revision 1.9 1997/09/17 12:19:21 adam
67 * Zebra version corresponds to YAZ version 1.4.
68 * Changed Zebra server so that it doesn't depend on global common_resource.
70 * Revision 1.8 1997/09/09 13:38:14 adam
71 * Partial port to WIN95/NT.
73 * Revision 1.7 1997/09/05 15:30:10 adam
74 * Changed prototype for chr_map_input - added const.
75 * Added support for C++, headers uses extern "C" for public definitions.
77 * Revision 1.6 1997/09/04 13:54:40 adam
78 * Added MARC filter - type grs.marc.<syntax> where syntax refers
79 * to abstract syntax. New method tellf in retrieve/extract method.
81 * Revision 1.5 1997/07/15 16:29:03 adam
82 * Initialized dummy variable to keep checker gcc happy.
84 * Revision 1.4 1997/04/30 08:56:08 quinn
87 * Revision 1.2 1996/10/11 16:06:43 quinn
88 * Revision 1.3 1997/02/24 10:41:50 adam
89 * Cleanup of code and commented out the "end element-end-record" code.
91 * Revision 1.2 1996/10/11 16:06:43 quinn
92 * Fixed arguments to nodetogr
94 * Revision 1.1 1996/10/11 10:57:25 adam
95 * New module recctrl. Used to manage records (extract/retrieval).
97 * Revision 1.29 1996/10/08 10:30:21 quinn
100 * Revision 1.28 1996/10/07 16:06:40 quinn
103 * Revision 1.27 1996/06/11 10:54:12 quinn
106 * Revision 1.26 1996/06/06 12:08:45 quinn
107 * Added showRecord function
109 * Revision 1.25 1996/06/04 14:18:53 quinn
112 * Revision 1.24 1996/06/04 13:27:54 quinn
113 * More work on charmapping
115 * Revision 1.23 1996/06/04 10:19:01 adam
116 * Minor changes - removed include of ctype.h.
118 * Revision 1.22 1996/06/03 10:15:27 quinn
119 * Various character-mapping.
121 * Revision 1.21 1996/05/31 13:27:24 quinn
122 * Character-conversion in phrases, too.
124 * Revision 1.19 1996/05/16 15:31:14 quinn
127 * Revision 1.18 1996/05/09 07:28:56 quinn
128 * Work towards phrases and multiple registers
130 * Revision 1.17 1996/05/01 13:46:37 adam
131 * First work on multiple records in one file.
132 * New option, -offset, to the "unread" command in the filter module.
134 * Revision 1.16 1996/01/17 14:57:54 adam
135 * Prototype changed for reader functions in extract/retrieve. File
136 * is identified by 'void *' instead of 'int.
138 * Revision 1.15 1996/01/08 19:15:47 adam
139 * New input filter that works!
141 * Revision 1.14 1995/12/15 12:36:11 adam
142 * Retrieval calls data1_read_regx when subType is specified.
144 * Revision 1.13 1995/12/15 12:24:43 quinn
145 * *** empty log message ***
147 * Revision 1.12 1995/12/15 12:20:28 quinn
148 * *** empty log message ***
150 * Revision 1.11 1995/12/15 12:07:57 quinn
151 * Changed extraction strategy.
153 * Revision 1.10 1995/12/14 11:10:48 quinn
156 * Revision 1.9 1995/12/13 17:14:05 quinn
157 * *** empty log message ***
159 * Revision 1.8 1995/12/13 15:33:18 quinn
160 * *** empty log message ***
162 * Revision 1.7 1995/12/13 13:45:39 quinn
163 * Changed data1 to use nmem.
165 * Revision 1.6 1995/12/04 14:22:30 adam
166 * Extra arg to recType_byName.
167 * Started work on new regular expression parsed input to
168 * structured records.
170 * Revision 1.5 1995/11/28 14:18:37 quinn
173 * Revision 1.4 1995/11/21 13:14:49 quinn
174 * Fixed end-of-data-field problem (maybe).
176 * Revision 1.3 1995/11/15 19:13:09 adam
177 * Work on record management.
183 #include <sys/types.h>
194 #define GRS_MAX_WORD 512
200 struct grs_handler *next;
203 struct grs_handlers {
204 struct grs_handler *handlers;
207 static int read_grs_type (struct grs_handlers *h,
208 struct grs_read_info *p, const char *type,
211 struct grs_handler *gh = h->handlers;
212 const char *cp = strchr (type, '.');
214 if (cp == NULL || cp == type)
216 cp = strlen(type) + type;
220 strcpy (p->type, cp+1);
221 for (gh = h->handlers; gh; gh = gh->next)
223 if (!memcmp (type, gh->type->type, cp-type))
228 gh->clientData = (*gh->type->init)();
230 p->clientData = gh->clientData;
231 *root = (gh->type->read)(p);
232 gh->clientData = p->clientData;
239 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
241 struct grs_handler *gh = malloc (sizeof(*gh));
242 gh->next = h->handlers;
249 static void *grs_init(RecType recType)
251 struct grs_handlers *h = malloc (sizeof(*h));
254 grs_add_handler (h, recTypeGrs_sgml);
255 grs_add_handler (h, recTypeGrs_regx);
257 grs_add_handler (h, recTypeGrs_tcl);
259 grs_add_handler (h, recTypeGrs_marc);
263 static void grs_destroy(void *clientData)
265 struct grs_handlers *h = clientData;
266 struct grs_handler *gh = h->handlers, *gh_next;
271 (*gh->type->destroy)(gh->clientData);
278 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
281 (*p->init)(p, &wrd); /* set defaults */
282 for (; n; n = n->next)
284 if (p->flagShowRecords) /* display element description to user */
286 if (n->which == DATA1N_root)
288 printf("%*s", level * 4, "");
289 printf("Record type: '%s'\n", n->u.root.absyn->name);
291 else if (n->which == DATA1N_tag)
295 printf("%*s", level * 4, "");
296 if (!(e = n->u.tag.element))
297 printf("Local tag: '%s'\n", n->u.tag.tag);
300 printf("Elm: '%s' ", e->name);
303 data1_tag *t = e->tag;
305 printf("TagNam: '%s' ", t->names->name);
308 printf("%s[%d],", t->tagset->name, t->tagset->type);
311 if (t->which == DATA1T_numeric)
312 printf("%d)", t->value.numeric);
314 printf("'%s')", t->value.string);
322 if (dumpkeys(n->child, p, level + 1) < 0)
325 if (n->which == DATA1N_data)
327 data1_node *par = get_parent_tag(p->dh, n);
328 data1_termlist *tlist = 0;
329 data1_datatype dtype = DATA1K_string;
331 if (p->flagShowRecords)
333 printf("%*s", level * 4, "");
335 if (n->u.data.len > 32)
336 printf("'%.24s ... %.6s'\n", n->u.data.data,
337 n->u.data.data + n->u.data.len-6);
338 else if (n->u.data.len > 0)
339 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
347 * cycle up towards the root until we find a tag with an att..
348 * this has the effect of indexing locally defined tags with
349 * the attribute of their ancestor in the record.
352 while (!par->u.tag.element)
353 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
355 if (!par || !(tlist = par->u.tag.element->termlists))
357 if (par->u.tag.element->tag)
358 dtype = par->u.tag.element->tag->kind;
359 for (; tlist; tlist = tlist->next)
361 if (p->flagShowRecords)
363 printf("%*sIdx: [%s]", (level + 1) * 4, "",
365 printf("%s:%s [%d]\n",
366 tlist->att->parent->name,
367 tlist->att->name, tlist->att->value);
371 wrd.reg_type = *tlist->structure;
372 wrd.string = n->u.data.data;
373 wrd.length = n->u.data.len;
374 wrd.attrSet = (int) (tlist->att->parent->reference);
375 wrd.attrUse = tlist->att->locals->local;
380 if (p->flagShowRecords && n->which == DATA1N_root)
382 printf("%*s-------------\n\n", level * 4, "");
388 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
391 int oidtmp[OID_SIZE];
393 oe.proto = PROTO_Z3950;
394 oe.oclass = CLASS_SCHEMA;
395 oe.value = n->u.root.absyn->reference;
397 if ((oid_ent_to_oid (&oe, oidtmp)))
398 (*p->addSchema)(p, oidtmp);
400 return dumpkeys(n, p, 0);
403 static int grs_extract(void *clientData, struct recExtractCtrl *p)
407 struct grs_read_info gri;
409 int oidtmp[OID_SIZE];
410 struct grs_handlers *h = clientData;
412 mem = nmem_create ();
413 gri.readf = p->readf;
414 gri.seekf = p->seekf;
415 gri.tellf = p->tellf;
418 gri.offset = p->offset;
422 if (read_grs_type (h, &gri, p->subType, &n))
423 return RECCTRL_EXTRACT_ERROR;
425 return RECCTRL_EXTRACT_EOF;
427 oe.proto = PROTO_Z3950;
428 oe.oclass = CLASS_SCHEMA;
429 oe.value = n->u.root.absyn->reference;
430 if ((oid_ent_to_oid (&oe, oidtmp)))
431 (*p->addSchema)(p, oidtmp);
433 if (dumpkeys(n, p, 0) < 0)
435 data1_free_tree(p->dh, n);
436 return RECCTRL_EXTRACT_ERROR;
438 data1_free_tree(p->dh, n);
440 return RECCTRL_EXTRACT_OK;
444 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
446 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
448 data1_esetname *eset;
454 case Z_RecordComp_simple:
455 if (c->u.simple->which != Z_ElementSetNames_generic)
456 return 26; /* only generic form supported. Fix this later */
457 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
458 c->u.simple->u.generic)))
460 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
461 return 25; /* invalid esetname */
463 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
464 c->u.simple->u.generic);
467 case Z_RecordComp_complex:
468 if (c->u.complex->generic)
470 /* insert check for schema */
471 if ((p = c->u.complex->generic->elementSpec))
474 case Z_ElementSpec_elementSetName:
476 data1_getesetbyname(dh,
478 p->u.elementSetName)))
480 logf(LOG_LOG, "Unknown esetname '%s'",
481 p->u.elementSetName);
482 return 25; /* invalid esetname */
484 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
485 p->u.elementSetName);
488 case Z_ElementSpec_externalSpec:
489 if (p->u.externalSpec->which == Z_External_espec1)
491 logf(LOG_DEBUG, "Got Espec-1");
492 espec = p->u.externalSpec-> u.espec1;
496 logf(LOG_LOG, "Unknown external espec.");
497 return 25; /* bad. what is proper diagnostic? */
507 logf (LOG_DEBUG, "Element: Espec-1 match");
508 return data1_doespec1(dh, n, espec);
512 logf (LOG_DEBUG, "Element: all match");
517 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
519 data1_node *node = 0, *onode = 0;
522 int res, selected = 0;
524 struct grs_read_info gri;
526 struct grs_handlers *h = clientData;
529 gri.readf = p->readf;
530 gri.seekf = p->seekf;
531 gri.tellf = p->tellf;
538 logf (LOG_DEBUG, "grs_retrieve");
539 if (read_grs_type (h, &gri, p->subType, &node))
551 logf (LOG_DEBUG, "grs_retrieve: size");
552 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
555 dnew->u.data.what = DATA1I_text;
556 dnew->u.data.data = dnew->lbuf;
557 sprintf(dnew->u.data.data, "%d", p->recordSize);
558 dnew->u.data.len = strlen(dnew->u.data.data);
561 tagname = res_get_def(p->res, "tagrank", "rank");
562 if (strcmp(tagname, "0") && p->score >= 0 && (dnew =
563 data1_insert_taggeddata(p->dh, node,
567 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
568 dnew->u.data.what = DATA1I_num;
569 dnew->u.data.data = dnew->lbuf;
570 sprintf(dnew->u.data.data, "%d", p->score);
571 dnew->u.data.len = strlen(dnew->u.data.data);
574 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
575 if (strcmp(tagname, "0") && p->localno > 0 &&
576 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
578 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
579 dnew->u.data.what = DATA1I_text;
580 dnew->u.data.data = dnew->lbuf;
581 sprintf(dnew->u.data.data, "%d", p->localno);
582 dnew->u.data.len = strlen(dnew->u.data.data);
585 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
586 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
587 node->u.root.absyn->reference != VAL_NONE)
591 int oidtmp[OID_SIZE];
593 oe.proto = PROTO_Z3950;
594 oe.oclass = CLASS_SCHEMA;
595 oe.value = node->u.root.absyn->reference;
597 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
600 data1_handle dh = p->dh;
604 for (ii = oid; *ii >= 0; ii++)
608 sprintf(p, "%d", *ii);
613 if ((dnew = data1_insert_taggeddata(dh, node, node,
614 "schemaIdentifier", mem)))
616 dnew->u.data.what = DATA1I_oid;
617 dnew->u.data.data = nmem_malloc(mem, p - tmp);
618 memcpy(dnew->u.data.data, tmp, p - tmp);
619 dnew->u.data.len = p - tmp;
624 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
626 * Does the requested format match a known schema-mapping? (this reflects
627 * the overlap of schema and formatting which is inherent in the MARC
629 * NOTE: This should look at the schema-specification in the compspec
632 for (map = node->u.root.absyn->maptabs; map; map = map->next)
633 if (map->target_absyn_ref == p->input_format)
636 if (!(node = data1_map_record(p->dh, onode, map, mem)))
645 logf (LOG_DEBUG, "grs_retrieve: element spec");
646 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
650 data1_free_tree(p->dh, onode);
651 data1_free_tree(p->dh, node);
655 else if (p->comp && !res)
658 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
659 switch (p->output_format = (p->input_format != VAL_NONE ?
660 p->input_format : VAL_SUTRS))
662 data1_marctab *marctab;
667 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
669 p->diagnostic = 238; /* not available in requested syntax */
674 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
681 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
688 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
693 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
694 memcpy (new_buf, p->rec_buf, p->rec_len);
695 p->rec_buf = new_buf;
699 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
704 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
705 memcpy (new_buf, p->rec_buf, p->rec_len);
706 p->rec_buf = new_buf;
710 if (!node->u.root.absyn)
715 for (marctab = node->u.root.absyn->marc; marctab;
716 marctab = marctab->next)
717 if (marctab->reference == p->input_format)
724 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
730 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
731 memcpy (new_buf, p->rec_buf, p->rec_len);
732 p->rec_buf = new_buf;
736 data1_free_tree(p->dh, node);
738 data1_free_tree(p->dh, onode);
743 static struct recType grs_type =
752 RecType recTypeGrs = &grs_type;