Prepare for extra root tag node for better XML support
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  *
5  * $Id: recgrs.c,v 1.53 2002-07-03 10:05:19 adam Exp $
6  */
7
8 #include <stdio.h>
9 #include <assert.h>
10 #include <sys/types.h>
11 #ifndef WIN32
12 #include <unistd.h>
13 #endif
14
15 #include <yaz/log.h>
16 #include <yaz/oid.h>
17
18 #include <recctrl.h>
19 #include "grsread.h"
20
21 #define GRS_MAX_WORD 512
22
23 struct grs_handler {
24     RecTypeGrs type;
25     void *clientData;
26     int initFlag;
27     struct grs_handler *next;
28 };
29
30 struct grs_handlers {
31     struct grs_handler *handlers;
32 };
33
34 static int read_grs_type (struct grs_handlers *h,
35                           struct grs_read_info *p, const char *type,
36                           data1_node **root)
37 {
38     struct grs_handler *gh = h->handlers;
39     const char *cp = strchr (type, '.');
40
41     if (cp == NULL || cp == type)
42     {
43         cp = strlen(type) + type;
44         *p->type = 0;
45     }
46     else
47         strcpy (p->type, cp+1);
48     for (gh = h->handlers; gh; gh = gh->next)
49     {
50         if (!memcmp (type, gh->type->type, cp-type))
51         {
52             if (!gh->initFlag)
53             {
54                 gh->initFlag = 1;
55                 gh->clientData = (*gh->type->init)();
56             }
57             p->clientData = gh->clientData;
58             *root = (gh->type->read)(p);
59             gh->clientData = p->clientData;
60             return 0;
61         }
62     }
63     return 1;
64 }
65
66 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
67 {
68     struct grs_handler *gh = (struct grs_handler *) xmalloc (sizeof(*gh));
69     gh->next = h->handlers;
70     h->handlers = gh;
71     gh->initFlag = 0;
72     gh->clientData = 0;
73     gh->type = t;
74 }
75
76 static void *grs_init(RecType recType)
77 {
78     struct grs_handlers *h = (struct grs_handlers *) xmalloc (sizeof(*h));
79     h->handlers = 0;
80
81     grs_add_handler (h, recTypeGrs_sgml);
82     grs_add_handler (h, recTypeGrs_regx);
83 #if HAVE_TCL_H
84     grs_add_handler (h, recTypeGrs_tcl);
85 #endif
86     grs_add_handler (h, recTypeGrs_marc);
87 #if YAZ_HAVE_EXPAT
88     grs_add_handler (h, recTypeGrs_xml);
89 #endif
90     return h;
91 }
92
93 static void grs_destroy(void *clientData)
94 {
95     struct grs_handlers *h = (struct grs_handlers *) clientData;
96     struct grs_handler *gh = h->handlers, *gh_next;
97     while (gh)
98     {
99         gh_next = gh->next;
100         if (gh->initFlag)
101             (*gh->type->destroy)(gh->clientData);
102         xfree (gh);
103         gh = gh_next;
104     }
105     xfree (h);
106 }
107
108 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
109                          int level, RecWord *wrd, int use)
110 {
111     int i;
112     char tag_path_full[1024];
113     size_t flen = 0;
114     data1_node *nn;
115
116     switch (n->which)
117     {
118     case DATA1N_data:
119         wrd->reg_type = 'w';
120         wrd->string = n->u.data.data;
121         wrd->length = n->u.data.len;
122         wrd->attrSet = VAL_IDXPATH,
123         wrd->attrUse = use;
124         if (p->flagShowRecords)
125         {
126             printf("%*s data=", (level + 1) * 4, "");
127             for (i = 0; i<wrd->length && i < 8; i++)
128                 fputc (wrd->string[i], stdout);
129             printf("\n");
130         }
131         else
132         {
133             (*p->tokenAdd)(wrd);
134         }
135         break;
136     case DATA1N_tag:
137         for (nn = n; nn; nn = nn->parent)
138         {
139             if (nn->which == DATA1N_tag)
140             {
141                 size_t tlen = strlen(nn->u.tag.tag);
142                 if (tlen + flen > (sizeof(tag_path_full)-2))
143                     return;
144                 memcpy (tag_path_full + flen, nn->u.tag.tag, tlen);
145                 flen += tlen;
146                 tag_path_full[flen++] = '/';
147             }
148             else if (nn->which == DATA1N_root)
149                 break;
150         }
151         wrd->reg_type = '0';
152         wrd->string = tag_path_full;
153         wrd->length = flen;
154         wrd->attrSet = VAL_IDXPATH;
155         wrd->attrUse = use;
156         if (p->flagShowRecords)
157         {
158             printf("%*s tag=", (level + 1) * 4, "");
159             for (i = 0; i<wrd->length && i < 40; i++)
160                 fputc (wrd->string[i], stdout);
161             if (i == 40)
162                 printf (" ..");
163             printf("\n");
164         }
165         else
166         {
167             (*p->tokenAdd)(wrd);
168         }
169         break;
170     }
171 }
172
173 static void index_termlist (data1_node *par, data1_node *n,
174                             struct recExtractCtrl *p, int level, RecWord *wrd)
175 {
176     data1_termlist *tlist = 0;
177     data1_datatype dtype = DATA1K_string;
178     /*
179      * cycle up towards the root until we find a tag with an att..
180      * this has the effect of indexing locally defined tags with
181      * the attribute of their ancestor in the record.
182      */
183     
184     while (!par->u.tag.element)
185         if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
186             break;
187     if (!par || !(tlist = par->u.tag.element->termlists))
188         return;
189     if (par->u.tag.element->tag)
190         dtype = par->u.tag.element->tag->kind;
191     
192     for (; tlist; tlist = tlist->next)
193     {
194         char xattr[512];
195         /* consider source */
196         wrd->string = 0;
197         
198         if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
199         {
200             wrd->string = n->u.data.data;
201             wrd->length = n->u.data.len;
202         }
203         else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
204         {
205             wrd->string = n->u.tag.tag;
206             wrd->length = strlen(n->u.tag.tag);
207         }
208         else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
209             n->which == DATA1N_tag)
210         {
211             data1_xattr *p = n->u.tag.attributes;
212             while (p && strcmp (p->name, xattr))
213                 p = p->next;
214             if (p)
215             {
216                 wrd->string = p->value;
217                 wrd->length = strlen(p->value);
218             }
219         }
220         if (wrd->string)
221         {
222             if (p->flagShowRecords)
223             {
224                 int i;
225                 printf("%*sIdx: [%s]", (level + 1) * 4, "",
226                        tlist->structure);
227                 printf("%s:%s [%d] %s",
228                        tlist->att->parent->name,
229                        tlist->att->name, tlist->att->value,
230                        tlist->source);
231                 printf (" data=\"");
232                 for (i = 0; i<wrd->length && i < 8; i++)
233                     fputc (wrd->string[i], stdout);
234                 fputc ('"', stdout);
235                 if (wrd->length > 8)
236                     printf (" ...");
237                 fputc ('\n', stdout);
238             }
239             else
240             {
241                 wrd->reg_type = *tlist->structure;
242                 wrd->attrSet = (int) (tlist->att->parent->reference);
243                 wrd->attrUse = tlist->att->locals->local;
244                 (*p->tokenAdd)(wrd);
245             }
246         }
247     }
248 }
249
250 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
251                     RecWord *wrd)
252 {
253     for (; n; n = n->next)
254     {
255         if (p->flagShowRecords) /* display element description to user */
256         {
257             if (n->which == DATA1N_root)
258             {
259                 printf("%*s", level * 4, "");
260                 printf("Record type: '%s'\n", n->u.root.type);
261             }
262             else if (n->which == DATA1N_tag)
263             {
264                 data1_element *e;
265
266                 printf("%*s", level * 4, "");
267                 if (!(e = n->u.tag.element))
268                     printf("Local tag: '%s'\n", n->u.tag.tag);
269                 else
270                 {
271                     printf("Elm: '%s' ", e->name);
272                     if (e->tag)
273                     {
274                         data1_tag *t = e->tag;
275
276                         printf("TagNam: '%s' ", t->names->name);
277                         printf("(");
278                         if (t->tagset)
279                             printf("%s[%d],", t->tagset->name, t->tagset->type);
280                         else
281                             printf("?,");
282                         if (t->which == DATA1T_numeric)
283                             printf("%d)", t->value.numeric);
284                         else
285                             printf("'%s')", t->value.string);
286                     }
287                     printf("\n");
288                 }
289             }
290         }
291
292         if (n->which == DATA1N_tag)
293         {
294             index_termlist (n, n, p, level, wrd);
295             /* index start tag */
296             if (!n->root->u.root.absyn)
297                 index_xpath (n, p, level, wrd, 1);
298         }
299
300         if (n->child)
301             if (dumpkeys(n->child, p, level + 1, wrd) < 0)
302                 return -1;
303
304
305         if (n->which == DATA1N_data)
306         {
307             data1_node *par = get_parent_tag(p->dh, n);
308
309             if (p->flagShowRecords)
310             {
311                 printf("%*s", level * 4, "");
312                 printf("Data: ");
313                 if (n->u.data.len > 32)
314                     printf("'%.24s ... %.6s'\n", n->u.data.data,
315                            n->u.data.data + n->u.data.len-6);
316                 else if (n->u.data.len > 0)
317                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
318                 else
319                     printf("NULL\n");
320             }
321
322             if (par)
323                 index_termlist (par, n, p, level, wrd);
324             if (!n->root->u.root.absyn)
325                 index_xpath (n, p, level, wrd, 1016);
326
327         }
328
329         if (n->which == DATA1N_tag)
330         {
331             /* index end tag */
332             if (!n->root->u.root.absyn)
333                 index_xpath (n, p, level, wrd, 2);
334         }
335
336
337         if (p->flagShowRecords && n->which == DATA1N_root)
338         {
339             printf("%*s-------------\n\n", level * 4, "");
340         }
341     }
342     return 0;
343 }
344
345 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
346 {
347     oident oe;
348     int oidtmp[OID_SIZE];
349     RecWord wrd;
350
351     oe.proto = PROTO_Z3950;
352     oe.oclass = CLASS_SCHEMA;
353     if (n->u.root.absyn)
354     {
355         oe.value = n->u.root.absyn->reference;
356         
357         if ((oid_ent_to_oid (&oe, oidtmp)))
358             (*p->schemaAdd)(p, oidtmp);
359     }
360     (*p->init)(p, &wrd);
361     return dumpkeys(n, p, 0, &wrd);
362 }
363
364 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
365                            NMEM mem)
366 {
367     data1_node *n;
368     struct grs_read_info gri;
369     oident oe;
370     int oidtmp[OID_SIZE];
371     RecWord wrd;
372
373     gri.readf = p->readf;
374     gri.seekf = p->seekf;
375     gri.tellf = p->tellf;
376     gri.endf = p->endf;
377     gri.fh = p->fh;
378     gri.offset = p->offset;
379     gri.mem = mem;
380     gri.dh = p->dh;
381
382     if (read_grs_type (h, &gri, p->subType, &n))
383         return RECCTRL_EXTRACT_ERROR;
384     if (!n)
385         return RECCTRL_EXTRACT_EOF;
386     oe.proto = PROTO_Z3950;
387     oe.oclass = CLASS_SCHEMA;
388 #if 0
389     if (!n->u.root.absyn)
390         return RECCTRL_EXTRACT_ERROR;
391 #endif
392     if (n->u.root.absyn)
393     {
394         oe.value = n->u.root.absyn->reference;
395         if ((oid_ent_to_oid (&oe, oidtmp)))
396             (*p->schemaAdd)(p, oidtmp);
397     }
398 #if 0
399     data1_pr_tree (p->dh, n, stdout);
400 #endif
401     (*p->init)(p, &wrd);
402     if (dumpkeys(n, p, 0, &wrd) < 0)
403     {
404         data1_free_tree(p->dh, n);
405         return RECCTRL_EXTRACT_ERROR;
406     }
407     data1_free_tree(p->dh, n);
408     return RECCTRL_EXTRACT_OK;
409 }
410
411 static int grs_extract(void *clientData, struct recExtractCtrl *p)
412 {
413     int ret;
414     NMEM mem = nmem_create ();
415     struct grs_handlers *h = (struct grs_handlers *) clientData;
416
417     ret = grs_extract_sub(h, p, mem);
418     nmem_destroy(mem);
419     return ret;
420 }
421
422 /*
423  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
424  */
425 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
426 {
427     data1_esetname *eset;
428     Z_Espec1 *espec = 0;
429     Z_ElementSpec *p;
430
431     switch (c->which)
432     {
433     case Z_RecordComp_simple:
434         if (c->u.simple->which != Z_ElementSetNames_generic)
435             return 26; /* only generic form supported. Fix this later */
436         if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
437                                          c->u.simple->u.generic)))
438         {
439             logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
440             return 25; /* invalid esetname */
441         }
442         logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
443              c->u.simple->u.generic);
444         espec = eset->spec;
445         break;
446     case Z_RecordComp_complex:
447         if (c->u.complex->generic)
448         {
449             /* insert check for schema */
450             if ((p = c->u.complex->generic->elementSpec))
451             {
452                 switch (p->which)
453                 {
454                 case Z_ElementSpec_elementSetName:
455                     if (!(eset =
456                           data1_getesetbyname(dh, n->u.root.absyn,
457                                               p->u.elementSetName)))
458                     {
459                         logf(LOG_LOG, "Unknown esetname '%s'",
460                              p->u.elementSetName);
461                         return 25; /* invalid esetname */
462                     }
463                     logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
464                          p->u.elementSetName);
465                     espec = eset->spec;
466                     break;
467                 case Z_ElementSpec_externalSpec:
468                     if (p->u.externalSpec->which == Z_External_espec1)
469                     {
470                         logf(LOG_DEBUG, "Got Espec-1");
471                         espec = p->u.externalSpec-> u.espec1;
472                     }
473                     else
474                     {
475                         logf(LOG_LOG, "Unknown external espec.");
476                         return 25; /* bad. what is proper diagnostic? */
477                     }
478                     break;
479                 }
480             }
481         }
482         else
483             return 26; /* fix */
484     }
485     if (espec)
486     {
487         logf (LOG_DEBUG, "Element: Espec-1 match");
488         return data1_doespec1(dh, n, espec);
489     }
490     else
491     {
492         logf (LOG_DEBUG, "Element: all match");
493         return -1;
494     }
495 }
496
497 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
498                               NMEM mem)
499 {
500     const char *idzebra_ns[7];
501
502     idzebra_ns[0] = "xmlns:idzebra";
503     idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
504     idzebra_ns[2] = 0;
505
506     data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
507
508     data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
509                            mem);
510     if (p->score != -1)
511         data1_mk_tag_data_int (p->dh, top, "idzebra:score",
512                                p->score, mem);
513     
514     data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
515                            mem);
516     if (p->fname)
517         data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
518                                p->fname, mem);
519 }
520
521 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
522 {
523     data1_node *node = 0, *onode = 0, *top;
524     data1_node *dnew;
525     data1_maptab *map;
526     int res, selected = 0;
527     NMEM mem;
528     struct grs_read_info gri;
529     char *tagname;
530     struct grs_handlers *h = (struct grs_handlers *) clientData;
531     int requested_schema = VAL_NONE;
532     data1_marctab *marctab;
533     int dummy;
534     
535     mem = nmem_create();
536     gri.readf = p->readf;
537     gri.seekf = p->seekf;
538     gri.tellf = p->tellf;
539     gri.endf = NULL;
540     gri.fh = p->fh;
541     gri.offset = 0;
542     gri.mem = mem;
543     gri.dh = p->dh;
544
545     logf (LOG_DEBUG, "grs_retrieve");
546     if (read_grs_type (h, &gri, p->subType, &node))
547     {
548         p->diagnostic = 14;
549         nmem_destroy (mem);
550         return 0;
551     }
552     if (!node)
553     {
554         p->diagnostic = 14;
555         nmem_destroy (mem);
556         return 0;
557     }
558 #if 0
559     data1_pr_tree (p->dh, node, stdout);
560 #endif
561     top = data1_get_root_tag (p->dh, node);
562
563     logf (LOG_DEBUG, "grs_retrieve: size");
564     if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
565     {
566         dnew->u.data.what = DATA1I_text;
567         dnew->u.data.data = dnew->lbuf;
568         sprintf(dnew->u.data.data, "%d", p->recordSize);
569         dnew->u.data.len = strlen(dnew->u.data.data);
570     }
571
572     tagname = res_get_def(p->res, "tagrank", "rank");
573     if (strcmp(tagname, "0") && p->score >= 0 &&
574         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
575     {
576         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
577         dnew->u.data.what = DATA1I_num;
578         dnew->u.data.data = dnew->lbuf;
579         sprintf(dnew->u.data.data, "%d", p->score);
580         dnew->u.data.len = strlen(dnew->u.data.data);
581     }
582
583     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
584     if (strcmp(tagname, "0") && p->localno > 0 &&
585          (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
586     {
587         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
588         dnew->u.data.what = DATA1I_text;
589         dnew->u.data.data = dnew->lbuf;
590
591         sprintf(dnew->u.data.data, "%d", p->localno);
592         dnew->u.data.len = strlen(dnew->u.data.data);
593     }
594 #if 0
595     data1_pr_tree (p->dh, node, stdout);
596 #endif
597     if (p->comp && p->comp->which == Z_RecordComp_complex &&
598         p->comp->u.complex->generic &&
599         p->comp->u.complex->generic->schema)
600     {
601         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
602         if (oe)
603             requested_schema = oe->value;
604     }
605
606     /* If schema has been specified, map if possible, then check that
607      * we got the right one 
608      */
609     if (requested_schema != VAL_NONE)
610     {
611         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
612         for (map = node->u.root.absyn->maptabs; map; map = map->next)
613         {
614             if (map->target_absyn_ref == requested_schema)
615             {
616                 onode = node;
617                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
618                 {
619                     p->diagnostic = 14;
620                     nmem_destroy (mem);
621                     return 0;
622                 }
623                 break;
624             }
625         }
626         if (node->u.root.absyn &&
627             requested_schema != node->u.root.absyn->reference)
628         {
629             p->diagnostic = 238;
630             nmem_destroy (mem);
631             return 0;
632         }
633     }
634     /*
635      * Does the requested format match a known syntax-mapping? (this reflects
636      * the overlap of schema and formatting which is inherent in the MARC
637      * family)
638      */
639     yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
640     if (node->u.root.absyn)
641         for (map = node->u.root.absyn->maptabs; map; map = map->next)
642         {
643             if (map->target_absyn_ref == p->input_format)
644             {
645                 onode = node;
646                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
647                 {
648                     p->diagnostic = 14;
649                     nmem_destroy (mem);
650                     return 0;
651                 }
652                 break;
653             }
654         }
655     yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
656     if (node->u.root.absyn &&
657         node->u.root.absyn->reference != VAL_NONE &&
658         p->input_format == VAL_GRS1)
659     {
660         oident oe;
661         Odr_oid *oid;
662         int oidtmp[OID_SIZE];
663         
664         oe.proto = PROTO_Z3950;
665         oe.oclass = CLASS_SCHEMA;
666         oe.value = node->u.root.absyn->reference;
667         
668         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
669         {
670             char tmp[128];
671             data1_handle dh = p->dh;
672             char *p = tmp;
673             int *ii;
674             
675             for (ii = oid; *ii >= 0; ii++)
676             {
677                 if (p != tmp)
678                         *(p++) = '.';
679                 sprintf(p, "%d", *ii);
680                 p += strlen(p);
681             }
682             *(p++) = '\0';
683                 
684             if ((dnew = data1_mk_tag_data_wd(dh, node, 
685                                              "schemaIdentifier", mem)))
686             {
687                 dnew->u.data.what = DATA1I_oid;
688                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
689                 memcpy(dnew->u.data.data, tmp, p - tmp);
690                 dnew->u.data.len = p - tmp;
691             }
692         }
693     }
694
695     logf (LOG_DEBUG, "grs_retrieve: element spec");
696     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
697     {
698         p->diagnostic = res;
699         if (onode)
700             data1_free_tree(p->dh, onode);
701         data1_free_tree(p->dh, node);
702         nmem_destroy(mem);
703         return 0;
704     }
705     else if (p->comp && !res)
706         selected = 1;
707
708 #if 0
709     data1_pr_tree (p->dh, node, stdout);
710 #endif
711     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
712     switch (p->output_format = (p->input_format != VAL_NONE ?
713                                 p->input_format : VAL_SUTRS))
714     {
715         
716     case VAL_TEXT_XML:
717         add_idzebra_info (p, top, mem);
718
719         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
720                                               &p->rec_len)))
721             p->diagnostic = 238;
722         else
723         {
724             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
725             memcpy (new_buf, p->rec_buf, p->rec_len);
726             p->rec_buf = new_buf;
727         }
728         break;
729     case VAL_GRS1:
730         dummy = 0;
731         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
732                                           p->odr, &dummy)))
733             p->diagnostic = 238; /* not available in requested syntax */
734         else
735             p->rec_len = (size_t) (-1);
736         break;
737     case VAL_EXPLAIN:
738         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
739                                                p->odr)))
740             p->diagnostic = 238;
741         else
742             p->rec_len = (size_t) (-1);
743         break;
744     case VAL_SUMMARY:
745         if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
746                                                p->odr)))
747             p->diagnostic = 238;
748         else
749             p->rec_len = (size_t) (-1);
750         break;
751     case VAL_SUTRS:
752         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
753                                            &p->rec_len)))
754             p->diagnostic = 238;
755         else
756         {
757             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
758             memcpy (new_buf, p->rec_buf, p->rec_len);
759             p->rec_buf = new_buf;
760         }
761         break;
762     case VAL_SOIF:
763         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
764                                             &p->rec_len)))
765             p->diagnostic = 238;
766         else
767         {
768             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
769             memcpy (new_buf, p->rec_buf, p->rec_len);
770             p->rec_buf = new_buf;
771         }
772         break;
773     default:
774         if (!node->u.root.absyn)
775         {
776             p->diagnostic = 238;
777             break;
778         }
779         for (marctab = node->u.root.absyn->marc; marctab;
780              marctab = marctab->next)
781             if (marctab->reference == p->input_format)
782                 break;
783         if (!marctab)
784         {
785             p->diagnostic = 238;
786             break;
787         }
788         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
789                                         selected, &p->rec_len)))
790             p->diagnostic = 238;
791         else
792         {
793             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
794             memcpy (new_buf, p->rec_buf, p->rec_len);
795                 p->rec_buf = new_buf;
796         }
797     }
798     if (node)
799         data1_free_tree(p->dh, node);
800     if (onode)
801         data1_free_tree(p->dh, onode);
802     nmem_destroy(mem);
803     return 0;
804 }
805
806 static struct recType grs_type =
807 {
808     "grs",
809     grs_init,
810     grs_destroy,
811     grs_extract,
812     grs_retrieve
813 };
814
815 RecType recTypeGrs = &grs_type;