gen_locale.c 30 KB

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