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 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649
|
/* SC A Spreadsheet Calculator
* Expression interpreter and assorted support routines.
*
* original by James Gosling, September 1982
* modified by Mark Weiser and Bruce Israel,
* University of Maryland
*
* More mods Robert Bond, 12/86
* More mods by Alan Silverstein, 3-4/88, see list of changes.
* More mods by Peter Doemel, 2/93: support for REG_COMP
* $Revision: 1.1 $
*/
#include <config.h>
#include <curses.h>
#ifdef HAVE_X11_X_H
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif /* HAVE_X11_X_H */
#include <sys/types.h>
#ifdef aiws
#undef _C_func /* Fixes for undefined symbols on AIX */
#endif
#ifdef IEEE_MATH
#include <ieeefp.h>
#endif /* IEEE_MATH */
#include <math.h>
#include <signal.h>
#include <setjmp.h>
#include <ctype.h>
extern int errno; /* set by math functions */
#include "sc.h"
#ifdef HAVE_X11_X_H
# include "scXstuff.h"
#endif /* HAVE_X11_X_H */
#if defined(RE_COMP)
char *re_comp();
#endif
#if defined(REGCMP)
char *regcmp();
char *regex();
#endif
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
#include <regex.h>
# if NeedFunctionPrototypes
int regcomp(regex_t *preg, const char *pattern, int cflags);
int regexec(const regex_t *preg, const char *string, size_t nmatch,
regmatch_t pmatch[], int eflags);
void regfree(regex_t *preg);
char *regerrmsg(interrcode);
# else
char *regerrmsg();
# endif
#endif
/* Use this structure to save the the last 'g' command */
struct go_save {
int g_type;
double g_n;
char *g_s;
int g_row;
int g_col;
int errsearch;
} gs;
/* g_type can be: */
#define G_NONE 0 /* Starting value - must be 0*/
#define G_NUM 1
#define G_STR 2
#define G_CELL 3
#define ISVALID(r,c) ((r)>=0 && (r)<maxrows && (c)>=0 && (c)<maxcols)
jmp_buf fpe_save;
int exprerr; /* Set by eval() and seval() if expression errors */
double prescale = 1.0; /* Prescale for constants in let() */
int extfunc = 0; /* Enable/disable external functions */
int loading = 0; /* Set when readfile() is active */
int gmyrow, gmycol; /* globals used to implement @myrow, @mycol cmds */
/* a linked list of free [struct enodes]'s, uses .e.o.left as the pointer */
struct enode *freeenodes = NULL;
static int RealEvalAll PROTO((void));
static void RealEvalOne PROTO((struct ent *, int, int, int *));
static int constant PROTO((struct enode *));
static void copyrtv PROTO((int, int, int, int, int, int));
static void decodev PROTO((struct ent_ptr));
static void decompile PROTO((struct enode *, int));
static void decompile_list PROTO((struct enode *));
static double doavg PROTO((int, int, int, int));
static char * docapital PROTO((char *));
static char * docase PROTO((int, char *));
static char * docat PROTO((char *, char *));
static double docount PROTO((int, int, int, int));
static char * dodate PROTO((long));
static double doeqs PROTO((char *, char *));
static char * doext PROTO((char *, double));
static char * dofmt PROTO((char *, double));
static double doindex PROTO((double, int, int, int, int));
static double dolookup PROTO((struct enode *, int, int, int, int, int, int));
static double dolmax PROTO((struct enode *));
static double dolmin PROTO((struct enode *));
static double doprod PROTO((int, int, int, int));
static double domax PROTO((int, int, int, int));
static double domin PROTO((int, int, int, int));
static double donval PROTO((char *, double));
static double dostddev PROTO((int, int, int, int));
static char * dostindex PROTO((double, int, int, int, int));
static char * dosubstr PROTO((char *, int, int));
static double dosum PROTO((int, int, int, int));
static char * dosval PROTO((char *, double));
static double doston PROTO((char *));
static double dotime PROTO((int, double));
static double dotts PROTO((int, int, int));
static double eval PROTO((struct enode *));
static RETSIGTYPE eval_fpe PROTO((int)); /* Trap for FPE errors in eval */
static double finfunc PROTO((int, double, double, double));
static double fn1_eval PROTO((double (*)(), double));
static double fn2_eval PROTO((double (*)(), double, double));
static struct ent *getent PROTO((char *, double));
static void g_free PROTO((void));
static void index_arg PROTO((char *, struct enode *));
static void list_arg PROTO((char *, struct enode *));
static void one_arg PROTO((char *, struct enode *));
static void range_arg PROTO((char *, struct enode *));
static char * seval PROTO((struct enode *));
#ifdef RINT /* Otherwise we use the one in math.h */
double rint PROTO((double));
#endif
static void three_arg PROTO((char *, struct enode *));
static void two_arg PROTO((char *, struct enode *));
static void two_arg_index PROTO((char *, struct enode *));
int cellerror = CELLOK; /* is there an error in this cell */
#define dtr(x) ((x)*(PI/(double)180.0))
#define rtd(x) ((x)*(180.0/(double)PI))
static double
finfunc(fun,v1,v2,v3)
int fun;
double v1,v2,v3;
{
double answer,p;
p = fn2_eval(pow, 1 + v2, v3);
switch(fun)
{
case PV:
if (v2)
answer = v1 * (1 - 1/p) / v2;
else
{ cellerror = CELLERROR;
answer = (double)0;
}
break;
case FV:
if (v2)
answer = v1 * (p - 1) / v2;
else
{ cellerror = CELLERROR;
answer = (double)0;
}
break;
case PMT:
/* CHECK IF ~= 1 - 1/1 */
if (p && p != (double)1)
answer = v1 * v2 / (1 - 1/p);
else
{ cellerror = CELLERROR;
answer = (double)0;
}
break;
default:
scerror("Unknown function in finfunc");
cellerror = CELLERROR;
return((double)0);
}
return(answer);
}
static char *
dostindex( val, minr, minc, maxr, maxc)
double val;
int minr, minc, maxr, maxc;
{
register r,c;
register struct ent *p;
char *pr;
int x;
x = (int) val;
r = minr; c = minc;
p = (struct ent *)0;
if ( minr == maxr ) { /* look along the row */
c = minc + x - 1;
if (c <= maxc && c >=minc)
p = *ATBL(tbl, r, c);
} else if ( minc == maxc ) { /* look down the column */
r = minr + x - 1;
if (r <= maxr && r >=minr)
p = *ATBL(tbl, r, c);
} else {
scerror("range specified to @stindex");
cellerror = CELLERROR;
return((char *)0);
}
if (p && p->label) {
pr = scxmalloc((unsigned)(strlen(p->label)+1));
(void) strcpy(pr, p->label);
if (p->cellerror)
cellerror = CELLINVALID;
return (pr);
} else
return((char *)0);
}
static double
doindex( val, minr, minc, maxr, maxc)
double val;
int minr, minc, maxr, maxc;
{
double v;
register r,c;
register struct ent *p;
int x;
x = (int) val;
v = (double)0;
r = minr; c = minc;
if ( minr == maxr ) { /* look along the row */
c = minc + x - 1;
if (c <= maxc && c >=minc
&& (p = *ATBL(tbl, r, c)) && p->flags&is_valid )
{ if (p->cellerror)
cellerror = CELLINVALID;
return p->v;
}
}
else if ( minc == maxc ){ /* look down the column */
r = minr + x - 1;
if (r <= maxr && r >=minr
&& (p = *ATBL(tbl, r, c)) && p->flags&is_valid )
{ if (p->cellerror)
cellerror = CELLINVALID;
return p->v;
}
}
else {
scerror(" range specified to @index");
cellerror = CELLERROR;
}
return v;
}
static double
dolookup( val, minr, minc, maxr, maxc, offr, offc)
struct enode * val;
int minr, minc, maxr, maxc, offr, offc;
{
double v, ret = (double)0;
register r,c;
register struct ent *p = (struct ent *)0;
int incr,incc,fndr,fndc;
char *s;
incr = (offc != 0); incc = (offr != 0);
if (etype(val) == NUM) {
cellerror = CELLOK;
v = eval(val);
for (r = minr, c = minc; r <= maxr && c <= maxc; r+=incr, c+=incc) {
if ( (p = *ATBL(tbl, r, c)) && p->flags&is_valid ) {
if (p->v <= v) {
fndr = incc ? (minr + offr) : r;
fndc = incr ? (minc + offc) : c;
if (ISVALID(fndr,fndc))
p = *ATBL(tbl, fndr, fndc);
else {
scerror(" range specified to @[hv]lookup");
cellerror = CELLERROR;
}
if ( p && p->flags&is_valid)
{ if (p->cellerror)
cellerror = CELLINVALID;
ret = p->v;
}
} else break;
}
}
} else {
cellerror = CELLOK;
s = seval(val);
for (r = minr, c = minc; r <= maxr && c <= maxc; r+=incr, c+=incc) {
if ( (p = *ATBL(tbl, r, c)) && p->label ) {
if (strcmp(p->label,s) == 0) {
fndr = incc ? (minr + offr) : r;
fndc = incr ? (minc + offc) : c;
if (ISVALID(fndr,fndc))
{ p = *ATBL(tbl, fndr, fndc);
if (p->cellerror)
cellerror = CELLINVALID;
}
else {
scerror(" range specified to @[hv]lookup");
cellerror = CELLERROR;
}
break;
}
}
}
if ( p && p->flags&is_valid)
ret = p->v;
scxfree(s);
}
return ret;
}
static double
docount(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
int v;
register r,c;
register struct ent *p;
v = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid)
{ if (p->cellerror)
cellerror = CELLINVALID;
v++;
}
return v;
}
static double
dosum(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double v;
register r,c;
register struct ent *p;
v = (double)0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid)
{ if (p->cellerror)
cellerror = CELLINVALID;
v += p->v;
}
return v;
}
static double
doprod(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double v;
register r,c;
register struct ent *p;
v = 1;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid)
{ if (p->cellerror)
cellerror = CELLINVALID;
v *= p->v;
}
return v;
}
static double
doavg(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double v;
register r,c,count;
register struct ent *p;
v = (double)0;
count = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid) {
if (p->cellerror)
cellerror = CELLINVALID;
v += p->v;
count++;
}
if (count == 0)
return ((double) 0);
return (v / (double)count);
}
static double
dostddev(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double lp, rp, v, nd;
register r,c,n;
register struct ent *p;
n = 0;
lp = 0;
rp = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid) {
if (p->cellerror)
cellerror = CELLINVALID;
v = p->v;
lp += v*v;
rp += v;
n++;
}
if ((n == 0) || (n == 1))
return ((double) 0);
nd = (double)n;
return (sqrt((nd*lp-rp*rp)/(nd*(nd-1))));
}
static double
domax(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double v = (double)0;
register r,c,count;
register struct ent *p;
count = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid) {
if (p->cellerror)
cellerror = CELLINVALID;
if (!count) {
v = p->v;
count++;
} else if (p->v > v)
v = p->v;
}
if (count == 0)
return ((double) 0);
return (v);
}
static double
domin(minr, minc, maxr, maxc)
int minr, minc, maxr, maxc;
{
double v = (double)0;
register r,c,count;
register struct ent *p;
count = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++)
if ((p = *ATBL(tbl, r, c)) && p->flags&is_valid) {
if (p->cellerror)
cellerror = CELLINVALID;
if (!count) {
v = p->v;
count++;
} else if (p->v < v)
v = p->v;
}
if (count == 0)
return ((double) 0);
return (v);
}
#define sec_min 60
#define sec_hr 3600L
#define sec_day 86400L
#define sec_yr 31471200L /* 364.25 days/yr */
#define sec_mo 2622600L /* sec_yr/12: sort of an average */
int mdays[12]={ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
double
dodts(mo, day, yr)
int mo, day, yr;
{
long trial;
register struct tm *tp;
register int i;
register long jdate;
mdays[1] = 28 + (yr%4 == 0);
if (mo < 1 || mo > 12 || day < 1 || day > mdays[--mo] ||
yr > 1999 || yr < 1970) {
scerror("@dts: invalid argument");
cellerror = CELLERROR;
return(0.0);
}
jdate = day-1;
for (i=0; i<mo; i++)
jdate += mdays[i];
for (i = 1970; i < yr; i++)
jdate += 365 + (i%4 == 0);
trial = jdate * sec_day;
yr -= 1900;
tp = localtime(&trial);
if (tp->tm_year != yr) {
/*
* We may fail this test once a year because of time zone
* and daylight savings time errors. This bounces the
* trial time past the boundary. The error introduced is
* corrected below.
*/
trial += sec_day*(yr - tp->tm_year);
tp = localtime(&trial);
}
if (tp->tm_mon != mo) {
/* We may fail this test once a month. */
trial += sec_day*(mo - tp->tm_mon);
tp = localtime(&trial);
}
if (tp->tm_mday + tp->tm_hour + tp->tm_min + tp->tm_sec != day) {
trial -= (tp->tm_mday - day)*sec_day + tp->tm_hour*sec_hr
+ tp->tm_min*sec_min + tp->tm_sec;
}
return ((double)trial);
}
static double
dotts(hr, min, sec)
int hr, min, sec;
{
if (hr < 0 || hr > 23 || min < 0 || min > 59 || sec < 0 || sec > 59) {
scerror("@tts: Invalid argument");
cellerror = CELLERROR;
return ((double)0);
}
return ((double)(sec+min*60+hr*3600));
}
static double
dotime(which, when)
int which;
double when;
{
static long t_cache;
static struct tm tm_cache;
struct tm *tp;
long tloc;
if (which == NOW)
return (double)time((long *)0);
tloc = (long)when;
if (tloc != t_cache) {
tp = localtime(&tloc);
tm_cache = *tp;
tm_cache.tm_mon += 1;
tm_cache.tm_year += 1900;
t_cache = tloc;
}
switch (which) {
case HOUR: return((double)(tm_cache.tm_hour));
case MINUTE: return((double)(tm_cache.tm_min));
case SECOND: return((double)(tm_cache.tm_sec));
case MONTH: return((double)(tm_cache.tm_mon));
case DAY: return((double)(tm_cache.tm_mday));
case YEAR: return((double)(tm_cache.tm_year));
}
/* Safety net */
cellerror = CELLERROR;
return ((double)0);
}
static double
doston(s)
char *s;
{
double v;
if (!s)
return((double)0);
v = atof(s);
/* (void)strtof(s, &v); */
scxfree(s);
return(v);
}
static double
doeqs(s1, s2)
char *s1, *s2;
{
double v;
if (!s1 && !s2)
return((double)1.0);
if (!s1 || !s2)
v = 0.0;
else if (strcmp(s1, s2) == 0)
v = 1.0;
else
v = 0.0;
if (s1)
scxfree(s1);
if (s2)
scxfree(s2);
return(v);
}
/*
* Given a string representing a column name and a value which is a column
* number, return a pointer to the selected cell's entry, if any, else NULL.
* Use only the integer part of the column number. Always free the string.
*/
static struct ent *
getent (colstr, rowdoub)
char *colstr;
double rowdoub;
{
int collen; /* length of string */
int row, col; /* integer values */
struct ent *p = (struct ent *)0; /* selected entry */
if (!colstr)
{ cellerror = CELLERROR;
return((struct ent *)0);
}
if (((row = (int) floor (rowdoub)) >= 0)
&& (row < maxrows) /* in range */
&& ((collen = strlen (colstr)) <= 2) /* not too long */
&& ((col = atocol (colstr, collen)) >= 0)
&& (col < maxcols)) /* in range */
{
p = *ATBL(tbl, row, col);
if ((p != NULL) && p->cellerror)
cellerror = CELLINVALID;
}
scxfree (colstr);
return (p);
}
/*
* Given a string representing a column name and a value which is a column
* number, return the selected cell's numeric value, if any.
*/
static double
donval (colstr, rowdoub)
char *colstr;
double rowdoub;
{
struct ent *ep;
return (((ep = getent (colstr, rowdoub)) && ((ep -> flags) & is_valid)) ?
(ep -> v) : (double)0);
}
/*
* The list routines (e.g. dolmax) are called with an LMAX enode.
* The left pointer is a chain of ELIST nodes, the right pointer
* is a value.
*/
static double
dolmax(ep)
struct enode *ep;
{
register int count = 0;
register double maxval = 0; /* Assignment to shut up lint */
register struct enode *p;
register double v;
cellerror = CELLOK;
for (p = ep; p; p = p->e.o.left) {
v = eval(p->e.o.right);
if (!count || v > maxval) {
maxval = v; count++;
}
}
if (count) return maxval;
else return (double)0;
}
static double
dolmin(ep)
struct enode *ep;
{
register int count = 0;
register double minval = 0; /* Assignment to shut up lint */
register struct enode *p;
register double v;
cellerror = CELLOK;
for (p = ep; p; p = p->e.o.left) {
v = eval(p->e.o.right);
if (!count || v < minval) {
minval = v; count++;
}
}
if (count) return minval;
else return (double)0;
}
static double
eval(e)
register struct enode *e;
{
if (e == (struct enode *)0)
{ cellerror = CELLINVALID;
return (double)0;
}
switch (e->op) {
case '+': return (eval(e->e.o.left) + eval(e->e.o.right));
case '-': return (eval(e->e.o.left) - eval(e->e.o.right));
case '*': return (eval(e->e.o.left) * eval(e->e.o.right));
case '/': { double num, denom;
num = eval(e->e.o.left);
denom = eval(e->e.o.right);
if (denom)
/* if (1) */
/* to test num div 0 */
return(num/denom);
else
{ cellerror = CELLERROR;
return((double) 0);
}
}
case '%': { double num, denom;
num = floor(eval(e->e.o.left));
denom = floor(eval (e->e.o.right));
if (denom)
return(num - floor(num/denom)*denom);
else
{ cellerror = CELLERROR;
return((double) 0);
}
}
case '^': return (fn2_eval(pow,eval(e->e.o.left),eval(e->e.o.right)));
case '<': return (eval(e->e.o.left) < eval(e->e.o.right));
case '=': return (eval(e->e.o.left) == eval(e->e.o.right));
case '>': return (eval(e->e.o.left) > eval(e->e.o.right));
case '&': return (eval(e->e.o.left) && eval(e->e.o.right));
case '|': return (eval(e->e.o.left) || eval(e->e.o.right));
case IF:
case '?': return eval(e->e.o.left) ? eval(e->e.o.right->e.o.left)
: eval(e->e.o.right->e.o.right);
case 'm': return (-eval(e->e.o.right));
case 'f': return (eval(e->e.o.right));
case '~': return (eval(e->e.o.right) == 0.0);
case O_CONST: return (e->e.k);
case O_VAR: if (e->e.v.vp->cellerror)
cellerror = CELLINVALID;
return (e->e.v.vp->v);
case INDEX:
case LOOKUP:
case HLOOKUP:
case VLOOKUP:
{ int r,c;
int maxr, maxc;
int minr, minc;
maxr = e->e.o.right->e.r.right.vp -> row;
maxc = e->e.o.right->e.r.right.vp -> col;
minr = e->e.o.right->e.r.left.vp -> row;
minc = e->e.o.right->e.r.left.vp -> col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
switch(e->op){
case LOOKUP:
return dolookup(e->e.o.left, minr, minc, maxr, maxc,
minr==maxr, minc==maxc);
case HLOOKUP:
return dolookup(e->e.o.left->e.o.left, minr,minc,maxr,maxc,
(int) eval(e->e.o.left->e.o.right), 0);
case VLOOKUP:
return dolookup(e->e.o.left->e.o.left, minr,minc,maxr,maxc,
0, (int) eval(e->e.o.left->e.o.right));
case INDEX:
return doindex(eval(e->e.o.left), minr, minc, maxr, maxc);
}
}
case REDUCE | '+': /* sum */
case REDUCE | '*': /* prod */
case REDUCE | 'a': /* avg */
case REDUCE | 'c': /* count */
case REDUCE | 's': /* stddev */
case REDUCE | IRR: /* internal rate of return */
case REDUCE | MAXR: /* max-range */
case REDUCE | MINR: /* min-range */
{ int r,c;
int maxr, maxc;
int minr, minc;
maxr = e->e.r.right.vp -> row;
maxc = e->e.r.right.vp -> col;
minr = e->e.r.left.vp -> row;
minc = e->e.r.left.vp -> col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
switch (e->op & ~REDUCE) {
case '+': return dosum(minr, minc, maxr, maxc);
case '*': return doprod(minr, minc, maxr, maxc);
case 'a': return doavg(minr, minc, maxr, maxc);
case 'c': return docount(minr, minc, maxr, maxc);
case 's': return dostddev(minr, minc, maxr, maxc);
case IRR: return doirr(minr, minc, maxr, maxc);
case MAXR: return domax(minr, minc, maxr, maxc);
case MINR: return domin(minr, minc, maxr, maxc);
}
}
case ABS: return (fn1_eval( fabs, eval(e->e.o.right)));
case ACOS: return (fn1_eval( acos, eval(e->e.o.right)));
case ASIN: return (fn1_eval( asin, eval(e->e.o.right)));
case ATAN: return (fn1_eval( atan, eval(e->e.o.right)));
case ATAN2: return (fn2_eval( atan2, eval(e->e.o.left), eval(e->e.o.right)));
case CEIL: return (fn1_eval( ceil, eval(e->e.o.right)));
case COS: return (fn1_eval( cos, eval(e->e.o.right)));
case EXP: return (fn1_eval( exp, eval(e->e.o.right)));
case FABS: return (fn1_eval( fabs, eval(e->e.o.right)));
case FLOOR: return (fn1_eval( floor, eval(e->e.o.right)));
case HYPOT: return (fn2_eval( hypot, eval(e->e.o.left), eval(e->e.o.right)));
case LOG: return (fn1_eval( log, eval(e->e.o.right)));
case LOG10: return (fn1_eval( log10, eval(e->e.o.right)));
case POW: return (fn2_eval( pow, eval(e->e.o.left), eval(e->e.o.right)));
case SIN: return (fn1_eval( sin, eval(e->e.o.right)));
case SQRT: return (fn1_eval( sqrt, eval(e->e.o.right)));
case TAN: return (fn1_eval( tan, eval(e->e.o.right)));
case DTR: return (dtr(eval(e->e.o.right)));
case RTD: return (rtd(eval(e->e.o.right)));
case RND:
if (rndinfinity)
{ double temp = eval(e->e.o.right);
return(temp-floor(temp) < 0.5 ?
floor(temp) : ceil(temp));
}
else
return rint(eval(e->e.o.right));
case ROUND:
{ int prec = (int) eval(e->e.o.right);
double scal = 1;
if (0 < prec)
do scal *= 10; while (0 < --prec);
else if (prec < 0)
do scal /= 10; while (++prec < 0);
if (rndinfinity)
{ double temp = eval(e->e.o.left);
temp *= scal;
temp = ((temp-floor(temp)) < 0.5 ?
floor(temp) : ceil(temp));
return(temp / scal);
}
else
return(rint(eval(e->e.o.left) * scal) / scal);
}
case FV:
case PV:
case PMT: return(finfunc(e->op,eval(e->e.o.left),
eval(e->e.o.right->e.o.left),
eval(e->e.o.right->e.o.right)));
case HOUR: return (dotime(HOUR, eval(e->e.o.right)));
case MINUTE: return (dotime(MINUTE, eval(e->e.o.right)));
case SECOND: return (dotime(SECOND, eval(e->e.o.right)));
case MONTH: return (dotime(MONTH, eval(e->e.o.right)));
case DAY: return (dotime(DAY, eval(e->e.o.right)));
case YEAR: return (dotime(YEAR, eval(e->e.o.right)));
case NOW: return (dotime(NOW, (double)0.0));
case DTS: return (dodts((int)eval(e->e.o.left),
(int)eval(e->e.o.right->e.o.left),
(int)eval(e->e.o.right->e.o.right)));
case TTS: return (dotts((int)eval(e->e.o.left),
(int)eval(e->e.o.right->e.o.left),
(int)eval(e->e.o.right->e.o.right)));
case STON: return (doston(seval(e->e.o.right)));
case EQS: return (doeqs(seval(e->e.o.right),seval(e->e.o.left)));
case LMAX: return dolmax(e);
case LMIN: return dolmin(e);
case NVAL: return (donval(seval(e->e.o.left),eval(e->e.o.right)));
case MYROW: return ((double) gmyrow);
case MYCOL: return ((double) gmycol);
case NUMITER: return ((double) repct);
default: scerror("Illegal numeric expression");
exprerr = 1;
}
cellerror = CELLERROR;
return((double)0.0);
}
static RETSIGTYPE /* static added by Bob Parbs 12-92 */
eval_fpe(sig) /* Trap for FPE errors in eval */
int sig;
{
#if defined(i386) && !defined(M_XENIX)
/*asm(" fnclex");*/
/*asm(" fwait");*/
#else
#ifdef IEEE_MATH
(void)fpsetsticky((fp_except)0); /* Clear exception */
#endif /* IEEE_MATH */
#ifdef PC
_fpreset();
#endif
#endif
/* re-establish signal handler for next time */
(void) signal(SIGFPE, eval_fpe);
longjmp(fpe_save, 1);
}
static double
fn1_eval(fn, arg)
double (*fn)();
double arg;
{
double res;
errno = 0;
res = (*fn)(arg);
if(errno)
cellerror = CELLERROR;
return res;
}
static double
fn2_eval(fn, arg1, arg2)
double (*fn)();
double arg1, arg2;
{
double res;
errno = 0;
res = (*fn)(arg1, arg2);
if(errno)
cellerror = CELLERROR;
return res;
}
/*
* Rules for string functions:
* Take string arguments which they scxfree.
* All returned strings are assumed to be xalloced.
*/
static char *
docat(s1, s2)
register char *s1, *s2;
{
register char *p;
char *arg1, *arg2;
if (!s1 && !s2)
return((char *)0);
arg1 = s1 ? s1 : "";
arg2 = s2 ? s2 : "";
p = scxmalloc((unsigned)(strlen(arg1)+strlen(arg2)+1));
(void) strcpy(p, arg1);
(void) strcat(p, arg2);
if (s1)
scxfree(s1);
if (s2)
scxfree(s2);
return(p);
}
static char *
dodate(tloc)
long tloc;
{
char *tp;
char *p;
tp = ctime(&tloc);
tp[24] = '\0';
p = scxmalloc((unsigned)25);
(void) strcpy(p, tp);
return(p);
}
static char *
dofmt(fmtstr, v)
char *fmtstr;
double v;
{
char buff[FBUFLEN];
char *p;
if (!fmtstr)
return((char *)0);
(void) sprintf(buff, fmtstr, v);
p = scxmalloc((unsigned)(strlen(buff)+1));
(void) strcpy(p, buff);
scxfree(fmtstr);
return(p);
}
/*
* Given a command name and a value, run the command with the given value and
* read and return its first output line (only) as an allocated string, always
* a copy of prevstr, which is set appropriately first unless external
* functions are disabled, in which case the previous value is used. The
* handling of prevstr and freeing of command is tricky. Returning an
* allocated string in all cases, even if null, insures cell expressions are
* written to files, etc.
*/
#if defined(VMS) || defined(MSDOS)
static char *
doext(command, value)
char *command;
double value;
{
scerror("Warning: External functions unavailable on VMS");
cellerror = CELLERROR; /* not sure if this should be a cellerror */
if (command)
scxfree(command);
return (strcpy (scxmalloc((unsigned) 1), "\0"));
}
#else /* VMS */
static char *
doext (command, value)
char *command;
double value;
{
static char *prevstr = (char *)0; /* previous result */
static unsigned prevlen = 0;
char buff[FBUFLEN]; /* command line/return, not permanently alloc */
if (!extfunc) {
sprintf(stringbuf, "Warning: external functions disabled; using %s value",
((prevstr == NULL) || (*prevstr == '\0')) ?
"null" : "previous");
scerror(stringbuf);
if (command)
scxfree (command);
} else {
if ((! command) || (! *command)) {
scerror("Warning: external function given null command name");
cellerror = CELLERROR;
if (command) scxfree (command);
} else {
FILE *pp;
(void) sprintf (buff, "%s %g", command, value); /* build cmd line */
scxfree (command);
scerror("Running external function...");
if (!using_X)
(void) refresh();
if ((pp = popen (buff, "r")) == (FILE *) NULL) { /* run it */
sprintf(stringbuf, "Warning: running \"%s\" failed", buff);
scerror(stringbuf);
cellerror = CELLERROR;
}
else {
if (fgets (buff, sizeof(buff)-1, pp) == NULL) /* one line */
scerror("Warning: external function returned nothing");
else {
char *cp;
scerror(""); /* erase notice */
buff[sizeof(buff)-1] = '\0';
if ((cp = strchr (buff, '\n')) != NULL)/*contains newline*/
*cp = '\0'; /* end string there */
if (strlen(buff) + 1 > prevlen)
{ prevlen = strlen(buff) + 40;
prevstr = scxrealloc(prevstr, prevlen);
}
(void) strcpy (prevstr, buff);
/* save alloc'd copy */
}
(void) pclose (pp);
} /* else */
} /* else */
} /* else */
if (prevstr)
return (strcpy (scxmalloc ((unsigned) (strlen (prevstr) + 1)), prevstr));
else
return (strcpy(scxmalloc((unsigned)1), ""));
}
#endif /* VMS */
/*
* Given a string representing a column name and a value which is a column
* number, return the selected cell's string value, if any. Even if none,
* still allocate and return a null string so the cell has a label value so
* the expression is saved in a file, etc.
*/
static char *
dosval (colstr, rowdoub)
char *colstr;
double rowdoub;
{
struct ent *ep;
char *llabel;
llabel = (ep = getent (colstr, rowdoub)) ? (ep -> label) : "";
return (strcpy (scxmalloc ((unsigned) (strlen (llabel) + 1)), llabel));
}
/*
* Substring: Note that v1 and v2 are one-based to users, but zero-based
* when calling this routine.
*/
static char *
dosubstr(s, v1, v2)
char *s;
register int v1,v2;
{
register char *s1, *s2;
char *p;
if (!s)
return((char *)0);
if (v2 >= (int)strlen (s)) /* past end */
v2 = strlen (s) - 1; /* to end */
if (v1 < 0 || v1 > v2) { /* out of range, return null string */
scxfree(s);
p = scxmalloc((unsigned)1);
p[0] = '\0';
return(p);
}
s2 = p = scxmalloc((unsigned)(v2-v1+2));
s1 = &s[v1];
for(; v1 <= v2; s1++, s2++, v1++)
*s2 = *s1;
*s2 = '\0';
scxfree(s);
return(p);
}
/*
* character casing: make upper case, make lower case
*/
static char *
docase( acase, s)
int acase;
char *s;
{
char *p = s;
if (s == NULL)
return(NULL);
if( acase == UPPER ) {
while( *p != '\0' ) {
if( islower(*p) )
*p = toupper(*p);
p++;
}
}
else if ( acase == LOWER ) {
while( *p != '\0' ) {
if (isupper(*p))
*p = tolower(*p);
p++;
}
}
return (s);
}
/*
* make proper capitals of every word in a string
* if the string has mixed case we say the string is lower
* and we will upcase only first letters of words
* if the string is all upper we will lower rest of words.
*/
static char *
docapital( s )
char *s;
{
char *p;
int skip = 1;
int AllUpper = 1;
if (s == NULL)
return(NULL);
for( p = s; *p != '\0' && AllUpper != 0; p++ )
if( isalpha(*p) && islower(*p) ) AllUpper = 0;
for (p = s; *p != '\0'; p++) {
if (!isalnum(*p))
skip = 1;
else
if (skip == 1) {
skip = 0;
if (islower(*p))
*p = toupper(*p);
}
else /* if the string was all upper before */
if (isupper(*p) && AllUpper != 0)
*p = tolower(*p);
}
return(s);
}
static char *
seval(se)
register struct enode *se;
{
register char *p;
if (se == (struct enode *)0) return (char *)0;
switch (se->op) {
case O_SCONST: p = scxmalloc((unsigned)(strlen(se->e.s)+1));
(void) strcpy(p, se->e.s);
return(p);
case O_VAR: {
struct ent *ep;
ep = se->e.v.vp;
if (!ep->label)
return((char *)0);
p = scxmalloc((unsigned)(strlen(ep->label)+1));
(void) strcpy(p, ep->label);
return(p);
}
case '#': return(docat(seval(se->e.o.left), seval(se->e.o.right)));
case 'f': return(seval(se->e.o.right));
case IF:
case '?': return(eval(se->e.o.left) ? seval(se->e.o.right->e.o.left)
: seval(se->e.o.right->e.o.right));
case DATE: return(dodate((long)(eval(se->e.o.right))));
case FMT: return(dofmt(seval(se->e.o.left), eval(se->e.o.right)));
case UPPER: return(docase(UPPER, seval(se->e.o.right)));
case LOWER: return(docase(LOWER, seval(se->e.o.right)));
case CAPITAL:return(docapital(seval(se->e.o.right)));
case STINDEX:
{ register r,c;
register maxr, maxc;
register minr, minc;
maxr = se->e.o.right->e.r.right.vp -> row;
maxc = se->e.o.right->e.r.right.vp -> col;
minr = se->e.o.right->e.r.left.vp -> row;
minc = se->e.o.right->e.r.left.vp -> col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
return dostindex(eval(se->e.o.left), minr, minc, maxr, maxc);
}
case EXT: return(doext(seval(se->e.o.left), eval(se->e.o.right)));
case SVAL: return(dosval(seval(se->e.o.left), eval(se->e.o.right)));
case SUBSTR: return(dosubstr(seval(se->e.o.left),
(int)eval(se->e.o.right->e.o.left) - 1,
(int)eval(se->e.o.right->e.o.right) - 1));
case COLTOA: return(strcpy(scxmalloc((unsigned)10),
coltoa((int)eval(se->e.o.right)+1)));
default:
scerror("Illegal string expression");
exprerr = 1;
return(NULL);
}
}
/*
* The graph formed by cell expressions which use other cells's values is not
* evaluated "bottom up". The whole table is merely re-evaluated cell by cell,
* top to bottom, left to right, in RealEvalAll(). Each cell's expression uses
* constants in other cells. However, RealEvalAll() notices when a cell gets a
* new numeric or string value, and reports if this happens for any cell.
* EvalAll() repeats calling RealEvalAll() until there are no changes or the
* evaluation count expires.
*/
int propagation = 10; /* max number of times to try calculation */
int repct = 1; /* Make repct a global variable so that the
function @numiter can access it */
void
setiterations(i)
int i;
{
if(i<1) {
scerror("iteration count must be at least 1");
propagation = 1;
}
else propagation = i;
}
void
EvalAll () {
int lastcnt;
repct = 1;
(void) signal(SIGFPE, eval_fpe);
while ((lastcnt = RealEvalAll()) && (++repct <= propagation));
if((propagation>1)&& (lastcnt >0 ))
{ sprintf(stringbuf, "Still changing after %d iterations",propagation-1);
scerror(stringbuf);
}
(void) signal(SIGFPE, doquit);
}
/*
* Evaluate all cells which have expressions and alter their numeric or string
* values. Return the number of cells which changed.
*/
static int
RealEvalAll () {
register int i,j;
int chgct = 0;
register struct ent *p;
if(calc_order == BYROWS ) {
for (i=0; i<=maxrow; i++)
for (j=0; j<=maxcol; j++)
if ((p = *ATBL(tbl,i,j)) && !(p->flags&is_locked) && p->expr) RealEvalOne(p,i,j,&chgct);
}
else if ( calc_order == BYCOLS ) {
for (j=0; j<=maxcol; j++)
{ for (i=0; i<=maxrow; i++)
if ((p = *ATBL(tbl,i,j)) && !(p->flags&is_locked) && p->expr) RealEvalOne(p,i,j,&chgct);
}
}
else scerror("Internal error calc_order");
return(chgct);
}
static void /* static added by Bob Parbs 12-92 */
RealEvalOne(p, i, j, chgct)
register struct ent *p;
int i, j, *chgct;
{
if (p->flags & is_strexpr) {
char *v;
if (setjmp(fpe_save)) {
sprintf(stringbuf, "Floating point exception %s", v_name(i, j));
scerror(stringbuf);
cellerror = CELLERROR;
v = "";
} else {
cellerror = CELLOK;
v = seval(p->expr);
}
p->cellerror = cellerror;
if (!v && !p->label) /* Everything's fine */
return;
if (!p->label || !v || strcmp(v, p->label) != 0 || cellerror) {
(*chgct)++;
p->flags |= is_changed;
changed++;
}
if(p->label)
scxfree(p->label);
p->label = v;
} else {
double v;
if (setjmp(fpe_save)) {
sprintf(stringbuf, "Floating point exception %s", v_name(i, j));
scerror(stringbuf);
cellerror = CELLERROR;
v = (double)0.0;
} else {
cellerror = CELLOK;
gmyrow=i; gmycol=j;
v = eval (p->expr);
}
/*
if ((p->cellerror = cellerror) || (v != p->v)) {
p->v = v;
if (!cellerror)*/ /* don't keep eval'ing a error */
/* (*chgct)++;
p->flags |= is_changed|is_valid;
changed++;
}
*/
if ( (p->cellerror = cellerror) || (p->v == 0 && v != 0)
|| (fabs((v - p->v)/p->v) > 1e-8) ) {
p->v = v;
if (!cellerror) /* don't keep eval'ing a error */
(*chgct)++;
p->flags |= is_changed|is_valid;
changed++;
}
}
}
struct enode *
new(op, a1, a2)
int op;
struct enode *a1, *a2;
{
register struct enode *p;
if (freeenodes)
{ p = freeenodes;
freeenodes = p->e.o.left;
}
else
p = (struct enode *) scxmalloc ((unsigned)sizeof (struct enode));
p->op = op;
p->e.o.left = a1;
p->e.o.right = a2;
return p;
}
struct enode *
new_var(op, a1)
int op;
struct ent_ptr a1;
{
register struct enode *p;
if (freeenodes)
{ p = freeenodes;
freeenodes = p->e.o.left;
}
else
p = (struct enode *) scxmalloc ((unsigned)sizeof (struct enode));
p->op = op;
p->e.v = a1;
return p;
}
struct enode *
new_range(op, a1)
int op;
struct range_s a1;
{
register struct enode *p;
if (freeenodes)
{ p = freeenodes;
freeenodes = p->e.o.left;
}
else
p = (struct enode *) scxmalloc ((unsigned)sizeof (struct enode));
p->op = op;
p->e.r = a1;
return p;
}
struct enode *
new_const(op, a1)
int op;
double a1;
{
register struct enode *p;
if (freeenodes) /* reuse an already free'd enode */
{ p = freeenodes;
freeenodes = p->e.o.left;
}
else
p = (struct enode *) scxmalloc ((unsigned)sizeof (struct enode));
p->op = op;
p->e.k = a1;
return p;
}
struct enode *
new_str(s)
char *s;
{
register struct enode *p;
if (freeenodes) /* reuse an already free'd enode */
{ p = freeenodes;
freeenodes = p->e.o.left;
}
else
p = (struct enode *) scxmalloc ((unsigned)sizeof(struct enode));
p->op = O_SCONST;
p->e.s = s;
return(p);
}
/*
* copy from v1:v2 to dv1:dv2
*
* -special case where dv2, the destination max is empty,
* in which case force the destination to conform to the source.
*/
void
copy(dv1, dv2, v1, v2)
struct ent *dv1, *dv2, *v1, *v2;
{
int minsr, minsc;
int maxsr, maxsc;
int mindr, mindc;
int maxdr, maxdc;
int vr, vc;
int r, c;
mindr = dv1->row;
mindc = dv1->col;
maxdr = dv2->row;
maxdc = dv2->col;
if (mindr>maxdr) r = maxdr, maxdr = mindr, mindr = r;
if (mindc>maxdc) c = maxdc, maxdc = mindc, mindc = c;
maxsr = v2->row;
maxsc = v2->col;
minsr = v1->row;
minsc = v1->col;
if (minsr>maxsr) r = maxsr, maxsr = minsr, minsr = r;
if (minsc>maxsc) c = maxsc, maxsc = minsc, minsc = c;
mindr = dv1->row;
mindc = dv1->col;
if (dv2) {
maxdr = dv2->row;
maxdc = dv2->col;
}
else {
/* special case, conform the destination to the source */
maxdr = mindr + maxsr - minsr;
maxdc = mindc + maxsc - minsc;
}
if (mindr>maxdr) r = maxdr, maxdr = mindr, mindr = r;
if (mindc>maxdc) c = maxdc, maxdc = mindc, mindc = c;
checkbounds(&maxdr, &maxdc);
/* needed to make sure overlaps work */
/* flush_saved();
erase_and_pull(mindr, mindc, maxdr, maxdc, 0, 1);
*/
if (minsr == maxsr && minsc == maxsc) {
/* Source is a single cell */
for(vr = mindr; vr <= maxdr; vr++)
for (vc = mindc; vc <= maxdc; vc++)
copyrtv(vr, vc, minsr, minsc, maxsr, maxsc);
} else if (minsr == maxsr) {
/* Source is a single row */
for (vr = mindr; vr <= maxdr; vr++)
copyrtv(vr, mindc, minsr, minsc, maxsr, maxsc);
} else if (minsc == maxsc) {
/* Source is a single column */
for (vc = mindc; vc <= maxdc; vc++)
copyrtv(mindr, vc, minsr, minsc, maxsr, maxsc);
} else {
/* Everything else */
copyrtv(mindr, mindc, minsr, minsc, maxsr, maxsc);
}
sync_refs();
}
static void
copyrtv(vr, vc, minsr, minsc, maxsr, maxsc)
int vr, vc, minsr, minsc, maxsr, maxsc;
{
register struct ent *p;
register struct ent *n;
int sr, sc; /* source row/col */
int sdc, ssc;
int dr, dc; /* destination row/col */
register rsteps, csteps;
int scsteps;
register rinc, cinc;
if (vr == minsr && vc == minsc)
return; /* copying onto itself. */
rsteps = maxsr - minsr + 1;
csteps = maxsc - minsc + 1;
rinc = cinc = 1;
dr = vr; dc = vc;
sr = minsr; sc = minsc;
if (dr > sr) {
sr += rsteps - 1;
dr += rsteps - 1;
rinc = -1;
}
if (dc > sc) {
sc += csteps - 1;
dc += csteps - 1;
cinc = -1;
}
ssc = sc; sdc = dc; scsteps = csteps;
for (; --rsteps >= 0; sr += rinc, dr += rinc)
{
for (csteps = scsteps, sc = ssc, dc = sdc;
--csteps >= 0; sc += cinc, dc += cinc)
{
if ((p = *ATBL(tbl, sr, sc)) != NULL)
{ n = lookat (dr, dc);
if (n->flags&is_locked) continue;
(void) clearent(n);
copyent( n, p, dr - sr, dc - sc);
}
else if ((n = *ATBL(tbl, dr, dc)) != NULL)
(void) clearent(n);
}
}
}
/* ERASE a Range of cells */
void
eraser(v1, v2)
struct ent *v1, *v2;
{
FullUpdate++;
flush_saved();
erase_area(v1->row, v1->col, v2->row, v2->col);
sync_refs();
}
/* Goto subroutines */
static void /* static added by Bob Parbs 12-92 */
g_free()
{
switch (gs.g_type) {
case G_STR: scxfree(gs.g_s); break;
default: break;
}
gs.g_type = G_NONE;
gs.errsearch = 0;
}
/* repeat the last goto command */
void
go_last()
{
switch (gs.g_type) {
case G_NONE:
scerror("Nothing to repeat"); break;
case G_NUM:
num_search(gs.g_n, gs.errsearch);
break;
case G_CELL:
moveto(gs.g_row, gs.g_col);
break;
case G_STR:
gs.g_type = G_NONE; /* Don't free the string */
str_search(gs.g_s);
break;
default: scerror("go_last: internal error");
}
}
/* place the cursor on a given cell */
void
moveto(row, col)
int row, col;
{
currow = row;
curcol = col;
g_free();
gs.g_type = G_CELL;
gs.g_row = currow;
gs.g_col = curcol;
}
/*
* 'goto' either a given number,'error', or 'invalid' starting at currow,curcol
*/
void
num_search(n, errsearch)
double n;
int errsearch;
{
register struct ent *p;
register int r,c;
int endr, endc;
g_free();
gs.g_type = G_NUM;
gs.g_n = n;
gs.errsearch = errsearch;
if (currow > maxrow)
endr = maxrow ? maxrow-1 : 0;
else
endr = currow;
if (curcol > maxcol)
endc = maxcol ? maxcol-1 : 0;
else
endc = curcol;
r = endr;
c = endc;
do {
if (c < maxcol)
c++;
else {
if (r < maxrow) {
while(++r < maxrow && row_hidden[r]) /* */;
c = 0;
} else {
r = 0;
c = 0;
}
}
if (r == endr && c == endc) {
if (errsearch)
{ sprintf(stringbuf, "no %s cell found",
errsearch == CELLERROR ? "ERROR" : "INVALID");
scerror(stringbuf);
}
else
scerror("Number not found");
return;
}
p = *ATBL(tbl, r, c);
} while (col_hidden[c] || !p || !(p->flags & is_valid)
|| (!errsearch && (p->v != n))
|| (errsearch && !((p->cellerror == errsearch) ||
(p->cellerror == errsearch)))); /* CELLERROR vs CELLINVALID */
currow = r;
curcol = c;
}
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
char *
regerrmsg(errcode)
int errcode;
{
switch(errcode) {
case REG_NOMATCH:
return "regexec() failed to match.";
case REG_ECOLLATE:
return "Invalid collation element referenced.";
case REG_EESCAPE:
return "Trailing \\ in pattern.";
case REG_ENEWLINE:
return "<newline> found before end of pattern and REG_NEWLINE flag not set.";
case REG_ESUBREG:
return "Number in \\digit invalid or in error.";
case REG_EBRACK:
return "[ ] imbalance.";
case REG_EPAREN:
return "\\( \\) imbalance in basic regular expression or ( ) imbalance in extended regular expression.";
case REG_EBRACE:
return "\\{ \\} imbalance.";
case REG_ERANGE:
return "Invalid endpoint in range statement.";
case REG_ESPACE:
return "Out of memory for compiled pattern.";
case REG_EMEM:
return "Out of memory while matching expression.";
/* case REG_EBADRPT:
return "Misuse of *, +, or ? in extended regular expression."; */
case REG_ECTYPE:
return "Invalid character class type named.";
case REG_ENSUB:
return "Too many parenthesis pairs or nesting level too deep.";
case REG_EABRACE:
return "Number too large in \\{ \\} construct.";
case REG_EBBRACE:
return "Invalid number in \\{ \\} construct.";
case REG_ECBRACE:
return "More than two numbers in \\{ \\} construct.";
case REG_EDBRACE:
return "First number exceeds second in \\{ \\} construct.";
case REG_ENOSEARCH:
return "No remembered search string.";
case REG_EDUPOPER:
return "Duplication operator in illegal position.";
case REG_ENOEXPR:
return "No expression within ( ) or on one side of an |.";
default:
return "regerrmsg: INVALID ERRORCODE";
}
}
#endif
/* 'goto' a cell containing a matching string */
void
str_search(s)
char *s;
{
register struct ent *p;
register int r,c;
int endr, endc;
#if defined(RE_COMP) || defined(REGCMP)
char *tmp;
#endif
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
int status;
regex_t preg;
if ((status = regcomp(&preg, s, 0)) != 0) {
scxfree(s);
cellerror = CELLERROR;
scerror(regerrmsg(status));
return;
}
#endif
#if defined(RE_COMP)
if ((tmp = re_comp(s)) != (char *)0) {
scxfree(s);
scerror(tmp);
return;
}
#endif
#if defined(REGCMP)
if ((tmp = regcmp(s, (char *)0)) == (char *)0) {
scxfree(s);
cellerror = CELLERROR;
scerror("Invalid search string");
return;
}
#endif
g_free();
gs.g_type = G_STR;
gs.g_s = s;
if (currow > maxrow)
endr = maxrow ? maxrow-1 : 0;
else
endr = currow;
if (curcol > maxcol)
endc = maxcol ? maxcol-1 : 0;
else
endc = curcol;
r = endr;
c = endc;
do {
if (c < maxcol)
c++;
else {
if (r < maxrow) {
while(++r < maxrow && row_hidden[r]) /* */;
c = 0;
} else {
r = 0;
c = 0;
}
}
if (r == endr && c == endc) {
scerror("String not found");
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
regfree(&preg);
#elif defined(REGCMP)
free(tmp);
#endif
return;
}
p = *ATBL(tbl, r, c);
} while(col_hidden[c] || !p || !(p->label)
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
|| (regexec(&preg, p->label, (size_t)0, NULL, 0) != 0));
#elif defined(RE_COMP)
|| (re_exec(p->label) == 0));
#elif defined(REGCMP)
|| (regex(tmp, p->label) == (char *)0));
#else
|| (strcmp(s, p->label) != 0));
#endif
currow = r;
curcol = c;
#if defined(REG_COMP) /* Peter Doemel, 10-Feb-1993 */
regfree(&preg);
#elif defined(REGCMP)
free(tmp);
#endif
}
/* fill a range with constants */
void
fill (v1, v2, start, inc)
struct ent *v1, *v2;
double start, inc;
{
register r,c;
register struct ent *n;
int maxr, maxc;
int minr, minc;
maxr = v2->row;
maxc = v2->col;
minr = v1->row;
minc = v1->col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
checkbounds(&maxr, &maxc);
if (minr < 0) minr = 0;
if (minc < 0) minc = 0;
FullUpdate++;
flush_saved();
/* save the contents */
/* erase_and_pull(minr, minc, maxr, maxc, 0, 1);*/
if( calc_order == BYROWS ) {
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++) {
n = lookat (r, c);
if (n->flags&is_locked) continue;
(void) clearent(n);
n->v = start;
start += inc;
n->flags |= (is_changed|is_valid);
}
}
else if ( calc_order == BYCOLS ) {
for (c = minc; c<=maxc; c++)
for (r = minr; r<=maxr; r++) {
n = lookat (r, c);
(void) clearent(n);
n->v = start;
start += inc;
n->flags |= (is_changed|is_valid);
}
}
else scerror(" Internal error calc_order");
sync_refs();
changed++;
}
/* lock a range of cells */
void
lock_cells (v1, v2)
struct ent *v1, *v2;
{
register r,c;
register struct ent *n;
int maxr, maxc;
int minr, minc;
maxr = v2->row;
maxc = v2->col;
minr = v1->row;
minc = v1->col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
checkbounds(&maxr, &maxc);
if (minr < 0) minr = 0;
if (minc < 0) minc = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++) {
n = lookat (r, c);
n->flags |= is_locked;
}
}
/* unlock a range of cells */
void
unlock_cells (v1, v2)
struct ent *v1, *v2;
{
register r,c;
register struct ent *n;
int maxr, maxc;
int minr, minc;
maxr = v2->row;
maxc = v2->col;
minr = v1->row;
minc = v1->col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
checkbounds(&maxr, &maxc);
if (minr < 0) minr = 0;
if (minc < 0) minc = 0;
for (r = minr; r<=maxr; r++)
for (c = minc; c<=maxc; c++) {
n = lookat (r, c);
n->flags &= ~is_locked;
}
}
/* set the numeric part of a cell */
void
let (v, e)
struct ent *v;
struct enode *e;
{
double val;
unsigned isconstant = constant(e);
if (loading && !isconstant)
val = (double)0.0;
else
{
exprerr = 0;
(void) signal(SIGFPE, eval_fpe);
if (setjmp(fpe_save)) {
sprintf(stringbuf, "Floating point exception in cell %s", v_name(v->row, v->col));
scerror(stringbuf);
val = (double)0.0;
cellerror = CELLERROR;
} else {
cellerror = CELLOK;
val = eval(e);
}
if (v->cellerror != cellerror)
{ v->flags |= is_changed;
changed++; modflg++;
FullUpdate++;
v->cellerror = cellerror;
}
(void) signal(SIGFPE, doquit);
if (exprerr) {
efree(e);
return;
}
}
if (isconstant) {
/* prescale input unless it has a decimal */
#if defined(IEEE_MATH) && !defined(NO_FMOD)
if (!loading && (prescale < (double)0.9999999) &&
(fmod(val, (double)1.0) == (double)0))
#else
if (!loading && (prescale < (double)0.9999999) &&
((val - floor(val)) == (double)0))
#endif
val *= prescale;
v->v = val;
if (!(v->flags & is_strexpr)) {
efree(v->expr);
v->expr = (struct enode *)0;
}
efree(e);
}
else
{
efree(v->expr);
v->expr = e;
v->flags &= ~is_strexpr;
}
changed++; modflg++;
v->flags |= (is_changed|is_valid);
}
void
slet (v, se, flushdir)
struct ent *v;
struct enode *se;
int flushdir;
{
char *p;
exprerr = 0;
(void) signal(SIGFPE, eval_fpe);
if (setjmp(fpe_save)) {
sprintf(stringbuf, "Floating point exception in cell %s", v_name(v->row, v->col));
scerror(stringbuf);
cellerror = CELLERROR;
p = "";
} else {
cellerror = CELLOK;
p = seval(se);
}
if (v->cellerror != cellerror)
{ v->flags |= is_changed;
changed++; modflg++;
FullUpdate++;
v->cellerror = cellerror;
}
(void) signal(SIGFPE, doquit);
if (exprerr) {
efree(se);
return;
}
if (constant(se)) {
label(v, p, flushdir);
if (p)
scxfree(p);
efree(se);
if (v->flags & is_strexpr) {
efree(v->expr);
v->expr = (struct enode *)0;
v->flags &= ~is_strexpr;
}
return;
}
efree(v->expr);
v->expr = se;
v->flags |= (is_changed|is_strexpr);
if (flushdir<0) v->flags |= is_leftflush;
if (flushdir==0)
v->flags |= is_label;
else v->flags &= ~is_label;
FullUpdate++;
changed++;
modflg++;
}
void
format_cell(v1, v2, s)
struct ent *v1, *v2;
char *s;
{
register r,c;
register struct ent *n;
int maxr, maxc;
int minr, minc;
maxr = v2->row;
maxc = v2->col;
minr = v1->row;
minc = v1->col;
if (minr>maxr) r = maxr, maxr = minr, minr = r;
if (minc>maxc) c = maxc, maxc = minc, minc = c;
checkbounds(&maxr, &maxc);
if (minr < 0) minr = 0;
if (minc < 0) minc = 0;
FullUpdate++;
modflg++;
for (r = minr; r <= maxr; r++)
for (c = minc; c <= maxc; c++) {
n = lookat (r, c);
if (n->flags&is_locked) {
sprintf(stringbuf, "Cell %s%d is locked", coltoa(n->col), n->row);
scerror(stringbuf);
continue;
}
if (n->format)
scxfree(n->format);
n->format = 0;
if (s && *s != '\0')
n->format = strcpy(scxmalloc((unsigned)(strlen(s)+1)), s);
n->flags |= is_changed;
}
}
void
hide_row(arg)
int arg;
{
if (arg < 0) {
scerror("Invalid Range");
return;
}
if (arg >= maxrows-1)
{
if (!growtbl(GROWROW, arg+1, 0))
{ scerror("You can't hide the last row");
return;
}
}
FullUpdate++;
row_hidden[arg] = 1;
}
void
hide_col(arg)
int arg;
{
if (arg < 0) {
scerror("Invalid Range");
return;
}
if (arg >= maxcols-1)
{ if ((arg >= ABSMAXCOLS-1) || !growtbl(GROWCOL, 0, arg+1))
{ scerror("You can't hide the last col");
return;
}
}
FullUpdate++;
col_hidden[arg] = TRUE;
}
void
clearent (v)
struct ent *v;
{
if (!v)
return;
label(v,"",-1);
v->v = (double)0;
if (v->expr)
efree(v->expr);
v->expr = (struct enode *)0;
if (v->format)
scxfree(v->format);
v->format = (char *)0;
v->flags |= (is_changed);
v->flags &= ~(is_valid);
changed++;
modflg++;
}
/*
* Say if an expression is a constant (return 1) or not.
*/
static int /* static added by Bob Parbs 12-92 */
constant (e)
register struct enode *e;
{
return (
e == (struct enode *)0
|| e -> op == O_CONST
|| e -> op == O_SCONST
|| (
e -> op != O_VAR
&& (e -> op & REDUCE) != REDUCE
&& constant (e -> e.o.left)
&& constant (e -> e.o.right)
&& e -> op != EXT /* functions look like constants but aren't */
&& e -> op != NVAL
&& e -> op != SVAL
&& e -> op != NOW
&& e -> op != MYROW
&& e -> op != MYCOL
&& e -> op != NUMITER
)
);
}
void
efree (e)
struct enode *e;
{
if (e) {
if (e->op != O_VAR && e->op !=O_CONST && e->op != O_SCONST
&& (e->op & REDUCE) != REDUCE) {
efree(e->e.o.left);
efree(e->e.o.right);
}
if (e->op == O_SCONST && e->e.s)
scxfree(e->e.s);
e->e.o.left = freeenodes;
freeenodes = e;
}
}
void
label (v, s, flushdir)
register struct ent *v;
register char *s;
int flushdir;
{
if (v) {
if (flushdir==0 && v->flags&is_valid) {
register struct ent *tv;
if (v->col>0 && ((tv=lookat(v->row,v->col-1))->flags&is_valid)==0)
v = tv, flushdir = 1;
else if (((tv=lookat (v->row,v->col+1))->flags&is_valid)==0)
v = tv, flushdir = -1;
else flushdir = -1;
}
if (v->label) scxfree((char *)(v->label));
if (s && s[0]) {
v->label = scxmalloc ((unsigned)(strlen(s)+1));
(void) strcpy (v->label, s);
} else
v->label = (char *)0;
if (flushdir<0) v->flags |= is_leftflush;
else v->flags &= ~is_leftflush;
if (flushdir==0) v->flags |= is_label;
else v->flags &= ~is_label;
FullUpdate++;
modflg++;
}
}
static void
decodev (v)
struct ent_ptr v;
{
register struct range *r;
if (!v.vp) (void) sprintf (line+linelim,"VAR?");
else if ((r = find_range((char *)0, 0, v.vp, v.vp)) && !r->r_is_range)
(void) sprintf(line+linelim, "%s", r->r_name);
else
(void) sprintf (line+linelim, "%s%s%s%d",
v.vf & FIX_COL ? "$" : "",
coltoa(v.vp->col),
v.vf & FIX_ROW ? "$" : "",
v.vp->row);
linelim += strlen (line+linelim);
}
char *
coltoa(col)
int col;
{
static char rname[3];
register char *p = rname;
if (col > 25) {
*p++ = col/26 + 'A' - 1;
col %= 26;
}
*p++ = col+'A';
*p = '\0';
return(rname);
}
/*
* To make list elements come out in the same order
* they were entered, we must do a depth-first eval
* of the ELIST tree
*/
static void
decompile_list(p)
struct enode *p;
{
if (!p) return;
decompile_list(p->e.o.left); /* depth first */
decompile(p->e.o.right, 0);
line[linelim++] = ',';
}
static void /* static added by Bob Parbs 12-92 */
decompile(e, priority)
register struct enode *e;
int priority;
{
register char *s;
if (e) {
int mypriority;
switch (e->op) {
default: mypriority = 99; break;
case '?': mypriority = 1; break;
case ':': mypriority = 2; break;
case '|': mypriority = 3; break;
case '&': mypriority = 4; break;
case '<': case '=': case '>': mypriority = 6; break;
case '+': case '-': case '#': mypriority = 8; break;
case '*': case '/': case '%': mypriority = 10; break;
case '^': mypriority = 12; break;
}
if (mypriority<priority) line[linelim++] = '(';
switch (e->op) {
case 'f': for (s="fixed "; (line[linelim++] = *s++););
linelim--;
decompile (e->e.o.right, 30);
break;
case 'm': line[linelim++] = '-';
decompile (e->e.o.right, 30);
break;
case '~': line[linelim++] = '~';
decompile (e->e.o.right, 30);
break;
case O_VAR: decodev (e->e.v);
break;
case O_CONST: (void) sprintf (line+linelim,"%.15g",e->e.k);
linelim += strlen (line+linelim);
break;
case O_SCONST: (void) sprintf (line+linelim, "\"%s\"", e->e.s);
linelim += strlen(line+linelim);
break;
case REDUCE | '+': range_arg( "@sum(", e); break;
case REDUCE | '*': range_arg( "@prod(", e); break;
case REDUCE | 'a': range_arg( "@avg(", e); break;
case REDUCE | 'c': range_arg( "@count(", e); break;
case REDUCE | 's': range_arg( "@stddev(", e); break;
case (REDUCE | IRR): range_arg("@irr(", e); break;
case (REDUCE | MAXR): range_arg( "@max(", e); break;
case (REDUCE | MINR): range_arg( "@min(", e); break;
case ABS: one_arg( "@abs(", e); break;
case ACOS: one_arg( "@acos(", e); break;
case ASIN: one_arg( "@asin(", e); break;
case ATAN: one_arg( "@atan(", e); break;
case ATAN2: two_arg( "@atan2(", e); break;
case CEIL: one_arg( "@ceil(", e); break;
case COS: one_arg( "@cos(", e); break;
case EXP: one_arg( "@exp(", e); break;
case FABS: one_arg( "@fabs(", e); break;
case FLOOR: one_arg( "@floor(", e); break;
case HYPOT: two_arg( "@hypot(", e); break;
case LOG: one_arg( "@ln(", e); break;
case LOG10: one_arg( "@log(", e); break;
case POW: two_arg( "@pow(", e); break;
case SIN: one_arg( "@sin(", e); break;
case SQRT: one_arg( "@sqrt(", e); break;
case TAN: one_arg( "@tan(", e); break;
case DTR: one_arg( "@dtr(", e); break;
case RTD: one_arg( "@rtd(", e); break;
case RND: one_arg( "@rnd(", e); break;
case ROUND: two_arg( "@round(", e); break;
case HOUR: one_arg( "@hour(", e); break;
case MINUTE: one_arg( "@minute(", e); break;
case SECOND: one_arg( "@second(", e); break;
case MONTH: one_arg( "@month(", e); break;
case DAY: one_arg( "@day(", e); break;
case YEAR: one_arg( "@year(", e); break;
case DATE: one_arg( "@date(", e); break;
case UPPER: one_arg( "@upper(", e); break;
case LOWER: one_arg( "@lower(", e); break;
case CAPITAL: one_arg( "@capital(", e); break;
case DTS: three_arg( "@dts(", e); break;
case TTS: three_arg( "@tts(", e); break;
case STON: one_arg( "@ston(", e); break;
case FMT: two_arg( "@fmt(", e); break;
case EQS: two_arg( "@eqs(", e); break;
case NOW: for ( s = "@now"; (line[linelim++] = *s++););
linelim--;
break;
case LMAX: list_arg("@max(", e); break;
case LMIN: list_arg("@min(", e); break;
case FV: three_arg("@fv(", e); break;
case PV: three_arg("@pv(", e); break;
case PMT: three_arg("@pmt(", e); break;
case NVAL: two_arg("@nval(", e); break;
case SVAL: two_arg("@sval(", e); break;
case EXT: two_arg("@ext(", e); break;
case SUBSTR: three_arg("@substr(", e); break;
case STINDEX: index_arg("@stindex(", e); break;
case INDEX: index_arg("@index(", e); break;
case LOOKUP: index_arg("@lookup(", e); break;
case HLOOKUP: two_arg_index("@hlookup(", e); break;
case VLOOKUP: two_arg_index("@vlookup(", e); break;
case IF: three_arg("@if(", e); break;
case MYROW: for ( s = "@myrow"; (line[linelim++] = *s++););
linelim--;
break;
case MYCOL: for ( s = "@mycol"; (line[linelim++] = *s++););
linelim--;
break;
case COLTOA: one_arg( "@coltoa(", e); break;
case NUMITER: for ( s = "@numiter"; (line[linelim++] = *s++););
linelim--;
break;
/* FIXME-test this code (also what is the matrix priority (in gram.y) */
case MATRIX_ADD: three_arg("@matrix_add(", e); break;
case MATRIX_SUB: three_arg("@matrix_sub(", e); break;
case MATRIX_INV: two_arg("@matrix_inv(", e); break;
case MATRIX_MULT: three_arg("@matrix_mult(", e); break;
case MATRIX_TRANS: two_arg("@matrix_trans(", e); break;
case IRR:
scerror("IRR");
default: decompile (e->e.o.left, mypriority);
line[linelim++] = e->op;
decompile (e->e.o.right, mypriority+1);
break;
}
if (mypriority<priority) line[linelim++] = ')';
} else line[linelim++] = '?';
}
static void
index_arg(s, e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile( e-> e.o.left, 0 );
range_arg(", ", e->e.o.right);
}
static void
two_arg_index(s, e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile( e->e.o.left->e.o.left, 0 );
range_arg(",", e->e.o.right);
linelim--;
line[linelim++] = ',';
decompile( e->e.o.left->e.o.right, 0 );
line[linelim++] = ')';
}
static void
list_arg(s, e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile (e->e.o.right, 0);
line[linelim++] = ',';
decompile_list(e->e.o.left);
line[linelim - 1] = ')';
}
static void
one_arg(s, e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile (e->e.o.right, 0);
line[linelim++] = ')';
}
static void
two_arg(s,e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile (e->e.o.left, 0);
line[linelim++] = ',';
decompile (e->e.o.right, 0);
line[linelim++] = ')';
}
static void
three_arg(s,e)
char *s;
struct enode *e;
{
for (; (line[linelim++] = *s++););
linelim--;
decompile (e->e.o.left, 0);
line[linelim++] = ',';
decompile (e->e.o.right->e.o.left, 0);
line[linelim++] = ',';
decompile (e->e.o.right->e.o.right, 0);
line[linelim++] = ')';
}
static void
range_arg(s,e)
char *s;
struct enode *e;
{
struct range *r;
for (; (line[linelim++] = *s++););
linelim--;
if ((r = find_range((char *)0, 0, e->e.r.left.vp,
e->e.r.right.vp)) && r->r_is_range) {
(void) sprintf(line+linelim, "%s", r->r_name);
linelim += strlen(line+linelim);
} else {
decodev (e->e.r.left);
line[linelim++] = ':';
decodev (e->e.r.right);
}
line[linelim++] = ')';
}
void
editfmt (row, col)
int row, col;
{
register struct ent *p;
p = lookat (row, col);
if (p->format) {
(void) sprintf (line, "fmt %s \"%s\"", v_name(row, col), p->format);
linelim = strlen(line);
}
}
void
editv (row, col)
int row, col;
{
register struct ent *p;
p = lookat (row, col);
(void) sprintf (line, "let %s = ", v_name(row, col));
linelim = strlen(line);
if (p->flags & is_strexpr || p->expr == 0) {
(void) sprintf (line+linelim, "%.15g", p->v);
linelim += strlen (line+linelim);
} else {
editexp(row,col);
}
}
void
editexp(row,col)
int row, col;
{
register struct ent *p;
p = lookat (row, col);
decompile (p->expr, 0);
line[linelim] = '\0';
}
void
edits (row, col)
int row, col;
{
register struct ent *p;
p = lookat (row, col);
if( p->flags&is_label )
(void) sprintf( line, "label %s = ", v_name(row, col));
else
(void) sprintf (line, "%sstring %s = ",
((p->flags&is_leftflush) ? "left" : "right"),
v_name(row, col));
linelim = strlen(line);
if (p->flags & is_strexpr && p->expr) {
editexp(row, col);
} else if (p->label) {
(void) sprintf (line+linelim, "\"%s\"", p->label);
linelim += strlen (line+linelim);
} else {
(void) sprintf (line+linelim, "\"");
linelim += 1;
}
}
|