]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1998-2015 Free Software Foundation, Inc. |
69f155d4 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998. | |
4 | ||
43bc8ac6 | 5 | This program is free software; you can redistribute it and/or modify |
2e2efe65 RM |
6 | it under the terms of the GNU General Public License as published |
7 | by the Free Software Foundation; version 2 of the License, or | |
8 | (at your option) any later version. | |
69f155d4 | 9 | |
43bc8ac6 | 10 | This program is distributed in the hope that it will be useful, |
69f155d4 | 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
43bc8ac6 UD |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | GNU General Public License for more details. | |
69f155d4 | 14 | |
43bc8ac6 | 15 | You should have received a copy of the GNU General Public License |
59ba27a6 | 16 | along with this program; if not, see <http://www.gnu.org/licenses/>. */ |
69f155d4 UD |
17 | |
18 | #ifdef HAVE_CONFIG_H | |
19 | # include <config.h> | |
20 | #endif | |
21 | ||
22 | #include <errno.h> | |
23 | #include <error.h> | |
24 | #include <limits.h> | |
4b10dd6c UD |
25 | #include <obstack.h> |
26 | #include <search.h> | |
69f155d4 UD |
27 | #include <stdlib.h> |
28 | #include <string.h> | |
29 | #include <unistd.h> | |
e054f494 | 30 | #include <stdint.h> |
69f155d4 | 31 | |
f2b98f97 | 32 | #include "localedef.h" |
69f155d4 | 33 | #include "linereader.h" |
4b10dd6c | 34 | #include "charmap.h" |
69f155d4 UD |
35 | #include "repertoire.h" |
36 | #include "simple-hash.h" | |
69f155d4 UD |
37 | |
38 | ||
39 | /* Simple keyword hashing for the repertoiremap. */ | |
4b10dd6c UD |
40 | static const struct keyword_t *repertoiremap_hash (const char *str, |
41 | unsigned int len); | |
42 | static void repertoire_new_char (struct linereader *lr, hash_table *ht, | |
43 | hash_table *rt, struct obstack *ob, | |
44 | uint32_t value, const char *from, | |
45 | const char *to, int decimal_ellipsis); | |
46 | static int repertoire_compare (const void *p1, const void *p2); | |
47 | ||
48 | /* Already known repertoire maps. */ | |
49 | static void *known; | |
69f155d4 | 50 | |
3c833378 UD |
51 | /* List of repertoire maps which are not available and which have been |
52 | reported to not be. */ | |
53 | static void *unavailable; | |
54 | ||
69f155d4 UD |
55 | |
56 | struct repertoire_t * | |
57 | repertoire_read (const char *filename) | |
58 | { | |
12a9fabe | 59 | struct linereader *repfile; |
69f155d4 | 60 | struct repertoire_t *result; |
4b10dd6c UD |
61 | struct repertoire_t **resultp; |
62 | struct repertoire_t search; | |
69f155d4 UD |
63 | int state; |
64 | char *from_name = NULL; | |
65 | char *to_name = NULL; | |
4b10dd6c UD |
66 | enum token_t ellipsis = tok_none; |
67 | ||
68 | search.name = filename; | |
69 | resultp = tfind (&search, &known, &repertoire_compare); | |
70 | if (resultp != NULL) | |
71 | return *resultp; | |
69f155d4 UD |
72 | |
73 | /* Determine path. */ | |
12a9fabe UD |
74 | repfile = lr_open (filename, repertoiremap_hash); |
75 | if (repfile == NULL) | |
69f155d4 UD |
76 | { |
77 | if (strchr (filename, '/') == NULL) | |
78 | { | |
4b10dd6c | 79 | char *i18npath = getenv ("I18NPATH"); |
12a9fabe UD |
80 | if (i18npath != NULL && *i18npath != '\0') |
81 | { | |
db2f05ba RM |
82 | const size_t pathlen = strlen (i18npath); |
83 | char i18npathbuf[pathlen + 1]; | |
84 | char path[strlen (filename) + 1 + pathlen | |
12a9fabe UD |
85 | + sizeof ("/repertoiremaps/") - 1]; |
86 | char *next; | |
db2f05ba | 87 | i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1); |
69f155d4 | 88 | |
12a9fabe UD |
89 | while (repfile == NULL |
90 | && (next = strsep (&i18npath, ":")) != NULL) | |
91 | { | |
92 | stpcpy (stpcpy (stpcpy (path, next), "/repertoiremaps/"), | |
93 | filename); | |
94 | ||
95 | repfile = lr_open (path, repertoiremap_hash); | |
4b10dd6c UD |
96 | |
97 | if (repfile == NULL) | |
98 | { | |
b60ea6ff | 99 | stpcpy (stpcpy (stpcpy (path, next), "/"), filename); |
4b10dd6c UD |
100 | |
101 | repfile = lr_open (path, repertoiremap_hash); | |
102 | } | |
12a9fabe UD |
103 | } |
104 | } | |
105 | ||
106 | if (repfile == NULL) | |
107 | { | |
108 | /* Look in the systems charmap directory. */ | |
109 | char *buf = xmalloc (strlen (filename) + 1 | |
110 | + sizeof (REPERTOIREMAP_PATH)); | |
111 | ||
112 | stpcpy (stpcpy (stpcpy (buf, REPERTOIREMAP_PATH), "/"), | |
113 | filename); | |
114 | repfile = lr_open (buf, repertoiremap_hash); | |
115 | ||
fab1a490 | 116 | free (buf); |
12a9fabe | 117 | } |
69f155d4 UD |
118 | } |
119 | ||
12a9fabe | 120 | if (repfile == NULL) |
3c833378 | 121 | return NULL; |
69f155d4 UD |
122 | } |
123 | ||
4b10dd6c UD |
124 | /* We don't want symbolic names in string to be translated. */ |
125 | repfile->translate_strings = 0; | |
126 | ||
69f155d4 UD |
127 | /* Allocate room for result. */ |
128 | result = (struct repertoire_t *) xmalloc (sizeof (struct repertoire_t)); | |
129 | memset (result, '\0', sizeof (struct repertoire_t)); | |
130 | ||
4b10dd6c UD |
131 | result->name = xstrdup (filename); |
132 | ||
69f155d4 UD |
133 | #define obstack_chunk_alloc malloc |
134 | #define obstack_chunk_free free | |
135 | obstack_init (&result->mem_pool); | |
136 | ||
4b10dd6c UD |
137 | if (init_hash (&result->char_table, 256) |
138 | || init_hash (&result->reverse_table, 256) | |
139 | || init_hash (&result->seq_table, 256)) | |
69f155d4 UD |
140 | { |
141 | free (result); | |
142 | return NULL; | |
143 | } | |
144 | ||
145 | /* We use a state machine to describe the charmap description file | |
146 | format. */ | |
147 | state = 1; | |
148 | while (1) | |
149 | { | |
150 | /* What's on? */ | |
47e8b443 | 151 | struct token *now = lr_token (repfile, NULL, NULL, NULL, verbose); |
69f155d4 UD |
152 | enum token_t nowtok = now->tok; |
153 | struct token *arg; | |
154 | ||
155 | if (nowtok == tok_eof) | |
156 | break; | |
157 | ||
158 | switch (state) | |
159 | { | |
160 | case 1: | |
161 | /* We haven't yet read any character definition. This is where | |
162 | we accept escape_char and comment_char definitions. */ | |
163 | if (nowtok == tok_eol) | |
164 | /* Ignore empty lines. */ | |
165 | continue; | |
166 | ||
167 | if (nowtok == tok_escape_char || nowtok == tok_comment_char) | |
168 | { | |
169 | /* We know that we need an argument. */ | |
47e8b443 | 170 | arg = lr_token (repfile, NULL, NULL, NULL, verbose); |
69f155d4 UD |
171 | |
172 | if (arg->tok != tok_ident) | |
173 | { | |
12a9fabe | 174 | lr_error (repfile, _("syntax error in prolog: %s"), |
69f155d4 UD |
175 | _("bad argument")); |
176 | ||
12a9fabe | 177 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
178 | continue; |
179 | } | |
180 | ||
4b10dd6c | 181 | if (arg->val.str.lenmb != 1) |
69f155d4 | 182 | { |
12a9fabe | 183 | lr_error (repfile, _("\ |
69f155d4 UD |
184 | argument to <%s> must be a single character"), |
185 | nowtok == tok_escape_char ? "escape_char" | |
186 | : "comment_char"); | |
187 | ||
12a9fabe | 188 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
189 | continue; |
190 | } | |
191 | ||
192 | if (nowtok == tok_escape_char) | |
4b10dd6c | 193 | repfile->escape_char = *arg->val.str.startmb; |
69f155d4 | 194 | else |
4b10dd6c | 195 | repfile->comment_char = *arg->val.str.startmb; |
69f155d4 | 196 | |
12a9fabe | 197 | lr_ignore_rest (repfile, 1); |
69f155d4 UD |
198 | continue; |
199 | } | |
200 | ||
201 | if (nowtok == tok_charids) | |
202 | { | |
12a9fabe | 203 | lr_ignore_rest (repfile, 1); |
69f155d4 UD |
204 | |
205 | state = 2; | |
206 | continue; | |
207 | } | |
208 | ||
209 | /* Otherwise we start reading the character definitions. */ | |
210 | state = 2; | |
211 | /* FALLTHROUGH */ | |
212 | ||
213 | case 2: | |
214 | /* We are now are in the body. Each line | |
215 | must have the format "%s %s %s\n" or "%s...%s %s %s\n". */ | |
216 | if (nowtok == tok_eol) | |
217 | /* Ignore empty lines. */ | |
218 | continue; | |
219 | ||
220 | if (nowtok == tok_end) | |
221 | { | |
222 | state = 90; | |
223 | continue; | |
224 | } | |
225 | ||
226 | if (nowtok != tok_bsymbol) | |
227 | { | |
12a9fabe | 228 | lr_error (repfile, |
69f155d4 UD |
229 | _("syntax error in repertoire map definition: %s"), |
230 | _("no symbolic name given")); | |
231 | ||
12a9fabe | 232 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
233 | continue; |
234 | } | |
235 | ||
236 | /* If the previous line was not completely correct free the | |
237 | used memory. */ | |
238 | if (from_name != NULL) | |
239 | obstack_free (&result->mem_pool, from_name); | |
240 | ||
241 | from_name = (char *) obstack_copy0 (&result->mem_pool, | |
4b10dd6c UD |
242 | now->val.str.startmb, |
243 | now->val.str.lenmb); | |
69f155d4 UD |
244 | to_name = NULL; |
245 | ||
246 | state = 3; | |
247 | continue; | |
248 | ||
249 | case 3: | |
250 | /* We have two possibilities: We can see an ellipsis or an | |
251 | encoding value. */ | |
4b10dd6c UD |
252 | if (nowtok == tok_ellipsis3 || nowtok == tok_ellipsis4 |
253 | || nowtok == tok_ellipsis2) | |
69f155d4 | 254 | { |
4b10dd6c | 255 | ellipsis = nowtok; |
69f155d4 UD |
256 | state = 4; |
257 | continue; | |
258 | } | |
259 | /* FALLTHROUGH */ | |
260 | ||
261 | case 5: | |
262 | /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where | |
263 | the xxx mean a hexadecimal value. */ | |
264 | state = 2; | |
265 | ||
266 | errno = 0; | |
4b10dd6c | 267 | if (nowtok != tok_ucs4) |
69f155d4 | 268 | { |
12a9fabe | 269 | lr_error (repfile, |
69f155d4 UD |
270 | _("syntax error in repertoire map definition: %s"), |
271 | _("no <Uxxxx> or <Uxxxxxxxx> value given")); | |
272 | ||
12a9fabe | 273 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
274 | continue; |
275 | } | |
276 | ||
277 | /* We've found a new valid definition. */ | |
4b10dd6c UD |
278 | repertoire_new_char (repfile, &result->char_table, |
279 | &result->reverse_table, &result->mem_pool, | |
280 | now->val.ucs4, from_name, to_name, | |
281 | ellipsis != tok_ellipsis2); | |
69f155d4 UD |
282 | |
283 | /* Ignore the rest of the line. */ | |
12a9fabe | 284 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
285 | |
286 | from_name = NULL; | |
287 | to_name = NULL; | |
288 | ||
289 | continue; | |
290 | ||
291 | case 4: | |
292 | if (nowtok != tok_bsymbol) | |
293 | { | |
12a9fabe | 294 | lr_error (repfile, |
69f155d4 UD |
295 | _("syntax error in repertoire map definition: %s"), |
296 | _("no symbolic name given for end of range")); | |
297 | ||
12a9fabe | 298 | lr_ignore_rest (repfile, 0); |
69f155d4 UD |
299 | state = 2; |
300 | continue; | |
301 | } | |
302 | ||
303 | /* Copy the to-name in a safe place. */ | |
304 | to_name = (char *) obstack_copy0 (&result->mem_pool, | |
4b10dd6c UD |
305 | repfile->token.val.str.startmb, |
306 | repfile->token.val.str.lenmb); | |
69f155d4 UD |
307 | |
308 | state = 5; | |
309 | continue; | |
310 | ||
311 | case 90: | |
312 | if (nowtok != tok_charids) | |
12a9fabe | 313 | lr_error (repfile, _("\ |
11bf311e | 314 | %1$s: definition does not end with `END %1$s'"), "CHARIDS"); |
69f155d4 | 315 | |
12a9fabe | 316 | lr_ignore_rest (repfile, nowtok == tok_charids); |
69f155d4 UD |
317 | break; |
318 | } | |
319 | ||
320 | break; | |
321 | } | |
322 | ||
323 | if (state != 2 && state != 90 && !be_quiet) | |
f2b98f97 UD |
324 | WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"), |
325 | repfile->fname)); | |
69f155d4 | 326 | |
12a9fabe | 327 | lr_close (repfile); |
69f155d4 | 328 | |
4b10dd6c UD |
329 | if (tsearch (result, &known, &repertoire_compare) == NULL) |
330 | /* Something went wrong. */ | |
c69136ae | 331 | WITH_CUR_LOCALE (error (0, errno, _("cannot save new repertoire map"))); |
4b10dd6c | 332 | |
69f155d4 UD |
333 | return result; |
334 | } | |
335 | ||
336 | ||
3c833378 UD |
337 | void |
338 | repertoire_complain (const char *name) | |
339 | { | |
340 | if (tfind (name, &unavailable, (__compar_fn_t) strcmp) == NULL) | |
341 | { | |
f2b98f97 UD |
342 | WITH_CUR_LOCALE (error (0, errno, _("\ |
343 | repertoire map file `%s' not found"), name)); | |
3c833378 UD |
344 | |
345 | /* Remember that we reported this map. */ | |
346 | tsearch (name, &unavailable, (__compar_fn_t) strcmp); | |
347 | } | |
348 | } | |
349 | ||
350 | ||
4b10dd6c UD |
351 | static int |
352 | repertoire_compare (const void *p1, const void *p2) | |
353 | { | |
354 | struct repertoire_t *r1 = (struct repertoire_t *) p1; | |
355 | struct repertoire_t *r2 = (struct repertoire_t *) p2; | |
356 | ||
357 | return strcmp (r1->name, r2->name); | |
358 | } | |
359 | ||
360 | ||
69f155d4 | 361 | static const struct keyword_t * |
4b10dd6c | 362 | repertoiremap_hash (const char *str, unsigned int len) |
69f155d4 | 363 | { |
646b6970 | 364 | static const struct keyword_t wordlist[] = |
69f155d4 | 365 | { |
12a9fabe UD |
366 | {"escape_char", tok_escape_char, 0}, |
367 | {"comment_char", tok_comment_char, 0}, | |
69f155d4 UD |
368 | {"CHARIDS", tok_charids, 0}, |
369 | {"END", tok_end, 0}, | |
370 | }; | |
371 | ||
372 | if (len == 11 && memcmp (wordlist[0].name, str, 11) == 0) | |
373 | return &wordlist[0]; | |
374 | if (len == 12 && memcmp (wordlist[1].name, str, 12) == 0) | |
375 | return &wordlist[1]; | |
376 | if (len == 7 && memcmp (wordlist[2].name, str, 7) == 0) | |
377 | return &wordlist[2]; | |
378 | if (len == 3 && memcmp (wordlist[3].name, str, 3) == 0) | |
379 | return &wordlist[3]; | |
380 | ||
381 | return NULL; | |
382 | } | |
4b10dd6c UD |
383 | |
384 | ||
385 | static void | |
386 | repertoire_new_char (struct linereader *lr, hash_table *ht, hash_table *rt, | |
387 | struct obstack *ob, uint32_t value, const char *from, | |
388 | const char *to, int decimal_ellipsis) | |
389 | { | |
390 | char *from_end; | |
391 | char *to_end; | |
392 | const char *cp; | |
393 | char *buf = NULL; | |
394 | int prefix_len, len1, len2; | |
701666b7 | 395 | unsigned long int from_nr, to_nr, cnt; |
4b10dd6c UD |
396 | |
397 | if (to == NULL) | |
398 | { | |
399 | insert_entry (ht, from, strlen (from), | |
400 | (void *) (unsigned long int) value); | |
401 | /* Please note that it isn't a bug if a symbol is defined more | |
402 | than once. All later definitions are simply discarded. */ | |
403 | ||
404 | insert_entry (rt, obstack_copy (ob, &value, sizeof (value)), | |
405 | sizeof (value), (void *) from); | |
406 | ||
407 | return; | |
408 | } | |
409 | ||
410 | /* We have a range: the names must have names with equal prefixes | |
411 | and an equal number of digits, where the second number is greater | |
412 | or equal than the first. */ | |
413 | len1 = strlen (from); | |
414 | len2 = strlen (to); | |
415 | ||
416 | if (len1 != len2) | |
417 | { | |
418 | invalid_range: | |
419 | lr_error (lr, _("invalid names for character range")); | |
420 | return; | |
421 | } | |
422 | ||
423 | cp = &from[len1 - 1]; | |
424 | if (decimal_ellipsis) | |
425 | while (isdigit (*cp) && cp >= from) | |
426 | --cp; | |
427 | else | |
428 | while (isxdigit (*cp) && cp >= from) | |
429 | { | |
430 | if (!isdigit (*cp) && !isupper (*cp)) | |
431 | lr_error (lr, _("\ | |
432 | hexadecimal range format should use only capital characters")); | |
433 | --cp; | |
434 | } | |
435 | ||
436 | prefix_len = (cp - from) + 1; | |
437 | ||
438 | if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0) | |
439 | goto invalid_range; | |
440 | ||
441 | errno = 0; | |
442 | from_nr = strtoul (&from[prefix_len], &from_end, decimal_ellipsis ? 10 : 16); | |
443 | if (*from_end != '\0' || (from_nr == ULONG_MAX && errno == ERANGE) | |
444 | || ((to_nr = strtoul (&to[prefix_len], &to_end, | |
445 | decimal_ellipsis ? 10 : 16)) == ULONG_MAX | |
446 | && errno == ERANGE) | |
447 | || *to_end != '\0') | |
448 | { | |
70e51ab9 UD |
449 | lr_error (lr, _("<%s> and <%s> are invalid names for range"), |
450 | from, to); | |
4b10dd6c UD |
451 | return; |
452 | } | |
453 | ||
454 | if (from_nr > to_nr) | |
455 | { | |
11bf311e | 456 | lr_error (lr, _("upper limit in range is smaller than lower limit")); |
4b10dd6c UD |
457 | return; |
458 | } | |
459 | ||
460 | for (cnt = from_nr; cnt <= to_nr; ++cnt) | |
461 | { | |
462 | uint32_t this_value = value + (cnt - from_nr); | |
463 | ||
701666b7 | 464 | obstack_printf (ob, decimal_ellipsis ? "%.*s%0*ld" : "%.*s%0*lX", |
4b10dd6c | 465 | prefix_len, from, len1 - prefix_len, cnt); |
1c95408c | 466 | obstack_1grow (ob, '\0'); |
4b10dd6c UD |
467 | |
468 | insert_entry (ht, buf, len1, | |
469 | (void *) (unsigned long int) this_value); | |
470 | /* Please note we don't examine the return value since it is no error | |
471 | if we have two definitions for a symbol. */ | |
472 | ||
473 | insert_entry (rt, obstack_copy (ob, &this_value, sizeof (this_value)), | |
474 | sizeof (this_value), (void *) from); | |
475 | } | |
476 | } | |
477 | ||
478 | ||
479 | uint32_t | |
480 | repertoire_find_value (const struct repertoire_t *rep, const char *name, | |
481 | size_t len) | |
482 | { | |
483 | void *result; | |
484 | ||
5b5255f1 | 485 | if (rep == NULL) |
f0a4b6b1 | 486 | return ILLEGAL_CHAR_VALUE; |
5b5255f1 | 487 | |
4b10dd6c UD |
488 | if (find_entry ((hash_table *) &rep->char_table, name, len, &result) < 0) |
489 | return ILLEGAL_CHAR_VALUE; | |
490 | ||
491 | return (uint32_t) ((unsigned long int) result); | |
492 | } | |
493 | ||
494 | ||
495 | const char * | |
496 | repertoire_find_symbol (const struct repertoire_t *rep, uint32_t ucs) | |
497 | { | |
498 | void *result; | |
499 | ||
5b5255f1 | 500 | if (rep == NULL) |
f0a4b6b1 | 501 | return NULL; |
5b5255f1 | 502 | |
4b10dd6c UD |
503 | if (find_entry ((hash_table *) &rep->reverse_table, &ucs, sizeof (ucs), |
504 | &result) < 0) | |
505 | return NULL; | |
506 | ||
507 | return (const char *) result; | |
508 | } | |
509 | ||
510 | ||
511 | struct charseq * | |
512 | repertoire_find_seq (const struct repertoire_t *rep, uint32_t ucs) | |
513 | { | |
514 | void *result; | |
515 | ||
5b5255f1 | 516 | if (rep == NULL) |
f0a4b6b1 | 517 | return NULL; |
5b5255f1 | 518 | |
4b10dd6c UD |
519 | if (find_entry ((hash_table *) &rep->seq_table, &ucs, sizeof (ucs), |
520 | &result) < 0) | |
521 | return NULL; | |
522 | ||
523 | return (struct charseq *) result; | |
524 | } |