2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.18 1998-03-05 08:41:31 adam
10 * Revision 1.17 1998/02/10 12:03:06 adam
13 * Revision 1.16 1998/01/29 13:38:17 adam
14 * Fixed problem with mapping to record with unknown schema.
16 * Revision 1.15 1998/01/26 10:37:57 adam
19 * Revision 1.14 1997/11/06 11:41:01 adam
20 * Implemented "begin variant" for the sgml.regx filter.
22 * Revision 1.13 1997/10/31 12:35:44 adam
23 * Added a few log statements.
25 * Revision 1.12 1997/10/29 12:02:22 adam
26 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
28 * Revision 1.11 1997/10/27 14:34:00 adam
29 * Work on generic character mapping depending on "structure" field
30 * in abstract syntax file.
32 * Revision 1.10 1997/09/18 08:59:21 adam
33 * Extra generic handle for the character mapping routines.
35 * Revision 1.9 1997/09/17 12:19:21 adam
36 * Zebra version corresponds to YAZ version 1.4.
37 * Changed Zebra server so that it doesn't depend on global common_resource.
39 * Revision 1.8 1997/09/09 13:38:14 adam
40 * Partial port to WIN95/NT.
42 * Revision 1.7 1997/09/05 15:30:10 adam
43 * Changed prototype for chr_map_input - added const.
44 * Added support for C++, headers uses extern "C" for public definitions.
46 * Revision 1.6 1997/09/04 13:54:40 adam
47 * Added MARC filter - type grs.marc.<syntax> where syntax refers
48 * to abstract syntax. New method tellf in retrieve/extract method.
50 * Revision 1.5 1997/07/15 16:29:03 adam
51 * Initialized dummy variable to keep checker gcc happy.
53 * Revision 1.4 1997/04/30 08:56:08 quinn
56 * Revision 1.2 1996/10/11 16:06:43 quinn
57 * Revision 1.3 1997/02/24 10:41:50 adam
58 * Cleanup of code and commented out the "end element-end-record" code.
60 * Revision 1.2 1996/10/11 16:06:43 quinn
61 * Fixed arguments to nodetogr
63 * Revision 1.1 1996/10/11 10:57:25 adam
64 * New module recctrl. Used to manage records (extract/retrieval).
66 * Revision 1.29 1996/10/08 10:30:21 quinn
69 * Revision 1.28 1996/10/07 16:06:40 quinn
72 * Revision 1.27 1996/06/11 10:54:12 quinn
75 * Revision 1.26 1996/06/06 12:08:45 quinn
76 * Added showRecord function
78 * Revision 1.25 1996/06/04 14:18:53 quinn
81 * Revision 1.24 1996/06/04 13:27:54 quinn
82 * More work on charmapping
84 * Revision 1.23 1996/06/04 10:19:01 adam
85 * Minor changes - removed include of ctype.h.
87 * Revision 1.22 1996/06/03 10:15:27 quinn
88 * Various character-mapping.
90 * Revision 1.21 1996/05/31 13:27:24 quinn
91 * Character-conversion in phrases, too.
93 * Revision 1.19 1996/05/16 15:31:14 quinn
96 * Revision 1.18 1996/05/09 07:28:56 quinn
97 * Work towards phrases and multiple registers
99 * Revision 1.17 1996/05/01 13:46:37 adam
100 * First work on multiple records in one file.
101 * New option, -offset, to the "unread" command in the filter module.
103 * Revision 1.16 1996/01/17 14:57:54 adam
104 * Prototype changed for reader functions in extract/retrieve. File
105 * is identified by 'void *' instead of 'int.
107 * Revision 1.15 1996/01/08 19:15:47 adam
108 * New input filter that works!
110 * Revision 1.14 1995/12/15 12:36:11 adam
111 * Retrieval calls data1_read_regx when subType is specified.
113 * Revision 1.13 1995/12/15 12:24:43 quinn
114 * *** empty log message ***
116 * Revision 1.12 1995/12/15 12:20:28 quinn
117 * *** empty log message ***
119 * Revision 1.11 1995/12/15 12:07:57 quinn
120 * Changed extraction strategy.
122 * Revision 1.10 1995/12/14 11:10:48 quinn
125 * Revision 1.9 1995/12/13 17:14:05 quinn
126 * *** empty log message ***
128 * Revision 1.8 1995/12/13 15:33:18 quinn
129 * *** empty log message ***
131 * Revision 1.7 1995/12/13 13:45:39 quinn
132 * Changed data1 to use nmem.
134 * Revision 1.6 1995/12/04 14:22:30 adam
135 * Extra arg to recType_byName.
136 * Started work on new regular expression parsed input to
137 * structured records.
139 * Revision 1.5 1995/11/28 14:18:37 quinn
142 * Revision 1.4 1995/11/21 13:14:49 quinn
143 * Fixed end-of-data-field problem (maybe).
145 * Revision 1.3 1995/11/15 19:13:09 adam
146 * Work on record management.
152 #include <sys/types.h>
163 #define GRS_MAX_WORD 512
165 static int seqno = 0;
167 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
171 data1_node *(*func)(struct grs_read_info *p);
173 { "sgml", grs_read_sgml },
174 { "regx", grs_read_regx },
175 { "marc", grs_read_marc },
178 const char *cp = strchr (type, '.');
181 if (cp == NULL || cp == type)
183 cp = strlen(type) + type;
187 strcpy (p->type, cp+1);
188 for (i=0; tab[i].type; i++)
190 if (!memcmp (type, tab[i].type, cp-type))
191 return (tab[i].func)(p);
196 static void grs_init(void)
200 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
203 (*p->init)(p, &wrd); /* set defaults */
204 for (; n; n = n->next)
206 if (p->flagShowRecords) /* display element description to user */
208 if (n->which == DATA1N_root)
210 printf("%*s", level * 4, "");
211 printf("Record type: '%s'\n", n->u.root.absyn->name);
213 else if (n->which == DATA1N_tag)
217 printf("%*s", level * 4, "");
218 if (!(e = n->u.tag.element))
219 printf("Local tag: '%s'\n", n->u.tag.tag);
222 printf("Elm: '%s' ", e->name);
225 data1_tag *t = e->tag;
227 printf("TagNam: '%s' ", t->names->name);
230 printf("%s[%d],", t->tagset->name, t->tagset->type);
233 if (t->which == DATA1T_numeric)
234 printf("%d)", t->value.numeric);
236 printf("'%s')", t->value.string);
244 if (dumpkeys(n->child, p, level + 1) < 0)
247 if (n->which == DATA1N_data)
249 data1_node *par = get_parent_tag(p->dh, n);
250 data1_termlist *tlist = 0;
251 data1_datatype dtype = DATA1K_string;
253 if (p->flagShowRecords)
255 printf("%*s", level * 4, "");
257 if (n->u.data.len > 32)
258 printf("'%.24s ... %.6s'\n", n->u.data.data,
259 n->u.data.data + n->u.data.len-6);
260 else if (n->u.data.len > 0)
261 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
269 * cycle up towards the root until we find a tag with an att..
270 * this has the effect of indexing locally defined tags with
271 * the attribute of their ancestor in the record.
274 while (!par->u.tag.element)
275 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
277 if (!par || !(tlist = par->u.tag.element->termlists))
279 if (par->u.tag.element->tag)
280 dtype = par->u.tag.element->tag->kind;
281 for (; tlist; tlist = tlist->next)
283 if (p->flagShowRecords)
285 printf("%*sIdx: [%s]", (level + 1) * 4, "",
287 printf("%s:%s [%d]\n",
288 tlist->att->parent->name,
289 tlist->att->name, tlist->att->value);
293 wrd.reg_type = *tlist->structure;
295 wrd.string = n->u.data.data;
296 wrd.length = n->u.data.len;
297 wrd.attrSet = tlist->att->parent->ordinal;
298 wrd.attrUse = tlist->att->locals->local;
304 if (p->flagShowRecords && n->which == DATA1N_root)
306 printf("%*s-------------\n\n", level * 4, "");
312 static int grs_extract(struct recExtractCtrl *p)
316 struct grs_read_info gri;
319 mem = nmem_create ();
320 gri.readf = p->readf;
321 gri.seekf = p->seekf;
322 gri.tellf = p->tellf;
325 gri.offset = p->offset;
329 n = read_grs_type (&gri, p->subType);
332 if (dumpkeys(n, p, 0) < 0)
334 data1_free_tree(p->dh, n);
337 data1_free_tree(p->dh, n);
343 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
345 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
347 data1_esetname *eset;
353 case Z_RecordComp_simple:
354 if (c->u.simple->which != Z_ElementSetNames_generic)
355 return 26; /* only generic form supported. Fix this later */
356 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
357 c->u.simple->u.generic)))
359 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
360 return 25; /* invalid esetname */
362 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
363 c->u.simple->u.generic);
366 case Z_RecordComp_complex:
367 if (c->u.complex->generic)
369 /* insert check for schema */
370 if ((p = c->u.complex->generic->elementSpec))
373 case Z_ElementSpec_elementSetName:
375 data1_getesetbyname(dh,
377 p->u.elementSetName)))
379 logf(LOG_LOG, "Unknown esetname '%s'",
380 p->u.elementSetName);
381 return 25; /* invalid esetname */
383 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
384 p->u.elementSetName);
387 case Z_ElementSpec_externalSpec:
388 if (p->u.externalSpec->which == Z_External_espec1)
390 logf(LOG_DEBUG, "Got Espec-1");
391 espec = p->u.externalSpec-> u.espec1;
395 logf(LOG_LOG, "Unknown external espec.");
396 return 25; /* bad. what is proper diagnostic? */
406 logf (LOG_LOG, "Element: Espec-1 match");
407 return data1_doespec1(dh, n, espec);
411 logf (LOG_DEBUG, "Element: all match");
416 static int grs_retrieve(struct recRetrieveCtrl *p)
418 data1_node *node = 0, *onode = 0;
421 int res, selected = 0;
423 struct grs_read_info gri;
426 gri.readf = p->readf;
427 gri.seekf = p->seekf;
428 gri.tellf = p->tellf;
435 logf (LOG_DEBUG, "grs_retrieve");
436 node = read_grs_type (&gri, p->subType);
443 logf (LOG_DEBUG, "grs_retrieve: size");
444 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
447 dnew->u.data.what = DATA1I_text;
448 dnew->u.data.data = dnew->lbuf;
449 sprintf(dnew->u.data.data, "%d", p->recordSize);
450 dnew->u.data.len = strlen(dnew->u.data.data);
453 logf (LOG_DEBUG, "grs_retrieve: score");
454 if (p->score >= 0 && (dnew =
455 data1_insert_taggeddata(p->dh, node,
459 dnew->u.data.what = DATA1I_num;
460 dnew->u.data.data = dnew->lbuf;
461 sprintf(dnew->u.data.data, "%d", p->score);
462 dnew->u.data.len = strlen(dnew->u.data.data);
465 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
466 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
467 "localControlNumber", mem)))
469 dnew->u.data.what = DATA1I_text;
470 dnew->u.data.data = dnew->lbuf;
471 sprintf(dnew->u.data.data, "%d", p->localno);
472 dnew->u.data.len = strlen(dnew->u.data.data);
475 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
476 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
477 node->u.root.absyn->reference != VAL_NONE)
481 int oidtmp[OID_SIZE];
483 oe.proto = PROTO_Z3950;
484 oe.oclass = CLASS_SCHEMA;
485 oe.value = node->u.root.absyn->reference;
487 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
490 data1_handle dh = p->dh;
494 for (ii = oid; *ii >= 0; ii++)
498 sprintf(p, "%d", *ii);
503 if ((dnew = data1_insert_taggeddata(dh, node, node,
504 "schemaIdentifier", mem)))
506 dnew->u.data.what = DATA1I_oid;
507 dnew->u.data.data = nmem_malloc(mem, p - tmp);
508 memcpy(dnew->u.data.data, tmp, p - tmp);
509 dnew->u.data.len = p - tmp;
514 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
516 * Does the requested format match a known schema-mapping? (this reflects
517 * the overlap of schema and formatting which is inherent in the MARC
519 * NOTE: This should look at the schema-specification in the compspec
522 for (map = node->u.root.absyn->maptabs; map; map = map->next)
523 if (map->target_absyn_ref == p->input_format)
526 if (!(node = data1_map_record(p->dh, onode, map, mem)))
535 logf (LOG_DEBUG, "grs_retrieve: element spec");
536 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
540 data1_free_tree(p->dh, onode);
541 data1_free_tree(p->dh, node);
545 else if (p->comp && !res)
548 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
549 switch (p->output_format = (p->input_format != VAL_NONE ?
550 p->input_format : VAL_SUTRS))
552 data1_marctab *marctab;
557 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
559 p->diagnostic = 238; /* not available in requested syntax */
564 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
571 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
578 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
583 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
588 if (!node->u.root.absyn)
593 for (marctab = node->u.root.absyn->marc; marctab;
594 marctab = marctab->next)
595 if (marctab->reference == p->input_format)
602 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
611 data1_free_tree(p->dh, node);
613 data1_free_tree(p->dh, onode);
618 static struct recType grs_type =
626 RecType recTypeGrs = &grs_type;