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