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