1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197
|
#define _POSIX_C_SOURCE 2
#include <ctype.h>
#include <errno.h>
#include <glob.h>
#include <printf.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <values.h>
#ifndef __BYTE_ORDER
#ifdef BYTE_ORDER
#define __BYTE_ORDER BYTE_ORDER
#define __LITTLE_ENDIAN LITTLE_ENDIAN
#define __BIG_ENDIAN BIG_ENDIAN
#define __PDP_ENDIAN PDP_ENDIAN
#else
#error __BYTE_ORDER undefined.
#endif
#endif
#include "2UTF.h"
static char version[] = " 2UTF V" VERSION " \n";
static char blurb[] =
" © Copyright 1997, 1998 by Ričardas Čepas <rch@pub.osf.lt> and others. \n"
" Copying policy: BSD style. \n"
" See file ``copyright'' provided with the 2UTF distribution. \n"
" No warranty. Use at your own risk. \n";
/* There are such functions defined in libc5 but it don't work for me :-( */
#ifndef USE_LIBC
#define mbtowc our_mbtowc
#define wctomb our_wctomb
#endif
#ifndef CONFIG_PATHNAMES
#define CONFIG_PATHNAMES "/usr/local/etc/2UTF.config /usr/etc/2UTF.config /etc/2UTF.config"
#endif
#define max_string_length 1178
#ifndef ALIASES
#define ALIASES "/var/local/lib/2UTF.aliases"
#endif
#ifndef PATH
#define PATH "/usr/local/share/i18n/charmaps"
#endif
#ifndef PATH2
#define PATH2 "/usr/share/i18n/charmaps"
#endif
#ifndef PATH3
#define PATH3 "/usr/share/i18n/charmap"
#endif
#ifndef MAX_PATHNAMES
#define MAX_PATHNAMES 24
#endif
#ifndef MAX_EXT_CHARSETS
#define MAX_EXT_CHARSETS 50
#endif
char aliases_pathname[] = ALIASES;
char default_charmap_format[] = " %*s /x%2x <U%X> ";
char *charmap_format[] =
{default_charmap_format, " 0x%x 0x%X ", " 0x%x 0x%X "};
unsigned char *compiled_paths[] =
{PATH "/", PATH2 "/", PATH3 "/", ""};
const int compiled_paths_number = 3;
int paths_number = 3;
unsigned char *read_paths[MAX_PATHNAMES];
unsigned char **paths = compiled_paths;
int ext_charsets_number = 0;
const char FILENAME[] = "FILENAME";
const char IO_err[] = "I/O error.";
const char IO_err_reading_config[] = "I/O error reading configuration file.";
const char ambig_opt[] = "ambiguous option.";
const char avail_charmaps[] = "charmaps and aliases I can handle and available in cache:";
const char avail_ext_charsets[] = "charsets I can handle via external filters:";
const char bad_charmap_format[] = "charmap file format error ?";
const char bad_line[] = "BAD LINE in aliases file:";
const char buffer_overflow[] = "buffer overflow by";
/*const char bad_sscanf_format_string[]= "bad sscanf(3) format string"; */
const char can_not_exec[] = "can't execute such command or fork subprocess:";
const char can_not_find_alias[] = "can't find such alias:";
const char can_not_open[] = "can't open for reading file";
const char can_not_open_any[] = " -- can't open matching files.";
const char can_not_create[] = "can't create file";
const char for_[] = "for";
const char help[] = "%s"
" Converts char-sets to and from Unicode. Decodes MIME text messages. \n"
"\n"
" Usage: \n"
"2UTF [-short_options] [--long_option ...] [charmap_file_or_alias] <input >output \n"
"fromUTF ... \n"
" If exact match for <charmap_file_or_alias> (converted to uppercase, \n" \
"`-' and '_' ignored) isn't found *<charmap_file_or_alias>* glob pattern \n" \
"is used. Without <charmap_file_or_alias> mail message is assumed.\n"
" Options: \n"
" -- stops option checking for the rest of the command line \n"
" -2 --UCS-2 --ucs-2 2 byte wide characters \n"
" -4 --UCS-4 --ucs-4 4 byte wide characters \n"
/*" -w --UCS-wchar_t --ucs-wchar_t sizeof(wchar_t) byte wide characters \n" */
" -8 --UTF-8 --utf-8 (default) multibyte characters \n"
" -C --create-aliases (re)creates aliases database \n"
" -c FILENAME --charmap-file=FILENAME \n"
" -d[N] --debug[=N] debug level (1-9), default 1 \n"
" -f[FORMAT] --format[=FORMAT] sscanf(3) format string for reading charmap \n"
"file. Lines beginning with %% or # are ignored. Default is \"%s\" \n"
" -o --forward (default for 2UTF) converts to Unicode \n"
" -H --html &<>\" appeared after approximations are escaped\n"
" -h -? --? -help --help this help \n"
" -hp -?p --help=pathnames outputs various pathnames and directories \n"
" -l --list-charmaps lists charmaps & aliases and exits \n"
" -r --reverse (default for fromUTF) tries convert back to the single bytes \n"
" -W --show-charmap shows glyphs in charmap order \n"
" -S --spit-glyphs shows glyphs in console font (F000-F1FF) \n"
" -S... --spit-glyphs=[min][-][max] shows glyphs at given hex range. \n"
"Allowed range is from 0 to 7FFFFFFF. \n"
" -s --switch-to-UTF-8 outputs <ESC>%%G to stderr for switching \n"
"current virtual console to UTF-8 mode \n"
" -u[X] --unknown-char[=X] substitute X for unknown characters. \n"
"Default is '%c' (0x%.2X). \n"
" -v --verbose \n"
" -V --version --blurb shows version and copyright info. \n"
" Rightmost option takes precedence. Long options may be abbreviated. \n"
;
const char incomplete_charmap[] = "warning: incomplete charmap definiton";
const char internal_err[] = "internal error ?";
const char long_file[] = "warning: long charmap file.";
const char another_match[] = "warning: another match for this alias";
const char more_help[] = "``2UTF -h'' gives more information. \n";
const char multiply_matches[] = "warning: multiply matches for glob pattern";
const char needs_update[] = "aliases database needs update.";
const char no_charmaps[] = "no charmap files found.";
const char no_pathnames[] = "no pathnames found in configuration file.";
const char out_of_mem[] = "out of memory !";
const char paths_help_config_pathname[] = " Looks for configuration file as: \n";
const char paths_help_compiled[] = " If no directories in configuration file " \
"are found looks for charmap files in: \n";
const char paths_help_used[] = " Currently looks for charmap files in: \n";
const char paths_help_aliases[] =
" Aliases are cached in: \n"
"``" ALIASES "'' \n"
" \n";
const char short_help[] = \
"Usage: 2UTF|fromUTF [-short_options] [--long_option ...] [charset] <in >out \n";
const char too_many_pathnames[] = "too many pathnames in configuration file.";
const char too_many_ext_charsets[] = \
"too many charset definitions in configuration file.";
const char unexp_EOF[] = "unexpected end of file.";
const char unimplemented[] = "Sorry, this is not implemented.";
const char unknown_opt[] = "unknown option.";
const char using[] = "using";
const char will_use[] = "will use";
int Debug = FALSE, Mail = FALSE, reverse = FALSE, verbose = FALSE;
struct charset_type
unknown_charset = {stdout, "", NO, UNKNOWN, NULL},
USASCII_charset = {stdout, "us-ascii", IS, USASCII, NULL},
UTF8_charset = {stdout, "UTF-8", IS, UTF8, NULL}
;
struct charset_type *charset_p = &unknown_charset;
struct line_buf_type line =
{NULL, 0};
struct
{
unsigned char *names, *to_UTF, *from_UTF;
enum USASCII_is_subset_type USASCII_is_subset;
}
ext_charsets[MAX_EXT_CHARSETS + 1];
wchar_t unknown_wchar = 0xFFFD;
inline int our_wctomb (char *s, unsigned long wc);
inline int our_mbtowc (unsigned long *p, char *s, unsigned n);
inline enum charmap_file_format_type
Charmap_file_format_type (char *pathname)
{
char *ptr;
if (pathname && (ptr = strrchr (pathname, '.')))
{
if (strcmp (".TXT", ptr) == 0 || Strcasecmp (".x", ptr) == 0)
return (TXT);
}
return (DEFAULT);
}
void
Close_pipe (void)
{
if (charset_p->type == BUF_PIPE || charset_p->type == NON_BUF_PIPE)
pclose (charset_p->pipe);
}
char *
Convert (char *to, char *from)
{
register length;
if (charset_p->type == KNOWN)
{
while (*from)
{
length = wctomb (to, charset_p->charmap[(int) (unsigned char) *from]);
if (length != -1)
to += length;
else if (*(to += wctomb (to, unknown_wchar)) == -1)
Error ("Bad unknown_wchar value");
from++;
}
return (to);
}
else
return (Stpcpy (to, from));
}
int
Create_aliases (void)
{
FILE *aliases_file;
struct charmap_file_type charmap_file;
char alias[max_string_length], *charmap_filename, *ptr, *pathname, \
s[max_string_length];
glob_t glob_buffer =
{0, NULL, 0, 0};
int glob_flags = 0, length;
register c, index;
wchar_t charmap[256], unknown_wchar = 0;
if ((aliases_file = fopen (aliases_pathname, "wt")) == NULL)
{
/* fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_create, \
aliases_pathname); */
return (0);
}
for (length = strlen (paths[0]), index = 1; index < paths_number; index++)
if (strlen (paths[index]) > strlen (paths[index - 1]))
length = strlen (paths[index]);
pathname = xmalloc (length + 5);
if (Debug >= 9)
fprintf (stderr, "2UTF: malloc (%i) \n", (length + 5));
for (index = 0; index < paths_number; index++)
{
strcat (strcpy (pathname, paths[index]), "*");
c = glob (pathname, glob_flags, NULL, &glob_buffer);
glob_flags |= GLOB_APPEND;
switch (c)
{
case GLOB_NOSPACE:
Error (out_of_mem);
default:
if (Debug >= 5)
fprintf (stderr, "2UTF: glob() returned %i \n", c);
}
}
free (pathname);
if (glob_buffer.gl_pathc < 1)
Error (no_charmaps);
for (index = 0; index < glob_buffer.gl_pathc; index++)
{
if ((charmap_file.stream = fopen (glob_buffer.gl_pathv[index], "rt")) \
== NULL)
{
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_open, \
glob_buffer.gl_pathv[index]);
}
else
{
charmap_file.format = \
Charmap_file_format_type (glob_buffer.gl_pathv[index]);
c = Get_charmap (charmap, &charmap_file, unknown_wchar);
switch (c)
{
case err_short:
if (verbose)
fprintf (stderr, "2UTF: %s: \n ``%s'' \n", \
incomplete_charmap, glob_buffer.gl_pathv[index]);
case err_long:
if (Debug >= 8 && c == err_long)
fprintf (stderr, "2UTF: %s ``%s'' \n", long_file, \
glob_buffer.gl_pathv[index]);
case OK:
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", using, \
glob_buffer.gl_pathv[index]);
if ((ptr = strrchr (glob_buffer.gl_pathv[index], '/')) == NULL)
ptr = glob_buffer.gl_pathv[index];
else
ptr++;
charmap_filename = xstrdup (ptr);
if (charmap_file.format != DEFAULT \
&&strrchr (charmap_filename, '.'))
*strrchr (charmap_filename, '.') = '\0';
fprintf (aliases_file, "%s %s ", \
glob_buffer.gl_pathv[index], Strtoupper (charmap_filename));
rewind (charmap_file.stream);
while (fgets (s, max_string_length, charmap_file.stream) != NULL)
if (s[0] == '%' && sscanf ((s + 1), " alias %s ", alias) >= 1)
fprintf (aliases_file, "%s ", alias);
fputs (" \n", aliases_file);
if (!feof (charmap_file.stream))
fprintf (stderr, "\a2UTF: %s ``%s'' \n", IO_err, \
glob_buffer.gl_pathv[index]);
break;
case err_few_chars:
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", bad_charmap_format, \
glob_buffer.gl_pathv[index]);
break;
case err_IO:
fprintf (stderr, "\a2UTF: %s ``%s'' \n", IO_err, \
glob_buffer.gl_pathv[index]);
break;
case err_internal:
fprintf (stderr, "\a2UTF: %s ``%s'' \n", internal_err, \
glob_buffer.gl_pathv[index]);
break;
}
if (EOF == fclose (charmap_file.stream))
Error (IO_err);
}
}
if (EOF == fclose (aliases_file))
Error (IO_err);
globfree (&glob_buffer);
return (1);
}
void
Error (const char *message)
{
register c;
fprintf (stderr, "2UTF: %s \n", message);
if (Mail)
while ((c = getc (stdin)) != EOF)
putchar (c);
exit (1);
}
FILE *
Fopen_charmap (char *charmap_filename, \
struct charmap_file_type *charmap_file_p)
{
FILE *aliases_file;
char *charmap_name, *pathname, *ptr, *s = NULL;
glob_t glob_buffer =
{0, NULL, 0, 0};
int found_alias = FALSE, can_not_create_aliases_file = FALSE;
register c, index, length;
size_t s_length = 0;
charmap_file_p->stream = NULL;
charmap_file_p->format = DEFAULT;
if (strchr (charmap_filename, '/') != NULL)
return (fopen (charmap_filename, "rt"));
if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL)
{
fprintf (stderr, "2UTF: %s %s \n", can_not_open, aliases_pathname);
if (!Create_aliases ())
{
can_not_create_aliases_file = 1;
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_create, \
aliases_pathname);
}
else if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL)
Error (internal_err);
}
charmap_name = xmalloc (strlen (charmap_filename) + 5);
if (Debug >= 9)
fprintf (stderr, "2UTF: malloc (%i) \n", (strlen (charmap_filename) + 5));
Strtoupper (\
strcat (strcat (strcpy (charmap_name, " "), charmap_filename), " "));
if (aliases_file != NULL)
{
for (index = 0; index < 2; index++)
{
if (Debug >= 3)
fprintf (stderr, "2UTF: Looking for alias ``%s'' \n", charmap_name);
while (Getline ((unsigned char **) &s, &s_length, aliases_file) \
!= (size_t) -1)
if ((strchr (s, ' ') != NULL) && \
(Str_str (strchr (s, ' '), charmap_name) != NULL))
{
strtok (s, "\n");
if (!found_alias)
{
if (verbose)
fprintf (stderr, "2UTF: %s - %s: \n ``%s'' \n",
charmap_name, will_use, s);
charmap_file_p->stream = fopen (ptr = strtok (s, " "), "rt");
charmap_file_p->format = Charmap_file_format_type (ptr);
}
else if (verbose)
fprintf (stderr, "2UTF: %s - %s: \n ``%s'' \n",
charmap_name, another_match, s);
else
fprintf (stderr, "2UTF: %s. \n", another_match);
found_alias = 1;
}
if (!feof (aliases_file))
if (ferror (aliases_file))
Error (IO_err);
else
Error (out_of_mem);
if (!found_alias)
{
if (index == 0)
{
clearerr (aliases_file);
rewind (aliases_file);
memmove (charmap_name, &charmap_name[1], \
strlen (charmap_name) - 1);
strtok (charmap_name, " ");
}
}
else
break;
}
free (s);
if (EOF == fclose (aliases_file))
Error (IO_err);
}
if (!found_alias || charmap_file_p->stream == NULL)
{
if (charmap_name[0] == ' ')
{
memmove (charmap_name, &charmap_name[1], strlen (charmap_name) - 1);
strtok (charmap_name, " ");
}
if (Debug >= 3)
fprintf (stderr, "2UTF: Looking for file ``%s'' \n", charmap_name);
for (length = strlen (paths[0]), index = 1; index < paths_number; index++)
if (strlen (paths[index]) > strlen (paths[index - 1]))
length = strlen (paths[index]);
pathname = xmalloc (length + strlen (charmap_name) + 5);
if (Debug >= 9)
fprintf (stderr, "2UTF: malloc (%i) \n", \
(length + strlen (charmap_name) + 5));
for (index = 0; index < paths_number; index++)
{
strcat (strcpy (pathname, paths[index]), charmap_name);
if (Debug >= 3)
fprintf (stderr, "2UTF: Looking for ``%s'' \n", pathname);
if ((charmap_file_p->stream = fopen (pathname, "rt")) != NULL)
{
charmap_file_p->format = Charmap_file_format_type (pathname);
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", using, pathname);
break;
}
}
if (charmap_file_p->stream == NULL)
{
for (index = 0; index < paths_number; index++)
{
strcat (strcpy (pathname, paths[index]), "*");
strcat (strcat (pathname, charmap_name), "*");
if (Debug >= 3)
fprintf (stderr, "2UTF: Looking for ``%s'' \n", pathname);
/*c = glob (pathname, GLOB_NOSORT, NULL, &glob_buffer); */
c = glob (pathname, 0, NULL, &glob_buffer);
switch (c)
{
case 0:
if (glob_buffer.gl_pathc >= 1)
{
if (glob_buffer.gl_pathc >= 2)
fprintf (stderr, "2UTF: %s ``%s'' (%i). \n", \
multiply_matches, pathname, glob_buffer.gl_pathc);
if ((charmap_file_p->stream = \
fopen (glob_buffer.gl_pathv[0], "rt")) != NULL)
{
charmap_file_p->format = \
Charmap_file_format_type (glob_buffer.gl_pathv[0]);
if (glob_buffer.gl_pathc != 1)
fprintf (stderr, "2UTF: %s ``%s'' \n", using, \
glob_buffer.gl_pathv[0]);
goto break_for;
}
else if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_open, \
glob_buffer.gl_pathv[0]);
}
break;
case GLOB_NOSPACE:
Error (out_of_mem);
default:
if (Debug >= 5)
fprintf (stderr, "2UTF: glob() returned %i \n", c);
}
}
break_for:
globfree (&glob_buffer);
}
free (pathname);
free (charmap_name);
if (!found_alias && verbose && charmap_file_p->stream != NULL)
fprintf (stderr, "2UTF: %s \n", needs_update);
}
if (found_alias && charmap_file_p->stream == NULL)
fprintf (stderr, "2UTF: %s \n", needs_update);
if (((found_alias && charmap_file_p->stream == NULL) || \
(!found_alias && charmap_file_p->stream != NULL)) \
&&strcmp (charmap_format[0], default_charmap_format) == 0)
if (!can_not_create_aliases_file)
if (!Create_aliases ())
{
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_create, \
aliases_pathname);
}
else if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL)
Error (internal_err);
else if (EOF == fclose (aliases_file))
Error (IO_err);
return (charmap_file_p->stream);
}
/* Bits Hex Min Hex Max Byte Sequence in Binary
* 7 00000000 0000007f 0vvvvvvv
* 11 00000080 000007FF 110vvvvv 10vvvvvv
* 16 00000800 0000FFFF 1110vvvv 10vvvvvv 10vvvvvv
* 21 00010000 001FFFFF 11110vvv 10vvvvvv 10vvvvvv 10vvvvvv
* 26 00200000 03FFFFFF 111110vv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv
* 31 04000000 7FFFFFFF 1111110v 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv 10vvvvvv
*/
inline int
Freadmb (wchar_t * wchar_p, FILE * stream)
{
char mb[6];
register length, i, c;
if ((c = getc (stream)) == EOF)
return (EOF);
else if ((c & 0x80) == 0)
{
*wchar_p = c;
return (1);
}
else if ((c & 0x40) == 0)
return (0);
else if ((c & 0x20) == 0)
length = 2;
else if ((c & 0x10) == 0)
length = 3;
else if ((c & 0x8) == 0)
length = 4;
else if ((c & 0x4) == 0)
length = 5;
else if ((c & 0x2) == 0)
length = 6;
else
return (0);
mb[0] = c;
for (i = 1; i < length; i++)
{
if ((c = getc (stream)) == EOF)
return (EOF);
else if ((c & 0xC0) != 0x80) /* !=10xx xxxx */
{
ungetc (c, stream);
return (0);
}
else
{
mb[i] = c;
}
}
if (mbtowc (wchar_p, mb, length) != length)
return (0);
else
return (1);
}
extern size_t Getdelim (unsigned char **linebuf_p, \
size_t *bufsize_p, int delimiter, FILE *stream)
{
size_t current_ind = 0;
int c;
do
{
if (*bufsize_p <= current_ind + 10)
*linebuf_p = xrealloc (*linebuf_p, *bufsize_p += 256);
if ((c = getc (stream)) == EOF \
|| ((*linebuf_p)[current_ind++] = c) == delimiter)
break;
}
while (TRUE);
(*linebuf_p)[current_ind] = '\0';
return (current_ind ? current_ind : -1);
}
int
Get_charmap (wchar_t * charmap, \
struct charmap_file_type *charmap_file_p, wchar_t unknown_wchar)
{
char s[max_string_length];
int index;
if (charmap_file_p->stream == NULL)
return (err_internal);
for (index = 0; index < 256; index++)
charmap[index] = unknown_wchar;
for (index = 0; index < MAXINT; index++)
{
do
if (fgets (s, max_string_length, charmap_file_p->stream) == NULL)
if (feof (charmap_file_p->stream))
if (index < 20)
return (err_few_chars);
else if (index < 256)
return (err_short);
else if (index == 256)
return (OK);
else
return (err_long);
else
return (err_IO);
while (!Parse_string (charmap_format[charmap_file_p->format], charmap, s));
}
return (err_internal);
}
void
Help (unsigned char default_unknown_char)
{
fprintf (stderr, help, version, charmap_format[DEFAULT], default_unknown_char, \
(unsigned int) default_unknown_char);
}
void
Long2short_options (int argc, char **argv, \
const struct long2short_options_type *long2short_options)
{
char *equal_ptr;
int arg, long_opt_ind, matched_long_opt;
for (arg=1; arg < argc; arg++)
{
if (argv[arg][0]=='-')
if (argv[arg][1]=='-')
{
if (argv[arg][2]=='\0')
break;
if ((equal_ptr=strchr (argv[arg], '=')))
*(equal_ptr) = '\0';
matched_long_opt = -1;
for (long_opt_ind=0; long2short_options[long_opt_ind].long_option!=NULL; \
long_opt_ind++)
{
if (Str_has_prefix (long2short_options[long_opt_ind].long_option, \
argv[arg]+2)==0)
if (matched_long_opt == -1)
matched_long_opt = long_opt_ind;
else if (long2short_options[long_opt_ind].short_option \
!= long2short_options[matched_long_opt].short_option)
{
fprintf (stderr, "2UTF: ``%s'' - %s \n", argv[arg], ambig_opt);
exit (1);
}
}
if (matched_long_opt == -1)
{
fprintf (stderr, "2UTF: ``%s'' - %s \n", argv[arg], unknown_opt);
exit (1);
}
else
{
*(argv[arg]+1)=long2short_options[matched_long_opt].short_option;
if (equal_ptr)
memmove (argv[arg]+2, equal_ptr+1, strlen (equal_ptr+1) + 1);
else
*(argv[arg]+2)='\0';
}
}
else if (argv[arg][1]=='?') \
argv[arg][1]='h';
else if (strcmp (argv[arg]+1, "help")==0)
argv[arg][1]='h', argv[arg][2]='\0';
}
}
#define Print_approx_Macro(c, wchar, unknown_char) \
{ \
/*#include "approx.c"*/ \
int index = 257; \
\
if ((wchar) != 0xFEFF) /* skip byte order mark */ \
{ \
if ((c) == 1) \
{ \
for (index = 0; index < 256; index++) \
if (charset_p->charmap[index] == (wchar)) \
{ \
if (putc (index, charset_p->pipe) == EOF) \
Error (IO_err); \
break; \
} \
} \
if (index >= 256) \
if ((size_t) (wchar) \
<NUM_OF_ELEM (approx2ascii) \
&&approx2ascii[wchar][0] != '\0') \
{ \
for (index = 0; approx2ascii[wchar][index] != '\0' \
&& index < (int) NUM_OF_ELEM (approx2ascii[0]); index++) \
if (charset_p->charmap[approx2ascii[wchar][index]] \
!=approx2ascii[wchar][index]) \
{ \
index = -1; \
break; \
} /* check for non US-ASCII compatibles */ \
if (index != -1) \
if (html_doc) \
for (index = 0; approx2ascii[wchar][index] != '\0' \
&& index < (int) NUM_OF_ELEM (approx2ascii[0]); index++) \
Putchar_html_Macro (approx2ascii[wchar][index]) \
else \
fprintf (charset_p->pipe, "%.*s", \
(int) NUM_OF_ELEM (approx2ascii[0]), approx2ascii[wchar]); \
else if (putc (unknown_char, charset_p->pipe) == EOF) \
Error (IO_err); \
} \
else if (putc (unknown_char, charset_p->pipe) == EOF) \
Error (IO_err); \
} \
}
/*if ((((int)some) < 0x20) || ((((int)some) > 0x7F) && (((int)some) < 0xA0)))*/
#define Putchar_html_Macro(some) \
{ \
int exit_code; \
\
switch (some) \
{ \
case '"': \
exit_code = fputs(""", charset_p->pipe); \
break; \
case '&': \
exit_code = fputs("&", charset_p->pipe); \
break; \
case '<': \
exit_code = fputs("<", charset_p->pipe); \
break; \
case '>': \
exit_code = fputs(">", charset_p->pipe); \
break; \
default: \
exit_code = putc(some, charset_p->pipe); \
} \
if (exit_code == EOF) \
Error (IO_err); \
}
void
Print_paths (void)
{
int i;
unsigned char *config_pathname = CONFIG_PATHNAMES;
fprintf (stderr, paths_help_config_pathname);
do
fprintf (stderr, "``%s'' \n", config_pathname);
/* check for double null terminator */
while (*(config_pathname = strchr (config_pathname, '\0') + 1) != '\0');
fprintf (stderr, paths_help_compiled);
for (i = 0; i < compiled_paths_number; i++)
fprintf (stderr, "``%s'' \n", compiled_paths[i]);
fprintf (stderr, paths_help_used);
for (i = 0; i < paths_number; i++)
fprintf (stderr, "``%s'' \n", paths[i]);
fprintf (stderr, paths_help_aliases);
}
int
List_charmaps ()
{
FILE *aliases_file;
char *ptr, s[max_string_length];
int index;
if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL)
{
fprintf (stderr, "2UTF: %s %s", can_not_open, aliases_pathname);
if (!Create_aliases ())
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_create, \
aliases_pathname);
else if ((aliases_file = fopen (aliases_pathname, "rt")) == NULL)
Error (internal_err);
}
if (aliases_file == NULL)
return (0);
fprintf (stderr, "\n2UTF: %s \n\n", avail_charmaps);
while (fgets (s, max_string_length, aliases_file) != NULL)
if ((ptr = strchr (s, ' ')) == NULL)
fprintf (stderr, "\n2UTF: %s ``%s'' \n", bad_line, s);
else
fprintf (stderr, "%s", ptr + 1);
if (EOF == fclose (aliases_file))
Error (IO_err);
fprintf (stderr, "\n2UTF: %s \n\n", avail_ext_charsets);
for (index = 0; index < ext_charsets_number; index++)
fprintf (stderr, "%s \n", ext_charsets[index].names);
return (1);
}
inline int
Look_for_ext_charset (enum USASCII_is_subset_type USASCII_is_subset)
{
FILE *ret;
unsigned char *charset_name, *command_line, *cp2;
int index;
*(charset_name = xmalloc (strlen (charset_p->name) + 3)) = ' ';
*(cp2 = Stpcpy (charset_name + 1, charset_p->name)) = ' ';
*(cp2 + 1) = '\0';
Strtoupper (charset_name);
if (Debug >= 3)
fprintf (stderr, "2UTF: Looking for name " \
"``%s'' in configuration file \n", charset_name);
for (index = 0; index < ext_charsets_number; index++)
{
if (Str_str (ext_charsets[index].names, charset_name))
{
charset_p->USASCII_is_subset = ext_charsets[index].USASCII_is_subset;
if (reverse)
command_line = ext_charsets[index].from_UTF;
else
command_line = ext_charsets[index].to_UTF;
if (command_line == NULL)
break;
if (command_line[0] == '%')
{
command_line++;
charset_p->type = NON_BUF_PIPE;
}
else
charset_p->type = BUF_PIPE;
if (USASCII_is_subset == IS && charset_p->USASCII_is_subset == NO)
{
charset_p = &USASCII_charset;
return (TRUE);
}
ret = popen (command_line, "w");
if (Debug >= 4)
fprintf (stderr, "2UTF: popen (\"%s\", \"w\") returned %lx \n", \
command_line, (unsigned long int) ret);
if (ret == NULL)
{
if (verbose)
fprintf (stderr, "2UTF: %s \n ``%s'' \n ", \
can_not_exec, command_line);
return (EOF);
}
else
{
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", using, \
ext_charsets[index].names);
/* For the output of interractive programms */
setvbuf (ret, NULL, Mail ? _IOLBF : _IONBF, 0);
charset_p->pipe = ret;
return (TRUE);
}
}
}
return (FALSE);
}
void
Output_consumed_chars (unsigned char *space_between_enc_words, \
unsigned char *charset_name, \
size_t charset_name_length, unsigned char *encoding)
{
int index = -1;
unsigned char *charp, *encp;
while (space_between_enc_words[++index])
PUTC_IN_UTF8 (space_between_enc_words[index]);
space_between_enc_words[0] = '\0';
charp = charset_name;
PUTC_IN_UTF8 ('=');
PUTC_IN_UTF8 ('?');
while ((size_t) (charp - charset_name) < charset_name_length)
{
PUTC_IN_UTF8 (*charp);
charp++;
}
if ((encp = encoding))
{
PUTC_IN_UTF8 ('?');
while (*encp)
{
PUTC_IN_UTF8 (*encp);
encp++;
}
}
}
inline int
Parse_string (char *charmap_format_string, wchar_t * charmap, char *s)
{
unsigned int index;
unsigned long unichar;
if (s[0] == '#' || s[0] == '%')
return (0);
if (sscanf (s, charmap_format_string, &index, &unichar) == 2)
{
charmap[index] = (wchar_t) unichar;
if (Debug > 8)
fprintf (stderr, "2UTF: ``%s'' --> 0x%.2X is U+%.4lX.\n" \
,s, index, charmap[index]);
return (1);
}
else
return (0);
}
inline void
Pipe_to_UTF8 (FILE * in_stream)
{
register c;
if (charset_p->type == KNOWN)
while ((c = getc (in_stream)) != EOF)
Putc_in_UTF8 (c, charset_p->pipe);
else
while ((c = getc (in_stream)) != EOF)
if (putc (c, charset_p->pipe) == EOF)
Error (IO_err);
if (ferror (in_stream))
Error (IO_err);
}
inline int
Putc_in_UTF8 (int c, FILE * out_stream)
{
char s[12];
register length;
length = wctomb (s, charset_p->charmap[c]);
if (length != -1)
{
if (fwrite (s, 1, length, out_stream) != (size_t) length)
Error (IO_err);
}
else if (putc (0x80, out_stream) == EOF)
Error (IO_err);
return (c);
}
int
Read_config_file ()
{
FILE *config_file;
enum
{
PATHS, CHARSETS
}
state;
size_t size;
unsigned char *config_pathnames = CONFIG_PATHNAMES;
unsigned char *config_pathname = config_pathnames;
unsigned char *cp, *cp2, *nl;
/* check for double null terminator */
while (*(config_pathname = strchr (config_pathname, '\0') + 1) != '\0');
config_pathname = config_pathnames;
do
{
if (Debug > 4)
fprintf (stderr, "2UTF: Looking for ``%s'' \n", config_pathname);
if ((config_file = fopen (config_pathname, "rt")) == NULL)
if (*(config_pathname = strchr (config_pathname, '\0') + 1) == '\0')
return (FALSE);
}
while (config_file == NULL);
if (Debug > 2)
fprintf (stderr, "2UTF: Opened configuration file ``%s'' \n", \
config_pathname);
if ((size = Getdelim (&line.buf, &line.length, '\0', \
config_file)) == (size_t) -1 || EOF == fclose (config_file))
Error (IO_err_reading_config);
cp = line.buf;
paths_number = 0;
paths = read_paths;
ext_charsets_number = 0;
state = PATHS;
do
{
if ((nl = strchr (cp, '\n')) != NULL)
*nl = '\0';
if (*cp != '#')
{
switch (state)
{
case PATHS:
if ((cp = strtok (cp, " \t\n")))
{
if (Strcasecmp (cp, "[charsets]") == 0)
{
state = CHARSETS;
}
else
{
if (paths_number >= MAX_PATHNAMES)
Error (too_many_pathnames);
paths[paths_number++] = xstrdup (cp);
if (Debug > 2)
fprintf (stderr, \
"2UTF: Pathname for charmaps in " \
"configuration file: \n " \
"``%s'' \n", paths[paths_number - 1]);
}
}
break;
case CHARSETS:
{
if (ext_charsets_number >= MAX_EXT_CHARSETS)
Error (too_many_ext_charsets);
ext_charsets[ext_charsets_number].USASCII_is_subset = NO;
if (strncmp (cp += strspn (cp, " \t"), "[US-ASCII_is_subset]", \
strlen ("[US-ASCII_is_subset]")) == 0)
{
cp += strlen ("[US-ASCII_is_subset]") + 1;
ext_charsets[ext_charsets_number].USASCII_is_subset = IS;
}
*(cp2 = ext_charsets[ext_charsets_number].names = \
xmalloc (strlen (cp) + 3)) = ' ';
*(cp2 = Stpcpy (cp2 + 1, cp)) = ' ';
*(cp2 + 1) = '\0';
if (!strtok (cp, " \t\n") || nl == NULL)
break;
if (Debug > 2)
{
fprintf (stderr, "2UTF: Charset names: ``%s'' \n", \
ext_charsets[ext_charsets_number].names);
if (ext_charsets[ext_charsets_number].USASCII_is_subset \
== IS)
fprintf (stderr, "2UTF: US-ASCII is subset " \
"of this charset. \n");
}
Strtoupper (cp2 = ext_charsets[ext_charsets_number].names);
while (*cp2++)
if (*cp2 == '\t')
*cp2 = ' ';
cp = nl + 1;
if ((nl = strchr (cp, '\n')) != NULL)
*nl = '\0';
ext_charsets[ext_charsets_number].to_UTF = xstrdup (cp);
if (!strtok (cp, " \t\n"))
ext_charsets[ext_charsets_number].to_UTF = NULL;
else if (Debug > 2)
fprintf (stderr, "2UTF: To UTF-8: ``%s'' \n", \
ext_charsets[ext_charsets_number].to_UTF);
if (nl != NULL)
{
cp = nl + 1;
if ((nl = strchr (cp, '\n')) != NULL)
*nl = '\0';
ext_charsets[ext_charsets_number].from_UTF = xstrdup (cp);
if (!strtok (cp, " \t\n"))
ext_charsets[ext_charsets_number].from_UTF = NULL;
else if (Debug > 2)
fprintf (stderr, "2UTF: From UTF-8: ``%s'' \n", \
ext_charsets[ext_charsets_number].from_UTF);
}
if (ext_charsets[ext_charsets_number].to_UTF == NULL \
&&ext_charsets[ext_charsets_number].from_UTF == NULL)
break;
if (Debug > 2)
fprintf (stderr, "2UTF: Entry %i O.K. \n", \
ext_charsets_number);
ext_charsets_number++;
}
break;
}
}
if (nl == NULL)
break;
else
{
cp = nl + 1;
continue;
}
}
while (TRUE);
if (paths_number <= 0)
paths = compiled_paths;
/*Error (no_pathnames); */
return (TRUE);
}
inline int
Seek_boundary (FILE * in_stream, char *boundary, int push_boundary)
{
int ret = FALSE;
size_t length=0;
unsigned char *lbp;
while (ret == FALSE)
{
if (in_stream)
{
if ((length = \
Getline (&line.buf, &line.length, in_stream)) == (size_t) -1)
if (ferror (in_stream))
Error (IO_err);
else
return (FALSE);
lbp = line.buf;
if (lbp[0] == '-' && lbp[1] == '-')
{
ret = Str_is_boundary (lbp + 2, boundary, push_boundary);
push_boundary = FALSE;
}
}
else
{
ret = Str_is_boundary (lbp = NULL, boundary, push_boundary);
push_boundary = FALSE;
}
if (ret == TRUE || ret == END_BOUNDARY)
if (!charset_p->USASCII_is_subset)
Validate_charset ("us-ascii", 0);
if (ret < OUTER_BOUNDARY)
{
if (lbp)
while (lbp < line.buf + length)
{
PUTC_IN_UTF8 ((unsigned char) *lbp);
lbp++;
}
else
fprintf (charset_p->pipe, \
ret == END_BOUNDARY ? "--%s--\n" : "--%s\n", boundary);
}
}
return (ret);
}
void
Short_help (int exit_code)
{
fprintf (stderr, "%s%s", short_help, more_help);
exit (exit_code);
}
int
Strcasecmp (const char *str1, const char *str2)
{
while (toupper(*str1)==toupper(*str2) && *str1)
{
str1++;
str2++;
}
return ( toupper(*str1)-toupper(*str2) );
}
int
Strcase_has_prefix (const char *string, const char *prefix)
{
while (toupper(*string)==toupper(*prefix) && *prefix)
{
string++;
prefix++;
}
return (*prefix=='\0' ? 0 : toupper(*string)-toupper(*prefix) );
}
char *
Stpcpy (char *To, const char *From)
{
register char *to=To;
while ((*to=*From++))
to++;
return (to);
}
char *
Stpncpy (char *To, const char *From, size_t count)
{
register char *to=To;
char *ret;
while ((size_t)(to-To) < count)
if ((*to=*From++))
to++;
else
break;
ret = to;
while ((size_t)(to-To) < count)
*to++ = '\0';
return (ret);
}
int
Str_has_prefix (const char *string, const char *prefix)
{
while (*string==*prefix && *prefix)
{
string++;
prefix++;
}
return (*prefix=='\0' ? 0 : *string-*prefix );
}
int
Str_is_UTF8 (char *charmap_filename)
{
register i, iplus;
/* Unicode-1-1-UTF-8 */
if (Strcasecmp (charmap_filename, "UTF-8") == 0 || \
Strcasecmp (charmap_filename, "UTF-2") == 0 || \
(Strcase_has_prefix (charmap_filename, "Unicode-") == 0 \
&&(iplus = strspn ( \
charmap_filename + (i = strlen ("Unicode-")), "1234567")) > 0 && \
charmap_filename[i += iplus] == '-' && \
(iplus = strspn (charmap_filename + (i += 1), "01234567")) > 0 && \
(Strcasecmp (charmap_filename + (i += iplus), "-UTF-8") == 0 || \
Strcasecmp (charmap_filename + i, "-UTF-2") == 0) \
) \
)
return (TRUE);
else
return (FALSE);
}
inline int
Str_is_boundary (char *linebuf, char *boundary, int push_boundary)
{
int boundary_len;
int ret = FALSE, index;
static struct
{
char **lines;
int last, pending, pending_ret;
}
boundaries =
{
NULL, -1, -1
};
unsigned char *lbp;
if (push_boundary)
{
if (++boundaries.last % 10 == 0)
boundaries.lines = xrealloc (boundaries.lines, \
sizeof (boundaries.lines[0]) * (boundaries.last + 10));
boundaries.lines[boundaries.last] = xstrdup (boundary);
}
if (boundaries.pending != -1)
{
if (linebuf)
Error (internal_err);
if (line.length < 90)
line.buf = xrealloc (line.buf, 90);
*(lbp = Stpcpy (line.buf, boundaries.lines[boundaries.pending])) = '\n';
lbp[1] = '\0';
if (boundaries.pending_ret == END_BOUNDARY)
strcpy (lbp, "--\n");
lbp = line.buf;
}
else if (!(lbp = linebuf))
Error (internal_err);
for (index = boundaries.last; index >= 0; index--)
{
boundary_len = strlen (boundaries.lines[index]);
if (strncmp (lbp, boundaries.lines[index], boundary_len) == 0)
if (lbp[boundary_len \
+strspn (lbp + boundary_len, " \t")] == '\n')
{
if (Debug >= 2)
fprintf (stderr, " header boundary line found\n");
ret = TRUE;
break;
}
else if (lbp[boundary_len] == '-' \
&&lbp[boundary_len + 1] == '-' \
&&(lbp[boundary_len + 2 \
+strspn (lbp + boundary_len + 2, " \t")] == '\n'))
{
if (Debug >= 2)
fprintf (stderr, " -- end boundary line found\n");
ret = END_BOUNDARY;
break;
}
}
if (ret)
if (index == boundaries.last)
boundaries.pending = -1;
else
{
boundaries.pending = index;
if (Debug)
fprintf (stderr, " -- this is boundary of outer message !\n");
boundaries.pending_ret = ret;
ret = OUTER_BOUNDARY + boundaries.last - index;
}
if (ret >= END_BOUNDARY)
boundaries.last--;
return (ret);
}
char *
Str_str (char *haystack_, char *needle_)
{
char *haystack, *needle;
register char *n_, *n;
register index;
haystack = xstrdup (haystack_);
needle = xstrdup (needle_);
n = haystack;
n_ = haystack_;
for (index = 0; index < 2; index++)
{
while (*n_)
if (!(*n_ == '-' || *n_ == '_'))
*n++ = *n_++;
else
n_++;
*n = '\0';
n = needle;
n_ = needle_;
}
n = strstr (haystack, needle) ? haystack_ : NULL;
free (haystack);
free (needle);
return (n);
}
inline char *
Strtoupper (char *string)
{
register index;
for (index = 0; string[index] != 0; index++)
string[index] = toupper (string[index]);
return string;
}
int
Spit (wchar_t min, wchar_t max, wchar_t charmap[], char unknown_char)
{
char s[22], header[] = " 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F \n";
register wchar_t index;
register length;
int prefix_length;
prefix_length = sprintf (s, "%.2lx", max);
fprintf (charset_p->pipe, "%*c%s", prefix_length, ' ', header);
for (index = min / 32 * 32; index <= max / 32 * 32 + 31; index++)
{
if (index % 32 == 0)
fprintf (charset_p->pipe, \
"%*.2lX: ", prefix_length, (unsigned long) index);
length = wctomb (s, charmap == NULL ? index : charmap[index]);
if ((length == 1) && \
(((unsigned) s[0] < 0x20) || ((unsigned) s[0] == 0x7F)))
{
length = wctomb (s, (wchar_t) '.');
}
if (length != -1)
{
if (fwrite (s, 1, length, charset_p->pipe) != (size_t) length)
Error (IO_err);
}
else if (putc (unknown_char, charset_p->pipe) == EOF)
Error (IO_err);
if (index < 0x3000 || (index >= 0xF000 && index < 0xF900) || \
(index >= 0xFF60 && index < 0xFFE0) || index >= 0xFFE8)
if (putc (' ', charset_p->pipe) == EOF)
Error (IO_err);
switch (index % 32)
{
case 31:
putc ('\n', charset_p->pipe);
break;
case 15:
putc (' ', charset_p->pipe);
}
if (index % (32 * 8) == (32 * 8 - 1))
fprintf (charset_p->pipe, "%*c%s", prefix_length, ' ', header);
if (index == 0x7FFFFFFF)
break;
}
return (1);
}
int
Validate_charset (char *charset_name, \
enum USASCII_is_subset_type USASCII_is_subset)
{
int c, element;
static int charsets_count = 0;
static struct charset_type *charsets = NULL, *old_charset_p;
struct charmap_file_type charmap_file =
{NULL, DEFAULT};
struct charset_type *charsets_old_addr;
wchar_t wchar;
if (charset_name != NULL)
{
old_charset_p = charset_p;
if (charset_p->name != NULL && \
Strcasecmp (charset_name, charset_p->name) == 0)
{
if (Debug >= 7)
fprintf (stderr, "Charset the same: `%s' \n", charset_name);
if (USASCII_is_subset)
if (!charset_p->USASCII_is_subset)
{
charset_p = &USASCII_charset;
if (Debug >= 2)
fprintf (stderr, "US-ASCII isn't subset " \
"of `%s', changing to US-ASCII", charset_name);
}
}
else if (Strcasecmp (charset_name, "US-ASCII") == 0)
{
charset_p = &USASCII_charset;
}
else if (strlen (charset_name) < (size_t) (Mail ? 4 : 2) \
||Strcasecmp (charset_name, "'unknown'") == 0)
{
if (Debug && Strcasecmp (charset_name, "'unknown'") != 0)
fprintf (stderr, "Too short charset name: `%s\'\n", charset_name);
charset_p = &unknown_charset;
}
else if (Str_is_UTF8 (charset_name))
{
charset_p = &UTF8_charset;
}
else
{
for (element = charsets_count - 1; element >= 0; element--)
{
if (Strcasecmp (charsets[element].name, charset_name) == 0)
{
charset_p = &charsets[element];
if (charset_p == old_charset_p)
Error (internal_err);
if (charset_p->type == BUF_PIPE)
goto Reopen;
else
goto Return1;
}
}
charsets_old_addr = charsets;
charsets = xrealloc (charsets, \
(sizeof (struct charset_type)) * ++charsets_count);
if (NULL != charsets_old_addr \
&&old_charset_p >= charsets_old_addr \
&&old_charset_p <= &charsets_old_addr[charsets_count - 2])
old_charset_p += charsets - charsets_old_addr;
charset_p = &charsets[charsets_count - 1];
Reopen:
charset_p->type = UNKNOWN;
charset_p->pipe = unknown_charset.pipe;
strcpy (charset_p->name, charset_name);
if (Look_for_ext_charset (USASCII_is_subset) == FALSE)
{
charset_p->charmap = xmalloc (256 * sizeof (wchar_t));
if (Fopen_charmap (charset_p->name, &charmap_file))
{
c = Get_charmap (charset_p->charmap, \
&charmap_file, unknown_wchar);
if (EOF == fclose (charmap_file.stream))
Error (IO_err);
switch (c)
{
case err_few_chars:
Error (bad_charmap_format);
case err_IO:
Error (IO_err);
case err_internal:
Error (internal_err);
case err_long:
if (Debug >= 8)
fprintf (stderr, "2UTF: %s \n", long_file);
break;
case err_short:
if (verbose)
fprintf (stderr, "2UTF: %s: \n", incomplete_charmap);
break;
}
charset_p->type = KNOWN;
charset_p->pipe = stdout;
charset_p->USASCII_is_subset = IS;
for (wchar = ' '; wchar <= '~'; wchar++)
if (charset_p->charmap[wchar] != wchar)
{
charset_p->USASCII_is_subset = NO;
if (USASCII_is_subset)
charset_p = &USASCII_charset;
break;
}
}
else
free (charset_p->charmap);
}
}
Return1:
if (Debug >= 2)
fprintf (stderr, "Changing charset from `%s' to `%s\'\n", \
old_charset_p->name, charset_p->name);
if (old_charset_p->type == BUF_PIPE \
&&old_charset_p->pipe != charset_p->pipe)
{
pclose (old_charset_p->pipe);
old_charset_p->pipe = NULL;
}
else
fflush (old_charset_p->pipe);
return (charset_p->type == UNKNOWN ? FALSE : TRUE);
}
else
{ /* restore charset selected before the current */
if (Debug >= 2)
fprintf (stderr, "Changing charset from `%s' to `%s\'\n", \
charset_p->name, old_charset_p->name);
if (charset_p->type == BUF_PIPE)
{
pclose (charset_p->pipe);
charset_p->pipe = NULL;
charsets_count--;
}
else
fflush (charset_p->pipe);
charset_p = old_charset_p;
if (charset_p->type == BUF_PIPE)
if (Look_for_ext_charset (USASCII_is_subset) == FALSE)
fprintf (stderr, \
"2UTF: Can't find the same ext. charset name second time ?? \n");
return (charset_p->type == UNKNOWN ? FALSE : TRUE);
}
}
inline void *
xmalloc (size_t size)
{
register void *pointer = malloc (size);
if (pointer == NULL)
Error (out_of_mem);
return pointer;
}
inline void *
xrealloc (void *pointer, size_t size)
{
register void *new_pointer = realloc (pointer, size);
if (new_pointer == NULL)
Error (out_of_mem);
return new_pointer;
}
inline char *
xstrdup (const char *string)
{
return (strcpy (xmalloc (strlen (string) + 1), string));
}
/*
inline char *
xstrndup (const char *string, size_t size)
{
register char *new_string = strndup (string, size);
if (new_string == NULL)
Error (out_of_mem);
return new_string;
}
*/
int
main (int argc, char *argv[]) /* char *env[]) */
{
#include "approx.c"
/*FILE *charmap_file = NULL; */
unsigned char default_unknown_char = 0x80;
char unknown_char = default_unknown_char;
char *charmap_filename = NULL, *ptr;
unsigned char *next_token, /* s[max_string_length], */ *stop_char_ptr;
const char *optstring = "248Cc:d::f::Hh::lorS::su::vVWw";
const struct long2short_options_type long2short_options[] =
{
{
"UCS-2", '2'
}
,
{
"UCS2", '2'
}
,
{
"ucs-2", '2'
}
,
{
"ucs2", '2'
}
,
{
"UCS-4", '4'
}
,
{
"UCS4", '4'
}
,
{
"ucs-4", '4'
}
,
{
"ucs4", '4'
}
,
{
"UCS-wchar_t", 'w'
}
,
{
"ucs-wchar_t", 'w'
}
,
{
"UTF-8", '8'
}
,
{
"UTF8", '8'
}
,
{
"utf-8", '8'
}
,
{
"utf8", '8'
}
,
{
"create-aliases", 'C'
}
,
{
"charmap-file", 'c'
}
,
{
"debug", 'd'
}
,
{
"format", 'f'
}
,
{
"forward", 'o'
}
,
{
"help", 'h'
}
,
{
"?", 'h'
}
,
{
"HTML", 'H'
}
,
{
"html", 'H'
}
,
{
"list", 'l'
}
,
{
"reverse", 'r'
}
,
{
"show-charmap", 'W'
}
,
{
"spit-glyphs", 'S'
}
,
{
"switch-to-UTF-8", 's'
}
,
{
"switch-to-UTF8", 's'
}
,
{
"switch-to-utf-8", 's'
}
,
{
"switch-to-utf8", 's'
}
,
{
"unknown-char", 'u'
}
,
{
"verbose", 'v'
}
,
{
"version", 'V'
}
,
{
"blurb", 'V'
}
,
{
NULL, '\0'
}
};
enum
{
UCS2, UCS4, UCSwchar_t, UTF8f
}
output_format = UTF8f;
int index, create_aliases = FALSE, \
file_arg = 0, html_doc = FALSE, list_charmaps = FALSE, \
show_charmap = FALSE, spit = FALSE, \
swap, switch_to_unicode = FALSE;
/*register length, c; */
register c;
long int l;
union
{
u_int8_t byte[4];
u_int16_t b16[2];
u_int32_t b32;
}
u_int;
wchar_t range, spit_min = 0xF000, spit_max = 0xF1FF, wchar;
/* standard UCS byte order is big endian */
#if (__BYTE_ORDER != __BIG_ENDIAN)
swap = TRUE;
#else
swap = FALSE;
#endif
if (argc <= 1)
{
}
else
{
/*while ((c = getopt_long (argc, argv, optstring, long_options, NULL)) != -1)*/
Long2short_options (argc, argv, long2short_options);
while ((c = getopt (argc, argv, optstring)) != -1)
{
if (verbose)
fprintf (stderr, " -%c", c);
switch (c)
{
case '?':
case ':':
Short_help(3);
case '2':
output_format = UCS2;
break;
case '4':
case 'w':
output_format = UCS4;
break;
case '8':
output_format = UTF8f;
break;
case 'C':
create_aliases = 1;
break;
case 'V':
fprintf (stderr, "%s%s", version, blurb);
exit (0);
case 'c':
charmap_filename = xstrdup (optarg);
if (verbose)
fprintf (stderr, "'%s'", charmap_filename);
break;
case 'd':
Debug = 1;
if (optarg)
Debug = atoi (optarg);
if (verbose)
fprintf (stderr, "%i", Debug);
verbose = TRUE;
break;
case 'f':
if (optarg)
{
charmap_format[DEFAULT] = xstrdup (optarg);
}
else
charmap_format[DEFAULT] = default_charmap_format;
if (verbose)
fprintf (stderr, "'%s'", charmap_format[DEFAULT]);
break;
case 'H':
html_doc = TRUE;
break;
case 'h':
if (optarg && optarg[0] == 'p')
{
Read_config_file ();
Print_paths ();
}
else
Help (default_unknown_char);
exit (0);
case 'l':
list_charmaps = TRUE;
break;
case 'o':
reverse = FALSE;
break;
case 'r':
reverse = TRUE;
break;
case 'S':
spit = 1;
if (optarg)
{
if (strcspn (optarg + strspn (optarg, "\t\n "), "-,;") == 0)
{
spit_min = 0;
if ((next_token = strtok (optarg, "\t\n -,;")) == NULL)
spit = 2;
}
else
{
if (strpbrk (optarg + strspn (optarg, "\t\n -,;"), "-,;"))
spit = 2;
if ((next_token = strtok (optarg, "\t\n -,;")) == NULL)
Short_help(5);
errno = 0;
range = strtoul (next_token, (char **) &stop_char_ptr, 16);
if (errno != ERANGE && *stop_char_ptr == '\0' \
&&range >= 0 && range <= 0x7FFFFFFF)
spit_min = range;
else
Short_help(5);
next_token = strtok (NULL, "\t\n -,;");
}
if (next_token)
{
errno = 0;
range = strtoul (next_token, (char **) &stop_char_ptr, 16);
if (errno != ERANGE && *stop_char_ptr == '\0' \
&&range >= 0 && range <= 0x7FFFFFFF)
spit_max = range;
else
Short_help(5);
}
else if (spit == 2)
spit_max = (spit_min > 0x7FFFFFFF - 511) ? \
0x7FFFFFFF : spit_min / 32 * 32 + 511;
else
spit_max = spit_min / 32 * 32 + 31;
if (spit_max < spit_min)
{
range = spit_min;
spit_min = spit_max;
spit_max = range;
}
if (strtok (NULL, "\t\n -,;"))
Short_help(5);
}
/*fprintf(stderr,"min = %lx\n",spit_min); */
if (verbose)
fprintf (stderr, "0x%.4lX-0x%.4lX\n", spit_min, spit_max);
break;
case 's':
switch_to_unicode = TRUE;
break;
case 'u':
if (optarg)
if (strlen (optarg) <= 1)
unknown_char = optarg[0];
else
{
errno = 0;
l = strtol (optarg, (char **) &stop_char_ptr, 0);
if (errno != ERANGE && *stop_char_ptr == '\0' && l <= 0xFF)
unknown_char = l;
else
Short_help(5);
}
else
unknown_char = default_unknown_char;
if (verbose)
fprintf (stderr, "0x%.2X", unknown_char);
break;
case 'v':
verbose = TRUE;
fprintf (stderr, " -v");
break;
case 'W':
show_charmap = TRUE;
break;
/* case 'w':
output_format = UCSwchar_t;
break; */
default:
fprintf (stderr, "getopt returned character code 0x%X ??\n", c);
}
}
if (verbose)
putc ('\n', stderr);
}
if (!(ptr = strrchr (argv[0], '/')))
ptr = argv[0];
else
ptr++;
if (Strcasecmp (ptr, "fromUTF") == 0)
reverse = !reverse;
if (Debug > 7)
fprintf (stderr, "2UTF: argv[0]=``%s'', filename=``%s''. \n", argv[0], ptr);
if (Debug)
if (reverse)
fprintf (stderr, "2UTF: from UTF \n");
else
fprintf (stderr, "2UTF: to UTF \n");
if (switch_to_unicode)
fprintf (stderr, "\033%%G");
Read_config_file ();
if (atexit (&Close_pipe) != 0)
Error (internal_err);
if (create_aliases)
if (!(c = Create_aliases ()))
{
fprintf (stderr, "2UTF: %s ``%s'' \n", can_not_create, \
aliases_pathname);
exit (7);
}
if (list_charmaps)
exit (List_charmaps ()? 0 : 8);
if (spit)
{
Spit (spit_min, spit_max, NULL, unknown_char);
exit (0);
}
if (optind < argc)
charmap_filename = argv[file_arg = argc - 1];
else if (charmap_filename == NULL)
if (create_aliases)
exit (0);
else
{
/* Assuming mail message */
if (output_format == UCS2 || output_format == UCS4 || reverse)
Error (unimplemented);
Mail = TRUE;
c = parse_message (0);
if (charset_p->type == BUF_PIPE || charset_p->type == NON_BUF_PIPE)
pclose (charset_p->pipe);
charset_p = &unknown_charset;
Pipe_to_UTF8 (stdin);
if (c != END_BOUNDARY)
{
if (Debug)
fprintf (stderr, "parse of message failed: %d\n", c);
exit (1);
}
else if (Debug)
fprintf (stderr, "parse of message complete\n");
exit (0);
}
/* For the output of interractive programms */
setvbuf (stdout, NULL, _IONBF, 0);
if (show_charmap)
{
reverse = TRUE;
unknown_wchar = (wchar_t) '?';
}
if (!Validate_charset (charmap_filename, 0))
{ /* (charset_p->type == UNKNOWN) */
if (strchr (charmap_filename, '/') == NULL)
{
fprintf (stderr, "%s ``%s'' \n", \
can_not_find_alias, charmap_filename);
for (index = 0; index < paths_number; index++)
fprintf (stderr, " %s*%s* \n", paths[index], charmap_filename);
}
else
fprintf (stderr, " %s \n", charmap_filename);
Error (can_not_open_any);
}
else if (charset_p->type == UTF8)
{
if (verbose)
fprintf (stderr, "2UTF: %s ``%s'' \n", using, "Unicode UTF-8");
}
if (charmap_filename != argv[file_arg])
free (charmap_filename);
if (show_charmap)
{
unknown_wchar = (wchar_t) '?';
if (charset_p->type != UTF8)
Spit (0, 255, charset_p->charmap, unknown_char);
else
Spit (0, 0x7FFFFFFF, NULL, unknown_char);
exit (0);
}
if (charset_p->type != KNOWN \
&& (output_format == UCS2 || output_format == UCS4))
Error (unimplemented);
if (reverse)
{
if (output_format == UCS2)
{
while (fread (u_int.byte, 1, 2, stdin) == 2)
{
/* check Byte Order Mark */
if (0xFFFE == u_int.b16[0])
swap = TRUE;
else if (0xFEFF == u_int.b16[0])
swap = FALSE;
else
{
if (swap)
{
c = u_int.byte[0];
u_int.byte[0] = u_int.byte[1];
u_int.byte[1] = c;
}
wchar = u_int.b16[0];
Print_approx_Macro (1, wchar, unknown_char)
}
}
}
else if (output_format == UCS4)
{
while (fread (u_int.byte, 1, 4, stdin) == 4)
{
#if (__BYTE_ORDER == __PDP_ENDIAN)
#error
c = u_int.b16[0];
u_int.b16[0] = u_int.b16[1];
u_int.b16[1] = c;
#endif
/* check Byte Order Mark */
if (0xFFFE0000 == u_int.b32)
swap = TRUE;
else if (0x0000FEFF == u_int.b32)
swap = FALSE;
else
{
if (swap)
{
c = u_int.byte[0];
u_int.byte[0] = u_int.byte[3];
u_int.byte[3] = c;
c = u_int.byte[1];
u_int.byte[1] = u_int.byte[2];
u_int.byte[2] = c;
}
wchar = u_int.b32;
Print_approx_Macro (1, wchar, unknown_char)
}
}
}
else if (output_format == UTF8f)
{
if (charset_p->type == KNOWN)
while ((c = Freadmb (&wchar, stdin)) != EOF)
Print_approx_Macro (c, wchar, unknown_char)
else if (charset_p->type == UTF8)
while ((c = getc (stdin)) != EOF)
if (putc (c, charset_p->pipe) == EOF)
Error (IO_err);
else
Error (internal_err);
if (ferror (stdin))
Error (IO_err);
}
else
Error (internal_err);
}
else
/* not reverse */
{
if (output_format == UCS2)
{
putchar (0xFE);
putchar (0xFF);
while ((c = getchar ()) != EOF)
{
u_int.b16[0] = (u_int16_t) charset_p->charmap[c];
#if (__BYTE_ORDER != __BIG_ENDIAN)
c = u_int.byte[0];
u_int.byte[0] = u_int.byte[1];
u_int.byte[1] = c;
#endif
if (fwrite (u_int.byte, 1, 2, stdout) != 2)
Error (IO_err);
}
}
else if (output_format == UCS4)
{
putchar (0);
putchar (0);
putchar (0xFE);
putchar (0xFF);
while ((c = getchar ()) != EOF)
{
u_int.b32 = (u_int32_t) charset_p->charmap[c];
#if (__BYTE_ORDER == __LITTLE_ENDIAN)
c = u_int.byte[0];
u_int.byte[0] = u_int.byte[3];
u_int.byte[3] = c;
c = u_int.byte[1];
u_int.byte[1] = u_int.byte[2];
u_int.byte[2] = c;
#elif (__BYTE_ORDER == __PDP_ENDIAN)
c = u_int.byte[0];
u_int.byte[0] = u_int.byte[1];
u_int.byte[1] = c;
c = u_int.byte[2];
u_int.byte[2] = u_int.byte[3];
u_int.byte[3] = c;
#elif (__BYTE_ORDER == __BIG_ENDIAN)
#else
#error Unknown byte order.
#endif
if (fwrite (u_int.byte, 1, 4, stdout) != 4)
Error (IO_err);
}
}
/* else if (output_format == UCSwchar_t)
{
while ((c = getchar ()) != EOF)
if (fwrite (&charset_p->charmap[c], sizeof (wchar_t), 1, stdout) != 1)
Error (IO_err);
}
*/
else if (output_format == UTF8f)
Pipe_to_UTF8 (stdin);
else
Error (internal_err);
}
if (feof (stdin))
exit (0);
else
Error (IO_err);
exit (101);
}
|