2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.22 1998-10-16 08:14:37 adam
8 * Updated record control system.
10 * Revision 1.21 1998/07/01 09:16:10 adam
11 * Element localno only added when it's greater than 0.
13 * Revision 1.20 1998/05/20 10:12:26 adam
14 * Implemented automatic EXPLAIN database maintenance.
15 * Modified Zebra to work with ASN.1 compiled version of YAZ.
17 * Revision 1.19 1998/03/11 11:19:05 adam
18 * Changed the way sequence numbers are generated.
20 * Revision 1.18 1998/03/05 08:41:31 adam
23 * Revision 1.17 1998/02/10 12:03:06 adam
26 * Revision 1.16 1998/01/29 13:38:17 adam
27 * Fixed problem with mapping to record with unknown schema.
29 * Revision 1.15 1998/01/26 10:37:57 adam
32 * Revision 1.14 1997/11/06 11:41:01 adam
33 * Implemented "begin variant" for the sgml.regx filter.
35 * Revision 1.13 1997/10/31 12:35:44 adam
36 * Added a few log statements.
38 * Revision 1.12 1997/10/29 12:02:22 adam
39 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
41 * Revision 1.11 1997/10/27 14:34:00 adam
42 * Work on generic character mapping depending on "structure" field
43 * in abstract syntax file.
45 * Revision 1.10 1997/09/18 08:59:21 adam
46 * Extra generic handle for the character mapping routines.
48 * Revision 1.9 1997/09/17 12:19:21 adam
49 * Zebra version corresponds to YAZ version 1.4.
50 * Changed Zebra server so that it doesn't depend on global common_resource.
52 * Revision 1.8 1997/09/09 13:38:14 adam
53 * Partial port to WIN95/NT.
55 * Revision 1.7 1997/09/05 15:30:10 adam
56 * Changed prototype for chr_map_input - added const.
57 * Added support for C++, headers uses extern "C" for public definitions.
59 * Revision 1.6 1997/09/04 13:54:40 adam
60 * Added MARC filter - type grs.marc.<syntax> where syntax refers
61 * to abstract syntax. New method tellf in retrieve/extract method.
63 * Revision 1.5 1997/07/15 16:29:03 adam
64 * Initialized dummy variable to keep checker gcc happy.
66 * Revision 1.4 1997/04/30 08:56:08 quinn
69 * Revision 1.2 1996/10/11 16:06:43 quinn
70 * Revision 1.3 1997/02/24 10:41:50 adam
71 * Cleanup of code and commented out the "end element-end-record" code.
73 * Revision 1.2 1996/10/11 16:06:43 quinn
74 * Fixed arguments to nodetogr
76 * Revision 1.1 1996/10/11 10:57:25 adam
77 * New module recctrl. Used to manage records (extract/retrieval).
79 * Revision 1.29 1996/10/08 10:30:21 quinn
82 * Revision 1.28 1996/10/07 16:06:40 quinn
85 * Revision 1.27 1996/06/11 10:54:12 quinn
88 * Revision 1.26 1996/06/06 12:08:45 quinn
89 * Added showRecord function
91 * Revision 1.25 1996/06/04 14:18:53 quinn
94 * Revision 1.24 1996/06/04 13:27:54 quinn
95 * More work on charmapping
97 * Revision 1.23 1996/06/04 10:19:01 adam
98 * Minor changes - removed include of ctype.h.
100 * Revision 1.22 1996/06/03 10:15:27 quinn
101 * Various character-mapping.
103 * Revision 1.21 1996/05/31 13:27:24 quinn
104 * Character-conversion in phrases, too.
106 * Revision 1.19 1996/05/16 15:31:14 quinn
109 * Revision 1.18 1996/05/09 07:28:56 quinn
110 * Work towards phrases and multiple registers
112 * Revision 1.17 1996/05/01 13:46:37 adam
113 * First work on multiple records in one file.
114 * New option, -offset, to the "unread" command in the filter module.
116 * Revision 1.16 1996/01/17 14:57:54 adam
117 * Prototype changed for reader functions in extract/retrieve. File
118 * is identified by 'void *' instead of 'int.
120 * Revision 1.15 1996/01/08 19:15:47 adam
121 * New input filter that works!
123 * Revision 1.14 1995/12/15 12:36:11 adam
124 * Retrieval calls data1_read_regx when subType is specified.
126 * Revision 1.13 1995/12/15 12:24:43 quinn
127 * *** empty log message ***
129 * Revision 1.12 1995/12/15 12:20:28 quinn
130 * *** empty log message ***
132 * Revision 1.11 1995/12/15 12:07:57 quinn
133 * Changed extraction strategy.
135 * Revision 1.10 1995/12/14 11:10:48 quinn
138 * Revision 1.9 1995/12/13 17:14:05 quinn
139 * *** empty log message ***
141 * Revision 1.8 1995/12/13 15:33:18 quinn
142 * *** empty log message ***
144 * Revision 1.7 1995/12/13 13:45:39 quinn
145 * Changed data1 to use nmem.
147 * Revision 1.6 1995/12/04 14:22:30 adam
148 * Extra arg to recType_byName.
149 * Started work on new regular expression parsed input to
150 * structured records.
152 * Revision 1.5 1995/11/28 14:18:37 quinn
155 * Revision 1.4 1995/11/21 13:14:49 quinn
156 * Fixed end-of-data-field problem (maybe).
158 * Revision 1.3 1995/11/15 19:13:09 adam
159 * Work on record management.
165 #include <sys/types.h>
176 #define GRS_MAX_WORD 512
178 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
182 data1_node *(*func)(struct grs_read_info *p);
184 { "sgml", grs_read_sgml },
185 { "regx", grs_read_regx },
186 { "marc", grs_read_marc },
189 const char *cp = strchr (type, '.');
192 if (cp == NULL || cp == type)
194 cp = strlen(type) + type;
198 strcpy (p->type, cp+1);
199 for (i=0; tab[i].type; i++)
201 if (!memcmp (type, tab[i].type, cp-type))
202 return (tab[i].func)(p);
207 static void grs_init(RecType recType)
211 static void grs_destroy(RecType recType)
215 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
218 (*p->init)(p, &wrd); /* set defaults */
219 for (; n; n = n->next)
221 if (p->flagShowRecords) /* display element description to user */
223 if (n->which == DATA1N_root)
225 printf("%*s", level * 4, "");
226 printf("Record type: '%s'\n", n->u.root.absyn->name);
228 else if (n->which == DATA1N_tag)
232 printf("%*s", level * 4, "");
233 if (!(e = n->u.tag.element))
234 printf("Local tag: '%s'\n", n->u.tag.tag);
237 printf("Elm: '%s' ", e->name);
240 data1_tag *t = e->tag;
242 printf("TagNam: '%s' ", t->names->name);
245 printf("%s[%d],", t->tagset->name, t->tagset->type);
248 if (t->which == DATA1T_numeric)
249 printf("%d)", t->value.numeric);
251 printf("'%s')", t->value.string);
259 if (dumpkeys(n->child, p, level + 1) < 0)
262 if (n->which == DATA1N_data)
264 data1_node *par = get_parent_tag(p->dh, n);
265 data1_termlist *tlist = 0;
266 data1_datatype dtype = DATA1K_string;
268 if (p->flagShowRecords)
270 printf("%*s", level * 4, "");
272 if (n->u.data.len > 32)
273 printf("'%.24s ... %.6s'\n", n->u.data.data,
274 n->u.data.data + n->u.data.len-6);
275 else if (n->u.data.len > 0)
276 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
284 * cycle up towards the root until we find a tag with an att..
285 * this has the effect of indexing locally defined tags with
286 * the attribute of their ancestor in the record.
289 while (!par->u.tag.element)
290 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
292 if (!par || !(tlist = par->u.tag.element->termlists))
294 if (par->u.tag.element->tag)
295 dtype = par->u.tag.element->tag->kind;
296 for (; tlist; tlist = tlist->next)
298 if (p->flagShowRecords)
300 printf("%*sIdx: [%s]", (level + 1) * 4, "",
302 printf("%s:%s [%d]\n",
303 tlist->att->parent->name,
304 tlist->att->name, tlist->att->value);
308 wrd.reg_type = *tlist->structure;
309 wrd.string = n->u.data.data;
310 wrd.length = n->u.data.len;
311 wrd.attrSet = (int) (tlist->att->parent->reference);
312 wrd.attrUse = tlist->att->locals->local;
317 if (p->flagShowRecords && n->which == DATA1N_root)
319 printf("%*s-------------\n\n", level * 4, "");
325 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
328 int oidtmp[OID_SIZE];
330 oe.proto = PROTO_Z3950;
331 oe.oclass = CLASS_SCHEMA;
332 oe.value = n->u.root.absyn->reference;
334 if ((oid_ent_to_oid (&oe, oidtmp)))
335 (*p->addSchema)(p, oidtmp);
337 return dumpkeys(n, p, 0);
340 static int grs_extract(struct recExtractCtrl *p)
344 struct grs_read_info gri;
346 int oidtmp[OID_SIZE];
348 mem = nmem_create ();
349 gri.readf = p->readf;
350 gri.seekf = p->seekf;
351 gri.tellf = p->tellf;
354 gri.offset = p->offset;
358 n = read_grs_type (&gri, p->subType);
362 oe.proto = PROTO_Z3950;
363 oe.oclass = CLASS_SCHEMA;
364 oe.value = n->u.root.absyn->reference;
365 if ((oid_ent_to_oid (&oe, oidtmp)))
366 (*p->addSchema)(p, oidtmp);
368 if (dumpkeys(n, p, 0) < 0)
370 data1_free_tree(p->dh, n);
373 data1_free_tree(p->dh, n);
379 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
381 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
383 data1_esetname *eset;
389 case Z_RecordComp_simple:
390 if (c->u.simple->which != Z_ElementSetNames_generic)
391 return 26; /* only generic form supported. Fix this later */
392 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
393 c->u.simple->u.generic)))
395 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
396 return 25; /* invalid esetname */
398 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
399 c->u.simple->u.generic);
402 case Z_RecordComp_complex:
403 if (c->u.complex->generic)
405 /* insert check for schema */
406 if ((p = c->u.complex->generic->elementSpec))
409 case Z_ElementSpec_elementSetName:
411 data1_getesetbyname(dh,
413 p->u.elementSetName)))
415 logf(LOG_LOG, "Unknown esetname '%s'",
416 p->u.elementSetName);
417 return 25; /* invalid esetname */
419 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
420 p->u.elementSetName);
423 case Z_ElementSpec_externalSpec:
424 if (p->u.externalSpec->which == Z_External_espec1)
426 logf(LOG_DEBUG, "Got Espec-1");
427 espec = p->u.externalSpec-> u.espec1;
431 logf(LOG_LOG, "Unknown external espec.");
432 return 25; /* bad. what is proper diagnostic? */
442 logf (LOG_LOG, "Element: Espec-1 match");
443 return data1_doespec1(dh, n, espec);
447 logf (LOG_DEBUG, "Element: all match");
452 static int grs_retrieve(struct recRetrieveCtrl *p)
454 data1_node *node = 0, *onode = 0;
457 int res, selected = 0;
459 struct grs_read_info gri;
462 gri.readf = p->readf;
463 gri.seekf = p->seekf;
464 gri.tellf = p->tellf;
471 logf (LOG_DEBUG, "grs_retrieve");
472 node = read_grs_type (&gri, p->subType);
479 logf (LOG_DEBUG, "grs_retrieve: size");
480 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
483 dnew->u.data.what = DATA1I_text;
484 dnew->u.data.data = dnew->lbuf;
485 sprintf(dnew->u.data.data, "%d", p->recordSize);
486 dnew->u.data.len = strlen(dnew->u.data.data);
489 logf (LOG_DEBUG, "grs_retrieve: score");
490 if (p->score >= 0 && (dnew =
491 data1_insert_taggeddata(p->dh, node,
495 dnew->u.data.what = DATA1I_num;
496 dnew->u.data.data = dnew->lbuf;
497 sprintf(dnew->u.data.data, "%d", p->score);
498 dnew->u.data.len = strlen(dnew->u.data.data);
501 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
502 if (p->localno > 0 && (dnew = data1_insert_taggeddata(p->dh, node, node,
503 "localControlNumber", mem)))
505 dnew->u.data.what = DATA1I_text;
506 dnew->u.data.data = dnew->lbuf;
507 sprintf(dnew->u.data.data, "%d", p->localno);
508 dnew->u.data.len = strlen(dnew->u.data.data);
511 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
512 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
513 node->u.root.absyn->reference != VAL_NONE)
517 int oidtmp[OID_SIZE];
519 oe.proto = PROTO_Z3950;
520 oe.oclass = CLASS_SCHEMA;
521 oe.value = node->u.root.absyn->reference;
523 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
526 data1_handle dh = p->dh;
530 for (ii = oid; *ii >= 0; ii++)
534 sprintf(p, "%d", *ii);
539 if ((dnew = data1_insert_taggeddata(dh, node, node,
540 "schemaIdentifier", mem)))
542 dnew->u.data.what = DATA1I_oid;
543 dnew->u.data.data = nmem_malloc(mem, p - tmp);
544 memcpy(dnew->u.data.data, tmp, p - tmp);
545 dnew->u.data.len = p - tmp;
550 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
552 * Does the requested format match a known schema-mapping? (this reflects
553 * the overlap of schema and formatting which is inherent in the MARC
555 * NOTE: This should look at the schema-specification in the compspec
558 for (map = node->u.root.absyn->maptabs; map; map = map->next)
559 if (map->target_absyn_ref == p->input_format)
562 if (!(node = data1_map_record(p->dh, onode, map, mem)))
571 logf (LOG_DEBUG, "grs_retrieve: element spec");
572 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
576 data1_free_tree(p->dh, onode);
577 data1_free_tree(p->dh, node);
581 else if (p->comp && !res)
584 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
585 switch (p->output_format = (p->input_format != VAL_NONE ?
586 p->input_format : VAL_SUTRS))
588 data1_marctab *marctab;
593 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
595 p->diagnostic = 238; /* not available in requested syntax */
600 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
607 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
614 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
619 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
624 if (!node->u.root.absyn)
629 for (marctab = node->u.root.absyn->marc; marctab;
630 marctab = marctab->next)
631 if (marctab->reference == p->input_format)
638 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
647 data1_free_tree(p->dh, node);
649 data1_free_tree(p->dh, onode);
654 static struct recType grs_type =
663 RecType recTypeGrs = &grs_type;