gen_ldc.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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_i32(FILE *f, const int32_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 & 7)) {
  114. fprintf(f, "\n\t");
  115. }
  116. fprintf(f, "%11d, ", p[i]);
  117. }
  118. fprintf(f, "\n},\n");
  119. }
  120. void out_size_t(FILE *f, const size_t *p, size_t n, char *comment)
  121. {
  122. size_t i;
  123. fprintf(f, "{\t/* %s */", comment);
  124. for (i = 0 ; i < n ; i++) {
  125. if (!(i & 3)) {
  126. fprintf(f, "\n\t");
  127. }
  128. if (p[i]) {
  129. fprintf(f, "%#010zx, ", p[i]);
  130. } else {
  131. fprintf(f, "%#10zx, ", p[i]);
  132. }
  133. }
  134. fprintf(f, "\n},\n");
  135. }
  136. int main(int argc, char **argv)
  137. {
  138. char *output_file = "locale_data.c";
  139. FILE *lso; /* static object */
  140. int i;
  141. #ifdef __LOCALE_DATA_MAGIC_SIZE
  142. unsigned char magic[__LOCALE_DATA_MAGIC_SIZE];
  143. memset(magic, 0, __LOCALE_DATA_MAGIC_SIZE);
  144. #endif /* __LOCALE_DATA_MAGIC_SIZE */
  145. if (argc == 2)
  146. output_file = argv[1];
  147. if (!(lso = fopen(output_file, "w"))) {
  148. printf("cannot open output file '%s'!\n", output_file);
  149. return EXIT_FAILURE;
  150. }
  151. fprintf(lso,
  152. "#include <stddef.h>\n"
  153. "#include <stdint.h>\n"
  154. /* "#define __CTYPE_HAS_8_BIT_LOCALES\n" */
  155. "#ifndef __WCHAR_ENABLED\n"
  156. "#error __WCHAR_ENABLED not defined\n"
  157. "#endif\n"
  158. "#include \"c8tables.h\"\n"
  159. "#include \"wctables.h\"\n"
  160. "#include \"lt_defines.h\"\n"
  161. "#include \"locale_mmap.h\"\n\n"
  162. "static const __locale_mmap_t locale_mmap = {\n\n"
  163. );
  164. #ifdef __LOCALE_DATA_MAGIC_SIZE
  165. out_uc(lso, magic, __LOCALE_DATA_MAGIC_SIZE, "magic");
  166. #endif /* __LOCALE_DATA_MAGIC_SIZE */
  167. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  168. out_uc(lso, __LOCALE_DATA_Cctype_data, __LOCALE_DATA_Cctype_TBL_LEN, "tbl8ctype");
  169. out_uc(lso, __LOCALE_DATA_Cuplow_data, __LOCALE_DATA_Cuplow_TBL_LEN, "tbl8uplow");
  170. #ifdef __WCHAR_ENABLED
  171. out_u16(lso, __LOCALE_DATA_Cc2wc_data, __LOCALE_DATA_Cc2wc_TBL_LEN, "tbl8c2wc");
  172. out_uc(lso, __LOCALE_DATA_Cwc2c_data, __LOCALE_DATA_Cwc2c_TBL_LEN, "tbl8wc2c");
  173. /* translit */
  174. #endif /* __WCHAR_ENABLED */
  175. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  176. #ifdef __WCHAR_ENABLED
  177. out_uc(lso, __LOCALE_DATA_WCctype_data, __LOCALE_DATA_WCctype_TBL_LEN, "tblwctype");
  178. out_uc(lso, __LOCALE_DATA_WCuplow_data, __LOCALE_DATA_WCuplow_TBL_LEN, "tblwuplow");
  179. out_i32(lso, __LOCALE_DATA_WCuplow_diff_data, __LOCALE_DATA_WCuplow_diff_TBL_LEN, "tblwuplow_diff");
  180. /* const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
  181. /* width?? */
  182. #endif /* __WCHAR_ENABLED */
  183. out_uc(lso, __lc_ctype_data, __lc_ctype_data_LEN, "lc_ctype_data");
  184. out_uc(lso, __lc_numeric_data, __lc_numeric_data_LEN, "lc_numeric_data");
  185. out_uc(lso, __lc_monetary_data, __lc_monetary_data_LEN, "lc_monetary_data");
  186. out_uc(lso, __lc_time_data, __lc_time_data_LEN, "lc_time_data");
  187. /* TODO -- collate*/
  188. out_uc(lso, __lc_messages_data, __lc_messages_data_LEN, "lc_messages_data");
  189. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  190. fprintf(lso, "{ /* codeset_8_bit array */\n");
  191. for (i = 0 ; i < __LOCALE_DATA_NUM_CODESETS ; i++) {
  192. fprintf(lso, "{ /* codeset_8_bit[%d] */\n", i);
  193. out_uc(lso, codeset_8_bit[i].idx8ctype, __LOCALE_DATA_Cctype_IDX_LEN, "idx8ctype");
  194. out_uc(lso, codeset_8_bit[i].idx8uplow, __LOCALE_DATA_Cuplow_IDX_LEN, "idx8uplow");
  195. out_uc(lso, codeset_8_bit[i].idx8c2wc, __LOCALE_DATA_Cc2wc_IDX_LEN, "idx8c2wc");
  196. out_uc(lso, codeset_8_bit[i].idx8wc2c, __LOCALE_DATA_Cwc2c_II_LEN, "idx8wc2c");
  197. fprintf(lso, "},\n");
  198. }
  199. fprintf(lso, "},\n");
  200. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  201. out_uc(lso, __lc_ctype_rows, __lc_ctype_rows_LEN, "lc_ctype_rows");
  202. out_u16(lso, __lc_ctype_item_offsets, __lc_ctype_item_offsets_LEN, "lc_ctype_item_offsets");
  203. out_u16(lso, __lc_ctype_item_idx, __lc_ctype_item_idx_LEN, "lc_ctype_item_idx");
  204. out_uc(lso, __lc_numeric_rows, __lc_numeric_rows_LEN, "lc_numeric_rows");
  205. out_u16(lso, __lc_numeric_item_offsets, __lc_numeric_item_offsets_LEN, "lc_numeric_item_offsets");
  206. out_u16(lso, __lc_numeric_item_idx, __lc_numeric_item_idx_LEN, "lc_numeric_item_idx");
  207. out_uc(lso, __lc_monetary_rows, __lc_monetary_rows_LEN, "lc_monetary_rows");
  208. out_u16(lso, __lc_monetary_item_offsets, __lc_monetary_item_offsets_LEN, "lc_monetary_item_offsets");
  209. out_u16(lso, __lc_monetary_item_idx, __lc_monetary_item_idx_LEN, "lc_monetary_item_idx");
  210. out_uc(lso, __lc_time_rows, __lc_time_rows_LEN, "lc_time_rows");
  211. out_u16(lso, __lc_time_item_offsets, __lc_time_item_offsets_LEN, "lc_time_item_offsets");
  212. out_u16(lso, __lc_time_item_idx, __lc_time_item_idx_LEN, "lc_time_item_idx");
  213. out_uc(lso, __lc_messages_rows, __lc_messages_rows_LEN, "lc_messages_rows");
  214. out_u16(lso, __lc_messages_item_offsets, __lc_messages_item_offsets_LEN, "lc_messages_item_offsets");
  215. out_u16(lso, __lc_messages_item_idx, __lc_messages_item_idx_LEN, "lc_messages_item_idx");
  216. /* collate should be last*/
  217. assert(sizeof(__locale_collate_tbl)/sizeof(__locale_collate_tbl[0]) == __lc_collate_data_LEN) ;
  218. out_u16(lso, __locale_collate_tbl, __lc_collate_data_LEN, "collate_data");
  219. {
  220. unsigned char co_buf[__LOCALE_DATA_CATEGORIES] = {
  221. __lc_ctype_item_offsets_LEN,
  222. __lc_numeric_item_offsets_LEN,
  223. __lc_monetary_item_offsets_LEN,
  224. __lc_time_item_offsets_LEN,
  225. 0,
  226. __lc_messages_item_offsets_LEN
  227. };
  228. out_uc(lso, co_buf, __LOCALE_DATA_CATEGORIES, "lc_common_item_offsets_LEN");
  229. }
  230. out_size_t(lso, common_tbl_offsets, __LOCALE_DATA_CATEGORIES * 4, "lc_common_tbl_offsets");
  231. /* offsets from start of locale_mmap_t */
  232. /* rows, item_offsets, item_idx, data */
  233. #ifdef __LOCALE_DATA_NUM_LOCALES
  234. out_uc(lso, __locales, __LOCALE_DATA_NUM_LOCALES * __LOCALE_DATA_WIDTH_LOCALES, "locales");
  235. out_uc(lso, __locale_names5, 5 * __LOCALE_DATA_NUM_LOCALE_NAMES, "locale_names5");
  236. #ifdef __LOCALE_DATA_AT_MODIFIERS_LENGTH
  237. out_uc(lso, __locale_at_modifiers, __LOCALE_DATA_AT_MODIFIERS_LENGTH, "locale_at_modifiers");
  238. #else
  239. #error __LOCALE_DATA_AT_MODIFIERS_LENGTH not defined!
  240. #endif /* __LOCALE_DATA_AT_MODIFIERS_LENGTH */
  241. #endif /* __LOCALE_DATA_NUM_LOCALES */
  242. out_uc(lso, lc_names, __lc_names_LEN, "lc_names");
  243. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  244. out_uc(lso, (const unsigned char*) __LOCALE_DATA_CODESET_LIST, sizeof(__LOCALE_DATA_CODESET_LIST), "codeset_list");
  245. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  246. fprintf(lso,
  247. "\n};\n\n"
  248. "const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n"
  249. );
  250. if (ferror(lso) || fclose(lso)) {
  251. printf("error writing!\n");
  252. return EXIT_FAILURE;
  253. }
  254. return EXIT_SUCCESS;
  255. }
  256. /* ---------------------------------------------------------------------- */
  257. /* TODO:
  258. * collate data (8-bit weighted single char only)
  259. * @ mappings!
  260. * codeset list? yes, since we'll want to be able to inspect them...
  261. * that means putting some header stuff in magic
  262. * fix ctype LEN defines in gen_c8tables
  263. */