Changed prototype for chr_map_input - added const.
[idzebra-moved-to-github.git] / util / charmap.c
1 /*
2  * Copyright (C) 1996-1997, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: charmap.c,v $
7  * Revision 1.12  1997-09-05 15:30:11  adam
8  * Changed prototype for chr_map_input - added const.
9  * Added support for C++, headers uses extern "C" for public definitions.
10  *
11  * Revision 1.11  1997/09/05 09:52:32  adam
12  * Extra argument added to function chr_read_maptab (tab path).
13  *
14  * Revision 1.10  1997/07/01 13:01:08  adam
15  * Bug fix in routine find_entry: didn't take into account the len arg.
16  *
17  * Revision 1.9  1996/10/29 13:48:14  adam
18  * Updated to use zebrautl.h instead of alexutil.h.
19  *
20  * Revision 1.8  1996/10/18 12:39:23  adam
21  * Uses LOG_DEBUG instead of LOG_WARN for "Character map overlap".
22  *
23  * Revision 1.7  1996/06/06  12:08:56  quinn
24  * Fixed bug.
25  *
26  * Revision 1.6  1996/06/04  13:28:00  quinn
27  * More work on charmapping
28  *
29  * Revision 1.5  1996/06/04  08:32:15  quinn
30  * Moved default keymap to keychars.c
31  *
32  * Revision 1.4  1996/06/03  16:32:13  quinn
33  * Temporary bug-fix
34  *
35  * Revision 1.3  1996/06/03  15:17:46  quinn
36  * Fixed bug.
37  *
38  * Revision 1.2  1996/06/03  10:15:09  quinn
39  * Fixed bug in mapping function.
40  *
41  * Revision 1.1  1996/05/31  09:07:18  quinn
42  * Work on character-set handling
43  *
44  *
45  */
46
47 /*
48  * Support module to handle character-conversions into and out of the
49  * Zebra dictionary.
50  */
51
52 #include <ctype.h>
53 #include <string.h>
54 #include <assert.h>
55
56 #include <zebrautl.h>
57 #include <yaz-util.h>
58 #include <charmap.h>
59 #include <tpath.h>
60
61 #define CHR_MAXSTR 1024
62 #define CHR_MAXEQUIV 32
63
64 const char *CHR_UNKNOWN = "\001";
65 const char *CHR_SPACE   = "\002";
66 const char *CHR_BASE    = "\003";
67
68 /*
69  * Character map trie node.
70  */
71 struct chr_t_entry
72 {
73     chr_t_entry **children; /* array of children */
74     unsigned char *target;  /* target for this node, if any */
75     unsigned char *equiv;   /* equivalent to, or sumthin */
76 };
77
78 /*
79  * General argument structure for callback functions (internal use only)
80  */
81 typedef struct chrwork 
82 {
83     chrmaptab *map;
84     char string[CHR_MAXSTR+1];
85 } chrwork;
86
87 /*
88  * Add an entry to the character map.
89  */
90 static chr_t_entry *set_map_string(chr_t_entry *root, const char *from,
91                                    int len, char *to)
92 {
93     if (!root)
94     {
95         root = xmalloc(sizeof(*root));
96         root->children = 0;
97         root->target = 0;
98     }
99     if (!len)
100     {
101         if (!root->target || (char*) root->target == CHR_SPACE ||
102             (char*) root->target == CHR_UNKNOWN)
103             root->target = (unsigned char *) xstrdup(to);
104         else if ((char*) to != CHR_SPACE)
105             logf(LOG_DEBUG, "Character map overlap");
106     }
107     else
108     {
109         if (!root->children)
110         {
111             int i;
112
113             root->children = xmalloc(sizeof(chr_t_entry*) * 256);
114             for (i = 0; i < 256; i++)
115                 root->children[i] = 0;
116         }
117         if (!(root->children[(unsigned char) *from] =
118             set_map_string(root->children[(unsigned char) *from], from + 1,
119             len - 1, to)))
120             return 0;
121     }
122     return root;
123 }
124
125 int chr_map_chrs(chr_t_entry *t, char **from, int len, int *read, char **to,
126     int max)
127 {
128     int i = 0;
129     unsigned char *s;
130
131     while (len && t->children && t->children[(unsigned char) **from])
132     {
133         t = t->children[(unsigned char) **from];
134         (*from)++;
135         len--;
136     }
137     /* if there were no matches, we are still at the root node,
138        which always has a null mapping */
139     for (s = t->target; *s && max; s++)
140     {
141         **to = *s;
142         s++;
143         (*to)++;
144         max--;
145         i++;
146     }
147     return i;
148 }
149
150
151 static chr_t_entry *find_entry(chr_t_entry *t, const char **from, int len)
152 {
153     chr_t_entry *res;
154
155     if (len && t->children && t->children[(unsigned char) **from])
156     {
157         const char *pos = *from;
158
159         (*from)++;
160         if ((res = find_entry(t->children[(unsigned char) *pos],
161             from, len - 1)))
162             return res;
163         /* no match */
164         *from = pos;
165     }
166     /* no children match. use ourselves, if we have a target */
167    return t->target ? t : 0;
168 }
169
170 const char **chr_map_input(chr_t_entry *t, const char **from, int len)
171 {
172     static const char *buf[2] = {0, 0};
173     chr_t_entry *res;
174
175     if (!(res = find_entry(t, from, len)))
176         abort();
177     buf[0] = (char *) res->target;
178     return buf;
179 }
180
181
182 static unsigned char prim(char **s)
183 {
184     unsigned char c;
185     unsigned int i;
186
187     if (**s == '\\')
188     {
189         (*s)++;
190         c = **s;
191         switch (c)
192         {
193             case '\\': c = '\\'; (*s)++; break;
194             case 'r': c = '\r'; (*s)++; break;
195             case 'n': c = '\n'; (*s)++; break;
196             case 't': c = '\t'; (*s)++; break;
197             case 's': c = ' '; (*s)++; break;
198             case 'x': sscanf(*s, "x%2x", &i); c = i; *s += 3; break;
199             case '{': case '[': case '(': case '}': case ']': case ')':
200                 (*s)++;
201                 break;
202             default: sscanf(*s, "%3o", &i); c = i; *s += 3; break;
203         }
204         return c;
205     }
206     c = **s;
207     ++(*s);
208     return c;
209 }
210
211 /*
212  * Callback function.
213  * Add an entry to the value space.
214  */
215 static void fun_addentry(const char *s, void *data, int num)
216 {
217     chrmaptab *tab = data;
218     char tmp[2];
219
220     tmp[0] = num; tmp[1] = '\0';
221     tab->input = set_map_string(tab->input, s, strlen(s), tmp);
222     tab->output[num + tab->base_uppercase] = (unsigned char *) xstrdup(s);
223 }
224
225 /* 
226  * Callback function.
227  * Add a space-entry to the value space.
228  */
229 static void fun_addspace(const char *s, void *data, int num)
230 {
231     chrmaptab *tab = data;
232     tab->input = set_map_string(tab->input, s, strlen(s), (char*) CHR_SPACE);
233 }
234
235 /*
236  * Create a string containing the mapped characters provided.
237  */
238 static void fun_mkstring(const char *s, void *data, int num)
239 {
240     chrwork *arg = data;
241     const char **res, *p = s;
242
243     res = chr_map_input(arg->map->input, &s, strlen(s));
244     if (*res == (char*) CHR_UNKNOWN)
245         logf(LOG_WARN, "Map: '%s' has no mapping", p);
246     strncat(arg->string, *res, CHR_MAXSTR - strlen(arg->string));
247     arg->string[CHR_MAXSTR] = '\0';
248 }
249
250 /*
251  * Add a map to the string contained in the argument.
252  */
253 static void fun_addmap(const char *s, void *data, int num)
254 {
255     chrwork *arg = data;
256
257     assert(arg->map->input);
258     set_map_string(arg->map->input, s, strlen(s), arg->string);
259 }
260
261 static int scan_string(char *s, void (*fun)(const char *c, void *data, int num),
262     void *data, int *num)
263 {
264     unsigned char c, str[1024], begin, end, *p;
265
266     while (*s)
267     {
268         switch (*s)
269         {
270             case '{':
271                 s++;
272                 begin = prim(&s);
273                 if (*s != '-')
274                 {
275                     logf(LOG_FATAL, "Bad range in char-map");
276                     return -1;
277                 }
278                 s++;
279                 end = prim(&s);
280                 if (end <= begin)
281                 {
282                     logf(LOG_FATAL, "Bad range in char-map");
283                     return -1;
284                 }
285                 s++;
286                 for (c = begin; c <= end; c++)
287                 {
288                     str[0] = c; str[1] = '\0';
289                     (*fun)((char *) str, data, num ? (*num)++ : 0);
290                 }
291                 break;
292             case '[': s++; abort(); break;
293             case '(':
294                 p = (unsigned char*) ++s;
295                 /* Find the end-marker, ignoring escapes */
296                 do
297                 {
298                     if (!(p = (unsigned char*) strchr((char*) p, ')')))
299                     {
300                         logf(LOG_FATAL, "Missing ')' in string");
301                         return -1;
302                     }
303                 }
304                 while (*(p -  1) == '\\');
305                 *p = 0;
306                 (*fun)(s, data, num ? (*num)++ : 0);
307                 s = (char*) p + 1;
308                 break;
309             default:
310                 c = prim(&s);
311                 str[0] = c; str[1] = '\0';
312                 (*fun)((char *) str, data, num ? (*num)++ : 0);
313         }
314     }
315     return 0;
316 }
317
318 chrmaptab *chr_read_maptab(const char *tabpath, const char *name)
319 {
320     FILE *f;
321     char line[512], *argv[50];
322     chrmaptab *res = xmalloc(sizeof(*res));
323     int argc, num = (int) *CHR_BASE, i;
324
325     if (!(f = yaz_path_fopen(tabpath, name, "r")))
326     {
327         logf(LOG_WARN|LOG_ERRNO, "%s", name);
328         return 0;
329     }
330     res = xmalloc(sizeof(*res));
331     res->input = xmalloc(sizeof(*res->input));
332     res->input->target = (unsigned char*) CHR_UNKNOWN;
333     res->input->equiv = 0;
334 #if 1
335     res->input->children = xmalloc(sizeof(res->input) * 256);
336     for (i = 0; i < 256; i++)
337     {
338         res->input->children[i] = xmalloc(sizeof(*res->input));
339         res->input->children[i]->children = 0;
340         res->input->children[i]->target = (unsigned char*) CHR_UNKNOWN;
341         res->input->children[i]->equiv = 0;
342     }
343 #else
344     res->input->children = 0;
345 #endif
346     res->query_equiv = 0;
347     for (i = *CHR_BASE; i < 256; i++)
348         res->output[i] = 0;
349     res->output[(int) *CHR_SPACE] = (unsigned char *) " ";
350     res->output[(int) *CHR_UNKNOWN] = (unsigned char*) "@";
351     res->base_uppercase = 0;
352
353     while ((argc = readconf_line(f, line, 512, argv, 50)))
354         if (!yaz_matchstr(argv[0], "lowercase"))
355         {
356             if (argc != 2)
357             {
358                 logf(LOG_FATAL, "Syntax error in charmap");
359                 fclose(f);
360                 return 0;
361             }
362             if (scan_string(argv[1], fun_addentry, res, &num) < 0)
363             {
364                 logf(LOG_FATAL, "Bad value-set specification");
365                 fclose(f);
366                 return 0;
367             }
368             res->base_uppercase = num;
369             res->output[(int) *CHR_SPACE + num] = (unsigned char *) " ";
370             res->output[(int) *CHR_UNKNOWN + num] = (unsigned char*) "@";
371             num = (int) *CHR_BASE;
372         }
373         else if (!yaz_matchstr(argv[0], "uppercase"))
374         {
375             if (!res->base_uppercase)
376             {
377                 logf(LOG_FATAL, "Uppercase directive with no lowercase set");
378                 fclose(f);
379                 return 0;
380             }
381             if (argc != 2)
382             {
383                 logf(LOG_FATAL, "Syntax error in charmap");
384                 fclose(f);
385                 return 0;
386             }
387             if (scan_string(argv[1], fun_addentry, res, &num) < 0)
388             {
389                 logf(LOG_FATAL, "Bad value-set specification");
390                 fclose(f);
391                 return 0;
392             }
393         }
394         else if (!yaz_matchstr(argv[0], "space"))
395         {
396             if (argc != 2)
397             {
398                 logf(LOG_FATAL, "Syntax error in charmap");
399                 fclose(f);
400                 return 0;
401             }
402             if (scan_string(argv[1], fun_addspace, res, 0) < 0)
403             {
404                 logf(LOG_FATAL, "Bad space specification");
405                 fclose(f);
406                 return 0;
407             }
408         }
409         else if (!yaz_matchstr(argv[0], "map"))
410         {
411             chrwork buf;
412
413             if (argc != 3)
414             {
415                 logf(LOG_FATAL, "charmap MAP directive requires 2 args");
416                 fclose(f);
417                 return 0;
418             }
419             buf.map = res;
420             buf.string[0] = '\0';
421             if (scan_string(argv[2], fun_mkstring, &buf, 0) < 0)
422             {
423                 logf(LOG_FATAL, "Bad map target");
424                 fclose(f);
425                 return 0;
426             }
427             if (scan_string(argv[1], fun_addmap, &buf, 0) < 0)
428             {
429                 logf(LOG_FATAL, "Bad map source");
430                 fclose(f);
431                 return 0;
432             }
433         }
434         else
435         {
436 #if 0
437             logf(LOG_WARN, "Syntax error at '%s' in %s", line, file);
438             fclose(f);
439             return 0;
440 #endif
441         }
442     fclose(f);
443     return res;
444 }