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