2 * Copyright (C) 1994-1996, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.3 1997-02-24 10:41:50 adam
8 * Cleanup of code and commented out the "end element-end-record" code.
10 * Revision 1.2 1996/10/11 16:06:43 quinn
11 * Fixed arguments to nodetogr
13 * Revision 1.1 1996/10/11 10:57:25 adam
14 * New module recctrl. Used to manage records (extract/retrieval).
16 * Revision 1.29 1996/10/08 10:30:21 quinn
19 * Revision 1.28 1996/10/07 16:06:40 quinn
22 * Revision 1.27 1996/06/11 10:54:12 quinn
25 * Revision 1.26 1996/06/06 12:08:45 quinn
26 * Added showRecord function
28 * Revision 1.25 1996/06/04 14:18:53 quinn
31 * Revision 1.24 1996/06/04 13:27:54 quinn
32 * More work on charmapping
34 * Revision 1.23 1996/06/04 10:19:01 adam
35 * Minor changes - removed include of ctype.h.
37 * Revision 1.22 1996/06/03 10:15:27 quinn
38 * Various character-mapping.
40 * Revision 1.21 1996/05/31 13:27:24 quinn
41 * Character-conversion in phrases, too.
43 * Revision 1.19 1996/05/16 15:31:14 quinn
46 * Revision 1.18 1996/05/09 07:28:56 quinn
47 * Work towards phrases and multiple registers
49 * Revision 1.17 1996/05/01 13:46:37 adam
50 * First work on multiple records in one file.
51 * New option, -offset, to the "unread" command in the filter module.
53 * Revision 1.16 1996/01/17 14:57:54 adam
54 * Prototype changed for reader functions in extract/retrieve. File
55 * is identified by 'void *' instead of 'int.
57 * Revision 1.15 1996/01/08 19:15:47 adam
58 * New input filter that works!
60 * Revision 1.14 1995/12/15 12:36:11 adam
61 * Retrieval calls data1_read_regx when subType is specified.
63 * Revision 1.13 1995/12/15 12:24:43 quinn
64 * *** empty log message ***
66 * Revision 1.12 1995/12/15 12:20:28 quinn
67 * *** empty log message ***
69 * Revision 1.11 1995/12/15 12:07:57 quinn
70 * Changed extraction strategy.
72 * Revision 1.10 1995/12/14 11:10:48 quinn
75 * Revision 1.9 1995/12/13 17:14:05 quinn
76 * *** empty log message ***
78 * Revision 1.8 1995/12/13 15:33:18 quinn
79 * *** empty log message ***
81 * Revision 1.7 1995/12/13 13:45:39 quinn
82 * Changed data1 to use nmem.
84 * Revision 1.6 1995/12/04 14:22:30 adam
85 * Extra arg to recType_byName.
86 * Started work on new regular expression parsed input to
89 * Revision 1.5 1995/11/28 14:18:37 quinn
92 * Revision 1.4 1995/11/21 13:14:49 quinn
93 * Fixed end-of-data-field problem (maybe).
95 * Revision 1.3 1995/11/15 19:13:09 adam
96 * Work on record management.
102 #include <sys/types.h>
112 #define GRS_MAX_WORD 512
114 static int seqno = 0;
116 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
120 data1_node *(*func)(struct grs_read_info *p);
122 { "sgml", grs_read_sgml },
123 { "regx", grs_read_regx },
126 const char *cp = strchr (type, '.');
129 if (cp == NULL || cp == type)
131 cp = strlen(type) + type;
135 strcpy (p->type, cp+1);
136 for (i=0; tab[i].type; i++)
138 if (!memcmp (type, tab[i].type, cp-type))
139 return (tab[i].func)(p);
144 static void grs_init(void)
148 static void dumpkeys_word(data1_node *n, struct recExtractCtrl *p,
151 char *b = n->u.data.data;
155 remain = n->u.data.len - (b - n->u.data.data);
157 map = (*p->map_chrs_input)(&b, remain);
162 char buf[GRS_MAX_WORD+1];
166 while (map && *map && **map == *CHR_SPACE)
168 remain = n->u.data.len - (b - n->u.data.data);
170 map = (*p->map_chrs_input)(&b, remain);
177 while (map && *map && **map != *CHR_SPACE)
181 while (i < GRS_MAX_WORD && *cp)
183 remain = n->u.data.len - (b - n->u.data.data);
185 map = (*p->map_chrs_input)(&b, remain);
192 (*p->init)(&wrd); /* set defaults */
193 wrd.which = Word_String;
196 wrd.attrSet = att->parent->ordinal;
197 wrd.attrUse = att->locals->local;
202 static void dumpkeys_phrase(data1_node *n, struct recExtractCtrl *p,
205 char *b = n->u.data.data;
206 char buf[GRS_MAX_WORD+1], **map = 0;
210 remain = n->u.data.len - (b - n->u.data.data);
212 map = (*p->map_chrs_input)(&b, remain);
214 while (remain > 0 && i < GRS_MAX_WORD)
216 while (map && *map && **map == *CHR_SPACE)
218 remain = n->u.data.len - (b - n->u.data.data);
220 map = (*p->map_chrs_input)(&b, remain);
227 if (i && i < GRS_MAX_WORD)
228 buf[i++] = *CHR_SPACE;
229 while (map && *map && **map != *CHR_SPACE)
233 if (i >= GRS_MAX_WORD)
235 while (i < GRS_MAX_WORD && *cp)
237 remain = n->u.data.len - (b - n->u.data.data);
239 map = (*p->map_chrs_input)(&b, remain);
248 wrd.which = Word_Phrase;
251 wrd.attrSet = att->parent->ordinal;
252 wrd.attrUse = att->locals->local;
256 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
258 for (; n; n = n->next)
260 if (p->flagShowRecords) /* display element description to user */
262 if (n->which == DATA1N_root)
264 printf("%*s", level * 4, "");
265 printf("Record type: '%s'\n", n->u.root.absyn->name);
267 else if (n->which == DATA1N_tag)
271 printf("%*s", level * 4, "");
272 if (!(e = n->u.tag.element))
273 printf("Local tag: '%s'\n", n->u.tag.tag);
276 printf("Elm: '%s' ", e->name);
279 data1_tag *t = e->tag;
281 printf("TagNam: '%s' ", t->names->name);
284 printf("%s[%d],", t->tagset->name, t->tagset->type);
287 if (t->which == DATA1T_numeric)
288 printf("%d)", t->value.numeric);
290 printf("'%s')", t->value.string);
298 if (dumpkeys(n->child, p, level + 1) < 0)
301 if (n->which == DATA1N_data)
303 data1_node *par = get_parent_tag(n);
304 data1_termlist *tlist = 0;
306 if (p->flagShowRecords)
308 printf("%*s", level * 4, "");
310 if (n->u.data.len > 20)
311 printf("'%.20s...'\n", n->u.data.data);
312 else if (n->u.data.len > 0)
313 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
321 * cycle up towards the root until we find a tag with an att..
322 * this has the effect of indexing locally defined tags with
323 * the attribute of their ancestor in the record.
326 while (!par->u.tag.element)
327 if (!par->parent || !(par = get_parent_tag(par->parent)))
331 else if (par->u.tag.element->termlists)
332 tlist = par->u.tag.element->termlists;
336 for (; tlist; tlist = tlist->next)
338 if (p->flagShowRecords)
340 printf("%*sIdx: [", (level + 1) * 4, "");
341 switch (tlist->structure)
343 case DATA1S_word: printf("w"); break;
344 case DATA1S_phrase: printf("p"); break;
345 default: printf("?"); break;
348 printf("%s:%s [%d]\n", tlist->att->parent->name,
349 tlist->att->name, tlist->att->value);
351 else switch (tlist->structure)
354 dumpkeys_word(n, p, tlist->att); break;
356 dumpkeys_phrase(n, p, tlist->att); break;
358 logf(LOG_FATAL, "Bad structure type in dumpkeys");
363 if (p->flagShowRecords && n->which == DATA1N_root)
365 printf("%*s-------------\n\n", level * 4, "");
371 static int grs_extract(struct recExtractCtrl *p)
374 NMEM mem = nmem_create();
375 struct grs_read_info gri;
378 gri.readf = p->readf;
379 gri.seekf = p->seekf;
382 gri.offset = p->offset;
385 n = read_grs_type (&gri, p->subType);
388 if (dumpkeys(n, p, 0) < 0)
399 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
401 static int process_comp(data1_node *n, Z_RecordComposition *c)
403 data1_esetname *eset;
409 case Z_RecordComp_simple:
410 if (c->u.simple->which != Z_ElementSetNames_generic)
411 return 26; /* only generic form supported. Fix this later */
412 if (!(eset = data1_getesetbyname(n->u.root.absyn,
413 c->u.simple->u.generic)))
415 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
416 return 25; /* invalid esetname */
418 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
419 c->u.simple->u.generic);
422 case Z_RecordComp_complex:
423 if (c->u.complex->generic)
425 /* insert check for schema */
426 if ((p = c->u.complex->generic->elementSpec))
429 case Z_ElementSpec_elementSetName:
430 if (!(eset = data1_getesetbyname(n->u.root.absyn,
431 p->u.elementSetName)))
433 logf(LOG_LOG, "Unknown esetname '%s'",
434 p->u.elementSetName);
435 return 25; /* invalid esetname */
437 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
438 p->u.elementSetName);
441 case Z_ElementSpec_externalSpec:
442 if (p->u.externalSpec->which == Z_External_espec1)
444 logf(LOG_DEBUG, "Got Espec-1");
445 espec = p->u.externalSpec-> u.espec1;
449 logf(LOG_LOG, "Unknown external espec.");
450 return 25; /* bad. what is proper diagnostic? */
459 return data1_doespec1(n, espec);
464 static int grs_retrieve(struct recRetrieveCtrl *p)
466 data1_node *node = 0, *onode = 0;
469 int res, selected = 0;
470 NMEM mem = nmem_create();
471 struct grs_read_info gri;
473 gri.readf = p->readf;
474 gri.seekf = p->seekf;
480 node = read_grs_type (&gri, p->subType);
481 /* node = data1_read_record(p->readf, p->fh, mem); */
487 if (p->score >= 0 && (new = data1_insert_taggeddata(node, node, "rank",
490 new->u.data.what = DATA1I_num;
491 new->u.data.data = new->u.data.lbuf;
492 sprintf(new->u.data.data, "%d", p->score);
493 new->u.data.len = strlen(new->u.data.data);
495 if ((new = data1_insert_taggeddata(node, node, "localControlNumber", mem)))
497 new->u.data.what = DATA1I_text;
498 new->u.data.data = new->u.data.lbuf;
499 sprintf(new->u.data.data, "%d", p->localno);
500 new->u.data.len = strlen(new->u.data.data);
502 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
503 node->u.root.absyn->reference != VAL_NONE)
508 oe.proto = PROTO_Z3950;
509 oe.oclass = CLASS_SCHEMA;
510 oe.value = node->u.root.absyn->reference;
512 if ((oid = oid_getoidbyent(&oe)))
518 for (ii = oid; *ii >= 0; ii++)
522 sprintf(p, "%d", *ii);
527 if ((new = data1_insert_taggeddata(node, node, "schemaIdentifier",
530 new->u.data.what = DATA1I_oid;
531 new->u.data.data = nmem_malloc(mem, p - tmp);
532 memcpy(new->u.data.data, tmp, p - tmp);
533 new->u.data.len = p - tmp;
539 * Does the requested format match a known schema-mapping? (this reflects
540 * the overlap of schema and formatting which is inherent in the MARC
542 * NOTE: This should look at the schema-specification in the compspec
545 for (map = node->u.root.absyn->maptabs; map; map = map->next)
546 if (map->target_absyn_ref == p->input_format)
549 if (!(node = data1_map_record(onode, map, mem)))
558 if (p->comp && (res = process_comp(node, p->comp)) > 0)
562 data1_free_tree(onode);
563 data1_free_tree(node);
567 else if (p->comp && !res)
570 switch (p->output_format = (p->input_format != VAL_NONE ?
571 p->input_format : VAL_SUTRS))
573 data1_marctab *marctab;
577 if (!(p->rec_buf = data1_nodetogr(node, selected, p->odr, &dummy)))
578 p->diagnostic = 2; /* this should be better specified */
583 if (!(p->rec_buf = data1_nodetoexplain(node, selected, p->odr)))
584 p->diagnostic = 2; /* this should be better specified */
589 if (!(p->rec_buf = data1_nodetosummary(node, selected, p->odr)))
595 if (!(p->rec_buf = data1_nodetobuf(node, selected,
603 if (!(p->rec_buf = data1_nodetosoif(node, selected,
611 for (marctab = node->u.root.absyn->marc; marctab;
612 marctab = marctab->next)
613 if (marctab->reference == p->input_format)
620 if (!(p->rec_buf = data1_nodetomarc(marctab, node, selected,
628 data1_free_tree(node);
630 data1_free_tree(onode);
635 static struct recType grs_type =
643 RecType recTypeGrs = &grs_type;