gen-unicode-ctype.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. /* Generate a Unicode conforming LC_CTYPE category from a UnicodeData file.
  2. Copyright (C) 2000-2001 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. Contributed by Bruno Haible <haible@clisp.cons.org>, 2000.
  5. The GNU C Library is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the License, or (at your option) any later version.
  9. The GNU C Library is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, write to the Free
  15. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  16. 02111-1307 USA. */
  17. /* Usage example:
  18. $ gen-unicode /usr/local/share/Unidata/UnicodeData.txt 3.1
  19. */
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include <string.h>
  24. #include <time.h>
  25. /* This structure represents one line in the UnicodeData.txt file. */
  26. struct unicode_attribute
  27. {
  28. const char *name; /* Character name */
  29. const char *category; /* General category */
  30. const char *combining; /* Canonical combining classes */
  31. const char *bidi; /* Bidirectional category */
  32. const char *decomposition; /* Character decomposition mapping */
  33. const char *decdigit; /* Decimal digit value */
  34. const char *digit; /* Digit value */
  35. const char *numeric; /* Numeric value */
  36. int mirrored; /* mirrored */
  37. const char *oldname; /* Old Unicode 1.0 name */
  38. const char *comment; /* Comment */
  39. unsigned int upper; /* Uppercase mapping */
  40. unsigned int lower; /* Lowercase mapping */
  41. unsigned int title; /* Titlecase mapping */
  42. };
  43. /* Missing fields are represented with "" for strings, and NONE for
  44. characters. */
  45. #define NONE (~(unsigned int)0)
  46. /* The entire contents of the UnicodeData.txt file. */
  47. struct unicode_attribute unicode_attributes [0x110000];
  48. /* Stores in unicode_attributes[i] the values from the given fields. */
  49. static void
  50. fill_attribute (unsigned int i,
  51. const char *field1, const char *field2,
  52. const char *field3, const char *field4,
  53. const char *field5, const char *field6,
  54. const char *field7, const char *field8,
  55. const char *field9, const char *field10,
  56. const char *field11, const char *field12,
  57. const char *field13, const char *field14)
  58. {
  59. struct unicode_attribute * uni;
  60. if (i >= 0x110000)
  61. {
  62. fprintf (stderr, "index too large\n");
  63. exit (1);
  64. }
  65. if (strcmp (field2, "Cs") == 0)
  66. /* Surrogates are UTF-16 artefacts, not real characters. Ignore them. */
  67. return;
  68. uni = &unicode_attributes[i];
  69. /* Copy the strings. */
  70. uni->name = strdup (field1);
  71. uni->category = (field2[0] == '\0' ? "" : strdup (field2));
  72. uni->combining = (field3[0] == '\0' ? "" : strdup (field3));
  73. uni->bidi = (field4[0] == '\0' ? "" : strdup (field4));
  74. uni->decomposition = (field5[0] == '\0' ? "" : strdup (field5));
  75. uni->decdigit = (field6[0] == '\0' ? "" : strdup (field6));
  76. uni->digit = (field7[0] == '\0' ? "" : strdup (field7));
  77. uni->numeric = (field8[0] == '\0' ? "" : strdup (field8));
  78. uni->mirrored = (field9[0] == 'Y');
  79. uni->oldname = (field10[0] == '\0' ? "" : strdup (field10));
  80. uni->comment = (field11[0] == '\0' ? "" : strdup (field11));
  81. uni->upper = (field12[0] =='\0' ? NONE : strtoul (field12, NULL, 16));
  82. uni->lower = (field13[0] =='\0' ? NONE : strtoul (field13, NULL, 16));
  83. uni->title = (field14[0] =='\0' ? NONE : strtoul (field14, NULL, 16));
  84. }
  85. /* Maximum length of a field in the UnicodeData.txt file. */
  86. #define FIELDLEN 120
  87. /* Reads the next field from STREAM. The buffer BUFFER has size FIELDLEN.
  88. Reads up to (but excluding) DELIM.
  89. Returns 1 when a field was successfully read, otherwise 0. */
  90. static int
  91. getfield (FILE *stream, char *buffer, int delim)
  92. {
  93. int count = 0;
  94. int c;
  95. for (; (c = getc (stream)), (c != EOF && c != delim); )
  96. {
  97. /* The original unicode.org UnicodeData.txt file happens to have
  98. CR/LF line terminators. Silently convert to LF. */
  99. if (c == '\r')
  100. continue;
  101. /* Put c into the buffer. */
  102. if (++count >= FIELDLEN - 1)
  103. {
  104. fprintf (stderr, "field too long\n");
  105. exit (1);
  106. }
  107. *buffer++ = c;
  108. }
  109. if (c == EOF)
  110. return 0;
  111. *buffer = '\0';
  112. return 1;
  113. }
  114. /* Stores in unicode_attributes[] the entire contents of the UnicodeData.txt
  115. file. */
  116. static void
  117. fill_attributes (const char *unicodedata_filename)
  118. {
  119. unsigned int i, j;
  120. FILE *stream;
  121. char field0[FIELDLEN];
  122. char field1[FIELDLEN];
  123. char field2[FIELDLEN];
  124. char field3[FIELDLEN];
  125. char field4[FIELDLEN];
  126. char field5[FIELDLEN];
  127. char field6[FIELDLEN];
  128. char field7[FIELDLEN];
  129. char field8[FIELDLEN];
  130. char field9[FIELDLEN];
  131. char field10[FIELDLEN];
  132. char field11[FIELDLEN];
  133. char field12[FIELDLEN];
  134. char field13[FIELDLEN];
  135. char field14[FIELDLEN];
  136. int lineno = 0;
  137. for (i = 0; i < 0x110000; i++)
  138. unicode_attributes[i].name = NULL;
  139. stream = fopen (unicodedata_filename, "r");
  140. if (stream == NULL)
  141. {
  142. fprintf (stderr, "error during fopen of '%s'\n", unicodedata_filename);
  143. exit (1);
  144. }
  145. for (;;)
  146. {
  147. int n;
  148. lineno++;
  149. n = getfield (stream, field0, ';');
  150. n += getfield (stream, field1, ';');
  151. n += getfield (stream, field2, ';');
  152. n += getfield (stream, field3, ';');
  153. n += getfield (stream, field4, ';');
  154. n += getfield (stream, field5, ';');
  155. n += getfield (stream, field6, ';');
  156. n += getfield (stream, field7, ';');
  157. n += getfield (stream, field8, ';');
  158. n += getfield (stream, field9, ';');
  159. n += getfield (stream, field10, ';');
  160. n += getfield (stream, field11, ';');
  161. n += getfield (stream, field12, ';');
  162. n += getfield (stream, field13, ';');
  163. n += getfield (stream, field14, '\n');
  164. if (n == 0)
  165. break;
  166. if (n != 15)
  167. {
  168. fprintf (stderr, "short line in'%s':%d\n",
  169. unicodedata_filename, lineno);
  170. exit (1);
  171. }
  172. i = strtoul (field0, NULL, 16);
  173. if (field1[0] == '<'
  174. && strlen (field1) >= 9
  175. && !strcmp (field1 + strlen(field1) - 8, ", First>"))
  176. {
  177. /* Deal with a range. */
  178. lineno++;
  179. n = getfield (stream, field0, ';');
  180. n += getfield (stream, field1, ';');
  181. n += getfield (stream, field2, ';');
  182. n += getfield (stream, field3, ';');
  183. n += getfield (stream, field4, ';');
  184. n += getfield (stream, field5, ';');
  185. n += getfield (stream, field6, ';');
  186. n += getfield (stream, field7, ';');
  187. n += getfield (stream, field8, ';');
  188. n += getfield (stream, field9, ';');
  189. n += getfield (stream, field10, ';');
  190. n += getfield (stream, field11, ';');
  191. n += getfield (stream, field12, ';');
  192. n += getfield (stream, field13, ';');
  193. n += getfield (stream, field14, '\n');
  194. if (n != 15)
  195. {
  196. fprintf (stderr, "missing end range in '%s':%d\n",
  197. unicodedata_filename, lineno);
  198. exit (1);
  199. }
  200. if (!(field1[0] == '<'
  201. && strlen (field1) >= 8
  202. && !strcmp (field1 + strlen (field1) - 7, ", Last>")))
  203. {
  204. fprintf (stderr, "missing end range in '%s':%d\n",
  205. unicodedata_filename, lineno);
  206. exit (1);
  207. }
  208. field1[strlen (field1) - 7] = '\0';
  209. j = strtoul (field0, NULL, 16);
  210. for (; i <= j; i++)
  211. fill_attribute (i, field1+1, field2, field3, field4, field5,
  212. field6, field7, field8, field9, field10,
  213. field11, field12, field13, field14);
  214. }
  215. else
  216. {
  217. /* Single character line */
  218. fill_attribute (i, field1, field2, field3, field4, field5,
  219. field6, field7, field8, field9, field10,
  220. field11, field12, field13, field14);
  221. }
  222. }
  223. if (ferror (stream) || fclose (stream))
  224. {
  225. fprintf (stderr, "error reading from '%s'\n", unicodedata_filename);
  226. exit (1);
  227. }
  228. }
  229. /* Character mappings. */
  230. static unsigned int
  231. to_upper (unsigned int ch)
  232. {
  233. if (unicode_attributes[ch].name != NULL
  234. && unicode_attributes[ch].upper != NONE)
  235. return unicode_attributes[ch].upper;
  236. else
  237. return ch;
  238. }
  239. static unsigned int
  240. to_lower (unsigned int ch)
  241. {
  242. if (unicode_attributes[ch].name != NULL
  243. && unicode_attributes[ch].lower != NONE)
  244. return unicode_attributes[ch].lower;
  245. else
  246. return ch;
  247. }
  248. static unsigned int
  249. to_title (unsigned int ch)
  250. {
  251. if (unicode_attributes[ch].name != NULL
  252. && unicode_attributes[ch].title != NONE)
  253. return unicode_attributes[ch].title;
  254. else
  255. return ch;
  256. }
  257. /* Character class properties. */
  258. static bool
  259. is_upper (unsigned int ch)
  260. {
  261. return (to_lower (ch) != ch);
  262. }
  263. static bool
  264. is_lower (unsigned int ch)
  265. {
  266. return (to_upper (ch) != ch)
  267. /* <U00DF> is lowercase, but without simple to_upper mapping. */
  268. || (ch == 0x00DF);
  269. }
  270. static bool
  271. is_alpha (unsigned int ch)
  272. {
  273. return (unicode_attributes[ch].name != NULL
  274. && ((unicode_attributes[ch].category[0] == 'L'
  275. /* Theppitak Karoonboonyanan <thep@links.nectec.or.th> says
  276. <U0E2F>, <U0E46> should belong to is_punct. */
  277. && (ch != 0x0E2F) && (ch != 0x0E46))
  278. /* Theppitak Karoonboonyanan <thep@links.nectec.or.th> says
  279. <U0E31>, <U0E34>..<U0E3A>, <U0E47>..<U0E4E> are is_alpha. */
  280. || (ch == 0x0E31)
  281. || (ch >= 0x0E34 && ch <= 0x0E3A)
  282. || (ch >= 0x0E47 && ch <= 0x0E4E)
  283. /* Avoid warning for <U0345>. */
  284. || (ch == 0x0345)
  285. /* Avoid warnings for <U2160>..<U217F>. */
  286. || (unicode_attributes[ch].category[0] == 'N'
  287. && unicode_attributes[ch].category[1] == 'l')
  288. /* Avoid warnings for <U24B6>..<U24E9>. */
  289. || (unicode_attributes[ch].category[0] == 'S'
  290. && unicode_attributes[ch].category[1] == 'o'
  291. && strstr (unicode_attributes[ch].name, " LETTER ")
  292. != NULL)
  293. /* Consider all the non-ASCII digits as alphabetic.
  294. ISO C 99 forbids us to have them in category "digit",
  295. but we want iswalnum to return true on them. */
  296. || (unicode_attributes[ch].category[0] == 'N'
  297. && unicode_attributes[ch].category[1] == 'd'
  298. && !(ch >= 0x0030 && ch <= 0x0039))));
  299. }
  300. static bool
  301. is_digit (unsigned int ch)
  302. {
  303. #if 0
  304. return (unicode_attributes[ch].name != NULL
  305. && unicode_attributes[ch].category[0] == 'N'
  306. && unicode_attributes[ch].category[1] == 'd');
  307. /* Note: U+0BE7..U+0BEF and U+1369..U+1371 are digit systems without
  308. a zero. Must add <0> in front of them by hand. */
  309. #else
  310. /* SUSV2 gives us some freedom for the "digit" category, but ISO C 99
  311. takes it away:
  312. 7.25.2.1.5:
  313. The iswdigit function tests for any wide character that corresponds
  314. to a decimal-digit character (as defined in 5.2.1).
  315. 5.2.1:
  316. the 10 decimal digits 0 1 2 3 4 5 6 7 8 9
  317. */
  318. return (ch >= 0x0030 && ch <= 0x0039);
  319. #endif
  320. }
  321. static bool
  322. is_outdigit (unsigned int ch)
  323. {
  324. return (ch >= 0x0030 && ch <= 0x0039);
  325. }
  326. static bool
  327. is_blank (unsigned int ch)
  328. {
  329. return (ch == 0x0009 /* '\t' */
  330. /* Category Zs without mention of "<noBreak>" */
  331. || (unicode_attributes[ch].name != NULL
  332. && unicode_attributes[ch].category[0] == 'Z'
  333. && unicode_attributes[ch].category[1] == 's'
  334. && !strstr (unicode_attributes[ch].decomposition, "<noBreak>")));
  335. }
  336. static bool
  337. is_space (unsigned int ch)
  338. {
  339. /* Don't make U+00A0 a space. Non-breaking space means that all programs
  340. should treat it like a punctuation character, not like a space. */
  341. return (ch == 0x0020 /* ' ' */
  342. || ch == 0x000C /* '\f' */
  343. || ch == 0x000A /* '\n' */
  344. || ch == 0x000D /* '\r' */
  345. || ch == 0x0009 /* '\t' */
  346. || ch == 0x000B /* '\v' */
  347. /* Categories Zl, Zp, and Zs without mention of "<noBreak>" */
  348. || (unicode_attributes[ch].name != NULL
  349. && unicode_attributes[ch].category[0] == 'Z'
  350. && (unicode_attributes[ch].category[1] == 'l'
  351. || unicode_attributes[ch].category[1] == 'p'
  352. || (unicode_attributes[ch].category[1] == 's'
  353. && !strstr (unicode_attributes[ch].decomposition,
  354. "<noBreak>")))));
  355. }
  356. static bool
  357. is_cntrl (unsigned int ch)
  358. {
  359. return (unicode_attributes[ch].name != NULL
  360. && (!strcmp (unicode_attributes[ch].name, "<control>")
  361. /* Categories Zl and Zp */
  362. || (unicode_attributes[ch].category[0] == 'Z'
  363. && (unicode_attributes[ch].category[1] == 'l'
  364. || unicode_attributes[ch].category[1] == 'p'))));
  365. }
  366. static bool
  367. is_xdigit (unsigned int ch)
  368. {
  369. #if 0
  370. return is_digit (ch)
  371. || (ch >= 0x0041 && ch <= 0x0046)
  372. || (ch >= 0x0061 && ch <= 0x0066);
  373. #else
  374. /* SUSV2 gives us some freedom for the "xdigit" category, but ISO C 99
  375. takes it away:
  376. 7.25.2.1.12:
  377. The iswxdigit function tests for any wide character that corresponds
  378. to a hexadecimal-digit character (as defined in 6.4.4.1).
  379. 6.4.4.1:
  380. hexadecimal-digit: one of 0 1 2 3 4 5 6 7 8 9 a b c d e f A B C D E F
  381. */
  382. return (ch >= 0x0030 && ch <= 0x0039)
  383. || (ch >= 0x0041 && ch <= 0x0046)
  384. || (ch >= 0x0061 && ch <= 0x0066);
  385. #endif
  386. }
  387. static bool
  388. is_graph (unsigned int ch)
  389. {
  390. return (unicode_attributes[ch].name != NULL
  391. && strcmp (unicode_attributes[ch].name, "<control>")
  392. && !is_space (ch));
  393. }
  394. static bool
  395. is_print (unsigned int ch)
  396. {
  397. return (unicode_attributes[ch].name != NULL
  398. && strcmp (unicode_attributes[ch].name, "<control>")
  399. /* Categories Zl and Zp */
  400. && !(unicode_attributes[ch].name != NULL
  401. && unicode_attributes[ch].category[0] == 'Z'
  402. && (unicode_attributes[ch].category[1] == 'l'
  403. || unicode_attributes[ch].category[1] == 'p')));
  404. }
  405. static bool
  406. is_punct (unsigned int ch)
  407. {
  408. #if 0
  409. return (unicode_attributes[ch].name != NULL
  410. && unicode_attributes[ch].category[0] == 'P');
  411. #else
  412. /* The traditional POSIX definition of punctuation is every graphic,
  413. non-alphanumeric character. */
  414. return (is_graph (ch) && !is_alpha (ch) && !is_digit (ch));
  415. #endif
  416. }
  417. static bool
  418. is_combining (unsigned int ch)
  419. {
  420. /* Up to Unicode 3.0.1 we took the Combining property from the PropList.txt
  421. file. In 3.0.1 it was identical to the union of the general categories
  422. "Mn", "Mc", "Me". In Unicode 3.1 this property has been dropped from the
  423. PropList.txt file, so we take the latter definition. */
  424. return (unicode_attributes[ch].name != NULL
  425. && unicode_attributes[ch].category[0] == 'M'
  426. && (unicode_attributes[ch].category[1] == 'n'
  427. || unicode_attributes[ch].category[1] == 'c'
  428. || unicode_attributes[ch].category[1] == 'e'));
  429. }
  430. static bool
  431. is_combining_level3 (unsigned int ch)
  432. {
  433. return is_combining (ch)
  434. && !(unicode_attributes[ch].combining[0] != '\0'
  435. && unicode_attributes[ch].combining[0] != '0'
  436. && strtoul (unicode_attributes[ch].combining, NULL, 10) >= 200);
  437. }
  438. /* Return the UCS symbol string for a Unicode character. */
  439. static const char *
  440. ucs_symbol (unsigned int i)
  441. {
  442. static char buf[11+1];
  443. sprintf (buf, (i < 0x10000 ? "<U%04X>" : "<U%08X>"), i);
  444. return buf;
  445. }
  446. /* Return the UCS symbol range string for a Unicode characters interval. */
  447. static const char *
  448. ucs_symbol_range (unsigned int low, unsigned int high)
  449. {
  450. static char buf[24+1];
  451. strcpy (buf, ucs_symbol (low));
  452. strcat (buf, "..");
  453. strcat (buf, ucs_symbol (high));
  454. return buf;
  455. }
  456. /* Output a character class (= property) table. */
  457. static void
  458. output_charclass (FILE *stream, const char *classname,
  459. bool (*func) (unsigned int))
  460. {
  461. char table[0x110000];
  462. unsigned int i;
  463. bool need_semicolon;
  464. const int max_column = 75;
  465. int column;
  466. for (i = 0; i < 0x110000; i++)
  467. table[i] = (int) func (i);
  468. fprintf (stream, "%s ", classname);
  469. need_semicolon = false;
  470. column = 1000;
  471. for (i = 0; i < 0x110000; )
  472. {
  473. if (!table[i])
  474. i++;
  475. else
  476. {
  477. unsigned int low, high;
  478. char buf[25];
  479. low = i;
  480. do
  481. i++;
  482. while (i < 0x110000 && table[i]);
  483. high = i - 1;
  484. if (low == high)
  485. strcpy (buf, ucs_symbol (low));
  486. else
  487. strcpy (buf, ucs_symbol_range (low, high));
  488. if (need_semicolon)
  489. {
  490. fprintf (stream, ";");
  491. column++;
  492. }
  493. if (column + strlen (buf) > max_column)
  494. {
  495. fprintf (stream, "/\n ");
  496. column = 3;
  497. }
  498. fprintf (stream, "%s", buf);
  499. column += strlen (buf);
  500. need_semicolon = true;
  501. }
  502. }
  503. fprintf (stream, "\n");
  504. }
  505. /* Output a character mapping table. */
  506. static void
  507. output_charmap (FILE *stream, const char *mapname,
  508. unsigned int (*func) (unsigned int))
  509. {
  510. char table[0x110000];
  511. unsigned int i;
  512. bool need_semicolon;
  513. const int max_column = 75;
  514. int column;
  515. for (i = 0; i < 0x110000; i++)
  516. table[i] = (func (i) != i);
  517. fprintf (stream, "%s ", mapname);
  518. need_semicolon = false;
  519. column = 1000;
  520. for (i = 0; i < 0x110000; i++)
  521. if (table[i])
  522. {
  523. char buf[25+1];
  524. strcpy (buf, "(");
  525. strcat (buf, ucs_symbol (i));
  526. strcat (buf, ",");
  527. strcat (buf, ucs_symbol (func (i)));
  528. strcat (buf, ")");
  529. if (need_semicolon)
  530. {
  531. fprintf (stream, ";");
  532. column++;
  533. }
  534. if (column + strlen (buf) > max_column)
  535. {
  536. fprintf (stream, "/\n ");
  537. column = 3;
  538. }
  539. fprintf (stream, "%s", buf);
  540. column += strlen (buf);
  541. need_semicolon = true;
  542. }
  543. fprintf (stream, "\n");
  544. }
  545. /* Output the width table. */
  546. static void
  547. output_widthmap (FILE *stream)
  548. {
  549. }
  550. /* Output the tables to the given file. */
  551. static void
  552. output_tables (const char *filename, const char *version)
  553. {
  554. FILE *stream;
  555. unsigned int ch;
  556. stream = fopen (filename, "w");
  557. if (stream == NULL)
  558. {
  559. fprintf (stderr, "cannot open '%s' for writing\n", filename);
  560. exit (1);
  561. }
  562. fprintf (stream, "escape_char /\n");
  563. fprintf (stream, "comment_char %%\n");
  564. fprintf (stream, "\n");
  565. fprintf (stream, "%% Generated automatically by gen-unicode-ctype for Unicode %s.\n",
  566. version);
  567. fprintf (stream, "\n");
  568. fprintf (stream, "LC_IDENTIFICATION\n");
  569. fprintf (stream, "title \"Unicode %s FDCC-set\"\n", version);
  570. fprintf (stream, "source \"UnicodeData.txt, PropList.txt\"\n");
  571. fprintf (stream, "address \"\"\n");
  572. fprintf (stream, "contact \"\"\n");
  573. fprintf (stream, "email \"bug-glibc-locales@gnu.org\"\n");
  574. fprintf (stream, "tel \"\"\n");
  575. fprintf (stream, "fax \"\"\n");
  576. fprintf (stream, "language \"\"\n");
  577. fprintf (stream, "territory \"Earth\"\n");
  578. fprintf (stream, "revision \"%s\"\n", version);
  579. {
  580. time_t now;
  581. char date[11];
  582. now = time (NULL);
  583. strftime (date, sizeof (date), "%Y-%m-%d", gmtime (&now));
  584. fprintf (stream, "date \"%s\"\n", date);
  585. }
  586. fprintf (stream, "category \"unicode:2001\";LC_CTYPE\n");
  587. fprintf (stream, "END LC_IDENTIFICATION\n");
  588. fprintf (stream, "\n");
  589. /* Verifications. */
  590. for (ch = 0; ch < 0x110000; ch++)
  591. {
  592. /* toupper restriction: "Only characters specified for the keywords
  593. lower and upper shall be specified. */
  594. if (to_upper (ch) != ch && !(is_lower (ch) || is_upper (ch)))
  595. fprintf (stderr,
  596. "%s is not upper|lower but toupper(0x%04X) = 0x%04X\n",
  597. ucs_symbol (ch), ch, to_upper (ch));
  598. /* tolower restriction: "Only characters specified for the keywords
  599. lower and upper shall be specified. */
  600. if (to_lower (ch) != ch && !(is_lower (ch) || is_upper (ch)))
  601. fprintf (stderr,
  602. "%s is not upper|lower but tolower(0x%04X) = 0x%04X\n",
  603. ucs_symbol (ch), ch, to_lower (ch));
  604. /* alpha restriction: "Characters classified as either upper or lower
  605. shall automatically belong to this class. */
  606. if ((is_lower (ch) || is_upper (ch)) && !is_alpha (ch))
  607. fprintf (stderr, "%s is upper|lower but not alpha\n", ucs_symbol (ch));
  608. /* alpha restriction: "No character specified for the keywords cntrl,
  609. digit, punct or space shall be specified." */
  610. if (is_alpha (ch) && is_cntrl (ch))
  611. fprintf (stderr, "%s is alpha and cntrl\n", ucs_symbol (ch));
  612. if (is_alpha (ch) && is_digit (ch))
  613. fprintf (stderr, "%s is alpha and digit\n", ucs_symbol (ch));
  614. if (is_alpha (ch) && is_punct (ch))
  615. fprintf (stderr, "%s is alpha and punct\n", ucs_symbol (ch));
  616. if (is_alpha (ch) && is_space (ch))
  617. fprintf (stderr, "%s is alpha and space\n", ucs_symbol (ch));
  618. /* space restriction: "No character specified for the keywords upper,
  619. lower, alpha, digit, graph or xdigit shall be specified."
  620. upper, lower, alpha already checked above. */
  621. if (is_space (ch) && is_digit (ch))
  622. fprintf (stderr, "%s is space and digit\n", ucs_symbol (ch));
  623. if (is_space (ch) && is_graph (ch))
  624. fprintf (stderr, "%s is space and graph\n", ucs_symbol (ch));
  625. if (is_space (ch) && is_xdigit (ch))
  626. fprintf (stderr, "%s is space and xdigit\n", ucs_symbol (ch));
  627. /* cntrl restriction: "No character specified for the keywords upper,
  628. lower, alpha, digit, punct, graph, print or xdigit shall be
  629. specified." upper, lower, alpha already checked above. */
  630. if (is_cntrl (ch) && is_digit (ch))
  631. fprintf (stderr, "%s is cntrl and digit\n", ucs_symbol (ch));
  632. if (is_cntrl (ch) && is_punct (ch))
  633. fprintf (stderr, "%s is cntrl and punct\n", ucs_symbol (ch));
  634. if (is_cntrl (ch) && is_graph (ch))
  635. fprintf (stderr, "%s is cntrl and graph\n", ucs_symbol (ch));
  636. if (is_cntrl (ch) && is_print (ch))
  637. fprintf (stderr, "%s is cntrl and print\n", ucs_symbol (ch));
  638. if (is_cntrl (ch) && is_xdigit (ch))
  639. fprintf (stderr, "%s is cntrl and xdigit\n", ucs_symbol (ch));
  640. /* punct restriction: "No character specified for the keywords upper,
  641. lower, alpha, digit, cntrl, xdigit or as the <space> character shall
  642. be specified." upper, lower, alpha, cntrl already checked above. */
  643. if (is_punct (ch) && is_digit (ch))
  644. fprintf (stderr, "%s is punct and digit\n", ucs_symbol (ch));
  645. if (is_punct (ch) && is_xdigit (ch))
  646. fprintf (stderr, "%s is punct and xdigit\n", ucs_symbol (ch));
  647. if (is_punct (ch) && (ch == 0x0020))
  648. fprintf (stderr, "%s is punct\n", ucs_symbol (ch));
  649. /* graph restriction: "No character specified for the keyword cntrl
  650. shall be specified." Already checked above. */
  651. /* print restriction: "No character specified for the keyword cntrl
  652. shall be specified." Already checked above. */
  653. /* graph - print relation: differ only in the <space> character.
  654. How is this possible if there are more than one space character?!
  655. I think susv2/xbd/locale.html should speak of "space characters",
  656. not "space character". */
  657. if (is_print (ch) && !(is_graph (ch) || /* ch == 0x0020 */ is_space (ch)))
  658. fprintf (stderr,
  659. "%s is print but not graph|<space>\n", ucs_symbol (ch));
  660. if (!is_print (ch) && (is_graph (ch) || ch == 0x0020))
  661. fprintf (stderr,
  662. "%s is graph|<space> but not print\n", ucs_symbol (ch));
  663. }
  664. fprintf (stream, "LC_CTYPE\n");
  665. output_charclass (stream, "upper", is_upper);
  666. output_charclass (stream, "lower", is_lower);
  667. output_charclass (stream, "alpha", is_alpha);
  668. output_charclass (stream, "digit", is_digit);
  669. output_charclass (stream, "outdigit", is_outdigit);
  670. output_charclass (stream, "blank", is_blank);
  671. output_charclass (stream, "space", is_space);
  672. output_charclass (stream, "cntrl", is_cntrl);
  673. output_charclass (stream, "punct", is_punct);
  674. output_charclass (stream, "xdigit", is_xdigit);
  675. output_charclass (stream, "graph", is_graph);
  676. output_charclass (stream, "print", is_print);
  677. output_charclass (stream, "class \"combining\";", is_combining);
  678. output_charclass (stream, "class \"combining_level3\";", is_combining_level3);
  679. output_charmap (stream, "toupper", to_upper);
  680. output_charmap (stream, "tolower", to_lower);
  681. output_charmap (stream, "map \"totitle\";", to_title);
  682. output_widthmap (stream);
  683. fprintf (stream, "END LC_CTYPE\n");
  684. if (ferror (stream) || fclose (stream))
  685. {
  686. fprintf (stderr, "error writing to '%s'\n", filename);
  687. exit (1);
  688. }
  689. }
  690. int
  691. main (int argc, char * argv[])
  692. {
  693. if (argc != 3)
  694. {
  695. fprintf (stderr, "Usage: %s UnicodeData.txt version\n", argv[0]);
  696. exit (1);
  697. }
  698. fill_attributes (argv[1]);
  699. output_tables ("unicode", argv[2]);
  700. return 0;
  701. }