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