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