]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/repertoire.c
1 /* Copyright (C) 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
32 #include "linereader.h"
34 #include "repertoire.h"
35 #include "simple-hash.h"
38 extern void *xmalloc (size_t __n
);
41 /* Simple keyword hashing for the repertoiremap. */
42 static const struct keyword_t
*repertoiremap_hash (const char *str
, int len
);
46 repertoire_read (const char *filename
)
48 struct linereader
*repfile
;
49 struct repertoire_t
*result
;
51 char *from_name
= NULL
;
55 repfile
= lr_open (filename
, repertoiremap_hash
);
58 if (strchr (filename
, '/') == NULL
)
60 char *i18npath
= __secure_getenv ("I18NPATH");
61 if (i18npath
!= NULL
&& *i18npath
!= '\0')
63 char path
[strlen (filename
) + 1 + strlen (i18npath
)
64 + sizeof ("/repertoiremaps/") - 1];
66 i18npath
= strdupa (i18npath
);
69 while (repfile
== NULL
70 && (next
= strsep (&i18npath
, ":")) != NULL
)
72 stpcpy (stpcpy (stpcpy (path
, next
), "/repertoiremaps/"),
75 repfile
= lr_open (path
, repertoiremap_hash
);
81 /* Look in the systems charmap directory. */
82 char *buf
= xmalloc (strlen (filename
) + 1
83 + sizeof (REPERTOIREMAP_PATH
));
85 stpcpy (stpcpy (stpcpy (buf
, REPERTOIREMAP_PATH
), "/"),
87 repfile
= lr_open (buf
, repertoiremap_hash
);
96 error (0, errno
, _("repertoire map file `%s' not found"), filename
);
101 /* Allocate room for result. */
102 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
103 memset (result
, '\0', sizeof (struct repertoire_t
));
105 #define obstack_chunk_alloc malloc
106 #define obstack_chunk_free free
107 obstack_init (&result
->mem_pool
);
109 if (init_hash (&result
->char_table
, 256))
115 /* We use a state machine to describe the charmap description file
121 struct token
*now
= lr_token (repfile
, NULL
);
122 enum token_t nowtok
= now
->tok
;
125 if (nowtok
== tok_eof
)
131 /* We haven't yet read any character definition. This is where
132 we accept escape_char and comment_char definitions. */
133 if (nowtok
== tok_eol
)
134 /* Ignore empty lines. */
137 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
139 /* We know that we need an argument. */
140 arg
= lr_token (repfile
, NULL
);
142 if (arg
->tok
!= tok_ident
)
144 lr_error (repfile
, _("syntax error in prolog: %s"),
147 lr_ignore_rest (repfile
, 0);
151 if (arg
->val
.str
.len
!= 1)
153 lr_error (repfile
, _("\
154 argument to <%s> must be a single character"),
155 nowtok
== tok_escape_char
? "escape_char"
158 lr_ignore_rest (repfile
, 0);
162 if (nowtok
== tok_escape_char
)
163 repfile
->escape_char
= *arg
->val
.str
.start
;
165 repfile
->comment_char
= *arg
->val
.str
.start
;
167 lr_ignore_rest (repfile
, 1);
171 if (nowtok
== tok_charids
)
173 lr_ignore_rest (repfile
, 1);
179 /* Otherwise we start reading the character definitions. */
184 /* We are now are in the body. Each line
185 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
186 if (nowtok
== tok_eol
)
187 /* Ignore empty lines. */
190 if (nowtok
== tok_end
)
196 if (nowtok
!= tok_bsymbol
)
199 _("syntax error in repertoire map definition: %s"),
200 _("no symbolic name given"));
202 lr_ignore_rest (repfile
, 0);
206 /* If the previous line was not completely correct free the
208 if (from_name
!= NULL
)
209 obstack_free (&result
->mem_pool
, from_name
);
211 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
220 /* We have two possibilities: We can see an ellipsis or an
222 if (nowtok
== tok_ellipsis
)
230 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
231 the xxx mean a hexadecimal value. */
235 if (nowtok
!= tok_ucs2
&& nowtok
!= tok_ucs4
)
238 _("syntax error in repertoire map definition: %s"),
239 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
241 lr_ignore_rest (repfile
, 0);
245 /* We've found a new valid definition. */
246 charset_new_char (repfile
, &result
->char_table
, 4,
247 now
->val
.charcode
.val
, from_name
, to_name
);
249 /* Ignore the rest of the line. */
250 lr_ignore_rest (repfile
, 0);
258 if (nowtok
!= tok_bsymbol
)
261 _("syntax error in repertoire map definition: %s"),
262 _("no symbolic name given for end of range"));
264 lr_ignore_rest (repfile
, 0);
269 /* Copy the to-name in a safe place. */
270 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
271 repfile
->token
.val
.str
.start
,
272 repfile
->token
.val
.str
.len
);
278 if (nowtok
!= tok_charids
)
279 lr_error (repfile
, _("\
280 `%1$s' definition does not end with `END %1$s'"), "CHARIDS");
282 lr_ignore_rest (repfile
, nowtok
== tok_charids
);
289 if (state
!= 2 && state
!= 90 && !be_quiet
)
290 error (0, 0, _("%s: premature end of file"), repfile
->fname
);
298 static const struct keyword_t
*
299 repertoiremap_hash (const char *str
, int len
)
301 static const struct keyword_t wordlist
[0] =
303 {"escape_char", tok_escape_char
, 0},
304 {"comment_char", tok_comment_char
, 0},
305 {"CHARIDS", tok_charids
, 0},
309 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
311 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
313 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
315 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)