gen_ldc.c 9.5 KB

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