(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / locale / programs / ld-telephone.c
blob5a60c016b0ce76447ff757ed4de83096efd5b657
1 /* Copyright (C) 1998, 1999, 2000, 2001, 2002 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 Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
24 #include <error.h>
25 #include <langinfo.h>
26 #include <string.h>
27 #include <sys/uio.h>
29 #include <assert.h>
31 #include "localedef.h"
32 #include "localeinfo.h"
33 #include "locfile.h"
36 /* The real definition of the struct for the LC_TELEPHONE locale. */
37 struct locale_telephone_t
39 const char *tel_int_fmt;
40 const char *tel_dom_fmt;
41 const char *int_select;
42 const char *int_prefix;
46 static void
47 telephone_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
50 if (!ignore_content)
51 locale->categories[LC_TELEPHONE].telephone = (struct locale_telephone_t *)
52 xcalloc (1, sizeof (struct locale_telephone_t));
54 if (lr != NULL)
56 lr->translate_strings = 1;
57 lr->return_widestr = 0;
62 void
63 telephone_finish (struct localedef_t *locale, const struct charmap_t *charmap)
65 struct locale_telephone_t *telephone =
66 locale->categories[LC_TELEPHONE].telephone;
67 int nothing = 0;
69 /* Now resolve copying and also handle completely missing definitions. */
70 if (telephone == NULL)
72 /* First see whether we were supposed to copy. If yes, find the
73 actual definition. */
74 if (locale->copy_name[LC_TELEPHONE] != NULL)
76 /* Find the copying locale. This has to happen transitively since
77 the locale we are copying from might also copying another one. */
78 struct localedef_t *from = locale;
81 from = find_locale (LC_TELEPHONE, from->copy_name[LC_TELEPHONE],
82 from->repertoire_name, charmap);
83 while (from->categories[LC_TELEPHONE].telephone == NULL
84 && from->copy_name[LC_TELEPHONE] != NULL);
86 telephone = locale->categories[LC_TELEPHONE].telephone
87 = from->categories[LC_TELEPHONE].telephone;
90 /* If there is still no definition issue an warning and create an
91 empty one. */
92 if (telephone == NULL)
94 if (! be_quiet)
95 WITH_CUR_LOCALE (error (0, 0, _("\
96 No definition for %s category found"), "LC_TELEPHONE"));
97 telephone_startup (NULL, locale, 0);
98 telephone = locale->categories[LC_TELEPHONE].telephone;
99 nothing = 1;
103 if (telephone->tel_int_fmt == NULL)
105 if (! nothing)
106 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
107 "LC_TELEPHONE", "tel_int_fmt"));
108 /* Use as the default value the value of the i18n locale. */
109 telephone->tel_int_fmt = "+%c %a %l";
111 else
113 /* We must check whether the format string contains only the
114 allowed escape sequences. */
115 const char *cp = telephone->tel_int_fmt;
117 if (*cp == '\0')
118 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
119 "LC_TELEPHONE", "tel_int_fmt"));
120 else
121 while (*cp != '\0')
123 if (*cp == '%')
125 if (strchr ("aAlc", *++cp) == NULL)
127 WITH_CUR_LOCALE (error (0, 0, _("\
128 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_int_fmt"));
129 break;
132 ++cp;
136 if (telephone->tel_dom_fmt == NULL)
137 telephone->tel_dom_fmt = "";
138 else if (telephone->tel_dom_fmt[0] != '\0')
140 /* We must check whether the format string contains only the
141 allowed escape sequences. */
142 const char *cp = telephone->tel_dom_fmt;
144 while (*cp != '\0')
146 if (*cp == '%')
148 if (strchr ("aAlc", *++cp) == NULL)
150 WITH_CUR_LOCALE (error (0, 0, _("\
151 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_dom_fmt"));
152 break;
155 ++cp;
159 #define TEST_ELEM(cat) \
160 if (telephone->cat == NULL) \
162 if (verbose && ! nothing) \
163 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
164 "LC_TELEPHONE", #cat)); \
165 telephone->cat = ""; \
168 TEST_ELEM (int_select);
169 TEST_ELEM (int_prefix);
173 void
174 telephone_output (struct localedef_t *locale, const struct charmap_t *charmap,
175 const char *output_path)
177 struct locale_telephone_t *telephone =
178 locale->categories[LC_TELEPHONE].telephone;
179 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE)];
180 struct locale_file data;
181 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE)];
182 size_t cnt = 0;
184 data.magic = LIMAGIC (LC_TELEPHONE);
185 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE);
186 iov[cnt].iov_base = (void *) &data;
187 iov[cnt].iov_len = sizeof (data);
188 ++cnt;
190 iov[cnt].iov_base = (void *) idx;
191 iov[cnt].iov_len = sizeof (idx);
192 ++cnt;
194 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
195 iov[cnt].iov_base = (void *) telephone->tel_int_fmt;
196 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
197 ++cnt;
199 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
200 iov[cnt].iov_base = (void *) telephone->tel_dom_fmt;
201 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
202 ++cnt;
204 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
205 iov[cnt].iov_base = (void *) telephone->int_select;
206 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
207 ++cnt;
209 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
210 iov[cnt].iov_base = (void *) telephone->int_prefix;
211 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
212 ++cnt;
214 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
215 iov[cnt].iov_base = (void *) charmap->code_set_name;;
216 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
217 ++cnt;
219 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE));
221 write_locale_data (output_path, LC_TELEPHONE, "LC_TELEPHONE",
222 2 + _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE), iov);
226 /* The parser for the LC_TELEPHONE section of the locale definition. */
227 void
228 telephone_read (struct linereader *ldfile, struct localedef_t *result,
229 const struct charmap_t *charmap, const char *repertoire_name,
230 int ignore_content)
232 struct locale_telephone_t *telephone;
233 struct token *now;
234 struct token *arg;
235 enum token_t nowtok;
237 /* The rest of the line containing `LC_TELEPHONE' must be free. */
238 lr_ignore_rest (ldfile, 1);
242 now = lr_token (ldfile, charmap, result, NULL, verbose);
243 nowtok = now->tok;
245 while (nowtok == tok_eol);
247 /* If we see `copy' now we are almost done. */
248 if (nowtok == tok_copy)
250 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_telephone,
251 LC_TELEPHONE, "LC_TELEPHONE", ignore_content);
252 return;
255 /* Prepare the data structures. */
256 telephone_startup (ldfile, result, ignore_content);
257 telephone = result->categories[LC_TELEPHONE].telephone;
259 while (1)
261 /* Of course we don't proceed beyond the end of file. */
262 if (nowtok == tok_eof)
263 break;
265 /* Ingore empty lines. */
266 if (nowtok == tok_eol)
268 now = lr_token (ldfile, charmap, result, NULL, verbose);
269 nowtok = now->tok;
270 continue;
273 switch (nowtok)
275 #define STR_ELEM(cat) \
276 case tok_##cat: \
277 /* Ignore the rest of the line if we don't need the input of \
278 this line. */ \
279 if (ignore_content) \
281 lr_ignore_rest (ldfile, 0); \
282 break; \
285 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
286 if (arg->tok != tok_string) \
287 goto err_label; \
288 if (telephone->cat != NULL) \
289 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
290 "LC_TELEPHONE", #cat); \
291 else if (!ignore_content && arg->val.str.startmb == NULL) \
293 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
294 "LC_TELEPHONE", #cat); \
295 telephone->cat = ""; \
297 else if (!ignore_content) \
298 telephone->cat = arg->val.str.startmb; \
299 break
301 STR_ELEM (tel_int_fmt);
302 STR_ELEM (tel_dom_fmt);
303 STR_ELEM (int_select);
304 STR_ELEM (int_prefix);
306 case tok_end:
307 /* Next we assume `LC_TELEPHONE'. */
308 arg = lr_token (ldfile, charmap, result, NULL, verbose);
309 if (arg->tok == tok_eof)
310 break;
311 if (arg->tok == tok_eol)
312 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TELEPHONE");
313 else if (arg->tok != tok_lc_telephone)
314 lr_error (ldfile, _("\
315 %1$s: definition does not end with `END %1$s'"), "LC_TELEPHONE");
316 lr_ignore_rest (ldfile, arg->tok == tok_lc_telephone);
317 return;
319 default:
320 err_label:
321 SYNTAX_ERROR (_("%s: syntax error"), "LC_TELEPHONE");
324 /* Prepare for the next round. */
325 now = lr_token (ldfile, charmap, result, NULL, verbose);
326 nowtok = now->tok;
329 /* When we come here we reached the end of the file. */
330 lr_error (ldfile, _("%s: premature end of file"), "LC_TELEPHONE");