2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 1997-10-29 12:02:22 adam
8 * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
10 * Revision 1.11 1997/10/27 14:34:00 adam
11 * Work on generic character mapping depending on "structure" field
12 * in abstract syntax file.
14 * Revision 1.10 1997/09/18 08:59:21 adam
15 * Extra generic handle for the character mapping routines.
17 * Revision 1.9 1997/09/17 12:19:21 adam
18 * Zebra version corresponds to YAZ version 1.4.
19 * Changed Zebra server so that it doesn't depend on global common_resource.
21 * Revision 1.8 1997/09/09 13:38:14 adam
22 * Partial port to WIN95/NT.
24 * Revision 1.7 1997/09/05 15:30:10 adam
25 * Changed prototype for chr_map_input - added const.
26 * Added support for C++, headers uses extern "C" for public definitions.
28 * Revision 1.6 1997/09/04 13:54:40 adam
29 * Added MARC filter - type grs.marc.<syntax> where syntax refers
30 * to abstract syntax. New method tellf in retrieve/extract method.
32 * Revision 1.5 1997/07/15 16:29:03 adam
33 * Initialized dummy variable to keep checker gcc happy.
35 * Revision 1.4 1997/04/30 08:56:08 quinn
38 * Revision 1.2 1996/10/11 16:06:43 quinn
39 * Revision 1.3 1997/02/24 10:41:50 adam
40 * Cleanup of code and commented out the "end element-end-record" code.
42 * Revision 1.2 1996/10/11 16:06:43 quinn
43 * Fixed arguments to nodetogr
45 * Revision 1.1 1996/10/11 10:57:25 adam
46 * New module recctrl. Used to manage records (extract/retrieval).
48 * Revision 1.29 1996/10/08 10:30:21 quinn
51 * Revision 1.28 1996/10/07 16:06:40 quinn
54 * Revision 1.27 1996/06/11 10:54:12 quinn
57 * Revision 1.26 1996/06/06 12:08:45 quinn
58 * Added showRecord function
60 * Revision 1.25 1996/06/04 14:18:53 quinn
63 * Revision 1.24 1996/06/04 13:27:54 quinn
64 * More work on charmapping
66 * Revision 1.23 1996/06/04 10:19:01 adam
67 * Minor changes - removed include of ctype.h.
69 * Revision 1.22 1996/06/03 10:15:27 quinn
70 * Various character-mapping.
72 * Revision 1.21 1996/05/31 13:27:24 quinn
73 * Character-conversion in phrases, too.
75 * Revision 1.19 1996/05/16 15:31:14 quinn
78 * Revision 1.18 1996/05/09 07:28:56 quinn
79 * Work towards phrases and multiple registers
81 * Revision 1.17 1996/05/01 13:46:37 adam
82 * First work on multiple records in one file.
83 * New option, -offset, to the "unread" command in the filter module.
85 * Revision 1.16 1996/01/17 14:57:54 adam
86 * Prototype changed for reader functions in extract/retrieve. File
87 * is identified by 'void *' instead of 'int.
89 * Revision 1.15 1996/01/08 19:15:47 adam
90 * New input filter that works!
92 * Revision 1.14 1995/12/15 12:36:11 adam
93 * Retrieval calls data1_read_regx when subType is specified.
95 * Revision 1.13 1995/12/15 12:24:43 quinn
96 * *** empty log message ***
98 * Revision 1.12 1995/12/15 12:20:28 quinn
99 * *** empty log message ***
101 * Revision 1.11 1995/12/15 12:07:57 quinn
102 * Changed extraction strategy.
104 * Revision 1.10 1995/12/14 11:10:48 quinn
107 * Revision 1.9 1995/12/13 17:14:05 quinn
108 * *** empty log message ***
110 * Revision 1.8 1995/12/13 15:33:18 quinn
111 * *** empty log message ***
113 * Revision 1.7 1995/12/13 13:45:39 quinn
114 * Changed data1 to use nmem.
116 * Revision 1.6 1995/12/04 14:22:30 adam
117 * Extra arg to recType_byName.
118 * Started work on new regular expression parsed input to
119 * structured records.
121 * Revision 1.5 1995/11/28 14:18:37 quinn
124 * Revision 1.4 1995/11/21 13:14:49 quinn
125 * Fixed end-of-data-field problem (maybe).
127 * Revision 1.3 1995/11/15 19:13:09 adam
128 * Work on record management.
134 #include <sys/types.h>
146 #define GRS_MAX_WORD 512
148 static int seqno = 0;
150 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
154 data1_node *(*func)(struct grs_read_info *p);
156 { "sgml", grs_read_sgml },
157 { "regx", grs_read_regx },
158 { "marc", grs_read_marc },
161 const char *cp = strchr (type, '.');
164 if (cp == NULL || cp == type)
166 cp = strlen(type) + type;
170 strcpy (p->type, cp+1);
171 for (i=0; tab[i].type; i++)
173 if (!memcmp (type, tab[i].type, cp-type))
174 return (tab[i].func)(p);
179 static void grs_init(void)
183 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
184 data1_att *att, int reg_type)
186 const char *b = n->u.data.data;
188 const char **map = 0;
190 remain = n->u.data.len - (b - n->u.data.data);
192 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
197 char buf[GRS_MAX_WORD+1];
201 while (map && *map && **map == *CHR_SPACE)
203 remain = n->u.data.len - (b - n->u.data.data);
205 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
212 while (map && *map && **map != *CHR_SPACE)
214 const char *cp = *map;
216 while (i < GRS_MAX_WORD && *cp)
218 remain = n->u.data.len - (b - n->u.data.data);
220 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
227 (*p->init)(&wrd); /* set defaults */
228 wrd.reg_type = reg_type;
231 wrd.attrSet = att->parent->ordinal;
232 wrd.attrUse = att->locals->local;
237 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
238 data1_att *att, int reg_type)
240 const char *b = n->u.data.data;
241 char buf[GRS_MAX_WORD+1];
242 const char **map = 0;
246 remain = n->u.data.len - (b - n->u.data.data);
248 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
250 while (remain > 0 && i < GRS_MAX_WORD)
252 while (map && *map && **map == *CHR_SPACE)
254 remain = n->u.data.len - (b - n->u.data.data);
256 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
263 if (i && i < GRS_MAX_WORD)
264 buf[i++] = *CHR_SPACE;
265 while (map && *map && **map != *CHR_SPACE)
267 const char *cp = *map;
269 if (i >= GRS_MAX_WORD)
271 while (i < GRS_MAX_WORD && *cp)
273 remain = n->u.data.len - (b - n->u.data.data);
275 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
285 wrd.reg_type = reg_type;
288 wrd.attrSet = att->parent->ordinal;
289 wrd.attrUse = att->locals->local;
293 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
295 for (; n; n = n->next)
297 if (p->flagShowRecords) /* display element description to user */
299 if (n->which == DATA1N_root)
301 printf("%*s", level * 4, "");
302 printf("Record type: '%s'\n", n->u.root.absyn->name);
304 else if (n->which == DATA1N_tag)
308 printf("%*s", level * 4, "");
309 if (!(e = n->u.tag.element))
310 printf("Local tag: '%s'\n", n->u.tag.tag);
313 printf("Elm: '%s' ", e->name);
316 data1_tag *t = e->tag;
318 printf("TagNam: '%s' ", t->names->name);
321 printf("%s[%d],", t->tagset->name, t->tagset->type);
324 if (t->which == DATA1T_numeric)
325 printf("%d)", t->value.numeric);
327 printf("'%s')", t->value.string);
335 if (dumpkeys(n->child, p, level + 1) < 0)
338 if (n->which == DATA1N_data)
340 data1_node *par = get_parent_tag(p->dh, n);
341 data1_termlist *tlist = 0;
342 data1_datatype dtype = DATA1K_string;
344 if (p->flagShowRecords)
346 printf("%*s", level * 4, "");
348 if (n->u.data.len > 20)
349 printf("'%.20s...'\n", n->u.data.data);
350 else if (n->u.data.len > 0)
351 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
359 * cycle up towards the root until we find a tag with an att..
360 * this has the effect of indexing locally defined tags with
361 * the attribute of their ancestor in the record.
364 while (!par->u.tag.element)
365 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
367 if (!par || !(tlist = par->u.tag.element->termlists))
369 if (par->u.tag.element->tag)
370 dtype = par->u.tag.element->tag->kind;
371 for (; tlist; tlist = tlist->next)
373 if (p->flagShowRecords)
375 printf("%*sIdx: [%s]", (level + 1) * 4, "",
377 printf("%s:%s [%d]\n",
378 tlist->att->parent->name,
379 tlist->att->name, tlist->att->value);
382 if (zebra_maps_is_complete (p->zebra_maps,
384 dumpkeys_complete_field(n, p, tlist->att,
387 dumpkeys_incomplete_field(n, p, tlist->att,
391 if (p->flagShowRecords && n->which == DATA1N_root)
393 printf("%*s-------------\n\n", level * 4, "");
399 static int grs_extract(struct recExtractCtrl *p)
402 NMEM mem = nmem_create();
403 struct grs_read_info gri;
406 gri.readf = p->readf;
407 gri.seekf = p->seekf;
408 gri.tellf = p->tellf;
411 gri.offset = p->offset;
415 n = read_grs_type (&gri, p->subType);
418 if (dumpkeys(n, p, 0) < 0)
420 data1_free_tree(p->dh, n);
423 data1_free_tree(p->dh, n);
429 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
431 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
433 data1_esetname *eset;
439 case Z_RecordComp_simple:
440 if (c->u.simple->which != Z_ElementSetNames_generic)
441 return 26; /* only generic form supported. Fix this later */
442 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
443 c->u.simple->u.generic)))
445 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
446 return 25; /* invalid esetname */
448 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
449 c->u.simple->u.generic);
452 case Z_RecordComp_complex:
453 if (c->u.complex->generic)
455 /* insert check for schema */
456 if ((p = c->u.complex->generic->elementSpec))
459 case Z_ElementSpec_elementSetName:
461 data1_getesetbyname(dh,
463 p->u.elementSetName)))
465 logf(LOG_LOG, "Unknown esetname '%s'",
466 p->u.elementSetName);
467 return 25; /* invalid esetname */
469 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
470 p->u.elementSetName);
473 case Z_ElementSpec_externalSpec:
474 if (p->u.externalSpec->which == Z_External_espec1)
476 logf(LOG_DEBUG, "Got Espec-1");
477 espec = p->u.externalSpec-> u.espec1;
481 logf(LOG_LOG, "Unknown external espec.");
482 return 25; /* bad. what is proper diagnostic? */
491 return data1_doespec1(dh, n, espec);
496 static int grs_retrieve(struct recRetrieveCtrl *p)
498 data1_node *node = 0, *onode = 0;
501 int res, selected = 0;
502 NMEM mem = nmem_create();
503 struct grs_read_info gri;
505 gri.readf = p->readf;
506 gri.seekf = p->seekf;
507 gri.tellf = p->tellf;
514 node = read_grs_type (&gri, p->subType);
520 if (p->score >= 0 && (new =
521 data1_insert_taggeddata(p->dh, node,
525 new->u.data.what = DATA1I_num;
526 new->u.data.data = new->lbuf;
527 sprintf(new->u.data.data, "%d", p->score);
528 new->u.data.len = strlen(new->u.data.data);
530 if ((new = data1_insert_taggeddata(p->dh, node, node,
531 "localControlNumber", mem)))
533 new->u.data.what = DATA1I_text;
534 new->u.data.data = new->lbuf;
535 sprintf(new->u.data.data, "%d", p->localno);
536 new->u.data.len = strlen(new->u.data.data);
538 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
539 node->u.root.absyn->reference != VAL_NONE)
543 int oidtmp[OID_SIZE];
545 oe.proto = PROTO_Z3950;
546 oe.oclass = CLASS_SCHEMA;
547 oe.value = node->u.root.absyn->reference;
549 if ((oid = oid_ent_to_oid (&oe, oidtmp)))
552 data1_handle dh = p->dh;
556 for (ii = oid; *ii >= 0; ii++)
560 sprintf(p, "%d", *ii);
565 if ((new = data1_insert_taggeddata(dh, node, node,
566 "schemaIdentifier", mem)))
568 new->u.data.what = DATA1I_oid;
569 new->u.data.data = nmem_malloc(mem, p - tmp);
570 memcpy(new->u.data.data, tmp, p - tmp);
571 new->u.data.len = p - tmp;
577 * Does the requested format match a known schema-mapping? (this reflects
578 * the overlap of schema and formatting which is inherent in the MARC
580 * NOTE: This should look at the schema-specification in the compspec
583 for (map = node->u.root.absyn->maptabs; map; map = map->next)
584 if (map->target_absyn_ref == p->input_format)
587 if (!(node = data1_map_record(p->dh, onode, map, mem)))
596 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
600 data1_free_tree(p->dh, onode);
601 data1_free_tree(p->dh, node);
605 else if (p->comp && !res)
608 switch (p->output_format = (p->input_format != VAL_NONE ?
609 p->input_format : VAL_SUTRS))
611 data1_marctab *marctab;
616 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
618 p->diagnostic = 2; /* this should be better specified */
623 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
625 p->diagnostic = 2; /* this should be better specified */
630 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
637 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
645 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
653 for (marctab = node->u.root.absyn->marc; marctab;
654 marctab = marctab->next)
655 if (marctab->reference == p->input_format)
662 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
671 data1_free_tree(p->dh, node);
673 data1_free_tree(p->dh, onode);
678 static struct recType grs_type =
686 RecType recTypeGrs = &grs_type;