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