gen_locale.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <limits.h>
  6. #include <assert.h>
  7. #include <locale.h>
  8. #include <langinfo.h>
  9. #include <nl_types.h>
  10. #include <stdint.h>
  11. #include "c8tables.h"
  12. #define __LOCALE_DATA_CATEGORIES 6
  13. /* must agree with ordering of gen_mmap! */
  14. static const unsigned char *lc_names[] = {
  15. "LC_CTYPE",
  16. "LC_NUMERIC",
  17. "LC_MONETARY",
  18. "LC_TIME",
  19. "LC_COLLATE",
  20. "LC_MESSAGES",
  21. #if __LOCALE_DATA_CATEGORIES == 12
  22. "LC_PAPER",
  23. "LC_NAME",
  24. "LC_ADDRESS",
  25. "LC_TELEPHONE",
  26. "LC_MEASUREMENT",
  27. "LC_IDENTIFICATION",
  28. #elif __LOCALE_DATA_CATEGORIES != 6
  29. #error unsupported __LOCALE_DATA_CATEGORIES value!
  30. #endif
  31. };
  32. typedef struct {
  33. char *glibc_name;
  34. char name[5];
  35. char dot_cs; /* 0 if no codeset specified */
  36. char cs;
  37. unsigned char idx_name;
  38. unsigned char lc_time_row;
  39. unsigned char lc_numeric_row;
  40. unsigned char lc_monetary_row;
  41. unsigned char lc_messages_row;
  42. unsigned char lc_ctype_row;
  43. #if __LOCALE_DATA_CATEGORIES != 6
  44. #error unsupported __LOCALE_DATA_CATEGORIES value
  45. #endif
  46. } locale_entry;
  47. static void read_at_mappings(void);
  48. static void read_enable_disable(void);
  49. static void read_locale_list(void);
  50. static int find_codeset_num(const char *cs);
  51. static int find_at_string_num(const char *as);
  52. static int le_cmp(const void *, const void *);
  53. static void dump_table8(const char *name, const char *tbl, int len);
  54. static void dump_table8c(const char *name, const char *tbl, int len);
  55. static void dump_table16(const char *name, const int *tbl, int len);
  56. static void do_lc_time(void);
  57. static void do_lc_numeric(void);
  58. static void do_lc_monetary(void);
  59. static void do_lc_messages(void);
  60. static void do_lc_ctype(void);
  61. static FILE *fp;
  62. static FILE *ofp;
  63. static char line_buf[80];
  64. static char at_mappings[256];
  65. static char at_mapto[256];
  66. static char at_strings[1024];
  67. static char *at_strings_end;
  68. static locale_entry locales[700];
  69. static char glibc_locale_names[60000];
  70. static int num_locales;
  71. static int default_utf8;
  72. static int default_8bit;
  73. static int total_size;
  74. static int null_count;
  75. static void do_locale_names(void)
  76. {
  77. /* "C" locale name is handled specially by the setlocale code. */
  78. int uniq = 0;
  79. int i;
  80. if (num_locales <= 1) {
  81. /* printf("error - only C locale?\n"); */
  82. /* exit(EXIT_FAILURE); */
  83. fprintf(ofp, "static const unsigned char __locales[%d];\n", (3 + __LOCALE_DATA_CATEGORIES));
  84. fprintf(ofp, "static const unsigned char __locale_names5[5];\n");
  85. } else {
  86. if (default_utf8) {
  87. fprintf(ofp, "#define __CTYPE_HAS_UTF_8_LOCALES\t\t\t1\n");
  88. }
  89. fprintf(ofp, "#define __LOCALE_DATA_CATEGORIES\t\t\t%d\n", __LOCALE_DATA_CATEGORIES);
  90. fprintf(ofp, "#define __LOCALE_DATA_WIDTH_LOCALES\t\t\t%d\n", 3+__LOCALE_DATA_CATEGORIES);
  91. fprintf(ofp, "#define __LOCALE_DATA_NUM_LOCALES\t\t\t%d\n", num_locales);
  92. fprintf(ofp, "static const unsigned char __locales[%d] = {\n",
  93. (num_locales) * (3 + __LOCALE_DATA_CATEGORIES));
  94. for (i=0 ; i < num_locales ; i++) {
  95. if (memcmp(locales[i].name, locales[i-1].name, 5) != 0) {
  96. locales[i].idx_name = uniq;
  97. ++uniq;
  98. } else {
  99. locales[i].idx_name = uniq - 1;
  100. }
  101. fprintf(ofp, "\t%#4x, ", (int)((unsigned char) locales[i].idx_name));
  102. fprintf(ofp, "\t%#4x, ", (int)((unsigned char) locales[i].dot_cs));
  103. fprintf(ofp, "\t%#4x, ", (int)((unsigned char) locales[i].cs));
  104. /* lc_ctype would store translit flags and turkish up/low flag. */
  105. fprintf(ofp, "%#4x, ", (int)((unsigned char) locales[i].lc_ctype_row));
  106. fprintf(ofp, "%#4x, ", (int)((unsigned char) locales[i].lc_numeric_row));
  107. fprintf(ofp, "%#4x, ", (int)((unsigned char) locales[i].lc_monetary_row));
  108. fprintf(ofp, "%#4x, ", (int)((unsigned char) locales[i].lc_time_row));
  109. #if 1
  110. /* lc_collate */
  111. if (strlen(locales[i].glibc_name) >= 5) {
  112. fprintf(ofp, "COL_IDX_%.2s_%.2s, ", locales[i].glibc_name, locales[i].glibc_name+3);
  113. } else if (!strcmp(locales[i].glibc_name, "C")) {
  114. fprintf(ofp, "COL_IDX_C , ");
  115. } else {
  116. printf("don't know how to handle COL_IDX_ for %s\n", locales[i].glibc_name);
  117. exit(EXIT_FAILURE);
  118. }
  119. #else
  120. fprintf(ofp, "%#4x, ", 0); /* place holder for lc_collate */
  121. #endif
  122. fprintf(ofp, "%#4x, ", (int)((unsigned char) locales[i].lc_messages_row));
  123. fprintf(ofp, "\t/* %s */\n", locales[i].glibc_name);
  124. }
  125. fprintf(ofp, "};\n\n");
  126. fprintf(ofp, "#define __LOCALE_DATA_NUM_LOCALE_NAMES\t\t%d\n", uniq );
  127. fprintf(ofp, "static const unsigned char __locale_names5[%d] = \n\t", uniq * 5);
  128. uniq = 0;
  129. for (i=1 ; i < num_locales ; i++) {
  130. if (memcmp(locales[i].name, locales[i-1].name, 5) != 0) {
  131. fprintf(ofp, "\"%5.5s\" ", locales[i].name);
  132. ++uniq;
  133. if ((uniq % 8) == 0) {
  134. fprintf(ofp, "\n\t");
  135. }
  136. }
  137. }
  138. fprintf(ofp,";\n\n");
  139. if (at_strings_end > at_strings) {
  140. int i, j;
  141. char *p;
  142. i = 0;
  143. p = at_strings;
  144. while (*p) {
  145. ++i;
  146. p += 1 + (unsigned char) *p;
  147. }
  148. /* len, char, string\0 */
  149. fprintf(ofp, "#define __LOCALE_DATA_AT_MODIFIERS_LENGTH\t\t%d\n",
  150. i + (at_strings_end - at_strings));
  151. fprintf(ofp, "static const unsigned char __locale_at_modifiers[%d] = {",
  152. i + (at_strings_end - at_strings));
  153. i = 0;
  154. p = at_strings;
  155. while (*p) {
  156. fprintf(ofp, "\n\t%4d, '%c',",
  157. (unsigned char) *p, /* len of string\0 */
  158. at_mapto[i]);
  159. for (j=1 ; j < ((unsigned char) *p) ; j++) {
  160. fprintf(ofp, " '%c',", p[j]);
  161. }
  162. fprintf(ofp, " 0,");
  163. ++i;
  164. p += 1 + (unsigned char) *p;
  165. }
  166. fprintf(ofp, "\n};\n\n");
  167. }
  168. {
  169. int pos[__LOCALE_DATA_CATEGORIES];
  170. pos[0] = __LOCALE_DATA_CATEGORIES;
  171. for (i=0 ; i < __LOCALE_DATA_CATEGORIES ; i++) {
  172. fprintf(ofp, "#define __%s\t\t%d\n", lc_names[i], i);
  173. if (i + 1 < __LOCALE_DATA_CATEGORIES) {
  174. pos[i+1] = 1 + strlen(lc_names[i]) + pos[i];
  175. }
  176. }
  177. if (pos[__LOCALE_DATA_CATEGORIES-1] > 255) {
  178. printf("error - lc_names is too big (%d)\n", pos[__LOCALE_DATA_CATEGORIES-1]);
  179. exit(EXIT_FAILURE);
  180. }
  181. fprintf(ofp, "#define __LC_ALL\t\t%d\n\n", i);
  182. fprintf(ofp, "#define __lc_names_LEN\t\t%d\n",
  183. pos[__LOCALE_DATA_CATEGORIES-1] + strlen(lc_names[__LOCALE_DATA_CATEGORIES-1]) + 1);
  184. total_size += pos[__LOCALE_DATA_CATEGORIES-1] + strlen(lc_names[__LOCALE_DATA_CATEGORIES-1]) + 1;
  185. fprintf(ofp, "static unsigned const char lc_names[%d] =\n",
  186. pos[__LOCALE_DATA_CATEGORIES-1] + strlen(lc_names[__LOCALE_DATA_CATEGORIES-1]) + 1);
  187. fprintf(ofp, "\t\"");
  188. for (i=0 ; i < __LOCALE_DATA_CATEGORIES ; i++) {
  189. fprintf(ofp, "\\x%02x", (unsigned char) pos[i]);
  190. }
  191. fprintf(ofp, "\"");
  192. for (i=0 ; i < __LOCALE_DATA_CATEGORIES ; i++) {
  193. fprintf(ofp, "\n\t\"%s\\0\"", lc_names[i]);
  194. }
  195. fprintf(ofp, ";\n\n");
  196. }
  197. printf("locale data = %d name data = %d for %d uniq\n",
  198. num_locales * (3 + __LOCALE_DATA_CATEGORIES), uniq * 5, uniq);
  199. total_size += num_locales * (3 + __LOCALE_DATA_CATEGORIES) + uniq * 5;
  200. }
  201. }
  202. static void read_at_mappings(void)
  203. {
  204. char *p;
  205. char *m;
  206. int mc = 0;
  207. do {
  208. if (!(p = strtok(line_buf, " \t\n")) || (*p == '#')) {
  209. if (!fgets(line_buf, sizeof(line_buf), fp)) {
  210. if (ferror(fp)) {
  211. printf("error reading file\n");
  212. exit(EXIT_FAILURE);
  213. }
  214. return; /* EOF */
  215. }
  216. if ((*line_buf == '#') && (line_buf[1] == '-')) {
  217. break;
  218. }
  219. continue;
  220. }
  221. if (*p == '@') {
  222. if (p[1] == 0) {
  223. printf("error: missing @modifier name\n");
  224. exit(EXIT_FAILURE);
  225. }
  226. m = p; /* save the modifier name */
  227. if (!(p = strtok(NULL, " \t\n")) || p[1] || (((unsigned char) *p) > 0x7f)) {
  228. printf("error: missing or illegal @modifier mapping char\n");
  229. exit(EXIT_FAILURE);
  230. }
  231. if (at_mappings[(int)((unsigned char) *p)]) {
  232. printf("error: reused @modifier mapping char\n");
  233. exit(EXIT_FAILURE);
  234. }
  235. at_mappings[(int)((unsigned char) *p)] = 1;
  236. at_mapto[mc] = *p;
  237. ++mc;
  238. *at_strings_end = (char)( (unsigned char) (strlen(m)) );
  239. strcpy(++at_strings_end, m+1);
  240. at_strings_end += (unsigned char) at_strings_end[-1];
  241. printf("@mapping: \"%s\" to '%c'\n", m, *p);
  242. if (((p = strtok(NULL, " \t\n")) != NULL) && (*p != '#')) {
  243. printf("ignoring trailing text: %s...\n", p);
  244. }
  245. *line_buf = 0;
  246. continue;
  247. }
  248. break;
  249. } while (1);
  250. #if 0
  251. {
  252. p = at_strings;
  253. if (!*p) {
  254. printf("no @ strings\n");
  255. return;
  256. }
  257. do {
  258. printf("%s\n", p+1);
  259. p += 1 + (unsigned char) *p;
  260. } while (*p);
  261. }
  262. #endif
  263. }
  264. static void read_enable_disable(void)
  265. {
  266. char *p;
  267. do {
  268. if (!(p = strtok(line_buf, " =\t\n")) || (*p == '#')) {
  269. if (!fgets(line_buf, sizeof(line_buf), fp)) {
  270. if (ferror(fp)) {
  271. printf("error reading file\n");
  272. exit(EXIT_FAILURE);
  273. }
  274. return; /* EOF */
  275. }
  276. if ((*line_buf == '#') && (line_buf[1] == '-')) {
  277. break;
  278. }
  279. continue;
  280. }
  281. if (!strcmp(p, "UTF-8")) {
  282. if (!(p = strtok(NULL, " =\t\n"))
  283. || ((toupper(*p) != 'Y') && (toupper(*p) != 'N'))) {
  284. printf("error: missing or illegal UTF-8 setting\n");
  285. exit(EXIT_FAILURE);
  286. }
  287. default_utf8 = (toupper(*p) == 'Y');
  288. printf("UTF-8 locales are %sabled\n", "dis\0en"+ (default_utf8 << 2));
  289. } else if (!strcmp(p, "8-BIT")) {
  290. if (!(p = strtok(NULL, " =\t\n"))
  291. || ((toupper(*p) != 'Y') && (toupper(*p) != 'N'))) {
  292. printf("error: missing or illegal 8-BIT setting\n");
  293. exit(EXIT_FAILURE);
  294. }
  295. default_8bit = (toupper(*p) == 'Y');
  296. printf("8-BIT locales are %sabled\n", "dis\0en" + (default_8bit << 2));
  297. } else {
  298. break;
  299. }
  300. if (((p = strtok(NULL, " \t\n")) != NULL) && (*p != '#')) {
  301. printf("ignoring trailing text: %s...\n", p);
  302. }
  303. *line_buf = 0;
  304. continue;
  305. } while (1);
  306. }
  307. #ifdef __LOCALE_DATA_CODESET_LIST
  308. static int find_codeset_num(const char *cs)
  309. {
  310. int r = 2;
  311. char *s = __LOCALE_DATA_CODESET_LIST;
  312. /* 7-bit is 1, UTF-8 is 2, 8-bits are > 2 */
  313. if (strcmp(cs, "UTF-8") != 0) {
  314. ++r;
  315. while (*s && strcmp(__LOCALE_DATA_CODESET_LIST+ ((unsigned char) *s), cs)) {
  316. /* printf("tried %s\n", __LOCALE_DATA_CODESET_LIST + ((unsigned char) *s)); */
  317. ++r;
  318. ++s;
  319. }
  320. if (!*s) {
  321. printf("error: unsupported codeset %s\n", cs);
  322. exit(EXIT_FAILURE);
  323. }
  324. }
  325. return r;
  326. }
  327. #else
  328. static int find_codeset_num(const char *cs)
  329. {
  330. int r = 2;
  331. /* 7-bit is 1, UTF-8 is 2, 8-bits are > 2 */
  332. if (strcmp(cs, "UTF-8") != 0) {
  333. printf("error: unsupported codeset %s\n", cs);
  334. exit(EXIT_FAILURE);
  335. }
  336. return r;
  337. }
  338. #endif
  339. static int find_at_string_num(const char *as)
  340. {
  341. int i = 0;
  342. char *p = at_strings;
  343. while (*p) {
  344. if (!strcmp(p+1, as)) {
  345. return i;
  346. }
  347. ++i;
  348. p += 1 + (unsigned char) *p;
  349. }
  350. printf("error: unmapped @string %s\n", as);
  351. exit(EXIT_FAILURE);
  352. }
  353. static void read_locale_list(void)
  354. {
  355. char *p;
  356. char *s;
  357. char *ln; /* locale name */
  358. char *ls; /* locale name ll_CC */
  359. char *as; /* at string */
  360. char *ds; /* dot string */
  361. char *cs; /* codeset */
  362. int i;
  363. typedef struct {
  364. char *glibc_name;
  365. char name[5];
  366. char dot_cs; /* 0 if no codeset specified */
  367. char cs;
  368. } locale_entry;
  369. /* First the C locale. */
  370. locales[0].glibc_name = locales[0].name;
  371. strncpy(locales[0].name,"C",5);
  372. locales[0].dot_cs = 0;
  373. locales[0].cs = 1; /* 7-bit encoding */
  374. ++num_locales;
  375. do {
  376. if (!(p = strtok(line_buf, " \t\n")) || (*p == '#')) {
  377. if (!fgets(line_buf, sizeof(line_buf), fp)) {
  378. if (ferror(fp)) {
  379. printf("error reading file\n");
  380. exit(EXIT_FAILURE);
  381. }
  382. return; /* EOF */
  383. }
  384. if ((*line_buf == '#') && (line_buf[1] == '-')) {
  385. break;
  386. }
  387. continue;
  388. }
  389. s = glibc_locale_names;
  390. for (i=0 ; i < num_locales ; i++) {
  391. if (!strcmp(s+1, p)) {
  392. break;
  393. }
  394. s += 1 + ((unsigned char) *s);
  395. }
  396. if (i < num_locales) {
  397. printf("ignoring dulplicate locale name: %s", p);
  398. *line_buf = 0;
  399. continue;
  400. }
  401. /* New locale, but don't increment num until codeset verified! */
  402. *s = (char)((unsigned char) (strlen(p) + 1));
  403. strcpy(s+1, p);
  404. locales[num_locales].glibc_name = s+1;
  405. ln = p; /* save locale name */
  406. if (!(p = strtok(NULL, " \t\n"))) {
  407. printf("error: missing codeset for locale %s\n", ln);
  408. exit(EXIT_FAILURE);
  409. }
  410. cs = p;
  411. i = find_codeset_num(p);
  412. if ((i == 2) && !default_utf8) {
  413. printf("ignoring UTF-8 locale %s\n", ln);
  414. *line_buf = 0;
  415. continue;
  416. } else if ((i > 2) && !default_8bit) {
  417. printf("ignoring 8-bit codeset locale %s\n", ln);
  418. *line_buf = 0;
  419. continue;
  420. }
  421. locales[num_locales].cs = (char)((unsigned char) i);
  422. if (((p = strtok(NULL, " \t\n")) != NULL) && (*p != '#')) {
  423. printf("ignoring trailing text: %s...\n", p);
  424. }
  425. /* Now go back to locale string for .codeset and @modifier */
  426. as = strtok(ln, "@");
  427. if (as) {
  428. as = strtok(NULL, "@");
  429. }
  430. ds = strtok(ln, ".");
  431. if (ds) {
  432. ds = strtok(NULL, ".");
  433. }
  434. ls = ln;
  435. if ((strlen(ls) != 5) || (ls[2] != '_')) {
  436. printf("error: illegal locale name %s\n", ls);
  437. exit(EXIT_FAILURE);
  438. }
  439. i = 0; /* value for unspecified codeset */
  440. if (ds) {
  441. i = find_codeset_num(ds);
  442. if ((i == 2) && !default_utf8) {
  443. printf("ignoring UTF-8 locale %s\n", ln);
  444. *line_buf = 0;
  445. continue;
  446. } else if ((i > 2) && !default_8bit) {
  447. printf("ignoring 8-bit codeset locale %s\n", ln);
  448. *line_buf = 0;
  449. continue;
  450. }
  451. }
  452. locales[num_locales].dot_cs = (char)((unsigned char) i);
  453. if (as) {
  454. i = find_at_string_num(as);
  455. ls[2] = at_mapto[i];
  456. }
  457. memcpy(locales[num_locales].name, ls, 5);
  458. /* printf("locale: %5.5s %2d %2d %s\n", */
  459. /* locales[num_locales].name, */
  460. /* locales[num_locales].cs, */
  461. /* locales[num_locales].dot_cs, */
  462. /* locales[num_locales].glibc_name */
  463. /* ); */
  464. ++num_locales;
  465. *line_buf = 0;
  466. } while (1);
  467. }
  468. static int le_cmp(const void *a, const void *b)
  469. {
  470. const locale_entry *p;
  471. const locale_entry *q;
  472. int r;
  473. p = (const locale_entry *) a;
  474. q = (const locale_entry *) b;
  475. if (!(r = p->name[0] - q->name[0])
  476. && !(r = p->name[1] - q->name[1])
  477. && !(r = p->name[3] - q->name[3])
  478. && !(r = p->name[4] - q->name[4])
  479. && !(r = p->name[2] - q->name[2])
  480. && !(r = -(p->cs - q->cs))
  481. ) {
  482. r = -(p->dot_cs - q->dot_cs);
  483. /* Reverse the ordering of the codesets so UTF-8 comes last.
  484. * Work-around (hopefully) for glibc bug affecting at least
  485. * the euro currency symbol. */
  486. }
  487. return r;
  488. }
  489. int main(int argc, char **argv)
  490. {
  491. char *output_file = "locale_tables.h";
  492. if ((argc < 2 || argc > 3) || (!(fp = fopen(*++argv, "r")))) {
  493. printf("error: missing filename or file!\n");
  494. return EXIT_FAILURE;
  495. }
  496. at_strings_end = at_strings;
  497. read_at_mappings();
  498. read_enable_disable();
  499. read_locale_list();
  500. fclose(fp);
  501. /* handle C locale specially */
  502. qsort(locales+1, num_locales-1, sizeof(locale_entry), le_cmp);
  503. #if 0
  504. for (i=0 ; i < num_locales ; i++) {
  505. printf("locale: %5.5s %2d %2d %s\n",
  506. locales[i].name,
  507. locales[i].cs,
  508. locales[i].dot_cs,
  509. locales[i].glibc_name
  510. );
  511. }
  512. #endif
  513. if (argc == 3)
  514. output_file = *++argv;
  515. if (output_file == NULL || !(ofp = fopen(output_file, "w"))) {
  516. printf("cannot open output file '%s'!\n",
  517. output_file);
  518. return EXIT_FAILURE;
  519. }
  520. do_lc_time();
  521. do_lc_numeric();
  522. do_lc_monetary();
  523. do_lc_messages();
  524. do_lc_ctype();
  525. do_locale_names();
  526. fclose(ofp);
  527. printf("total data size = %d\n", total_size);
  528. printf("null count = %d\n", null_count);
  529. return EXIT_SUCCESS;
  530. }
  531. static char *idx[10000];
  532. static char buf[100000];
  533. static char *last;
  534. static int uniq;
  535. static int addblock(const char *s, size_t n) /* l includes nul terminator */
  536. {
  537. int j;
  538. if (!s) {
  539. ++null_count;
  540. return 0;
  541. }
  542. for (j=0 ; (j < uniq) && (idx[j] + n < last) ; j++) {
  543. if (!memcmp(s, idx[j], n)) {
  544. return idx[j] - buf;
  545. }
  546. }
  547. if (uniq >= sizeof(idx)) {
  548. printf("too many uniq strings!\n");
  549. exit(EXIT_FAILURE);
  550. }
  551. if (last + n >= buf + sizeof(buf)) {
  552. printf("need to increase size of buf!\n");
  553. exit(EXIT_FAILURE);
  554. }
  555. idx[uniq] = last;
  556. ++uniq;
  557. memcpy(last, s, n);
  558. last += n;
  559. return idx[uniq - 1] - buf;
  560. }
  561. static int addstring(const char *s)
  562. {
  563. int j;
  564. size_t l;
  565. if (!s) {
  566. ++null_count;
  567. return 0;
  568. }
  569. for (j=0 ; j < uniq ; j++) {
  570. if (!strcmp(s, idx[j])) {
  571. return idx[j] - buf;
  572. }
  573. }
  574. if (uniq >= sizeof(idx)) {
  575. printf("too many uniq strings!\n");
  576. exit(EXIT_FAILURE);
  577. }
  578. l = strlen(s) + 1;
  579. if (last + l >= buf + sizeof(buf)) {
  580. printf("need to increase size of buf!\n");
  581. exit(EXIT_FAILURE);
  582. }
  583. idx[uniq] = last;
  584. ++uniq;
  585. strcpy(last, s);
  586. last += l;
  587. return idx[uniq - 1] - buf;
  588. }
  589. #define DO_LC_COMMON(CATEGORY) \
  590. printf("buf-size=%d uniq=%d rows=%d\n", \
  591. (int)(last - buf), uniq, lc_##CATEGORY##_uniq); \
  592. printf("total = %d + %d * %d + %d = %d\n", \
  593. num_locales, lc_##CATEGORY##_uniq, NUM_NL_##CATEGORY, (int)(last - buf), \
  594. i = num_locales + lc_##CATEGORY##_uniq*NUM_NL_##CATEGORY + (int)(last - buf)); \
  595. total_size += i; \
  596. dump_table8c("__lc_" #CATEGORY "_data", buf, (int)(last - buf)); \
  597. for (i=0 ; i < lc_##CATEGORY##_uniq ; i++) { \
  598. m = locales[i].lc_##CATEGORY##_row; \
  599. for (k=0 ; k < NUM_NL_##CATEGORY ; k++) { \
  600. buf[NUM_NL_##CATEGORY*i + k] = (char)((unsigned char) lc_##CATEGORY##_uniq_X[i][k]); \
  601. } \
  602. } \
  603. dump_table8("__lc_" #CATEGORY "_rows", buf, lc_##CATEGORY##_uniq * NUM_NL_##CATEGORY); \
  604. buf16[0] =0; \
  605. for (i=0 ; i < NUM_NL_##CATEGORY - 1 ; i++) { \
  606. buf16[i+1] = buf16[i] + lc_##CATEGORY##_count[i]; \
  607. } \
  608. dump_table16("__lc_" #CATEGORY "_item_offsets", buf16, NUM_NL_##CATEGORY); \
  609. m = 0; \
  610. for (k=0 ; k < NUM_NL_##CATEGORY ; k++) { \
  611. for (i=0 ; i < lc_##CATEGORY##_count[k] ; i++) { \
  612. buf16[m] = lc_##CATEGORY##_item[k][i]; \
  613. ++m; \
  614. } \
  615. } \
  616. dump_table16("__lc_" #CATEGORY "_item_idx", buf16, m);
  617. #define DL_LC_LOOPTAIL(CATEGORY) \
  618. if (k > NUM_NL_##CATEGORY) { \
  619. printf("error -- lc_" #CATEGORY " nl_item count > %d!\n", NUM_NL_##CATEGORY); \
  620. exit(EXIT_FAILURE); \
  621. } \
  622. { \
  623. int r; \
  624. for (r=0 ; r < lc_##CATEGORY##_uniq ; r++) { \
  625. if (!memcmp(lc_##CATEGORY##_uniq_X[lc_##CATEGORY##_uniq], \
  626. lc_##CATEGORY##_uniq_X[r], NUM_NL_##CATEGORY)) { \
  627. break; \
  628. } \
  629. } \
  630. if (r == lc_##CATEGORY##_uniq) { /* new locale row */ \
  631. ++lc_##CATEGORY##_uniq; \
  632. if (lc_##CATEGORY##_uniq > 255) { \
  633. printf("too many unique lc_" #CATEGORY " rows!\n"); \
  634. exit(EXIT_FAILURE); \
  635. } \
  636. } \
  637. locales[i].lc_##CATEGORY##_row = r; \
  638. }
  639. static int buf16[100*256];
  640. static void dump_table8(const char *name, const char *tbl, int len)
  641. {
  642. int i;
  643. fprintf(ofp, "#define %s_LEN\t\t%d\n", name, len);
  644. fprintf(ofp, "static const unsigned char %s[%d] = {", name, len);
  645. for (i=0 ; i < len ; i++) {
  646. if ((i % 12) == 0) {
  647. fprintf(ofp, "\n\t");
  648. }
  649. fprintf(ofp, "%#4x, ", (int)((unsigned char) tbl[i]));
  650. }
  651. fprintf(ofp, "\n};\n\n");
  652. }
  653. #define __C_isdigit(c) \
  654. ((sizeof(c) == sizeof(char)) \
  655. ? (((unsigned char)((c) - '0')) < 10) \
  656. : (((unsigned int)((c) - '0')) < 10))
  657. #define __C_isalpha(c) \
  658. ((sizeof(c) == sizeof(char)) \
  659. ? (((unsigned char)(((c) | 0x20) - 'a')) < 26) \
  660. : (((unsigned int)(((c) | 0x20) - 'a')) < 26))
  661. #define __C_isalnum(c) (__C_isalpha(c) || __C_isdigit(c))
  662. static void dump_table8c(const char *name, const char *tbl, int len)
  663. {
  664. int i;
  665. fprintf(ofp, "#define %s_LEN\t\t%d\n", name, len);
  666. fprintf(ofp, "static const unsigned char %s[%d] = {", name, len);
  667. for (i=0 ; i < len ; i++) {
  668. if ((i % 12) == 0) {
  669. fprintf(ofp, "\n\t");
  670. }
  671. if (__C_isalnum(tbl[i]) || (tbl[i] == ' ')) {
  672. fprintf(ofp, " '%c', ", (int)((unsigned char) tbl[i]));
  673. } else {
  674. fprintf(ofp, "%#4x, ", (int)((unsigned char) tbl[i]));
  675. }
  676. }
  677. fprintf(ofp, "\n};\n\n");
  678. }
  679. static void dump_table16(const char *name, const int *tbl, int len)
  680. {
  681. int i;
  682. fprintf(ofp, "#define %s_LEN\t\t%d\n", name, len);
  683. fprintf(ofp, "static const uint16_t %s[%d] = {", name, len);
  684. for (i=0 ; i < len ; i++) {
  685. if ((i % 8) == 0) {
  686. fprintf(ofp, "\n\t");
  687. }
  688. if (tbl[i] != (uint16_t) tbl[i]) {
  689. printf("error - falls outside uint16 range!\n");
  690. exit(EXIT_FAILURE);
  691. }
  692. fprintf(ofp, "%#6x, ", tbl[i]);
  693. }
  694. fprintf(ofp, "\n};\n\n");
  695. }
  696. #define NUM_NL_time 50
  697. static int lc_time_item[NUM_NL_time][256];
  698. static int lc_time_count[NUM_NL_time];
  699. static unsigned char lc_time_uniq_X[700][NUM_NL_time];
  700. static int lc_time_uniq;
  701. #define DO_NL_S(X) lc_time_S(X, k++)
  702. static void lc_time_S(int X, int k)
  703. {
  704. size_t len;
  705. int j, m;
  706. const char *s = nl_langinfo(X);
  707. const char *p;
  708. static const char nulbuf[] = "";
  709. if (X == ALT_DIGITS) {
  710. len = 1;
  711. if (!s) {
  712. s = nulbuf;
  713. }
  714. if (*s) {
  715. p = s;
  716. for (j = 0 ; j < 100 ; j++) {
  717. while (*p) {
  718. ++p;
  719. }
  720. ++p;
  721. }
  722. len = p - s;
  723. }
  724. j = addblock(s, len);
  725. /* if (len > 1) fprintf(stderr, "alt_digit: called addblock with len %zd\n", len); */
  726. } else if (X == ERA) {
  727. if (!s) {
  728. s = nulbuf;
  729. }
  730. p = s;
  731. while (*p) {
  732. while (*p) {
  733. ++p;
  734. }
  735. ++p;
  736. }
  737. ++p;
  738. j = addblock(s, p - s);
  739. /* if (p-s > 1) fprintf(stderr, "era: called addblock with len %d\n", p-s); */
  740. } else {
  741. j = addstring(s);
  742. }
  743. for (m=0 ; m < lc_time_count[k] ; m++) {
  744. if (lc_time_item[k][m] == j) {
  745. break;
  746. }
  747. }
  748. if (m == lc_time_count[k]) { /* new for this nl_item */
  749. if (m > 255) {
  750. printf("too many nl_item %d entries in lc_time\n", k);
  751. exit(EXIT_FAILURE);
  752. }
  753. lc_time_item[k][m] = j;
  754. ++lc_time_count[k];
  755. }
  756. lc_time_uniq_X[lc_time_uniq][k] = m;
  757. }
  758. static void do_lc_time(void)
  759. {
  760. int i, k, m;
  761. last = buf+1;
  762. uniq = 1;
  763. *buf = 0;
  764. *idx = buf;
  765. for (i=0 ; i < num_locales ; i++) {
  766. k = 0;
  767. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  768. printf("setlocale(LC_ALL,%s) failed!\n",
  769. locales[i].glibc_name);
  770. }
  771. DO_NL_S(ABDAY_1);
  772. DO_NL_S(ABDAY_2);
  773. DO_NL_S(ABDAY_3);
  774. DO_NL_S(ABDAY_4);
  775. DO_NL_S(ABDAY_5);
  776. DO_NL_S(ABDAY_6);
  777. DO_NL_S(ABDAY_7);
  778. DO_NL_S(DAY_1);
  779. DO_NL_S(DAY_2);
  780. DO_NL_S(DAY_3);
  781. DO_NL_S(DAY_4);
  782. DO_NL_S(DAY_5);
  783. DO_NL_S(DAY_6);
  784. DO_NL_S(DAY_7);
  785. DO_NL_S(ABMON_1);
  786. DO_NL_S(ABMON_2);
  787. DO_NL_S(ABMON_3);
  788. DO_NL_S(ABMON_4);
  789. DO_NL_S(ABMON_5);
  790. DO_NL_S(ABMON_6);
  791. DO_NL_S(ABMON_7);
  792. DO_NL_S(ABMON_8);
  793. DO_NL_S(ABMON_9);
  794. DO_NL_S(ABMON_10);
  795. DO_NL_S(ABMON_11);
  796. DO_NL_S(ABMON_12);
  797. DO_NL_S(MON_1);
  798. DO_NL_S(MON_2);
  799. DO_NL_S(MON_3);
  800. DO_NL_S(MON_4);
  801. DO_NL_S(MON_5);
  802. DO_NL_S(MON_6);
  803. DO_NL_S(MON_7);
  804. DO_NL_S(MON_8);
  805. DO_NL_S(MON_9);
  806. DO_NL_S(MON_10);
  807. DO_NL_S(MON_11);
  808. DO_NL_S(MON_12);
  809. DO_NL_S(AM_STR);
  810. DO_NL_S(PM_STR);
  811. DO_NL_S(D_T_FMT);
  812. DO_NL_S(D_FMT);
  813. DO_NL_S(T_FMT);
  814. DO_NL_S(T_FMT_AMPM);
  815. DO_NL_S(ERA);
  816. DO_NL_S(ERA_YEAR); /* non SuSv3 */
  817. DO_NL_S(ERA_D_FMT);
  818. DO_NL_S(ALT_DIGITS);
  819. DO_NL_S(ERA_D_T_FMT);
  820. DO_NL_S(ERA_T_FMT);
  821. DL_LC_LOOPTAIL(time)
  822. }
  823. DO_LC_COMMON(time)
  824. }
  825. #undef DO_NL_S
  826. #define NUM_NL_numeric 3
  827. static int lc_numeric_item[NUM_NL_numeric][256];
  828. static int lc_numeric_count[NUM_NL_numeric];
  829. static unsigned char lc_numeric_uniq_X[700][NUM_NL_numeric];
  830. static int lc_numeric_uniq;
  831. #define DO_NL_S(X) lc_numeric_S(X, k++)
  832. static void lc_numeric_S(int X, int k)
  833. {
  834. int j, m;
  835. char buf[256];
  836. char *e;
  837. char *s;
  838. char c;
  839. s = nl_langinfo(X);
  840. if (X == GROUPING) {
  841. if (s) {
  842. if ((*s == CHAR_MAX) || (*s == -1)) { /* stupid glibc... :-( */
  843. s = "";
  844. }
  845. e = s;
  846. c = 0;
  847. while (*e) { /* find end of string */
  848. if (*e == CHAR_MAX) {
  849. c = CHAR_MAX;
  850. ++e;
  851. break;
  852. }
  853. ++e;
  854. }
  855. if ((e - s) > sizeof(buf)) {
  856. printf("grouping specifier too long\n");
  857. exit(EXIT_FAILURE);
  858. }
  859. strncpy(buf, s, (e-s));
  860. e = buf + (e-s);
  861. *e = 0; /* Make sure we're null-terminated. */
  862. if (c != CHAR_MAX) { /* remove duplicate repeats */
  863. while (e > buf) {
  864. --e;
  865. if (*e != e[-1]) {
  866. break;
  867. }
  868. }
  869. *++e = 0;
  870. }
  871. s = buf;
  872. }
  873. }
  874. j = addstring(s);
  875. for (m=0 ; m < lc_numeric_count[k] ; m++) {
  876. if (lc_numeric_item[k][m] == j) {
  877. break;
  878. }
  879. }
  880. if (m == lc_numeric_count[k]) { /* new for this nl_item */
  881. if (m > 255) {
  882. printf("too many nl_item %d entries in lc_numeric\n", k);
  883. exit(EXIT_FAILURE);
  884. }
  885. lc_numeric_item[k][m] = j;
  886. ++lc_numeric_count[k];
  887. }
  888. /* printf("\\x%02x", m); */
  889. lc_numeric_uniq_X[lc_numeric_uniq][k] = m;
  890. }
  891. static void do_lc_numeric(void)
  892. {
  893. int i, k, m;
  894. last = buf+1;
  895. uniq = 1;
  896. *buf = 0;
  897. *idx = buf;
  898. for (i=0 ; i < num_locales ; i++) {
  899. k = 0;
  900. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  901. printf("setlocale(LC_ALL,%s) failed!\n",
  902. locales[i].glibc_name);
  903. }
  904. DO_NL_S(RADIXCHAR); /* DECIMAL_POINT */
  905. DO_NL_S(THOUSEP); /* THOUSANDS_SEP */
  906. DO_NL_S(GROUPING);
  907. DL_LC_LOOPTAIL(numeric)
  908. }
  909. DO_LC_COMMON(numeric)
  910. }
  911. #undef DO_NL_S
  912. #define NUM_NL_monetary (7+14+1)
  913. static int lc_monetary_item[NUM_NL_monetary][256];
  914. static int lc_monetary_count[NUM_NL_monetary];
  915. static unsigned char lc_monetary_uniq_X[700][NUM_NL_monetary];
  916. static int lc_monetary_uniq;
  917. #define DO_NL_S(X) lc_monetary_S(X, k++)
  918. /* #define DO_NL_C(X) printf("%#02x", (int)(unsigned char)(*nl_langinfo(X))); */
  919. #define DO_NL_C(X) lc_monetary_C(X, k++)
  920. static void lc_monetary_C(int X, int k)
  921. {
  922. int j, m;
  923. char c_buf[2];
  924. #warning fix the char entries for monetary... target signedness of char may be different!
  925. c_buf[1] = 0;
  926. c_buf[0] = *nl_langinfo(X);
  927. j = addstring(c_buf);
  928. for (m=0 ; m < lc_monetary_count[k] ; m++) {
  929. if (lc_monetary_item[k][m] == j) {
  930. break;
  931. }
  932. }
  933. if (m == lc_monetary_count[k]) { /* new for this nl_item */
  934. if (m > 255) {
  935. printf("too many nl_item %d entries in lc_monetary\n", k);
  936. exit(EXIT_FAILURE);
  937. }
  938. lc_monetary_item[k][m] = j;
  939. ++lc_monetary_count[k];
  940. }
  941. /* printf("\\x%02x", m); */
  942. lc_monetary_uniq_X[lc_monetary_uniq][k] = m;
  943. }
  944. static void lc_monetary_S(int X, int k)
  945. {
  946. int j, m;
  947. char buf[256];
  948. char *e;
  949. char *s;
  950. char c;
  951. s = nl_langinfo(X);
  952. if (X == MON_GROUPING) {
  953. if (s) {
  954. if ((*s == CHAR_MAX) || (*s == -1)) { /* stupid glibc... :-( */
  955. s = "";
  956. }
  957. e = s;
  958. c = 0;
  959. while (*e) { /* find end of string */
  960. if (*e == CHAR_MAX) {
  961. c = CHAR_MAX;
  962. ++e;
  963. break;
  964. }
  965. ++e;
  966. }
  967. if ((e - s) > sizeof(buf)) {
  968. printf("mon_grouping specifier too long\n");
  969. exit(EXIT_FAILURE);
  970. }
  971. strncpy(buf, s, (e-s));
  972. e = buf + (e-s);
  973. *e = 0; /* Make sure we're null-terminated. */
  974. if (c != CHAR_MAX) { /* remove duplicate repeats */
  975. while (e > buf) {
  976. --e;
  977. if (*e != e[-1]) {
  978. break;
  979. }
  980. }
  981. *++e = 0;
  982. }
  983. s = buf;
  984. }
  985. }
  986. j = addstring(s);
  987. for (m=0 ; m < lc_monetary_count[k] ; m++) {
  988. if (lc_monetary_item[k][m] == j) {
  989. break;
  990. }
  991. }
  992. if (m == lc_monetary_count[k]) { /* new for this nl_item */
  993. if (m > 255) {
  994. printf("too many nl_item %d entries in lc_monetary\n", k);
  995. exit(EXIT_FAILURE);
  996. }
  997. lc_monetary_item[k][m] = j;
  998. ++lc_monetary_count[k];
  999. }
  1000. /* printf("\\x%02x", m); */
  1001. lc_monetary_uniq_X[lc_monetary_uniq][k] = m;
  1002. }
  1003. static void do_lc_monetary(void)
  1004. {
  1005. int i, k, m;
  1006. last = buf+1;
  1007. uniq = 1;
  1008. *buf = 0;
  1009. *idx = buf;
  1010. for (i=0 ; i < num_locales ; i++) {
  1011. k = 0;
  1012. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1013. printf("setlocale(LC_ALL,%s) failed!\n",
  1014. locales[i].glibc_name);
  1015. }
  1016. /* non SUSv3 */
  1017. DO_NL_S(INT_CURR_SYMBOL);
  1018. DO_NL_S(CURRENCY_SYMBOL);
  1019. DO_NL_S(MON_DECIMAL_POINT);
  1020. DO_NL_S(MON_THOUSANDS_SEP);
  1021. DO_NL_S(MON_GROUPING);
  1022. DO_NL_S(POSITIVE_SIGN);
  1023. DO_NL_S(NEGATIVE_SIGN);
  1024. DO_NL_C(INT_FRAC_DIGITS);
  1025. DO_NL_C(FRAC_DIGITS);
  1026. DO_NL_C(P_CS_PRECEDES);
  1027. DO_NL_C(P_SEP_BY_SPACE);
  1028. DO_NL_C(N_CS_PRECEDES);
  1029. DO_NL_C(N_SEP_BY_SPACE);
  1030. DO_NL_C(P_SIGN_POSN);
  1031. DO_NL_C(N_SIGN_POSN);
  1032. DO_NL_C(INT_P_CS_PRECEDES);
  1033. DO_NL_C(INT_P_SEP_BY_SPACE);
  1034. DO_NL_C(INT_N_CS_PRECEDES);
  1035. DO_NL_C(INT_N_SEP_BY_SPACE);
  1036. DO_NL_C(INT_P_SIGN_POSN);
  1037. DO_NL_C(INT_N_SIGN_POSN);
  1038. DO_NL_S(CRNCYSTR); /* CURRENCY_SYMBOL */
  1039. DL_LC_LOOPTAIL(monetary)
  1040. }
  1041. DO_LC_COMMON(monetary)
  1042. }
  1043. #undef DO_NL_S
  1044. #define NUM_NL_messages 4
  1045. static int lc_messages_item[NUM_NL_messages][256];
  1046. static int lc_messages_count[NUM_NL_messages];
  1047. static unsigned char lc_messages_uniq_X[700][NUM_NL_messages];
  1048. static int lc_messages_uniq;
  1049. #define DO_NL_S(X) lc_messages_S(X, k++)
  1050. static void lc_messages_S(int X, int k)
  1051. {
  1052. int j, m;
  1053. j = addstring(nl_langinfo(X));
  1054. for (m=0 ; m < lc_messages_count[k] ; m++) {
  1055. if (lc_messages_item[k][m] == j) {
  1056. break;
  1057. }
  1058. }
  1059. if (m == lc_messages_count[k]) { /* new for this nl_item */
  1060. if (m > 255) {
  1061. printf("too many nl_item %d entries in lc_messages\n", k);
  1062. exit(EXIT_FAILURE);
  1063. }
  1064. lc_messages_item[k][m] = j;
  1065. ++lc_messages_count[k];
  1066. }
  1067. /* printf("\\x%02x", m); */
  1068. lc_messages_uniq_X[lc_messages_uniq][k] = m;
  1069. }
  1070. static void do_lc_messages(void)
  1071. {
  1072. int i, k, m;
  1073. last = buf+1;
  1074. uniq = 1;
  1075. *buf = 0;
  1076. *idx = buf;
  1077. for (i=0 ; i < num_locales ; i++) {
  1078. k = 0;
  1079. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1080. printf("setlocale(LC_ALL,%s) failed!\n",
  1081. locales[i].glibc_name);
  1082. }
  1083. DO_NL_S(YESEXPR);
  1084. DO_NL_S(NOEXPR);
  1085. DO_NL_S(YESSTR);
  1086. DO_NL_S(NOSTR);
  1087. DL_LC_LOOPTAIL(messages)
  1088. }
  1089. DO_LC_COMMON(messages)
  1090. }
  1091. #undef DO_NL_S
  1092. #define NUM_NL_ctype 10
  1093. static int lc_ctype_item[NUM_NL_ctype][256];
  1094. static int lc_ctype_count[NUM_NL_ctype];
  1095. static unsigned char lc_ctype_uniq_X[700][NUM_NL_ctype];
  1096. static int lc_ctype_uniq;
  1097. #define DO_NL_S(X) lc_ctype_S(X, k++)
  1098. static void lc_ctype_S(int X, int k)
  1099. {
  1100. int j, m;
  1101. j = addstring(nl_langinfo(X));
  1102. for (m=0 ; m < lc_ctype_count[k] ; m++) {
  1103. if (lc_ctype_item[k][m] == j) {
  1104. break;
  1105. }
  1106. }
  1107. if (m == lc_ctype_count[k]) { /* new for this nl_item */
  1108. if (m > 255) {
  1109. printf("too many nl_item %d entries in lc_ctype\n", k);
  1110. exit(EXIT_FAILURE);
  1111. }
  1112. lc_ctype_item[k][m] = j;
  1113. ++lc_ctype_count[k];
  1114. }
  1115. /* printf("\\x%02x", m); */
  1116. lc_ctype_uniq_X[lc_ctype_uniq][k] = m;
  1117. }
  1118. static void do_lc_ctype(void)
  1119. {
  1120. int i, k, m;
  1121. last = buf+1;
  1122. uniq = 1;
  1123. *buf = 0;
  1124. *idx = buf;
  1125. for (i=0 ; i < num_locales ; i++) {
  1126. k = 0;
  1127. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1128. printf("setlocale(LC_ALL,%s) failed!\n",
  1129. locales[i].glibc_name);
  1130. }
  1131. DO_NL_S(_NL_CTYPE_OUTDIGIT0_MB);
  1132. DO_NL_S(_NL_CTYPE_OUTDIGIT1_MB);
  1133. DO_NL_S(_NL_CTYPE_OUTDIGIT2_MB);
  1134. DO_NL_S(_NL_CTYPE_OUTDIGIT3_MB);
  1135. DO_NL_S(_NL_CTYPE_OUTDIGIT4_MB);
  1136. DO_NL_S(_NL_CTYPE_OUTDIGIT5_MB);
  1137. DO_NL_S(_NL_CTYPE_OUTDIGIT6_MB);
  1138. DO_NL_S(_NL_CTYPE_OUTDIGIT7_MB);
  1139. DO_NL_S(_NL_CTYPE_OUTDIGIT8_MB);
  1140. DO_NL_S(_NL_CTYPE_OUTDIGIT9_MB);
  1141. DL_LC_LOOPTAIL(ctype)
  1142. }
  1143. DO_LC_COMMON(ctype)
  1144. }