Fixed problem with mapping to record with unknown schema.
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: recgrs.c,v $
7  * Revision 1.16  1998-01-29 13:38:17  adam
8  * Fixed problem with mapping to record with unknown schema.
9  *
10  * Revision 1.15  1998/01/26 10:37:57  adam
11  * Better diagnostics.
12  *
13  * Revision 1.14  1997/11/06 11:41:01  adam
14  * Implemented "begin variant" for the sgml.regx filter.
15  *
16  * Revision 1.13  1997/10/31 12:35:44  adam
17  * Added a few log statements.
18  *
19  * Revision 1.12  1997/10/29 12:02:22  adam
20  * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
21  *
22  * Revision 1.11  1997/10/27 14:34:00  adam
23  * Work on generic character mapping depending on "structure" field
24  * in abstract syntax file.
25  *
26  * Revision 1.10  1997/09/18 08:59:21  adam
27  * Extra generic handle for the character mapping routines.
28  *
29  * Revision 1.9  1997/09/17 12:19:21  adam
30  * Zebra version corresponds to YAZ version 1.4.
31  * Changed Zebra server so that it doesn't depend on global common_resource.
32  *
33  * Revision 1.8  1997/09/09 13:38:14  adam
34  * Partial port to WIN95/NT.
35  *
36  * Revision 1.7  1997/09/05 15:30:10  adam
37  * Changed prototype for chr_map_input - added const.
38  * Added support for C++, headers uses extern "C" for public definitions.
39  *
40  * Revision 1.6  1997/09/04 13:54:40  adam
41  * Added MARC filter - type grs.marc.<syntax> where syntax refers
42  * to abstract syntax. New method tellf in retrieve/extract method.
43  *
44  * Revision 1.5  1997/07/15 16:29:03  adam
45  * Initialized dummy variable to keep checker gcc happy.
46  *
47  * Revision 1.4  1997/04/30 08:56:08  quinn
48  * null
49  *
50  * Revision 1.2  1996/10/11  16:06:43  quinn
51  * Revision 1.3  1997/02/24 10:41:50  adam
52  * Cleanup of code and commented out the "end element-end-record" code.
53  *
54  * Revision 1.2  1996/10/11 16:06:43  quinn
55  * Fixed arguments to nodetogr
56  *
57  * Revision 1.1  1996/10/11  10:57:25  adam
58  * New module recctrl. Used to manage records (extract/retrieval).
59  *
60  * Revision 1.29  1996/10/08 10:30:21  quinn
61  * Fixed type mismatch
62  *
63  * Revision 1.28  1996/10/07  16:06:40  quinn
64  * Added SOIF support
65  *
66  * Revision 1.27  1996/06/11  10:54:12  quinn
67  * Relevance work
68  *
69  * Revision 1.26  1996/06/06  12:08:45  quinn
70  * Added showRecord function
71  *
72  * Revision 1.25  1996/06/04  14:18:53  quinn
73  * Charmap work
74  *
75  * Revision 1.24  1996/06/04  13:27:54  quinn
76  * More work on charmapping
77  *
78  * Revision 1.23  1996/06/04  10:19:01  adam
79  * Minor changes - removed include of ctype.h.
80  *
81  * Revision 1.22  1996/06/03  10:15:27  quinn
82  * Various character-mapping.
83  *
84  * Revision 1.21  1996/05/31  13:27:24  quinn
85  * Character-conversion in phrases, too.
86  *
87  * Revision 1.19  1996/05/16  15:31:14  quinn
88  * a7
89  *
90  * Revision 1.18  1996/05/09  07:28:56  quinn
91  * Work towards phrases and multiple registers
92  *
93  * Revision 1.17  1996/05/01  13:46:37  adam
94  * First work on multiple records in one file.
95  * New option, -offset, to the "unread" command in the filter module.
96  *
97  * Revision 1.16  1996/01/17  14:57:54  adam
98  * Prototype changed for reader functions in extract/retrieve. File
99  *  is identified by 'void *' instead of 'int.
100  *
101  * Revision 1.15  1996/01/08  19:15:47  adam
102  * New input filter that works!
103  *
104  * Revision 1.14  1995/12/15  12:36:11  adam
105  * Retrieval calls data1_read_regx when subType is specified.
106  *
107  * Revision 1.13  1995/12/15  12:24:43  quinn
108  * *** empty log message ***
109  *
110  * Revision 1.12  1995/12/15  12:20:28  quinn
111  * *** empty log message ***
112  *
113  * Revision 1.11  1995/12/15  12:07:57  quinn
114  * Changed extraction strategy.
115  *
116  * Revision 1.10  1995/12/14  11:10:48  quinn
117  * Explain work
118  *
119  * Revision 1.9  1995/12/13  17:14:05  quinn
120  * *** empty log message ***
121  *
122  * Revision 1.8  1995/12/13  15:33:18  quinn
123  * *** empty log message ***
124  *
125  * Revision 1.7  1995/12/13  13:45:39  quinn
126  * Changed data1 to use nmem.
127  *
128  * Revision 1.6  1995/12/04  14:22:30  adam
129  * Extra arg to recType_byName.
130  * Started work on new regular expression parsed input to
131  * structured records.
132  *
133  * Revision 1.5  1995/11/28  14:18:37  quinn
134  * Set output_format.
135  *
136  * Revision 1.4  1995/11/21  13:14:49  quinn
137  * Fixed end-of-data-field problem (maybe).
138  *
139  * Revision 1.3  1995/11/15  19:13:09  adam
140  * Work on record management.
141  *
142  */
143
144 #include <stdio.h>
145 #include <assert.h>
146 #include <sys/types.h>
147 #ifndef WINDOWS
148 #include <unistd.h>
149 #endif
150
151 #include <log.h>
152 #include <oid.h>
153
154 #include <recctrl.h>
155 #include <charmap.h>
156 #include "grsread.h"
157
158 #define GRS_MAX_WORD 512
159
160 static int seqno = 0;
161
162 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
163 {
164     static struct {
165         char *type;
166         data1_node *(*func)(struct grs_read_info *p);
167     } tab[] = {
168         { "sgml",  grs_read_sgml },
169         { "regx",  grs_read_regx },
170         { "marc",  grs_read_marc },
171         { NULL, NULL }
172     };
173     const char *cp = strchr (type, '.');
174     int i;
175
176     if (cp == NULL || cp == type)
177     {
178         cp = strlen(type) + type;
179         *p->type = 0;
180     }
181     else
182         strcpy (p->type, cp+1);
183     for (i=0; tab[i].type; i++)
184     {
185         if (!memcmp (type, tab[i].type, cp-type))
186             return (tab[i].func)(p);
187     }
188     return NULL;
189 }
190
191 static void grs_init(void)
192 {
193 }
194
195 static void dumpkeys_incomplete_field(data1_node *n, struct recExtractCtrl *p,
196                                       data1_att *att, int reg_type)
197 {
198     const char *b = n->u.data.data;
199     int remain;
200     const char **map = 0;
201
202     remain = n->u.data.len - (b - n->u.data.data);
203     if (remain > 0)
204         map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
205
206     while (map)
207     {
208         RecWord wrd;
209         char buf[GRS_MAX_WORD+1];
210         int i, remain;
211
212         /* Skip spaces */
213         while (map && *map && **map == *CHR_SPACE)
214         {
215             remain = n->u.data.len - (b - n->u.data.data);
216             if (remain > 0)
217                 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
218             else
219                 map = 0;
220         }
221         if (!map)
222             break;
223         i = 0;
224         while (map && *map && **map != *CHR_SPACE)
225         {
226             const char *cp = *map;
227
228             while (i < GRS_MAX_WORD && *cp)
229                 buf[i++] = *(cp++);
230             remain = n->u.data.len - (b - n->u.data.data);
231             if (remain > 0)
232                 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
233             else
234                 map = 0;
235         }
236         if (!i)
237             return;
238         buf[i] = '\0';
239         (*p->init)(&wrd);      /* set defaults */
240         wrd.reg_type = reg_type;
241         wrd.seqno = seqno++;
242         wrd.string = buf;
243         wrd.attrSet = att->parent->ordinal;
244         wrd.attrUse = att->locals->local;
245         (*p->add)(&wrd);
246     }
247 }
248
249 static void dumpkeys_complete_field(data1_node *n, struct recExtractCtrl *p,
250                                     data1_att *att, int reg_type)
251 {
252     const char *b = n->u.data.data;
253     char buf[GRS_MAX_WORD+1];
254     const char **map = 0;
255     RecWord wrd;
256     int i = 0, remain;
257
258     remain = n->u.data.len - (b - n->u.data.data);
259     if (remain > 0)
260         map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
261
262     while (remain > 0 && i < GRS_MAX_WORD)
263     {
264         while (map && *map && **map == *CHR_SPACE)
265         {
266             remain = n->u.data.len - (b - n->u.data.data);
267             if (remain > 0)
268                 map = zebra_maps_input(p->zebra_maps, reg_type, &b, remain);
269             else
270                 map = 0;
271         }
272         if (!map)
273             break;
274
275         if (i && i < GRS_MAX_WORD)
276             buf[i++] = *CHR_SPACE;
277         while (map && *map && **map != *CHR_SPACE)
278         {
279             const char *cp = *map;
280
281             if (i >= GRS_MAX_WORD)
282                 break;
283             while (i < GRS_MAX_WORD && *cp)
284                 buf[i++] = *(cp++);
285             remain = n->u.data.len - (b - n->u.data.data);
286             if (remain > 0)
287                 map = zebra_maps_input (p->zebra_maps, reg_type, &b, remain);
288             else
289                 map = 0;
290         }
291     }
292     if (!i)
293         return;
294     buf[i] = '\0';
295     (*p->init)(&wrd);
296     
297     wrd.reg_type = reg_type;
298     wrd.seqno = seqno++;
299     wrd.string = buf;
300     wrd.attrSet = att->parent->ordinal;
301     wrd.attrUse = att->locals->local;
302     (*p->add)(&wrd);
303 }
304
305 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
306 {
307     for (; n; n = n->next)
308     {
309         if (p->flagShowRecords) /* display element description to user */
310         {
311             if (n->which == DATA1N_root)
312             {
313                 printf("%*s", level * 4, "");
314                 printf("Record type: '%s'\n", n->u.root.absyn->name);
315             }
316             else if (n->which == DATA1N_tag)
317             {
318                 data1_element *e;
319
320                 printf("%*s", level * 4, "");
321                 if (!(e = n->u.tag.element))
322                     printf("Local tag: '%s'\n", n->u.tag.tag);
323                 else
324                 {
325                     printf("Elm: '%s' ", e->name);
326                     if (e->tag)
327                     {
328                         data1_tag *t = e->tag;
329
330                         printf("TagNam: '%s' ", t->names->name);
331                         printf("(");
332                         if (t->tagset)
333                             printf("%s[%d],", t->tagset->name, t->tagset->type);
334                         else
335                             printf("?,");
336                         if (t->which == DATA1T_numeric)
337                             printf("%d)", t->value.numeric);
338                         else
339                             printf("'%s')", t->value.string);
340                     }
341                     printf("\n");
342                 }
343             }
344         }
345
346         if (n->child)
347             if (dumpkeys(n->child, p, level + 1) < 0)
348                 return -1;
349
350         if (n->which == DATA1N_data)
351         {
352             data1_node *par = get_parent_tag(p->dh, n);
353             data1_termlist *tlist = 0;
354             data1_datatype dtype = DATA1K_string;
355
356             if (p->flagShowRecords)
357             {
358                 printf("%*s", level * 4, "");
359                 printf("Data: ");
360                 if (n->u.data.len > 20)
361                     printf("'%.20s...'\n", n->u.data.data);
362                 else if (n->u.data.len > 0)
363                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
364                 else
365                     printf("NULL\n");
366             }
367
368             assert(par);
369
370             /*
371              * cycle up towards the root until we find a tag with an att..
372              * this has the effect of indexing locally defined tags with
373              * the attribute of their ancestor in the record.
374              */
375
376             while (!par->u.tag.element)
377                 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
378                     break;
379             if (!par || !(tlist = par->u.tag.element->termlists))
380                 continue;
381             if (par->u.tag.element->tag)
382                 dtype = par->u.tag.element->tag->kind;
383             for (; tlist; tlist = tlist->next)
384             {
385                 if (p->flagShowRecords)
386                 {
387                     printf("%*sIdx: [%s]", (level + 1) * 4, "",
388                            tlist->structure);
389                     printf("%s:%s [%d]\n",
390                            tlist->att->parent->name,
391                            tlist->att->name, tlist->att->value);
392                 }
393                 else
394                     if (zebra_maps_is_complete (p->zebra_maps,
395                                                 *tlist->structure))
396                         dumpkeys_complete_field(n, p, tlist->att,
397                                                 *tlist->structure);
398                     else
399                         dumpkeys_incomplete_field(n, p, tlist->att,
400                                                   *tlist->structure);
401             }
402         }
403         if (p->flagShowRecords && n->which == DATA1N_root)
404         {
405             printf("%*s-------------\n\n", level * 4, "");
406         }
407     }
408     return 0;
409 }
410
411 static int grs_extract(struct recExtractCtrl *p)
412 {
413     data1_node *n;
414     NMEM mem;
415     struct grs_read_info gri;
416     seqno = 0;
417
418     mem = nmem_create (); 
419     gri.readf = p->readf;
420     gri.seekf = p->seekf;
421     gri.tellf = p->tellf;
422     gri.endf = p->endf;
423     gri.fh = p->fh;
424     gri.offset = p->offset;
425     gri.mem = mem;
426     gri.dh = p->dh;
427
428     n = read_grs_type (&gri, p->subType);
429     if (!n)
430         return -1;
431     if (dumpkeys(n, p, 0) < 0)
432     {
433         data1_free_tree(p->dh, n);
434         return -2;
435     }
436     data1_free_tree(p->dh, n);
437     nmem_destroy(mem);
438     return 0;
439 }
440
441 /*
442  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
443  */
444 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
445 {
446     data1_esetname *eset;
447     Z_Espec1 *espec = 0;
448     Z_ElementSpec *p;
449
450     switch (c->which)
451     {
452         case Z_RecordComp_simple:
453             if (c->u.simple->which != Z_ElementSetNames_generic)
454                 return 26; /* only generic form supported. Fix this later */
455             if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
456                 c->u.simple->u.generic)))
457             {
458                 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
459                 return 25; /* invalid esetname */
460             }
461             logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
462                 c->u.simple->u.generic);
463             espec = eset->spec;
464             break;
465         case Z_RecordComp_complex:
466             if (c->u.complex->generic)
467             {
468                 /* insert check for schema */
469                 if ((p = c->u.complex->generic->elementSpec))
470                     switch (p->which)
471                     {
472                         case Z_ElementSpec_elementSetName:
473                             if (!(eset =
474                                   data1_getesetbyname(dh,
475                                                       n->u.root.absyn,
476                                                       p->u.elementSetName)))
477                             {
478                                 logf(LOG_LOG, "Unknown esetname '%s'",
479                                     p->u.elementSetName);
480                                 return 25; /* invalid esetname */
481                             }
482                             logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
483                                 p->u.elementSetName);
484                             espec = eset->spec;
485                             break;
486                         case Z_ElementSpec_externalSpec:
487                             if (p->u.externalSpec->which == Z_External_espec1)
488                             {
489                                 logf(LOG_DEBUG, "Got Espec-1");
490                                 espec = p->u.externalSpec-> u.espec1;
491                             }
492                             else
493                             {
494                                 logf(LOG_LOG, "Unknown external espec.");
495                                 return 25; /* bad. what is proper diagnostic? */
496                             }
497                             break;
498                     }
499             }
500             else
501                 return 26; /* fix */
502     }
503     if (espec)
504     {
505         logf (LOG_LOG, "Element: Espec-1 match");
506         return data1_doespec1(dh, n, espec);
507     }
508     else
509     {
510         logf (LOG_DEBUG, "Element: all match");
511         return -1;
512     }
513 }
514
515 static int grs_retrieve(struct recRetrieveCtrl *p)
516 {
517     data1_node *node = 0, *onode = 0;
518     data1_node *new;
519     data1_maptab *map;
520     int res, selected = 0;
521     NMEM mem;
522     struct grs_read_info gri;
523     
524     mem = nmem_create();
525     gri.readf = p->readf;
526     gri.seekf = p->seekf;
527     gri.tellf = p->tellf;
528     gri.endf = NULL;
529     gri.fh = p->fh;
530     gri.offset = 0;
531     gri.mem = mem;
532     gri.dh = p->dh;
533
534     logf (LOG_DEBUG, "grs_retrieve");
535     node = read_grs_type (&gri, p->subType);
536     if (!node)
537     {
538         p->diagnostic = 14;
539         nmem_destroy (mem);
540         return 0;
541     }
542     logf (LOG_DEBUG, "grs_retrieve: score");
543     if (p->score >= 0 && (new =
544                           data1_insert_taggeddata(p->dh, node,
545                                                   node, "rank",
546                                                   mem)))
547     {
548         new->u.data.what = DATA1I_num;
549         new->u.data.data = new->lbuf;
550         sprintf(new->u.data.data, "%d", p->score);
551         new->u.data.len = strlen(new->u.data.data);
552     }
553     logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
554     if ((new = data1_insert_taggeddata(p->dh, node, node,
555                                        "localControlNumber", mem)))
556     {
557         new->u.data.what = DATA1I_text;
558         new->u.data.data = new->lbuf;
559         sprintf(new->u.data.data, "%d", p->localno);
560         new->u.data.len = strlen(new->u.data.data);
561     }
562     logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
563     if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
564         node->u.root.absyn->reference != VAL_NONE)
565     {
566         oident oe;
567         Odr_oid *oid;
568         int oidtmp[OID_SIZE];
569
570         oe.proto = PROTO_Z3950;
571         oe.oclass = CLASS_SCHEMA;
572         oe.value = node->u.root.absyn->reference;
573
574         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
575         {
576             char tmp[128];
577             data1_handle dh = p->dh;
578             char *p = tmp;
579             int *ii;
580
581             for (ii = oid; *ii >= 0; ii++)
582             {
583                 if (p != tmp)
584                     *(p++) = '.';
585                 sprintf(p, "%d", *ii);
586                 p += strlen(p);
587             }
588             *(p++) = '\0';
589
590             if ((new = data1_insert_taggeddata(dh, node, node,
591                                                "schemaIdentifier", mem)))
592             {
593                 new->u.data.what = DATA1I_oid;
594                 new->u.data.data = nmem_malloc(mem, p - tmp);
595                 memcpy(new->u.data.data, tmp, p - tmp);
596                 new->u.data.len = p - tmp;
597             }
598         }
599     }
600
601     logf (LOG_DEBUG, "grs_retrieve: schema mapping");
602     /*
603      * Does the requested format match a known schema-mapping? (this reflects
604      * the overlap of schema and formatting which is inherent in the MARC
605      * family)
606      * NOTE: This should look at the schema-specification in the compspec
607      * as well.
608      */
609     for (map = node->u.root.absyn->maptabs; map; map = map->next)
610         if (map->target_absyn_ref == p->input_format)
611         {
612             onode = node;
613             if (!(node = data1_map_record(p->dh, onode, map, mem)))
614             {
615                 p->diagnostic = 14;
616                 nmem_destroy (mem);
617                 return 0;
618             }
619             break;
620         }
621
622     logf (LOG_DEBUG, "grs_retrieve: element spec");
623     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
624     {
625         p->diagnostic = res;
626         if (onode)
627             data1_free_tree(p->dh, onode);
628         data1_free_tree(p->dh, node);
629         nmem_destroy(mem);
630         return 0;
631     }
632     else if (p->comp && !res)
633         selected = 1;
634
635     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
636     switch (p->output_format = (p->input_format != VAL_NONE ?
637         p->input_format : VAL_SUTRS))
638     {
639         data1_marctab *marctab;
640         int dummy;
641
642         case VAL_GRS1:
643             dummy = 0;
644             if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
645                                               p->odr, &dummy)))
646                 p->diagnostic = 238; /* not available in requested syntax */
647             else
648                 p->rec_len = -1;
649             break;
650         case VAL_EXPLAIN:
651             if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
652                                                    p->odr)))
653                 p->diagnostic = 238;
654             else
655                 p->rec_len = -1;
656             break;
657         case VAL_SUMMARY:
658             if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
659                                                    p->odr)))
660                 p->diagnostic = 238;
661             else
662                 p->rec_len = -1;
663             break;
664         case VAL_SUTRS:
665             if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
666                 (int*)&p->rec_len)))
667                 p->diagnostic = 238;
668             break;
669         case VAL_SOIF:
670             if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
671                                                 (int*)&p->rec_len)))
672                 p->diagnostic = 238;
673             break;
674         default:
675             if (!node->u.root.absyn)
676             {
677                 p->diagnostic = 238;
678                 break;
679             }
680             for (marctab = node->u.root.absyn->marc; marctab;
681                 marctab = marctab->next)
682                 if (marctab->reference == p->input_format)
683                     break;
684             if (!marctab)
685             {
686                 p->diagnostic = 238;
687                 break;
688             }
689             if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
690                                                 selected,
691                                                 (int*)&p->rec_len)))
692             {
693                 p->diagnostic = 238;
694                 break;
695             }
696     }
697     if (node)
698         data1_free_tree(p->dh, node);
699     if (onode)
700         data1_free_tree(p->dh, onode);
701     nmem_destroy(mem);
702     return 0;
703 }
704
705 static struct recType grs_type =
706 {
707     "grs",
708     grs_init,
709     grs_extract,
710     grs_retrieve
711 };
712
713 RecType recTypeGrs = &grs_type;