powerpc64: Fix by using the configure value $libc_cv_cc_submachine [BZ #31629]
[glibc.git] / locale / programs / ld-measurement.c
blobc4061264ea8fd3218c9ce65f943a687ad7283fad
1 /* Copyright (C) 1998-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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/>. */
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
21 #include <langinfo.h>
22 #include <string.h>
23 #include <stdint.h>
24 #include <sys/uio.h>
26 #include <assert.h>
28 #include "localedef.h"
29 #include "localeinfo.h"
30 #include "locfile.h"
33 /* The real definition of the struct for the LC_MEASUREMENT locale. */
34 struct locale_measurement_t
36 unsigned char measurement;
40 static void
41 measurement_startup (struct linereader *lr, struct localedef_t *locale,
42 int ignore_content)
44 if (!ignore_content)
45 locale->categories[LC_MEASUREMENT].measurement =
46 (struct locale_measurement_t *)
47 xcalloc (1, sizeof (struct locale_measurement_t));
49 if (lr != NULL)
51 lr->translate_strings = 1;
52 lr->return_widestr = 0;
57 void
58 measurement_finish (struct localedef_t *locale,
59 const struct charmap_t *charmap)
61 struct locale_measurement_t *measurement =
62 locale->categories[LC_MEASUREMENT].measurement;
63 int nothing = 0;
65 /* Now resolve copying and also handle completely missing definitions. */
66 if (measurement == NULL)
68 /* First see whether we were supposed to copy. If yes, find the
69 actual definition. */
70 if (locale->copy_name[LC_MEASUREMENT] != NULL)
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;
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);
83 measurement = locale->categories[LC_MEASUREMENT].measurement
84 = from->categories[LC_MEASUREMENT].measurement;
87 /* If there is still no definition issue an warning and create an
88 empty one. */
89 if (measurement == NULL)
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;
99 if (measurement->measurement == 0)
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;
107 else
109 if (measurement->measurement > 3)
110 record_error (0, 0, _("%s: invalid value for field `%s'"),
111 "LC_MEASUREMENT", "measurement");
116 void
117 measurement_output (struct localedef_t *locale,
118 const struct charmap_t *charmap, const char *output_path)
120 struct locale_measurement_t *measurement =
121 locale->categories[LC_MEASUREMENT].measurement;
122 struct locale_file file;
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);
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)
137 struct locale_measurement_t *measurement;
138 struct token *now;
139 struct token *arg;
140 enum token_t nowtok;
142 /* The rest of the line containing `LC_MEASUREMENT' must be free. */
143 lr_ignore_rest (ldfile, 1);
147 now = lr_token (ldfile, charmap, result, NULL, verbose);
148 nowtok = now->tok;
150 while (nowtok == tok_eol);
152 /* If we see `copy' now we are almost done. */
153 if (nowtok == tok_copy)
155 handle_copy (ldfile, charmap, repertoire_name, result,
156 tok_lc_measurement, LC_MEASUREMENT, "LC_MEASUREMENT",
157 ignore_content);
158 return;
161 /* Prepare the data structures. */
162 measurement_startup (ldfile, result, ignore_content);
163 measurement = result->categories[LC_MEASUREMENT].measurement;
165 while (1)
167 /* Of course we don't proceed beyond the end of file. */
168 if (nowtok == tok_eof)
169 break;
171 /* Ingore empty lines. */
172 if (nowtok == tok_eol)
174 now = lr_token (ldfile, charmap, result, NULL, verbose);
175 nowtok = now->tok;
176 continue;
179 switch (nowtok)
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) \
187 lr_ignore_rest (ldfile, 0); \
188 break; \
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
201 INT_ELEM (measurement);
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;
217 default:
218 err_label:
219 SYNTAX_ERROR (_("%s: syntax error"), "LC_MEASUREMENT");
222 /* Prepare for the next round. */
223 now = lr_token (ldfile, charmap, result, NULL, verbose);
224 nowtok = now->tok;
227 /* When we come here we reached the end of the file. */
228 lr_error (ldfile, _("%s: premature end of file"),
229 "LC_MEASUREMENT");