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