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