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