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