Changed the way sequence numbers are generated.
[idzebra-moved-to-github.git] / recctrl / recgrs.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: recgrs.c,v $
7  * Revision 1.19  1998-03-11 11:19:05  adam
8  * Changed the way sequence numbers are generated.
9  *
10  * Revision 1.18  1998/03/05 08:41:31  adam
11  * Minor changes.
12  *
13  * Revision 1.17  1998/02/10 12:03:06  adam
14  * Implemented Sort.
15  *
16  * Revision 1.16  1998/01/29 13:38:17  adam
17  * Fixed problem with mapping to record with unknown schema.
18  *
19  * Revision 1.15  1998/01/26 10:37:57  adam
20  * Better diagnostics.
21  *
22  * Revision 1.14  1997/11/06 11:41:01  adam
23  * Implemented "begin variant" for the sgml.regx filter.
24  *
25  * Revision 1.13  1997/10/31 12:35:44  adam
26  * Added a few log statements.
27  *
28  * Revision 1.12  1997/10/29 12:02:22  adam
29  * Using oid_ent_to_oid used instead of the non thread-safe oid_getoidbyent.
30  *
31  * Revision 1.11  1997/10/27 14:34:00  adam
32  * Work on generic character mapping depending on "structure" field
33  * in abstract syntax file.
34  *
35  * Revision 1.10  1997/09/18 08:59:21  adam
36  * Extra generic handle for the character mapping routines.
37  *
38  * Revision 1.9  1997/09/17 12:19:21  adam
39  * Zebra version corresponds to YAZ version 1.4.
40  * Changed Zebra server so that it doesn't depend on global common_resource.
41  *
42  * Revision 1.8  1997/09/09 13:38:14  adam
43  * Partial port to WIN95/NT.
44  *
45  * Revision 1.7  1997/09/05 15:30:10  adam
46  * Changed prototype for chr_map_input - added const.
47  * Added support for C++, headers uses extern "C" for public definitions.
48  *
49  * Revision 1.6  1997/09/04 13:54:40  adam
50  * Added MARC filter - type grs.marc.<syntax> where syntax refers
51  * to abstract syntax. New method tellf in retrieve/extract method.
52  *
53  * Revision 1.5  1997/07/15 16:29:03  adam
54  * Initialized dummy variable to keep checker gcc happy.
55  *
56  * Revision 1.4  1997/04/30 08:56:08  quinn
57  * null
58  *
59  * Revision 1.2  1996/10/11  16:06:43  quinn
60  * Revision 1.3  1997/02/24 10:41:50  adam
61  * Cleanup of code and commented out the "end element-end-record" code.
62  *
63  * Revision 1.2  1996/10/11 16:06:43  quinn
64  * Fixed arguments to nodetogr
65  *
66  * Revision 1.1  1996/10/11  10:57:25  adam
67  * New module recctrl. Used to manage records (extract/retrieval).
68  *
69  * Revision 1.29  1996/10/08 10:30:21  quinn
70  * Fixed type mismatch
71  *
72  * Revision 1.28  1996/10/07  16:06:40  quinn
73  * Added SOIF support
74  *
75  * Revision 1.27  1996/06/11  10:54:12  quinn
76  * Relevance work
77  *
78  * Revision 1.26  1996/06/06  12:08:45  quinn
79  * Added showRecord function
80  *
81  * Revision 1.25  1996/06/04  14:18:53  quinn
82  * Charmap work
83  *
84  * Revision 1.24  1996/06/04  13:27:54  quinn
85  * More work on charmapping
86  *
87  * Revision 1.23  1996/06/04  10:19:01  adam
88  * Minor changes - removed include of ctype.h.
89  *
90  * Revision 1.22  1996/06/03  10:15:27  quinn
91  * Various character-mapping.
92  *
93  * Revision 1.21  1996/05/31  13:27:24  quinn
94  * Character-conversion in phrases, too.
95  *
96  * Revision 1.19  1996/05/16  15:31:14  quinn
97  * a7
98  *
99  * Revision 1.18  1996/05/09  07:28:56  quinn
100  * Work towards phrases and multiple registers
101  *
102  * Revision 1.17  1996/05/01  13:46:37  adam
103  * First work on multiple records in one file.
104  * New option, -offset, to the "unread" command in the filter module.
105  *
106  * Revision 1.16  1996/01/17  14:57:54  adam
107  * Prototype changed for reader functions in extract/retrieve. File
108  *  is identified by 'void *' instead of 'int.
109  *
110  * Revision 1.15  1996/01/08  19:15:47  adam
111  * New input filter that works!
112  *
113  * Revision 1.14  1995/12/15  12:36:11  adam
114  * Retrieval calls data1_read_regx when subType is specified.
115  *
116  * Revision 1.13  1995/12/15  12:24:43  quinn
117  * *** empty log message ***
118  *
119  * Revision 1.12  1995/12/15  12:20:28  quinn
120  * *** empty log message ***
121  *
122  * Revision 1.11  1995/12/15  12:07:57  quinn
123  * Changed extraction strategy.
124  *
125  * Revision 1.10  1995/12/14  11:10:48  quinn
126  * Explain work
127  *
128  * Revision 1.9  1995/12/13  17:14:05  quinn
129  * *** empty log message ***
130  *
131  * Revision 1.8  1995/12/13  15:33:18  quinn
132  * *** empty log message ***
133  *
134  * Revision 1.7  1995/12/13  13:45:39  quinn
135  * Changed data1 to use nmem.
136  *
137  * Revision 1.6  1995/12/04  14:22:30  adam
138  * Extra arg to recType_byName.
139  * Started work on new regular expression parsed input to
140  * structured records.
141  *
142  * Revision 1.5  1995/11/28  14:18:37  quinn
143  * Set output_format.
144  *
145  * Revision 1.4  1995/11/21  13:14:49  quinn
146  * Fixed end-of-data-field problem (maybe).
147  *
148  * Revision 1.3  1995/11/15  19:13:09  adam
149  * Work on record management.
150  *
151  */
152
153 #include <stdio.h>
154 #include <assert.h>
155 #include <sys/types.h>
156 #ifndef WINDOWS
157 #include <unistd.h>
158 #endif
159
160 #include <log.h>
161 #include <oid.h>
162
163 #include <recctrl.h>
164 #include "grsread.h"
165
166 #define GRS_MAX_WORD 512
167
168 static data1_node *read_grs_type (struct grs_read_info *p, const char *type)
169 {
170     static struct {
171         char *type;
172         data1_node *(*func)(struct grs_read_info *p);
173     } tab[] = {
174         { "sgml",  grs_read_sgml },
175         { "regx",  grs_read_regx },
176         { "marc",  grs_read_marc },
177         { NULL, NULL }
178     };
179     const char *cp = strchr (type, '.');
180     int i;
181
182     if (cp == NULL || cp == type)
183     {
184         cp = strlen(type) + type;
185         *p->type = 0;
186     }
187     else
188         strcpy (p->type, cp+1);
189     for (i=0; tab[i].type; i++)
190     {
191         if (!memcmp (type, tab[i].type, cp-type))
192             return (tab[i].func)(p);
193     }
194     return NULL;
195 }
196
197 static void grs_init(void)
198 {
199 }
200
201 static int dumpkeys(data1_node *n, struct recExtractCtrl *p, int level)
202 {
203     RecWord wrd;
204     (*p->init)(p, &wrd);      /* set defaults */
205     for (; n; n = n->next)
206     {
207         if (p->flagShowRecords) /* display element description to user */
208         {
209             if (n->which == DATA1N_root)
210             {
211                 printf("%*s", level * 4, "");
212                 printf("Record type: '%s'\n", n->u.root.absyn->name);
213             }
214             else if (n->which == DATA1N_tag)
215             {
216                 data1_element *e;
217
218                 printf("%*s", level * 4, "");
219                 if (!(e = n->u.tag.element))
220                     printf("Local tag: '%s'\n", n->u.tag.tag);
221                 else
222                 {
223                     printf("Elm: '%s' ", e->name);
224                     if (e->tag)
225                     {
226                         data1_tag *t = e->tag;
227
228                         printf("TagNam: '%s' ", t->names->name);
229                         printf("(");
230                         if (t->tagset)
231                             printf("%s[%d],", t->tagset->name, t->tagset->type);
232                         else
233                             printf("?,");
234                         if (t->which == DATA1T_numeric)
235                             printf("%d)", t->value.numeric);
236                         else
237                             printf("'%s')", t->value.string);
238                     }
239                     printf("\n");
240                 }
241             }
242         }
243
244         if (n->child)
245             if (dumpkeys(n->child, p, level + 1) < 0)
246                 return -1;
247
248         if (n->which == DATA1N_data)
249         {
250             data1_node *par = get_parent_tag(p->dh, n);
251             data1_termlist *tlist = 0;
252             data1_datatype dtype = DATA1K_string;
253
254             if (p->flagShowRecords)
255             {
256                 printf("%*s", level * 4, "");
257                 printf("Data: ");
258                 if (n->u.data.len > 32)
259                     printf("'%.24s ... %.6s'\n", n->u.data.data,
260                            n->u.data.data + n->u.data.len-6);
261                 else if (n->u.data.len > 0)
262                     printf("'%.*s'\n", n->u.data.len, n->u.data.data);
263                 else
264                     printf("NULL\n");
265             }
266
267             assert(par);
268
269             /*
270              * cycle up towards the root until we find a tag with an att..
271              * this has the effect of indexing locally defined tags with
272              * the attribute of their ancestor in the record.
273              */
274
275             while (!par->u.tag.element)
276                 if (!par->parent || !(par=get_parent_tag(p->dh, par->parent)))
277                     break;
278             if (!par || !(tlist = par->u.tag.element->termlists))
279                 continue;
280             if (par->u.tag.element->tag)
281                 dtype = par->u.tag.element->tag->kind;
282             for (; tlist; tlist = tlist->next)
283             {
284                 if (p->flagShowRecords)
285                 {
286                     printf("%*sIdx: [%s]", (level + 1) * 4, "",
287                            tlist->structure);
288                     printf("%s:%s [%d]\n",
289                            tlist->att->parent->name,
290                            tlist->att->name, tlist->att->value);
291                 }
292                 else
293                 {
294                     wrd.reg_type = *tlist->structure;
295                     wrd.string = n->u.data.data;
296                     wrd.length = n->u.data.len;
297                     wrd.attrSet = tlist->att->parent->ordinal;
298                     wrd.attrUse = tlist->att->locals->local;
299                     (*p->add)(&wrd);
300                 }
301             }
302         }
303         if (p->flagShowRecords && n->which == DATA1N_root)
304         {
305             printf("%*s-------------\n\n", level * 4, "");
306         }
307     }
308     return 0;
309 }
310
311 static int grs_extract(struct recExtractCtrl *p)
312 {
313     data1_node *n;
314     NMEM mem;
315     struct grs_read_info gri;
316
317     mem = nmem_create (); 
318     gri.readf = p->readf;
319     gri.seekf = p->seekf;
320     gri.tellf = p->tellf;
321     gri.endf = p->endf;
322     gri.fh = p->fh;
323     gri.offset = p->offset;
324     gri.mem = mem;
325     gri.dh = p->dh;
326
327     n = read_grs_type (&gri, p->subType);
328     if (!n)
329         return -1;
330     if (dumpkeys(n, p, 0) < 0)
331     {
332         data1_free_tree(p->dh, n);
333         return -2;
334     }
335     data1_free_tree(p->dh, n);
336     nmem_destroy(mem);
337     return 0;
338 }
339
340 /*
341  * Return: -1: Nothing done. 0: Ok. >0: Bib-1 diagnostic.
342  */
343 static int process_comp(data1_handle dh, data1_node *n, Z_RecordComposition *c)
344 {
345     data1_esetname *eset;
346     Z_Espec1 *espec = 0;
347     Z_ElementSpec *p;
348
349     switch (c->which)
350     {
351         case Z_RecordComp_simple:
352             if (c->u.simple->which != Z_ElementSetNames_generic)
353                 return 26; /* only generic form supported. Fix this later */
354             if (!(eset = data1_getesetbyname(dh, n->u.root.absyn,
355                 c->u.simple->u.generic)))
356             {
357                 logf(LOG_LOG, "Unknown esetname '%s'", c->u.simple->u.generic);
358                 return 25; /* invalid esetname */
359             }
360             logf(LOG_DEBUG, "Esetname '%s' in simple compspec",
361                 c->u.simple->u.generic);
362             espec = eset->spec;
363             break;
364         case Z_RecordComp_complex:
365             if (c->u.complex->generic)
366             {
367                 /* insert check for schema */
368                 if ((p = c->u.complex->generic->elementSpec))
369                     switch (p->which)
370                     {
371                         case Z_ElementSpec_elementSetName:
372                             if (!(eset =
373                                   data1_getesetbyname(dh,
374                                                       n->u.root.absyn,
375                                                       p->u.elementSetName)))
376                             {
377                                 logf(LOG_LOG, "Unknown esetname '%s'",
378                                     p->u.elementSetName);
379                                 return 25; /* invalid esetname */
380                             }
381                             logf(LOG_DEBUG, "Esetname '%s' in complex compspec",
382                                 p->u.elementSetName);
383                             espec = eset->spec;
384                             break;
385                         case Z_ElementSpec_externalSpec:
386                             if (p->u.externalSpec->which == Z_External_espec1)
387                             {
388                                 logf(LOG_DEBUG, "Got Espec-1");
389                                 espec = p->u.externalSpec-> u.espec1;
390                             }
391                             else
392                             {
393                                 logf(LOG_LOG, "Unknown external espec.");
394                                 return 25; /* bad. what is proper diagnostic? */
395                             }
396                             break;
397                     }
398             }
399             else
400                 return 26; /* fix */
401     }
402     if (espec)
403     {
404         logf (LOG_LOG, "Element: Espec-1 match");
405         return data1_doespec1(dh, n, espec);
406     }
407     else
408     {
409         logf (LOG_DEBUG, "Element: all match");
410         return -1;
411     }
412 }
413
414 static int grs_retrieve(struct recRetrieveCtrl *p)
415 {
416     data1_node *node = 0, *onode = 0;
417     data1_node *dnew;
418     data1_maptab *map;
419     int res, selected = 0;
420     NMEM mem;
421     struct grs_read_info gri;
422     
423     mem = nmem_create();
424     gri.readf = p->readf;
425     gri.seekf = p->seekf;
426     gri.tellf = p->tellf;
427     gri.endf = NULL;
428     gri.fh = p->fh;
429     gri.offset = 0;
430     gri.mem = mem;
431     gri.dh = p->dh;
432
433     logf (LOG_DEBUG, "grs_retrieve");
434     node = read_grs_type (&gri, p->subType);
435     if (!node)
436     {
437         p->diagnostic = 14;
438         nmem_destroy (mem);
439         return 0;
440     }
441     logf (LOG_DEBUG, "grs_retrieve: size");
442     if ((dnew = data1_insert_taggeddata(p->dh, node, node,
443                                        "size", mem)))
444     {
445         dnew->u.data.what = DATA1I_text;
446         dnew->u.data.data = dnew->lbuf;
447         sprintf(dnew->u.data.data, "%d", p->recordSize);
448         dnew->u.data.len = strlen(dnew->u.data.data);
449     }
450
451     logf (LOG_DEBUG, "grs_retrieve: score");
452     if (p->score >= 0 && (dnew =
453                           data1_insert_taggeddata(p->dh, node,
454                                                   node, "rank",
455                                                   mem)))
456     {
457         dnew->u.data.what = DATA1I_num;
458         dnew->u.data.data = dnew->lbuf;
459         sprintf(dnew->u.data.data, "%d", p->score);
460         dnew->u.data.len = strlen(dnew->u.data.data);
461     }
462
463     logf (LOG_DEBUG, "grs_retrieve: localControlNumber");
464     if ((dnew = data1_insert_taggeddata(p->dh, node, node,
465                                        "localControlNumber", mem)))
466     {
467         dnew->u.data.what = DATA1I_text;
468         dnew->u.data.data = dnew->lbuf;
469         sprintf(dnew->u.data.data, "%d", p->localno);
470         dnew->u.data.len = strlen(dnew->u.data.data);
471     }
472
473     logf (LOG_DEBUG, "grs_retrieve: schemaIdentifier");
474     if (p->input_format == VAL_GRS1 && node->u.root.absyn &&
475         node->u.root.absyn->reference != VAL_NONE)
476     {
477         oident oe;
478         Odr_oid *oid;
479         int oidtmp[OID_SIZE];
480
481         oe.proto = PROTO_Z3950;
482         oe.oclass = CLASS_SCHEMA;
483         oe.value = node->u.root.absyn->reference;
484
485         if ((oid = oid_ent_to_oid (&oe, oidtmp)))
486         {
487             char tmp[128];
488             data1_handle dh = p->dh;
489             char *p = tmp;
490             int *ii;
491
492             for (ii = oid; *ii >= 0; ii++)
493             {
494                 if (p != tmp)
495                     *(p++) = '.';
496                 sprintf(p, "%d", *ii);
497                 p += strlen(p);
498             }
499             *(p++) = '\0';
500
501             if ((dnew = data1_insert_taggeddata(dh, node, node,
502                                                "schemaIdentifier", mem)))
503             {
504                 dnew->u.data.what = DATA1I_oid;
505                 dnew->u.data.data = nmem_malloc(mem, p - tmp);
506                 memcpy(dnew->u.data.data, tmp, p - tmp);
507                 dnew->u.data.len = p - tmp;
508             }
509         }
510     }
511
512     logf (LOG_DEBUG, "grs_retrieve: schema mapping");
513     /*
514      * Does the requested format match a known schema-mapping? (this reflects
515      * the overlap of schema and formatting which is inherent in the MARC
516      * family)
517      * NOTE: This should look at the schema-specification in the compspec
518      * as well.
519      */
520     for (map = node->u.root.absyn->maptabs; map; map = map->next)
521         if (map->target_absyn_ref == p->input_format)
522         {
523             onode = node;
524             if (!(node = data1_map_record(p->dh, onode, map, mem)))
525             {
526                 p->diagnostic = 14;
527                 nmem_destroy (mem);
528                 return 0;
529             }
530             break;
531         }
532
533     logf (LOG_DEBUG, "grs_retrieve: element spec");
534     if (p->comp && (res = process_comp(p->dh, node, p->comp)) > 0)
535     {
536         p->diagnostic = res;
537         if (onode)
538             data1_free_tree(p->dh, onode);
539         data1_free_tree(p->dh, node);
540         nmem_destroy(mem);
541         return 0;
542     }
543     else if (p->comp && !res)
544         selected = 1;
545
546     logf (LOG_DEBUG, "grs_retrieve: transfer syntax mapping");
547     switch (p->output_format = (p->input_format != VAL_NONE ?
548         p->input_format : VAL_SUTRS))
549     {
550         data1_marctab *marctab;
551         int dummy;
552
553         case VAL_GRS1:
554             dummy = 0;
555             if (!(p->rec_buf = data1_nodetogr(p->dh, node, selected,
556                                               p->odr, &dummy)))
557                 p->diagnostic = 238; /* not available in requested syntax */
558             else
559                 p->rec_len = -1;
560             break;
561         case VAL_EXPLAIN:
562             if (!(p->rec_buf = data1_nodetoexplain(p->dh, node, selected,
563                                                    p->odr)))
564                 p->diagnostic = 238;
565             else
566                 p->rec_len = -1;
567             break;
568         case VAL_SUMMARY:
569             if (!(p->rec_buf = data1_nodetosummary(p->dh, node, selected,
570                                                    p->odr)))
571                 p->diagnostic = 238;
572             else
573                 p->rec_len = -1;
574             break;
575         case VAL_SUTRS:
576             if (!(p->rec_buf = data1_nodetobuf(p->dh, node, selected,
577                 (int*)&p->rec_len)))
578                 p->diagnostic = 238;
579             break;
580         case VAL_SOIF:
581             if (!(p->rec_buf = data1_nodetosoif(p->dh, node, selected,
582                                                 (int*)&p->rec_len)))
583                 p->diagnostic = 238;
584             break;
585         default:
586             if (!node->u.root.absyn)
587             {
588                 p->diagnostic = 238;
589                 break;
590             }
591             for (marctab = node->u.root.absyn->marc; marctab;
592                 marctab = marctab->next)
593                 if (marctab->reference == p->input_format)
594                     break;
595             if (!marctab)
596             {
597                 p->diagnostic = 238;
598                 break;
599             }
600             if (!(p->rec_buf = data1_nodetomarc(p->dh, marctab, node,
601                                                 selected,
602                                                 (int*)&p->rec_len)))
603             {
604                 p->diagnostic = 238;
605                 break;
606             }
607     }
608     if (node)
609         data1_free_tree(p->dh, node);
610     if (onode)
611         data1_free_tree(p->dh, onode);
612     nmem_destroy(mem);
613     return 0;
614 }
615
616 static struct recType grs_type =
617 {
618     "grs",
619     grs_init,
620     grs_extract,
621     grs_retrieve
622 };
623
624 RecType recTypeGrs = &grs_type;