1 /* Copyright (C) 1998-2018 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 This program is free software; you can redistribute it and/or modify
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, see <http://www.gnu.org/licenses/>. */
31 #include "localedef.h"
32 #include "linereader.h"
34 #include "repertoire.h"
35 #include "simple-hash.h"
38 /* Simple keyword hashing for the repertoiremap. */
39 static const struct keyword_t
*repertoiremap_hash (const char *str
,
41 static void repertoire_new_char (struct linereader
*lr
, hash_table
*ht
,
42 hash_table
*rt
, struct obstack
*ob
,
43 uint32_t value
, const char *from
,
44 const char *to
, int decimal_ellipsis
);
45 static int repertoire_compare (const void *p1
, const void *p2
);
47 /* Already known repertoire maps. */
50 /* List of repertoire maps which are not available and which have been
51 reported to not be. */
52 static void *unavailable
;
56 repertoire_read (const char *filename
)
58 struct linereader
*repfile
;
59 struct repertoire_t
*result
;
60 struct repertoire_t
**resultp
;
61 struct repertoire_t search
;
63 char *from_name
= NULL
;
65 enum token_t ellipsis
= tok_none
;
67 search
.name
= filename
;
68 resultp
= tfind (&search
, &known
, &repertoire_compare
);
73 repfile
= lr_open (filename
, repertoiremap_hash
);
76 if (strchr (filename
, '/') == NULL
)
78 char *i18npath
= getenv ("I18NPATH");
79 if (i18npath
!= NULL
&& *i18npath
!= '\0')
81 const size_t pathlen
= strlen (i18npath
);
82 char i18npathbuf
[pathlen
+ 1];
83 char path
[strlen (filename
) + 1 + pathlen
84 + sizeof ("/repertoiremaps/") - 1];
86 i18npath
= memcpy (i18npathbuf
, i18npath
, pathlen
+ 1);
88 while (repfile
== NULL
89 && (next
= strsep (&i18npath
, ":")) != NULL
)
91 stpcpy (stpcpy (stpcpy (path
, next
), "/repertoiremaps/"),
94 repfile
= lr_open (path
, repertoiremap_hash
);
98 stpcpy (stpcpy (stpcpy (path
, next
), "/"), filename
);
100 repfile
= lr_open (path
, repertoiremap_hash
);
107 /* Look in the systems charmap directory. */
108 char *buf
= xmalloc (strlen (filename
) + 1
109 + sizeof (REPERTOIREMAP_PATH
));
111 stpcpy (stpcpy (stpcpy (buf
, REPERTOIREMAP_PATH
), "/"),
113 repfile
= lr_open (buf
, repertoiremap_hash
);
123 /* We don't want symbolic names in string to be translated. */
124 repfile
->translate_strings
= 0;
126 /* Allocate room for result. */
127 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
128 memset (result
, '\0', sizeof (struct repertoire_t
));
130 result
->name
= xstrdup (filename
);
132 #define obstack_chunk_alloc malloc
133 #define obstack_chunk_free free
134 obstack_init (&result
->mem_pool
);
136 if (init_hash (&result
->char_table
, 256)
137 || init_hash (&result
->reverse_table
, 256)
138 || init_hash (&result
->seq_table
, 256))
144 /* We use a state machine to describe the charmap description file
150 struct token
*now
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
151 enum token_t nowtok
= now
->tok
;
154 if (nowtok
== tok_eof
)
160 /* We haven't yet read any character definition. This is where
161 we accept escape_char and comment_char definitions. */
162 if (nowtok
== tok_eol
)
163 /* Ignore empty lines. */
166 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
168 /* We know that we need an argument. */
169 arg
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
171 if (arg
->tok
!= tok_ident
)
173 lr_error (repfile
, _("syntax error in prolog: %s"),
176 lr_ignore_rest (repfile
, 0);
180 if (arg
->val
.str
.lenmb
!= 1)
182 lr_error (repfile
, _("\
183 argument to <%s> must be a single character"),
184 nowtok
== tok_escape_char
? "escape_char"
187 lr_ignore_rest (repfile
, 0);
191 if (nowtok
== tok_escape_char
)
192 repfile
->escape_char
= *arg
->val
.str
.startmb
;
194 repfile
->comment_char
= *arg
->val
.str
.startmb
;
196 lr_ignore_rest (repfile
, 1);
200 if (nowtok
== tok_charids
)
202 lr_ignore_rest (repfile
, 1);
208 /* Otherwise we start reading the character definitions. */
213 /* We are now are in the body. Each line
214 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
215 if (nowtok
== tok_eol
)
216 /* Ignore empty lines. */
219 if (nowtok
== tok_end
)
225 if (nowtok
!= tok_bsymbol
)
228 _("syntax error in repertoire map definition: %s"),
229 _("no symbolic name given"));
231 lr_ignore_rest (repfile
, 0);
235 /* If the previous line was not completely correct free the
237 if (from_name
!= NULL
)
238 obstack_free (&result
->mem_pool
, from_name
);
240 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
241 now
->val
.str
.startmb
,
249 /* We have two possibilities: We can see an ellipsis or an
251 if (nowtok
== tok_ellipsis3
|| nowtok
== tok_ellipsis4
252 || nowtok
== tok_ellipsis2
)
261 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
262 the xxx mean a hexadecimal value. */
266 if (nowtok
!= tok_ucs4
)
269 _("syntax error in repertoire map definition: %s"),
270 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
272 lr_ignore_rest (repfile
, 0);
276 /* We've found a new valid definition. */
277 repertoire_new_char (repfile
, &result
->char_table
,
278 &result
->reverse_table
, &result
->mem_pool
,
279 now
->val
.ucs4
, from_name
, to_name
,
280 ellipsis
!= tok_ellipsis2
);
282 /* Ignore the rest of the line. */
283 lr_ignore_rest (repfile
, 0);
291 if (nowtok
!= tok_bsymbol
)
294 _("syntax error in repertoire map definition: %s"),
295 _("no symbolic name given for end of range"));
297 lr_ignore_rest (repfile
, 0);
302 /* Copy the to-name in a safe place. */
303 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
304 repfile
->token
.val
.str
.startmb
,
305 repfile
->token
.val
.str
.lenmb
);
311 if (nowtok
!= tok_charids
)
312 lr_error (repfile
, _("\
313 %1$s: definition does not end with `END %1$s'"), "CHARIDS");
315 lr_ignore_rest (repfile
, nowtok
== tok_charids
);
322 if (state
!= 2 && state
!= 90 && !be_quiet
)
323 record_error (0, 0, _("%s: premature end of file"),
328 if (tsearch (result
, &known
, &repertoire_compare
) == NULL
)
329 /* Something went wrong. */
330 record_error (0, errno
, _("cannot save new repertoire map"));
337 repertoire_complain (const char *name
)
339 if (tfind (name
, &unavailable
, (__compar_fn_t
) strcmp
) == NULL
)
341 record_error (0, errno
, _("\
342 repertoire map file `%s' not found"), name
);
344 /* Remember that we reported this map. */
345 tsearch (name
, &unavailable
, (__compar_fn_t
) strcmp
);
351 repertoire_compare (const void *p1
, const void *p2
)
353 struct repertoire_t
*r1
= (struct repertoire_t
*) p1
;
354 struct repertoire_t
*r2
= (struct repertoire_t
*) p2
;
356 return strcmp (r1
->name
, r2
->name
);
360 static const struct keyword_t
*
361 repertoiremap_hash (const char *str
, unsigned int len
)
363 static const struct keyword_t wordlist
[] =
365 {"escape_char", tok_escape_char
, 0},
366 {"comment_char", tok_comment_char
, 0},
367 {"CHARIDS", tok_charids
, 0},
371 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
373 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
375 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
377 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)
385 repertoire_new_char (struct linereader
*lr
, hash_table
*ht
, hash_table
*rt
,
386 struct obstack
*ob
, uint32_t value
, const char *from
,
387 const char *to
, int decimal_ellipsis
)
393 int prefix_len
, len1
, len2
;
394 unsigned long int from_nr
, to_nr
, cnt
;
398 insert_entry (ht
, from
, strlen (from
),
399 (void *) (unsigned long int) value
);
400 /* Please note that it isn't a bug if a symbol is defined more
401 than once. All later definitions are simply discarded. */
403 insert_entry (rt
, obstack_copy (ob
, &value
, sizeof (value
)),
404 sizeof (value
), (void *) from
);
409 /* We have a range: the names must have names with equal prefixes
410 and an equal number of digits, where the second number is greater
411 or equal than the first. */
412 len1
= strlen (from
);
418 lr_error (lr
, _("invalid names for character range"));
422 cp
= &from
[len1
- 1];
423 if (decimal_ellipsis
)
424 while (isdigit (*cp
) && cp
>= from
)
427 while (isxdigit (*cp
) && cp
>= from
)
429 if (!isdigit (*cp
) && !isupper (*cp
))
431 hexadecimal range format should use only capital characters"));
435 prefix_len
= (cp
- from
) + 1;
437 if (cp
== &from
[len1
- 1] || strncmp (from
, to
, prefix_len
) != 0)
441 from_nr
= strtoul (&from
[prefix_len
], &from_end
, decimal_ellipsis
? 10 : 16);
442 if (*from_end
!= '\0' || (from_nr
== ULONG_MAX
&& errno
== ERANGE
)
443 || ((to_nr
= strtoul (&to
[prefix_len
], &to_end
,
444 decimal_ellipsis
? 10 : 16)) == ULONG_MAX
448 lr_error (lr
, _("<%s> and <%s> are invalid names for range"),
455 lr_error (lr
, _("upper limit in range is smaller than lower limit"));
459 for (cnt
= from_nr
; cnt
<= to_nr
; ++cnt
)
461 uint32_t this_value
= value
+ (cnt
- from_nr
);
463 obstack_printf (ob
, decimal_ellipsis
? "%.*s%0*ld" : "%.*s%0*lX",
464 prefix_len
, from
, len1
- prefix_len
, cnt
);
465 obstack_1grow (ob
, '\0');
467 insert_entry (ht
, buf
, len1
,
468 (void *) (unsigned long int) this_value
);
469 /* Please note we don't examine the return value since it is no error
470 if we have two definitions for a symbol. */
472 insert_entry (rt
, obstack_copy (ob
, &this_value
, sizeof (this_value
)),
473 sizeof (this_value
), (void *) from
);
479 repertoire_find_value (const struct repertoire_t
*rep
, const char *name
,
485 return ILLEGAL_CHAR_VALUE
;
487 if (find_entry ((hash_table
*) &rep
->char_table
, name
, len
, &result
) < 0)
488 return ILLEGAL_CHAR_VALUE
;
490 return (uint32_t) ((unsigned long int) result
);
495 repertoire_find_symbol (const struct repertoire_t
*rep
, uint32_t ucs
)
502 if (find_entry ((hash_table
*) &rep
->reverse_table
, &ucs
, sizeof (ucs
),
506 return (const char *) result
;
511 repertoire_find_seq (const struct repertoire_t
*rep
, uint32_t ucs
)
518 if (find_entry ((hash_table
*) &rep
->seq_table
, &ucs
, sizeof (ucs
),
522 return (struct charseq
*) result
;