2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.21 1998-07-01 09:16:10 adam
8 * Element localno only added when it's greater than 0.
10 * Revision 1.20 1998/05/20 10:12:26 adam
11 * Implemented automatic EXPLAIN database maintenance.
12 * Modified Zebra to work with ASN.1 compiled version of YAZ.
14 * Revision 1.19 1998/03/11 11:19:05 adam
15 * Changed the way sequence numbers are generated.
17 * Revision 1.18 1998/03/05 08:41:31 adam
20 * Revision 1.17 1998/02/10 12:03:06 adam
23 * Revision 1.16 1998/01/29 13:38:17 adam
24 * Fixed problem with mapping to record with unknown schema.
26 * Revision 1.15 1998/01/26 10:37:57 adam
29 * Revision 1.14 1997/11/06 11:41:01 adam
30 * Implemented "begin variant" for the sgml.regx filter.
32 * Revision 1.13 1997/10/31 12:35:44 adam
33 * Added a few log statements.
35 * Revision 1.12 1997/10/29 12:02:22 adam
36 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
38 * Revision 1.11 1997/10/27 14:34:00 adam
39 * Work on generic character mapping depending on "structure" field
40 * in abstract syntax file.
42 * Revision 1.10 1997/09/18 08:59:21 adam
43 * Extra generic handle for the character mapping routines.
45 * Revision 1.9 1997/09/17 12:19:21 adam
46 * Zebra version corresponds to YAZ version 1.4.
47 * Changed Zebra server so that it doesn't depend on global common_resource.
49 * Revision 1.8 1997/09/09 13:38:14 adam
50 * Partial port to WIN95/NT.
52 * Revision 1.7 1997/09/05 15:30:10 adam
53 * Changed prototype for chr_map_input - added const.
54 * Added support for C++, headers uses extern "C" for public definitions.
56 * Revision 1.6 1997/09/04 13:54:40 adam
57 * Added MARC filter - type grs.marc.<syntax> where syntax refers
58 * to abstract syntax. New method tellf in retrieve/extract method.
60 * Revision 1.5 1997/07/15 16:29:03 adam
61 * Initialized dummy variable to keep checker gcc happy.
63 * Revision 1.4 1997/04/30 08:56:08 quinn
66 * Revision 1.2 1996/10/11 16:06:43 quinn
67 * Revision 1.3 1997/02/24 10:41:50 adam
68 * Cleanup of code and commented out the "end element-end-record" code.
70 * Revision 1.2 1996/10/11 16:06:43 quinn
71 * Fixed arguments to nodetogr
73 * Revision 1.1 1996/10/11 10:57:25 adam
74 * New module recctrl. Used to manage records (extract/retrieval).
76 * Revision 1.29 1996/10/08 10:30:21 quinn
79 * Revision 1.28 1996/10/07 16:06:40 quinn
82 * Revision 1.27 1996/06/11 10:54:12 quinn
85 * Revision 1.26 1996/06/06 12:08:45 quinn
86 * Added showRecord function
88 * Revision 1.25 1996/06/04 14:18:53 quinn
91 * Revision 1.24 1996/06/04 13:27:54 quinn
92 * More work on charmapping
94 * Revision 1.23 1996/06/04 10:19:01 adam
95 * Minor changes - removed include of ctype.h.
97 * Revision 1.22 1996/06/03 10:15:27 quinn
98 * Various character-mapping.
100 * Revision 1.21 1996/05/31 13:27:24 quinn
101 * Character-conversion in phrases, too.
103 * Revision 1.19 1996/05/16 15:31:14 quinn
106 * Revision 1.18 1996/05/09 07:28:56 quinn
107 * Work towards phrases and multiple registers
109 * Revision 1.17 1996/05/01 13:46:37 adam
110 * First work on multiple records in one file.
111 * New option, -offset, to the "unread" command in the filter module.
113 * Revision 1.16 1996/01/17 14:57:54 adam
114 * Prototype changed for reader functions in extract/retrieve. File
115 * is identified by 'void *' instead of 'int.
117 * Revision 1.15 1996/01/08 19:15:47 adam
118 * New input filter that works!
120 * Revision 1.14 1995/12/15 12:36:11 adam
121 * Retrieval calls data1_read_regx when subType is specified.
123 * Revision 1.13 1995/12/15 12:24:43 quinn
124 * *** empty log message ***
126 * Revision 1.12 1995/12/15 12:20:28 quinn
127 * *** empty log message ***
129 * Revision 1.11 1995/12/15 12:07:57 quinn
130 * Changed extraction strategy.
132 * Revision 1.10 1995/12/14 11:10:48 quinn
135 * Revision 1.9 1995/12/13 17:14:05 quinn
136 * *** empty log message ***
138 * Revision 1.8 1995/12/13 15:33:18 quinn
139 * *** empty log message ***
141 * Revision 1.7 1995/12/13 13:45:39 quinn
142 * Changed data1 to use nmem.
144 * Revision 1.6 1995/12/04 14:22:30 adam
145 * Extra arg to recType_byName.
146 * Started work on new regular expression parsed input to
147 * structured records.
149 * Revision 1.5 1995/11/28 14:18:37 quinn
152 * Revision 1.4 1995/11/21 13:14:49 quinn
153 * Fixed end-of-data-field problem (maybe).
155 * Revision 1.3 1995/11/15 19:13:09 adam
156 * Work on record management.
162 #include <sys/types.h>
173 #define GRS_MAX_WORD 512
175 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
179 data1_node *(*func)(struct grs_read_info *p);
181 { "sgml", grs_read_sgml },
182 { "regx", grs_read_regx },
183 { "marc", grs_read_marc },
186 const char *cp = strchr (type, '.');
189 if (cp == NULL || cp == type)
191 cp = strlen(type) + type;
195 strcpy (p->type, cp+1);
196 for (i=0; tab[i].type; i++)
198 if (!memcmp (type, tab[i].type, cp-type))
199 return (tab[i].func)(p);
204 static void grs_init(void)
208 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
211 (*p->init)(p, &wrd); /* set defaults */
212 for (; n; n = n->next)
214 if (p->flagShowRecords) /* display element description to user */
216 if (n->which == DATA1N_root)
218 printf("%*s", level * 4, "");
219 printf("Record type: '%s'\n", n->u.root.absyn->name);
221 else if (n->which == DATA1N_tag)
225 printf("%*s", level * 4, "");
226 if (!(e = n->u.tag.element))
227 printf("Local tag: '%s'\n", n->u.tag.tag);
230 printf("Elm: '%s' ", e->name);
233 data1_tag *t = e->tag;
235 printf("TagNam: '%s' ", t->names->name);
238 printf("%s[%d],", t->tagset->name, t->tagset->type);
241 if (t->which == DATA1T_numeric)
242 printf("%d)", t->value.numeric);
244 printf("'%s')", t->value.string);
252 if (dumpkeys(n->child, p, level + 1) < 0)
255 if (n->which == DATA1N_data)
257 data1_node *par = get_parent_tag(p->dh, n);
258 data1_termlist *tlist = 0;
259 data1_datatype dtype = DATA1K_string;
261 if (p->flagShowRecords)
263 printf("%*s", level * 4, "");
265 if (n->u.data.len > 32)
266 printf("'%.24s ... %.6s'\n", n->u.data.data,
267 n->u.data.data + n->u.data.len-6);
268 else if (n->u.data.len > 0)
269 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
277 * cycle up towards the root until we find a tag with an att..
278 * this has the effect of indexing locally defined tags with
279 * the attribute of their ancestor in the record.
282 while (!par->u.tag.element)
283 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
285 if (!par || !(tlist = par->u.tag.element->termlists))
287 if (par->u.tag.element->tag)
288 dtype = par->u.tag.element->tag->kind;
289 for (; tlist; tlist = tlist->next)
291 if (p->flagShowRecords)
293 printf("%*sIdx: [%s]", (level + 1) * 4, "",
295 printf("%s:%s [%d]\n",
296 tlist->att->parent->name,
297 tlist->att->name, tlist->att->value);
301 wrd.reg_type = *tlist->structure;
302 wrd.string = n->u.data.data;
303 wrd.length = n->u.data.len;
304 wrd.attrSet = (int) (tlist->att->parent->reference);
305 wrd.attrUse = tlist->att->locals->local;
310 if (p->flagShowRecords && n->which == DATA1N_root)
312 printf("%*s-------------\n\n", level * 4, "");
318 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
321 int oidtmp[OID_SIZE];
323 oe.proto = PROTO_Z3950;
324 oe.oclass = CLASS_SCHEMA;
325 oe.value = n->u.root.absyn->reference;
327 if ((oid_ent_to_oid (&oe, oidtmp)))
328 (*p->addSchema)(p, oidtmp);
330 return dumpkeys(n, p, 0);
333 static int grs_extract(struct recExtractCtrl *p)
337 struct grs_read_info gri;
339 int oidtmp[OID_SIZE];
341 mem = nmem_create ();
342 gri.readf = p->readf;
343 gri.seekf = p->seekf;
344 gri.tellf = p->tellf;
347 gri.offset = p->offset;
351 n = read_grs_type (&gri, p->subType);
355 oe.proto = PROTO_Z3950;
356 oe.oclass = CLASS_SCHEMA;
357 oe.value = n->u.root.absyn->reference;
358 if ((oid_ent_to_oid (&oe, oidtmp)))
359 (*p->addSchema)(p, oidtmp);
361 if (dumpkeys(n, p, 0) < 0)
363 data1_free_tree(p->dh, n);
366 data1_free_tree(p->dh, n);
372 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
374 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
376 data1_esetname *eset;
382 case Z_RecordComp_simple:
383 if (c->u.simple->which != Z_ElementSetNames_generic)
384 return 26; /* only generic form supported. Fix this later */
385 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
386 c->u.simple->u.generic)))
388 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
389 return 25; /* invalid esetname */
391 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
392 c->u.simple->u.generic);
395 case Z_RecordComp_complex:
396 if (c->u.complex->generic)
398 /* insert check for schema */
399 if ((p = c->u.complex->generic->elementSpec))
402 case Z_ElementSpec_elementSetName:
404 data1_getesetbyname(dh,
406 p->u.elementSetName)))
408 logf(LOG_LOG, "Unknown esetname '%s'",
409 p->u.elementSetName);
410 return 25; /* invalid esetname */
412 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
413 p->u.elementSetName);
416 case Z_ElementSpec_externalSpec:
417 if (p->u.externalSpec->which == Z_External_espec1)
419 logf(LOG_DEBUG, "Got Espec-1");
420 espec = p->u.externalSpec-> u.espec1;
424 logf(LOG_LOG, "Unknown external espec.");
425 return 25; /* bad. what is proper diagnostic? */
435 logf (LOG_LOG, "Element: Espec-1 match");
436 return data1_doespec1(dh, n, espec);
440 logf (LOG_DEBUG, "Element: all match");
445 static int grs_retrieve(struct recRetrieveCtrl *p)
447 data1_node *node = 0, *onode = 0;
450 int res, selected = 0;
452 struct grs_read_info gri;
455 gri.readf = p->readf;
456 gri.seekf = p->seekf;
457 gri.tellf = p->tellf;
464 logf (LOG_DEBUG, "grs_retrieve");
465 node = read_grs_type (&gri, p->subType);
472 logf (LOG_DEBUG, "grs_retrieve: size");
473 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
476 dnew->u.data.what = DATA1I_text;
477 dnew->u.data.data = dnew->lbuf;
478 sprintf(dnew->u.data.data, "%d", p->recordSize);
479 dnew->u.data.len = strlen(dnew->u.data.data);
482 logf (LOG_DEBUG, "grs_retrieve: score");
483 if (p->score >= 0 && (dnew =
484 data1_insert_taggeddata(p->dh, node,
488 dnew->u.data.what = DATA1I_num;
489 dnew->u.data.data = dnew->lbuf;
490 sprintf(dnew->u.data.data, "%d", p->score);
491 dnew->u.data.len = strlen(dnew->u.data.data);
494 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
495 if (p->localno > 0 && (dnew = data1_insert_taggeddata(p->dh, node, node,
496 "localControlNumber", mem)))
498 dnew->u.data.what = DATA1I_text;
499 dnew->u.data.data = dnew->lbuf;
500 sprintf(dnew->u.data.data, "%d", p->localno);
501 dnew->u.data.len = strlen(dnew->u.data.data);
504 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
505 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
506 node->u.root.absyn->reference != VAL_NONE)
510 int oidtmp[OID_SIZE];
512 oe.proto = PROTO_Z3950;
513 oe.oclass = CLASS_SCHEMA;
514 oe.value = node->u.root.absyn->reference;
516 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
519 data1_handle dh = p->dh;
523 for (ii = oid; *ii >= 0; ii++)
527 sprintf(p, "%d", *ii);
532 if ((dnew = data1_insert_taggeddata(dh, node, node,
533 "schemaIdentifier", mem)))
535 dnew->u.data.what = DATA1I_oid;
536 dnew->u.data.data = nmem_malloc(mem, p - tmp);
537 memcpy(dnew->u.data.data, tmp, p - tmp);
538 dnew->u.data.len = p - tmp;
543 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
545 * Does the requested format match a known schema-mapping? (this reflects
546 * the overlap of schema and formatting which is inherent in the MARC
548 * NOTE: This should look at the schema-specification in the compspec
551 for (map = node->u.root.absyn->maptabs; map; map = map->next)
552 if (map->target_absyn_ref == p->input_format)
555 if (!(node = data1_map_record(p->dh, onode, map, mem)))
564 logf (LOG_DEBUG, "grs_retrieve: element spec");
565 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
569 data1_free_tree(p->dh, onode);
570 data1_free_tree(p->dh, node);
574 else if (p->comp && !res)
577 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
578 switch (p->output_format = (p->input_format != VAL_NONE ?
579 p->input_format : VAL_SUTRS))
581 data1_marctab *marctab;
586 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
588 p->diagnostic = 238; /* not available in requested syntax */
593 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
600 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
607 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
612 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
617 if (!node->u.root.absyn)
622 for (marctab = node->u.root.absyn->marc; marctab;
623 marctab = marctab->next)
624 if (marctab->reference == p->input_format)
631 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
640 data1_free_tree(p->dh, node);
642 data1_free_tree(p->dh, onode);
647 static struct recType grs_type =
655 RecType recTypeGrs = &grs_type;