Update syscall lists for Linux 6.6
[glibc.git] / locale / programs / ld-telephone.c
blob1481e8edbc16134f8aa997ec38aba802d5ba3e75
1 /* Copyright (C) 1998-2023 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published
6 by the Free Software Foundation; version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "localeinfo.h"
30 #include "locfile.h"
33 /* The real definition of the struct for the LC_TELEPHONE locale. */
34 struct locale_telephone_t
36 const char *tel_int_fmt;
37 const char *tel_dom_fmt;
38 const char *int_select;
39 const char *int_prefix;
43 static void
44 telephone_startup (struct linereader *lr, struct localedef_t *locale,
45 int ignore_content)
47 if (!ignore_content)
48 locale->categories[LC_TELEPHONE].telephone = (struct locale_telephone_t *)
49 xcalloc (1, sizeof (struct locale_telephone_t));
51 if (lr != NULL)
53 lr->translate_strings = 1;
54 lr->return_widestr = 0;
59 void
60 telephone_finish (struct localedef_t *locale, const struct charmap_t *charmap)
62 struct locale_telephone_t *telephone =
63 locale->categories[LC_TELEPHONE].telephone;
64 int nothing = 0;
66 /* Now resolve copying and also handle completely missing definitions. */
67 if (telephone == NULL)
69 /* First see whether we were supposed to copy. If yes, find the
70 actual definition. */
71 if (locale->copy_name[LC_TELEPHONE] != NULL)
73 /* Find the copying locale. This has to happen transitively since
74 the locale we are copying from might also copying another one. */
75 struct localedef_t *from = locale;
78 from = find_locale (LC_TELEPHONE, from->copy_name[LC_TELEPHONE],
79 from->repertoire_name, charmap);
80 while (from->categories[LC_TELEPHONE].telephone == NULL
81 && from->copy_name[LC_TELEPHONE] != NULL);
83 telephone = locale->categories[LC_TELEPHONE].telephone
84 = from->categories[LC_TELEPHONE].telephone;
87 /* If there is still no definition issue an warning and create an
88 empty one. */
89 if (telephone == NULL)
91 record_warning (_("\
92 No definition for %s category found"), "LC_TELEPHONE");
93 telephone_startup (NULL, locale, 0);
94 telephone = locale->categories[LC_TELEPHONE].telephone;
95 nothing = 1;
99 if (telephone->tel_int_fmt == NULL)
101 if (! nothing)
102 record_error (0, 0, _("%s: field `%s' not defined"),
103 "LC_TELEPHONE", "tel_int_fmt");
104 /* Use as the default value the value of the i18n locale. */
105 telephone->tel_int_fmt = "+%c %a%t%l";
107 else
109 /* We must check whether the format string contains only the
110 allowed escape sequences. */
111 const char *cp = telephone->tel_int_fmt;
113 if (*cp == '\0')
114 record_error (0, 0, _("%s: field `%s' must not be empty"),
115 "LC_TELEPHONE", "tel_int_fmt");
116 else
117 while (*cp != '\0')
119 if (*cp == '%')
121 if (strchr ("aAcCelt", *++cp) == NULL)
123 record_error (0, 0, _("\
124 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_int_fmt");
125 break;
128 ++cp;
132 if (telephone->tel_dom_fmt == NULL)
133 telephone->tel_dom_fmt = "";
134 else if (telephone->tel_dom_fmt[0] != '\0')
136 /* We must check whether the format string contains only the
137 allowed escape sequences. */
138 const char *cp = telephone->tel_dom_fmt;
140 while (*cp != '\0')
142 if (*cp == '%')
144 if (strchr ("aAcCelt", *++cp) == NULL)
146 record_error (0, 0, _("\
147 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_dom_fmt");
148 break;
151 ++cp;
155 #define TEST_ELEM(cat) \
156 if (telephone->cat == NULL) \
158 if (verbose && ! nothing) \
159 record_warning (_("%s: field `%s' not defined"), "LC_TELEPHONE", \
160 #cat); \
161 telephone->cat = ""; \
164 TEST_ELEM (int_select);
165 TEST_ELEM (int_prefix);
169 void
170 telephone_output (struct localedef_t *locale, const struct charmap_t *charmap,
171 const char *output_path)
173 struct locale_telephone_t *telephone =
174 locale->categories[LC_TELEPHONE].telephone;
175 struct locale_file file;
177 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE));
178 add_locale_string (&file, telephone->tel_int_fmt);
179 add_locale_string (&file, telephone->tel_dom_fmt);
180 add_locale_string (&file, telephone->int_select);
181 add_locale_string (&file, telephone->int_prefix);
182 add_locale_string (&file, charmap->code_set_name);
183 write_locale_data (output_path, LC_TELEPHONE, "LC_TELEPHONE", &file);
187 /* The parser for the LC_TELEPHONE section of the locale definition. */
188 void
189 telephone_read (struct linereader *ldfile, struct localedef_t *result,
190 const struct charmap_t *charmap, const char *repertoire_name,
191 int ignore_content)
193 struct locale_telephone_t *telephone;
194 struct token *now;
195 struct token *arg;
196 enum token_t nowtok;
198 /* The rest of the line containing `LC_TELEPHONE' must be free. */
199 lr_ignore_rest (ldfile, 1);
203 now = lr_token (ldfile, charmap, result, NULL, verbose);
204 nowtok = now->tok;
206 while (nowtok == tok_eol);
208 /* If we see `copy' now we are almost done. */
209 if (nowtok == tok_copy)
211 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_telephone,
212 LC_TELEPHONE, "LC_TELEPHONE", ignore_content);
213 return;
216 /* Prepare the data structures. */
217 telephone_startup (ldfile, result, ignore_content);
218 telephone = result->categories[LC_TELEPHONE].telephone;
220 while (1)
222 /* Of course we don't proceed beyond the end of file. */
223 if (nowtok == tok_eof)
224 break;
226 /* Ingore empty lines. */
227 if (nowtok == tok_eol)
229 now = lr_token (ldfile, charmap, result, NULL, verbose);
230 nowtok = now->tok;
231 continue;
234 switch (nowtok)
236 #define STR_ELEM(cat) \
237 case tok_##cat: \
238 /* Ignore the rest of the line if we don't need the input of \
239 this line. */ \
240 if (ignore_content) \
242 lr_ignore_rest (ldfile, 0); \
243 break; \
246 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
247 if (arg->tok != tok_string) \
248 goto err_label; \
249 if (telephone->cat != NULL) \
250 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
251 "LC_TELEPHONE", #cat); \
252 else if (!ignore_content && arg->val.str.startmb == NULL) \
254 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
255 "LC_TELEPHONE", #cat); \
256 telephone->cat = ""; \
258 else if (!ignore_content) \
259 telephone->cat = arg->val.str.startmb; \
260 break
262 STR_ELEM (tel_int_fmt);
263 STR_ELEM (tel_dom_fmt);
264 STR_ELEM (int_select);
265 STR_ELEM (int_prefix);
267 case tok_end:
268 /* Next we assume `LC_TELEPHONE'. */
269 arg = lr_token (ldfile, charmap, result, NULL, verbose);
270 if (arg->tok == tok_eof)
271 break;
272 if (arg->tok == tok_eol)
273 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TELEPHONE");
274 else if (arg->tok != tok_lc_telephone)
275 lr_error (ldfile, _("\
276 %1$s: definition does not end with `END %1$s'"), "LC_TELEPHONE");
277 lr_ignore_rest (ldfile, arg->tok == tok_lc_telephone);
278 return;
280 default:
281 err_label:
282 SYNTAX_ERROR (_("%s: syntax error"), "LC_TELEPHONE");
285 /* Prepare for the next round. */
286 now = lr_token (ldfile, charmap, result, NULL, verbose);
287 nowtok = now->tok;
290 /* When we come here we reached the end of the file. */
291 lr_error (ldfile, _("%s: premature end of file"), "LC_TELEPHONE");