Towards GPL
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /* $Id: recgrs.c,v 1.59 2002-08-02 19:26:56 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             if (!n->root->u.root.absyn)
382                 index_xpath (n, p, level, wrd, 1);
383         }
384
385         if (n->child)
386             if (dumpkeys(n->child, p, level + 1, wrd) < 0)
387                 return -1;
388
389
390         if (n->which == DATA1N_data)
391         {
392             data1_node *par = get_parent_tag(p->dh, n);
393
394             if (p->flagShowRecords)
395             {
396                 printf("%*s", level * 4, "");
397                 printf("Data: ");
398                 if (n->u.data.len > 32)
399                     printf("'%.24s ... %.6s'\n", n->u.data.data,
400                            n->u.data.data + n->u.data.len-6);
401                 else if (n->u.data.len > 0)
402                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
403                 else
404                     printf("NULL\n");
405             }
406
407             if (par)
408                 index_termlist (par, n, p, level, wrd);
409             if (!n->root->u.root.absyn)
410                 index_xpath (n, p, level, wrd, 1016);
411
412         }
413
414         if (n->which == DATA1N_tag)
415         {
416             /* index end tag */
417             if (!n->root->u.root.absyn)
418                 index_xpath (n, p, level, wrd, 2);
419         }
420
421
422         if (p->flagShowRecords && n->which == DATA1N_root)
423         {
424             printf("%*s-------------\n\n", level * 4, "");
425         }
426     }
427     return 0;
428 }
429
430 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
431 {
432     oident oe;
433     int oidtmp[OID_SIZE];
434     RecWord wrd;
435
436     oe.proto = PROTO_Z3950;
437     oe.oclass = CLASS_SCHEMA;
438     if (n->u.root.absyn)
439     {
440         oe.value = n->u.root.absyn->reference;
441         
442         if ((oid_ent_to_oid (&oe, oidtmp)))
443             (*p->schemaAdd)(p, oidtmp);
444     }
445     (*p->init)(p, &wrd);
446
447     return dumpkeys(n, p, 0, &wrd);
448 }
449
450 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
451                            NMEM mem)
452 {
453     data1_node *n;
454     struct grs_read_info gri;
455     oident oe;
456     int oidtmp[OID_SIZE];
457     RecWord wrd;
458
459     gri.readf = p->readf;
460     gri.seekf = p->seekf;
461     gri.tellf = p->tellf;
462     gri.endf = p->endf;
463     gri.fh = p->fh;
464     gri.offset = p->offset;
465     gri.mem = mem;
466     gri.dh = p->dh;
467
468     if (read_grs_type (h, &gri, p->subType, &n))
469         return RECCTRL_EXTRACT_ERROR;
470     if (!n)
471         return RECCTRL_EXTRACT_EOF;
472     oe.proto = PROTO_Z3950;
473     oe.oclass = CLASS_SCHEMA;
474 #if 0
475     if (!n->u.root.absyn)
476         return RECCTRL_EXTRACT_ERROR;
477 #endif
478     if (n->u.root.absyn)
479     {
480         oe.value = n->u.root.absyn->reference;
481         if ((oid_ent_to_oid (&oe, oidtmp)))
482             (*p->schemaAdd)(p, oidtmp);
483     }
484
485     /* ensure our data1 tree is UTF-8 */
486     data1_iconv (p->dh, mem, n, "UTF-8", data1_get_encoding(p->dh, n));
487
488 #if 0
489     data1_pr_tree (p->dh, n, stdout);
490 #endif
491
492     (*p->init)(p, &wrd);
493     if (dumpkeys(n, p, 0, &wrd) < 0)
494     {
495         data1_free_tree(p->dh, n);
496         return RECCTRL_EXTRACT_ERROR;
497     }
498     data1_free_tree(p->dh, n);
499     return RECCTRL_EXTRACT_OK;
500 }
501
502 static int grs_extract(void *clientData, struct recExtractCtrl *p)
503 {
504     int ret;
505     NMEM mem = nmem_create ();
506     struct grs_handlers *h = (struct grs_handlers *) clientData;
507
508     ret = grs_extract_sub(h, p, mem);
509     nmem_destroy(mem);
510     return ret;
511 }
512
513 /*
514  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
515  */
516 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
517 {
518     data1_esetname *eset;
519     Z_Espec1 *espec = 0;
520     Z_ElementSpec *p;
521
522     switch (c->which)
523     {
524     case Z_RecordComp_simple:
525         if (c->u.simple->which != Z_ElementSetNames_generic)
526             return 26; /* only generic form supported. Fix this later */
527         if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
528                                          c->u.simple->u.generic)))
529         {
530             logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
531             return 25; /* invalid esetname */
532         }
533         logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
534              c->u.simple->u.generic);
535         espec = eset->spec;
536         break;
537     case Z_RecordComp_complex:
538         if (c->u.complex->generic)
539         {
540             /* insert check for schema */
541             if ((p = c->u.complex->generic->elementSpec))
542             {
543                 switch (p->which)
544                 {
545                 case Z_ElementSpec_elementSetName:
546                     if (!(eset =
547                           data1_getesetbyname(dh, n->u.root.absyn,
548                                               p->u.elementSetName)))
549                     {
550                         logf(LOG_LOG, "Unknown esetname '%s'",
551                              p->u.elementSetName);
552                         return 25; /* invalid esetname */
553                     }
554                     logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
555                          p->u.elementSetName);
556                     espec = eset->spec;
557                     break;
558                 case Z_ElementSpec_externalSpec:
559                     if (p->u.externalSpec->which == Z_External_espec1)
560                     {
561                         logf(LOG_DEBUG, "Got Espec-1");
562                         espec = p->u.externalSpec-> u.espec1;
563                     }
564                     else
565                     {
566                         logf(LOG_LOG, "Unknown external espec.");
567                         return 25; /* bad. what is proper diagnostic? */
568                     }
569                     break;
570                 }
571             }
572         }
573         else
574             return 26; /* fix */
575     }
576     if (espec)
577     {
578         logf (LOG_DEBUG, "Element: Espec-1 match");
579         return data1_doespec1(dh, n, espec);
580     }
581     else
582     {
583         logf (LOG_DEBUG, "Element: all match");
584         return -1;
585     }
586 }
587
588 static void add_idzebra_info (struct recRetrieveCtrl *p, data1_node *top,
589                               NMEM mem)
590 {
591     const char *idzebra_ns[7];
592
593     idzebra_ns[0] = "xmlns:idzebra";
594     idzebra_ns[1] = "http://www.indexdata.dk/zebra/";
595     idzebra_ns[2] = 0;
596
597     data1_tag_add_attr (p->dh, mem, top, idzebra_ns);
598
599     data1_mk_tag_data_int (p->dh, top, "idzebra:size", p->recordSize,
600                            mem);
601     if (p->score != -1)
602         data1_mk_tag_data_int (p->dh, top, "idzebra:score",
603                                p->score, mem);
604     
605     data1_mk_tag_data_int (p->dh, top, "idzebra:localnumber", p->localno,
606                            mem);
607     if (p->fname)
608         data1_mk_tag_data_text(p->dh, top, "idzebra:filename",
609                                p->fname, mem);
610 }
611
612 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
613 {
614     data1_node *node = 0, *onode = 0, *top;
615     data1_node *dnew;
616     data1_maptab *map;
617     int res, selected = 0;
618     NMEM mem;
619     struct grs_read_info gri;
620     char *tagname;
621     struct grs_handlers *h = (struct grs_handlers *) clientData;
622     int requested_schema = VAL_NONE;
623     data1_marctab *marctab;
624     int dummy;
625     
626     mem = nmem_create();
627     gri.readf = p->readf;
628     gri.seekf = p->seekf;
629     gri.tellf = p->tellf;
630     gri.endf = NULL;
631     gri.fh = p->fh;
632     gri.offset = 0;
633     gri.mem = mem;
634     gri.dh = p->dh;
635
636     logf (LOG_DEBUG, "grs_retrieve");
637     if (read_grs_type (h, &gri, p->subType, &node))
638     {
639         p->diagnostic = 14;
640         nmem_destroy (mem);
641         return 0;
642     }
643     if (!node)
644     {
645         p->diagnostic = 14;
646         nmem_destroy (mem);
647         return 0;
648     }
649     /* ensure our data1 tree is UTF-8 */
650     data1_iconv (p->dh, mem, node, "UTF-8", data1_get_encoding(p->dh, node));
651
652 #if 0
653     data1_pr_tree (p->dh, node, stdout);
654 #endif
655     top = data1_get_root_tag (p->dh, node);
656
657     logf (LOG_DEBUG, "grs_retrieve: size");
658     if ((dnew = data1_mk_tag_data_wd(p->dh, top, "size", mem)))
659     {
660         dnew->u.data.what = DATA1I_text;
661         dnew->u.data.data = dnew->lbuf;
662         sprintf(dnew->u.data.data, "%d", p->recordSize);
663         dnew->u.data.len = strlen(dnew->u.data.data);
664     }
665
666     tagname = res_get_def(p->res, "tagrank", "rank");
667     if (strcmp(tagname, "0") && p->score >= 0 &&
668         (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
669     {
670         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
671         dnew->u.data.what = DATA1I_num;
672         dnew->u.data.data = dnew->lbuf;
673         sprintf(dnew->u.data.data, "%d", p->score);
674         dnew->u.data.len = strlen(dnew->u.data.data);
675     }
676
677     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
678     if (strcmp(tagname, "0") && p->localno > 0 &&
679          (dnew = data1_mk_tag_data_wd(p->dh, top, tagname, mem)))
680     {
681         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
682         dnew->u.data.what = DATA1I_text;
683         dnew->u.data.data = dnew->lbuf;
684
685         sprintf(dnew->u.data.data, "%d", p->localno);
686         dnew->u.data.len = strlen(dnew->u.data.data);
687     }
688 #if 0
689     data1_pr_tree (p->dh, node, stdout);
690 #endif
691     if (p->comp && p->comp->which == Z_RecordComp_complex &&
692         p->comp->u.complex->generic &&
693         p->comp->u.complex->generic->schema)
694     {
695         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
696         if (oe)
697             requested_schema = oe->value;
698     }
699
700     /* If schema has been specified, map if possible, then check that
701      * we got the right one 
702      */
703     if (requested_schema != VAL_NONE)
704     {
705         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
706         for (map = node->u.root.absyn->maptabs; map; map = map->next)
707         {
708             if (map->target_absyn_ref == requested_schema)
709             {
710                 onode = node;
711                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
712                 {
713                     p->diagnostic = 14;
714                     nmem_destroy (mem);
715                     return 0;
716                 }
717                 break;
718             }
719         }
720         if (node->u.root.absyn &&
721             requested_schema != node->u.root.absyn->reference)
722         {
723             p->diagnostic = 238;
724             nmem_destroy (mem);
725             return 0;
726         }
727     }
728     /*
729      * Does the requested format match a known syntax-mapping? (this reflects
730      * the overlap of schema and formatting which is inherent in the MARC
731      * family)
732      */
733     yaz_log (LOG_DEBUG, "grs_retrieve: syntax mapping");
734     if (node->u.root.absyn)
735         for (map = node->u.root.absyn->maptabs; map; map = map->next)
736         {
737             if (map->target_absyn_ref == p->input_format)
738             {
739                 onode = node;
740                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
741                 {
742                     p->diagnostic = 14;
743                     nmem_destroy (mem);
744                     return 0;
745                 }
746                 break;
747             }
748         }
749     yaz_log (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
750     if (node->u.root.absyn &&
751         node->u.root.absyn->reference != VAL_NONE &&
752         p->input_format == VAL_GRS1)
753     {
754         oident oe;
755         Odr_oid *oid;
756         int oidtmp[OID_SIZE];
757         
758         oe.proto = PROTO_Z3950;
759         oe.oclass = CLASS_SCHEMA;
760         oe.value = node->u.root.absyn->reference;
761         
762         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
763         {
764             char tmp[128];
765             data1_handle dh = p->dh;
766             char *p = tmp;
767             int *ii;
768             
769             for (ii = oid; *ii >= 0; ii++)
770             {
771                 if (p != tmp)
772                         *(p++) = '.';
773                 sprintf(p, "%d", *ii);
774                 p += strlen(p);
775             }
776             *(p++) = '\0';
777                 
778             if ((dnew = data1_mk_tag_data_wd(dh, node, 
779                                              "schemaIdentifier", mem)))
780             {
781                 dnew->u.data.what = DATA1I_oid;
782                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
783                 memcpy(dnew->u.data.data, tmp, p - tmp);
784                 dnew->u.data.len = p - tmp;
785             }
786         }
787     }
788
789     logf (LOG_DEBUG, "grs_retrieve: element spec");
790     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
791     {
792         p->diagnostic = res;
793         if (onode)
794             data1_free_tree(p->dh, onode);
795         data1_free_tree(p->dh, node);
796         nmem_destroy(mem);
797         return 0;
798     }
799     else if (p->comp && !res)
800         selected = 1;
801
802 #if 0
803     data1_pr_tree (p->dh, node, stdout);
804 #endif
805     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
806     switch (p->output_format = (p->input_format != VAL_NONE ?
807                                 p->input_format : VAL_SUTRS))
808     {
809     case VAL_TEXT_XML:
810         add_idzebra_info (p, top, mem);
811
812         if (p->encoding)
813             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
814
815         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
816                                               &p->rec_len)))
817             p->diagnostic = 238;
818         else
819         {
820             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
821             memcpy (new_buf, p->rec_buf, p->rec_len);
822             p->rec_buf = new_buf;
823         }
824         break;
825     case VAL_GRS1:
826         dummy = 0;
827         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
828                                           p->odr, &dummy)))
829             p->diagnostic = 238; /* not available in requested syntax */
830         else
831             p->rec_len = (size_t) (-1);
832         break;
833     case VAL_EXPLAIN:
834         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
835                                                p->odr)))
836             p->diagnostic = 238;
837         else
838             p->rec_len = (size_t) (-1);
839         break;
840     case VAL_SUMMARY:
841         if (!(p->rec_buf = data1_nodetosummary(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_SUTRS:
848         if (p->encoding)
849             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
850         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
851                                            &p->rec_len)))
852             p->diagnostic = 238;
853         else
854         {
855             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
856             memcpy (new_buf, p->rec_buf, p->rec_len);
857             p->rec_buf = new_buf;
858         }
859         break;
860     case VAL_SOIF:
861         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
862                                             &p->rec_len)))
863             p->diagnostic = 238;
864         else
865         {
866             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
867             memcpy (new_buf, p->rec_buf, p->rec_len);
868             p->rec_buf = new_buf;
869         }
870         break;
871     default:
872         if (!node->u.root.absyn)
873         {
874             p->diagnostic = 238;
875             break;
876         }
877         for (marctab = node->u.root.absyn->marc; marctab;
878              marctab = marctab->next)
879             if (marctab->reference == p->input_format)
880                 break;
881         if (!marctab)
882         {
883             p->diagnostic = 238;
884             break;
885         }
886         if (p->encoding)
887             data1_iconv (p->dh, mem, node, p->encoding, "UTF-8");
888         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
889                                         selected, &p->rec_len)))
890             p->diagnostic = 238;
891         else
892         {
893             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
894             memcpy (new_buf, p->rec_buf, p->rec_len);
895                 p->rec_buf = new_buf;
896         }
897     }
898     if (node)
899         data1_free_tree(p->dh, node);
900     if (onode)
901         data1_free_tree(p->dh, onode);
902     nmem_destroy(mem);
903     return 0;
904 }
905
906 static struct recType grs_type =
907 {
908     "grs",
909     grs_init,
910     grs_destroy,
911     grs_extract,
912     grs_retrieve
913 };
914
915 RecType recTypeGrs = &grs_type;