lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include <assert.h> |
| 5 | #include <stdint.h> |
| 6 | #include <stddef.h> |
| 7 | |
| 8 | #ifndef __WCHAR_ENABLED |
| 9 | #warning WHOA!!! __WCHAR_ENABLED is not defined! defining it now... |
| 10 | #define __WCHAR_ENABLED |
| 11 | #endif |
| 12 | |
| 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 |
| 19 | |
| 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 */ |
| 24 | |
| 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 */ |
| 36 | |
| 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) */ |
| 41 | |
| 42 | #include "locale_collate.h" |
| 43 | #include "locale_tables.h" |
| 44 | |
| 45 | #include "locale_mmap.h" |
| 46 | |
| 47 | /* #undef __PASTE2 */ |
| 48 | /* #define __PASTE2(A,B) A ## B */ |
| 49 | /* #undef __PASTE3 */ |
| 50 | /* #define __PASTE3(A,B,C) A ## B ## C */ |
| 51 | |
| 52 | |
| 53 | /* #define __LOCALE_DATA_MAGIC_SIZE 64 */ |
| 54 | |
| 55 | /* #define COMMON_MMAP(X) \ */ |
| 56 | /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */ |
| 57 | |
| 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)]; \ */ |
| 62 | |
| 63 | /* ---------------------------------------------------------------------- */ |
| 64 | |
| 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)) \ |
| 70 | |
| 71 | |
| 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), |
| 79 | }; |
| 80 | |
| 81 | |
| 82 | void out_uc(FILE *f, const unsigned char *p, size_t n, char *comment) |
| 83 | { |
| 84 | size_t i; |
| 85 | |
| 86 | fprintf(f, "{\t/* %s */", comment); |
| 87 | for (i = 0 ; i < n ; i++) { |
| 88 | if (!(i & 7)) { |
| 89 | fprintf(f, "\n\t"); |
| 90 | } |
| 91 | if (p[i]) { |
| 92 | fprintf(f, "%#04x, ", p[i]); |
| 93 | } else { |
| 94 | fprintf(f, "%#4x, ", p[i]); |
| 95 | } |
| 96 | } |
| 97 | fprintf(f, "\n},\n"); |
| 98 | } |
| 99 | |
| 100 | void out_u16(FILE *f, const uint16_t *p, size_t n, char *comment) |
| 101 | { |
| 102 | size_t i; |
| 103 | |
| 104 | fprintf(f, "{\t/* %s */", comment); |
| 105 | for (i = 0 ; i < n ; i++) { |
| 106 | if (!(i & 7)) { |
| 107 | fprintf(f, "\n\t"); |
| 108 | } |
| 109 | if (p[i]) { |
| 110 | fprintf(f, "%#06x, ", p[i]); |
| 111 | } else { |
| 112 | fprintf(f, "%#6x, ", p[i]); |
| 113 | } |
| 114 | } |
| 115 | fprintf(f, "\n},\n"); |
| 116 | } |
| 117 | |
| 118 | void out_i16(FILE *f, const int16_t *p, size_t n, char *comment) |
| 119 | { |
| 120 | size_t i; |
| 121 | |
| 122 | fprintf(f, "{\t/* %s */", comment); |
| 123 | for (i = 0 ; i < n ; i++) { |
| 124 | if (!(i & 7)) { |
| 125 | fprintf(f, "\n\t"); |
| 126 | } |
| 127 | fprintf(f, "%6d, ", p[i]); |
| 128 | } |
| 129 | fprintf(f, "\n},\n"); |
| 130 | } |
| 131 | |
| 132 | void out_size_t(FILE *f, const size_t *p, size_t n, char *comment) |
| 133 | { |
| 134 | size_t i; |
| 135 | |
| 136 | fprintf(f, "{\t/* %s */", comment); |
| 137 | for (i = 0 ; i < n ; i++) { |
| 138 | if (!(i & 3)) { |
| 139 | fprintf(f, "\n\t"); |
| 140 | } |
| 141 | if (p[i]) { |
| 142 | fprintf(f, "%#010zx, ", p[i]); |
| 143 | } else { |
| 144 | fprintf(f, "%#10zx, ", p[i]); |
| 145 | } |
| 146 | } |
| 147 | fprintf(f, "\n},\n"); |
| 148 | } |
| 149 | |
| 150 | |
| 151 | int main(int argc, char **argv) |
| 152 | { |
| 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]; |
| 158 | |
| 159 | memset(magic, 0, __LOCALE_DATA_MAGIC_SIZE); |
| 160 | #endif /* __LOCALE_DATA_MAGIC_SIZE */ |
| 161 | |
| 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; |
| 167 | } |
| 168 | |
| 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" |
| 181 | ); |
| 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"); |
| 207 | |
| 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"); |
| 217 | } |
| 218 | fprintf(lso, "},\n"); |
| 219 | #endif /* __CTYPE_HAS_8_BIT_LOCALES */ |
| 220 | |
| 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"); |
| 224 | |
| 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"); |
| 228 | |
| 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"); |
| 232 | |
| 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"); |
| 236 | |
| 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"); |
| 240 | |
| 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"); |
| 244 | |
| 245 | |
| 246 | { |
| 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, |
| 252 | 0, |
| 253 | __lc_messages_item_offsets_LEN |
| 254 | }; |
| 255 | out_uc(lso, co_buf, __LOCALE_DATA_CATEGORIES, "lc_common_item_offsets_LEN"); |
| 256 | } |
| 257 | |
| 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 */ |
| 261 | |
| 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 */ |
| 271 | |
| 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 */ |
| 276 | |
| 277 | fprintf(lso, |
| 278 | "\n};\n\n" |
| 279 | "const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n" |
| 280 | ); |
| 281 | |
| 282 | if (ferror(lso) || fclose(lso)) { |
| 283 | printf("error writing!\n"); |
| 284 | return EXIT_FAILURE; |
| 285 | } |
| 286 | |
| 287 | return EXIT_SUCCESS; |
| 288 | } |
| 289 | |
| 290 | /* ---------------------------------------------------------------------- */ |
| 291 | |
| 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 |
| 298 | */ |