2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1997-09-17 12:19:21 adam
8 * Zebra version corresponds to YAZ version 1.4.
9 * Changed Zebra server so that it doesn't depend on global common_resource.
11 * Revision 1.8 1997/09/09 13:38:14 adam
12 * Partial port to WIN95/NT.
14 * Revision 1.7 1997/09/05 15:30:10 adam
15 * Changed prototype for chr_map_input - added const.
16 * Added support for C++, headers uses extern "C" for public definitions.
18 * Revision 1.6 1997/09/04 13:54:40 adam
19 * Added MARC filter - type grs.marc.<syntax> where syntax refers
20 * to abstract syntax. New method tellf in retrieve/extract method.
22 * Revision 1.5 1997/07/15 16:29:03 adam
23 * Initialized dummy variable to keep checker gcc happy.
25 * Revision 1.4 1997/04/30 08:56:08 quinn
28 * Revision 1.2 1996/10/11 16:06:43 quinn
29 * Revision 1.3 1997/02/24 10:41:50 adam
30 * Cleanup of code and commented out the "end element-end-record" code.
32 * Revision 1.2 1996/10/11 16:06:43 quinn
33 * Fixed arguments to nodetogr
35 * Revision 1.1 1996/10/11 10:57:25 adam
36 * New module recctrl. Used to manage records (extract/retrieval).
38 * Revision 1.29 1996/10/08 10:30:21 quinn
41 * Revision 1.28 1996/10/07 16:06:40 quinn
44 * Revision 1.27 1996/06/11 10:54:12 quinn
47 * Revision 1.26 1996/06/06 12:08:45 quinn
48 * Added showRecord function
50 * Revision 1.25 1996/06/04 14:18:53 quinn
53 * Revision 1.24 1996/06/04 13:27:54 quinn
54 * More work on charmapping
56 * Revision 1.23 1996/06/04 10:19:01 adam
57 * Minor changes - removed include of ctype.h.
59 * Revision 1.22 1996/06/03 10:15:27 quinn
60 * Various character-mapping.
62 * Revision 1.21 1996/05/31 13:27:24 quinn
63 * Character-conversion in phrases, too.
65 * Revision 1.19 1996/05/16 15:31:14 quinn
68 * Revision 1.18 1996/05/09 07:28:56 quinn
69 * Work towards phrases and multiple registers
71 * Revision 1.17 1996/05/01 13:46:37 adam
72 * First work on multiple records in one file.
73 * New option, -offset, to the "unread" command in the filter module.
75 * Revision 1.16 1996/01/17 14:57:54 adam
76 * Prototype changed for reader functions in extract/retrieve. File
77 * is identified by 'void *' instead of 'int.
79 * Revision 1.15 1996/01/08 19:15:47 adam
80 * New input filter that works!
82 * Revision 1.14 1995/12/15 12:36:11 adam
83 * Retrieval calls data1_read_regx when subType is specified.
85 * Revision 1.13 1995/12/15 12:24:43 quinn
86 * *** empty log message ***
88 * Revision 1.12 1995/12/15 12:20:28 quinn
89 * *** empty log message ***
91 * Revision 1.11 1995/12/15 12:07:57 quinn
92 * Changed extraction strategy.
94 * Revision 1.10 1995/12/14 11:10:48 quinn
97 * Revision 1.9 1995/12/13 17:14:05 quinn
98 * *** empty log message ***
100 * Revision 1.8 1995/12/13 15:33:18 quinn
101 * *** empty log message ***
103 * Revision 1.7 1995/12/13 13:45:39 quinn
104 * Changed data1 to use nmem.
106 * Revision 1.6 1995/12/04 14:22:30 adam
107 * Extra arg to recType_byName.
108 * Started work on new regular expression parsed input to
109 * structured records.
111 * Revision 1.5 1995/11/28 14:18:37 quinn
114 * Revision 1.4 1995/11/21 13:14:49 quinn
115 * Fixed end-of-data-field problem (maybe).
117 * Revision 1.3 1995/11/15 19:13:09 adam
118 * Work on record management.
124 #include <sys/types.h>
136 #define GRS_MAX_WORD 512
138 static int seqno = 0;
140 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
144 data1_node *(*func)(struct grs_read_info *p);
146 { "sgml", grs_read_sgml },
147 { "regx", grs_read_regx },
148 { "marc", grs_read_marc },
151 const char *cp = strchr (type, '.');
154 if (cp == NULL || cp == type)
156 cp = strlen(type) + type;
160 strcpy (p->type, cp+1);
161 for (i=0; tab[i].type; i++)
163 if (!memcmp (type, tab[i].type, cp-type))
164 return (tab[i].func)(p);
169 static void grs_init(void)
173 static void dumpkeys_word(data1_node *n, struct recExtractCtrl *p,
176 const char *b = n->u.data.data;
178 const char **map = 0;
180 remain = n->u.data.len - (b - n->u.data.data);
182 map = (*p->map_chrs_input)(&b, remain);
187 char buf[GRS_MAX_WORD+1];
191 while (map && *map && **map == *CHR_SPACE)
193 remain = n->u.data.len - (b - n->u.data.data);
195 map = (*p->map_chrs_input)(&b, remain);
202 while (map && *map && **map != *CHR_SPACE)
204 const char *cp = *map;
206 while (i < GRS_MAX_WORD && *cp)
208 remain = n->u.data.len - (b - n->u.data.data);
210 map = (*p->map_chrs_input)(&b, remain);
217 (*p->init)(&wrd); /* set defaults */
218 wrd.which = Word_String;
221 wrd.attrSet = att->parent->ordinal;
222 wrd.attrUse = att->locals->local;
227 static void dumpkeys_phrase(data1_node *n, struct recExtractCtrl *p,
230 const char *b = n->u.data.data;
231 char buf[GRS_MAX_WORD+1];
232 const char **map = 0;
236 remain = n->u.data.len - (b - n->u.data.data);
238 map = (*p->map_chrs_input)(&b, remain);
240 while (remain > 0 && i < GRS_MAX_WORD)
242 while (map && *map && **map == *CHR_SPACE)
244 remain = n->u.data.len - (b - n->u.data.data);
246 map = (*p->map_chrs_input)(&b, remain);
253 if (i && i < GRS_MAX_WORD)
254 buf[i++] = *CHR_SPACE;
255 while (map && *map && **map != *CHR_SPACE)
257 const char *cp = *map;
259 if (i >= GRS_MAX_WORD)
261 while (i < GRS_MAX_WORD && *cp)
263 remain = n->u.data.len - (b - n->u.data.data);
265 map = (*p->map_chrs_input)(&b, remain);
274 wrd.which = Word_Phrase;
277 wrd.attrSet = att->parent->ordinal;
278 wrd.attrUse = att->locals->local;
282 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
284 for (; n; n = n->next)
286 if (p->flagShowRecords) /* display element description to user */
288 if (n->which == DATA1N_root)
290 printf("%*s", level * 4, "");
291 printf("Record type: '%s'\n", n->u.root.absyn->name);
293 else if (n->which == DATA1N_tag)
297 printf("%*s", level * 4, "");
298 if (!(e = n->u.tag.element))
299 printf("Local tag: '%s'\n", n->u.tag.tag);
302 printf("Elm: '%s' ", e->name);
305 data1_tag *t = e->tag;
307 printf("TagNam: '%s' ", t->names->name);
310 printf("%s[%d],", t->tagset->name, t->tagset->type);
313 if (t->which == DATA1T_numeric)
314 printf("%d)", t->value.numeric);
316 printf("'%s')", t->value.string);
324 if (dumpkeys(n->child, p, level + 1) < 0)
327 if (n->which == DATA1N_data)
329 data1_node *par = get_parent_tag(p->dh, n);
330 data1_termlist *tlist = 0;
332 if (p->flagShowRecords)
334 printf("%*s", level * 4, "");
336 if (n->u.data.len > 20)
337 printf("'%.20s...'\n", n->u.data.data);
338 else if (n->u.data.len > 0)
339 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
347 * cycle up towards the root until we find a tag with an att..
348 * this has the effect of indexing locally defined tags with
349 * the attribute of their ancestor in the record.
352 while (!par->u.tag.element)
353 if (!par->parent || !(par = get_parent_tag(p->dh, par->parent)))
357 else if (par->u.tag.element->termlists)
358 tlist = par->u.tag.element->termlists;
362 for (; tlist; tlist = tlist->next)
364 if (p->flagShowRecords)
366 printf("%*sIdx: [", (level + 1) * 4, "");
367 switch (tlist->structure)
369 case DATA1S_word: printf("w"); break;
370 case DATA1S_phrase: printf("p"); break;
371 default: printf("?"); break;
374 printf("%s:%s [%d]\n", tlist->att->parent->name,
375 tlist->att->name, tlist->att->value);
377 else switch (tlist->structure)
380 dumpkeys_word(n, p, tlist->att); break;
382 dumpkeys_phrase(n, p, tlist->att); break;
384 logf(LOG_FATAL, "Bad structure type in dumpkeys");
389 if (p->flagShowRecords && n->which == DATA1N_root)
391 printf("%*s-------------\n\n", level * 4, "");
397 static int grs_extract(struct recExtractCtrl *p)
400 NMEM mem = nmem_create();
401 struct grs_read_info gri;
404 gri.readf = p->readf;
405 gri.seekf = p->seekf;
406 gri.tellf = p->tellf;
409 gri.offset = p->offset;
413 n = read_grs_type (&gri, p->subType);
416 if (dumpkeys(n, p, 0) < 0)
418 data1_free_tree(p->dh, n);
421 data1_free_tree(p->dh, n);
427 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
429 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
431 data1_esetname *eset;
437 case Z_RecordComp_simple:
438 if (c->u.simple->which != Z_ElementSetNames_generic)
439 return 26; /* only generic form supported. Fix this later */
440 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
441 c->u.simple->u.generic)))
443 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
444 return 25; /* invalid esetname */
446 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
447 c->u.simple->u.generic);
450 case Z_RecordComp_complex:
451 if (c->u.complex->generic)
453 /* insert check for schema */
454 if ((p = c->u.complex->generic->elementSpec))
457 case Z_ElementSpec_elementSetName:
459 data1_getesetbyname(dh,
461 p->u.elementSetName)))
463 logf(LOG_LOG, "Unknown esetname '%s'",
464 p->u.elementSetName);
465 return 25; /* invalid esetname */
467 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
468 p->u.elementSetName);
471 case Z_ElementSpec_externalSpec:
472 if (p->u.externalSpec->which == Z_External_espec1)
474 logf(LOG_DEBUG, "Got Espec-1");
475 espec = p->u.externalSpec-> u.espec1;
479 logf(LOG_LOG, "Unknown external espec.");
480 return 25; /* bad. what is proper diagnostic? */
489 return data1_doespec1(dh, n, espec);
494 static int grs_retrieve(struct recRetrieveCtrl *p)
496 data1_node *node = 0, *onode = 0;
499 int res, selected = 0;
500 NMEM mem = nmem_create();
501 struct grs_read_info gri;
503 gri.readf = p->readf;
504 gri.seekf = p->seekf;
505 gri.tellf = p->tellf;
512 node = read_grs_type (&gri, p->subType);
518 if (p->score >= 0 && (new =
519 data1_insert_taggeddata(p->dh, node,
523 new->u.data.what = DATA1I_num;
524 new->u.data.data = new->u.data.lbuf;
525 sprintf(new->u.data.data, "%d", p->score);
526 new->u.data.len = strlen(new->u.data.data);
528 if ((new = data1_insert_taggeddata(p->dh, node, node,
529 "localControlNumber", mem)))
531 new->u.data.what = DATA1I_text;
532 new->u.data.data = new->u.data.lbuf;
533 sprintf(new->u.data.data, "%d", p->localno);
534 new->u.data.len = strlen(new->u.data.data);
536 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
537 node->u.root.absyn->reference != VAL_NONE)
542 oe.proto = PROTO_Z3950;
543 oe.oclass = CLASS_SCHEMA;
544 oe.value = node->u.root.absyn->reference;
546 if ((oid = oid_getoidbyent(&oe)))
549 data1_handle dh = p->dh;
553 for (ii = oid; *ii >= 0; ii++)
557 sprintf(p, "%d", *ii);
562 if ((new = data1_insert_taggeddata(dh, node, node,
563 "schemaIdentifier", mem)))
565 new->u.data.what = DATA1I_oid;
566 new->u.data.data = nmem_malloc(mem, p - tmp);
567 memcpy(new->u.data.data, tmp, p - tmp);
568 new->u.data.len = p - tmp;
574 * Does the requested format match a known schema-mapping? (this reflects
575 * the overlap of schema and formatting which is inherent in the MARC
577 * NOTE: This should look at the schema-specification in the compspec
580 for (map = node->u.root.absyn->maptabs; map; map = map->next)
581 if (map->target_absyn_ref == p->input_format)
584 if (!(node = data1_map_record(p->dh, onode, map, mem)))
593 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
597 data1_free_tree(p->dh, onode);
598 data1_free_tree(p->dh, node);
602 else if (p->comp && !res)
605 switch (p->output_format = (p->input_format != VAL_NONE ?
606 p->input_format : VAL_SUTRS))
608 data1_marctab *marctab;
613 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
615 p->diagnostic = 2; /* this should be better specified */
620 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
622 p->diagnostic = 2; /* this should be better specified */
627 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
634 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
642 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
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,
668 data1_free_tree(p->dh, node);
670 data1_free_tree(p->dh, onode);
675 static struct recType grs_type =
683 RecType recTypeGrs = &grs_type;