Missing grs_add_handler for Perl
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /* $Id: recgrs.c,v 1.69 2002-11-15 21:57:41 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
3    Index Data Aps
4
5 This file is part of the Zebra server.
6
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra.  If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA.
21 */
22
23 #include <stdio.h>
24 #include <assert.h>
25 #include <sys/types.h>
26 #ifndef WIN32
27 #include <unistd.h>
28 #endif
29
30 #include <yaz/log.h>
31 #include <yaz/oid.h>
32
33 #include <recctrl.h>
34 #include "grsread.h"
35
36 #define GRS_MAX_WORD 512
37
38 struct grs_handler {
39     RecTypeGrs type;
40     void *clientData;
41     int initFlag;
42     struct grs_handler *next;
43 };
44
45 struct grs_handlers {
46     struct grs_handler *handlers;
47 };
48
49 static int read_grs_type (struct grs_handlers *h,
50                           struct grs_read_info *p, const char *type,
51                           data1_node **root)
52 {
53     struct grs_handler *gh = h->handlers;
54     const char *cp = strchr (type, '.');
55
56     if (cp == NULL || cp == type)
57     {
58         cp = strlen(type) + type;
59         *p->type = 0;
60     }
61     else
62         strcpy (p->type, cp+1);
63     for (gh = h->handlers; gh; gh = gh->next)
64     {
65         if (!memcmp (type, gh->type->type, cp-type))
66         {
67             if (!gh->initFlag)
68             {
69                 gh->initFlag = 1;
70                 gh->clientData = (*gh->type->init)();
71             }
72             p->clientData = gh->clientData;
73             *root = (gh->type->read)(p);
74             gh->clientData = p->clientData;
75             return 0;
76         }
77     }
78     return 1;
79 }
80
81 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
82 {
83     struct grs_handler *gh = (struct grs_handler *) xmalloc (sizeof(*gh));
84     gh->next = h->handlers;
85     h->handlers = gh;
86     gh->initFlag = 0;
87     gh->clientData = 0;
88     gh->type = t;
89 }
90
91 static void *grs_init(RecType recType)
92 {
93     struct grs_handlers *h = (struct grs_handlers *) xmalloc (sizeof(*h));
94     h->handlers = 0;
95
96     grs_add_handler (h, recTypeGrs_sgml);
97     grs_add_handler (h, recTypeGrs_regx);
98 #if HAVE_TCL_H
99     grs_add_handler (h, recTypeGrs_tcl);
100 #endif
101     grs_add_handler (h, recTypeGrs_marc);
102 #if HAVE_EXPAT_H
103     grs_add_handler (h, recTypeGrs_xml);
104 #endif
105 #if HAVE_PERL
106     grs_add_handler (h, recTypeGrs_perl);
107 #endif
108     return h;
109 }
110
111 static void grs_destroy(void *clientData)
112 {
113     struct grs_handlers *h = (struct grs_handlers *) clientData;
114     struct grs_handler *gh = h->handlers, *gh_next;
115     while (gh)
116     {
117         gh_next = gh->next;
118         if (gh->initFlag)
119             (*gh->type->destroy)(gh->clientData);
120         xfree (gh);
121         gh = gh_next;
122     }
123     xfree (h);
124 }
125
126 /* use
127      1   start element (tag)
128      2   end element
129      3   start attr (and attr-exact)
130      4   end attr
131
132   1016   cdata
133   1015   attr data
134 */
135
136 static void index_xpath (data1_node *n, struct recExtractCtrl *p,
137                          int level, RecWord *wrd, int use)
138 {
139     int i;
140     char tag_path_full[1024];
141     size_t flen = 0;
142     data1_node *nn;
143
144     switch (n->which)
145     {
146     case DATA1N_data:
147         wrd->reg_type = 'w';
148         wrd->string = n->u.data.data;
149         wrd->length = n->u.data.len;
150         wrd->attrSet = VAL_IDXPATH,
151         wrd->attrUse = use;
152         if (p->flagShowRecords)
153         {
154             printf("%*s data=", (level + 1) * 4, "");
155             for (i = 0; i<wrd->length && i < 8; i++)
156                 fputc (wrd->string[i], stdout);
157             printf("\n");
158         }
159         else
160         {
161             (*p->tokenAdd)(wrd);
162         }
163         break;
164     case DATA1N_tag:
165         for (nn = n; nn; nn = nn->parent)
166         {
167             if (nn->which == DATA1N_tag)
168             {
169                 size_t tlen = strlen(nn->u.tag.tag);
170                 if (tlen + flen > (sizeof(tag_path_full)-2))
171                     return;
172                 memcpy (tag_path_full + flen, nn->u.tag.tag, tlen);
173                 flen += tlen;
174                 tag_path_full[flen++] = '/';
175             }
176             else if (nn->which == DATA1N_root)
177                 break;
178         }
179         wrd->reg_type = '0';
180         wrd->string = tag_path_full;
181         wrd->length = flen;
182         wrd->attrSet = VAL_IDXPATH;
183         wrd->attrUse = use;
184         if (p->flagShowRecords)
185         {
186             printf("%*s tag=", (level + 1) * 4, "");
187             for (i = 0; i<wrd->length && i < 40; i++)
188                 fputc (wrd->string[i], stdout);
189             if (i == 40)
190                 printf (" ..");
191             printf("\n");
192         }
193         else
194         {
195             data1_xattr *xp;
196             (*p->tokenAdd)(wrd);   /* index element pag (AKA tag path) */
197             if (use == 1)
198             {
199                 for (xp = n->u.tag.attributes; xp; xp = xp->next)
200                 {
201                     char comb[512];
202                     /* attribute  (no value) */
203                     wrd->reg_type = '0';
204                     wrd->attrUse = 3;
205                     wrd->string = xp->name;
206                     wrd->length = strlen(xp->name);
207                     
208                     wrd->seqno--;
209                     (*p->tokenAdd)(wrd);
210
211                     if (xp->value &&
212                         strlen(xp->name) + strlen(xp->value) < sizeof(comb)-2)
213                     {
214                         /* attribute value exact */
215                         strcpy (comb, xp->name);
216                         strcat (comb, "=");
217                         strcat (comb, xp->value);
218                         
219                         wrd->attrUse = 3;
220                         wrd->reg_type = '0';
221                         wrd->string = comb;
222                         wrd->length = strlen(comb);
223                         wrd->seqno--;
224                         
225                         (*p->tokenAdd)(wrd);
226                     }
227                 }                
228                 for (xp = n->u.tag.attributes; xp; xp = xp->next)
229                 {
230                     char attr_tag_path_full[1024];
231                     int int_len = flen;
232                     
233                     sprintf (attr_tag_path_full, "@%s/%.*s",
234                              xp->name, int_len, tag_path_full);
235
236                     wrd->reg_type = '0';
237                     wrd->attrUse = 1;
238                     wrd->string = attr_tag_path_full;
239                     wrd->length = strlen(attr_tag_path_full);
240                     (*p->tokenAdd)(wrd);
241                     
242                     if (xp->value)
243                     {
244                         wrd->attrUse = 1015;
245                         wrd->reg_type = 'w';
246                         wrd->string = xp->value;
247                         wrd->length = strlen(xp->value);
248                         (*p->tokenAdd)(wrd);
249                     }
250                     
251                     wrd->reg_type = '0';
252                     wrd->attrUse = 2;
253                     wrd->string = attr_tag_path_full;
254                     wrd->length = strlen(attr_tag_path_full);
255                     (*p->tokenAdd)(wrd);
256                 }
257             }
258         }
259     }
260 }
261
262 static void index_termlist (data1_node *par, data1_node *n,
263                             struct recExtractCtrl *p, int level, RecWord *wrd)
264 {
265     data1_termlist *tlist = 0;
266     data1_datatype dtype = DATA1K_string;
267     /*
268      * cycle up towards the root until we find a tag with an att..
269      * this has the effect of indexing locally defined tags with
270      * the attribute of their ancestor in the record.
271      */
272     
273     while (!par->u.tag.element)
274         if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
275             break;
276     if (!par || !(tlist = par->u.tag.element->termlists))
277         return;
278     if (par->u.tag.element->tag)
279         dtype = par->u.tag.element->tag->kind;
280     
281     for (; tlist; tlist = tlist->next)
282     {
283         char xattr[512];
284         /* consider source */
285         wrd->string = 0;
286         
287         if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
288         {
289             wrd->string = n->u.data.data;
290             wrd->length = n->u.data.len;
291         }
292         else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
293         {
294             wrd->string = n->u.tag.tag;
295             wrd->length = strlen(n->u.tag.tag);
296         }
297         else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
298             n->which == DATA1N_tag)
299         {
300             data1_xattr *p = n->u.tag.attributes;
301             while (p && strcmp (p->name, xattr))
302                 p = p->next;
303             if (p)
304             {
305                 wrd->string = p->value;
306                 wrd->length = strlen(p->value);
307             }
308         }
309         if (wrd->string)
310         {
311             if (p->flagShowRecords)
312             {
313                 int i;
314                 printf("%*sIdx: [%s]", (level + 1) * 4, "",
315                        tlist->structure);
316                 printf("%s:%s [%d] %s",
317                        tlist->att->parent->name,
318                        tlist->att->name, tlist->att->value,
319                        tlist->source);
320                 printf (" data=\"");
321                 for (i = 0; i<wrd->length && i < 8; i++)
322                     fputc (wrd->string[i], stdout);
323                 fputc ('"', stdout);
324                 if (wrd->length > 8)
325                     printf (" ...");
326                 fputc ('\n', stdout);
327             }
328             else
329             {
330                 wrd->reg_type = *tlist->structure;
331                 wrd->attrSet = (int) (tlist->att->parent->reference);
332                 wrd->attrUse = tlist->att->locals->local;
333                 (*p->tokenAdd)(wrd);
334             }
335         }
336     }
337 }
338
339 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level,
340                     RecWord *wrd)
341 {
342     for (; n; n = n->next)
343     {
344         if (p->flagShowRecords) /* display element description to user */
345         {
346             if (n->which == DATA1N_root)
347             {
348                 printf("%*s", level * 4, "");
349                 printf("Record type: '%s'\n", n->u.root.type);
350             }
351             else if (n->which == DATA1N_tag)
352             {
353                 data1_element *e;
354
355                 printf("%*s", level * 4, "");
356                 if (!(e = n->u.tag.element))
357                     printf("Local tag: '%s'\n", n->u.tag.tag);
358                 else
359                 {
360                     printf("Elm: '%s' ", e->name);
361                     if (e->tag)
362                     {
363                         data1_tag *t = e->tag;
364
365                         printf("TagNam: '%s' ", t->names->name);
366                         printf("(");
367                         if (t->tagset)
368                             printf("%s[%d],", t->tagset->name, t->tagset->type);
369                         else
370                             printf("?,");
371                         if (t->which == DATA1T_numeric)
372                             printf("%d)", t->value.numeric);
373                         else
374                             printf("'%s')", t->value.string);
375                     }
376                     printf("\n");
377                 }
378             }
379         }
380
381         if (n->which == DATA1N_tag)
382         {
383             index_termlist (n, n, p, level, wrd);
384             /* index start tag */
385             assert (n->root->u.root.absyn);
386             
387             if (!n->root->u.root.absyn)
388                 index_xpath (n, p, level, wrd, 1);
389             else if (n->root->u.root.absyn->enable_xpath_indexing)
390                 index_xpath (n, p, level, wrd, 1);
391         }
392
393         if (n->child)
394             if (dumpkeys(n->child, p, level + 1, wrd) < 0)
395                 return -1;
396
397
398         if (n->which == DATA1N_data)
399         {
400             data1_node *par = get_parent_tag(p->dh, n);
401
402             if (p->flagShowRecords)
403             {
404                 printf("%*s", level * 4, "");
405                 printf("Data: ");
406                 if (n->u.data.len > 256)
407                     printf("'%.240s ... %.6s'\n", n->u.data.data,
408                            n->u.data.data + n->u.data.len-6);
409                 else if (n->u.data.len > 0)
410                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
411                 else
412                     printf("NULL\n");
413             }
414
415             if (par)
416                 index_termlist (par, n, p, level, wrd);
417             if (!n->root->u.root.absyn)
418                 index_xpath (n, p, level, wrd, 1016);
419             else if (n->root->u.root.absyn->enable_xpath_indexing)
420                 index_xpath (n, p, level, wrd, 1016);
421         }
422
423         if (n->which == DATA1N_tag)
424         {
425             /* index end tag */
426             if (!n->root->u.root.absyn)
427                 index_xpath (n, p, level, wrd, 2);
428             else if (n->root->u.root.absyn->enable_xpath_indexing)
429                 index_xpath (n, p, level, wrd, 2);
430         }
431
432         if (p->flagShowRecords && n->which == DATA1N_root)
433         {
434             printf("%*s-------------\n\n", level * 4, "");
435         }
436     }
437     return 0;
438 }
439
440 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
441 {
442     oident oe;
443     int oidtmp[OID_SIZE];
444     RecWord wrd;
445
446     oe.proto = PROTO_Z3950;
447     oe.oclass = CLASS_SCHEMA;
448     if (n->u.root.absyn)
449     {
450         oe.value = n->u.root.absyn->reference;
451         
452         if ((oid_ent_to_oid (&oe, oidtmp)))
453             (*p->schemaAdd)(p, oidtmp);
454     }
455     (*p->init)(p, &wrd);
456
457     return dumpkeys(n, p, 0, &wrd);
458 }
459
460 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
461                            NMEM mem)
462 {
463     data1_node *n;
464     struct grs_read_info gri;
465     oident oe;
466     int oidtmp[OID_SIZE];
467     RecWord wrd;
468
469     gri.readf = p->readf;
470     gri.seekf = p->seekf;
471     gri.tellf = p->tellf;
472     gri.endf = p->endf;
473     gri.fh = p->fh;
474     gri.offset = p->offset;
475     gri.mem = mem;
476     gri.dh = p->dh;
477
478     if (read_grs_type (h, &gri, p->subType, &n))
479         return RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER;
480     if (!n)
481         return RECCTRL_EXTRACT_EOF;
482     oe.proto = PROTO_Z3950;
483     oe.oclass = CLASS_SCHEMA;
484 #if 0
485     if (!n->u.root.absyn)
486         return RECCTRL_EXTRACT_ERROR;
487 #endif
488     if (n->u.root.absyn)
489     {
490         oe.value = n->u.root.absyn->reference;
491         if ((oid_ent_to_oid (&oe, oidtmp)))
492             (*p->schemaAdd)(p, oidtmp);
493     }
494
495     /* ensure our data1 tree is UTF-8 */
496     data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
497
498 #if 0
499     data1_pr_tree (p->dh, n, stdout);
500 #endif
501
502     (*p->init)(p, &wrd);
503     if (dumpkeys(n, p, 0, &wrd) < 0)
504     {
505         data1_free_tree(p->dh, n);
506         return RECCTRL_EXTRACT_ERROR_GENERIC;
507     }
508     data1_free_tree(p->dh, n);
509     return RECCTRL_EXTRACT_OK;
510 }
511
512 static int grs_extract(void *clientData, struct recExtractCtrl *p)
513 {
514     int ret;
515     NMEM mem = nmem_create ();
516     struct grs_handlers *h = (struct grs_handlers *) clientData;
517
518     ret = grs_extract_sub(h, p, mem);
519     nmem_destroy(mem);
520     return ret;
521 }
522
523 /*
524  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
525  */
526 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
527 {
528     data1_esetname *eset;
529     Z_Espec1 *espec = 0;
530     Z_ElementSpec *p;
531
532     switch (c->which)
533     {
534     case Z_RecordComp_simple:
535         if (c->u.simple->which != Z_ElementSetNames_generic)
536             return 26; /* only generic form supported. Fix this later */
537         if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
538                                          c->u.simple->u.generic)))
539         {
540             logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
541             return 25; /* invalid esetname */
542         }
543         logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
544              c->u.simple->u.generic);
545         espec = eset->spec;
546         break;
547     case Z_RecordComp_complex:
548         if (c->u.complex->generic)
549         {
550             /* insert check for schema */
551             if ((p = c->u.complex->generic->elementSpec))
552             {
553                 switch (p->which)
554                 {
555                 case Z_ElementSpec_elementSetName:
556                     if (!(eset =
557                           data1_getesetbyname(dh, n->u.root.absyn,
558                                               p->u.elementSetName)))
559                     {
560                         logf(LOG_LOG, "Unknown esetname '%s'",
561                              p->u.elementSetName);
562                         return 25; /* invalid esetname */
563                     }
564                     logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
565                          p->u.elementSetName);
566                     espec = eset->spec;
567                     break;
568                 case Z_ElementSpec_externalSpec:
569                     if (p->u.externalSpec->which == Z_External_espec1)
570                     {
571                         logf(LOG_DEBUG, "Got Espec-1");
572                         espec = p->u.externalSpec-> u.espec1;
573                     }
574                     else
575                     {
576                         logf(LOG_LOG, "Unknown external espec.");
577                         return 25; /* bad. what is proper diagnostic? */
578                     }
579                     break;
580                 }
581             }
582         }
583         else
584             return 26; /* fix */
585     }
586     if (espec)
587     {
588         logf (LOG_DEBUG, "Element: Espec-1 match");
589         return data1_doespec1(dh, n, espec);
590     }
591     else
592     {
593         logf (LOG_DEBUG, "Element: all match");
594         return -1;
595     }
596 }
597
598 /* Add Zebra info in separate namespace ...
599         <root 
600          ...
601          <metadata xmlns="http://www.indexdata.dk/zebra/">
602           <size>359</size>
603           <localnumber>447</localnumber>
604           <filename>records/genera.xml</filename>
605          </metadata>
606         </root>
607 */
608
609 static void zebra_xml_metadata (struct recRetrieveCtrl *p, data1_node *top,
610                                 NMEM mem)
611 {
612     const char *idzebra_ns[3];
613     const char *i2 = "\n  ";
614     const char *i4 = "\n    ";
615     data1_node *n;
616
617     idzebra_ns[0] = "xmlns";
618     idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
619     idzebra_ns[2] = 0;
620
621     data1_mk_text (p->dh, mem, i2, top);
622
623     n = data1_mk_tag (p->dh, mem, "idzebra", idzebra_ns, top);
624
625     data1_mk_text (p->dh, mem, "\n", top);
626
627     data1_mk_text (p->dh, mem, i4, n);
628     
629     data1_mk_tag_data_int (p->dh, n, "size", p->recordSize, mem);
630
631     if (p->score != -1)
632     {
633         data1_mk_text (p->dh, mem, i4, n);
634         data1_mk_tag_data_int (p->dh, n, "score", p->score, mem);
635     }
636     data1_mk_text (p->dh, mem, i4, n);
637     data1_mk_tag_data_int (p->dh, n, "localnumber", p->localno, mem);
638     if (p->fname)
639     {
640         data1_mk_text (p->dh, mem, i4, n);
641         data1_mk_tag_data_text(p->dh, n, "filename", p->fname, mem);
642     }
643     data1_mk_text (p->dh, mem, i2, n);
644 }
645
646 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
647 {
648     data1_node *node = 0, *onode = 0, *top;
649     data1_node *dnew;
650     data1_maptab *map;
651     int res, selected = 0;
652     NMEM mem;
653     struct grs_read_info gri;
654     char *tagname;
655     struct grs_handlers *h = (struct grs_handlers *) clientData;
656     int requested_schema = VAL_NONE;
657     data1_marctab *marctab;
658     int dummy;
659     
660     mem = nmem_create();
661     gri.readf = p->readf;
662     gri.seekf = p->seekf;
663     gri.tellf = p->tellf;
664     gri.endf = NULL;
665     gri.fh = p->fh;
666     gri.offset = 0;
667     gri.mem = mem;
668     gri.dh = p->dh;
669
670     logf (LOG_DEBUG, "grs_retrieve");
671     if (read_grs_type (h, &gri, p->subType, &node))
672     {
673         p->diagnostic = 14;
674         nmem_destroy (mem);
675         return 0;
676     }
677     if (!node)
678     {
679         p->diagnostic = 14;
680         nmem_destroy (mem);
681         return 0;
682     }
683     /* ensure our data1 tree is UTF-8 */
684     data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
685
686 #if 0
687     data1_pr_tree (p->dh, node, stdout);
688 #endif
689     top = data1_get_root_tag (p->dh, node);
690
691     logf (LOG_DEBUG, "grs_retrieve: size");
692     if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
693     {
694         dnew->u.data.what = DATA1I_text;
695         dnew->u.data.data = dnew->lbuf;
696         sprintf(dnew->u.data.data, "%d", p->recordSize);
697         dnew->u.data.len = strlen(dnew->u.data.data);
698     }
699
700     tagname = res_get_def(p->res, "tagrank", "rank");
701     if (strcmp(tagname, "0") && p->score >= 0 &&
702         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
703     {
704         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
705         dnew->u.data.what = DATA1I_num;
706         dnew->u.data.data = dnew->lbuf;
707         sprintf(dnew->u.data.data, "%d", p->score);
708         dnew->u.data.len = strlen(dnew->u.data.data);
709     }
710
711     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
712     if (strcmp(tagname, "0") && p->localno > 0 &&
713          (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
714     {
715         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
716         dnew->u.data.what = DATA1I_text;
717         dnew->u.data.data = dnew->lbuf;
718
719         sprintf(dnew->u.data.data, "%d", p->localno);
720         dnew->u.data.len = strlen(dnew->u.data.data);
721     }
722 #if 0
723     data1_pr_tree (p->dh, node, stdout);
724 #endif
725     if (p->comp && p->comp->which == Z_RecordComp_complex &&
726         p->comp->u.complex->generic &&
727         p->comp->u.complex->generic->schema)
728     {
729         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
730         if (oe)
731             requested_schema = oe->value;
732     }
733
734     /* If schema has been specified, map if possible, then check that
735      * we got the right one 
736      */
737     if (requested_schema != VAL_NONE)
738     {
739         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
740         for (map = node->u.root.absyn->maptabs; map; map = map->next)
741         {
742             if (map->target_absyn_ref == requested_schema)
743             {
744                 onode = node;
745                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
746                 {
747                     p->diagnostic = 14;
748                     nmem_destroy (mem);
749                     return 0;
750                 }
751                 break;
752             }
753         }
754         if (node->u.root.absyn &&
755             requested_schema != node->u.root.absyn->reference)
756         {
757             p->diagnostic = 238;
758             nmem_destroy (mem);
759             return 0;
760         }
761     }
762     /*
763      * Does the requested format match a known syntax-mapping? (this reflects
764      * the overlap of schema and formatting which is inherent in the MARC
765      * family)
766      */
767     yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
768     if (node->u.root.absyn)
769         for (map = node->u.root.absyn->maptabs; map; map = map->next)
770         {
771             if (map->target_absyn_ref == p->input_format)
772             {
773                 onode = node;
774                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
775                 {
776                     p->diagnostic = 14;
777                     nmem_destroy (mem);
778                     return 0;
779                 }
780                 break;
781             }
782         }
783     yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
784     if (node->u.root.absyn &&
785         node->u.root.absyn->reference != VAL_NONE &&
786         p->input_format == VAL_GRS1)
787     {
788         oident oe;
789         Odr_oid *oid;
790         int oidtmp[OID_SIZE];
791         
792         oe.proto = PROTO_Z3950;
793         oe.oclass = CLASS_SCHEMA;
794         oe.value = node->u.root.absyn->reference;
795         
796         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
797         {
798             char tmp[128];
799             data1_handle dh = p->dh;
800             char *p = tmp;
801             int *ii;
802             
803             for (ii = oid; *ii >= 0; ii++)
804             {
805                 if (p != tmp)
806                         *(p++) = '.';
807                 sprintf(p, "%d", *ii);
808                 p += strlen(p);
809             }
810             if ((dnew = data1_mk_tag_data_wd(dh, top, 
811                                              "schemaIdentifier", mem)))
812             {
813                 dnew->u.data.what = DATA1I_oid;
814                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
815                 memcpy(dnew->u.data.data, tmp, p - tmp);
816                 dnew->u.data.len = p - tmp;
817             }
818         }
819     }
820
821     logf (LOG_DEBUG, "grs_retrieve: element spec");
822     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
823     {
824         p->diagnostic = res;
825         if (onode)
826             data1_free_tree(p->dh, onode);
827         data1_free_tree(p->dh, node);
828         nmem_destroy(mem);
829         return 0;
830     }
831     else if (p->comp && !res)
832         selected = 1;
833
834 #if 0
835     data1_pr_tree (p->dh, node, stdout);
836 #endif
837     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
838     switch (p->output_format = (p->input_format != VAL_NONE ?
839                                 p->input_format : VAL_SUTRS))
840     {
841     case VAL_TEXT_XML:
842         zebra_xml_metadata (p, top, mem);
843
844 #if 0
845         data1_pr_tree (p->dh, node, stdout);
846 #endif
847
848         if (p->encoding)
849             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
850
851         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
852                                               &p->rec_len)))
853             p->diagnostic = 238;
854         else
855         {
856             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
857             memcpy (new_buf, p->rec_buf, p->rec_len);
858             p->rec_buf = new_buf;
859         }
860         break;
861     case VAL_GRS1:
862         dummy = 0;
863         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
864                                           p->odr, &dummy)))
865             p->diagnostic = 238; /* not available in requested syntax */
866         else
867             p->rec_len = (size_t) (-1);
868         break;
869     case VAL_EXPLAIN:
870         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
871                                                p->odr)))
872             p->diagnostic = 238;
873         else
874             p->rec_len = (size_t) (-1);
875         break;
876     case VAL_SUMMARY:
877         if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
878                                                p->odr)))
879             p->diagnostic = 238;
880         else
881             p->rec_len = (size_t) (-1);
882         break;
883     case VAL_SUTRS:
884         if (p->encoding)
885             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
886         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
887                                            &p->rec_len)))
888             p->diagnostic = 238;
889         else
890         {
891             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
892             memcpy (new_buf, p->rec_buf, p->rec_len);
893             p->rec_buf = new_buf;
894         }
895         break;
896     case VAL_SOIF:
897         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
898                                             &p->rec_len)))
899             p->diagnostic = 238;
900         else
901         {
902             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
903             memcpy (new_buf, p->rec_buf, p->rec_len);
904             p->rec_buf = new_buf;
905         }
906         break;
907     default:
908         if (!node->u.root.absyn)
909         {
910             p->diagnostic = 238;
911             break;
912         }
913         for (marctab = node->u.root.absyn->marc; marctab;
914              marctab = marctab->next)
915             if (marctab->reference == p->input_format)
916                 break;
917         if (!marctab)
918         {
919             p->diagnostic = 238;
920             break;
921         }
922         if (p->encoding)
923             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
924         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
925                                         selected, &p->rec_len)))
926             p->diagnostic = 238;
927         else
928         {
929             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
930             memcpy (new_buf, p->rec_buf, p->rec_len);
931                 p->rec_buf = new_buf;
932         }
933     }
934     if (node)
935         data1_free_tree(p->dh, node);
936     if (onode)
937         data1_free_tree(p->dh, onode);
938     nmem_destroy(mem);
939     return 0;
940 }
941
942 static struct recType grs_type =
943 {
944     "grs",
945     grs_init,
946     grs_destroy,
947     grs_extract,
948     grs_retrieve
949 };
950
951 RecType recTypeGrs = &grs_type;