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