1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
33 #include "localedef.h"
34 #include "linereader.h"
36 #include "repertoire.h"
37 #include "simple-hash.h"
40 /* Simple keyword hashing for the repertoiremap. */
41 static const struct keyword_t
*repertoiremap_hash (const char *str
,
43 static void repertoire_new_char (struct linereader
*lr
, hash_table
*ht
,
44 hash_table
*rt
, struct obstack
*ob
,
45 uint32_t value
, const char *from
,
46 const char *to
, int decimal_ellipsis
);
47 static int repertoire_compare (const void *p1
, const void *p2
);
49 /* Already known repertoire maps. */
52 /* List of repertoire maps which are not available and which have been
53 reported to not be. */
54 static void *unavailable
;
58 repertoire_read (const char *filename
)
60 struct linereader
*repfile
;
61 struct repertoire_t
*result
;
62 struct repertoire_t
**resultp
;
63 struct repertoire_t search
;
65 char *from_name
= NULL
;
67 enum token_t ellipsis
= tok_none
;
69 search
.name
= filename
;
70 resultp
= tfind (&search
, &known
, &repertoire_compare
);
75 repfile
= lr_open (filename
, repertoiremap_hash
);
78 if (strchr (filename
, '/') == NULL
)
80 char *i18npath
= getenv ("I18NPATH");
81 if (i18npath
!= NULL
&& *i18npath
!= '\0')
83 char path
[strlen (filename
) + 1 + strlen (i18npath
)
84 + sizeof ("/repertoiremaps/") - 1];
86 i18npath
= strdupa (i18npath
);
89 while (repfile
== NULL
90 && (next
= strsep (&i18npath
, ":")) != NULL
)
92 stpcpy (stpcpy (stpcpy (path
, next
), "/repertoiremaps/"),
95 repfile
= lr_open (path
, repertoiremap_hash
);
99 stpcpy (stpcpy (path
, next
), filename
);
101 repfile
= lr_open (path
, repertoiremap_hash
);
108 /* Look in the systems charmap directory. */
109 char *buf
= xmalloc (strlen (filename
) + 1
110 + sizeof (REPERTOIREMAP_PATH
));
112 stpcpy (stpcpy (stpcpy (buf
, REPERTOIREMAP_PATH
), "/"),
114 repfile
= lr_open (buf
, repertoiremap_hash
);
125 /* We don't want symbolic names in string to be translated. */
126 repfile
->translate_strings
= 0;
128 /* Allocate room for result. */
129 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
130 memset (result
, '\0', sizeof (struct repertoire_t
));
132 result
->name
= xstrdup (filename
);
134 #define obstack_chunk_alloc malloc
135 #define obstack_chunk_free free
136 obstack_init (&result
->mem_pool
);
138 if (init_hash (&result
->char_table
, 256)
139 || init_hash (&result
->reverse_table
, 256)
140 || init_hash (&result
->seq_table
, 256))
146 /* We use a state machine to describe the charmap description file
152 struct token
*now
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
153 enum token_t nowtok
= now
->tok
;
156 if (nowtok
== tok_eof
)
162 /* We haven't yet read any character definition. This is where
163 we accept escape_char and comment_char definitions. */
164 if (nowtok
== tok_eol
)
165 /* Ignore empty lines. */
168 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
170 /* We know that we need an argument. */
171 arg
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
173 if (arg
->tok
!= tok_ident
)
175 lr_error (repfile
, _("syntax error in prolog: %s"),
178 lr_ignore_rest (repfile
, 0);
182 if (arg
->val
.str
.lenmb
!= 1)
184 lr_error (repfile
, _("\
185 argument to <%s> must be a single character"),
186 nowtok
== tok_escape_char
? "escape_char"
189 lr_ignore_rest (repfile
, 0);
193 if (nowtok
== tok_escape_char
)
194 repfile
->escape_char
= *arg
->val
.str
.startmb
;
196 repfile
->comment_char
= *arg
->val
.str
.startmb
;
198 lr_ignore_rest (repfile
, 1);
202 if (nowtok
== tok_charids
)
204 lr_ignore_rest (repfile
, 1);
210 /* Otherwise we start reading the character definitions. */
215 /* We are now are in the body. Each line
216 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
217 if (nowtok
== tok_eol
)
218 /* Ignore empty lines. */
221 if (nowtok
== tok_end
)
227 if (nowtok
!= tok_bsymbol
)
230 _("syntax error in repertoire map definition: %s"),
231 _("no symbolic name given"));
233 lr_ignore_rest (repfile
, 0);
237 /* If the previous line was not completely correct free the
239 if (from_name
!= NULL
)
240 obstack_free (&result
->mem_pool
, from_name
);
242 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
243 now
->val
.str
.startmb
,
251 /* We have two possibilities: We can see an ellipsis or an
253 if (nowtok
== tok_ellipsis3
|| nowtok
== tok_ellipsis4
254 || nowtok
== tok_ellipsis2
)
263 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
264 the xxx mean a hexadecimal value. */
268 if (nowtok
!= tok_ucs4
)
271 _("syntax error in repertoire map definition: %s"),
272 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
274 lr_ignore_rest (repfile
, 0);
278 /* We've found a new valid definition. */
279 repertoire_new_char (repfile
, &result
->char_table
,
280 &result
->reverse_table
, &result
->mem_pool
,
281 now
->val
.ucs4
, from_name
, to_name
,
282 ellipsis
!= tok_ellipsis2
);
284 /* Ignore the rest of the line. */
285 lr_ignore_rest (repfile
, 0);
293 if (nowtok
!= tok_bsymbol
)
296 _("syntax error in repertoire map definition: %s"),
297 _("no symbolic name given for end of range"));
299 lr_ignore_rest (repfile
, 0);
304 /* Copy the to-name in a safe place. */
305 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
306 repfile
->token
.val
.str
.startmb
,
307 repfile
->token
.val
.str
.lenmb
);
313 if (nowtok
!= tok_charids
)
314 lr_error (repfile
, _("\
315 `%1$s' definition does not end with `END %1$s'"), "CHARIDS");
317 lr_ignore_rest (repfile
, nowtok
== tok_charids
);
324 if (state
!= 2 && state
!= 90 && !be_quiet
)
325 WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"),
330 if (tsearch (result
, &known
, &repertoire_compare
) == NULL
)
331 /* Something went wrong. */
332 WITH_CUR_LOCALE (error (0, errno
, _("cannot safe new repertoire map")));
339 repertoire_complain (const char *name
)
341 if (tfind (name
, &unavailable
, (__compar_fn_t
) strcmp
) == NULL
)
343 WITH_CUR_LOCALE (error (0, errno
, _("\
344 repertoire map file `%s' not found"), name
));
346 /* Remember that we reported this map. */
347 tsearch (name
, &unavailable
, (__compar_fn_t
) strcmp
);
353 repertoire_compare (const void *p1
, const void *p2
)
355 struct repertoire_t
*r1
= (struct repertoire_t
*) p1
;
356 struct repertoire_t
*r2
= (struct repertoire_t
*) p2
;
358 return strcmp (r1
->name
, r2
->name
);
362 static const struct keyword_t
*
363 repertoiremap_hash (const char *str
, unsigned int len
)
365 static const struct keyword_t wordlist
[] =
367 {"escape_char", tok_escape_char
, 0},
368 {"comment_char", tok_comment_char
, 0},
369 {"CHARIDS", tok_charids
, 0},
373 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
375 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
377 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
379 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)
387 repertoire_new_char (struct linereader
*lr
, hash_table
*ht
, hash_table
*rt
,
388 struct obstack
*ob
, uint32_t value
, const char *from
,
389 const char *to
, int decimal_ellipsis
)
395 int prefix_len
, len1
, len2
;
396 unsigned int from_nr
, to_nr
, cnt
;
400 insert_entry (ht
, from
, strlen (from
),
401 (void *) (unsigned long int) value
);
402 /* Please note that it isn't a bug if a symbol is defined more
403 than once. All later definitions are simply discarded. */
405 insert_entry (rt
, obstack_copy (ob
, &value
, sizeof (value
)),
406 sizeof (value
), (void *) from
);
411 /* We have a range: the names must have names with equal prefixes
412 and an equal number of digits, where the second number is greater
413 or equal than the first. */
414 len1
= strlen (from
);
420 lr_error (lr
, _("invalid names for character range"));
424 cp
= &from
[len1
- 1];
425 if (decimal_ellipsis
)
426 while (isdigit (*cp
) && cp
>= from
)
429 while (isxdigit (*cp
) && cp
>= from
)
431 if (!isdigit (*cp
) && !isupper (*cp
))
433 hexadecimal range format should use only capital characters"));
437 prefix_len
= (cp
- from
) + 1;
439 if (cp
== &from
[len1
- 1] || strncmp (from
, to
, prefix_len
) != 0)
443 from_nr
= strtoul (&from
[prefix_len
], &from_end
, decimal_ellipsis
? 10 : 16);
444 if (*from_end
!= '\0' || (from_nr
== ULONG_MAX
&& errno
== ERANGE
)
445 || ((to_nr
= strtoul (&to
[prefix_len
], &to_end
,
446 decimal_ellipsis
? 10 : 16)) == ULONG_MAX
450 lr_error (lr
, _("<%s> and <%s> are invalid names for range"),
457 lr_error (lr
, _("upper limit in range is not smaller then lower limit"));
461 for (cnt
= from_nr
; cnt
<= to_nr
; ++cnt
)
463 uint32_t this_value
= value
+ (cnt
- from_nr
);
465 obstack_printf (ob
, decimal_ellipsis
? "%.*s%0*d" : "%.*s%0*X",
466 prefix_len
, from
, len1
- prefix_len
, cnt
);
467 obstack_1grow (ob
, '\0');
469 insert_entry (ht
, buf
, len1
,
470 (void *) (unsigned long int) this_value
);
471 /* Please note we don't examine the return value since it is no error
472 if we have two definitions for a symbol. */
474 insert_entry (rt
, obstack_copy (ob
, &this_value
, sizeof (this_value
)),
475 sizeof (this_value
), (void *) from
);
481 repertoire_find_value (const struct repertoire_t
*rep
, const char *name
,
487 return ILLEGAL_CHAR_VALUE
;
489 if (find_entry ((hash_table
*) &rep
->char_table
, name
, len
, &result
) < 0)
490 return ILLEGAL_CHAR_VALUE
;
492 return (uint32_t) ((unsigned long int) result
);
497 repertoire_find_symbol (const struct repertoire_t
*rep
, uint32_t ucs
)
504 if (find_entry ((hash_table
*) &rep
->reverse_table
, &ucs
, sizeof (ucs
),
508 return (const char *) result
;
513 repertoire_find_seq (const struct repertoire_t
*rep
, uint32_t ucs
)
520 if (find_entry ((hash_table
*) &rep
->seq_table
, &ucs
, sizeof (ucs
),
524 return (struct charseq
*) result
;