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