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