1 /* Copyright (C) 1998,1999,2000,2001,2002,2004 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 const size_t pathlen
= strlen (i18npath
);
84 char i18npathbuf
[pathlen
+ 1];
85 char path
[strlen (filename
) + 1 + pathlen
86 + sizeof ("/repertoiremaps/") - 1];
88 i18npath
= memcpy (i18npathbuf
, i18npath
, pathlen
+ 1);
90 while (repfile
== NULL
91 && (next
= strsep (&i18npath
, ":")) != NULL
)
93 stpcpy (stpcpy (stpcpy (path
, next
), "/repertoiremaps/"),
96 repfile
= lr_open (path
, repertoiremap_hash
);
100 stpcpy (stpcpy (path
, next
), filename
);
102 repfile
= lr_open (path
, repertoiremap_hash
);
109 /* Look in the systems charmap directory. */
110 char *buf
= xmalloc (strlen (filename
) + 1
111 + sizeof (REPERTOIREMAP_PATH
));
113 stpcpy (stpcpy (stpcpy (buf
, REPERTOIREMAP_PATH
), "/"),
115 repfile
= lr_open (buf
, repertoiremap_hash
);
126 /* We don't want symbolic names in string to be translated. */
127 repfile
->translate_strings
= 0;
129 /* Allocate room for result. */
130 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
131 memset (result
, '\0', sizeof (struct repertoire_t
));
133 result
->name
= xstrdup (filename
);
135 #define obstack_chunk_alloc malloc
136 #define obstack_chunk_free free
137 obstack_init (&result
->mem_pool
);
139 if (init_hash (&result
->char_table
, 256)
140 || init_hash (&result
->reverse_table
, 256)
141 || init_hash (&result
->seq_table
, 256))
147 /* We use a state machine to describe the charmap description file
153 struct token
*now
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
154 enum token_t nowtok
= now
->tok
;
157 if (nowtok
== tok_eof
)
163 /* We haven't yet read any character definition. This is where
164 we accept escape_char and comment_char definitions. */
165 if (nowtok
== tok_eol
)
166 /* Ignore empty lines. */
169 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
171 /* We know that we need an argument. */
172 arg
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
174 if (arg
->tok
!= tok_ident
)
176 lr_error (repfile
, _("syntax error in prolog: %s"),
179 lr_ignore_rest (repfile
, 0);
183 if (arg
->val
.str
.lenmb
!= 1)
185 lr_error (repfile
, _("\
186 argument to <%s> must be a single character"),
187 nowtok
== tok_escape_char
? "escape_char"
190 lr_ignore_rest (repfile
, 0);
194 if (nowtok
== tok_escape_char
)
195 repfile
->escape_char
= *arg
->val
.str
.startmb
;
197 repfile
->comment_char
= *arg
->val
.str
.startmb
;
199 lr_ignore_rest (repfile
, 1);
203 if (nowtok
== tok_charids
)
205 lr_ignore_rest (repfile
, 1);
211 /* Otherwise we start reading the character definitions. */
216 /* We are now are in the body. Each line
217 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
218 if (nowtok
== tok_eol
)
219 /* Ignore empty lines. */
222 if (nowtok
== tok_end
)
228 if (nowtok
!= tok_bsymbol
)
231 _("syntax error in repertoire map definition: %s"),
232 _("no symbolic name given"));
234 lr_ignore_rest (repfile
, 0);
238 /* If the previous line was not completely correct free the
240 if (from_name
!= NULL
)
241 obstack_free (&result
->mem_pool
, from_name
);
243 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
244 now
->val
.str
.startmb
,
252 /* We have two possibilities: We can see an ellipsis or an
254 if (nowtok
== tok_ellipsis3
|| nowtok
== tok_ellipsis4
255 || nowtok
== tok_ellipsis2
)
264 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
265 the xxx mean a hexadecimal value. */
269 if (nowtok
!= tok_ucs4
)
272 _("syntax error in repertoire map definition: %s"),
273 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
275 lr_ignore_rest (repfile
, 0);
279 /* We've found a new valid definition. */
280 repertoire_new_char (repfile
, &result
->char_table
,
281 &result
->reverse_table
, &result
->mem_pool
,
282 now
->val
.ucs4
, from_name
, to_name
,
283 ellipsis
!= tok_ellipsis2
);
285 /* Ignore the rest of the line. */
286 lr_ignore_rest (repfile
, 0);
294 if (nowtok
!= tok_bsymbol
)
297 _("syntax error in repertoire map definition: %s"),
298 _("no symbolic name given for end of range"));
300 lr_ignore_rest (repfile
, 0);
305 /* Copy the to-name in a safe place. */
306 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
307 repfile
->token
.val
.str
.startmb
,
308 repfile
->token
.val
.str
.lenmb
);
314 if (nowtok
!= tok_charids
)
315 lr_error (repfile
, _("\
316 `%1$s' definition does not end with `END %1$s'"), "CHARIDS");
318 lr_ignore_rest (repfile
, nowtok
== tok_charids
);
325 if (state
!= 2 && state
!= 90 && !be_quiet
)
326 WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"),
331 if (tsearch (result
, &known
, &repertoire_compare
) == NULL
)
332 /* Something went wrong. */
333 WITH_CUR_LOCALE (error (0, errno
, _("cannot safe new repertoire map")));
340 repertoire_complain (const char *name
)
342 if (tfind (name
, &unavailable
, (__compar_fn_t
) strcmp
) == NULL
)
344 WITH_CUR_LOCALE (error (0, errno
, _("\
345 repertoire map file `%s' not found"), name
));
347 /* Remember that we reported this map. */
348 tsearch (name
, &unavailable
, (__compar_fn_t
) strcmp
);
354 repertoire_compare (const void *p1
, const void *p2
)
356 struct repertoire_t
*r1
= (struct repertoire_t
*) p1
;
357 struct repertoire_t
*r2
= (struct repertoire_t
*) p2
;
359 return strcmp (r1
->name
, r2
->name
);
363 static const struct keyword_t
*
364 repertoiremap_hash (const char *str
, unsigned int len
)
366 static const struct keyword_t wordlist
[] =
368 {"escape_char", tok_escape_char
, 0},
369 {"comment_char", tok_comment_char
, 0},
370 {"CHARIDS", tok_charids
, 0},
374 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
376 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
378 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
380 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)
388 repertoire_new_char (struct linereader
*lr
, hash_table
*ht
, hash_table
*rt
,
389 struct obstack
*ob
, uint32_t value
, const char *from
,
390 const char *to
, int decimal_ellipsis
)
396 int prefix_len
, len1
, len2
;
397 unsigned int from_nr
, to_nr
, cnt
;
401 insert_entry (ht
, from
, strlen (from
),
402 (void *) (unsigned long int) value
);
403 /* Please note that it isn't a bug if a symbol is defined more
404 than once. All later definitions are simply discarded. */
406 insert_entry (rt
, obstack_copy (ob
, &value
, sizeof (value
)),
407 sizeof (value
), (void *) from
);
412 /* We have a range: the names must have names with equal prefixes
413 and an equal number of digits, where the second number is greater
414 or equal than the first. */
415 len1
= strlen (from
);
421 lr_error (lr
, _("invalid names for character range"));
425 cp
= &from
[len1
- 1];
426 if (decimal_ellipsis
)
427 while (isdigit (*cp
) && cp
>= from
)
430 while (isxdigit (*cp
) && cp
>= from
)
432 if (!isdigit (*cp
) && !isupper (*cp
))
434 hexadecimal range format should use only capital characters"));
438 prefix_len
= (cp
- from
) + 1;
440 if (cp
== &from
[len1
- 1] || strncmp (from
, to
, prefix_len
) != 0)
444 from_nr
= strtoul (&from
[prefix_len
], &from_end
, decimal_ellipsis
? 10 : 16);
445 if (*from_end
!= '\0' || (from_nr
== ULONG_MAX
&& errno
== ERANGE
)
446 || ((to_nr
= strtoul (&to
[prefix_len
], &to_end
,
447 decimal_ellipsis
? 10 : 16)) == ULONG_MAX
451 lr_error (lr
, _("<%s> and <%s> are invalid names for range"),
458 lr_error (lr
, _("upper limit in range is not smaller then lower limit"));
462 for (cnt
= from_nr
; cnt
<= to_nr
; ++cnt
)
464 uint32_t this_value
= value
+ (cnt
- from_nr
);
466 obstack_printf (ob
, decimal_ellipsis
? "%.*s%0*d" : "%.*s%0*X",
467 prefix_len
, from
, len1
- prefix_len
, cnt
);
468 obstack_1grow (ob
, '\0');
470 insert_entry (ht
, buf
, len1
,
471 (void *) (unsigned long int) this_value
);
472 /* Please note we don't examine the return value since it is no error
473 if we have two definitions for a symbol. */
475 insert_entry (rt
, obstack_copy (ob
, &this_value
, sizeof (this_value
)),
476 sizeof (this_value
), (void *) from
);
482 repertoire_find_value (const struct repertoire_t
*rep
, const char *name
,
488 return ILLEGAL_CHAR_VALUE
;
490 if (find_entry ((hash_table
*) &rep
->char_table
, name
, len
, &result
) < 0)
491 return ILLEGAL_CHAR_VALUE
;
493 return (uint32_t) ((unsigned long int) result
);
498 repertoire_find_symbol (const struct repertoire_t
*rep
, uint32_t ucs
)
505 if (find_entry ((hash_table
*) &rep
->reverse_table
, &ucs
, sizeof (ucs
),
509 return (const char *) result
;
514 repertoire_find_seq (const struct repertoire_t
*rep
, uint32_t ucs
)
521 if (find_entry ((hash_table
*) &rep
->seq_table
, &ucs
, sizeof (ucs
),
525 return (struct charseq
*) result
;