gen_ldc.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 Cctype_TBL_LEN 328 */
  18. /* #define Cuplow_TBL_LEN 400 */
  19. /* #define Cc2wc_TBL_LEN 1448 */
  20. /* #define 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 WCctype_TBL_LEN (WCctype_II_LEN + WCctype_TI_LEN + WCctype_UT_LEN)
  33. #define WCuplow_TBL_LEN (WCuplow_II_LEN + WCuplow_TI_LEN + WCuplow_UT_LEN)
  34. #define WCuplow_diff_TBL_LEN (2 * WCuplow_diffs)
  35. /* #define WCcomb_TBL_LEN (WCcomb_II_LEN + WCcomb_TI_LEN + WCcomb_UT_LEN) */
  36. #include "locale_tables.h"
  37. #include "locale_mmap.h"
  38. /* #undef __PASTE2 */
  39. /* #define __PASTE2(A,B) A ## B */
  40. /* #undef __PASTE3 */
  41. /* #define __PASTE3(A,B,C) A ## B ## C */
  42. /* #define MAGIC_SIZE 64 */
  43. /* #define COMMON_MMAP(X) \ */
  44. /* unsigned char __PASTE3(lc_,X,_data)[__PASTE3(__lc_,X,_data_LEN)]; */
  45. /* #define COMMON_MMIDX(X) \ */
  46. /* unsigned char __PASTE3(lc_,X,_rows)[__PASTE3(__lc_,X,_rows_LEN)]; \ */
  47. /* uint16_t __PASTE3(lc_,X,_item_offsets)[__PASTE3(__lc_,X,_item_offsets_LEN)]; \ */
  48. /* uint16_t __PASTE3(lc_,X,_item_idx)[__PASTE3(__lc_,X,_item_idx_LEN)]; \ */
  49. /* ---------------------------------------------------------------------- */
  50. #define COMMON_OFFSETS(X) \
  51. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
  52. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
  53. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
  54. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
  55. static const size_t common_tbl_offsets[CATEGORIES*4] = {
  56. 0, 0, 0, 0, /* ctype */
  57. COMMON_OFFSETS(numeric),
  58. COMMON_OFFSETS(monetary),
  59. COMMON_OFFSETS(time),
  60. 0, 0, 0, 0, /* collate */
  61. COMMON_OFFSETS(messages),
  62. };
  63. void out_uc(FILE *f, const unsigned char *p, size_t n, char *comment)
  64. {
  65. size_t i;
  66. fprintf(f, "{\t/* %s */", comment);
  67. for (i = 0 ; i < n ; i++) {
  68. if (!(i & 7)) {
  69. fprintf(f, "\n\t");
  70. }
  71. if (p[i]) {
  72. fprintf(f, "%#04x, ", p[i]);
  73. } else {
  74. fprintf(f, "%#4x, ", p[i]);
  75. }
  76. }
  77. fprintf(f, "\n},\n");
  78. }
  79. void out_u16(FILE *f, const uint16_t *p, size_t n, char *comment)
  80. {
  81. size_t i;
  82. fprintf(f, "{\t/* %s */", comment);
  83. for (i = 0 ; i < n ; i++) {
  84. if (!(i & 7)) {
  85. fprintf(f, "\n\t");
  86. }
  87. if (p[i]) {
  88. fprintf(f, "%#06x, ", p[i]);
  89. } else {
  90. fprintf(f, "%#6x, ", p[i]);
  91. }
  92. }
  93. fprintf(f, "\n},\n");
  94. }
  95. void out_i16(FILE *f, const int16_t *p, size_t n, char *comment)
  96. {
  97. size_t i;
  98. fprintf(f, "{\t/* %s */", comment);
  99. for (i = 0 ; i < n ; i++) {
  100. if (!(i & 7)) {
  101. fprintf(f, "\n\t");
  102. }
  103. fprintf(f, "%6d, ", p[i]);
  104. }
  105. fprintf(f, "\n},\n");
  106. }
  107. void out_size_t(FILE *f, const size_t *p, size_t n, char *comment)
  108. {
  109. size_t i;
  110. fprintf(f, "{\t/* %s */", comment);
  111. for (i = 0 ; i < n ; i++) {
  112. if (!(i & 3)) {
  113. fprintf(f, "\n\t");
  114. }
  115. if (p[i]) {
  116. fprintf(f, "%#010zx, ", p[i]);
  117. } else {
  118. fprintf(f, "%#10zx, ", p[i]);
  119. }
  120. }
  121. fprintf(f, "\n},\n");
  122. }
  123. int main(void)
  124. {
  125. FILE *lso; /* static object */
  126. int i;
  127. unsigned char magic[MAGIC_SIZE];
  128. memset(magic, 0, MAGIC_SIZE);
  129. if (!(lso = fopen("locale_data.c", "w"))) {
  130. printf("can't open locale_data.c!\n");
  131. return EXIT_FAILURE;
  132. }
  133. fprintf(lso,
  134. "#include <stddef.h>\n"
  135. "#include <stdint.h>\n"
  136. /* "#define __CTYPE_HAS_8_BIT_LOCALES\n" */
  137. "#ifndef __WCHAR_ENABLED\n"
  138. "#error __WCHAR_ENABLED not defined\n"
  139. "#endif\n"
  140. "#include \"c8tables.h\"\n"
  141. "#include \"wctables.h\"\n"
  142. "#include \"lt_defines.h\"\n"
  143. "#include \"locale_mmap.h\"\n\n"
  144. "static const __locale_mmap_t locale_mmap = {\n\n"
  145. );
  146. out_uc(lso, magic, MAGIC_SIZE, "magic");
  147. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  148. out_uc(lso, Cctype_data, Cctype_TBL_LEN, "tbl8ctype");
  149. out_uc(lso, Cuplow_data, Cuplow_TBL_LEN, "tbl8uplow");
  150. #ifdef __WCHAR_ENABLED
  151. out_u16(lso, Cc2wc_data, Cc2wc_TBL_LEN, "tbl8c2wc");
  152. out_uc(lso, Cwc2c_data, Cwc2c_TBL_LEN, "tbl8wc2c");
  153. /* translit */
  154. #endif /* __WCHAR_ENABLED */
  155. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  156. #ifdef __WCHAR_ENABLED
  157. out_uc(lso, WCctype_data, WCctype_TBL_LEN, "tblwctype");
  158. out_uc(lso, WCuplow_data, WCuplow_TBL_LEN, "tblwuplow");
  159. out_i16(lso, WCuplow_diff_data, WCuplow_diff_TBL_LEN, "tblwuplow_diff");
  160. /* const unsigned char tblwcomb[WCcomb_TBL_LEN]; */
  161. /* width?? */
  162. #endif /* __WCHAR_ENABLED */
  163. out_uc(lso, __lc_numeric_data, __lc_numeric_data_LEN, "lc_numeric_data");
  164. out_uc(lso, __lc_monetary_data, __lc_monetary_data_LEN, "lc_monetary_data");
  165. out_uc(lso, __lc_time_data, __lc_time_data_LEN, "lc_time_data");
  166. /* TODO -- collate*/
  167. out_uc(lso, __lc_messages_data, __lc_messages_data_LEN, "lc_messages_data");
  168. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  169. fprintf(lso, "{ /* codeset_8_bit array */\n");
  170. for (i = 0 ; i < NUM_CODESETS ; i++) {
  171. fprintf(lso, "{ /* codeset_8_bit[%d] */\n", i);
  172. out_uc(lso, codeset_8_bit[i].idx8ctype, Cctype_IDX_LEN, "idx8ctype");
  173. out_uc(lso, codeset_8_bit[i].idx8uplow, Cuplow_IDX_LEN, "idx8uplow");
  174. out_uc(lso, codeset_8_bit[i].idx8c2wc, Cc2wc_IDX_LEN, "idx8c2wc");
  175. out_uc(lso, codeset_8_bit[i].idx8wc2c, Cwc2c_II_LEN, "idx8wc2c");
  176. fprintf(lso, "},\n");
  177. }
  178. fprintf(lso, "},\n");
  179. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  180. out_uc(lso, __lc_numeric_rows, __lc_numeric_rows_LEN, "lc_numeric_rows");
  181. out_u16(lso, __lc_numeric_item_offsets, __lc_numeric_item_offsets_LEN, "lc_numeric_item_offsets");
  182. out_u16(lso, __lc_numeric_item_idx, __lc_numeric_item_idx_LEN, "lc_numeric_item_idx");
  183. out_uc(lso, __lc_monetary_rows, __lc_monetary_rows_LEN, "lc_monetary_rows");
  184. out_u16(lso, __lc_monetary_item_offsets, __lc_monetary_item_offsets_LEN, "lc_monetary_item_offsets");
  185. out_u16(lso, __lc_monetary_item_idx, __lc_monetary_item_idx_LEN, "lc_monetary_item_idx");
  186. out_uc(lso, __lc_time_rows, __lc_time_rows_LEN, "lc_time_rows");
  187. out_u16(lso, __lc_time_item_offsets, __lc_time_item_offsets_LEN, "lc_time_item_offsets");
  188. out_u16(lso, __lc_time_item_idx, __lc_time_item_idx_LEN, "lc_time_item_idx");
  189. /* TODO -- collate*/
  190. out_uc(lso, __lc_messages_rows, __lc_messages_rows_LEN, "lc_messages_rows");
  191. out_u16(lso, __lc_messages_item_offsets, __lc_messages_item_offsets_LEN, "lc_messages_item_offsets");
  192. out_u16(lso, __lc_messages_item_idx, __lc_messages_item_idx_LEN, "lc_messages_item_idx");
  193. {
  194. unsigned char co_buf[CATEGORIES] = {
  195. 1, __lc_numeric_item_offsets_LEN, __lc_monetary_item_offsets_LEN,
  196. __lc_time_item_offsets_LEN, 0, __lc_messages_item_offsets_LEN
  197. };
  198. out_uc(lso, co_buf, CATEGORIES, "lc_common_item_offsets_LEN");
  199. }
  200. out_size_t(lso, common_tbl_offsets, CATEGORIES * 4, "lc_common_tbl_offsets");
  201. /* offsets from start of locale_mmap_t */
  202. /* rows, item_offsets, item_idx, data */
  203. #ifdef NUM_LOCALES
  204. out_uc(lso, __locales, NUM_LOCALES * WIDTH_LOCALES, "locales");
  205. out_uc(lso, __locale_names5, 5 * NUM_LOCALE_NAMES, "locale_names5");
  206. #ifdef LOCALE_AT_MODIFIERS_LENGTH
  207. out_uc(lso, __locale_at_modifiers, LOCALE_AT_MODIFIERS_LENGTH, "locale_at_modifiers");
  208. #else
  209. #error LOCALE_AT_MODIFIERS_LENGTH not defined!
  210. #endif /* LOCALE_AT_MODIFIERS_LENGTH */
  211. #endif /* NUM_LOCALES */
  212. out_uc(lso, lc_names, lc_names_LEN, "lc_names");
  213. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  214. out_uc(lso, (const unsigned char*) CODESET_LIST, sizeof(CODESET_LIST), "codeset_list");
  215. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  216. fprintf(lso,
  217. "\n};\n\n"
  218. "const __locale_mmap_t *__locale_mmap = &locale_mmap;\n\n"
  219. );
  220. if (ferror(lso) || fclose(lso)) {
  221. printf("error writing!\n");
  222. return EXIT_FAILURE;
  223. }
  224. return EXIT_SUCCESS;
  225. }
  226. /* ---------------------------------------------------------------------- */
  227. #if 0
  228. #define WRITE_COMMON_MMAP(X) \
  229. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)), SEEK_SET); \
  230. for (i=0 ; i < __PASTE3(__lc_,X,_data_LEN) ; i++) { \
  231. putc(__PASTE3(__lc_,X,_data)[i], fp); \
  232. }
  233. #define WRITE_COMMON_MMIDX(X) \
  234. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), SEEK_SET); \
  235. for (i=0 ; i < __PASTE3(__lc_,X,_rows_LEN) ; i++) { \
  236. putc(__PASTE3(__lc_,X,_rows)[i], fp); \
  237. } \
  238. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), SEEK_SET); \
  239. for (i=0 ; i < __PASTE3(__lc_,X,_item_offsets_LEN) ; i++) { \
  240. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[0], fp); \
  241. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_offsets)[i]))[1], fp); \
  242. } \
  243. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), SEEK_SET); \
  244. for (i=0 ; i < __PASTE3(__lc_,X,_item_idx_LEN) ; i++) { \
  245. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[0], fp); \
  246. putc( ((unsigned char *) &(__PASTE3(__lc_,X,_item_idx)[i]))[1], fp); \
  247. }
  248. #define WRITE_WC_DATA(X) \
  249. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
  250. for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
  251. putc(__PASTE3(WC,X,_data)[i], fp); \
  252. }
  253. #define WRITE_WC_I16_DATA(X) \
  254. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tblw,X)), SEEK_SET); \
  255. for (i=0 ; i < __PASTE3(WC,X,_TBL_LEN) ; i++) { \
  256. putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[0], fp); \
  257. putc( ((unsigned char *) &(__PASTE3(WC,X,_data)[i]))[1], fp); \
  258. }
  259. #define WRITE_C_DATA(X) \
  260. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
  261. for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
  262. putc(__PASTE3(C,X,_data)[i], fp); \
  263. }
  264. #define WRITE_C_U16_DATA(X) \
  265. fseek(fp, (long) offsetof(__locale_mmap_t, __PASTE2(tbl8,X)), SEEK_SET); \
  266. for (i=0 ; i < __PASTE3(C,X,_TBL_LEN) ; i++) { \
  267. putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[0], fp); \
  268. putc( ((unsigned char *) &(__PASTE3(C,X,_data)[i]))[1], fp); \
  269. }
  270. /**********************************************************************/
  271. #define COMMON_OFFSETS(X) \
  272. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_rows)), \
  273. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_offsets)), \
  274. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_item_idx)), \
  275. offsetof(__locale_mmap_t, __PASTE3(lc_,X,_data)) \
  276. static const size_t common_tbl_offsets[CATEGORIES*4] = {
  277. 0, 0, 0, 0, /* ctype */
  278. COMMON_OFFSETS(numeric),
  279. COMMON_OFFSETS(monetary),
  280. COMMON_OFFSETS(time),
  281. 0, 0, 0, 0, /* collate */
  282. COMMON_OFFSETS(messages),
  283. };
  284. int main(void)
  285. {
  286. FILE *fp;
  287. size_t i;
  288. unsigned char *p;
  289. if (!(fp = fopen("locale.mmap", "w"))) {
  290. printf("error - can't open locale.mmap for writing!");
  291. return EXIT_FAILURE;
  292. }
  293. for (i=0 ; i < sizeof(__locale_mmap_t) ; i++) {
  294. putc(0, fp); /* Zero out the file. */
  295. }
  296. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  297. WRITE_C_DATA(ctype);
  298. WRITE_C_DATA(uplow);
  299. #ifdef __WCHAR_ENABLED
  300. WRITE_C_U16_DATA(c2wc);
  301. WRITE_C_DATA(wc2c);
  302. /* translit */
  303. #endif /* __WCHAR_ENABLED */
  304. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  305. #ifdef __WCHAR_ENABLED
  306. WRITE_WC_DATA(ctype);
  307. WRITE_WC_DATA(uplow);
  308. WRITE_WC_I16_DATA(uplow_diff);
  309. /* WRITE_WC_DATA(comb); */
  310. /* width?? */
  311. #endif /* __WCHAR_ENABLED */
  312. WRITE_COMMON_MMAP(numeric);
  313. WRITE_COMMON_MMAP(monetary);
  314. WRITE_COMMON_MMAP(time);
  315. /* TODO -- collate*/
  316. WRITE_COMMON_MMAP(messages);
  317. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  318. fseek(fp, (long) offsetof(__locale_mmap_t, codeset_8_bit), SEEK_SET); \
  319. p = (unsigned char *) codeset_8_bit;
  320. for (i=0 ; i < sizeof(codeset_8_bit) ; i++) {
  321. putc(p[i], fp);
  322. }
  323. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  324. WRITE_COMMON_MMIDX(numeric);
  325. WRITE_COMMON_MMIDX(monetary);
  326. WRITE_COMMON_MMIDX(time);
  327. /* TODO -- collate*/
  328. WRITE_COMMON_MMIDX(messages);
  329. fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_item_offsets_LEN), SEEK_SET);
  330. putc(1, fp); /* ctype -- (codeset) handled specially */
  331. putc(__lc_numeric_item_offsets_LEN, fp);
  332. putc(__lc_monetary_item_offsets_LEN, fp);
  333. putc(__lc_time_item_offsets_LEN, fp);
  334. putc(0, fp); /* collate */
  335. putc(__lc_messages_item_offsets_LEN, fp);
  336. fseek(fp, (long) offsetof(__locale_mmap_t, lc_common_tbl_offsets), SEEK_SET);
  337. for (i=0 ; i < sizeof(common_tbl_offsets) ; i++) {
  338. putc(((unsigned char *)common_tbl_offsets)[i], fp);
  339. }
  340. #ifdef NUM_LOCALES
  341. fseek(fp, (long) offsetof(__locale_mmap_t, locales), SEEK_SET);
  342. for (i=0 ; i < (NUM_LOCALES * WIDTH_LOCALES) ; i++) {
  343. putc(__locales[i], fp);
  344. }
  345. fseek(fp, (long) offsetof(__locale_mmap_t, locale_names5), SEEK_SET);
  346. for (i=0 ; i < 5 * NUM_LOCALE_NAMES ; i++) {
  347. putc(__locale_names5[i], fp);
  348. }
  349. #ifdef LOCALE_AT_MODIFIERS_LENGTH
  350. fseek(fp, (long) offsetof(__locale_mmap_t, locale_at_modifiers), SEEK_SET);
  351. for (i=0 ; i < LOCALE_AT_MODIFIERS_LENGTH ; i++) {
  352. putc(__locale_at_modifiers[i], fp);
  353. }
  354. #endif /* LOCALE_AT_MODIFIERS_LENGTH */
  355. #endif /* NUM_LOCALES */
  356. fseek(fp, (long) offsetof(__locale_mmap_t, lc_names), SEEK_SET);
  357. for (i=0 ; i < lc_names_LEN ; i++) {
  358. putc(lc_names[i], fp);
  359. }
  360. #ifdef __CTYPE_HAS_8_BIT_LOCALES
  361. fseek(fp, (long) offsetof(__locale_mmap_t, codeset_list), SEEK_SET);
  362. for (i=0 ; i < sizeof(CODESET_LIST) ; i++) {
  363. putc((unsigned char)(CODESET_LIST[i]), fp);
  364. }
  365. #endif /* __CTYPE_HAS_8_BIT_LOCALES */
  366. if (ferror(fp) || (fclose(fp) == EOF)) {
  367. printf("error - stream in error state or fclose failed!");
  368. return EXIT_FAILURE;
  369. }
  370. printf("sizeof(__locale_mmap_t) = %zd\n", sizeof(__locale_mmap_t));
  371. return EXIT_SUCCESS;
  372. }
  373. #endif
  374. /* TODO:
  375. * collate data (8-bit weighted single char only)
  376. * @ mappings!
  377. * codeset list? yes, since we'll want to be able to inspect them...
  378. * that means putting some header stuff in magic
  379. * fix ctype LEN defines in gen_c8tables
  380. */