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 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369
|
/*
* - Empty and line-drawing cells might span, but that's not handled.
* - Need to handle table cell spacing. But that's a table attribute in
* HTML and a column attribute in tbl.
* - Perhaps tags to use for headers should be read from file to allow
* better user-level style configuration.
* - SetCFA() doesn't do much with center/adjust interaction.
* - Need a FileInfo() equivalent here for error messages.
* - Need to set initial style when the body starts?
* - Wrapper needs function to call during loop and termination value
* to look for.
* - Size changes - ignore in title?
* - Should be able to override the fonts file, both at compile time, and
* from the command line. Of course, since fonts are currently ignored,
* this doesn't matter much yet.
*/
/*
* tc2html - read troffcvt output, convert to HTML
*
* Syntax: tc2html [-D] [-E] [-T title] [file] ...
*
* -D enable debug code
* -E echo tokens as they are read
* -T specify document title
*
* The main trick for this postprocessor is to get troffcvt to write
* output that's useful for determining where HTML entities like titles,
* paragraphs, and lists begin and/or end. For figuring out these
* higher-level constructs, it doesn't do much good to know just
* low-level things like breaks and font changes. The way to do this
* is to use an action file with troffcvt that maps macro calls to
* "\html xxx" lines, where xxx is an HTML entity name.
*
* 03 Jan 97 Paul DuBois dubois@primate.wisc.edu
*
* 03 Jan 97
* - Created
*/
#include <stdio.h>
#include <ctype.h>
#ifndef STRING_H
#define STRING_H <string.h>
#endif
#include STRING_H
#include "portlib.h"
#include "etm.h"
#include "memmgr.h"
#include "tokenscan.h"
#include "tcgen.h"
#include "tcunix.h"
#include "tcread.h"
#include "tctbl.h"
#include "tc2html.h"
static void WriterInit (void);
static int BeginFile (void);
static void ReadFile (void);
static void ControlLine (void);
static void SetFont (char *name, short inDisplay);
static void SetCFA (short newCenter, short newFill, short newAdjust);
static void PlainText (void);
static void SpecialText (void);
static void ResetTabs (void);
static void SetTabStop (long pos, char type);
static int ReadSpecials (char *filename);
static SpChar *LookupSpecial (char *name);
static KeyMap *LookupKey (char *p);
static void DoAnchor (short newAnchor, short argc, char **argv);
static void DoElement (short newElt, short argc, char **argv);
static void BeginDocHead (void);
static void BeginDocBody (void);
static void TerminateDefList (void);
static void FlushTOC (void);
static void PushElt (short eltType, short misc);
static void PopElt (void);
static void AddBreak (void);
static void AddPara (void);
static void AddLine (long length, short direction);
static void AddStr (char *p);
static void AddLitStr (char *p);
static void AddChar (char c);
static void AddTitleChar (char c);
static void AddHeaderChar (char c);
static void AddTOCChar (char c);
static void StrAddStr (char *buf, int size, char *str);
static void TableBegin (short argc, char **argv);
static void TableEnd (void);
static void TableRowBegin (short argc, char **argv);
static void TableRowEnd (void);
static void TableRowLine (short argc, char **argv);
static void TableCellInfo (short argc, char **argv);
static void TableCellBegin (short argc, char **argv);
static void TableCellEnd (void);
static void TableEmptyCell (void);
static void TableSpannedCell (void);
static void TableCellLine (short argc, char **argv);
static void CellBegin (char type, short vspan, short hspan, char vadjust);
static void ControlOut (char *p, short brkBefore, short brkAfter);
static void TextStrOut (char *p);
static void TextChOut (int c);
static void HTMLChOut (char *p);
static void StrOut (char *p);
static void ChOut (int c);
static void Flush (void);
static char *CharToHTML (int c);
static int AllowOutput (int yesno);
static int ReadFonts (char *filename);
static Font *LookupFont (char *name);
static KeyMap keyMap[] =
{
{ "anchor-href", keyAnchor, anchorHref },
{ "anchor-name", keyAnchor, anchorName },
{ "anchor-toc", keyAnchor, anchorTOC },
{ "anchor-end", keyAnchor, anchorEnd },
{ "title", keyElement, eltTitle },
{ "para", keyElement, eltPara },
{ "display", keyElement, eltDisplay },
{ "display-end", keyElement, eltDisplayEnd },
{ "display-indent", keyElement, eltDisplayIndent },
{ "list", keyElement, eltList },
{ "list-end", keyElement, eltListEnd },
{ "list-item", keyElement, eltListItem },
{ "header", keyElement, eltHeader },
{ "header-end", keyElement, eltHeaderEnd },
{ "blockquote", keyElement, eltBlockquote },
{ "blockquote-end", keyElement, eltBlockquoteEnd },
{ "definition-term", keyElement, eltDefTerm },
{ "definition-desc", keyElement, eltDefDesc },
{ "shift-right", keyElement, eltShiftRight },
{ "shift-left", keyElement, eltShiftLeft },
{ NULL, keyUnknown, 0 }
};
static int allowOutput = 1; /* zero if output is throttled */
static long resolution = 432;
/*
* cCount is the number of characters on the current output line. It's
* incremented at the lowest output level, in ChOut(). tCount is the number
* of text (non-tag) characters on the line. It's used to determine when to
* ignore leading spaces in TextChOut(). (Ignore when character to write
* is a space and tCount is zero.) etCount is the "effective" number of
* text characters on a line. For instance, ">" is written as ">" -- this
* increments tCount by 4, but etCount by only 1. etCount is needed for
* doing spacing for tabstops.
*/
static int cCount = 0;
static int tCount = 0;
static int etCount = 0;
static Font *fontList = (Font *) NULL;
static Font *curFont = (Font *) NULL;
static SpChar *spList = (SpChar *) NULL;
static int debug = 0;
static int echo = 0;
/*
* docPart indicates which part of the document is being collected:
* - inHead = document HEAD part
* - inBody = document BODY part
* - inNone = uninitialized
*
* bodyElt indicates which body element is being collected. It's
* irrelevant unless docPart is inBody.
*
* If output is ready to be written while docPart is inNone, the part is
* initialized to inBody, using the default element of "generic paragraph."
*/
static short docPart = inNone;
static short bodyElt = eltUnknown;
static Elt eltStack[maxElt]; /* element stack */
static short eltTop = -1; /* initially stack is empty */
static short listDepth = 0; /* list nesting depth */
static short displayFlag = 0; /* non-zero if in display */
static short displayIndent = 0; /* display indent */
static short blockquoteFlag = 0; /* non-zero if in block quote */
static short anchorFlag = 0; /* non-zero if in <a xxx> (turned on */
/* by ahref, aname, or atoc) */
/*
* This is non-NULL is an an href anchor is pending (collected from the
* input but not yet output). This is used to "float" an anchor past any
* whitespace that may follow it to the next non-white character. (The
* reason this is done is to avoid leading underlined whitespace, which is
* ugly.)
*/
static char *pendingAnchor = (char *) NULL;
/*
* Table-of-contents stuff
*/
static TOC *tocHead = (TOC *) NULL; /* list of TOC entries */
static TOC *tocTail = (TOC *) NULL;
static char tocBuf[maxTOC] = ""; /* text of current TOC entry */
static short tocCount = 0; /* TOC entry index */
static short tocLevel = 0; /* level of current entry */
/*
* Title text is saved in a buffer because it's used both for the document
* title in the document head and for the initial heading in the document
* body. When tc2html detects that the document head -> document body
* transition has been crossed, it writes the document head, writes the
* beginning of the document body, and writes the initial header.
*
* Text intended for other structural elements of the body is written to the
* output as soon as it has been seen.
*
* cmdLineTitle is non-null if a -T option is given on the command line to
* override any document title found in the document. (Mainly useful when
* NO title is present in the document.)
*/
static char title[maxTitle] = "";
static char header[maxHeader] = "";
static char *cmdLineTitle = (char *) NULL;
/*
* Definition-list stuff. Definition lists are terminated implicitly
* by most structural markers except definition-list markers themselves.
*
* defListFlag values:
* 0 not in a definition list
* 1 collecting a term
* 2 collecting a description
*
* emptyDefTerm is used during definition term collection to specify whether
* or not any text has been found in the term. If not, then a <BR> is written
* when the term is closed to force a little whitespace.
*/
static short defListFlag = 0;
static short emptyDefTerm = 0;
/* right/left shift level */
static short shiftLevel = 0;
/*
* Per-table values
*/
static short tblCols = 0; /* number of columns in table */
static short tblRows = 0; /* number of rows in table */
static short centerTbl= 0; /* non-zero if table is centered */
/*
* Per-cell values.
*/
static char cellType[tblMaxCol]; /* cell type */
static short cellVSpan[tblMaxCol]; /* cell vspan flag */
static short cellHSpan[tblMaxCol]; /* cell hspan extent */
static char cellVAdjust[tblMaxCol]; /* cell vertical adjust */
static short cellBorder[tblMaxCol]; /* cell borders */
/*
* Counters for keeping track of position within table.
* tblColIdx counts horizontally during processing of \table-column-info
* lines, during \table-cell-info lines, and while processing each row of
* table data. tblRowIdx counts rows of table data.
*/
static short tblColIdx = 0;
static short tblRowIdx = 0;
static short adjust = adjLeft; /* line adjustment mode */
static short fill = 0; /* non-zero if fill mode on */
static short center = 0; /* non-zero if centering on */
static short underline = 0; /* non-zero if underlining on */
static short tabPos[maxTabStops];
static char tabType[maxTabStops];
static short tabCount = 0;
int
main (int argc, char *argv[])
{
int inc;
ETMInit (NULL);
UnixSetProgPath (argv[0]);
TCRSetOpenLibFileProc (UnixOpenLibFile);
--argc;
++argv;
while (argc > 0 && argv[0][0] == '-')
{
inc = 1;
if (strcmp (argv[0], "-D") == 0)
debug = 0xffff;
else if (strcmp (argv[0], "-E") == 0)
echo = 1;
else if (strncmp (argv[0], "-T", 2) == 0)
{
if (argv[0][2] != '\0') /* -Ttitle */
cmdLineTitle = &argv[0][2];
else if (argc < 1)
ETMPanic ("Missing title after -T");
else
{
cmdLineTitle = argv[1];
argc -= inc;
argv += inc;
}
}
else
ETMPanic ("Unknown option: %s", argv[0]);
argc -= inc;
argv += inc;
}
if (debug || echo)
{
/* don't want output order mixed up by buffering */
setbuf (stdout, (char *) NULL);
setbuf (stderr, (char *) NULL);
}
WriterInit ();
if (argc > 1)
ETMPanic ("Only one input file allowed");
if (argc == 0) /* stdin */
ReadFile ();
else
{
if (freopen (argv[0], "r", stdin) == (FILE *) NULL)
ETMMsg ("Cannot open: %s", argv[0]);
else
ReadFile ();
}
ETMEnd ();
exit (0);
/*NOTREACHED*/
}
static void
WriterInit (void)
{
if (!ReadFonts (defaultFontFile))
ETMPanic ("cannot find %s file", defaultFontFile);
if (!ReadSpecials (specialCharFile))
ETMPanic ("cannot find %s file", specialCharFile);
}
static int
BeginFile (void)
{
(void) AllowOutput (1);
ControlOut ("HTML", 1, 1);
ControlOut ("!-- this file was generated by troffcvt and tc2html --",
1, 1);
/* initialize font info to initial font */
/*SetFont ("R", 0);*/
(void) AllowOutput (0);
TCRInit ();
return (1);
}
static void
ReadFile (void)
{
int i;
(void) BeginFile ();
while (TCRGetToken () != tcrEOF)
{
if (echo)
{
ETMMsg ("class %d maj %d min %d <%s>",
tcrClass, tcrMajor, tcrMinor, tcrArgv[0]);
if (tcrClass == tcrControl || tcrClass == tcrSText)
{
for (i = 1; i < tcrArgc; i++)
ETMMsg ("\t<%s>", tcrArgv[i]);
}
}
switch (tcrClass)
{
case tcrControl: ControlLine (); break;
case tcrText: PlainText (); break;
case tcrSText: SpecialText (); break;
default: ETMPanic ("ReadFile: unknown token %d <%s>",
tcrClass, tcrArgv[0]);
}
}
/*
* If document contains HEAD part only, write a fake paragraph
* to force HEAD flush
*/
if (docPart == inHead)
DoElement (eltPara, 0, (char **) NULL);
/*
* Terminate final font setting
* Close any dangling style attributes
* Close any dangling anchor
* Close any open definition list
* Undo any pending right shifts
* Close any open BODY elements
* Flush table of contents if there is one
* Close document BODY part
*/
if (docPart == inBody)
{
if (curFont != (Font *) NULL)
StrOut (curFont->endTag);
if (underline)
ControlOut ("/UNDER", 1, 1);
if (anchorFlag)
DoAnchor (anchorEnd, 0, (char **) NULL);
TerminateDefList ();
while (shiftLevel-- > 0)
ControlOut ("/UL", 1, 1);
while (eltTop >= 0)
PopElt ();
SetCFA (0, 1, adjLeft);
FlushTOC ();
ControlOut ("/BODY", 1, 1);
}
ControlOut ("/HTML", 1, 1);
}
/*
* Read special character list. Each line has the character name
* and the HTML string to write out for a given special character.
*/
static int
ReadSpecials (char *filename)
{
FILE *f;
char buf[bufSiz];
SpChar *sp;
char *name, *value;
if ((f = TCROpenLibFile (filename, "r")) == (FILE *) NULL)
return (0);
while (TCRGetLine (buf, (int) sizeof (buf), f))
{
if (buf[0] == '#')
continue;
TSScanInit (buf);
if ((name = TSScan ()) == (char *) NULL) /* char name */
continue;
value = TSScan (); /* NULL if missing */
sp = New (SpChar);
sp->spName = StrAlloc (name);
/*
* If value missing, make NULL; this will cause writer
* to write "[[name]]" instead of dropping it.
*/
if (value == (char *) NULL)
sp->spValue = (char *) NULL;
else
sp->spValue = StrAlloc (value);
sp->spNext = spList;
spList = sp;
}
(void) fclose (f);
return (1);
}
static SpChar *
LookupSpecial (char *name)
{
SpChar *sp;
for (sp = spList; sp != (SpChar *) NULL; sp = sp->spNext)
{
if (strcmp (name, sp->spName) == 0)
break;
}
return (sp); /* NULL if not found */
}
static KeyMap *
LookupKey (char *p)
{
KeyMap *kp;
for (kp = keyMap; kp->name != (char *) NULL; kp++)
{
if (strcmp (p, kp->name) == 0)
return (kp);
}
return ((KeyMap *) NULL);
}
static void
ControlLine (void)
{
KeyMap *kp;
char buf[bufSiz];
short newCenter, newFill, newAdjust;
double d;
switch (tcrMajor)
{
default:
ETMMsg ("ControlLine: bad control major code: %d <%s>",
tcrMajor, tcrArgv[0]);
break;
case tcrCUnknown:
/*
* Special \html controls are handled here
*/
if (strcmp (tcrArgv[0], "\\html") == 0)
{
kp = LookupKey (tcrArgv[1]);
if (kp == (KeyMap *) NULL)
ETMMsg ("unknown \\html key: %s", tcrArgv[1]);
else
{
switch (kp->type)
{
default:
ETMPanic ("ControlLine: logic error");
case keyAnchor:
DoAnchor (kp->subtype,
tcrArgc - 2, &tcrArgv[2]);
break;
case keyElement:
DoElement (kp->subtype,
tcrArgc - 2, &tcrArgv[2]);
break;
}
}
}
else
{
ETMMsg ("ControlLine: unknown control token: <%s>",
tcrArgv[0]);
}
break;
case tcrComment:
break;
case tcrSetupBegin:
break;
case tcrResolution:
resolution = StrToLong (tcrArgv[1]);
break;
case tcrSetupEnd:
(void) AllowOutput (1);
break;
case tcrInputLine:
break;
case tcrPass:
/*
* \pass lines indicate information that should be passed
* straight through to the output
*/
Flush ();
StrOut (tcrArgv[1]);
Flush ();
break;
case tcrOther:
break;
case tcrBreak:
AddBreak ();
break;
case tcrCFA:
newCenter = center;
newFill = fill;
newAdjust = adjust;
switch (tcrMinor)
{
case tcrNofill:
newFill = 0;
break;
case tcrCenter:
newCenter = 1;
break;
case tcrAdjFull:
case tcrAdjLeft:
newCenter = 0;
newFill = 1;
newAdjust = adjLeft;
break;
case tcrAdjRight:
newCenter = 0;
newFill = 1;
newAdjust = adjRight;
break;
case tcrAdjCenter:
newCenter = 1;
break;
}
SetCFA (newCenter, newFill, newAdjust);
break;
case tcrPageLength:
break;
case tcrPageNumber:
break;
case tcrFont:
if (tcrArgc < 2)
ETMMsg ("missing font on \\font line");
else
SetFont (tcrArgv[1], displayFlag);
break;
case tcrPointSize:
break;
case tcrSpacing:
break;
case tcrLineSpacing:
break;
case tcrOffset:
break;
case tcrIndent:
break;
case tcrTempIndent:
break;
case tcrLineLength:
break;
case tcrTitleLength:
break;
case tcrTitleBegin:
break;
case tcrTitleEnd:
break;
case tcrSpace:
/*
* For multi-line spacing, put out <BR>'s for all but last
* line. If the spacing amount is less than a full line,
* ignore it.
*
* Assume 6 lines/inch in the calculation below.
*/
d = (StrToDouble (tcrArgv[1]) * 6.0 ) / resolution;
while (d > 1)
{
AddBreak;
--d;
}
if (d >= 1)
AddPara();
break;
case tcrUnderline:
break;
case tcrCUnderline:
break;
case tcrNoUnderline:
break;
case tcrULineFont:
break;
case tcrBracketBegin:
AddStr ("<BRACKET<"); /* ugly but makes it visible */
break;
case tcrBracketEnd:
AddStr (">BRACKET>");
break;
case tcrBreakSpread:
break;
case tcrExtraSpace:
break;
case tcrLine:
AddLine (StrToLong (tcrArgv[1]), tcrArgv[2][0]);
break;
case tcrMark:
break;
case tcrMotion:
break;
case tcrBeginOverstrike:
AddStr ("<OVERSTRIKE<"); /* ugly but makes it visible */
break;
case tcrOverstrikeEnd:
AddStr (">OVERSTRIKE>");
break;
case tcrBeginPage:
AddPara ();
break;
case tcrZeroWidth:
break;
case tcrSpaceSize:
break;
case tcrConstantWidth:
break;
case tcrNoConstantWidth:
break;
case tcrNeed:
break;
case tcrEmbolden:
break;
case tcrSEmbolden:
break;
case tcrResetTabs:
ResetTabs ();
break;
case tcrFirstTab:
ResetTabs ();
/* fall through... */
case tcrNextTab:
SetTabStop (StrToLong (tcrArgv[1]), tcrArgv[2][0]);
break;
case tcrHyphenate:
break;
case tcrDiversionBegin:
break;
case tcrDiversionAppend:
break;
case tcrDiversionEnd:
break;
case tcrTabChar:
break;
case tcrLeaderChar:
break;
case tcrTableBegin:
TableBegin (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrTableEnd:
TableEnd ();
break;
case tcrColumnInfo:
/* ignore this */
break;
case tcrRowBegin:
TableRowBegin (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrRowEnd:
TableRowEnd ();
break;
case tcrRowLine:
TableRowLine (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrCellInfo:
TableCellInfo (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrCellBegin:
TableCellBegin (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrCellEnd:
TableCellEnd ();
break;
case tcrCellLine:
TableCellLine (tcrArgc - 1, &tcrArgv[1]);
break;
case tcrEmptyCell:
TableEmptyCell ();
break;
case tcrSpannedCell:
TableSpannedCell ();
break;
}
}
static void
ResetTabs (void)
{
tabCount = 0;
}
static void
SetTabStop (long pos, char type)
{
if (tabCount >= maxTabStops)
{
ETMMsg ("too many tabstops");
return;
}
/*
* Assume charsPerInch characters/inch
*/
tabPos[tabCount] = (pos * charsPerInch) / resolution;
tabType[tabCount] = type;
++tabCount;
}
/*
* Set the font. The tags to use depend on whether or not we're currently
* in a display.
*/
static void
SetFont (char *name, short inDisplay)
{
Font *fp;
/* do nothing if font hasn't changed */
if (curFont != (Font *) NULL
&& strcmp (curFont->fontName, name) == 0
&& curFont->inDisplay == inDisplay)
return;
if (docPart == inHead) /* ignore title font changes */
return;
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
if ((fp = LookupFont (name)) == (Font *) NULL)
{
ETMMsg ("no info for font <%s>", name);
return;
}
/* turn off previous font, if one is known */
if (curFont != (Font *) NULL)
{
if (!curFont->inDisplay)
StrOut (curFont->endTag);
else
StrOut (curFont->dispEndTag);
}
/* switch to new font and turn it on */
curFont = fp;
curFont->inDisplay = inDisplay;
if (!curFont->inDisplay)
StrOut (curFont->beginTag);
else
StrOut (curFont->dispBeginTag);
}
static void
SetCFA (short newCenter, short newFill, short newAdjust)
{
char buf[bufSiz];
if (docPart == inNone) /* just remember values, no output */
{
center = newCenter;
fill = newFill;
adjust = newAdjust;
return;
}
buf[0] = '\0';
if (center && !newCenter) /* turn off centering */
(void) strcat (buf, "</CENTER>");
if (!center && newCenter) /* turn on centering */
(void) strcat (buf, "<CENTER>");
center = newCenter;
fill = newFill;
adjust = newAdjust;
if (buf[0] == '\0') /* no output to write */
return;
switch (docPart)
{
default:
ETMPanic ("SetCFA: logic error");
case inHead:
StrAddStr (header, sizeof (header), buf);
break;
case inBody:
Flush ();
StrOut (buf);
Flush ();
break;
}
}
static void
PlainText (void)
{
/*
* Line breaks are indicated explicitly by \break, so ignore
* linefeeds.
*/
if (tcrMajor != lf)
{
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
AddChar (tcrMajor);
}
}
static void
SpecialText (void)
{
SpChar *sp;
char buf[bufSiz];
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
if ((sp = LookupSpecial (&tcrArgv[0][1])) == (SpChar *) NULL
|| sp->spValue == (char *) NULL)
{
sprintf (buf, "[[%s]]", &tcrArgv[0][1]);
AddStr (buf);
}
else
AddLitStr (sp->spValue);
}
/*
* Have a new anchor tag
*/
static void
DoAnchor (short newAnchor, short argc, char **argv)
{
TOC *tp;
char buf[bufSiz];
short misc;
if (!allowOutput)
return;
if (newAnchor == anchorUnknown) /* can't tell what to do, */
return; /* so do nothing */
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
switch (newAnchor)
{
case anchorHref:
if (argc == 0)
{
ETMMsg ("problem: missing anchor link text");
break;
}
if (anchorFlag)
{
ETMMsg ("problem: nested anchor, ignored");
break;
}
sprintf (buf, "<A HREF=\"%s\">", argv[0]);
switch (docPart)
{
default:
ETMPanic ("DoAnchor: logic error 1");
case inHead:
StrAddStr (header, sizeof (header), buf);
StrAddStr (header, sizeof (header), "\n");
break;
case inBody:
pendingAnchor = StrAllocNP (buf);
if (pendingAnchor == (char *) NULL)
ETMPanic ("DoAnchor: cannot allocate anchor");
break;
}
anchorFlag = 1;
break;
case anchorName:
if (argc == 0)
{
ETMMsg ("problem: missing anchor name text");
break;
}
if (anchorFlag)
{
ETMMsg ("problem: nested anchor, ignored");
break;
}
sprintf (buf, "<A NAME=\"%s\">", argv[0]);
switch (docPart)
{
default:
ETMPanic ("DoAnchor: logic error 2");
case inHead:
StrAddStr (header, sizeof (header), buf);
StrAddStr (header, sizeof (header), "\n");
break;
case inBody:
Flush ();
StrOut (buf);
Flush ();
break;
}
anchorFlag = 1;
break;
case anchorTOC:
if (argc == 0)
{
ETMMsg ("problem: TOC entry with no level found, ignored");
break;
}
if (anchorFlag)
{
ETMMsg ("problem: nested anchor, ignored");
break;
}
/*
* Set up to collect the text and allocate a TOC strutcure.
* Terminate any current definition list; this assumes a
* TOC entry won't meaningfully occur in the middle of such.
*/
TerminateDefList ();
tocBuf[0] = '\0';
tp = (TOC *) AllocNP (sizeof (TOC));
tp->tocNext = (TOC *) NULL;
tp->tocStr = (char *) NULL;
if (tp == (TOC *) NULL)
ETMPanic ("DoAnchor: cannot allocate TOC entry");
tocLevel = StrToShort (argv[0]);
if (tocLevel < 1)
tocLevel = 1;
tp->tocLevel = tocLevel;
if (tocHead == (TOC *) NULL)
tocHead = tp;
else
tocTail->tocNext = tp;
tocTail = tp;
sprintf (buf, "<A NAME=TOC_%d>", ++tocCount);
switch (docPart)
{
default:
ETMPanic ("DoAnchor: logic error 3");
case inHead:
StrAddStr (header, sizeof (header), buf);
StrAddStr (header, sizeof (header), "\n");
break;
case inBody:
Flush ();
StrOut (buf);
Flush ();
break;
}
anchorFlag = 1;
break;
case anchorEnd:
if (!anchorFlag)
{
ETMMsg ("problem: unbalanced anchor-end, ignored");
break;
}
switch (docPart)
{
default:
ETMPanic ("DoAnchor: logic error 4");
case inHead:
StrAddStr (header, sizeof (header), "</A>\n");
break;
case inBody:
/*
* If there is a pending anchor here, there will be
* an empty <A HREF=xxx></A> sequence; warn about it.
*/
if (pendingAnchor != (char *) NULL)
ETMMsg ("problem: empty HREF anchor");
ControlOut ("/A", 0, 0);
break;
}
/* If tocLevel > 0, we were collecting a TOC entry */
if (tocLevel > 0)
{
if (tocBuf[0] == '\0')
ETMMsg ("problem: empty TOC entry");
tocTail->tocStr = StrAllocNP (tocBuf);
if (tocTail->tocStr == (char *) NULL)
ETMPanic ("DoAnchor: cannot allocate TOC entry text");
tocLevel = 0;
}
anchorFlag = 0;
break;
}
}
/*
* Begin a new HTML element. newElt is the type of element, and argc
* and argv are the rest of the argument vector from the \html input
* line.
*
* This has to detect structured element changes, shut down any current
* element that's being collected that no longer is, and open up any element
* that's now being collected that wasn't before.
*
* When the document head -> document body transition is crossed, flush the
* document title and initial header.
*/
static void
DoElement (short newElt, short argc, char **argv)
{
short misc;
if (!allowOutput)
return;
if (newElt == eltUnknown) /* can't tell what to do, */
return; /* so do nothing */
/*
* First check for title -- it's the only HEAD element. If it's
* present, this will cause the document HEAD part to contain a
* title and the text will also be used for the initial BODY part
* heading.
*/
if (newElt == eltTitle)
{
switch (docPart)
{
default:
ETMPanic ("DoElement: logic error 1");
case inNone:
BeginDocHead ();
docPart = inHead;
break;
case inHead: /* title occurred after one was already seen */
ETMMsg ("multiple titles found?");
break;
case inBody: /* title occurred after body began? */
ETMMsg ("out of place title found");
break;
}
return;
}
/*
* All other elements are BODY elements. If new element is the
* first body element we've seen, the HEAD part needs to be flushed
* and the BODY part needs to begin. Shove a paragraph element on
* the stack right away to make sure the element stack is non-empty;
* this eliminates a lot of checking against that condition later.
*/
if (docPart != inBody)
{
BeginDocBody ();
docPart = inBody;
PushElt (eltPara, listNone);
if (newElt == eltPara) /* we just handled it! */
return;
}
switch (newElt)
{
default:
ETMPanic ("DoElement: logic error 2, element = %hd", newElt);
case eltPara:
TerminateDefList ();
AddPara (); /* add para to current element */
break;
case eltDisplay:
/*TerminateDefList ();*/
if (displayFlag)
ETMMsg ("problem: display inside display");
PushElt (eltDisplay, listNone);
break;
case eltDisplayEnd:
if (eltStack[eltTop].eltType != eltDisplay)
ETMMsg ("problem: display-end not inside display");
else
PopElt ();
break;
case eltDisplayIndent:
if (argc == 0)
displayIndent = 0;
else
displayIndent = StrToShort (argv[0]);
break;
case eltList:
TerminateDefList ();
if (displayFlag)
ETMMsg ("problem: list inside display");
PushElt (eltList, listUnordered);
break;
case eltListEnd:
if (displayFlag)
ETMMsg ("problem: list-end inside display");
if (eltStack[eltTop].eltType != eltList)
ETMMsg ("problem: list-end not inside list");
else
PopElt ();
break;
case eltListItem:
if (displayFlag)
ETMMsg ("problem: list-item inside display");
if (listDepth == 0)
ETMMsg ("problem: list-item not inside list");
ControlOut ("LI", 1, 1);
break;
case eltHeader:
TerminateDefList ();
if (argc == 0)
{
ETMMsg ("header with no level found, ignored");
break;
}
else
misc = StrToShort (argv[0]);
if (bodyElt == eltHeader)
ETMMsg ("problem: nested headers found");
if (displayFlag)
ETMMsg ("problem: header inside display");
if (listDepth)
ETMMsg ("problem: header inside list");
PushElt (eltHeader, misc);
break;
case eltHeaderEnd:
if (eltStack[eltTop].eltType != eltHeader)
ETMMsg ("problem: header-end w/o matching header, ignored");
else
PopElt ();
break;
case eltBlockquote:
TerminateDefList ();
if (blockquoteFlag)
ETMMsg ("problem: nested blockquotes found");
if (displayFlag)
ETMMsg ("problem: blockquote inside display");
PushElt (eltBlockquote, 0);
break;
case eltBlockquoteEnd:
if (eltStack[eltTop].eltType != eltBlockquote)
ETMMsg ("problem: blockquote-end w/o matching blockquote, ignored");
else
PopElt ();
break;
case eltDefTerm: /* begin new definition term */
if (displayFlag)
ETMMsg ("problem: definition list inside display");
if (listDepth)
ETMMsg ("problem: definition list inside list");
switch (defListFlag)
{
case defListNone:
/*
* Start new list, new term
*/
ControlOut ("DL", 1, 1);
ControlOut ("DT", 1, 1);
break;
case defListTerm:
break; /* ?? */
case defListDesc:
/*
* Close current description, start new term
*/
ControlOut ("/DD", 1, 1);
ControlOut ("DT", 1, 1);
break;
}
defListFlag = defListTerm;
emptyDefTerm = 1;
break;
case eltDefDesc: /* begin new definition description */
if (displayFlag)
ETMMsg ("problem: definition list end inside display");
if (listDepth)
ETMMsg ("problem: definition list end inside list");
switch (defListFlag)
{
case defListNone:
/*
* Start new list, put out empty term, start new
* description. <BR> in the term forces a little
* space between this item and the previous.
*/
ControlOut ("DL", 1, 1);
ControlOut ("DT", 1, 0);
ControlOut ("BR", 0, 0);
ControlOut ("/DT", 0, 1);
ControlOut ("DD", 1, 1);
break;
case defListTerm:
/*
* Close current term, start new description.
*/
if (emptyDefTerm)
ControlOut ("BR", 0, 1);
ControlOut ("/DT", 1, 1);
ControlOut ("DD", 1, 1);
break;
case defListDesc:
break; /* ??? */
}
defListFlag = defListDesc;
break;
/*
* Right and left shift are implemented by <UL> and </UL>, which is
* no doubt a terrible way to do it. It'll have to do until there's
* a better way.
*/
case eltShiftRight:
TerminateDefList ();
ControlOut ("UL", 1, 1);
++shiftLevel;
break;
case eltShiftLeft:
if (shiftLevel > 0)
{
TerminateDefList ();
ControlOut ("/UL", 1, 1);
--shiftLevel;
}
break;
}
}
/*
* Begin the document HEAD part. Set up to start saving text in the title
* and header buffers. (Two buffers are used because line breaks and lines
* are written to the header buffer as is, but as spaces and '/' characters
* in the title -- this keeps the title all on a single line.) When the
* document BODY part begins, the title buffer is flushed to the document
* HEAD part and the header buffer is written as the initial heading of the
* BODY part.
*
* Should only be called from DoElement().
*/
static void
BeginDocHead (void)
{
title[0] = '\0';
header[0] = '\0';
/* if centering is on, add <CENTER> to header */
if (center)
StrAddStr (header, sizeof (header), "<CENTER>\n");
/* if underlining is on, add <UNDER> to header */
if (underline)
StrAddStr (header, sizeof (header), "\n<UNDER>\n");
}
/*
* Begin the document BODY part. First flush out the HEAD part, including
* the document title if there is one. Then put out the BODY tag and write
* the initial header if there is one.
*
* Should only be called from DoElement().
*
* cmdLineTitle, if set, overrides any title that was found in the body.
* However, if there is a title and it was found to be centered and/or
* underlined, the command-line title will also be centered and/or
* underlined.
*/
static void
BeginDocBody (void)
{
char *p;
/* drop trailing spaces on title */
p = title + strlen (title) - 1;
while (p > title && *(p-1) == ' ')
*--p = '\0';
/* put out HEAD if title isn't empty */
if (cmdLineTitle != (char *) NULL || title[0] != '\0')
{
ControlOut ("HEAD", 1, 1);
ControlOut (titleTag, 1, 1);
if (cmdLineTitle != (char *) NULL)
{
for (p = cmdLineTitle; *p != '\0'; p++)
StrOut (CharToHTML (*p));
}
else
StrOut (title);
ControlOut (endTitleTag, 1, 1);
ControlOut ("/HEAD", 1, 1);
}
title[0] = '\0';
ControlOut ("BODY", 1, 1);
if (cmdLineTitle != (char *) NULL)
{
ControlOut (headerTag, 1, 1);
if (center)
ControlOut ("CENTER", 1, 1);
if (underline)
ControlOut ("UNDER", 1, 1);
for (p = cmdLineTitle; *p != '\0'; p++)
StrOut (CharToHTML (*p));
if (center)
ControlOut ("/CENTER", 1, 1);
if (underline)
ControlOut ("/UNDER", 1, 1);
ControlOut (endHeaderTag, 1, 1);
}
else if (header[0] != '\0')
{
/*
* If centering is on, add an end-centering marker to the
* header. Any following text in the body will get its
* own begin-centering marker. Ditto for underlining.
*/
ControlOut (headerTag, 1, 1);
StrOut (header);
if (center)
ControlOut ("/CENTER", 1, 1);
if (underline)
ControlOut ("/UNDER", 1, 1);
ControlOut (endHeaderTag, 1, 1);
}
header[0] = '\0';
if (center)
ControlOut ("CENTER", 1, 1);
if (underline)
ControlOut ("UNDER", 1, 1);
}
static void
TerminateDefList (void)
{
switch (defListFlag)
{
default:
ETMPanic ("TerminateDefList: logic error");
case defListNone:
/* no current definition list - nothing to do */
break;
case defListTerm:
/*
* Close current term, write empty description, close list.
* <BR> in the term forces a little space between this item
* and the previous in case the term is empty.
*/
if (emptyDefTerm)
ControlOut ("BR", 0, 1);
ControlOut ("/DT", 1, 1);
ControlOut ("DD", 1, 0);
ControlOut ("/DD", 0, 1);
ControlOut ("/DL", 1, 1);
break;
case defListDesc:
/*
* Close current description, close list
*/
ControlOut ("/DD", 0, 1);
ControlOut ("/DL", 1, 1);
break;
}
defListFlag = defListNone;
}
static void
FlushTOC (void)
{
TOC *tp;
short level = 0;
char buf[bufSiz];
if (tocHead == (TOC *) NULL) /* no TOC */
return;
ControlOut ("!-- TOC BEGIN --", 1, 1);
tocCount = 0;
for (tp = tocHead; tp != (TOC *) NULL; tp = tp->tocNext)
{
/*
* If current level doesn't match entry level, decrease or
* increase current level to match.
*/
while (level > tp->tocLevel)
{
ControlOut ("/UL", 1, 1);
--level;
}
while (level < tp->tocLevel)
{
ControlOut ("UL", 1, 1);
++level;
}
ControlOut ("LI", 1, 1);
sprintf (buf, "A HREF=#TOC_%d", ++tocCount);
ControlOut (buf, 0, 0);
StrOut (tp->tocStr);
ControlOut ("/A", 0, 1);
}
while (level > 0)
{
ControlOut ("/UL", 1, 1);
--level;
}
ControlOut ("!-- TOC END --", 1, 1);
}
static void
PushElt (short eltType, short misc)
{
char buf[bufSiz];
if (++eltTop > maxElt - 1)
ETMPanic ("PushElt: element stack overflow");
eltStack[eltTop].eltType = eltType;
eltStack[eltTop].eltFont = curFont;
eltStack[eltTop].eltMisc = misc;
bodyElt = eltType; /* set current body element type */
/* begin new element */
switch (bodyElt)
{
default:
ETMPanic ("PushElt: logic error, type = %hd", bodyElt);
case eltPara:
ControlOut ("P", 1, 1); /* AddPara()??? */
break;
case eltDisplay:
displayFlag = 1;
SetFont (displayFont, displayFlag);
ControlOut ("PRE", 1, 1);
break;
case eltList:
ControlOut ("UL", 1, 1); /* DEPENDS ON TYPE */
++listDepth;
break;
case eltHeader:
sprintf (buf, "H%hd", misc); /* misc = header level */
ControlOut (buf, 1, 1);
break;
case eltBlockquote:
ControlOut ("BLOCKQUOTE", 1, 1);
++blockquoteFlag;
break;
break;
}
}
static void
PopElt (void)
{
char buf[bufSiz];
if (eltTop < 0)
ETMPanic ("PopElt: element stack underflow");
/* reset font, style if necessary */
/* close current element */
switch (eltStack[eltTop].eltType)
{
default:
ETMPanic ("PopElt: logic error, type = %hd", eltStack[eltTop].eltType);
case eltPara:
/* nothing to do */
break;
case eltDisplay:
ControlOut ("/PRE", 1, 1);
displayFlag = 0;
SetFont ("R", displayFlag);
break;
case eltList:
ControlOut ("/UL", 1, 1); /* DEPENDS ON TYPE */
--listDepth;
break;
case eltHeader:
/* eltMisc = header level */
sprintf (buf, "/H%hd", eltStack[eltTop].eltMisc);
ControlOut (buf, 1, 1);
break;
case eltBlockquote:
ControlOut ("/BLOCKQUOTE", 1, 1);
--blockquoteFlag;
break;
}
eltTop--;
bodyElt = eltStack[eltTop].eltType; /* restore previous type */
curFont = eltStack[eltTop].eltFont;
/*
* SHOULD I DO THIS?
if (curFont != (Font *) NULL)
SetFont (curFont->fontName);
*/
}
/*
* Perform a break. The action to take here depends on the current state.
*/
static void
AddBreak (void)
{
if (docPart == inNone)
{
ETMMsg ("leading break skipped");
return;
}
if (docPart == inHead)
{
AddTitleChar (' ');
StrAddStr (header, sizeof (header), "<BR>\n");
return;
}
/* handle body elements */
switch (bodyElt)
{
default:
ETMPanic ("AddBreak: logic error, part %d", bodyElt);
case eltPara:
case eltList:
case eltHeader:
case eltBlockquote:
ControlOut ("BR", 0, 1);
break;
case eltDisplay:
TextChOut (lf);
break;
}
if (tocLevel > 0) /* TOC entry is being collected */
StrAddStr (tocBuf, sizeof (tocBuf), "<BR>\n");
}
/*
* This should only be called from DoElement().
*/
static void
AddPara (void)
{
if (docPart == inNone)
{
ETMMsg ("leading space skipped");
return;
}
if (docPart == inHead)
{
AddTitleChar (' '); /* keep title all on one line */
StrAddStr (header, sizeof (header), "<P>\n");
return;
}
/* handle body elements */
switch (bodyElt)
{
default:
ETMPanic ("AddPara: logic error, elt %d", bodyElt);
case eltPara:
case eltList:
case eltHeader:
case eltBlockquote:
ControlOut ("P", 1, 1);
break;
case eltDisplay:
TextChOut (lf);
break;
}
if (tocLevel > 0) /* TOC entry is being collected */
StrAddStr (tocBuf, sizeof (tocBuf), "<P>\n");
}
/*
* Draw a line (horizontal only; ignore vertical).
*/
static void
AddLine (long length, short direction)
{
if (direction != 'h')
return;
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
if (docPart == inHead)
{
AddTitleChar ('/'); /* keep title all on one line */
StrAddStr (header, sizeof (header), "<HR>\n");
return;
}
/* handle body elements */
switch (bodyElt)
{
default:
ETMPanic ("AddLine: logic error, elt %d", bodyElt);
case eltPara:
case eltList:
case eltHeader:
case eltDisplay:
case eltBlockquote:
ControlOut ("HR", 1, 1);
break;
}
/* don't collect anything for TOC here */
}
/*
* Add a string literally to the output. (No escaping of characters
* that are special to HTML like & or < or >.) len is the effective
* length of the string, i.e., the number of characters what would be
* displayed in a browser. For example, the effective length of a
* string like "Á" is one.
*/
static void
AddLitStr (char *p)
{
char *p1, *p2;
char c;
if (p == (char *) NULL)
return;
switch (docPart)
{
default:
ETMPanic ("AddLitStr: logic error");
case inHead:
StrAddStr (title, sizeof (title), p);
StrAddStr (header, sizeof (header), p);
break;
case inBody:
/*
* This is the ugly part. Write characters of the string
* one by one. If a "&" is seen, assume it's part of a "&xxx;"
* sequence and pass it to HTMLChOut() all at once.
* Be careful about sequences that may have the terminating
* ";" missing.
*/
p1 = p;
while (*p1 != '\0')
{
if (*p1 != '&') /* not a "&xxx;" sequence */
TextChOut (*p1++);
else
{
/* save beginning of "&xxx;" and find end */
p2 = p1++;
while (*p1 != ';' && *p1 != '\0')
++p1;
if (*p1 == ';')
{
c = *(++p1); /* save next char */
*p1 = '\0';
HTMLChOut (p2);
*p1 = c;
}
else /* *p1 is '\0' */
{
ETMMsg ("malformed sequence, missing semicolon: <%s>", p2);
HTMLChOut (p2);
/* supply the missing semicolon */
TextChOut (';');
}
}
}
break;
}
if (tocLevel > 0)
StrAddStr (tocBuf, sizeof (tocBuf), p);
}
/*
* Add a string to the output. Escaping is done of characters
* that are special to HTML like & or < or >.
*/
static void
AddStr (char *p)
{
if (p == (char *) NULL)
return;
while (*p != '\0')
AddChar (*p++);
}
/*
* Add a character to the output. Escaping is done of characters
* that are special to HTML like & or < or >.
*/
static void
AddChar (char c)
{
switch (docPart)
{
default:
ETMPanic ("AddChar: logic error");
case inHead:
AddTitleChar (c);
AddHeaderChar (c);
break;
case inBody:
TextChOut (c);
if (defListFlag == defListTerm)
emptyDefTerm = 0;
break;
}
if (tocLevel > 0) /* TOC entry is being collected */
AddTOCChar (c);
}
static void
AddTitleChar (char c)
{
StrAddStr (title, sizeof (title), CharToHTML (c));
}
static void
AddHeaderChar (char c)
{
StrAddStr (header, sizeof (header), CharToHTML (c));
}
static void
AddTOCChar (char c)
{
StrAddStr (tocBuf, sizeof (tocBuf), CharToHTML (c));
}
static void
StrAddStr (char *buf, int size, char *str)
{
/* check whether or not str can be added to buf */
if (strlen (buf) + strlen (str) + 1 > size)
ETMPanic ("StrAddStr: buffer overflow");
(void) strcat (buf, str);
}
/*
* Table element routines
*/
static void
TableBegin (short argc, char **argv)
{
if (docPart == inNone) /* part unknown - set to default */
DoElement (eltPara, 0, (char **) NULL);
if (argc != 8)
ETMPanic ("TableBegin: wrong number of args: %hd", argc);
tblCols = StrToShort (argv[tblColsArg]);
tblRows = StrToShort (argv[tblRowsArg]);
if (tblCols == 0)
ETMPanic ("TableBegin: zero column table");
if (tblCols > tblMaxCol)
ETMPanic ("TableBegin: too many table columns (%hd), max = %d",
tblCols, tblMaxCol);
ControlOut ("BR", 1, 1);
if (argv[tblAlignArg][0] == 'C') /* center the table */
{
ControlOut ("CENTER", 1, 1);
centerTbl = 1;
}
Flush ();
StrOut ("<TABLE CELLPADDING=3");
if (argv[tblExpandArg][0] == 'y') /* expand */
StrOut (" WIDTH=\"100%\"");
/*
* Turn on cell borders. This isn't really technically very
* accurate, but I think HTML tables look better that way.
*/
StrOut (" BORDER");
ChOut ('>');
Flush ();
}
static void
TableEnd (void)
{
ControlOut ("/TABLE", 1, 1);
if (centerTbl)
{
ControlOut ("/CENTER", 1, 1);
centerTbl = 0;
}
ControlOut ("BR", 1, 1);
}
static void
TableRowBegin (short argc, char **argv)
{
ControlOut ("TR", 1, 1);
tblColIdx = 0;
}
static void
TableRowEnd (void)
{
ControlOut ("/TR", 1, 1);
++tblRowIdx;
}
static void
TableRowLine (short argc, char **argv)
{
ControlOut ("TR", 1, 1);
printf ("<TD COLSPAN=%hd></TD>\n", tblCols);
ControlOut ("/TR", 1, 1);
++tblRowIdx;
}
static void
TableCellInfo (short argc, char **argv)
{
short vspan, hspan;
if (argc != 5)
ETMPanic ("TableCellInfo: wrong number of args: %hd", argc);
cellType[tblColIdx] = argv[cellTypeArg][0];
cellVSpan[tblColIdx] = StrToShort (argv[cellVSpanArg]);
cellHSpan[tblColIdx] = StrToShort (argv[cellHSpanArg]);
cellVAdjust[tblColIdx] = argv[cellVAdjustArg][0];
/* border value is ignored */
++tblColIdx;
if (tblColIdx >= tblCols)
tblColIdx = 0;
}
static void
TableCellBegin (short argc, char **argv)
{
CellBegin (cellType[tblColIdx],
cellVSpan[tblColIdx],
cellHSpan[tblColIdx],
cellVAdjust[tblColIdx]);
}
static void
TableCellEnd (void)
{
ControlOut ("/TD", 1, 1);
++tblColIdx;
}
static void
TableEmptyCell (void)
{
CellBegin (cellType[tblColIdx],
cellVSpan[tblColIdx],
cellHSpan[tblColIdx],
cellVAdjust[tblColIdx]);
ControlOut ("BR", 0, 0);
ControlOut ("/TD", 0, 1);
++tblColIdx;
}
static void
TableSpannedCell (void)
{
++tblColIdx;
}
/*
* This is really poor. What's a better way?
*/
static void
TableCellLine (short argc, char **argv)
{
/* override type with 'C' to force centering of the line */
CellBegin ('C',
cellVSpan[tblColIdx],
cellHSpan[tblColIdx],
cellVAdjust[tblColIdx]);
StrOut ("---");
ControlOut ("/TD", 1, 1);
++tblColIdx;
}
static void
CellBegin (char type, short vspan, short hspan, char vadjust)
{
Flush ();
StrOut ("<TD");
/*
* Align the cell contents. Default is left. Numeric alignment isn't
* supported, so use right-alignment as the best substitute.
*/
switch (type)
{
default:
break;
case 'C':
StrOut (" ALIGN=CENTER");
break;
case 'R':
case 'N':
StrOut (" ALIGN=RIGHT");
break;
}
if (vadjust == 'T')
StrOut (" VALIGN=TOP");
if (vspan > 1)
printf (" ROWSPAN=%hd", vspan);
if (hspan > 1)
printf (" COLSPAN=%hd", hspan);
ChOut ('>');
Flush ();
}
/*
* basic output routines
*
* ControlOut() - write control word.
* TextStrOut() - write out a string of paragraph text. Does HTML escaping.
* TextChOut() - write out a character of paragraph text. Does HTML escaping.
* StrOut() - write out a string.
* ChOut() - write out a character. ALL output (except ETM messages)
* comes through this routine.
* AllowOutput () - allow or throttle output.
*/
static void
ControlOut (char *p, short brkBefore, short brkAfter)
{
if (p != (char *) NULL)
{
if (brkBefore) /* break if not already */
Flush (); /* at beginning of line */
ChOut ('<');
StrOut (p);
ChOut ('>');
if (brkAfter)
Flush ();
}
}
static void
TextStrOut (char *p)
{
if (p != (char *) NULL)
{
while (*p != '\0')
TextChOut (*p++);
}
}
/*
* HTML-ize a character and return pointer to resulting string.
* This keeps characters like < and > from being interpreted
* specially by Web browsers. Return value is a pointer into a
* static buffer, and should be copied if the caller needs to hang
* onto it for a while.
*/
static char *
CharToHTML (int c)
{
static char buf[7];
switch (c)
{
default:
buf[0] = c;
buf[1] = '\0';
break;
case '&':
(void) strcpy (buf, "&");
break;
case '<':
(void) strcpy (buf, "<");
break;
case '>':
(void) strcpy (buf, ">");
break;
case '"':
(void) strcpy (buf, """);
break;
}
return (buf);
}
/*
* Write a character of document text.
*/
static void
TextChOut (int c)
{
HTMLChOut (CharToHTML (c));
}
/*
* Write a character of document text. The character is passed in as
* a string, which is assumed to be the HTML representation of a single
* character (e.g., "x" for x, "&" for &.
* - Sets tCount to the number of text (non-tag) characters on the line.
* - Sets etCount to the number of effective text (non-tag) characters.
* - Sets cCount to the number of characters on the line (via ChOut ()).
* - Does line wrapping if not in a display or in fill mode.
* - Handles display indent.
*
* Characters written to produce display indent are not included in the
* tCount or etCount line output character counts. Tabbing in displays is
* thus relative to the indent, not to the document left margin.
*/
static void
HTMLChOut (char *p)
{
int len;
int i;
/*
* Check to make sure this is either a single character string or
* that it begins with &.
*/
if (*p == '\0' || (*p != '&' && *(p+1) != '\0'))
ETMPanic ("HTMLChOut: logic error (%s)", p);
/*
* Don't do line wrapping if collecting a display or
* in nofill mode.
*/
if (displayFlag || fill == 0)
{
/*
* If this is the start of a display line, put out spaces to
* add the leading indent. Exception: if the character is a
* linefeed, the line is blank, so there's no need for leading
* spaces.
*/
if (displayFlag && tCount == 0 && *p != lf)
{
for (i = 0; i < displayIndent; i++)
ChOut (' ');
/*
* tCount and etCount are deliberately NOT set here,
* so tabs in displays are relative to the indent,
* not to the left margin.
*/
}
if (*p == tab) /* print a tab */
{
/*
* Assume worst case (that all tabstops are too small),
* in which case only print a single space. If an
* appropriate tabstop is found, use it.
*/
len = 1;
for (i = 0; i < tabCount; i++)
{
if (tabPos[i] > etCount)
{
len = tabPos[i] - etCount;
break;
}
}
for (i = 0; i < len; i++)
ChOut (' ');
tCount += len;
etCount += len;
}
else /* not a tab */
{
StrOut (p);
if (*p != lf)
{
tCount += strlen (p);
++etCount;
}
}
return;
}
/*
* Not in display or fill mode. Do line wrapping to keep output
* lines from becoming really long. When that's done, it also
* becomes useful to ignore leading spaces that occur at the
* beginning of the next line. Tabs are not processed if line
* wrapping occurs - just print a space.
*/
if (*p == tab)
*p = ' ';
if (*p == ' ' && tCount == 0) /* leading space */
return;
if (*p == ' ' && cCount > 60) /* break line */
Flush (); /* (sets cCount, tCount to zero */
else
{
StrOut (p);
tCount += strlen (p);
++etCount;
}
}
/*
* StrOut() and ChOut() result in cCount being set. If a linefeed is
* printed, both cCount and tCount are set to zero.
*/
static void
StrOut (char *p)
{
if (p != (char *) NULL)
{
while (*p != '\0')
ChOut (*p++);
}
}
static void
ChOut (int c)
{
if (!allowOutput)
return;
if (c != ' ' && pendingAnchor != (char *) NULL)
{
if (fputs (pendingAnchor, stdout) == EOF)
ETMPanic ("Write error, cannot continue");
cCount += strlen (pendingAnchor);
Free (pendingAnchor);
pendingAnchor = (char *) NULL;
}
if (putc (c, stdout) == EOF)
ETMPanic ("Write error, cannot continue");
++cCount;
if (c == lf)
{
cCount = 0;
tCount = 0;
etCount = 0;
}
}
static void
Flush (void)
{
if (cCount > 0)
ChOut (lf);
}
static int
AllowOutput (int yesno)
{
int prev = allowOutput;
allowOutput = yesno;
return (prev);
}
/*
* Read font information file. Format of lines is
*
* troff-font-name begin-tag end-tag begin-tag2 end-tag2
*
* begin-tag2 and end-tag2 are used when in displays
*
* For example:
* R "" "" "" ""
* I <I> </I> <I> </I>
* B <B> </B> <B> </B>
* CW <TT> </TT> "" ""
*/
static int
ReadFonts (char *filename)
{
FILE *f;
char buf[bufSiz];
Font *fp;
char *name, *begin, *end, *dbegin, *dend;
if ((f = TCROpenLibFile (filename, "r")) == (FILE *) NULL)
return (0);
while (TCRGetLine (buf, (int) sizeof (buf), f))
{
if (buf[0] == '#') /* skip comments */
continue;
TSScanInit (buf);
name = TSScan ();
begin = TSScan ();
end = TSScan ();
dbegin = TSScan ();
dend = TSScan ();
if (dend == (char *) NULL)
continue;
fp = New (Font);
fp->fontName = StrAlloc (name);
fp->beginTag = StrAlloc (begin);
fp->endTag = StrAlloc (end);
fp->dispBeginTag = StrAlloc (dbegin);
fp->dispEndTag = StrAlloc (dend);
fp->inDisplay = 0;
fp->next = fontList;
fontList = fp;
}
(void) fclose (f);
return (1);
}
static Font *
LookupFont (char *name)
{
Font *fp;
for (fp = fontList; fp != (Font *) NULL; fp = fp->next)
{
if (strcmp (name, fp->fontName) == 0)
break;
}
return (fp); /* NULL if not found */
}
|