2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.16 1998-01-29 13:38:17 adam
8 * Fixed problem with mapping to record with unknown schema.
10 * Revision 1.15 1998/01/26 10:37:57 adam
13 * Revision 1.14 1997/11/06 11:41:01 adam
14 * Implemented "begin variant" for the sgml.regx filter.
16 * Revision 1.13 1997/10/31 12:35:44 adam
17 * Added a few log statements.
19 * Revision 1.12 1997/10/29 12:02:22 adam
20 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
22 * Revision 1.11 1997/10/27 14:34:00 adam
23 * Work on generic character mapping depending on "structure" field
24 * in abstract syntax file.
26 * Revision 1.10 1997/09/18 08:59:21 adam
27 * Extra generic handle for the character mapping routines.
29 * Revision 1.9 1997/09/17 12:19:21 adam
30 * Zebra version corresponds to YAZ version 1.4.
31 * Changed Zebra server so that it doesn't depend on global common_resource.
33 * Revision 1.8 1997/09/09 13:38:14 adam
34 * Partial port to WIN95/NT.
36 * Revision 1.7 1997/09/05 15:30:10 adam
37 * Changed prototype for chr_map_input - added const.
38 * Added support for C++, headers uses extern "C" for public definitions.
40 * Revision 1.6 1997/09/04 13:54:40 adam
41 * Added MARC filter - type grs.marc.<syntax> where syntax refers
42 * to abstract syntax. New method tellf in retrieve/extract method.
44 * Revision 1.5 1997/07/15 16:29:03 adam
45 * Initialized dummy variable to keep checker gcc happy.
47 * Revision 1.4 1997/04/30 08:56:08 quinn
50 * Revision 1.2 1996/10/11 16:06:43 quinn
51 * Revision 1.3 1997/02/24 10:41:50 adam
52 * Cleanup of code and commented out the "end element-end-record" code.
54 * Revision 1.2 1996/10/11 16:06:43 quinn
55 * Fixed arguments to nodetogr
57 * Revision 1.1 1996/10/11 10:57:25 adam
58 * New module recctrl. Used to manage records (extract/retrieval).
60 * Revision 1.29 1996/10/08 10:30:21 quinn
63 * Revision 1.28 1996/10/07 16:06:40 quinn
66 * Revision 1.27 1996/06/11 10:54:12 quinn
69 * Revision 1.26 1996/06/06 12:08:45 quinn
70 * Added showRecord function
72 * Revision 1.25 1996/06/04 14:18:53 quinn
75 * Revision 1.24 1996/06/04 13:27:54 quinn
76 * More work on charmapping
78 * Revision 1.23 1996/06/04 10:19:01 adam
79 * Minor changes - removed include of ctype.h.
81 * Revision 1.22 1996/06/03 10:15:27 quinn
82 * Various character-mapping.
84 * Revision 1.21 1996/05/31 13:27:24 quinn
85 * Character-conversion in phrases, too.
87 * Revision 1.19 1996/05/16 15:31:14 quinn
90 * Revision 1.18 1996/05/09 07:28:56 quinn
91 * Work towards phrases and multiple registers
93 * Revision 1.17 1996/05/01 13:46:37 adam
94 * First work on multiple records in one file.
95 * New option, -offset, to the "unread" command in the filter module.
97 * Revision 1.16 1996/01/17 14:57:54 adam
98 * Prototype changed for reader functions in extract/retrieve. File
99 * is identified by 'void *' instead of 'int.
101 * Revision 1.15 1996/01/08 19:15:47 adam
102 * New input filter that works!
104 * Revision 1.14 1995/12/15 12:36:11 adam
105 * Retrieval calls data1_read_regx when subType is specified.
107 * Revision 1.13 1995/12/15 12:24:43 quinn
108 * *** empty log message ***
110 * Revision 1.12 1995/12/15 12:20:28 quinn
111 * *** empty log message ***
113 * Revision 1.11 1995/12/15 12:07:57 quinn
114 * Changed extraction strategy.
116 * Revision 1.10 1995/12/14 11:10:48 quinn
119 * Revision 1.9 1995/12/13 17:14:05 quinn
120 * *** empty log message ***
122 * Revision 1.8 1995/12/13 15:33:18 quinn
123 * *** empty log message ***
125 * Revision 1.7 1995/12/13 13:45:39 quinn
126 * Changed data1 to use nmem.
128 * Revision 1.6 1995/12/04 14:22:30 adam
129 * Extra arg to recType_byName.
130 * Started work on new regular expression parsed input to
131 * structured records.
133 * Revision 1.5 1995/11/28 14:18:37 quinn
136 * Revision 1.4 1995/11/21 13:14:49 quinn
137 * Fixed end-of-data-field problem (maybe).
139 * Revision 1.3 1995/11/15 19:13:09 adam
140 * Work on record management.
146 #include <sys/types.h>
158 #define GRS_MAX_WORD 512
160 static int seqno = 0;
162 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
166 data1_node *(*func)(struct grs_read_info *p);
168 { "sgml", grs_read_sgml },
169 { "regx", grs_read_regx },
170 { "marc", grs_read_marc },
173 const char *cp = strchr (type, '.');
176 if (cp == NULL || cp == type)
178 cp = strlen(type) + type;
182 strcpy (p->type, cp+1);
183 for (i=0; tab[i].type; i++)
185 if (!memcmp (type, tab[i].type, cp-type))
186 return (tab[i].func)(p);
191 static void grs_init(void)
195 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
196 data1_att *att, int reg_type)
198 const char *b = n->u.data.data;
200 const char **map = 0;
202 remain = n->u.data.len - (b - n->u.data.data);
204 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
209 char buf[GRS_MAX_WORD+1];
213 while (map && *map && **map == *CHR_SPACE)
215 remain = n->u.data.len - (b - n->u.data.data);
217 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
224 while (map && *map && **map != *CHR_SPACE)
226 const char *cp = *map;
228 while (i < GRS_MAX_WORD && *cp)
230 remain = n->u.data.len - (b - n->u.data.data);
232 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
239 (*p->init)(&wrd); /* set defaults */
240 wrd.reg_type = reg_type;
243 wrd.attrSet = att->parent->ordinal;
244 wrd.attrUse = att->locals->local;
249 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
250 data1_att *att, int reg_type)
252 const char *b = n->u.data.data;
253 char buf[GRS_MAX_WORD+1];
254 const char **map = 0;
258 remain = n->u.data.len - (b - n->u.data.data);
260 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
262 while (remain > 0 && i < GRS_MAX_WORD)
264 while (map && *map && **map == *CHR_SPACE)
266 remain = n->u.data.len - (b - n->u.data.data);
268 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
275 if (i && i < GRS_MAX_WORD)
276 buf[i++] = *CHR_SPACE;
277 while (map && *map && **map != *CHR_SPACE)
279 const char *cp = *map;
281 if (i >= GRS_MAX_WORD)
283 while (i < GRS_MAX_WORD && *cp)
285 remain = n->u.data.len - (b - n->u.data.data);
287 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
297 wrd.reg_type = reg_type;
300 wrd.attrSet = att->parent->ordinal;
301 wrd.attrUse = att->locals->local;
305 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
307 for (; n; n = n->next)
309 if (p->flagShowRecords) /* display element description to user */
311 if (n->which == DATA1N_root)
313 printf("%*s", level * 4, "");
314 printf("Record type: '%s'\n", n->u.root.absyn->name);
316 else if (n->which == DATA1N_tag)
320 printf("%*s", level * 4, "");
321 if (!(e = n->u.tag.element))
322 printf("Local tag: '%s'\n", n->u.tag.tag);
325 printf("Elm: '%s' ", e->name);
328 data1_tag *t = e->tag;
330 printf("TagNam: '%s' ", t->names->name);
333 printf("%s[%d],", t->tagset->name, t->tagset->type);
336 if (t->which == DATA1T_numeric)
337 printf("%d)", t->value.numeric);
339 printf("'%s')", t->value.string);
347 if (dumpkeys(n->child, p, level + 1) < 0)
350 if (n->which == DATA1N_data)
352 data1_node *par = get_parent_tag(p->dh, n);
353 data1_termlist *tlist = 0;
354 data1_datatype dtype = DATA1K_string;
356 if (p->flagShowRecords)
358 printf("%*s", level * 4, "");
360 if (n->u.data.len > 20)
361 printf("'%.20s...'\n", n->u.data.data);
362 else if (n->u.data.len > 0)
363 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
371 * cycle up towards the root until we find a tag with an att..
372 * this has the effect of indexing locally defined tags with
373 * the attribute of their ancestor in the record.
376 while (!par->u.tag.element)
377 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
379 if (!par || !(tlist = par->u.tag.element->termlists))
381 if (par->u.tag.element->tag)
382 dtype = par->u.tag.element->tag->kind;
383 for (; tlist; tlist = tlist->next)
385 if (p->flagShowRecords)
387 printf("%*sIdx: [%s]", (level + 1) * 4, "",
389 printf("%s:%s [%d]\n",
390 tlist->att->parent->name,
391 tlist->att->name, tlist->att->value);
394 if (zebra_maps_is_complete (p->zebra_maps,
396 dumpkeys_complete_field(n, p, tlist->att,
399 dumpkeys_incomplete_field(n, p, tlist->att,
403 if (p->flagShowRecords && n->which == DATA1N_root)
405 printf("%*s-------------\n\n", level * 4, "");
411 static int grs_extract(struct recExtractCtrl *p)
415 struct grs_read_info gri;
418 mem = nmem_create ();
419 gri.readf = p->readf;
420 gri.seekf = p->seekf;
421 gri.tellf = p->tellf;
424 gri.offset = p->offset;
428 n = read_grs_type (&gri, p->subType);
431 if (dumpkeys(n, p, 0) < 0)
433 data1_free_tree(p->dh, n);
436 data1_free_tree(p->dh, n);
442 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
444 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
446 data1_esetname *eset;
452 case Z_RecordComp_simple:
453 if (c->u.simple->which != Z_ElementSetNames_generic)
454 return 26; /* only generic form supported. Fix this later */
455 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
456 c->u.simple->u.generic)))
458 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
459 return 25; /* invalid esetname */
461 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
462 c->u.simple->u.generic);
465 case Z_RecordComp_complex:
466 if (c->u.complex->generic)
468 /* insert check for schema */
469 if ((p = c->u.complex->generic->elementSpec))
472 case Z_ElementSpec_elementSetName:
474 data1_getesetbyname(dh,
476 p->u.elementSetName)))
478 logf(LOG_LOG, "Unknown esetname '%s'",
479 p->u.elementSetName);
480 return 25; /* invalid esetname */
482 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
483 p->u.elementSetName);
486 case Z_ElementSpec_externalSpec:
487 if (p->u.externalSpec->which == Z_External_espec1)
489 logf(LOG_DEBUG, "Got Espec-1");
490 espec = p->u.externalSpec-> u.espec1;
494 logf(LOG_LOG, "Unknown external espec.");
495 return 25; /* bad. what is proper diagnostic? */
505 logf (LOG_LOG, "Element: Espec-1 match");
506 return data1_doespec1(dh, n, espec);
510 logf (LOG_DEBUG, "Element: all match");
515 static int grs_retrieve(struct recRetrieveCtrl *p)
517 data1_node *node = 0, *onode = 0;
520 int res, selected = 0;
522 struct grs_read_info gri;
525 gri.readf = p->readf;
526 gri.seekf = p->seekf;
527 gri.tellf = p->tellf;
534 logf (LOG_DEBUG, "grs_retrieve");
535 node = read_grs_type (&gri, p->subType);
542 logf (LOG_DEBUG, "grs_retrieve: score");
543 if (p->score >= 0 && (new =
544 data1_insert_taggeddata(p->dh, node,
548 new->u.data.what = DATA1I_num;
549 new->u.data.data = new->lbuf;
550 sprintf(new->u.data.data, "%d", p->score);
551 new->u.data.len = strlen(new->u.data.data);
553 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
554 if ((new = data1_insert_taggeddata(p->dh, node, node,
555 "localControlNumber", mem)))
557 new->u.data.what = DATA1I_text;
558 new->u.data.data = new->lbuf;
559 sprintf(new->u.data.data, "%d", p->localno);
560 new->u.data.len = strlen(new->u.data.data);
562 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
563 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
564 node->u.root.absyn->reference != VAL_NONE)
568 int oidtmp[OID_SIZE];
570 oe.proto = PROTO_Z3950;
571 oe.oclass = CLASS_SCHEMA;
572 oe.value = node->u.root.absyn->reference;
574 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
577 data1_handle dh = p->dh;
581 for (ii = oid; *ii >= 0; ii++)
585 sprintf(p, "%d", *ii);
590 if ((new = data1_insert_taggeddata(dh, node, node,
591 "schemaIdentifier", mem)))
593 new->u.data.what = DATA1I_oid;
594 new->u.data.data = nmem_malloc(mem, p - tmp);
595 memcpy(new->u.data.data, tmp, p - tmp);
596 new->u.data.len = p - tmp;
601 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
603 * Does the requested format match a known schema-mapping? (this reflects
604 * the overlap of schema and formatting which is inherent in the MARC
606 * NOTE: This should look at the schema-specification in the compspec
609 for (map = node->u.root.absyn->maptabs; map; map = map->next)
610 if (map->target_absyn_ref == p->input_format)
613 if (!(node = data1_map_record(p->dh, onode, map, mem)))
622 logf (LOG_DEBUG, "grs_retrieve: element spec");
623 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
627 data1_free_tree(p->dh, onode);
628 data1_free_tree(p->dh, node);
632 else if (p->comp && !res)
635 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
636 switch (p->output_format = (p->input_format != VAL_NONE ?
637 p->input_format : VAL_SUTRS))
639 data1_marctab *marctab;
644 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
646 p->diagnostic = 238; /* not available in requested syntax */
651 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
658 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
665 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
670 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
675 if (!node->u.root.absyn)
680 for (marctab = node->u.root.absyn->marc; marctab;
681 marctab = marctab->next)
682 if (marctab->reference == p->input_format)
689 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
698 data1_free_tree(p->dh, node);
700 data1_free_tree(p->dh, onode);
705 static struct recType grs_type =
713 RecType recTypeGrs = &grs_type;