(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / locale / programs / repertoire.c
blob933b88f5cda1c10b73ab06895715384075f90645
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
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <errno.h>
25 #include <error.h>
26 #include <limits.h>
27 #include <obstack.h>
28 #include <search.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
33 #include "localedef.h"
34 #include "linereader.h"
35 #include "charmap.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,
42 unsigned int len);
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. */
50 static void *known;
52 /* List of repertoire maps which are not available and which have been
53 reported to not be. */
54 static void *unavailable;
57 struct repertoire_t *
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;
64 int state;
65 char *from_name = NULL;
66 char *to_name = NULL;
67 enum token_t ellipsis = tok_none;
69 search.name = filename;
70 resultp = tfind (&search, &known, &repertoire_compare);
71 if (resultp != NULL)
72 return *resultp;
74 /* Determine path. */
75 repfile = lr_open (filename, repertoiremap_hash);
76 if (repfile == NULL)
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];
87 char *next;
88 i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1);
90 while (repfile == NULL
91 && (next = strsep (&i18npath, ":")) != NULL)
93 stpcpy (stpcpy (stpcpy (path, next), "/repertoiremaps/"),
94 filename);
96 repfile = lr_open (path, repertoiremap_hash);
98 if (repfile == NULL)
100 stpcpy (stpcpy (path, next), filename);
102 repfile = lr_open (path, repertoiremap_hash);
107 if (repfile == NULL)
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), "/"),
114 filename);
115 repfile = lr_open (buf, repertoiremap_hash);
117 if (repfile == NULL)
118 free (buf);
122 if (repfile == NULL)
123 return NULL;
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))
143 free (result);
144 return NULL;
147 /* We use a state machine to describe the charmap description file
148 format. */
149 state = 1;
150 while (1)
152 /* What's on? */
153 struct token *now = lr_token (repfile, NULL, NULL, NULL, verbose);
154 enum token_t nowtok = now->tok;
155 struct token *arg;
157 if (nowtok == tok_eof)
158 break;
160 switch (state)
162 case 1:
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. */
167 continue;
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"),
177 _("bad argument"));
179 lr_ignore_rest (repfile, 0);
180 continue;
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"
188 : "comment_char");
190 lr_ignore_rest (repfile, 0);
191 continue;
194 if (nowtok == tok_escape_char)
195 repfile->escape_char = *arg->val.str.startmb;
196 else
197 repfile->comment_char = *arg->val.str.startmb;
199 lr_ignore_rest (repfile, 1);
200 continue;
203 if (nowtok == tok_charids)
205 lr_ignore_rest (repfile, 1);
207 state = 2;
208 continue;
211 /* Otherwise we start reading the character definitions. */
212 state = 2;
213 /* FALLTHROUGH */
215 case 2:
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. */
220 continue;
222 if (nowtok == tok_end)
224 state = 90;
225 continue;
228 if (nowtok != tok_bsymbol)
230 lr_error (repfile,
231 _("syntax error in repertoire map definition: %s"),
232 _("no symbolic name given"));
234 lr_ignore_rest (repfile, 0);
235 continue;
238 /* If the previous line was not completely correct free the
239 used memory. */
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,
245 now->val.str.lenmb);
246 to_name = NULL;
248 state = 3;
249 continue;
251 case 3:
252 /* We have two possibilities: We can see an ellipsis or an
253 encoding value. */
254 if (nowtok == tok_ellipsis3 || nowtok == tok_ellipsis4
255 || nowtok == tok_ellipsis2)
257 ellipsis = nowtok;
258 state = 4;
259 continue;
261 /* FALLTHROUGH */
263 case 5:
264 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
265 the xxx mean a hexadecimal value. */
266 state = 2;
268 errno = 0;
269 if (nowtok != tok_ucs4)
271 lr_error (repfile,
272 _("syntax error in repertoire map definition: %s"),
273 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
275 lr_ignore_rest (repfile, 0);
276 continue;
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);
288 from_name = NULL;
289 to_name = NULL;
291 continue;
293 case 4:
294 if (nowtok != tok_bsymbol)
296 lr_error (repfile,
297 _("syntax error in repertoire map definition: %s"),
298 _("no symbolic name given for end of range"));
300 lr_ignore_rest (repfile, 0);
301 state = 2;
302 continue;
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);
310 state = 5;
311 continue;
313 case 90:
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);
319 break;
322 break;
325 if (state != 2 && state != 90 && !be_quiet)
326 WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"),
327 repfile->fname));
329 lr_close (repfile);
331 if (tsearch (result, &known, &repertoire_compare) == NULL)
332 /* Something went wrong. */
333 WITH_CUR_LOCALE (error (0, errno, _("cannot safe new repertoire map")));
335 return result;
339 void
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);
353 static int
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},
371 {"END", tok_end, 0},
374 if (len == 11 && memcmp (wordlist[0].name, str, 11) == 0)
375 return &wordlist[0];
376 if (len == 12 && memcmp (wordlist[1].name, str, 12) == 0)
377 return &wordlist[1];
378 if (len == 7 && memcmp (wordlist[2].name, str, 7) == 0)
379 return &wordlist[2];
380 if (len == 3 && memcmp (wordlist[3].name, str, 3) == 0)
381 return &wordlist[3];
383 return NULL;
387 static void
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)
392 char *from_end;
393 char *to_end;
394 const char *cp;
395 char *buf = NULL;
396 int prefix_len, len1, len2;
397 unsigned int from_nr, to_nr, cnt;
399 if (to == NULL)
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);
409 return;
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);
416 len2 = strlen (to);
418 if (len1 != len2)
420 invalid_range:
421 lr_error (lr, _("invalid names for character range"));
422 return;
425 cp = &from[len1 - 1];
426 if (decimal_ellipsis)
427 while (isdigit (*cp) && cp >= from)
428 --cp;
429 else
430 while (isxdigit (*cp) && cp >= from)
432 if (!isdigit (*cp) && !isupper (*cp))
433 lr_error (lr, _("\
434 hexadecimal range format should use only capital characters"));
435 --cp;
438 prefix_len = (cp - from) + 1;
440 if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
441 goto invalid_range;
443 errno = 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
448 && errno == ERANGE)
449 || *to_end != '\0')
451 lr_error (lr, _("<%s> and <%s> are invalid names for range"),
452 from, to);
453 return;
456 if (from_nr > to_nr)
458 lr_error (lr, _("upper limit in range is not smaller then lower limit"));
459 return;
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);
481 uint32_t
482 repertoire_find_value (const struct repertoire_t *rep, const char *name,
483 size_t len)
485 void *result;
487 if (rep == NULL)
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);
497 const char *
498 repertoire_find_symbol (const struct repertoire_t *rep, uint32_t ucs)
500 void *result;
502 if (rep == NULL)
503 return NULL;
505 if (find_entry ((hash_table *) &rep->reverse_table, &ucs, sizeof (ucs),
506 &result) < 0)
507 return NULL;
509 return (const char *) result;
513 struct charseq *
514 repertoire_find_seq (const struct repertoire_t *rep, uint32_t ucs)
516 void *result;
518 if (rep == NULL)
519 return NULL;
521 if (find_entry ((hash_table *) &rep->seq_table, &ucs, sizeof (ucs),
522 &result) < 0)
523 return NULL;
525 return (struct charseq *) result;