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