2 * Copyright (C) 1994-1997, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1997-09-18 08:59:21 adam
8 * Extra generic handle for the character mapping routines.
10 * Revision 1.9 1997/09/17 12:19:21 adam
11 * Zebra version corresponds to YAZ version 1.4.
12 * Changed Zebra server so that it doesn't depend on global common_resource.
14 * Revision 1.8 1997/09/09 13:38:14 adam
15 * Partial port to WIN95/NT.
17 * Revision 1.7 1997/09/05 15:30:10 adam
18 * Changed prototype for chr_map_input - added const.
19 * Added support for C++, headers uses extern "C" for public definitions.
21 * Revision 1.6 1997/09/04 13:54:40 adam
22 * Added MARC filter - type grs.marc.<syntax> where syntax refers
23 * to abstract syntax. New method tellf in retrieve/extract method.
25 * Revision 1.5 1997/07/15 16:29:03 adam
26 * Initialized dummy variable to keep checker gcc happy.
28 * Revision 1.4 1997/04/30 08:56:08 quinn
31 * Revision 1.2 1996/10/11 16:06:43 quinn
32 * Revision 1.3 1997/02/24 10:41:50 adam
33 * Cleanup of code and commented out the "end element-end-record" code.
35 * Revision 1.2 1996/10/11 16:06:43 quinn
36 * Fixed arguments to nodetogr
38 * Revision 1.1 1996/10/11 10:57:25 adam
39 * New module recctrl. Used to manage records (extract/retrieval).
41 * Revision 1.29 1996/10/08 10:30:21 quinn
44 * Revision 1.28 1996/10/07 16:06:40 quinn
47 * Revision 1.27 1996/06/11 10:54:12 quinn
50 * Revision 1.26 1996/06/06 12:08:45 quinn
51 * Added showRecord function
53 * Revision 1.25 1996/06/04 14:18:53 quinn
56 * Revision 1.24 1996/06/04 13:27:54 quinn
57 * More work on charmapping
59 * Revision 1.23 1996/06/04 10:19:01 adam
60 * Minor changes - removed include of ctype.h.
62 * Revision 1.22 1996/06/03 10:15:27 quinn
63 * Various character-mapping.
65 * Revision 1.21 1996/05/31 13:27:24 quinn
66 * Character-conversion in phrases, too.
68 * Revision 1.19 1996/05/16 15:31:14 quinn
71 * Revision 1.18 1996/05/09 07:28:56 quinn
72 * Work towards phrases and multiple registers
74 * Revision 1.17 1996/05/01 13:46:37 adam
75 * First work on multiple records in one file.
76 * New option, -offset, to the "unread" command in the filter module.
78 * Revision 1.16 1996/01/17 14:57:54 adam
79 * Prototype changed for reader functions in extract/retrieve. File
80 * is identified by 'void *' instead of 'int.
82 * Revision 1.15 1996/01/08 19:15:47 adam
83 * New input filter that works!
85 * Revision 1.14 1995/12/15 12:36:11 adam
86 * Retrieval calls data1_read_regx when subType is specified.
88 * Revision 1.13 1995/12/15 12:24:43 quinn
89 * *** empty log message ***
91 * Revision 1.12 1995/12/15 12:20:28 quinn
92 * *** empty log message ***
94 * Revision 1.11 1995/12/15 12:07:57 quinn
95 * Changed extraction strategy.
97 * Revision 1.10 1995/12/14 11:10:48 quinn
100 * Revision 1.9 1995/12/13 17:14:05 quinn
101 * *** empty log message ***
103 * Revision 1.8 1995/12/13 15:33:18 quinn
104 * *** empty log message ***
106 * Revision 1.7 1995/12/13 13:45:39 quinn
107 * Changed data1 to use nmem.
109 * Revision 1.6 1995/12/04 14:22:30 adam
110 * Extra arg to recType_byName.
111 * Started work on new regular expression parsed input to
112 * structured records.
114 * Revision 1.5 1995/11/28 14:18:37 quinn
117 * Revision 1.4 1995/11/21 13:14:49 quinn
118 * Fixed end-of-data-field problem (maybe).
120 * Revision 1.3 1995/11/15 19:13:09 adam
121 * Work on record management.
127 #include <sys/types.h>
139 #define GRS_MAX_WORD 512
141 static int seqno = 0;
143 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
147 data1_node *(*func)(struct grs_read_info *p);
149 { "sgml", grs_read_sgml },
150 { "regx", grs_read_regx },
151 { "marc", grs_read_marc },
154 const char *cp = strchr (type, '.');
157 if (cp == NULL || cp == type)
159 cp = strlen(type) + type;
163 strcpy (p->type, cp+1);
164 for (i=0; tab[i].type; i++)
166 if (!memcmp (type, tab[i].type, cp-type))
167 return (tab[i].func)(p);
172 static void grs_init(void)
176 static void dumpkeys_word(data1_node *n, struct recExtractCtrl *p,
179 const char *b = n->u.data.data;
181 const char **map = 0;
183 remain = n->u.data.len - (b - n->u.data.data);
185 map = (*p->map_chrs_input)(0, &b, remain);
190 char buf[GRS_MAX_WORD+1];
194 while (map && *map && **map == *CHR_SPACE)
196 remain = n->u.data.len - (b - n->u.data.data);
198 map = (*p->map_chrs_input)(0, &b, remain);
205 while (map && *map && **map != *CHR_SPACE)
207 const char *cp = *map;
209 while (i < GRS_MAX_WORD && *cp)
211 remain = n->u.data.len - (b - n->u.data.data);
213 map = (*p->map_chrs_input)(0, &b, remain);
220 (*p->init)(&wrd); /* set defaults */
221 wrd.which = Word_String;
224 wrd.attrSet = att->parent->ordinal;
225 wrd.attrUse = att->locals->local;
230 static void dumpkeys_phrase(data1_node *n, struct recExtractCtrl *p,
233 const char *b = n->u.data.data;
234 char buf[GRS_MAX_WORD+1];
235 const char **map = 0;
239 remain = n->u.data.len - (b - n->u.data.data);
241 map = (*p->map_chrs_input)(0, &b, remain);
243 while (remain > 0 && i < GRS_MAX_WORD)
245 while (map && *map && **map == *CHR_SPACE)
247 remain = n->u.data.len - (b - n->u.data.data);
249 map = (*p->map_chrs_input)(0, &b, remain);
256 if (i && i < GRS_MAX_WORD)
257 buf[i++] = *CHR_SPACE;
258 while (map && *map && **map != *CHR_SPACE)
260 const char *cp = *map;
262 if (i >= GRS_MAX_WORD)
264 while (i < GRS_MAX_WORD && *cp)
266 remain = n->u.data.len - (b - n->u.data.data);
268 map = (*p->map_chrs_input)(0, &b, remain);
277 wrd.which = Word_Phrase;
280 wrd.attrSet = att->parent->ordinal;
281 wrd.attrUse = att->locals->local;
285 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
287 for (; n; n = n->next)
289 if (p->flagShowRecords) /* display element description to user */
291 if (n->which == DATA1N_root)
293 printf("%*s", level * 4, "");
294 printf("Record type: '%s'\n", n->u.root.absyn->name);
296 else if (n->which == DATA1N_tag)
300 printf("%*s", level * 4, "");
301 if (!(e = n->u.tag.element))
302 printf("Local tag: '%s'\n", n->u.tag.tag);
305 printf("Elm: '%s' ", e->name);
308 data1_tag *t = e->tag;
310 printf("TagNam: '%s' ", t->names->name);
313 printf("%s[%d],", t->tagset->name, t->tagset->type);
316 if (t->which == DATA1T_numeric)
317 printf("%d)", t->value.numeric);
319 printf("'%s')", t->value.string);
327 if (dumpkeys(n->child, p, level + 1) < 0)
330 if (n->which == DATA1N_data)
332 data1_node *par = get_parent_tag(p->dh, n);
333 data1_termlist *tlist = 0;
335 if (p->flagShowRecords)
337 printf("%*s", level * 4, "");
339 if (n->u.data.len > 20)
340 printf("'%.20s...'\n", n->u.data.data);
341 else if (n->u.data.len > 0)
342 printf("'%.*s'\n", n->u.data.len, n->u.data.data);
350 * cycle up towards the root until we find a tag with an att..
351 * this has the effect of indexing locally defined tags with
352 * the attribute of their ancestor in the record.
355 while (!par->u.tag.element)
356 if (!par->parent || !(par = get_parent_tag(p->dh, par->parent)))
360 else if (par->u.tag.element->termlists)
361 tlist = par->u.tag.element->termlists;
365 for (; tlist; tlist = tlist->next)
367 if (p->flagShowRecords)
369 printf("%*sIdx: [", (level + 1) * 4, "");
370 switch (tlist->structure)
372 case DATA1S_word: printf("w"); break;
373 case DATA1S_phrase: printf("p"); break;
374 default: printf("?"); break;
377 printf("%s:%s [%d]\n", tlist->att->parent->name,
378 tlist->att->name, tlist->att->value);
380 else switch (tlist->structure)
383 dumpkeys_word(n, p, tlist->att); break;
385 dumpkeys_phrase(n, p, tlist->att); break;
387 logf(LOG_FATAL, "Bad structure type in dumpkeys");
392 if (p->flagShowRecords && n->which == DATA1N_root)
394 printf("%*s-------------\n\n", level * 4, "");
400 static int grs_extract(struct recExtractCtrl *p)
403 NMEM mem = nmem_create();
404 struct grs_read_info gri;
407 gri.readf = p->readf;
408 gri.seekf = p->seekf;
409 gri.tellf = p->tellf;
412 gri.offset = p->offset;
416 n = read_grs_type (&gri, p->subType);
419 if (dumpkeys(n, p, 0) < 0)
421 data1_free_tree(p->dh, n);
424 data1_free_tree(p->dh, n);
430 * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
432 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
434 data1_esetname *eset;
440 case Z_RecordComp_simple:
441 if (c->u.simple->which != Z_ElementSetNames_generic)
442 return 26; /* only generic form supported. Fix this later */
443 if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
444 c->u.simple->u.generic)))
446 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
447 return 25; /* invalid esetname */
449 logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
450 c->u.simple->u.generic);
453 case Z_RecordComp_complex:
454 if (c->u.complex->generic)
456 /* insert check for schema */
457 if ((p = c->u.complex->generic->elementSpec))
460 case Z_ElementSpec_elementSetName:
462 data1_getesetbyname(dh,
464 p->u.elementSetName)))
466 logf(LOG_LOG, "Unknown esetname '%s'",
467 p->u.elementSetName);
468 return 25; /* invalid esetname */
470 logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
471 p->u.elementSetName);
474 case Z_ElementSpec_externalSpec:
475 if (p->u.externalSpec->which == Z_External_espec1)
477 logf(LOG_DEBUG, "Got Espec-1");
478 espec = p->u.externalSpec-> u.espec1;
482 logf(LOG_LOG, "Unknown external espec.");
483 return 25; /* bad. what is proper diagnostic? */
492 return data1_doespec1(dh, n, espec);
497 static int grs_retrieve(struct recRetrieveCtrl *p)
499 data1_node *node = 0, *onode = 0;
502 int res, selected = 0;
503 NMEM mem = nmem_create();
504 struct grs_read_info gri;
506 gri.readf = p->readf;
507 gri.seekf = p->seekf;
508 gri.tellf = p->tellf;
515 node = read_grs_type (&gri, p->subType);
521 if (p->score >= 0 && (new =
522 data1_insert_taggeddata(p->dh, node,
526 new->u.data.what = DATA1I_num;
527 new->u.data.data = new->u.data.lbuf;
528 sprintf(new->u.data.data, "%d", p->score);
529 new->u.data.len = strlen(new->u.data.data);
531 if ((new = data1_insert_taggeddata(p->dh, node, node,
532 "localControlNumber", mem)))
534 new->u.data.what = DATA1I_text;
535 new->u.data.data = new->u.data.lbuf;
536 sprintf(new->u.data.data, "%d", p->localno);
537 new->u.data.len = strlen(new->u.data.data);
539 if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
540 node->u.root.absyn->reference != VAL_NONE)
545 oe.proto = PROTO_Z3950;
546 oe.oclass = CLASS_SCHEMA;
547 oe.value = node->u.root.absyn->reference;
549 if ((oid = oid_getoidbyent(&oe)))
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;