Unroll x86-64 strlen
[glibc.git] / locale / programs / ld-name.c
blob9f328828cf24fcaf70c20d3a016f09ba9eb44f79
1 /* Copyright (C) 1998,1999,2000,2001,2002,2005 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@cygnus.com>, 1998.
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.
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19 #ifdef HAVE_CONFIG_H
20 # include <config.h>
21 #endif
23 #include <langinfo.h>
24 #include <string.h>
25 #include <sys/uio.h>
27 #include <assert.h>
29 #include "localedef.h"
30 #include "localeinfo.h"
31 #include "locfile.h"
34 /* The real definition of the struct for the LC_NAME locale. */
35 struct locale_name_t
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;
46 static void
47 name_startup (struct linereader *lr, struct localedef_t *locale,
48 int ignore_content)
50 if (!ignore_content)
51 locale->categories[LC_NAME].name =
52 (struct locale_name_t *) xcalloc (1, sizeof (struct locale_name_t));
54 if (lr != NULL)
56 lr->translate_strings = 1;
57 lr->return_widestr = 0;
62 void
63 name_finish (struct localedef_t *locale, const struct charmap_t *charmap)
65 struct locale_name_t *name = locale->categories[LC_NAME].name;
66 int nothing = 0;
68 /* Now resolve copying and also handle completely missing definitions. */
69 if (name == NULL)
71 /* First see whether we were supposed to copy. If yes, find the
72 actual definition. */
73 if (locale->copy_name[LC_NAME] != NULL)
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;
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);
85 name = locale->categories[LC_NAME].name
86 = from->categories[LC_NAME].name;
89 /* If there is still no definition issue an warning and create an
90 empty one. */
91 if (name == NULL)
93 if (! be_quiet)
94 WITH_CUR_LOCALE (error (0, 0, _("\
95 No definition for %s category found"), "LC_NAME"));
96 name_startup (NULL, locale, 0);
97 name = locale->categories[LC_NAME].name;
98 nothing = 1;
102 if (name->name_fmt == NULL)
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";
110 else
112 /* We must check whether the format string contains only the
113 allowed escape sequences. */
114 const char *cp = name->name_fmt;
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')
122 if (*cp == '%')
124 if (*++cp == 'R')
125 /* Romanize-flag. */
126 ++cp;
127 if (strchr ("dfFgGlomMpsSt", *cp) == NULL)
129 WITH_CUR_LOCALE (error (0, 0, _("\
130 %s: invalid escape sequence in field `%s'"), "LC_NAME", "name_fmt"));
131 break;
134 ++cp;
138 #define TEST_ELEM(cat) \
139 if (name->cat == NULL) \
141 if (verbose && ! nothing) \
142 WITH_CUR_LOCALE (error (0, 0, _("%s: field `%s' not defined"), \
143 "LC_NAME", #cat)); \
144 name->cat = ""; \
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);
155 void
156 name_output (struct localedef_t *locale, const struct charmap_t *charmap,
157 const char *output_path)
159 struct locale_name_t *name = locale->categories[LC_NAME].name;
160 struct iovec iov[2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
161 struct locale_file data;
162 uint32_t idx[_NL_ITEM_INDEX (_NL_NUM_LC_NAME)];
163 size_t cnt = 0;
165 data.magic = LIMAGIC (LC_NAME);
166 data.n = _NL_ITEM_INDEX (_NL_NUM_LC_NAME);
167 iov[cnt].iov_base = (void *) &data;
168 iov[cnt].iov_len = sizeof (data);
169 ++cnt;
171 iov[cnt].iov_base = (void *) idx;
172 iov[cnt].iov_len = sizeof (idx);
173 ++cnt;
175 idx[cnt - 2] = iov[0].iov_len + iov[1].iov_len;
176 iov[cnt].iov_base = (void *) name->name_fmt;
177 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
178 ++cnt;
180 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
181 iov[cnt].iov_base = (void *) name->name_gen;
182 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
183 ++cnt;
185 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
186 iov[cnt].iov_base = (void *) name->name_mr;
187 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
188 ++cnt;
190 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
191 iov[cnt].iov_base = (void *) name->name_mrs;
192 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
193 ++cnt;
195 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
196 iov[cnt].iov_base = (void *) name->name_miss;
197 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
198 ++cnt;
200 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
201 iov[cnt].iov_base = (void *) name->name_ms;
202 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
203 ++cnt;
205 idx[cnt - 2] = idx[cnt - 3] + iov[cnt - 1].iov_len;
206 iov[cnt].iov_base = (void *) charmap->code_set_name;
207 iov[cnt].iov_len = strlen (iov[cnt].iov_base) + 1;
208 ++cnt;
210 assert (cnt == 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME));
212 write_locale_data (output_path, LC_NAME, "LC_NAME",
213 2 + _NL_ITEM_INDEX (_NL_NUM_LC_NAME), iov);
217 /* The parser for the LC_NAME section of the locale definition. */
218 void
219 name_read (struct linereader *ldfile, struct localedef_t *result,
220 const struct charmap_t *charmap, const char *repertoire_name,
221 int ignore_content)
223 struct locale_name_t *name;
224 struct token *now;
225 struct token *arg;
226 enum token_t nowtok;
228 /* The rest of the line containing `LC_NAME' must be empty. */
229 lr_ignore_rest (ldfile, 1);
233 now = lr_token (ldfile, charmap, result, NULL, verbose);
234 nowtok = now->tok;
236 while (nowtok == tok_eol);
238 /* If we see `copy' now we are almost done. */
239 if (nowtok == tok_copy)
241 handle_copy (ldfile, charmap, repertoire_name, result, tok_lc_name,
242 LC_NAME, "LC_NAME", ignore_content);
243 return;
246 /* Prepare the data structures. */
247 name_startup (ldfile, result, ignore_content);
248 name = result->categories[LC_NAME].name;
250 while (1)
252 /* Of course we don't proceed beyond the end of file. */
253 if (nowtok == tok_eof)
254 break;
256 /* Ignore empty lines. */
257 if (nowtok == tok_eol)
259 now = lr_token (ldfile, charmap, result, NULL, verbose);
260 nowtok = now->tok;
261 continue;
264 switch (nowtok)
266 #define STR_ELEM(cat) \
267 case tok_##cat: \
268 /* Ignore the rest of the line if we don't need the input of \
269 this line. */ \
270 if (ignore_content) \
272 lr_ignore_rest (ldfile, 0); \
273 break; \
276 arg = lr_token (ldfile, charmap, result, NULL, verbose); \
277 if (arg->tok != tok_string) \
278 goto err_label; \
279 if (name->cat != NULL) \
280 lr_error (ldfile, _("%s: field `%s' declared more than once"), \
281 "LC_NAME", #cat); \
282 else if (!ignore_content && arg->val.str.startmb == NULL) \
284 lr_error (ldfile, _("%s: unknown character in field `%s'"), \
285 "LC_NAME", #cat); \
286 name->cat = ""; \
288 else if (!ignore_content) \
289 name->cat = arg->val.str.startmb; \
290 break
292 STR_ELEM (name_fmt);
293 STR_ELEM (name_gen);
294 STR_ELEM (name_mr);
295 STR_ELEM (name_mrs);
296 STR_ELEM (name_miss);
297 STR_ELEM (name_ms);
299 case tok_end:
300 /* Next we assume `LC_NAME'. */
301 arg = lr_token (ldfile, charmap, result, NULL, verbose);
302 if (arg->tok == tok_eof)
303 break;
304 if (arg->tok == tok_eol)
305 lr_error (ldfile, _("%s: incomplete `END' line"), "LC_NAME");
306 else if (arg->tok != tok_lc_name)
307 lr_error (ldfile, _("\
308 %1$s: definition does not end with `END %1$s'"), "LC_NAME");
309 lr_ignore_rest (ldfile, arg->tok == tok_lc_name);
310 return;
312 default:
313 err_label:
314 SYNTAX_ERROR (_("%s: syntax error"), "LC_NAME");
317 /* Prepare for the next round. */
318 now = lr_token (ldfile, charmap, result, NULL, verbose);
319 nowtok = now->tok;
322 /* When we come here we reached the end of the file. */
323 lr_error (ldfile, _("%s: premature end of file"), "LC_NAME");