]> git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/repertoire.c
Update.
[thirdparty/glibc.git] / 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.
4
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.
9
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.
14
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. */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <errno.h>
25 #include <error.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <libintl.h>
31
32 #include "linereader.h"
33 #include "charset.h"
34 #include "repertoire.h"
35 #include "simple-hash.h"
36
37
38 extern void *xmalloc (size_t __n);
39
40
41 /* Simple keyword hashing for the repertoiremap. */
42 static const struct keyword_t *repertoiremap_hash (const char *str, int len);
43
44
45 struct repertoire_t *
46 repertoire_read (const char *filename)
47 {
48 struct linereader *repfile;
49 struct repertoire_t *result;
50 int state;
51 char *from_name = NULL;
52 char *to_name = NULL;
53
54 /* Determine path. */
55 repfile = lr_open (filename, repertoiremap_hash);
56 if (repfile == NULL)
57 {
58 if (strchr (filename, '/') == NULL)
59 {
60 char *i18npath = __secure_getenv ("I18NPATH");
61 if (i18npath != NULL && *i18npath != '\0')
62 {
63 char path[strlen (filename) + 1 + strlen (i18npath)
64 + sizeof ("/repertoiremaps/") - 1];
65 char *next;
66 i18npath = strdupa (i18npath);
67
68
69 while (repfile == NULL
70 && (next = strsep (&i18npath, ":")) != NULL)
71 {
72 stpcpy (stpcpy (stpcpy (path, next), "/repertoiremaps/"),
73 filename);
74
75 repfile = lr_open (path, repertoiremap_hash);
76 }
77 }
78
79 if (repfile == NULL)
80 {
81 /* Look in the systems charmap directory. */
82 char *buf = xmalloc (strlen (filename) + 1
83 + sizeof (REPERTOIREMAP_PATH));
84
85 stpcpy (stpcpy (stpcpy (buf, REPERTOIREMAP_PATH), "/"),
86 filename);
87 repfile = lr_open (buf, repertoiremap_hash);
88
89 if (repfile == NULL)
90 free (buf);
91 }
92 }
93
94 if (repfile == NULL)
95 {
96 error (0, errno, _("repertoire map file `%s' not found"), filename);
97 return NULL;
98 }
99 }
100
101 /* Allocate room for result. */
102 result = (struct repertoire_t *) xmalloc (sizeof (struct repertoire_t));
103 memset (result, '\0', sizeof (struct repertoire_t));
104
105 #define obstack_chunk_alloc malloc
106 #define obstack_chunk_free free
107 obstack_init (&result->mem_pool);
108
109 if (init_hash (&result->char_table, 256))
110 {
111 free (result);
112 return NULL;
113 }
114
115 /* We use a state machine to describe the charmap description file
116 format. */
117 state = 1;
118 while (1)
119 {
120 /* What's on? */
121 struct token *now = lr_token (repfile, NULL);
122 enum token_t nowtok = now->tok;
123 struct token *arg;
124
125 if (nowtok == tok_eof)
126 break;
127
128 switch (state)
129 {
130 case 1:
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. */
135 continue;
136
137 if (nowtok == tok_escape_char || nowtok == tok_comment_char)
138 {
139 /* We know that we need an argument. */
140 arg = lr_token (repfile, NULL);
141
142 if (arg->tok != tok_ident)
143 {
144 lr_error (repfile, _("syntax error in prolog: %s"),
145 _("bad argument"));
146
147 lr_ignore_rest (repfile, 0);
148 continue;
149 }
150
151 if (arg->val.str.len != 1)
152 {
153 lr_error (repfile, _("\
154 argument to <%s> must be a single character"),
155 nowtok == tok_escape_char ? "escape_char"
156 : "comment_char");
157
158 lr_ignore_rest (repfile, 0);
159 continue;
160 }
161
162 if (nowtok == tok_escape_char)
163 repfile->escape_char = *arg->val.str.start;
164 else
165 repfile->comment_char = *arg->val.str.start;
166
167 lr_ignore_rest (repfile, 1);
168 continue;
169 }
170
171 if (nowtok == tok_charids)
172 {
173 lr_ignore_rest (repfile, 1);
174
175 state = 2;
176 continue;
177 }
178
179 /* Otherwise we start reading the character definitions. */
180 state = 2;
181 /* FALLTHROUGH */
182
183 case 2:
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. */
188 continue;
189
190 if (nowtok == tok_end)
191 {
192 state = 90;
193 continue;
194 }
195
196 if (nowtok != tok_bsymbol)
197 {
198 lr_error (repfile,
199 _("syntax error in repertoire map definition: %s"),
200 _("no symbolic name given"));
201
202 lr_ignore_rest (repfile, 0);
203 continue;
204 }
205
206 /* If the previous line was not completely correct free the
207 used memory. */
208 if (from_name != NULL)
209 obstack_free (&result->mem_pool, from_name);
210
211 from_name = (char *) obstack_copy0 (&result->mem_pool,
212 now->val.str.start,
213 now->val.str.len);
214 to_name = NULL;
215
216 state = 3;
217 continue;
218
219 case 3:
220 /* We have two possibilities: We can see an ellipsis or an
221 encoding value. */
222 if (nowtok == tok_ellipsis)
223 {
224 state = 4;
225 continue;
226 }
227 /* FALLTHROUGH */
228
229 case 5:
230 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
231 the xxx mean a hexadecimal value. */
232 state = 2;
233
234 errno = 0;
235 if (nowtok != tok_ucs2 && nowtok != tok_ucs4)
236 {
237 lr_error (repfile,
238 _("syntax error in repertoire map definition: %s"),
239 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
240
241 lr_ignore_rest (repfile, 0);
242 continue;
243 }
244
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);
248
249 /* Ignore the rest of the line. */
250 lr_ignore_rest (repfile, 0);
251
252 from_name = NULL;
253 to_name = NULL;
254
255 continue;
256
257 case 4:
258 if (nowtok != tok_bsymbol)
259 {
260 lr_error (repfile,
261 _("syntax error in repertoire map definition: %s"),
262 _("no symbolic name given for end of range"));
263
264 lr_ignore_rest (repfile, 0);
265 state = 2;
266 continue;
267 }
268
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);
273
274 state = 5;
275 continue;
276
277 case 90:
278 if (nowtok != tok_charids)
279 lr_error (repfile, _("\
280 `%1$s' definition does not end with `END %1$s'"), "CHARIDS");
281
282 lr_ignore_rest (repfile, nowtok == tok_charids);
283 break;
284 }
285
286 break;
287 }
288
289 if (state != 2 && state != 90 && !be_quiet)
290 error (0, 0, _("%s: premature end of file"), repfile->fname);
291
292 lr_close (repfile);
293
294 return result;
295 }
296
297
298 static const struct keyword_t *
299 repertoiremap_hash (const char *str, int len)
300 {
301 static const struct keyword_t wordlist[0] =
302 {
303 {"escape_char", tok_escape_char, 0},
304 {"comment_char", tok_comment_char, 0},
305 {"CHARIDS", tok_charids, 0},
306 {"END", tok_end, 0},
307 };
308
309 if (len == 11 && memcmp (wordlist[0].name, str, 11) == 0)
310 return &wordlist[0];
311 if (len == 12 && memcmp (wordlist[1].name, str, 12) == 0)
312 return &wordlist[1];
313 if (len == 7 && memcmp (wordlist[2].name, str, 7) == 0)
314 return &wordlist[2];
315 if (len == 3 && memcmp (wordlist[3].name, str, 3) == 0)
316 return &wordlist[3];
317
318 return NULL;
319 }