2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.19 1998-03-11 11:19:05 adam
8 * Changed the way sequence numbers are generated.
10 * Revision 1.18 1998/03/05 08:41:31 adam
13 * Revision 1.17 1998/02/10 12:03:06 adam
16 * Revision 1.16 1998/01/29 13:38:17 adam
17 * Fixed problem with mapping to record with unknown schema.
19 * Revision 1.15 1998/01/26 10:37:57 adam
22 * Revision 1.14 1997/11/06 11:41:01 adam
23 * Implemented "begin variant" for the sgml.regx filter.
25 * Revision 1.13 1997/10/31 12:35:44 adam
26 * Added a few log statements.
28 * Revision 1.12 1997/10/29 12:02:22 adam
29 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
31 * Revision 1.11 1997/10/27 14:34:00 adam
32 * Work on generic character mapping depending on "structure" field
33 * in abstract syntax file.
35 * Revision 1.10 1997/09/18 08:59:21 adam
36 * Extra generic handle for the character mapping routines.
38 * Revision 1.9 1997/09/17 12:19:21 adam
39 * Zebra version corresponds to YAZ version 1.4.
40 * Changed Zebra server so that it doesn't depend on global common_resource.
42 * Revision 1.8 1997/09/09 13:38:14 adam
43 * Partial port to WIN95/NT.
45 * Revision 1.7 1997/09/05 15:30:10 adam
46 * Changed prototype for chr_map_input - added const.
47 * Added support for C++, headers uses extern "C" for public definitions.
49 * Revision 1.6 1997/09/04 13:54:40 adam
50 * Added MARC filter - type grs.marc.<syntax> where syntax refers
51 * to abstract syntax. New method tellf in retrieve/extract method.
53 * Revision 1.5 1997/07/15 16:29:03 adam
54 * Initialized dummy variable to keep checker gcc happy.
56 * Revision 1.4 1997/04/30 08:56:08 quinn
59 * Revision 1.2 1996/10/11 16:06:43 quinn
60 * Revision 1.3 1997/02/24 10:41:50 adam
61 * Cleanup of code and commented out the "end element-end-record" code.
63 * Revision 1.2 1996/10/11 16:06:43 quinn
64 * Fixed arguments to nodetogr
66 * Revision 1.1 1996/10/11 10:57:25 adam
67 * New module recctrl. Used to manage records (extract/retrieval).
69 * Revision 1.29 1996/10/08 10:30:21 quinn
72 * Revision 1.28 1996/10/07 16:06:40 quinn
75 * Revision 1.27 1996/06/11 10:54:12 quinn
78 * Revision 1.26 1996/06/06 12:08:45 quinn
79 * Added showRecord function
81 * Revision 1.25 1996/06/04 14:18:53 quinn
84 * Revision 1.24 1996/06/04 13:27:54 quinn
85 * More work on charmapping
87 * Revision 1.23 1996/06/04 10:19:01 adam
88 * Minor changes - removed include of ctype.h.
90 * Revision 1.22 1996/06/03 10:15:27 quinn
91 * Various character-mapping.
93 * Revision 1.21 1996/05/31 13:27:24 quinn
94 * Character-conversion in phrases, too.
96 * Revision 1.19 1996/05/16 15:31:14 quinn
99 * Revision 1.18 1996/05/09 07:28:56 quinn
100 * Work towards phrases and multiple registers
102 * Revision 1.17 1996/05/01 13:46:37 adam
103 * First work on multiple records in one file.
104 * New option, -offset, to the "unread" command in the filter module.
106 * Revision 1.16 1996/01/17 14:57:54 adam
107 * Prototype changed for reader functions in extract/retrieve. File
108 * is identified by 'void *' instead of 'int.
110 * Revision 1.15 1996/01/08 19:15:47 adam
111 * New input filter that works!
113 * Revision 1.14 1995/12/15 12:36:11 adam
114 * Retrieval calls data1_read_regx when subType is specified.
116 * Revision 1.13 1995/12/15 12:24:43 quinn
117 * *** empty log message ***
119 * Revision 1.12 1995/12/15 12:20:28 quinn
120 * *** empty log message ***
122 * Revision 1.11 1995/12/15 12:07:57 quinn
123 * Changed extraction strategy.
125 * Revision 1.10 1995/12/14 11:10:48 quinn
128 * Revision 1.9 1995/12/13 17:14:05 quinn
129 * *** empty log message ***
131 * Revision 1.8 1995/12/13 15:33:18 quinn
132 * *** empty log message ***
134 * Revision 1.7 1995/12/13 13:45:39 quinn
135 * Changed data1 to use nmem.
137 * Revision 1.6 1995/12/04 14:22:30 adam
138 * Extra arg to recType_byName.
139 * Started work on new regular expression parsed input to
140 * structured records.
142 * Revision 1.5 1995/11/28 14:18:37 quinn
145 * Revision 1.4 1995/11/21 13:14:49 quinn
146 * Fixed end-of-data-field problem (maybe).
148 * Revision 1.3 1995/11/15 19:13:09 adam
149 * Work on record management.
155 #include <sys/types.h>
166 #define GRS_MAX_WORD 512
168 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
172 data1_node *(*func)(struct grs_read_info *p);
174 { "sgml", grs_read_sgml },
175 { "regx", grs_read_regx },
176 { "marc", grs_read_marc },
179 const char *cp = strchr (type, '.');
182 if (cp == NULL || cp == type)
184 cp = strlen(type) + type;
188 strcpy (p->type, cp+1);
189 for (i=0; tab[i].type; i++)
191 if (!memcmp (type, tab[i].type, cp-type))
192 return (tab[i].func)(p);
197 static void grs_init(void)
201 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
204 (*p->init)(p, &wrd); /* set defaults */
205 for (; n; n = n->next)
207 if (p->flagShowRecords) /* display element description to user */
209 if (n->which == DATA1N_root)
211 printf("%*s", level * 4, "");
212 printf("Record type: '%s'\n", n->u.root.absyn->name);
214 else if (n->which == DATA1N_tag)
218 printf("%*s", level * 4, "");
219 if (!(e = n->u.tag.element))
220 printf("Local tag: '%s'\n", n->u.tag.tag);
223 printf("Elm: '%s' ", e->name);
226 data1_tag *t = e->tag;
228 printf("TagNam: '%s' ", t->names->name);
231 printf("%s[%d],", t->tagset->name, t->tagset->type);
234 if (t->which == DATA1T_numeric)
235 printf("%d)", t->value.numeric);
237 printf("'%s')", t->value.string);
245 if (dumpkeys(n->child, p, level + 1) < 0)
248 if (n->which == DATA1N_data)
250 data1_node *par = get_parent_tag(p->dh, n);
251 data1_termlist *tlist = 0;
252 data1_datatype dtype = DATA1K_string;
254 if (p->flagShowRecords)
256 printf("%*s", level * 4, "");
258 if (n->u.data.len > 32)
259 printf("'%.24s ... %.6s'\n", n->u.data.data,
260 n->u.data.data + n->u.data.len-6);
261 else if (n->u.data.len > 0)
262 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
270 * cycle up towards the root until we find a tag with an att..
271 * this has the effect of indexing locally defined tags with
272 * the attribute of their ancestor in the record.
275 while (!par->u.tag.element)
276 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
278 if (!par || !(tlist = par->u.tag.element->termlists))
280 if (par->u.tag.element->tag)
281 dtype = par->u.tag.element->tag->kind;
282 for (; tlist; tlist = tlist->next)
284 if (p->flagShowRecords)
286 printf("%*sIdx: [%s]", (level + 1) * 4, "",
288 printf("%s:%s [%d]\n",
289 tlist->att->parent->name,
290 tlist->att->name, tlist->att->value);
294 wrd.reg_type = *tlist->structure;
295 wrd.string = n->u.data.data;
296 wrd.length = n->u.data.len;
297 wrd.attrSet = tlist->att->parent->ordinal;
298 wrd.attrUse = tlist->att->locals->local;
303 if (p->flagShowRecords && n->which == DATA1N_root)
305 printf("%*s-------------\n\n", level * 4, "");
311 static int grs_extract(struct recExtractCtrl *p)
315 struct grs_read_info gri;
317 mem = nmem_create ();
318 gri.readf = p->readf;
319 gri.seekf = p->seekf;
320 gri.tellf = p->tellf;
323 gri.offset = p->offset;
327 n = read_grs_type (&gri, p->subType);
330 if (dumpkeys(n, p, 0) < 0)
332 data1_free_tree(p->dh, n);
335 data1_free_tree(p->dh, n);
341 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
343 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
345 data1_esetname *eset;
351 case Z_RecordComp_simple:
352 if (c->u.simple->which != Z_ElementSetNames_generic)
353 return 26; /* only generic form supported. Fix this later */
354 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
355 c->u.simple->u.generic)))
357 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
358 return 25; /* invalid esetname */
360 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
361 c->u.simple->u.generic);
364 case Z_RecordComp_complex:
365 if (c->u.complex->generic)
367 /* insert check for schema */
368 if ((p = c->u.complex->generic->elementSpec))
371 case Z_ElementSpec_elementSetName:
373 data1_getesetbyname(dh,
375 p->u.elementSetName)))
377 logf(LOG_LOG, "Unknown esetname '%s'",
378 p->u.elementSetName);
379 return 25; /* invalid esetname */
381 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
382 p->u.elementSetName);
385 case Z_ElementSpec_externalSpec:
386 if (p->u.externalSpec->which == Z_External_espec1)
388 logf(LOG_DEBUG, "Got Espec-1");
389 espec = p->u.externalSpec-> u.espec1;
393 logf(LOG_LOG, "Unknown external espec.");
394 return 25; /* bad. what is proper diagnostic? */
404 logf (LOG_LOG, "Element: Espec-1 match");
405 return data1_doespec1(dh, n, espec);
409 logf (LOG_DEBUG, "Element: all match");
414 static int grs_retrieve(struct recRetrieveCtrl *p)
416 data1_node *node = 0, *onode = 0;
419 int res, selected = 0;
421 struct grs_read_info gri;
424 gri.readf = p->readf;
425 gri.seekf = p->seekf;
426 gri.tellf = p->tellf;
433 logf (LOG_DEBUG, "grs_retrieve");
434 node = read_grs_type (&gri, p->subType);
441 logf (LOG_DEBUG, "grs_retrieve: size");
442 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
445 dnew->u.data.what = DATA1I_text;
446 dnew->u.data.data = dnew->lbuf;
447 sprintf(dnew->u.data.data, "%d", p->recordSize);
448 dnew->u.data.len = strlen(dnew->u.data.data);
451 logf (LOG_DEBUG, "grs_retrieve: score");
452 if (p->score >= 0 && (dnew =
453 data1_insert_taggeddata(p->dh, node,
457 dnew->u.data.what = DATA1I_num;
458 dnew->u.data.data = dnew->lbuf;
459 sprintf(dnew->u.data.data, "%d", p->score);
460 dnew->u.data.len = strlen(dnew->u.data.data);
463 logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
464 if ((dnew = data1_insert_taggeddata(p->dh, node, node,
465 "localControlNumber", mem)))
467 dnew->u.data.what = DATA1I_text;
468 dnew->u.data.data = dnew->lbuf;
469 sprintf(dnew->u.data.data, "%d", p->localno);
470 dnew->u.data.len = strlen(dnew->u.data.data);
473 logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
474 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
475 node->u.root.absyn->reference != VAL_NONE)
479 int oidtmp[OID_SIZE];
481 oe.proto = PROTO_Z3950;
482 oe.oclass = CLASS_SCHEMA;
483 oe.value = node->u.root.absyn->reference;
485 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
488 data1_handle dh = p->dh;
492 for (ii = oid; *ii >= 0; ii++)
496 sprintf(p, "%d", *ii);
501 if ((dnew = data1_insert_taggeddata(dh, node, node,
502 "schemaIdentifier", mem)))
504 dnew->u.data.what = DATA1I_oid;
505 dnew->u.data.data = nmem_malloc(mem, p - tmp);
506 memcpy(dnew->u.data.data, tmp, p - tmp);
507 dnew->u.data.len = p - tmp;
512 logf (LOG_DEBUG, "grs_retrieve: schema mapping");
514 * Does the requested format match a known schema-mapping? (this reflects
515 * the overlap of schema and formatting which is inherent in the MARC
517 * NOTE: This should look at the schema-specification in the compspec
520 for (map = node->u.root.absyn->maptabs; map; map = map->next)
521 if (map->target_absyn_ref == p->input_format)
524 if (!(node = data1_map_record(p->dh, onode, map, mem)))
533 logf (LOG_DEBUG, "grs_retrieve: element spec");
534 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
538 data1_free_tree(p->dh, onode);
539 data1_free_tree(p->dh, node);
543 else if (p->comp && !res)
546 logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
547 switch (p->output_format = (p->input_format != VAL_NONE ?
548 p->input_format : VAL_SUTRS))
550 data1_marctab *marctab;
555 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
557 p->diagnostic = 238; /* not available in requested syntax */
562 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
569 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
576 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
581 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
586 if (!node->u.root.absyn)
591 for (marctab = node->u.root.absyn->marc; marctab;
592 marctab = marctab->next)
593 if (marctab->reference == p->input_format)
600 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
609 data1_free_tree(p->dh, node);
611 data1_free_tree(p->dh, onode);
616 static struct recType grs_type =
624 RecType recTypeGrs = &grs_type;