]> git.ipfire.org Git - thirdparty/glibc.git/blame - locale/programs/ld-telephone.c
Prefer https to http for gnu.org and fsf.org URLs
[thirdparty/glibc.git] / locale / programs / ld-telephone.c
CommitLineData
04277e02 1/* Copyright (C) 1998-2019 Free Software Foundation, Inc.
4b10dd6c
UD
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
43bc8ac6 5 This program is free software; you can redistribute it and/or modify
2e2efe65
RM
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.
4b10dd6c 9
43bc8ac6 10 This program is distributed in the hope that it will be useful,
4b10dd6c 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
43bc8ac6
UD
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
4b10dd6c 14
43bc8ac6 15 You should have received a copy of the GNU General Public License
5a82c748 16 along with this program; if not, see <https://www.gnu.org/licenses/>. */
4b10dd6c
UD
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
4b10dd6c
UD
22#include <langinfo.h>
23#include <string.h>
e054f494 24#include <stdint.h>
4b10dd6c
UD
25#include <sys/uio.h>
26
27#include <assert.h>
28
f2b98f97 29#include "localedef.h"
4b10dd6c
UD
30#include "localeinfo.h"
31#include "locfile.h"
32
33
34/* The real definition of the struct for the LC_TELEPHONE locale. */
35struct locale_telephone_t
36{
37 const char *tel_int_fmt;
38 const char *tel_dom_fmt;
39 const char *int_select;
40 const char *int_prefix;
41};
42
43
44static void
45telephone_startup (struct linereader *lr, struct localedef_t *locale,
46 int ignore_content)
47{
48 if (!ignore_content)
49 locale->categories[LC_TELEPHONE].telephone = (struct locale_telephone_t *)
50 xcalloc (1, sizeof (struct locale_telephone_t));
51
b9eb05d6
UD
52 if (lr != NULL)
53 {
54 lr->translate_strings = 1;
55 lr->return_widestr = 0;
56 }
4b10dd6c
UD
57}
58
59
60void
47e8b443 61telephone_finish (struct localedef_t *locale, const struct charmap_t *charmap)
4b10dd6c
UD
62{
63 struct locale_telephone_t *telephone =
64 locale->categories[LC_TELEPHONE].telephone;
b9eb05d6
UD
65 int nothing = 0;
66
67 /* Now resolve copying and also handle completely missing definitions. */
68 if (telephone == NULL)
69 {
70 /* First see whether we were supposed to copy. If yes, find the
71 actual definition. */
72 if (locale->copy_name[LC_TELEPHONE] != NULL)
73 {
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;
77
78 do
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);
83
84 telephone = locale->categories[LC_TELEPHONE].telephone
85 = from->categories[LC_TELEPHONE].telephone;
86 }
87
88 /* If there is still no definition issue an warning and create an
89 empty one. */
90 if (telephone == NULL)
91 {
f16491eb
CD
92 record_warning (_("\
93No definition for %s category found"), "LC_TELEPHONE");
b9eb05d6
UD
94 telephone_startup (NULL, locale, 0);
95 telephone = locale->categories[LC_TELEPHONE].telephone;
96 nothing = 1;
97 }
98 }
4b10dd6c
UD
99
100 if (telephone->tel_int_fmt == NULL)
101 {
b9eb05d6 102 if (! nothing)
f16491eb
CD
103 record_error (0, 0, _("%s: field `%s' not defined"),
104 "LC_TELEPHONE", "tel_int_fmt");
4b10dd6c 105 /* Use as the default value the value of the i18n locale. */
7c84d5ce 106 telephone->tel_int_fmt = "+%c %a%t%l";
4b10dd6c
UD
107 }
108 else
109 {
110 /* We must check whether the format string contains only the
111 allowed escape sequences. */
112 const char *cp = telephone->tel_int_fmt;
113
114 if (*cp == '\0')
f16491eb
CD
115 record_error (0, 0, _("%s: field `%s' must not be empty"),
116 "LC_TELEPHONE", "tel_int_fmt");
4b10dd6c
UD
117 else
118 while (*cp != '\0')
119 {
120 if (*cp == '%')
121 {
7c84d5ce 122 if (strchr ("aAcCelt", *++cp) == NULL)
4b10dd6c 123 {
f16491eb
CD
124 record_error (0, 0, _("\
125%s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_int_fmt");
4b10dd6c
UD
126 break;
127 }
128 }
129 ++cp;
130 }
131 }
132
133 if (telephone->tel_dom_fmt == NULL)
134 telephone->tel_dom_fmt = "";
135 else if (telephone->tel_dom_fmt[0] != '\0')
136 {
137 /* We must check whether the format string contains only the
138 allowed escape sequences. */
139 const char *cp = telephone->tel_dom_fmt;
140
141 while (*cp != '\0')
142 {
143 if (*cp == '%')
144 {
7c84d5ce 145 if (strchr ("aAcCelt", *++cp) == NULL)
4b10dd6c 146 {
f16491eb
CD
147 record_error (0, 0, _("\
148%s: invalid escape sequence in field `%s'"), "LC_TELEPHONE", "tel_dom_fmt");
4b10dd6c
UD
149 break;
150 }
151 }
152 ++cp;
153 }
154 }
155
156#define TEST_ELEM(cat) \
157 if (telephone->cat == NULL) \
158 { \
b9eb05d6 159 if (verbose && ! nothing) \
f16491eb
CD
160 record_warning (_("%s: field `%s' not defined"), "LC_TELEPHONE", \
161 #cat); \
4b10dd6c
UD
162 telephone->cat = ""; \
163 }
164
165 TEST_ELEM (int_select);
166 TEST_ELEM (int_prefix);
167}
168
169
170void
47e8b443 171telephone_output (struct localedef_t *locale, const struct charmap_t *charmap,
4b10dd6c
UD
172 const char *output_path)
173{
174 struct locale_telephone_t *telephone =
175 locale->categories[LC_TELEPHONE].telephone;
1ecbb381
RS
176 struct locale_file file;
177
178 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_TELEPHONE));
179 add_locale_string (&file, telephone->tel_int_fmt);
180 add_locale_string (&file, telephone->tel_dom_fmt);
181 add_locale_string (&file, telephone->int_select);
182 add_locale_string (&file, telephone->int_prefix);
183 add_locale_string (&file, charmap->code_set_name);
184 write_locale_data (output_path, LC_TELEPHONE, "LC_TELEPHONE", &file);
4b10dd6c
UD
185}
186
187
188/* The parser for the LC_TELEPHONE section of the locale definition. */
189void
190telephone_read (struct linereader *ldfile, struct localedef_t *result,
47e8b443 191 const struct charmap_t *charmap, const char *repertoire_name,
4b10dd6c
UD
192 int ignore_content)
193{
4b10dd6c
UD
194 struct locale_telephone_t *telephone;
195 struct token *now;
196 struct token *arg;
197 enum token_t nowtok;
198
4b10dd6c
UD
199 /* The rest of the line containing `LC_TELEPHONE' must be free. */
200 lr_ignore_rest (ldfile, 1);
201
202 do
203 {
47e8b443 204 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
205 nowtok = now->tok;
206 }
207 while (nowtok == tok_eol);
208
209 /* If we see `copy' now we are almost done. */
210 if (nowtok == tok_copy)
211 {
01ff9d0b 212 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_telephone,
b9eb05d6 213 LC_TELEPHONE, "LC_TELEPHONE", ignore_content);
4b10dd6c
UD
214 return;
215 }
216
217 /* Prepare the data structures. */
218 telephone_startup (ldfile, result, ignore_content);
219 telephone = result->categories[LC_TELEPHONE].telephone;
220
221 while (1)
222 {
223 /* Of course we don't proceed beyond the end of file. */
224 if (nowtok == tok_eof)
225 break;
226
227 /* Ingore empty lines. */
228 if (nowtok == tok_eol)
229 {
47e8b443 230 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
231 nowtok = now->tok;
232 continue;
233 }
234
235 switch (nowtok)
236 {
237#define STR_ELEM(cat) \
238 case tok_##cat: \
b9eb05d6
UD
239 /* Ignore the rest of the line if we don't need the input of \
240 this line. */ \
241 if (ignore_content) \
242 { \
243 lr_ignore_rest (ldfile, 0); \
244 break; \
245 } \
246 \
47e8b443 247 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
4b10dd6c
UD
248 if (arg->tok != tok_string) \
249 goto err_label; \
250 if (telephone->cat != NULL) \
251 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
252 "LC_TELEPHONE", #cat); \
253 else if (!ignore_content && arg->val.str.startmb == NULL) \
254 { \
255 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
256 "LC_TELEPHONE", #cat); \
257 telephone->cat = ""; \
258 } \
259 else if (!ignore_content) \
260 telephone->cat = arg->val.str.startmb; \
261 break
262
263 STR_ELEM (tel_int_fmt);
264 STR_ELEM (tel_dom_fmt);
265 STR_ELEM (int_select);
266 STR_ELEM (int_prefix);
267
268 case tok_end:
269 /* Next we assume `LC_TELEPHONE'. */
47e8b443 270 arg = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
271 if (arg->tok == tok_eof)
272 break;
273 if (arg->tok == tok_eol)
274 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_TELEPHONE");
275 else if (arg->tok != tok_lc_telephone)
276 lr_error (ldfile, _("\
277%1$s: definition does not end with `END %1$s'"), "LC_TELEPHONE");
278 lr_ignore_rest (ldfile, arg->tok == tok_lc_telephone);
279 return;
280
281 default:
282 err_label:
283 SYNTAX_ERROR (_("%s: syntax error"), "LC_TELEPHONE");
284 }
285
286 /* Prepare for the next round. */
47e8b443 287 now = lr_token (ldfile, charmap, result, NULL, verbose);
4b10dd6c
UD
288 nowtok = now->tok;
289 }
290
291 /* When we come here we reached the end of the file. */
292 lr_error (ldfile, _("%s: premature end of file"), "LC_TELEPHONE");
293}