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