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/>. */
28 #include "localedef.h"
29 #include "localeinfo.h"
33 /* The real definition of the struct for the LC_NAME locale. */
40 const char *name_miss
;
46 name_startup (struct linereader
*lr
, struct localedef_t
*locale
,
50 locale
->categories
[LC_NAME
].name
=
51 (struct locale_name_t
*) xcalloc (1, sizeof (struct locale_name_t
));
55 lr
->translate_strings
= 1;
56 lr
->return_widestr
= 0;
62 name_finish (struct localedef_t
*locale
, const struct charmap_t
*charmap
)
64 struct locale_name_t
*name
= locale
->categories
[LC_NAME
].name
;
67 /* Now resolve copying and also handle completely missing definitions. */
70 /* First see whether we were supposed to copy. If yes, find the
72 if (locale
->copy_name
[LC_NAME
] != NULL
)
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
;
79 from
= find_locale (LC_NAME
, from
->copy_name
[LC_NAME
],
80 from
->repertoire_name
, charmap
);
81 while (from
->categories
[LC_NAME
].name
== NULL
82 && from
->copy_name
[LC_NAME
] != NULL
);
84 name
= locale
->categories
[LC_NAME
].name
85 = from
->categories
[LC_NAME
].name
;
88 /* If there is still no definition issue an warning and create an
93 No definition for %s category found"), "LC_NAME");
94 name_startup (NULL
, locale
, 0);
95 name
= locale
->categories
[LC_NAME
].name
;
100 if (name
->name_fmt
== NULL
)
103 record_error (0, 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";
110 /* We must check whether the format string contains only the
111 allowed escape sequences. */
112 const char *cp
= name
->name_fmt
;
115 record_error (0, 0, _("%s: field `%s' must not be empty"),
116 "LC_NAME", "name_fmt");
125 if (strchr ("dfFgGlomMpsSt", *cp
) == NULL
)
127 record_error (0, 0, _("\
128 %s: invalid escape sequence in field `%s'"), "LC_NAME", "name_fmt");
136 #define TEST_ELEM(cat) \
137 if (name->cat == NULL) \
139 if (verbose && ! nothing) \
140 record_warning (_("%s: field `%s' not defined"), "LC_NAME", #cat); \
144 TEST_ELEM (name_gen
);
146 TEST_ELEM (name_mrs
);
147 TEST_ELEM (name_miss
);
153 name_output (struct localedef_t
*locale
, const struct charmap_t
*charmap
,
154 const char *output_path
)
156 struct locale_name_t
*name
= locale
->categories
[LC_NAME
].name
;
157 struct locale_file file
;
159 init_locale_data (&file
, _NL_ITEM_INDEX (_NL_NUM_LC_NAME
));
160 add_locale_string (&file
, name
->name_fmt
);
161 add_locale_string (&file
, name
->name_gen
);
162 add_locale_string (&file
, name
->name_mr
);
163 add_locale_string (&file
, name
->name_mrs
);
164 add_locale_string (&file
, name
->name_miss
);
165 add_locale_string (&file
, name
->name_ms
);
166 add_locale_string (&file
, charmap
->code_set_name
);
167 write_locale_data (output_path
, LC_NAME
, "LC_NAME", &file
);
171 /* The parser for the LC_NAME section of the locale definition. */
173 name_read (struct linereader
*ldfile
, struct localedef_t
*result
,
174 const struct charmap_t
*charmap
, const char *repertoire_name
,
177 struct locale_name_t
*name
;
182 /* The rest of the line containing `LC_NAME' must be empty. */
183 lr_ignore_rest (ldfile
, 1);
187 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
190 while (nowtok
== tok_eol
);
192 /* If we see `copy' now we are almost done. */
193 if (nowtok
== tok_copy
)
195 handle_copy (ldfile
, charmap
, repertoire_name
, result
, tok_lc_name
,
196 LC_NAME
, "LC_NAME", ignore_content
);
200 /* Prepare the data structures. */
201 name_startup (ldfile
, result
, ignore_content
);
202 name
= result
->categories
[LC_NAME
].name
;
206 /* Of course we don't proceed beyond the end of file. */
207 if (nowtok
== tok_eof
)
210 /* Ignore empty lines. */
211 if (nowtok
== tok_eol
)
213 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
220 #define STR_ELEM(cat) \
222 /* Ignore the rest of the line if we don't need the input of \
224 if (ignore_content) \
226 lr_ignore_rest (ldfile, 0); \
230 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
231 if (arg->tok != tok_string) \
233 if (name->cat != NULL) \
234 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
236 else if (!ignore_content && arg->val.str.startmb == NULL) \
238 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
242 else if (!ignore_content) \
243 name->cat = arg->val.str.startmb; \
250 STR_ELEM (name_miss
);
254 /* Next we assume `LC_NAME'. */
255 arg
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
256 if (arg
->tok
== tok_eof
)
258 if (arg
->tok
== tok_eol
)
259 lr_error (ldfile
, _("%s: incomplete `END' line"), "LC_NAME");
260 else if (arg
->tok
!= tok_lc_name
)
261 lr_error (ldfile
, _("\
262 %1$s: definition does not end with `END %1$s'"), "LC_NAME");
263 lr_ignore_rest (ldfile
, arg
->tok
== tok_lc_name
);
268 SYNTAX_ERROR (_("%s: syntax error"), "LC_NAME");
271 /* Prepare for the next round. */
272 now
= lr_token (ldfile
, charmap
, result
, NULL
, verbose
);
276 /* When we come here we reached the end of the file. */
277 lr_error (ldfile
, _("%s: premature end of file"), "LC_NAME");