2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.6 1997-09-04 13:54:40 adam
8 * Added MARC filter - type grs.marc.<syntax> where syntax refers
9 * to abstract syntax. New method tellf in retrieve/extract method.
11 * Revision 1.5 1997/07/15 16:29:03 adam
12 * Initialized dummy variable to keep checker gcc happy.
14 * Revision 1.4 1997/04/30 08:56:08 quinn
17 * Revision 1.2 1996/10/11 16:06:43 quinn
18 * Revision 1.3 1997/02/24 10:41:50 adam
19 * Cleanup of code and commented out the "end element-end-record" code.
21 * Revision 1.2 1996/10/11 16:06:43 quinn
22 * Fixed arguments to nodetogr
24 * Revision 1.1 1996/10/11 10:57:25 adam
25 * New module recctrl. Used to manage records (extract/retrieval).
27 * Revision 1.29 1996/10/08 10:30:21 quinn
30 * Revision 1.28 1996/10/07 16:06:40 quinn
33 * Revision 1.27 1996/06/11 10:54:12 quinn
36 * Revision 1.26 1996/06/06 12:08:45 quinn
37 * Added showRecord function
39 * Revision 1.25 1996/06/04 14:18:53 quinn
42 * Revision 1.24 1996/06/04 13:27:54 quinn
43 * More work on charmapping
45 * Revision 1.23 1996/06/04 10:19:01 adam
46 * Minor changes - removed include of ctype.h.
48 * Revision 1.22 1996/06/03 10:15:27 quinn
49 * Various character-mapping.
51 * Revision 1.21 1996/05/31 13:27:24 quinn
52 * Character-conversion in phrases, too.
54 * Revision 1.19 1996/05/16 15:31:14 quinn
57 * Revision 1.18 1996/05/09 07:28:56 quinn
58 * Work towards phrases and multiple registers
60 * Revision 1.17 1996/05/01 13:46:37 adam
61 * First work on multiple records in one file.
62 * New option, -offset, to the "unread" command in the filter module.
64 * Revision 1.16 1996/01/17 14:57:54 adam
65 * Prototype changed for reader functions in extract/retrieve. File
66 * is identified by 'void *' instead of 'int.
68 * Revision 1.15 1996/01/08 19:15:47 adam
69 * New input filter that works!
71 * Revision 1.14 1995/12/15 12:36:11 adam
72 * Retrieval calls data1_read_regx when subType is specified.
74 * Revision 1.13 1995/12/15 12:24:43 quinn
75 * *** empty log message ***
77 * Revision 1.12 1995/12/15 12:20:28 quinn
78 * *** empty log message ***
80 * Revision 1.11 1995/12/15 12:07:57 quinn
81 * Changed extraction strategy.
83 * Revision 1.10 1995/12/14 11:10:48 quinn
86 * Revision 1.9 1995/12/13 17:14:05 quinn
87 * *** empty log message ***
89 * Revision 1.8 1995/12/13 15:33:18 quinn
90 * *** empty log message ***
92 * Revision 1.7 1995/12/13 13:45:39 quinn
93 * Changed data1 to use nmem.
95 * Revision 1.6 1995/12/04 14:22:30 adam
96 * Extra arg to recType_byName.
97 * Started work on new regular expression parsed input to
100 * Revision 1.5 1995/11/28 14:18:37 quinn
103 * Revision 1.4 1995/11/21 13:14:49 quinn
104 * Fixed end-of-data-field problem (maybe).
106 * Revision 1.3 1995/11/15 19:13:09 adam
107 * Work on record management.
113 #include <sys/types.h>
123 #define GRS_MAX_WORD 512
125 static int seqno = 0;
127 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
131 data1_node *(*func)(struct grs_read_info *p);
133 { "sgml", grs_read_sgml },
134 { "regx", grs_read_regx },
135 { "marc", grs_read_marc },
138 const char *cp = strchr (type, '.');
141 if (cp == NULL || cp == type)
143 cp = strlen(type) + type;
147 strcpy (p->type, cp+1);
148 for (i=0; tab[i].type; i++)
150 if (!memcmp (type, tab[i].type, cp-type))
151 return (tab[i].func)(p);
156 static void grs_init(void)
160 static void dumpkeys_word(data1_node *n, struct recExtractCtrl *p,
163 char *b = n->u.data.data;
167 remain = n->u.data.len - (b - n->u.data.data);
169 map = (*p->map_chrs_input)(&b, remain);
174 char buf[GRS_MAX_WORD+1];
178 while (map && *map && **map == *CHR_SPACE)
180 remain = n->u.data.len - (b - n->u.data.data);
182 map = (*p->map_chrs_input)(&b, remain);
189 while (map && *map && **map != *CHR_SPACE)
193 while (i < GRS_MAX_WORD && *cp)
195 remain = n->u.data.len - (b - n->u.data.data);
197 map = (*p->map_chrs_input)(&b, remain);
204 (*p->init)(&wrd); /* set defaults */
205 wrd.which = Word_String;
208 wrd.attrSet = att->parent->ordinal;
209 wrd.attrUse = att->locals->local;
214 static void dumpkeys_phrase(data1_node *n, struct recExtractCtrl *p,
217 char *b = n->u.data.data;
218 char buf[GRS_MAX_WORD+1], **map = 0;
222 remain = n->u.data.len - (b - n->u.data.data);
224 map = (*p->map_chrs_input)(&b, remain);
226 while (remain > 0 && i < GRS_MAX_WORD)
228 while (map && *map && **map == *CHR_SPACE)
230 remain = n->u.data.len - (b - n->u.data.data);
232 map = (*p->map_chrs_input)(&b, remain);
239 if (i && i < GRS_MAX_WORD)
240 buf[i++] = *CHR_SPACE;
241 while (map && *map && **map != *CHR_SPACE)
245 if (i >= GRS_MAX_WORD)
247 while (i < GRS_MAX_WORD && *cp)
249 remain = n->u.data.len - (b - n->u.data.data);
251 map = (*p->map_chrs_input)(&b, remain);
260 wrd.which = Word_Phrase;
263 wrd.attrSet = att->parent->ordinal;
264 wrd.attrUse = att->locals->local;
268 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
270 for (; n; n = n->next)
272 if (p->flagShowRecords) /* display element description to user */
274 if (n->which == DATA1N_root)
276 printf("%*s", level * 4, "");
277 printf("Record type: '%s'\n", n->u.root.absyn->name);
279 else if (n->which == DATA1N_tag)
283 printf("%*s", level * 4, "");
284 if (!(e = n->u.tag.element))
285 printf("Local tag: '%s'\n", n->u.tag.tag);
288 printf("Elm: '%s' ", e->name);
291 data1_tag *t = e->tag;
293 printf("TagNam: '%s' ", t->names->name);
296 printf("%s[%d],", t->tagset->name, t->tagset->type);
299 if (t->which == DATA1T_numeric)
300 printf("%d)", t->value.numeric);
302 printf("'%s')", t->value.string);
310 if (dumpkeys(n->child, p, level + 1) < 0)
313 if (n->which == DATA1N_data)
315 data1_node *par = get_parent_tag(n);
316 data1_termlist *tlist = 0;
318 if (p->flagShowRecords)
320 printf("%*s", level * 4, "");
322 if (n->u.data.len > 20)
323 printf("'%.20s...'\n", n->u.data.data);
324 else if (n->u.data.len > 0)
325 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
333 * cycle up towards the root until we find a tag with an att..
334 * this has the effect of indexing locally defined tags with
335 * the attribute of their ancestor in the record.
338 while (!par->u.tag.element)
339 if (!par->parent || !(par = get_parent_tag(par->parent)))
343 else if (par->u.tag.element->termlists)
344 tlist = par->u.tag.element->termlists;
348 for (; tlist; tlist = tlist->next)
350 if (p->flagShowRecords)
352 printf("%*sIdx: [", (level + 1) * 4, "");
353 switch (tlist->structure)
355 case DATA1S_word: printf("w"); break;
356 case DATA1S_phrase: printf("p"); break;
357 default: printf("?"); break;
360 printf("%s:%s [%d]\n", tlist->att->parent->name,
361 tlist->att->name, tlist->att->value);
363 else switch (tlist->structure)
366 dumpkeys_word(n, p, tlist->att); break;
368 dumpkeys_phrase(n, p, tlist->att); break;
370 logf(LOG_FATAL, "Bad structure type in dumpkeys");
375 if (p->flagShowRecords && n->which == DATA1N_root)
377 printf("%*s-------------\n\n", level * 4, "");
383 static int grs_extract(struct recExtractCtrl *p)
386 NMEM mem = nmem_create();
387 struct grs_read_info gri;
390 gri.readf = p->readf;
391 gri.seekf = p->seekf;
392 gri.tellf = p->tellf;
395 gri.offset = p->offset;
398 n = read_grs_type (&gri, p->subType);
401 if (dumpkeys(n, p, 0) < 0)
412 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
414 static int process_comp(data1_node *n, Z_RecordComposition *c)
416 data1_esetname *eset;
422 case Z_RecordComp_simple:
423 if (c->u.simple->which != Z_ElementSetNames_generic)
424 return 26; /* only generic form supported. Fix this later */
425 if (!(eset = data1_getesetbyname(n->u.root.absyn,
426 c->u.simple->u.generic)))
428 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
429 return 25; /* invalid esetname */
431 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
432 c->u.simple->u.generic);
435 case Z_RecordComp_complex:
436 if (c->u.complex->generic)
438 /* insert check for schema */
439 if ((p = c->u.complex->generic->elementSpec))
442 case Z_ElementSpec_elementSetName:
443 if (!(eset = data1_getesetbyname(n->u.root.absyn,
444 p->u.elementSetName)))
446 logf(LOG_LOG, "Unknown esetname '%s'",
447 p->u.elementSetName);
448 return 25; /* invalid esetname */
450 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
451 p->u.elementSetName);
454 case Z_ElementSpec_externalSpec:
455 if (p->u.externalSpec->which == Z_External_espec1)
457 logf(LOG_DEBUG, "Got Espec-1");
458 espec = p->u.externalSpec-> u.espec1;
462 logf(LOG_LOG, "Unknown external espec.");
463 return 25; /* bad. what is proper diagnostic? */
472 return data1_doespec1(n, espec);
477 static int grs_retrieve(struct recRetrieveCtrl *p)
479 data1_node *node = 0, *onode = 0;
482 int res, selected = 0;
483 NMEM mem = nmem_create();
484 struct grs_read_info gri;
486 gri.readf = p->readf;
487 gri.seekf = p->seekf;
488 gri.tellf = p->tellf;
494 node = read_grs_type (&gri, p->subType);
495 /* node = data1_read_record(p->readf, p->fh, mem); */
501 if (p->score >= 0 && (new = data1_insert_taggeddata(node, node, "rank",
504 new->u.data.what = DATA1I_num;
505 new->u.data.data = new->u.data.lbuf;
506 sprintf(new->u.data.data, "%d", p->score);
507 new->u.data.len = strlen(new->u.data.data);
509 if ((new = data1_insert_taggeddata(node, node, "localControlNumber", mem)))
511 new->u.data.what = DATA1I_text;
512 new->u.data.data = new->u.data.lbuf;
513 sprintf(new->u.data.data, "%d", p->localno);
514 new->u.data.len = strlen(new->u.data.data);
516 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
517 node->u.root.absyn->reference != VAL_NONE)
522 oe.proto = PROTO_Z3950;
523 oe.oclass = CLASS_SCHEMA;
524 oe.value = node->u.root.absyn->reference;
526 if ((oid = oid_getoidbyent(&oe)))
532 for (ii = oid; *ii >= 0; ii++)
536 sprintf(p, "%d", *ii);
541 if ((new = data1_insert_taggeddata(node, node, "schemaIdentifier",
544 new->u.data.what = DATA1I_oid;
545 new->u.data.data = nmem_malloc(mem, p - tmp);
546 memcpy(new->u.data.data, tmp, p - tmp);
547 new->u.data.len = p - tmp;
553 * Does the requested format match a known schema-mapping? (this reflects
554 * the overlap of schema and formatting which is inherent in the MARC
556 * NOTE: This should look at the schema-specification in the compspec
559 for (map = node->u.root.absyn->maptabs; map; map = map->next)
560 if (map->target_absyn_ref == p->input_format)
563 if (!(node = data1_map_record(onode, map, mem)))
572 if (p->comp && (res = process_comp(node, p->comp)) > 0)
576 data1_free_tree(onode);
577 data1_free_tree(node);
581 else if (p->comp && !res)
584 switch (p->output_format = (p->input_format != VAL_NONE ?
585 p->input_format : VAL_SUTRS))
587 data1_marctab *marctab;
592 if (!(p->rec_buf = data1_nodetogr(node, selected, p->odr, &dummy)))
593 p->diagnostic = 2; /* this should be better specified */
598 if (!(p->rec_buf = data1_nodetoexplain(node, selected, p->odr)))
599 p->diagnostic = 2; /* this should be better specified */
604 if (!(p->rec_buf = data1_nodetosummary(node, selected, p->odr)))
610 if (!(p->rec_buf = data1_nodetobuf(node, selected,
618 if (!(p->rec_buf = data1_nodetosoif(node, selected,
626 for (marctab = node->u.root.absyn->marc; marctab;
627 marctab = marctab->next)
628 if (marctab->reference == p->input_format)
635 if (!(p->rec_buf = data1_nodetomarc(marctab, node, selected,
643 data1_free_tree(node);
645 data1_free_tree(onode);
650 static struct recType grs_type =
658 RecType recTypeGrs = &grs_type;