Improved installation. Updated for inclusion of YAZ header files.
[idzebra-moved-to-github.git] / util / zebramap.c
1 /*
2  * Copyright (C) 1994-1999, Index Data 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: zebramap.c,v $
7  * Revision 1.19  1999-11-30 13:48:04  adam
8  * Improved installation. Updated for inclusion of YAZ header files.
9  *
10  * Revision 1.18  1999/10/15 08:27:46  adam
11  * Fixed replace handler. 8-bit fix.
12  *
13  * Revision 1.17  1999/09/08 12:13:21  adam
14  * Fixed minor bug "replace"-mappings. Removed some logging messages.
15  *
16  * Revision 1.16  1999/09/07 07:19:21  adam
17  * Work on character mapping. Implemented replace rules.
18  *
19  * Revision 1.15  1999/05/26 07:49:14  adam
20  * C++ compilation.
21  *
22  * Revision 1.14  1999/02/19 10:37:40  adam
23  * Minor fix.
24  *
25  * Revision 1.13  1999/02/18 15:01:04  adam
26  * Structure=key uses register type 0.
27  *
28  * Revision 1.12  1999/02/12 13:29:25  adam
29  * Implemented position-flag for registers.
30  *
31  * Revision 1.11  1998/10/13 20:09:19  adam
32  * Changed call to readconf_line.
33  *
34  * Revision 1.10  1998/06/23 15:33:37  adam
35  * Added feature to specify sort criteria in query (type 7 specifies
36  * sort flags).
37  *
38  * Revision 1.9  1998/04/02 14:35:30  adam
39  * First version of Zebra that works with compiled ASN.1.
40  *
41  * Revision 1.8  1998/03/05 08:42:44  adam
42  * Minor changes to zebramap data structures. Query mapping rules changed.
43  *
44  * Revision 1.7  1998/02/10 12:03:07  adam
45  * Implemented Sort.
46  *
47  * Revision 1.6  1998/01/29 13:36:01  adam
48  * Structure word-list, free-form-text and document-text all
49  * trigger ranked search.
50  *
51  * Revision 1.5  1997/11/19 10:22:14  adam
52  * Bug fix (introduced by previous commit).
53  *
54  * Revision 1.4  1997/11/18 10:05:08  adam
55  * Changed character map facility so that admin can specify character
56  * mapping files for each register type, w, p, etc.
57  *
58  * Revision 1.3  1997/11/17 15:35:26  adam
59  * Bug fix. Relation=relevance wasn't observed.
60  *
61  * Revision 1.2  1997/10/31 12:39:30  adam
62  * Changed log message.
63  *
64  * Revision 1.1  1997/10/27 14:33:06  adam
65  * Moved towards generic character mapping depending on "structure"
66  * field in abstract syntax file. Fixed a few memory leaks. Fixed
67  * bug with negative integers when doing searches with relational
68  * operators.
69  *
70  */
71
72 #include <assert.h>
73 #include <ctype.h>
74
75 #include <yaz/yaz-util.h>
76 #include <charmap.h>
77 #include <zebramap.h>
78
79 #define ZEBRA_MAP_TYPE_SORT  1
80 #define ZEBRA_MAP_TYPE_INDEX 2
81
82 #define ZEBRA_REPLACE_ANY  300
83
84 struct zm_token {
85     int *token_from;
86     char *token_to;
87     int token_min;
88     struct zm_token *next;
89 };
90
91 struct zebra_map {
92     unsigned reg_id;
93     int completeness;
94     int positioned;
95     int type;
96     union {
97         struct {
98             int dummy;
99         } index;
100         struct {
101             int entry_size;
102         } sort;
103     } u;
104     chrmaptab maptab;
105     const char *maptab_name;
106     struct zebra_map *next;
107     struct zm_token *replace_tokens;
108 };
109
110 struct zebra_maps {
111     char *tabpath;
112     NMEM nmem;
113     struct zebra_map *map_list;
114     char temp_map_str[2];
115     const char *temp_map_ptr[2];
116     struct zebra_map **lookup_array;
117     WRBUF wrbuf_1, wrbuf_2;
118 };
119
120 void zebra_maps_close (ZebraMaps zms)
121 {
122     struct zebra_map *zm = zms->map_list;
123     while (zm)
124     {
125         if (zm->maptab)
126             chrmaptab_destroy (zm->maptab);
127         zm = zm->next;
128     }
129     wrbuf_free (zms->wrbuf_1, 1);
130     wrbuf_free (zms->wrbuf_2, 1);
131     nmem_destroy (zms->nmem);
132     xfree (zms);
133 }
134
135 static void zebra_map_read (ZebraMaps zms, const char *name)
136 {
137     FILE *f;
138     char line[512];
139     char *argv[10];
140     int argc;
141     int lineno = 0;
142     struct zebra_map **zm = 0, *zp;
143
144     if (!(f = yaz_path_fopen(zms->tabpath, name, "r")))
145     {
146         logf(LOG_WARN|LOG_ERRNO, "%s", name);
147         return ;
148     }
149     while ((argc = readconf_line(f, &lineno, line, 512, argv, 10)))
150     {
151         if (!yaz_matchstr (argv[0], "index") && argc == 2)
152         {
153             if (!zm)
154                 zm = &zms->map_list;
155             else
156                 zm = &(*zm)->next;
157             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
158             (*zm)->reg_id = argv[1][0];
159             (*zm)->maptab_name = NULL;
160             (*zm)->maptab = NULL;
161             (*zm)->type = ZEBRA_MAP_TYPE_INDEX;
162             (*zm)->completeness = 0;
163             (*zm)->positioned = 1;
164             (*zm)->replace_tokens = 0;
165         }
166         else if (!yaz_matchstr (argv[0], "sort") && argc == 2)
167         {
168             if (!zm)
169                 zm = &zms->map_list;
170             else
171                 zm = &(*zm)->next;
172             *zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(**zm));
173             (*zm)->reg_id = argv[1][0];
174             (*zm)->maptab_name = NULL;
175             (*zm)->type = ZEBRA_MAP_TYPE_SORT;
176             (*zm)->u.sort.entry_size = 80;
177             (*zm)->maptab = NULL;
178             (*zm)->completeness = 0;
179             (*zm)->positioned = 0;
180             (*zm)->replace_tokens = 0;
181         }
182         else if (zm && !yaz_matchstr (argv[0], "charmap") && argc == 2)
183         {
184             (*zm)->maptab_name = nmem_strdup (zms->nmem, argv[1]);
185         }
186         else if (zm && !yaz_matchstr (argv[0], "completeness") && argc == 2)
187         {
188             (*zm)->completeness = atoi (argv[1]);
189         }
190         else if (zm && !yaz_matchstr (argv[0], "position") && argc == 2)
191         {
192             (*zm)->positioned = atoi (argv[1]);
193         }
194         else if (zm && !yaz_matchstr (argv[0], "entrysize") && argc == 2)
195         {
196             if ((*zm)->type == ZEBRA_MAP_TYPE_SORT)
197                 (*zm)->u.sort.entry_size = atoi (argv[1]);
198         }
199         else if (zm && !yaz_matchstr (argv[0], "replace") && argc >= 2)
200         {
201             struct zm_token *token = nmem_malloc (zms->nmem, sizeof(*token));
202             token->next = (*zm)->replace_tokens;
203             (*zm)->replace_tokens = token;
204 #if 0
205             logf (LOG_LOG, "replace %s", argv[1]);
206 #endif
207             token->token_from = 0;
208             if (argc >= 2)
209             {
210                 char *cp = argv[1];
211                 int *dp = token->token_from = (int *)
212                     nmem_malloc (zms->nmem, (1+strlen(cp))*sizeof(int));
213                 while (*cp)
214                     if (*cp == '$')
215                     {
216                         *dp++ = ' ';
217                         cp++;
218                     }
219                     else if (*cp == '.')
220                     {
221                         *dp++ = ZEBRA_REPLACE_ANY;
222                         cp++;
223                     }
224                     else
225                     {
226                         *dp++ = zebra_prim(&cp);
227 #if 0
228                         logf (LOG_LOG, "  char %2X %c", dp[-1], dp[-1]);
229 #endif
230                     }
231                 *dp = '\0';
232             }
233             if (argc >= 3)
234             {
235                 char *cp = argv[2];
236                 char *dp = token->token_to =
237                     nmem_malloc (zms->nmem, strlen(cp)+1);
238                 while (*cp)
239                     if (*cp == '$')
240                     {
241                         *dp++ = ' ';
242                         cp++;
243                     }
244                     else
245                         *dp++ = zebra_prim(&cp);
246                 *dp = '\0';
247             }
248             else
249                 token->token_to = 0;
250         }
251     }
252     if (zm)
253         (*zm)->next = NULL;
254     fclose (f);
255
256     for (zp = zms->map_list; zp; zp = zp->next)
257         zms->lookup_array[zp->reg_id] = zp;
258 }
259
260 static void zms_map_handle (void *p, const char *name, const char *value)
261 {
262     ZebraMaps zms = (ZebraMaps) p;
263     
264     zebra_map_read (zms, value);
265 }
266
267 ZebraMaps zebra_maps_open (Res res)
268 {
269     ZebraMaps zms = (ZebraMaps) xmalloc (sizeof(*zms));
270     int i;
271
272     zms->nmem = nmem_create ();
273     zms->tabpath = nmem_strdup (zms->nmem,
274                                 res_get_def (res, "profilePath", "."));
275     zms->map_list = NULL;
276
277     zms->temp_map_str[0] = '\0';
278     zms->temp_map_str[1] = '\0';
279
280     zms->temp_map_ptr[0] = zms->temp_map_str;
281     zms->temp_map_ptr[1] = NULL;
282
283     zms->lookup_array = (struct zebra_map**)
284         nmem_malloc (zms->nmem, sizeof(*zms->lookup_array)*256);
285     for (i = 0; i<256; i++)
286         zms->lookup_array[i] = 0;
287     if (!res || !res_trav (res, "index", zms, zms_map_handle))
288         zebra_map_read (zms, "default.idx");
289
290     zms->wrbuf_1 = wrbuf_alloc();
291     zms->wrbuf_2 = wrbuf_alloc();
292     return zms;
293 }
294
295 struct zebra_map *zebra_map_get (ZebraMaps zms, unsigned reg_id)
296 {
297     return zms->lookup_array[reg_id];
298 }
299
300 chrmaptab zebra_charmap_get (ZebraMaps zms, unsigned reg_id)
301 {
302     struct zebra_map *zm = zebra_map_get (zms, reg_id);
303     if (!zm)
304     {
305         zm = (struct zebra_map *) nmem_malloc (zms->nmem, sizeof(*zm));
306         logf (LOG_WARN, "Unknown register type: %c", reg_id);
307
308         zm->reg_id = reg_id;
309         zm->maptab_name = NULL;
310         zm->maptab = NULL;
311         zm->type = ZEBRA_MAP_TYPE_INDEX;
312         zm->completeness = 0;
313         zm->next = zms->map_list;
314         zms->map_list = zm->next;
315
316         zms->lookup_array[zm->reg_id & 255] = zm;
317     }
318     if (!zm->maptab)
319     {
320         if (!zm->maptab_name || !yaz_matchstr (zm->maptab_name, "@"))
321             return NULL;
322         if (!(zm->maptab = chrmaptab_create (zms->tabpath,
323                                              zm->maptab_name, 0)))
324             logf(LOG_WARN, "Failed to read character table %s",
325                  zm->maptab_name);
326         else
327             logf(LOG_DEBUG, "Read character table %s", zm->maptab_name);
328     }
329     return zm->maptab;
330 }
331
332 const char **zebra_maps_input (ZebraMaps zms, unsigned reg_id,
333                                const char **from, int len)
334 {
335     chrmaptab maptab;
336
337     maptab = zebra_charmap_get (zms, reg_id);
338     if (maptab)
339         return chr_map_input(maptab, from, len);
340     
341     zms->temp_map_str[0] = **from;
342
343     (*from)++;
344     return zms->temp_map_ptr;
345 }
346
347 #if 0
348 int zebra_maps_input_tokens (ZebraMaps zms, unsigned reg_id,
349                              const char *input_str, int input_len,
350                              WRBUF wrbuf)
351 {
352     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
353     int len[4];
354     char *str[3];
355     int input_i = 0;
356     int first = 1;
357     const char **out;
358         
359     if (!maptab)
360     {
361         wrbuf_write (wrbuf, input_str, input_len);
362         return -1;
363     }
364     str[0] = " ";
365     len[0] = 1;
366     str[1] = input_str;
367     len[1] = input_len;
368     str[2] = " ";
369     len[2] = 1;
370     len[3] = -1;
371     
372     out = chr_map_input (maptab, str, len);
373     while (len[1] > 0)
374     {
375         while (out && *out && **out == *CHR_SPACE)
376             out = chr_map_input (maptab, str, len);
377     }
378 }
379 #endif
380
381 const char *zebra_maps_output(ZebraMaps zms, unsigned reg_id,
382                               const char **from)
383 {
384     chrmaptab maptab = zebra_charmap_get (zms, reg_id);
385     if (!maptab)
386         return 0;
387     return chr_map_output (maptab, from, 1);
388 }
389
390
391 /* ------------------------------------ */
392
393 typedef struct {
394     int type;
395     int major;
396     int minor;
397     Z_AttributeElement **attributeList;
398     int num_attributes;
399 } AttrType;
400
401 static int attr_find (AttrType *src, oid_value *attributeSetP)
402 {
403     while (src->major < src->num_attributes)
404     {
405         Z_AttributeElement *element;
406
407         element = src->attributeList[src->major];
408         if (src->type == *element->attributeType)
409         {
410             switch (element->which) 
411             {
412             case Z_AttributeValue_numeric:
413                 ++(src->major);
414                 if (element->attributeSet && attributeSetP)
415                 {
416                     oident *attrset;
417
418                     attrset = oid_getentbyoid (element->attributeSet);
419                     *attributeSetP = attrset->value;
420                 }
421                 return *element->value.numeric;
422                 break;
423             case Z_AttributeValue_complex:
424                 if (src->minor >= element->value.complex->num_list ||
425                     element->value.complex->list[src->minor]->which !=  
426                     Z_StringOrNumeric_numeric)
427                     break;
428                 ++(src->minor);
429                 if (element->attributeSet && attributeSetP)
430                 {
431                     oident *attrset;
432
433                     attrset = oid_getentbyoid (element->attributeSet);
434                     *attributeSetP = attrset->value;
435                 }
436                 return *element->value.complex->list[src->minor-1]->u.numeric;
437             default:
438                 assert (0);
439             }
440         }
441         ++(src->major);
442     }
443     return -1;
444 }
445
446 static void attr_init_APT (AttrType *src, Z_AttributesPlusTerm *zapt, int type)
447 {
448 #ifdef ASN_COMPILED
449     src->attributeList = zapt->attributes->attributes;
450     src->num_attributes = zapt->attributes->num_attributes;
451 #else
452     src->attributeList = zapt->attributeList;
453     src->num_attributes = zapt->num_attributes;
454 #endif
455     src->type = type;
456     src->major = 0;
457     src->minor = 0;
458 }
459
460 static void attr_init_AttrList (AttrType *src, Z_AttributeList *list, int type)
461 {
462     src->attributeList = list->attributes;
463     src->num_attributes = list->num_attributes;
464     src->type = type;
465     src->major = 0;
466     src->minor = 0;
467 }
468
469 /* ------------------------------------ */
470
471 int zebra_maps_is_complete (ZebraMaps zms, unsigned reg_id)
472
473     struct zebra_map *zm = zebra_map_get (zms, reg_id);
474     if (zm)
475         return zm->completeness;
476     return 0;
477 }
478
479 int zebra_maps_is_positioned (ZebraMaps zms, unsigned reg_id)
480 {
481     struct zebra_map *zm = zebra_map_get (zms, reg_id);
482     if (zm)
483         return zm->positioned;
484     return 0;
485 }
486     
487 int zebra_maps_is_sort (ZebraMaps zms, unsigned reg_id)
488 {
489     struct zebra_map *zm = zebra_map_get (zms, reg_id);
490     if (zm)
491         return zm->type == ZEBRA_MAP_TYPE_SORT;
492     return 0;
493 }
494
495 int zebra_maps_sort (ZebraMaps zms, Z_SortAttributes *sortAttributes)
496 {
497     AttrType use;
498     attr_init_AttrList (&use, sortAttributes->list, 1);
499
500     return attr_find (&use, NULL);
501 }
502
503 int zebra_maps_attr (ZebraMaps zms, Z_AttributesPlusTerm *zapt,
504                      unsigned *reg_id, char **search_type, char **rank_type,
505                      int *complete_flag, int *sort_flag)
506 {
507     AttrType completeness;
508     AttrType structure;
509     AttrType relation;
510     AttrType sort_relation;
511     int completeness_value;
512     int structure_value;
513     int relation_value;
514     int sort_relation_value;
515
516     attr_init_APT (&structure, zapt, 4);
517     attr_init_APT (&completeness, zapt, 6);
518     attr_init_APT (&relation, zapt, 2);
519     attr_init_APT (&sort_relation, zapt, 7);
520
521     completeness_value = attr_find (&completeness, NULL);
522     structure_value = attr_find (&structure, NULL);
523     relation_value = attr_find (&relation, NULL);
524     sort_relation_value = attr_find (&sort_relation, NULL);
525
526     if (completeness_value == 2 || completeness_value == 3)
527         *complete_flag = 1;
528     else
529         *complete_flag = 0;
530     *reg_id = 0;
531
532     *sort_flag = (sort_relation_value > 0) ? 1 : 0;
533     *search_type = "phrase";
534     *rank_type = "void";
535     if (relation_value == 102)
536         *rank_type = "rank";
537     
538     if (*complete_flag)
539         *reg_id = 'p';
540     else
541         *reg_id = 'w';
542     switch (structure_value)
543     {
544     case 6:   /* word list */
545         *search_type = "and-list";
546         break;
547     case 105: /* free-form-text */
548         *search_type = "or-list";
549         break;
550     case 106: /* document-text */
551         *search_type = "or-list";
552         break;  
553     case -1:
554     case 1:   /* phrase */
555     case 2:   /* word */
556     case 108: /* string */ 
557         *search_type = "phrase";
558         break;
559     case 107: /* local-number */
560         *search_type = "local";
561         *reg_id = 0;
562         break;
563     case 109: /* numeric string */
564         *reg_id = 'n';
565         *search_type = "numeric";
566         break;
567     case 104: /* urx */
568         *reg_id = 'u';
569         *search_type = "phrase";
570         break;
571     case 3:   /* key */
572         *reg_id = '0';
573         *search_type = "phrase";
574         break;
575     default:
576         return -1;
577     }
578     return 0;
579 }
580
581 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
582                       const char *input_str, int input_len, WRBUF wrbuf);
583
584 WRBUF zebra_replace(ZebraMaps zms, unsigned reg_id, const char *ex_list,
585                     const char *input_str, int input_len)
586 {
587     struct zebra_map *zm = zebra_map_get (zms, reg_id);
588
589     wrbuf_rewind(zms->wrbuf_1);
590     wrbuf_write(zms->wrbuf_1, input_str, input_len);
591     if (!zm->replace_tokens)
592         return zms->wrbuf_1;
593   
594 #if 0
595     logf (LOG_LOG, "in:%.*s:", wrbuf_len(zms->wrbuf_1),
596           wrbuf_buf(zms->wrbuf_1));
597 #endif
598     for (;;)
599     {
600         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_1),
601                                wrbuf_len(zms->wrbuf_1), zms->wrbuf_2))
602             return zms->wrbuf_2;
603         if (!zebra_replace_sub(zms, reg_id, ex_list, wrbuf_buf(zms->wrbuf_2),
604                                wrbuf_len(zms->wrbuf_2), zms->wrbuf_1))
605             return zms->wrbuf_1;
606     }
607     return 0;
608 }
609
610 int zebra_replace_sub(ZebraMaps zms, unsigned reg_id, const char *ex_list,
611                       const char *input_str, int input_len, WRBUF wrbuf)
612 {
613     int i = -1;
614     int no_replaces = 0;
615     struct zebra_map *zm = zebra_map_get (zms, reg_id);
616
617     wrbuf_rewind(wrbuf);
618     for (i = -1; i <= input_len; )
619     {
620         struct zm_token *token;
621         char replace_string[128];
622         int replace_out;
623         int replace_in = 0;
624
625         for (token = zm->replace_tokens; !replace_in && token;
626              token = token->next)
627         {
628             int j = 0;
629             int replace_done = 0;
630             replace_out = 0;
631             for (;; j++)
632             {
633                 int c;
634                 if (!token->token_from[j])
635                 {
636                     replace_in = j;
637                     break;
638                 }
639                 if (ex_list && strchr (ex_list, token->token_from[j]))
640                     break;
641                 if (i+j < 0 || j+i >= input_len)
642                     c = ' ';
643                 else
644                     c = input_str[j+i] & 255;
645                 if (token->token_from[j] == ZEBRA_REPLACE_ANY)
646                 {
647                     if (c == ' ')
648                         break;
649                     replace_string[replace_out++] = c;
650                 }
651                 else
652                 {
653                     if (c != token->token_from[j])
654                     {
655                         break;
656                     }
657                     if (!replace_done)
658                     {
659                         const char *cp = token->token_to;
660                         replace_done = 1;
661                         for (; cp && *cp; cp++)
662                             replace_string[replace_out++] = *cp;
663                     }
664                 }
665             }
666         }
667         if (!replace_in)
668         {
669             if (i >= 0 && i < input_len)
670                 wrbuf_putc(wrbuf, input_str[i]);
671             i++;
672         }
673         else
674         {
675             no_replaces++;
676             if (replace_out)
677                 wrbuf_write(wrbuf, replace_string, replace_out);
678             i += replace_in;
679         }
680     }
681 #if 0
682     logf (LOG_LOG, "out:%.*s:", wrbuf_len(wrbuf), wrbuf_buf(wrbuf));
683 #endif
684     return no_replaces;
685 }