1 /* Copyright (C) 1996-2022 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 #ifndef _LOCFILE_H
18 #define _LOCFILE_H 1
19
20 #include <byteswap.h>
21 #include <stdbool.h>
22 #include <stdint.h>
23 #include <sys/uio.h>
24
25 #include "obstack.h"
26 #include "linereader.h"
27 #include "localedef.h"
28
29 /* Structure for storing the contents of a category file. */
30 struct locale_file
31 {
32 size_t n_elements, next_element;
33 uint32_t *offsets;
34 struct obstack data;
35 int structure_stage;
36 };
37
38
39 /* Macros used in the parser. */
40 #define SYNTAX_ERROR(string, args...) \
41 do \
42 { \
43 lr_error (ldfile, string, ## args); \
44 lr_ignore_rest (ldfile, 0); \
45 } \
46 while (0)
47
48
49 /* General handling of `copy'. */
50 extern void handle_copy (struct linereader *ldfile,
51 const struct charmap_t *charmap,
52 const char *repertoire_name,
53 struct localedef_t *result, enum token_t token,
54 int locale, const char *locale_name,
55 int ignore_content);
56
57 /* Found in locfile.c. */
58 extern int locfile_read (struct localedef_t *result,
59 const struct charmap_t *charmap);
60
61 /* Check validity of all the locale data. */
62 extern void check_all_categories (struct localedef_t *definitions,
63 const struct charmap_t *charmap);
64
65 /* Write out all locale categories. */
66 extern void write_all_categories (struct localedef_t *definitions,
67 const struct charmap_t *charmap,
68 const char *locname,
69 const char *output_path);
70
71 extern bool swap_endianness_p;
72
73 /* Change the output to be big-endian if BIG_ENDIAN is true and
74 little-endian otherwise. */
75 static inline void
set_big_endian(bool big_endian)76 set_big_endian (bool big_endian)
77 {
78 swap_endianness_p = (big_endian != (__BYTE_ORDER == __BIG_ENDIAN));
79 }
80
81 /* Munge VALUE so that, when stored, it has the correct byte order
82 for the output files. */
83 static uint32_t
84 __attribute__ ((unused))
maybe_swap_uint32(uint32_t value)85 maybe_swap_uint32 (uint32_t value)
86 {
87 return swap_endianness_p ? bswap_32 (value) : value;
88 }
89
90 /* Likewise, but munge an array of N uint32_ts starting at ARRAY. */
91 static inline void
maybe_swap_uint32_array(uint32_t * array,size_t n)92 maybe_swap_uint32_array (uint32_t *array, size_t n)
93 {
94 if (swap_endianness_p)
95 while (n-- > 0)
96 array[n] = bswap_32 (array[n]);
97 }
98
99 /* Like maybe_swap_uint32_array, but the array of N elements is at
100 the end of OBSTACK's current object. */
101 static inline void
maybe_swap_uint32_obstack(struct obstack * obstack,size_t n)102 maybe_swap_uint32_obstack (struct obstack *obstack, size_t n)
103 {
104 maybe_swap_uint32_array ((uint32_t *) obstack_next_free (obstack) - n, n);
105 }
106
107 /* Write out the data. */
108 extern void init_locale_data (struct locale_file *file, size_t n_elements);
109 extern void align_locale_data (struct locale_file *file, size_t boundary);
110 extern void add_locale_empty (struct locale_file *file);
111 extern void add_locale_raw_data (struct locale_file *file, const void *data,
112 size_t size);
113 extern void add_locale_raw_obstack (struct locale_file *file,
114 struct obstack *obstack);
115 extern void add_locale_string (struct locale_file *file, const char *string);
116 extern void add_locale_wstring (struct locale_file *file,
117 const uint32_t *string);
118 extern void add_locale_uint32 (struct locale_file *file, uint32_t value);
119 extern void add_locale_uint32_array (struct locale_file *file,
120 const uint32_t *data, size_t n_elems);
121 extern void add_locale_char (struct locale_file *file, char value);
122 extern void start_locale_structure (struct locale_file *file);
123 extern void end_locale_structure (struct locale_file *file);
124 extern void start_locale_prelude (struct locale_file *file);
125 extern void end_locale_prelude (struct locale_file *file);
126 extern void write_locale_data (const char *output_path, int catidx,
127 const char *category, struct locale_file *file);
128
129
130 /* Entrypoints for the parsers of the individual categories. */
131
132 /* Handle LC_CTYPE category. */
133 extern void ctype_read (struct linereader *ldfile,
134 struct localedef_t *result,
135 const struct charmap_t *charmap,
136 const char *repertoire_name,
137 int ignore_content);
138 extern void ctype_finish (struct localedef_t *locale,
139 const struct charmap_t *charmap);
140 extern void ctype_output (struct localedef_t *locale,
141 const struct charmap_t *charmap,
142 const char *output_path);
143 extern uint32_t *find_translit (struct localedef_t *locale,
144 const struct charmap_t *charmap, uint32_t wch);
145
146 /* Handle LC_COLLATE category. */
147 extern void collate_read (struct linereader *ldfile,
148 struct localedef_t *result,
149 const struct charmap_t *charmap,
150 const char *repertoire_name,
151 int ignore_content);
152 extern void collate_finish (struct localedef_t *locale,
153 const struct charmap_t *charmap);
154 extern void collate_output (struct localedef_t *locale,
155 const struct charmap_t *charmap,
156 const char *output_path);
157
158 /* Handle LC_MONETARY category. */
159 extern void monetary_read (struct linereader *ldfile,
160 struct localedef_t *result,
161 const struct charmap_t *charmap,
162 const char *repertoire_name,
163 int ignore_content);
164 extern void monetary_finish (struct localedef_t *locale,
165 const struct charmap_t *charmap);
166 extern void monetary_output (struct localedef_t *locale,
167 const struct charmap_t *charmap,
168 const char *output_path);
169
170 /* Handle LC_NUMERIC category. */
171 extern void numeric_read (struct linereader *ldfile,
172 struct localedef_t *result,
173 const struct charmap_t *charmap,
174 const char *repertoire_name,
175 int ignore_content);
176 extern void numeric_finish (struct localedef_t *locale,
177 const struct charmap_t *charmap);
178 extern void numeric_output (struct localedef_t *locale,
179 const struct charmap_t *charmap,
180 const char *output_path);
181
182 /* Handle LC_MESSAGES category. */
183 extern void messages_read (struct linereader *ldfile,
184 struct localedef_t *result,
185 const struct charmap_t *charmap,
186 const char *repertoire_name,
187 int ignore_content);
188 extern void messages_finish (struct localedef_t *locale,
189 const struct charmap_t *charmap);
190 extern void messages_output (struct localedef_t *locale,
191 const struct charmap_t *charmap,
192 const char *output_path);
193
194 /* Handle LC_TIME category. */
195 extern void time_read (struct linereader *ldfile,
196 struct localedef_t *result,
197 const struct charmap_t *charmap,
198 const char *repertoire_name,
199 int ignore_content);
200 extern void time_finish (struct localedef_t *locale,
201 const struct charmap_t *charmap);
202 extern void time_output (struct localedef_t *locale,
203 const struct charmap_t *charmap,
204 const char *output_path);
205
206 /* Handle LC_PAPER category. */
207 extern void paper_read (struct linereader *ldfile,
208 struct localedef_t *result,
209 const struct charmap_t *charmap,
210 const char *repertoire_name,
211 int ignore_content);
212 extern void paper_finish (struct localedef_t *locale,
213 const struct charmap_t *charmap);
214 extern void paper_output (struct localedef_t *locale,
215 const struct charmap_t *charmap,
216 const char *output_path);
217
218 /* Handle LC_NAME category. */
219 extern void name_read (struct linereader *ldfile,
220 struct localedef_t *result,
221 const struct charmap_t *charmap,
222 const char *repertoire_name,
223 int ignore_content);
224 extern void name_finish (struct localedef_t *locale,
225 const struct charmap_t *charmap);
226 extern void name_output (struct localedef_t *locale,
227 const struct charmap_t *charmap,
228 const char *output_path);
229
230 /* Handle LC_ADDRESS category. */
231 extern void address_read (struct linereader *ldfile,
232 struct localedef_t *result,
233 const struct charmap_t *charmap,
234 const char *repertoire_name,
235 int ignore_content);
236 extern void address_finish (struct localedef_t *locale,
237 const struct charmap_t *charmap);
238 extern void address_output (struct localedef_t *locale,
239 const struct charmap_t *charmap,
240 const char *output_path);
241
242 /* Handle LC_TELEPHONE category. */
243 extern void telephone_read (struct linereader *ldfile,
244 struct localedef_t *result,
245 const struct charmap_t *charmap,
246 const char *repertoire_name,
247 int ignore_content);
248 extern void telephone_finish (struct localedef_t *locale,
249 const struct charmap_t *charmap);
250 extern void telephone_output (struct localedef_t *locale,
251 const struct charmap_t *charmap,
252 const char *output_path);
253
254 /* Handle LC_MEASUREMENT category. */
255 extern void measurement_read (struct linereader *ldfile,
256 struct localedef_t *result,
257 const struct charmap_t *charmap,
258 const char *repertoire_name,
259 int ignore_content);
260 extern void measurement_finish (struct localedef_t *locale,
261 const struct charmap_t *charmap);
262 extern void measurement_output (struct localedef_t *locale,
263 const struct charmap_t *charmap,
264 const char *output_path);
265
266 /* Handle LC_IDENTIFICATION category. */
267 extern void identification_read (struct linereader *ldfile,
268 struct localedef_t *result,
269 const struct charmap_t *charmap,
270 const char *repertoire_name,
271 int ignore_content);
272 extern void identification_finish (struct localedef_t *locale,
273 const struct charmap_t *charmap);
274 extern void identification_output (struct localedef_t *locale,
275 const struct charmap_t *charmap,
276 const char *output_path);
277
278 #endif /* locfile.h */
279