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 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595
|
/*
* xgraph - A Simple Plotter for X
*
* David Harrison
* University of California, Berkeley
* 1986, 1987, 1988, 1989
*
* Please see copyright.h concerning the formal reproduction rights
* of this software.
*/
#include <stdlib.h>
#include <string.h>
#include "copyright.h"
#include <stdio.h>
#include <math.h>
#include <pwd.h>
#include <ctype.h>
#include "xgout.h"
#include "xgraph.h"
#include "xtb.h"
#include "hard_devices.h"
#include "params.h"
void InitSets();
void ReadDefaults();
void ParseArgs();
int ReadData();
void DrawWindow();
void DelWindow();
void PrintWindow();
int HandleZoom();
void msg_box();
int TransformCompute();
void DrawTitle();
void DrawLegend();
void DrawGridAndAxis();
void DrawData();
void WriteValue();
int getDigits();
/*
* USE_NEW_COLORS selects the default color scheme. Set to one of the following:
*
* 0 if you want the original colors.
* 1 for less garish colors (preferred by Radford Neal)
* 2 for less garish "Web-Safe Colors".
* 3 for close to original colors, but Web-Safe (prefered by Peter D Gilbert)
*/
#define USE_NEW_COLORS 1
#define USE_GEOMETRY 1 /* Set to 0 to disable =WxH+X+Y argument */
#define PDG 1 /* Set to 0 to disable PDG's modifications */
#if PDG
/*
* Changes bracketed by 'PDG' were made by Peter D Gilbert
* (peter.gilbert@digital.com).
*/
#endif
#define ZOOM
#define TOOLBOX
#define NEW_READER
/* Portability */
#ifdef CRAY
#undef MAXFLOAT
#define MAXFLOAT 10.e300
#endif /* CRAY */
#ifndef MAXFLOAT
#ifndef __CYGWIN__
#define MAXFLOAT HUGE
#else
#define MAXFLOAT HUGE_VAL
#endif
#endif
#define BIGINT 0xfffffff
#define GRIDPOWER 10
#define INITSIZE 128
#define CONTROL_D '\004'
#define CONTROL_C '\003'
#define TILDE '~'
#define BTNPAD 1
#define BTNINTER 3
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define ABS(x) ((x) < 0 ? -(x) : (x))
#define ZERO_THRES 1.0E-07
/* To get around an inaccurate log */
#define nlog10(x) (x == 0.0 ? 0.0 : log10(x) + 1e-15)
#define ISCOLOR (wi->dev_info.dev_flags & D_COLOR)
#define PIXVALUE(set) ((set) % MAXATTR)
#define LINESTYLE(set) \
(ISCOLOR ? ((set)/MAXATTR) : ((set) % MAXATTR))
#define MARKSTYLE(set) \
(colorMark ? COLMARK(set) : BWMARK(set))
#define COLMARK(set) \
((set) / MAXATTR)
#define BWMARK(set) \
((set) % MAXATTR)
#define LOG_X 0x01
#define LOG_Y 0x02
/*
* Default settings for xgraph parameters
*/
#define DEF_BORDER_WIDTH "2"
#define DEF_BORDER_COLOR "Black"
#define DEF_TITLE_TEXT ""
#define DEF_XUNIT_TEXT "X"
#define DEF_YUNIT_TEXT "Y"
#define DEF_TICK_FLAG "off"
#define DEF_MARK_FLAG "off"
#define DEF_PIXMARK_FLAG "off"
#define DEF_LARGEPIX_FLAG "off"
#define DEF_DIFFMARK_FLAG "off"
#define DEF_BB_FLAG "off"
#define DEF_NOLINE_FLAG "off"
#define DEF_LOGX_FLAG "off"
#define DEF_LOGY_FLAG "off"
#define DEF_BAR_FLAG "off"
#define DEF_BAR_BASE "0.0"
#define DEF_BAR_WIDTH "-1.0"
#define DEF_LINE_WIDTH "0"
#define DEF_GRID_SIZE "0"
#define DEF_GRID_STYLE "10"
#define DEF_LABEL_FONT "helvetica-12"
#define DEF_TITLE_FONT "helvetica-18"
#define DEF_GEOMETRY "600x600+100+100"
#define DEF_REVERSE "off"
#define DEF_DEVICE "Postscript"
#define DEF_DISPOSITION "To Device"
#define DEF_FILEORDEV ""
#define DEF_MARKER_FLAG "off"
#define DEF_DIFFMARK_FLAG "off"
#define DEF_PIXMARK_FLAG "off"
#define DEF_LARGEPIX_FLAG "off"
/* Low > High means set it based on the data */
#define DEF_LOW_LIMIT "1.0"
#define DEF_HIGH_LIMIT "0.0"
/* Black and white defaults */
#define DEF_BW_BACKGROUND "white"
#define DEF_BW_BORDER "black"
#define DEF_BW_ZEROCOLOR "black"
#define DEF_BW_ZEROWIDTH "3"
#define DEF_BW_ZEROSTYLE "1"
#define DEF_BW_FOREGROUND "black"
/* Color defaults */
#if USE_NEW_COLORS == 0
#define DEF_COL_BACKGROUND "#ccc"
#elif USE_NEW_COLORS == 1
#define DEF_COL_BACKGROUND "#b0b0b0"
#else
/* For some reason, "#cccccc" doesn't work as well as the following */
#define DEF_COL_BACKGROUND "#ccc"
#endif
#define DEF_COL_BORDER "black"
#define DEF_COL_ZEROCOLOR "white"
#define DEF_COL_ZEROWIDTH "0"
#define DEF_COL_ZEROSTYLE "1"
#define DEF_COL_FOREGROUND "black"
#define DEF_COL_FIRSTSTYLE "1"
/* Default line styles */
static char *defStyle[MAXATTR] = {
"1", "10", "11110000", "010111", "1110",
"1111111100000000", "11001111", "0011000111"
};
/* Default color names */
#if USE_NEW_COLORS == 0 /* These were the original colors */
static char *defColors[MAXATTR] = {
"red", "SpringGreen", "blue", "yellow",
"cyan", "sienna", "orange", "coral"
};
#elif USE_NEW_COLORS == 1 /* Less garish ones preferred by R. Neal */
static char *defColors[MAXATTR] = {
"#a00000", "#009000", "#0000a0", "#908000",
"#007070", "#800080", "#b05050", "#306020",
};
#elif USE_NEW_COLORS == 2 /* Less garish "Web-Safe Colors" */
static char *defColors[MAXATTR] = {
"#990000", "#009900", "#000099", "#999900",
"#006666", "#990099", "#cc6666", "#336633",
};
#elif USE_NEW_COLORS == 3 /* Close to the original colors, but web-safe */
static char *defColors[MAXATTR] = {
/* From /usr/lib/X11/rgb.txt : */
/* orange = 255 160 122 = ff a0 00 -> #ff9900 has troubles; #cc9900 works */
/* coral = 255 127 80 = ff 7f 50 -> #ff6666 has troubles; #ff6633 works */
"#ff0000", "#00ff99", "#0000ff", "#ffff00",
"#00ffff", "#996633", "#cc9900", "#ff6633"
};
#endif
extern void init_X();
extern void do_error();
static char *tildeExpand();
static void ReverseIt();
static void set_mark_flags();
static void Traverse();
typedef struct point_list {
int numPoints; /* Number of points in group */
int allocSize; /* Allocated size */
double *xvec; /* X values */
double *yvec; /* Y values */
struct point_list *next; /* Next set of points */
} PointList;
typedef struct new_data_set {
char *setName; /* Name of data set */
PointList *list; /* List of point arrays */
} NewDataSet;
static NewDataSet PlotData[MAXSETS];
static XSegment *Xsegs; /* Point space for X */
/* Basic transformation stuff */
static double llx, lly, urx, ury; /* Bounding box of all data */
#define HARDCOPY_IN_PROGRESS 0x01
typedef struct local_win {
double loX, loY, hiX, hiY; /* Local bounding box of window */
int XOrgX, XOrgY; /* Origin of bounding box on screen */
int XOppX, XOppY; /* Other point defining bounding box */
double UsrOrgX, UsrOrgY; /* Origin of bounding box in user space */
double UsrOppX, UsrOppY; /* Other point of bounding box */
double XUnitsPerPixel; /* X Axis scale factor */
double YUnitsPerPixel; /* Y Axis scale factor */
xgOut dev_info; /* Device information */
Window close, hardcopy; /* Buttons for closing and hardcopy */
Window about; /* Version information */
int flags; /* Window flags */
} LocalWin;
#define SCREENX(ws, userX) \
(((int) (((userX) - ws->UsrOrgX)/ws->XUnitsPerPixel + 0.5)) + ws->XOrgX)
#define SCREENY(ws, userY) \
(ws->XOppY - ((int) (((userY) - ws->UsrOrgY)/ws->YUnitsPerPixel + 0.5)))
static XContext win_context = (XContext) 0;
/* Other globally set defaults */
Display *disp; /* Open display */
Visual *vis; /* Standard visual */
Colormap cmap; /* Standard colormap */
int screen; /* Screen number */
int depth; /* Depth of screen */
static int numFiles = 0; /* Number of input files */
static char *inFileNames[MAXSETS]; /* File names */
/* Total number of active windows */
static int Num_Windows = 0;
static char *Prog_Name;
#if PDG
static char *Window_Name;
#endif
static int XErrHandler(); /* Handles error messages */
int main(argc, argv)
int argc;
char *argv[];
/*
* This sets up the hard-wired defaults and reads the X defaults.
* The command line format is: xgraph [host:display].
*/
{
Window primary, NewWindow();
XEvent theEvent;
LocalWin *win_info;
Cursor zoomCursor;
FILE *strm;
XColor fg_color, bg_color;
char keys[MAXKEYS], *disp_name;
int nbytes, idx, maxitems, flags;
int errs = 0;
/* Open up new display */
Prog_Name = argv[0];
#if PDG
Window_Name = NULL;
#endif
disp_name = NULL; /* PDG 1/7/99 */
for (idx = 1; idx < argc-1; idx++) {
if (strcmp(argv[idx], "-display") == 0) {
disp_name = argv[idx+1];
break;
}
}
disp = XOpenDisplay(disp_name);
if (!disp) {
(void) fprintf(stderr, "%s: cannot open display `%s'\n", argv[0], disp_name);
abort();
}
XSetErrorHandler(XErrHandler);
/* Set up hard-wired defaults and allocate spaces */
InitSets();
/* Read X defaults and override hard-coded defaults */
ReadDefaults();
/* Parse the argument list looking for input files */
ParseArgs(argc, argv, 0);
/* Read the data into the data sets */
llx = lly = MAXFLOAT;
urx = ury = -MAXFLOAT;
for (idx = 0; idx < numFiles; idx++) {
#if PDG
if (!Window_Name) Window_Name = inFileNames[idx];
#endif
strm = fopen(inFileNames[idx], "r");
if (!strm) {
(void) fprintf(stderr, "Warning: cannot open file `%s'\n",
inFileNames[idx]);
} else {
if ((maxitems = ReadData(strm, inFileNames[idx])) < 0) {
errs++;
}
(void) fclose(strm);
}
}
if (!numFiles) {
if ((maxitems = ReadData(stdin, (char *) 0)) < 0) {
errs++;
}
}
if (errs) {
(void) fprintf(stderr, "Problems found with input data.\n");
exit(1);
}
#if PDG
if (!Window_Name) Window_Name = Prog_Name;
#endif
/* Parse the argument list to set options */
ParseArgs(argc, argv, 1);
Xsegs = (XSegment *) malloc((unsigned) (maxitems * sizeof(XSegment)));
/* Reverse Video Hack */
if (PM_BOOL("ReverseVideo")) ReverseIt();
hard_init();
if (PM_BOOL("Debug")) {
(void) XSynchronize(disp, 1);
param_dump();
}
/* Logarithmic and bounding box computation */
flags = 0;
if (PM_BOOL("LogX")) flags |= LOG_X;
if (PM_BOOL("LogY")) flags |= LOG_Y;
Traverse(flags);
/* Nasty hack here for bar graphs */
if (PM_BOOL("BarGraph")) {
double base;
llx -= PM_DBL("BarWidth");
urx += PM_DBL("BarWidth");
base = PM_DBL("BarBase");
if (base < lly) lly = base;
if (base > ury) ury = base;
}
/* Create initial window */
xtb_init(disp, screen, PM_PIXEL("Foreground"), PM_PIXEL("Background"),
PM_FONT("LabelFont"));
primary = NewWindow(
#if !PDG
Prog_Name,
#else
Window_Name,
#endif
PM_DBL("XLowLimit"), PM_DBL("YLowLimit"),
PM_DBL("XHighLimit"), PM_DBL("YHighLimit"),
1.0, 1);
if (!primary) {
(void) fprintf(stderr, "Main window would not open\n");
exit(1);
}
zoomCursor = XCreateFontCursor(disp, XC_sizing);
fg_color = PM_COLOR("Foreground");
bg_color = PM_COLOR("Background");
XRecolorCursor(disp, zoomCursor, &fg_color, &bg_color);
Num_Windows = 1;
while (Num_Windows > 0) {
XNextEvent(disp, &theEvent);
if (xtb_dispatch(&theEvent) != XTB_NOTDEF) continue;
if (XFindContext(theEvent.xany.display,
theEvent.xany.window,
win_context, (caddr_t *) &win_info)) {
/* Nothing found */
continue;
}
switch (theEvent.type) {
case Expose:
if (theEvent.xexpose.count <= 0) {
XWindowAttributes win_attr;
XGetWindowAttributes(disp, theEvent.xany.window, &win_attr);
win_info->dev_info.area_w = win_attr.width;
win_info->dev_info.area_h = win_attr.height;
init_X(win_info->dev_info.user_state);
DrawWindow(win_info);
}
break;
case KeyPress:
nbytes = XLookupString(&theEvent.xkey, keys, MAXKEYS,
(KeySym *) 0, (XComposeStatus *) 0);
for (idx = 0; idx < nbytes; idx++) {
if (keys[idx] == CONTROL_D) {
/* Delete this window */
DelWindow(theEvent.xkey.window, win_info);
} else if (keys[idx] == CONTROL_C) {
/* Exit program */
Num_Windows = 0;
} else if (keys[idx] == 'h') {
PrintWindow(theEvent.xany.window, win_info);
}
}
break;
case ButtonPress:
/* Handle creating a new window */
Num_Windows += HandleZoom(
#if !PDG
Prog_Name,
#else
Window_Name,
#endif
&theEvent.xbutton,
win_info, zoomCursor);
break;
default:
(void) fprintf(stderr, "Unknown event type: %x\n", theEvent.type);
break;
}
}
return 0;
}
#define BLACK_THRES 30000
static void ReversePix(param_name)
char *param_name; /* Name of color parameter */
/*
* Looks up `param_name' in the parameters database. If found, the
* color is examined and judged to be either black or white based
* upon its red, green, and blue intensities. The sense of the
* color is then reversed and reset to its opposite.
*/
{
params val;
if (param_get(param_name, &val)) {
if ((val.pixv.value.red < BLACK_THRES) &&
(val.pixv.value.green < BLACK_THRES) &&
(val.pixv.value.blue < BLACK_THRES)) {
/* Color is black */
param_reset(param_name, "white");
} else {
/* Color is white */
param_reset(param_name, "black");
}
} else {
(void) fprintf(stderr, "Cannot reverse color `%s'\n", param_name);
}
}
static void ReverseIt()
/*
* This routine attempts to implement reverse video. It steps through
* all of the important colors in the parameters database and makes
* black white (and vice versa).
*/
{
int i;
char buf[1024];
for (i = 0; i < MAXATTR; i++) {
(void) sprintf(buf, "%d.Color", i);
ReversePix(buf);
}
ReversePix("Foreground");
ReversePix("Border");
ReversePix("ZeroColor");
ReversePix("Background");
}
static void Traverse(flags)
int flags; /* Options */
/*
* Traverses through all of the data applying certain options to the
* data and computing the overall bounding box. The flags are:
* LOG_X Take the log of the X axis
* LOG_Y Take the log of the Y axis
*/
{
int i, j;
PointList *spot;
for (i = 0; i < MAXSETS; i++) {
for (spot = PlotData[i].list; spot; spot = spot->next) {
for (j = 0; j < spot->numPoints; j++) {
if (flags & LOG_Y) {
if (spot->yvec[j] > 0.0) {
spot->yvec[j] = log10(spot->yvec[j]);
} else {
(void) fprintf(stderr, "Cannot plot non-positive Y values\n\
when the logarithmic option is selected.\n");
exit(1);
}
}
if (flags & LOG_X) {
if (spot->xvec[j] > 0.0) {
spot->xvec[j] = log10(spot->xvec[j]);
} else {
(void) fprintf(stderr, "Cannot plot non-positive X values\n\
when the logarithmic option is selected.\n");
exit(1);
}
}
/* Update global bounding box */
if (spot->xvec[j] < llx) llx = spot->xvec[j];
if (spot->xvec[j] > urx) urx = spot->xvec[j];
if (spot->yvec[j] < lly) lly = spot->yvec[j];
if (spot->yvec[j] > ury) ury = spot->yvec[j];
}
}
}
}
/*
* Button handling functions
*/
/*ARGSUSED*/
xtb_hret del_func(win, bval, info)
Window win; /* Button window */
int bval; /* Button value */
char *info; /* User information */
/*
* This routine is called when the `Close' button is pressed in
* an xgraph window. It causes the window to go away.
*/
{
Window the_win = (Window) info;
LocalWin *win_info;
xtb_bt_set(win, 1, (char *) 0, 0);
if (!XFindContext(disp, the_win, win_context, (caddr_t *) &win_info)) {
if (win_info->flags & HARDCOPY_IN_PROGRESS) {
do_error("Can't close window while\nhardcopy dialog is posted.\n");
xtb_bt_set(win, 0, (char *) 0, 0);
} else {
DelWindow(the_win, win_info);
}
}
return XTB_HANDLED;
}
/*ARGSUSED*/
xtb_hret hcpy_func(win, bval, info)
Window win; /* Button Window */
int bval; /* Button value */
char *info; /* User Information */
/*
* This routine is called when the hardcopy button is pressed
* in an xgraph window. It causes the output dialog to be
* posted.
*/
{
Window the_win = (Window) info;
LocalWin *win_info;
xtb_bt_set(win, 1, (char *) 0, 0);
if (!XFindContext(disp, the_win, win_context, (caddr_t *) &win_info)) {
win_info->flags |= HARDCOPY_IN_PROGRESS;
PrintWindow(the_win, win_info);
win_info->flags &= (~HARDCOPY_IN_PROGRESS);
}
xtb_bt_set(win, 0, (char *) 0, 0);
return XTB_HANDLED;
}
static
/*ARGSUSED*/
xtb_hret abt_func(win, bval, info)
Window win; /* Button window */
int bval; /* Button value */
char *info; /* User information */
{
static char *msg_fmt =
"Version %s\n\
Written by David Harrison,\n\
University of California, Berkeley\n\
Modified and packaged by Radford Neal,\n\
2000-09-10. Send comments to\n\
radford@cs.toronto.edu";
static int active = 0;
char msg_buf[1024];
if (!active) {
active = 1;
xtb_bt_set(win, 1, (char *) 0, 0);
(void) sprintf(msg_buf, msg_fmt, VERSION_STRING);
msg_box("XGraph", msg_buf);
xtb_bt_set(win, 0, (char *) 0, 0);
active = 0;
}
return XTB_HANDLED;
}
#define NORMSIZE 600
#define MINDIM 100
Window NewWindow(progname, lowX, lowY, upX, upY, asp, primary)
char *progname; /* Name of program */
double lowX, lowY; /* Lower left corner */
double upX, upY; /* Upper right corner */
double asp; /* Aspect ratio */
int primary; /* Is this the primary window? */
/*
* Creates and maps a new window. This includes allocating its
* local structure and associating it with the XId for the window.
* The aspect ratio is specified as the ratio of width over height.
*/
{
Window new_window;
LocalWin *new_info;
static Cursor theCursor = (Cursor) 0;
XSizeHints sizehints;
XSetWindowAttributes wattr;
XWMHints wmhints;
XColor fg_color, bg_color;
int geo_mask;
int width, height;
unsigned long wamask;
double pad;
params geom;
new_info = (LocalWin *) malloc(sizeof(LocalWin));
if (upX > lowX) {
new_info->loX = lowX;
new_info->hiX = upX;
} else {
new_info->loX = llx;
new_info->hiX = urx;
}
if (upY > lowY) {
new_info->loY = lowY;
new_info->hiY = upY;
} else {
new_info->loY = lly;
new_info->hiY = ury;
}
/* Increase the padding for aesthetics */
if (new_info->hiX - new_info->loX == 0.0) {
pad = MAX(0.5, fabs(new_info->hiX/2.0));
new_info->hiX += pad;
new_info->loX -= pad;
}
if (new_info->hiY - new_info->loY == 0) {
pad = MAX(0.5, fabs(ury/2.0));
new_info->hiY += pad;
new_info->loY -= pad;
}
/* Add 10% padding to bounding box (div by 20 yeilds 5%) */
pad = (new_info->hiX - new_info->loX) / 20.0;
new_info->loX -= pad; new_info->hiX += pad;
pad = (new_info->hiY - new_info->loY) / 20.0;
new_info->loY -= pad; new_info->hiY += pad;
sizehints.x = sizehints.y = 100;
width = height = NORMSIZE;
#if USE_GEOMETRY
if (param_get("GEOMETRY",&geom)) {
sscanf (geom.strv.value, "%dx%d%d%d",
&width, &height, &sizehints.x, &sizehints.y);
}
if (!primary) {
sizehints.x -= 35;
if (sizehints.x<0) sizehints.x = 0;
sizehints.y += 25;
}
#endif
/* Aspect ratio computation */
if (asp < 1.0) {
width = ((int) (((double) NORMSIZE) * asp));
} else {
height = ((int) (((double) NORMSIZE) / asp));
}
height = MAX(MINDIM, height);
width = MAX(MINDIM, width);
wamask = CWBackPixel | CWBorderPixel | CWColormap;
wattr.background_pixel = PM_PIXEL("Background");
wattr.border_pixel = PM_PIXEL("Border");
wattr.colormap = cmap;
sizehints.flags = PPosition|PSize;
sizehints.width = width;
sizehints.height = height;
new_window = XCreateWindow(disp, RootWindow(disp, screen),
sizehints.x, sizehints.y,
(unsigned int) sizehints.width,
(unsigned int) sizehints.height,
(unsigned int) PM_INT("BorderSize"),
depth, InputOutput, vis,
wamask, &wattr);
if (new_window) {
xtb_frame cl_frame, hd_frame, ab_frame;
XStoreName(disp, new_window, progname);
XSetIconName(disp, new_window, progname);
wmhints.flags = InputHint | StateHint;
wmhints.input = True;
wmhints.initial_state = NormalState;
XSetWMHints(disp, new_window, &wmhints);
geo_mask = XParseGeometry(PM_STR("Geometry"), &sizehints.x, &sizehints.y,
(unsigned int *) &sizehints.width,
(unsigned int *) &sizehints.height);
if (geo_mask & (XValue | YValue)) {
sizehints.flags = (sizehints.flags & ~PPosition) | USPosition;
}
if (geo_mask & (WidthValue | HeightValue)) {
sizehints.flags = (sizehints.flags & ~PSize) | USSize;
}
XSetNormalHints(disp, new_window, &sizehints);
/* Set device info */
set_X(new_window, &(new_info->dev_info));
/* Make buttons */
xtb_bt_new(new_window, "Close", del_func,
(xtb_data) new_window, &cl_frame);
new_info->close = cl_frame.win;
XMoveWindow(disp, new_info->close, (int) BTNPAD, (int) BTNPAD);
xtb_bt_new(new_window, "Hardcopy", hcpy_func,
(xtb_data) new_window, &hd_frame);
new_info->hardcopy = hd_frame.win;
XMoveWindow(disp, new_info->hardcopy,
(int) (BTNPAD + cl_frame.width + BTNINTER),
BTNPAD);
xtb_bt_new(new_window, "About", abt_func,
(xtb_data) new_window, &ab_frame);
new_info->about = ab_frame.win;
XMoveWindow(disp, new_info->about,
(int) (BTNPAD + cl_frame.width + BTNINTER +
hd_frame.width + BTNINTER), BTNPAD);
new_info->flags = 0;
XSelectInput(disp, new_window,
ExposureMask|KeyPressMask|ButtonPressMask);
if (!theCursor) {
theCursor = XCreateFontCursor(disp, XC_top_left_arrow);
fg_color = PM_COLOR("Foreground");
bg_color = PM_COLOR("Background");
XRecolorCursor(disp, theCursor, &fg_color, &bg_color);
}
XDefineCursor(disp, new_window, theCursor);
if (!win_context) {
win_context = XUniqueContext();
}
XSaveContext(disp, new_window, win_context, (caddr_t) new_info);
XMapWindow(disp, new_window);
return new_window;
} else {
return (Window) 0;
}
}
void DelWindow(win, win_info)
Window win; /* Window */
LocalWin *win_info; /* Local Info */
/*
* This routine actually deletes the specified window and
* decrements the window count.
*/
{
xtb_data info;
XDeleteContext(disp, win, win_context);
xtb_bt_del(win_info->close, &info);
xtb_bt_del(win_info->hardcopy, &info);
xtb_bt_del(win_info->about, &info);
free((char *) win_info);
XDestroyWindow(disp, win);
Num_Windows -= 1;
}
void PrintWindow(win, win_info)
Window win; /* Window */
LocalWin *win_info; /* Local Info */
/*
* This routine posts a dialog asking about the hardcopy
* options desired. If the user hits `OK', the hard
* copy is performed.
*/
{
ho_dialog(win, Prog_Name, (char *) win_info);
}
static XRectangle boxEcho;
static GC echoGC = (GC) 0;
#define DRAWBOX \
if (startX < curX) { \
boxEcho.x = startX; \
boxEcho.width = curX - startX; \
} else { \
boxEcho.x = curX; \
boxEcho.width = startX - curX; \
} \
if (startY < curY) { \
boxEcho.y = startY; \
boxEcho.height = curY - startY; \
} else { \
boxEcho.y = curY; \
boxEcho.height = startY - curY; \
} \
XDrawRectangles(disp, win, echoGC, &boxEcho, 1);
#define TRANX(xval) \
(((double) ((xval) - wi->XOrgX)) * wi->XUnitsPerPixel + wi->UsrOrgX)
#define TRANY(yval) \
(wi->UsrOppY - (((double) ((yval) - wi->XOrgY)) * wi->YUnitsPerPixel))
int HandleZoom(progname, evt, wi, cur)
char *progname;
XButtonPressedEvent *evt;
LocalWin *wi;
Cursor cur;
{
Window win, new_win;
Window root_rtn, child_rtn;
XEvent theEvent;
int startX, startY, curX, curY, newX, newY, stopFlag, numwin;
int root_x, root_y;
unsigned int mask_rtn;
double loX, loY, hiX, hiY, asp;
win = evt->window;
if (XGrabPointer(disp, win, True,
(unsigned int) (ButtonPressMask|ButtonReleaseMask|
PointerMotionMask|PointerMotionHintMask),
GrabModeAsync, GrabModeAsync,
win, cur, CurrentTime) != GrabSuccess) {
XBell(disp, 0);
return 0;
}
if (echoGC == (GC) 0) {
unsigned long gcmask;
XGCValues gcvals;
gcmask = GCForeground | GCFunction;
gcvals.foreground = PM_PIXEL("ZeroColor") ^ PM_PIXEL("Background");
gcvals.function = GXxor;
echoGC = XCreateGC(disp, win, gcmask, &gcvals);
}
startX = evt->x; startY = evt->y;
XQueryPointer(disp, win, &root_rtn, &child_rtn, &root_x, &root_y,
&curX, &curY, &mask_rtn);
/* Draw first box */
DRAWBOX;
stopFlag = 0;
while (!stopFlag) {
XNextEvent(disp, &theEvent);
switch (theEvent.xany.type) {
case MotionNotify:
XQueryPointer(disp, win, &root_rtn, &child_rtn, &root_x, &root_y,
&newX, &newY, &mask_rtn);
/* Undraw the old one */
DRAWBOX;
/* Draw the new one */
curX = newX; curY = newY;
DRAWBOX;
break;
case ButtonRelease:
DRAWBOX;
XUngrabPointer(disp, CurrentTime);
stopFlag = 1;
if ((startX-curX != 0) && (startY-curY != 0)) {
/* Figure out relative bounding box */
loX = TRANX(startX); loY = TRANY(startY);
hiX = TRANX(curX); hiY = TRANY(curY);
if (loX > hiX) {
double temp;
temp = hiX;
hiX = loX;
loX = temp;
}
if (loY > hiY) {
double temp;
temp = hiY;
hiY = loY;
loY = temp;
}
/* physical aspect ratio */
asp = ((double) ABS(startX-curX))/((double) ABS(startY-curY));
new_win = NewWindow(progname, loX, loY, hiX, hiY, asp, 0);
if (new_win) {
numwin = 1;
} else {
numwin = 0;
}
} else {
numwin = 0;
}
break;
#if PDG
case Expose: /* 12 */
DRAWBOX;
XUngrabPointer(disp, CurrentTime);
stopFlag = 1;
numwin = 0;
if (theEvent.xexpose.count <= 0) {
XWindowAttributes win_attr;
XGetWindowAttributes(disp, theEvent.xany.window, &win_attr);
wi->dev_info.area_w = win_attr.width;
wi->dev_info.area_h = win_attr.height;
init_X(wi->dev_info.user_state);
DrawWindow(wi);
}
break;
#endif
default:
printf("unknown event: %d\n", theEvent.xany.type);
break;
}
}
return numwin;
}
void InitSets()
/*
* Initializes the data sets with default information. Sets up
* original values for parameters in parameters package.
*/
{
int idx;
char buf[1024];
/*
* Used to do all kinds of searching through visuals, etc.
* Got complaints -- so back to the simple version.
*/
vis = DefaultVisual(disp, DefaultScreen(disp));
cmap = DefaultColormap(disp, DefaultScreen(disp));
screen = DefaultScreen(disp);
depth = DefaultDepth(disp, DefaultScreen(disp));
param_init(disp, cmap);
param_set("Debug", BOOL, "false");
param_set("Geometry", STR, DEF_GEOMETRY);
param_set("ReverseVideo", BOOL, DEF_REVERSE);
param_set("BorderSize", INT, DEF_BORDER_WIDTH);
param_set("TitleText", STR, DEF_TITLE_TEXT);
param_set("XUnitText", STR, DEF_XUNIT_TEXT);
param_set("YUnitText", STR, DEF_YUNIT_TEXT); /* YUnits */
param_set("Ticks", BOOL, DEF_TICK_FLAG);
param_set("Markers", BOOL, DEF_MARKER_FLAG); /* markFlag (-m) */
param_set("StyleMarkers", BOOL, DEF_DIFFMARK_FLAG); /* colorMark (-M) */
param_set("PixelMarkers", BOOL, DEF_PIXMARK_FLAG); /* pixelMarks (-p) */
param_set("LargePixels", BOOL, DEF_LARGEPIX_FLAG); /* bigPixel (-P) */
param_set("BoundBox", BOOL, DEF_BB_FLAG);
param_set("NoLines", BOOL, DEF_NOLINE_FLAG);
param_set("LogX", BOOL, DEF_LOGX_FLAG);
param_set("LogY", BOOL, DEF_LOGY_FLAG); /* logYFlag */
param_set("BarGraph", BOOL, DEF_BAR_FLAG);
param_set("BarBase", DBL, DEF_BAR_BASE);
param_set("BarWidth", DBL, DEF_BAR_WIDTH);
param_set("LineWidth", INT, DEF_LINE_WIDTH);
param_set("GridSize", INT, DEF_GRID_SIZE);
param_set("GridStyle", STYLE, DEF_GRID_STYLE);
param_set("Device", STR, DEF_DEVICE);
param_set("Disposition", STR, DEF_DISPOSITION);
param_set("FileOrDev", STR, DEF_FILEORDEV);
/* Set the user bounding box */
param_set("XLowLimit", DBL, DEF_LOW_LIMIT);
param_set("YLowLimit", DBL, DEF_LOW_LIMIT);
param_set("XHighLimit", DBL, DEF_HIGH_LIMIT);
param_set("YHighLimit", DBL, DEF_HIGH_LIMIT);
/* Depends critically on whether the display has color */
if (depth < 4) {
/* Its black and white */
param_set("Background", PIXEL, DEF_BW_BACKGROUND);
param_set("Border", PIXEL, DEF_BW_BORDER);
param_set("ZeroColor", PIXEL, DEF_BW_ZEROCOLOR);
param_set("ZeroWidth", INT, DEF_BW_ZEROWIDTH);
param_set("ZeroStyle", STYLE, DEF_BW_ZEROSTYLE);
param_set("Foreground", PIXEL, DEF_BW_FOREGROUND);
/* Initialize set defaults */
for (idx = 0; idx < MAXATTR; idx++) {
(void) sprintf(buf, "%d.Style", idx);
param_set(buf, STYLE, defStyle[idx]);
(void) sprintf(buf, "%d.Color", idx);
param_set(buf, PIXEL, DEF_BW_FOREGROUND);
}
} else {
/* Its color */
param_set("Background", PIXEL, DEF_COL_BACKGROUND);
param_set("Border", PIXEL, DEF_COL_BORDER);
param_set("ZeroColor", PIXEL, DEF_COL_ZEROCOLOR);
param_set("ZeroWidth", INT, DEF_COL_ZEROWIDTH);
param_set("ZeroStyle", STYLE, DEF_COL_ZEROSTYLE);
param_set("Foreground", PIXEL, DEF_COL_FOREGROUND);
/* Initalize attribute colors defaults */
for (idx = 0; idx < MAXATTR; idx++) {
(void) sprintf(buf, "%d.Style", idx);
param_set(buf, STYLE, defStyle[idx]);
(void) sprintf(buf, "%d.Color", idx);
param_set(buf, PIXEL, defColors[idx]);
}
}
param_set("LabelFont", FONT, DEF_LABEL_FONT);
param_set("TitleFont", FONT, DEF_TITLE_FONT);
/* Initialize the data sets */
for (idx = 0; idx < MAXSETS; idx++) {
(void) sprintf(buf, "Set %d", idx);
PlotData[idx].setName = STRDUP(buf);
PlotData[idx].list = (PointList *) 0;
}
}
static char *def_str;
#define DEF(name, type) \
if ((def_str = XGetDefault(disp, Prog_Name, (name)))) { \
param_set(name, (type), def_str); \
}
void ReadDefaults()
/*
* Reads X default values which override the hard-coded defaults
* set up by InitSets.
*/
{
char newname[100];
int idx;
DEF("Debug", BOOL);
DEF("Geometry", STR);
DEF("Background", PIXEL);
DEF("BorderSize", INT);
DEF("Border", PIXEL);
DEF("GridSize", INT);
DEF("GridStyle", STYLE);
DEF("Foreground", PIXEL);
DEF("ZeroColor", PIXEL);
DEF("ZeroStyle", STYLE);
DEF("ZeroWidth", INT);
DEF("LabelFont", FONT);
DEF("TitleFont", FONT);
DEF("Ticks", BOOL);
DEF("Device", STR);
DEF("Disposition", STR);
DEF("FileOrDev", STR);
DEF("PixelMarkers", BOOL);
DEF("LargePixels", BOOL);
DEF("Markers", BOOL);
DEF("StyleMarkers", BOOL);
DEF("BoundBox", BOOL);
DEF("NoLines", BOOL);
DEF("LineWidth", INT);
/* Read device specific parameters */
for (idx = 0; idx < hard_count; idx++) {
sprintf(newname, "%s.Dimension", hard_devices[idx].dev_name);
DEF(newname, DBL); /* hard_devices[idx].dev_max_dim */
sprintf(newname, "%s.OutputTitleFont", hard_devices[idx].dev_name);
DEF(newname, STR); /* hard_devices[idx].dev_title_font */
sprintf(newname, "%s.OutputTitleSize", hard_devices[idx].dev_name);
DEF(newname, DBL); /* hard_devices[idx].dev_title_size */
sprintf(newname, "%s.OutputAxisFont", hard_devices[idx].dev_name);
DEF(newname, STR); /* hard_devices[idx].dev_axis_font */
sprintf(newname, "%s.OutputAxisSize", hard_devices[idx].dev_name);
DEF(newname, DBL); /* hard_devices[idx].dev_axis_size */
}
/* Read the default line and color attributes */
for (idx = 0; idx < MAXATTR; idx++) {
(void) sprintf(newname, "%d.Style", idx);
DEF(newname, STYLE); /* AllAttrs[idx].lineStyleLen */
(void) sprintf(newname, "%d.Color", idx);
DEF(newname, PIXEL); /* AllAttrs[idx].pixelValue */
}
DEF("ReverseVideo", BOOL);
}
#define FS(str) (void) fprintf(stderr, str)
int argerror(err, val)
char *err, *val;
{
(void) fprintf(stderr, "Error: %s: %s\n\n", val, err);
FS("format: xgraph [-<digit> set_name] [-bar] [-bb] [-bd border_color]\n");
FS(" [-bg background_color] [-brb bar_base] [-brw bar_width]\n");
FS(" [-bw bdr_width] [-db] [-fg foreground_color] [-gw grid_size]\n");
FS(" [-gs grid_style] [-lf label_font] [-lnx] [-lny] [-lw line_width]\n");
FS(" [-lx x1,x2] [-ly y1,y2] [-m] [-M] [-nl] [-p] [-P] [-rv]\n");
FS(" [-t title] [-tf title_font] [-tk] [-x x_unit_name]\n");
FS(" [-y y_unit_name] [-zg zero_color] [-zw zero_size]\n");
FS(" [=WxH+X+Y] [-display <host>:<disp>.<screen>] file...\n\n");
FS("-bar Draw bar graph with base -brb and width -brw\n");
FS("-bb Draw bounding box around data\n");
FS("-db Turn on debugging\n");
FS("-lnx Logarithmic scale for X axis\n");
FS("-lny Logarithmic scale for Y axis\n");
FS("-m -M Mark points distinctively (M varies with color)\n");
FS("-nl Don't draw lines (scatter plot)\n");
FS("-p -P Mark points with dot (P means big dot)\n");
FS("-rv Reverse video on black and white displays\n");
FS("-tk Draw tick marks instead of full grid\n");
exit(1);
}
#define ARG(opt, name) \
if (strcmp(argv[idx], opt) == 0) { \
if (do_it) param_set(name, BOOL, "on"); \
idx++; continue; \
}
#define ARG2(opt, name, type, missing) \
if (strcmp(argv[idx], opt) == 0) { \
if (idx+1 >= argc) argerror(missing, argv[idx]); \
if (do_it) param_set(name, type, argv[idx+1]); \
idx += 2; continue;\
}
#define MAXLO 30
void ParseArgs(argc, argv, do_it)
int argc;
char *argv[];
int do_it;
/*
* This routine parses the argument list for xgraph. There are too
* many to mention here so I won't. If `do_it' is non-zero, options
* are actually changed. If `do_it' is zero, the argument list
* is parsed but the options aren't set. The routine is called
* once to obtain the input files then again after the data is
* read to set the options.
*/
{
int idx, set;
char *hi;
idx = 1;
while (idx < argc) {
if (argv[idx][0] == '-') {
/* Check to see if its a data set name */
if (sscanf(argv[idx], "-%d", &set) == 1) {
/* The next string is a set name */
if (idx+1 >= argc) argerror("missing set name", argv[idx]);
if (do_it) {
PlotData[set].setName = argv[idx+1];
}
idx += 2;
} else {
/* Some non-dataset option */
ARG2("-x", "XUnitText", STR, "missing axis name");
ARG2("-y", "YUnitText", STR, "missing axis name");
ARG2("-t", "TitleText", STR, "missing plot title");
ARG2("-fg", "Foreground", PIXEL, "missing color name");
ARG2("-bg", "Background", PIXEL, "missing color name");
ARG2("-bd", "Border", PIXEL, "missing color name");
ARG2("-bw", "BorderSize", INT, "missing border size");
ARG2("-zg", "ZeroColor", PIXEL, "missing color name");
ARG2("-zw", "ZeroWidth", INT, "missing width");
ARG2("-tf", "TitleFont", FONT, "missing font name");
ARG2("-lf", "LabelFont", FONT, "missing font name");
ARG("-rv", "ReverseVideo");
ARG("-tk", "Ticks");
ARG("-bb", "BoundBox");
if (strcmp(argv[idx], "-lx") == 0) {
/* Limit the X coordinates */
if (idx+1 >= argc) argerror("missing coordinate(s)",
argv[idx]);
if ((hi = index(argv[idx+1], ','))) {
char low[MAXLO];
(void) strncpy(low, argv[idx+1], hi-argv[idx+1]);
low[hi-argv[idx+1]] = '\0';
hi++;
if (do_it) {
param_set("XLowLimit", DBL, argv[idx+1]);
param_set("XHighLimit", DBL, hi);
}
} else {
argerror("limit coordinates not specified right",
argv[idx]);
}
idx += 2;
continue;
}
if (strcmp(argv[idx], "-ly") == 0) {
/* Limit the Y coordinates */
if (idx+1 >= argc) argerror("missing coordinate(s)",
argv[idx]);
if ((hi = index(argv[idx+1], ','))) {
char low[MAXLO];
(void) strncpy(low, argv[idx+1], hi-argv[idx+1]);
low[hi-argv[idx+1]] = '\0';
hi++;
if (do_it) {
param_set("YLowLimit", DBL, argv[idx+1]);
param_set("YHighLimit", DBL, hi);
}
} else {
argerror("limit coordinates not specified right",
argv[idx]);
}
idx += 2;
continue;
}
ARG2("-lw", "LineWidth", INT, "missing line width");
ARG("-nl", "NoLines");
ARG("-m", "Markers");
ARG("-M", "StyleMarkers");
ARG("-p", "PixelMarkers");
ARG("-P", "LargePixels");
ARG("-lnx", "LogX");
ARG("-lny", "LogY");
ARG("-bar", "BarGraph");
ARG2("-brw", "BarWidth", DBL, "missing width");
ARG2("-brb", "BarBase", DBL, "missing base");
ARG("-db", "Debug");
ARG2("-gw", "GridSize", INT, "missing grid size");
ARG2("-gs", "GridStyle", STYLE, "missing grid style");
if (strcmp(argv[idx], "-display") == 0) {
/* Harmless display specification */
idx += 2;
continue;
}
argerror("unknown option", argv[idx]);
}
} else if (argv[idx][0] == '=') {
/* Its a geometry specification */
if (do_it) param_set("Geometry", STR, argv[idx]+1);
idx++;
} else {
/* It might be the host:display string */
if (rindex(argv[idx], ':') == (char *) 0) {
/* Should be an input file */
inFileNames[numFiles] = argv[idx];
numFiles++;
}
idx++;
}
}
}
/*
* New dataset reading code
*/
static int setNumber = 0;
static PointList **curSpot = (PointList **) 0;
static PointList *curList = (PointList *) 0;
static int newGroup = 0;
static int redundant_set = 0;
static int rdSet(fn)
char *fn; /* Reading from file `fn' */
/*
* Set up new dataset. Will return zero if there are too many data sets.
*/
{
char setname[100];
if (!redundant_set) {
if (setNumber < MAXSETS) {
(void) sprintf(setname, "Set %d", setNumber);
if ((strcmp(PlotData[setNumber].setName, setname) == 0) && fn) {
PlotData[setNumber].setName = fn;
}
curSpot = &(PlotData[setNumber].list);
PlotData[setNumber].list = (PointList *) 0;
newGroup = 1;
setNumber++;
redundant_set = 1;
return 1;
} else {
return 0;
}
} else {
return 1;
}
}
static void rdSetName(name)
char *name; /* New set name */
/*
* Sets the name of a data set. Automatically makes a copy.
*/
{
PlotData[setNumber-1].setName = STRDUP(name);
}
static void rdGroup()
/*
* Set up for reading new group of points within a dataset.
*/
{
newGroup = 1;
}
static void rdPoint(xval, yval)
double xval, yval; /* New point */
/*
* Adds a new point to the current group of the current
* data set.
*/
{
if (newGroup) {
*curSpot = (PointList *) malloc(sizeof(PointList));
curList = *curSpot;
curSpot = &(curList->next);
curList->numPoints = 0;
curList->allocSize = INITSIZE;
curList->xvec = (double *) malloc((unsigned)(INITSIZE * sizeof(double)));
curList->yvec = (double *) malloc((unsigned)(INITSIZE * sizeof(double)));
curList->next = (PointList *) 0;
newGroup = 0;
}
if (curList->numPoints >= curList->allocSize) {
curList->allocSize *= 2;
curList->xvec = (double *) realloc((char *) curList->xvec,
(unsigned) (curList->allocSize *
sizeof(double)));
curList->yvec = (double *) realloc((char *) curList->yvec,
(unsigned) (curList->allocSize *
sizeof(double)));
}
curList->xvec[curList->numPoints] = xval;
curList->yvec[curList->numPoints] = yval;
(curList->numPoints)++;
redundant_set = 0;
}
static int rdFindMax()
/*
* Returns the maximum number of items in any one group of any
* data set.
*/
{
int i;
PointList *list;
int max = -1;
for (i = 0; i < setNumber; i++) {
for (list = PlotData[i].list; list; list = list->next) {
if (list->numPoints > max) max = list->numPoints;
}
}
return max;
}
typedef enum line_type {
EMPTY, COMMENT, SETNAME, DRAWPNT, MOVEPNT, SETPARAM, ERROR
} LineType;
typedef struct point_defn {
double xval, yval;
} Point;
typedef struct parmval_defn {
char *name, *value;
} ParmVals;
typedef struct line_info {
LineType type;
union val_defn {
char *str; /* SETNAME, ERROR */
Point pnt; /* DRAWPNT, MOVEPNT */
ParmVals parm; /* SETPARAM */
} val;
} LineInfo;
static LineType parse_line(line, result)
char *line; /* Line to parse */
LineInfo *result; /* Returned result */
/*
* Parses `line' into one of the types given in the definition
* of LineInfo. The appropriate values are filled into `result'.
* Below are the current formats for each type:
* EMPTY: All white space
* COMMENT: Starts with "#"
* SETNAME: A name enclosed in double quotes
* DRAWPNT: Two numbers optionally preceded by keyword "draw"
* MOVEPNT: Two numbers preceded by keyword "move"
* SETPARAM: Two non-null strings separated by ":"
* ERROR: Not any of the above (an error message is returned)
* Note that often the values are pointers into the line itself
* and should be copied if they are to be used over a long period.
*/
{
char *first;
/* Find first non-space character */
while (*line && isspace(*line)) line++;
if (*line) {
if (*line == '#') {
/* comment */
result->type = COMMENT;
} else if (*line == '"') {
/* setname */
result->type = SETNAME;
line++;
result->val.str = line;
while (*line && (*line != '\n') && (*line != '"')) line++;
if (*line) *line = '\0';
} else {
first = line;
while (*line && !isspace(*line)) line++;
if (*line) {
*line = '\0';
if (strcasecmp(first, "move") == 0) {
/* MOVEPNT */
if (sscanf(line+1, "%lf %lf",
&result->val.pnt.xval,
&result->val.pnt.yval) == 2) {
result->type = MOVEPNT;
} else {
result->type = ERROR;
result->val.str = "Cannot read move coordinates";
}
} else if (strcasecmp(first, "draw") == 0) {
/* DRAWPNT */
if (sscanf(line+1, "%lf %lf",
&result->val.pnt.xval,
&result->val.pnt.yval) == 2) {
result->type = DRAWPNT;
} else {
result->type = ERROR;
result->val.str = "Cannot read draw coordinates";
}
} else if (first[strlen(first)-1] == ':') {
/* SETPARAM */
first[strlen(first)-1] = '\0';
result->val.parm.name = first;
line++;
while (*line && isspace(*line)) line++;
/* may be a \n at end of it */
if (line[strlen(line)-1] == '\n') {
line[strlen(line)-1] = '\0';
}
result->val.parm.value = line;
result->type = SETPARAM;
} else if (sscanf(first, "%lf", &result->val.pnt.xval) == 1) {
/* DRAWPNT */
if (sscanf(line+1, "%lf", &result->val.pnt.yval) == 1) {
result->type = DRAWPNT;
} else {
result->type = ERROR;
result->val.str = "Cannot read second coordinate";
}
} else {
/* ERROR */
result->type = ERROR;
result->val.str = "Unknown line type";
}
} else {
/* ERROR */
result->type = ERROR;
result->val.str = "Premature end of line";
}
}
} else {
/* empty */
result->type = EMPTY;
}
return result->type;
}
int ReadData(stream, filename)
FILE *stream;
char *filename;
/*
* Reads in the data sets from the supplied stream. If the format
* is correct, it returns the current maximum number of points across
* all data sets. If there is an error, it returns -1.
*/
{
char buffer[MAXBUFSIZE];
LineInfo info;
int line_count = 0;
int errors = 0;
if (!rdSet(filename)) {
(void) fprintf(stderr, "Error in file `%s' at line %d:\n %s\n",
filename, line_count,
"Too many data sets - extra data ignored");
return -1;
}
while (fgets(buffer, MAXBUFSIZE, stream)) {
line_count++;
switch (parse_line(buffer, &info)) {
case EMPTY:
if (!rdSet(filename)) {
(void) fprintf(stderr, "Error in file `%s' at line %d:\n %s\n",
filename, line_count,
"Too many data sets - extra data ignored");
return -1;
}
break;
case COMMENT:
/* nothing */
break;
case SETNAME:
rdSetName(info.val.str);
break;
case DRAWPNT:
rdPoint(info.val.pnt.xval, info.val.pnt.yval);
break;
case MOVEPNT:
rdGroup();
rdPoint(info.val.pnt.xval, info.val.pnt.yval);
break;
case SETPARAM:
param_reset(info.val.parm.name, info.val.parm.value);
break;
default:
if (filename) {
(void) fprintf(stderr, "Error in file `%s' at line %d:\n %s\n",
filename, line_count, info.val.str);
errors++;
}
break;
}
}
if (errors) return -1; else return rdFindMax();
}
void DrawWindow(win_info)
LocalWin *win_info; /* Window information */
/*
* Draws the data in the window. Does not clear the window.
* The data is scaled so that all of the data will fit.
* Grid lines are drawn at the nearest power of 10 in engineering
* notation. Draws axis numbers along bottom and left hand edges.
* Centers title at top of window.
*/
{
/* Figure out the transformation constants */
if (TransformCompute(win_info)) {
/* Draw the title */
DrawTitle(win_info);
/* Draw the legend */
DrawLegend(win_info);
/* Draw the axis unit labels, grid lines, and grid labels */
DrawGridAndAxis(win_info);
/* Draw the data sets themselves */
DrawData(win_info);
}
}
void DrawTitle(wi)
LocalWin *wi; /* Window information */
/*
* This routine draws the title of the graph centered in
* the window. It is spaced down from the top by an amount
* specified by the constant PADDING. The font must be
* fixed width. The routine returns the height of the
* title in pixels.
*/
{
wi->dev_info.xg_text(wi->dev_info.user_state,
wi->dev_info.area_w/2,
wi->dev_info.axis_pad,
PM_STR("TitleText"), T_TOP, T_TITLE);
}
int TransformCompute(wi)
LocalWin *wi; /* Window information */
/*
* This routine figures out how to draw the axis labels and grid lines.
* Both linear and logarithmic axes are supported. Axis labels are
* drawn in engineering notation. The power of the axes are labeled
* in the normal axis labeling spots. The routine also figures
* out the necessary transformation information for the display
* of the points (it touches XOrgX, XOrgY, UsrOrgX, UsrOrgY, and
* UnitsPerPixel).
*/
{
double bbCenX, bbCenY, bbHalfWidth, bbHalfHeight;
int idx, maxName, leftWidth;
char err[MAXBUFSIZE];
char *XUnitText = PM_STR("XUnitText");
/*
* First, we figure out the origin in the X window. Above
* the space we have the title and the Y axis unit label.
* To the left of the space we have the Y axis grid labels.
*/
wi->XOrgX = wi->dev_info.bdr_pad + (7 * wi->dev_info.axis_width)
+ wi->dev_info.bdr_pad;
wi->XOrgY = wi->dev_info.bdr_pad + wi->dev_info.title_height
+ wi->dev_info.bdr_pad + wi->dev_info.axis_height
+ wi->dev_info.axis_height/2 + wi->dev_info.bdr_pad;
/*
* Now we find the lower right corner. Below the space we
* have the X axis grid labels. To the right of the space we
* have the X axis unit label and the legend. We assume the
* worst case size for the unit label.
*/
maxName = 0;
for (idx = 0; idx < MAXSETS; idx++) {
if (PlotData[idx].list) {
int tempSize;
tempSize = strlen(PlotData[idx].setName);
if (tempSize > maxName) maxName = tempSize;
}
}
/* Worst case size of the X axis label: */
leftWidth = (strlen(XUnitText)) * wi->dev_info.axis_width;
if ((maxName*wi->dev_info.axis_width)+wi->dev_info.bdr_pad > leftWidth)
leftWidth = maxName * wi->dev_info.axis_width + wi->dev_info.bdr_pad;
wi->XOppX = wi->dev_info.area_w - wi->dev_info.bdr_pad - leftWidth;
wi->XOppY = wi->dev_info.area_h - wi->dev_info.bdr_pad
- wi->dev_info.axis_height - wi->dev_info.bdr_pad;
if ((wi->XOrgX >= wi->XOppX) || (wi->XOrgY >= wi->XOppY)) {
do_error(strcpy(err, "Drawing area is too small\n"));
return 0;
}
/*
* We now have a bounding box for the drawing region.
* Figure out the units per pixel using the data set bounding box.
*/
wi->XUnitsPerPixel = (wi->hiX - wi->loX)/((double) (wi->XOppX - wi->XOrgX));
wi->YUnitsPerPixel = (wi->hiY - wi->loY)/((double) (wi->XOppY - wi->XOrgY));
/*
* Find origin in user coordinate space. We keep the center of
* the original bounding box in the same place.
*/
bbCenX = (wi->loX + wi->hiX) / 2.0;
bbCenY = (wi->loY + wi->hiY) / 2.0;
bbHalfWidth = ((double) (wi->XOppX - wi->XOrgX))/2.0 * wi->XUnitsPerPixel;
bbHalfHeight = ((double) (wi->XOppY - wi->XOrgY))/2.0 * wi->YUnitsPerPixel;
wi->UsrOrgX = bbCenX - bbHalfWidth;
wi->UsrOrgY = bbCenY - bbHalfHeight;
wi->UsrOppX = bbCenX + bbHalfWidth;
wi->UsrOppY = bbCenY + bbHalfHeight;
/*
* Everything is defined so we can now use the SCREENX and SCREENY
* transformations.
*/
return 1;
}
void DrawGridAndAxis(wi)
LocalWin *wi; /* Window information */
/*
* This routine draws grid line labels in engineering notation,
* the grid lines themselves, and unit labels on the axes.
*/
{
int expX, expY; /* Engineering powers */
int startX;
int Yspot, Xspot;
#if !PDG
char power[10], value[10], final[MAXBUFSIZE+10];
#else
char power[10], value[40], final[MAXBUFSIZE+10];
int Xdigits, Ydigits;
#endif
double Xincr, Yincr, Xstart, Ystart, Yindex, Xindex, larger;
XSegment segs[2];
double initGrid(), stepGrid();
int tickFlag = PM_BOOL("Ticks");
int logXFlag = PM_BOOL("LogX");
int logYFlag = PM_BOOL("LogY");
char *XUnitText = PM_STR("XUnitText");
char *YUnitText = PM_STR("YUnitText");
/*
* Grid display powers are computed by taking the log of
* the largest numbers and rounding down to the nearest
* multiple of 3.
*/
if (logXFlag) {
expX = 0;
} else {
if (fabs(wi->UsrOrgX) > fabs(wi->UsrOppX)) {
larger = fabs(wi->UsrOrgX);
} else {
larger = fabs(wi->UsrOppX);
}
expX = ((int) floor(nlog10(larger)/3.0)) * 3;
}
if (logYFlag) {
expY = 0;
} else {
if (fabs(wi->UsrOrgY) > fabs(wi->UsrOppY)) {
larger = fabs(wi->UsrOrgY);
} else {
larger = fabs(wi->UsrOppY);
}
expY = ((int) floor(nlog10(larger)/3.0)) * 3;
}
/*
* With the powers computed, we can draw the axis labels.
*/
if (expY != 0) {
(void) strcpy(final, YUnitText);
(void) strcat(final, " x 10");
Xspot = wi->dev_info.bdr_pad +
((strlen(YUnitText)+5) * wi->dev_info.axis_width);
Yspot = wi->dev_info.bdr_pad * 2 + wi->dev_info.title_height +
wi->dev_info.axis_height/2;
wi->dev_info.xg_text(wi->dev_info.user_state,
Xspot, Yspot, final, T_RIGHT, T_AXIS);
(void) sprintf(power, "%d", expY);
wi->dev_info.xg_text(wi->dev_info.user_state,
Xspot, Yspot, power, T_LOWERLEFT, T_AXIS);
} else {
Yspot = wi->dev_info.bdr_pad * 2 + wi->dev_info.title_height;
wi->dev_info.xg_text(wi->dev_info.user_state,
wi->dev_info.bdr_pad, Yspot, YUnitText,
T_UPPERLEFT, T_AXIS);
}
startX = wi->dev_info.area_w - wi->dev_info.bdr_pad;
if (expX != 0) {
(void) sprintf(power, "%d", expX);
startX -= (strlen(power) * wi->dev_info.axis_width);
wi->dev_info.xg_text(wi->dev_info.user_state,
startX, wi->XOppY, power, T_LOWERLEFT, T_AXIS);
(void) strcpy(final, XUnitText);
(void) strcat(final, " x 10");
wi->dev_info.xg_text(wi->dev_info.user_state,
startX, wi->XOppY, final, T_RIGHT, T_AXIS);
} else {
wi->dev_info.xg_text(wi->dev_info.user_state,
startX, wi->XOppY, XUnitText, T_RIGHT, T_AXIS);
}
/*
* First, the grid line labels
*/
Yincr = (wi->dev_info.axis_pad + wi->dev_info.axis_height) * wi->YUnitsPerPixel;
#if PDG
Ydigits = getDigits(wi->UsrOrgY, wi->UsrOppY, Yincr, expY, logYFlag);
#endif
Ystart = initGrid(wi->UsrOrgY, Yincr, logYFlag);
for (Yindex = Ystart; Yindex < wi->UsrOppY; Yindex = stepGrid()) {
Yspot = SCREENY(wi, Yindex);
/* Write the axis label */
#if !PDG
WriteValue(value, Yindex, expY, logYFlag);
#else
WriteValue(value, Yindex, expY, logYFlag, Ydigits);
#endif
wi->dev_info.xg_text(wi->dev_info.user_state,
wi->dev_info.bdr_pad +
(7 * wi->dev_info.axis_width),
Yspot, value, T_RIGHT, T_AXIS);
}
Xincr = (wi->dev_info.axis_pad + (wi->dev_info.axis_width * 7)) * wi->XUnitsPerPixel;
#if PDG
Xdigits = getDigits(wi->UsrOrgX, wi->UsrOppX, Xincr, expX, logXFlag);
#endif
Xstart = initGrid(wi->UsrOrgX, Xincr, logXFlag);
for (Xindex = Xstart; Xindex < wi->UsrOppX; Xindex = stepGrid()) {
Xspot = SCREENX(wi, Xindex);
/* Write the axis label */
#if !PDG
WriteValue(value, Xindex, expX, logXFlag);
#else
WriteValue(value, Xindex, expX, logXFlag, Xdigits);
#endif
wi->dev_info.xg_text(wi->dev_info.user_state,
Xspot,
wi->dev_info.area_h - wi->dev_info.bdr_pad,
value, T_BOTTOM, T_AXIS);
}
/*
* Now, the grid lines or tick marks
*/
Yincr = (wi->dev_info.axis_pad + wi->dev_info.axis_height) * wi->YUnitsPerPixel;
Ystart = initGrid(wi->UsrOrgY, Yincr, logYFlag);
for (Yindex = Ystart; Yindex < wi->UsrOppY; Yindex = stepGrid()) {
Yspot = SCREENY(wi, Yindex);
/* Draw the grid line or tick mark */
if (tickFlag) {
segs[0].x1 = wi->XOrgX;
segs[0].x2 = wi->XOrgX + wi->dev_info.tick_len;
segs[1].x1 = wi->XOppX - wi->dev_info.tick_len;
segs[1].x2 = wi->XOppX;
segs[0].y1 = segs[0].y2 = segs[1].y1 = segs[1].y2 = Yspot;
} else {
segs[0].x1 = wi->XOrgX; segs[0].x2 = wi->XOppX;
segs[0].y1 = segs[0].y2 = Yspot;
}
if ((ABS(Yindex) < ZERO_THRES) && !logYFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, segs, PM_INT("ZeroWidth"),
L_ZERO, 0, 0);
if (tickFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &(segs[1]), PM_INT("ZeroWidth"),
L_ZERO, 0, 0);
}
} else {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, segs, PM_INT("GridSize"),
L_AXIS, 0, 0);
if (tickFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &(segs[1]), PM_INT("GridSize"),
L_AXIS, 0, 0);
}
}
}
Xincr = (wi->dev_info.axis_pad + (wi->dev_info.axis_width * 7)) * wi->XUnitsPerPixel;
Xstart = initGrid(wi->UsrOrgX, Xincr, logXFlag);
for (Xindex = Xstart; Xindex < wi->UsrOppX; Xindex = stepGrid()) {
Xspot = SCREENX(wi, Xindex);
/* Draw the grid line or tick marks */
if (tickFlag) {
segs[0].x1 = segs[0].x2 = segs[1].x1 = segs[1].x2 = Xspot;
segs[0].y1 = wi->XOrgY;
segs[0].y2 = wi->XOrgY + wi->dev_info.tick_len;
segs[1].y1 = wi->XOppY - wi->dev_info.tick_len;
segs[1].y2 = wi->XOppY;
} else {
segs[0].x1 = segs[0].x2 = Xspot;
segs[0].y1 = wi->XOrgY; segs[0].y2 = wi->XOppY;
}
if ((ABS(Xindex) < ZERO_THRES) && !logXFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, segs, PM_INT("ZeroWidth"), L_ZERO, 0, 0);
if (tickFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &(segs[1]), PM_INT("ZeroWidth"),
L_ZERO, 0, 0);
}
} else {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, segs, PM_INT("GridSize"), L_AXIS, 0, 0);
if (tickFlag) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &(segs[1]), PM_INT("GridSize"), L_AXIS, 0, 0);
}
}
}
/* Check to see if he wants a bounding box */
if (PM_BOOL("BoundBox")) {
XSegment bb[4];
/* Draw bounding box */
bb[0].x1 = bb[0].x2 = bb[1].x1 = bb[3].x2 = wi->XOrgX;
bb[0].y1 = bb[2].y2 = bb[3].y1 = bb[3].y2 = wi->XOrgY;
bb[1].x2 = bb[2].x1 = bb[2].x2 = bb[3].x1 = wi->XOppX;
bb[0].y2 = bb[1].y1 = bb[1].y2 = bb[2].y1 = wi->XOppY;
wi->dev_info.xg_seg(wi->dev_info.user_state,
4, bb, PM_INT("GridSize"), L_AXIS, 0, 0);
}
}
static double gridBase, gridStep, gridJuke[101];
static int gridNJuke, gridCurJuke;
#define ADD_GRID(val) (gridJuke[gridNJuke++] = log10(val))
double initGrid(low, step, logFlag)
double low; /* desired low value */
double step; /* desired step (user coords) */
int logFlag; /* is axis logarithmic? */
{
double ratio, x;
double RoundUp(), stepGrid();
gridNJuke = gridCurJuke = 0;
gridJuke[gridNJuke++] = 0.0;
if (logFlag) {
ratio = pow(10.0, step);
gridBase = floor(low);
gridStep = ceil(step);
if (ratio <= 3.0) {
if (ratio > 2.0) {
ADD_GRID(3.0);
} else if (ratio > 1.333) {
ADD_GRID(2.0); ADD_GRID(5.0);
} else if (ratio > 1.25) {
ADD_GRID(1.5); ADD_GRID(2.0); ADD_GRID(3.0);
ADD_GRID(5.0); ADD_GRID(7.0);
} else {
for (x = 1.0; x < 10.0 && (x+.5)/(x+.4) >= ratio; x += .5) {
ADD_GRID(x + .1); ADD_GRID(x + .2);
ADD_GRID(x + .3); ADD_GRID(x + .4);
ADD_GRID(x + .5);
}
if (floor(x) != x) ADD_GRID(x += .5);
for ( ; x < 10.0 && (x+1.0)/(x+.5) >= ratio; x += 1.0) {
ADD_GRID(x + .5); ADD_GRID(x + 1.0);
}
for ( ; x < 10.0 && (x+1.0)/x >= ratio; x += 1.0) {
ADD_GRID(x + 1.0);
}
if (x == 7.0) {
gridNJuke--;
x = 6.0;
}
if (x < 7.0) {
ADD_GRID(x + 2.0);
}
if (x == 10.0) gridNJuke--;
}
x = low - gridBase;
for (gridCurJuke = -1; x >= gridJuke[gridCurJuke+1]; gridCurJuke++){
}
}
} else {
gridStep = RoundUp(step);
gridBase = floor(low / gridStep) * gridStep;
}
return(stepGrid());
}
double stepGrid()
{
if (++gridCurJuke >= gridNJuke) {
gridCurJuke = 0;
gridBase += gridStep;
}
return(gridBase + gridJuke[gridCurJuke]);
}
double RoundUp(val)
double val; /* Value */
/*
* This routine rounds up the given positive number such that
* it is some power of ten times either 1, 2, or 5. It is
* used to find increments for grid lines.
*/
{
int exponent, idx;
exponent = (int) floor(nlog10(val));
if (exponent < 0) {
for (idx = exponent; idx < 0; idx++) {
val *= 10.0;
}
} else {
for (idx = 0; idx < exponent; idx++) {
val /= 10.0;
}
}
if (val > 5.0) val = 10.0;
else if (val > 2.0) val = 5.0;
else if (val > 1.0) val = 2.0;
else val = 1.0;
if (exponent < 0) {
for (idx = exponent; idx < 0; idx++) {
val /= 10.0;
}
} else {
for (idx = 0; idx < exponent; idx++) {
val *= 10.0;
}
}
return val;
}
#if PDG
int getDigits(low, high, step, expv, logFlag)
double low; /* desired low value */
double high; /* desired high value */
double step; /* desired step (user coords) */
int expv; /* Exponent */
int logFlag; /* is axis logarithmic? */
{
int digits;
for (digits = 2; digits < 30; digits++)
{
double start, index;
char str1[40], str2[40];
start = initGrid(low, step, logFlag);
WriteValue(str1, start, expv, logFlag, digits);
for (index = stepGrid(); index < high; index = stepGrid()) {
WriteValue(str2, index, expv, logFlag, digits);
if (!strcmp(str1, str2)) goto next_digits;
strcpy(str1, str2);
}
/* All adjacent values compare unequal */
return digits;
next_digits: ;
}
/* We've reached the limit on number of digits */
return digits;
}
#endif
void WriteValue(str, val, expv, logFlag
#if PDG
, digits
#endif
)
char *str; /* String to write into */
double val; /* Value to print */
int expv; /* Exponent */
int logFlag; /* Is this a log axis? */
#if PDG
int digits; /* digits after decimal */
#endif
/*
* Writes the value provided into the string in a fixed format
* consisting of seven characters. The format is:
* -ddd.dd
*/
{
int idx;
if (logFlag) {
if (val == floor(val)) {
(void) sprintf(str, "%.0e", pow(10.0, val));
} else {
#if !PDG
(void) sprintf(str, "%.2g", pow(10.0, val - floor(val)));
#else
(void) sprintf(str, "%.*g", digits, pow(10.0, val - floor(val)));
#endif
}
} else {
if (expv < 0) {
for (idx = expv; idx < 0; idx++) {
val *= 10.0;
}
} else {
for (idx = 0; idx < expv; idx++) {
val /= 10.0;
}
}
#if !PDG
(void) sprintf(str, "%.2f", val);
#else
(void) sprintf(str, "%.*f", digits, val);
#endif
}
}
#define LEFT_CODE 0x01
#define RIGHT_CODE 0x02
#define BOTTOM_CODE 0x04
#define TOP_CODE 0x08
/* Clipping algorithm from Neumann and Sproull by Cohen and Sutherland */
#define C_CODE(xval, yval, rtn) \
rtn = 0; \
if ((xval) < wi->UsrOrgX) rtn = LEFT_CODE; \
else if ((xval) > wi->UsrOppX) rtn = RIGHT_CODE; \
if ((yval) < wi->UsrOrgY) rtn |= BOTTOM_CODE; \
else if ((yval) > wi->UsrOppY) rtn |= TOP_CODE
void DrawData(wi)
LocalWin *wi;
/*
* This routine draws the data sets themselves using the macros
* for translating coordinates.
*/
{
double sx1, sy1, sx2, sy2, tx, ty;
int idx, subindex;
int code1, code2, cd, mark_inside;
int X_idx;
XSegment *ptr;
PointList *thisList;
int markFlag, pixelMarks, bigPixel, colorMark;
int noLines = PM_BOOL("NoLines");
int lineWidth = PM_INT("LineWidth");
set_mark_flags(&markFlag, &pixelMarks, &bigPixel, &colorMark);
for (idx = 0; idx < MAXSETS; idx++) {
thisList = PlotData[idx].list;
while (thisList) {
X_idx = 0;
for (subindex = 0; subindex < thisList->numPoints-1; subindex++) {
/* Put segment in (sx1,sy1) (sx2,sy2) */
sx1 = thisList->xvec[subindex];
sy1 = thisList->yvec[subindex];
sx2 = thisList->xvec[subindex+1];
sy2 = thisList->yvec[subindex+1];
/* Now clip to current window boundary */
C_CODE(sx1, sy1, code1);
C_CODE(sx2, sy2, code2);
mark_inside = (code1 == 0);
while (code1 || code2) {
if (code1 & code2) break;
cd = (code1 ? code1 : code2);
if (cd & LEFT_CODE) { /* Crosses left edge */
ty = sy1 + (sy2 - sy1) * (wi->UsrOrgX - sx1) / (sx2 - sx1);
tx = wi->UsrOrgX;
} else if (cd & RIGHT_CODE) { /* Crosses right edge */
ty = sy1 + (sy2 - sy1) * (wi->UsrOppX - sx1) / (sx2 - sx1);
tx = wi->UsrOppX;
} else if (cd & BOTTOM_CODE) { /* Crosses bottom edge */
tx = sx1 + (sx2 - sx1) * (wi->UsrOrgY - sy1) / (sy2 - sy1);
ty = wi->UsrOrgY;
} else if (cd & TOP_CODE) { /* Crosses top edge */
tx = sx1 + (sx2 - sx1) * (wi->UsrOppY - sy1) / (sy2 - sy1);
ty = wi->UsrOppY;
}
if (cd == code1) {
sx1 = tx; sy1 = ty;
C_CODE(sx1, sy1, code1);
} else {
sx2 = tx; sy2 = ty;
C_CODE(sx2, sy2, code2);
}
}
if (!code1 && !code2) {
/* Add segment to list */
Xsegs[X_idx].x1 = SCREENX(wi, sx1);
Xsegs[X_idx].y1 = SCREENY(wi, sy1);
Xsegs[X_idx].x2 = SCREENX(wi, sx2);
Xsegs[X_idx].y2 = SCREENY(wi, sy2);
X_idx++;
}
/* Draw markers if requested and they are in drawing region */
if (markFlag && mark_inside) {
if (pixelMarks) {
if (bigPixel) {
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x1, Xsegs[X_idx-1].y1,
P_DOT, 0, idx % MAXATTR);
} else {
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x1, Xsegs[X_idx-1].y1,
P_PIXEL, 0, PIXVALUE(idx));
}
} else {
/* Distinctive markers */
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x1, Xsegs[X_idx-1].y1,
P_MARK, MARKSTYLE(idx),
PIXVALUE(idx));
}
}
/* Draw bar elements if requested */
if (PM_BOOL("BarGraph")) {
int barPixels, baseSpot;
XSegment line;
barPixels = (int) ((PM_DBL("BarWidth")/wi->XUnitsPerPixel) + 0.5);
if (barPixels <= 0) barPixels = 1;
baseSpot = SCREENY(wi, PM_DBL("BarBase"));
line.x1 = line.x2 = Xsegs[X_idx-1].x1;
line.y1 = baseSpot; line.y2 = Xsegs[X_idx-1].y1;
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &line, barPixels, L_VAR,
LINESTYLE(idx), PIXVALUE(idx));
}
}
/* Handle last marker */
if (markFlag && (thisList->numPoints > 0)) {
C_CODE(thisList->xvec[thisList->numPoints-1],
thisList->yvec[thisList->numPoints-1],
mark_inside);
if (mark_inside == 0) {
#if !PDG
if (pixelMarks) {
if (bigPixel) {
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x2, Xsegs[X_idx-1].y2,
P_DOT, 0, idx % MAXATTR);
} else {
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x2, Xsegs[X_idx-1].y2,
P_PIXEL, 0, PIXVALUE(idx));
}
} else {
/* Distinctive markers */
wi->dev_info.xg_dot(wi->dev_info.user_state,
Xsegs[X_idx-1].x2, Xsegs[X_idx-1].y2,
P_MARK, MARKSTYLE(idx),
PIXVALUE(idx));
}
#else
/* Do it this way to handle a PointList of a single point */
XPoint d;
if (X_idx > 0) {
d.x = Xsegs[X_idx-1].x2; /* This was computed above */
d.y = Xsegs[X_idx-1].y2;
} else {
d.x = SCREENX(wi, thisList->xvec[thisList->numPoints-1]);
d.y = SCREENY(wi, thisList->yvec[thisList->numPoints-1]);
}
if (pixelMarks) {
if (bigPixel) {
wi->dev_info.xg_dot(wi->dev_info.user_state,
d.x, d.y,
P_DOT, 0, idx % MAXATTR);
} else {
wi->dev_info.xg_dot(wi->dev_info.user_state,
d.x, d.y,
P_PIXEL, 0, PIXVALUE(idx));
}
} else {
/* Distinctive markers */
wi->dev_info.xg_dot(wi->dev_info.user_state,
d.x, d.y,
P_MARK, MARKSTYLE(idx),
PIXVALUE(idx));
}
#endif
}
}
/* Handle last bar */
if ((thisList->numPoints > 0) && PM_BOOL("BarGraph")) {
int barPixels, baseSpot;
XSegment line;
barPixels = (int) ((PM_DBL("BarWidth")/wi->XUnitsPerPixel) + 0.5);
if (barPixels <= 0) barPixels = 1;
baseSpot = SCREENY(wi, PM_DBL("BarBase"));
line.x1 = line.x2 = Xsegs[X_idx-1].x2;
line.y1 = baseSpot; line.y2 = Xsegs[X_idx-1].y2;
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &line, barPixels, L_VAR,
LINESTYLE(idx), PIXVALUE(idx));
}
/* Draw segments */
if (thisList->numPoints > 0 && (!noLines) && (X_idx > 0)) {
ptr = Xsegs;
while (X_idx > wi->dev_info.max_segs) {
wi->dev_info.xg_seg(wi->dev_info.user_state,
wi->dev_info.max_segs, ptr,
lineWidth, L_VAR,
LINESTYLE(idx), PIXVALUE(idx));
ptr += wi->dev_info.max_segs;
X_idx -= wi->dev_info.max_segs;
}
wi->dev_info.xg_seg(wi->dev_info.user_state,
X_idx, ptr,
lineWidth, L_VAR,
LINESTYLE(idx), PIXVALUE(idx));
}
/* Next subset */
thisList = thisList->next;
}
}
}
void DrawLegend(wi)
LocalWin *wi;
/*
* This draws a legend of the data sets displayed. Only those that
* will fit are drawn.
*/
{
int idx, spot, lineLen, oneLen;
XSegment leg_line;
int markFlag, pixelMarks, bigPixel, colorMark;
set_mark_flags(&markFlag, &pixelMarks, &bigPixel, &colorMark);
spot = wi->XOrgY;
lineLen = 0;
/* First pass draws the text */
for (idx = 0; idx < MAXSETS; idx++) {
if ((PlotData[idx].list) &&
(spot + wi->dev_info.axis_height + 2 < wi->XOppY))
{
/* Meets the criteria */
oneLen = strlen(PlotData[idx].setName);
if (oneLen > lineLen) lineLen = oneLen;
wi->dev_info.xg_text(wi->dev_info.user_state,
wi->XOppX + wi->dev_info.bdr_pad,
spot+2,
PlotData[idx].setName,
T_UPPERLEFT, T_AXIS);
spot += 2 + wi->dev_info.axis_height + wi->dev_info.bdr_pad;
}
}
lineLen = lineLen * wi->dev_info.axis_width;
leg_line.x1 = wi->XOppX + wi->dev_info.bdr_pad;
leg_line.x2 = leg_line.x1 + lineLen;
spot = wi->XOrgY;
/* second pass draws the lines */
for (idx = 0; idx < MAXSETS; idx++) {
if ((PlotData[idx].list) &&
(spot + wi->dev_info.axis_height + 2 < wi->XOppY))
{
leg_line.y1 = leg_line.y2 = spot - wi->dev_info.legend_pad;
wi->dev_info.xg_seg(wi->dev_info.user_state,
1, &leg_line, 1, L_VAR,
LINESTYLE(idx), PIXVALUE(idx));
if (markFlag && !pixelMarks) {
wi->dev_info.xg_dot(wi->dev_info.user_state,
leg_line.x1, leg_line.y1,
P_MARK, MARKSTYLE(idx), PIXVALUE(idx));
}
spot += 2 + wi->dev_info.axis_height + wi->dev_info.bdr_pad;
}
}
}
static void set_mark_flags(markFlag, pixelMarks, bigPixel, colorMark)
int *markFlag;
int *pixelMarks;
int *bigPixel;
int *colorMark;
/*
* Determines the values of the old boolean flags based on the
* new values in the parameters database.
*/
{
*markFlag = 0; *pixelMarks = 0; *colorMark = 0; *bigPixel = 0;
if (PM_BOOL("Markers")) {
*markFlag = 1; *pixelMarks = 0; *colorMark = 0;
}
if (PM_BOOL("PixelMarkers")) {
*markFlag = 1; *pixelMarks = 1; *bigPixel = 0;
}
if (PM_BOOL("LargePixels")) {
*markFlag = 1; *pixelMarks = 1; *bigPixel = 1;
}
if (PM_BOOL("StyleMarkers")) {
*markFlag = 1; *pixelMarks = 0; *colorMark = 1;
}
}
#define RND(val) ((int) ((val) + 0.5))
/*ARGSUSED*/
void do_hardcopy(prog, info, init_fun, dev_spec, file_or_dev, maxdim,
ti_fam, ti_size, ax_fam, ax_size, doc_p)
char *prog; /* Program name for Xdefaults */
char *info; /* Some state information */
int (*init_fun)(); /* Hardcopy init function */
char *dev_spec; /* Device specification (if any) */
char *file_or_dev; /* Filename or device spec */
double maxdim; /* Maximum dimension in cm */
char *ti_fam, *ax_fam; /* Font family names */
double ti_size, ax_size; /* Font sizes in points */
int doc_p; /* Documentation predicate */
/*
* This routine resets the function pointers to those specified
* by `init_fun' and causes a screen redisplay. If `dev_spec'
* is non-zero, it will be considered a sprintf string with
* one %s which will be filled in with `file_or_dev' and fed
* to popen(3) to obtain a stream. Otherwise, `file_or_dev'
* is considered to be a file and is opened for writing. The
* resulting stream is fed to the initialization routine for
* the device.
*/
{
LocalWin *curWin = (LocalWin *) info;
LocalWin thisWin;
FILE *out_stream;
char buf[MAXBUFSIZE], err[MAXBUFSIZE], ierr[ERRBUFSIZE];
char tilde[MAXBUFSIZE*10];
int final_w, final_h, flags;
double ratio;
if (dev_spec) {
(void) sprintf(buf, dev_spec, file_or_dev);
out_stream = popen(buf, "w");
if (!out_stream) {
do_error(sprintf(err, "Unable to issue command:\n %s\n", buf));
return;
}
} else {
tildeExpand(tilde, file_or_dev);
out_stream = fopen(tilde, "w");
if (!out_stream) {
do_error(sprintf(err, "Unable to open file `%s'\n", tilde));
return;
}
}
thisWin = *curWin;
ratio = ((double) thisWin.dev_info.area_w) /
((double) thisWin.dev_info.area_h);
if (thisWin.dev_info.area_w > thisWin.dev_info.area_h) {
final_w = RND(maxdim * 10000.0);
final_h = RND(maxdim/ratio * 10000.0);
} else {
final_w = RND(maxdim * ratio * 10000.0);
final_h = RND(maxdim * 10000.0);
}
ierr[0] = '\0';
flags = 0;
if (doc_p) flags |= D_DOCU;
if ((*init_fun)(out_stream, final_w, final_h, ti_fam, ti_size,
ax_fam, ax_size, flags, &(thisWin.dev_info), ierr)) {
DrawWindow(&thisWin);
if (thisWin.dev_info.xg_end) {
thisWin.dev_info.xg_end(thisWin.dev_info.user_state);
}
} else {
do_error(ierr);
}
if (dev_spec) {
(void) pclose(out_stream);
} else {
(void) fclose(out_stream);
}
}
static char *tildeExpand(out, in)
char *out; /* Output space for expanded file name */
char *in; /* Filename with tilde */
/*
* This routine expands out a file name passed in `in' and places
* the expanded version in `out'. It returns `out'.
*/
{
char username[50], *userPntr;
struct passwd *userRecord;
out[0] = '\0';
/* Skip over the white space in the initial path */
while ((*in == ' ') || (*in == '\t')) in++;
/* Tilde? */
if (in[0] == TILDE) {
/* Copy user name into 'username' */
in++; userPntr = &(username[0]);
while ((*in != '\0') && (*in != '/')) {
*(userPntr++) = *(in++);
}
*(userPntr) = '\0';
/* See if we have to fill in the user name ourselves */
if (strlen(username) == 0) {
userRecord = getpwuid(getuid());
} else {
userRecord = getpwnam(username);
}
if (userRecord) {
/* Found user in passwd file. Concatenate user directory */
strcat(out, userRecord->pw_dir);
}
}
/* Concantenate remaining portion of file name */
strcat(out, in);
return out;
}
#define ERR_MSG_SIZE 2048
/*ARGSUSED*/
static int XErrHandler(disp_ptr, evt)
Display *disp_ptr;
XErrorEvent *evt;
/*
* Displays a nicely formatted message and core dumps.
*/
{
char err_buf[ERR_MSG_SIZE], mesg[ERR_MSG_SIZE], number[ERR_MSG_SIZE];
char *mtype = "XlibMessage";
XGetErrorText(disp_ptr, evt->error_code, err_buf, ERR_MSG_SIZE);
(void) fprintf(stderr, "X Error: %s\n", err_buf);
XGetErrorDatabaseText(disp_ptr, mtype, "MajorCode",
"Request Major code %d", mesg, ERR_MSG_SIZE);
(void) fprintf(stderr, mesg, evt->request_code);
(void) sprintf(number, "%d", evt->request_code);
XGetErrorDatabaseText(disp_ptr, "XRequest", number, "", err_buf,
ERR_MSG_SIZE);
(void) fprintf(stderr, " (%s)\n", err_buf);
abort();
}
|