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