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