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