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