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