gen_locale.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337
  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. #warning fix the char entries for monetary... target signedness of char may be different!
  940. c_buf[1] = 0;
  941. c_buf[0] = *nl_langinfo(X);
  942. j = addstring(c_buf);
  943. for (m=0 ; m < lc_monetary_count[k] ; m++) {
  944. if (lc_monetary_item[k][m] == j) {
  945. break;
  946. }
  947. }
  948. if (m == lc_monetary_count[k]) { /* new for this nl_item */
  949. if (m > 255) {
  950. error_msg("too many nl_item %d entries in lc_monetary", k);
  951. }
  952. lc_monetary_item[k][m] = j;
  953. ++lc_monetary_count[k];
  954. }
  955. /* verbose_msg(VDETAIL,"\\x%02x", m); */
  956. lc_monetary_uniq_X[lc_monetary_uniq][k] = m;
  957. }
  958. static void lc_monetary_S(int X, int k)
  959. {
  960. int j, m;
  961. char buf[256];
  962. char *e;
  963. char *s;
  964. char c;
  965. s = nl_langinfo(X);
  966. if (X == MON_GROUPING) {
  967. if (s) {
  968. if ((*s == CHAR_MAX) || (*s == -1)) { /* stupid glibc... :-( */
  969. s = "";
  970. }
  971. e = s;
  972. c = 0;
  973. while (*e) { /* find end of string */
  974. if (*e == CHAR_MAX) {
  975. c = CHAR_MAX;
  976. ++e;
  977. break;
  978. }
  979. ++e;
  980. }
  981. if ((e - s) > sizeof(buf)) {
  982. error_msg("mon_grouping specifier too long");
  983. }
  984. strncpy(buf, s, (e-s));
  985. e = buf + (e-s);
  986. *e = 0; /* Make sure we're null-terminated. */
  987. if (c != CHAR_MAX) { /* remove duplicate repeats */
  988. while (e > buf) {
  989. --e;
  990. if (*e != e[-1]) {
  991. break;
  992. }
  993. }
  994. *++e = 0;
  995. }
  996. s = buf;
  997. }
  998. }
  999. j = addstring(s);
  1000. for (m=0 ; m < lc_monetary_count[k] ; m++) {
  1001. if (lc_monetary_item[k][m] == j) {
  1002. break;
  1003. }
  1004. }
  1005. if (m == lc_monetary_count[k]) { /* new for this nl_item */
  1006. if (m > 255) {
  1007. error_msg("too many nl_item %d entries in lc_monetary", k);
  1008. }
  1009. lc_monetary_item[k][m] = j;
  1010. ++lc_monetary_count[k];
  1011. }
  1012. /* verbose_msg(VDETAIL,"\\x%02x", m); */
  1013. lc_monetary_uniq_X[lc_monetary_uniq][k] = m;
  1014. }
  1015. static void do_lc_monetary(void)
  1016. {
  1017. int i, k, m;
  1018. last = buf+1;
  1019. uniq = 1;
  1020. *buf = 0;
  1021. *idx = buf;
  1022. for (i=0 ; i < num_locales ; i++) {
  1023. k = 0;
  1024. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1025. verbose_msg(VDETAIL,"setlocale(LC_ALL,%s) failed!\n",
  1026. locales[i].glibc_name);
  1027. }
  1028. /* non SUSv3 */
  1029. DO_NL_S(INT_CURR_SYMBOL);
  1030. DO_NL_S(CURRENCY_SYMBOL);
  1031. DO_NL_S(MON_DECIMAL_POINT);
  1032. DO_NL_S(MON_THOUSANDS_SEP);
  1033. DO_NL_S(MON_GROUPING);
  1034. DO_NL_S(POSITIVE_SIGN);
  1035. DO_NL_S(NEGATIVE_SIGN);
  1036. DO_NL_C(INT_FRAC_DIGITS);
  1037. DO_NL_C(FRAC_DIGITS);
  1038. DO_NL_C(P_CS_PRECEDES);
  1039. DO_NL_C(P_SEP_BY_SPACE);
  1040. DO_NL_C(N_CS_PRECEDES);
  1041. DO_NL_C(N_SEP_BY_SPACE);
  1042. DO_NL_C(P_SIGN_POSN);
  1043. DO_NL_C(N_SIGN_POSN);
  1044. DO_NL_C(INT_P_CS_PRECEDES);
  1045. DO_NL_C(INT_P_SEP_BY_SPACE);
  1046. DO_NL_C(INT_N_CS_PRECEDES);
  1047. DO_NL_C(INT_N_SEP_BY_SPACE);
  1048. DO_NL_C(INT_P_SIGN_POSN);
  1049. DO_NL_C(INT_N_SIGN_POSN);
  1050. DO_NL_S(CRNCYSTR); /* CURRENCY_SYMBOL */
  1051. DL_LC_LOOPTAIL(monetary)
  1052. }
  1053. DO_LC_COMMON(monetary)
  1054. }
  1055. #undef DO_NL_S
  1056. #define NUM_NL_messages 4
  1057. static int lc_messages_item[NUM_NL_messages][256];
  1058. static int lc_messages_count[NUM_NL_messages];
  1059. static unsigned char lc_messages_uniq_X[700][NUM_NL_messages];
  1060. static int lc_messages_uniq;
  1061. #define DO_NL_S(X) lc_messages_S(X, k++)
  1062. static void lc_messages_S(int X, int k)
  1063. {
  1064. int j, m;
  1065. j = addstring(nl_langinfo(X));
  1066. for (m=0 ; m < lc_messages_count[k] ; m++) {
  1067. if (lc_messages_item[k][m] == j) {
  1068. break;
  1069. }
  1070. }
  1071. if (m == lc_messages_count[k]) { /* new for this nl_item */
  1072. if (m > 255) {
  1073. error_msg("too many nl_item %d entries in lc_messages", k);
  1074. }
  1075. lc_messages_item[k][m] = j;
  1076. ++lc_messages_count[k];
  1077. }
  1078. /* verbose_msg(VDETAIL, "\\x%02x", m); */
  1079. lc_messages_uniq_X[lc_messages_uniq][k] = m;
  1080. }
  1081. static void do_lc_messages(void)
  1082. {
  1083. int i, k, m;
  1084. last = buf+1;
  1085. uniq = 1;
  1086. *buf = 0;
  1087. *idx = buf;
  1088. for (i=0 ; i < num_locales ; i++) {
  1089. k = 0;
  1090. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1091. verbose_msg(VDETAIL, "setlocale(LC_ALL,%s) failed!\n",
  1092. locales[i].glibc_name);
  1093. }
  1094. DO_NL_S(YESEXPR);
  1095. DO_NL_S(NOEXPR);
  1096. DO_NL_S(YESSTR);
  1097. DO_NL_S(NOSTR);
  1098. DL_LC_LOOPTAIL(messages)
  1099. }
  1100. DO_LC_COMMON(messages)
  1101. }
  1102. #undef DO_NL_S
  1103. #define NUM_NL_ctype 10
  1104. static int lc_ctype_item[NUM_NL_ctype][256];
  1105. static int lc_ctype_count[NUM_NL_ctype];
  1106. static unsigned char lc_ctype_uniq_X[700][NUM_NL_ctype];
  1107. static int lc_ctype_uniq;
  1108. #define DO_NL_S(X) lc_ctype_S(X, k++)
  1109. static void lc_ctype_S(int X, int k)
  1110. {
  1111. int j, m;
  1112. j = addstring(nl_langinfo(X));
  1113. for (m=0 ; m < lc_ctype_count[k] ; m++) {
  1114. if (lc_ctype_item[k][m] == j) {
  1115. break;
  1116. }
  1117. }
  1118. if (m == lc_ctype_count[k]) { /* new for this nl_item */
  1119. if (m > 255) {
  1120. error_msg("too many nl_item %d entries in lc_ctype", k);
  1121. }
  1122. lc_ctype_item[k][m] = j;
  1123. ++lc_ctype_count[k];
  1124. }
  1125. /* verbose_msg(VDETAIL, "\\x%02x", m); */
  1126. lc_ctype_uniq_X[lc_ctype_uniq][k] = m;
  1127. }
  1128. static void do_lc_ctype(void)
  1129. {
  1130. int i, k, m;
  1131. last = buf+1;
  1132. uniq = 1;
  1133. *buf = 0;
  1134. *idx = buf;
  1135. for (i=0 ; i < num_locales ; i++) {
  1136. k = 0;
  1137. if (!setlocale(LC_ALL, locales[i].glibc_name)) {
  1138. verbose_msg(VDETAIL, "setlocale(LC_ALL,%s) failed!\n",
  1139. locales[i].glibc_name);
  1140. }
  1141. DO_NL_S(_NL_CTYPE_OUTDIGIT0_MB);
  1142. DO_NL_S(_NL_CTYPE_OUTDIGIT1_MB);
  1143. DO_NL_S(_NL_CTYPE_OUTDIGIT2_MB);
  1144. DO_NL_S(_NL_CTYPE_OUTDIGIT3_MB);
  1145. DO_NL_S(_NL_CTYPE_OUTDIGIT4_MB);
  1146. DO_NL_S(_NL_CTYPE_OUTDIGIT5_MB);
  1147. DO_NL_S(_NL_CTYPE_OUTDIGIT6_MB);
  1148. DO_NL_S(_NL_CTYPE_OUTDIGIT7_MB);
  1149. DO_NL_S(_NL_CTYPE_OUTDIGIT8_MB);
  1150. DO_NL_S(_NL_CTYPE_OUTDIGIT9_MB);
  1151. DL_LC_LOOPTAIL(ctype)
  1152. }
  1153. DO_LC_COMMON(ctype)
  1154. }