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