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