2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 1998-02-10 12:03:06 adam
10 * Revision 1.16 1998/01/29 13:38:17 adam
11 * Fixed problem with mapping to record with unknown schema.
13 * Revision 1.15 1998/01/26 10:37:57 adam
16 * Revision 1.14 1997/11/06 11:41:01 adam
17 * Implemented "begin variant" for the sgml.regx filter.
19 * Revision 1.13 1997/10/31 12:35:44 adam
20 * Added a few log statements.
22 * Revision 1.12 1997/10/29 12:02:22 adam
23 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
25 * Revision 1.11 1997/10/27 14:34:00 adam
26 * Work on generic character mapping depending on "structure" field
27 * in abstract syntax file.
29 * Revision 1.10 1997/09/18 08:59:21 adam
30 * Extra generic handle for the character mapping routines.
32 * Revision 1.9 1997/09/17 12:19:21 adam
33 * Zebra version corresponds to YAZ version 1.4.
34 * Changed Zebra server so that it doesn't depend on global common_resource.
36 * Revision 1.8 1997/09/09 13:38:14 adam
37 * Partial port to WIN95/NT.
39 * Revision 1.7 1997/09/05 15:30:10 adam
40 * Changed prototype for chr_map_input - added const.
41 * Added support for C++, headers uses extern "C" for public definitions.
43 * Revision 1.6 1997/09/04 13:54:40 adam
44 * Added MARC filter - type grs.marc.<syntax> where syntax refers
45 * to abstract syntax. New method tellf in retrieve/extract method.
47 * Revision 1.5 1997/07/15 16:29:03 adam
48 * Initialized dummy variable to keep checker gcc happy.
50 * Revision 1.4 1997/04/30 08:56:08 quinn
53 * Revision 1.2 1996/10/11 16:06:43 quinn
54 * Revision 1.3 1997/02/24 10:41:50 adam
55 * Cleanup of code and commented out the "end element-end-record" code.
57 * Revision 1.2 1996/10/11 16:06:43 quinn
58 * Fixed arguments to nodetogr
60 * Revision 1.1 1996/10/11 10:57:25 adam
61 * New module recctrl. Used to manage records (extract/retrieval).
63 * Revision 1.29 1996/10/08 10:30:21 quinn
66 * Revision 1.28 1996/10/07 16:06:40 quinn
69 * Revision 1.27 1996/06/11 10:54:12 quinn
72 * Revision 1.26 1996/06/06 12:08:45 quinn
73 * Added showRecord function
75 * Revision 1.25 1996/06/04 14:18:53 quinn
78 * Revision 1.24 1996/06/04 13:27:54 quinn
79 * More work on charmapping
81 * Revision 1.23 1996/06/04 10:19:01 adam
82 * Minor changes - removed include of ctype.h.
84 * Revision 1.22 1996/06/03 10:15:27 quinn
85 * Various character-mapping.
87 * Revision 1.21 1996/05/31 13:27:24 quinn
88 * Character-conversion in phrases, too.
90 * Revision 1.19 1996/05/16 15:31:14 quinn
93 * Revision 1.18 1996/05/09 07:28:56 quinn
94 * Work towards phrases and multiple registers
96 * Revision 1.17 1996/05/01 13:46:37 adam
97 * First work on multiple records in one file.
98 * New option, -offset, to the "unread" command in the filter module.
100 * Revision 1.16 1996/01/17 14:57:54 adam
101 * Prototype changed for reader functions in extract/retrieve. File
102 * is identified by 'void *' instead of 'int.
104 * Revision 1.15 1996/01/08 19:15:47 adam
105 * New input filter that works!
107 * Revision 1.14 1995/12/15 12:36:11 adam
108 * Retrieval calls data1_read_regx when subType is specified.
110 * Revision 1.13 1995/12/15 12:24:43 quinn
111 * *** empty log message ***
113 * Revision 1.12 1995/12/15 12:20:28 quinn
114 * *** empty log message ***
116 * Revision 1.11 1995/12/15 12:07:57 quinn
117 * Changed extraction strategy.
119 * Revision 1.10 1995/12/14 11:10:48 quinn
122 * Revision 1.9 1995/12/13 17:14:05 quinn
123 * *** empty log message ***
125 * Revision 1.8 1995/12/13 15:33:18 quinn
126 * *** empty log message ***
128 * Revision 1.7 1995/12/13 13:45:39 quinn
129 * Changed data1 to use nmem.
131 * Revision 1.6 1995/12/04 14:22:30 adam
132 * Extra arg to recType_byName.
133 * Started work on new regular expression parsed input to
134 * structured records.
136 * Revision 1.5 1995/11/28 14:18:37 quinn
139 * Revision 1.4 1995/11/21 13:14:49 quinn
140 * Fixed end-of-data-field problem (maybe).
142 * Revision 1.3 1995/11/15 19:13:09 adam
143 * Work on record management.
149 #include <sys/types.h>
160 #define GRS_MAX_WORD 512
162 static int seqno = 0;
164 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
168 data1_node *(*func)(struct grs_read_info *p);
170 { "sgml", grs_read_sgml },
171 { "regx", grs_read_regx },
172 { "marc", grs_read_marc },
175 const char *cp = strchr (type, '.');
178 if (cp == NULL || cp == type)
180 cp = strlen(type) + type;
184 strcpy (p->type, cp+1);
185 for (i=0; tab[i].type; i++)
187 if (!memcmp (type, tab[i].type, cp-type))
188 return (tab[i].func)(p);
193 static void grs_init(void)
197 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
200 (*p->init)(p, &wrd); /* set defaults */
201 for (; n; n = n->next)
203 if (p->flagShowRecords) /* display element description to user */
205 if (n->which == DATA1N_root)
207 printf("%*s", level * 4, "");
208 printf("Record type: '%s'\n", n->u.root.absyn->name);
210 else if (n->which == DATA1N_tag)
214 printf("%*s", level * 4, "");
215 if (!(e = n->u.tag.element))
216 printf("Local tag: '%s'\n", n->u.tag.tag);
219 printf("Elm: '%s' ", e->name);
222 data1_tag *t = e->tag;
224 printf("TagNam: '%s' ", t->names->name);
227 printf("%s[%d],", t->tagset->name, t->tagset->type);
230 if (t->which == DATA1T_numeric)
231 printf("%d)", t->value.numeric);
233 printf("'%s')", t->value.string);
241 if (dumpkeys(n->child, p, level + 1) < 0)
244 if (n->which == DATA1N_data)
246 data1_node *par = get_parent_tag(p->dh, n);
247 data1_termlist *tlist = 0;
248 data1_datatype dtype = DATA1K_string;
250 if (p->flagShowRecords)
252 printf("%*s", level * 4, "");
254 if (n->u.data.len > 20)
255 printf("'%.20s...'\n", n->u.data.data);
256 else if (n->u.data.len > 0)
257 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
265 * cycle up towards the root until we find a tag with an att..
266 * this has the effect of indexing locally defined tags with
267 * the attribute of their ancestor in the record.
270 while (!par->u.tag.element)
271 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
273 if (!par || !(tlist = par->u.tag.element->termlists))
275 if (par->u.tag.element->tag)
276 dtype = par->u.tag.element->tag->kind;
277 for (; tlist; tlist = tlist->next)
279 if (p->flagShowRecords)
281 printf("%*sIdx: [%s]", (level + 1) * 4, "",
283 printf("%s:%s [%d]\n",
284 tlist->att->parent->name,
285 tlist->att->name, tlist->att->value);
289 wrd.reg_type = *tlist->structure;
291 wrd.string = n->u.data.data;
292 wrd.length = n->u.data.len;
293 wrd.attrSet = tlist->att->parent->ordinal;
294 wrd.attrUse = tlist->att->locals->local;
300 if (p->flagShowRecords && n->which == DATA1N_root)
302 printf("%*s-------------\n\n", level * 4, "");
308 static int grs_extract(struct recExtractCtrl *p)
312 struct grs_read_info gri;
315 mem = nmem_create ();
316 gri.readf = p->readf;
317 gri.seekf = p->seekf;
318 gri.tellf = p->tellf;
321 gri.offset = p->offset;
325 n = read_grs_type (&gri, p->subType);
328 if (dumpkeys(n, p, 0) < 0)
330 data1_free_tree(p->dh, n);
333 data1_free_tree(p->dh, n);
339 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
341 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
343 data1_esetname *eset;
349 case Z_RecordComp_simple:
350 if (c->u.simple->which != Z_ElementSetNames_generic)
351 return 26; /* only generic form supported. Fix this later */
352 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
353 c->u.simple->u.generic)))
355 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
356 return 25; /* invalid esetname */
358 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
359 c->u.simple->u.generic);
362 case Z_RecordComp_complex:
363 if (c->u.complex->generic)
365 /* insert check for schema */
366 if ((p = c->u.complex->generic->elementSpec))
369 case Z_ElementSpec_elementSetName:
371 data1_getesetbyname(dh,
373 p->u.elementSetName)))
375 logf(LOG_LOG, "Unknown esetname '%s'",
376 p->u.elementSetName);
377 return 25; /* invalid esetname */
379 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
380 p->u.elementSetName);
383 case Z_ElementSpec_externalSpec:
384 if (p->u.externalSpec->which == Z_External_espec1)
386 logf(LOG_DEBUG, "Got Espec-1");
387 espec = p->u.externalSpec-> u.espec1;
391 logf(LOG_LOG, "Unknown external espec.");
392 return 25; /* bad. what is proper diagnostic? */
402 logf (LOG_LOG, "Element: Espec-1 match");
403 return data1_doespec1(dh, n, espec);
407 logf (LOG_DEBUG, "Element: all match");
412 static int grs_retrieve(struct recRetrieveCtrl *p)
414 data1_node *node = 0, *onode = 0;
417 int res, selected = 0;
419 struct grs_read_info gri;
422 gri.readf = p->readf;
423 gri.seekf = p->seekf;
424 gri.tellf = p->tellf;
431 logf (LOG_DEBUG, "grs_retrieve");
432 node = read_grs_type (&gri, p->subType);
439 logf (LOG_DEBUG, "grs_retrieve: score");
440 if (p->score >= 0 && (new =
441 data1_insert_taggeddata(p->dh, node,
445 new->u.data.what = DATA1I_num;
446 new->u.data.data = new->lbuf;
447 sprintf(new->u.data.data, "%d", p->score);
448 new->u.data.len = strlen(new->u.data.data);
450 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
451 if ((new = data1_insert_taggeddata(p->dh, node, node,
452 "localControlNumber", mem)))
454 new->u.data.what = DATA1I_text;
455 new->u.data.data = new->lbuf;
456 sprintf(new->u.data.data, "%d", p->localno);
457 new->u.data.len = strlen(new->u.data.data);
459 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
460 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
461 node->u.root.absyn->reference != VAL_NONE)
465 int oidtmp[OID_SIZE];
467 oe.proto = PROTO_Z3950;
468 oe.oclass = CLASS_SCHEMA;
469 oe.value = node->u.root.absyn->reference;
471 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
474 data1_handle dh = p->dh;
478 for (ii = oid; *ii >= 0; ii++)
482 sprintf(p, "%d", *ii);
487 if ((new = data1_insert_taggeddata(dh, node, node,
488 "schemaIdentifier", mem)))
490 new->u.data.what = DATA1I_oid;
491 new->u.data.data = nmem_malloc(mem, p - tmp);
492 memcpy(new->u.data.data, tmp, p - tmp);
493 new->u.data.len = p - tmp;
498 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
500 * Does the requested format match a known schema-mapping? (this reflects
501 * the overlap of schema and formatting which is inherent in the MARC
503 * NOTE: This should look at the schema-specification in the compspec
506 for (map = node->u.root.absyn->maptabs; map; map = map->next)
507 if (map->target_absyn_ref == p->input_format)
510 if (!(node = data1_map_record(p->dh, onode, map, mem)))
519 logf (LOG_DEBUG, "grs_retrieve: element spec");
520 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
524 data1_free_tree(p->dh, onode);
525 data1_free_tree(p->dh, node);
529 else if (p->comp && !res)
532 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
533 switch (p->output_format = (p->input_format != VAL_NONE ?
534 p->input_format : VAL_SUTRS))
536 data1_marctab *marctab;
541 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
543 p->diagnostic = 238; /* not available in requested syntax */
548 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
555 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
562 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
567 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
572 if (!node->u.root.absyn)
577 for (marctab = node->u.root.absyn->marc; marctab;
578 marctab = marctab->next)
579 if (marctab->reference == p->input_format)
586 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
595 data1_free_tree(p->dh, node);
597 data1_free_tree(p->dh, onode);
602 static struct recType grs_type =
610 RecType recTypeGrs = &grs_type;