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