Directive systag.
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /* $Id: recgrs.c,v 1.70 2002-12-02 16:55:14 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     const 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     tagname = data1_systag_lookup(node->u.root.absyn, "size", "size");
693     if (tagname &&
694         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
695     {
696         dnew->u.data.what = DATA1I_text;
697         dnew->u.data.data = dnew->lbuf;
698         sprintf(dnew->u.data.data, "%d", p->recordSize);
699         dnew->u.data.len = strlen(dnew->u.data.data);
700     }
701     
702     tagname = data1_systag_lookup(node->u.root.absyn, "rank", "rank");
703     if (tagname && p->score >= 0 &&
704         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
705     {
706         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
707         dnew->u.data.what = DATA1I_num;
708         dnew->u.data.data = dnew->lbuf;
709         sprintf(dnew->u.data.data, "%d", p->score);
710         dnew->u.data.len = strlen(dnew->u.data.data);
711     }
712
713     tagname = data1_systag_lookup(node->u.root.absyn, "sysno",
714                                   "localControlNumber");
715     if (tagname && p->localno > 0 &&
716         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
717     {
718         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
719         dnew->u.data.what = DATA1I_text;
720         dnew->u.data.data = dnew->lbuf;
721         
722         sprintf(dnew->u.data.data, "%d", p->localno);
723         dnew->u.data.len = strlen(dnew->u.data.data);
724     }
725 #if 0
726     data1_pr_tree (p->dh, node, stdout);
727 #endif
728     if (p->comp && p->comp->which == Z_RecordComp_complex &&
729         p->comp->u.complex->generic &&
730         p->comp->u.complex->generic->schema)
731     {
732         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
733         if (oe)
734             requested_schema = oe->value;
735     }
736
737     /* If schema has been specified, map if possible, then check that
738      * we got the right one 
739      */
740     if (requested_schema != VAL_NONE)
741     {
742         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
743         for (map = node->u.root.absyn->maptabs; map; map = map->next)
744         {
745             if (map->target_absyn_ref == requested_schema)
746             {
747                 onode = node;
748                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
749                 {
750                     p->diagnostic = 14;
751                     nmem_destroy (mem);
752                     return 0;
753                 }
754                 break;
755             }
756         }
757         if (node->u.root.absyn &&
758             requested_schema != node->u.root.absyn->reference)
759         {
760             p->diagnostic = 238;
761             nmem_destroy (mem);
762             return 0;
763         }
764     }
765     /*
766      * Does the requested format match a known syntax-mapping? (this reflects
767      * the overlap of schema and formatting which is inherent in the MARC
768      * family)
769      */
770     yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
771     if (node->u.root.absyn)
772         for (map = node->u.root.absyn->maptabs; map; map = map->next)
773         {
774             if (map->target_absyn_ref == p->input_format)
775             {
776                 onode = node;
777                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
778                 {
779                     p->diagnostic = 14;
780                     nmem_destroy (mem);
781                     return 0;
782                 }
783                 break;
784             }
785         }
786     yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
787     if (node->u.root.absyn &&
788         node->u.root.absyn->reference != VAL_NONE &&
789         p->input_format == VAL_GRS1)
790     {
791         oident oe;
792         Odr_oid *oid;
793         int oidtmp[OID_SIZE];
794         
795         oe.proto = PROTO_Z3950;
796         oe.oclass = CLASS_SCHEMA;
797         oe.value = node->u.root.absyn->reference;
798         
799         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
800         {
801             char tmp[128];
802             data1_handle dh = p->dh;
803             char *p = tmp;
804             int *ii;
805             
806             for (ii = oid; *ii >= 0; ii++)
807             {
808                 if (p != tmp)
809                         *(p++) = '.';
810                 sprintf(p, "%d", *ii);
811                 p += strlen(p);
812             }
813             if ((dnew = data1_mk_tag_data_wd(dh, top, 
814                                              "schemaIdentifier", mem)))
815             {
816                 dnew->u.data.what = DATA1I_oid;
817                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
818                 memcpy(dnew->u.data.data, tmp, p - tmp);
819                 dnew->u.data.len = p - tmp;
820             }
821         }
822     }
823
824     logf (LOG_DEBUG, "grs_retrieve: element spec");
825     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
826     {
827         p->diagnostic = res;
828         if (onode)
829             data1_free_tree(p->dh, onode);
830         data1_free_tree(p->dh, node);
831         nmem_destroy(mem);
832         return 0;
833     }
834     else if (p->comp && !res)
835         selected = 1;
836
837 #if 0
838     data1_pr_tree (p->dh, node, stdout);
839 #endif
840     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
841     switch (p->output_format = (p->input_format != VAL_NONE ?
842                                 p->input_format : VAL_SUTRS))
843     {
844     case VAL_TEXT_XML:
845         zebra_xml_metadata (p, top, mem);
846
847 #if 0
848         data1_pr_tree (p->dh, node, stdout);
849 #endif
850
851         if (p->encoding)
852             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
853
854         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
855                                               &p->rec_len)))
856             p->diagnostic = 238;
857         else
858         {
859             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
860             memcpy (new_buf, p->rec_buf, p->rec_len);
861             p->rec_buf = new_buf;
862         }
863         break;
864     case VAL_GRS1:
865         dummy = 0;
866         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
867                                           p->odr, &dummy)))
868             p->diagnostic = 238; /* not available in requested syntax */
869         else
870             p->rec_len = (size_t) (-1);
871         break;
872     case VAL_EXPLAIN:
873         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
874                                                p->odr)))
875             p->diagnostic = 238;
876         else
877             p->rec_len = (size_t) (-1);
878         break;
879     case VAL_SUMMARY:
880         if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
881                                                p->odr)))
882             p->diagnostic = 238;
883         else
884             p->rec_len = (size_t) (-1);
885         break;
886     case VAL_SUTRS:
887         if (p->encoding)
888             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
889         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
890                                            &p->rec_len)))
891             p->diagnostic = 238;
892         else
893         {
894             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
895             memcpy (new_buf, p->rec_buf, p->rec_len);
896             p->rec_buf = new_buf;
897         }
898         break;
899     case VAL_SOIF:
900         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
901                                             &p->rec_len)))
902             p->diagnostic = 238;
903         else
904         {
905             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
906             memcpy (new_buf, p->rec_buf, p->rec_len);
907             p->rec_buf = new_buf;
908         }
909         break;
910     default:
911         if (!node->u.root.absyn)
912         {
913             p->diagnostic = 238;
914             break;
915         }
916         for (marctab = node->u.root.absyn->marc; marctab;
917              marctab = marctab->next)
918             if (marctab->reference == p->input_format)
919                 break;
920         if (!marctab)
921         {
922             p->diagnostic = 238;
923             break;
924         }
925         if (p->encoding)
926             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
927         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
928                                         selected, &p->rec_len)))
929             p->diagnostic = 238;
930         else
931         {
932             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
933             memcpy (new_buf, p->rec_buf, p->rec_len);
934                 p->rec_buf = new_buf;
935         }
936     }
937     if (node)
938         data1_free_tree(p->dh, node);
939     if (onode)
940         data1_free_tree(p->dh, onode);
941     nmem_destroy(mem);
942     return 0;
943 }
944
945 static struct recType grs_type =
946 {
947     "grs",
948     grs_init,
949     grs_destroy,
950     grs_extract,
951     grs_retrieve
952 };
953
954 RecType recTypeGrs = &grs_type;