2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.15 1998-01-26 10:37:57 adam
10 * Revision 1.14 1997/11/06 11:41:01 adam
11 * Implemented "begin variant" for the sgml.regx filter.
13 * Revision 1.13 1997/10/31 12:35:44 adam
14 * Added a few log statements.
16 * Revision 1.12 1997/10/29 12:02:22 adam
17 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
19 * Revision 1.11 1997/10/27 14:34:00 adam
20 * Work on generic character mapping depending on "structure" field
21 * in abstract syntax file.
23 * Revision 1.10 1997/09/18 08:59:21 adam
24 * Extra generic handle for the character mapping routines.
26 * Revision 1.9 1997/09/17 12:19:21 adam
27 * Zebra version corresponds to YAZ version 1.4.
28 * Changed Zebra server so that it doesn't depend on global common_resource.
30 * Revision 1.8 1997/09/09 13:38:14 adam
31 * Partial port to WIN95/NT.
33 * Revision 1.7 1997/09/05 15:30:10 adam
34 * Changed prototype for chr_map_input - added const.
35 * Added support for C++, headers uses extern "C" for public definitions.
37 * Revision 1.6 1997/09/04 13:54:40 adam
38 * Added MARC filter - type grs.marc.<syntax> where syntax refers
39 * to abstract syntax. New method tellf in retrieve/extract method.
41 * Revision 1.5 1997/07/15 16:29:03 adam
42 * Initialized dummy variable to keep checker gcc happy.
44 * Revision 1.4 1997/04/30 08:56:08 quinn
47 * Revision 1.2 1996/10/11 16:06:43 quinn
48 * Revision 1.3 1997/02/24 10:41:50 adam
49 * Cleanup of code and commented out the "end element-end-record" code.
51 * Revision 1.2 1996/10/11 16:06:43 quinn
52 * Fixed arguments to nodetogr
54 * Revision 1.1 1996/10/11 10:57:25 adam
55 * New module recctrl. Used to manage records (extract/retrieval).
57 * Revision 1.29 1996/10/08 10:30:21 quinn
60 * Revision 1.28 1996/10/07 16:06:40 quinn
63 * Revision 1.27 1996/06/11 10:54:12 quinn
66 * Revision 1.26 1996/06/06 12:08:45 quinn
67 * Added showRecord function
69 * Revision 1.25 1996/06/04 14:18:53 quinn
72 * Revision 1.24 1996/06/04 13:27:54 quinn
73 * More work on charmapping
75 * Revision 1.23 1996/06/04 10:19:01 adam
76 * Minor changes - removed include of ctype.h.
78 * Revision 1.22 1996/06/03 10:15:27 quinn
79 * Various character-mapping.
81 * Revision 1.21 1996/05/31 13:27:24 quinn
82 * Character-conversion in phrases, too.
84 * Revision 1.19 1996/05/16 15:31:14 quinn
87 * Revision 1.18 1996/05/09 07:28:56 quinn
88 * Work towards phrases and multiple registers
90 * Revision 1.17 1996/05/01 13:46:37 adam
91 * First work on multiple records in one file.
92 * New option, -offset, to the "unread" command in the filter module.
94 * Revision 1.16 1996/01/17 14:57:54 adam
95 * Prototype changed for reader functions in extract/retrieve. File
96 * is identified by 'void *' instead of 'int.
98 * Revision 1.15 1996/01/08 19:15:47 adam
99 * New input filter that works!
101 * Revision 1.14 1995/12/15 12:36:11 adam
102 * Retrieval calls data1_read_regx when subType is specified.
104 * Revision 1.13 1995/12/15 12:24:43 quinn
105 * *** empty log message ***
107 * Revision 1.12 1995/12/15 12:20:28 quinn
108 * *** empty log message ***
110 * Revision 1.11 1995/12/15 12:07:57 quinn
111 * Changed extraction strategy.
113 * Revision 1.10 1995/12/14 11:10:48 quinn
116 * Revision 1.9 1995/12/13 17:14:05 quinn
117 * *** empty log message ***
119 * Revision 1.8 1995/12/13 15:33:18 quinn
120 * *** empty log message ***
122 * Revision 1.7 1995/12/13 13:45:39 quinn
123 * Changed data1 to use nmem.
125 * Revision 1.6 1995/12/04 14:22:30 adam
126 * Extra arg to recType_byName.
127 * Started work on new regular expression parsed input to
128 * structured records.
130 * Revision 1.5 1995/11/28 14:18:37 quinn
133 * Revision 1.4 1995/11/21 13:14:49 quinn
134 * Fixed end-of-data-field problem (maybe).
136 * Revision 1.3 1995/11/15 19:13:09 adam
137 * Work on record management.
143 #include <sys/types.h>
155 #define GRS_MAX_WORD 512
157 static int seqno = 0;
159 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
163 data1_node *(*func)(struct grs_read_info *p);
165 { "sgml", grs_read_sgml },
166 { "regx", grs_read_regx },
167 { "marc", grs_read_marc },
170 const char *cp = strchr (type, '.');
173 if (cp == NULL || cp == type)
175 cp = strlen(type) + type;
179 strcpy (p->type, cp+1);
180 for (i=0; tab[i].type; i++)
182 if (!memcmp (type, tab[i].type, cp-type))
183 return (tab[i].func)(p);
188 static void grs_init(void)
192 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
193 data1_att *att, int reg_type)
195 const char *b = n->u.data.data;
197 const char **map = 0;
199 remain = n->u.data.len - (b - n->u.data.data);
201 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
206 char buf[GRS_MAX_WORD+1];
210 while (map && *map && **map == *CHR_SPACE)
212 remain = n->u.data.len - (b - n->u.data.data);
214 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
221 while (map && *map && **map != *CHR_SPACE)
223 const char *cp = *map;
225 while (i < GRS_MAX_WORD && *cp)
227 remain = n->u.data.len - (b - n->u.data.data);
229 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
236 (*p->init)(&wrd); /* set defaults */
237 wrd.reg_type = reg_type;
240 wrd.attrSet = att->parent->ordinal;
241 wrd.attrUse = att->locals->local;
246 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
247 data1_att *att, int reg_type)
249 const char *b = n->u.data.data;
250 char buf[GRS_MAX_WORD+1];
251 const char **map = 0;
255 remain = n->u.data.len - (b - n->u.data.data);
257 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
259 while (remain > 0 && i < GRS_MAX_WORD)
261 while (map && *map && **map == *CHR_SPACE)
263 remain = n->u.data.len - (b - n->u.data.data);
265 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
272 if (i && i < GRS_MAX_WORD)
273 buf[i++] = *CHR_SPACE;
274 while (map && *map && **map != *CHR_SPACE)
276 const char *cp = *map;
278 if (i >= GRS_MAX_WORD)
280 while (i < GRS_MAX_WORD && *cp)
282 remain = n->u.data.len - (b - n->u.data.data);
284 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
294 wrd.reg_type = reg_type;
297 wrd.attrSet = att->parent->ordinal;
298 wrd.attrUse = att->locals->local;
302 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
304 for (; n; n = n->next)
306 if (p->flagShowRecords) /* display element description to user */
308 if (n->which == DATA1N_root)
310 printf("%*s", level * 4, "");
311 printf("Record type: '%s'\n", n->u.root.absyn->name);
313 else if (n->which == DATA1N_tag)
317 printf("%*s", level * 4, "");
318 if (!(e = n->u.tag.element))
319 printf("Local tag: '%s'\n", n->u.tag.tag);
322 printf("Elm: '%s' ", e->name);
325 data1_tag *t = e->tag;
327 printf("TagNam: '%s' ", t->names->name);
330 printf("%s[%d],", t->tagset->name, t->tagset->type);
333 if (t->which == DATA1T_numeric)
334 printf("%d)", t->value.numeric);
336 printf("'%s')", t->value.string);
344 if (dumpkeys(n->child, p, level + 1) < 0)
347 if (n->which == DATA1N_data)
349 data1_node *par = get_parent_tag(p->dh, n);
350 data1_termlist *tlist = 0;
351 data1_datatype dtype = DATA1K_string;
353 if (p->flagShowRecords)
355 printf("%*s", level * 4, "");
357 if (n->u.data.len > 20)
358 printf("'%.20s...'\n", n->u.data.data);
359 else if (n->u.data.len > 0)
360 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
368 * cycle up towards the root until we find a tag with an att..
369 * this has the effect of indexing locally defined tags with
370 * the attribute of their ancestor in the record.
373 while (!par->u.tag.element)
374 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
376 if (!par || !(tlist = par->u.tag.element->termlists))
378 if (par->u.tag.element->tag)
379 dtype = par->u.tag.element->tag->kind;
380 for (; tlist; tlist = tlist->next)
382 if (p->flagShowRecords)
384 printf("%*sIdx: [%s]", (level + 1) * 4, "",
386 printf("%s:%s [%d]\n",
387 tlist->att->parent->name,
388 tlist->att->name, tlist->att->value);
391 if (zebra_maps_is_complete (p->zebra_maps,
393 dumpkeys_complete_field(n, p, tlist->att,
396 dumpkeys_incomplete_field(n, p, tlist->att,
400 if (p->flagShowRecords && n->which == DATA1N_root)
402 printf("%*s-------------\n\n", level * 4, "");
408 static int grs_extract(struct recExtractCtrl *p)
412 struct grs_read_info gri;
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 n = read_grs_type (&gri, p->subType);
428 if (dumpkeys(n, p, 0) < 0)
430 data1_free_tree(p->dh, n);
433 data1_free_tree(p->dh, n);
439 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
441 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
443 data1_esetname *eset;
449 case Z_RecordComp_simple:
450 if (c->u.simple->which != Z_ElementSetNames_generic)
451 return 26; /* only generic form supported. Fix this later */
452 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
453 c->u.simple->u.generic)))
455 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
456 return 25; /* invalid esetname */
458 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
459 c->u.simple->u.generic);
462 case Z_RecordComp_complex:
463 if (c->u.complex->generic)
465 /* insert check for schema */
466 if ((p = c->u.complex->generic->elementSpec))
469 case Z_ElementSpec_elementSetName:
471 data1_getesetbyname(dh,
473 p->u.elementSetName)))
475 logf(LOG_LOG, "Unknown esetname '%s'",
476 p->u.elementSetName);
477 return 25; /* invalid esetname */
479 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
480 p->u.elementSetName);
483 case Z_ElementSpec_externalSpec:
484 if (p->u.externalSpec->which == Z_External_espec1)
486 logf(LOG_DEBUG, "Got Espec-1");
487 espec = p->u.externalSpec-> u.espec1;
491 logf(LOG_LOG, "Unknown external espec.");
492 return 25; /* bad. what is proper diagnostic? */
502 logf (LOG_LOG, "Element: Espec-1 match");
503 return data1_doespec1(dh, n, espec);
507 logf (LOG_DEBUG, "Element: all match");
512 static int grs_retrieve(struct recRetrieveCtrl *p)
514 data1_node *node = 0, *onode = 0;
517 int res, selected = 0;
519 struct grs_read_info gri;
522 gri.readf = p->readf;
523 gri.seekf = p->seekf;
524 gri.tellf = p->tellf;
531 logf (LOG_DEBUG, "grs_retrieve");
532 node = read_grs_type (&gri, p->subType);
539 logf (LOG_DEBUG, "grs_retrieve: score");
540 if (p->score >= 0 && (new =
541 data1_insert_taggeddata(p->dh, node,
545 new->u.data.what = DATA1I_num;
546 new->u.data.data = new->lbuf;
547 sprintf(new->u.data.data, "%d", p->score);
548 new->u.data.len = strlen(new->u.data.data);
550 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
551 if ((new = data1_insert_taggeddata(p->dh, node, node,
552 "localControlNumber", mem)))
554 new->u.data.what = DATA1I_text;
555 new->u.data.data = new->lbuf;
556 sprintf(new->u.data.data, "%d", p->localno);
557 new->u.data.len = strlen(new->u.data.data);
559 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
560 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
561 node->u.root.absyn->reference != VAL_NONE)
565 int oidtmp[OID_SIZE];
567 oe.proto = PROTO_Z3950;
568 oe.oclass = CLASS_SCHEMA;
569 oe.value = node->u.root.absyn->reference;
571 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
574 data1_handle dh = p->dh;
578 for (ii = oid; *ii >= 0; ii++)
582 sprintf(p, "%d", *ii);
587 if ((new = data1_insert_taggeddata(dh, node, node,
588 "schemaIdentifier", mem)))
590 new->u.data.what = DATA1I_oid;
591 new->u.data.data = nmem_malloc(mem, p - tmp);
592 memcpy(new->u.data.data, tmp, p - tmp);
593 new->u.data.len = p - tmp;
598 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
600 * Does the requested format match a known schema-mapping? (this reflects
601 * the overlap of schema and formatting which is inherent in the MARC
603 * NOTE: This should look at the schema-specification in the compspec
606 for (map = node->u.root.absyn->maptabs; map; map = map->next)
607 if (map->target_absyn_ref == p->input_format)
610 if (!(node = data1_map_record(p->dh, onode, map, mem)))
619 logf (LOG_DEBUG, "grs_retrieve: element spec");
620 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
624 data1_free_tree(p->dh, onode);
625 data1_free_tree(p->dh, node);
629 else if (p->comp && !res)
632 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
633 switch (p->output_format = (p->input_format != VAL_NONE ?
634 p->input_format : VAL_SUTRS))
636 data1_marctab *marctab;
641 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
643 p->diagnostic = 238; /* not available in requested syntax */
648 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
655 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
662 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
667 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
672 for (marctab = node->u.root.absyn->marc; marctab;
673 marctab = marctab->next)
674 if (marctab->reference == p->input_format)
681 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
690 data1_free_tree(p->dh, node);
692 data1_free_tree(p->dh, onode);
697 static struct recType grs_type =
705 RecType recTypeGrs = &grs_type;