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