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