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

source code of glibc/locale/programs/ld-name.c