2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.25 1999-02-18 15:01:26 adam
10 * Revision 1.24 1999/02/02 14:51:28 adam
11 * Updated WIN32 code specific sections. Changed header.
13 * Revision 1.23 1998/10/18 07:51:10 adam
14 * Changed one logf call.
16 * Revision 1.22 1998/10/16 08:14:37 adam
17 * Updated record control system.
19 * Revision 1.21 1998/07/01 09:16:10 adam
20 * Element localno only added when it's greater than 0.
22 * Revision 1.20 1998/05/20 10:12:26 adam
23 * Implemented automatic EXPLAIN database maintenance.
24 * Modified Zebra to work with ASN.1 compiled version of YAZ.
26 * Revision 1.19 1998/03/11 11:19:05 adam
27 * Changed the way sequence numbers are generated.
29 * Revision 1.18 1998/03/05 08:41:31 adam
32 * Revision 1.17 1998/02/10 12:03:06 adam
35 * Revision 1.16 1998/01/29 13:38:17 adam
36 * Fixed problem with mapping to record with unknown schema.
38 * Revision 1.15 1998/01/26 10:37:57 adam
41 * Revision 1.14 1997/11/06 11:41:01 adam
42 * Implemented "begin variant" for the sgml.regx filter.
44 * Revision 1.13 1997/10/31 12:35:44 adam
45 * Added a few log statements.
47 * Revision 1.12 1997/10/29 12:02:22 adam
48 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
50 * Revision 1.11 1997/10/27 14:34:00 adam
51 * Work on generic character mapping depending on "structure" field
52 * in abstract syntax file.
54 * Revision 1.10 1997/09/18 08:59:21 adam
55 * Extra generic handle for the character mapping routines.
57 * Revision 1.9 1997/09/17 12:19:21 adam
58 * Zebra version corresponds to YAZ version 1.4.
59 * Changed Zebra server so that it doesn't depend on global common_resource.
61 * Revision 1.8 1997/09/09 13:38:14 adam
62 * Partial port to WIN95/NT.
64 * Revision 1.7 1997/09/05 15:30:10 adam
65 * Changed prototype for chr_map_input - added const.
66 * Added support for C++, headers uses extern "C" for public definitions.
68 * Revision 1.6 1997/09/04 13:54:40 adam
69 * Added MARC filter - type grs.marc.<syntax> where syntax refers
70 * to abstract syntax. New method tellf in retrieve/extract method.
72 * Revision 1.5 1997/07/15 16:29:03 adam
73 * Initialized dummy variable to keep checker gcc happy.
75 * Revision 1.4 1997/04/30 08:56:08 quinn
78 * Revision 1.2 1996/10/11 16:06:43 quinn
79 * Revision 1.3 1997/02/24 10:41:50 adam
80 * Cleanup of code and commented out the "end element-end-record" code.
82 * Revision 1.2 1996/10/11 16:06:43 quinn
83 * Fixed arguments to nodetogr
85 * Revision 1.1 1996/10/11 10:57:25 adam
86 * New module recctrl. Used to manage records (extract/retrieval).
88 * Revision 1.29 1996/10/08 10:30:21 quinn
91 * Revision 1.28 1996/10/07 16:06:40 quinn
94 * Revision 1.27 1996/06/11 10:54:12 quinn
97 * Revision 1.26 1996/06/06 12:08:45 quinn
98 * Added showRecord function
100 * Revision 1.25 1996/06/04 14:18:53 quinn
103 * Revision 1.24 1996/06/04 13:27:54 quinn
104 * More work on charmapping
106 * Revision 1.23 1996/06/04 10:19:01 adam
107 * Minor changes - removed include of ctype.h.
109 * Revision 1.22 1996/06/03 10:15:27 quinn
110 * Various character-mapping.
112 * Revision 1.21 1996/05/31 13:27:24 quinn
113 * Character-conversion in phrases, too.
115 * Revision 1.19 1996/05/16 15:31:14 quinn
118 * Revision 1.18 1996/05/09 07:28:56 quinn
119 * Work towards phrases and multiple registers
121 * Revision 1.17 1996/05/01 13:46:37 adam
122 * First work on multiple records in one file.
123 * New option, -offset, to the "unread" command in the filter module.
125 * Revision 1.16 1996/01/17 14:57:54 adam
126 * Prototype changed for reader functions in extract/retrieve. File
127 * is identified by 'void *' instead of 'int.
129 * Revision 1.15 1996/01/08 19:15:47 adam
130 * New input filter that works!
132 * Revision 1.14 1995/12/15 12:36:11 adam
133 * Retrieval calls data1_read_regx when subType is specified.
135 * Revision 1.13 1995/12/15 12:24:43 quinn
136 * *** empty log message ***
138 * Revision 1.12 1995/12/15 12:20:28 quinn
139 * *** empty log message ***
141 * Revision 1.11 1995/12/15 12:07:57 quinn
142 * Changed extraction strategy.
144 * Revision 1.10 1995/12/14 11:10:48 quinn
147 * Revision 1.9 1995/12/13 17:14:05 quinn
148 * *** empty log message ***
150 * Revision 1.8 1995/12/13 15:33:18 quinn
151 * *** empty log message ***
153 * Revision 1.7 1995/12/13 13:45:39 quinn
154 * Changed data1 to use nmem.
156 * Revision 1.6 1995/12/04 14:22:30 adam
157 * Extra arg to recType_byName.
158 * Started work on new regular expression parsed input to
159 * structured records.
161 * Revision 1.5 1995/11/28 14:18:37 quinn
164 * Revision 1.4 1995/11/21 13:14:49 quinn
165 * Fixed end-of-data-field problem (maybe).
167 * Revision 1.3 1995/11/15 19:13:09 adam
168 * Work on record management.
174 #include <sys/types.h>
185 #define GRS_MAX_WORD 512
187 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
191 data1_node *(*func)(struct grs_read_info *p);
193 { "sgml", grs_read_sgml },
194 { "regx", grs_read_regx },
195 { "marc", grs_read_marc },
198 const char *cp = strchr (type, '.');
201 if (cp == NULL || cp == type)
203 cp = strlen(type) + type;
207 strcpy (p->type, cp+1);
208 for (i=0; tab[i].type; i++)
210 if (!memcmp (type, tab[i].type, cp-type))
211 return (tab[i].func)(p);
216 static void grs_init(RecType recType)
220 static void grs_destroy(RecType recType)
224 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
227 (*p->init)(p, &wrd); /* set defaults */
228 for (; n; n = n->next)
230 if (p->flagShowRecords) /* display element description to user */
232 if (n->which == DATA1N_root)
234 printf("%*s", level * 4, "");
235 printf("Record type: '%s'\n", n->u.root.absyn->name);
237 else if (n->which == DATA1N_tag)
241 printf("%*s", level * 4, "");
242 if (!(e = n->u.tag.element))
243 printf("Local tag: '%s'\n", n->u.tag.tag);
246 printf("Elm: '%s' ", e->name);
249 data1_tag *t = e->tag;
251 printf("TagNam: '%s' ", t->names->name);
254 printf("%s[%d],", t->tagset->name, t->tagset->type);
257 if (t->which == DATA1T_numeric)
258 printf("%d)", t->value.numeric);
260 printf("'%s')", t->value.string);
268 if (dumpkeys(n->child, p, level + 1) < 0)
271 if (n->which == DATA1N_data)
273 data1_node *par = get_parent_tag(p->dh, n);
274 data1_termlist *tlist = 0;
275 data1_datatype dtype = DATA1K_string;
277 if (p->flagShowRecords)
279 printf("%*s", level * 4, "");
281 if (n->u.data.len > 32)
282 printf("'%.24s ... %.6s'\n", n->u.data.data,
283 n->u.data.data + n->u.data.len-6);
284 else if (n->u.data.len > 0)
285 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
293 * cycle up towards the root until we find a tag with an att..
294 * this has the effect of indexing locally defined tags with
295 * the attribute of their ancestor in the record.
298 while (!par->u.tag.element)
299 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
301 if (!par || !(tlist = par->u.tag.element->termlists))
303 if (par->u.tag.element->tag)
304 dtype = par->u.tag.element->tag->kind;
305 for (; tlist; tlist = tlist->next)
307 if (p->flagShowRecords)
309 printf("%*sIdx: [%s]", (level + 1) * 4, "",
311 printf("%s:%s [%d]\n",
312 tlist->att->parent->name,
313 tlist->att->name, tlist->att->value);
317 wrd.reg_type = *tlist->structure;
318 wrd.string = n->u.data.data;
319 wrd.length = n->u.data.len;
320 wrd.attrSet = (int) (tlist->att->parent->reference);
321 wrd.attrUse = tlist->att->locals->local;
326 if (p->flagShowRecords && n->which == DATA1N_root)
328 printf("%*s-------------\n\n", level * 4, "");
334 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
337 int oidtmp[OID_SIZE];
339 oe.proto = PROTO_Z3950;
340 oe.oclass = CLASS_SCHEMA;
341 oe.value = n->u.root.absyn->reference;
343 if ((oid_ent_to_oid (&oe, oidtmp)))
344 (*p->addSchema)(p, oidtmp);
346 return dumpkeys(n, p, 0);
349 static int grs_extract(struct recExtractCtrl *p)
353 struct grs_read_info gri;
355 int oidtmp[OID_SIZE];
357 mem = nmem_create ();
358 gri.readf = p->readf;
359 gri.seekf = p->seekf;
360 gri.tellf = p->tellf;
363 gri.offset = p->offset;
367 n = read_grs_type (&gri, p->subType);
371 oe.proto = PROTO_Z3950;
372 oe.oclass = CLASS_SCHEMA;
373 oe.value = n->u.root.absyn->reference;
374 if ((oid_ent_to_oid (&oe, oidtmp)))
375 (*p->addSchema)(p, oidtmp);
377 if (dumpkeys(n, p, 0) < 0)
379 data1_free_tree(p->dh, n);
382 data1_free_tree(p->dh, n);
388 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
390 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
392 data1_esetname *eset;
398 case Z_RecordComp_simple:
399 if (c->u.simple->which != Z_ElementSetNames_generic)
400 return 26; /* only generic form supported. Fix this later */
401 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
402 c->u.simple->u.generic)))
404 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
405 return 25; /* invalid esetname */
407 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
408 c->u.simple->u.generic);
411 case Z_RecordComp_complex:
412 if (c->u.complex->generic)
414 /* insert check for schema */
415 if ((p = c->u.complex->generic->elementSpec))
418 case Z_ElementSpec_elementSetName:
420 data1_getesetbyname(dh,
422 p->u.elementSetName)))
424 logf(LOG_LOG, "Unknown esetname '%s'",
425 p->u.elementSetName);
426 return 25; /* invalid esetname */
428 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
429 p->u.elementSetName);
432 case Z_ElementSpec_externalSpec:
433 if (p->u.externalSpec->which == Z_External_espec1)
435 logf(LOG_DEBUG, "Got Espec-1");
436 espec = p->u.externalSpec-> u.espec1;
440 logf(LOG_LOG, "Unknown external espec.");
441 return 25; /* bad. what is proper diagnostic? */
451 logf (LOG_DEBUG, "Element: Espec-1 match");
452 return data1_doespec1(dh, n, espec);
456 logf (LOG_DEBUG, "Element: all match");
461 static int grs_retrieve(struct recRetrieveCtrl *p)
463 data1_node *node = 0, *onode = 0;
466 int res, selected = 0;
468 struct grs_read_info gri;
471 gri.readf = p->readf;
472 gri.seekf = p->seekf;
473 gri.tellf = p->tellf;
480 logf (LOG_DEBUG, "grs_retrieve");
481 node = read_grs_type (&gri, p->subType);
488 logf (LOG_DEBUG, "grs_retrieve: size");
489 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
492 dnew->u.data.what = DATA1I_text;
493 dnew->u.data.data = dnew->lbuf;
494 sprintf(dnew->u.data.data, "%d", p->recordSize);
495 dnew->u.data.len = strlen(dnew->u.data.data);
498 logf (LOG_DEBUG, "grs_retrieve: score");
499 if (p->score >= 0 && (dnew =
500 data1_insert_taggeddata(p->dh, node,
504 dnew->u.data.what = DATA1I_num;
505 dnew->u.data.data = dnew->lbuf;
506 sprintf(dnew->u.data.data, "%d", p->score);
507 dnew->u.data.len = strlen(dnew->u.data.data);
510 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
511 if (p->localno > 0 && (dnew = data1_insert_taggeddata(p->dh, node, node,
512 "localControlNumber", mem)))
514 dnew->u.data.what = DATA1I_text;
515 dnew->u.data.data = dnew->lbuf;
516 sprintf(dnew->u.data.data, "%d", p->localno);
517 dnew->u.data.len = strlen(dnew->u.data.data);
520 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
521 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
522 node->u.root.absyn->reference != VAL_NONE)
526 int oidtmp[OID_SIZE];
528 oe.proto = PROTO_Z3950;
529 oe.oclass = CLASS_SCHEMA;
530 oe.value = node->u.root.absyn->reference;
532 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
535 data1_handle dh = p->dh;
539 for (ii = oid; *ii >= 0; ii++)
543 sprintf(p, "%d", *ii);
548 if ((dnew = data1_insert_taggeddata(dh, node, node,
549 "schemaIdentifier", mem)))
551 dnew->u.data.what = DATA1I_oid;
552 dnew->u.data.data = nmem_malloc(mem, p - tmp);
553 memcpy(dnew->u.data.data, tmp, p - tmp);
554 dnew->u.data.len = p - tmp;
559 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
561 * Does the requested format match a known schema-mapping? (this reflects
562 * the overlap of schema and formatting which is inherent in the MARC
564 * NOTE: This should look at the schema-specification in the compspec
567 for (map = node->u.root.absyn->maptabs; map; map = map->next)
568 if (map->target_absyn_ref == p->input_format)
571 if (!(node = data1_map_record(p->dh, onode, map, mem)))
580 logf (LOG_DEBUG, "grs_retrieve: element spec");
581 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
585 data1_free_tree(p->dh, onode);
586 data1_free_tree(p->dh, node);
590 else if (p->comp && !res)
593 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
594 switch (p->output_format = (p->input_format != VAL_NONE ?
595 p->input_format : VAL_SUTRS))
597 data1_marctab *marctab;
602 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
604 p->diagnostic = 238; /* not available in requested syntax */
609 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
616 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
623 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
628 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
629 memcpy (new_buf, p->rec_buf, p->rec_len);
630 p->rec_buf = new_buf;
634 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
639 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
640 memcpy (new_buf, p->rec_buf, p->rec_len);
641 p->rec_buf = new_buf;
645 if (!node->u.root.absyn)
650 for (marctab = node->u.root.absyn->marc; marctab;
651 marctab = marctab->next)
652 if (marctab->reference == p->input_format)
659 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
665 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
666 memcpy (new_buf, p->rec_buf, p->rec_len);
667 p->rec_buf = new_buf;
671 data1_free_tree(p->dh, node);
673 data1_free_tree(p->dh, onode);
678 static struct recType grs_type =
687 RecType recTypeGrs = &grs_type;