2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1997-09-05 15:30:10 adam
8 * Changed prototype for chr_map_input - added const.
9 * Added support for C++, headers uses extern "C" for public definitions.
11 * Revision 1.6 1997/09/04 13:54:40 adam
12 * Added MARC filter - type grs.marc.<syntax> where syntax refers
13 * to abstract syntax. New method tellf in retrieve/extract method.
15 * Revision 1.5 1997/07/15 16:29:03 adam
16 * Initialized dummy variable to keep checker gcc happy.
18 * Revision 1.4 1997/04/30 08:56:08 quinn
21 * Revision 1.2 1996/10/11 16:06:43 quinn
22 * Revision 1.3 1997/02/24 10:41:50 adam
23 * Cleanup of code and commented out the "end element-end-record" code.
25 * Revision 1.2 1996/10/11 16:06:43 quinn
26 * Fixed arguments to nodetogr
28 * Revision 1.1 1996/10/11 10:57:25 adam
29 * New module recctrl. Used to manage records (extract/retrieval).
31 * Revision 1.29 1996/10/08 10:30:21 quinn
34 * Revision 1.28 1996/10/07 16:06:40 quinn
37 * Revision 1.27 1996/06/11 10:54:12 quinn
40 * Revision 1.26 1996/06/06 12:08:45 quinn
41 * Added showRecord function
43 * Revision 1.25 1996/06/04 14:18:53 quinn
46 * Revision 1.24 1996/06/04 13:27:54 quinn
47 * More work on charmapping
49 * Revision 1.23 1996/06/04 10:19:01 adam
50 * Minor changes - removed include of ctype.h.
52 * Revision 1.22 1996/06/03 10:15:27 quinn
53 * Various character-mapping.
55 * Revision 1.21 1996/05/31 13:27:24 quinn
56 * Character-conversion in phrases, too.
58 * Revision 1.19 1996/05/16 15:31:14 quinn
61 * Revision 1.18 1996/05/09 07:28:56 quinn
62 * Work towards phrases and multiple registers
64 * Revision 1.17 1996/05/01 13:46:37 adam
65 * First work on multiple records in one file.
66 * New option, -offset, to the "unread" command in the filter module.
68 * Revision 1.16 1996/01/17 14:57:54 adam
69 * Prototype changed for reader functions in extract/retrieve. File
70 * is identified by 'void *' instead of 'int.
72 * Revision 1.15 1996/01/08 19:15:47 adam
73 * New input filter that works!
75 * Revision 1.14 1995/12/15 12:36:11 adam
76 * Retrieval calls data1_read_regx when subType is specified.
78 * Revision 1.13 1995/12/15 12:24:43 quinn
79 * *** empty log message ***
81 * Revision 1.12 1995/12/15 12:20:28 quinn
82 * *** empty log message ***
84 * Revision 1.11 1995/12/15 12:07:57 quinn
85 * Changed extraction strategy.
87 * Revision 1.10 1995/12/14 11:10:48 quinn
90 * Revision 1.9 1995/12/13 17:14:05 quinn
91 * *** empty log message ***
93 * Revision 1.8 1995/12/13 15:33:18 quinn
94 * *** empty log message ***
96 * Revision 1.7 1995/12/13 13:45:39 quinn
97 * Changed data1 to use nmem.
99 * Revision 1.6 1995/12/04 14:22:30 adam
100 * Extra arg to recType_byName.
101 * Started work on new regular expression parsed input to
102 * structured records.
104 * Revision 1.5 1995/11/28 14:18:37 quinn
107 * Revision 1.4 1995/11/21 13:14:49 quinn
108 * Fixed end-of-data-field problem (maybe).
110 * Revision 1.3 1995/11/15 19:13:09 adam
111 * Work on record management.
117 #include <sys/types.h>
127 #define GRS_MAX_WORD 512
129 static int seqno = 0;
131 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
135 data1_node *(*func)(struct grs_read_info *p);
137 { "sgml", grs_read_sgml },
138 { "regx", grs_read_regx },
139 { "marc", grs_read_marc },
142 const char *cp = strchr (type, '.');
145 if (cp == NULL || cp == type)
147 cp = strlen(type) + type;
151 strcpy (p->type, cp+1);
152 for (i=0; tab[i].type; i++)
154 if (!memcmp (type, tab[i].type, cp-type))
155 return (tab[i].func)(p);
160 static void grs_init(void)
164 static void dumpkeys_word(data1_node *n, struct recExtractCtrl *p,
167 const char *b = n->u.data.data;
169 const char **map = 0;
171 remain = n->u.data.len - (b - n->u.data.data);
173 map = (*p->map_chrs_input)(&b, remain);
178 char buf[GRS_MAX_WORD+1];
182 while (map && *map && **map == *CHR_SPACE)
184 remain = n->u.data.len - (b - n->u.data.data);
186 map = (*p->map_chrs_input)(&b, remain);
193 while (map && *map && **map != *CHR_SPACE)
195 const char *cp = *map;
197 while (i < GRS_MAX_WORD && *cp)
199 remain = n->u.data.len - (b - n->u.data.data);
201 map = (*p->map_chrs_input)(&b, remain);
208 (*p->init)(&wrd); /* set defaults */
209 wrd.which = Word_String;
212 wrd.attrSet = att->parent->ordinal;
213 wrd.attrUse = att->locals->local;
218 static void dumpkeys_phrase(data1_node *n, struct recExtractCtrl *p,
221 const char *b = n->u.data.data;
222 char buf[GRS_MAX_WORD+1];
223 const char **map = 0;
227 remain = n->u.data.len - (b - n->u.data.data);
229 map = (*p->map_chrs_input)(&b, remain);
231 while (remain > 0 && i < GRS_MAX_WORD)
233 while (map && *map && **map == *CHR_SPACE)
235 remain = n->u.data.len - (b - n->u.data.data);
237 map = (*p->map_chrs_input)(&b, remain);
244 if (i && i < GRS_MAX_WORD)
245 buf[i++] = *CHR_SPACE;
246 while (map && *map && **map != *CHR_SPACE)
248 const char *cp = *map;
250 if (i >= GRS_MAX_WORD)
252 while (i < GRS_MAX_WORD && *cp)
254 remain = n->u.data.len - (b - n->u.data.data);
256 map = (*p->map_chrs_input)(&b, remain);
265 wrd.which = Word_Phrase;
268 wrd.attrSet = att->parent->ordinal;
269 wrd.attrUse = att->locals->local;
273 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
275 for (; n; n = n->next)
277 if (p->flagShowRecords) /* display element description to user */
279 if (n->which == DATA1N_root)
281 printf("%*s", level * 4, "");
282 printf("Record type: '%s'\n", n->u.root.absyn->name);
284 else if (n->which == DATA1N_tag)
288 printf("%*s", level * 4, "");
289 if (!(e = n->u.tag.element))
290 printf("Local tag: '%s'\n", n->u.tag.tag);
293 printf("Elm: '%s' ", e->name);
296 data1_tag *t = e->tag;
298 printf("TagNam: '%s' ", t->names->name);
301 printf("%s[%d],", t->tagset->name, t->tagset->type);
304 if (t->which == DATA1T_numeric)
305 printf("%d)", t->value.numeric);
307 printf("'%s')", t->value.string);
315 if (dumpkeys(n->child, p, level + 1) < 0)
318 if (n->which == DATA1N_data)
320 data1_node *par = get_parent_tag(n);
321 data1_termlist *tlist = 0;
323 if (p->flagShowRecords)
325 printf("%*s", level * 4, "");
327 if (n->u.data.len > 20)
328 printf("'%.20s...'\n", n->u.data.data);
329 else if (n->u.data.len > 0)
330 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
338 * cycle up towards the root until we find a tag with an att..
339 * this has the effect of indexing locally defined tags with
340 * the attribute of their ancestor in the record.
343 while (!par->u.tag.element)
344 if (!par->parent || !(par = get_parent_tag(par->parent)))
348 else if (par->u.tag.element->termlists)
349 tlist = par->u.tag.element->termlists;
353 for (; tlist; tlist = tlist->next)
355 if (p->flagShowRecords)
357 printf("%*sIdx: [", (level + 1) * 4, "");
358 switch (tlist->structure)
360 case DATA1S_word: printf("w"); break;
361 case DATA1S_phrase: printf("p"); break;
362 default: printf("?"); break;
365 printf("%s:%s [%d]\n", tlist->att->parent->name,
366 tlist->att->name, tlist->att->value);
368 else switch (tlist->structure)
371 dumpkeys_word(n, p, tlist->att); break;
373 dumpkeys_phrase(n, p, tlist->att); break;
375 logf(LOG_FATAL, "Bad structure type in dumpkeys");
380 if (p->flagShowRecords && n->which == DATA1N_root)
382 printf("%*s-------------\n\n", level * 4, "");
388 static int grs_extract(struct recExtractCtrl *p)
391 NMEM mem = nmem_create();
392 struct grs_read_info gri;
395 gri.readf = p->readf;
396 gri.seekf = p->seekf;
397 gri.tellf = p->tellf;
400 gri.offset = p->offset;
403 n = read_grs_type (&gri, p->subType);
406 if (dumpkeys(n, p, 0) < 0)
417 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
419 static int process_comp(data1_node *n, Z_RecordComposition *c)
421 data1_esetname *eset;
427 case Z_RecordComp_simple:
428 if (c->u.simple->which != Z_ElementSetNames_generic)
429 return 26; /* only generic form supported. Fix this later */
430 if (!(eset = data1_getesetbyname(n->u.root.absyn,
431 c->u.simple->u.generic)))
433 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
434 return 25; /* invalid esetname */
436 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
437 c->u.simple->u.generic);
440 case Z_RecordComp_complex:
441 if (c->u.complex->generic)
443 /* insert check for schema */
444 if ((p = c->u.complex->generic->elementSpec))
447 case Z_ElementSpec_elementSetName:
448 if (!(eset = data1_getesetbyname(n->u.root.absyn,
449 p->u.elementSetName)))
451 logf(LOG_LOG, "Unknown esetname '%s'",
452 p->u.elementSetName);
453 return 25; /* invalid esetname */
455 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
456 p->u.elementSetName);
459 case Z_ElementSpec_externalSpec:
460 if (p->u.externalSpec->which == Z_External_espec1)
462 logf(LOG_DEBUG, "Got Espec-1");
463 espec = p->u.externalSpec-> u.espec1;
467 logf(LOG_LOG, "Unknown external espec.");
468 return 25; /* bad. what is proper diagnostic? */
477 return data1_doespec1(n, espec);
482 static int grs_retrieve(struct recRetrieveCtrl *p)
484 data1_node *node = 0, *onode = 0;
487 int res, selected = 0;
488 NMEM mem = nmem_create();
489 struct grs_read_info gri;
491 gri.readf = p->readf;
492 gri.seekf = p->seekf;
493 gri.tellf = p->tellf;
499 node = read_grs_type (&gri, p->subType);
500 /* node = data1_read_record(p->readf, p->fh, mem); */
506 if (p->score >= 0 && (new = data1_insert_taggeddata(node, node, "rank",
509 new->u.data.what = DATA1I_num;
510 new->u.data.data = new->u.data.lbuf;
511 sprintf(new->u.data.data, "%d", p->score);
512 new->u.data.len = strlen(new->u.data.data);
514 if ((new = data1_insert_taggeddata(node, node, "localControlNumber", mem)))
516 new->u.data.what = DATA1I_text;
517 new->u.data.data = new->u.data.lbuf;
518 sprintf(new->u.data.data, "%d", p->localno);
519 new->u.data.len = strlen(new->u.data.data);
521 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
522 node->u.root.absyn->reference != VAL_NONE)
527 oe.proto = PROTO_Z3950;
528 oe.oclass = CLASS_SCHEMA;
529 oe.value = node->u.root.absyn->reference;
531 if ((oid = oid_getoidbyent(&oe)))
537 for (ii = oid; *ii >= 0; ii++)
541 sprintf(p, "%d", *ii);
546 if ((new = data1_insert_taggeddata(node, node, "schemaIdentifier",
549 new->u.data.what = DATA1I_oid;
550 new->u.data.data = nmem_malloc(mem, p - tmp);
551 memcpy(new->u.data.data, tmp, p - tmp);
552 new->u.data.len = p - tmp;
558 * Does the requested format match a known schema-mapping? (this reflects
559 * the overlap of schema and formatting which is inherent in the MARC
561 * NOTE: This should look at the schema-specification in the compspec
564 for (map = node->u.root.absyn->maptabs; map; map = map->next)
565 if (map->target_absyn_ref == p->input_format)
568 if (!(node = data1_map_record(onode, map, mem)))
577 if (p->comp && (res = process_comp(node, p->comp)) > 0)
581 data1_free_tree(onode);
582 data1_free_tree(node);
586 else if (p->comp && !res)
589 switch (p->output_format = (p->input_format != VAL_NONE ?
590 p->input_format : VAL_SUTRS))
592 data1_marctab *marctab;
597 if (!(p->rec_buf = data1_nodetogr(node, selected, p->odr, &dummy)))
598 p->diagnostic = 2; /* this should be better specified */
603 if (!(p->rec_buf = data1_nodetoexplain(node, selected, p->odr)))
604 p->diagnostic = 2; /* this should be better specified */
609 if (!(p->rec_buf = data1_nodetosummary(node, selected, p->odr)))
615 if (!(p->rec_buf = data1_nodetobuf(node, selected,
623 if (!(p->rec_buf = data1_nodetosoif(node, selected,
631 for (marctab = node->u.root.absyn->marc; marctab;
632 marctab = marctab->next)
633 if (marctab->reference == p->input_format)
640 if (!(p->rec_buf = data1_nodetomarc(marctab, node, selected,
648 data1_free_tree(node);
650 data1_free_tree(onode);
655 static struct recType grs_type =
663 RecType recTypeGrs = &grs_type;