PowerPC: remove branch prediction from rint implementation
[glibc.git] / locale / programs / ld-telephone.c
blobd842ea2e0ce3cc943f89736fc6c99b12e00bcb29
1 /* Copyright (C) 1998,1999,2000,2001,2002,2005 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, see <http://www.gnu.org/licenses/>. */
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
22 #include <error.h>
23 #include <langinfo.h>
24 #include <string.h>
25 #include <sys/uio.h>
27 #include <assert.h>
29 #include "localedef.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_TELEPHONE locale. */
35 struct locale_telephone_t
37 const char *tel_int_fmt;
38 const char *tel_dom_fmt;
39 const char *int_select;
40 const char *int_prefix;
44 static void
45 telephone_startup (struct linereader *lr, struct localedef_t *locale,
46 int ignore_content)
48 if (!ignore_content)
49 locale->categories[LC_TELEPHONE].telephone = (struct locale_telephone_t *)
50 xcalloc (1, sizeof (struct locale_telephone_t));
52 if (lr != NULL)
54 lr->translate_strings = 1;
55 lr->return_widestr = 0;
60 void
61 telephone_finish (struct localedef_t *locale, const struct charmap_t *charmap)
63 struct locale_telephone_t *telephone =
64 locale->categories[LC_TELEPHONE].telephone;
65 int nothing = 0;
67 /* Now resolve copying and also handle completely missing definitions. */
68 if (telephone == NULL)
70 /* First see whether we were supposed to copy. If yes, find the
71 actual definition. */
72 if (locale->copy_name[LC_TELEPHONE] != NULL)
74 /* Find the copying locale. This has to happen transitively since
75 the locale we are copying from might also copying another one. */
76 struct localedef_t *from = locale;
79 from = find_locale (LC_TELEPHONE, from->copy_name[LC_TELEPHONE],
80 from->repertoire_name, charmap);
81 while (from->categories[LC_TELEPHONE].telephone == NULL
82 && from->copy_name[LC_TELEPHONE] != NULL);
84 telephone = locale->categories[LC_TELEPHONE].telephone
85 = from->categories[LC_TELEPHONE].telephone;
88 /* If there is still no definition issue an warning and create an
89 empty one. */
90 if (telephone == NULL)
92 if (! be_quiet)
93 WITH_CUR_LOCALE (error (0, 0, _("\
94 No definition for %s category found"), "LC_TELEPHONE"));
95 telephone_startup (NULL, locale, 0);
96 telephone = locale->categories[LC_TELEPHONE].telephone;
97 nothing = 1;
101 if (telephone->tel_int_fmt == NULL)
103 if (! nothing)
104 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
105 "LC_TELEPHONE", "tel_int_fmt"));
106 /* Use as the default value the value of the i18n locale. */
107 telephone->tel_int_fmt = "+%c %a %l";
109 else
111 /* We must check whether the format string contains only the
112 allowed escape sequences. */
113 const char *cp = telephone->tel_int_fmt;
115 if (*cp == '\0')
116 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
117 "LC_TELEPHONE", "tel_int_fmt"));
118 else
119 while (*cp != '\0')
121 if (*cp == '%')
123 if (strchr ("aAlc", *++cp) == NULL)
125 WITH_CUR_LOCALE (error (0, 0, _("\
126 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_int_fmt"));
127 break;
130 ++cp;
134 if (telephone->tel_dom_fmt == NULL)
135 telephone->tel_dom_fmt = "";
136 else if (telephone->tel_dom_fmt[0] != '\0')
138 /* We must check whether the format string contains only the
139 allowed escape sequences. */
140 const char *cp = telephone->tel_dom_fmt;
142 while (*cp != '\0')
144 if (*cp == '%')
146 if (strchr ("aAlc", *++cp) == NULL)
148 WITH_CUR_LOCALE (error (0, 0, _("\
149 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_dom_fmt"));
150 break;
153 ++cp;
157 #define TEST_ELEM(cat) \
158 if (telephone->cat == NULL) \
160 if (verbose && ! nothing) \
161 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
162 "LC_TELEPHONE", #cat)); \
163 telephone->cat = ""; \
166 TEST_ELEM (int_select);
167 TEST_ELEM (int_prefix);
171 void
172 telephone_output (struct localedef_t *locale, const struct charmap_t *charmap,
173 const char *output_path)
175 struct locale_telephone_t *telephone =
176 locale->categories[LC_TELEPHONE].telephone;
177 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE)];
178 struct locale_file data;
179 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE)];
180 size_t cnt = 0;
182 data.magic = LIMAGIC (LC_TELEPHONE);
183 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE);
184 iov[cnt].iov_base = (void *) &data;
185 iov[cnt].iov_len = sizeof (data);
186 ++cnt;
188 iov[cnt].iov_base = (void *) idx;
189 iov[cnt].iov_len = sizeof (idx);
190 ++cnt;
192 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
193 iov[cnt].iov_base = (void *) telephone->tel_int_fmt;
194 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
195 ++cnt;
197 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
198 iov[cnt].iov_base = (void *) telephone->tel_dom_fmt;
199 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
200 ++cnt;
202 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
203 iov[cnt].iov_base = (void *) telephone->int_select;
204 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
205 ++cnt;
207 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
208 iov[cnt].iov_base = (void *) telephone->int_prefix;
209 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
210 ++cnt;
212 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
213 iov[cnt].iov_base = (void *) charmap->code_set_name;;
214 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
215 ++cnt;
217 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE));
219 write_locale_data (output_path, LC_TELEPHONE, "LC_TELEPHONE",
220 2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE), iov);
224 /* The parser for the LC_TELEPHONE section of the locale definition. */
225 void
226 telephone_read (struct linereader *ldfile, struct localedef_t *result,
227 const struct charmap_t *charmap, const char *repertoire_name,
228 int ignore_content)
230 struct locale_telephone_t *telephone;
231 struct token *now;
232 struct token *arg;
233 enum token_t nowtok;
235 /* The rest of the line containing `LC_TELEPHONE' must be free. */
236 lr_ignore_rest (ldfile, 1);
240 now = lr_token (ldfile, charmap, result, NULL, verbose);
241 nowtok = now->tok;
243 while (nowtok == tok_eol);
245 /* If we see `copy' now we are almost done. */
246 if (nowtok == tok_copy)
248 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_telephone,
249 LC_TELEPHONE, "LC_TELEPHONE", ignore_content);
250 return;
253 /* Prepare the data structures. */
254 telephone_startup (ldfile, result, ignore_content);
255 telephone = result->categories[LC_TELEPHONE].telephone;
257 while (1)
259 /* Of course we don't proceed beyond the end of file. */
260 if (nowtok == tok_eof)
261 break;
263 /* Ingore empty lines. */
264 if (nowtok == tok_eol)
266 now = lr_token (ldfile, charmap, result, NULL, verbose);
267 nowtok = now->tok;
268 continue;
271 switch (nowtok)
273 #define STR_ELEM(cat) \
274 case tok_##cat: \
275 /* Ignore the rest of the line if we don't need the input of \
276 this line. */ \
277 if (ignore_content) \
279 lr_ignore_rest (ldfile, 0); \
280 break; \
283 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
284 if (arg->tok != tok_string) \
285 goto err_label; \
286 if (telephone->cat != NULL) \
287 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
288 "LC_TELEPHONE", #cat); \
289 else if (!ignore_content && arg->val.str.startmb == NULL) \
291 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
292 "LC_TELEPHONE", #cat); \
293 telephone->cat = ""; \
295 else if (!ignore_content) \
296 telephone->cat = arg->val.str.startmb; \
297 break
299 STR_ELEM (tel_int_fmt);
300 STR_ELEM (tel_dom_fmt);
301 STR_ELEM (int_select);
302 STR_ELEM (int_prefix);
304 case tok_end:
305 /* Next we assume `LC_TELEPHONE'. */
306 arg = lr_token (ldfile, charmap, result, NULL, verbose);
307 if (arg->tok == tok_eof)
308 break;
309 if (arg->tok == tok_eol)
310 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TELEPHONE");
311 else if (arg->tok != tok_lc_telephone)
312 lr_error (ldfile, _("\
313 %1$s: definition does not end with `END %1$s'"), "LC_TELEPHONE");
314 lr_ignore_rest (ldfile, arg->tok == tok_lc_telephone);
315 return;
317 default:
318 err_label:
319 SYNTAX_ERROR (_("%s: syntax error"), "LC_TELEPHONE");
322 /* Prepare for the next round. */
323 now = lr_token (ldfile, charmap, result, NULL, verbose);
324 nowtok = now->tok;
327 /* When we come here we reached the end of the file. */
328 lr_error (ldfile, _("%s: premature end of file"), "LC_TELEPHONE");