]>
git.ipfire.org Git - thirdparty/glibc.git/blob - locale/programs/locfile.c
1 /* Copyright (C) 1996, 1997, 1998, 1999 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@gnu.org>, 1996.
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. */
28 #include <sys/param.h>
31 #include "localedef.h"
34 #include "locfile-kw.h"
38 locfile_read (struct localedef_t
*result
, struct charmap_t
*charmap
)
40 const char *filename
= result
->name
;
41 const char *repertoire_name
= result
->repertoire_name
;
42 int locale_mask
= result
->needed
^ result
->avail
;
43 struct linereader
*ldfile
;
45 /* If no repertoire name was specified use the global one. */
46 if (repertoire_name
== NULL
)
47 repertoire_name
= repertoire_global
;
49 /* Open the locale definition file. */
50 ldfile
= lr_open (filename
, locfile_hash
);
53 if (filename
[0] != '/')
55 char *i18npath
= getenv ("I18NPATH");
56 if (i18npath
!= NULL
&& *i18npath
!= '\0')
58 char path
[strlen (filename
) + 1 + strlen (i18npath
)
59 + sizeof ("/locales/") - 1];
61 i18npath
= strdupa (i18npath
);
65 && (next
= strsep (&i18npath
, ":")) != NULL
)
67 stpcpy (stpcpy (stpcpy (path
, next
), "/locales/"), filename
);
69 ldfile
= lr_open (path
, locfile_hash
);
73 stpcpy (stpcpy (path
, next
), filename
);
75 ldfile
= lr_open (path
, locfile_hash
);
80 /* Test in the default directory. */
83 char path
[strlen (filename
) + 1 + sizeof (LOCSRCDIR
)];
85 stpcpy (stpcpy (stpcpy (path
, LOCSRCDIR
), "/"), filename
);
86 ldfile
= lr_open (path
, locfile_hash
);
94 /* Parse locale definition file and store result in RESULT. */
97 struct token
*now
= lr_token (ldfile
, charmap
, NULL
);
98 enum token_t nowtok
= now
->tok
;
101 if (nowtok
== tok_eof
)
104 if (nowtok
== tok_eol
)
105 /* Ignore empty lines. */
110 case tok_escape_char
:
111 case tok_comment_char
:
112 /* We need an argument. */
113 arg
= lr_token (ldfile
, charmap
, NULL
);
115 if (arg
->tok
!= tok_ident
)
117 SYNTAX_ERROR (_("bad argument"));
121 if (arg
->val
.str
.lenmb
!= 1)
123 lr_error (ldfile
, _("\
124 argument to `%s' must be a single character"),
125 nowtok
== tok_escape_char
126 ? "escape_char" : "comment_char");
128 lr_ignore_rest (ldfile
, 0);
132 if (nowtok
== tok_escape_char
)
133 ldfile
->escape_char
= *arg
->val
.str
.startmb
;
135 ldfile
->comment_char
= *arg
->val
.str
.startmb
;
138 case tok_repertoiremap
:
139 /* We need an argument. */
140 arg
= lr_token (ldfile
, charmap
, NULL
);
142 if (arg
->tok
!= tok_ident
)
144 SYNTAX_ERROR (_("bad argument"));
148 if (repertoire_name
== NULL
)
150 repertoire_name
= memcpy (xmalloc (arg
->val
.str
.lenmb
+ 1),
151 arg
->val
.str
.startmb
,
153 ((char *) repertoire_name
)[arg
->val
.str
.lenmb
] = '\0';
158 ctype_read (ldfile
, result
, charmap
, repertoire_name
,
159 (locale_mask
& CTYPE_LOCALE
) == 0);
160 result
->avail
|= locale_mask
& CTYPE_LOCALE
;
164 collate_read (ldfile
, result
, charmap
, repertoire_name
,
165 (locale_mask
& COLLATE_LOCALE
) == 0);
166 result
->avail
|= locale_mask
& COLLATE_LOCALE
;
169 case tok_lc_monetary
:
170 monetary_read (ldfile
, result
, charmap
, repertoire_name
,
171 (locale_mask
& MONETARY_LOCALE
) == 0);
172 result
->avail
|= locale_mask
& MONETARY_LOCALE
;
176 numeric_read (ldfile
, result
, charmap
, repertoire_name
,
177 (locale_mask
& NUMERIC_LOCALE
) == 0);
178 result
->avail
|= locale_mask
& NUMERIC_LOCALE
;
182 time_read (ldfile
, result
, charmap
, repertoire_name
,
183 (locale_mask
& TIME_LOCALE
) == 0);
184 result
->avail
|= locale_mask
& TIME_LOCALE
;
187 case tok_lc_messages
:
188 messages_read (ldfile
, result
, charmap
, repertoire_name
,
189 (locale_mask
& MESSAGES_LOCALE
) == 0);
190 result
->avail
|= locale_mask
& MESSAGES_LOCALE
;
194 paper_read (ldfile
, result
, charmap
, repertoire_name
,
195 (locale_mask
& PAPER_LOCALE
) == 0);
196 result
->avail
|= locale_mask
& PAPER_LOCALE
;
200 name_read (ldfile
, result
, charmap
, repertoire_name
,
201 (locale_mask
& NAME_LOCALE
) == 0);
202 result
->avail
|= locale_mask
& NAME_LOCALE
;
206 address_read (ldfile
, result
, charmap
, repertoire_name
,
207 (locale_mask
& ADDRESS_LOCALE
) == 0);
208 result
->avail
|= locale_mask
& ADDRESS_LOCALE
;
211 case tok_lc_telephone
:
212 telephone_read (ldfile
, result
, charmap
, repertoire_name
,
213 (locale_mask
& TELEPHONE_LOCALE
) == 0);
214 result
->avail
|= locale_mask
& TELEPHONE_LOCALE
;
217 case tok_lc_measurement
:
218 measurement_read (ldfile
, result
, charmap
, repertoire_name
,
219 (locale_mask
& MEASUREMENT_LOCALE
) == 0);
220 result
->avail
|= locale_mask
& MEASUREMENT_LOCALE
;
223 case tok_lc_identification
:
224 identification_read (ldfile
, result
, charmap
, repertoire_name
,
225 (locale_mask
& IDENTIFICATION_LOCALE
) == 0);
226 result
->avail
|= locale_mask
& IDENTIFICATION_LOCALE
;
231 syntax error: not inside a locale definition section"));
235 /* The rest of the line must be empty. */
236 lr_ignore_rest (ldfile
, 1);
239 /* We read all of the file. */
246 static void (*const check_funcs
[]) (struct localedef_t
*,
247 struct charmap_t
*) =
249 [LC_CTYPE
] = ctype_finish
,
250 [LC_COLLATE
] = collate_finish
,
251 [LC_MESSAGES
] = messages_finish
,
252 [LC_MONETARY
] = monetary_finish
,
253 [LC_NUMERIC
] = numeric_finish
,
254 [LC_TIME
] = time_finish
,
255 [LC_PAPER
] = paper_finish
,
256 [LC_NAME
] = name_finish
,
257 [LC_ADDRESS
] = address_finish
,
258 [LC_TELEPHONE
] = telephone_finish
,
259 [LC_MEASUREMENT
] = measurement_finish
,
260 [LC_IDENTIFICATION
] = identification_finish
265 check_all_categories (struct localedef_t
*definitions
,
266 struct charmap_t
*charmap
)
270 for (cnt
= 0; cnt
< sizeof (check_funcs
) / sizeof (check_funcs
[0]); ++cnt
)
271 if (check_funcs
[cnt
] != NULL
)
272 check_funcs
[cnt
] (definitions
, charmap
);
276 static void (*const write_funcs
[]) (struct localedef_t
*, struct charmap_t
*,
279 [LC_CTYPE
] = ctype_output
,
280 [LC_COLLATE
] = collate_output
,
281 [LC_MESSAGES
] = messages_output
,
282 [LC_MONETARY
] = monetary_output
,
283 [LC_NUMERIC
] = numeric_output
,
284 [LC_TIME
] = time_output
,
285 [LC_PAPER
] = paper_output
,
286 [LC_NAME
] = name_output
,
287 [LC_ADDRESS
] = address_output
,
288 [LC_TELEPHONE
] = telephone_output
,
289 [LC_MEASUREMENT
] = measurement_output
,
290 [LC_IDENTIFICATION
] = identification_output
295 write_all_categories (struct localedef_t
*definitions
,
296 struct charmap_t
*charmap
,
297 const char *output_path
)
301 for (cnt
= 0; cnt
< sizeof (write_funcs
) / sizeof (write_funcs
[0]); ++cnt
)
302 if (check_funcs
[cnt
] != NULL
)
303 write_funcs
[cnt
] (definitions
, charmap
, output_path
);
308 write_locale_data (const char *output_path
, const char *category
,
309 size_t n_elem
, struct iovec
*vec
)
311 size_t cnt
, step
, maxiov
;
315 fname
= malloc (strlen (output_path
) + 2 * strlen (category
) + 6);
317 error (5, errno
, _("memory exhausted"));
319 /* Normally we write to the directory pointed to by the OUTPUT_PATH.
320 But for LC_MESSAGES we have to take care for the translation
321 data. This means we need to have a directory LC_MESSAGES in
322 which we place the file under the name SYS_LC_MESSAGES. */
323 sprintf (fname
, "%s/%s", output_path
, category
);
324 if (strcmp (category
, "LC_MESSAGES") == 0)
328 if (stat (fname
, &st
) < 0)
330 if (mkdir (fname
, 0777) < 0)
331 fd
= creat (fname
, 0666);
338 else if (S_ISREG (st
.st_mode
))
339 fd
= creat (fname
, 0666);
347 fd
= creat (fname
, 0666);
351 int save_err
= errno
;
355 sprintf (fname
, "%1$s/%2$s/SYS_%2$s", output_path
, category
);
356 fd
= creat (fname
, 0666);
364 error (0, save_err
, _("\
365 cannot open output file `%s' for category `%s'"),
375 maxiov
= sysconf (_SC_UIO_MAXIOV
);
378 /* Write the data using writev. But we must take care for the
379 limitation of the implementation. */
380 for (cnt
= 0; cnt
< n_elem
; cnt
+= step
)
384 step
= MIN (maxiov
, step
);
386 if (writev (fd
, &vec
[cnt
], step
) < 0)
389 error (0, errno
, _("failure while writing data for category `%s'"),