1/* Copyright (C) 1998-2016 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
4
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.
9
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.
14
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/>. */
17
18#ifdef HAVE_CONFIG_H
19# include <config.h>
20#endif
21
22#include <langinfo.h>
23#include <string.h>
24#include <stdint.h>
25#include <sys/uio.h>
26
27#include <assert.h>
28
29#include "localedef.h"
30#include "localeinfo.h"
31#include "locfile.h"
32
33
34/* The real definition of the struct for the LC_NAME locale. */
35struct locale_name_t
36{
37 const char *name_fmt;
38 const char *name_gen;
39 const char *name_mr;
40 const char *name_mrs;
41 const char *name_miss;
42 const char *name_ms;
43};
44
45
46static void
47name_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
49{
50 if (!ignore_content)
51 locale->categories[LC_NAME].name =
52 (struct locale_name_t *) xcalloc (1, sizeof (struct locale_name_t));
53
54 if (lr != NULL)
55 {
56 lr->translate_strings = 1;
57 lr->return_widestr = 0;
58 }
59}
60
61
62void
63name_finish (struct localedef_t *locale, const struct charmap_t *charmap)
64{
65 struct locale_name_t *name = locale->categories[LC_NAME].name;
66 int nothing = 0;
67
68 /* Now resolve copying and also handle completely missing definitions. */
69 if (name == NULL)
70 {
71 /* First see whether we were supposed to copy. If yes, find the
72 actual definition. */
73 if (locale->copy_name[LC_NAME] != NULL)
74 {
75 /* Find the copying locale. This has to happen transitively since
76 the locale we are copying from might also copying another one. */
77 struct localedef_t *from = locale;
78
79 do
80 from = find_locale (LC_NAME, from->copy_name[LC_NAME],
81 from->repertoire_name, charmap);
82 while (from->categories[LC_NAME].name == NULL
83 && from->copy_name[LC_NAME] != NULL);
84
85 name = locale->categories[LC_NAME].name
86 = from->categories[LC_NAME].name;
87 }
88
89 /* If there is still no definition issue an warning and create an
90 empty one. */
91 if (name == NULL)
92 {
93 if (! be_quiet)
94 WITH_CUR_LOCALE (error (0, 0, _("\
95No definition for %s category found"), "LC_NAME"));
96 name_startup (NULL, locale, 0);
97 name = locale->categories[LC_NAME].name;
98 nothing = 1;
99 }
100 }
101
102 if (name->name_fmt == NULL)
103 {
104 if (! nothing)
105 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"),
106 "LC_NAME", "name_fmt"));
107 /* Use as the default value the value of the i18n locale. */
108 name->name_fmt = "%p%t%g%t%m%t%f";
109 }
110 else
111 {
112 /* We must check whether the format string contains only the
113 allowed escape sequences. */
114 const char *cp = name->name_fmt;
115
116 if (*cp == '\0')
117 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' must not be empty"),
118 "LC_NAME", "name_fmt"));
119 else
120 while (*cp != '\0')
121 {
122 if (*cp == '%')
123 {
124 if (*++cp == 'R')
125 /* Romanize-flag. */
126 ++cp;
127 if (strchr ("dfFgGlomMpsSt", *cp) == NULL)
128 {
129 WITH_CUR_LOCALE (error (0, 0, _("\
130%s: invalid escape sequence in field `%s'"), "LC_NAME", "name_fmt"));
131 break;
132 }
133 }
134 ++cp;
135 }
136 }
137
138#define TEST_ELEM(cat) \
139 if (name->cat == NULL) \
140 { \
141 if (verbose && ! nothing) \
142 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
143 "LC_NAME", #cat)); \
144 name->cat = ""; \
145 }
146
147 TEST_ELEM (name_gen);
148 TEST_ELEM (name_mr);
149 TEST_ELEM (name_mrs);
150 TEST_ELEM (name_miss);
151 TEST_ELEM (name_ms);
152}
153
154
155void
156name_output (struct localedef_t *locale, const struct charmap_t *charmap,
157 const char *output_path)
158{
159 struct locale_name_t *name = locale->categories[LC_NAME].name;
160 struct locale_file file;
161
162 init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_NAME));
163 add_locale_string (&file, name->name_fmt);
164 add_locale_string (&file, name->name_gen);
165 add_locale_string (&file, name->name_mr);
166 add_locale_string (&file, name->name_mrs);
167 add_locale_string (&file, name->name_miss);
168 add_locale_string (&file, name->name_ms);
169 add_locale_string (&file, charmap->code_set_name);
170 write_locale_data (output_path, LC_NAME, "LC_NAME", &file);
171}
172
173
174/* The parser for the LC_NAME section of the locale definition. */
175void
176name_read (struct linereader *ldfile, struct localedef_t *result,
177 const struct charmap_t *charmap, const char *repertoire_name,
178 int ignore_content)
179{
180 struct locale_name_t *name;
181 struct token *now;
182 struct token *arg;
183 enum token_t nowtok;
184
185 /* The rest of the line containing `LC_NAME' must be empty. */
186 lr_ignore_rest (ldfile, 1);
187
188 do
189 {
190 now = lr_token (ldfile, charmap, result, NULL, verbose);
191 nowtok = now->tok;
192 }
193 while (nowtok == tok_eol);
194
195 /* If we see `copy' now we are almost done. */
196 if (nowtok == tok_copy)
197 {
198 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_name,
199 LC_NAME, "LC_NAME", ignore_content);
200 return;
201 }
202
203 /* Prepare the data structures. */
204 name_startup (ldfile, result, ignore_content);
205 name = result->categories[LC_NAME].name;
206
207 while (1)
208 {
209 /* Of course we don't proceed beyond the end of file. */
210 if (nowtok == tok_eof)
211 break;
212
213 /* Ignore empty lines. */
214 if (nowtok == tok_eol)
215 {
216 now = lr_token (ldfile, charmap, result, NULL, verbose);
217 nowtok = now->tok;
218 continue;
219 }
220
221 switch (nowtok)
222 {
223#define STR_ELEM(cat) \
224 case tok_##cat: \
225 /* Ignore the rest of the line if we don't need the input of \
226 this line. */ \
227 if (ignore_content) \
228 { \
229 lr_ignore_rest (ldfile, 0); \
230 break; \
231 } \
232 \
233 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
234 if (arg->tok != tok_string) \
235 goto err_label; \
236 if (name->cat != NULL) \
237 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
238 "LC_NAME", #cat); \
239 else if (!ignore_content && arg->val.str.startmb == NULL) \
240 { \
241 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
242 "LC_NAME", #cat); \
243 name->cat = ""; \
244 } \
245 else if (!ignore_content) \
246 name->cat = arg->val.str.startmb; \
247 break
248
249 STR_ELEM (name_fmt);
250 STR_ELEM (name_gen);
251 STR_ELEM (name_mr);
252 STR_ELEM (name_mrs);
253 STR_ELEM (name_miss);
254 STR_ELEM (name_ms);
255
256 case tok_end:
257 /* Next we assume `LC_NAME'. */
258 arg = lr_token (ldfile, charmap, result, NULL, verbose);
259 if (arg->tok == tok_eof)
260 break;
261 if (arg->tok == tok_eol)
262 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NAME");
263 else if (arg->tok != tok_lc_name)
264 lr_error (ldfile, _("\
265%1$s: definition does not end with `END %1$s'"), "LC_NAME");
266 lr_ignore_rest (ldfile, arg->tok == tok_lc_name);
267 return;
268
269 default:
270 err_label:
271 SYNTAX_ERROR (_("%s: syntax error"), "LC_NAME");
272 }
273
274 /* Prepare for the next round. */
275 now = lr_token (ldfile, charmap, result, NULL, verbose);
276 nowtok = now->tok;
277 }
278
279 /* When we come here we reached the end of the file. */
280 lr_error (ldfile, _("%s: premature end of file"), "LC_NAME");
281}
282