| 1 | /* Copyright (C) 1998-2023 Free Software Foundation, Inc. |
| 2 | This file is part of the GNU C Library. |
| 3 | |
| 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. |
| 8 | |
| 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. |
| 13 | |
| 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/>. */ |
| 16 | |
| 17 | #ifdef HAVE_CONFIG_H |
| 18 | # include <config.h> |
| 19 | #endif |
| 20 | |
| 21 | #include <langinfo.h> |
| 22 | #include <string.h> |
| 23 | #include <stdint.h> |
| 24 | #include <sys/uio.h> |
| 25 | |
| 26 | #include <assert.h> |
| 27 | |
| 28 | #include "localedef.h" |
| 29 | #include "localeinfo.h" |
| 30 | #include "locfile.h" |
| 31 | |
| 32 | |
| 33 | /* The real definition of the struct for the LC_MEASUREMENT locale. */ |
| 34 | struct locale_measurement_t |
| 35 | { |
| 36 | unsigned char measurement; |
| 37 | }; |
| 38 | |
| 39 | |
| 40 | static void |
| 41 | measurement_startup (struct linereader *lr, struct localedef_t *locale, |
| 42 | int ignore_content) |
| 43 | { |
| 44 | if (!ignore_content) |
| 45 | locale->categories[LC_MEASUREMENT].measurement = |
| 46 | (struct locale_measurement_t *) |
| 47 | xcalloc (1, sizeof (struct locale_measurement_t)); |
| 48 | |
| 49 | if (lr != NULL) |
| 50 | { |
| 51 | lr->translate_strings = 1; |
| 52 | lr->return_widestr = 0; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | |
| 57 | void |
| 58 | measurement_finish (struct localedef_t *locale, |
| 59 | const struct charmap_t *charmap) |
| 60 | { |
| 61 | struct locale_measurement_t *measurement = |
| 62 | locale->categories[LC_MEASUREMENT].measurement; |
| 63 | int nothing = 0; |
| 64 | |
| 65 | /* Now resolve copying and also handle completely missing definitions. */ |
| 66 | if (measurement == NULL) |
| 67 | { |
| 68 | /* First see whether we were supposed to copy. If yes, find the |
| 69 | actual definition. */ |
| 70 | if (locale->copy_name[LC_MEASUREMENT] != NULL) |
| 71 | { |
| 72 | /* Find the copying locale. This has to happen transitively since |
| 73 | the locale we are copying from might also copying another one. */ |
| 74 | struct localedef_t *from = locale; |
| 75 | |
| 76 | do |
| 77 | from = find_locale (LC_MEASUREMENT, |
| 78 | from->copy_name[LC_MEASUREMENT], |
| 79 | from->repertoire_name, charmap); |
| 80 | while (from->categories[LC_MEASUREMENT].measurement == NULL |
| 81 | && from->copy_name[LC_MEASUREMENT] != NULL); |
| 82 | |
| 83 | measurement = locale->categories[LC_MEASUREMENT].measurement |
| 84 | = from->categories[LC_MEASUREMENT].measurement; |
| 85 | } |
| 86 | |
| 87 | /* If there is still no definition issue an warning and create an |
| 88 | empty one. */ |
| 89 | if (measurement == NULL) |
| 90 | { |
| 91 | record_warning (_("\ |
| 92 | No definition for %s category found" ), "LC_MEASUREMENT" ); |
| 93 | measurement_startup (NULL, locale, 0); |
| 94 | measurement = locale->categories[LC_MEASUREMENT].measurement; |
| 95 | nothing = 1; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if (measurement->measurement == 0) |
| 100 | { |
| 101 | if (! nothing) |
| 102 | record_error (0, 0, _("%s: field `%s' not defined" ), |
| 103 | "LC_MEASUREMENT" , "measurement" ); |
| 104 | /* Use as the default value the value of the i18n locale. */ |
| 105 | measurement->measurement = 1; |
| 106 | } |
| 107 | else |
| 108 | { |
| 109 | if (measurement->measurement > 3) |
| 110 | record_error (0, 0, _("%s: invalid value for field `%s'" ), |
| 111 | "LC_MEASUREMENT" , "measurement" ); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | |
| 116 | void |
| 117 | measurement_output (struct localedef_t *locale, |
| 118 | const struct charmap_t *charmap, const char *output_path) |
| 119 | { |
| 120 | struct locale_measurement_t *measurement = |
| 121 | locale->categories[LC_MEASUREMENT].measurement; |
| 122 | struct locale_file file; |
| 123 | |
| 124 | init_locale_data (&file, _NL_ITEM_INDEX (_NL_NUM_LC_MEASUREMENT)); |
| 125 | add_locale_char (&file, measurement->measurement); |
| 126 | add_locale_string (&file, charmap->code_set_name); |
| 127 | write_locale_data (output_path, LC_MEASUREMENT, "LC_MEASUREMENT" , &file); |
| 128 | } |
| 129 | |
| 130 | |
| 131 | /* The parser for the LC_MEASUREMENT section of the locale definition. */ |
| 132 | void |
| 133 | measurement_read (struct linereader *ldfile, struct localedef_t *result, |
| 134 | const struct charmap_t *charmap, const char *repertoire_name, |
| 135 | int ignore_content) |
| 136 | { |
| 137 | struct locale_measurement_t *measurement; |
| 138 | struct token *now; |
| 139 | struct token *arg; |
| 140 | enum token_t nowtok; |
| 141 | |
| 142 | /* The rest of the line containing `LC_MEASUREMENT' must be free. */ |
| 143 | lr_ignore_rest (ldfile, 1); |
| 144 | |
| 145 | do |
| 146 | { |
| 147 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 148 | nowtok = now->tok; |
| 149 | } |
| 150 | while (nowtok == tok_eol); |
| 151 | |
| 152 | /* If we see `copy' now we are almost done. */ |
| 153 | if (nowtok == tok_copy) |
| 154 | { |
| 155 | handle_copy (ldfile, charmap, repertoire_name, result, |
| 156 | tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT" , |
| 157 | ignore_content); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | /* Prepare the data structures. */ |
| 162 | measurement_startup (ldfile, result, ignore_content); |
| 163 | measurement = result->categories[LC_MEASUREMENT].measurement; |
| 164 | |
| 165 | while (1) |
| 166 | { |
| 167 | /* Of course we don't proceed beyond the end of file. */ |
| 168 | if (nowtok == tok_eof) |
| 169 | break; |
| 170 | |
| 171 | /* Ingore empty lines. */ |
| 172 | if (nowtok == tok_eol) |
| 173 | { |
| 174 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 175 | nowtok = now->tok; |
| 176 | continue; |
| 177 | } |
| 178 | |
| 179 | switch (nowtok) |
| 180 | { |
| 181 | #define INT_ELEM(cat) \ |
| 182 | case tok_##cat: \ |
| 183 | /* Ignore the rest of the line if we don't need the input of \ |
| 184 | this line. */ \ |
| 185 | if (ignore_content) \ |
| 186 | { \ |
| 187 | lr_ignore_rest (ldfile, 0); \ |
| 188 | break; \ |
| 189 | } \ |
| 190 | \ |
| 191 | arg = lr_token (ldfile, charmap, result, NULL, verbose); \ |
| 192 | if (arg->tok != tok_number) \ |
| 193 | goto err_label; \ |
| 194 | else if (measurement->cat != 0) \ |
| 195 | lr_error (ldfile, _("%s: field `%s' declared more than once"), \ |
| 196 | "LC_MEASUREMENT", #cat); \ |
| 197 | else if (!ignore_content) \ |
| 198 | measurement->cat = arg->val.num; \ |
| 199 | break |
| 200 | |
| 201 | INT_ELEM (measurement); |
| 202 | |
| 203 | case tok_end: |
| 204 | /* Next we assume `LC_MEASUREMENT'. */ |
| 205 | arg = lr_token (ldfile, charmap, result, NULL, verbose); |
| 206 | if (arg->tok == tok_eof) |
| 207 | break; |
| 208 | if (arg->tok == tok_eol) |
| 209 | lr_error (ldfile, _("%s: incomplete `END' line" ), |
| 210 | "LC_MEASUREMENT" ); |
| 211 | else if (arg->tok != tok_lc_measurement) |
| 212 | lr_error (ldfile, _("\ |
| 213 | %1$s: definition does not end with `END %1$s'" ), "LC_MEASUREMENT" ); |
| 214 | lr_ignore_rest (ldfile, arg->tok == tok_lc_measurement); |
| 215 | return; |
| 216 | |
| 217 | default: |
| 218 | err_label: |
| 219 | SYNTAX_ERROR (_("%s: syntax error" ), "LC_MEASUREMENT" ); |
| 220 | } |
| 221 | |
| 222 | /* Prepare for the next round. */ |
| 223 | now = lr_token (ldfile, charmap, result, NULL, verbose); |
| 224 | nowtok = now->tok; |
| 225 | } |
| 226 | |
| 227 | /* When we come here we reached the end of the file. */ |
| 228 | lr_error (ldfile, _("%s: premature end of file" ), |
| 229 | "LC_MEASUREMENT" ); |
| 230 | } |
| 231 | |