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