1 /* $Id: charmap.c,v 1.27 2003-01-13 10:53:16 oleg Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
5 This file is part of the Zebra server.
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
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
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
26 * Support module to handle character-conversions into and out of the
34 typedef unsigned ucs4_t;
36 #include <yaz/yaz-util.h>
40 #define CHR_MAXSTR 1024
41 #define CHR_MAXEQUIV 32
43 const char *CHR_UNKNOWN = "\001";
44 const char *CHR_SPACE = "\002";
45 const char *CHR_BASE = "\003";
49 chr_t_entry *input; /* mapping table for input data */
50 chr_t_entry *q_input; /* mapping table for queries */
51 unsigned char *output[256]; /* return mapping - for display of registers */
52 int base_uppercase; /* Start of upper-case ordinals */
57 * Character map trie node.
61 chr_t_entry **children; /* array of children */
62 unsigned char **target; /* target for this node, if any */
66 * General argument structure for callback functions (internal use only)
68 typedef struct chrwork
71 char string[CHR_MAXSTR+1];
75 * Add an entry to the character map.
77 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
78 const char *from, int len, char *to,
85 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
91 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
94 root->target && root->target[0] && root->target[0][0] &&
95 strcmp (root->target[0], CHR_UNKNOWN))
97 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
100 root->target = (unsigned char **)
101 nmem_malloc(nmem, sizeof(*root->target)*2);
102 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
112 root->children = (chr_t_entry **)
113 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
114 for (i = 0; i < 256; i++)
115 root->children[i] = 0;
117 if (!(root->children[(unsigned char) *from] =
118 set_map_string(root->children[(unsigned char) *from], nmem,
119 from + 1, len - 1, to, from_0)))
125 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
129 if (len && t->children && t->children[(unsigned char) **from])
131 const char *pos = *from;
134 if ((res = find_entry(t->children[(unsigned char) *pos],
140 /* no children match. use ourselves, if we have a target */
141 return t->target ? t : 0;
144 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
149 { /* switch to next buffer */
155 if (*len > 0 && t->children && t->children[(unsigned char) **from])
157 const char *old_from = *from;
162 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
169 /* no children match. use ourselves, if we have a target */
170 return t->target ? t : 0;
173 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
175 chr_t_entry *t = maptab->input;
178 if (!(res = find_entry_x(t, from, len)))
180 return (const char **) (res->target);
183 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
185 chr_t_entry *t = maptab->input;
191 if (!(res = find_entry_x(t, from, len_tmp)))
193 return (const char **) (res->target);
196 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
198 unsigned char c = ** (unsigned char **) from;
200 return (const char*) maptab->output[c];
203 unsigned char zebra_prim(char **s)
208 yaz_log (LOG_DEBUG, "prim %.3s", *s);
215 case '\\': c = '\\'; (*s)++; break;
216 case 'r': c = '\r'; (*s)++; break;
217 case 'n': c = '\n'; (*s)++; break;
218 case 't': c = '\t'; (*s)++; break;
219 case 's': c = ' '; (*s)++; break;
220 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
231 sscanf(*s, "%3o", &i);
247 ucs4_t zebra_prim_w(ucs4_t **s)
253 yaz_log (LOG_DEBUG, "prim %.3s", (char *) *s);
260 case '\\': c = '\\'; (*s)++; break;
261 case 'r': c = '\r'; (*s)++; break;
262 case 'n': c = '\n'; (*s)++; break;
263 case 't': c = '\t'; (*s)++; break;
264 case 's': c = ' '; (*s)++; break;
270 sscanf(fmtstr, "x%2x", &i);
287 sscanf(fmtstr, "%3o", &i);
300 yaz_log (LOG_DEBUG, "out %d", c);
306 * Add an entry to the value space.
308 static void fun_addentry(const char *s, void *data, int num)
310 chrmaptab tab = (chrmaptab) data;
313 tmp[0] = num; tmp[1] = '\0';
314 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
315 tab->output[num + tab->base_uppercase] =
316 (unsigned char *) nmem_strdup(tab->nmem, s);
321 * Add a space-entry to the value space.
323 static void fun_addspace(const char *s, void *data, int num)
325 chrmaptab tab = (chrmaptab) data;
326 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
327 (char*) CHR_SPACE, 0);
331 * Create a string containing the mapped characters provided.
333 static void fun_mkstring(const char *s, void *data, int num)
335 chrwork *arg = (chrwork *) data;
336 const char **res, *p = s;
338 res = chr_map_input(arg->map, &s, strlen(s));
339 if (*res == (char*) CHR_UNKNOWN)
340 logf(LOG_WARN, "Map: '%s' has no mapping", p);
341 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
342 arg->string[CHR_MAXSTR] = '\0';
346 * Add a map to the string contained in the argument.
348 static void fun_add_map(const char *s, void *data, int num)
350 chrwork *arg = (chrwork *) data;
352 assert(arg->map->input);
353 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
354 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
356 for (s = arg->string; *s; s++)
357 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
361 * Add a query map to the string contained in the argument.
363 static void fun_add_qmap(const char *s, void *data, int num)
365 chrwork *arg = (chrwork *) data;
367 assert(arg->map->q_input);
368 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
369 set_map_string(arg->map->q_input, arg->map->nmem, s,
370 strlen(s), arg->string, 0);
371 for (s = arg->string; *s; s++)
372 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
375 static int scan_to_utf8 (yaz_iconv_t t, ucs4_t *from, size_t inlen,
376 char *outbuf, size_t outbytesleft)
378 size_t inbytesleft = inlen * sizeof(ucs4_t);
379 char *inbuf = (char*) from;
383 *outbuf++ = *from; /* ISO-8859-1 is OK here */
386 ret = yaz_iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
387 if (ret == (size_t) (-1))
389 yaz_log (LOG_WARN|LOG_ERRNO, "bad unicode sequence");
397 static int scan_string(char *s_native,
398 yaz_iconv_t t_unicode, yaz_iconv_t t_utf8,
399 void (*fun)(const char *c, void *data, int num),
400 void *data, int *num)
405 ucs4_t *s0, *s = arg;
406 ucs4_t c, begin, end;
411 char *outbuf = (char *) arg;
412 char *inbuf = s_native;
413 size_t outbytesleft = sizeof(arg)-4;
414 size_t inbytesleft = strlen(s_native);
416 ret = yaz_iconv(t_unicode, &inbuf, &inbytesleft,
417 &outbuf, &outbytesleft);
418 if (ret == (size_t)(-1))
420 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
424 for (i = 0; s_native[i]; i++)
425 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
427 arg[i] = 0; /* terminate */
428 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
436 begin = zebra_prim_w(&s);
439 logf(LOG_FATAL, "Bad range in char-map");
443 end = zebra_prim_w(&s);
446 logf(LOG_FATAL, "Bad range in char-map");
450 for (c = begin; c <= end; c++)
452 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
454 (*fun)(str, data, num ? (*num)++ : 0);
457 case '[': s++; abort(); break;
461 while (*s != ')' || s[-1] == '\\')
464 if (scan_to_utf8 (t_utf8, s0, s - s0, str, sizeof(str)-1))
466 (*fun)(str, data, num ? (*num)++ : 0);
470 c = zebra_prim_w(&s);
471 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
473 (*fun)(str, data, num ? (*num)++ : 0);
479 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
483 char line[512], *argv[50];
487 int argc, num = (int) *CHR_BASE, i;
489 yaz_iconv_t t_unicode = 0;
490 yaz_iconv_t t_utf8 = 0;
491 unsigned endian = 31;
492 const char *ucs4_native = "UCS-4";
494 if (*(char*) &endian == 31) /* little endian? */
495 ucs4_native = "UCS-4LE";
497 t_utf8 = yaz_iconv_open ("UTF-8", ucs4_native);
499 logf (LOG_DEBUG, "maptab %s open", name);
500 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
502 logf(LOG_WARN|LOG_ERRNO, "%s", name);
505 nmem = nmem_create ();
506 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
508 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
509 res->input->target = (unsigned char **)
510 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
511 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
512 res->input->target[1] = 0;
513 res->input->children = (chr_t_entry **)
514 nmem_malloc(res->nmem, sizeof(res->input) * 256);
515 for (i = 0; i < 256; i++)
517 res->input->children[i] = (chr_t_entry *)
518 nmem_malloc(res->nmem, sizeof(*res->input));
519 res->input->children[i]->children = 0;
520 res->input->children[i]->target = (unsigned char **)
521 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
522 res->input->children[i]->target[1] = 0;
525 res->input->children[i]->target[0] = (unsigned char *)
526 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
527 res->input->children[i]->target[0][0] = i;
528 res->input->children[i]->target[0][1] = 0;
531 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
533 res->q_input = (chr_t_entry *)
534 nmem_malloc(res->nmem, sizeof(*res->q_input));
535 res->q_input->target = 0;
536 res->q_input->children = 0;
538 for (i = *CHR_BASE; i < 256; i++)
540 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
541 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
542 res->base_uppercase = 0;
544 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
545 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
549 logf(LOG_FATAL, "Syntax error in charmap");
552 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
555 logf(LOG_FATAL, "Bad value-set specification");
558 res->base_uppercase = num;
559 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
560 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
561 num = (int) *CHR_BASE;
563 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
565 if (!res->base_uppercase)
567 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
572 logf(LOG_FATAL, "Missing arg for uppercase directive");
575 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
578 logf(LOG_FATAL, "Bad value-set specification");
582 else if (!map_only && !yaz_matchstr(argv[0], "space"))
586 logf(LOG_FATAL, "Syntax error in charmap");
589 if (scan_string(argv[1], t_unicode, t_utf8,
590 fun_addspace, res, 0) < 0)
592 logf(LOG_FATAL, "Bad space specification");
596 else if (!yaz_matchstr(argv[0], "map"))
602 logf(LOG_FATAL, "charmap directive map requires 2 args");
606 buf.string[0] = '\0';
607 if (scan_string(argv[2], t_unicode, t_utf8,
608 fun_mkstring, &buf, 0) < 0)
610 logf(LOG_FATAL, "Bad map target");
613 if (scan_string(argv[1], t_unicode, t_utf8,
614 fun_add_map, &buf, 0) < 0)
616 logf(LOG_FATAL, "Bad map source");
620 else if (!yaz_matchstr(argv[0], "qmap"))
626 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
630 buf.string[0] = '\0';
631 if (scan_string(argv[2], t_unicode, t_utf8,
632 fun_mkstring, &buf, 0) < 0)
634 logf(LOG_FATAL, "Bad qmap target");
637 if (scan_string(argv[1], t_unicode, t_utf8,
638 fun_add_qmap, &buf, 0) < 0)
640 logf(LOG_FATAL, "Bad qmap source");
644 else if (!yaz_matchstr(argv[0], "encoding"))
647 * Fix me. When t_unicode==0 and use encoding directive in *.chr file the beheviour of the
648 * zebra need to comment next part of code.
653 yaz_iconv_close (t_unicode);
654 t_unicode = yaz_iconv_open (ucs4_native, argv[1]);
658 * Fix me. It is additional staff for conversion of characters from local encoding
659 * of *.chr file to UTF-8 (internal encoding).
660 * NOTE: The derective encoding must be first directive in *.chr file.
663 yaz_iconv_close(t_utf8);
664 t_utf8 = yaz_iconv_open ("UTF-8", argv[1]);
668 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
674 chrmaptab_destroy(res);
677 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
679 yaz_iconv_close(t_utf8);
681 yaz_iconv_close(t_unicode);
685 void chrmaptab_destroy(chrmaptab tab)
688 nmem_destroy (tab->nmem);