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