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 | #define WANT_DATA |
| 9 | #include "c8tables.h" |
| 10 | |
| 11 | /* #define Cctype_TBL_LEN 328 */ |
| 12 | /* #define Cuplow_TBL_LEN 400 */ |
| 13 | /* #define Cc2wc_TBL_LEN 1448 */ |
| 14 | /* #define Cwc2c_TBL_LEN 3744 */ |
| 15 | |
| 16 | #define WANT_WCctype_data |
| 17 | #define WANT_WCuplow_data |
| 18 | #define WANT_WCuplow_diff_data |
| 19 | /* #define WANT_WCcomb_data */ |
| 20 | /* #define WANT_WCwidth_data */ |
| 21 | #include "wctables.h" |
| 22 | #undef WANT_WCctype_data |
| 23 | #undef WANT_WCuplow_data |
| 24 | #undef WANT_WCuplow_diff_data |
| 25 | /* #undef WANT_WCcomb_data */ |
| 26 | /* #undef WANT_WCwidth_data */ |
| 27 | |
| 28 | /* #define WCctype_TBL_LEN (WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN) */ |
| 29 | /* #define WCuplow_TBL_LEN (WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN) */ |
| 30 | /* #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs) */ |
| 31 | /* #define WCcomb_TBL_LEN (WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */ |
| 32 | |
| 33 | #include "locale_tables.h" |
| 34 | |
| 35 | #include "locale_mmap.h" |
| 36 | |
| 37 | /* #undef __PASTE2 */ |
| 38 | /* #define __PASTE2(A,B) A ## B */ |
| 39 | /* #undef __PASTE3 */ |
| 40 | /* #define __PASTE3(A,B,C) A ## B ## C */ |
| 41 | |
| 42 | |
| 43 | /* #define MAGIC_SIZE 64 */ |
| 44 | |
| 45 | /* #define COMMON_MMAP(X) \ */ |
| 46 | /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */ |
| 47 | |
| 48 | /* #define COMMON_MMIDX(X) \ */ |
| 49 | /* unsigned char __PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */ |
| 50 | /* uint16_t __PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */ |
| 51 | /* uint16_t __PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */ |
| 52 | |
| 53 | #define WRITE_COMMON_MMAP(X) \ |
| 54 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)), SEEK_SET); \ |
| 55 | for (i=0 ; i < __PASTE3(__lc_,X,_data_LEN) ; i++) { \ |
| 56 | putc(__PASTE3(__lc_,X,_data)[i], fp); \ |
| 57 | } |
| 58 | |
| 59 | #define WRITE_COMMON_MMIDX(X) \ |
| 60 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), SEEK_SET); \ |
| 61 | for (i=0 ; i < __PASTE3(__lc_,X,_rows_LEN) ; i++) { \ |
| 62 | putc(__PASTE3(__lc_,X,_rows)[i], fp); \ |
| 63 | } \ |
| 64 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), SEEK_SET); \ |
| 65 | for (i=0 ; i < __PASTE3(__lc_,X,_item_offsets_LEN) ; i++) { \ |
| 66 | putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[0], fp); \ |
| 67 | putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[1], fp); \ |
| 68 | } \ |
| 69 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), SEEK_SET); \ |
| 70 | for (i=0 ; i < __PASTE3(__lc_,X,_item_idx_LEN) ; i++) { \ |
| 71 | putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[0], fp); \ |
| 72 | putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[1], fp); \ |
| 73 | } |
| 74 | |
| 75 | #define WRITE_WC_DATA(X) \ |
| 76 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \ |
| 77 | for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \ |
| 78 | putc(__PASTE3(WC,X,_data)[i], fp); \ |
| 79 | } |
| 80 | |
| 81 | #define WRITE_WC_I16_DATA(X) \ |
| 82 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \ |
| 83 | for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \ |
| 84 | putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[0], fp); \ |
| 85 | putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[1], fp); \ |
| 86 | } |
| 87 | |
| 88 | #define WRITE_C_DATA(X) \ |
| 89 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \ |
| 90 | for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \ |
| 91 | putc(__PASTE3(C,X,_data)[i], fp); \ |
| 92 | } |
| 93 | |
| 94 | #define WRITE_C_U16_DATA(X) \ |
| 95 | fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \ |
| 96 | for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \ |
| 97 | putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[0], fp); \ |
| 98 | putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[1], fp); \ |
| 99 | } |
| 100 | |
| 101 | /**********************************************************************/ |
| 102 | |
| 103 | #define COMMON_OFFSETS(X) \ |
| 104 | offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \ |
| 105 | offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \ |
| 106 | offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \ |
| 107 | offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \ |
| 108 | |
| 109 | |
| 110 | static const size_t common_tbl_offsets[CATEGORIES*4] = { |
| 111 | 0, 0, 0, 0, /* ctype */ |
| 112 | COMMON_OFFSETS(numeric), |
| 113 | COMMON_OFFSETS(monetary), |
| 114 | COMMON_OFFSETS(time), |
| 115 | 0, 0, 0, 0, /* collate */ |
| 116 | COMMON_OFFSETS(messages), |
| 117 | }; |
| 118 | |
| 119 | |
| 120 | |
| 121 | |
| 122 | |
| 123 | int main(void) |
| 124 | { |
| 125 | FILE *fp; |
| 126 | size_t i; |
| 127 | unsigned char *p; |
| 128 | |
| 129 | if (!(fp = fopen("locale.mmap", "w"))) { |
| 130 | printf("cannot open locale.mmap for writing!"); |
| 131 | return EXIT_FAILURE; |
| 132 | } |
| 133 | |
| 134 | for (i=0 ; i < sizeof(__locale_mmap_t) ; i++) { |
| 135 | putc(0, fp); /* Zero out the file. */ |
| 136 | } |
| 137 | |
| 138 | #ifdef __CTYPE_HAS_8_BIT_LOCALES |
| 139 | WRITE_C_DATA(ctype); |
| 140 | WRITE_C_DATA(uplow); |
| 141 | #ifdef __WCHAR_ENABLED |
| 142 | WRITE_C_U16_DATA(c2wc); |
| 143 | WRITE_C_DATA(wc2c); |
| 144 | /* translit */ |
| 145 | #endif /* __WCHAR_ENABLED */ |
| 146 | #endif /* __CTYPE_HAS_8_BIT_LOCALES */ |
| 147 | |
| 148 | #ifdef __WCHAR_ENABLED |
| 149 | WRITE_WC_DATA(ctype); |
| 150 | WRITE_WC_DATA(uplow); |
| 151 | WRITE_WC_I16_DATA(uplow_diff); |
| 152 | /* WRITE_WC_DATA(comb); */ |
| 153 | /* width?? */ |
| 154 | #endif /* __WCHAR_ENABLED */ |
| 155 | |
| 156 | WRITE_COMMON_MMAP(numeric); |
| 157 | WRITE_COMMON_MMAP(monetary); |
| 158 | WRITE_COMMON_MMAP(time); |
| 159 | /* TODO -- collate*/ |
| 160 | WRITE_COMMON_MMAP(messages); |
| 161 | |
| 162 | #ifdef __CTYPE_HAS_8_BIT_LOCALES |
| 163 | fseek(fp, (long) offsetof(__locale_mmap_t, codeset_8_bit), SEEK_SET); \ |
| 164 | p = (unsigned char *) codeset_8_bit; |
| 165 | for (i=0 ; i < sizeof(codeset_8_bit) ; i++) { |
| 166 | putc(p[i], fp); |
| 167 | } |
| 168 | #endif /* __CTYPE_HAS_8_BIT_LOCALES */ |
| 169 | |
| 170 | WRITE_COMMON_MMIDX(numeric); |
| 171 | WRITE_COMMON_MMIDX(monetary); |
| 172 | WRITE_COMMON_MMIDX(time); |
| 173 | /* TODO -- collate*/ |
| 174 | WRITE_COMMON_MMIDX(messages); |
| 175 | |
| 176 | fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_item_offsets_LEN), SEEK_SET); |
| 177 | putc(1, fp); /* ctype -- (codeset) handled specially */ |
| 178 | putc(__lc_numeric_item_offsets_LEN, fp); |
| 179 | putc(__lc_monetary_item_offsets_LEN, fp); |
| 180 | putc(__lc_time_item_offsets_LEN, fp); |
| 181 | putc(0, fp); /* collate */ |
| 182 | putc(__lc_messages_item_offsets_LEN, fp); |
| 183 | |
| 184 | fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_tbl_offsets), SEEK_SET); |
| 185 | for (i=0 ; i < sizeof(common_tbl_offsets) ; i++) { |
| 186 | putc(((unsigned char *)common_tbl_offsets)[i], fp); |
| 187 | } |
| 188 | |
| 189 | #ifdef NUM_LOCALES |
| 190 | fseek(fp, (long) offsetof(__locale_mmap_t, locales), SEEK_SET); |
| 191 | for (i=0 ; i < (NUM_LOCALES * WIDTH_LOCALES) ; i++) { |
| 192 | putc(__locales[i], fp); |
| 193 | } |
| 194 | |
| 195 | fseek(fp, (long) offsetof(__locale_mmap_t, locale_names5), SEEK_SET); |
| 196 | for (i=0 ; i < 5 * NUM_LOCALE_NAMES ; i++) { |
| 197 | putc(__locale_names5[i], fp); |
| 198 | } |
| 199 | |
| 200 | #ifdef LOCALE_AT_MODIFIERS_LENGTH |
| 201 | fseek(fp, (long) offsetof(__locale_mmap_t, locale_at_modifiers), SEEK_SET); |
| 202 | for (i=0 ; i < LOCALE_AT_MODIFIERS_LENGTH ; i++) { |
| 203 | putc(__locale_at_modifiers[i], fp); |
| 204 | } |
| 205 | #endif /* LOCALE_AT_MODIFIERS_LENGTH */ |
| 206 | #endif /* NUM_LOCALES */ |
| 207 | |
| 208 | fseek(fp, (long) offsetof(__locale_mmap_t, lc_names), SEEK_SET); |
| 209 | for (i=0 ; i < lc_names_LEN ; i++) { |
| 210 | putc(lc_names[i], fp); |
| 211 | } |
| 212 | |
| 213 | #ifdef __CTYPE_HAS_8_BIT_LOCALES |
| 214 | fseek(fp, (long) offsetof(__locale_mmap_t, codeset_list), SEEK_SET); |
| 215 | for (i=0 ; i < sizeof(CODESET_LIST) ; i++) { |
| 216 | putc((unsigned char)(CODESET_LIST[i]), fp); |
| 217 | } |
| 218 | #endif /* __CTYPE_HAS_8_BIT_LOCALES */ |
| 219 | |
| 220 | |
| 221 | if (ferror(fp) || (fclose(fp) == EOF)) { |
| 222 | printf("error - stream in error state or fclose failed!"); |
| 223 | return EXIT_FAILURE; |
| 224 | } |
| 225 | |
| 226 | printf("sizeof(__locale_mmap_t) = %zd\n", sizeof(__locale_mmap_t)); |
| 227 | |
| 228 | return EXIT_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | /* TODO: |
| 232 | * collate data (8-bit weighted single char only) |
| 233 | * @ mappings! |
| 234 | * codeset list? yes, since we'll want to be able to inspect them... |
| 235 | * that means putting some header stuff in magic |
| 236 | * fix ctype LEN defines in gen_c8tables |
| 237 | */ |