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