2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.26 1999-03-02 16:15:44 quinn
8 * Added "tagsysno" and "tagrank" directives to zebra.cfg.
10 * Revision 1.25 1999/02/18 15:01:26 adam
13 * Revision 1.24 1999/02/02 14:51:28 adam
14 * Updated WIN32 code specific sections. Changed header.
16 * Revision 1.23 1998/10/18 07:51:10 adam
17 * Changed one logf call.
19 * Revision 1.22 1998/10/16 08:14:37 adam
20 * Updated record control system.
22 * Revision 1.21 1998/07/01 09:16:10 adam
23 * Element localno only added when it's greater than 0.
25 * Revision 1.20 1998/05/20 10:12:26 adam
26 * Implemented automatic EXPLAIN database maintenance.
27 * Modified Zebra to work with ASN.1 compiled version of YAZ.
29 * Revision 1.19 1998/03/11 11:19:05 adam
30 * Changed the way sequence numbers are generated.
32 * Revision 1.18 1998/03/05 08:41:31 adam
35 * Revision 1.17 1998/02/10 12:03:06 adam
38 * Revision 1.16 1998/01/29 13:38:17 adam
39 * Fixed problem with mapping to record with unknown schema.
41 * Revision 1.15 1998/01/26 10:37:57 adam
44 * Revision 1.14 1997/11/06 11:41:01 adam
45 * Implemented "begin variant" for the sgml.regx filter.
47 * Revision 1.13 1997/10/31 12:35:44 adam
48 * Added a few log statements.
50 * Revision 1.12 1997/10/29 12:02:22 adam
51 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
53 * Revision 1.11 1997/10/27 14:34:00 adam
54 * Work on generic character mapping depending on "structure" field
55 * in abstract syntax file.
57 * Revision 1.10 1997/09/18 08:59:21 adam
58 * Extra generic handle for the character mapping routines.
60 * Revision 1.9 1997/09/17 12:19:21 adam
61 * Zebra version corresponds to YAZ version 1.4.
62 * Changed Zebra server so that it doesn't depend on global common_resource.
64 * Revision 1.8 1997/09/09 13:38:14 adam
65 * Partial port to WIN95/NT.
67 * Revision 1.7 1997/09/05 15:30:10 adam
68 * Changed prototype for chr_map_input - added const.
69 * Added support for C++, headers uses extern "C" for public definitions.
71 * Revision 1.6 1997/09/04 13:54:40 adam
72 * Added MARC filter - type grs.marc.<syntax> where syntax refers
73 * to abstract syntax. New method tellf in retrieve/extract method.
75 * Revision 1.5 1997/07/15 16:29:03 adam
76 * Initialized dummy variable to keep checker gcc happy.
78 * Revision 1.4 1997/04/30 08:56:08 quinn
81 * Revision 1.2 1996/10/11 16:06:43 quinn
82 * Revision 1.3 1997/02/24 10:41:50 adam
83 * Cleanup of code and commented out the "end element-end-record" code.
85 * Revision 1.2 1996/10/11 16:06:43 quinn
86 * Fixed arguments to nodetogr
88 * Revision 1.1 1996/10/11 10:57:25 adam
89 * New module recctrl. Used to manage records (extract/retrieval).
91 * Revision 1.29 1996/10/08 10:30:21 quinn
94 * Revision 1.28 1996/10/07 16:06:40 quinn
97 * Revision 1.27 1996/06/11 10:54:12 quinn
100 * Revision 1.26 1996/06/06 12:08:45 quinn
101 * Added showRecord function
103 * Revision 1.25 1996/06/04 14:18:53 quinn
106 * Revision 1.24 1996/06/04 13:27:54 quinn
107 * More work on charmapping
109 * Revision 1.23 1996/06/04 10:19:01 adam
110 * Minor changes - removed include of ctype.h.
112 * Revision 1.22 1996/06/03 10:15:27 quinn
113 * Various character-mapping.
115 * Revision 1.21 1996/05/31 13:27:24 quinn
116 * Character-conversion in phrases, too.
118 * Revision 1.19 1996/05/16 15:31:14 quinn
121 * Revision 1.18 1996/05/09 07:28:56 quinn
122 * Work towards phrases and multiple registers
124 * Revision 1.17 1996/05/01 13:46:37 adam
125 * First work on multiple records in one file.
126 * New option, -offset, to the "unread" command in the filter module.
128 * Revision 1.16 1996/01/17 14:57:54 adam
129 * Prototype changed for reader functions in extract/retrieve. File
130 * is identified by 'void *' instead of 'int.
132 * Revision 1.15 1996/01/08 19:15:47 adam
133 * New input filter that works!
135 * Revision 1.14 1995/12/15 12:36:11 adam
136 * Retrieval calls data1_read_regx when subType is specified.
138 * Revision 1.13 1995/12/15 12:24:43 quinn
139 * *** empty log message ***
141 * Revision 1.12 1995/12/15 12:20:28 quinn
142 * *** empty log message ***
144 * Revision 1.11 1995/12/15 12:07:57 quinn
145 * Changed extraction strategy.
147 * Revision 1.10 1995/12/14 11:10:48 quinn
150 * Revision 1.9 1995/12/13 17:14:05 quinn
151 * *** empty log message ***
153 * Revision 1.8 1995/12/13 15:33:18 quinn
154 * *** empty log message ***
156 * Revision 1.7 1995/12/13 13:45:39 quinn
157 * Changed data1 to use nmem.
159 * Revision 1.6 1995/12/04 14:22:30 adam
160 * Extra arg to recType_byName.
161 * Started work on new regular expression parsed input to
162 * structured records.
164 * Revision 1.5 1995/11/28 14:18:37 quinn
167 * Revision 1.4 1995/11/21 13:14:49 quinn
168 * Fixed end-of-data-field problem (maybe).
170 * Revision 1.3 1995/11/15 19:13:09 adam
171 * Work on record management.
177 #include <sys/types.h>
188 #define GRS_MAX_WORD 512
190 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
194 data1_node *(*func)(struct grs_read_info *p);
196 { "sgml", grs_read_sgml },
197 { "regx", grs_read_regx },
198 { "marc", grs_read_marc },
201 const char *cp = strchr (type, '.');
204 if (cp == NULL || cp == type)
206 cp = strlen(type) + type;
210 strcpy (p->type, cp+1);
211 for (i=0; tab[i].type; i++)
213 if (!memcmp (type, tab[i].type, cp-type))
214 return (tab[i].func)(p);
219 static void grs_init(RecType recType)
223 static void grs_destroy(RecType recType)
227 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
230 (*p->init)(p, &wrd); /* set defaults */
231 for (; n; n = n->next)
233 if (p->flagShowRecords) /* display element description to user */
235 if (n->which == DATA1N_root)
237 printf("%*s", level * 4, "");
238 printf("Record type: '%s'\n", n->u.root.absyn->name);
240 else if (n->which == DATA1N_tag)
244 printf("%*s", level * 4, "");
245 if (!(e = n->u.tag.element))
246 printf("Local tag: '%s'\n", n->u.tag.tag);
249 printf("Elm: '%s' ", e->name);
252 data1_tag *t = e->tag;
254 printf("TagNam: '%s' ", t->names->name);
257 printf("%s[%d],", t->tagset->name, t->tagset->type);
260 if (t->which == DATA1T_numeric)
261 printf("%d)", t->value.numeric);
263 printf("'%s')", t->value.string);
271 if (dumpkeys(n->child, p, level + 1) < 0)
274 if (n->which == DATA1N_data)
276 data1_node *par = get_parent_tag(p->dh, n);
277 data1_termlist *tlist = 0;
278 data1_datatype dtype = DATA1K_string;
280 if (p->flagShowRecords)
282 printf("%*s", level * 4, "");
284 if (n->u.data.len > 32)
285 printf("'%.24s ... %.6s'\n", n->u.data.data,
286 n->u.data.data + n->u.data.len-6);
287 else if (n->u.data.len > 0)
288 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
296 * cycle up towards the root until we find a tag with an att..
297 * this has the effect of indexing locally defined tags with
298 * the attribute of their ancestor in the record.
301 while (!par->u.tag.element)
302 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
304 if (!par || !(tlist = par->u.tag.element->termlists))
306 if (par->u.tag.element->tag)
307 dtype = par->u.tag.element->tag->kind;
308 for (; tlist; tlist = tlist->next)
310 if (p->flagShowRecords)
312 printf("%*sIdx: [%s]", (level + 1) * 4, "",
314 printf("%s:%s [%d]\n",
315 tlist->att->parent->name,
316 tlist->att->name, tlist->att->value);
320 wrd.reg_type = *tlist->structure;
321 wrd.string = n->u.data.data;
322 wrd.length = n->u.data.len;
323 wrd.attrSet = (int) (tlist->att->parent->reference);
324 wrd.attrUse = tlist->att->locals->local;
329 if (p->flagShowRecords && n->which == DATA1N_root)
331 printf("%*s-------------\n\n", level * 4, "");
337 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
340 int oidtmp[OID_SIZE];
342 oe.proto = PROTO_Z3950;
343 oe.oclass = CLASS_SCHEMA;
344 oe.value = n->u.root.absyn->reference;
346 if ((oid_ent_to_oid (&oe, oidtmp)))
347 (*p->addSchema)(p, oidtmp);
349 return dumpkeys(n, p, 0);
352 static int grs_extract(struct recExtractCtrl *p)
356 struct grs_read_info gri;
358 int oidtmp[OID_SIZE];
360 mem = nmem_create ();
361 gri.readf = p->readf;
362 gri.seekf = p->seekf;
363 gri.tellf = p->tellf;
366 gri.offset = p->offset;
370 n = read_grs_type (&gri, p->subType);
374 oe.proto = PROTO_Z3950;
375 oe.oclass = CLASS_SCHEMA;
376 oe.value = n->u.root.absyn->reference;
377 if ((oid_ent_to_oid (&oe, oidtmp)))
378 (*p->addSchema)(p, oidtmp);
380 if (dumpkeys(n, p, 0) < 0)
382 data1_free_tree(p->dh, n);
385 data1_free_tree(p->dh, n);
391 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
393 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
395 data1_esetname *eset;
401 case Z_RecordComp_simple:
402 if (c->u.simple->which != Z_ElementSetNames_generic)
403 return 26; /* only generic form supported. Fix this later */
404 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
405 c->u.simple->u.generic)))
407 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
408 return 25; /* invalid esetname */
410 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
411 c->u.simple->u.generic);
414 case Z_RecordComp_complex:
415 if (c->u.complex->generic)
417 /* insert check for schema */
418 if ((p = c->u.complex->generic->elementSpec))
421 case Z_ElementSpec_elementSetName:
423 data1_getesetbyname(dh,
425 p->u.elementSetName)))
427 logf(LOG_LOG, "Unknown esetname '%s'",
428 p->u.elementSetName);
429 return 25; /* invalid esetname */
431 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
432 p->u.elementSetName);
435 case Z_ElementSpec_externalSpec:
436 if (p->u.externalSpec->which == Z_External_espec1)
438 logf(LOG_DEBUG, "Got Espec-1");
439 espec = p->u.externalSpec-> u.espec1;
443 logf(LOG_LOG, "Unknown external espec.");
444 return 25; /* bad. what is proper diagnostic? */
454 logf (LOG_DEBUG, "Element: Espec-1 match");
455 return data1_doespec1(dh, n, espec);
459 logf (LOG_DEBUG, "Element: all match");
464 static int grs_retrieve(struct recRetrieveCtrl *p)
466 data1_node *node = 0, *onode = 0;
469 int res, selected = 0;
471 struct grs_read_info gri;
475 gri.readf = p->readf;
476 gri.seekf = p->seekf;
477 gri.tellf = p->tellf;
484 logf (LOG_DEBUG, "grs_retrieve");
485 node = read_grs_type (&gri, p->subType);
492 logf (LOG_DEBUG, "grs_retrieve: size");
493 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
496 dnew->u.data.what = DATA1I_text;
497 dnew->u.data.data = dnew->lbuf;
498 sprintf(dnew->u.data.data, "%d", p->recordSize);
499 dnew->u.data.len = strlen(dnew->u.data.data);
502 tagname = res_get_def(p->res, "tagrank", "rank");
503 if (strcmp(tagname, "0") && p->score >= 0 && (dnew =
504 data1_insert_taggeddata(p->dh, node,
508 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
509 dnew->u.data.what = DATA1I_num;
510 dnew->u.data.data = dnew->lbuf;
511 sprintf(dnew->u.data.data, "%d", p->score);
512 dnew->u.data.len = strlen(dnew->u.data.data);
515 tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
516 if (strcmp(tagname, "0") && p->localno > 0 &&
517 (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
519 logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
520 dnew->u.data.what = DATA1I_text;
521 dnew->u.data.data = dnew->lbuf;
522 sprintf(dnew->u.data.data, "%d", p->localno);
523 dnew->u.data.len = strlen(dnew->u.data.data);
526 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
527 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
528 node->u.root.absyn->reference != VAL_NONE)
532 int oidtmp[OID_SIZE];
534 oe.proto = PROTO_Z3950;
535 oe.oclass = CLASS_SCHEMA;
536 oe.value = node->u.root.absyn->reference;
538 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
541 data1_handle dh = p->dh;
545 for (ii = oid; *ii >= 0; ii++)
549 sprintf(p, "%d", *ii);
554 if ((dnew = data1_insert_taggeddata(dh, node, node,
555 "schemaIdentifier", mem)))
557 dnew->u.data.what = DATA1I_oid;
558 dnew->u.data.data = nmem_malloc(mem, p - tmp);
559 memcpy(dnew->u.data.data, tmp, p - tmp);
560 dnew->u.data.len = p - tmp;
565 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
567 * Does the requested format match a known schema-mapping? (this reflects
568 * the overlap of schema and formatting which is inherent in the MARC
570 * NOTE: This should look at the schema-specification in the compspec
573 for (map = node->u.root.absyn->maptabs; map; map = map->next)
574 if (map->target_absyn_ref == p->input_format)
577 if (!(node = data1_map_record(p->dh, onode, map, mem)))
586 logf (LOG_DEBUG, "grs_retrieve: element spec");
587 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
591 data1_free_tree(p->dh, onode);
592 data1_free_tree(p->dh, node);
596 else if (p->comp && !res)
599 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
600 switch (p->output_format = (p->input_format != VAL_NONE ?
601 p->input_format : VAL_SUTRS))
603 data1_marctab *marctab;
608 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
610 p->diagnostic = 238; /* not available in requested syntax */
615 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
622 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
629 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
634 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
635 memcpy (new_buf, p->rec_buf, p->rec_len);
636 p->rec_buf = new_buf;
640 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
645 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
646 memcpy (new_buf, p->rec_buf, p->rec_len);
647 p->rec_buf = new_buf;
651 if (!node->u.root.absyn)
656 for (marctab = node->u.root.absyn->marc; marctab;
657 marctab = marctab->next)
658 if (marctab->reference == p->input_format)
665 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
671 char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
672 memcpy (new_buf, p->rec_buf, p->rec_len);
673 p->rec_buf = new_buf;
677 data1_free_tree(p->dh, node);
679 data1_free_tree(p->dh, onode);
684 static struct recType grs_type =
693 RecType recTypeGrs = &grs_type;