]>
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. */
31 #include "linereader.h"
33 #include "repertoire.h"
34 #include "simple-hash.h"
37 extern void *xmalloc (size_t __n
);
40 /* Simple keyword hashing for the repertoiremap. */
41 static struct repertoire_t
*parse_repertoiremap (const char *filename
);
42 static const struct keyword_t
*repertoiremap_hash (const char *str
, int len
);
46 repertoire_read (const char *filename
)
48 const char *pathnfile
;
49 struct repertoire_t
*result
= NULL
;
51 if (euidaccess (filename
, R_OK
) >= 0)
53 else if (filename
[0] != '/')
55 char *cp
= xmalloc (strlen (filename
) + sizeof CHARMAP_PATH
+ 1);
56 stpcpy (stpcpy (stpcpy (cp
, CHARMAP_PATH
), "/"), filename
);
58 pathnfile
= (const char *) cp
;
63 if (pathnfile
!= NULL
)
65 result
= parse_repertoiremap (pathnfile
);
67 if (result
== NULL
&& !be_quiet
)
68 error (0, errno
, _("repertoire map file `%s' not found"), filename
);
75 static struct repertoire_t
*
76 parse_repertoiremap (const char *filename
)
78 struct linereader
*cmfile
;
79 struct repertoire_t
*result
;
81 char *from_name
= NULL
;
85 cmfile
= lr_open (filename
, repertoiremap_hash
);
88 if (strchr (filename
, '/') == NULL
)
90 /* Look in the systems charmap directory. */
91 char *buf
= xmalloc (strlen (filename
) + 1
92 + sizeof (REPERTOIREMAP_PATH
));
94 stpcpy (stpcpy (stpcpy (buf
, REPERTOIREMAP_PATH
), "/"), filename
);
95 cmfile
= lr_open (buf
, repertoiremap_hash
);
105 /* Allocate room for result. */
106 result
= (struct repertoire_t
*) xmalloc (sizeof (struct repertoire_t
));
107 memset (result
, '\0', sizeof (struct repertoire_t
));
109 #define obstack_chunk_alloc malloc
110 #define obstack_chunk_free free
111 obstack_init (&result
->mem_pool
);
113 if (init_hash (&result
->char_table
, 256))
119 /* We use a state machine to describe the charmap description file
125 struct token
*now
= lr_token (cmfile
, NULL
);
126 enum token_t nowtok
= now
->tok
;
129 if (nowtok
== tok_eof
)
135 /* We haven't yet read any character definition. This is where
136 we accept escape_char and comment_char definitions. */
137 if (nowtok
== tok_eol
)
138 /* Ignore empty lines. */
141 if (nowtok
== tok_escape_char
|| nowtok
== tok_comment_char
)
143 /* We know that we need an argument. */
144 arg
= lr_token (cmfile
, NULL
);
146 if (arg
->tok
!= tok_ident
)
148 lr_error (cmfile
, _("syntax error in prolog: %s"),
151 lr_ignore_rest (cmfile
, 0);
155 if (arg
->val
.str
.len
!= 1)
157 lr_error (cmfile
, _("\
158 argument to <%s> must be a single character"),
159 nowtok
== tok_escape_char
? "escape_char"
162 lr_ignore_rest (cmfile
, 0);
166 if (nowtok
== tok_escape_char
)
167 cmfile
->escape_char
= *arg
->val
.str
.start
;
169 cmfile
->comment_char
= *arg
->val
.str
.start
;
171 lr_ignore_rest (cmfile
, 1);
175 if (nowtok
== tok_charids
)
177 lr_ignore_rest (cmfile
, 1);
183 /* Otherwise we start reading the character definitions. */
188 /* We are now are in the body. Each line
189 must have the format "%s %s %s\n" or "%s...%s %s %s\n". */
190 if (nowtok
== tok_eol
)
191 /* Ignore empty lines. */
194 if (nowtok
== tok_end
)
200 if (nowtok
!= tok_bsymbol
)
203 _("syntax error in repertoire map definition: %s"),
204 _("no symbolic name given"));
206 lr_ignore_rest (cmfile
, 0);
210 /* If the previous line was not completely correct free the
212 if (from_name
!= NULL
)
213 obstack_free (&result
->mem_pool
, from_name
);
215 from_name
= (char *) obstack_copy0 (&result
->mem_pool
,
224 /* We have two possibilities: We can see an ellipsis or an
226 if (nowtok
== tok_ellipsis
)
234 /* We expect a value of the form <Uxxxx> or <Uxxxxxxxx> where
235 the xxx mean a hexadecimal value. */
239 if (nowtok
!= tok_ucs2
&& nowtok
!= tok_ucs4
)
242 _("syntax error in repertoire map definition: %s"),
243 _("no <Uxxxx> or <Uxxxxxxxx> value given"));
245 lr_ignore_rest (cmfile
, 0);
249 /* We've found a new valid definition. */
250 charset_new_char (cmfile
, &result
->char_table
, 4,
251 now
->val
.charcode
.val
, from_name
, to_name
);
253 /* Ignore the rest of the line. */
254 lr_ignore_rest (cmfile
, 0);
262 if (nowtok
!= tok_bsymbol
)
265 _("syntax error in repertoire map definition: %s"),
266 _("no symbolic name given for end of range"));
268 lr_ignore_rest (cmfile
, 0);
273 /* Copy the to-name in a safe place. */
274 to_name
= (char *) obstack_copy0 (&result
->mem_pool
,
275 cmfile
->token
.val
.str
.start
,
276 cmfile
->token
.val
.str
.len
);
282 if (nowtok
!= tok_charids
)
283 lr_error (cmfile
, _("\
284 `%1$s' definition does not end with `END %1$s'"), "CHARIDS");
286 lr_ignore_rest (cmfile
, nowtok
== tok_charids
);
293 if (state
!= 2 && state
!= 90 && !be_quiet
)
294 error (0, 0, _("%s: premature end of file"), cmfile
->fname
);
302 static const struct keyword_t
*
303 repertoiremap_hash (const char *str
, int len
)
305 static const struct keyword_t wordlist
[0] =
307 {"escape_char", tok_escape_char
, 1},
308 {"comment_char", tok_comment_char
, 1},
309 {"CHARIDS", tok_charids
, 0},
313 if (len
== 11 && memcmp (wordlist
[0].name
, str
, 11) == 0)
315 if (len
== 12 && memcmp (wordlist
[1].name
, str
, 12) == 0)
317 if (len
== 7 && memcmp (wordlist
[2].name
, str
, 7) == 0)
319 if (len
== 3 && memcmp (wordlist
[3].name
, str
, 3) == 0)