2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.20 1998-05-20 10:12:26 adam
8 * Implemented automatic EXPLAIN database maintenance.
9 * Modified Zebra to work with ASN.1 compiled version of YAZ.
11 * Revision 1.19 1998/03/11 11:19:05 adam
12 * Changed the way sequence numbers are generated.
14 * Revision 1.18 1998/03/05 08:41:31 adam
17 * Revision 1.17 1998/02/10 12:03:06 adam
20 * Revision 1.16 1998/01/29 13:38:17 adam
21 * Fixed problem with mapping to record with unknown schema.
23 * Revision 1.15 1998/01/26 10:37:57 adam
26 * Revision 1.14 1997/11/06 11:41:01 adam
27 * Implemented "begin variant" for the sgml.regx filter.
29 * Revision 1.13 1997/10/31 12:35:44 adam
30 * Added a few log statements.
32 * Revision 1.12 1997/10/29 12:02:22 adam
33 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
35 * Revision 1.11 1997/10/27 14:34:00 adam
36 * Work on generic character mapping depending on "structure" field
37 * in abstract syntax file.
39 * Revision 1.10 1997/09/18 08:59:21 adam
40 * Extra generic handle for the character mapping routines.
42 * Revision 1.9 1997/09/17 12:19:21 adam
43 * Zebra version corresponds to YAZ version 1.4.
44 * Changed Zebra server so that it doesn't depend on global common_resource.
46 * Revision 1.8 1997/09/09 13:38:14 adam
47 * Partial port to WIN95/NT.
49 * Revision 1.7 1997/09/05 15:30:10 adam
50 * Changed prototype for chr_map_input - added const.
51 * Added support for C++, headers uses extern "C" for public definitions.
53 * Revision 1.6 1997/09/04 13:54:40 adam
54 * Added MARC filter - type grs.marc.<syntax> where syntax refers
55 * to abstract syntax. New method tellf in retrieve/extract method.
57 * Revision 1.5 1997/07/15 16:29:03 adam
58 * Initialized dummy variable to keep checker gcc happy.
60 * Revision 1.4 1997/04/30 08:56:08 quinn
63 * Revision 1.2 1996/10/11 16:06:43 quinn
64 * Revision 1.3 1997/02/24 10:41:50 adam
65 * Cleanup of code and commented out the "end element-end-record" code.
67 * Revision 1.2 1996/10/11 16:06:43 quinn
68 * Fixed arguments to nodetogr
70 * Revision 1.1 1996/10/11 10:57:25 adam
71 * New module recctrl. Used to manage records (extract/retrieval).
73 * Revision 1.29 1996/10/08 10:30:21 quinn
76 * Revision 1.28 1996/10/07 16:06:40 quinn
79 * Revision 1.27 1996/06/11 10:54:12 quinn
82 * Revision 1.26 1996/06/06 12:08:45 quinn
83 * Added showRecord function
85 * Revision 1.25 1996/06/04 14:18:53 quinn
88 * Revision 1.24 1996/06/04 13:27:54 quinn
89 * More work on charmapping
91 * Revision 1.23 1996/06/04 10:19:01 adam
92 * Minor changes - removed include of ctype.h.
94 * Revision 1.22 1996/06/03 10:15:27 quinn
95 * Various character-mapping.
97 * Revision 1.21 1996/05/31 13:27:24 quinn
98 * Character-conversion in phrases, too.
100 * Revision 1.19 1996/05/16 15:31:14 quinn
103 * Revision 1.18 1996/05/09 07:28:56 quinn
104 * Work towards phrases and multiple registers
106 * Revision 1.17 1996/05/01 13:46:37 adam
107 * First work on multiple records in one file.
108 * New option, -offset, to the "unread" command in the filter module.
110 * Revision 1.16 1996/01/17 14:57:54 adam
111 * Prototype changed for reader functions in extract/retrieve. File
112 * is identified by 'void *' instead of 'int.
114 * Revision 1.15 1996/01/08 19:15:47 adam
115 * New input filter that works!
117 * Revision 1.14 1995/12/15 12:36:11 adam
118 * Retrieval calls data1_read_regx when subType is specified.
120 * Revision 1.13 1995/12/15 12:24:43 quinn
121 * *** empty log message ***
123 * Revision 1.12 1995/12/15 12:20:28 quinn
124 * *** empty log message ***
126 * Revision 1.11 1995/12/15 12:07:57 quinn
127 * Changed extraction strategy.
129 * Revision 1.10 1995/12/14 11:10:48 quinn
132 * Revision 1.9 1995/12/13 17:14:05 quinn
133 * *** empty log message ***
135 * Revision 1.8 1995/12/13 15:33:18 quinn
136 * *** empty log message ***
138 * Revision 1.7 1995/12/13 13:45:39 quinn
139 * Changed data1 to use nmem.
141 * Revision 1.6 1995/12/04 14:22:30 adam
142 * Extra arg to recType_byName.
143 * Started work on new regular expression parsed input to
144 * structured records.
146 * Revision 1.5 1995/11/28 14:18:37 quinn
149 * Revision 1.4 1995/11/21 13:14:49 quinn
150 * Fixed end-of-data-field problem (maybe).
152 * Revision 1.3 1995/11/15 19:13:09 adam
153 * Work on record management.
159 #include <sys/types.h>
170 #define GRS_MAX_WORD 512
172 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
176 data1_node *(*func)(struct grs_read_info *p);
178 { "sgml", grs_read_sgml },
179 { "regx", grs_read_regx },
180 { "marc", grs_read_marc },
183 const char *cp = strchr (type, '.');
186 if (cp == NULL || cp == type)
188 cp = strlen(type) + type;
192 strcpy (p->type, cp+1);
193 for (i=0; tab[i].type; i++)
195 if (!memcmp (type, tab[i].type, cp-type))
196 return (tab[i].func)(p);
201 static void grs_init(void)
205 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
208 (*p->init)(p, &wrd); /* set defaults */
209 for (; n; n = n->next)
211 if (p->flagShowRecords) /* display element description to user */
213 if (n->which == DATA1N_root)
215 printf("%*s", level * 4, "");
216 printf("Record type: '%s'\n", n->u.root.absyn->name);
218 else if (n->which == DATA1N_tag)
222 printf("%*s", level * 4, "");
223 if (!(e = n->u.tag.element))
224 printf("Local tag: '%s'\n", n->u.tag.tag);
227 printf("Elm: '%s' ", e->name);
230 data1_tag *t = e->tag;
232 printf("TagNam: '%s' ", t->names->name);
235 printf("%s[%d],", t->tagset->name, t->tagset->type);
238 if (t->which == DATA1T_numeric)
239 printf("%d)", t->value.numeric);
241 printf("'%s')", t->value.string);
249 if (dumpkeys(n->child, p, level + 1) < 0)
252 if (n->which == DATA1N_data)
254 data1_node *par = get_parent_tag(p->dh, n);
255 data1_termlist *tlist = 0;
256 data1_datatype dtype = DATA1K_string;
258 if (p->flagShowRecords)
260 printf("%*s", level * 4, "");
262 if (n->u.data.len > 32)
263 printf("'%.24s ... %.6s'\n", n->u.data.data,
264 n->u.data.data + n->u.data.len-6);
265 else if (n->u.data.len > 0)
266 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
274 * cycle up towards the root until we find a tag with an att..
275 * this has the effect of indexing locally defined tags with
276 * the attribute of their ancestor in the record.
279 while (!par->u.tag.element)
280 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
282 if (!par || !(tlist = par->u.tag.element->termlists))
284 if (par->u.tag.element->tag)
285 dtype = par->u.tag.element->tag->kind;
286 for (; tlist; tlist = tlist->next)
288 if (p->flagShowRecords)
290 printf("%*sIdx: [%s]", (level + 1) * 4, "",
292 printf("%s:%s [%d]\n",
293 tlist->att->parent->name,
294 tlist->att->name, tlist->att->value);
298 wrd.reg_type = *tlist->structure;
299 wrd.string = n->u.data.data;
300 wrd.length = n->u.data.len;
301 wrd.attrSet = (int) (tlist->att->parent->reference);
302 wrd.attrUse = tlist->att->locals->local;
307 if (p->flagShowRecords && n->which == DATA1N_root)
309 printf("%*s-------------\n\n", level * 4, "");
315 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
318 int oidtmp[OID_SIZE];
320 oe.proto = PROTO_Z3950;
321 oe.oclass = CLASS_SCHEMA;
322 oe.value = n->u.root.absyn->reference;
324 if ((oid_ent_to_oid (&oe, oidtmp)))
325 (*p->addSchema)(p, oidtmp);
327 return dumpkeys(n, p, 0);
330 static int grs_extract(struct recExtractCtrl *p)
334 struct grs_read_info gri;
336 int oidtmp[OID_SIZE];
338 mem = nmem_create ();
339 gri.readf = p->readf;
340 gri.seekf = p->seekf;
341 gri.tellf = p->tellf;
344 gri.offset = p->offset;
348 n = read_grs_type (&gri, p->subType);
352 oe.proto = PROTO_Z3950;
353 oe.oclass = CLASS_SCHEMA;
354 oe.value = n->u.root.absyn->reference;
355 if ((oid_ent_to_oid (&oe, oidtmp)))
356 (*p->addSchema)(p, oidtmp);
358 if (dumpkeys(n, p, 0) < 0)
360 data1_free_tree(p->dh, n);
363 data1_free_tree(p->dh, n);
369 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
371 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
373 data1_esetname *eset;
379 case Z_RecordComp_simple:
380 if (c->u.simple->which != Z_ElementSetNames_generic)
381 return 26; /* only generic form supported. Fix this later */
382 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
383 c->u.simple->u.generic)))
385 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
386 return 25; /* invalid esetname */
388 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
389 c->u.simple->u.generic);
392 case Z_RecordComp_complex:
393 if (c->u.complex->generic)
395 /* insert check for schema */
396 if ((p = c->u.complex->generic->elementSpec))
399 case Z_ElementSpec_elementSetName:
401 data1_getesetbyname(dh,
403 p->u.elementSetName)))
405 logf(LOG_LOG, "Unknown esetname '%s'",
406 p->u.elementSetName);
407 return 25; /* invalid esetname */
409 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
410 p->u.elementSetName);
413 case Z_ElementSpec_externalSpec:
414 if (p->u.externalSpec->which == Z_External_espec1)
416 logf(LOG_DEBUG, "Got Espec-1");
417 espec = p->u.externalSpec-> u.espec1;
421 logf(LOG_LOG, "Unknown external espec.");
422 return 25; /* bad. what is proper diagnostic? */
432 logf (LOG_LOG, "Element: Espec-1 match");
433 return data1_doespec1(dh, n, espec);
437 logf (LOG_DEBUG, "Element: all match");
442 static int grs_retrieve(struct recRetrieveCtrl *p)
444 data1_node *node = 0, *onode = 0;
447 int res, selected = 0;
449 struct grs_read_info gri;
452 gri.readf = p->readf;
453 gri.seekf = p->seekf;
454 gri.tellf = p->tellf;
461 logf (LOG_DEBUG, "grs_retrieve");
462 node = read_grs_type (&gri, p->subType);
469 logf (LOG_DEBUG, "grs_retrieve: size");
470 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
473 dnew->u.data.what = DATA1I_text;
474 dnew->u.data.data = dnew->lbuf;
475 sprintf(dnew->u.data.data, "%d", p->recordSize);
476 dnew->u.data.len = strlen(dnew->u.data.data);
479 logf (LOG_DEBUG, "grs_retrieve: score");
480 if (p->score >= 0 && (dnew =
481 data1_insert_taggeddata(p->dh, node,
485 dnew->u.data.what = DATA1I_num;
486 dnew->u.data.data = dnew->lbuf;
487 sprintf(dnew->u.data.data, "%d", p->score);
488 dnew->u.data.len = strlen(dnew->u.data.data);
491 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
492 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
493 "localControlNumber", mem)))
495 dnew->u.data.what = DATA1I_text;
496 dnew->u.data.data = dnew->lbuf;
497 sprintf(dnew->u.data.data, "%d", p->localno);
498 dnew->u.data.len = strlen(dnew->u.data.data);
501 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
502 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
503 node->u.root.absyn->reference != VAL_NONE)
507 int oidtmp[OID_SIZE];
509 oe.proto = PROTO_Z3950;
510 oe.oclass = CLASS_SCHEMA;
511 oe.value = node->u.root.absyn->reference;
513 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
516 data1_handle dh = p->dh;
520 for (ii = oid; *ii >= 0; ii++)
524 sprintf(p, "%d", *ii);
529 if ((dnew = data1_insert_taggeddata(dh, node, node,
530 "schemaIdentifier", mem)))
532 dnew->u.data.what = DATA1I_oid;
533 dnew->u.data.data = nmem_malloc(mem, p - tmp);
534 memcpy(dnew->u.data.data, tmp, p - tmp);
535 dnew->u.data.len = p - tmp;
540 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
542 * Does the requested format match a known schema-mapping? (this reflects
543 * the overlap of schema and formatting which is inherent in the MARC
545 * NOTE: This should look at the schema-specification in the compspec
548 for (map = node->u.root.absyn->maptabs; map; map = map->next)
549 if (map->target_absyn_ref == p->input_format)
552 if (!(node = data1_map_record(p->dh, onode, map, mem)))
561 logf (LOG_DEBUG, "grs_retrieve: element spec");
562 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
566 data1_free_tree(p->dh, onode);
567 data1_free_tree(p->dh, node);
571 else if (p->comp && !res)
574 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
575 switch (p->output_format = (p->input_format != VAL_NONE ?
576 p->input_format : VAL_SUTRS))
578 data1_marctab *marctab;
583 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
585 p->diagnostic = 238; /* not available in requested syntax */
590 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
597 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
604 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
609 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
614 if (!node->u.root.absyn)
619 for (marctab = node->u.root.absyn->marc; marctab;
620 marctab = marctab->next)
621 if (marctab->reference == p->input_format)
628 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
637 data1_free_tree(p->dh, node);
639 data1_free_tree(p->dh, onode);
644 static struct recType grs_type =
652 RecType recTypeGrs = &grs_type;