fix static linking for FDPIC toolchains
[uclibc-ng.git] / extra / locale / gen_ldc.c
blob2cedbddaf38b976b1e703506e06f4f4c7c5137d9
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <stdint.h>
6 #include <stddef.h>
8 #ifndef __WCHAR_ENABLED
9 #warning WHOA!!! __WCHAR_ENABLED is not defined! defining it now...
10 #define __WCHAR_ENABLED
11 #endif
13 #define WANT_DATA
14 #include "c8tables.h"
15 #ifndef __CTYPE_HAS_8_BIT_LOCALES
16 #warning __CTYPE_HAS_8_BIT_LOCALES is not defined...
17 /* #define __CTYPE_HAS_8_BIT_LOCALES */
18 #endif
20 /* #define __LOCALE_DATA_Cctype_TBL_LEN 328 */
21 /* #define __LOCALE_DATA_Cuplow_TBL_LEN 400 */
22 /* #define __LOCALE_DATA_Cc2wc_TBL_LEN 1448 */
23 /* #define __LOCALE_DATA_Cwc2c_TBL_LEN 3744 */
25 #define WANT_WCctype_data
26 #define WANT_WCuplow_data
27 #define WANT_WCuplow_diff_data
28 /* #define WANT_WCcomb_data */
29 /* #define WANT_WCwidth_data */
30 #include "wctables.h"
31 #undef WANT_WCctype_data
32 #undef WANT_WCuplow_data
33 #undef WANT_WCuplow_diff_data
34 /* #undef WANT_WCcomb_data */
35 /* #undef WANT_WCwidth_data */
37 #define __LOCALE_DATA_WCctype_TBL_LEN (__LOCALE_DATA_WCctype_II_LEN + __LOCALE_DATA_WCctype_TI_LEN + __LOCALE_DATA_WCctype_UT_LEN)
38 #define __LOCALE_DATA_WCuplow_TBL_LEN (__LOCALE_DATA_WCuplow_II_LEN + __LOCALE_DATA_WCuplow_TI_LEN + __LOCALE_DATA_WCuplow_UT_LEN)
39 #define __LOCALE_DATA_WCuplow_diff_TBL_LEN (2 * __LOCALE_DATA_WCuplow_diffs)
40 /* #define WCcomb_TBL_LEN (WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
42 #include "locale_collate.h"
43 #include "locale_tables.h"
45 #include "locale_mmap.h"
47 /* #undef __PASTE2 */
48 /* #define __PASTE2(A,B) A ## B */
49 /* #undef __PASTE3 */
50 /* #define __PASTE3(A,B,C) A ## B ## C */
53 /* #define __LOCALE_DATA_MAGIC_SIZE 64 */
55 /* #define COMMON_MMAP(X) \ */
56 /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */
58 /* #define COMMON_MMIDX(X) \ */
59 /* unsigned char __PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */
60 /* uint16_t __PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */
61 /* uint16_t __PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */
63 /* ---------------------------------------------------------------------- */
65 #define COMMON_OFFSETS(X) \
66 offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
67 offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
68 offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
69 offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
72 static const size_t common_tbl_offsets[__LOCALE_DATA_CATEGORIES*4] = {
73 COMMON_OFFSETS(ctype),
74 COMMON_OFFSETS(numeric),
75 COMMON_OFFSETS(monetary),
76 COMMON_OFFSETS(time),
77 0, 0, 0, 0, /* collate */
78 COMMON_OFFSETS(messages),
82 void out_uc(FILE *f, const unsigned char *p, size_t n, char *comment)
84 size_t i;
86 fprintf(f, "{\t/* %s */", comment);
87 for (i = 0 ; i < n ; i++) {
88 if (!(i & 7)) {
89 fprintf(f, "\n\t");
91 if (p[i]) {
92 fprintf(f, "%#04x, ", p[i]);
93 } else {
94 fprintf(f, "%#4x, ", p[i]);
97 fprintf(f, "\n},\n");
100 void out_u16(FILE *f, const uint16_t *p, size_t n, char *comment)
102 size_t i;
104 fprintf(f, "{\t/* %s */", comment);
105 for (i = 0 ; i < n ; i++) {
106 if (!(i & 7)) {
107 fprintf(f, "\n\t");
109 if (p[i]) {
110 fprintf(f, "%#06x, ", p[i]);
111 } else {
112 fprintf(f, "%#6x, ", p[i]);
115 fprintf(f, "\n},\n");
118 void out_i16(FILE *f, const int16_t *p, size_t n, char *comment)
120 size_t i;
122 fprintf(f, "{\t/* %s */", comment);
123 for (i = 0 ; i < n ; i++) {
124 if (!(i & 7)) {
125 fprintf(f, "\n\t");
127 fprintf(f, "%6d, ", p[i]);
129 fprintf(f, "\n},\n");
132 void out_size_t(FILE *f, const size_t *p, size_t n, char *comment)
134 size_t i;
136 fprintf(f, "{\t/* %s */", comment);
137 for (i = 0 ; i < n ; i++) {
138 if (!(i & 3)) {
139 fprintf(f, "\n\t");
141 if (p[i]) {
142 fprintf(f, "%#010zx, ", p[i]);
143 } else {
144 fprintf(f, "%#10zx, ", p[i]);
147 fprintf(f, "\n},\n");
151 int main(int argc, char **argv)
153 char *output_file = "locale_data.c";
154 FILE *lso; /* static object */
155 int i;
156 #ifdef __LOCALE_DATA_MAGIC_SIZE
157 unsigned char magic[__LOCALE_DATA_MAGIC_SIZE];
159 memset(magic, 0, __LOCALE_DATA_MAGIC_SIZE);
160 #endif /* __LOCALE_DATA_MAGIC_SIZE */
162 if (argc == 2)
163 output_file = argv[1];
164 if (!(lso = fopen(output_file, "w"))) {
165 printf("cannot open output file '%s'!\n", output_file);
166 return EXIT_FAILURE;
169 fprintf(lso,
170 "#include <stddef.h>\n"
171 "#include <stdint.h>\n"
172 /* "#define __CTYPE_HAS_8_BIT_LOCALES\n" */
173 "#ifndef __WCHAR_ENABLED\n"
174 "#error __WCHAR_ENABLED not defined\n"
175 "#endif\n"
176 "#include \"c8tables.h\"\n"
177 "#include \"wctables.h\"\n"
178 "#include \"lt_defines.h\"\n"
179 "#include \"locale_mmap.h\"\n\n"
180 "static const __locale_mmap_t locale_mmap = {\n\n"
182 #ifdef __LOCALE_DATA_MAGIC_SIZE
183 out_uc(lso, magic, __LOCALE_DATA_MAGIC_SIZE, "magic");
184 #endif /* __LOCALE_DATA_MAGIC_SIZE */
185 #ifdef __CTYPE_HAS_8_BIT_LOCALES
186 out_uc(lso, __LOCALE_DATA_Cctype_data, __LOCALE_DATA_Cctype_TBL_LEN, "tbl8ctype");
187 out_uc(lso, __LOCALE_DATA_Cuplow_data, __LOCALE_DATA_Cuplow_TBL_LEN, "tbl8uplow");
188 #ifdef __WCHAR_ENABLED
189 out_u16(lso, __LOCALE_DATA_Cc2wc_data, __LOCALE_DATA_Cc2wc_TBL_LEN, "tbl8c2wc");
190 out_uc(lso, __LOCALE_DATA_Cwc2c_data, __LOCALE_DATA_Cwc2c_TBL_LEN, "tbl8wc2c");
191 /* translit */
192 #endif /* __WCHAR_ENABLED */
193 #endif /* __CTYPE_HAS_8_BIT_LOCALES */
194 #ifdef __WCHAR_ENABLED
195 out_uc(lso, __LOCALE_DATA_WCctype_data, __LOCALE_DATA_WCctype_TBL_LEN, "tblwctype");
196 out_uc(lso, __LOCALE_DATA_WCuplow_data, __LOCALE_DATA_WCuplow_TBL_LEN, "tblwuplow");
197 out_i16(lso, __LOCALE_DATA_WCuplow_diff_data, __LOCALE_DATA_WCuplow_diff_TBL_LEN, "tblwuplow_diff");
198 /* const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
199 /* width?? */
200 #endif /* __WCHAR_ENABLED */
201 out_uc(lso, __lc_ctype_data, __lc_ctype_data_LEN, "lc_ctype_data");
202 out_uc(lso, __lc_numeric_data, __lc_numeric_data_LEN, "lc_numeric_data");
203 out_uc(lso, __lc_monetary_data, __lc_monetary_data_LEN, "lc_monetary_data");
204 out_uc(lso, __lc_time_data, __lc_time_data_LEN, "lc_time_data");
205 /* TODO -- collate*/
206 out_uc(lso, __lc_messages_data, __lc_messages_data_LEN, "lc_messages_data");
208 #ifdef __CTYPE_HAS_8_BIT_LOCALES
209 fprintf(lso, "{ /* codeset_8_bit array */\n");
210 for (i = 0 ; i < __LOCALE_DATA_NUM_CODESETS ; i++) {
211 fprintf(lso, "{ /* codeset_8_bit[%d] */\n", i);
212 out_uc(lso, codeset_8_bit[i].idx8ctype, __LOCALE_DATA_Cctype_IDX_LEN, "idx8ctype");
213 out_uc(lso, codeset_8_bit[i].idx8uplow, __LOCALE_DATA_Cuplow_IDX_LEN, "idx8uplow");
214 out_uc(lso, codeset_8_bit[i].idx8c2wc, __LOCALE_DATA_Cc2wc_IDX_LEN, "idx8c2wc");
215 out_uc(lso, codeset_8_bit[i].idx8wc2c, __LOCALE_DATA_Cwc2c_II_LEN, "idx8wc2c");
216 fprintf(lso, "},\n");
218 fprintf(lso, "},\n");
219 #endif /* __CTYPE_HAS_8_BIT_LOCALES */
221 out_uc(lso, __lc_ctype_rows, __lc_ctype_rows_LEN, "lc_ctype_rows");
222 out_u16(lso, __lc_ctype_item_offsets, __lc_ctype_item_offsets_LEN, "lc_ctype_item_offsets");
223 out_u16(lso, __lc_ctype_item_idx, __lc_ctype_item_idx_LEN, "lc_ctype_item_idx");
225 out_uc(lso, __lc_numeric_rows, __lc_numeric_rows_LEN, "lc_numeric_rows");
226 out_u16(lso, __lc_numeric_item_offsets, __lc_numeric_item_offsets_LEN, "lc_numeric_item_offsets");
227 out_u16(lso, __lc_numeric_item_idx, __lc_numeric_item_idx_LEN, "lc_numeric_item_idx");
229 out_uc(lso, __lc_monetary_rows, __lc_monetary_rows_LEN, "lc_monetary_rows");
230 out_u16(lso, __lc_monetary_item_offsets, __lc_monetary_item_offsets_LEN, "lc_monetary_item_offsets");
231 out_u16(lso, __lc_monetary_item_idx, __lc_monetary_item_idx_LEN, "lc_monetary_item_idx");
233 out_uc(lso, __lc_time_rows, __lc_time_rows_LEN, "lc_time_rows");
234 out_u16(lso, __lc_time_item_offsets, __lc_time_item_offsets_LEN, "lc_time_item_offsets");
235 out_u16(lso, __lc_time_item_idx, __lc_time_item_idx_LEN, "lc_time_item_idx");
237 out_uc(lso, __lc_messages_rows, __lc_messages_rows_LEN, "lc_messages_rows");
238 out_u16(lso, __lc_messages_item_offsets, __lc_messages_item_offsets_LEN, "lc_messages_item_offsets");
239 out_u16(lso, __lc_messages_item_idx, __lc_messages_item_idx_LEN, "lc_messages_item_idx");
241 /* collate should be last*/
242 assert(sizeof(__locale_collate_tbl)/sizeof(__locale_collate_tbl[0]) == __lc_collate_data_LEN) ;
243 out_u16(lso, __locale_collate_tbl, __lc_collate_data_LEN, "collate_data");
247 unsigned char co_buf[__LOCALE_DATA_CATEGORIES] = {
248 __lc_ctype_item_offsets_LEN,
249 __lc_numeric_item_offsets_LEN,
250 __lc_monetary_item_offsets_LEN,
251 __lc_time_item_offsets_LEN,
253 __lc_messages_item_offsets_LEN
255 out_uc(lso, co_buf, __LOCALE_DATA_CATEGORIES, "lc_common_item_offsets_LEN");
258 out_size_t(lso, common_tbl_offsets, __LOCALE_DATA_CATEGORIES * 4, "lc_common_tbl_offsets");
259 /* offsets from start of locale_mmap_t */
260 /* rows, item_offsets, item_idx, data */
262 #ifdef __LOCALE_DATA_NUM_LOCALES
263 out_uc(lso, __locales, __LOCALE_DATA_NUM_LOCALES * __LOCALE_DATA_WIDTH_LOCALES, "locales");
264 out_uc(lso, __locale_names5, 5 * __LOCALE_DATA_NUM_LOCALE_NAMES, "locale_names5");
265 #ifdef __LOCALE_DATA_AT_MODIFIERS_LENGTH
266 out_uc(lso, __locale_at_modifiers, __LOCALE_DATA_AT_MODIFIERS_LENGTH, "locale_at_modifiers");
267 #else
268 #error __LOCALE_DATA_AT_MODIFIERS_LENGTH not defined!
269 #endif /* __LOCALE_DATA_AT_MODIFIERS_LENGTH */
270 #endif /* __LOCALE_DATA_NUM_LOCALES */
272 out_uc(lso, lc_names, __lc_names_LEN, "lc_names");
273 #ifdef __CTYPE_HAS_8_BIT_LOCALES
274 out_uc(lso, (const unsigned char*) __LOCALE_DATA_CODESET_LIST, sizeof(__LOCALE_DATA_CODESET_LIST), "codeset_list");
275 #endif /* __CTYPE_HAS_8_BIT_LOCALES */
277 fprintf(lso,
278 "\n};\n\n"
279 "const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n"
282 if (ferror(lso) || fclose(lso)) {
283 printf("error writing!\n");
284 return EXIT_FAILURE;
287 return EXIT_SUCCESS;
290 /* ---------------------------------------------------------------------- */
292 /* TODO:
293 * collate data (8-bit weighted single char only)
294 * @ mappings!
295 * codeset list? yes, since we'll want to be able to inspect them...
296 * that means putting some header stuff in magic
297 * fix ctype LEN defines in gen_c8tables