2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1996-06-06 12:08:56 quinn
10 * Revision 1.6 1996/06/04 13:28:00 quinn
11 * More work on charmapping
13 * Revision 1.5 1996/06/04 08:32:15 quinn
14 * Moved default keymap to keychars.c
16 * Revision 1.4 1996/06/03 16:32:13 quinn
19 * Revision 1.3 1996/06/03 15:17:46 quinn
22 * Revision 1.2 1996/06/03 10:15:09 quinn
23 * Fixed bug in mapping function.
25 * Revision 1.1 1996/05/31 09:07:18 quinn
26 * Work on character-set handling
32 * Support module to handle character-conversions into and out of the
45 #define CHR_MAXSTR 1024
46 #define CHR_MAXEQUIV 32
48 const char *CHR_UNKNOWN = "\001";
49 const char *CHR_SPACE = "\002";
50 const char *CHR_BASE = "\003";
52 extern char *data1_tabpath;
55 * Character map trie node.
59 chr_t_entry **children; /* array of children */
60 unsigned char *target; /* target for this node, if any */
61 unsigned char *equiv; /* equivalent to, or sumthin */
65 * General argument structure for callback functions (internal use only)
67 typedef struct chrwork
70 char string[CHR_MAXSTR+1];
74 * Add an entry to the character map.
76 static chr_t_entry *set_map_string(chr_t_entry *root, char *from, int len,
81 root = xmalloc(sizeof(*root));
87 if (!root->target || (char*) root->target == CHR_SPACE ||
88 (char*) root->target == CHR_UNKNOWN)
89 root->target = (unsigned char *) xstrdup(to);
90 else if ((char*) to != CHR_SPACE)
91 logf(LOG_WARN, "Character map overlap");
99 root->children = xmalloc(sizeof(chr_t_entry*) * 256);
100 for (i = 0; i < 256; i++)
101 root->children[i] = 0;
103 if (!(root->children[(unsigned char) *from] =
104 set_map_string(root->children[(unsigned char) *from], from + 1,
111 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
117 while (len && t->children && t->children[(unsigned char) **from])
119 t = t->children[(unsigned char) **from];
123 /* if there were no matches, we are still at the root node,
124 which always has a null mapping */
125 for (s = t->target; *s && max; s++)
138 static chr_t_entry *find_entry(chr_t_entry *t, char **from, int len)
142 if (t->children && t->children[(unsigned char) **from])
147 if ((res = find_entry(t->children[(unsigned char) *pos],
153 /* no children match. use ourselves, if we have a target */
154 return t->target ? t : 0;
157 char **chr_map_input(chr_t_entry *t, char **from, int len)
159 static char *buf[2] = {0, 0};
162 if (!(res = find_entry(t, from, len)))
164 buf[0] = (char *) res->target;
170 char **chr_map_input(chr_t_entry *t, char **from, int len)
172 static char *buf[2] = {0, 0}, str[2] = {0, 0};
177 while (len && t->children && t->children[(unsigned char) **from])
179 t = t->children[(unsigned char) **from];
183 buf[0] = (char*) t->target;
185 else /* null mapping */
192 else if (isspace(**from))
193 buf[0] = (char*) CHR_SPACE;
195 buf[0] = (char*) CHR_UNKNOWN;
200 /* return (char*) t->target; */
205 static unsigned char prim(char **s)
216 case '\\': c = '\\'; (*s)++; break;
217 case 'r': c = '\r'; (*s)++; break;
218 case 'n': c = '\n'; (*s)++; break;
219 case 't': c = '\t'; (*s)++; break;
220 case 's': c = ' '; (*s)++; break;
221 case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
222 case '{': case '[': case '(': case '}': case ']': case ')':
225 default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
236 * Add an entry to the value space.
238 static void fun_addentry(char *s, void *data, int num)
240 chrmaptab *tab = data;
243 tmp[0] = num; tmp[1] = '\0';
244 tab->input = set_map_string(tab->input, s, strlen(s), tmp);
245 tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
250 * Add a space-entry to the value space.
252 static void fun_addspace(char *s, void *data, int num)
254 chrmaptab *tab = data;
255 tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
259 * Create a string containing the mapped characters provided.
261 static void fun_mkstring(char *s, void *data, int num)
266 res = chr_map_input(arg->map->input, &s, strlen(s));
267 if (*res == (char*) CHR_UNKNOWN)
268 logf(LOG_WARN, "Map: '%s' has no mapping", p);
269 strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
270 arg->string[CHR_MAXSTR] = '\0';
274 * Add a map to the string contained in the argument.
276 static void fun_addmap(char *s, void *data, int num)
280 assert(arg->map->input);
281 set_map_string(arg->map->input, s, strlen(s), arg->string);
284 static int scan_string(char *s, void (*fun)(char *c, void *data, int num),
285 void *data, int *num)
287 unsigned char c, str[1024], begin, end, *p;
298 logf(LOG_FATAL, "Bad range in char-map");
305 logf(LOG_FATAL, "Bad range in char-map");
309 for (c = begin; c <= end; c++)
311 str[0] = c; str[1] = '\0';
312 (*fun)((char *) str, data, num ? (*num)++ : 0);
315 case '[': s++; abort(); break;
317 p = (unsigned char*) ++s;
318 /* Find the end-marker, ignoring escapes */
321 if (!(p = (unsigned char*) strchr((char*) p, ')')))
323 logf(LOG_FATAL, "Missing ')' in string");
327 while (*(p - 1) == '\\');
329 (*fun)(s, data, num ? (*num)++ : 0);
334 str[0] = c; str[1] = '\0';
335 (*fun)((char *) str, data, num ? (*num)++ : 0);
341 chrmaptab *chr_read_maptab(char *name)
344 char line[512], *argv[50];
345 chrmaptab *res = xmalloc(sizeof(*res));
346 int argc, num = (int) *CHR_BASE, i;
348 if (!(f = yaz_path_fopen(data1_tabpath, name, "r")))
350 logf(LOG_WARN|LOG_ERRNO, "%s", name);
353 res = xmalloc(sizeof(*res));
354 res->input = xmalloc(sizeof(*res->input));
355 res->input->target = (unsigned char*) CHR_UNKNOWN;
356 res->input->equiv = 0;
358 res->input->children = xmalloc(sizeof(res->input) * 256);
359 for (i = 0; i < 256; i++)
361 res->input->children[i] = xmalloc(sizeof(*res->input));
362 res->input->children[i]->children = 0;
363 res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
364 res->input->children[i]->equiv = 0;
367 res->input->children = 0;
369 res->query_equiv = 0;
370 for (i = *CHR_BASE; i < 256; i++)
372 res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
373 res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
374 res->base_uppercase = 0;
376 while ((argc = readconf_line(f, line, 512, argv, 50)))
377 if (!yaz_matchstr(argv[0], "lowercase"))
381 logf(LOG_FATAL, "Syntax error in charmap");
385 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
387 logf(LOG_FATAL, "Bad value-set specification");
391 res->base_uppercase = num;
392 res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
393 res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
394 num = (int) *CHR_BASE;
396 else if (!yaz_matchstr(argv[0], "uppercase"))
398 if (!res->base_uppercase)
400 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
406 logf(LOG_FATAL, "Syntax error in charmap");
410 if (scan_string(argv[1], fun_addentry, res, &num) < 0)
412 logf(LOG_FATAL, "Bad value-set specification");
417 else if (!yaz_matchstr(argv[0], "space"))
421 logf(LOG_FATAL, "Syntax error in charmap");
425 if (scan_string(argv[1], fun_addspace, res, 0) < 0)
427 logf(LOG_FATAL, "Bad space specification");
432 else if (!yaz_matchstr(argv[0], "map"))
438 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
443 buf.string[0] = '\0';
444 if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
446 logf(LOG_FATAL, "Bad map target");
450 if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
452 logf(LOG_FATAL, "Bad map source");
460 logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);