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