| 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 <stdlib.h> |
| 24 | #include <string.h> |
| 25 | #include <stdint.h> |
| 26 | #include <sys/uio.h> |
| 27 | |
| 28 | #include <assert.h> |
| 29 | |
| 30 | #include "localedef.h" |
| 31 | #include "localeinfo.h" |
| 32 | #include "locfile.h" |
| 33 | |
| 34 | |
| 35 | /* The real definition of the struct for the LC_IDENTIFICATION locale. */ |
| 36 | struct locale_identification_t |
| 37 | { |
| 38 | const char *title; |
| 39 | const char *source; |
| 40 | const char *address; |
| 41 | const char *contact; |
| 42 | const char *email; |
| 43 | const char *tel; |
| 44 | const char *fax; |
| 45 | const char *language; |
| 46 | const char *territory; |
| 47 | const char *audience; |
| 48 | const char *application; |
| 49 | const char *abbreviation; |
| 50 | const char *revision; |
| 51 | const char *date; |
| 52 | const char *category[__LC_LAST]; |
| 53 | }; |
| 54 | |
| 55 | |
| 56 | static const char *category_name[__LC_LAST] = |
| 57 | { |
| 58 | [LC_CTYPE] = "LC_CTYPE" , |
| 59 | [LC_NUMERIC] = "LC_NUMERIC" , |
| 60 | [LC_TIME] = "LC_TIME" , |
| 61 | [LC_COLLATE] = "LC_COLLATE" , |
| 62 | [LC_MONETARY] = "LC_MONETARY" , |
| 63 | [LC_MESSAGES] = "LC_MESSAGES" , |
| 64 | [LC_ALL] = "LC_ALL" , |
| 65 | [LC_PAPER] = "LC_PAPER" , |
| 66 | [LC_NAME] = "LC_NAME" , |
| 67 | [LC_ADDRESS] = "LC_ADDRESS" , |
| 68 | [LC_TELEPHONE] = "LC_TELEPHONE" , |
| 69 | [LC_MEASUREMENT] = "LC_MEASUREMENT" , |
| 70 | [LC_IDENTIFICATION] = "LC_IDENTIFICATION" |
| 71 | }; |
| 72 | |
| 73 | |
| 74 | static void |
| 75 | identification_startup (struct linereader *lr, struct localedef_t *locale, |
| 76 | int ignore_content) |
| 77 | { |
| 78 | if (!ignore_content) |
| 79 | { |
| 80 | locale->categories[LC_IDENTIFICATION].identification = |
| 81 | (struct locale_identification_t *) |
| 82 | xcalloc (1, sizeof (struct locale_identification_t)); |
| 83 | |
| 84 | locale->categories[LC_IDENTIFICATION].identification->category[LC_ALL] = |
| 85 | "" ; |
| 86 | } |
| 87 | |
| 88 | if (lr != NULL) |
| 89 | { |
| 90 | lr->translate_strings = 1; |
| 91 | lr->return_widestr = 0; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | |
| 96 | void |
| 97 | identification_finish (struct localedef_t *locale, |
| 98 | const struct charmap_t *charmap) |
| 99 | { |
| 100 | struct locale_identification_t *identification |
| 101 | = locale->categories[LC_IDENTIFICATION].identification; |
| 102 | int nothing = 0; |
| 103 | size_t num; |
| 104 | |
| 105 | /* Now resolve copying and also handle completely missing definitions. */ |
| 106 | if (identification == NULL) |
| 107 | { |
| 108 | /* First see whether we were supposed to copy. If yes, find the |
| 109 | actual definition. */ |
| 110 | if (locale->copy_name[LC_IDENTIFICATION] != NULL) |
| 111 | { |
| 112 | /* Find the copying locale. This has to happen transitively since |
| 113 | the locale we are copying from might also copying another one. */ |
| 114 | struct localedef_t *from = locale; |
| 115 | |
| 116 | do |
| 117 | from = find_locale (LC_IDENTIFICATION, |
| 118 | from->copy_name[LC_IDENTIFICATION], |
| 119 | from->repertoire_name, charmap); |
| 120 | while (from->categories[LC_IDENTIFICATION].identification == NULL |
| 121 | && from->copy_name[LC_IDENTIFICATION] != NULL); |
| 122 | |
| 123 | identification = locale->categories[LC_IDENTIFICATION].identification |
| 124 | = from->categories[LC_IDENTIFICATION].identification; |
| 125 | } |
| 126 | |
| 127 | /* If there is still no definition issue an warning and create an |
| 128 | empty one. */ |
| 129 | if (identification == NULL) |
| 130 | { |
| 131 | record_warning (_("\ |
| 132 | No definition for %s category found" ), "LC_IDENTIFICATION" ); |
| 133 | identification_startup (NULL, locale, 0); |
| 134 | identification |
| 135 | = locale->categories[LC_IDENTIFICATION].identification; |
| 136 | nothing = 1; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | #define TEST_ELEM(cat) \ |
| 141 | if (identification->cat == NULL) \ |
| 142 | { \ |
| 143 | if (verbose && ! nothing) \ |
| 144 | record_warning (_("%s: field `%s' not defined"), "LC_IDENTIFICATION", \ |
| 145 | #cat); \ |
| 146 | identification->cat = ""; \ |
| 147 | } |
| 148 | |
| 149 | TEST_ELEM (title); |
| 150 | TEST_ELEM (source); |
| 151 | TEST_ELEM (address); |
| 152 | TEST_ELEM (contact); |
| 153 | TEST_ELEM (email); |
| 154 | TEST_ELEM (tel); |
| 155 | TEST_ELEM (fax); |
| 156 | TEST_ELEM (language); |
| 157 | TEST_ELEM (territory); |
| 158 | TEST_ELEM (audience); |
| 159 | TEST_ELEM (application); |
| 160 | TEST_ELEM (abbreviation); |
| 161 | TEST_ELEM (revision); |
| 162 | TEST_ELEM (date); |
| 163 | |
| 164 | for (num = 0; num < __LC_LAST; ++num) |
| 165 | { |
| 166 | /* We don't accept/parse this category, so skip it early. */ |
| 167 | if (num == LC_ALL) |
| 168 | continue; |
| 169 | |
| 170 | if (identification->category[num] == NULL) |
| 171 | { |
| 172 | if (verbose && ! nothing) |
| 173 | record_warning (_("\ |
| 174 | %s: no identification for category `%s'" ), "LC_IDENTIFICATION" , |
| 175 | category_name[num]); |
| 176 | identification->category[num] = "" ; |
| 177 | } |
| 178 | else |
| 179 | { |
| 180 | /* Only list the standards we care about. This is based on the |
| 181 | ISO 30112 WD10 [2014] standard which supersedes all previous |
| 182 | revisions of the ISO 14652 standard. */ |
| 183 | static const char * const standards[] = |
| 184 | { |
| 185 | "posix:1993" , |
| 186 | "i18n:2004" , |
| 187 | "i18n:2012" , |
| 188 | }; |
| 189 | size_t i; |
| 190 | bool matched = false; |
| 191 | |
| 192 | for (i = 0; i < sizeof (standards) / sizeof (standards[0]); ++i) |
| 193 | if (strcmp (identification->category[num], standards[i]) == 0) |
| 194 | matched = true; |
| 195 | |
| 196 | if (matched != true) |
| 197 | record_error (0, 0, _("\ |
| 198 | %s: unknown standard `%s' for category `%s'" ), |
| 199 | "LC_IDENTIFICATION" , |
| 200 | identification->category[num], |
| 201 | category_name[num]); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | |
| 207 | void |
| 208 | identification_output (struct localedef_t *locale, |
| 209 | const struct charmap_t *charmap, |
| 210 | const char *output_path) |
| 211 | { |
| 212 | struct locale_identification_t *identification |
| 213 | = locale->categories[LC_IDENTIFICATION].identification; |
| 214 | struct locale_file file; |
| 215 | size_t num; |
| 216 | |
| 217 | init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_IDENTIFICATION)); |
| 218 | add_locale_string (&file, identification->title); |
| 219 | add_locale_string (&file, identification->source); |
| 220 | add_locale_string (&file, identification->address); |
| 221 | add_locale_string (&file, identification->contact); |
| 222 | add_locale_string (&file, identification->email); |
| 223 | add_locale_string (&file, identification->tel); |
| 224 | add_locale_string (&file, identification->fax); |
| 225 | add_locale_string (&file, identification->language); |
| 226 | add_locale_string (&file, identification->territory); |
| 227 | add_locale_string (&file, identification->audience); |
| 228 | add_locale_string (&file, identification->application); |
| 229 | add_locale_string (&file, identification->abbreviation); |
| 230 | add_locale_string (&file, identification->revision); |
| 231 | add_locale_string (&file, identification->date); |
| 232 | start_locale_structure (&file); |
| 233 | for (num = 0; num < __LC_LAST; ++num) |
| 234 | if (num != LC_ALL) |
| 235 | add_locale_string (&file, identification->category[num]); |
| 236 | end_locale_structure (&file); |
| 237 | add_locale_string (&file, charmap->code_set_name); |
| 238 | write_locale_data (output_path, LC_IDENTIFICATION, "LC_IDENTIFICATION" , |
| 239 | &file); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | /* The parser for the LC_IDENTIFICATION section of the locale definition. */ |
| 244 | void |
| 245 | identification_read (struct linereader *ldfile, struct localedef_t *result, |
| 246 | const struct charmap_t *charmap, const char *repertoire_name, |
| 247 | int ignore_content) |
| 248 | { |
| 249 | struct locale_identification_t *identification; |
| 250 | struct token *now; |
| 251 | struct token *arg; |
| 252 | struct token *cattok; |
| 253 | int category; |
| 254 | enum token_t nowtok; |
| 255 | |
| 256 | /* The rest of the line containing `LC_IDENTIFICATION' must be free. */ |
| 257 | lr_ignore_rest (ldfile, 1); |
| 258 | |
| 259 | do |
| 260 | { |
| 261 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 262 | nowtok = now->tok; |
| 263 | } |
| 264 | while (nowtok == tok_eol); |
| 265 | |
| 266 | /* If we see `copy' now we are almost done. */ |
| 267 | if (nowtok == tok_copy) |
| 268 | { |
| 269 | handle_copy (ldfile, charmap, repertoire_name, result, |
| 270 | tok_lc_identification, LC_IDENTIFICATION, |
| 271 | "LC_IDENTIFICATION" , ignore_content); |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | /* Prepare the data structures. */ |
| 276 | identification_startup (ldfile, result, ignore_content); |
| 277 | identification = result->categories[LC_IDENTIFICATION].identification; |
| 278 | |
| 279 | while (1) |
| 280 | { |
| 281 | /* Of course we don't proceed beyond the end of file. */ |
| 282 | if (nowtok == tok_eof) |
| 283 | break; |
| 284 | |
| 285 | /* Ignore empty lines. */ |
| 286 | if (nowtok == tok_eol) |
| 287 | { |
| 288 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 289 | nowtok = now->tok; |
| 290 | continue; |
| 291 | } |
| 292 | |
| 293 | switch (nowtok) |
| 294 | { |
| 295 | #define STR_ELEM(cat) \ |
| 296 | case tok_##cat: \ |
| 297 | /* Ignore the rest of the line if we don't need the input of \ |
| 298 | this line. */ \ |
| 299 | if (ignore_content) \ |
| 300 | { \ |
| 301 | lr_ignore_rest (ldfile, 0); \ |
| 302 | break; \ |
| 303 | } \ |
| 304 | \ |
| 305 | arg = lr_token (ldfile, charmap, result, NULL, verbose); \ |
| 306 | if (arg->tok != tok_string) \ |
| 307 | goto err_label; \ |
| 308 | if (identification->cat != NULL) \ |
| 309 | lr_error (ldfile, _("\ |
| 310 | %s: field `%s' declared more than once"), "LC_IDENTIFICATION", #cat); \ |
| 311 | else if (!ignore_content && arg->val.str.startmb == NULL) \ |
| 312 | { \ |
| 313 | lr_error (ldfile, _("\ |
| 314 | %s: unknown character in field `%s'"), "LC_IDENTIFICATION", #cat); \ |
| 315 | identification->cat = ""; \ |
| 316 | } \ |
| 317 | else if (!ignore_content) \ |
| 318 | identification->cat = arg->val.str.startmb; \ |
| 319 | break |
| 320 | |
| 321 | STR_ELEM (title); |
| 322 | STR_ELEM (source); |
| 323 | STR_ELEM (address); |
| 324 | STR_ELEM (contact); |
| 325 | STR_ELEM (email); |
| 326 | STR_ELEM (tel); |
| 327 | STR_ELEM (fax); |
| 328 | STR_ELEM (language); |
| 329 | STR_ELEM (territory); |
| 330 | STR_ELEM (audience); |
| 331 | STR_ELEM (application); |
| 332 | STR_ELEM (abbreviation); |
| 333 | STR_ELEM (revision); |
| 334 | STR_ELEM (date); |
| 335 | |
| 336 | case tok_category: |
| 337 | /* Ignore the rest of the line if we don't need the input of |
| 338 | this line. */ |
| 339 | if (ignore_content) |
| 340 | { |
| 341 | lr_ignore_rest (ldfile, 0); |
| 342 | break; |
| 343 | } |
| 344 | |
| 345 | /* We expect two operands. */ |
| 346 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
| 347 | if (arg->tok != tok_string && arg->tok != tok_ident) |
| 348 | goto err_label; |
| 349 | /* Next is a semicolon. */ |
| 350 | cattok = lr_token (ldfile, charmap, result, NULL, verbose); |
| 351 | if (cattok->tok != tok_semicolon) |
| 352 | goto err_label; |
| 353 | /* Now a LC_xxx identifier. */ |
| 354 | cattok = lr_token (ldfile, charmap, result, NULL, verbose); |
| 355 | switch (cattok->tok) |
| 356 | { |
| 357 | #define CATEGORY(lname, uname) \ |
| 358 | case tok_lc_##lname: \ |
| 359 | category = LC_##uname; \ |
| 360 | break |
| 361 | |
| 362 | CATEGORY (identification, IDENTIFICATION); |
| 363 | CATEGORY (ctype, CTYPE); |
| 364 | CATEGORY (collate, COLLATE); |
| 365 | CATEGORY (time, TIME); |
| 366 | CATEGORY (numeric, NUMERIC); |
| 367 | CATEGORY (monetary, MONETARY); |
| 368 | CATEGORY (messages, MESSAGES); |
| 369 | CATEGORY (paper, PAPER); |
| 370 | CATEGORY (name, NAME); |
| 371 | CATEGORY (address, ADDRESS); |
| 372 | CATEGORY (telephone, TELEPHONE); |
| 373 | CATEGORY (measurement, MEASUREMENT); |
| 374 | |
| 375 | default: |
| 376 | goto err_label; |
| 377 | } |
| 378 | if (identification->category[category] != NULL) |
| 379 | { |
| 380 | lr_error (ldfile, _("\ |
| 381 | %s: duplicate category version definition" ), "LC_IDENTIFICATION" ); |
| 382 | free (arg->val.str.startmb); |
| 383 | } |
| 384 | else |
| 385 | identification->category[category] = arg->val.str.startmb; |
| 386 | break; |
| 387 | |
| 388 | case tok_end: |
| 389 | /* Next we assume `LC_IDENTIFICATION'. */ |
| 390 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
| 391 | if (arg->tok == tok_eof) |
| 392 | break; |
| 393 | if (arg->tok == tok_eol) |
| 394 | lr_error (ldfile, _("%s: incomplete `END' line" ), |
| 395 | "LC_IDENTIFICATION" ); |
| 396 | else if (arg->tok != tok_lc_identification) |
| 397 | lr_error (ldfile, _("\ |
| 398 | %1$s: definition does not end with `END %1$s'" ), "LC_IDENTIFICATION" ); |
| 399 | lr_ignore_rest (ldfile, arg->tok == tok_lc_identification); |
| 400 | return; |
| 401 | |
| 402 | default: |
| 403 | err_label: |
| 404 | SYNTAX_ERROR (_("%s: syntax error" ), "LC_IDENTIFICATION" ); |
| 405 | } |
| 406 | |
| 407 | /* Prepare for the next round. */ |
| 408 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 409 | nowtok = now->tok; |
| 410 | } |
| 411 | |
| 412 | /* When we come here we reached the end of the file. */ |
| 413 | lr_error (ldfile, _("%s: premature end of file" ), "LC_IDENTIFICATION" ); |
| 414 | } |
| 415 | |