Update to new FSF address. GPLv2
[idzebra-moved-to-github.git] / util / charmap.c
1 /* $Id: charmap.c,v 1.29.2.6 2006-04-04 09:11:30 adam Exp $
2    Copyright (C) 1995-2005
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 /*
26  * Support module to handle character-conversions into and out of the
27  * Zebra dictionary.
28  */
29
30 #include <ctype.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <assert.h>
34
35 typedef unsigned ucs4_t;
36
37 #include <charmap.h>
38
39 #include <yaz/yaz-util.h>
40
41 #define CHR_MAXSTR 1024
42 #define CHR_MAXEQUIV 32
43
44 const unsigned char CHR_FIELD_BEGIN = '^';
45
46 const char *CHR_UNKNOWN = "\001";
47 const char *CHR_SPACE   = "\002";
48 const char *CHR_BASE    = "\003";
49 const char *CHR_CUT     = "\376";  /* high number to keep BASE stable */
50
51 struct chrmaptab_info
52 {
53     chr_t_entry *input;         /* mapping table for input data */
54     chr_t_entry *q_input;       /* mapping table for queries */
55     unsigned char *output[256]; /* return mapping - for display of registers */
56     int base_uppercase;         /* Start of upper-case ordinals */
57     NMEM nmem;
58 };
59
60 /*
61  * Character map trie node.
62  */
63 struct chr_t_entry
64 {
65     chr_t_entry **children;  /* array of children */
66     unsigned char **target;  /* target for this node, if any */
67 };
68
69 /*
70  * General argument structure for callback functions (internal use only)
71  */
72 typedef struct chrwork 
73 {
74     chrmaptab map;
75     char string[CHR_MAXSTR+1];
76 } chrwork;
77
78 /*
79  * Callback for equivalent stuff
80  */
81 typedef struct
82 {
83     NMEM nmem;
84     int no_eq;
85     char *eq[CHR_MAXEQUIV];
86 } chr_equiv_work;
87 /*
88  * Add an entry to the character map.
89  */
90 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
91                                    const char *from, int len, char *to,
92                                    const char *from_0)
93 {
94     if (!from_0)
95         from_0 = from;
96     if (!root)
97     {
98         root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
99         root->children = 0;
100         root->target = 0;
101     }
102     if (!len)
103     {
104         if (!root->target || !root->target[0] || strcmp((const char *)
105                                                         root->target[0], to))
106         {
107             if (from_0 && 
108                 root->target && root->target[0] && root->target[0][0] &&
109                 strcmp ((const char *)root->target[0], CHR_UNKNOWN))
110             {
111                 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
112                          from_0);
113             }
114             root->target = (unsigned char **)
115                 nmem_malloc(nmem, sizeof(*root->target)*2);
116             root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
117             root->target[1] = 0;
118         }
119     }
120     else
121     {
122         if (!root->children)
123         {
124             int i;
125
126             root->children = (chr_t_entry **)
127                 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
128             for (i = 0; i < 256; i++)
129                 root->children[i] = 0;
130         }
131         if (!(root->children[(unsigned char) *from] =
132             set_map_string(root->children[(unsigned char) *from], nmem,
133                            from + 1, len - 1, to, from_0)))
134             return 0;
135     }
136     return root;
137 }
138
139 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
140 {
141     chr_t_entry *res;
142
143     if (len && t->children && t->children[(unsigned char) **from])
144     {
145         const char *pos = *from;
146
147         (*from)++;
148         if ((res = find_entry(t->children[(unsigned char) *pos],
149             from, len - 1)))
150             return res;
151         /* no match */
152         *from = pos;
153     }
154     /* no children match. use ourselves, if we have a target */
155     return t->target ? t : 0;
156 }
157
158 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len, int first)
159 {
160     chr_t_entry *res;
161
162     while (*len <= 0)
163     {   /* switch to next buffer */
164         if (*len < 0)
165             break;
166         from++;
167         len++;
168     }
169     if (*len > 0 && t->children)
170     {
171         const char *old_from = *from;
172         int old_len = *len;
173
174         res = 0;
175
176         if (first && t->children[CHR_FIELD_BEGIN])
177         {
178             if ((res = find_entry_x(t->children[CHR_FIELD_BEGIN], from, len, 0)) && res != t->children[CHR_FIELD_BEGIN])
179                 return res;
180             else
181                 res = 0;
182             /* otherwhise there was no match on beginning of field, move on */
183         } 
184         
185         if (!res && t->children[(unsigned char) **from])
186         {
187             (*len)--;
188             (*from)++;
189             if ((res = find_entry_x(t->children[(unsigned char) *old_from],
190                                     from, len, 0)))
191                 return res;
192             /* no match */
193             *len = old_len;
194             *from = old_from;
195         }
196     }
197     /* no children match. use ourselves, if we have a target */
198     return t->target ? t : 0;
199 }
200
201 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len, int first)
202 {
203     chr_t_entry *t = maptab->input;
204     chr_t_entry *res;
205
206     if (!(res = find_entry_x(t, from, len, first)))
207         abort();
208     return (const char **) (res->target);
209 }
210
211 const char **chr_map_input(chrmaptab maptab, const char **from, int len, int first)
212 {
213     chr_t_entry *t = maptab->input;
214     chr_t_entry *res;
215     int len_tmp[2];
216
217     len_tmp[0] = len;
218     len_tmp[1] = -1;
219     if (!(res = find_entry_x(t, from, len_tmp, first)))
220         abort();
221     return (const char **) (res->target);
222 }
223
224 const char **chr_map_q_input(chrmaptab maptab,
225                              const char **from, int len, int first)
226 {
227     chr_t_entry *t = maptab->q_input;
228     chr_t_entry *res;
229     int len_tmp[2];
230     
231     len_tmp[0] = len;
232     len_tmp[1] = -1;
233     if (!(res = find_entry_x(t, from, len_tmp, first)))
234         return 0;
235     return (const char **) (res->target);
236 }
237
238 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
239 {
240     unsigned char c = ** (unsigned char **) from;
241     (*from)++;
242     return (const char*) maptab->output[c];
243 }
244
245 unsigned char zebra_prim(char **s)
246 {
247     unsigned char c;
248     unsigned int i = 0;
249
250     yaz_log (LOG_DEBUG, "prim %.3s", *s);
251     if (**s == '\\')
252     {
253         (*s)++;
254         c = **s;
255         switch (c)
256         {
257         case '\\': c = '\\'; (*s)++; break;
258         case 'r': c = '\r'; (*s)++; break;
259         case 'n': c = '\n'; (*s)++; break;
260         case 't': c = '\t'; (*s)++; break;
261         case 's': c = ' '; (*s)++; break;
262         case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
263         case '0':
264         case '1':
265         case '2':
266         case '3':
267         case '4':
268         case '5':
269         case '6':
270         case '7':
271         case '8':
272         case '9':
273             sscanf(*s, "%3o", &i);
274             c = i;
275             *s += 3;
276             break;
277         default:
278             (*s)++;
279         }
280     }
281     else
282     {
283         c = **s;
284         ++(*s);
285     }
286     return c;
287 }
288
289 static int zebra_ucs4_strlen(ucs4_t *s)
290 {
291     int i = 0;
292     while (*s++)
293         i++;
294     return i;
295 }
296
297 ucs4_t zebra_prim_w(ucs4_t **s)
298 {
299     ucs4_t c;
300     ucs4_t i = 0;
301     char fmtstr[8];
302
303     yaz_log (LOG_DEBUG, "prim_w %.3s", (char *) *s);
304     if (**s == '\\')
305     {
306         (*s)++;
307         c = **s;
308         switch (c)
309         {
310         case '\\': c = '\\'; (*s)++; break;
311         case 'r': c = '\r'; (*s)++; break;
312         case 'n': c = '\n'; (*s)++; break;
313         case 't': c = '\t'; (*s)++; break;
314         case 's': c = ' '; (*s)++; break;
315         case 'x': 
316             if (zebra_ucs4_strlen(*s) >= 3)
317             {
318                 fmtstr[0] = (*s)[1];
319                 fmtstr[1] = (*s)[2];
320                 fmtstr[2] = 0;
321                 sscanf(fmtstr, "%x", &i);
322                 c = i;
323                 *s += 3;
324             }
325             break;
326         case '0':
327         case '1':
328         case '2':
329         case '3':
330         case '4':
331         case '5':
332         case '6':
333         case '7':
334         case '8':
335         case '9':
336             if (zebra_ucs4_strlen(*s) >= 3)
337             {
338                 fmtstr[0] = (*s)[0];
339                 fmtstr[1] = (*s)[1];
340                 fmtstr[2] = (*s)[2];
341                 fmtstr[3] = 0;
342                 sscanf(fmtstr, "%o", &i);
343                 c = i;
344                 *s += 3;
345             }
346             break;
347         case 'L':
348             if (zebra_ucs4_strlen(*s) >= 5)
349             {
350                 fmtstr[0] = (*s)[1];
351                 fmtstr[1] = (*s)[2];
352                 fmtstr[2] = (*s)[3];
353                 fmtstr[3] = (*s)[4];
354                 fmtstr[4] = 0;
355                 sscanf(fmtstr, "%x", &i);
356                 c = i;
357                 *s += 5;
358             }
359             break;
360         default:
361             (*s)++;
362         }
363     }
364     else
365     {
366         c = **s;
367         ++(*s);
368     }
369     yaz_log (LOG_DEBUG, "out %d", c);
370     return c;
371 }
372
373 /*
374  * Callback function.
375  * Add an entry to the value space.
376  */
377 static void fun_addentry(const char *s, void *data, int num)
378 {
379     chrmaptab tab = (chrmaptab) data;
380     char tmp[2];
381     
382     tmp[0] = num; tmp[1] = '\0';
383     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
384     tab->output[num + tab->base_uppercase] =
385         (unsigned char *) nmem_strdup(tab->nmem, s);
386 }
387
388 /* 
389  * Callback function.
390  * Add a space-entry to the value space.
391  */
392 static void fun_addspace(const char *s, void *data, int num)
393 {
394     chrmaptab tab = (chrmaptab) data;
395     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
396                                 (char*) CHR_SPACE, 0);
397 }
398
399 /* 
400  * Callback function.
401  * Add a space-entry to the value space.
402  */
403 static void fun_addcut(const char *s, void *data, int num)
404 {
405     chrmaptab tab = (chrmaptab) data;
406     tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
407                                 (char*) CHR_CUT, 0);
408 }
409
410 /*
411  * Create a string containing the mapped characters provided.
412  */
413 static void fun_mkstring(const char *s, void *data, int num)
414 {
415     chrwork *arg = (chrwork *) data;
416     const char **res, *p = s;
417
418     res = chr_map_input(arg->map, &s, strlen(s), 0);
419     if (*res == (char*) CHR_UNKNOWN)
420         yaz_log(LOG_WARN, "Map: '%s' has no mapping", p);
421     strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
422     arg->string[CHR_MAXSTR] = '\0';
423 }
424
425 /*
426  * Create an unmodified string (scan_string handler).
427  */
428 static void fun_add_equivalent_string(const char *s, void *data, int num)
429 {
430     chr_equiv_work *arg = (chr_equiv_work *) data;
431     
432     if (arg->no_eq == CHR_MAXEQUIV)
433         return;
434     arg->eq[arg->no_eq++] = nmem_strdup(arg->nmem, s);
435 }
436
437 /*
438  * Add a map to the string contained in the argument.
439  */
440 static void fun_add_map(const char *s, void *data, int num)
441 {
442     chrwork *arg = (chrwork *) data;
443
444     assert(arg->map->input);
445     yaz_log (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
446     set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
447                    0);
448     for (s = arg->string; *s; s++)
449         yaz_log (LOG_DEBUG, " %3d", (unsigned char) *s);
450 }
451
452 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
453                         char *outbuf, size_t outbytesleft)
454 {
455     size_t inbytesleft = inlen * sizeof(ucs4_t);
456     char *inbuf = (char*) from;
457     size_t ret;
458    
459     if (t == 0)
460         *outbuf++ = *from;  /* ISO-8859-1 is OK here */
461     else
462     {
463         ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
464         if (ret == (size_t) (-1))
465         {
466             yaz_log(LOG_LOG, "from: %2X %2X %2X %2X",
467                     from[0], from[1], from[2], from[3]);
468             yaz_log (LOG_WARN|LOG_ERRNO, "bad unicode sequence");
469             return -1;
470         }
471     }
472     *outbuf = '\0';
473     return 0;
474 }
475
476 static int scan_string(char *s_native,
477                        yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
478                        void (*fun)(const char *c, void *data, int num),
479                        void *data, int *num)
480 {
481     char str[1024];
482
483     ucs4_t arg[512];
484     ucs4_t arg_prim[512];
485     ucs4_t *s0, *s = arg;
486     ucs4_t c, begin, end;
487     size_t i;
488
489     if (t_unicode != 0)
490     {
491         char *outbuf = (char *) arg;
492         char *inbuf = s_native;
493         size_t outbytesleft = sizeof(arg)-4;
494         size_t inbytesleft = strlen(s_native);
495         size_t ret;             
496         ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
497                         &outbuf, &outbytesleft);
498         if (ret == (size_t)(-1))
499             return -1;
500         i = (outbuf - (char*) arg)/sizeof(ucs4_t);
501     }
502     else
503     { 
504         for (i = 0; s_native[i]; i++)
505             arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
506     }
507     arg[i] = 0;      /* terminate */
508     if (s[0] == 0xfeff || s[0] == 0xfeff)  /* skip byte Order Mark */
509         s++;
510     while (*s)
511     {
512         switch (*s)
513         {
514         case '{':
515             s++;
516             begin = zebra_prim_w(&s);
517             if (*s != '-')
518             {
519                 yaz_log(LOG_FATAL, "Bad range in char-map");
520                 return -1;
521             }
522             s++;
523             end = zebra_prim_w(&s);
524             if (end <= begin)
525             {
526                 yaz_log(LOG_FATAL, "Bad range in char-map");
527                 return -1;
528             }
529             s++;
530             for (c = begin; c <= end; c++)
531             {
532                 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
533                     return -1;
534                 (*fun)(str, data, num ? (*num)++ : 0);
535             }
536             break;
537         case '[': s++; abort(); break;
538         case '(':
539             ++s;
540             s0 = s; i = 0;
541             while (*s != ')' || s[-1] == '\\')
542                 arg_prim[i++] = zebra_prim_w(&s);
543             arg_prim[i] = 0;
544             if (scan_to_utf8 (t_utf8, arg_prim, zebra_ucs4_strlen(arg_prim), str, sizeof(str)-1))
545                 return -1;
546             (*fun)(str, data, num ? (*num)++ : 0);
547             s++;
548             break;
549         default:
550             c = zebra_prim_w(&s);
551             if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
552                 return -1;
553             (*fun)(str, data, num ? (*num)++ : 0);
554         }
555     }
556     return 0;
557 }
558
559 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
560                            const char *tabroot)
561 {
562     FILE *f;
563     char line[512], *argv[50];
564     chrmaptab res;
565     int lineno = 0;
566     int errors = 0;
567     int argc, num = (int) *CHR_BASE, i;
568     NMEM nmem;
569     yaz_iconv_t t_unicode = 0;
570     yaz_iconv_t t_utf8 = 0;
571     unsigned endian = 31;
572     const char *ucs4_native = "UCS-4";
573
574     if (*(char*) &endian == 31)      /* little endian? */
575         ucs4_native = "UCS-4LE";
576
577     t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
578
579     yaz_log (LOG_DEBUG, "maptab %s open", name);
580     if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
581     {
582         yaz_log(LOG_WARN|LOG_ERRNO, "%s", name);
583         return 0;
584     }
585     nmem = nmem_create ();
586     res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
587     res->nmem = nmem;
588     res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
589     res->input->target = (unsigned char **)
590         nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
591     res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
592     res->input->target[1] = 0;
593     res->input->children = (chr_t_entry **)
594         nmem_malloc(res->nmem, sizeof(res->input) * 256);
595     for (i = 0; i < 256; i++)
596     {
597         res->input->children[i] = (chr_t_entry *)
598             nmem_malloc(res->nmem, sizeof(*res->input));
599         res->input->children[i]->children = 0;
600         res->input->children[i]->target = (unsigned char **)
601             nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
602         res->input->children[i]->target[1] = 0;
603         if (map_only)
604         {
605             res->input->children[i]->target[0] = (unsigned char *)
606                 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
607             res->input->children[i]->target[0][0] = i;
608             res->input->children[i]->target[0][1] = 0;
609         }
610         else
611             res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
612     }
613     res->q_input = (chr_t_entry *)
614         nmem_malloc(res->nmem, sizeof(*res->q_input));
615     res->q_input->target = 0;
616     res->q_input->children = 0;
617
618     for (i = *CHR_BASE; i < 256; i++)
619         res->output[i] = 0;
620     res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
621     res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
622     res->base_uppercase = 0;
623
624     while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
625         if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
626         {
627             if (argc != 2)
628             {
629                 yaz_log(LOG_FATAL, "Syntax error in charmap");
630                 ++errors;
631             }
632             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
633                             res, &num) < 0)
634             {
635                 yaz_log(LOG_FATAL, "Bad value-set specification");
636                 ++errors;
637             }
638             res->base_uppercase = num;
639             res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
640             res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
641             num = (int) *CHR_BASE;
642         }
643         else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
644         {
645             if (!res->base_uppercase)
646             {
647                 yaz_log(LOG_FATAL, "Uppercase directive with no lowercase set");
648                 ++errors;
649             }
650             if (argc != 2)
651             {
652                 yaz_log(LOG_FATAL, "Missing arg for uppercase directive");
653                 ++errors;
654             }
655             if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
656                             res, &num) < 0)
657             {
658                 yaz_log(LOG_FATAL, "Bad value-set specification");
659                 ++errors;
660             }
661         }
662         else if (!map_only && !yaz_matchstr(argv[0], "space"))
663         {
664             if (argc != 2)
665             {
666                 yaz_log(LOG_FATAL, "Syntax error in charmap for space");
667                 ++errors;
668             }
669             if (scan_string(argv[1], t_unicode, t_utf8,
670                             fun_addspace, res, 0) < 0)
671             {
672                 yaz_log(LOG_FATAL, "Bad space specification");
673                 ++errors;
674             }
675         }
676         else if (!map_only && !yaz_matchstr(argv[0], "cut"))
677         {
678             if (argc != 2)
679             {
680                 yaz_log(LOG_FATAL, "Syntax error in charmap for cut");
681                 ++errors;
682             }
683             if (scan_string(argv[1], t_unicode, t_utf8,
684                             fun_addcut, res, 0) < 0)
685             {
686                 yaz_log(LOG_FATAL, "Bad cut specification");
687                 ++errors;
688             }
689         }
690         else if (!yaz_matchstr(argv[0], "map"))
691         {
692             chrwork buf;
693
694             if (argc != 3)
695             {
696                 yaz_log(LOG_FATAL, "charmap directive map requires 2 args");
697                 ++errors;
698             }
699             buf.map = res;
700             buf.string[0] = '\0';
701             if (scan_string(argv[2], t_unicode, t_utf8,
702                             fun_mkstring, &buf, 0) < 0)
703             {
704                 yaz_log(LOG_FATAL, "Bad map target");
705                 ++errors;
706             }
707             if (scan_string(argv[1], t_unicode, t_utf8,
708                             fun_add_map, &buf, 0) < 0)
709             {
710                 yaz_log(LOG_FATAL, "Bad map source");
711                 ++errors;
712             }
713         }
714         else if (!yaz_matchstr(argv[0], "equivalent"))
715         {
716             chr_equiv_work w;
717
718             if (argc != 2)
719             {
720                 yaz_log(LOG_FATAL, "equivalent requires 1 argument");
721                 ++errors;
722             }
723             w.nmem = res->nmem;
724             w.no_eq = 0;
725             if (scan_string(argv[1], t_unicode, t_utf8, 
726                             fun_add_equivalent_string, &w, 0) < 0)
727             {
728                 yaz_log(LOG_FATAL, "equivalent: invalid string");
729                 ++errors;
730             }
731             else if (w.no_eq == 0)
732             {
733                 yaz_log(LOG_FATAL, "equivalent: no strings");
734                 ++errors;
735             }
736             else
737             {
738                 char *result_str;
739                 int i, slen = 5;
740
741                 /* determine length of regular expression */
742                 for (i = 0; i<w.no_eq; i++)
743                     slen += strlen(w.eq[i]) + 1;
744                 result_str = nmem_malloc(res->nmem, slen + 5);
745
746                 /* build the regular expression */
747                 *result_str = '\0';
748                 slen = 0;
749                 for (i = 0; i<w.no_eq; i++)
750                 {
751                     result_str[slen++]  = i ? '|' : '(';
752                     strcpy(result_str + slen, w.eq[i]);
753                     slen += strlen(w.eq[i]);
754                 }
755                 result_str[slen++] = ')';
756                 result_str[slen] = '\0';
757
758                 /* each eq will map to this regular expression */
759                 for (i = 0; i<w.no_eq; i++)
760                 {
761                     set_map_string(res->q_input, res->nmem,
762                                    w.eq[i], strlen(w.eq[i]),
763                                    result_str, 0);
764                 }
765             }
766         }
767         else if (!yaz_matchstr(argv[0], "encoding"))
768         {
769             /*
770              * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
771              * zebra need to comment next part of code.
772              */
773
774             /* Original code */
775 #if 1
776             if (t_unicode != 0)
777                 yaz_iconv_close (t_unicode);
778             t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
779 #endif
780             /*
781              * Fix me. It is additional staff for conversion of characters from local encoding
782              * of *.chr file to UTF-8 (internal encoding).
783              * NOTE: The derective encoding must be first directive in *.chr file.
784              */
785             /* For whatever reason Oleg enabled this.. */
786 #if 0
787             if (t_utf8 != 0)
788                 yaz_iconv_close(t_utf8);
789             t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
790 #endif
791         }
792         else
793         {
794             yaz_log(LOG_WARN, "Syntax error at '%s' in %s", line, name);
795         }
796     
797     yaz_fclose(f);
798     if (errors)
799     {
800         chrmaptab_destroy(res);
801         res = 0;
802     }
803     yaz_log (LOG_DEBUG, "maptab %s close %d errors", name, errors);
804     if (t_utf8 != 0)
805         yaz_iconv_close(t_utf8);
806     if (t_unicode != 0)
807         yaz_iconv_close(t_unicode);
808     return res;
809 }
810
811 void chrmaptab_destroy(chrmaptab tab)
812 {
813     if (tab)
814         nmem_destroy (tab->nmem);
815 }
816
817