2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.24 1999-02-02 14:51:28 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.23 1998/10/18 07:51:10 adam
11 * Changed one logf call.
13 * Revision 1.22 1998/10/16 08:14:37 adam
14 * Updated record control system.
16 * Revision 1.21 1998/07/01 09:16:10 adam
17 * Element localno only added when it's greater than 0.
19 * Revision 1.20 1998/05/20 10:12:26 adam
20 * Implemented automatic EXPLAIN database maintenance.
21 * Modified Zebra to work with ASN.1 compiled version of YAZ.
23 * Revision 1.19 1998/03/11 11:19:05 adam
24 * Changed the way sequence numbers are generated.
26 * Revision 1.18 1998/03/05 08:41:31 adam
29 * Revision 1.17 1998/02/10 12:03:06 adam
32 * Revision 1.16 1998/01/29 13:38:17 adam
33 * Fixed problem with mapping to record with unknown schema.
35 * Revision 1.15 1998/01/26 10:37:57 adam
38 * Revision 1.14 1997/11/06 11:41:01 adam
39 * Implemented "begin variant" for the sgml.regx filter.
41 * Revision 1.13 1997/10/31 12:35:44 adam
42 * Added a few log statements.
44 * Revision 1.12 1997/10/29 12:02:22 adam
45 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
47 * Revision 1.11 1997/10/27 14:34:00 adam
48 * Work on generic character mapping depending on "structure" field
49 * in abstract syntax file.
51 * Revision 1.10 1997/09/18 08:59:21 adam
52 * Extra generic handle for the character mapping routines.
54 * Revision 1.9 1997/09/17 12:19:21 adam
55 * Zebra version corresponds to YAZ version 1.4.
56 * Changed Zebra server so that it doesn't depend on global common_resource.
58 * Revision 1.8 1997/09/09 13:38:14 adam
59 * Partial port to WIN95/NT.
61 * Revision 1.7 1997/09/05 15:30:10 adam
62 * Changed prototype for chr_map_input - added const.
63 * Added support for C++, headers uses extern "C" for public definitions.
65 * Revision 1.6 1997/09/04 13:54:40 adam
66 * Added MARC filter - type grs.marc.<syntax> where syntax refers
67 * to abstract syntax. New method tellf in retrieve/extract method.
69 * Revision 1.5 1997/07/15 16:29:03 adam
70 * Initialized dummy variable to keep checker gcc happy.
72 * Revision 1.4 1997/04/30 08:56:08 quinn
75 * Revision 1.2 1996/10/11 16:06:43 quinn
76 * Revision 1.3 1997/02/24 10:41:50 adam
77 * Cleanup of code and commented out the "end element-end-record" code.
79 * Revision 1.2 1996/10/11 16:06:43 quinn
80 * Fixed arguments to nodetogr
82 * Revision 1.1 1996/10/11 10:57:25 adam
83 * New module recctrl. Used to manage records (extract/retrieval).
85 * Revision 1.29 1996/10/08 10:30:21 quinn
88 * Revision 1.28 1996/10/07 16:06:40 quinn
91 * Revision 1.27 1996/06/11 10:54:12 quinn
94 * Revision 1.26 1996/06/06 12:08:45 quinn
95 * Added showRecord function
97 * Revision 1.25 1996/06/04 14:18:53 quinn
100 * Revision 1.24 1996/06/04 13:27:54 quinn
101 * More work on charmapping
103 * Revision 1.23 1996/06/04 10:19:01 adam
104 * Minor changes - removed include of ctype.h.
106 * Revision 1.22 1996/06/03 10:15:27 quinn
107 * Various character-mapping.
109 * Revision 1.21 1996/05/31 13:27:24 quinn
110 * Character-conversion in phrases, too.
112 * Revision 1.19 1996/05/16 15:31:14 quinn
115 * Revision 1.18 1996/05/09 07:28:56 quinn
116 * Work towards phrases and multiple registers
118 * Revision 1.17 1996/05/01 13:46:37 adam
119 * First work on multiple records in one file.
120 * New option, -offset, to the "unread" command in the filter module.
122 * Revision 1.16 1996/01/17 14:57:54 adam
123 * Prototype changed for reader functions in extract/retrieve. File
124 * is identified by 'void *' instead of 'int.
126 * Revision 1.15 1996/01/08 19:15:47 adam
127 * New input filter that works!
129 * Revision 1.14 1995/12/15 12:36:11 adam
130 * Retrieval calls data1_read_regx when subType is specified.
132 * Revision 1.13 1995/12/15 12:24:43 quinn
133 * *** empty log message ***
135 * Revision 1.12 1995/12/15 12:20:28 quinn
136 * *** empty log message ***
138 * Revision 1.11 1995/12/15 12:07:57 quinn
139 * Changed extraction strategy.
141 * Revision 1.10 1995/12/14 11:10:48 quinn
144 * Revision 1.9 1995/12/13 17:14:05 quinn
145 * *** empty log message ***
147 * Revision 1.8 1995/12/13 15:33:18 quinn
148 * *** empty log message ***
150 * Revision 1.7 1995/12/13 13:45:39 quinn
151 * Changed data1 to use nmem.
153 * Revision 1.6 1995/12/04 14:22:30 adam
154 * Extra arg to recType_byName.
155 * Started work on new regular expression parsed input to
156 * structured records.
158 * Revision 1.5 1995/11/28 14:18:37 quinn
161 * Revision 1.4 1995/11/21 13:14:49 quinn
162 * Fixed end-of-data-field problem (maybe).
164 * Revision 1.3 1995/11/15 19:13:09 adam
165 * Work on record management.
171 #include <sys/types.h>
182 #define GRS_MAX_WORD 512
184 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
188 data1_node *(*func)(struct grs_read_info *p);
190 { "sgml", grs_read_sgml },
191 { "regx", grs_read_regx },
192 { "marc", grs_read_marc },
195 const char *cp = strchr (type, '.');
198 if (cp == NULL || cp == type)
200 cp = strlen(type) + type;
204 strcpy (p->type, cp+1);
205 for (i=0; tab[i].type; i++)
207 if (!memcmp (type, tab[i].type, cp-type))
208 return (tab[i].func)(p);
213 static void grs_init(RecType recType)
217 static void grs_destroy(RecType recType)
221 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
224 (*p->init)(p, &wrd); /* set defaults */
225 for (; n; n = n->next)
227 if (p->flagShowRecords) /* display element description to user */
229 if (n->which == DATA1N_root)
231 printf("%*s", level * 4, "");
232 printf("Record type: '%s'\n", n->u.root.absyn->name);
234 else if (n->which == DATA1N_tag)
238 printf("%*s", level * 4, "");
239 if (!(e = n->u.tag.element))
240 printf("Local tag: '%s'\n", n->u.tag.tag);
243 printf("Elm: '%s' ", e->name);
246 data1_tag *t = e->tag;
248 printf("TagNam: '%s' ", t->names->name);
251 printf("%s[%d],", t->tagset->name, t->tagset->type);
254 if (t->which == DATA1T_numeric)
255 printf("%d)", t->value.numeric);
257 printf("'%s')", t->value.string);
265 if (dumpkeys(n->child, p, level + 1) < 0)
268 if (n->which == DATA1N_data)
270 data1_node *par = get_parent_tag(p->dh, n);
271 data1_termlist *tlist = 0;
272 data1_datatype dtype = DATA1K_string;
274 if (p->flagShowRecords)
276 printf("%*s", level * 4, "");
278 if (n->u.data.len > 32)
279 printf("'%.24s ... %.6s'\n", n->u.data.data,
280 n->u.data.data + n->u.data.len-6);
281 else if (n->u.data.len > 0)
282 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
290 * cycle up towards the root until we find a tag with an att..
291 * this has the effect of indexing locally defined tags with
292 * the attribute of their ancestor in the record.
295 while (!par->u.tag.element)
296 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
298 if (!par || !(tlist = par->u.tag.element->termlists))
300 if (par->u.tag.element->tag)
301 dtype = par->u.tag.element->tag->kind;
302 for (; tlist; tlist = tlist->next)
304 if (p->flagShowRecords)
306 printf("%*sIdx: [%s]", (level + 1) * 4, "",
308 printf("%s:%s [%d]\n",
309 tlist->att->parent->name,
310 tlist->att->name, tlist->att->value);
314 wrd.reg_type = *tlist->structure;
315 wrd.string = n->u.data.data;
316 wrd.length = n->u.data.len;
317 wrd.attrSet = (int) (tlist->att->parent->reference);
318 wrd.attrUse = tlist->att->locals->local;
323 if (p->flagShowRecords && n->which == DATA1N_root)
325 printf("%*s-------------\n\n", level * 4, "");
331 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
334 int oidtmp[OID_SIZE];
336 oe.proto = PROTO_Z3950;
337 oe.oclass = CLASS_SCHEMA;
338 oe.value = n->u.root.absyn->reference;
340 if ((oid_ent_to_oid (&oe, oidtmp)))
341 (*p->addSchema)(p, oidtmp);
343 return dumpkeys(n, p, 0);
346 static int grs_extract(struct recExtractCtrl *p)
350 struct grs_read_info gri;
352 int oidtmp[OID_SIZE];
354 mem = nmem_create ();
355 gri.readf = p->readf;
356 gri.seekf = p->seekf;
357 gri.tellf = p->tellf;
360 gri.offset = p->offset;
364 n = read_grs_type (&gri, p->subType);
368 oe.proto = PROTO_Z3950;
369 oe.oclass = CLASS_SCHEMA;
370 oe.value = n->u.root.absyn->reference;
371 if ((oid_ent_to_oid (&oe, oidtmp)))
372 (*p->addSchema)(p, oidtmp);
374 if (dumpkeys(n, p, 0) < 0)
376 data1_free_tree(p->dh, n);
379 data1_free_tree(p->dh, n);
385 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
387 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
389 data1_esetname *eset;
395 case Z_RecordComp_simple:
396 if (c->u.simple->which != Z_ElementSetNames_generic)
397 return 26; /* only generic form supported. Fix this later */
398 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
399 c->u.simple->u.generic)))
401 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
402 return 25; /* invalid esetname */
404 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
405 c->u.simple->u.generic);
408 case Z_RecordComp_complex:
409 if (c->u.complex->generic)
411 /* insert check for schema */
412 if ((p = c->u.complex->generic->elementSpec))
415 case Z_ElementSpec_elementSetName:
417 data1_getesetbyname(dh,
419 p->u.elementSetName)))
421 logf(LOG_LOG, "Unknown esetname '%s'",
422 p->u.elementSetName);
423 return 25; /* invalid esetname */
425 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
426 p->u.elementSetName);
429 case Z_ElementSpec_externalSpec:
430 if (p->u.externalSpec->which == Z_External_espec1)
432 logf(LOG_DEBUG, "Got Espec-1");
433 espec = p->u.externalSpec-> u.espec1;
437 logf(LOG_LOG, "Unknown external espec.");
438 return 25; /* bad. what is proper diagnostic? */
448 logf (LOG_DEBUG, "Element: Espec-1 match");
449 return data1_doespec1(dh, n, espec);
453 logf (LOG_DEBUG, "Element: all match");
458 static int grs_retrieve(struct recRetrieveCtrl *p)
460 data1_node *node = 0, *onode = 0;
463 int res, selected = 0;
465 struct grs_read_info gri;
468 gri.readf = p->readf;
469 gri.seekf = p->seekf;
470 gri.tellf = p->tellf;
477 logf (LOG_DEBUG, "grs_retrieve");
478 node = read_grs_type (&gri, p->subType);
485 logf (LOG_DEBUG, "grs_retrieve: size");
486 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
489 dnew->u.data.what = DATA1I_text;
490 dnew->u.data.data = dnew->lbuf;
491 sprintf(dnew->u.data.data, "%d", p->recordSize);
492 dnew->u.data.len = strlen(dnew->u.data.data);
495 logf (LOG_DEBUG, "grs_retrieve: score");
496 if (p->score >= 0 && (dnew =
497 data1_insert_taggeddata(p->dh, node,
501 dnew->u.data.what = DATA1I_num;
502 dnew->u.data.data = dnew->lbuf;
503 sprintf(dnew->u.data.data, "%d", p->score);
504 dnew->u.data.len = strlen(dnew->u.data.data);
507 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
508 if (p->localno > 0 && (dnew = data1_insert_taggeddata(p->dh, node, node,
509 "localControlNumber", mem)))
511 dnew->u.data.what = DATA1I_text;
512 dnew->u.data.data = dnew->lbuf;
513 sprintf(dnew->u.data.data, "%d", p->localno);
514 dnew->u.data.len = strlen(dnew->u.data.data);
517 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
518 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
519 node->u.root.absyn->reference != VAL_NONE)
523 int oidtmp[OID_SIZE];
525 oe.proto = PROTO_Z3950;
526 oe.oclass = CLASS_SCHEMA;
527 oe.value = node->u.root.absyn->reference;
529 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
532 data1_handle dh = p->dh;
536 for (ii = oid; *ii >= 0; ii++)
540 sprintf(p, "%d", *ii);
545 if ((dnew = data1_insert_taggeddata(dh, node, node,
546 "schemaIdentifier", mem)))
548 dnew->u.data.what = DATA1I_oid;
549 dnew->u.data.data = nmem_malloc(mem, p - tmp);
550 memcpy(dnew->u.data.data, tmp, p - tmp);
551 dnew->u.data.len = p - tmp;
556 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
558 * Does the requested format match a known schema-mapping? (this reflects
559 * the overlap of schema and formatting which is inherent in the MARC
561 * NOTE: This should look at the schema-specification in the compspec
564 for (map = node->u.root.absyn->maptabs; map; map = map->next)
565 if (map->target_absyn_ref == p->input_format)
568 if (!(node = data1_map_record(p->dh, onode, map, mem)))
577 logf (LOG_DEBUG, "grs_retrieve: element spec");
578 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
582 data1_free_tree(p->dh, onode);
583 data1_free_tree(p->dh, node);
587 else if (p->comp && !res)
590 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
591 switch (p->output_format = (p->input_format != VAL_NONE ?
592 p->input_format : VAL_SUTRS))
594 data1_marctab *marctab;
599 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
601 p->diagnostic = 238; /* not available in requested syntax */
606 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
613 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
620 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
625 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
630 if (!node->u.root.absyn)
635 for (marctab = node->u.root.absyn->marc; marctab;
636 marctab = marctab->next)
637 if (marctab->reference == p->input_format)
644 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
653 data1_free_tree(p->dh, node);
655 data1_free_tree(p->dh, onode);
660 static struct recType grs_type =
669 RecType recTypeGrs = &grs_type;