2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 1997-10-27 14:34:00 adam
8 * Work on generic character mapping depending on "structure" field
9 * in abstract syntax file.
11 * Revision 1.10 1997/09/18 08:59:21 adam
12 * Extra generic handle for the character mapping routines.
14 * Revision 1.9 1997/09/17 12:19:21 adam
15 * Zebra version corresponds to YAZ version 1.4.
16 * Changed Zebra server so that it doesn't depend on global common_resource.
18 * Revision 1.8 1997/09/09 13:38:14 adam
19 * Partial port to WIN95/NT.
21 * Revision 1.7 1997/09/05 15:30:10 adam
22 * Changed prototype for chr_map_input - added const.
23 * Added support for C++, headers uses extern "C" for public definitions.
25 * Revision 1.6 1997/09/04 13:54:40 adam
26 * Added MARC filter - type grs.marc.<syntax> where syntax refers
27 * to abstract syntax. New method tellf in retrieve/extract method.
29 * Revision 1.5 1997/07/15 16:29:03 adam
30 * Initialized dummy variable to keep checker gcc happy.
32 * Revision 1.4 1997/04/30 08:56:08 quinn
35 * Revision 1.2 1996/10/11 16:06:43 quinn
36 * Revision 1.3 1997/02/24 10:41:50 adam
37 * Cleanup of code and commented out the "end element-end-record" code.
39 * Revision 1.2 1996/10/11 16:06:43 quinn
40 * Fixed arguments to nodetogr
42 * Revision 1.1 1996/10/11 10:57:25 adam
43 * New module recctrl. Used to manage records (extract/retrieval).
45 * Revision 1.29 1996/10/08 10:30:21 quinn
48 * Revision 1.28 1996/10/07 16:06:40 quinn
51 * Revision 1.27 1996/06/11 10:54:12 quinn
54 * Revision 1.26 1996/06/06 12:08:45 quinn
55 * Added showRecord function
57 * Revision 1.25 1996/06/04 14:18:53 quinn
60 * Revision 1.24 1996/06/04 13:27:54 quinn
61 * More work on charmapping
63 * Revision 1.23 1996/06/04 10:19:01 adam
64 * Minor changes - removed include of ctype.h.
66 * Revision 1.22 1996/06/03 10:15:27 quinn
67 * Various character-mapping.
69 * Revision 1.21 1996/05/31 13:27:24 quinn
70 * Character-conversion in phrases, too.
72 * Revision 1.19 1996/05/16 15:31:14 quinn
75 * Revision 1.18 1996/05/09 07:28:56 quinn
76 * Work towards phrases and multiple registers
78 * Revision 1.17 1996/05/01 13:46:37 adam
79 * First work on multiple records in one file.
80 * New option, -offset, to the "unread" command in the filter module.
82 * Revision 1.16 1996/01/17 14:57:54 adam
83 * Prototype changed for reader functions in extract/retrieve. File
84 * is identified by 'void *' instead of 'int.
86 * Revision 1.15 1996/01/08 19:15:47 adam
87 * New input filter that works!
89 * Revision 1.14 1995/12/15 12:36:11 adam
90 * Retrieval calls data1_read_regx when subType is specified.
92 * Revision 1.13 1995/12/15 12:24:43 quinn
93 * *** empty log message ***
95 * Revision 1.12 1995/12/15 12:20:28 quinn
96 * *** empty log message ***
98 * Revision 1.11 1995/12/15 12:07:57 quinn
99 * Changed extraction strategy.
101 * Revision 1.10 1995/12/14 11:10:48 quinn
104 * Revision 1.9 1995/12/13 17:14:05 quinn
105 * *** empty log message ***
107 * Revision 1.8 1995/12/13 15:33:18 quinn
108 * *** empty log message ***
110 * Revision 1.7 1995/12/13 13:45:39 quinn
111 * Changed data1 to use nmem.
113 * Revision 1.6 1995/12/04 14:22:30 adam
114 * Extra arg to recType_byName.
115 * Started work on new regular expression parsed input to
116 * structured records.
118 * Revision 1.5 1995/11/28 14:18:37 quinn
121 * Revision 1.4 1995/11/21 13:14:49 quinn
122 * Fixed end-of-data-field problem (maybe).
124 * Revision 1.3 1995/11/15 19:13:09 adam
125 * Work on record management.
131 #include <sys/types.h>
143 #define GRS_MAX_WORD 512
145 static int seqno = 0;
147 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
151 data1_node *(*func)(struct grs_read_info *p);
153 { "sgml", grs_read_sgml },
154 { "regx", grs_read_regx },
155 { "marc", grs_read_marc },
158 const char *cp = strchr (type, '.');
161 if (cp == NULL || cp == type)
163 cp = strlen(type) + type;
167 strcpy (p->type, cp+1);
168 for (i=0; tab[i].type; i++)
170 if (!memcmp (type, tab[i].type, cp-type))
171 return (tab[i].func)(p);
176 static void grs_init(void)
180 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
181 data1_att *att, int reg_type)
183 const char *b = n->u.data.data;
185 const char **map = 0;
187 remain = n->u.data.len - (b - n->u.data.data);
189 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
194 char buf[GRS_MAX_WORD+1];
198 while (map && *map && **map == *CHR_SPACE)
200 remain = n->u.data.len - (b - n->u.data.data);
202 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
209 while (map && *map && **map != *CHR_SPACE)
211 const char *cp = *map;
213 while (i < GRS_MAX_WORD && *cp)
215 remain = n->u.data.len - (b - n->u.data.data);
217 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
224 (*p->init)(&wrd); /* set defaults */
225 wrd.reg_type = reg_type;
228 wrd.attrSet = att->parent->ordinal;
229 wrd.attrUse = att->locals->local;
234 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
235 data1_att *att, int reg_type)
237 const char *b = n->u.data.data;
238 char buf[GRS_MAX_WORD+1];
239 const char **map = 0;
243 remain = n->u.data.len - (b - n->u.data.data);
245 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
247 while (remain > 0 && i < GRS_MAX_WORD)
249 while (map && *map && **map == *CHR_SPACE)
251 remain = n->u.data.len - (b - n->u.data.data);
253 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
260 if (i && i < GRS_MAX_WORD)
261 buf[i++] = *CHR_SPACE;
262 while (map && *map && **map != *CHR_SPACE)
264 const char *cp = *map;
266 if (i >= GRS_MAX_WORD)
268 while (i < GRS_MAX_WORD && *cp)
270 remain = n->u.data.len - (b - n->u.data.data);
272 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
282 wrd.reg_type = reg_type;
285 wrd.attrSet = att->parent->ordinal;
286 wrd.attrUse = att->locals->local;
290 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
292 for (; n; n = n->next)
294 if (p->flagShowRecords) /* display element description to user */
296 if (n->which == DATA1N_root)
298 printf("%*s", level * 4, "");
299 printf("Record type: '%s'\n", n->u.root.absyn->name);
301 else if (n->which == DATA1N_tag)
305 printf("%*s", level * 4, "");
306 if (!(e = n->u.tag.element))
307 printf("Local tag: '%s'\n", n->u.tag.tag);
310 printf("Elm: '%s' ", e->name);
313 data1_tag *t = e->tag;
315 printf("TagNam: '%s' ", t->names->name);
318 printf("%s[%d],", t->tagset->name, t->tagset->type);
321 if (t->which == DATA1T_numeric)
322 printf("%d)", t->value.numeric);
324 printf("'%s')", t->value.string);
332 if (dumpkeys(n->child, p, level + 1) < 0)
335 if (n->which == DATA1N_data)
337 data1_node *par = get_parent_tag(p->dh, n);
338 data1_termlist *tlist = 0;
339 data1_datatype dtype = DATA1K_string;
341 if (p->flagShowRecords)
343 printf("%*s", level * 4, "");
345 if (n->u.data.len > 20)
346 printf("'%.20s...'\n", n->u.data.data);
347 else if (n->u.data.len > 0)
348 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
356 * cycle up towards the root until we find a tag with an att..
357 * this has the effect of indexing locally defined tags with
358 * the attribute of their ancestor in the record.
361 while (!par->u.tag.element)
362 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
364 if (!par || !(tlist = par->u.tag.element->termlists))
366 if (par->u.tag.element->tag)
367 dtype = par->u.tag.element->tag->kind;
368 for (; tlist; tlist = tlist->next)
370 if (p->flagShowRecords)
372 printf("%*sIdx: [%s]", (level + 1) * 4, "",
374 printf("%s:%s [%d]\n",
375 tlist->att->parent->name,
376 tlist->att->name, tlist->att->value);
379 if (zebra_maps_is_complete (p->zebra_maps,
381 dumpkeys_complete_field(n, p, tlist->att,
384 dumpkeys_incomplete_field(n, p, tlist->att,
388 if (p->flagShowRecords && n->which == DATA1N_root)
390 printf("%*s-------------\n\n", level * 4, "");
396 static int grs_extract(struct recExtractCtrl *p)
399 NMEM mem = nmem_create();
400 struct grs_read_info gri;
403 gri.readf = p->readf;
404 gri.seekf = p->seekf;
405 gri.tellf = p->tellf;
408 gri.offset = p->offset;
412 n = read_grs_type (&gri, p->subType);
415 if (dumpkeys(n, p, 0) < 0)
417 data1_free_tree(p->dh, n);
420 data1_free_tree(p->dh, n);
426 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
428 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
430 data1_esetname *eset;
436 case Z_RecordComp_simple:
437 if (c->u.simple->which != Z_ElementSetNames_generic)
438 return 26; /* only generic form supported. Fix this later */
439 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
440 c->u.simple->u.generic)))
442 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
443 return 25; /* invalid esetname */
445 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
446 c->u.simple->u.generic);
449 case Z_RecordComp_complex:
450 if (c->u.complex->generic)
452 /* insert check for schema */
453 if ((p = c->u.complex->generic->elementSpec))
456 case Z_ElementSpec_elementSetName:
458 data1_getesetbyname(dh,
460 p->u.elementSetName)))
462 logf(LOG_LOG, "Unknown esetname '%s'",
463 p->u.elementSetName);
464 return 25; /* invalid esetname */
466 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
467 p->u.elementSetName);
470 case Z_ElementSpec_externalSpec:
471 if (p->u.externalSpec->which == Z_External_espec1)
473 logf(LOG_DEBUG, "Got Espec-1");
474 espec = p->u.externalSpec-> u.espec1;
478 logf(LOG_LOG, "Unknown external espec.");
479 return 25; /* bad. what is proper diagnostic? */
488 return data1_doespec1(dh, n, espec);
493 static int grs_retrieve(struct recRetrieveCtrl *p)
495 data1_node *node = 0, *onode = 0;
498 int res, selected = 0;
499 NMEM mem = nmem_create();
500 struct grs_read_info gri;
502 gri.readf = p->readf;
503 gri.seekf = p->seekf;
504 gri.tellf = p->tellf;
511 node = read_grs_type (&gri, p->subType);
517 if (p->score >= 0 && (new =
518 data1_insert_taggeddata(p->dh, node,
522 new->u.data.what = DATA1I_num;
523 new->u.data.data = new->lbuf;
524 sprintf(new->u.data.data, "%d", p->score);
525 new->u.data.len = strlen(new->u.data.data);
527 if ((new = data1_insert_taggeddata(p->dh, node, node,
528 "localControlNumber", mem)))
530 new->u.data.what = DATA1I_text;
531 new->u.data.data = new->lbuf;
532 sprintf(new->u.data.data, "%d", p->localno);
533 new->u.data.len = strlen(new->u.data.data);
535 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
536 node->u.root.absyn->reference != VAL_NONE)
541 oe.proto = PROTO_Z3950;
542 oe.oclass = CLASS_SCHEMA;
543 oe.value = node->u.root.absyn->reference;
545 if ((oid = oid_getoidbyent(&oe)))
548 data1_handle dh = p->dh;
552 for (ii = oid; *ii >= 0; ii++)
556 sprintf(p, "%d", *ii);
561 if ((new = data1_insert_taggeddata(dh, node, node,
562 "schemaIdentifier", mem)))
564 new->u.data.what = DATA1I_oid;
565 new->u.data.data = nmem_malloc(mem, p - tmp);
566 memcpy(new->u.data.data, tmp, p - tmp);
567 new->u.data.len = p - tmp;
573 * Does the requested format match a known schema-mapping? (this reflects
574 * the overlap of schema and formatting which is inherent in the MARC
576 * NOTE: This should look at the schema-specification in the compspec
579 for (map = node->u.root.absyn->maptabs; map; map = map->next)
580 if (map->target_absyn_ref == p->input_format)
583 if (!(node = data1_map_record(p->dh, onode, map, mem)))
592 if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
596 data1_free_tree(p->dh, onode);
597 data1_free_tree(p->dh, node);
601 else if (p->comp && !res)
604 switch (p->output_format = (p->input_format != VAL_NONE ?
605 p->input_format : VAL_SUTRS))
607 data1_marctab *marctab;
612 if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
614 p->diagnostic = 2; /* this should be better specified */
619 if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
621 p->diagnostic = 2; /* this should be better specified */
626 if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
633 if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
641 if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
649 for (marctab = node->u.root.absyn->marc; marctab;
650 marctab = marctab->next)
651 if (marctab->reference == p->input_format)
658 if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
667 data1_free_tree(p->dh, node);
669 data1_free_tree(p->dh, onode);
674 static struct recType grs_type =
682 RecType recTypeGrs = &grs_type;