* inet/tst-network.c: Increment ERRORS for failing tests.
[glibc.git] / locale / programs / repertoire.c
blobcc83374c9f2441518229de50cf38e4049fe75e10
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. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <errno.h>
24 #include <error.h>
25 #include <limits.h>
26 #include <obstack.h>
27 #include <search.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
32 #include "localedef.h"
33 #include "linereader.h"
34 #include "charmap.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,
41 unsigned int len);
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. */
49 static void *known;
51 /* List of repertoire maps which are not available and which have been
52 reported to not be. */
53 static void *unavailable;
56 struct repertoire_t *
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;
63 int state;
64 char *from_name = NULL;
65 char *to_name = NULL;
66 enum token_t ellipsis = tok_none;
68 search.name = filename;
69 resultp = tfind (&search, &known, &repertoire_compare);
70 if (resultp != NULL)
71 return *resultp;
73 /* Determine path. */
74 repfile = lr_open (filename, repertoiremap_hash);
75 if (repfile == NULL)
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];
86 char *next;
87 i18npath = memcpy (i18npathbuf, i18npath, pathlen + 1);
89 while (repfile == NULL
90 && (next = strsep (&i18npath, ":")) != NULL)
92 stpcpy (stpcpy (stpcpy (path, next), "/repertoiremaps/"),
93 filename);
95 repfile = lr_open (path, repertoiremap_hash);
97 if (repfile == NULL)
99 stpcpy (stpcpy (path, next), filename);
101 repfile = lr_open (path, repertoiremap_hash);
106 if (repfile == NULL)
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), "/"),
113 filename);
114 repfile = lr_open (buf, repertoiremap_hash);
116 if (repfile == NULL)
117 free (buf);
121 if (repfile == NULL)
122 return NULL;
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))
142 free (result);
143 return NULL;
146 /* We use a state machine to describe the charmap description file
147 format. */
148 state = 1;
149 while (1)
151 /* What's on? */
152 struct token *now = lr_token (repfile, NULL, NULL, NULL, verbose);
153 enum token_t nowtok = now->tok;
154 struct token *arg;
156 if (nowtok == tok_eof)
157 break;
159 switch (state)
161 case 1:
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. */
166 continue;
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"),
176 _("bad argument"));
178 lr_ignore_rest (repfile, 0);
179 continue;
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"
187 : "comment_char");
189 lr_ignore_rest (repfile, 0);
190 continue;
193 if (nowtok == tok_escape_char)
194 repfile->escape_char = *arg->val.str.startmb;
195 else
196 repfile->comment_char = *arg->val.str.startmb;
198 lr_ignore_rest (repfile, 1);
199 continue;
202 if (nowtok == tok_charids)
204 lr_ignore_rest (repfile, 1);
206 state = 2;
207 continue;
210 /* Otherwise we start reading the character definitions. */
211 state = 2;
212 /* FALLTHROUGH */
214 case 2:
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. */
219 continue;
221 if (nowtok == tok_end)
223 state = 90;
224 continue;
227 if (nowtok != tok_bsymbol)
229 lr_error (repfile,
230 _("syntax error in repertoire map definition: %s"),
231 _("no symbolic name given"));
233 lr_ignore_rest (repfile, 0);
234 continue;
237 /* If the previous line was not completely correct free the
238 used memory. */
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,
244 now->val.str.lenmb);
245 to_name = NULL;
247 state = 3;
248 continue;
250 case 3:
251 /* We have two possibilities: We can see an ellipsis or an
252 encoding value. */
253 if (nowtok == tok_ellipsis3 || nowtok == tok_ellipsis4
254 || nowtok == tok_ellipsis2)
256 ellipsis = nowtok;
257 state = 4;
258 continue;
260 /* FALLTHROUGH */
262 case 5:
263 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
264 the xxx mean a hexadecimal value. */
265 state = 2;
267 errno = 0;
268 if (nowtok != tok_ucs4)
270 lr_error (repfile,
271 _("syntax error in repertoire map definition: %s"),
272 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
274 lr_ignore_rest (repfile, 0);
275 continue;
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);
287 from_name = NULL;
288 to_name = NULL;
290 continue;
292 case 4:
293 if (nowtok != tok_bsymbol)
295 lr_error (repfile,
296 _("syntax error in repertoire map definition: %s"),
297 _("no symbolic name given for end of range"));
299 lr_ignore_rest (repfile, 0);
300 state = 2;
301 continue;
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);
309 state = 5;
310 continue;
312 case 90:
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);
318 break;
321 break;
324 if (state != 2 && state != 90 && !be_quiet)
325 WITH_CUR_LOCALE (error (0, 0, _("%s: premature end of file"),
326 repfile->fname));
328 lr_close (repfile);
330 if (tsearch (result, &known, &repertoire_compare) == NULL)
331 /* Something went wrong. */
332 WITH_CUR_LOCALE (error (0, errno, _("cannot save new repertoire map")));
334 return result;
338 void
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);
352 static int
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},
370 {"END", tok_end, 0},
373 if (len == 11 && memcmp (wordlist[0].name, str, 11) == 0)
374 return &wordlist[0];
375 if (len == 12 && memcmp (wordlist[1].name, str, 12) == 0)
376 return &wordlist[1];
377 if (len == 7 && memcmp (wordlist[2].name, str, 7) == 0)
378 return &wordlist[2];
379 if (len == 3 && memcmp (wordlist[3].name, str, 3) == 0)
380 return &wordlist[3];
382 return NULL;
386 static void
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)
391 char *from_end;
392 char *to_end;
393 const char *cp;
394 char *buf = NULL;
395 int prefix_len, len1, len2;
396 unsigned long int from_nr, to_nr, cnt;
398 if (to == NULL)
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);
408 return;
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);
415 len2 = strlen (to);
417 if (len1 != len2)
419 invalid_range:
420 lr_error (lr, _("invalid names for character range"));
421 return;
424 cp = &from[len1 - 1];
425 if (decimal_ellipsis)
426 while (isdigit (*cp) && cp >= from)
427 --cp;
428 else
429 while (isxdigit (*cp) && cp >= from)
431 if (!isdigit (*cp) && !isupper (*cp))
432 lr_error (lr, _("\
433 hexadecimal range format should use only capital characters"));
434 --cp;
437 prefix_len = (cp - from) + 1;
439 if (cp == &from[len1 - 1] || strncmp (from, to, prefix_len) != 0)
440 goto invalid_range;
442 errno = 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
447 && errno == ERANGE)
448 || *to_end != '\0')
450 lr_error (lr, _("<%s> and <%s> are invalid names for range"),
451 from, to);
452 return;
455 if (from_nr > to_nr)
457 lr_error (lr, _("upper limit in range is smaller than lower limit"));
458 return;
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*ld" : "%.*s%0*lX",
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);
480 uint32_t
481 repertoire_find_value (const struct repertoire_t *rep, const char *name,
482 size_t len)
484 void *result;
486 if (rep == NULL)
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);
496 const char *
497 repertoire_find_symbol (const struct repertoire_t *rep, uint32_t ucs)
499 void *result;
501 if (rep == NULL)
502 return NULL;
504 if (find_entry ((hash_table *) &rep->reverse_table, &ucs, sizeof (ucs),
505 &result) < 0)
506 return NULL;
508 return (const char *) result;
512 struct charseq *
513 repertoire_find_seq (const struct repertoire_t *rep, uint32_t ucs)
515 void *result;
517 if (rep == NULL)
518 return NULL;
520 if (find_entry ((hash_table *) &rep->seq_table, &ucs, sizeof (ucs),
521 &result) < 0)
522 return NULL;
524 return (struct charseq *) result;