1 /* Copyright (C) 1998-2022 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
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.
8
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.
13
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/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
25
26 #include <assert.h>
27
28 #include "localedef.h"
29 #include "localeinfo.h"
30 #include "locfile.h"
31
32
33 /* The real definition of the struct for the LC_TELEPHONE locale. */
34 struct locale_telephone_t
35 {
36 const char *tel_int_fmt;
37 const char *tel_dom_fmt;
38 const char *int_select;
39 const char *int_prefix;
40 };
41
42
43 static void
telephone_startup(struct linereader * lr,struct localedef_t * locale,int ignore_content)44 telephone_startup (struct linereader *lr, struct localedef_t *locale,
45 int ignore_content)
46 {
47 if (!ignore_content)
48 locale->categories[LC_TELEPHONE].telephone = (struct locale_telephone_t *)
49 xcalloc (1, sizeof (struct locale_telephone_t));
50
51 if (lr != NULL)
52 {
53 lr->translate_strings = 1;
54 lr->return_widestr = 0;
55 }
56 }
57
58
59 void
telephone_finish(struct localedef_t * locale,const struct charmap_t * charmap)60 telephone_finish (struct localedef_t *locale, const struct charmap_t *charmap)
61 {
62 struct locale_telephone_t *telephone =
63 locale->categories[LC_TELEPHONE].telephone;
64 int nothing = 0;
65
66 /* Now resolve copying and also handle completely missing definitions. */
67 if (telephone == NULL)
68 {
69 /* First see whether we were supposed to copy. If yes, find the
70 actual definition. */
71 if (locale->copy_name[LC_TELEPHONE] != NULL)
72 {
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;
76
77 do
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);
82
83 telephone = locale->categories[LC_TELEPHONE].telephone
84 = from->categories[LC_TELEPHONE].telephone;
85 }
86
87 /* If there is still no definition issue an warning and create an
88 empty one. */
89 if (telephone == NULL)
90 {
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;
96 }
97 }
98
99 if (telephone->tel_int_fmt == NULL)
100 {
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";
106 }
107 else
108 {
109 /* We must check whether the format string contains only the
110 allowed escape sequences. */
111 const char *cp = telephone->tel_int_fmt;
112
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')
118 {
119 if (*cp == '%')
120 {
121 if (strchr ("aAcCelt", *++cp) == NULL)
122 {
123 record_error (0, 0, _("\
124 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_int_fmt");
125 break;
126 }
127 }
128 ++cp;
129 }
130 }
131
132 if (telephone->tel_dom_fmt == NULL)
133 telephone->tel_dom_fmt = "";
134 else if (telephone->tel_dom_fmt[0] != '\0')
135 {
136 /* We must check whether the format string contains only the
137 allowed escape sequences. */
138 const char *cp = telephone->tel_dom_fmt;
139
140 while (*cp != '\0')
141 {
142 if (*cp == '%')
143 {
144 if (strchr ("aAcCelt", *++cp) == NULL)
145 {
146 record_error (0, 0, _("\
147 %s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_dom_fmt");
148 break;
149 }
150 }
151 ++cp;
152 }
153 }
154
155 #define TEST_ELEM(cat) \
156 if (telephone->cat == NULL) \
157 { \
158 if (verbose && ! nothing) \
159 record_warning (_("%s: field `%s' not defined"), "LC_TELEPHONE", \
160 #cat); \
161 telephone->cat = ""; \
162 }
163
164 TEST_ELEM (int_select);
165 TEST_ELEM (int_prefix);
166 }
167
168
169 void
telephone_output(struct localedef_t * locale,const struct charmap_t * charmap,const char * output_path)170 telephone_output (struct localedef_t *locale, const struct charmap_t *charmap,
171 const char *output_path)
172 {
173 struct locale_telephone_t *telephone =
174 locale->categories[LC_TELEPHONE].telephone;
175 struct locale_file file;
176
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);
184 }
185
186
187 /* The parser for the LC_TELEPHONE section of the locale definition. */
188 void
telephone_read(struct linereader * ldfile,struct localedef_t * result,const struct charmap_t * charmap,const char * repertoire_name,int ignore_content)189 telephone_read (struct linereader *ldfile, struct localedef_t *result,
190 const struct charmap_t *charmap, const char *repertoire_name,
191 int ignore_content)
192 {
193 struct locale_telephone_t *telephone;
194 struct token *now;
195 struct token *arg;
196 enum token_t nowtok;
197
198 /* The rest of the line containing `LC_TELEPHONE' must be free. */
199 lr_ignore_rest (ldfile, 1);
200
201 do
202 {
203 now = lr_token (ldfile, charmap, result, NULL, verbose);
204 nowtok = now->tok;
205 }
206 while (nowtok == tok_eol);
207
208 /* If we see `copy' now we are almost done. */
209 if (nowtok == tok_copy)
210 {
211 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_telephone,
212 LC_TELEPHONE, "LC_TELEPHONE", ignore_content);
213 return;
214 }
215
216 /* Prepare the data structures. */
217 telephone_startup (ldfile, result, ignore_content);
218 telephone = result->categories[LC_TELEPHONE].telephone;
219
220 while (1)
221 {
222 /* Of course we don't proceed beyond the end of file. */
223 if (nowtok == tok_eof)
224 break;
225
226 /* Ingore empty lines. */
227 if (nowtok == tok_eol)
228 {
229 now = lr_token (ldfile, charmap, result, NULL, verbose);
230 nowtok = now->tok;
231 continue;
232 }
233
234 switch (nowtok)
235 {
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) \
241 { \
242 lr_ignore_rest (ldfile, 0); \
243 break; \
244 } \
245 \
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) \
253 { \
254 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
255 "LC_TELEPHONE", #cat); \
256 telephone->cat = ""; \
257 } \
258 else if (!ignore_content) \
259 telephone->cat = arg->val.str.startmb; \
260 break
261
262 STR_ELEM (tel_int_fmt);
263 STR_ELEM (tel_dom_fmt);
264 STR_ELEM (int_select);
265 STR_ELEM (int_prefix);
266
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;
279
280 default:
281 err_label:
282 SYNTAX_ERROR (_("%s: syntax error"), "LC_TELEPHONE");
283 }
284
285 /* Prepare for the next round. */
286 now = lr_token (ldfile, charmap, result, NULL, verbose);
287 nowtok = now->tok;
288 }
289
290 /* When we come here we reached the end of the file. */
291 lr_error (ldfile, _("%s: premature end of file"), "LC_TELEPHONE");
292 }
293