1 /* $Id: charmap.c,v 1.25 2002-08-02 19:26:57 adam 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
38 static size_t iconv(iconv_t t, char **buf, size_t *inbytesleft,
39 char **outbuf, size_t *outbytesleft)
45 typedef unsigned ucs4_t;
47 #include <yaz/yaz-util.h>
51 #define CHR_MAXSTR 1024
52 #define CHR_MAXEQUIV 32
54 const char *CHR_UNKNOWN = "\001";
55 const char *CHR_SPACE = "\002";
56 const char *CHR_BASE = "\003";
60 chr_t_entry *input; /* mapping table for input data */
61 chr_t_entry *q_input; /* mapping table for queries */
62 unsigned char *output[256]; /* return mapping - for display of registers */
63 int base_uppercase; /* Start of upper-case ordinals */
68 * Character map trie node.
72 chr_t_entry **children; /* array of children */
73 unsigned char **target; /* target for this node, if any */
77 * General argument structure for callback functions (internal use only)
79 typedef struct chrwork
82 char string[CHR_MAXSTR+1];
86 * Add an entry to the character map.
88 static chr_t_entry *set_map_string(chr_t_entry *root, NMEM nmem,
89 const char *from, int len, char *to,
96 root = (chr_t_entry *) nmem_malloc(nmem, sizeof(*root));
102 if (!root->target || !root->target[0] || strcmp(root->target[0], to))
105 root->target && root->target[0] && root->target[0][0] &&
106 strcmp (root->target[0], CHR_UNKNOWN))
108 yaz_log (LOG_WARN, "duplicate entry for charmap from '%s'",
111 root->target = (unsigned char **)
112 nmem_malloc(nmem, sizeof(*root->target)*2);
113 root->target[0] = (unsigned char *) nmem_strdup(nmem, to);
123 root->children = (chr_t_entry **)
124 nmem_malloc(nmem, sizeof(chr_t_entry*) * 256);
125 for (i = 0; i < 256; i++)
126 root->children[i] = 0;
128 if (!(root->children[(unsigned char) *from] =
129 set_map_string(root->children[(unsigned char) *from], nmem,
130 from + 1, len - 1, to, from_0)))
136 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
140 if (len && t->children && t->children[(unsigned char) **from])
142 const char *pos = *from;
145 if ((res = find_entry(t->children[(unsigned char) *pos],
151 /* no children match. use ourselves, if we have a target */
152 return t->target ? t : 0;
155 static chr_t_entry *find_entry_x(chr_t_entry *t, const char **from, int *len)
160 { /* switch to next buffer */
166 if (*len > 0 && t->children && t->children[(unsigned char) **from])
168 const char *old_from = *from;
173 if ((res = find_entry_x(t->children[(unsigned char) *old_from],
180 /* no children match. use ourselves, if we have a target */
181 return t->target ? t : 0;
184 const char **chr_map_input_x(chrmaptab maptab, const char **from, int *len)
186 chr_t_entry *t = maptab->input;
189 if (!(res = find_entry_x(t, from, len)))
191 return (const char **) (res->target);
194 const char **chr_map_input(chrmaptab maptab, const char **from, int len)
196 chr_t_entry *t = maptab->input;
202 if (!(res = find_entry_x(t, from, len_tmp)))
204 return (const char **) (res->target);
207 const char *chr_map_output(chrmaptab maptab, const char **from, int len)
209 unsigned char c = ** (unsigned char **) from;
211 return (const char*) maptab->output[c];
214 unsigned char zebra_prim(char **s)
219 yaz_log (LOG_DEBUG, "prim %.3s", *s);
226 case '\\': c = '\\'; (*s)++; break;
227 case 'r': c = '\r'; (*s)++; break;
228 case 'n': c = '\n'; (*s)++; break;
229 case 't': c = '\t'; (*s)++; break;
230 case 's': c = ' '; (*s)++; break;
231 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
242 sscanf(*s, "%3o", &i);
258 ucs4_t zebra_prim_w(ucs4_t **s)
264 yaz_log (LOG_DEBUG, "prim %.3s", (char *) *s);
271 case '\\': c = '\\'; (*s)++; break;
272 case 'r': c = '\r'; (*s)++; break;
273 case 'n': c = '\n'; (*s)++; break;
274 case 't': c = '\t'; (*s)++; break;
275 case 's': c = ' '; (*s)++; break;
281 sscanf(fmtstr, "x%2x", &i);
298 sscanf(fmtstr, "%3o", &i);
311 yaz_log (LOG_DEBUG, "out %d", c);
317 * Add an entry to the value space.
319 static void fun_addentry(const char *s, void *data, int num)
321 chrmaptab tab = (chrmaptab) data;
324 tmp[0] = num; tmp[1] = '\0';
325 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s), tmp, 0);
326 tab->output[num + tab->base_uppercase] =
327 (unsigned char *) nmem_strdup(tab->nmem, s);
332 * Add a space-entry to the value space.
334 static void fun_addspace(const char *s, void *data, int num)
336 chrmaptab tab = (chrmaptab) data;
337 tab->input = set_map_string(tab->input, tab->nmem, s, strlen(s),
338 (char*) CHR_SPACE, 0);
342 * Create a string containing the mapped characters provided.
344 static void fun_mkstring(const char *s, void *data, int num)
346 chrwork *arg = (chrwork *) data;
347 const char **res, *p = s;
349 res = chr_map_input(arg->map, &s, strlen(s));
350 if (*res == (char*) CHR_UNKNOWN)
351 logf(LOG_WARN, "Map: '%s' has no mapping", p);
352 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
353 arg->string[CHR_MAXSTR] = '\0';
357 * Add a map to the string contained in the argument.
359 static void fun_add_map(const char *s, void *data, int num)
361 chrwork *arg = (chrwork *) data;
363 assert(arg->map->input);
364 logf (LOG_DEBUG, "set map %.*s", (int) strlen(s), s);
365 set_map_string(arg->map->input, arg->map->nmem, s, strlen(s), arg->string,
367 for (s = arg->string; *s; s++)
368 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
372 * Add a query map to the string contained in the argument.
374 static void fun_add_qmap(const char *s, void *data, int num)
376 chrwork *arg = (chrwork *) data;
378 assert(arg->map->q_input);
379 logf (LOG_DEBUG, "set qmap %.*s", (int) strlen(s), s);
380 set_map_string(arg->map->q_input, arg->map->nmem, s,
381 strlen(s), arg->string, 0);
382 for (s = arg->string; *s; s++)
383 logf (LOG_DEBUG, " %3d", (unsigned char) *s);
386 static int scan_to_utf8 (iconv_t t, ucs4_t *from, size_t inlen,
387 char *outbuf, size_t outbytesleft)
389 size_t inbytesleft = inlen * sizeof(ucs4_t);
390 char *inbuf = (char*) from;
393 if (t == (iconv_t)(-1))
394 *outbuf++ = *from; /* ISO-8859-1 is OK here */
397 ret = iconv (t, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
398 if (ret == (size_t) (-1))
400 yaz_log (LOG_WARN|LOG_ERRNO, "bad unicode sequence");
408 static int scan_string(char *s_native,
409 iconv_t t_unicode, iconv_t t_utf8,
410 void (*fun)(const char *c, void *data, int num),
411 void *data, int *num)
416 ucs4_t *s0, *s = arg;
417 ucs4_t c, begin, end;
420 if (t_unicode != (iconv_t)(-1))
422 char *outbuf = (char *) arg;
423 char *inbuf = s_native;
424 size_t outbytesleft = sizeof(arg)-4;
425 size_t inbytesleft = strlen(s_native);
427 ret = iconv(t_unicode, &inbuf, &inbytesleft, &outbuf, &outbytesleft);
428 if (ret == (size_t)(-1))
430 i = (outbuf - (char*) arg)/sizeof(ucs4_t);
434 for (i = 0; s_native[i]; i++)
435 arg[i] = s_native[i] & 255; /* ISO-8859-1 conversion */
437 arg[i] = 0; /* terminate */
438 if (s[0] == 0xfeff || s[0] == 0xfeff) /* skip byte Order Mark */
446 begin = zebra_prim_w(&s);
449 logf(LOG_FATAL, "Bad range in char-map");
453 end = zebra_prim_w(&s);
456 logf(LOG_FATAL, "Bad range in char-map");
460 for (c = begin; c <= end; c++)
462 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
464 (*fun)(str, data, num ? (*num)++ : 0);
467 case '[': s++; abort(); break;
471 while (*s != ')' || s[-1] == '\\')
474 if (scan_to_utf8 (t_utf8, s0, s - s0, str, sizeof(str)-1))
476 (*fun)(str, data, num ? (*num)++ : 0);
480 c = zebra_prim_w(&s);
481 if (scan_to_utf8 (t_utf8, &c, 1, str, sizeof(str)-1))
483 (*fun)(str, data, num ? (*num)++ : 0);
489 chrmaptab chrmaptab_create(const char *tabpath, const char *name, int map_only,
493 char line[512], *argv[50];
497 int argc, num = (int) *CHR_BASE, i;
499 iconv_t t_unicode = (iconv_t)(-1);
500 iconv_t t_utf8 = (iconv_t)(-1);
501 unsigned endian = 31;
502 const char *ucs4_native = "UCS-4";
504 if (*(char*) &endian == 31) /* little endian? */
505 ucs4_native = "UCS-4LE";
508 t_utf8 = iconv_open ("UTF-8", ucs4_native);
510 logf (LOG_DEBUG, "maptab %s open", name);
511 if (!(f = yaz_fopen(tabpath, name, "r", tabroot)))
513 logf(LOG_WARN|LOG_ERRNO, "%s", name);
516 nmem = nmem_create ();
517 res = (chrmaptab) nmem_malloc(nmem, sizeof(*res));
519 res->input = (chr_t_entry *) nmem_malloc(res->nmem, sizeof(*res->input));
520 res->input->target = (unsigned char **)
521 nmem_malloc(res->nmem, sizeof(*res->input->target) * 2);
522 res->input->target[0] = (unsigned char*) CHR_UNKNOWN;
523 res->input->target[1] = 0;
524 res->input->children = (chr_t_entry **)
525 nmem_malloc(res->nmem, sizeof(res->input) * 256);
526 for (i = 0; i < 256; i++)
528 res->input->children[i] = (chr_t_entry *)
529 nmem_malloc(res->nmem, sizeof(*res->input));
530 res->input->children[i]->children = 0;
531 res->input->children[i]->target = (unsigned char **)
532 nmem_malloc (res->nmem, 2 * sizeof(unsigned char *));
533 res->input->children[i]->target[1] = 0;
536 res->input->children[i]->target[0] = (unsigned char *)
537 nmem_malloc (res->nmem, 2 * sizeof(unsigned char));
538 res->input->children[i]->target[0][0] = i;
539 res->input->children[i]->target[0][1] = 0;
542 res->input->children[i]->target[0] = (unsigned char*) CHR_UNKNOWN;
544 res->q_input = (chr_t_entry *)
545 nmem_malloc(res->nmem, sizeof(*res->q_input));
546 res->q_input->target = 0;
547 res->q_input->children = 0;
549 for (i = *CHR_BASE; i < 256; i++)
551 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
552 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
553 res->base_uppercase = 0;
555 while (!errors && (argc = readconf_line(f, &lineno, line, 512, argv, 50)))
556 if (!map_only && !yaz_matchstr(argv[0], "lowercase"))
560 logf(LOG_FATAL, "Syntax error in charmap");
563 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
566 logf(LOG_FATAL, "Bad value-set specification");
569 res->base_uppercase = num;
570 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
571 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
572 num = (int) *CHR_BASE;
574 else if (!map_only && !yaz_matchstr(argv[0], "uppercase"))
576 if (!res->base_uppercase)
578 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
583 logf(LOG_FATAL, "Missing arg for uppercase directive");
586 if (scan_string(argv[1], t_unicode, t_utf8, fun_addentry,
589 logf(LOG_FATAL, "Bad value-set specification");
593 else if (!map_only && !yaz_matchstr(argv[0], "space"))
597 logf(LOG_FATAL, "Syntax error in charmap");
600 if (scan_string(argv[1], t_unicode, t_utf8,
601 fun_addspace, res, 0) < 0)
603 logf(LOG_FATAL, "Bad space specification");
607 else if (!yaz_matchstr(argv[0], "map"))
613 logf(LOG_FATAL, "charmap directive map requires 2 args");
617 buf.string[0] = '\0';
618 if (scan_string(argv[2], t_unicode, t_utf8,
619 fun_mkstring, &buf, 0) < 0)
621 logf(LOG_FATAL, "Bad map target");
624 if (scan_string(argv[1], t_unicode, t_utf8,
625 fun_add_map, &buf, 0) < 0)
627 logf(LOG_FATAL, "Bad map source");
631 else if (!yaz_matchstr(argv[0], "qmap"))
637 logf(LOG_FATAL, "charmap directive qmap requires 2 args");
641 buf.string[0] = '\0';
642 if (scan_string(argv[2], t_unicode, t_utf8,
643 fun_mkstring, &buf, 0) < 0)
645 logf(LOG_FATAL, "Bad qmap target");
648 if (scan_string(argv[1], t_unicode, t_utf8,
649 fun_add_qmap, &buf, 0) < 0)
651 logf(LOG_FATAL, "Bad qmap source");
655 else if (!yaz_matchstr(argv[0], "encoding"))
658 if (t_unicode != (iconv_t)(-1))
659 iconv_close (t_unicode);
660 t_unicode = iconv_open (ucs4_native, argv[1]);
662 logf (LOG_WARN, "Encoding ignored. iconv not installed");
667 logf(LOG_WARN, "Syntax error at '%s' in %s", line, name);
673 chrmaptab_destroy(res);
676 logf (LOG_DEBUG, "maptab %s close %d errors", name, errors);
678 if (t_utf8 != (iconv_t)(-1))
680 if (t_unicode != (iconv_t)(-1))
681 iconv_close(t_unicode);
686 void chrmaptab_destroy(chrmaptab tab)
689 nmem_destroy (tab->nmem);