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