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