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