2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 1997-10-31 12:35:44 adam
8 * Added a few log statements.
10 * Revision 1.12 1997/10/29 12:02:22 adam
11 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
13 * Revision 1.11 1997/10/27 14:34:00 adam
14 * Work on generic character mapping depending on "structure" field
15 * in abstract syntax file.
17 * Revision 1.10 1997/09/18 08:59:21 adam
18 * Extra generic handle for the character mapping routines.
20 * Revision 1.9 1997/09/17 12:19:21 adam
21 * Zebra version corresponds to YAZ version 1.4.
22 * Changed Zebra server so that it doesn't depend on global common_resource.
24 * Revision 1.8 1997/09/09 13:38:14 adam
25 * Partial port to WIN95/NT.
27 * Revision 1.7 1997/09/05 15:30:10 adam
28 * Changed prototype for chr_map_input - added const.
29 * Added support for C++, headers uses extern "C" for public definitions.
31 * Revision 1.6 1997/09/04 13:54:40 adam
32 * Added MARC filter - type grs.marc.<syntax> where syntax refers
33 * to abstract syntax. New method tellf in retrieve/extract method.
35 * Revision 1.5 1997/07/15 16:29:03 adam
36 * Initialized dummy variable to keep checker gcc happy.
38 * Revision 1.4 1997/04/30 08:56:08 quinn
41 * Revision 1.2 1996/10/11 16:06:43 quinn
42 * Revision 1.3 1997/02/24 10:41:50 adam
43 * Cleanup of code and commented out the "end element-end-record" code.
45 * Revision 1.2 1996/10/11 16:06:43 quinn
46 * Fixed arguments to nodetogr
48 * Revision 1.1 1996/10/11 10:57:25 adam
49 * New module recctrl. Used to manage records (extract/retrieval).
51 * Revision 1.29 1996/10/08 10:30:21 quinn
54 * Revision 1.28 1996/10/07 16:06:40 quinn
57 * Revision 1.27 1996/06/11 10:54:12 quinn
60 * Revision 1.26 1996/06/06 12:08:45 quinn
61 * Added showRecord function
63 * Revision 1.25 1996/06/04 14:18:53 quinn
66 * Revision 1.24 1996/06/04 13:27:54 quinn
67 * More work on charmapping
69 * Revision 1.23 1996/06/04 10:19:01 adam
70 * Minor changes - removed include of ctype.h.
72 * Revision 1.22 1996/06/03 10:15:27 quinn
73 * Various character-mapping.
75 * Revision 1.21 1996/05/31 13:27:24 quinn
76 * Character-conversion in phrases, too.
78 * Revision 1.19 1996/05/16 15:31:14 quinn
81 * Revision 1.18 1996/05/09 07:28:56 quinn
82 * Work towards phrases and multiple registers
84 * Revision 1.17 1996/05/01 13:46:37 adam
85 * First work on multiple records in one file.
86 * New option, -offset, to the "unread" command in the filter module.
88 * Revision 1.16 1996/01/17 14:57:54 adam
89 * Prototype changed for reader functions in extract/retrieve. File
90 * is identified by 'void *' instead of 'int.
92 * Revision 1.15 1996/01/08 19:15:47 adam
93 * New input filter that works!
95 * Revision 1.14 1995/12/15 12:36:11 adam
96 * Retrieval calls data1_read_regx when subType is specified.
98 * Revision 1.13 1995/12/15 12:24:43 quinn
99 * *** empty log message ***
101 * Revision 1.12 1995/12/15 12:20:28 quinn
102 * *** empty log message ***
104 * Revision 1.11 1995/12/15 12:07:57 quinn
105 * Changed extraction strategy.
107 * Revision 1.10 1995/12/14 11:10:48 quinn
110 * Revision 1.9 1995/12/13 17:14:05 quinn
111 * *** empty log message ***
113 * Revision 1.8 1995/12/13 15:33:18 quinn
114 * *** empty log message ***
116 * Revision 1.7 1995/12/13 13:45:39 quinn
117 * Changed data1 to use nmem.
119 * Revision 1.6 1995/12/04 14:22:30 adam
120 * Extra arg to recType_byName.
121 * Started work on new regular expression parsed input to
122 * structured records.
124 * Revision 1.5 1995/11/28 14:18:37 quinn
127 * Revision 1.4 1995/11/21 13:14:49 quinn
128 * Fixed end-of-data-field problem (maybe).
130 * Revision 1.3 1995/11/15 19:13:09 adam
131 * Work on record management.
137 #include <sys/types.h>
149 #define GRS_MAX_WORD 512
151 static int seqno = 0;
153 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
157 data1_node *(*func)(struct grs_read_info *p);
159 { "sgml", grs_read_sgml },
160 { "regx", grs_read_regx },
161 { "marc", grs_read_marc },
164 const char *cp = strchr (type, '.');
167 if (cp == NULL || cp == type)
169 cp = strlen(type) + type;
173 strcpy (p->type, cp+1);
174 for (i=0; tab[i].type; i++)
176 if (!memcmp (type, tab[i].type, cp-type))
177 return (tab[i].func)(p);
182 static void grs_init(void)
186 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
187 data1_att *att, int reg_type)
189 const char *b = n->u.data.data;
191 const char **map = 0;
193 remain = n->u.data.len - (b - n->u.data.data);
195 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
200 char buf[GRS_MAX_WORD+1];
204 while (map && *map && **map == *CHR_SPACE)
206 remain = n->u.data.len - (b - n->u.data.data);
208 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
215 while (map && *map && **map != *CHR_SPACE)
217 const char *cp = *map;
219 while (i < GRS_MAX_WORD && *cp)
221 remain = n->u.data.len - (b - n->u.data.data);
223 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
230 (*p->init)(&wrd); /* set defaults */
231 wrd.reg_type = reg_type;
234 wrd.attrSet = att->parent->ordinal;
235 wrd.attrUse = att->locals->local;
240 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
241 data1_att *att, int reg_type)
243 const char *b = n->u.data.data;
244 char buf[GRS_MAX_WORD+1];
245 const char **map = 0;
249 remain = n->u.data.len - (b - n->u.data.data);
251 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
253 while (remain > 0 && i < GRS_MAX_WORD)
255 while (map && *map && **map == *CHR_SPACE)
257 remain = n->u.data.len - (b - n->u.data.data);
259 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
266 if (i && i < GRS_MAX_WORD)
267 buf[i++] = *CHR_SPACE;
268 while (map && *map && **map != *CHR_SPACE)
270 const char *cp = *map;
272 if (i >= GRS_MAX_WORD)
274 while (i < GRS_MAX_WORD && *cp)
276 remain = n->u.data.len - (b - n->u.data.data);
278 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
288 wrd.reg_type = reg_type;
291 wrd.attrSet = att->parent->ordinal;
292 wrd.attrUse = att->locals->local;
296 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
298 for (; n; n = n->next)
300 if (p->flagShowRecords) /* display element description to user */
302 if (n->which == DATA1N_root)
304 printf("%*s", level * 4, "");
305 printf("Record type: '%s'\n", n->u.root.absyn->name);
307 else if (n->which == DATA1N_tag)
311 printf("%*s", level * 4, "");
312 if (!(e = n->u.tag.element))
313 printf("Local tag: '%s'\n", n->u.tag.tag);
316 printf("Elm: '%s' ", e->name);
319 data1_tag *t = e->tag;
321 printf("TagNam: '%s' ", t->names->name);
324 printf("%s[%d],", t->tagset->name, t->tagset->type);
327 if (t->which == DATA1T_numeric)
328 printf("%d)", t->value.numeric);
330 printf("'%s')", t->value.string);
338 if (dumpkeys(n->child, p, level + 1) < 0)
341 if (n->which == DATA1N_data)
343 data1_node *par = get_parent_tag(p->dh, n);
344 data1_termlist *tlist = 0;
345 data1_datatype dtype = DATA1K_string;
347 if (p->flagShowRecords)
349 printf("%*s", level * 4, "");
351 if (n->u.data.len > 20)
352 printf("'%.20s...'\n", n->u.data.data);
353 else if (n->u.data.len > 0)
354 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
362 * cycle up towards the root until we find a tag with an att..
363 * this has the effect of indexing locally defined tags with
364 * the attribute of their ancestor in the record.
367 while (!par->u.tag.element)
368 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
370 if (!par || !(tlist = par->u.tag.element->termlists))
372 if (par->u.tag.element->tag)
373 dtype = par->u.tag.element->tag->kind;
374 for (; tlist; tlist = tlist->next)
376 if (p->flagShowRecords)
378 printf("%*sIdx: [%s]", (level + 1) * 4, "",
380 printf("%s:%s [%d]\n",
381 tlist->att->parent->name,
382 tlist->att->name, tlist->att->value);
385 if (zebra_maps_is_complete (p->zebra_maps,
387 dumpkeys_complete_field(n, p, tlist->att,
390 dumpkeys_incomplete_field(n, p, tlist->att,
394 if (p->flagShowRecords && n->which == DATA1N_root)
396 printf("%*s-------------\n\n", level * 4, "");
402 static int grs_extract(struct recExtractCtrl *p)
406 struct grs_read_info gri;
409 mem = nmem_create ();
410 gri.readf = p->readf;
411 gri.seekf = p->seekf;
412 gri.tellf = p->tellf;
415 gri.offset = p->offset;
419 n = read_grs_type (&gri, p->subType);
422 if (dumpkeys(n, p, 0) < 0)
424 data1_free_tree(p->dh, n);
427 data1_free_tree(p->dh, n);
433 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
435 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
437 data1_esetname *eset;
443 case Z_RecordComp_simple:
444 if (c->u.simple->which != Z_ElementSetNames_generic)
445 return 26; /* only generic form supported. Fix this later */
446 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
447 c->u.simple->u.generic)))
449 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
450 return 25; /* invalid esetname */
452 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
453 c->u.simple->u.generic);
456 case Z_RecordComp_complex:
457 if (c->u.complex->generic)
459 /* insert check for schema */
460 if ((p = c->u.complex->generic->elementSpec))
463 case Z_ElementSpec_elementSetName:
465 data1_getesetbyname(dh,
467 p->u.elementSetName)))
469 logf(LOG_LOG, "Unknown esetname '%s'",
470 p->u.elementSetName);
471 return 25; /* invalid esetname */
473 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
474 p->u.elementSetName);
477 case Z_ElementSpec_externalSpec:
478 if (p->u.externalSpec->which == Z_External_espec1)
480 logf(LOG_DEBUG, "Got Espec-1");
481 espec = p->u.externalSpec-> u.espec1;
485 logf(LOG_LOG, "Unknown external espec.");
486 return 25; /* bad. what is proper diagnostic? */
495 return data1_doespec1(dh, n, espec);
500 static int grs_retrieve(struct recRetrieveCtrl *p)
502 data1_node *node = 0, *onode = 0;
505 int res, selected = 0;
507 struct grs_read_info gri;
510 gri.readf = p->readf;
511 gri.seekf = p->seekf;
512 gri.tellf = p->tellf;
519 logf (LOG_DEBUG, "grs_retrieve");
520 node = read_grs_type (&gri, p->subType);
527 logf (LOG_DEBUG, "grs_retrieve: score");
528 if (p->score >= 0 && (new =
529 data1_insert_taggeddata(p->dh, node,
533 new->u.data.what = DATA1I_num;
534 new->u.data.data = new->lbuf;
535 sprintf(new->u.data.data, "%d", p->score);
536 new->u.data.len = strlen(new->u.data.data);
538 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
539 if ((new = data1_insert_taggeddata(p->dh, node, node,
540 "localControlNumber", mem)))
542 new->u.data.what = DATA1I_text;
543 new->u.data.data = new->lbuf;
544 sprintf(new->u.data.data, "%d", p->localno);
545 new->u.data.len = strlen(new->u.data.data);
547 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
548 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
549 node->u.root.absyn->reference != VAL_NONE)
553 int oidtmp[OID_SIZE];
555 oe.proto = PROTO_Z3950;
556 oe.oclass = CLASS_SCHEMA;
557 oe.value = node->u.root.absyn->reference;
559 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
562 data1_handle dh = p->dh;
566 for (ii = oid; *ii >= 0; ii++)
570 sprintf(p, "%d", *ii);
575 if ((new = data1_insert_taggeddata(dh, node, node,
576 "schemaIdentifier", mem)))
578 new->u.data.what = DATA1I_oid;
579 new->u.data.data = nmem_malloc(mem, p - tmp);
580 memcpy(new->u.data.data, tmp, p - tmp);
581 new->u.data.len = p - tmp;
586 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
588 * Does the requested format match a known schema-mapping? (this reflects
589 * the overlap of schema and formatting which is inherent in the MARC
591 * NOTE: This should look at the schema-specification in the compspec
594 for (map = node->u.root.absyn->maptabs; map; map = map->next)
595 if (map->target_absyn_ref == p->input_format)
598 if (!(node = data1_map_record(p->dh, onode, map, mem)))
607 logf (LOG_DEBUG, "grs_retrieve: element spec");
608 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
612 data1_free_tree(p->dh, onode);
613 data1_free_tree(p->dh, node);
617 else if (p->comp && !res)
620 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
621 switch (p->output_format = (p->input_format != VAL_NONE ?
622 p->input_format : VAL_SUTRS))
624 data1_marctab *marctab;
629 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
631 p->diagnostic = 2; /* this should be better specified */
636 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
638 p->diagnostic = 2; /* this should be better specified */
643 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
650 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
658 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
666 for (marctab = node->u.root.absyn->marc; marctab;
667 marctab = marctab->next)
668 if (marctab->reference == p->input_format)
675 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
684 data1_free_tree(p->dh, node);
686 data1_free_tree(p->dh, onode);
691 static struct recType grs_type =
699 RecType recTypeGrs = &grs_type;