1 /* Copyright (C) 1998-2002,2004,2005,2007 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, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
32 #include "localedef.h"
33 #include "linereader.h"
35 #include "repertoire.h"
36 #include "simple-hash.h"
39 /* Simple keyword hashing for the repertoiremap. */
40 static const struct keyword_t
*repertoiremap_hash (const char *str
,
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
);
48 /* Already known repertoire maps. */
51 /* List of repertoire maps which are not available and which have been
52 reported to not be. */
53 static void *unavailable
;
57 repertoire_read (const char *filename
)
59 struct linereader
*repfile
;
60 struct repertoire_t
*result
;
61 struct repertoire_t
**resultp
;
62 struct repertoire_t search
;
64 char *from_name
= NULL
;
66 enum token_t ellipsis
= tok_none
;
68 search
.name
= filename
;
69 resultp
= tfind (&search
, &known
, &repertoire_compare
);
74 repfile
= lr_open (filename
, repertoiremap_hash
);
77 if (strchr (filename
, '/') == NULL
)
79 char *i18npath
= getenv ("I18NPATH");
80 if (i18npath
!= NULL
&& *i18npath
!= '\0')
82 const size_t pathlen
= strlen (i18npath
);
83 char i18npathbuf
[pathlen
+ 1];
84 char path
[strlen (filename
) + 1 + pathlen
85 + sizeof ("/repertoiremaps/") - 1];
87 i18npath
= memcpy (i18npathbuf
, i18npath
, pathlen
+ 1);
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
);
124 /* We don't want symbolic names in string to be translated. */
125 repfile
->translate_strings
= 0;
127 /* Allocate room for result. */
128 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
129 memset (result
, '\0', sizeof (struct repertoire_t
));
131 result
->name
= xstrdup (filename
);
133 #define obstack_chunk_alloc malloc
134 #define obstack_chunk_free free
135 obstack_init (&result
->mem_pool
);
137 if (init_hash (&result
->char_table
, 256)
138 || init_hash (&result
->reverse_table
, 256)
139 || init_hash (&result
->seq_table
, 256))
145 /* We use a state machine to describe the charmap description file
151 struct token
*now
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
152 enum token_t nowtok
= now
->tok
;
155 if (nowtok
== tok_eof
)
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. */
167 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
169 /* We know that we need an argument. */
170 arg
= lr_token (repfile
, NULL
, NULL
, NULL
, verbose
);
172 if (arg
->tok
!= tok_ident
)
174 lr_error (repfile
, _("syntax error in prolog: %s"),
177 lr_ignore_rest (repfile
, 0);
181 if (arg
->val
.str
.lenmb
!= 1)
183 lr_error (repfile
, _("\
184 argument to <%s> must be a single character"),
185 nowtok
== tok_escape_char
? "escape_char"
188 lr_ignore_rest (repfile
, 0);
192 if (nowtok
== tok_escape_char
)
193 repfile
->escape_char
= *arg
->val
.str
.startmb
;
195 repfile
->comment_char
= *arg
->val
.str
.startmb
;
197 lr_ignore_rest (repfile
, 1);
201 if (nowtok
== tok_charids
)
203 lr_ignore_rest (repfile
, 1);
209 /* Otherwise we start reading the character definitions. */
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. */
220 if (nowtok
== tok_end
)
226 if (nowtok
!= tok_bsymbol
)
229 _("syntax error in repertoire map definition: %s"),
230 _("no symbolic name given"));
232 lr_ignore_rest (repfile
, 0);
236 /* If the previous line was not completely correct free the
238 if (from_name
!= NULL
)
239 obstack_free (&result
->mem_pool
, from_name
);
241 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
242 now
->val
.str
.startmb
,
250 /* We have two possibilities: We can see an ellipsis or an
252 if (nowtok
== tok_ellipsis3
|| nowtok
== tok_ellipsis4
253 || nowtok
== tok_ellipsis2
)
262 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
263 the xxx mean a hexadecimal value. */
267 if (nowtok
!= tok_ucs4
)
270 _("syntax error in repertoire map definition: %s"),
271 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
273 lr_ignore_rest (repfile
, 0);
277 /* We've found a new valid definition. */
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
);
283 /* Ignore the rest of the line. */
284 lr_ignore_rest (repfile
, 0);
292 if (nowtok
!= tok_bsymbol
)
295 _("syntax error in repertoire map definition: %s"),
296 _("no symbolic name given for end of range"));
298 lr_ignore_rest (repfile
, 0);
303 /* Copy the to-name in a safe place. */
304 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
305 repfile
->token
.val
.str
.startmb
,
306 repfile
->token
.val
.str
.lenmb
);
312 if (nowtok
!= tok_charids
)
313 lr_error (repfile
, _("\
314 %1$s: definition does not end with `END %1$s'"), "CHARIDS");
316 lr_ignore_rest (repfile
, nowtok
== tok_charids
);
323 if (state
!= 2 && state
!= 90 && !be_quiet
)
324 WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"),
329 if (tsearch (result
, &known
, &repertoire_compare
) == NULL
)
330 /* Something went wrong. */
331 WITH_CUR_LOCALE (error (0, errno
, _("cannot save new repertoire map")));
338 repertoire_complain (const char *name
)
340 if (tfind (name
, &unavailable
, (__compar_fn_t
) strcmp
) == NULL
)
342 WITH_CUR_LOCALE (error (0, errno
, _("\
343 repertoire map file `%s' not found"), name
));
345 /* Remember that we reported this map. */
346 tsearch (name
, &unavailable
, (__compar_fn_t
) strcmp
);
352 repertoire_compare (const void *p1
, const void *p2
)
354 struct repertoire_t
*r1
= (struct repertoire_t
*) p1
;
355 struct repertoire_t
*r2
= (struct repertoire_t
*) p2
;
357 return strcmp (r1
->name
, r2
->name
);
361 static const struct keyword_t
*
362 repertoiremap_hash (const char *str
, unsigned int len
)
364 static const struct keyword_t wordlist
[] =
366 {"escape_char", tok_escape_char
, 0},
367 {"comment_char", tok_comment_char
, 0},
368 {"CHARIDS", tok_charids
, 0},
372 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
374 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
376 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
378 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)
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
)
394 int prefix_len
, len1
, len2
;
395 unsigned long int from_nr
, to_nr
, cnt
;
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. */
404 insert_entry (rt
, obstack_copy (ob
, &value
, sizeof (value
)),
405 sizeof (value
), (void *) from
);
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
);
419 lr_error (lr
, _("invalid names for character range"));
423 cp
= &from
[len1
- 1];
424 if (decimal_ellipsis
)
425 while (isdigit (*cp
) && cp
>= from
)
428 while (isxdigit (*cp
) && cp
>= from
)
430 if (!isdigit (*cp
) && !isupper (*cp
))
432 hexadecimal range format should use only capital characters"));
436 prefix_len
= (cp
- from
) + 1;
438 if (cp
== &from
[len1
- 1] || strncmp (from
, to
, prefix_len
) != 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
449 lr_error (lr
, _("<%s> and <%s> are invalid names for range"),
456 lr_error (lr
, _("upper limit in range is smaller than lower limit"));
460 for (cnt
= from_nr
; cnt
<= to_nr
; ++cnt
)
462 uint32_t this_value
= value
+ (cnt
- from_nr
);
464 obstack_printf (ob
, decimal_ellipsis
? "%.*s%0*ld" : "%.*s%0*lX",
465 prefix_len
, from
, len1
- prefix_len
, cnt
);
466 obstack_1grow (ob
, '\0');
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. */
473 insert_entry (rt
, obstack_copy (ob
, &this_value
, sizeof (this_value
)),
474 sizeof (this_value
), (void *) from
);
480 repertoire_find_value (const struct repertoire_t
*rep
, const char *name
,
486 return ILLEGAL_CHAR_VALUE
;
488 if (find_entry ((hash_table
*) &rep
->char_table
, name
, len
, &result
) < 0)
489 return ILLEGAL_CHAR_VALUE
;
491 return (uint32_t) ((unsigned long int) result
);
496 repertoire_find_symbol (const struct repertoire_t
*rep
, uint32_t ucs
)
503 if (find_entry ((hash_table
*) &rep
->reverse_table
, &ucs
, sizeof (ucs
),
507 return (const char *) result
;
512 repertoire_find_seq (const struct repertoire_t
*rep
, uint32_t ucs
)
519 if (find_entry ((hash_table
*) &rep
->seq_table
, &ucs
, sizeof (ucs
),
523 return (struct charseq
*) result
;