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