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
|
/*
* eval.c - gawk parse tree interpreter
*/
/*
* Copyright (C) 1986, 1988, 1989, 1991-2007 the Free Software Foundation, Inc.
*
* This file is part of GAWK, the GNU implementation of the
* AWK Programming Language.
*
* GAWK is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* GAWK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "awk.h"
extern double pow P((double x, double y));
extern double modf P((double x, double *yp));
extern double fmod P((double x, double y));
static inline void make_scalar P((NODE *tree));
static int eval_condition P((NODE *tree));
static NODE *op_assign P((NODE *tree));
static NODE *func_call P((NODE *tree));
static NODE *match_op P((NODE *tree));
static void pop_forloop P((void));
static inline void pop_all_forloops P((void));
static void push_forloop P((const char *varname, NODE **elems, size_t nelems));
static void push_args P((int count, NODE *arglist, NODE **oldstack,
const char *func_name, char **varnames));
static inline void pop_fcall_stack P((void));
static void pop_fcall P((void));
static int comp_func P((const void *p1, const void *p2));
#if __GNUC__ < 2
NODE *_t; /* used as a temporary in macros */
#endif
#ifdef MSDOS
double _msc51bug; /* to get around a bug in MSC 5.1 */
#endif
NODE *ret_node;
int OFSlen;
int ORSlen;
int OFMTidx;
int CONVFMTidx;
/* Profiling stuff */
#ifdef PROFILING
#define INCREMENT(n) n++
#else
#define INCREMENT(n) /* nothing */
#endif
/* Macros and variables to save and restore function and loop bindings */
/*
* the val variable allows return/continue/break-out-of-context to be
* caught and diagnosed
*/
#define PUSH_BINDING(stack, x, val) (memcpy((char *)(stack), (const char *)(x), sizeof(jmp_buf)), val++)
#define RESTORE_BINDING(stack, x, val) (memcpy((char *)(x), (const char *)(stack), sizeof(jmp_buf)), val--)
static jmp_buf loop_tag; /* always the current binding */
static int loop_tag_valid = FALSE; /* nonzero when loop_tag valid */
static int func_tag_valid = FALSE;
static jmp_buf func_tag;
extern int exiting, exit_val;
/* This rather ugly macro is for VMS C */
#ifdef C
#undef C
#endif
#define C(c) ((char)c)
/*
* This table is used by the regexp routines to do case independant
* matching. Basically, every ascii character maps to itself, except
* uppercase letters map to lower case ones. This table has 256
* entries, for ISO 8859-1. Note also that if the system this
* is compiled on doesn't use 7-bit ascii, casetable[] should not be
* defined to the linker, so gawk should not load.
*
* Do NOT make this array static, it is used in several spots, not
* just in this file.
*
* 6/2004:
* This table is also used for IGNORECASE for == and !=, and index().
* Although with GLIBC, we could use tolower() everywhere and RE_ICASE
* for the regex matcher, precomputing this table once gives us a
* performance improvement. I also think it's better for portability
* to non-GLIBC systems. All the world is not (yet :-) GNU/Linux.
*/
#if 'a' == 97 /* it's ascii */
char casetable[] = {
'\000', '\001', '\002', '\003', '\004', '\005', '\006', '\007',
'\010', '\011', '\012', '\013', '\014', '\015', '\016', '\017',
'\020', '\021', '\022', '\023', '\024', '\025', '\026', '\027',
'\030', '\031', '\032', '\033', '\034', '\035', '\036', '\037',
/* ' ' '!' '"' '#' '$' '%' '&' ''' */
'\040', '\041', '\042', '\043', '\044', '\045', '\046', '\047',
/* '(' ')' '*' '+' ',' '-' '.' '/' */
'\050', '\051', '\052', '\053', '\054', '\055', '\056', '\057',
/* '0' '1' '2' '3' '4' '5' '6' '7' */
'\060', '\061', '\062', '\063', '\064', '\065', '\066', '\067',
/* '8' '9' ':' ';' '<' '=' '>' '?' */
'\070', '\071', '\072', '\073', '\074', '\075', '\076', '\077',
/* '@' 'A' 'B' 'C' 'D' 'E' 'F' 'G' */
'\100', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
/* 'H' 'I' 'J' 'K' 'L' 'M' 'N' 'O' */
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
/* 'P' 'Q' 'R' 'S' 'T' 'U' 'V' 'W' */
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
/* 'X' 'Y' 'Z' '[' '\' ']' '^' '_' */
'\170', '\171', '\172', '\133', '\134', '\135', '\136', '\137',
/* '`' 'a' 'b' 'c' 'd' 'e' 'f' 'g' */
'\140', '\141', '\142', '\143', '\144', '\145', '\146', '\147',
/* 'h' 'i' 'j' 'k' 'l' 'm' 'n' 'o' */
'\150', '\151', '\152', '\153', '\154', '\155', '\156', '\157',
/* 'p' 'q' 'r' 's' 't' 'u' 'v' 'w' */
'\160', '\161', '\162', '\163', '\164', '\165', '\166', '\167',
/* 'x' 'y' 'z' '{' '|' '}' '~' */
'\170', '\171', '\172', '\173', '\174', '\175', '\176', '\177',
/* Latin 1: */
C('\200'), C('\201'), C('\202'), C('\203'), C('\204'), C('\205'), C('\206'), C('\207'),
C('\210'), C('\211'), C('\212'), C('\213'), C('\214'), C('\215'), C('\216'), C('\217'),
C('\220'), C('\221'), C('\222'), C('\223'), C('\224'), C('\225'), C('\226'), C('\227'),
C('\230'), C('\231'), C('\232'), C('\233'), C('\234'), C('\235'), C('\236'), C('\237'),
C('\240'), C('\241'), C('\242'), C('\243'), C('\244'), C('\245'), C('\246'), C('\247'),
C('\250'), C('\251'), C('\252'), C('\253'), C('\254'), C('\255'), C('\256'), C('\257'),
C('\260'), C('\261'), C('\262'), C('\263'), C('\264'), C('\265'), C('\266'), C('\267'),
C('\270'), C('\271'), C('\272'), C('\273'), C('\274'), C('\275'), C('\276'), C('\277'),
C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\327'),
C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\337'),
C('\340'), C('\341'), C('\342'), C('\343'), C('\344'), C('\345'), C('\346'), C('\347'),
C('\350'), C('\351'), C('\352'), C('\353'), C('\354'), C('\355'), C('\356'), C('\357'),
C('\360'), C('\361'), C('\362'), C('\363'), C('\364'), C('\365'), C('\366'), C('\367'),
C('\370'), C('\371'), C('\372'), C('\373'), C('\374'), C('\375'), C('\376'), C('\377'),
};
#elif 'a' == 0x81 /* it's EBCDIC */
char casetable[] = {
/*00 NU SH SX EX PF HT LC DL */
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
/*08 SM VT FF CR SO SI */
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
/*10 DE D1 D2 TM RS NL BS IL */
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
/*18 CN EM CC C1 FS GS RS US */
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
/*20 DS SS FS BP LF EB EC */
0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27,
/*28 SM C2 EQ AK BL */
0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F,
/*30 SY PN RS UC ET */
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
/*38 C3 D4 NK SU */
0x38, 0x39, 0x3A, 0x3B, 0x3C, 0x3D, 0x3E, 0x3F,
/*40 SP */
0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
/*48 CENT . < ( + | */
0x48, 0x49, 0x4A, 0x4B, 0x4C, 0x4D, 0x4E, 0x4F,
/*50 & */
0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57,
/*58 ! $ * ) ; ^ */
0x58, 0x59, 0x5A, 0x5B, 0x5C, 0x5D, 0x5E, 0x5F,
/*60 - / */
0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
/*68 | , % _ > ? */
0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F,
/*70 */
0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
/*78 ` : # @ ' = " */
0x78, 0x79, 0x7A, 0x7B, 0x7C, 0x7D, 0x7E, 0x7F,
/*80 a b c d e f g */
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
/*88 h i { */
0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E, 0x8F,
/*90 j k l m n o p */
0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
/*98 q r } */
0x98, 0x99, 0x9A, 0x9B, 0x9C, 0x9D, 0x9E, 0x9F,
/*A0 ~ s t u v w x */
0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
/*A8 y z [ */
0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE, 0xAF,
/*B0 */
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7,
/*B8 ] */
0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
/*C0 { A B C D E F G */
0xC0, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
/*C8 H I */
0x88, 0x89, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
/*D0 } J K L M N O P */
0xD0, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
/*D8 Q R */
0x98, 0x99, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
/*E0 \ S T U V W X */
0xE0, 0xE1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6, 0xA7,
/*E8 Y Z */
0xA8, 0xA9, 0xEA, 0xEB, 0xEC, 0xED, 0xEE, 0xEF,
/*F0 0 1 2 3 4 5 6 7 */
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7,
/*F8 8 9 */
0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF
};
#else
#include "You lose. You will need a translation table for your character set."
#endif
#undef C
/* load_casetable --- for a non-ASCII locale, redo the table */
void
load_casetable(void)
{
#if defined(LC_CTYPE)
int i;
char *cp;
static int loaded = FALSE;
if (loaded || do_traditional)
return;
loaded = TRUE;
cp = setlocale(LC_CTYPE, NULL);
/* this is not per standard, but it's pretty safe */
if (cp == NULL || strcmp(cp, "C") == 0 || strcmp(cp, "POSIX") == 0)
return;
#ifndef ZOS_USS
for (i = 0200; i <= 0377; i++) {
if (isalpha(i) && islower(i) && i != toupper(i))
casetable[i] = toupper(i);
}
#endif
#endif
}
/*
* This table maps node types to strings for debugging.
* KEEP IN SYNC WITH awk.h!!!!
*/
static const char *const nodetypes[] = {
"Node_illegal",
"Node_times",
"Node_quotient",
"Node_mod",
"Node_plus",
"Node_minus",
"Node_cond_pair",
"Node_subscript",
"Node_concat",
"Node_exp",
"Node_preincrement",
"Node_predecrement",
"Node_postincrement",
"Node_postdecrement",
"Node_unary_minus",
"Node_field_spec",
"Node_assign",
"Node_assign_times",
"Node_assign_quotient",
"Node_assign_mod",
"Node_assign_plus",
"Node_assign_minus",
"Node_assign_exp",
"Node_assign_concat",
"Node_and",
"Node_or",
"Node_equal",
"Node_notequal",
"Node_less",
"Node_greater",
"Node_leq",
"Node_geq",
"Node_match",
"Node_nomatch",
"Node_not",
"Node_rule_list",
"Node_rule_node",
"Node_statement_list",
"Node_switch_body",
"Node_case_list",
"Node_if_branches",
"Node_expression_list",
"Node_param_list",
"Node_K_if",
"Node_K_switch",
"Node_K_case",
"Node_K_default",
"Node_K_while",
"Node_K_for",
"Node_K_arrayfor",
"Node_K_break",
"Node_K_continue",
"Node_K_print",
"Node_K_print_rec",
"Node_K_printf",
"Node_K_next",
"Node_K_exit",
"Node_K_do",
"Node_K_return",
"Node_K_delete",
"Node_K_delete_loop",
"Node_K_getline",
"Node_K_function",
"Node_K_nextfile",
"Node_redirect_output",
"Node_redirect_append",
"Node_redirect_pipe",
"Node_redirect_pipein",
"Node_redirect_input",
"Node_redirect_twoway",
"Node_var_new",
"Node_var",
"Node_var_array",
"Node_val",
"Node_builtin",
"Node_line_range",
"Node_in_array",
"Node_func",
"Node_func_call",
"Node_cond_exp",
"Node_regex",
"Node_dynregex",
"Node_hashnode",
"Node_ahash",
"Node_array_ref",
"Node_BINMODE",
"Node_CONVFMT",
"Node_FIELDWIDTHS",
"Node_FNR",
"Node_FS",
"Node_IGNORECASE",
"Node_LINT",
"Node_NF",
"Node_NR",
"Node_OFMT",
"Node_OFS",
"Node_ORS",
"Node_RS",
"Node_SUBSEP",
"Node_TEXTDOMAIN",
"Node_final --- this should never appear",
NULL
};
/* nodetype2str --- convert a node type into a printable value */
const char *
nodetype2str(NODETYPE type)
{
static char buf[40];
if (type >= Node_illegal && type <= Node_final)
return nodetypes[(int) type];
sprintf(buf, _("unknown nodetype %d"), (int) type);
return buf;
}
/* flags2str --- make a flags value readable */
const char *
flags2str(int flagval)
{
static const struct flagtab values[] = {
{ MALLOC, "MALLOC" },
{ TEMP, "TEMP" },
{ PERM, "PERM" },
{ STRING, "STRING" },
{ STRCUR, "STRCUR" },
{ NUMCUR, "NUMCUR" },
{ NUMBER, "NUMBER" },
{ MAYBE_NUM, "MAYBE_NUM" },
{ ARRAYMAXED, "ARRAYMAXED" },
{ FUNC, "FUNC" },
{ FIELD, "FIELD" },
{ INTLSTR, "INTLSTR" },
#ifdef WSTRCUR
{ WSTRCUR, "WSTRCUR" },
#endif
{ 0, NULL },
};
return genflags2str(flagval, values);
}
/* genflags2str --- general routine to convert a flag value to a string */
const char *
genflags2str(int flagval, const struct flagtab *tab)
{
static char buffer[BUFSIZ];
char *sp;
int i, space_left, space_needed;
sp = buffer;
space_left = BUFSIZ;
for (i = 0; tab[i].name != NULL; i++) {
if ((flagval & tab[i].val) != 0) {
/*
* note the trick, we want 1 or 0 for whether we need
* the '|' character.
*/
space_needed = (strlen(tab[i].name) + (sp != buffer));
if (space_left < space_needed)
fatal(_("buffer overflow in genflags2str"));
if (sp != buffer) {
*sp++ = '|';
space_left--;
}
strcpy(sp, tab[i].name);
/* note ordering! */
space_left -= strlen(sp);
sp += strlen(sp);
}
}
return buffer;
}
/*
* make_scalar --- make sure that tree is a scalar.
*
* tree is in a scalar context. If it is a variable, accomplish
* what's needed; otherwise, do nothing.
*
* Notice that nodes of type Node_var_new have undefined value in var_value
* (a.k.a. lnode)---even though awkgram.y:variable() initializes it,
* push_args() doesn't. Thus we have to initialize it.
*/
static inline void
make_scalar(NODE *tree)
{
switch (tree->type) {
case Node_var_array:
fatal(_("attempt to use array `%s' in a scalar context"),
array_vname(tree));
case Node_array_ref:
switch (tree->orig_array->type) {
case Node_var_array:
fatal(_("attempt to use array `%s' in a scalar context"),
array_vname(tree));
case Node_var_new:
tree->orig_array->type = Node_var;
tree->orig_array->var_value = Nnull_string;
break;
case Node_var:
break;
default:
cant_happen();
}
/* fall through */
case Node_var_new:
tree->type = Node_var;
tree->var_value = Nnull_string;
default:
/* shut up GCC */
break;
}
}
/*
* interpret:
* Tree is a bunch of rules to run. Returns zero if it hit an exit()
* statement
*/
int
interpret(register NODE *volatile tree)
{
jmp_buf volatile loop_tag_stack; /* shallow binding stack for loop_tag */
static jmp_buf rule_tag; /* tag the rule currently being run, for NEXT
* and EXIT statements. It is static because
* there are no nested rules */
register NODE *volatile t = NULL; /* temporary */
NODE **volatile lhs; /* lhs == Left Hand Side for assigns, etc */
NODE *volatile stable_tree;
int volatile traverse = TRUE; /* True => loop thru tree (Node_rule_list) */
/* avoid false source indications */
source = NULL;
sourceline = 0;
if (tree == NULL)
return 1;
sourceline = tree->source_line;
source = tree->source_file;
switch (tree->type) {
case Node_rule_node:
traverse = FALSE; /* False => one for-loop iteration only */
/* FALL THROUGH */
case Node_rule_list:
for (t = tree; t != NULL; t = t->rnode) {
if (traverse)
tree = t->lnode;
sourceline = tree->source_line;
source = tree->source_file;
INCREMENT(tree->exec_count);
switch (setjmp(rule_tag)) {
case 0: /* normal non-jump */
/* test pattern, if any */
if (tree->lnode == NULL ||
eval_condition(tree->lnode)) {
/* using the lnode exec_count is kludgey */
if (tree->lnode != NULL)
INCREMENT(tree->lnode->exec_count);
(void) interpret(tree->rnode);
}
break;
case TAG_CONTINUE: /* NEXT statement */
pop_all_forloops();
pop_fcall_stack();
return 1;
case TAG_BREAK: /* EXIT statement */
pop_all_forloops();
pop_fcall_stack();
return 0;
default:
cant_happen();
}
if (! traverse) /* case Node_rule_node */
break; /* don't loop */
}
break;
case Node_statement_list:
for (t = tree; t != NULL; t = t->rnode)
(void) interpret(t->lnode);
break;
case Node_K_if:
INCREMENT(tree->exec_count);
if (eval_condition(tree->lnode)) {
INCREMENT(tree->rnode->exec_count);
(void) interpret(tree->rnode->lnode);
} else {
(void) interpret(tree->rnode->rnode);
}
break;
case Node_K_switch:
{
NODE *switch_value;
NODE *switch_body;
NODE *case_list;
NODE *default_list;
NODE *case_stmt;
int match_found = FALSE;
PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
INCREMENT(tree->exec_count);
stable_tree = tree;
switch_value = tree_eval(stable_tree->lnode);
switch_body = stable_tree->rnode;
case_list = switch_body->lnode;
default_list = switch_body->rnode;
for (; case_list != NULL; case_list = case_list->rnode) {
case_stmt = case_list->lnode;
/*
* Once a match is found, all cases will be processed as they fall through,
* so continue to execute statements until a break is reached.
*/
if (! match_found) {
if (case_stmt->type == Node_K_default)
; /* do nothing */
else if (case_stmt->lnode->type == Node_regex) {
NODE *t1;
Regexp *rp;
/* see comments in match_op() code about this. */
int kludge_need_start = 0;
t1 = force_string(switch_value);
rp = re_update(case_stmt->lnode);
if (avoid_dfa(tree, t1->stptr, t1->stlen))
kludge_need_start = RE_NEED_START;
match_found = (research(rp, t1->stptr, 0, t1->stlen, kludge_need_start) >= 0);
if (t1 != switch_value)
free_temp(t1);
} else
match_found = (cmp_nodes(switch_value, case_stmt->lnode) == 0);
}
/* If a match was found, execute the statements associated with the case. */
if (match_found) {
INCREMENT(case_stmt->exec_count);
switch (setjmp(loop_tag)) {
case 0: /* Normal non-jump */
(void) interpret(case_stmt->rnode);
break;
case TAG_CONTINUE: /* continue statement */
free_temp(switch_value);
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
longjmp(loop_tag, TAG_CONTINUE);
break;
case TAG_BREAK: /* break statement */
free_temp(switch_value);
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
return 1;
default:
cant_happen();
}
}
}
free_temp(switch_value);
/*
* If a default section was found, execute the statements associated with it
* and execute any trailing case statements if the default falls through.
*/
if (! match_found && default_list != NULL) {
for (case_list = default_list;
case_list != NULL; case_list = case_list->rnode) {
case_stmt = case_list->lnode;
INCREMENT(case_stmt->exec_count);
switch (setjmp(loop_tag)) {
case 0: /* Normal non-jump */
(void) interpret(case_stmt->rnode);
break;
case TAG_CONTINUE: /* continue statement */
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
longjmp(loop_tag, TAG_CONTINUE);
break;
case TAG_BREAK: /* break statement */
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
return 1;
default:
cant_happen();
}
}
}
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
}
break;
case Node_K_while:
PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
stable_tree = tree;
while (eval_condition(stable_tree->lnode)) {
INCREMENT(stable_tree->exec_count);
switch (setjmp(loop_tag)) {
case 0: /* normal non-jump */
(void) interpret(stable_tree->rnode);
break;
case TAG_CONTINUE: /* continue statement */
break;
case TAG_BREAK: /* break statement */
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
return 1;
default:
cant_happen();
}
}
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
break;
case Node_K_do:
PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
stable_tree = tree;
do {
INCREMENT(stable_tree->exec_count);
switch (setjmp(loop_tag)) {
case 0: /* normal non-jump */
(void) interpret(stable_tree->rnode);
break;
case TAG_CONTINUE: /* continue statement */
break;
case TAG_BREAK: /* break statement */
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
return 1;
default:
cant_happen();
}
} while (eval_condition(stable_tree->lnode));
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
break;
case Node_K_for:
PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
(void) interpret(tree->forloop->init);
stable_tree = tree;
while (eval_condition(stable_tree->forloop->cond)) {
INCREMENT(stable_tree->exec_count);
switch (setjmp(loop_tag)) {
case 0: /* normal non-jump */
(void) interpret(stable_tree->lnode);
/* fall through */
case TAG_CONTINUE: /* continue statement */
(void) interpret(stable_tree->forloop->incr);
break;
case TAG_BREAK: /* break statement */
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
return 1;
default:
cant_happen();
}
}
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
break;
case Node_K_arrayfor:
{
Func_ptr after_assign = NULL;
NODE **list = NULL;
NODE *volatile array;
NODE *volatile save_array;
volatile size_t i, num_elems;
size_t j;
volatile int retval = 0;
int sort_indices = whiny_users;
#define hakvar forloop->init
#define arrvar forloop->incr
/* get the array */
save_array = tree->arrvar;
array = get_array(save_array);
/* sanity: do nothing if empty */
if (array->var_array == NULL || array->table_size == 0)
break; /* from switch */
/* allocate space for array */
num_elems = array->table_size;
emalloc(list, NODE **, num_elems * sizeof(NODE *), "for_loop");
/* populate it */
for (i = j = 0; i < array->array_size; i++) {
NODE *t = array->var_array[i];
if (t == NULL)
continue;
for (; t != NULL; t = t->ahnext) {
list[j++] = dupnode(t);
assert(list[j-1] == t);
}
}
if (sort_indices)
qsort(list, num_elems, sizeof(NODE *), comp_func); /* shazzam! */
/* now we can run the loop */
push_forloop(array->vname, list, num_elems);
PUSH_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
lhs = get_lhs(tree->hakvar, &after_assign, FALSE);
stable_tree = tree;
for (i = 0; i < num_elems; i++) {
INCREMENT(stable_tree->exec_count);
unref(*((NODE **) lhs));
*lhs = make_string(list[i]->ahname_str, list[i]->ahname_len);
if (after_assign)
(*after_assign)();
switch (setjmp(loop_tag)) {
case 0:
(void) interpret(stable_tree->lnode);
case TAG_CONTINUE:
break;
case TAG_BREAK:
retval = 1;
goto done;
default:
cant_happen();
}
}
done:
RESTORE_BINDING(loop_tag_stack, loop_tag, loop_tag_valid);
pop_forloop();
if (do_lint && num_elems != array->table_size)
lintwarn(_("for loop: array `%s' changed size from %ld to %ld during loop execution"),
array_vname(save_array), (long) num_elems, (long) array->table_size);
if (retval == 1)
return 1;
break;
}
#undef hakvar
#undef arrvar
case Node_K_break:
INCREMENT(tree->exec_count);
if (! loop_tag_valid) {
/*
* Old AT&T nawk treats break outside of loops like
* next. New ones catch it at parse time. Allow it if
* do_traditional is on, and complain if lint.
*/
static short warned = FALSE;
if (do_lint && ! warned) {
lintwarn(_("`break' outside a loop is not portable"));
warned = TRUE;
}
if (! do_traditional || do_posix)
fatal(_("`break' outside a loop is not allowed"));
longjmp(rule_tag, TAG_CONTINUE);
} else
longjmp(loop_tag, TAG_BREAK);
break;
case Node_K_continue:
INCREMENT(tree->exec_count);
if (! loop_tag_valid) {
/*
* Old AT&T nawk treats continue outside of loops like
* next. New ones catch it at parse time. Allow it if
* do_traditional is on, and complain if lint.
*/
static short warned = FALSE;
if (do_lint && ! warned) {
lintwarn(_("`continue' outside a loop is not portable"));
warned = TRUE;
}
if (! do_traditional || do_posix)
fatal(_("`continue' outside a loop is not allowed"));
longjmp(rule_tag, TAG_CONTINUE);
} else
longjmp(loop_tag, TAG_CONTINUE);
break;
case Node_K_print:
INCREMENT(tree->exec_count);
do_print(tree);
break;
case Node_K_print_rec:
INCREMENT(tree->exec_count);
do_print_rec(tree);
break;
case Node_K_printf:
INCREMENT(tree->exec_count);
do_printf(tree);
break;
case Node_K_delete:
INCREMENT(tree->exec_count);
do_delete(tree->lnode, tree->rnode);
break;
case Node_K_delete_loop:
INCREMENT(tree->exec_count);
do_delete_loop(tree->lnode, tree->rnode);
break;
case Node_K_next:
INCREMENT(tree->exec_count);
if (in_begin_rule)
fatal(_("`next' cannot be called from a BEGIN rule"));
else if (in_end_rule)
fatal(_("`next' cannot be called from an END rule"));
/* could add a lint check here for in a loop or function */
longjmp(rule_tag, TAG_CONTINUE);
break;
case Node_K_nextfile:
INCREMENT(tree->exec_count);
if (in_begin_rule)
fatal(_("`nextfile' cannot be called from a BEGIN rule"));
else if (in_end_rule)
fatal(_("`nextfile' cannot be called from an END rule"));
/* could add a lint check here for in a loop or function */
/*
* Have to do this cleanup here, since we don't longjump
* back to the main awk rule loop (rule_tag).
*/
pop_all_forloops();
pop_fcall_stack();
do_nextfile();
break;
case Node_K_exit:
INCREMENT(tree->exec_count);
/*
* In A,K,&W, p. 49, it says that an exit statement "...
* causes the program to behave as if the end of input had
* occurred; no more input is read, and the END actions, if
* any are executed." This implies that the rest of the rules
* are not done. So we immediately break out of the main loop.
*/
exiting = TRUE;
if (tree->lnode != NULL) {
t = tree_eval(tree->lnode);
exit_val = (int) force_number(t);
#ifdef VMS
if (exit_val == 0)
exit_val = EXIT_SUCCESS;
else if (exit_val == 1)
exit_val = EXIT_FAILURE;
/* else
just pass anything else on through */
#endif
free_temp(t);
}
longjmp(rule_tag, TAG_BREAK);
break;
case Node_K_return:
INCREMENT(tree->exec_count);
t = tree_eval(tree->lnode);
if ((t->flags & (PERM|TEMP)) != 0)
ret_node = t;
else {
ret_node = copynode(t); /* don't do a dupnode here */
ret_node->flags |= TEMP;
}
longjmp(func_tag, TAG_RETURN);
break;
default:
/*
* Appears to be an expression statement. Throw away the
* value.
*/
if (do_lint && (tree->type == Node_var || tree->type == Node_var_new))
lintwarn(_("statement has no effect"));
INCREMENT(tree->exec_count);
t = tree_eval(tree);
if (t) /* stopme() returns NULL */
free_temp(t);
break;
}
return 1;
}
/*
* calc_exp_posint --- calculate x^n for positive integral n,
* using exponentiation by squaring without recursion.
*/
static AWKNUM
calc_exp_posint(AWKNUM x, long n)
{
AWKNUM mult = 1;
while (n > 1) {
if ((n % 2) == 1)
mult *= x;
x *= x;
n /= 2;
}
return mult * x;
}
/* calc_exp --- calculate x1^x2 */
AWKNUM
calc_exp(AWKNUM x1, AWKNUM x2)
{
long lx;
if ((lx = x2) == x2) { /* integer exponent */
if (lx == 0)
return 1;
return (lx > 0) ? calc_exp_posint(x1, lx)
: 1.0 / calc_exp_posint(x1, -lx);
}
return (AWKNUM) pow((double) x1, (double) x2);
}
/* r_tree_eval --- evaluate a subtree */
NODE *
r_tree_eval(register NODE *tree, int iscond)
{
register NODE *r, *t1, *t2; /* return value & temporary subtrees */
register NODE **lhs;
register int di;
AWKNUM x, x1, x2;
#ifdef _CRAY
long lx2;
#endif
#ifndef TREE_EVAL_MACRO
if (tree == NULL)
cant_happen();
if (tree->type == Node_val) {
if (tree->stref <= 0)
cant_happen();
return ((tree->flags & INTLSTR) != 0
? r_force_string(tree)
: tree);
} else if (tree->type == Node_var) {
if (tree->var_value->stref <= 0)
cant_happen();
if (! var_uninitialized(tree))
return tree->var_value;
}
#endif
if (tree->type == Node_param_list) {
if ((tree->flags & FUNC) != 0)
fatal(_("can't use function name `%s' as variable or array"),
tree->vname);
tree = stack_ptr[tree->param_cnt];
if (tree == NULL) {
if (do_lint)
lintwarn(_("reference to uninitialized argument `%s'"),
tree->vname);
return Nnull_string;
}
if (do_lint && var_uninitialized(tree))
lintwarn(_("reference to uninitialized argument `%s'"),
tree->vname);
}
make_scalar(tree);
switch (tree->type) {
case Node_var:
if (do_lint && var_uninitialized(tree))
lintwarn(_("reference to uninitialized variable `%s'"),
tree->vname);
return tree->var_value;
case Node_and:
return tmp_number((AWKNUM) (eval_condition(tree->lnode)
&& eval_condition(tree->rnode)));
case Node_or:
return tmp_number((AWKNUM) (eval_condition(tree->lnode)
|| eval_condition(tree->rnode)));
case Node_not:
return tmp_number((AWKNUM) ! eval_condition(tree->lnode));
/* Builtins */
case Node_builtin:
return (*tree->builtin)(tree->subnode);
case Node_K_getline:
return do_getline(tree);
case Node_in_array:
return tmp_number((AWKNUM) (in_array(tree->lnode, tree->rnode) != NULL));
case Node_func_call:
return func_call(tree);
/* unary operations */
case Node_NR:
case Node_FNR:
case Node_NF:
case Node_FIELDWIDTHS:
case Node_FS:
case Node_RS:
case Node_field_spec:
case Node_subscript:
case Node_IGNORECASE:
case Node_OFS:
case Node_ORS:
case Node_OFMT:
case Node_CONVFMT:
case Node_BINMODE:
case Node_LINT:
case Node_SUBSEP:
case Node_TEXTDOMAIN:
lhs = get_lhs(tree, (Func_ptr *) NULL, TRUE);
return *lhs;
case Node_unary_minus:
t1 = tree_eval(tree->subnode);
x = -force_number(t1);
free_temp(t1);
return tmp_number(x);
case Node_cond_exp:
if (eval_condition(tree->lnode))
return tree_eval(tree->rnode->lnode);
return tree_eval(tree->rnode->rnode);
case Node_match:
case Node_nomatch:
case Node_regex:
case Node_dynregex:
return match_op(tree);
case Node_concat:
{
NODE **treelist;
NODE **strlist;
NODE *save_tree;
register NODE **treep;
register NODE **strp;
register size_t len;
register size_t supposed_len;
char *str;
register char *dest;
int alloc_count, str_count;
int i;
/*
* This is an efficiency hack for multiple adjacent string
* concatenations, to avoid recursion and string copies.
*
* Node_concat trees grow downward to the left, so
* descend to lowest (first) node, accumulating nodes
* to evaluate to strings as we go.
*/
/*
* But first, no arbitrary limits. Count the number of
* nodes and malloc the treelist and strlist arrays.
* There will be alloc_count + 1 items to concatenate. We
* also leave room for an extra pointer at the end to
* use as a sentinel. Thus, start alloc_count at 2.
*/
save_tree = tree;
for (alloc_count = 2; tree != NULL && tree->type == Node_concat;
tree = tree->lnode)
alloc_count++;
tree = save_tree;
emalloc(treelist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
emalloc(strlist, NODE **, sizeof(NODE *) * alloc_count, "tree_eval");
/* Now, here we go. */
treep = treelist;
while (tree != NULL && tree->type == Node_concat) {
*treep++ = tree->rnode;
tree = tree->lnode;
}
*treep = tree;
/*
* Now, evaluate to strings in LIFO order, accumulating
* the string length, so we can do a single malloc at the
* end.
*
* Evaluate the expressions first, then get their
* lengthes, in case one of the expressions has a
* side effect that changes one of the others.
* See test/nasty.awk.
*
* dupnode the results a la do_print, to give us
* more predicable behavior; compare gawk 3.0.6 to
* nawk/mawk on test/nasty.awk.
*/
strp = strlist;
supposed_len = len = 0;
while (treep >= treelist) {
NODE *n;
/* Here lies the wumpus's brother. R.I.P. */
n = force_string(tree_eval(*treep--));
*strp = dupnode(n);
free_temp(n);
supposed_len += (*strp)->stlen;
strp++;
}
*strp = NULL;
str_count = strp - strlist;
strp = strlist;
for (i = 0; i < str_count; i++) {
len += (*strp)->stlen;
strp++;
}
if (do_lint && supposed_len != len)
lintwarn(_("concatenation: side effects in one expression have changed the length of another!"));
emalloc(str, char *, len+2, "tree_eval");
str[len] = str[len+1] = '\0'; /* for good measure */
dest = str;
strp = strlist;
while (*strp != NULL) {
memcpy(dest, (*strp)->stptr, (*strp)->stlen);
dest += (*strp)->stlen;
unref(*strp);
strp++;
}
r = make_str_node(str, len, ALREADY_MALLOCED);
r->flags |= TEMP;
free(strlist);
free(treelist);
}
return r;
/* assignments */
case Node_assign_concat:
{
Func_ptr after_assign = NULL;
NODE *l, *r, *t;
/*
* Note that something lovely like this:
*
* BEGIN { a = "a"; a = a (a = "b"); print a }
*
* is not defined. It could print `ab' or `bb'.
* Gawk 3.1.3 prints `ab', so we do that too, simply
* by evaluating the LHS first. Ugh.
*
* Thanks to mary1john@earthlink.net for pointing
* out this issue.
*/
lhs = get_lhs(tree->lnode, &after_assign, FALSE);
*lhs = force_string(*lhs);
l = *lhs;
/*
* This is a hack. We temporarily increase the reference count
* on l in case evaluating r might change the original value
* of l. We have to be careful about reducing it afterwards.
* In particular, if the lhs changed during evaluation of the
* rhs, we have to compensate.
*
* See test/nasty.awk.
*/
t = dupnode(l);
r = force_string(tree_eval(tree->rnode));
if (l != *lhs) {
/*
* Something happened to the original
* during the evaluation of the rhs.
*/
unref(*lhs);
*lhs = l;
}
else
unref(t);
/*
* Don't clobber string constants!
*
* Also check stref; see test/strcat1.awk,
* the test for l->stref == 1 can't be an
* assertion.
*
* Thanks again to mary1john@earthlink.net for pointing
* out this issue.
*/
if (l != r && (l->flags & PERM) == 0 && l->stref == 1) {
size_t nlen = l->stlen + r->stlen + 2;
erealloc(l->stptr, char *, nlen, "interpret");
memcpy(l->stptr + l->stlen, r->stptr, r->stlen);
l->stlen += r->stlen;
l->stptr[l->stlen] = '\0';
free_wstr(l);
} else {
char *nval;
size_t nlen = l->stlen + r->stlen + 2;
emalloc(nval, char *, nlen, "interpret");
memcpy(nval, l->stptr, l->stlen);
memcpy(nval + l->stlen, r->stptr, r->stlen);
unref(*lhs);
*lhs = make_str_node(nval, l->stlen + r->stlen, ALREADY_MALLOCED);
}
(*lhs)->flags &= ~(NUMCUR|NUMBER);
free_temp(r);
if (after_assign)
(*after_assign)();
return *lhs;
}
case Node_assign:
{
Func_ptr after_assign = NULL;
if (do_lint && iscond)
lintwarn(_("assignment used in conditional context"));
r = tree_eval(tree->rnode);
lhs = get_lhs(tree->lnode, &after_assign, FALSE);
assign_val(lhs, r);
if (after_assign)
(*after_assign)();
return *lhs;
}
/* other assignment types are easier because they are numeric */
case Node_preincrement:
case Node_predecrement:
case Node_postincrement:
case Node_postdecrement:
case Node_assign_exp:
case Node_assign_times:
case Node_assign_quotient:
case Node_assign_mod:
case Node_assign_plus:
case Node_assign_minus:
return op_assign(tree);
default:
break; /* handled below */
}
/*
* Evaluate subtrees in order to do binary operation, then keep going.
* Use dupnode to make sure that these values don't disappear out
* from under us during recursive subexpression evaluation.
*/
t1 = dupnode(tree_eval(tree->lnode));
t2 = dupnode(tree_eval(tree->rnode));
switch (tree->type) {
case Node_geq:
case Node_leq:
case Node_greater:
case Node_less:
case Node_notequal:
case Node_equal:
di = cmp_nodes(t1, t2);
unref(t1);
unref(t2);
switch (tree->type) {
case Node_equal:
return tmp_number((AWKNUM) (di == 0));
case Node_notequal:
return tmp_number((AWKNUM) (di != 0));
case Node_less:
return tmp_number((AWKNUM) (di < 0));
case Node_greater:
return tmp_number((AWKNUM) (di > 0));
case Node_leq:
return tmp_number((AWKNUM) (di <= 0));
case Node_geq:
return tmp_number((AWKNUM) (di >= 0));
default:
cant_happen();
}
break;
default:
break; /* handled below */
}
x1 = force_number(t1);
x2 = force_number(t2);
unref(t1);
unref(t2);
switch (tree->type) {
case Node_exp:
return tmp_number(calc_exp(x1, x2));
case Node_times:
return tmp_number(x1 * x2);
case Node_quotient:
if (x2 == 0)
fatal(_("division by zero attempted"));
#ifdef _CRAY
/* special case for integer division, put in for Cray */
lx2 = x2;
if (lx2 == 0)
return tmp_number(x1 / x2);
lx = (long) x1 / lx2;
if (lx * x2 == x1)
return tmp_number((AWKNUM) lx);
else
#endif
return tmp_number(x1 / x2);
case Node_mod:
if (x2 == 0)
fatal(_("division by zero attempted in `%%'"));
#ifdef HAVE_FMOD
return tmp_number(fmod(x1, x2));
#else /* ! HAVE_FMOD */
(void) modf(x1 / x2, &x);
return tmp_number(x1 - x * x2);
#endif /* ! HAVE_FMOD */
case Node_plus:
return tmp_number(x1 + x2);
case Node_minus:
return tmp_number(x1 - x2);
default:
fatal(_("illegal type (%s) in tree_eval"), nodetype2str(tree->type));
}
return (NODE *) 0;
}
/* eval_condition --- is TREE true or false? Returns 0==false, non-zero==true */
static int
eval_condition(register NODE *tree)
{
register NODE *t1;
register int ret;
if (tree == NULL) /* Null trees are the easiest kinds */
return TRUE;
if (tree->type == Node_line_range) {
/*
* Node_line_range is kind of like Node_match, EXCEPT: the
* lnode field (more properly, the condpair field) is a node
* of a Node_cond_pair; whether we evaluate the lnode of that
* node or the rnode depends on the triggered word. More
* precisely: if we are not yet triggered, we tree_eval the
* lnode; if that returns true, we set the triggered word.
* If we are triggered (not ELSE IF, note), we tree_eval the
* rnode, clear triggered if it succeeds, and perform our
* action (regardless of success or failure). We want to be
* able to begin and end on a single input record, so this
* isn't an ELSE IF, as noted above.
*/
if (! tree->triggered) {
if (! eval_condition(tree->condpair->lnode))
return FALSE;
else
tree->triggered = TRUE;
}
/* Else we are triggered */
if (eval_condition(tree->condpair->rnode))
tree->triggered = FALSE;
return TRUE;
}
/*
* Could just be J.random expression. in which case, null and 0 are
* false, anything else is true
*/
t1 = m_tree_eval(tree, TRUE);
if (t1->flags & MAYBE_NUM)
(void) force_number(t1);
if (t1->flags & NUMBER)
ret = (t1->numbr != 0.0);
else
ret = (t1->stlen != 0);
free_temp(t1);
return ret;
}
/* cmp_nodes --- compare two nodes, returning negative, 0, positive */
int
cmp_nodes(register NODE *t1, register NODE *t2)
{
register int ret;
register size_t len1, len2;
register int l;
int ldiff;
if (t1 == t2)
return 0;
if (t1->flags & MAYBE_NUM)
(void) force_number(t1);
if (t2->flags & MAYBE_NUM)
(void) force_number(t2);
if ((t1->flags & NUMBER) && (t2->flags & NUMBER)) {
if (t1->numbr == t2->numbr)
return 0;
/* don't subtract, in case one or both are infinite */
else if (t1->numbr < t2->numbr)
return -1;
else
return 1;
}
(void) force_string(t1);
(void) force_string(t2);
len1 = t1->stlen;
len2 = t2->stlen;
ldiff = len1 - len2;
if (len1 == 0 || len2 == 0)
return ldiff;
l = (ldiff <= 0 ? len1 : len2);
if (IGNORECASE) {
const unsigned char *cp1 = (const unsigned char *) t1->stptr;
const unsigned char *cp2 = (const unsigned char *) t2->stptr;
#ifdef MBS_SUPPORT
if (gawk_mb_cur_max > 1) {
mbstate_t mbs;
memset(&mbs, 0, sizeof(mbstate_t));
ret = strncasecmpmbs((const char *) cp1, mbs,
(const char *) cp2, mbs, l);
} else
#endif
/* Could use tolower() here; see discussion above. */
for (ret = 0; l-- > 0 && ret == 0; cp1++, cp2++)
ret = casetable[*cp1] - casetable[*cp2];
} else
ret = memcmp(t1->stptr, t2->stptr, l);
return (ret == 0 ? ldiff : ret);
}
/* op_assign --- do +=, -=, etc. */
static NODE *
op_assign(register NODE *tree)
{
AWKNUM rval, lval;
NODE **lhs;
NODE *tmp;
Func_ptr after_assign = NULL;
int post = FALSE;
/*
* For += etc, do the rhs first, since it can rearrange things,
* and *then* get the lhs.
*/
if (tree->rnode != NULL) {
tmp = tree_eval(tree->rnode);
rval = force_number(tmp);
free_temp(tmp);
} else
rval = (AWKNUM) 1.0;
lhs = get_lhs(tree->lnode, &after_assign, TRUE);
lval = force_number(*lhs);
unref(*lhs);
switch(tree->type) {
case Node_postincrement:
post = TRUE;
/* fall through */
case Node_preincrement:
case Node_assign_plus:
*lhs = make_number(lval + rval);
break;
case Node_postdecrement:
post = TRUE;
/* fall through */
case Node_predecrement:
case Node_assign_minus:
*lhs = make_number(lval - rval);
break;
case Node_assign_exp:
*lhs = make_number(calc_exp(lval, rval));
break;
case Node_assign_times:
*lhs = make_number(lval * rval);
break;
case Node_assign_quotient:
if (rval == (AWKNUM) 0)
fatal(_("division by zero attempted in `/='"));
{
#ifdef _CRAY
long ltemp;
/* special case for integer division, put in for Cray */
ltemp = rval;
if (ltemp == 0) {
*lhs = make_number(lval / rval);
break;
}
ltemp = (long) lval / ltemp;
if (ltemp * lval == rval)
*lhs = make_number((AWKNUM) ltemp);
else
#endif /* _CRAY */
*lhs = make_number(lval / rval);
}
break;
case Node_assign_mod:
if (rval == (AWKNUM) 0)
fatal(_("division by zero attempted in `%%='"));
#ifdef HAVE_FMOD
*lhs = make_number(fmod(lval, rval));
#else /* ! HAVE_FMOD */
{
AWKNUM t1, t2;
(void) modf(lval / rval, &t1);
t2 = lval - rval * t1;
*lhs = make_number(t2);
}
#endif /* ! HAVE_FMOD */
break;
default:
cant_happen();
}
if (after_assign)
(*after_assign)();
/* for postincrement or postdecrement, return the old value */
return (post ? tmp_number(lval) : *lhs);
}
/*
* Avoiding memory leaks is difficult. In paticular, any of `next',
* `nextfile', `break' or `continue' (when not in a loop), can longjmp
* out to the outermost level. This leaks memory if it happens in a
* called function. It also leaks memory if it happens in a
* `for (iggy in foo)' loop, since such loops malloc an array of the
* current array indices to loop over, which provides stability.
*
* The following code takes care of these problems. First comes the
* array-loop management code. This can be a stack of arrays being looped
* on at any one time. This stack serves for both mainline code and
* function body code. As each loop starts and finishes, it pushes its
* info onto this stack and off of it; whether the loop is in a function
* body or not isn't relevant.
*
* Since the list of indices is created using dupnode(), when popping
* this stack it should be safe to unref() things, and then memory
* will get finally released when the function call stack is popped.
* This means that the loop_stack should be popped first upon a `next'.
*/
static struct loop_info {
const char *varname; /* variable name, for debugging */
NODE **elems; /* list of indices */
size_t nelems; /* how many there are */
} *loop_stack = NULL;
size_t nloops = 0; /* how many slots there are in the stack */
size_t nloops_active = 0; /* how many loops are actively stacked */
/* pop_forloop --- pop one for loop off the stack */
static void
pop_forloop()
{
int i, curloop;
struct loop_info *loop;
assert(nloops_active > 0);
curloop = --nloops_active; /* 0-based indexing */
loop = & loop_stack[curloop];
for (i = 0; i < loop->nelems; i++)
unref(loop->elems[i]);
free(loop->elems);
loop->elems = NULL;
loop->varname = NULL;
loop->nelems = 0;
}
/* pop_forloops --- pop the for loops stack all the way */
static inline void
pop_all_forloops()
{
while (nloops_active > 0)
pop_forloop(); /* decrements nloops_active for us */
}
/* push_forloop --- add a single for loop to the stack */
static void
push_forloop(const char *varname, NODE **elems, size_t nelems)
{
#define NLOOPS 4 /* seems like a good guess */
if (loop_stack == NULL) {
/* allocate stack, set vars */
nloops = NLOOPS;
emalloc(loop_stack, struct loop_info *, nloops * sizeof(struct loop_info),
"push_forloop");
} else if (nloops_active == nloops) {
/* grow stack, set vars */
nloops *= 2;
erealloc(loop_stack, struct loop_info *, nloops * sizeof(struct loop_info),
"push_forloop");
}
loop_stack[nloops_active].varname = varname;
loop_stack[nloops_active].elems = elems;
loop_stack[nloops_active].nelems = nelems;
nloops_active++;
}
/*
* 2/2004:
* N.B. The code that uses fcalls[] *always* uses indexing.
* This avoids severe problems in case fcalls gets realloc()'ed
* during recursive tree_eval()'s or whatever, so that we don't
* have to carefully reassign pointers into the array. The
* minor speed gain from using a pointer was offset too much
* by the hassles to get the code right and commented.
*
* Thanks and a tip of the hatlo to Brian Kernighan.
*/
static struct fcall {
const char *fname; /* function name */
size_t count; /* how many args */
NODE *arglist; /* list thereof */
NODE **prevstack; /* function stack frame of previous function */
NODE **stack; /* function stack frame of current function */
} *fcalls = NULL;
static long fcall_list_size = 0;
static long curfcall = -1;
/*
* get_curfunc_arg_count --- return number actual parameters
*
* This is for use by dynamically loaded C extension functions.
*/
size_t
get_curfunc_arg_count(void)
{
NODE *argp;
size_t argc;
assert(curfcall >= 0);
/* count the # of expressions in argument expression list */
for (argc = 0, argp = fcalls[curfcall].arglist;
argp != NULL; argp = argp->rnode)
argc++;
return argc;
}
/* pop_fcall --- pop off a single function call */
static void
pop_fcall()
{
NODE *n, **sp;
int count;
assert(curfcall >= 0);
stack_ptr = fcalls[curfcall].prevstack;
sp = fcalls[curfcall].stack;
for (count = fcalls[curfcall].count; count > 0; count--) {
n = *sp++;
if (n->type == Node_var) /* local variable */
unref(n->var_value);
else if (n->type == Node_var_array) /* local array */
assoc_clear(n);
freenode(n);
}
if (fcalls[curfcall].stack) {
free((char *) fcalls[curfcall].stack);
fcalls[curfcall].stack = NULL;
}
curfcall--;
}
/* pop_fcall_stack --- pop off all function args, don't leak memory */
static inline void
pop_fcall_stack()
{
while (curfcall >= 0)
pop_fcall();
}
/* push_args --- push function arguments onto the stack */
static void
push_args(int count,
NODE *argp,
NODE **oldstack,
const char *func_name,
char **varnames)
{
NODE *arg, *r, **sp;
int i;
if (fcall_list_size == 0) { /* first time */
emalloc(fcalls, struct fcall *, 10 * sizeof(struct fcall),
"push_args");
fcall_list_size = 10;
}
if (++curfcall >= fcall_list_size) {
fcall_list_size *= 2;
erealloc(fcalls, struct fcall *,
fcall_list_size * sizeof(struct fcall), "push_args");
}
if (count > 0)
emalloc(fcalls[curfcall].stack, NODE **, count*sizeof(NODE *), "push_args");
else
fcalls[curfcall].stack = NULL;
fcalls[curfcall].count = count;
fcalls[curfcall].fname = func_name; /* not used, for debugging, just in case */
fcalls[curfcall].arglist = argp;
fcalls[curfcall].prevstack = oldstack;
sp = fcalls[curfcall].stack;
/* for each calling arg. add NODE * on stack */
for (i = 0; i < count; i++) {
getnode(r);
*sp++ = r;
if (argp == NULL) {
/* local variable */
r->type = Node_var_new;
r->var_value = Nnull_string;
r->vname = varnames[i];
r->rnode = NULL;
continue;
}
arg = argp->lnode;
/* call by reference for arrays; see below also */
if (arg->type == Node_param_list)
arg = fcalls[curfcall].prevstack[arg->param_cnt];
if (arg->type == Node_var_array || arg->type == Node_var_new) {
r->type = Node_array_ref;
r->orig_array = arg;
r->prev_array = arg;
} else if (arg->type == Node_array_ref) {
*r = *arg;
r->prev_array = arg;
} else {
NODE *n = tree_eval(arg);
r->type = Node_var;
r->lnode = dupnode(n);
r->rnode = (NODE *) NULL;
free_temp(n);
}
r->vname = varnames[i];
argp = argp->rnode;
}
if (argp != NULL) {
/* Left over calling args. */
warning(
_("function `%s' called with more arguments than declared"),
func_name);
/* Evaluate them, they may have side effects: */
do {
arg = argp->lnode;
if (arg->type == Node_param_list)
arg = fcalls[curfcall].prevstack[arg->param_cnt];
if (arg->type != Node_var_array &&
arg->type != Node_array_ref &&
arg->type != Node_var_new)
free_temp(tree_eval(arg));
} while ((argp = argp->rnode) != NULL);
}
stack_ptr = fcalls[curfcall].stack;
}
/* func_call --- call a function, call by reference for arrays */
NODE **stack_ptr;
static NODE *
func_call(NODE *tree)
{
register NODE *r;
NODE *name, *arg_list;
NODE *f;
jmp_buf volatile func_tag_stack;
jmp_buf volatile loop_tag_stack;
int volatile save_loop_tag_valid = FALSE;
NODE *save_ret_node;
extern NODE *ret_node;
size_t current_nloops_active = 0;
/* tree->rnode is a Node_val giving function name */
/* tree->lnode is Node_expression_list of calling args. */
name = tree->rnode;
arg_list = tree->lnode;
/* retrieve function definition node */
if (tree->funcbody != NULL)
f = tree->funcbody;
else {
f = lookup(name->stptr);
if (f == NULL || f->type != Node_func)
fatal(_("function `%s' not defined"), name->stptr);
tree->funcbody = f; /* save for next call */
}
#ifdef FUNC_TRACE
fprintf(stderr, "function `%s' called\n", name->stptr);
#endif
push_args(f->lnode->param_cnt, arg_list, stack_ptr, name->stptr,
f->parmlist);
/*
* Execute function body, saving context, as a return statement
* will longjmp back here.
*
* Have to save and restore the loop_tag stuff so that a return
* inside a loop in a function body doesn't scrog any loops going
* on in the main program. We save the necessary info in variables
* local to this function so that function nesting works OK.
* We also only bother to save the loop stuff if we're in a loop
* when the function is called.
*/
if (loop_tag_valid) {
int junk = 0;
save_loop_tag_valid = (volatile int) loop_tag_valid;
PUSH_BINDING(loop_tag_stack, loop_tag, junk);
loop_tag_valid = FALSE;
}
PUSH_BINDING(func_tag_stack, func_tag, func_tag_valid);
current_nloops_active = nloops_active;
save_ret_node = ret_node;
ret_node = Nnull_string; /* default return value */
INCREMENT(f->exec_count); /* count function calls */
if (setjmp(func_tag) == 0)
(void) interpret(f->rnode);
while (nloops_active > current_nloops_active)
pop_forloop();
r = ret_node;
ret_node = (NODE *) save_ret_node;
RESTORE_BINDING(func_tag_stack, func_tag, func_tag_valid);
pop_fcall();
/* Restore the loop_tag stuff if necessary. */
if (save_loop_tag_valid) {
int junk = 0;
loop_tag_valid = (int) save_loop_tag_valid;
RESTORE_BINDING(loop_tag_stack, loop_tag, junk);
}
return r;
}
#ifdef PROFILING
/* dump_fcall_stack --- print a backtrace of the awk function calls */
void
dump_fcall_stack(FILE *fp)
{
int i;
if (curfcall < 0)
return;
fprintf(fp, _("\n\t# Function Call Stack:\n\n"));
for (i = curfcall; i >= 0; i--)
fprintf(fp, "\t# %3d. %s\n", i+1, fcalls[i].fname);
fprintf(fp, _("\t# -- main --\n"));
}
#endif /* PROFILING */
/*
* r_get_lhs:
* This returns a POINTER to a node pointer. get_lhs(ptr) is the current
* value of the var, or where to store the var's new value
*
* For the special variables, don't unref their current value if it's
* the same as the internal copy; perhaps the current one is used in
* a concatenation or some other expression somewhere higher up in the
* call chain. Ouch.
*/
NODE **
r_get_lhs(register NODE *ptr, Func_ptr *assign, int reference)
{
register NODE **aptr = NULL;
register NODE *n;
if (assign)
*assign = NULL; /* for safety */
if (ptr->type == Node_param_list) {
if ((ptr->flags & FUNC) != 0)
fatal(_("can't use function name `%s' as variable or array"), ptr->vname);
ptr = stack_ptr[ptr->param_cnt];
}
make_scalar(ptr);
switch (ptr->type) {
case Node_var:
if (do_lint && reference && var_uninitialized(ptr))
lintwarn(_("reference to uninitialized variable `%s'"),
ptr->vname);
aptr = &(ptr->var_value);
#ifdef GAWKDEBUG
if (ptr->var_value->stref <= 0)
cant_happen();
#endif
break;
case Node_FIELDWIDTHS:
aptr = &(FIELDWIDTHS_node->var_value);
if (assign != NULL)
*assign = set_FIELDWIDTHS;
break;
case Node_RS:
aptr = &(RS_node->var_value);
if (assign != NULL)
*assign = set_RS;
break;
case Node_FS:
aptr = &(FS_node->var_value);
if (assign != NULL)
*assign = set_FS;
break;
case Node_FNR:
if (FNR_node->var_value->numbr != FNR) {
unref(FNR_node->var_value);
FNR_node->var_value = make_number((AWKNUM) FNR);
}
aptr = &(FNR_node->var_value);
if (assign != NULL)
*assign = set_FNR;
break;
case Node_NR:
if (NR_node->var_value->numbr != NR) {
unref(NR_node->var_value);
NR_node->var_value = make_number((AWKNUM) NR);
}
aptr = &(NR_node->var_value);
if (assign != NULL)
*assign = set_NR;
break;
case Node_NF:
if (NF == -1 || NF_node->var_value->numbr != NF) {
if (NF == -1)
(void) get_field(UNLIMITED-1, assign); /* parse record */
unref(NF_node->var_value);
NF_node->var_value = make_number((AWKNUM) NF);
}
aptr = &(NF_node->var_value);
if (assign != NULL)
*assign = set_NF;
break;
case Node_IGNORECASE:
aptr = &(IGNORECASE_node->var_value);
if (assign != NULL)
*assign = set_IGNORECASE;
break;
case Node_BINMODE:
aptr = &(BINMODE_node->var_value);
if (assign != NULL)
*assign = set_BINMODE;
break;
case Node_LINT:
aptr = &(LINT_node->var_value);
if (assign != NULL)
*assign = set_LINT;
break;
case Node_OFMT:
aptr = &(OFMT_node->var_value);
if (assign != NULL)
*assign = set_OFMT;
break;
case Node_CONVFMT:
aptr = &(CONVFMT_node->var_value);
if (assign != NULL)
*assign = set_CONVFMT;
break;
case Node_ORS:
aptr = &(ORS_node->var_value);
if (assign != NULL)
*assign = set_ORS;
break;
case Node_OFS:
aptr = &(OFS_node->var_value);
if (assign != NULL)
*assign = set_OFS;
break;
case Node_SUBSEP:
aptr = &(SUBSEP_node->var_value);
if (assign != NULL)
*assign = set_SUBSEP;
break;
case Node_TEXTDOMAIN:
aptr = &(TEXTDOMAIN_node->var_value);
if (assign != NULL)
*assign = set_TEXTDOMAIN;
break;
case Node_field_spec:
{
int field_num;
n = tree_eval(ptr->lnode);
if (do_lint) {
if ((n->flags & NUMBER) == 0) {
lintwarn(_("attempt to field reference from non-numeric value"));
if (n->stlen == 0)
lintwarn(_("attempt to reference from null string"));
}
}
field_num = (int) force_number(n);
free_temp(n);
if (field_num < 0)
fatal(_("attempt to access field %d"), field_num);
if (field_num == 0 && field0_valid) { /* short circuit */
aptr = &fields_arr[0];
if (assign != NULL)
*assign = reset_record;
} else
aptr = get_field(field_num, assign);
if (do_lint && reference && (*aptr == Null_field || *aptr == Nnull_string))
lintwarn(_("reference to uninitialized field `$%d'"),
field_num);
break;
}
case Node_subscript:
n = get_array(ptr->lnode);
aptr = assoc_lookup(n, concat_exp(ptr->rnode), reference);
break;
case Node_builtin:
#if 1
/* in gawk for a while */
fatal(_("assignment is not allowed to result of builtin function"));
#else
/*
* This is how Christos at Deshaw did it.
* Does this buy us anything?
*/
if (ptr->builtin == NULL)
fatal(_("assignment is not allowed to result of builtin function"));
ptr->callresult = (*ptr->builtin)(ptr->subnode);
aptr = &ptr->callresult;
break;
#endif
default:
fprintf(stderr, "type = %s\n", nodetype2str(ptr->type));
fflush(stderr);
cant_happen();
}
return aptr;
}
/* match_op --- do ~ and !~ */
static NODE *
match_op(register NODE *tree)
{
register NODE *t1;
register Regexp *rp;
int i;
int match = TRUE;
int kludge_need_start = 0; /* FIXME: --- see below */
if (tree->type == Node_nomatch)
match = FALSE;
if (tree->type == Node_regex)
t1 = *get_field(0, (Func_ptr *) 0);
else {
t1 = force_string(tree_eval(tree->lnode));
tree = tree->rnode;
}
rp = re_update(tree);
/*
* FIXME:
*
* Any place where research() is called with a last parameter of
* zero, we need to use the avoid_dfa test. This appears here and
* in the code for Node_K_switch.
*
* A new or improved dfa that distinguishes beginning/end of
* string from beginning/end of line will allow us to get rid of
* this temporary hack.
*
* The avoid_dfa() function is in re.c; it is not very smart.
*/
if (avoid_dfa(tree, t1->stptr, t1->stlen))
kludge_need_start = RE_NEED_START;
i = research(rp, t1->stptr, 0, t1->stlen, kludge_need_start);
i = (i == -1) ^ (match == TRUE);
free_temp(t1);
return tmp_number((AWKNUM) i);
}
/* set_IGNORECASE --- update IGNORECASE as appropriate */
void
set_IGNORECASE()
{
static short warned = FALSE;
if ((do_lint || do_traditional) && ! warned) {
warned = TRUE;
lintwarn(_("`IGNORECASE' is a gawk extension"));
}
load_casetable();
if (do_traditional)
IGNORECASE = FALSE;
else if ((IGNORECASE_node->var_value->flags & (STRING|STRCUR)) != 0) {
if ((IGNORECASE_node->var_value->flags & MAYBE_NUM) == 0)
IGNORECASE = (force_string(IGNORECASE_node->var_value)->stlen > 0);
else
IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
} else if ((IGNORECASE_node->var_value->flags & (NUMCUR|NUMBER)) != 0)
IGNORECASE = (force_number(IGNORECASE_node->var_value) != 0.0);
else
IGNORECASE = FALSE; /* shouldn't happen */
set_RS(); /* set_RS() calls set_FS() if need be, for us */
}
/* set_BINMODE --- set translation mode (OS/2, DOS, others) */
void
set_BINMODE()
{
static short warned = FALSE;
char *p;
NODE *v;
if ((do_lint || do_traditional) && ! warned) {
warned = TRUE;
lintwarn(_("`BINMODE' is a gawk extension"));
}
if (do_traditional)
BINMODE = 0;
else if ((BINMODE_node->var_value->flags & NUMBER) != 0) {
BINMODE = (int) force_number(BINMODE_node->var_value);
/* Make sure the value is rational. */
if (BINMODE < 0)
BINMODE = 0;
else if (BINMODE > 3)
BINMODE = 3;
}
else if ((BINMODE_node->var_value->flags & STRING) != 0) {
v = BINMODE_node->var_value;
p = v->stptr;
/*
* Allow only one of the following:
* "0", "1", "2", "3",
* "r", "w", "rw", "wr"
* ANYTHING ELSE goes to 3. So there.
*/
switch (v->stlen) {
case 1:
switch (p[0]) {
case '0':
case '1':
case '2':
case '3':
BINMODE = p[0] - '0';
break;
case 'r':
BINMODE = 1;
break;
case 'w':
BINMODE = 2;
break;
default:
BINMODE = 3;
goto bad_value;
break;
}
break;
case 2:
switch (p[0]) {
case 'r':
BINMODE = 3;
if (p[1] != 'w')
goto bad_value;
break;
case 'w':
BINMODE = 3;
if (p[1] != 'r')
goto bad_value;
break;
break;
default:
bad_value:
lintwarn(_("BINMODE value `%s' is invalid, treated as 3"), p);
break;
}
}
}
else
BINMODE = 3; /* shouldn't happen */
}
/* set_OFS --- update OFS related variables when OFS assigned to */
void
set_OFS()
{
OFS = force_string(OFS_node->var_value)->stptr;
OFSlen = OFS_node->var_value->stlen;
OFS[OFSlen] = '\0';
}
/* set_ORS --- update ORS related variables when ORS assigned to */
void
set_ORS()
{
ORS = force_string(ORS_node->var_value)->stptr;
ORSlen = ORS_node->var_value->stlen;
ORS[ORSlen] = '\0';
}
/* fmt_ok --- is the conversion format a valid one? */
NODE **fmt_list = NULL;
static int fmt_ok P((NODE *n));
static int fmt_index P((NODE *n));
static int
fmt_ok(NODE *n)
{
NODE *tmp = force_string(n);
const char *p = tmp->stptr;
#if ! defined(PRINTF_HAS_F_FORMAT) || PRINTF_HAS_F_FORMAT != 1
static const char float_formats[] = "efgEG";
#else
static const char float_formats[] = "efgEFG";
#endif
#if defined(HAVE_LOCALE_H)
static const char flags[] = " +-#'";
#else
static const char flags[] = " +-#";
#endif
if (*p++ != '%')
return 0;
while (*p && strchr(flags, *p) != NULL) /* flags */
p++;
while (*p && ISDIGIT(*p)) /* width - %*.*g is NOT allowed */
p++;
if (*p == '\0' || (*p != '.' && ! ISDIGIT(*p)))
return 0;
if (*p == '.')
p++;
while (*p && ISDIGIT(*p)) /* precision */
p++;
if (*p == '\0' || strchr(float_formats, *p) == NULL)
return 0;
if (*++p != '\0')
return 0;
return 1;
}
/* fmt_index --- track values of OFMT and CONVFMT to keep semantics correct */
static int
fmt_index(NODE *n)
{
register int ix = 0;
static int fmt_num = 4;
static int fmt_hiwater = 0;
if (fmt_list == NULL)
emalloc(fmt_list, NODE **, fmt_num*sizeof(*fmt_list), "fmt_index");
(void) force_string(n);
while (ix < fmt_hiwater) {
if (cmp_nodes(fmt_list[ix], n) == 0)
return ix;
ix++;
}
/* not found */
n->stptr[n->stlen] = '\0';
if (do_lint && ! fmt_ok(n))
lintwarn(_("bad `%sFMT' specification `%s'"),
n == CONVFMT_node->var_value ? "CONV"
: n == OFMT_node->var_value ? "O"
: "", n->stptr);
if (fmt_hiwater >= fmt_num) {
fmt_num *= 2;
erealloc(fmt_list, NODE **, fmt_num * sizeof(*fmt_list), "fmt_index");
}
fmt_list[fmt_hiwater] = dupnode(n);
return fmt_hiwater++;
}
/* set_OFMT --- track OFMT correctly */
void
set_OFMT()
{
OFMTidx = fmt_index(OFMT_node->var_value);
OFMT = fmt_list[OFMTidx]->stptr;
}
/* set_CONVFMT --- track CONVFMT correctly */
void
set_CONVFMT()
{
CONVFMTidx = fmt_index(CONVFMT_node->var_value);
CONVFMT = fmt_list[CONVFMTidx]->stptr;
}
/* set_LINT --- update LINT as appropriate */
void
set_LINT()
{
#ifndef NO_LINT
int old_lint = do_lint;
if ((LINT_node->var_value->flags & (STRING|STRCUR)) != 0) {
if ((LINT_node->var_value->flags & MAYBE_NUM) == 0) {
const char *lintval;
size_t lintlen;
do_lint = (force_string(LINT_node->var_value)->stlen > 0);
lintval = LINT_node->var_value->stptr;
lintlen = LINT_node->var_value->stlen;
if (do_lint) {
do_lint = LINT_ALL;
if (lintlen == 5 && strncmp(lintval, "fatal", 5) == 0)
lintfunc = r_fatal;
else if (lintlen == 7 && strncmp(lintval, "invalid", 7) == 0)
do_lint = LINT_INVALID;
else
lintfunc = warning;
} else
lintfunc = warning;
} else {
if (force_number(LINT_node->var_value) != 0.0)
do_lint = LINT_ALL;
else
do_lint = FALSE;
lintfunc = warning;
}
} else if ((LINT_node->var_value->flags & (NUMCUR|NUMBER)) != 0) {
if (force_number(LINT_node->var_value) != 0.0)
do_lint = LINT_ALL;
else
do_lint = FALSE;
lintfunc = warning;
} else
do_lint = FALSE; /* shouldn't happen */
if (! do_lint)
lintfunc = warning;
/* explicitly use warning() here, in case lintfunc == r_fatal */
if (old_lint != do_lint && old_lint && do_lint == FALSE)
warning(_("turning off `--lint' due to assignment to `LINT'"));
#endif /* ! NO_LINT */
}
/* set_TEXTDOMAIN --- update TEXTDOMAIN variable when TEXTDOMAIN assigned to */
void
set_TEXTDOMAIN()
{
int len;
TEXTDOMAIN = force_string(TEXTDOMAIN_node->var_value)->stptr;
len = TEXTDOMAIN_node->var_value->stlen;
TEXTDOMAIN[len] = '\0';
/*
* Note: don't call textdomain(); this value is for
* the awk program, not for gawk itself.
*/
}
/*
* assign_val --- do mechanics of assignment, for calling from multiple
* places.
*/
NODE *
assign_val(NODE **lhs_p, NODE *rhs)
{
if (rhs != *lhs_p) {
/*
* Since we know that the nodes are different,
* we can do the unref() before the dupnode().
*/
unref(*lhs_p);
*lhs_p = dupnode(rhs);
if ((*lhs_p)->type != Node_val)
(*lhs_p)->funcbody = NULL;
}
return *lhs_p;
}
/* update_ERRNO_saved --- update the value of ERRNO based on argument */
void
update_ERRNO_saved(int errcode)
{
char *cp;
cp = strerror(errcode);
cp = gettext(cp);
unref(ERRNO_node->var_value);
ERRNO_node->var_value = make_string(cp, strlen(cp));
}
/* update_ERRNO --- update the value of ERRNO based on errno */
void
update_ERRNO()
{
update_ERRNO_saved(errno);
}
/* comp_func --- array index comparison function for qsort */
static int
comp_func(const void *p1, const void *p2)
{
size_t len1, len2;
const char *str1, *str2;
const NODE *t1, *t2;
int cmp1;
t1 = *((const NODE *const *) p1);
t2 = *((const NODE *const *) p2);
/*
t1 = force_string(t1);
t2 = force_string(t2);
*/
len1 = t1->ahname_len;
str1 = t1->ahname_str;
len2 = t2->ahname_len;
str2 = t2->ahname_str;
/* Array indexes are strings, compare as such, always! */
cmp1 = memcmp(str1, str2, len1 < len2 ? len1 : len2);
/* if prefixes are equal, size matters */
return (cmp1 != 0 ? cmp1 :
len1 < len2 ? -1 : (len1 > len2));
}
|