work on string tag indexing
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /*
2  * Copyright (C) 1994-2001, Index Data
3  * All rights reserved.
4  *
5  * $Log: recgrs.c,v $
6  * Revision 1.44  2002-04-11 20:09:47  adam
7  * work on string tag indexing
8  *
9  * Revision 1.43  2002/03/21 23:06:36  adam
10  * Source 'tag' in abs-file
11  *
12  * Revision 1.42  2002/02/20 17:30:01  adam
13  * Work on new API. Locking system re-implemented
14  *
15  * Revision 1.41  2001/05/22 21:01:47  adam
16  * Removed print of data1 tree on stdout so that inetd works again.
17  *
18  * Revision 1.40  2001/03/29 21:31:31  adam
19  * Fixed "record begin" for Tcl filter.
20  *
21  * Revision 1.39  2000/12/05 19:09:15  adam
22  * Fixed problem where indexer could crash if abstract syntax was undefined.
23  *
24  * Revision 1.38  2000/12/05 14:44:58  adam
25  * Fixed minor bug that could cause zmbol to break it data were emitted
26  * with not parent tags.
27  *
28  * Revision 1.37  2000/12/05 12:22:53  adam
29  * Termlist source implemented (so that we can index values of XML/SGML
30  * attributes).
31  *
32  * Revision 1.36  2000/12/05 10:01:44  adam
33  * Fixed bug regarding user-defined attribute sets.
34  *
35  * Revision 1.35  2000/11/29 15:21:31  adam
36  * Fixed problem with passwd db.
37  *
38  * Revision 1.34  2000/02/25 13:24:49  adam
39  * Fixed bug regarding pointer conversion that showed up on OSF V5.
40  *
41  * Revision 1.33  1999/11/30 13:48:04  adam
42  * Improved installation. Updated for inclusion of YAZ header files.
43  *
44  * Revision 1.32  1999/09/07 07:19:21  adam
45  * Work on character mapping. Implemented replace rules.
46  *
47  * Revision 1.31  1999/07/14 10:56:43  adam
48  * Fixed potential memory leak.
49  *
50  * Revision 1.30  1999/07/06 12:26:41  adam
51  * Retrieval handler obeys schema and handles XML transfer syntax.
52  *
53  * Revision 1.29  1999/05/26 07:49:14  adam
54  * C++ compilation.
55  *
56  * Revision 1.28  1999/05/21 12:00:17  adam
57  * Better diagnostics for extraction process.
58  *
59  * Revision 1.27  1999/05/20 12:57:18  adam
60  * Implemented TCL filter. Updated recctrl system.
61  *
62  * Revision 1.26  1999/03/02 16:15:44  quinn
63  * Added "tagsysno" and "tagrank" directives to zebra.cfg.
64  *
65  * Revision 1.25  1999/02/18 15:01:26  adam
66  * Minor changes.
67  *
68  * Revision 1.24  1999/02/02 14:51:28  adam
69  * Updated WIN32 code specific sections. Changed header.
70  *
71  * Revision 1.23  1998/10/18 07:51:10  adam
72  * Changed one logf call.
73  *
74  * Revision 1.22  1998/10/16 08:14:37  adam
75  * Updated record control system.
76  *
77  * Revision 1.21  1998/07/01 09:16:10  adam
78  * Element localno only added when it's greater than 0.
79  *
80  * Revision 1.20  1998/05/20 10:12:26  adam
81  * Implemented automatic EXPLAIN database maintenance.
82  * Modified Zebra to work with ASN.1 compiled version of YAZ.
83  *
84  * Revision 1.19  1998/03/11 11:19:05  adam
85  * Changed the way sequence numbers are generated.
86  *
87  * Revision 1.18  1998/03/05 08:41:31  adam
88  * Minor changes.
89  *
90  * Revision 1.17  1998/02/10 12:03:06  adam
91  * Implemented Sort.
92  *
93  * Revision 1.16  1998/01/29 13:38:17  adam
94  * Fixed problem with mapping to record with unknown schema.
95  *
96  * Revision 1.15  1998/01/26 10:37:57  adam
97  * Better diagnostics.
98  *
99  * Revision 1.14  1997/11/06 11:41:01  adam
100  * Implemented "begin variant" for the sgml.regx filter.
101  *
102  * Revision 1.13  1997/10/31 12:35:44  adam
103  * Added a few log statements.
104  *
105  * Revision 1.12  1997/10/29 12:02:22  adam
106  * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
107  *
108  * Revision 1.11  1997/10/27 14:34:00  adam
109  * Work on generic character mapping depending on "structure" field
110  * in abstract syntax file.
111  *
112  * Revision 1.10  1997/09/18 08:59:21  adam
113  * Extra generic handle for the character mapping routines.
114  *
115  * Revision 1.9  1997/09/17 12:19:21  adam
116  * Zebra version corresponds to YAZ version 1.4.
117  * Changed Zebra server so that it doesn't depend on global common_resource.
118  *
119  * Revision 1.8  1997/09/09 13:38:14  adam
120  * Partial port to WIN95/NT.
121  *
122  * Revision 1.7  1997/09/05 15:30:10  adam
123  * Changed prototype for chr_map_input - added const.
124  * Added support for C++, headers uses extern "C" for public definitions.
125  *
126  * Revision 1.6  1997/09/04 13:54:40  adam
127  * Added MARC filter - type grs.marc.<syntax> where syntax refers
128  * to abstract syntax. New method tellf in retrieve/extract method.
129  *
130  * Revision 1.5  1997/07/15 16:29:03  adam
131  * Initialized dummy variable to keep checker gcc happy.
132  *
133  * Revision 1.4  1997/04/30 08:56:08  quinn
134  * null
135  *
136  * Revision 1.2  1996/10/11  16:06:43  quinn
137  * Revision 1.3  1997/02/24 10:41:50  adam
138  * Cleanup of code and commented out the "end element-end-record" code.
139  *
140  * Revision 1.2  1996/10/11 16:06:43  quinn
141  * Fixed arguments to nodetogr
142  *
143  * Revision 1.1  1996/10/11  10:57:25  adam
144  * New module recctrl. Used to manage records (extract/retrieval).
145  *
146  * Revision 1.29  1996/10/08 10:30:21  quinn
147  * Fixed type mismatch
148  *
149  * Revision 1.28  1996/10/07  16:06:40  quinn
150  * Added SOIF support
151  *
152  * Revision 1.27  1996/06/11  10:54:12  quinn
153  * Relevance work
154  *
155  * Revision 1.26  1996/06/06  12:08:45  quinn
156  * Added showRecord function
157  *
158  * Revision 1.25  1996/06/04  14:18:53  quinn
159  * Charmap work
160  *
161  * Revision 1.24  1996/06/04  13:27:54  quinn
162  * More work on charmapping
163  *
164  * Revision 1.23  1996/06/04  10:19:01  adam
165  * Minor changes - removed include of ctype.h.
166  *
167  * Revision 1.22  1996/06/03  10:15:27  quinn
168  * Various character-mapping.
169  *
170  * Revision 1.21  1996/05/31  13:27:24  quinn
171  * Character-conversion in phrases, too.
172  *
173  * Revision 1.19  1996/05/16  15:31:14  quinn
174  * a7
175  *
176  * Revision 1.18  1996/05/09  07:28:56  quinn
177  * Work towards phrases and multiple registers
178  *
179  * Revision 1.17  1996/05/01  13:46:37  adam
180  * First work on multiple records in one file.
181  * New option, -offset, to the "unread" command in the filter module.
182  *
183  * Revision 1.16  1996/01/17  14:57:54  adam
184  * Prototype changed for reader functions in extract/retrieve. File
185  *  is identified by 'void *' instead of 'int.
186  *
187  * Revision 1.15  1996/01/08  19:15:47  adam
188  * New input filter that works!
189  *
190  * Revision 1.14  1995/12/15  12:36:11  adam
191  * Retrieval calls data1_read_regx when subType is specified.
192  *
193  * Revision 1.13  1995/12/15  12:24:43  quinn
194  * *** empty log message ***
195  *
196  * Revision 1.12  1995/12/15  12:20:28  quinn
197  * *** empty log message ***
198  *
199  * Revision 1.11  1995/12/15  12:07:57  quinn
200  * Changed extraction strategy.
201  *
202  * Revision 1.10  1995/12/14  11:10:48  quinn
203  * Explain work
204  *
205  * Revision 1.9  1995/12/13  17:14:05  quinn
206  * *** empty log message ***
207  *
208  * Revision 1.8  1995/12/13  15:33:18  quinn
209  * *** empty log message ***
210  *
211  * Revision 1.7  1995/12/13  13:45:39  quinn
212  * Changed data1 to use nmem.
213  *
214  * Revision 1.6  1995/12/04  14:22:30  adam
215  * Extra arg to recType_byName.
216  * Started work on new regular expression parsed input to
217  * structured records.
218  *
219  * Revision 1.5  1995/11/28  14:18:37  quinn
220  * Set output_format.
221  *
222  * Revision 1.4  1995/11/21  13:14:49  quinn
223  * Fixed end-of-data-field problem (maybe).
224  *
225  * Revision 1.3  1995/11/15  19:13:09  adam
226  * Work on record management.
227  *
228  */
229
230 #include <stdio.h>
231 #include <assert.h>
232 #include <sys/types.h>
233 #ifndef WIN32
234 #include <unistd.h>
235 #endif
236
237 #include <yaz/log.h>
238 #include <yaz/oid.h>
239
240 #include <recctrl.h>
241 #include "grsread.h"
242
243 #define GRS_MAX_WORD 512
244
245 struct grs_handler {
246     RecTypeGrs type;
247     void *clientData;
248     int initFlag;
249     struct grs_handler *next;
250 };
251
252 struct grs_handlers {
253     struct grs_handler *handlers;
254 };
255
256 static int read_grs_type (struct grs_handlers *h,
257                           struct grs_read_info *p, const char *type,
258                           data1_node **root)
259 {
260     struct grs_handler *gh = h->handlers;
261     const char *cp = strchr (type, '.');
262
263     if (cp == NULL || cp == type)
264     {
265         cp = strlen(type) + type;
266         *p->type = 0;
267     }
268     else
269         strcpy (p->type, cp+1);
270     for (gh = h->handlers; gh; gh = gh->next)
271     {
272         if (!memcmp (type, gh->type->type, cp-type))
273         {
274             if (!gh->initFlag)
275             {
276                 gh->initFlag = 1;
277                 gh->clientData = (*gh->type->init)();
278             }
279             p->clientData = gh->clientData;
280             *root = (gh->type->read)(p);
281             gh->clientData = p->clientData;
282             return 0;
283         }
284     }
285     return 1;
286 }
287
288 static void grs_add_handler (struct grs_handlers *h, RecTypeGrs t)
289 {
290     struct grs_handler *gh = (struct grs_handler *) malloc (sizeof(*gh));
291     gh->next = h->handlers;
292     h->handlers = gh;
293     gh->initFlag = 0;
294     gh->clientData = 0;
295     gh->type = t;
296 }
297
298 static void *grs_init(RecType recType)
299 {
300     struct grs_handlers *h = (struct grs_handlers *) malloc (sizeof(*h));
301     h->handlers = 0;
302
303     grs_add_handler (h, recTypeGrs_sgml);
304     grs_add_handler (h, recTypeGrs_regx);
305 #if HAVE_TCL_H
306     grs_add_handler (h, recTypeGrs_tcl);
307 #endif
308     grs_add_handler (h, recTypeGrs_marc);
309     return h;
310 }
311
312 static void grs_destroy(void *clientData)
313 {
314     struct grs_handlers *h = (struct grs_handlers *) clientData;
315     struct grs_handler *gh = h->handlers, *gh_next;
316     while (gh)
317     {
318         gh_next = gh->next;
319         if (gh->initFlag)
320             (*gh->type->destroy)(gh->clientData);
321         free (gh);
322         gh = gh_next;
323     }
324     free (h);
325 }
326
327 static void index_string_tag (data1_node *n,
328                               struct recExtractCtrl *p,
329                               int level, RecWord *wrd,
330                               int use)
331 {
332     int i;
333     switch (n->which)
334     {
335     case DATA1N_data:
336         wrd->reg_type = 'w';
337         wrd->string = n->u.data.data;
338         wrd->length = n->u.data.len;
339         wrd->attrSet = VAL_BIB1;
340         wrd->attrUse = 1016;
341         if (p->flagShowRecords)
342         {
343             printf("%*s data=", (level + 1) * 4, "");
344             for (i = 0; i<wrd->length && i < 8; i++)
345                 fputc (wrd->string[i], stdout);
346             printf("\n");
347         }
348         else
349         {
350             (*p->tokenAdd)(wrd);
351         }
352         break;
353     case DATA1N_tag:
354         wrd->reg_type = 'w';
355         wrd->string = n->u.tag.tag;
356         wrd->length = strlen(n->u.tag.tag);
357         wrd->attrSet = VAL_BIB1;
358         wrd->attrUse = use;
359         if (p->flagShowRecords)
360         {
361             printf("%*s tag=", (level + 1) * 4, "");
362             for (i = 0; i<wrd->length && i < 8; i++)
363                 fputc (wrd->string[i], stdout);
364             printf("\n");
365         }
366         else
367         {
368             (*p->tokenAdd)(wrd);
369         }
370         break;
371     }
372 }
373
374 static void index_termlist (data1_node *par, data1_node *n,
375                             struct recExtractCtrl *p, int level, RecWord *wrd)
376 {
377     data1_termlist *tlist = 0;
378     data1_datatype dtype = DATA1K_string;
379     /*
380      * cycle up towards the root until we find a tag with an att..
381      * this has the effect of indexing locally defined tags with
382      * the attribute of their ancestor in the record.
383      */
384     
385     while (!par->u.tag.element)
386         if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
387             break;
388     if (!par || !(tlist = par->u.tag.element->termlists))
389         return;
390     if (par->u.tag.element->tag)
391         dtype = par->u.tag.element->tag->kind;
392     
393     for (; tlist; tlist = tlist->next)
394     {
395         char xattr[512];
396         /* consider source */
397         wrd->string = 0;
398         
399         if (!strcmp (tlist->source, "data") && n->which == DATA1N_data)
400         {
401             wrd->string = n->u.data.data;
402             wrd->length = n->u.data.len;
403         }
404         else if (!strcmp (tlist->source, "tag") && n->which == DATA1N_tag)
405         {
406             wrd->string = n->u.tag.tag;
407             wrd->length = strlen(n->u.tag.tag);
408         }
409         else if (sscanf (tlist->source, "attr(%511[^)])", xattr) == 1 &&
410             n->which == DATA1N_tag)
411         {
412             data1_xattr *p = n->u.tag.attributes;
413             while (p && strcmp (p->name, xattr))
414                 p = p->next;
415             if (p)
416             {
417                 wrd->string = p->value;
418                 wrd->length = strlen(p->value);
419             }
420         }
421         if (wrd->string)
422         {
423             if (p->flagShowRecords)
424             {
425                 int i;
426                 printf("%*sIdx: [%s]", (level + 1) * 4, "",
427                        tlist->structure);
428                 printf("%s:%s [%d] %s",
429                        tlist->att->parent->name,
430                        tlist->att->name, tlist->att->value,
431                        tlist->source);
432                 printf (" data=\"");
433                 for (i = 0; i<wrd->length && i < 8; i++)
434                     fputc (wrd->string[i], stdout);
435                 fputc ('"', stdout);
436                 if (wrd->length > 8)
437                     printf (" ...");
438                 fputc ('\n', stdout);
439             }
440             else
441             {
442                 wrd->reg_type = *tlist->structure;
443                 wrd->attrSet = (int) (tlist->att->parent->reference);
444                 wrd->attrUse = tlist->att->locals->local;
445                 (*p->tokenAdd)(wrd);
446             }
447         }
448     }
449 }
450
451 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
452 {
453     RecWord wrd;
454     (*p->init)(p, &wrd);      /* set defaults */
455     for (; n; n = n->next)
456     {
457         if (p->flagShowRecords) /* display element description to user */
458         {
459             if (n->which == DATA1N_root)
460             {
461                 printf("%*s", level * 4, "");
462                 printf("Record type: '%s'\n", n->u.root.type);
463             }
464             else if (n->which == DATA1N_tag)
465             {
466                 data1_element *e;
467
468                 printf("%*s", level * 4, "");
469                 if (!(e = n->u.tag.element))
470                     printf("Local tag: '%s'\n", n->u.tag.tag);
471                 else
472                 {
473                     printf("Elm: '%s' ", e->name);
474                     if (e->tag)
475                     {
476                         data1_tag *t = e->tag;
477
478                         printf("TagNam: '%s' ", t->names->name);
479                         printf("(");
480                         if (t->tagset)
481                             printf("%s[%d],", t->tagset->name, t->tagset->type);
482                         else
483                             printf("?,");
484                         if (t->which == DATA1T_numeric)
485                             printf("%d)", t->value.numeric);
486                         else
487                             printf("'%s')", t->value.string);
488                     }
489                     printf("\n");
490                 }
491             }
492         }
493
494         if (n->which == DATA1N_tag)
495         {
496             index_termlist (n, n, p, level, &wrd);
497             /* index start tag */
498             if (!n->root->u.root.absyn)
499                 index_string_tag (n, p, level, &wrd, 1);
500         }
501
502         if (n->child)
503             if (dumpkeys(n->child, p, level + 1) < 0)
504                 return -1;
505
506
507         if (n->which == DATA1N_data)
508         {
509             data1_node *par = get_parent_tag(p->dh, n);
510
511             if (p->flagShowRecords)
512             {
513                 printf("%*s", level * 4, "");
514                 printf("Data: ");
515                 if (n->u.data.len > 32)
516                     printf("'%.24s ... %.6s'\n", n->u.data.data,
517                            n->u.data.data + n->u.data.len-6);
518                 else if (n->u.data.len > 0)
519                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
520                 else
521                     printf("NULL\n");
522             }
523
524             if (par)
525                 index_termlist (par, n, p, level, &wrd);
526             if (!n->root->u.root.absyn)
527                 index_string_tag (n, p, level, &wrd, 1016);
528
529         }
530
531         if (n->which == DATA1N_tag)
532         {
533             /* index end tag */
534             if (!n->root->u.root.absyn)
535                 index_string_tag (n, p, level, &wrd, 2);
536         }
537
538
539         if (p->flagShowRecords && n->which == DATA1N_root)
540         {
541             printf("%*s-------------\n\n", level * 4, "");
542         }
543     }
544     return 0;
545 }
546
547 int grs_extract_tree(struct recExtractCtrl *p, data1_node *n)
548 {
549     oident oe;
550     int oidtmp[OID_SIZE];
551
552     oe.proto = PROTO_Z3950;
553     oe.oclass = CLASS_SCHEMA;
554     if (n->u.root.absyn)
555     {
556         oe.value = n->u.root.absyn->reference;
557         
558         if ((oid_ent_to_oid (&oe, oidtmp)))
559             (*p->schemaAdd)(p, oidtmp);
560     }
561     return dumpkeys(n, p, 0);
562 }
563
564 static int grs_extract_sub(struct grs_handlers *h, struct recExtractCtrl *p,
565                            NMEM mem)
566 {
567     data1_node *n;
568     struct grs_read_info gri;
569     oident oe;
570     int oidtmp[OID_SIZE];
571
572     gri.readf = p->readf;
573     gri.seekf = p->seekf;
574     gri.tellf = p->tellf;
575     gri.endf = p->endf;
576     gri.fh = p->fh;
577     gri.offset = p->offset;
578     gri.mem = mem;
579     gri.dh = p->dh;
580
581     if (read_grs_type (h, &gri, p->subType, &n))
582         return RECCTRL_EXTRACT_ERROR;
583     if (!n)
584         return RECCTRL_EXTRACT_EOF;
585     oe.proto = PROTO_Z3950;
586     oe.oclass = CLASS_SCHEMA;
587 #if 0
588     if (!n->u.root.absyn)
589         return RECCTRL_EXTRACT_ERROR;
590 #endif
591     if (n->u.root.absyn)
592     {
593         oe.value = n->u.root.absyn->reference;
594         if ((oid_ent_to_oid (&oe, oidtmp)))
595             (*p->schemaAdd)(p, oidtmp);
596     }
597 #if 0
598     data1_pr_tree (p->dh, n, stdout);
599 #endif
600     if (dumpkeys(n, p, 0) < 0)
601     {
602         data1_free_tree(p->dh, n);
603         return RECCTRL_EXTRACT_ERROR;
604     }
605     data1_free_tree(p->dh, n);
606     return RECCTRL_EXTRACT_OK;
607 }
608
609 static int grs_extract(void *clientData, struct recExtractCtrl *p)
610 {
611     int ret;
612     NMEM mem = nmem_create ();
613     struct grs_handlers *h = (struct grs_handlers *) clientData;
614
615     ret = grs_extract_sub(h, p, mem);
616     nmem_destroy(mem);
617     return ret;
618 }
619
620 /*
621  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
622  */
623 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
624 {
625     data1_esetname *eset;
626     Z_Espec1 *espec = 0;
627     Z_ElementSpec *p;
628
629     switch (c->which)
630     {
631     case Z_RecordComp_simple:
632         if (c->u.simple->which != Z_ElementSetNames_generic)
633             return 26; /* only generic form supported. Fix this later */
634         if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
635                                          c->u.simple->u.generic)))
636         {
637             logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
638             return 25; /* invalid esetname */
639         }
640         logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
641              c->u.simple->u.generic);
642         espec = eset->spec;
643         break;
644     case Z_RecordComp_complex:
645         if (c->u.complex->generic)
646         {
647             /* insert check for schema */
648             if ((p = c->u.complex->generic->elementSpec))
649             {
650                 switch (p->which)
651                 {
652                 case Z_ElementSpec_elementSetName:
653                     if (!(eset =
654                           data1_getesetbyname(dh, n->u.root.absyn,
655                                               p->u.elementSetName)))
656                     {
657                         logf(LOG_LOG, "Unknown esetname '%s'",
658                              p->u.elementSetName);
659                         return 25; /* invalid esetname */
660                     }
661                     logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
662                          p->u.elementSetName);
663                     espec = eset->spec;
664                     break;
665                 case Z_ElementSpec_externalSpec:
666                     if (p->u.externalSpec->which == Z_External_espec1)
667                     {
668                         logf(LOG_DEBUG, "Got Espec-1");
669                         espec = p->u.externalSpec-> u.espec1;
670                     }
671                     else
672                     {
673                         logf(LOG_LOG, "Unknown external espec.");
674                         return 25; /* bad. what is proper diagnostic? */
675                     }
676                     break;
677                 }
678             }
679         }
680         else
681             return 26; /* fix */
682     }
683     if (espec)
684     {
685         logf (LOG_DEBUG, "Element: Espec-1 match");
686         return data1_doespec1(dh, n, espec);
687     }
688     else
689     {
690         logf (LOG_DEBUG, "Element: all match");
691         return -1;
692     }
693 }
694
695 static int grs_retrieve(void *clientData, struct recRetrieveCtrl *p)
696 {
697     data1_node *node = 0, *onode = 0;
698     data1_node *dnew;
699     data1_maptab *map;
700     int res, selected = 0;
701     NMEM mem;
702     struct grs_read_info gri;
703     char *tagname;
704     struct grs_handlers *h = (struct grs_handlers *) clientData;
705     int requested_schema = VAL_NONE;
706     
707     mem = nmem_create();
708     gri.readf = p->readf;
709     gri.seekf = p->seekf;
710     gri.tellf = p->tellf;
711     gri.endf = NULL;
712     gri.fh = p->fh;
713     gri.offset = 0;
714     gri.mem = mem;
715     gri.dh = p->dh;
716
717     logf (LOG_DEBUG, "grs_retrieve");
718     if (read_grs_type (h, &gri, p->subType, &node))
719     {
720         p->diagnostic = 14;
721         nmem_destroy (mem);
722         return 0;
723     }
724     if (!node)
725     {
726         p->diagnostic = 14;
727         nmem_destroy (mem);
728         return 0;
729     }
730 #if 0
731     data1_pr_tree (p->dh, node, stdout);
732 #endif
733     logf (LOG_DEBUG, "grs_retrieve: size");
734     if ((dnew = data1_insert_taggeddata(p->dh, node, node,
735                                        "size", mem)))
736     {
737         dnew->u.data.what = DATA1I_text;
738         dnew->u.data.data = dnew->lbuf;
739         sprintf(dnew->u.data.data, "%d", p->recordSize);
740         dnew->u.data.len = strlen(dnew->u.data.data);
741     }
742
743     tagname = res_get_def(p->res, "tagrank", "rank");
744     if (strcmp(tagname, "0") && p->score >= 0 &&
745         (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
746     {
747         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
748         dnew->u.data.what = DATA1I_num;
749         dnew->u.data.data = dnew->lbuf;
750         sprintf(dnew->u.data.data, "%d", p->score);
751         dnew->u.data.len = strlen(dnew->u.data.data);
752     }
753
754     tagname = res_get_def(p->res, "tagsysno", "localControlNumber");
755     if (strcmp(tagname, "0") && p->localno > 0 &&
756          (dnew = data1_insert_taggeddata(p->dh, node, node, tagname, mem)))
757     {
758         logf (LOG_DEBUG, "grs_retrieve: %s", tagname);
759         dnew->u.data.what = DATA1I_text;
760         dnew->u.data.data = dnew->lbuf;
761         sprintf(dnew->u.data.data, "%d", p->localno);
762         dnew->u.data.len = strlen(dnew->u.data.data);
763     }
764
765     if (p->comp && p->comp->which == Z_RecordComp_complex &&
766         p->comp->u.complex->generic &&
767         p->comp->u.complex->generic->schema)
768     {
769         oident *oe = oid_getentbyoid (p->comp->u.complex->generic->schema);
770         if (oe)
771             requested_schema = oe->value;
772     }
773
774     /* If schema has been specified, map if possible, then check that
775      * we got the right one 
776      */
777     if (requested_schema != VAL_NONE)
778     {
779         logf (LOG_DEBUG, "grs_retrieve: schema mapping");
780         for (map = node->u.root.absyn->maptabs; map; map = map->next)
781         {
782             if (map->target_absyn_ref == requested_schema)
783             {
784                 onode = node;
785                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
786                 {
787                     p->diagnostic = 14;
788                     nmem_destroy (mem);
789                     return 0;
790                 }
791                 break;
792             }
793         }
794         if (node->u.root.absyn &&
795             requested_schema != node->u.root.absyn->reference)
796         {
797             p->diagnostic = 238;
798             nmem_destroy (mem);
799             return 0;
800         }
801     }
802     /*
803      * Does the requested format match a known syntax-mapping? (this reflects
804      * the overlap of schema and formatting which is inherent in the MARC
805      * family)
806      */
807     logf (LOG_DEBUG, "grs_retrieve: syntax mapping");
808     if (node->u.root.absyn)
809         for (map = node->u.root.absyn->maptabs; map; map = map->next)
810         {
811             if (map->target_absyn_ref == p->input_format)
812             {
813                 onode = node;
814                 if (!(node = data1_map_record(p->dh, onode, map, mem)))
815                 {
816                     p->diagnostic = 14;
817                     nmem_destroy (mem);
818                     return 0;
819                 }
820                 break;
821             }
822         }
823     logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
824     if (node->u.root.absyn &&
825         node->u.root.absyn->reference != VAL_NONE &&
826         p->input_format == VAL_GRS1)
827     {
828         oident oe;
829         Odr_oid *oid;
830         int oidtmp[OID_SIZE];
831         
832         oe.proto = PROTO_Z3950;
833         oe.oclass = CLASS_SCHEMA;
834         oe.value = node->u.root.absyn->reference;
835         
836         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
837         {
838             char tmp[128];
839             data1_handle dh = p->dh;
840             char *p = tmp;
841             int *ii;
842             
843             for (ii = oid; *ii >= 0; ii++)
844             {
845                 if (p != tmp)
846                         *(p++) = '.';
847                 sprintf(p, "%d", *ii);
848                 p += strlen(p);
849             }
850             *(p++) = '\0';
851                 
852             if ((dnew = data1_insert_taggeddata(dh, node, node,
853                                                 "schemaIdentifier", mem)))
854             {
855                 dnew->u.data.what = DATA1I_oid;
856                 dnew->u.data.data = (char *) nmem_malloc(mem, p - tmp);
857                 memcpy(dnew->u.data.data, tmp, p - tmp);
858                 dnew->u.data.len = p - tmp;
859             }
860         }
861     }
862
863     logf (LOG_DEBUG, "grs_retrieve: element spec");
864     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
865     {
866         p->diagnostic = res;
867         if (onode)
868             data1_free_tree(p->dh, onode);
869         data1_free_tree(p->dh, node);
870         nmem_destroy(mem);
871         return 0;
872     }
873     else if (p->comp && !res)
874         selected = 1;
875
876 #if 0
877     data1_pr_tree (p->dh, node, stdout);
878 #endif
879     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
880     switch (p->output_format = (p->input_format != VAL_NONE ?
881                                 p->input_format : VAL_SUTRS))
882     {
883         data1_marctab *marctab;
884         int dummy;
885         
886     case VAL_TEXT_XML:
887         if (!(p->rec_buf = data1_nodetoidsgml(p->dh, node, selected,
888                                               &p->rec_len)))
889             p->diagnostic = 238;
890         else
891         {
892             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
893             memcpy (new_buf, p->rec_buf, p->rec_len);
894             p->rec_buf = new_buf;
895         }
896         break;
897     case VAL_GRS1:
898         dummy = 0;
899         if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
900                                           p->odr, &dummy)))
901             p->diagnostic = 238; /* not available in requested syntax */
902         else
903             p->rec_len = (size_t) (-1);
904         break;
905     case VAL_EXPLAIN:
906         if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
907                                                p->odr)))
908             p->diagnostic = 238;
909         else
910             p->rec_len = (size_t) (-1);
911         break;
912     case VAL_SUMMARY:
913         if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
914                                                p->odr)))
915             p->diagnostic = 238;
916         else
917             p->rec_len = (size_t) (-1);
918         break;
919     case VAL_SUTRS:
920         if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
921                                            &p->rec_len)))
922             p->diagnostic = 238;
923         else
924         {
925             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
926             memcpy (new_buf, p->rec_buf, p->rec_len);
927             p->rec_buf = new_buf;
928         }
929         break;
930     case VAL_SOIF:
931         if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
932                                             &p->rec_len)))
933             p->diagnostic = 238;
934         else
935         {
936             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
937             memcpy (new_buf, p->rec_buf, p->rec_len);
938             p->rec_buf = new_buf;
939         }
940         break;
941     default:
942         if (!node->u.root.absyn)
943         {
944             p->diagnostic = 238;
945             break;
946         }
947         for (marctab = node->u.root.absyn->marc; marctab;
948              marctab = marctab->next)
949             if (marctab->reference == p->input_format)
950                 break;
951         if (!marctab)
952         {
953             p->diagnostic = 238;
954             break;
955         }
956         if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
957                                         selected, &p->rec_len)))
958             p->diagnostic = 238;
959         else
960         {
961             char *new_buf = (char*) odr_malloc (p->odr, p->rec_len);
962             memcpy (new_buf, p->rec_buf, p->rec_len);
963                 p->rec_buf = new_buf;
964         }
965     }
966     if (node)
967         data1_free_tree(p->dh, node);
968     if (onode)
969         data1_free_tree(p->dh, onode);
970     nmem_destroy(mem);
971     return 0;
972 }
973
974 static struct recType grs_type =
975 {
976     "grs",
977     grs_init,
978     grs_destroy,
979     grs_extract,
980     grs_retrieve
981 };
982
983 RecType recTypeGrs = &grs_type;