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