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