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