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