2 * Copyright (C) 1994-1998, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.23 1998-10-18 07:51:10 adam
8 * Changed one logf call.
10 * Revision 1.22 1998/10/16 08:14:37 adam
11 * Updated record control system.
13 * Revision 1.21 1998/07/01 09:16:10 adam
14 * Element localno only added when it's greater than 0.
16 * Revision 1.20 1998/05/20 10:12:26 adam
17 * Implemented automatic EXPLAIN database maintenance.
18 * Modified Zebra to work with ASN.1 compiled version of YAZ.
20 * Revision 1.19 1998/03/11 11:19:05 adam
21 * Changed the way sequence numbers are generated.
23 * Revision 1.18 1998/03/05 08:41:31 adam
26 * Revision 1.17 1998/02/10 12:03:06 adam
29 * Revision 1.16 1998/01/29 13:38:17 adam
30 * Fixed problem with mapping to record with unknown schema.
32 * Revision 1.15 1998/01/26 10:37:57 adam
35 * Revision 1.14 1997/11/06 11:41:01 adam
36 * Implemented "begin variant" for the sgml.regx filter.
38 * Revision 1.13 1997/10/31 12:35:44 adam
39 * Added a few log statements.
41 * Revision 1.12 1997/10/29 12:02:22 adam
42 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
44 * Revision 1.11 1997/10/27 14:34:00 adam
45 * Work on generic character mapping depending on "structure" field
46 * in abstract syntax file.
48 * Revision 1.10 1997/09/18 08:59:21 adam
49 * Extra generic handle for the character mapping routines.
51 * Revision 1.9 1997/09/17 12:19:21 adam
52 * Zebra version corresponds to YAZ version 1.4.
53 * Changed Zebra server so that it doesn't depend on global common_resource.
55 * Revision 1.8 1997/09/09 13:38:14 adam
56 * Partial port to WIN95/NT.
58 * Revision 1.7 1997/09/05 15:30:10 adam
59 * Changed prototype for chr_map_input - added const.
60 * Added support for C++, headers uses extern "C" for public definitions.
62 * Revision 1.6 1997/09/04 13:54:40 adam
63 * Added MARC filter - type grs.marc.<syntax> where syntax refers
64 * to abstract syntax. New method tellf in retrieve/extract method.
66 * Revision 1.5 1997/07/15 16:29:03 adam
67 * Initialized dummy variable to keep checker gcc happy.
69 * Revision 1.4 1997/04/30 08:56:08 quinn
72 * Revision 1.2 1996/10/11 16:06:43 quinn
73 * Revision 1.3 1997/02/24 10:41:50 adam
74 * Cleanup of code and commented out the "end element-end-record" code.
76 * Revision 1.2 1996/10/11 16:06:43 quinn
77 * Fixed arguments to nodetogr
79 * Revision 1.1 1996/10/11 10:57:25 adam
80 * New module recctrl. Used to manage records (extract/retrieval).
82 * Revision 1.29 1996/10/08 10:30:21 quinn
85 * Revision 1.28 1996/10/07 16:06:40 quinn
88 * Revision 1.27 1996/06/11 10:54:12 quinn
91 * Revision 1.26 1996/06/06 12:08:45 quinn
92 * Added showRecord function
94 * Revision 1.25 1996/06/04 14:18:53 quinn
97 * Revision 1.24 1996/06/04 13:27:54 quinn
98 * More work on charmapping
100 * Revision 1.23 1996/06/04 10:19:01 adam
101 * Minor changes - removed include of ctype.h.
103 * Revision 1.22 1996/06/03 10:15:27 quinn
104 * Various character-mapping.
106 * Revision 1.21 1996/05/31 13:27:24 quinn
107 * Character-conversion in phrases, too.
109 * Revision 1.19 1996/05/16 15:31:14 quinn
112 * Revision 1.18 1996/05/09 07:28:56 quinn
113 * Work towards phrases and multiple registers
115 * Revision 1.17 1996/05/01 13:46:37 adam
116 * First work on multiple records in one file.
117 * New option, -offset, to the "unread" command in the filter module.
119 * Revision 1.16 1996/01/17 14:57:54 adam
120 * Prototype changed for reader functions in extract/retrieve. File
121 * is identified by 'void *' instead of 'int.
123 * Revision 1.15 1996/01/08 19:15:47 adam
124 * New input filter that works!
126 * Revision 1.14 1995/12/15 12:36:11 adam
127 * Retrieval calls data1_read_regx when subType is specified.
129 * Revision 1.13 1995/12/15 12:24:43 quinn
130 * *** empty log message ***
132 * Revision 1.12 1995/12/15 12:20:28 quinn
133 * *** empty log message ***
135 * Revision 1.11 1995/12/15 12:07:57 quinn
136 * Changed extraction strategy.
138 * Revision 1.10 1995/12/14 11:10:48 quinn
141 * Revision 1.9 1995/12/13 17:14:05 quinn
142 * *** empty log message ***
144 * Revision 1.8 1995/12/13 15:33:18 quinn
145 * *** empty log message ***
147 * Revision 1.7 1995/12/13 13:45:39 quinn
148 * Changed data1 to use nmem.
150 * Revision 1.6 1995/12/04 14:22:30 adam
151 * Extra arg to recType_byName.
152 * Started work on new regular expression parsed input to
153 * structured records.
155 * Revision 1.5 1995/11/28 14:18:37 quinn
158 * Revision 1.4 1995/11/21 13:14:49 quinn
159 * Fixed end-of-data-field problem (maybe).
161 * Revision 1.3 1995/11/15 19:13:09 adam
162 * Work on record management.
168 #include <sys/types.h>
179 #define GRS_MAX_WORD 512
181 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
185 data1_node *(*func)(struct grs_read_info *p);
187 { "sgml", grs_read_sgml },
188 { "regx", grs_read_regx },
189 { "marc", grs_read_marc },
192 const char *cp = strchr (type, '.');
195 if (cp == NULL || cp == type)
197 cp = strlen(type) + type;
201 strcpy (p->type, cp+1);
202 for (i=0; tab[i].type; i++)
204 if (!memcmp (type, tab[i].type, cp-type))
205 return (tab[i].func)(p);
210 static void grs_init(RecType recType)
214 static void grs_destroy(RecType recType)
218 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
221 (*p->init)(p, &wrd); /* set defaults */
222 for (; n; n = n->next)
224 if (p->flagShowRecords) /* display element description to user */
226 if (n->which == DATA1N_root)
228 printf("%*s", level * 4, "");
229 printf("Record type: '%s'\n", n->u.root.absyn->name);
231 else if (n->which == DATA1N_tag)
235 printf("%*s", level * 4, "");
236 if (!(e = n->u.tag.element))
237 printf("Local tag: '%s'\n", n->u.tag.tag);
240 printf("Elm: '%s' ", e->name);
243 data1_tag *t = e->tag;
245 printf("TagNam: '%s' ", t->names->name);
248 printf("%s[%d],", t->tagset->name, t->tagset->type);
251 if (t->which == DATA1T_numeric)
252 printf("%d)", t->value.numeric);
254 printf("'%s')", t->value.string);
262 if (dumpkeys(n->child, p, level + 1) < 0)
265 if (n->which == DATA1N_data)
267 data1_node *par = get_parent_tag(p->dh, n);
268 data1_termlist *tlist = 0;
269 data1_datatype dtype = DATA1K_string;
271 if (p->flagShowRecords)
273 printf("%*s", level * 4, "");
275 if (n->u.data.len > 32)
276 printf("'%.24s ... %.6s'\n", n->u.data.data,
277 n->u.data.data + n->u.data.len-6);
278 else if (n->u.data.len > 0)
279 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
287 * cycle up towards the root until we find a tag with an att..
288 * this has the effect of indexing locally defined tags with
289 * the attribute of their ancestor in the record.
292 while (!par->u.tag.element)
293 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
295 if (!par || !(tlist = par->u.tag.element->termlists))
297 if (par->u.tag.element->tag)
298 dtype = par->u.tag.element->tag->kind;
299 for (; tlist; tlist = tlist->next)
301 if (p->flagShowRecords)
303 printf("%*sIdx: [%s]", (level + 1) * 4, "",
305 printf("%s:%s [%d]\n",
306 tlist->att->parent->name,
307 tlist->att->name, tlist->att->value);
311 wrd.reg_type = *tlist->structure;
312 wrd.string = n->u.data.data;
313 wrd.length = n->u.data.len;
314 wrd.attrSet = (int) (tlist->att->parent->reference);
315 wrd.attrUse = tlist->att->locals->local;
320 if (p->flagShowRecords && n->which == DATA1N_root)
322 printf("%*s-------------\n\n", level * 4, "");
328 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
331 int oidtmp[OID_SIZE];
333 oe.proto = PROTO_Z3950;
334 oe.oclass = CLASS_SCHEMA;
335 oe.value = n->u.root.absyn->reference;
337 if ((oid_ent_to_oid (&oe, oidtmp)))
338 (*p->addSchema)(p, oidtmp);
340 return dumpkeys(n, p, 0);
343 static int grs_extract(struct recExtractCtrl *p)
347 struct grs_read_info gri;
349 int oidtmp[OID_SIZE];
351 mem = nmem_create ();
352 gri.readf = p->readf;
353 gri.seekf = p->seekf;
354 gri.tellf = p->tellf;
357 gri.offset = p->offset;
361 n = read_grs_type (&gri, p->subType);
365 oe.proto = PROTO_Z3950;
366 oe.oclass = CLASS_SCHEMA;
367 oe.value = n->u.root.absyn->reference;
368 if ((oid_ent_to_oid (&oe, oidtmp)))
369 (*p->addSchema)(p, oidtmp);
371 if (dumpkeys(n, p, 0) < 0)
373 data1_free_tree(p->dh, n);
376 data1_free_tree(p->dh, n);
382 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
384 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
386 data1_esetname *eset;
392 case Z_RecordComp_simple:
393 if (c->u.simple->which != Z_ElementSetNames_generic)
394 return 26; /* only generic form supported. Fix this later */
395 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
396 c->u.simple->u.generic)))
398 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
399 return 25; /* invalid esetname */
401 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
402 c->u.simple->u.generic);
405 case Z_RecordComp_complex:
406 if (c->u.complex->generic)
408 /* insert check for schema */
409 if ((p = c->u.complex->generic->elementSpec))
412 case Z_ElementSpec_elementSetName:
414 data1_getesetbyname(dh,
416 p->u.elementSetName)))
418 logf(LOG_LOG, "Unknown esetname '%s'",
419 p->u.elementSetName);
420 return 25; /* invalid esetname */
422 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
423 p->u.elementSetName);
426 case Z_ElementSpec_externalSpec:
427 if (p->u.externalSpec->which == Z_External_espec1)
429 logf(LOG_DEBUG, "Got Espec-1");
430 espec = p->u.externalSpec-> u.espec1;
434 logf(LOG_LOG, "Unknown external espec.");
435 return 25; /* bad. what is proper diagnostic? */
445 logf (LOG_DEBUG, "Element: Espec-1 match");
446 return data1_doespec1(dh, n, espec);
450 logf (LOG_DEBUG, "Element: all match");
455 static int grs_retrieve(struct recRetrieveCtrl *p)
457 data1_node *node = 0, *onode = 0;
460 int res, selected = 0;
462 struct grs_read_info gri;
465 gri.readf = p->readf;
466 gri.seekf = p->seekf;
467 gri.tellf = p->tellf;
474 logf (LOG_DEBUG, "grs_retrieve");
475 node = read_grs_type (&gri, p->subType);
482 logf (LOG_DEBUG, "grs_retrieve: size");
483 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
486 dnew->u.data.what = DATA1I_text;
487 dnew->u.data.data = dnew->lbuf;
488 sprintf(dnew->u.data.data, "%d", p->recordSize);
489 dnew->u.data.len = strlen(dnew->u.data.data);
492 logf (LOG_DEBUG, "grs_retrieve: score");
493 if (p->score >= 0 && (dnew =
494 data1_insert_taggeddata(p->dh, node,
498 dnew->u.data.what = DATA1I_num;
499 dnew->u.data.data = dnew->lbuf;
500 sprintf(dnew->u.data.data, "%d", p->score);
501 dnew->u.data.len = strlen(dnew->u.data.data);
504 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
505 if (p->localno > 0 && (dnew = data1_insert_taggeddata(p->dh, node, node,
506 "localControlNumber", mem)))
508 dnew->u.data.what = DATA1I_text;
509 dnew->u.data.data = dnew->lbuf;
510 sprintf(dnew->u.data.data, "%d", p->localno);
511 dnew->u.data.len = strlen(dnew->u.data.data);
514 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
515 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
516 node->u.root.absyn->reference != VAL_NONE)
520 int oidtmp[OID_SIZE];
522 oe.proto = PROTO_Z3950;
523 oe.oclass = CLASS_SCHEMA;
524 oe.value = node->u.root.absyn->reference;
526 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
529 data1_handle dh = p->dh;
533 for (ii = oid; *ii >= 0; ii++)
537 sprintf(p, "%d", *ii);
542 if ((dnew = data1_insert_taggeddata(dh, node, node,
543 "schemaIdentifier", mem)))
545 dnew->u.data.what = DATA1I_oid;
546 dnew->u.data.data = nmem_malloc(mem, p - tmp);
547 memcpy(dnew->u.data.data, tmp, p - tmp);
548 dnew->u.data.len = p - tmp;
553 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
555 * Does the requested format match a known schema-mapping? (this reflects
556 * the overlap of schema and formatting which is inherent in the MARC
558 * NOTE: This should look at the schema-specification in the compspec
561 for (map = node->u.root.absyn->maptabs; map; map = map->next)
562 if (map->target_absyn_ref == p->input_format)
565 if (!(node = data1_map_record(p->dh, onode, map, mem)))
574 logf (LOG_DEBUG, "grs_retrieve: element spec");
575 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
579 data1_free_tree(p->dh, onode);
580 data1_free_tree(p->dh, node);
584 else if (p->comp && !res)
587 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
588 switch (p->output_format = (p->input_format != VAL_NONE ?
589 p->input_format : VAL_SUTRS))
591 data1_marctab *marctab;
596 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
598 p->diagnostic = 238; /* not available in requested syntax */
603 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
610 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
617 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
622 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
627 if (!node->u.root.absyn)
632 for (marctab = node->u.root.absyn->marc; marctab;
633 marctab = marctab->next)
634 if (marctab->reference == p->input_format)
641 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
650 data1_free_tree(p->dh, node);
652 data1_free_tree(p->dh, onode);
657 static struct recType grs_type =
666 RecType recTypeGrs = &grs_type;