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 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 9804 9805 9806 9807 9808 9809 9810 9811 9812 9813 9814 9815 9816 9817 9818 9819 9820 9821 9822 9823 9824 9825 9826 9827 9828 9829 9830 9831 9832 9833 9834 9835 9836 9837 9838 9839 9840 9841 9842 9843 9844 9845 9846 9847 9848 9849 9850 9851 9852 9853 9854 9855 9856 9857 9858 9859 9860 9861 9862 9863 9864 9865 9866 9867 9868 9869 9870 9871 9872 9873 9874 9875 9876 9877 9878 9879 9880 9881 9882 9883 9884 9885 9886 9887 9888 9889 9890 9891 9892 9893 9894 9895 9896 9897 9898 9899 9900 9901 9902 9903 9904 9905 9906 9907 9908 9909 9910 9911 9912 9913 9914 9915 9916 9917 9918 9919 9920 9921 9922 9923 9924 9925 9926 9927 9928 9929 9930 9931 9932 9933 9934 9935 9936 9937 9938 9939 9940 9941 9942 9943 9944 9945 9946 9947 9948 9949 9950 9951 9952 9953 9954 9955 9956 9957 9958 9959 9960 9961 9962 9963 9964 9965 9966 9967 9968 9969 9970 9971 9972 9973 9974 9975 9976 9977 9978 9979 9980 9981 9982 9983 9984 9985 9986 9987 9988 9989 9990 9991 9992 9993 9994 9995 9996 9997 9998 9999 10000 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 10011 10012 10013 10014 10015 10016 10017 10018 10019 10020 10021 10022 10023 10024 10025 10026 10027 10028 10029 10030 10031 10032 10033 10034 10035 10036 10037 10038 10039 10040 10041 10042 10043 10044 10045 10046 10047 10048 10049 10050 10051 10052 10053 10054 10055 10056 10057 10058 10059 10060 10061 10062 10063 10064 10065 10066 10067 10068 10069 10070 10071 10072 10073 10074 10075 10076 10077 10078 10079 10080 10081 10082 10083 10084 10085 10086 10087 10088 10089 10090 10091 10092 10093 10094 10095 10096 10097 10098 10099 10100 10101 10102 10103 10104 10105 10106 10107 10108 10109 10110 10111 10112 10113 10114 10115 10116 10117 10118 10119 10120 10121 10122 10123 10124 10125 10126 10127 10128 10129 10130 10131 10132 10133 10134 10135 10136 10137 10138 10139 10140 10141 10142 10143 10144 10145 10146 10147 10148 10149 10150 10151 10152 10153 10154 10155 10156 10157 10158 10159 10160 10161 10162 10163 10164 10165 10166 10167 10168 10169 10170 10171 10172 10173 10174 10175 10176 10177 10178 10179 10180 10181 10182 10183 10184 10185 10186 10187 10188 10189 10190 10191 10192 10193 10194 10195 10196 10197 10198 10199 10200 10201 10202 10203 10204 10205 10206 10207 10208 10209 10210 10211 10212 10213 10214 10215 10216 10217 10218 10219 10220 10221 10222 10223 10224 10225 10226 10227 10228 10229 10230 10231 10232 10233 10234 10235 10236 10237 10238 10239 10240 10241 10242 10243 10244 10245 10246 10247 10248 10249 10250 10251 10252 10253 10254 10255 10256 10257 10258 10259 10260 10261 10262 10263 10264 10265 10266 10267 10268 10269 10270 10271 10272 10273 10274 10275 10276 10277 10278 10279 10280 10281 10282 10283 10284 10285 10286 10287 10288 10289 10290 10291 10292 10293 10294 10295 10296 10297 10298 10299 10300 10301 10302 10303 10304 10305 10306 10307 10308 10309 10310 10311 10312 10313 10314 10315 10316 10317 10318 10319 10320 10321 10322 10323 10324 10325 10326 10327 10328 10329 10330 10331 10332 10333 10334 10335 10336 10337 10338 10339 10340 10341 10342 10343 10344 10345 10346 10347 10348 10349 10350 10351 10352 10353 10354 10355 10356 10357 10358 10359 10360 10361 10362 10363 10364 10365 10366 10367 10368 10369 10370 10371 10372 10373 10374 10375 10376 10377 10378 10379 10380 10381 10382 10383 10384 10385 10386 10387 10388 10389 10390 10391 10392 10393 10394 10395 10396 10397 10398 10399 10400 10401 10402 10403 10404 10405 10406 10407 10408 10409 10410 10411 10412 10413 10414 10415 10416 10417 10418 10419 10420 10421 10422 10423 10424 10425 10426 10427 10428 10429 10430 10431 10432 10433 10434 10435 10436 10437 10438 10439 10440 10441 10442 10443 10444 10445 10446 10447 10448 10449 10450 10451 10452 10453 10454 10455 10456 10457 10458 10459 10460 10461 10462 10463 10464 10465 10466 10467 10468 10469 10470 10471 10472 10473 10474 10475 10476 10477 10478 10479 10480 10481 10482 10483 10484 10485 10486 10487 10488 10489 10490 10491 10492 10493 10494 10495 10496 10497 10498 10499 10500 10501 10502 10503 10504 10505 10506 10507 10508 10509 10510 10511 10512 10513 10514 10515 10516 10517 10518 10519 10520 10521 10522 10523 10524 10525 10526 10527 10528 10529 10530 10531 10532 10533 10534 10535 10536 10537 10538 10539 10540 10541 10542 10543 10544 10545 10546 10547 10548 10549 10550 10551 10552 10553 10554 10555 10556 10557 10558 10559 10560 10561 10562 10563 10564 10565 10566 10567 10568 10569 10570 10571 10572 10573 10574 10575 10576 10577 10578 10579 10580 10581 10582 10583 10584 10585 10586 10587 10588 10589 10590 10591 10592 10593 10594 10595 10596 10597 10598 10599 10600 10601 10602 10603 10604 10605 10606 10607 10608 10609 10610 10611 10612 10613 10614 10615 10616 10617 10618 10619 10620 10621 10622 10623 10624 10625 10626 10627 10628 10629 10630 10631 10632 10633 10634 10635 10636 10637 10638 10639 10640 10641 10642 10643 10644 10645 10646 10647 10648 10649 10650 10651 10652 10653 10654 10655 10656 10657 10658 10659 10660 10661 10662 10663 10664 10665 10666 10667 10668 10669 10670 10671 10672 10673 10674 10675 10676 10677 10678 10679 10680 10681 10682 10683 10684 10685 10686 10687 10688 10689 10690 10691 10692 10693 10694 10695 10696 10697 10698 10699 10700 10701 10702 10703 10704 10705 10706 10707 10708 10709 10710 10711 10712 10713 10714 10715 10716 10717 10718 10719 10720 10721 10722 10723 10724 10725 10726 10727 10728 10729 10730 10731 10732 10733 10734 10735 10736 10737 10738 10739 10740 10741 10742 10743 10744 10745 10746 10747 10748 10749 10750 10751 10752 10753 10754 10755 10756 10757 10758 10759 10760 10761 10762 10763 10764 10765 10766 10767 10768 10769 10770 10771 10772 10773 10774 10775 10776 10777 10778 10779 10780 10781 10782 10783 10784 10785 10786 10787 10788 10789 10790 10791 10792 10793 10794 10795 10796 10797 10798 10799 10800 10801 10802 10803 10804 10805 10806 10807 10808 10809 10810 10811 10812 10813 10814 10815 10816 10817 10818 10819 10820 10821 10822 10823 10824 10825 10826 10827 10828 10829 10830 10831 10832 10833 10834 10835 10836 10837 10838 10839 10840 10841 10842 10843 10844 10845 10846 10847 10848 10849 10850 10851 10852 10853 10854 10855 10856 10857 10858 10859 10860 10861 10862 10863 10864 10865 10866 10867 10868 10869 10870 10871 10872 10873 10874 10875 10876 10877 10878 10879 10880 10881 10882 10883 10884 10885 10886 10887 10888 10889 10890 10891 10892 10893 10894 10895 10896 10897 10898 10899 10900 10901 10902 10903 10904 10905 10906 10907 10908 10909 10910 10911 10912 10913 10914 10915 10916 10917 10918 10919 10920 10921 10922 10923 10924 10925 10926 10927 10928 10929 10930 10931 10932 10933 10934 10935 10936 10937 10938 10939 10940 10941 10942 10943 10944 10945 10946 10947 10948 10949 10950 10951 10952 10953 10954 10955 10956 10957 10958 10959 10960 10961 10962 10963 10964 10965 10966 10967 10968 10969 10970 10971 10972 10973 10974 10975 10976 10977 10978 10979 10980 10981 10982 10983 10984 10985 10986 10987 10988 10989 10990 10991 10992 10993 10994 10995 10996 10997 10998 10999 11000 11001 11002 11003 11004 11005 11006 11007 11008 11009 11010 11011 11012 11013 11014 11015 11016 11017 11018 11019 11020 11021 11022 11023 11024 11025 11026 11027 11028 11029 11030 11031 11032 11033 11034 11035 11036 11037 11038 11039 11040 11041 11042 11043 11044 11045 11046 11047 11048 11049 11050 11051 11052 11053 11054 11055 11056 11057 11058 11059 11060 11061 11062 11063 11064 11065 11066 11067 11068 11069 11070 11071 11072 11073 11074 11075 11076 11077 11078 11079 11080 11081 11082 11083
|
/*
Copyright (C) 2001-2012, 2014-2023 Free Software Foundation, Inc.
Written by Keisuke Nishida, Roger While, Simon Sobisch, Ron Norman
This file is part of GnuCOBOL.
The GnuCOBOL runtime library is free software: you can redistribute it
and/or modify it under the terms of the GNU Lesser General Public License
as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
GnuCOBOL 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 Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.
*/
#include "tarstamp.h"
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdarg.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <ctype.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <math.h>
#ifdef HAVE_FINITE_IEEEFP_H
#include <ieeefp.h>
#endif
#include <time.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#ifndef STDIN_FILENO
#define STDIN_FILENO 0
#endif
#ifndef STDOUT_FILENO
#define STDOUT_FILENO 1
#endif
#ifndef STDERR_FILENO
#define STDERR_FILENO 2
#endif
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SYS_WAIT_H
#include <sys/wait.h>
#endif
#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef MOUSE_MOVED
#include <process.h>
#include <io.h>
#include <fcntl.h> /* for _O_BINARY only */
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifndef SIGFPE
#ifndef NSIG
#define NSIG 240
#endif
#define SIGFPE NSIG + 1
#endif
#ifdef HAVE_LOCALE_H
#include <locale.h>
#endif
/* library headers for version output */
#ifdef _WIN32
#ifndef __GMP_LIBGMP_DLL
#define __GMP_LIBGMP_DLL 1
#endif
#endif
#ifdef HAVE_GMP_H
#include <gmp.h>
#elif defined HAVE_MPIR_H
#include <mpir.h>
#else
#error either HAVE_GMP_H or HAVE_MPIR_H needs to be defined
#endif
#ifdef WITH_DB
#include <db.h>
#endif
#if defined (HAVE_NCURSESW_NCURSES_H)
#include <ncursesw/ncurses.h>
#define COB_GEN_SCREENIO
#elif defined (HAVE_NCURSESW_CURSES_H)
#include <ncursesw/curses.h>
#define COB_GEN_SCREENIO
#elif defined (HAVE_NCURSES_H)
#include <ncurses.h>
#define COB_GEN_SCREENIO
#elif defined (HAVE_NCURSES_NCURSES_H)
#include <ncurses/ncurses.h>
#define COB_GEN_SCREENIO
#elif defined (HAVE_PDCURSES_H)
#define PDC_NCMOUSE /* use ncurses compatible mouse API */
#include <pdcurses.h>
#define COB_GEN_SCREENIO
#elif defined (HAVE_CURSES_H)
#define PDC_NCMOUSE /* see comment above */
#include <curses.h>
#define COB_GEN_SCREENIO
#ifndef PDC_MOUSE_MOVED
#undef PDC_NCMOUSE
#endif
#endif
#if defined (__PDCURSES__)
/* Note: PDC will internally define NCURSES_MOUSE_VERSION with
a recent version when PDC_NCMOUSE was defined;
for older version define manually! */
#endif
#if defined (WITH_XML2)
#include <libxml/xmlversion.h>
#include <libxml/xmlwriter.h>
#include <libxml/parser.h>
#endif
#if defined (WITH_CJSON)
#if defined (HAVE_CJSON_CJSON_H)
#include <cjson/cJSON.h>
#elif defined (HAVE_CJSON_H)
#include <cJSON.h>
#else
#error CJSON without necessary header
#endif
#elif defined (WITH_JSON_C)
#if defined (HAVE_JSON_C_JSON_H)
#include <json-c/json_c_version.h>
#elif defined (HAVE_JSON_H)
#include <json_c_version.h>
#else
#error JSON-C without necessary header
#endif
#endif
/* end of library headers */
/* include internal and external libcob definitions, forcing exports */
#define COB_LIB_EXPIMP
#include "coblocal.h"
#include "cobgetopt.h"
/* sanity checks */
#if COB_MAX_WORDLEN > 255
#error COB_MAX_WORDLEN is too big, must be less than 256
#endif
#if COB_MAX_NAMELEN > COB_MAX_WORDLEN
#error COB_MAX_NAMELEN is too big, must be less than COB_MAX_WORDLEN
#endif
#define CB_IMSG_SIZE 24
#define CB_IVAL_SIZE (80 - CB_IMSG_SIZE - 4)
/* Stringify macros */
#define CB_STRINGIFY(s) #s
#define CB_XSTRINGIFY(s) CB_STRINGIFY (s)
/* C version info */
#ifdef __VERSION__
#if ! defined (_MSC_VER)
#if defined (__MINGW32__)
#define GC_C_VERSION_PRF "(MinGW) "
#elif defined (__DJGPP__)
#define GC_C_VERSION_PRF "(DJGPP) "
#elif defined (__ORANGEC__)
#define GC_C_VERSION_PRF "(OrangeC) "
#else
#define GC_C_VERSION_PRF ""
#endif
#elif defined (__c2__)
#define GC_C_VERSION_PRF "(Microsoft C2) "
#elif defined (__llvm__)
#define GC_C_VERSION_PRF "(LLVM / MSC) "
#else
#define GC_C_VERSION_PRF "(Microsoft) "
#endif
#define GC_C_VERSION CB_XSTRINGIFY (__VERSION__)
#elif defined (__xlc__)
#define GC_C_VERSION_PRF "(IBM XL C/C++) "
#define GC_C_VERSION CB_XSTRINGIFY (__xlc__)
#elif defined (__SUNPRO_C)
#define GC_C_VERSION_PRF "(Sun C) "
#define GC_C_VERSION CB_XSTRINGIFY (__SUNPRO_C)
#elif defined (_MSC_VER)
#define GC_C_VERSION_PRF "(Microsoft) "
#define GC_C_VERSION CB_XSTRINGIFY (_MSC_VER)
#elif defined (__BORLANDC__)
#define GC_C_VERSION_PRF "(Borland) "
#define GC_C_VERSION CB_XSTRINGIFY (__BORLANDC__)
#elif defined (__WATCOMC__)
#define GC_C_VERSION_PRF "(Watcom) "
#define GC_C_VERSION CB_XSTRINGIFY (__WATCOMC__)
#elif defined (__INTEL_COMPILER)
#define GC_C_VERSION_PRF "(Intel) "
#define GC_C_VERSION CB_XSTRINGIFY (__INTEL_COMPILER)
#elif defined(__TINYC__)
#define GC_C_VERSION_PRF "(Tiny C) "
#define GC_C_VERSION CB_XSTRINGIFY(__TINYC__)
#elif defined(__HP_cc)
#define GC_C_VERSION_PRF "(HP aC++/ANSI C) "
#define GC_C_VERSION CB_XSTRINGIFY(__HP_cc)
#elif defined(__hpux) || defined(_HPUX_SOURCE)
#if defined(__ia64)
#define GC_C_VERSION_PRF "(HPUX IA64) "
#else
#define GC_C_VERSION_PRF "(HPUX PA-RISC) "
#endif
#define GC_C_VERSION " C"
#else
#define GC_C_VERSION_PRF ""
#define GC_C_VERSION _("unknown")
#endif
#if COB_MAX_UNBOUNDED_SIZE > COB_MAX_FIELD_SIZE
#define COB_MAX_ALLOC_SIZE COB_MAX_UNBOUNDED_SIZE
#else
#define COB_MAX_ALLOC_SIZE COB_MAX_FIELD_SIZE
#endif
/* Global variables */
#define SPACE_16 " "
#define SPACE_64 SPACE_16 SPACE_16 SPACE_16 SPACE_16
#define SPACE_256 SPACE_64 SPACE_64 SPACE_64 SPACE_64
#define SPACE_1024 SPACE_256 SPACE_256 SPACE_256 SPACE_256
const char *COB_SPACES_ALPHABETIC = SPACE_1024;
#undef SPACE_16
#undef SPACE_64
#undef SPACE_256
#undef SPACE_1024
#define ZERO_16 "0000000000000000"
#define ZERO_64 ZERO_16 ZERO_16 ZERO_16 ZERO_16
#define ZERO_256 ZERO_64 ZERO_64 ZERO_64 ZERO_64
const char *COB_ZEROES_ALPHABETIC = ZERO_256;
#undef ZERO_16
#undef ZERO_64
#undef ZERO_256
struct cob_alloc_cache {
struct cob_alloc_cache *next; /* Pointer to next */
void *cob_pointer; /* Pointer to malloced space */
size_t size; /* Item size */
};
const int MAX_MODULE_ITERS = 10240;
struct cob_alloc_module {
struct cob_alloc_module *next; /* Pointer to next */
void *cob_pointer; /* Pointer to malloced space */
};
/* EXTERNAL structure */
struct cob_external {
struct cob_external *next; /* Pointer to next */
void *ext_alloc; /* Pointer to malloced space */
char *ename; /* External name */
int esize; /* Item size */
};
#define COB_ERRBUF_SIZE 1024
/* Local variables */
static int cob_initialized = 0;
static int check_mainhandle = 1;
static int cob_argc = 0;
static char **cob_argv = NULL;
static struct cob_alloc_cache *cob_alloc_base = NULL;
static struct cob_alloc_module *cob_module_list = NULL;
static cob_module *cob_module_err = NULL;
static const char *cob_last_sfile = NULL;
static const char *cob_last_progid = NULL;
static cob_global *cobglobptr = NULL;
static cob_settings *cobsetptr = NULL;
static int last_exception_code; /* Last exception: code */
static int active_error_handler = 0;
static int cannot_check_subscript = 0;
static const cob_field_attr const_alpha_attr =
{COB_TYPE_ALPHANUMERIC, 0, 0, 0, NULL};
static const cob_field_attr const_bin_nano_attr =
{COB_TYPE_NUMERIC_BINARY, 20, 9,
COB_FLAG_HAVE_SIGN, NULL};
static char *cob_local_env = NULL;
static int current_arg = 0;
static unsigned char *commlnptr = NULL;
static size_t commlncnt = 0;
static size_t cob_local_env_size = 0;
static struct cob_external *basext = NULL;
static size_t sort_nkeys = 0;
static cob_file_key *sort_keys = NULL;
static const unsigned char *sort_collate = NULL;
static const char *cob_source_file = NULL;
static unsigned int cob_source_line = 0;
#ifdef HAVE_DESIGNATED_INITS
const char *cob_statement_name[STMT_MAX_ENTRY] = {
[STMT_UNKNOWN] = "UNKNOWN"
/* note: STMT_UNKNOWN left out here */
#define COB_STATEMENT(ename,str) , [ename] = str
#include "statement.def" /* located and installed next to common.h */
#undef COB_STATEMENT
};
#else
const char *cob_statement_name[STMT_MAX_ENTRY];
static void init_statement_list (void);
#endif
int cob_statement_hash[STMT_MAX_ENTRY] = { 0 };
#ifdef COB_DEBUG_LOG
static int cob_debug_log_time = 0;
static FILE *cob_debug_file = NULL;
static int cob_debug_level = 9;
static char *cob_debug_mod = NULL;
static char cob_debug_modules[12][4] = {"", "", "", "", "", "", "", "", "", "", "", ""};
static char *cob_debug_file_name = NULL;
#endif
static char *strbuff = NULL;
static int cob_process_id = 0;
static int cob_temp_iteration = 0;
static unsigned int conf_runtime_error_displayed = 0;
static unsigned int last_runtime_error_line = 0;
static const char *last_runtime_error_file = NULL;
static char runtime_err_str[COB_ERRBUF_SIZE] = { 0 }; /* emergency buffer */
static int exit_code = 0; /* exit code when leaving libcob */
#ifndef COB_WITHOUT_JMP
/* longjmp buffer used instead of exit() if cob_init_with_return is used */
static jmp_buf return_jmp_buf;
static int return_jmp_buffer_set = 0;
#endif
#if defined (HAVE_SIGNAL_H) && defined (HAVE_SIG_ATOMIC_T)
volatile sig_atomic_t sig_is_handled = 0;
#endif
/* Function Pointer for external signal handling */
static void (*cob_ext_sighdl) (int) = NULL;
#if defined (_MSC_VER) && COB_USE_VC2008_OR_GREATER
static VOID (WINAPI *time_as_filetime_func) (LPFILETIME) = NULL;
#endif
#undef COB_EXCEPTION
#define COB_EXCEPTION(code, tag, name, critical) name,
static const char * const cob_exception_tab_name[] = {
"None", /* COB_EC_ZERO */
#include "exception.def"
"Invalid" /* COB_EC_MAX */
};
#undef COB_EXCEPTION
#define COB_EXCEPTION(code, tag, name, critical) 0x##code,
static const int cob_exception_tab_code[] = {
0, /* COB_EC_ZERO */
#include "exception.def"
0 /* COB_EC_MAX */
};
#undef COB_EXCEPTION
#define EXCEPTION_TAB_SIZE sizeof (cob_exception_tab_code) / sizeof (int)
/* Switches */
#define COB_SWITCH_MAX 36 /* maximum switches, must match cobc/tree.h! */
static int cob_switch[COB_SWITCH_MAX + 1];
/* BCD to Integer translation (full byte -> 0 - 99) */
static unsigned char b2i[]= {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 255, 255, 255, 255, 255, 255,
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 255, 255, 255, 255, 255, 255,
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 255, 255, 255, 255, 255, 255,
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 255, 255, 255, 255, 255, 255,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 255, 255, 255, 255, 255, 255,
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 255, 255, 255, 255, 255, 255,
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 255, 255, 255, 255, 255, 255,
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 255, 255, 255, 255, 255, 255,
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 255, 255, 255, 255, 255, 255,
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 };
#define IS_INVALID_BCD_DATA(c) (b2i[(unsigned char)c] == 255)
/* note: use of table d2i was tested and seen to be
slower for checking validity/invalidity
and only 0.2% faster for GET_D2I than COB_D2I */
#define IS_INVALID_DIGIT_DATA(c) ((unsigned char)(c - '0') > 9) /* not valid digits '0' - '9' */
#define IS_VALID_DIGIT_DATA(c) ((unsigned char)(c - '0') <= 9) /* valid digits '0' - '9' */
/* Runtime exit handling */
static struct exit_handlerlist {
struct exit_handlerlist *next;
int (*proc)(void);
unsigned char priority;
} *exit_hdlrs;
/* Runtime error handling */
static struct handlerlist {
struct handlerlist *next;
int (*proc)(char *s);
} *hdlrs;
/* note: set again (translated) in print_runtime_conf */
static const char *setting_group[] = {" hidden setting ", "CALL configuration",
"File I/O configuration", "Screen I/O configuration", "Miscellaneous",
"System configuration"};
static struct config_enum lwrupr[] = {{"LOWER", "1"}, {"UPPER", "2"}, {"not set", "0"}, {NULL, NULL}};
#if 0 /* boolean "not set" - used for file specific settings (4.x feature) */
static struct config_enum notset[] = {{"not set", "!"}, {NULL, NULL}};
#endif
static struct config_enum never[] = {{"never", "!"}, {NULL, NULL}};
static struct config_enum beepopts[] = {{"FLASH", "1"}, {"SPEAKER", "2"}, {"FALSE", "9"}, {"BEEP", "0"}, {NULL, NULL}};
static struct config_enum timeopts[] = {{"0", "1000"}, {"1", "100"}, {"2", "10"}, {"3", "1"}, {NULL, NULL}};
static struct config_enum syncopts[] = {{"P", "1"}, {NULL, NULL}};
static struct config_enum varseqopts[] = {{"0", "0"}, {"1", "1"}, {"2", "2"}, {"3", "3"}, {NULL, NULL}};
static struct config_enum coeopts[] = {{"0", "0"}, {"1", "1"}, {"2", "2"}, {"3", "3"}, {NULL, NULL}};
static char varseq_dflt[8] = "0";
static unsigned char min_conf_length = 0;
static const char *not_set;
/*
* Table of possible environment variables and/or runtime.cfg parameters:
Env Var name, Name used in run-time config file, Default value (NULL for aliases), Table of Alternate values,
Grouping for display of run-time options, Data type, Location within structure (adds computed length of referenced field),
Set by which runtime.cfg file, value set by a different keyword,
optional: Minimum accepted value, Maximum accepted value
*/
static struct config_tbl gc_conf[] = {
{"COB_LOAD_CASE", "load_case", "0", lwrupr, GRP_CALL, ENV_UINT | ENV_ENUMVAL, SETPOS (name_convert)},
{"COB_PHYSICAL_CANCEL", "physical_cancel", "0", never, GRP_CALL, ENV_BOOL | ENV_ENUMVAL, SETPOS (cob_physical_cancel)},
{"default_cancel_mode", "default_cancel_mode", NULL, NULL, GRP_HIDE, ENV_BOOL | ENV_NOT, SETPOS (cob_physical_cancel)},
{"LOGICAL_CANCELS", "logical_cancels", NULL, NULL, GRP_HIDE, ENV_BOOL | ENV_NOT, SETPOS (cob_physical_cancel)},
{"COB_LIBRARY_PATH", "library_path", NULL, NULL, GRP_CALL, ENV_PATH, SETPOS (cob_library_path)}, /* default value set in cob_init_call() */
{"COB_PRE_LOAD", "pre_load", NULL, NULL, GRP_CALL, ENV_STR, SETPOS (cob_preload_str)},
{"COB_BELL", "bell", "0", beepopts, GRP_SCREEN, ENV_UINT | ENV_ENUMVAL, SETPOS (cob_beep_value)},
{"COB_DEBUG_LOG", "debug_log", NULL, NULL, GRP_HIDE, ENV_FILE, SETPOS (cob_debug_log)},
{"COB_DISABLE_WARNINGS", "disable_warnings", "0", NULL, GRP_MISC, ENV_BOOL | ENV_NOT, SETPOS (cob_display_warn)},
{"COB_ENV_MANGLE", "env_mangle", "0", NULL, GRP_MISC, ENV_BOOL, SETPOS (cob_env_mangle)},
{"COB_REDIRECT_DISPLAY", "redirect_display", "0", NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_disp_to_stderr)},
{"COB_SCREEN_ESC", "screen_esc", "0", NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_use_esc)},
{"COB_SCREEN_EXCEPTIONS", "screen_exceptions", "0", NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_extended_status)},
{"COB_TIMEOUT_SCALE", "timeout_scale", "0", timeopts, GRP_SCREEN, ENV_UINT, SETPOS (cob_timeout_scale)},
{"COB_INSERT_MODE", "insert_mode", "0", NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_insert_mode)},
{"COB_MOUSE_FLAGS", "mouse_flags", "1", NULL, GRP_SCREEN, ENV_UINT, SETPOS (cob_mouse_flags)},
{"MOUSE_FLAGS", "mouse_flags", NULL, NULL, GRP_HIDE, ENV_UINT, SETPOS (cob_mouse_flags)},
#ifdef HAVE_MOUSEINTERVAL /* possibly add an internal option for mouse support, too */
{"COB_MOUSE_INTERVAL", "mouse_interval", "100", NULL, GRP_SCREEN, ENV_UINT, SETPOS (cob_mouse_interval), 0, 166},
#endif
{"COB_SET_DEBUG", "debugging_mode", "0", NULL, GRP_MISC, ENV_BOOL, SETPOS (cob_debugging_mode)},
{"COB_SET_TRACE", "set_trace", "0", NULL, GRP_MISC, ENV_BOOL, SETPOS (cob_line_trace)},
{"COB_TRACE_FILE", "trace_file", NULL, NULL, GRP_MISC, ENV_FILE, SETPOS (cob_trace_filename)},
{"COB_TRACE_FORMAT", "trace_format", "%P %S Line: %L", NULL, GRP_MISC, ENV_STR, SETPOS (cob_trace_format)},
{"COB_STACKTRACE", "stacktrace", "1", NULL, GRP_MISC, ENV_BOOL, SETPOS (cob_stacktrace)},
{"COB_CORE_ON_ERROR", "core_on_error", "0", coeopts, GRP_MISC, ENV_UINT | ENV_ENUMVAL, SETPOS (cob_core_on_error)},
{"COB_CORE_FILENAME", "core_filename", "./core.libcob", NULL, GRP_MISC, ENV_STR, SETPOS (cob_core_filename)},
{"COB_DUMP_FILE", "dump_file", NULL, NULL, GRP_MISC, ENV_FILE, SETPOS (cob_dump_filename)},
{"COB_DUMP_WIDTH", "dump_width", "100", NULL, GRP_MISC, ENV_UINT, SETPOS (cob_dump_width)},
#ifdef _WIN32
/* checked before configuration load if set from environment in cob_common_init() */
{"COB_UNIX_LF", "unix_lf", "0", NULL, GRP_FILE, ENV_BOOL, SETPOS (cob_unix_lf)},
#endif
{"USERNAME", "username", NULL, NULL, GRP_SYSENV, ENV_STR, SETPOS (cob_user_name)}, /* default set in cob_init() */
{"LOGNAME", "logname", NULL, NULL, GRP_HIDE, ENV_STR, SETPOS (cob_user_name)},
#if !defined (_WIN32) || defined (__MINGW32__) /* cygwin does not define _WIN32 */
{"LANG", "lang", NULL, NULL, GRP_SYSENV, ENV_STR, SETPOS (cob_sys_lang)},
#if defined (__linux__) || defined (__CYGWIN__) || defined (__MINGW32__)
{"OSTYPE", "ostype", NULL, NULL, GRP_SYSENV, ENV_STR, SETPOS (cob_sys_type)},
#endif
{"TERM", "term", NULL, NULL, GRP_SYSENV, ENV_STR, SETPOS (cob_sys_term)},
#endif
#if defined (_WIN32) && !defined (__MINGW32__)
{"OS", "ostype", NULL, NULL, GRP_SYSENV, ENV_STR, SETPOS (cob_sys_type)},
#endif
{"COB_FILE_PATH", "file_path", NULL, NULL, GRP_FILE, ENV_PATH, SETPOS (cob_file_path)},
{"COB_VARSEQ_FORMAT", "varseq_format", varseq_dflt, varseqopts, GRP_FILE, ENV_UINT | ENV_ENUM, SETPOS (cob_varseq_type)},
{"COB_LS_FIXED", "ls_fixed", "0", NULL, GRP_FILE, ENV_BOOL, SETPOS (cob_ls_fixed)},
{"STRIP_TRAILING_SPACES", "strip_trailing_spaces", NULL, NULL, GRP_HIDE, ENV_BOOL | ENV_NOT, SETPOS (cob_ls_fixed)},
{"COB_LS_VALIDATE","ls_validate", "1", NULL,GRP_FILE,ENV_BOOL,SETPOS(cob_ls_validate)},
{"COB_LS_NULLS", "ls_nulls", "0", NULL, GRP_FILE, ENV_BOOL, SETPOS (cob_ls_nulls)},
{"COB_LS_SPLIT", "ls_split", "1", NULL, GRP_FILE, ENV_BOOL, SETPOS (cob_ls_split)},
{"COB_SEQ_CONCAT_NAME","seq_concat_name","0",NULL,GRP_FILE,ENV_BOOL,SETPOS(cob_concat_name)},
{"COB_SEQ_CONCAT_SEP","seq_concat_sep","+",NULL,GRP_FILE,ENV_CHAR,SETPOS(cob_concat_sep),1},
{"COB_SORT_CHUNK", "sort_chunk", "256K", NULL, GRP_FILE, ENV_SIZE, SETPOS (cob_sort_chunk), (128 * 1024), (16 * 1024 * 1024)},
{"COB_SORT_MEMORY", "sort_memory", "128M", NULL, GRP_FILE, ENV_SIZE, SETPOS (cob_sort_memory), (1024*1024), 4294967294UL /* max. guaranteed - 1 */},
{"COB_SYNC", "sync", "0", syncopts, GRP_FILE, ENV_BOOL, SETPOS (cob_do_sync)},
#ifdef WITH_DB
{"DB_HOME", "db_home", NULL, NULL, GRP_FILE, ENV_FILE, SETPOS (bdb_home)},
#endif
{"COB_COL_JUST_LRC", "col_just_lrc", "true", NULL, GRP_FILE, ENV_BOOL, SETPOS (cob_col_just_lrc)},
{"COB_DISPLAY_PRINT_PIPE", "display_print_pipe", NULL, NULL, GRP_SCREEN, ENV_STR, SETPOS (cob_display_print_pipe)},
{"COBPRINTER", "printer", NULL, NULL, GRP_HIDE, ENV_STR, SETPOS (cob_display_print_pipe)},
{"COB_DISPLAY_PRINT_FILE", "display_print_file", NULL, NULL, GRP_SCREEN, ENV_STR,SETPOS (cob_display_print_filename)},
{"COB_DISPLAY_PUNCH_FILE", "display_punch_file", NULL, NULL, GRP_SCREEN, ENV_STR,SETPOS (cob_display_punch_filename)},
{"COB_LEGACY", "legacy", NULL, NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_legacy)},
{"COB_EXIT_WAIT", "exit_wait", "1", NULL, GRP_SCREEN, ENV_BOOL, SETPOS (cob_exit_wait)},
{"COB_EXIT_MSG", "exit_msg", NULL, NULL, GRP_SCREEN, ENV_STR, SETPOS (cob_exit_msg)}, /* default set in cob_init_screenio() */
{"COB_CURRENT_DATE" ,"current_date", NULL, NULL, GRP_MISC, ENV_STR, SETPOS (cob_date)},
{"COB_DATE", "date", NULL, NULL, GRP_HIDE, ENV_STR, SETPOS (cob_date)},
{NULL, NULL, 0, 0}
};
#define NUM_CONFIG (sizeof (gc_conf) /sizeof (struct config_tbl) - 1)
#define FUNC_NAME_IN_DEFAULT NUM_CONFIG + 1
/*
* Table of 'signal' supported by this system
*/
static struct signal_table {
short sig; /* Signal number */
short for_set; /* Set via 'cob_set_signal' 1=set if not SIG_IGN */
short for_dump; /* set via 'cob_set_dump_signal' */
short unused;
const char *shortname; /* Short signal name */
const char *description; /* Longer desciption message */
} signals[] = {
#ifdef SIGINT
{SIGINT,1,0,0,"SIGINT"},
#endif
#ifdef SIGHUP
{SIGHUP,1,0,0,"SIGHUP"},
#endif
#ifdef SIGQUIT
{SIGQUIT,1,0,0,"SIGQUIT"},
#endif
#ifdef SIGTERM
{SIGTERM,1,0,0,"SIGTERM"},
#endif
#ifdef SIGEMT
{SIGEMT,1,0,0,"SIGEMT"},
#endif
#ifdef SIGPIPE
{SIGPIPE,1,0,0,"SIGPIPE"},
#endif
#ifdef SIGIO
{SIGIO,1,0,0,"SIGIO"},
#endif
#ifdef SIGSEGV
{SIGSEGV,2,1,0,"SIGSEGV"},
#endif
#ifdef SIGBUS
{SIGBUS,2,1,0,"SIGBUS"},
#endif
{SIGFPE,1,1,0,"SIGFPE"}, /* always defined, if missing */
#ifdef SIGILL
{SIGILL,0,0,0,"SIGILL"},
#endif
#ifdef SIGABRT
{SIGABRT,0,0,0,"SIGABRT"},
#endif
#ifdef SIGKILL
{SIGKILL,0,0,0,"SIGKILL"},
#endif
#ifdef SIGALRM
{SIGALRM,0,0,0,"SIGALRM"},
#endif
#ifdef SIGSTOP
{SIGSTOP,0,0,0,"SIGSTOP"},
#endif
#ifdef SIGCHLD
{SIGCHLD,0,0,0,"SIGCHLD"},
#endif
#ifdef SIGCLD
{SIGCLD,0,0,0,"SIGCLD"},
#endif
{-1,0,0,0,"unknown"}
};
#define NUM_SIGNALS (int)((sizeof (signals) / sizeof (struct signal_table)) - 1)
const char *signal_msgid = "signal";
const char *warning_msgid = "warning: ";
const char *more_stack_frames_msgid = "(more COBOL runtime elements follow...)";
/* Local functions */
static int translate_boolean_to_int (const char* ptr);
static cob_s64_t get_sleep_nanoseconds (cob_field *nano_seconds);
static cob_s64_t get_sleep_nanoseconds_from_seconds (cob_field *decimal_seconds);
static void internal_nanosleep (cob_s64_t nsecs);
static int set_config_val (char *value, int pos);
static char *get_config_val (char *value, int pos, char *orgvalue);
static void cob_dump_module (char *reason);
static char abort_reason[COB_MINI_BUFF] = { 0 };
static unsigned int dump_trace_started; /* ensures that we dump/stacktrace only once */
#define DUMP_TRACE_DONE_DUMP (1U << 0)
#define DUMP_TRACE_DONE_TRACE (1U << 1)
#define DUMP_TRACE_ACTIVE_TRACE (1U << 2)
static void cob_stack_trace_internal (FILE *target, int verbose, int count);
#ifdef COB_DEBUG_LOG
static void cob_debug_open (void);
#endif
static void conf_runtime_error_value (const char *value, const int conf_pos);
static void conf_runtime_error (const int finish_error, const char *fmt, ...);
static void
cob_exit_common (void)
{
struct cob_external *p;
struct cob_external *q;
struct cob_alloc_cache *x;
struct cob_alloc_cache *y;
#ifdef HAVE_SETLOCALE
if (cobglobptr->cob_locale_orig) {
(void) setlocale (LC_ALL, cobglobptr->cob_locale_orig);
cob_free (cobglobptr->cob_locale_orig);
}
if (cobglobptr->cob_locale) {
cob_free (cobglobptr->cob_locale);
}
if (cobglobptr->cob_locale_ctype) {
cob_free (cobglobptr->cob_locale_ctype);
}
if (cobglobptr->cob_locale_collate) {
cob_free (cobglobptr->cob_locale_collate);
}
if (cobglobptr->cob_locale_messages) {
cob_free (cobglobptr->cob_locale_messages);
}
if (cobglobptr->cob_locale_monetary) {
cob_free (cobglobptr->cob_locale_monetary);
}
if (cobglobptr->cob_locale_numeric) {
cob_free (cobglobptr->cob_locale_numeric);
}
if (cobglobptr->cob_locale_time) {
cob_free (cobglobptr->cob_locale_time);
}
#endif
if (commlnptr) {
cob_free (commlnptr);
}
if (cob_local_env) {
cob_free (cob_local_env);
}
/* Free library routine stuff */
if (cobglobptr->cob_term_buff) {
cob_free (cobglobptr->cob_term_buff);
}
/* Free cached externals */
for (p = basext; p;) {
q = p;
p = p->next;
if (q->ename) {
cob_free (q->ename);
}
if (q->ext_alloc) {
cob_free (q->ext_alloc);
}
cob_free (q);
}
/* Free cached mallocs */
for (x = cob_alloc_base; x;) {
y = x;
x = x->next;
cob_free (y->cob_pointer);
cob_free (y);
}
/* Free last stuff */
if (cob_last_sfile) {
cob_free ((void *)cob_last_sfile);
}
if (cobglobptr) {
if (cobglobptr->cob_main_argv0) {
cob_free ((void *)(cobglobptr->cob_main_argv0));
}
cob_free (cobglobptr);
cobglobptr = NULL;
}
if (cobsetptr) {
void *data;
char *str;
unsigned int i;
if (cobsetptr->cob_config_file) {
for (i = 0; i < cobsetptr->cob_config_num; i++) {
if (cobsetptr->cob_config_file[i]) {
cob_free ((void *)cobsetptr->cob_config_file[i]);
}
}
cob_free ((void *)cobsetptr->cob_config_file);
}
/* Free all strings pointed to by cobsetptr */
for (i = 0; i < NUM_CONFIG; i++) {
if ((gc_conf[i].data_type & ENV_STR)
|| (gc_conf[i].data_type & ENV_FILE)
|| (gc_conf[i].data_type & ENV_PATH)) { /* String/Path to be stored as a string */
data = (void *)((char *)cobsetptr + gc_conf[i].data_loc);
memcpy (&str, data, sizeof (char *));
if (str != NULL) {
cob_free ((void *)str);
str = NULL;
memcpy (data, &str, sizeof (char *)); /* Reset pointer to NULL */
}
}
}
if (cobsetptr->cob_preload_str_set) {
cob_free((void*)(cobsetptr->cob_preload_str_set));
}
cob_free (cobsetptr);
cobsetptr = NULL;
}
cob_initialized = 0;
}
static void
cob_exit_common_modules (void)
{
cob_module *mod;
struct cob_alloc_module *ptr, *nxt;
int (*cancel_func)(const int);
/* Call each module to release local memory
- currently used for: decimals -
and remove it from the internal module list */
for (ptr = cob_module_list; ptr; ptr = nxt) {
mod = ptr->cob_pointer;
nxt = ptr->next;
if (mod && mod->module_cancel.funcint) {
mod->module_active = 0;
cancel_func = mod->module_cancel.funcint;
(void)cancel_func (-20); /* Clear just decimals */
}
cob_free (ptr);
}
cob_module_list = NULL;
}
static void
ss_terminate_routines (void)
{
if (!cob_initialized || !cobglobptr) {
return;
}
cob_exit_fileio_msg_only ();
cob_exit_screen_from_signal(1);
if (COB_MODULE_PTR && abort_reason[0] != 0) {
if (cobsetptr->cob_stacktrace) {
if (!(dump_trace_started & (DUMP_TRACE_DONE_TRACE | DUMP_TRACE_ACTIVE_TRACE))) {
dump_trace_started |= DUMP_TRACE_DONE_TRACE;
dump_trace_started |= DUMP_TRACE_ACTIVE_TRACE;
cob_stack_trace_internal (stderr, 1, 0);
dump_trace_started ^= DUMP_TRACE_ACTIVE_TRACE;
}
}
/* note: no dump code here as getting all the data out of the modules
isn't signal-safe at all; the code to write it out isn't either */
}
}
static void
cob_terminate_routines (void)
{
if (!cob_initialized || !cobglobptr) {
return;
}
fflush (stderr);
cob_exit_fileio_msg_only ();
if (COB_MODULE_PTR && abort_reason[0] != 0) {
if (cobsetptr->cob_stacktrace) {
if (!(dump_trace_started & (DUMP_TRACE_DONE_TRACE | DUMP_TRACE_ACTIVE_TRACE))) {
dump_trace_started |= DUMP_TRACE_DONE_TRACE;
dump_trace_started |= DUMP_TRACE_ACTIVE_TRACE;
cob_stack_trace_internal (stderr, 1, 0);
dump_trace_started ^= DUMP_TRACE_ACTIVE_TRACE;
}
}
if (!(dump_trace_started & DUMP_TRACE_DONE_DUMP)) {
dump_trace_started |= DUMP_TRACE_DONE_DUMP;
cob_dump_module (abort_reason);
}
}
if (cobsetptr->cob_dump_file == cobsetptr->cob_trace_file
|| cobsetptr->cob_dump_file == stderr) {
cobsetptr->cob_dump_file = NULL;
}
if (cobsetptr->cob_dump_file) {
fclose (cobsetptr->cob_dump_file);
cobsetptr->cob_dump_file = NULL;
}
#ifdef COB_DEBUG_LOG
/* close debug log (delete file if empty) */
if (cob_debug_file
&& cob_debug_file != stderr) {
/* note: cob_debug_file can only be identical to cob_trace_file
if same file name was used, not with external_trace_file */
if (cob_debug_file == cobsetptr->cob_trace_file) {
cobsetptr->cob_trace_file = NULL;
}
if (cob_debug_file_name != NULL
&& ftell (cob_debug_file) == 0) {
fclose (cob_debug_file);
unlink (cob_debug_file_name);
} else {
fclose (cob_debug_file);
}
}
cob_debug_file = NULL;
if (cob_debug_file_name) {
cob_free (cob_debug_file_name);
cob_debug_file_name = NULL;
}
#endif
if (cobsetptr->cob_trace_file
&& cobsetptr->cob_trace_file != stderr
&& !cobsetptr->external_trace_file /* note: may include stdout */) {
fclose (cobsetptr->cob_trace_file);
}
cobsetptr->cob_trace_file = NULL;
/* close punch file if self-opened */
if (cobsetptr->cob_display_punch_file
&& cobsetptr->cob_display_punch_filename) {
fclose (cobsetptr->cob_display_punch_file);
cobsetptr->cob_display_punch_file = NULL;
}
cob_exit_screen ();
cob_exit_fileio ();
cob_exit_reportio ();
cob_exit_mlio ();
cob_exit_intrinsic ();
cob_exit_strings ();
cob_exit_numeric ();
cob_exit_common_modules ();
cob_exit_call ();
cob_exit_common ();
}
static void
cob_get_source_line ()
{
if (cobglobptr
&& COB_MODULE_PTR) {
cob_module *mod = COB_MODULE_PTR;
while (mod && mod->module_stmt == 0) {
mod = mod->next;
}
if (mod
&& mod->module_stmt != 0
&& mod->module_sources) {
cob_source_file =
mod->module_sources[COB_GET_FILE_NUM (mod->module_stmt)];
cob_source_line = COB_GET_LINE_NUM (mod->module_stmt);
}
#if 0 /* note: those are also manually set
and therefore may not be reset here */
} else {
cob_source_file = NULL;
cob_source_line = 0;
#endif
}
}
/* reentrant version of strerror */
static char *
cob_get_strerror (void)
{
size_t size;
char *ret;
#ifdef HAVE_STRERROR
char *msg = strerror (errno);
size = strlen (msg);
#else
char msg[COB_ERRBUF_SIZE];
size = snprintf (msg, COB_ERRBUF_SIZE, _("system error %d"), errno);
if (size >= COB_ERRBUF_SIZE) {
/* very unlikely, but if that would be a bad msg catalog
we don't want to memcpy from invalid data */
size = COB_ERRBUF_SIZE - 1;
}
#endif
ret = cob_cache_malloc (size + 1);
memcpy (ret, msg, size + 1);
return ret;
}
static char ss_itoa_buf[12];
/* signal safe integer to string conversion,;
operates on ss_itoa_buff, returns length;
uses ideas of Shaoquan's
https://github.com/wsq003/itoa_for_linux/blob/master/itoa.c */
static size_t
ss_itoa_u10 (int value)
{
const unsigned int radix = 10;
char *p, *p2;
unsigned int digit;
unsigned int u;
size_t len;
p = ss_itoa_buf;
if (value < 0) {
*p++ = '-';
value = 0 - value;
}
u = (unsigned int)value;
p2 = p;
do {
digit = u % radix;
u /= radix;
*p++ = COB_I2D (digit);
} while (u > 0);
len = p - ss_itoa_buf;
*p-- = 0;
/* swap around */
do {
char temp = *p;
*p-- = *p2;
*p2++ = temp;
} while (p2 < p);
return len;
}
static COB_INLINE void
set_source_location (const char **file, unsigned int *line)
{
*file = cob_source_file; /* may be NULL */
*line = cob_source_line; /* may be zero */
if (cobglobptr) {
const cob_module *mod = COB_MODULE_PTR;
if (mod
&& mod->module_stmt != 0
&& mod->module_sources) {
*file = mod->module_sources[COB_GET_FILE_NUM(mod->module_stmt)];
*line = COB_GET_LINE_NUM(mod->module_stmt);
}
}
}
/* write integer to stderr using fixed buffer */
#define write_to_stderr_or_return_int(i) \
if (write (STDERR_FILENO, ss_itoa_buf, ss_itoa_u10 (i)) == -1) return
/* write char array (constant C string) to stderr */
#define write_to_stderr_or_return_arr(ch_arr) \
if (write (STDERR_FILENO, ch_arr, sizeof (ch_arr) - 1) == -1) return
/* write string to stderr, byte count computed with strlen,
str is evaluated twice */
#define write_to_stderr_or_return_str(str) \
if (write (STDERR_FILENO, str, strlen (str)) == -1) return
/* write integer to fileno using fixed buffer */
#define write_or_return_int(fileno,i) \
if (write (fileno, ss_itoa_buf, ss_itoa_u10 (i)) == -1) return
/* write char array (constant C string) to fileno */
#define write_or_return_arr(fileno, ch_arr) \
if (write (fileno, ch_arr, sizeof (ch_arr) - 1) == -1) return
/* write string to fileno, byte count computed with strlen,
str is evaluated twice */
#define write_or_return_str(fileno,str) \
if (write (fileno, str, strlen (str)) == -1) return
#if 0 /* unused */
/* write buffer with given byte count to stderr */
#define write_to_stderr_or_return_buf(buff,count) \
if (write (STDERR_FILENO, buff, count) == -1) return
/* write buffer with given byte count to fileno */
#define write_or_return_buf(fileno,buff,count) \
if (write (fileno, buff, count) == -1) return
#endif
static void
output_source_location (void)
{
const char *source_file;
unsigned int source_line;
set_source_location (&source_file, &source_line);
if (source_file) {
write_to_stderr_or_return_str (source_file);
if (source_line) {
write_to_stderr_or_return_arr (":");
write_to_stderr_or_return_int ((int)source_line);
}
write_to_stderr_or_return_arr (": ");
}
}
static int
create_dumpfile (void)
{
/* Note: this function is not called from a signal handler so may use stdio */
char cmd [COB_NORMAL_BUFF];
const char *default_name = "./core.libcob";
const char *envval;
size_t content_size;
int ret;
if (cobsetptr) {
envval = cobsetptr->cob_core_filename;
} else {
envval = cob_getenv_direct ("COB_CORE_FILENAME");
}
if (envval == NULL) {
envval = default_name;
}
content_size = snprintf (cmd, COB_NORMAL_BUFF, "gcore -a -o %s %d", envval, cob_sys_getpid ());
/* LCOV_EXCL_START */
if (content_size >= COB_NORMAL_BUFF) {
/* in unlikely case of "cob_core_filename is too long" use the simple one */
sprintf (cmd, "gcore -a -o %s %d", default_name, cob_sys_getpid ());
}
/* LCOV_EXCL_STOP */
ret = system (cmd);
if (ret) {
fprintf (stderr, "\nlibcob: ");
fprintf (stderr, _("requested coredump creation failed with status %d"), ret);
fprintf (stderr, "\n\t%s\t%s\n", _("executing:"), (char *)cmd);
}
return ret;
}
#ifdef HAVE_SIGNAL_H
static void
cob_sig_handler (int sig)
{
const char *signal_name;
const char *msg;
char signal_text[COB_MINI_BUFF] = { 0 };
size_t str_len;
#if defined (HAVE_SIGACTION) && !defined (SA_RESETHAND)
struct sigaction sa;
#endif
#ifdef HAVE_SIG_ATOMIC_T
if (sig_is_handled) {
#ifdef HAVE_RAISE
raise (sig);
#else
kill (getpid (), sig);
#endif
exit (sig);
}
sig_is_handled = 1;
#endif
#if 0 /* We do not generally flush whatever we may have in our streams
as stdio is not signal-safe;
we _may_ do this if not SIGSEGV/SIGBUS/SIGABRT */
fflush (stdout);
fflush (stderr);
#endif
signal_name = cob_get_sig_name (sig);
/* LCOV_EXCL_START */
if (signal_name == signals[NUM_SIGNALS].shortname) {
/* not translated as it is a very unlikely error case */
signal_name = signals[NUM_SIGNALS].description; /* translated unknown */
write_to_stderr_or_return_arr ("\ncob_sig_handler caught not handled signal: ");
write_to_stderr_or_return_int (sig);
write_to_stderr_or_return_arr ("\n");
}
/* LCOV_EXCL_STOP */
/* Skip dumping for SIGTERM, SIGINT and "other process" issue's SIGHUP, SIGPIPE */
switch (sig) {
case -1:
#ifdef SIGTERM
case SIGTERM:
#endif
#ifdef SIGINT
case SIGINT:
#endif
#ifdef SIGHUP
case SIGHUP:
#endif
#ifdef SIGPIPE
case SIGPIPE:
#endif
dump_trace_started |= DUMP_TRACE_DONE_DUMP;
/* Fall-through */
default:
break;
}
#ifdef HAVE_SIGACTION
#ifndef SA_RESETHAND
memset (&sa, 0, sizeof (sa));
sa.sa_handler = SIG_DFL;
(void)sigemptyset (&sa.sa_mask);
(void)sigaction (sig, &sa, NULL);
#endif
#else
(void)signal (sig, SIG_DFL);
#endif
cob_exit_screen ();
write_to_stderr_or_return_arr ("\n");
cob_get_source_line ();
output_source_location ();
msg = cob_get_sig_description (sig);
write_to_stderr_or_return_str (msg);
/* setup "signal %s" */
str_len = strlen (signal_msgid);
memcpy (signal_text, signal_msgid, str_len++);
signal_text[str_len] = ' ';
memcpy (signal_text + str_len, signal_name, strlen (signal_name));
write_to_stderr_or_return_arr (" (");
write_to_stderr_or_return_str (signal_text);
write_to_stderr_or_return_arr (")\n\n");
if (cob_initialized) {
if (abort_reason[0] == 0) {
memcpy (abort_reason, signal_text, COB_MINI_BUFF);
#if 0 /* Is there a use in this message ?*/
write_to_stderr_or_return_str (abnormal_termination_msgid);
write_to_stderr_or_return_arr ("\n");
#endif
}
}
/* early coredump if requested would be nice,
but that is not signal-safe so do SIGABRT later... */
if (cobsetptr && cobsetptr->cob_core_on_error == 3) {
cobsetptr->cob_core_on_error = 4;
}
switch (sig) {
case -1:
#ifdef SIGSEGV
case SIGSEGV:
#endif
#ifdef SIGBUS
case SIGBUS:
#endif
#ifdef SIGABRT
case SIGABRT:
#endif
if (cobsetptr && cobsetptr->cob_core_on_error != 0) {
ss_terminate_routines ();
break;
}
/* Fall-through */
default:
cob_terminate_routines ();
break;
}
/* call external signal handler if registered */
if (cob_ext_sighdl != NULL) {
(*cob_ext_sighdl) (sig);
cob_ext_sighdl = NULL;
}
exit_code = sig;
#ifndef COB_WITHOUT_JMP
if (return_jmp_buffer_set) {
longjmp (return_jmp_buf, -3);
}
#endif
/* if an explicit requested coredump could not
be created - raise SIGABRT here */
if (cobsetptr && cobsetptr->cob_core_on_error == 4) {
sig = SIGABRT;
}
signal (sig, SIG_DFL);
#ifdef HAVE_RAISE
raise (sig);
#else
kill (cob_sys_getpid (), sig);
#endif
#if 0 /* we don't necessarily want the OS to handle this,
so exit in all other cases*/
exit (sig);
#endif
}
#endif /* HAVE_SIGNAL_H */
/* Raise signal (run both internal and external handlers)
may return, depending on the signal
*/
void
cob_raise (int sig)
{
#ifdef HAVE_SIGNAL_H
/* let the registered signal handlers do their work */
#ifdef HAVE_RAISE
raise (sig);
#else
kill (cob_sys_getpid (), sig);
#endif
/* else: at least call external signal handler if registered */
#else
if (cob_ext_sighdl != NULL) {
(*cob_ext_sighdl) (sig);
cob_ext_sighdl = NULL;
}
#endif
}
static COB_INLINE struct signal_table
get_signal_entry (int sig)
{
unsigned int k;
for (k = 0; k < NUM_SIGNALS; k++) {
if (signals[k].sig == sig)
break;
}
/* if we didn't found a matching signal we end with the max: "unknown" */
return signals[k];
}
const char *
cob_get_sig_name (int sig)
{
struct signal_table signal_entry = get_signal_entry (sig);
return signal_entry.shortname;
}
const char *
cob_get_sig_description (int sig)
{
struct signal_table signal_entry = get_signal_entry (sig);
/* LCOV_EXCL_START */
if (!signal_entry.description) {
/* in theory we could get here before pre-initialization was finished,
and would want to have _anything_ to return then */
return "unknown";
}
/* LCOV_EXCL_STOP */
return signal_entry.description;
}
static void
cob_init_sig_descriptions (void)
{
int k;
for (k = 0; k <= NUM_SIGNALS; k++) {
/* always defined, if missing */
if (signals[k].sig == SIGFPE) {
signals[k].description = _("fatal arithmetic error");
#ifdef SIGINT
} else if (signals[k].sig == SIGINT) {
signals[k].description = _("interrupt from keyboard");
#endif
#ifdef SIGHUP
} else if (signals[k].sig == SIGHUP) {
signals[k].description = _("hangup");
#endif
#ifdef SIGQUIT
} else if (signals[k].sig == SIGQUIT) {
signals[k].description = _("quit");
#endif
#ifdef SIGTERM
} else if (signals[k].sig == SIGTERM) {
signals[k].description = _("termination");
#endif
#ifdef SIGEMT
} else if (signals[k].sig == SIGEMT) {
signals[k].description = _("emt termination");
#endif
#ifdef SIGPIPE
} else if (signals[k].sig == SIGPIPE) {
signals[k].description = _("broken pipe");
#endif
#ifdef SIGIO
} else if (signals[k].sig == SIGIO) {
signals[k].description = _("I/O signal");
#endif
#ifdef SIGSEGV
} else if (signals[k].sig == SIGSEGV) {
signals[k].description = _("attempt to reference invalid memory address");
#endif
#ifdef SIGBUS
} else if (signals[k].sig == SIGBUS) {
signals[k].description = _("bus error");
#endif
#ifdef SIGILL
} else if (signals[k].sig == SIGILL) {
signals[k].description = _("illegal instruction");
#endif
#ifdef SIGABRT
} else if (signals[k].sig == SIGABRT) {
signals[k].description = _("abort");
#endif
#ifdef SIGKILL
} else if (signals[k].sig == SIGKILL) {
signals[k].description = _("process killed");
#endif
#ifdef SIGALRM
} else if (signals[k].sig == SIGALRM) {
signals[k].description = _("alarm signal");
#endif
#ifdef SIGSTOP
} else if (signals[k].sig == SIGSTOP) {
signals[k].description = _("stop process");
#endif
#ifdef SIGCHLD
} else if (signals[k].sig == SIGCHLD) {
signals[k].description = _("child process stopped");
#endif
#ifdef SIGCLD
} else if (signals[k].sig == SIGCLD) {
signals[k].description = _("child process stopped");
#endif
} else {
signals[k].description = _("unknown");
}
}
/* TRANSLATORS: This msgid is used for an OS signal like SIGABRT. */
signal_msgid = _("signal");
/* TRANSLATORS: This msgid is shown for a requested but not complete stack trace. */
more_stack_frames_msgid = _("(more COBOL runtime elements follow...)");
#if 0 /* Is there a use in this message ?*/
abnormal_termination_msgid = (_("abnormal termination - file contents may be incorrect");
#endif
warning_msgid = _("warning: ");
}
static void
cob_set_signal (void)
{
#if defined (HAVE_SIGNAL_H)
int k;
#ifdef HAVE_SIGACTION
struct sigaction sa;
struct sigaction osa;
memset (&sa, 0, sizeof (sa));
memset (&osa, 0, sizeof (osa));
sa.sa_handler = cob_sig_handler;
#ifdef SA_RESETHAND
sa.sa_flags = SA_RESETHAND;
#endif
#ifdef SA_NOCLDSTOP
sa.sa_flags |= SA_NOCLDSTOP;
#endif
for (k = 0; k < NUM_SIGNALS; k++) {
if (signals[k].for_set) {
/* Take direct control of some hard errors */
if (signals[k].for_set == 2) {
(void)sigemptyset (&sa.sa_mask);
(void)sigaction (signals[k].sig, &sa, NULL);
} else {
/* for the others: only register if not configured
from the OS side to be ignored */
(void)sigaction (signals[k].sig, NULL, &osa);
if (osa.sa_handler != SIG_IGN) {
(void)sigemptyset (&sa.sa_mask);
(void)sigaction (signals[k].sig, &sa, NULL);
}
/* CHECKME: how should we handle externally registered
error handlers (handler != SIG_DFL)? */
}
}
}
#else /* still defined (HAVE_SIGNAL_H) */
for (k = 0; k < NUM_SIGNALS; k++) {
if (signals[k].for_set) {
/* Take direct control of some hard errors */
if (signals[k].for_set == 2) {
(void)signal (signals[k].sig, cob_sig_handler);
} else {
/* for the others: only register if not configured
from the OS side to be ignored */
if (signal (signals[k].sig, SIG_IGN) != SIG_IGN) {
(void)signal (signals[k].sig, cob_sig_handler);
}
/* CHECKME: how should we handle externally registered
error handlers (handler != SIG_DFL)? */
}
}
}
#endif
#endif
}
/* ASCII Sign - Reading and undo the "overpunch";
* Note: if used on an EBCDIC machine this is actually _not_ an overpunch
* but a replacement!
* positive: 0123456789
* negative: pqrstuvwxy
* returns one of: 1 = positive (non-negative), -1 = negative
*/
static int
cob_get_sign_ascii (unsigned char *p)
{
#ifdef COB_EBCDIC_MACHINE
switch (*p) {
case 'p':
*p = (unsigned char)'0';
return -1;
case 'q':
*p = (unsigned char)'1';
return -1;
case 'r':
*p = (unsigned char)'2';
return -1;
case 's':
*p = (unsigned char)'3';
return -1;
case 't':
*p = (unsigned char)'4';
return -1;
case 'u':
*p = (unsigned char)'5';
return -1;
case 'v':
*p = (unsigned char)'6';
return -1;
case 'w':
*p = (unsigned char)'7';
return -1;
case 'x':
*p = (unsigned char)'8';
return -1;
case 'y':
*p = (unsigned char)'9';
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
/* already without sign */
return -1;
}
*p = (unsigned char)'0';
return 1;
#else
if (*p >= (unsigned char)'p' && *p <= (unsigned char)'y') {
*p &= ~0x40; /* 0x71 'q' -> 0x31 -> '1' */
return -1;
}
if (IS_VALID_DIGIT_DATA (*p)) {
/* already without sign */
return 1;
}
*p = (unsigned char)'0';
return 1;
#endif
}
/* overpunches the pointer 'p' with the sign
* Note: if used on an EBCDIC machine this is actually _not_ an overpunch
* but a replacement! */
static void
cob_put_sign_ascii (unsigned char *p)
{
#ifdef COB_EBCDIC_MACHINE
switch (*p) {
case '0':
*p = (unsigned char)'p';
return;
case '1':
*p = (unsigned char)'q';
return;
case '2':
*p = (unsigned char)'r';
return;
case '3':
*p = (unsigned char)'s';
return;
case '4':
*p = (unsigned char)'t';
return;
case '5':
*p = (unsigned char)'u';
return;
case '6':
*p = (unsigned char)'v';
return;
case '7':
*p = (unsigned char)'w';
return;
case '8':
*p = (unsigned char)'x';
return;
case '9':
*p = (unsigned char)'y';
return;
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
/* already signed */
return;
default:
*p = (unsigned char)'0';
}
#else
*p |= 0x40;
#endif
}
/* EBCDIC Sign - Reading and undo the "overpunch";
* Note: if used on an ASCII machine this is actually _not_ an overpunch
* but a replacement!
* positive: {ABCDEFGHI
* unsigned: 0123456789
* negative: }JKLMNOPQR
* returns one of: 1 = positive (non-negative), -1 = negative, 0 = unsigned
*/
static int
cob_get_sign_ebcdic (unsigned char *p)
{
#ifdef COB_EBCDIC_MACHINE
char sign_nibble = *p & 0xF0;
/* What to do here outside of 0 - 9? */
if ((*p & 0x0F) > 9) {
*p = sign_nibble;
}
switch (sign_nibble) {
/* negative */
case 0xC0:
/* negative, non-preferred */
case 0xA0:
case 0xE0:
return 1;
/* positive */
case 0xD0:
/* positive, non-preferred */
case 0xB0:
return -1;
/* unsigned */
case 0xF0:
return 0;
return -1;
default:
/* What to do here outside of sign nibbles? */
return 1;
}
#else
switch (*p) {
case '{':
*p = (unsigned char)'0';
return 1;
case 'A':
*p = (unsigned char)'1';
return 1;
case 'B':
*p = (unsigned char)'2';
return 1;
case 'C':
*p = (unsigned char)'3';
return 1;
case 'D':
*p = (unsigned char)'4';
return 1;
case 'E':
*p = (unsigned char)'5';
return 1;
case 'F':
*p = (unsigned char)'6';
return 1;
case 'G':
*p = (unsigned char)'7';
return 1;
case 'H':
*p = (unsigned char)'8';
return 1;
case 'I':
*p = (unsigned char)'9';
return 1;
case '}':
*p = (unsigned char)'0';
return -1;
case 'J':
*p = (unsigned char)'1';
return -1;
case 'K':
*p = (unsigned char)'2';
return -1;
case 'L':
*p = (unsigned char)'3';
return -1;
case 'M':
*p = (unsigned char)'4';
return -1;
case 'N':
*p = (unsigned char)'5';
return -1;
case 'O':
*p = (unsigned char)'6';
return -1;
case 'P':
*p = (unsigned char)'7';
return -1;
case 'Q':
*p = (unsigned char)'8';
return -1;
case 'R':
*p = (unsigned char)'9';
return -1;
default:
if (*p >= '0' && *p <= '9') {
return 0;
}
/* What to do here outside of 0 - 9? */
if ((*p & 0x0F) > 9) {
*p = (unsigned char)'0';
} else {
*p = (unsigned char)COB_I2D (COB_D2I (*p));
}
return 1;
}
#endif
}
static void
cob_put_sign_ebcdic (unsigned char *p, const int sign)
{
if (sign == -1) {
switch (*p) {
case '0':
*p = (unsigned char)'}';
return;
case '1':
*p = (unsigned char)'J';
return;
case '2':
*p = (unsigned char)'K';
return;
case '3':
*p = (unsigned char)'L';
return;
case '4':
*p = (unsigned char)'M';
return;
case '5':
*p = (unsigned char)'N';
return;
case '6':
*p = (unsigned char)'O';
return;
case '7':
*p = (unsigned char)'P';
return;
case '8':
*p = (unsigned char)'Q';
return;
case '9':
*p = (unsigned char)'R';
return;
case '}':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
/* already signed */
return;
default:
/* What to do here */
*p = (unsigned char)'}';
return;
}
}
switch (*p) {
case '0':
*p = (unsigned char)'{';
return;
case '1':
*p = (unsigned char)'A';
return;
case '2':
*p = (unsigned char)'B';
return;
case '3':
*p = (unsigned char)'C';
return;
case '4':
*p = (unsigned char)'D';
return;
case '5':
*p = (unsigned char)'E';
return;
case '6':
*p = (unsigned char)'F';
return;
case '7':
*p = (unsigned char)'G';
return;
case '8':
*p = (unsigned char)'H';
return;
case '9':
*p = (unsigned char)'I';
return;
case '{':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
/* already signed */
return;
default:
/* What to do here */
*p = (unsigned char)'{';
return;
}
}
/* compare up to 'size' characters from buffer 'p'
to a single character 'c',
using collation 'col' */
static int
common_cmpc (const unsigned char *p, const unsigned int c,
const size_t size, const unsigned char *col)
{
register const unsigned char *end = p + size;
int ret;
const unsigned char c_col = col[c];
while (p < end) {
if ((ret = col[*p] - c_col) != 0) {
return ret;
}
p++;
}
return 0;
}
/* compare up to 'size' characters in 's1' to 's2'
using collation 'col' */
static int
common_cmps (const unsigned char *s1, const unsigned char *s2,
const size_t size, const unsigned char *col)
{
register const unsigned char *end = s1 + size;
int ret;
while (s1 < end) {
if ((ret = col[*s1] - col[*s2]) != 0) {
return ret;
}
s1++, s2++;
}
return 0;
}
/* compare up to 'size' characters in 'data' to characters
in 'c' with size 'compare_size' */
static int
compare_character (const unsigned char *data, size_t size,
const unsigned char *c, size_t compare_size)
{
int ret;
/* compare date with compare-data up to max compare-size */
if ((ret = memcmp (data, c, compare_size)) != 0) {
return ret;
}
/* first bytes in "data" are identical to "compare-data",
so use first bytes of "data" for next comparisons,
increasing it up to the complete data-size */
{
register const unsigned char *p; /* position to compare from */
size -= compare_size;
while (size > compare_size) {
p = data + compare_size;
if ((ret = memcmp (p, data, compare_size)) != 0) {
return ret;
}
size -= compare_size;
compare_size *= 2;
}
if (size > 0) {
p = data + compare_size;
return memcmp (p, data, size);
}
}
return 0;
}
/* compare up to 'size' characters in 'data' to spaces */
static int
compare_spaces (const unsigned char *data, size_t size)
{
if (size <= COB_SPACES_ALPHABETIC_BYTE_LENGTH) {
return memcmp (data, COB_SPACES_ALPHABETIC, size);
}
return compare_character (data, size,
(const unsigned char *)COB_SPACES_ALPHABETIC,
COB_SPACES_ALPHABETIC_BYTE_LENGTH);
}
/* compare up to 'size' characters in 'data' to zeroes */
static int
compare_zeroes (const unsigned char *data, size_t size)
{
if (size <= COB_ZEROES_ALPHABETIC_BYTE_LENGTH) {
return memcmp (data, COB_ZEROES_ALPHABETIC, size);
}
return compare_character (data, size,
(const unsigned char *)COB_ZEROES_ALPHABETIC,
COB_ZEROES_ALPHABETIC_BYTE_LENGTH);
}
/* compare content of field 'f1' to repeated content of 'f2' */
static int
cob_cmp_all (cob_field *f1, cob_field *f2)
{
const unsigned char *col = COB_MODULE_PTR->collating_sequence;
const unsigned char *data1 = COB_FIELD_DATA (f1);
const unsigned char *data2 = f2->data;
const size_t size1 = COB_FIELD_SIZE (f1);
const size_t size2 = f2->size;
/* CHECKME: What should be returned if f1 is negative? */
/* CHECKME: Do we ever get here with f2 being numeric? */
const int sign = COB_GET_SIGN_ADJUST (f1);
int ret = 0;
/* check without collation */
if (col == NULL) {
if (size2 == 1) {
if (*data2 == ' ') {
/* check for IF VAR = [ALL] SPACE[S] */
ret = compare_spaces (data1, size1);
} else
if (*data2 == '0') {
/* check for IF VAR = [ALL] ZERO[ES] */
ret = compare_zeroes (data1, size1);
} else {
/* check for IF VAR = ALL '5' / HIGH-VALUE / ... */
ret = compare_character (data1, size1, data2, 1);
}
} else
/* check for IF VAR = ALL ... / ... */
if (size1 > size2) {
ret = compare_character (data1, size1, data2, size2);
} else {
ret = compare_character (data1, size1, data2, size1);
}
} else
/* check with collation */
if (size2 == 1) {
/* check for IF VAR = ALL "9" / IF VAR = ZERO */
ret = common_cmpc (data1, *data2, size1, col);
} else {
/* check for IF VAR = ALL "AB" ... */
const size_t chunk_size = size2;
size_t size_loop = size1;
while (size_loop >= chunk_size) {
if ((ret = common_cmps (data1, data2, chunk_size, col)) != 0) {
break;
}
size_loop -= chunk_size;
data1 += chunk_size;
}
if (!ret
&& size1 > 0) {
ret = common_cmps (data1, data2, size_loop, col);
}
}
COB_PUT_SIGN_ADJUSTED (f1, sign);
return ret;
}
/* compare content of field 'f1' to content of 'f2', space padded,
using the optional collating sequence of the program */
static int
cob_cmp_alnum (cob_field *f1, cob_field *f2)
{
const unsigned char *col = COB_MODULE_PTR->collating_sequence;
const unsigned char *data1 = COB_FIELD_DATA (f1);
const unsigned char *data2 = COB_FIELD_DATA (f2);
const size_t size1 = COB_FIELD_SIZE (f1);
const size_t size2 = COB_FIELD_SIZE (f2);
const size_t min = (size1 < size2) ? size1 : size2;
int ret;
if (col == NULL) { /* check without collation */
/* Compare common substring */
if ((ret = memcmp (data1, data2, min)) != 0) {
return ret;
}
/* Compare the rest (if any) with spaces */
if (size1 > size2) {
const size_t spaces_to_test = size1 - min;
return compare_spaces (data1 + min, spaces_to_test);
} else if (size1 < size2) {
const size_t spaces_to_test = size2 - min;
return -compare_spaces (data2 + min, spaces_to_test);
}
} else { /* check with collation */
/* Compare common substring */
if ((ret = common_cmps (data1, data2, min, col)) != 0) {
return ret;
}
/* Compare the rest (if any) with spaces */
if (size1 > size2) {
const size_t spaces_to_test = size1 - min;
return common_cmpc (data1 + min, ' ', spaces_to_test, col);
} else if (size1 < size2) {
const size_t spaces_to_test = size2 - min;
return -common_cmpc (data2 + min, ' ', spaces_to_test, col);
}
}
return 0;
}
/* comparision of all key fields for SORT (without explicit collation)
in records pointed to by 'data1' and 'data2' */
static int
sort_compare (const void *data1, const void *data2)
{
size_t i;
int res;
cob_field f1;
cob_field f2;
for (i = 0; i < sort_nkeys; ++i) {
f1 = f2 = *sort_keys[i].field;
f1.data = (unsigned char *)data1 + sort_keys[i].offset;
f2.data = (unsigned char *)data2 + sort_keys[i].offset;
if (COB_FIELD_IS_NUMERIC (&f1)) {
res = cob_numeric_cmp (&f1, &f2);
} else {
res = memcmp (f1.data, f2.data, f1.size);
}
if (res != 0) {
return (sort_keys[i].flag == COB_ASCENDING) ? res : -res;
}
}
return 0;
}
/* comparision of all key fields for SORT (with explicit collation)
in records pointed to by 'data1' and 'data2' */
static int
sort_compare_collate (const void *data1, const void *data2)
{
size_t i;
int res;
cob_field f1;
cob_field f2;
for (i = 0; i < sort_nkeys; ++i) {
f1 = f2 = *sort_keys[i].field;
f1.data = (unsigned char *)data1 + sort_keys[i].offset;
f2.data = (unsigned char *)data2 + sort_keys[i].offset;
if (COB_FIELD_IS_NUMERIC (&f1)) {
res = cob_numeric_cmp (&f1, &f2);
} else {
res = common_cmps (f1.data, f2.data, f1.size, sort_collate);
}
if (res != 0) {
return (sort_keys[i].flag == COB_ASCENDING) ? res : -res;
}
}
return 0;
}
/* intermediate move using USAGE DISPLAY field to 'dst' using
buffer 'src' with given 'size' as source */
static void
cob_move_intermediate (cob_field *dst, const void *src, const size_t size)
{
cob_field intermediate;
intermediate.size = size;
intermediate.data = (cob_u8_ptr)src;
/* note: if the target is numeric then cob_move will convert
on the fly to numeric as necessary */
intermediate.attr = &const_alpha_attr;
cob_move (&intermediate, dst);
}
/* intermediate move from 'src' to 'dst'
as if it would be of COB_TYPE_ALPANUMERIC */
static void
cob_move_to_group_as_alnum (cob_field *src, cob_field *dst)
{
cob_field intermediate;
cob_field_attr attr;
/* group moves are defined as memcpy + fill, so move shaddow field with
same attributes and data storage but type alnum instead, which will
lead to "unpacked" numeric data in the group */
intermediate = *dst;
intermediate.attr = &attr;
attr = *dst->attr;
attr.type = COB_TYPE_ALPHANUMERIC;
cob_move (src, &intermediate);
}
/* open file using mode according to cob_unix_lf and
filename (append when starting with +) */
static FILE *
cob_open_logfile (const char *filename)
{
const char *mode;
if (!cobsetptr->cob_unix_lf) {
if (*filename == '+') {
filename++;
mode = "a";
} else {
mode = "w";
}
} else {
if (*filename == '+') {
filename++;
mode = "ab";
} else {
mode = "wb";
}
}
return fopen (filename, mode);
}
/* ensure that cob_trace_file is available for writing */
static void
cob_check_trace_file (void)
{
if (cobsetptr->cob_trace_file) {
return;
}
if (cobsetptr->cob_trace_filename) {
cobsetptr->cob_trace_file = cob_open_logfile (cobsetptr->cob_trace_filename);
if (!cobsetptr->cob_trace_file) {
/* could not open the file
unset the filename for not referencing it later */
cobsetptr->cob_trace_filename = NULL;
cobsetptr->cob_trace_file = stderr;
}
} else {
cobsetptr->cob_trace_file = stderr;
}
}
/* close current trace file (if open) and open/attach a new one */
static void
cob_new_trace_file (void)
{
FILE *old_trace_file = cobsetptr->cob_trace_file;
if (!cobsetptr->cob_trace_file
|| cobsetptr->external_trace_file
|| cobsetptr->cob_trace_file == stderr) {
cobsetptr->cob_trace_file = NULL;
cob_check_trace_file ();
return;
}
fclose (cobsetptr->cob_trace_file);
cobsetptr->cob_trace_file = NULL;
cob_check_trace_file ();
if (cobsetptr->cob_display_print_file
&& cobsetptr->cob_display_print_file == old_trace_file) {
cobsetptr->cob_display_print_file = cobsetptr->cob_trace_file;
}
if (cobsetptr->cob_dump_file
&& cobsetptr->cob_dump_file == old_trace_file) {
cobsetptr->cob_dump_file = cobsetptr->cob_trace_file;
}
#ifdef COB_DEBUG_LOG
if (cob_debug_file
&& cob_debug_file == old_trace_file) {
cob_debug_file = cobsetptr->cob_trace_file;
}
#endif
}
int
cob_check_env_true (char * s)
{
return s && ((strlen (s) == 1 && (*s == 'Y' || *s == 'y' || *s == '1'))
|| strcasecmp (s, "YES") == 0 || strcasecmp (s, "ON") == 0
|| strcasecmp (s, "TRUE") == 0);
}
int
cob_check_env_false (char * s)
{
return s && ((strlen (s) == 1 && (*s == 'N' || *s == 'n' || *s == '0'))
|| strcasecmp (s, "NO") == 0 || strcasecmp (s, "NONE") == 0
|| strcasecmp (s, "OFF") == 0 || strcasecmp (s, "FALSE") == 0);
}
static void
cob_rescan_env_vals (void)
{
int i;
int j;
int old_type;
char *env;
char *save_source_file = (char *) cob_source_file;
cob_source_file = NULL;
cob_source_line = 0;
/* Check for possible environment variables */
for (i = 0; i < NUM_CONFIG; i++) {
if (gc_conf[i].env_name
&& (env = getenv (gc_conf[i].env_name)) != NULL) {
old_type = gc_conf[i].data_type;
gc_conf[i].data_type |= STS_ENVSET;
if (*env != '\0' && set_config_val (env, i)) {
gc_conf[i].data_type = old_type;
/* Remove invalid setting */
(void)cob_unsetenv (gc_conf[i].env_name);
} else if (gc_conf[i].env_group == GRP_HIDE) {
/* Any alias present? */
for (j = 0; j < NUM_CONFIG; j++) {
if (j != i
&& gc_conf[i].data_loc == gc_conf[j].data_loc) {
gc_conf[j].data_type |= STS_ENVSET;
gc_conf[j].set_by = i;
}
}
}
}
}
cob_source_file = save_source_file;
/* Extended ACCEPT status returns */
if (cobsetptr->cob_extended_status == 0) {
cobsetptr->cob_use_esc = 0;
}
}
static COB_INLINE int
one_indexed_day_of_week_from_monday (int zero_indexed_from_sunday)
{
return ((zero_indexed_from_sunday + 6) % 7) + 1;
}
#if defined (_MSC_VER) && COB_USE_VC2008_OR_GREATER
static void
set_cob_time_ns_from_filetime (const FILETIME filetime, struct cob_time *cb_time)
{
ULONGLONG filetime_int;
filetime_int = (((ULONGLONG) filetime.dwHighDateTime) << 32)
+ filetime.dwLowDateTime;
/* FILETIMEs are accurate to 100 nanosecond intervals */
cb_time->nanosecond = (filetime_int % (ULONGLONG) 10000000) * 100;
}
#endif
/* Global functions */
/* get last exception (or 0 if not active) */
int
cob_get_last_exception_code (void)
{
return last_exception_code;
}
/* get exception name for last raised exception */
const char *
cob_get_last_exception_name (void)
{
size_t n;
/* direct match */
for (n = 1; n < EXCEPTION_TAB_SIZE; ++n) {
if (last_exception_code == cob_exception_tab_code[n]) {
return cob_exception_tab_name[n];
}
}
/* any match (last to first to get most specific one,
checking missing/not supported first) */
if (cob_last_exception_is (COB_EC_IMP_FEATURE_MISSING)) {
return cob_exception_tab_name[COB_EC_IMP_FEATURE_MISSING];
}
if (cob_last_exception_is (COB_EC_IMP_FEATURE_DISABLED)) {
return cob_exception_tab_name[COB_EC_IMP_FEATURE_DISABLED];
}
for (n = EXCEPTION_TAB_SIZE - 1; n != 0; --n) {
if ((last_exception_code & cob_exception_tab_code[n])
== cob_exception_tab_code[n]) {
return cob_exception_tab_name[n];
}
}
return NULL;
}
/* check if last exception is set and includes the given exception */
int
cob_last_exception_is (const int exception_to_check)
{
if ((last_exception_code & cob_exception_tab_code[exception_to_check])
== cob_exception_tab_code[exception_to_check]) {
return 1;
} else {
return 0;
}
}
/* set last exception,
used for EXCEPTION- functions and for cob_accept_exception_status,
only reset on SET LAST EXCEPTION TO OFF */
void
cob_set_exception (const int id)
{
cobglobptr->cob_exception_code = cob_exception_tab_code[id];
last_exception_code = cobglobptr->cob_exception_code;
cobglobptr->last_exception_statement = STMT_UNKNOWN;
cobglobptr->last_exception_id = NULL;
cobglobptr->last_exception_section = NULL;
cobglobptr->last_exception_paragraph = NULL;
if (id) {
static char excp_mod[COB_MAX_WORDLEN + 1];
static char excp_sec[COB_MAX_WORDLEN + 1];
static char excp_para[COB_MAX_WORDLEN + 1];
cob_module *mod = COB_MODULE_PTR;
cobglobptr->cob_got_exception = 1;
#if 0 /* consider addition for 4.x */
cobglobptr->last_exception_source = cob_source_file; /* needs to be strdup'd */
#endif
cobglobptr->last_exception_line = cob_source_line;
if (mod) {
#if 0 /* consider addition for 4.x */
if (mod->module_sources
&& mod->module_stmt != 0) {
/* note: it is likely best to not copy name + line but store the name+line
together, maybe even comma-separated when we have copy->copy-prog */
cobglobptr->last_exception_source = mod->module_sources
[COB_GET_FILE_NUM (mod->module_stmt)]; /* needs to be strdup'd */
cobglobptr->last_exception_line = COB_GET_LINE_NUM (mod->module_stmt);
#else
if (mod->module_stmt != 0) {
cobglobptr->last_exception_line = COB_GET_LINE_NUM (mod->module_stmt);
#endif
}
cobglobptr->last_exception_statement = mod->statement;
if (mod->module_name) {
strcpy (excp_mod, mod->module_name);
cobglobptr->last_exception_id = excp_mod;
}
if (mod->section_name) {
strcpy (excp_sec, mod->section_name);
cobglobptr->last_exception_section = excp_sec;
}
if (mod->paragraph_name) {
strcpy (excp_para, mod->paragraph_name);
cobglobptr->last_exception_paragraph = excp_para;
}
}
} else {
cobglobptr->cob_got_exception = 0;
#if 0 /* consider addition for 4.x */
if (cobglobptr->last_exception_source) {
cobglobptr->last_exception_source = NULL;
cob_free (cobglobptr->last_exception_source);
}
#endif
cobglobptr->last_exception_line = 0;
}
}
/* add to last exception, set if empty */
void
cob_add_exception (const int id)
{
if (!cobglobptr->cob_exception_code) {
cob_set_exception (id);
return;
}
cobglobptr->cob_exception_code |= cob_exception_tab_code[id];
last_exception_code |= cob_exception_tab_code[id];
}
/* return the last exception value */
void
cob_accept_exception_status (cob_field *f)
{
int exception = last_exception_code;
/* Note: MF set this to a 9(3) item, we do a translation here (only works for USAGE DISPLAY!);
MF: intended for CALL only, 0=ok, 1=ENOMEM, 2=module not found, 128 other CALL failure,
"unpredictable if the last statement was not a CALL, especially: adjusted by fileio" */
if (exception
&& f->size == 3 /* FIXME: current code works only for DISPLAY, adjust to work for other usages */
&& COB_FIELD_TYPE (f) == COB_TYPE_NUMERIC_DISPLAY) {
if (exception == cob_exception_tab_code[COB_EC_PROGRAM_RESOURCES]) {
exception = 1;
} else if (exception == cob_exception_tab_code[COB_EC_PROGRAM_NOT_FOUND]) {
exception = 2;
} else if (exception || cob_exception_tab_code[COB_EC_PROGRAM]) {
exception = 128;
}
}
cob_set_int (f, exception);
}
void
cob_accept_user_name (cob_field *f)
{
if (cobsetptr->cob_user_name) {
cob_move_intermediate (f, cobsetptr->cob_user_name,
strlen (cobsetptr->cob_user_name));
} else {
cob_move_intermediate (f, " ", (size_t)1);
}
}
void *
cob_malloc (const size_t size)
{
void *mptr;
mptr = calloc ((size_t)1, size);
/* LCOV_EXCL_START */
if (unlikely (!mptr)) {
cob_fatal_error (COB_FERROR_MEMORY);
}
/* LCOV_EXCL_STOP */
return mptr;
}
void *
cob_realloc (void * optr, const size_t osize, const size_t nsize)
{
void *mptr;
/* LCOV_EXCL_START */
if (unlikely (!optr)) {
cob_fatal_error (COB_FERROR_FREE);
}
/* LCOV_EXCL_STOP */
if (unlikely (osize == nsize)) { /* No size change */
return optr;
}
if (unlikely (osize > nsize)) { /* Reducing size */
return realloc (optr, nsize);
}
mptr = calloc ((size_t)1, nsize); /* New memory, past old is cleared */
/* LCOV_EXCL_START */
if (unlikely (!mptr)) {
cob_fatal_error (COB_FERROR_MEMORY);
}
/* LCOV_EXCL_STOP */
memcpy (mptr, optr, osize);
cob_free (optr);
return mptr;
}
void
cob_free (void * mptr)
{
#ifdef _DEBUG
/* LCOV_EXCL_START */
if (unlikely (!mptr)) {
cob_fatal_error (COB_FERROR_FREE);
}
/* LCOV_EXCL_STOP */
#endif
free (mptr);
}
void *
cob_fast_malloc (const size_t size)
{
void *mptr;
mptr = malloc (size);
/* LCOV_EXCL_START */
if (unlikely (!mptr)) {
cob_fatal_error (COB_FERROR_MEMORY);
}
/* LCOV_EXCL_STOP */
return mptr;
}
char *
cob_strdup (const char *p)
{
char *mptr;
size_t len;
len = strlen (p) + 1;
mptr = (char *)cob_fast_malloc (len);
memcpy (mptr, p, len);
return mptr;
}
char *
cob_strndup (const char *p, const size_t max_len)
{
char *mptr;
size_t len;
len = strlen (p);
if (len > max_len) {
len = max_len;
}
mptr = (char *)cob_fast_malloc (len + 1);
memcpy (mptr, p, len);
*(mptr + len) = 0;
return mptr;
}
/* Caching versions of malloc/free */
void *
cob_cache_malloc (const size_t size)
{
struct cob_alloc_cache *cache_ptr;
void *mptr;
cache_ptr = cob_malloc (sizeof (struct cob_alloc_cache));
mptr = cob_malloc (size);
cache_ptr->cob_pointer = mptr;
cache_ptr->size = size;
cache_ptr->next = cob_alloc_base;
cob_alloc_base = cache_ptr;
return mptr;
}
void *
cob_cache_realloc (void *ptr, const size_t size)
{
struct cob_alloc_cache *cache_ptr;
void *mptr;
if (!ptr) {
return cob_cache_malloc (size);
}
cache_ptr = cob_alloc_base;
for (; cache_ptr; cache_ptr = cache_ptr->next) {
if (ptr == cache_ptr->cob_pointer) {
if (size <= cache_ptr->size) {
return ptr;
}
mptr = cob_malloc (size);
memcpy (mptr, cache_ptr->cob_pointer, cache_ptr->size);
cob_free (cache_ptr->cob_pointer);
cache_ptr->cob_pointer = mptr;
cache_ptr->size = size;
return mptr;
}
}
return ptr;
}
void
cob_cache_free (void *ptr)
{
struct cob_alloc_cache *cache_ptr;
struct cob_alloc_cache *prev_ptr;
if (!ptr) {
return;
}
cache_ptr = cob_alloc_base;
prev_ptr = cob_alloc_base;
for (; cache_ptr; cache_ptr = cache_ptr->next) {
if (ptr == cache_ptr->cob_pointer) {
cob_free (cache_ptr->cob_pointer);
if (cache_ptr == cob_alloc_base) {
cob_alloc_base = cache_ptr->next;
} else {
prev_ptr->next = cache_ptr->next;
}
cob_free (cache_ptr);
return;
}
prev_ptr = cache_ptr;
}
}
static COB_INLINE int
hash (const char *s)
{
register const char *p = s;
register int val = 0;
while (*p) {
val += *p++;
}
return val;
}
static COB_INLINE void
init_statement_hashlist (void)
{
cob_statement_hash[STMT_UNKNOWN] = hash("UNKNOWN");
#define COB_STATEMENT(ename,str) \
cob_statement_hash[ename] = hash(str);
#include "statement.def" /* located and installed next to common.h */
#undef COB_STATEMENT
}
/* this function is a compat-only function for pre 3.2 */
static enum cob_statement
get_stmt_from_name (const char *stmt_name)
{
/* Note: When called from the same module then we commonly will
get the same pointer for the same statement name;
this allows us to use that for a pre-lookup instead of doing
the more expensive hash + compare each time */
#define STMT_CACHE_NUM 10
static const char *last_stmt_name[STMT_CACHE_NUM] = { 0 };
static enum cob_statement last_stmt[STMT_CACHE_NUM] = { 0 };
static unsigned int last_stmt_entry = STMT_CACHE_NUM - 1;
static unsigned int last_stmt_i = 0;
/* nothing to resolve */
if (!stmt_name) {
return STMT_UNKNOWN;
}
/* check if stmt_name was previously resolved, then
just go with the old result */
if (stmt_name == last_stmt_name[last_stmt_i]) {
return last_stmt[last_stmt_i];
} else {
unsigned int i;
for (i = 0; i < STMT_CACHE_NUM; i++) {
if (stmt_name == last_stmt_name[i]) {
last_stmt_i = i;
return last_stmt[i];
}
}
}
/* statement name not resolved - build hash and compare using
hash + strcmp; then add to previously resolved list */
{
const int stmt_hash = hash (stmt_name);
enum cob_statement stmt;
if (cob_statement_hash[STMT_UNKNOWN] == 0) {
init_statement_hashlist ();
}
/* overwrite */
if (last_stmt_entry == STMT_CACHE_NUM - 1) {
last_stmt_entry = 0;
} else {
last_stmt_entry++;
}
last_stmt_i = last_stmt_entry;
#define COB_STATEMENT(ename,str) \
if (stmt_hash == cob_statement_hash[ename] \
&& strcmp (str, stmt_name) == 0) { \
stmt = ename; \
} else
#include "statement.def" /* located and installed next to common.h */
#undef COB_STATEMENT
{
cob_runtime_warning ("not handled statement: %s", stmt_name);
return STMT_UNKNOWN;
}
last_stmt_name[last_stmt_i] = stmt_name;
last_stmt[last_stmt_i] = stmt;
return stmt;
}
#undef STMT_CACHE_NUM
}
/* cob_set_location is kept for backward compatibility (pre 3.0);
it stored the location for exception handling and related
intrinsic functions and did tracing, depending on a global flag */
void
cob_set_location (const char *sfile, const unsigned int sline,
const char *csect, const char *cpara,
const char *cstatement)
{
const enum cob_statement stmt = get_stmt_from_name (cstatement);
cob_module *mod = COB_MODULE_PTR;
mod->section_name = csect;
mod->paragraph_name = cpara;
cob_source_file = sfile;
cob_source_line = sline;
/* compat: add to module structure */
mod->statement = stmt;
if (cobsetptr->cob_line_trace) {
const char *s;
if (!cobsetptr->cob_trace_file) {
cob_check_trace_file ();
#if _MSC_VER /* fix dumb warning */
if (!cobsetptr->cob_trace_file) {
return;
}
#endif
}
if (!cob_last_sfile || strcmp (cob_last_sfile, sfile)) {
if (cob_last_sfile) {
cob_free ((void *)cob_last_sfile);
}
cob_last_sfile = cob_strdup (sfile);
fprintf (cobsetptr->cob_trace_file, "Source : '%s'\n", sfile);
}
if (COB_MODULE_PTR->module_name) {
s = COB_MODULE_PTR->module_name;
} else {
s = _("unknown");
}
fprintf (cobsetptr->cob_trace_file,
"Program-Id: %-16s Statement: %-21.21s Line: %u\n",
s, cstatement ? (char *)cstatement : _("unknown"),
sline);
fflush (cobsetptr->cob_trace_file);
}
}
/* cob_trace_section is kept for backward compatibility,
and will be removed in 4.x */
void
cob_trace_section (const char *para, const char *source, const int line)
{
const char *s;
if (cobsetptr->cob_line_trace) {
const cob_module *mod = COB_MODULE_PTR;
int pline = line;
if (!cobsetptr->cob_trace_file) {
cob_check_trace_file ();
#if _MSC_VER /* fix dumb warning */
if (!cobsetptr->cob_trace_file) {
return;
}
#endif
}
if (source &&
(!cob_last_sfile || strcmp (cob_last_sfile, source))) {
if (cob_last_sfile) {
cob_free ((void *)cob_last_sfile);
}
cob_last_sfile = cob_strdup (source);
fprintf (cobsetptr->cob_trace_file, "Source: '%s'\n", source);
}
if (mod->module_name) {
s = mod->module_name;
if (pline == 0 && mod->module_stmt) {
pline = COB_GET_LINE_NUM (mod->module_stmt);
}
} else {
s = _("unknown");
}
fprintf (cobsetptr->cob_trace_file, "Program-Id: %-16s ", s);
if (pline) {
fprintf (cobsetptr->cob_trace_file, "%-34.34sLine: %d\n", para, pline);
} else {
fprintf (cobsetptr->cob_trace_file, "%s\n", para);
}
fflush (cobsetptr->cob_trace_file);
}
}
/* New routines for handling 'trace' follow */
/* Note: As oposed to the old tracing these functions are only called
if the following vars are set:
COB_MODULE_PTR + ->module_stmt + ->module_sources
*/
static int
cob_trace_prep (void)
{
const char *s;
if (!cobsetptr->cob_trace_file) {
cob_check_trace_file ();
if (!cobsetptr->cob_trace_file)
return 1; /* silence warnings */
}
cob_get_source_line ();
if (cob_source_file
&& (!cob_last_sfile || strcmp (cob_last_sfile, cob_source_file))) {
if (cob_last_sfile) {
cob_free ((void *)cob_last_sfile);
}
cob_last_sfile = cob_strdup (cob_source_file);
fprintf (cobsetptr->cob_trace_file, "Source: '%s'\n", cob_source_file);
}
if (COB_MODULE_PTR->module_name) {
s = COB_MODULE_PTR->module_name;
} else {
s = _("unknown");
}
if (!cob_last_progid
|| strcmp (cob_last_progid, s)) {
cob_last_progid = s;
if (COB_MODULE_PTR->module_type == COB_MODULE_TYPE_FUNCTION) {
fprintf (cobsetptr->cob_trace_file, "Function-Id: %s\n", cob_last_progid);
} else {
fprintf (cobsetptr->cob_trace_file, "Program-Id: %s\n", cob_last_progid);
}
}
return 0;
}
static void
cob_trace_print (char *val)
{
int i;
int last_pos = (int)(strlen (cobsetptr->cob_trace_format) - 1);
/* note: only executed after cob_trace_prep (), so
call to cob_get_source_line () already done */
for (i = 0; cobsetptr->cob_trace_format[i] != 0; i++) {
if (cobsetptr->cob_trace_format[i] == '%') {
i++;
switch (cobsetptr->cob_trace_format[i]) {
case 'P':
case 'p':
if (COB_MODULE_PTR && COB_MODULE_PTR->module_type == COB_MODULE_TYPE_FUNCTION) {
if (i != last_pos) {
fprintf (cobsetptr->cob_trace_file, "Function-Id: %-16s", cob_last_progid);
} else {
fprintf (cobsetptr->cob_trace_file, "Function-Id: %s", cob_last_progid);
}
} else {
if (i != last_pos) {
fprintf (cobsetptr->cob_trace_file, "Program-Id: %-16s", cob_last_progid);
} else {
fprintf (cobsetptr->cob_trace_file, "Program-Id: %s", cob_last_progid);
}
}
break;
case 'I':
case 'i':
fprintf (cobsetptr->cob_trace_file, "%s", cob_last_progid);
break;
case 'L':
case 'l':
fprintf (cobsetptr->cob_trace_file, "%6u", cob_source_line);
break;
case 'S':
case 's':
if (i != last_pos) {
fprintf (cobsetptr->cob_trace_file, "%-42.42s", val);
} else {
fprintf (cobsetptr->cob_trace_file, "%s", val);
}
break;
case 'F':
case 'f':
if (i != last_pos) {
fprintf (cobsetptr->cob_trace_file, "Source: %-*.*s",
-COB_MAX_NAMELEN, COB_MAX_NAMELEN, cob_last_sfile);
} else {
fprintf (cobsetptr->cob_trace_file, "Source: %s", cob_last_sfile);
}
break;
default:
fputc ('%', cobsetptr->cob_trace_file);
fputc (cobsetptr->cob_trace_format[i], cobsetptr->cob_trace_file);
}
} else {
fputc (cobsetptr->cob_trace_format[i], cobsetptr->cob_trace_file);
}
}
fputc ('\n', cobsetptr->cob_trace_file);
fflush (cobsetptr->cob_trace_file);
}
void
cob_trace_sect (const char *name)
{
/* compat for pre 3.2 */
COB_MODULE_PTR->section_name = name;
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACE)) {
char val[60];
if (cob_trace_prep ()
|| name == NULL) {
return;
}
snprintf (val, sizeof (val), " Section: %s", name);
cob_trace_print (val);
return;
}
}
void
cob_trace_para (const char *name)
{
/* compat for pre 3.2 */
COB_MODULE_PTR->paragraph_name = name;
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACE)) {
char val[60];
if (cob_trace_prep ()
|| name == NULL) {
return;
}
snprintf (val, sizeof (val), "Paragraph: %s", name);
cob_trace_print (val);
return;
}
}
void
cob_trace_entry (const char *name)
{
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACE)) {
char val[60];
if (cob_trace_prep ()
|| name == NULL) {
return;
}
snprintf (val, sizeof (val), " Entry: %s", name);
cob_trace_print (val);
return;
}
}
void
cob_trace_exit (const char *name)
{
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACE)) {
char val[60];
if (cob_trace_prep ()
|| name == NULL) {
return;
}
snprintf (val, sizeof (val), " Exit: %s", name);
cob_trace_print (val);
return;
}
}
static void
do_trace_statement (const enum cob_statement stmt)
{
char val[60];
if (cob_trace_prep ()) {
return;
}
snprintf (val, sizeof (val), " %s",
stmt != STMT_UNKNOWN ? cob_statement_name[stmt] : _("unknown"));
cob_trace_print (val);
}
/* this function is altogether a compat-only function for pre 3.2,
later versions use cob_trace_statement(enum cob_statement) */
void
cob_trace_stmt (const char *stmt_name)
{
const enum cob_statement stmt = get_stmt_from_name (stmt_name);
/* compat: add to module structure */
COB_MODULE_PTR->statement = stmt;
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACEALL)) {
do_trace_statement (stmt);
}
}
void
cob_trace_statement (const enum cob_statement stmt)
{
/* actual tracing, if activated */
if (cobsetptr->cob_line_trace
&& (COB_MODULE_PTR->flag_debug_trace & COB_MODULE_TRACEALL)) {
do_trace_statement (stmt);
}
}
void
cob_nop (void)
{
/* this is only an empty function, a call to it may be inserted by cobc
to force some optimizations in the C compiler to not be triggered in
a quite portable way */
;
}
void
cob_ready_trace (void)
{
cobsetptr->cob_line_trace = 1;
}
void
cob_reset_trace (void)
{
cobsetptr->cob_line_trace = 0;
}
unsigned char *
cob_get_pointer (const void *srcptr)
{
void *tmptr;
memcpy (&tmptr, srcptr, sizeof (void *));
return (cob_u8_ptr)tmptr;
}
static void
call_exit_handlers_and_terminate (void)
{
if (exit_hdlrs != NULL) {
struct exit_handlerlist *h = exit_hdlrs;
while (h != NULL) {
/* ensure that exit handlers set their own locations */
cob_source_file = NULL;
cob_source_line = 0;
/* tell 'em they are not called with any parameters */
cobglobptr->cob_call_params = 0;
/* actual call and starting next iteration */
h->proc ();
h = h->next;
}
}
cob_terminate_routines ();
}
void
cob_stop_run (const int status)
{
if (!cob_initialized) {
cob_hard_failure ();
}
call_exit_handlers_and_terminate ();
exit_code = status;
#ifndef COB_WITHOUT_JMP
if (return_jmp_buffer_set) {
longjmp (return_jmp_buf, 1);
}
#endif
exit (status);
}
void
cob_stop_error (void)
{
cob_runtime_error ("STOP ERROR");
cob_hard_failure ();
}
static int
handle_core_on_error ()
{
unsigned int core_on_error = 0;
if (cob_initialized) {
core_on_error = cobsetptr->cob_core_on_error;
} else {
char *env_val = cob_getenv_direct ("COB_CORE_ON_ERROR");
if (env_val && env_val[0] && env_val [1] == 0
&& env_val[0] >= '0' && env_val[0] <= '3') {
core_on_error = COB_D2I (env_val[0]);
}
}
/* explicit create a coredump file */
if (core_on_error == 3) {
int ret = create_dumpfile ();
if (ret) {
/* creation did not work, set to "internally 4" */
if (cob_initialized) {
cobsetptr->cob_core_on_error = 4;
}
core_on_error = 4;
}
}
return core_on_error;
}
void
cob_hard_failure ()
{
unsigned int core_on_error = handle_core_on_error ();
if (core_on_error != 4) {
if (core_on_error == 2 && cob_initialized) {
/* prevent unloading modules */
cobsetptr->cob_physical_cancel = -1;
}
call_exit_handlers_and_terminate ();
}
exit_code = -1;
#ifndef COB_WITHOUT_JMP
if (return_jmp_buffer_set) {
longjmp (return_jmp_buf, -1);
}
#endif
/* if explicit requested for errors or
an explicit manual coredump creation did
not work raise an abort here */
if (core_on_error == 2
|| core_on_error == 4) {
cob_raise (SIGABRT);
}
exit (EXIT_FAILURE);
}
void
cob_hard_failure_internal (const char *prefix)
{
unsigned int core_on_error;
if (prefix) {
fprintf (stderr, "\n%s: ", prefix);
} else {
fprintf (stderr, "\n");
}
fprintf (stderr, _("Please report this!"));
fprintf (stderr, "\n");
core_on_error = handle_core_on_error ();
if (core_on_error != 4) {
if (core_on_error == 2 && cob_initialized) {
/* prevent unloading modules */
cobsetptr->cob_physical_cancel = -1;
}
call_exit_handlers_and_terminate ();
}
exit_code = -2;
#ifndef COB_WITHOUT_JMP
if (return_jmp_buffer_set) {
longjmp (return_jmp_buf, -2);
}
#endif
/* if explicit requested for errors or
an explicit manual coredump creation did
not work raise an abort here */
if (core_on_error == 2
|| core_on_error == 4) {
cob_raise (SIGABRT);
}
exit (EXIT_FAILURE);
}
/* get internal exit code, which is set on
STOP RUN / last GOBACK / runtime error / cob_tidy */
int
cob_last_exit_code ()
{
return exit_code;
}
/* get pointer to last runtime error (empty if no runtime error raised) */
const char *
cob_last_runtime_error ()
{
return runtime_err_str;
}
int
cob_is_initialized (void)
{
return (cobglobptr != NULL);
}
cob_global *
cob_get_global_ptr (void)
{
/* LCOV_EXCL_START */
if (unlikely (!cob_initialized)) {
cob_fatal_error (COB_FERROR_INITIALIZED);
}
/* LCOV_EXCL_STOP */
return cobglobptr;
}
int
cob_module_global_enter (cob_module **module, cob_global **mglobal,
const int auto_init, const int entry, const unsigned int *name_hash)
{
/* Check initialized */
if (unlikely (!cob_initialized)) {
if (auto_init) {
cob_init (0, NULL);
} else {
cob_fatal_error (COB_FERROR_INITIALIZED);
}
}
/* Set global pointer */
*mglobal = cobglobptr;
#if 0 /* cob_call_name_hash and cob_call_from_c are rw-branch only features
for now - TODO: activate on merge of r1547 */
/* Was caller a COBOL module */
if (name_hash != NULL
&& cobglobptr->cob_call_name_hash != 0) {
cobglobptr->cob_call_from_c = 1;
while (*name_hash != 0) { /* Scan table of values */
if (cobglobptr->cob_call_name_hash == *name_hash) {
cobglobptr->cob_call_from_c = 0;
break;
}
name_hash++;
}
}
#else
/* LCOV_EXCL_LINE */
COB_UNUSED (name_hash);
#endif
/* Check module pointer */
if (!*module) {
struct cob_alloc_module *mod_ptr;
*module = cob_cache_malloc (sizeof (cob_module));
/* Add to list of all modules activated */
mod_ptr = cob_malloc (sizeof (struct cob_alloc_module));
mod_ptr->cob_pointer = *module;
mod_ptr->next = cob_module_list;
cob_module_list = mod_ptr;
#if 0 /* cob_call_name_hash and cob_call_from_c are rw-branch only features
for now - TODO: activate on merge of r1547 */
} else if (entry == 0
&& !cobglobptr->cob_call_from_c) {
#else
} else if (entry == 0) {
#endif
register int k = 0;
register cob_module *mod;
for (mod = COB_MODULE_PTR; mod; mod = mod->next) {
if (*module == mod) {
/* CHECKME: can we move this in 4.x to the generated program
to be done _before_ executing cob_module_global_enter using
a _static_ variable ? */
if (cobglobptr->cob_stmt_exception) {
/* CALL has ON EXCEPTION so return to caller */
cob_set_exception (COB_EC_PROGRAM_RECURSIVE_CALL);
cobglobptr->cob_stmt_exception = 0;
return 1;
}
cob_module_err = mod;
cob_fatal_error (COB_FERROR_RECURSIVE);
}
/* LCOV_EXCL_START */
if (k++ == MAX_MODULE_ITERS) { /* prevent endless loop in case of broken list */
/* not translated as highly unexpected */
cob_runtime_warning ("max module iterations exceeded, possible broken chain");
break;
}
/* LCOV_EXCL_STOP */
}
}
/* Save parameter count, get number from argc if main program */
if (!COB_MODULE_PTR) {
cobglobptr->cob_call_params = cob_argc - 1;
}
(*module)->module_num_params = cobglobptr->cob_call_params;
/* Push module pointer */
(*module)->next = COB_MODULE_PTR;
COB_MODULE_PTR = *module;
COB_MODULE_PTR->module_stmt = 0;
COB_MODULE_PTR->statement = STMT_UNKNOWN;
cobglobptr->cob_stmt_exception = 0;
return 0;
}
void
cob_module_enter (cob_module **module, cob_global **mglobal,
const int auto_init)
{
(void)cob_module_global_enter (module, mglobal, auto_init, 0, 0);
}
void
cob_module_leave (cob_module *module)
{
COB_UNUSED (module);
/* Pop module pointer */
COB_MODULE_PTR = COB_MODULE_PTR->next;
}
void
cob_module_free (cob_module **module)
{
struct cob_alloc_module *ptr, *prv;
if (*module == NULL) {
return;
}
/* TODO: consider storing the last entry and a prev pointer
to optimize for the likely case of "program added last is removed"
instead of checking _all_ previous entries */
prv = NULL;
/* Remove from list of all modules activated */
for (ptr = cob_module_list; ptr; ptr = ptr->next) {
if (ptr->cob_pointer == *module) {
if (prv == NULL) {
cob_module_list = ptr->next;
} else {
prv->next = ptr->next;
}
cob_free (ptr);
break;
}
prv = ptr;
}
#if 0 /* cob_module->param_buf and cob_module->param_field are rw-branch only features
for now - TODO: activate on merge of r1547 */
&& !cobglobptr->cob_call_from_c
if ((*module)->param_buf != NULL)
cob_cache_free ((*module)->param_buf);
if ((*module)->param_field != NULL)
cob_cache_free ((*module)->param_field);
#endif
cob_cache_free (*module);
*module = NULL;
}
/* save module environment - returns an allocated cob_func_loc (free at cob_restore_func)
and the intermediate return field (must be freed by caller) */
struct cob_func_loc *
cob_save_func (cob_field **savefld, const int params,
const int eparams, ...)
{
struct cob_func_loc *fl;
int numparams;
if (unlikely (params > eparams)) {
numparams = eparams;
} else {
numparams = params;
}
/* Allocate return field */
*savefld = cob_malloc (sizeof (cob_field));
/* Allocate save area */
fl = cob_malloc (sizeof (struct cob_func_loc));
fl->func_params = cob_malloc (sizeof (void *) * ((size_t)numparams + 1U));
fl->data = cob_malloc (sizeof (void *) * ((size_t)numparams + 1U));
/* Save values */
fl->save_module = COB_MODULE_PTR->next;
fl->save_call_params = cobglobptr->cob_call_params;
fl->save_proc_parms = COB_MODULE_PTR->cob_procedure_params;
fl->save_num_params = COB_MODULE_PTR->module_num_params;
/* Set current values */
COB_MODULE_PTR->cob_procedure_params = fl->func_params;
cobglobptr->cob_call_params = numparams;
if (numparams) {
va_list args;
int n;
va_start (args, eparams);
for (n = 0; n < numparams; ++n) {
fl->func_params[n] = va_arg (args, cob_field *);
if (fl->func_params[n]) {
fl->data[n] = fl->func_params[n]->data;
}
}
va_end (args);
}
return fl;
}
/* restores module environment - frees the passed cob_func_loc */
void
cob_restore_func (struct cob_func_loc *fl)
{
/* Restore calling environment */
cobglobptr->cob_call_params = fl->save_call_params;
#if 0 /* RXWRXW - MODNEXT */
COB_MODULE_PTR->next = fl->save_module;
#endif
COB_MODULE_PTR->cob_procedure_params = fl->save_proc_parms;
COB_MODULE_PTR->module_num_params = fl->save_num_params;
cob_free (fl->data);
cob_free (fl->func_params);
cob_free (fl);
}
struct ver_t {
int major, minor, point;
unsigned int version;
};
/*
* Convert version components to an integer value for comparison.
*/
static COB_INLINE unsigned int
version_bitstring( const struct ver_t module )
{
unsigned int version =
((unsigned int)module.major << 24) |
((unsigned int)module.minor << 16) |
((unsigned int)module.point << 8);
return version;
}
void
cob_check_version (const char *prog,
const char *packver_prog, const int patchlev_prog)
{
int nparts;
struct ver_t lib = { 9, 9, 9 };
struct ver_t app = { 0 };
nparts = sscanf (PACKAGE_VERSION, "%d.%d.%d",
&lib.major, &lib.minor, &lib.point);
if (nparts >= 2) {
lib.version = version_bitstring(lib);
(void)sscanf (packver_prog, "%d.%d.%d",
&app.major, &app.minor, &app.point);
app.version = version_bitstring(app);
if (app.major == 2 && app.minor < 2) {
goto err;
}
/* COB_MODULE_PTR is expected to be set, because cob_module_global_enter
was called _directly_ before this function, also with 2.2,
still checking at least for the case of "misuse" of this function as
undocumented ABI call [as in our testsuite ...] */
if (cobglobptr && COB_MODULE_PTR && !COB_MODULE_PTR->gc_version) {
COB_MODULE_PTR->gc_version = packver_prog;
}
if (app.version == lib.version
&& patchlev_prog <= PATCH_LEVEL) {
return;
}
if (app.version < lib.version) {
/* we only claim compatibility to 2.2+ */
struct ver_t minimal = { 2, 2 };
if (app.version <= version_bitstring (minimal)) {
cannot_check_subscript = 1;
}
minimal.minor = 1;
if (app.version >= version_bitstring (minimal)) {
return;
}
}
}
err:
/* TODO: when CALLed - raise exception so program can go ON EXCEPTION */
cob_runtime_error (_("version mismatch"));
cob_runtime_hint (_("%s has version %s.%d"), prog,
packver_prog, patchlev_prog);
cob_runtime_hint (_("%s has version %s.%d"), "libcob",
PACKAGE_VERSION, PATCH_LEVEL);
cob_hard_failure ();
}
void
cob_parameter_check (const char *func_name, const int num_arguments)
{
if (cobglobptr->cob_call_params < num_arguments) {
/* TODO: raise exception */
cob_runtime_error (_("CALL to %s requires %d arguments"),
func_name, num_arguments);
cob_hard_failure ();
}
}
void
cob_correct_numeric (cob_field *f)
{
register unsigned char *p = f->data;
unsigned char *end = p + f->size;
unsigned char *c;
if (!COB_FIELD_IS_NUMDISP (f)) {
return;
}
if (COB_FIELD_HAVE_SIGN (f)) {
/* Adjust for sign byte */
if (unlikely (COB_FIELD_SIGN_LEADING (f))) {
c = p++;
} else {
c = --end;
}
if (unlikely (COB_FIELD_SIGN_SEPARATE (f))) {
if (*c != '+' && *c != '-') {
*c = '+';
}
} else if (unlikely (COB_MODULE_PTR->ebcdic_sign)) {
switch (*c) {
case '{':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case '}':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
break;
case '0':
*c = '{';
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
*c = 'A' + (*c - '1');
break;
case 0:
case ' ':
*c = '{';
break;
default:
break;
}
} else {
if (!*c || *c == ' ') {
*c = '0';
}
}
} else {
c = end - 1;
if (unlikely (COB_MODULE_PTR->ebcdic_sign)) {
switch (*c) {
case 0:
case ' ':
case '{':
case '}':
*c = '0';
break;
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
*c = '1' + (*c - 'A');
break;
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
*c = '1' + (*c - 'J');
break;
default:
break;
}
} else {
switch (*c) {
case 0:
case ' ':
case 'p':
*c = '0';
break;
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
*c = *c - 'p';
break;
default:
break;
}
}
}
while (p < end) {
switch (*p) {
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
break;
case 0:
case ' ':
*p = '0';
break;
default:
if ((*p & 0x0F) <= 9) {
*p = COB_I2D (*p & 0x0F);
}
break;
}
p++;
}
}
static int
cob_check_numdisp (const cob_field *f)
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size;
if (COB_FIELD_HAVE_SIGN (f)) {
/* Adjust for sign byte */
unsigned char c;
if (unlikely (COB_FIELD_SIGN_LEADING (f))) {
c = *p++;
} else {
c = *(--end);
}
if (unlikely (COB_FIELD_SIGN_SEPARATE (f))) {
if (c != '+' && c != '-') {
return 0;
}
} else if (IS_INVALID_DIGIT_DATA (c)) {
if (COB_MODULE_PTR->ebcdic_sign) {
switch (c) {
case '{':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case '}':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
break;
default:
return 0;
}
} else {
switch (c) {
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
break;
default:
return 0;
}
}
}
}
while (p < end) {
if (IS_INVALID_DIGIT_DATA (*p)) {
return 0;
}
p++;
}
return 1;
}
/* Sign */
static COB_INLINE COB_A_INLINE unsigned char *
locate_sign (cob_field *f)
{
if (COB_FIELD_SIGN_LEADING (f)) {
return f->data;
}
return f->data + f->size - 1;
}
/* get sign from DISPLAY/PACKED field 'f';
if 'adjust_ebcdic' is set then original DISPLAY data is "unpunched"
for `ebcdic_sign` and return adjusted;
that allows conversion without handling that afterwards
returns one of: 1 = positive (non-negative); -1 = negative;
2 = positive (non-negative), adjusted;
-2 = negative, adjusted;
0 = neither DISPLAY nor PACKED */
int
cob_real_get_sign (cob_field *f, const int adjust_ebcdic)
{
unsigned char *p;
switch (COB_FIELD_TYPE (f)) {
case COB_TYPE_NUMERIC_DISPLAY:
p = locate_sign (f);
/* Get sign */
if (unlikely (COB_FIELD_SIGN_SEPARATE (f))) {
return (*p == '-') ? -1 : 1;
}
if (IS_VALID_DIGIT_DATA (*p)) {
return 1;
}
if (*p == ' ') {
#if 0 /* RXWRXW - Space sign */
*p = '0';
#endif
return 1;
}
if (adjust_ebcdic) {
#ifdef COB_EBCDIC_MACHINE
if (COB_MODULE_PTR->ebcdic_sign) {
return cob_get_sign_ebcdic (p);
}
return cob_get_sign_ascii (p) < 0 ? -2 : 2;
#else
if (COB_MODULE_PTR->ebcdic_sign) {
return cob_get_sign_ebcdic (p) < 0 ? -2 : 2;
}
return ((*p & 0xF0) == 0x70) ? -1 : 1;
#endif
} else {
if (COB_MODULE_PTR->ebcdic_sign) {
return cob_get_sign_ebcdic (p);
}
return cob_get_sign_ascii (p);
}
case COB_TYPE_NUMERIC_PACKED:
if (COB_FIELD_NO_SIGN_NIBBLE (f)) {
return 1;
}
p = f->data + f->size - 1;
return ((*p & 0x0F) == 0x0D) ? -1 : 1;
}
return 0;
}
/* store sign to DISPLAY/PACKED fields */
void
cob_real_put_sign (cob_field *f, const int sign)
{
unsigned char *p;
switch (COB_FIELD_TYPE (f)) {
case COB_TYPE_NUMERIC_DISPLAY:
/* Note: we only locate the sign if needed,
as the common case will be "nothing to do" */
if (unlikely (COB_FIELD_SIGN_SEPARATE (f))) {
const unsigned char c = (sign == -1) ? '-' : '+';
p = locate_sign (f);
if (*p != c) {
*p = c;
}
} else if (unlikely (COB_MODULE_PTR->ebcdic_sign)) {
p = locate_sign (f);
cob_put_sign_ebcdic (p, sign);
} else if (sign == -1) {
p = locate_sign (f);
cob_put_sign_ascii (p);
}
return;
case COB_TYPE_NUMERIC_PACKED:
if (COB_FIELD_NO_SIGN_NIBBLE (f)) {
return;
}
p = f->data + f->size - 1;
if (sign == -1) {
*p = (*p & 0xF0) | 0x0D;
} else {
*p = (*p & 0xF0) | 0x0C;
}
return;
}
}
/* Registration of external handlers */
void
cob_reg_sighnd (void (*sighnd) (int))
{
if (!cob_initialized) {
cob_set_signal ();
}
cob_ext_sighdl = sighnd;
}
/* Switch */
int
cob_get_switch (const int n)
{
if (n < 0 || n > COB_SWITCH_MAX) {
return 0;
}
return cob_switch[n];
}
void
cob_set_switch (const int n, const int flag)
{
if (n < 0 || n > COB_SWITCH_MAX) {
return;
}
if (flag == 0) {
cob_switch[n] = 0;
} else if (flag == 1) {
cob_switch[n] = 1;
}
}
int
cob_cmp (cob_field *f1, cob_field *f2)
{
unsigned short f1_type = COB_FIELD_TYPE (f1);
unsigned short f2_type = COB_FIELD_TYPE (f2);
const int f1_is_numeric = f1_type & COB_TYPE_NUMERIC;
const int f2_is_numeric = f2_type & COB_TYPE_NUMERIC;
/* both numeric -> direct numeric compare */
if (f1_is_numeric && f2_is_numeric) {
return cob_numeric_cmp (f1, f2);
}
/* one is an internal ALL field (ZERO,LOW-VALUE, ...) */
if (f2_type == COB_TYPE_ALPHANUMERIC_ALL) {
if (f2->size == 1 && f2->data[0] == '0'
&& f1_is_numeric) {
return cob_cmp_int (f1, 0);
}
return cob_cmp_all (f1, f2);
}
if (f1_type == COB_TYPE_ALPHANUMERIC_ALL) {
if (f1->size == 1 && f1->data[0] == '0'
&& f2_is_numeric) {
return -cob_cmp_int (f2, 0);
}
return -cob_cmp_all (f2, f1);
}
#if 0 /* FIXME later: must cater for national fields, too,
at least if that is numeric NATIONAL */
if (COB_FIELD_IS_NATIONAL (f1)) {
...
}
#endif
/* all else -> alphanumeric comparision */
/* if one is numeric (cannot be "both", as checked above), then
convert that to alphanumeric for final test;
note: this is _very_ seldom the case, during "make checkall"
only in test "Alphanumeric and binary numeric" */
if (f1_is_numeric || f2_is_numeric) {
/* Note: the standard explicit defines how to handle that:
intermediate MOVE to a NUMERIC_DISPLAY with same amount
of digits (= drop sign and implied decimal point), then
compare that */
cob_field field;
cob_field_attr attr;
unsigned char buff[COB_MAX_DIGITS + 10];
/* note: the standard explicit forbits floating-point numeric
in this scenario */
if (f1_is_numeric
&& f1_type != COB_TYPE_NUMERIC_DISPLAY) {
COB_FIELD_INIT (COB_FIELD_DIGITS (f1), buff, &attr);
attr = *f1->attr;
attr.type = COB_TYPE_NUMERIC_DISPLAY;
attr.flags &= ~COB_FLAG_HAVE_SIGN;
cob_move (f1, &field);
f1 = &field;
}
if (f2_is_numeric
&& f2_type != COB_TYPE_NUMERIC_DISPLAY) {
COB_FIELD_INIT (COB_FIELD_DIGITS (f2), buff, &attr);
attr = *f2->attr;
attr.type = COB_TYPE_NUMERIC_DISPLAY;
attr.flags &= ~COB_FLAG_HAVE_SIGN;
cob_move (f2, &field);
f2 = &field;
}
if (COB_FIELD_HAVE_SIGN (f1)) {
/* Note: if field is numeric then it is always
USAGE DISPLAY here */
if (f1 != &field) {
const int sign = COB_GET_SIGN (f1);
int ret = cob_cmp_alnum (f1, f2);
COB_PUT_SIGN (f1, sign);
return ret;
} else {
/* we operate on a buffer already, just go on */
(void)cob_real_get_sign (f1, 0);
return cob_cmp_alnum (f1, f2);
}
}
if (COB_FIELD_HAVE_SIGN (f2)) {
/* Note: if field is numeric then it is always
USAGE DISPLAY here */
if (f2 != &field) {
const int sign = COB_GET_SIGN (f2);
int ret = cob_cmp_alnum (f1, f2);
COB_PUT_SIGN (f2, sign);
return ret;
} else {
/* we operate on a buffer already, just go on */
(void)cob_real_get_sign (f2, 0);
return cob_cmp_alnum (f1, f2);
}
}
/* done here to have the data for non-signed numeric vs. non-numeric in scope */
return cob_cmp_alnum (f1, f2);
}
/* both data not numeric: compare as string */
return cob_cmp_alnum (f1, f2);
}
/* Class check */
int
cob_is_omitted (const cob_field *f)
{
return f->data == NULL;
}
int
cob_is_numeric (const cob_field *f)
{
switch (COB_FIELD_TYPE (f)) {
case COB_TYPE_NUMERIC_BINARY:
return 1;
case COB_TYPE_NUMERIC_FLOAT:
{
float fval;
memcpy (&fval, f->data, sizeof (float));
return !ISFINITE ((double)fval);
}
case COB_TYPE_NUMERIC_DOUBLE:
{
double dval;
memcpy (&dval, f->data, sizeof (double));
return !ISFINITE (dval);
}
case COB_TYPE_NUMERIC_L_DOUBLE:
{
long double lval;
memcpy (&lval, f->data, sizeof (long double));
return !ISFINITE ((double)lval);
}
case COB_TYPE_NUMERIC_PACKED:
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size - 1;
/* Check sign */
{
const char sign = *end & 0x0F;
if (COB_FIELD_NO_SIGN_NIBBLE (f)) {
/* COMP-6 - Check last nibble */
if (sign > 0x09) {
return 0;
}
} else if (COB_FIELD_HAVE_SIGN (f)) {
if (COB_MODULE_PTR->flag_host_sign
&& sign == 0x0F) {
/* all fine, go on */
} else
if (sign != 0x0C
&& sign != 0x0D) {
return 0;
}
} else if (sign != 0x0F) {
return 0;
}
}
/* Check high nibble of last byte */
if ((*end & 0xF0) > 0x90) {
return 0;
}
/* Check digits */
while (p < end) {
if (IS_INVALID_BCD_DATA (*p)) {
return 0;
}
p++;
}
return 1;
}
case COB_TYPE_NUMERIC_DISPLAY:
return cob_check_numdisp (f);
case COB_TYPE_NUMERIC_FP_DEC64:
#ifdef WORDS_BIGENDIAN
return (f->data[0] & 0x78U) != 0x78U;
#else
return (f->data[7] & 0x78U) != 0x78U;
#endif
case COB_TYPE_NUMERIC_FP_DEC128:
#ifdef WORDS_BIGENDIAN
return (f->data[0] & 0x78U) != 0x78U;
#else
return (f->data[15] & 0x78U) != 0x78U;
#endif
default:
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size;
while (p < end) {
if (IS_INVALID_DIGIT_DATA (*p)) {
return 0;
}
p++;
}
return 1;
}
}
}
int
cob_is_alpha (const cob_field *f)
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size;
while (p < end) {
if (*p == (unsigned char)' '
|| isalpha (*p)) {
p++;
} else {
return 0;
}
}
return 1;
}
int
cob_is_upper (const cob_field *f)
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size;
while (p < end) {
if (*p == (unsigned char)' '
|| isupper (*p)) {
p++;
} else {
return 0;
}
}
return 1;
}
int
cob_is_lower (const cob_field *f)
{
register const unsigned char *p = f->data;
const unsigned char *end = p + f->size;
while (p < end) {
if (*p == (unsigned char)' '
|| islower (*p)) {
p++;
} else {
return 0;
}
}
return 1;
}
/* Table sort */
void
cob_table_sort_init (const size_t nkeys, const unsigned char *collating_sequence)
{
sort_nkeys = 0;
sort_keys = cob_malloc (nkeys * sizeof (cob_file_key));
if (collating_sequence) {
sort_collate = collating_sequence;
} else {
sort_collate = COB_MODULE_PTR->collating_sequence;
}
}
void
cob_table_sort_init_key (cob_field *field, const int flag,
const unsigned int offset)
{
sort_keys[sort_nkeys].field = field;
sort_keys[sort_nkeys].flag = flag;
sort_keys[sort_nkeys].offset = offset;
sort_nkeys++;
}
void
cob_table_sort (cob_field *f, const int n)
{
if (sort_collate) {
qsort (f->data, (size_t) n, f->size, sort_compare_collate);
} else {
qsort (f->data, (size_t) n, f->size, sort_compare);
}
cob_free (sort_keys);
}
/* Run-time error checking */
void
cob_check_beyond_exit (const char *name)
{
/* possibly allow to lower this to a runtime warning later */
cob_runtime_error (_("code execution leaving %s"), name);
cob_hard_failure ();
}
void
cob_check_based (const unsigned char *x, const char *name)
{
if (!x) {
/* name includes '' already and can be ... 'x' (addressed by 'y') */
/* TODO: raise exception */
cob_runtime_error (_("BASED/LINKAGE item %s has NULL address"), name);
cob_hard_failure ();
}
}
/* internal test for writing outside of storage iduring CALL / UDF invocation,
checking 'fence_pre'/'fence_post' to contain 0xFFFEFDFC00 / 0xFAFBFCFD00;
'statement' specifies the place where that happened, which may
include psuedo-statements "INIT CALL" and "INIT UDF",
'name' (optional) specifies the variable where this was recognized */
void
cob_check_fence (const char *fence_pre, const char *fence_post,
const enum cob_statement stmt, const char *name)
{
if (memcmp (fence_pre, "\xFF\xFE\xFD\xFC\xFB\xFA\xFF", 8)
|| memcmp (fence_post, "\xFA\xFB\xFC\xFD\xFE\xFF\xFA", 8)) {
if (name) {
cob_runtime_error (_("memory violation detected for '%s' after %s"),
name, cob_statement_name[stmt]);
} else {
cob_runtime_error (_("memory violation detected after %s"),
cob_statement_name[stmt]);
}
cob_hard_failure ();
}
}
/* raise argument mismatch after pushing a temporary static "current module"
as COB_MODULE_PTR; caller needs to restore pop it afterwards! */
static int
raise_arg_mismatch (const char *entry_name,
const char **module_sources, unsigned int module_stmt)
{
static cob_module mod_temp;
cob_module *mod = &mod_temp;
memset (mod, 0, sizeof (cob_module));
mod->next = COB_MODULE_PTR;
mod->module_name = entry_name; /* not correct, but enough */
mod->module_sources = module_sources;
mod->statement = STMT_ENTRY;
mod->module_stmt = module_stmt;
COB_MODULE_PTR = mod;
cob_set_exception (COB_EC_PROGRAM_ARG_MISMATCH);
if (cobglobptr->cob_stmt_exception) {
/* CALL has ON EXCEPTION so return to caller */
cobglobptr->cob_stmt_exception = 0;
return 0;
}
return 1;
}
/* validates that the data item 'name' was passed by the caller
and has at least as much size as used in the callee,
used during CALL in the entry points of the callee to check
for COB_EC_PROGRAM_ARG_MISMATCH */
int
cob_check_linkage_size (const char *entry_name,
const char *name, const unsigned int ordinal_pos,
const int optional, const unsigned long size,
const char **module_sources, unsigned int module_stmt)
{
/* name includes '' already and can be ... 'x' of 'y' */
if (!cobglobptr || !COB_MODULE_PTR) {
/* unlikely case: runtime not initialized, or we have no module
so caller _must_ be something other than a GnuCOBOL module
(while ENTRY-CONVENTION is COBOL) -> skip these checks */
/* possibly raise (an optional) runtime warning */
return 0;
} else if (cobglobptr->cob_call_params < ordinal_pos) {
if (optional) {
return 0;
} else {
if (raise_arg_mismatch (entry_name, module_sources, module_stmt)) {
cob_runtime_error (_("LINKAGE item %s not passed by caller"), name);
cob_hard_failure ();
}
COB_MODULE_PTR = COB_MODULE_PTR->next;
}
return -1;
} else {
/* note: the current module points to the caller, as we
are early in the called function (its entry point) */
const cob_field *parameter = COB_MODULE_PTR->cob_procedure_params[ordinal_pos - 1];
if (!parameter || !parameter->data) {
if (optional) {
return 0;
} else {
if (raise_arg_mismatch (entry_name, module_sources, module_stmt)) {
cob_runtime_error (_("LINKAGE item %s not passed by caller"), name);
cob_hard_failure ();
}
COB_MODULE_PTR = COB_MODULE_PTR->next;
}
return -1;
} else {
if (parameter->size < size) {
if (raise_arg_mismatch (entry_name, module_sources, module_stmt)) {
cob_runtime_error (_("LINKAGE item %s (size %lu) too small in the caller (size %lu)"),
name, size, (unsigned long) parameter->size);
cob_hard_failure ();
}
COB_MODULE_PTR = COB_MODULE_PTR->next;
return -1;
} else if ((unsigned long)parameter->size != size) {
/* possible warning that can additionally be activated */
}
}
}
return 0;
}
/* validates that the data item 'name' has a non-null data 'x',
used for both CALL (COB_EC_PROGRAM_ARG_MISMATCH) and
for actual use of the argument (COB_EC_PROGRAM_ARG_OMITTED) */
void
cob_check_linkage (const unsigned char *x, const char *name, const int check_type)
{
if (!x) {
/* name includes '' already and can be ... 'x' of 'y' */
switch (check_type) {
case 0: /* check for passed items on module entry */
cob_set_exception (COB_EC_PROGRAM_ARG_MISMATCH);
if (cobglobptr->cob_stmt_exception) {
/* CALL has ON EXCEPTION so return to caller */
cobglobptr->cob_stmt_exception = 0;
return;
}
cob_runtime_error (_("LINKAGE item %s not passed by caller"), name);
break;
case 1: /* check for passed OPTIONAL items on item use */
cob_set_exception (COB_EC_PROGRAM_ARG_OMITTED);
cob_runtime_error (_("LINKAGE item %s not passed by caller"), name);
break;
}
cob_hard_failure ();
}
}
const char *
explain_field_type (const cob_field *f)
{
switch (COB_FIELD_TYPE (f)) {
case COB_TYPE_GROUP:
return "GROUP";
case COB_TYPE_BOOLEAN:
return "BOOLEAN";
case COB_TYPE_NUMERIC_DISPLAY:
return "NUMERIC DISPLAY";
case COB_TYPE_NUMERIC_BINARY:
return "BINARY";
case COB_TYPE_NUMERIC_PACKED:
if (COB_FIELD_NO_SIGN_NIBBLE (f)) {
return "COMP-6";
}
if (!COB_FIELD_HAVE_SIGN (f)) {
return "PACKED-DECIMAL (unsigned)";
}
return "PACKED-DECIMAL";
case COB_TYPE_NUMERIC_FLOAT:
return "FLOAT";
case COB_TYPE_NUMERIC_DOUBLE:
return "DOUBLE"; /* FLOAT-LONG */
case COB_TYPE_NUMERIC_L_DOUBLE:
return "LONG DOUBLE"; /* FLOAT-EXTENDED */
case COB_TYPE_NUMERIC_FP_DEC64:
return "FP DECIMAL 64";
case COB_TYPE_NUMERIC_FP_DEC128:
return "FP DECIMAL 128";
case COB_TYPE_NUMERIC_FP_BIN32:
return "FP BINARY 32";
case COB_TYPE_NUMERIC_FP_BIN64:
return "FP BINARY 64";
case COB_TYPE_NUMERIC_FP_BIN128:
return "FP BINARY 128";
/* note: may be not reached depending on endianness */
case COB_TYPE_NUMERIC_COMP5:
return "COMP-5";
case COB_TYPE_NUMERIC_EDITED:
return "NUMERIC EDITED";
case COB_TYPE_ALPHANUMERIC:
return "ALPHANUMERIC";
case COB_TYPE_ALPHANUMERIC_ALL:
return "ALPHANUMERIC ALL";
case COB_TYPE_ALPHANUMERIC_EDITED:
return "ALPHANUMERIC EDITED";
case COB_TYPE_NATIONAL:
return "NATIONAL";
case COB_TYPE_NATIONAL_EDITED:
return "NATIONAL EDITED";
default:
break;
}
return "UNKNOWN";
}
void
cob_check_numeric (const cob_field *f, const char *name)
{
if (!cob_is_numeric (f)) {
register unsigned char *data = f->data;
unsigned char *end = data + f->size;
char *p;
char *buff;
cob_set_exception (COB_EC_DATA_INCOMPATIBLE);
buff = cob_fast_malloc ((size_t)COB_SMALL_BUFF);
p = buff;
if (COB_FIELD_IS_NUMDISP(f) || COB_FIELD_IS_ANY_ALNUM(f)) {
while (data < end) {
if (isprint (*data)) {
*p++ = *data++;
} else {
p += sprintf (p, "\\%03o", *data++);
}
}
} else {
p += sprintf (p, "0x");
while (data < end) {
p += sprintf (p, "%02x", *data++);
}
}
*p = '\0';
cob_runtime_error (_("'%s' (Type: %s) not numeric: '%s'"),
name, explain_field_type(f), buff);
cob_free (buff);
cob_hard_failure ();
}
}
void
cob_check_odo (const int i, const int min, const int max,
const char *name, const char *dep_name)
{
/* Check OCCURS DEPENDING ON item */
if (i < min || i > max) {
cob_set_exception (COB_EC_BOUND_ODO);
/* Hack for call from 2.0 modules as the module signature was changed :-(
Note: depending on the actual C runtime this may work or directly break
*/
/* LCOV_EXCL_START */
if (dep_name == NULL) {
dep_name = name;
name = "unknown field";
}
/* LCOV_EXCL_STOP */
cob_runtime_error (_("OCCURS DEPENDING ON '%s' out of bounds: %d"),
dep_name, i);
if (i > max) {
cob_runtime_hint (_("maximum subscript for '%s': %d"), name, max);
} else {
cob_runtime_hint (_("minimum subscript for '%s': %d"), name, min);
}
cob_hard_failure ();
}
}
void
cob_check_subscript (const int i, const int max,
const char *name, const int odo_item)
{
#if 1
/* Hack for call from 2.0 modules as the module signature was changed :-(
Note: depending on the actual C runtime this may work or directly break
*/
/* LCOV_EXCL_START */
if (cannot_check_subscript) {
/* Check zero subscript */
if (i == 0) {
cob_set_exception (COB_EC_BOUND_SUBSCRIPT);
cob_runtime_error (_("subscript of '%s' out of bounds: %d"), "unknown field", i);
cob_hard_failure ();
}
return;
}
/* LCOV_EXCL_STOP */
#endif
/* Check subscript */
if (i < 1 || i > max) {
cob_set_exception (COB_EC_BOUND_SUBSCRIPT);
cob_runtime_error (_("subscript of '%s' out of bounds: %d"), name, i);
if (i >= 1) {
if (odo_item) {
cob_runtime_hint (_("current maximum subscript for '%s': %d"),
name, max);
} else {
cob_runtime_hint (_("maximum subscript for '%s': %d"),
name, max);
}
}
cob_hard_failure ();
}
}
void
cob_check_ref_mod_detailed (const char *name, const int abend, const int zero_allowed,
const int size, const int offset, const int length)
{
const int minimal_length = zero_allowed ? 0 : 1;
/* Check offset */
if (offset < 1 || offset > size) {
cob_set_exception (COB_EC_BOUND_REF_MOD);
if (offset < 1) {
cob_runtime_error (_("offset of '%s' out of bounds: %d"),
name, offset);
} else {
cob_runtime_error (_("offset of '%s' out of bounds: %d, maximum: %d"),
name, offset, size);
}
if (abend) {
cob_hard_failure ();
}
}
/* Check plain length */
if (length < minimal_length || length > size) {
cob_set_exception (COB_EC_BOUND_REF_MOD);
if (length < minimal_length) {
cob_runtime_error (_("length of '%s' out of bounds: %d"),
name, length);
} else {
cob_runtime_error (_("length of '%s' out of bounds: %d, maximum: %d"),
name, length, size);
}
if (abend) {
cob_hard_failure ();
}
}
/* Check length with offset */
if (offset + length - 1 > size) {
cob_set_exception (COB_EC_BOUND_REF_MOD);
cob_runtime_error (_("length of '%s' out of bounds: %d, starting at: %d, maximum: %d"),
name, length, offset, size);
if (abend) {
cob_hard_failure ();
}
}
}
/* kept for 2.2-3.1-rc1 compat only */
void
cob_check_ref_mod (const int offset, const int length,
const int size, const char* name)
{
cob_check_ref_mod_detailed (name, 1, 0, size, offset, length);
}
void
cob_check_ref_mod_minimal (const char* name, const int offset, const int length)
{
/* Check offset */
if (offset < 1) {
cob_set_exception (COB_EC_BOUND_REF_MOD);
cob_runtime_error (_("offset of '%s' out of bounds: %d"),
name, offset);
cob_hard_failure ();
}
/* Check length */
if (length < 1) {
cob_set_exception (COB_EC_BOUND_REF_MOD);
cob_runtime_error (_("length of '%s' out of bounds: %d"),
name, length);
cob_hard_failure ();
}
}
/* check if already allocated, if yes returns its address and sets exlength */
static void *
cob_external_addr_lookup (const char *exname, int *exlength)
{
struct cob_external *eptr;
for (eptr = basext; eptr; eptr = eptr->next) {
if (!strcmp (exname, eptr->ename)) {
if (exlength) {
*exlength = eptr->esize;
}
return eptr->ext_alloc;
}
}
return NULL;
}
/* allocate new external entry;
returns allocated pointer with requested size */
static void *
cob_external_addr_create (const char *exname, int exlength)
{
struct cob_external *eptr;
eptr = cob_malloc (sizeof (struct cob_external));
eptr->next = basext;
eptr->esize = exlength;
eptr->ename = cob_strdup (exname);
eptr->ext_alloc = cob_malloc ((size_t)exlength);
basext = eptr;
return eptr->ext_alloc;
}
/* lookup external item, if already created before check given length;
returns allocated pointer with at least requested size */
void *
cob_external_addr (const char *exname, const int exlength)
{
int stored_length;
void *ret;
/* special external "C" registers */
if (exlength == sizeof (int)
&& !strcmp (exname, "ERRNO")) {
return &errno;
}
/* Locate or allocate EXTERNAL item */
ret = cob_external_addr_lookup (exname, &stored_length);
if (ret != NULL) {
if (exlength > stored_length) {
cob_runtime_error (_ ("EXTERNAL item '%s' previously allocated with size %d, requested size is %d"),
exname, stored_length, exlength);
cob_hard_failure ();
}
if (exlength < stored_length) {
cob_runtime_warning (_ ("EXTERNAL item '%s' previously allocated with size %d, requested size is %d"),
exname, stored_length, exlength);
}
cobglobptr->cob_initial_external = 0;
} else {
ret = cob_external_addr_create (exname, exlength);
cobglobptr->cob_initial_external = 1;
}
return ret;
}
#if defined (_MSC_VER) && COB_USE_VC2008_OR_GREATER
/* Get function pointer for most precise time function
GetSystemTimePreciseAsFileTime is available since OS-version Windows 2000
GetSystemTimeAsFileTime is available since OS-version Windows 8 / Server 2012
*/
static void
get_function_ptr_for_precise_time (void)
{
HMODULE kernel32_handle;
kernel32_handle = GetModuleHandle (TEXT ("kernel32.dll"));
if (kernel32_handle != NULL) {
time_as_filetime_func = (VOID (WINAPI *) (LPFILETIME))
GetProcAddress (kernel32_handle, "GetSystemTimePreciseAsFileTime");
}
if (time_as_filetime_func == NULL) {
time_as_filetime_func = GetSystemTimeAsFileTime;
}
}
#endif
/* split the timep to cob_time and set the offset from UTC */
void
static set_cob_time_from_localtime (time_t curtime,
struct cob_time *cb_time) {
struct tm *tmptr;
#if !defined (_BSD_SOURCE) && !defined (HAVE_TIMEZONE)
time_t utctime, lcltime, difftime;
#endif
static time_t last_time = 0;
static struct cob_time last_cobtime;
/* FIXME: on setting related locale set last_time = 0 */
if (curtime == last_time) {
memcpy (cb_time, &last_cobtime, sizeof (struct cob_time));
return;
}
if (last_time != 0) {
const time_t sec_passed = curtime - last_time;
/* most likely we are in the same hour as before - in which case
there is no need to lookup the tz-database via localtime -
instead directly adjust the last value */
if (sec_passed > 0 && sec_passed < 3600) {
memcpy (cb_time, &last_cobtime, sizeof (struct cob_time));
cb_time->second += (int)sec_passed;
if (cb_time->second < 60) {
memcpy (&last_cobtime, cb_time, sizeof (struct cob_time));
last_time = curtime;
return;
} else {
cb_time->minute += cb_time->second / 60;
/* note: if the minute is >= 60 then we need to adjust the hour
and may end up with DST issues (or simple day switch),
we may also get into minute == 60 for leap second cases;
do a full recalculation in this case */
if (cb_time->minute < 60) {
cb_time->second = cb_time->second % 60;
memcpy (&last_cobtime, cb_time, sizeof (struct cob_time));
last_time = curtime;
return;
}
}
}
}
last_time = curtime;
tmptr = localtime (&curtime);
cb_time->year = tmptr->tm_year + 1900;
cb_time->month = tmptr->tm_mon + 1;
cb_time->day_of_month = tmptr->tm_mday;
cb_time->day_of_week = one_indexed_day_of_week_from_monday (tmptr->tm_wday);
cb_time->day_of_year = tmptr->tm_yday + 1;
cb_time->hour = tmptr->tm_hour;
cb_time->minute = tmptr->tm_min;
/* LCOV_EXCL_START */
/* Leap seconds ? */
if (tmptr->tm_sec >= 60) {
tmptr->tm_sec = 59;
}
/* LCOV_EXCL_STOP */
cb_time->second = tmptr->tm_sec;
cb_time->nanosecond = 0;
cb_time->is_daylight_saving_time = tmptr->tm_isdst;
#if defined (_BSD_SOURCE)
cb_time->offset_known = 1;
cb_time->utc_offset = tmptr->tm_gmtoff / 60;
#elif defined (HAVE_TIMEZONE)
cb_time->offset_known = 1;
cb_time->utc_offset = timezone / -60;
/* LCOV_EXCL_START */
if (tmptr->tm_isdst) {
cb_time->utc_offset += 60;
}
/* LCOV_EXCL_STOP */
#else
lcltime = mktime (tmptr);
tmptr = gmtime (&curtime);
utctime = mktime (tmptr);
if (utctime != -1 && lcltime != -1) { /* LCOV_EXCL_BR_LINE */
difftime = utctime - lcltime;
/* LCOV_EXCL_START */
if (tmptr->tm_isdst) {
difftime -= 3600;
}
/* LCOV_EXCL_STOP */
cb_time->utc_offset = difftime / 60;
cb_time->offset_known = 1;
/* LCOV_EXCL_START */
} else {
cb_time->offset_known = 0;
cb_time->utc_offset = 0;
}
/* LCOV_EXCL_STOP */
#endif
/* keep backup for next call */
memcpy (&last_cobtime, cb_time, sizeof (struct cob_time));
}
#if defined (_WIN32) /* cygwin does not define _WIN32 */
static struct cob_time
cob_get_current_date_and_time_from_os (const enum cob_datetime_res res)
{
SYSTEMTIME local_time;
#if defined (_MSC_VER) && COB_USE_VC2008_OR_GREATER
FILETIME filetime;
SYSTEMTIME utc_time;
#endif
time_t curtime;
struct cob_time cb_time;
curtime = time (NULL);
set_cob_time_from_localtime (curtime, &cb_time);
if (res <= DTR_TIME_NO_NANO) {
cb_time.nanosecond = 0;
return cb_time;
}
/* Get nanoseconds with highest precision possible */
#if defined (_MSC_VER) && COB_USE_VC2008_OR_GREATER
if (!time_as_filetime_func) {
get_function_ptr_for_precise_time ();
}
#pragma warning(suppress: 6011) /* the function pointer is always set by get_function_ptr_for_precise_time */
(time_as_filetime_func) (&filetime);
/* fallback to GetLocalTime if one of the following does not work */
if (FileTimeToSystemTime (&filetime, &utc_time) &&
SystemTimeToTzSpecificLocalTime (NULL, &utc_time, &local_time)) {
set_cob_time_ns_from_filetime (filetime, &cb_time);
return cb_time;
}
#endif
GetLocalTime (&local_time);
cb_time.nanosecond = local_time.wMilliseconds * 1000000;
return cb_time;
}
#else
static struct cob_time
cob_get_current_date_and_time_from_os (const enum cob_datetime_res res)
{
#if defined (HAVE_CLOCK_GETTIME)
struct timespec time_spec;
#elif defined (HAVE_SYS_TIME_H) && defined (HAVE_GETTIMEOFDAY)
struct timeval tmv;
#endif
time_t curtime;
struct cob_time cb_time;
/* Get the current time */
#if defined (HAVE_CLOCK_GETTIME)
clock_gettime (CLOCK_REALTIME, &time_spec);
curtime = time_spec.tv_sec;
#elif defined (HAVE_SYS_TIME_H) && defined (HAVE_GETTIMEOFDAY)
gettimeofday (&tmv, NULL);
curtime = tmv.tv_sec;
#else
curtime = time (NULL);
#endif
set_cob_time_from_localtime (curtime, &cb_time);
/* Get nanoseconds or microseconds, if possible */
#if defined (HAVE_CLOCK_GETTIME)
cb_time.nanosecond = (int) time_spec.tv_nsec;
#elif defined (HAVE_SYS_TIME_H) && defined (HAVE_GETTIMEOFDAY)
cb_time.nanosecond = tmv.tv_usec * 1000;
#else
cb_time.nanosecond = 0;
#endif
return cb_time;
}
#endif
/* obsolete function, only used in cobc before 3.2 */
struct cob_time
cob_get_current_date_and_time (void)
{
return cob_get_current_datetime (DTR_TIME_NO_NANO);
}
struct cob_time
cob_get_current_datetime (const enum cob_datetime_res res)
{
struct cob_time cb_time = cob_get_current_date_and_time_from_os (res);
/* Do we have a constant time? */
if (cobsetptr != NULL
&& cobsetptr->cob_time_constant.year != 0) {
if (cobsetptr->cob_time_constant.hour != -1) {
cb_time.hour = cobsetptr->cob_time_constant.hour;
}
if (cobsetptr->cob_time_constant.minute != -1) {
cb_time.minute = cobsetptr->cob_time_constant.minute;
}
if (cobsetptr->cob_time_constant.second != -1) {
cb_time.second = cobsetptr->cob_time_constant.second;
}
if (cobsetptr->cob_time_constant.nanosecond != -1) {
cb_time.nanosecond = cobsetptr->cob_time_constant.nanosecond;
}
if (cobsetptr->cob_time_constant.offset_known) {
cb_time.offset_known = cobsetptr->cob_time_constant.offset_known;
cb_time.utc_offset = cobsetptr->cob_time_constant.utc_offset;
}
if (cobsetptr->cob_time_constant_is_calculated) {
cb_time.year = cobsetptr->cob_time_constant.year;
cb_time.month = cobsetptr->cob_time_constant.month;
cb_time.day_of_month = cobsetptr->cob_time_constant.day_of_month;
cb_time.day_of_week = cobsetptr->cob_time_constant.day_of_week;
cb_time.day_of_year = cobsetptr->cob_time_constant.day_of_year;
cb_time.is_daylight_saving_time = cobsetptr->cob_time_constant.is_daylight_saving_time;
} else {
int needs_calculation = 0;
/* Note: constant time but X not part of constant --> -1 */
if (cobsetptr->cob_time_constant.year != -1) {
cb_time.year = cobsetptr->cob_time_constant.year;
needs_calculation = 1;
}
if (cobsetptr->cob_time_constant.month != -1) {
cb_time.month = cobsetptr->cob_time_constant.month;
needs_calculation = 1;
}
if (cobsetptr->cob_time_constant.day_of_month != -1) {
cb_time.day_of_month = cobsetptr->cob_time_constant.day_of_month;
needs_calculation = 1;
}
/* set day_of_week, day_of_year, is_daylight_saving_time, if necessary */
if (needs_calculation) {
time_t t;
struct tm *tmptr;
/* allocate tmptr (needs a correct time) */
time (&t);
tmptr = localtime (&t);
tmptr->tm_isdst = -1;
tmptr->tm_sec = cb_time.second;
tmptr->tm_min = cb_time.minute;
tmptr->tm_hour = cb_time.hour;
tmptr->tm_year = cb_time.year - 1900;
tmptr->tm_mon = cb_time.month - 1;
tmptr->tm_mday = cb_time.day_of_month;
tmptr->tm_wday = -1;
tmptr->tm_yday = -1;
(void)mktime(tmptr);
cb_time.day_of_week = one_indexed_day_of_week_from_monday (tmptr->tm_wday);
cb_time.day_of_year = tmptr->tm_yday + 1;
cb_time.is_daylight_saving_time = tmptr->tm_isdst;
}
}
}
/* Leap seconds ? */
if (cb_time.second >= 60) {
cb_time.second = 59;
}
return cb_time;
}
int
cob_set_date_from_epoch (struct cob_time *cb_time, const unsigned char *p)
{
struct tm *tmptr;
time_t t = 0;
long long seconds = 0;
while (IS_VALID_DIGIT_DATA (*p)) {
seconds = seconds * 10 + COB_D2I (*p++);
}
if (*p != 0 || seconds > 253402300799) {
/* The value (as a unix timestamp) corresponds to date
"Dec 31 9999 23:59:59 UTC", which is the latest date that __DATE__
and __TIME__ can store. */
return 1;
}
/* allocate tmptr for epoch */
tmptr = localtime (&t);
/* set seconds, minutes, hours and big days */
tmptr->tm_sec = seconds % 60;
seconds /= 60;
tmptr->tm_min = seconds % 60;
seconds /= 60;
tmptr->tm_hour = seconds % 24;
seconds /= 24;
tmptr->tm_mday = (int)seconds;
tmptr->tm_isdst = -1;
/* normalize if needed (definitely for epoch, but also for example 30 Feb
to be changed to correct march date),
set tm_wday, tm_yday and tm_isdst */
if (mktime (tmptr) == -1) {
return 1;
}
cb_time->year = tmptr->tm_year + 1900;
cb_time->month = tmptr->tm_mon + 1;
cb_time->day_of_month = tmptr->tm_mday;
cb_time->hour = tmptr->tm_hour;
cb_time->minute = tmptr->tm_min;
cb_time->second = tmptr->tm_sec;
cb_time->nanosecond = -1;
cb_time->day_of_week = tmptr->tm_wday + 1;
cb_time->day_of_year = tmptr->tm_yday + 1;
cb_time->is_daylight_saving_time = tmptr->tm_isdst;
return 0;
}
static void
check_current_date ()
{
int yr, mm, dd, hh, mi, ss, ns;
int offset = 9999;
int i, ret;
time_t t;
struct tm *tmptr;
char iso_timezone[7] = { 0 };
unsigned char *p = (unsigned char*)cobsetptr->cob_date;
if (p == NULL) {
return;
}
/* skip quotes and space-characters */
while (*p == '\''
|| *p == '"'
|| isspace (*p)) {
p++;
}
/* extract epoch, if specified */
if (*p == '@') {
/* @sssssssss seconds since epoch */
ret = cob_set_date_from_epoch (&cobsetptr->cob_time_constant, ++p);
if (ret) {
cob_runtime_warning (_("COB_CURRENT_DATE '%s' is invalid"), cobsetptr->cob_date);
}
return;
}
yr = mm = dd = hh = mi = ss = ns = -1;
ret = 0;
/* extract date */
if (*p) {
yr = 0;
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
yr = yr * 10 + COB_D2I (*p);
if (++i == 4) {
p++;
break;
}
}
if (i != 2 && i != 4) {
/* possible template with partial system lookup */
if (*p == 'Y') {
while (*p == 'Y') p++;
} else {
ret = 1;
}
yr = -1;
} else if (yr < 100) {
yr += 2000;
}
if (*p == '/'
|| *p == '-') {
p++;
}
}
if (*p) {
mm = 0;
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
mm = mm * 10 + COB_D2I (*p);
if (++i == 2) {
p++;
break;
}
}
if (i != 2) {
/* possible template with partial system lookup */
if (*p == 'M') {
while (*p == 'M') p++;
} else {
ret = 1;
}
mm = -1;
} else if (mm < 1 || mm > 12) {
ret = 1;
}
if (*p == '/'
|| *p == '-') {
p++;
}
}
if (*p) {
dd = 0;
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
dd = dd * 10 + COB_D2I (*p);
if (++i == 2) {
p++;
break;
}
}
if (i != 2) {
/* possible template with partial system lookup */
if (*p == 'D') {
while (*p == 'D') p++;
} else {
ret = 1;
}
dd = -1;
} else if (dd < 1 || dd > 31) {
ret = 1;
}
}
/* extract time */
if (*p) {
hh = 0;
while (isspace (*p)) p++;
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
hh = hh * 10 + COB_D2I (*p);
if (++i == 2) {
p++;
break;
}
}
if (i != 2) {
/* possible template with partial system lookup */
if (*p == 'H') {
while (*p == 'H') p++;
} else {
ret = 1;
}
hh = -1;
} else if (hh > 23) {
ret = 1;
}
if (*p == ':'
|| *p == '-')
p++;
}
if (*p) {
mi = 0;
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
mi = mi * 10 + COB_D2I (*p);
if (++i == 2) {
p++;
break;
}
}
if (i != 2) {
/* possible template with partial system lookup */
if (*p == 'M') {
while (*p == 'M') p++;
} else {
ret = 1;
}
mi = -1;
} else if (mi > 59) {
ret = 1;
}
if (*p == ':'
|| *p == '-') {
p++;
}
}
if (*p != 0
&& *p != 'Z'
&& *p != '+'
&& *p != '-') {
ss = 0;
for (i = 0; *p != 0; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
ss = ss * 10 + COB_D2I (*p);
if (++i == 2) {
p++;
break;
}
}
if (i != 2) {
/* possible template with partial system lookup */
if (*p == 'S') {
while (*p == 'S') p++;
} else {
ret = 1;
}
ss = -1;
/* leap second would be 60 */
} else if (ss > 60) {
ret = 1;
}
}
/* extract nanoseconds */
if (*p != 0
&& *p != 'Z'
&& *p != '+'
&& *p != '-') {
ns = 0;
if (*p == '.'
|| *p == ':') {
p++;
}
for (i = 0; *p; p++) {
if (IS_INVALID_DIGIT_DATA (*p)) {
break;
}
ns = ns * 10 + COB_D2I (*p);
if (++i == 9) {
p++;
break;
}
}
}
/* extract UTC offset */
if (*p == 'Z') {
offset = 0;
iso_timezone[0] = 'Z';
} else
if (*p == '+'
|| *p == '-') {
/* we operate on a buffer here to drop the included ":" */
int len = snprintf (&iso_timezone[0], 7, "%s", p);
if (len == 3) {
memcpy (iso_timezone + 3, "00", 3);
} else
if (len >= 5 && iso_timezone[3] == ':') {
snprintf (&iso_timezone[3], 3, "%s", p + 4);
len--;
}
if (len > 5) {
ret = 1;
}
for (i = 1; i < 5 && iso_timezone[i] != 0; i++) {
if (IS_INVALID_DIGIT_DATA (iso_timezone[i])) {
break;
}
}
i--;
if (i == 4) {
offset = COB_D2I (iso_timezone[1]) * 60 * 10
+ COB_D2I (iso_timezone[2]) * 60
+ COB_D2I (iso_timezone[3]) * 10
+ COB_D2I (iso_timezone[4]);
if (iso_timezone[0] == '-') {
offset *= -1;
}
} else {
ret = 1;
iso_timezone[0] = '\0';
}
}
if (ret != 0) {
cob_runtime_warning (_("COB_CURRENT_DATE '%s' is invalid"), cobsetptr->cob_date);
return;
}
/* get local time, allocate tmptr */
time (&t);
tmptr = localtime (&t);
/* override given parts in time */
if (ss != -1) {
tmptr->tm_sec = ss;
}
if (mi != -1) {
tmptr->tm_min = mi;
}
if (hh != -1) {
tmptr->tm_hour = hh;
}
if (yr != -1) {
tmptr->tm_year = yr - 1900;
}
if (mm != -1) {
tmptr->tm_mon = mm - 1;
}
if (dd != -1) {
tmptr->tm_mday = dd;
}
tmptr->tm_isdst = -1;
/* normalize if needed (for example 30 Feb to be changed to
correct march date), set tm_wday, tm_yday and tm_isdst */
(void) mktime (tmptr);
/* set datetime constant */
if (yr != -1) {
cobsetptr->cob_time_constant.year = tmptr->tm_year + 1900;
} else {
cobsetptr->cob_time_constant.year = -1;
}
if (mm != -1) {
cobsetptr->cob_time_constant.month = tmptr->tm_mon + 1;
} else {
cobsetptr->cob_time_constant.month = -1;
}
if (dd != -1) {
cobsetptr->cob_time_constant.day_of_month = tmptr->tm_mday;
} else {
cobsetptr->cob_time_constant.day_of_month = -1;
}
if (hh != -1) {
cobsetptr->cob_time_constant.hour = tmptr->tm_hour;
} else {
cobsetptr->cob_time_constant.hour = -1;
}
if (mi != -1) {
cobsetptr->cob_time_constant.minute = tmptr->tm_min;
} else {
cobsetptr->cob_time_constant.minute = -1;
}
if (ss != -1) {
cobsetptr->cob_time_constant.second = tmptr->tm_sec;
} else {
cobsetptr->cob_time_constant.second = -1;
}
cobsetptr->cob_time_constant.nanosecond = ns;
/* the following are only set in the constant, if the complete date is set,
otherwise in the "current" instances */
if (yr != -1 && mm != -1 && dd != -1) {
cobsetptr->cob_time_constant_is_calculated = 1;
cobsetptr->cob_time_constant.day_of_week = one_indexed_day_of_week_from_monday (tmptr->tm_wday);
cobsetptr->cob_time_constant.day_of_year = tmptr->tm_yday + 1;
cobsetptr->cob_time_constant.is_daylight_saving_time = tmptr->tm_isdst;
} else {
cobsetptr->cob_time_constant_is_calculated = 0;
cobsetptr->cob_time_constant.day_of_week = -1;
cobsetptr->cob_time_constant.day_of_year = -1;
cobsetptr->cob_time_constant.is_daylight_saving_time = -1;
}
if (iso_timezone[0] != '\0') {
cobsetptr->cob_time_constant.offset_known = 1;
cobsetptr->cob_time_constant.utc_offset = offset;
} else {
cobsetptr->cob_time_constant.offset_known = 0;
cobsetptr->cob_time_constant.utc_offset = 0;
}
}
/* ACCEPT FROM system-name / DISPLAY UPON system-name */
/* get date as YYMMDD */
void
cob_accept_date (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_DATE);
const cob_u32_t val = time.day_of_month
+ time.month * 100
+ (time.year % 100) * 10000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 6;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
/* get date as YYYYMMDD */
void
cob_accept_date_yyyymmdd (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_DATE);
const cob_u32_t val = time.day_of_month
+ time.month * 100
+ time.year * 10000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 8;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
/* get day as YYDDD */
void
cob_accept_day (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_DATE);
const cob_u32_t val = time.day_of_year + (time.year % 100) * 1000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 5;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
/* get day as YYYYDDD */
void
cob_accept_day_yyyyddd (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_DATE);
const cob_u32_t val = time.day_of_year + time.year * 1000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 7;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
/* get day of week as 1 (monday) - 7 (sunday) */
void
cob_accept_day_of_week (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_DATE);
const unsigned char day = (unsigned char)(time.day_of_week + '0');
const unsigned short digits = 1;
cob_move_intermediate (f, &day, digits);
}
/* get time as HHMMSS[ss] */
void
cob_accept_time (cob_field *f)
{
const struct cob_time time = f->size > 6
? cob_get_current_datetime (DTR_FULL)
: cob_get_current_datetime (DTR_TIME_NO_NANO);
const cob_u32_t val = (time.nanosecond / 10000000)
+ time.second * 100
+ time.minute * 10000
+ time.hour * 1000000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 8;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
/* get time as HHMMSSssssss */
void
cob_accept_microsecond_time (cob_field *f)
{
const struct cob_time time = cob_get_current_datetime (DTR_FULL);
const cob_u64_t val = (cob_u64_t)(time.nanosecond / 1000)
+ (cob_u64_t)time.second * 1000000
+ (cob_u64_t)time.minute * 100000000
+ (cob_u64_t)time.hour * 10000000000;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 12;
COB_FIELD_INIT (sizeof (cob_u64_t), (unsigned char *)&val, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
if (COB_FIELD_TYPE (f) != COB_TYPE_GROUP) {
cob_move (&field, f);
} else {
cob_move_to_group_as_alnum (&field, f);
}
}
void
cob_display_command_line (cob_field *f)
{
if (commlnptr) {
cob_free (commlnptr);
}
commlnptr = cob_malloc (f->size + 1U);
commlncnt = f->size;
memcpy (commlnptr, f->data, commlncnt);
}
void
cob_accept_command_line (cob_field *f)
{
char *buff;
size_t i;
size_t size;
size_t len;
if (commlncnt) {
cob_move_intermediate (f, commlnptr, commlncnt);
return;
}
if (cob_argc <= 1) {
cob_move_intermediate (f, " ", (size_t)1);
return;
}
size = 0;
for (i = 1; i < (size_t)cob_argc; ++i) {
size += (strlen (cob_argv[i]) + 1);
if (size > f->size) {
break;
}
}
buff = cob_malloc (size);
buff[0] = ' ';
size = 0;
for (i = 1; i < (size_t)cob_argc; ++i) {
len = strlen (cob_argv[i]);
memcpy (buff + size, cob_argv[i], len);
size += len;
if (i != (size_t)cob_argc - 1U) {
buff[size++] = ' ';
}
if (size > f->size) {
break;
}
}
cob_move_intermediate (f, buff, size);
cob_free (buff);
}
/* Argument number */
void
cob_display_arg_number (cob_field *f)
{
int n;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 9;
COB_FIELD_INIT (4, (unsigned char *)&n, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
cob_move (f, &field);
if (n < 0 || n >= cob_argc) {
cob_set_exception (COB_EC_IMP_DISPLAY);
return;
}
current_arg = n;
}
void
cob_accept_arg_number (cob_field *f)
{
const cob_u32_t n = cob_argc - 1;
cob_field field;
cob_field_attr attr;
const unsigned short digits = 9;
COB_FIELD_INIT (sizeof (cob_u32_t), (unsigned char *)&n, &attr);
COB_ATTR_INIT (COB_TYPE_NUMERIC_BINARY, digits, 0, 0, NULL);
cob_move (&field, f);
}
void
cob_accept_arg_value (cob_field *f)
{
if (current_arg >= cob_argc) {
cob_set_exception (COB_EC_IMP_ACCEPT);
return;
}
cob_move_intermediate (f, cob_argv[current_arg],
strlen (cob_argv[current_arg]));
current_arg++;
}
/* Environment variable handling */
#ifdef _MSC_VER
/* _MSC does *NOT* have `setenv` (!)
But as the handling of the fallback `putenv` is different in POSIX and _MSC
(POSIX stores no duplicate of `putenv`, where _MSC does), we pretend to
have support for `setenv` and define it here with the same behaviour: */
static COB_INLINE COB_A_INLINE int
setenv (const char *name, const char *value, int overwrite) {
/* remark: _putenv_s does always overwrite, add a check for overwrite = 1 if necessary later */
COB_UNUSED (overwrite);
return _putenv_s (name, value);
}
static COB_INLINE COB_A_INLINE int
unsetenv (const char *name) {
return _putenv_s (name, "");
}
#endif
/* set entry into environment, with/without overwriting existing values */
int
cob_setenv (const char *name, const char *value, int overwrite) {
#if defined (HAVE_SETENV) && HAVE_SETENV
return setenv (name, value, overwrite);
#else
char *env;
size_t len;
COB_UNUSED (overwrite);
len = strlen (name) + strlen (value) + 2U;
env = cob_fast_malloc (len);
sprintf (env, "%s=%s", name, value);
return putenv (env);
#endif
}
/* remove entry from environment */
int
cob_unsetenv (const char *name) {
#if defined(HAVE_SETENV) && HAVE_SETENV
return unsetenv (name);
#else
char *env;
env = cob_fast_malloc (strlen (name) + 2U);
sprintf (env, "%s=", name);
return putenv (env);
#endif
}
/* resolve entry from environment */
char *
cob_getenv_direct (const char *name) {
return getenv (name);
}
/* resolve entry from environment and return an allocated string copy
--> call cob_free after use! */
char *
cob_getenv (const char *name)
{
if (name) {
char *p = getenv (name);
if (p) {
return cob_strdup (p);
}
}
return NULL;
}
int
cob_putenv (char *name)
{
int ret;
if (name && strchr (name, '=')) {
ret = putenv (cob_strdup (name));
if (!ret) {
cob_rescan_env_vals ();
}
return ret;
}
return -1;
}
void
cob_display_environment (const cob_field *f)
{
int ret;
if (cob_local_env_size < f->size) {
cob_local_env_size = f->size;
if (cob_local_env) {
cob_free (cob_local_env);
}
cob_local_env = cob_malloc (cob_local_env_size + 1U);
}
ret = cob_field_to_string (f, cob_local_env, cob_local_env_size, CCM_NONE);
if (ret < 0) {
return;
}
if (unlikely (cobsetptr->cob_env_mangle)) {
const size_t len = ret;
size_t i;
for (i = 0; i < len; ++i) {
if (!isalnum ((int)cob_local_env[i])) {
cob_local_env[i] = '_';
}
}
}
}
/* DISPLAY ... UPON ENVIRONMENT VALUE */
void
cob_display_env_value (const cob_field *f)
{
int ret;
if (!cob_local_env
|| !cob_local_env[0]) {
cob_set_exception (COB_EC_IMP_DISPLAY);
return;
}
{
char buff[COB_MEDIUM_BUFF];
int flen = cob_field_to_string (f, buff,
COB_MEDIUM_MAX, CCM_NONE);
if (flen < 0) {
cob_set_exception (COB_EC_IMP_DISPLAY);
return;
}
ret = cob_setenv (cob_local_env, buff, 1);
}
if (ret != 0) {
cob_set_exception (COB_EC_IMP_DISPLAY);
return;
}
/* Rescan term/screen variables */
cob_rescan_env_vals ();
}
void
cob_set_environment (const cob_field *f1, const cob_field *f2)
{
cob_display_environment (f1);
cob_display_env_value (f2);
}
void
cob_get_environment (const cob_field *envname, cob_field *envval)
{
char buff[COB_MEDIUM_BUFF];
char *p;
int flen;
if (envname->size == 0 || envval->size == 0) {
cob_set_exception (COB_EC_IMP_ACCEPT);
return;
}
flen = cob_field_to_string (envname, buff,
COB_MEDIUM_MAX, CCM_NONE);
if (flen < 1) {
cob_set_exception (COB_EC_IMP_ACCEPT);
return;
}
if (unlikely (cobsetptr->cob_env_mangle)) {
const char *p_end = buff + flen;
for (p = buff; p < p_end; ++p) {
if (!isalnum ((int)*p)) {
*p = '_';
}
}
}
p = getenv (buff);
if (p) {
cob_move_intermediate (envval, p, strlen (p));
} else {
cob_set_exception (COB_EC_IMP_ACCEPT);
cob_move_intermediate (envval, " ", 1);
}
}
void
cob_accept_environment (cob_field *f)
{
const char *p = NULL;
if (cob_local_env) {
p = getenv (cob_local_env);
}
if (p) {
cob_move_intermediate (f, p, strlen (p));
} else {
cob_set_exception (COB_EC_IMP_ACCEPT);
cob_move_intermediate (f, " ", 1);
}
}
void
cob_chain_setup (void *data, const size_t parm, const size_t size)
{
/* only set if given on command-line, otherwise use normal
program internal initialization */
if (parm <= (size_t)cob_argc - 1) {
const size_t len = strlen (cob_argv[parm]);
memset (data, ' ', size);
if (len <= size) {
memcpy (data, cob_argv[parm], len);
} else {
memcpy (data, cob_argv[parm], size);
}
}
}
void
cob_continue_after (cob_field *decimal_seconds)
{
cob_s64_t nanoseconds = get_sleep_nanoseconds_from_seconds (decimal_seconds);
if (nanoseconds < 0) {
cob_set_exception (COB_EC_CONTINUE_LESS_THAN_ZERO);
return;
}
internal_nanosleep (nanoseconds);
}
/* ALLOCATE statement
dataptr -> used for ALLOCATE identifier only, NULL otherwise
retptr -> RETURNING ret, may be NULL
initialize -> used for ALLOCATE CHARACTERS only, may be NULL
*/
void
cob_allocate (unsigned char **dataptr, cob_field *retptr,
cob_field *sizefld, cob_field *initialize)
{
const cob_s64_t fsize = cob_get_llint (sizefld);
void *mptr = NULL;
cobglobptr->cob_exception_code = 0;
if (fsize > COB_MAX_ALLOC_SIZE) {
cob_set_exception (COB_EC_STORAGE_IMP);
} else if (fsize > 0) {
const size_t memsize = (size_t)fsize;
if (initialize
&& initialize->data[0] == 0
&& COB_FIELD_TYPE (initialize) == COB_TYPE_ALPHANUMERIC_ALL) {
mptr = calloc (1, memsize);
} else {
mptr = malloc (memsize);
}
if (!mptr) {
cob_set_exception (COB_EC_STORAGE_NOT_AVAIL);
} else {
struct cob_alloc_cache *cache_ptr;
if (initialize) {
cob_field temp;
temp.size = memsize;
temp.data = mptr;
temp.attr = &const_alpha_attr;
cob_move (initialize, &temp);
}
#if 0
else {
memset (mptr, 0, memsize);
}
#endif
cache_ptr = cob_malloc (sizeof (struct cob_alloc_cache));
cache_ptr->cob_pointer = mptr;
cache_ptr->size = memsize;
cache_ptr->next = cob_alloc_base;
cob_alloc_base = cache_ptr;
}
}
if (dataptr) {
*dataptr = mptr;
}
if (retptr) {
*(void **)(retptr->data) = mptr;
}
}
/* FREE statement */
void
cob_free_alloc (unsigned char **ptr1, unsigned char *ptr2)
{
struct cob_alloc_cache *cache_ptr = cob_alloc_base;
struct cob_alloc_cache *prev_ptr = cob_alloc_base;
cobglobptr->cob_exception_code = 0;
if (ptr1 && *ptr1) {
void *vptr1;
vptr1 = *ptr1;
for (; cache_ptr; cache_ptr = cache_ptr->next) {
if (vptr1 == cache_ptr->cob_pointer) {
cob_free (cache_ptr->cob_pointer);
if (cache_ptr == cob_alloc_base) {
cob_alloc_base = cache_ptr->next;
} else {
prev_ptr->next = cache_ptr->next;
}
cob_free (cache_ptr);
*ptr1 = NULL;
return;
}
prev_ptr = cache_ptr;
}
cob_set_exception (COB_EC_STORAGE_NOT_ALLOC);
return;
}
if (ptr2 && *(void **)ptr2) {
for (; cache_ptr; cache_ptr = cache_ptr->next) {
if (*(void **)ptr2 == cache_ptr->cob_pointer) {
cob_free (cache_ptr->cob_pointer);
if (cache_ptr == cob_alloc_base) {
cob_alloc_base = cache_ptr->next;
} else {
prev_ptr->next = cache_ptr->next;
}
cob_free (cache_ptr);
*(void **)ptr2 = NULL;
return;
}
prev_ptr = cache_ptr;
}
cob_set_exception (COB_EC_STORAGE_NOT_ALLOC);
return;
}
}
#if 0 /* debug only */
void print_stat (const char *filename, struct stat sb)
{
printf("File name: ");
if (filename) {
printf ("%s\n", filename);
} else {
printf("- unknown -\n");
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
#ifdef S_IFBLK
case S_IFBLK: printf("block device\n"); break;
#endif
#ifdef S_IFCHR
case S_IFCHR: printf("character device\n"); break;
#endif
case S_IFDIR: printf("directory\n"); break;
#ifdef S_IFIFO
case S_IFIFO: printf("FIFO/pipe\n"); break;
#endif
#ifdef S_IFLNK
case S_IFLNK: printf("symlink\n"); break;
#endif
case S_IFREG: printf("regular file\n"); break;
#ifdef S_IFSOCK
case S_IFSOCK: printf("socket\n"); break;
#endif
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long)sb.st_ino);
printf("Mode: %lo (octal)\n",
(unsigned long)sb.st_mode);
printf("Link count: %ld\n", (long)sb.st_nlink);
printf("Ownership: UID=%ld GID=%ld\n",
(long)sb.st_uid, (long)sb.st_gid);
printf("File size: %lld bytes\n",
(long long)sb.st_size);
#if 0
printf("Preferred I/O block size: %ld bytes\n",
(long)sb.st_blksize);
printf("Blocks allocated: %lld\n",
(long long)sb.st_blocks);
#endif
printf("Last status change: %s", ctime(&sb.st_ctime));
printf("Last file access: %s", ctime(&sb.st_atime));
printf("Last file modification: %s", ctime(&sb.st_mtime));
}
#endif
static COB_INLINE int
check_valid_dir (const char *dir)
{
struct stat sb;
if (strlen (dir) > COB_NORMAL_MAX) return 1;
if (stat (dir, &sb) || !(S_ISDIR (sb.st_mode))) return 1;
#if 0
print_stat (dir, sb);
#endif
return 0;
}
static const char *
check_valid_env_tmpdir (const char *envname)
{
const char *dir;
dir = getenv (envname);
if (!dir || !dir[0]) {
return NULL;
}
if (check_valid_dir (dir)) {
cob_runtime_warning ("Temporary directory %s is invalid, adjust TMPDIR!", envname);
(void)cob_unsetenv (envname);
return NULL;
}
return dir;
}
/* return pointer to TMPDIR without trailing slash */
static const char *
cob_gettmpdir (void)
{
static const char *tmpdir = NULL;
char *tmp;
if (tmpdir != NULL) {
return tmpdir;
}
tmp = NULL;
/* target directory, also used by most called external tools*/
if ((tmpdir = check_valid_env_tmpdir ("TMPDIR")) == NULL
/* OS specific temporary paths */
#ifdef _WIN32
&& (tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL
&& (tmpdir = check_valid_env_tmpdir ("TMP")) == NULL
&& (tmpdir = check_valid_env_tmpdir ("USERPROFILE")) == NULL) {
#else
&& (tmpdir = check_valid_env_tmpdir ("TMP")) == NULL
&& (tmpdir = check_valid_env_tmpdir ("TEMP")) == NULL) {
if (!check_valid_dir ("/tmp")) {
tmp = cob_fast_malloc (5U);
strcpy (tmp, "/tmp");
tmpdir = tmp;
}
#endif
}
/* fallback if still not valid: current directory */
if (!tmpdir) {
tmp = cob_fast_malloc (2U);
tmp[0] = '.';
tmp[1] = 0;
tmpdir = tmp;
} else {
/* if we have a valid path: ensure there's no trailing slash */
size_t size = strlen (tmpdir) - 1;
if (tmpdir[size] == SLASH_CHAR) {
tmp = (char*)cob_fast_malloc (size + 1);
memcpy (tmp, tmpdir, size);
tmp[size] = 0;
tmpdir = tmp;
}
}
/* ensure TMPDIR is set for called tools (which partially break hard otherwise) */
(void)cob_setenv ("TMPDIR", tmpdir, 1);
if (tmp) {
cob_free ((void *)tmp);
}
/* get the pointer to the environment copy - as this may point to a different place -
store it for subsequent calls and finally return it to the caller */
tmpdir = getenv ("TMPDIR");
return tmpdir;
}
/* Set temporary file name */
void
cob_temp_name (char *filename, const char *ext)
{
int pid = cob_sys_getpid ();
#ifndef HAVE_8DOT3_FILENAMES
#define TEMP_EXT_SCHEMA "%s%ccob%d_%d%s"
#define TEMP_SORT_SCHEMA "%s%ccobsort%d_%d"
#else
/* 8.3 allows only short names... */
#define TEMP_EXT_SCHEMA "%s%cc%d_%d%s"
#define TEMP_SORT_SCHEMA "%s%cs%d_%d"
pid = pid % 9999;
#endif
if (ext) {
snprintf (filename, (size_t)COB_FILE_MAX, TEMP_EXT_SCHEMA,
cob_gettmpdir (), SLASH_CHAR, pid, cob_temp_iteration, ext);
} else {
snprintf (filename, (size_t)COB_FILE_MAX, TEMP_SORT_SCHEMA,
cob_gettmpdir (), SLASH_CHAR, pid, cob_temp_iteration);
}
#undef TEMP_EXT_SCHEMA
#undef TEMP_SORT_SCHEMA
}
void
cob_incr_temp_iteration (void)
{
cob_temp_iteration++;
}
int
cob_extern_init (void)
{
/* can be called multiple times (MF docs say: should be done in all threads) */
if (!cob_initialized) {
cob_init (0, NULL);
}
return 0;
}
char *
cob_command_line (int flags, int *pargc, char ***pargv,
char ***penvp, char **pname)
{
#if 0 /* RXWRXW cob_command_line */
char **spenvp;
char *spname;
#else
COB_UNUSED (penvp);
COB_UNUSED (pname);
#endif
COB_UNUSED (flags);
if (!cob_initialized) {
cob_fatal_error (COB_FERROR_INITIALIZED);
}
if (pargc && pargv) {
cob_argc = *pargc;
cob_argv = *pargv;
}
#if 0 /* RXWRXW cob_command_line */
if (penvp) {
spenvp = *penvp;
}
if (pname) {
spname = *pname;
}
#endif
/* What are we supposed to return here? */
return NULL;
}
int
cob_tidy (void)
{
if (!cob_initialized) {
exit_code = -1;
return 1;
}
exit_code = 0;
call_exit_handlers_and_terminate ();
return 0;
}
/* System routines */
/* CBL_EXIT_PROC - register exit handlers that will be called
before teardown (after posible error procedures) without
any parameters passed
'dispo': intallation flag (add/remove/priority)
'pptr': function / ENTRY point to be called */
int
cob_sys_exit_proc (const void *dispo, const void *pptr)
{
struct exit_handlerlist *hp, *h;
unsigned char install_flag;
/* only initialized to silence -Wmaybe-uninitialized */
unsigned char priority = 0;
int (**p)(void);
COB_CHK_PARMS (CBL_EXIT_PROC, 2);
#if 0 /* TODO: take care of ACU variant:
pptr is not an already resolved entry point
but a name which is to be cob_resolve'd (at use-time);
maybe resolve here and return -1 if not possible;
furthermore the second parameter is a mixed priority + install_flag */
if (something) {
const char *name = (char *)pptr;
pptr = cob_resolve_cobol (name, 0, 0);
if (!p) {
return -1;
}
priority = *(unsigned char *)dispo;
if (priority == 254) {
install_flag = 1;
} else if (priority == 255) {
install_flag = 2;
} else {
install_flag = 3;
}
} else {
#endif
memcpy (&p, &pptr, sizeof (void *));
if (!p || !*p) {
return -1;
}
install_flag = *(unsigned char*)dispo;
switch (install_flag) {
case 0:
priority = 64;
break;
case 1:
case 2:
/* remove / query - no need to check priority */
break;
case 3:
/* set with explicit priority */
priority = *((unsigned char*)pptr + sizeof (void*));
if (priority > 127) {
priority = 64;
}
break;
default:
return -1;
}
#if 0
}
#endif
hp = NULL;
h = exit_hdlrs;
/* Search handler, remove if not function 2 */
while (h != NULL) {
if (h->proc == *p) {
switch (install_flag) {
case 2:
/* Return priority of installed handler */
#if 0 /* TODO: take care of ACU variant: priority in return */
if (something) {
return priority;
}
#endif
*((unsigned char*)pptr + sizeof (void*)) = h->priority;
return 0;
case 0:
case 3:
if (priority == h->priority) {
return -1;
}
break;
default:
break;
}
if (hp != NULL) {
hp->next = h->next;
} else {
exit_hdlrs = h->next;
}
cob_free (h);
/* Remove handler --> done */
if (install_flag == 1) {
return 0;
}
break;
}
hp = h;
h = h->next;
}
if (install_flag == 1
|| install_flag == 2) {
/* deete or query priority with not available */
#if 0 /* TODO: take care of ACU variant: priority 255 = not available */
if (something) {
return 255;
}
#endif
return -1;
}
h = cob_malloc (sizeof (struct exit_handlerlist));
h->next = exit_hdlrs;
h->proc = *p;
h->priority = priority;
exit_hdlrs = h;
return 0;
}
/* CBL_ERROR_PROC - register error handlers that will be called
on runtime errors and may early-stop, those are called with a single
parameter containing the error message
'dispo': intallation flag (add/remove/priority)
'pptr': function / ENTRY point to be called */
int
cob_sys_error_proc (const void *dispo, const void *pptr)
{
struct handlerlist *hp, *h;
const unsigned char *x;
int (**p) (char *s);
COB_CHK_PARMS (CBL_ERROR_PROC, 2);
#if 0 /* TODO: take care of ACU variant:
pptr is not an already resolved entry point
but a name which is to be cob_resolve'd (at use-time);
maybe resolve here and return -1 if not possible */
if (something) {
const char *name = (char *)pptr;
pptr = cob_resolve_cobol (name, 0, 0);
if (!p) {
return -1;
}
} else {
#endif
memcpy (&p, &pptr, sizeof (void *));
if (!p || !*p) {
return -1;
}
#if 0
}
#endif
hp = NULL;
h = hdlrs;
/* Search for existing handler */
while (h != NULL) {
if (h->proc == *p) {
break;
}
hp = h;
h = h->next;
}
x = dispo;
if (*x != 0) {
/* Remove handler */
if (h != NULL) {
if (hp != NULL) {
hp->next = h->next;
} else {
hdlrs = h->next;
}
cob_free (h);
}
} else {
if (h == NULL) {
/* insert handler */
h = cob_malloc (sizeof (struct handlerlist));
h->next = hdlrs;
h->proc = *p;
hdlrs = h;
} else {
#if 0 /* TODO: take care of ACU variant: placing it first */
if (something) {
if (hp != NULL) {
hp->next = h->next;
}
h->next = hdlrs;
hdlrs = h;
} else {
/* MF-Variant: when already existing: do nothing */
return 0;
}
#else
/* MF-Variant: when already existing: do nothing */
return 0;
#endif
}
}
return 0;
}
int
cob_sys_runtime_error_proc (const void *err_flags, const void *err_msg)
{
const char *msg = (const char*)err_msg;
COB_CHK_PARMS (CBL_RUNTIME_ERROR, 2);
COB_UNUSED (err_flags);
if (msg && msg[0]) {
cob_runtime_error ("%s: %s",
_("Program abandoned at user request"), msg);
} else {
cob_runtime_error ("%s",
_("Program abandoned at user request"));
}
cob_hard_failure ();
}
int
cob_sys_system (const void *cmdline)
{
COB_CHK_PARMS (SYSTEM, 1);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
const char *cmd = cmdline;
size_t i = COB_MODULE_PTR->cob_procedure_params[0]->size;
/* FIXME: if caller wasn't COBOL then size must be evaluated by strlen */
i--;
do {
if (cmd[i] != ' ' && cmd[i] != 0) {
break;
}
} while (--i != 0);
if (i > 0) {
char *command;
/* LCOV_EXCL_START */
if (unlikely (i > COB_MEDIUM_MAX)) {
cob_runtime_warning (_("parameter to SYSTEM call is larger than %d characters"),
COB_MEDIUM_MAX);
return 1;
}
/* LCOV_EXCL_STOP */
#ifdef _WIN32
/* All known _WIN32 implementations use MSVCRT's system()
which passes the given commandline as parameter to "cmd /k".
Because "of compatibility" this checks if you have a
leading and trailing " and if yes simply removes them (!).
Check if this is the case and if it is handled already
by an *extra* pair of quotes, otherwise add these...
This fixes CALL 'SYSTEM' USING '"someprog" "opt"' being
executed as 'someprog" "opt'.
*/
if (i > 2 && cmd[0] == '"' && cmd[i] == '"'
&& (cmd[1] != '"' || cmd[i - 1] != '"')) {
command = cob_malloc (i + 4);
command[0] = '"';
memcpy (command + 1, cmd, i + 1);
command[i + 1] = '"';
} else {
#endif /* _WIN32 */
command = cob_malloc (i + 2);
memcpy (command, cmd, i + 1);
#ifdef _WIN32
}
#endif
{
int status;
if (cobglobptr->cob_screen_initialized) {
cob_screen_set_mode (0);
}
/* note: if the command cannot be executed _WIN32 always returns 1
while GNU/Linux returns -1 */
status = system (command);
if (cobglobptr->cob_screen_initialized) {
cob_screen_set_mode (1U);
}
#ifdef WIFSIGNALED
if (WIFSIGNALED (status)) {
int sig = WTERMSIG (status);
const char *signal_name = cob_get_sig_name (sig);
cob_runtime_warning (_("external process \"%s\" ended with signal %s (%d)"),
command, signal_name, sig);
}
#endif
cob_free (command);
#if 0 /* possibly do this, but only if explicit asked for via a new runtime configuration
as at least MicroFocus always returns all bytes here;
from its docs it _looks_ like ACU only returns the lower bytes ... */
#ifdef WEXITSTATUS
if (WIFEXITED (status)) {
status = WEXITSTATUS (status);
}
#endif
#endif
return status;
}
}
}
return 1;
}
/**
* Return some hosted C variables, argc, argv, stdin, stdout, stderr.
*/
int
cob_sys_hosted (void *p, const void *var)
{
const char *name = var;
cob_u8_ptr data = p;
size_t i;
COB_CHK_PARMS (CBL_GC_HOSTED, 2);
if (!data) {
return 1;
}
if (COB_MODULE_PTR->cob_procedure_params[1]) {
i = (int)COB_MODULE_PTR->cob_procedure_params[1]->size;
if ((i == 4) && !memcmp (name, "argc", 4)) {
*((int *)data) = cob_argc;
return 0;
}
if ((i == 4) && !memcmp (name, "argv", 4)) {
*((char ***)data) = cob_argv;
return 0;
}
if ((i == 5) && !memcmp (name, "stdin", 5)) {
*((FILE **)data) = stdin;
return 0;
}
if ((i == 6) && !memcmp (name, "stdout", 6)) {
*((FILE **)data) = stdout;
return 0;
}
if ((i == 6) && !memcmp (name, "stderr", 6)) {
*((FILE **)data) = stderr;
return 0;
}
if ((i == 5) && !memcmp (name, "errno", 5)) {
*((int **)data) = &errno;
return 0;
}
#if defined (HAVE_TIMEZONE)
if ((i == 6) && !memcmp (name, "tzname", 6)) {
/* Recheck: bcc raises "suspicious pointer conversion */
*((char ***)data) = tzname;
return 0;
}
if ((i == 8) && !memcmp (name, "timezone", 8)) {
*((long *)data) = timezone;
return 0;
}
if ((i == 8) && !memcmp (name, "daylight", 8)) {
*((int *)data) = daylight;
return 0;
}
#endif /* HAVE_TIMEZONE */
}
return 1;
}
int
cob_sys_and (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_AND, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] &= data_1[n];
}
return 0;
}
int
cob_sys_or (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_OR, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] |= data_1[n];
}
return 0;
}
int
cob_sys_nor (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_NOR, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] = ~(data_1[n] | data_2[n]);
}
return 0;
}
int
cob_sys_xor (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_XOR, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] ^= data_1[n];
}
return 0;
}
/* COBOL routine to perform for logical IMPLIES between the bits in two fields,
storing the result in the second field */
int
cob_sys_imp (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_IMP, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] = (~data_1[n]) | data_2[n];
}
return 0;
}
/* COBOL routine to perform for logical NOT IMPLIES between the bits in two fields,
storing the result in the second field */
int
cob_sys_nimp (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_NIMP, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] = data_1[n] & (~data_2[n]);
}
return 0;
}
/* COBOL routine to check for logical EQUIVALENCE between the bits in two fields,
storing the result in the second field */
int
cob_sys_eq (const void *p1, void *p2, const int length)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_EQ, 3);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_2[n] = ~(data_1[n] ^ data_2[n]);
}
return 0;
}
/* COBOL routine to perform a logical NOT on the bits of a field */
int
cob_sys_not (void *p1, const int length)
{
cob_u8_ptr data_1 = p1;
size_t n;
COB_CHK_PARMS (CBL_NOT, 2);
if (length <= 0) {
return 0;
}
for (n = 0; n < (size_t)length; ++n) {
data_1[n] = ~data_1[n];
}
return 0;
}
/* COBOL routine to pack the least significant bits in eight bytes into a single byte */
int
cob_sys_xf4 (void *p1, const void *p2)
{
cob_u8_ptr data_1 = p1;
const cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_XF4, 2);
*data_1 = 0;
for (n = 0; n < 8; ++n) {
*data_1 |= (data_2[n] & 1) << (7 - n);
}
return 0;
}
/* COBOL routine to unpack the bits in a byte into eight bytes */
int
cob_sys_xf5 (const void *p1, void *p2)
{
const cob_u8_ptr data_1 = p1;
cob_u8_ptr data_2 = p2;
size_t n;
COB_CHK_PARMS (CBL_XF5, 2);
for (n = 0; n < 8; ++n) {
data_2[n] = (*data_1 & (1 << (7 - n))) ? 1 : 0;
}
return 0;
}
/* COBOL (only) routine for different functions, including functions for
the programmable COBOL SWITCHES:
11: set COBOL switches 0-7 and debug switch
12: read COBOL switches 0-7 and debug switch
16: return number of CALL USING parameters
*/
int
cob_sys_x91 (void *p1, const void *p2, void *p3)
{
cob_u8_ptr result = p1;
const cob_u8_ptr func = p2;
cob_u8_ptr parm = p3;
unsigned char *p;
size_t i;
switch (*func) {
/* Set switches (0-7) + DEBUG module */
case 11:
p = parm;
for (i = 0; i < 8; ++i, ++p) {
if (*p == 0) {
cob_switch[i] = 0;
} else if (*p == 1) {
cob_switch[i] = 1;
}
}
/* MF additionally sets the ANSI DEBUG module switch */
if (COB_MODULE_PTR->cob_procedure_params[0]->size >= 9) {
p++;
cobsetptr->cob_debugging_mode = (*p == 1);
}
*result = 0;
break;
/* Get switches (0-7) */
case 12:
p = parm;
for (i = 0; i < 8; ++i, ++p) {
*p = (unsigned char)cob_switch[i];
}
/* MF additionally passes the ANSI DEBUG module switch */
if (COB_MODULE_PTR->cob_procedure_params[0]->size >= 9) {
p++;
*p = (unsigned char)cobsetptr->cob_debugging_mode;
}
*result = 0;
break;
/* Set switches (A-Z -> 11-36) */
case 13:
p = parm;
for (i = 11; i < 36; ++i, ++p) {
if (*p == 0) {
cob_switch[i] = 0;
} else if (*p == 1) {
cob_switch[i] = 1;
}
if (i == 'D' - 'A' + 11) {
cobsetptr->cob_debugging_mode = cob_switch[i];
} else if (i == 'N' - 'A' + 11) {
cobsetptr->cob_ls_nulls = cob_switch[i];
#if 0 /* TODO add in trunk*/
} else if (i == 'T' - 'A' + 11) {
cobsetptr->cob_ls_tabs = cob_switch[i];
#endif
}
}
*result = 0;
break;
/* Get switches (A-Z -> 11-36) */
case 14:
p = parm;
for (i = 1; i < 27; ++i, ++p) {
*p = (unsigned char)cob_switch[i];
}
*result = 0;
break;
#if 0 /* program lookup
may be implemented as soon as some legacy code
shows its exact use and a test case */
case 15:
p = parm + 1;
{
char name[256];
strncpy (name, p, *parm);
void * func = cob_resolve (name);
/* TODO: the full name should be copied back into p */
return (func != NULL);
}
break;
#endif
/* Return number of call parameters
according to the docs this is only set for programs CALLed from COBOL
NOT for main programs in contrast to C$NARG (cob_sys_return_args)
MF deprecated it in favor of CBL_GET_PROGRAM_INFO function 8 */
case 16:
*parm = (unsigned char)COB_MODULE_PTR->module_num_params;
*result = 0;
break;
#if 1 /* EXEC call "like DOS 4B call"
working prototype, may be finalized as soon as some legacy code
shows its exact use and a test case; CHECKME: what is the return
code with MF on UNIX where this is "not supported"? */
case 35:
p = parm + 1;
/* zero = just [re-]execute */
if (*parm != 0) {
/* note: we can't check for existence
as "pause" and similar inbuilts must also work;
CHECKME: possibly start via cmd.exe wrapper ? */
/* put on command line here */
{
cob_field field;
COB_FIELD_INIT (*parm, p, NULL);
cob_display_command_line (&field);
}
}
{
/* execute the command line */
int ret = system ((const char *)commlnptr);
*result = (unsigned char)ret;
}
break;
#endif
#if 0 /* note: 46-49 should be implemented in 4.x with file-specific settings */
/* enable/disable LS_NULLs for a specific FD */
case 46:
case 47:
/* enable/disable LS_TABs for a specific FD */
case 48:
case 49:
{
*result = 0;
cob_file *f = get_file (p3);
if (f == NULL
|| f->open_mode == COB_OPEN_CLOSED
|| f->open_mode == COB_OPEN_LOCKED) {
*result = 1;
} else if (*func == 46) {
f->ls_nulls = 1;
} else if (*func == 47) {
f->ls_nulls = 0;
} else if (*func == 48) {
f->ls_tabss = 1;
} else if (*func == 49) {
f->ls_tabs = 0;
}
}
break;
#endif
#if 0 /* directory search
may be implemented when CBL_DIR_SCAN / C$LISTDIR is added and
likely only finalized as soon as some legacy code
shows its exact use and a test case
MF deprecated it in favor of CBL_DIR_SCAN */
case 69:
*result = 1;
break;
#endif
/* unimplemented function */
default:
*result = 1;
break;
}
return 0;
}
int
cob_sys_toupper (void *p1, const int length)
{
cob_u8_ptr data = p1;
size_t n;
COB_CHK_PARMS (CBL_TOUPPER, 2);
if (length > 0) {
for (n = 0; n < (size_t)length; ++n) {
data[n] = (cob_u8_t)toupper (data[n]);
}
}
return 0;
}
int
cob_sys_tolower (void *p1, const int length)
{
cob_u8_ptr data = p1;
size_t n;
COB_CHK_PARMS (CBL_TOLOWER, 2);
if (length > 0) {
for (n = 0; n < (size_t)length; ++n) {
data[n] = (cob_u8_t)tolower (data[n]);
}
}
return 0;
}
/* maximum sleep time in seconds, currently 7 days */
#define MAX_SLEEP_TIME 3600*24*7
static cob_s64_t
get_sleep_nanoseconds (cob_field *nano_seconds) {
cob_s64_t nanoseconds = cob_get_llint (nano_seconds);
if (nanoseconds < 0) {
return -1;
}
if (nanoseconds >= ((cob_s64_t)MAX_SLEEP_TIME * 1000000000)) {
return (cob_s64_t)MAX_SLEEP_TIME * 1000000000;
} else {;
return nanoseconds;
}
}
static cob_s64_t
get_sleep_nanoseconds_from_seconds (cob_field *decimal_seconds) {
#define MAX_SLEEP_TIME 3600*24*7
cob_s64_t seconds = cob_get_llint (decimal_seconds);
if (seconds < 0) {
return -1;
}
if (seconds >= MAX_SLEEP_TIME) {
return (cob_s64_t)MAX_SLEEP_TIME * 1000000000;
} else {
cob_s64_t nanoseconds;
cob_field temp;
temp.size = 8;
temp.data = (unsigned char *)&nanoseconds;
temp.attr = &const_bin_nano_attr;
cob_move (decimal_seconds, &temp);
return nanoseconds;
}
}
static void
internal_nanosleep (cob_s64_t nsecs)
{
#ifdef HAVE_NANO_SLEEP
struct timespec tsec;
tsec.tv_sec = nsecs / 1000000000;
tsec.tv_nsec = nsecs % 1000000000;
nanosleep (&tsec, NULL);
#else
unsigned int msecs;
#if defined (_WIN32)
msecs = (unsigned int)(nsecs / 1000000);
if (msecs > 0) {
Sleep (msecs);
}
#else /* includes "defined (__370__) || defined (__OS400__)" */
msecs = (unsigned int)(nsecs / 100000000);
if (msecs % 10 > 4) {
msecs = (msecs / 10) + 1;
} else {
msecs = msecs / 10;
}
if (msecs > 0) {
sleep (msecs);
}
#endif
#endif
}
/* CBL_GC_NANOSLEEP / CBL_OC_NANOSLEEP, origin: OpenCOBOL */
int
cob_sys_oc_nanosleep (const void *data)
{
COB_UNUSED (data);
COB_CHK_PARMS (CBL_GC_NANOSLEEP, 1);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
cob_s64_t nsecs
= get_sleep_nanoseconds (COB_MODULE_PTR->cob_procedure_params[0]);
if (nsecs > 0) {
internal_nanosleep (nsecs);
}
return 0;
}
return -1;
}
/* C$SLEEP, origin: ACUCOBOL */
int
cob_sys_sleep (const void *data)
{
COB_UNUSED (data);
COB_CHK_PARMS (C$SLEEP, 1);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
cob_s64_t nanoseconds
= get_sleep_nanoseconds_from_seconds (COB_MODULE_PTR->cob_procedure_params[0]);
if (nanoseconds < 0) {
/* ACUCOBOL specifies a runtime error here... */
return -1;
}
internal_nanosleep (nanoseconds);
return 0;
}
return 0; /* CHECKME */
}
int
cob_sys_getpid (void)
{
if (!cob_process_id) {
cob_process_id = (int)getpid ();
}
return cob_process_id;
}
int
cob_sys_fork (void)
{
/* cygwin does not define _WIN32, but implements [slow] fork() and provides unistd.h
MSYS defines _WIN32, provides unistd.h and not implements fork()
*/
#if defined (HAVE_UNISTD_H) && !(defined (_WIN32))
int pid;
if ((pid = fork ()) == 0 ) {
cob_process_id = 0; /* reset cached value */
return 0; /* child process just returns */
}
if (pid < 0) { /* Some error happened */
cob_runtime_warning (_("error '%s' during CBL_GC_FORK"), cob_get_strerror ());
return -2;
}
return pid; /* parent gets process id of child */
#else
cob_runtime_warning (_("'%s' is not supported on this platform"), "CBL_GC_FORK");
return -1;
#endif
}
/* wait for a pid to end and return its exit code
error codes are returned as negative value
*/
int
cob_sys_waitpid (const void *p_id)
{
#ifdef HAVE_SYS_WAIT_H
int pid, status, wait_sts;
COB_UNUSED (p_id);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
pid = cob_get_int (COB_MODULE_PTR->cob_procedure_params[0]);
if (pid == cob_sys_getpid ()) {
status = 0 - EINVAL;
return status;
}
wait_sts = waitpid (pid, &status, 0);
if (wait_sts < 0) { /* Some error happened */
status = 0 - errno;
cob_runtime_warning (_("error '%s' for P%d during CBL_GC_WAITPID"),
cob_get_strerror (), pid);
return status;
}
status = WEXITSTATUS (status);
} else {
status = 0 - EINVAL;
}
return status;
#elif defined (_WIN32)
int pid, status;
HANDLE process = NULL;
DWORD ret;
COB_UNUSED (p_id);
status = 0;
if (COB_MODULE_PTR->cob_procedure_params[0]) {
pid = cob_get_int (COB_MODULE_PTR->cob_procedure_params[0]);
if (pid == cob_sys_getpid ()) {
status = 0 - ERROR_INVALID_DATA;
return status;
}
/* get process handle with least necessary rights
PROCESS_QUERY_LIMITED_INFORMATION is available since OS-version Vista / Server 2008
and always leads to ERROR_ACCESS_DENIED on older systems
PROCESS_QUERY_INFORMATION needs more rights
SYNCHRONIZE necessary for WaitForSingleObject
*/
#if defined (PROCESS_QUERY_LIMITED_INFORMATION)
process = OpenProcess (SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION, FALSE, pid);
#if !defined (_MSC_VER) || COB_USE_VC2012_OR_GREATER /* only try a higher level if we possibly compile on XP/2003 */
/* TODO: check what happens on WinXP / 2003 as PROCESS_QUERY_LIMITED_INFORMATION isn't available there */
if (!process && GetLastError () == ERROR_ACCESS_DENIED) {
process = OpenProcess (SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid);
}
#endif
#else
process = OpenProcess (SYNCHRONIZE | PROCESS_QUERY_INFORMATION, FALSE, pid);
#endif
/* if we don't get access to query the process' exit status try to get at least
access to the process end (needed for WaitForSingleObject)
*/
if (!process && GetLastError () == ERROR_ACCESS_DENIED) {
process = OpenProcess (SYNCHRONIZE, FALSE, pid);
status = -2;
}
if (process) {
/* wait until process exit */
ret = WaitForSingleObject (process, INFINITE);
if (ret == WAIT_FAILED) {
status = 0 - GetLastError ();
/* get exit code, if possible */
} else if (status != -2) {
if (!GetExitCodeProcess (process, &ret)) {
status = 0 - GetLastError ();
} else {
status = (int) ret;
}
}
CloseHandle (process);
} else {
status = 0 - GetLastError ();
}
} else {
status = 0 - ERROR_INVALID_DATA;
}
return status;
#else
COB_UNUSED (p_id);
cob_runtime_warning (_("'%s' is not supported on this platform"), "CBL_GC_WAITPID");
return -1;
#endif
}
/* set the number of arguments passed to the current program;
works both for main programs and called sub programs
Implemented according to ACUCOBOL-GT -> returns the number of arguments that were passed,
not like in MF implementation the number of arguments that were received */
int
cob_sys_return_args (void *data)
{
COB_UNUSED (data);
COB_CHK_PARMS (C$NARG, 1);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
cob_set_int (COB_MODULE_PTR->cob_procedure_params[0],
COB_MODULE_PTR->module_num_params);
}
return 0;
}
int
cob_sys_calledby (void *data)
{
size_t size;
size_t msize;
COB_CHK_PARMS (C$CALLEDBY, 1);
if (!COB_MODULE_PTR->cob_procedure_params[0]) {
/* TO-DO: check what ACU ccbl/runcbl returns,
the documentation doesn't say anything about this */
return -1;
}
size = COB_MODULE_PTR->cob_procedure_params[0]->size;
memset (data, ' ', size);
if (!COB_MODULE_PTR->next) {
return 0;
}
msize = strlen (COB_MODULE_PTR->next->module_name);
if (msize > size) {
msize = size;
}
memcpy (data, COB_MODULE_PTR->next->module_name, msize);
return 1;
}
int
cob_sys_parameter_size (void *data)
{
int n;
COB_UNUSED (data);
COB_CHK_PARMS (C$PARAMSIZE, 1);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
n = cob_get_int (COB_MODULE_PTR->cob_procedure_params[0]);
if (n > 0 && n <= COB_MODULE_PTR->module_num_params) {
n--;
if (COB_MODULE_PTR->next
&& COB_MODULE_PTR->next->cob_procedure_params[n]) {
return (int)COB_MODULE_PTR->next->cob_procedure_params[n]->size;
}
}
}
return 0;
}
int
cob_sys_getopt_long_long (void *so, void *lo, void *idx, const int long_only, void *return_char, void *opt_val)
{
/*
* cob_argc is a static int containing argc from runtime
* cob_argv is a static char ** containing argv from runtime
*/
size_t opt_val_size = 0;
size_t so_size = 0;
size_t lo_size = 0;
unsigned int lo_amount;
int exit_status;
char *shortoptions;
char *temp;
struct option *longoptions, *longoptions_root;
longoption_def *l = NULL;
int longind = 0;
unsigned int i;
int j;
int return_value;
COB_UNUSED (idx);
COB_UNUSED (lo);
COB_UNUSED (so);
COB_CHK_PARMS (CBL_GC_GETOPT, 6);
/* Read in sizes of some parameters */
if (COB_MODULE_PTR->cob_procedure_params[1]) {
lo_size = COB_MODULE_PTR->cob_procedure_params[1]->size;
}
if (COB_MODULE_PTR->cob_procedure_params[0]) {
so_size = COB_MODULE_PTR->cob_procedure_params[0]->size;
}
if (COB_MODULE_PTR->cob_procedure_params[5]) {
opt_val_size = COB_MODULE_PTR->cob_procedure_params[5]->size;
}
/* buffering longoptions (COBOL), target format (struct option) */
if (lo_size % sizeof (longoption_def) == 0) {
lo_amount = (int)lo_size / sizeof (longoption_def);
longoptions_root = (struct option*) cob_malloc (sizeof (struct option) * ((size_t)lo_amount + 1U));
} else {
cob_runtime_error (_("call to CBL_GC_GETOPT with wrong longoption size"));
cob_hard_failure ();
}
if (!COB_MODULE_PTR->cob_procedure_params[2]) {
cob_runtime_error (_("call to CBL_GC_GETOPT with missing longind"));
cob_hard_failure ();
}
longind = cob_get_int (COB_MODULE_PTR->cob_procedure_params[2]);
/* add 0-termination to strings */
shortoptions = cob_malloc (so_size + 1U);
if (COB_MODULE_PTR->cob_procedure_params[0]) {
cob_field_to_string (COB_MODULE_PTR->cob_procedure_params[0],
shortoptions, so_size, CCM_NONE);
}
if (COB_MODULE_PTR->cob_procedure_params[1]) {
l = (struct __longoption_def*) (COB_MODULE_PTR->cob_procedure_params[1]->data);
}
longoptions = longoptions_root;
for (i = 0; i < lo_amount; i++) {
j = sizeof (l->name) - 1;
while (j >= 0 && l->name[j] == ' ') {
l->name[j] = 0;
j--;
}
longoptions->name = l->name;
longoptions->has_arg = COB_D2I (l->has_option);
memcpy (&longoptions->flag, l->return_value_pointer, sizeof (l->return_value_pointer));
memcpy (&longoptions->val, &l->return_value, 4);
l = l + 1; /* +1 means pointer + 1*sizeof (longoption_def) */
longoptions = longoptions + 1;
}
/* appending final record, so getopt can spot the end of longoptions */
longoptions->name = NULL;
longoptions->has_arg = 0;
longoptions->flag = NULL;
longoptions->val = 0;
l -= lo_amount; /* Set pointer back to begin of longoptions */
longoptions -= lo_amount;
return_value = cob_getopt_long_long (cob_argc, cob_argv, shortoptions, longoptions, &longind, long_only);
temp = (char *) &return_value;
/* Write data back to COBOL */
#ifdef WORDS_BIGENDIAN
if (temp[3] == '?'
|| temp[3] == ':'
|| temp[3] == 'W'
|| temp[3] == 0) {
exit_status = temp[3] & 0xFF;
} else if (return_value == -1) {
exit_status = -1;
} else {
exit_status = 3;
}
/* cob_getopt_long_long sometimes returns and 'int' value and sometimes a 'x ' in the int */
if (temp[0] == 0
&& temp[1] == 0
&& temp[2] == 0) {
/* Move option value to 1st byte and SPACE fill the 'int' */
temp[0] = temp[3];
temp[1] = temp[2] = temp[3] = ' ';
}
#else
if (temp[0] == '?'
|| temp[0] == ':'
|| temp[0] == 'W'
|| temp[0] == -1
|| temp[0] == 0) {
exit_status = return_value;
} else {
exit_status = 3;
}
for (i = 3; i > 0; i--) {
if (temp[i] == 0) temp[i] = ' ';
else break;
}
#endif
cob_set_int (COB_MODULE_PTR->cob_procedure_params[2], longind);
memcpy (return_char, &return_value, 4);
if (cob_optarg != NULL) {
size_t optlen;
memset (opt_val, 0, opt_val_size);
optlen = strlen (cob_optarg);
if (optlen > opt_val_size) {
/* Return code 2 for "Option value too long => cut" */
optlen = opt_val_size;
exit_status = 2;
}
memcpy (opt_val, cob_optarg, optlen);
}
cob_free (shortoptions);
cob_free (longoptions_root);
return exit_status;
}
int
cob_sys_printable (void *p1, ...)
{
size_t datalen, n;
unsigned char *dotptr;
unsigned char dotrep;
va_list args;
cob_u8_ptr data;
char *previous_locale = NULL;
COB_CHK_PARMS (CBL_GC_PRINTABLE, 1);
if (!COB_MODULE_PTR->cob_procedure_params[0]) {
return 0;
}
datalen = COB_MODULE_PTR->cob_procedure_params[0]->size;
if (datalen == 0) {
return 0;
}
if (cobglobptr->cob_call_params > 1) {
va_start (args, p1);
dotptr = va_arg (args, unsigned char *);
va_end (args);
dotrep = *dotptr;
} else {
dotrep = (unsigned char)'.';
}
#ifdef HAVE_SETLOCALE
if (cobglobptr->cob_locale_ctype) {
previous_locale = setlocale (LC_CTYPE, NULL);
setlocale (LC_CTYPE, cobglobptr->cob_locale_ctype);
}
#endif
data = p1;
for (n = 0; n < datalen; ++n) {
if (!isprint (data[n])) {
data[n] = dotrep;
}
}
#ifdef HAVE_SETLOCALE
if (previous_locale) {
setlocale (LC_CTYPE, previous_locale);
}
#endif
return 0;
}
int
cob_sys_justify (void *p1, ...)
{
cob_u8_ptr data;
size_t datalen;
size_t left;
size_t right;
size_t movelen;
size_t centrelen;
size_t n;
size_t shifting;
COB_CHK_PARMS (C$JUSTIFY, 1);
if (!COB_MODULE_PTR->cob_procedure_params[0]) {
return 0;
}
data = p1;
datalen = COB_MODULE_PTR->cob_procedure_params[0]->size;
if (datalen < 2) {
return 0;
}
if (data[0] != ' ' && data[datalen - 1] != ' ') {
return 0;
}
for (left = 0; left < datalen; ++left) {
if (data[left] != ' ') {
break;
}
}
if (left == datalen) {
return 0;
}
right = 0;
for (n = datalen - 1; ; --n, ++right) {
if (data[n] != ' ') {
break;
}
if (n == 0) {
break;
}
}
movelen = datalen - left - right;
shifting = 0;
if (cobglobptr->cob_call_params > 1) {
unsigned char *direction;
va_list args;
va_start (args, p1);
direction = va_arg (args, unsigned char *);
va_end (args);
if (*direction == 'L') {
shifting = 1;
} else if (*direction == 'C') {
shifting = 2;
}
}
switch (shifting) {
case 1:
memmove (data, &data[left], movelen);
memset (&data[movelen], ' ', datalen - movelen);
break;
case 2:
centrelen = (left + right) / 2;
memmove (&data[centrelen], &data[left], movelen);
memset (data, ' ', centrelen);
if ((left + right) % 2) {
memset (&data[centrelen + movelen], ' ', centrelen + 1);
} else {
memset (&data[centrelen + movelen], ' ', centrelen);
}
break;
default:
memmove (&data[left + right], &data[left], movelen);
memset (data, ' ', datalen - movelen);
break;
}
return 0;
}
void
cob_set_locale (cob_field *locale, const int category)
{
#ifdef HAVE_SETLOCALE
char buff[COB_MINI_BUFF];
char *p;
if (locale) {
int flen = cob_field_to_string (locale, buff,
COB_MINI_MAX, CCM_NONE);
if (flen < 1) {
return;
}
p = buff;
} else {
p = NULL;
}
switch (category) {
case COB_LC_COLLATE:
p = setlocale (LC_COLLATE, p);
break;
case COB_LC_CTYPE:
p = setlocale (LC_CTYPE, p);
break;
#ifdef LC_MESSAGES
case COB_LC_MESSAGES:
p = setlocale (LC_MESSAGES, p);
break;
#endif
case COB_LC_MONETARY:
p = setlocale (LC_MONETARY, p);
break;
case COB_LC_NUMERIC:
p = setlocale (LC_NUMERIC, p);
break;
case COB_LC_TIME:
p = setlocale (LC_TIME, p);
break;
case COB_LC_ALL:
p = setlocale (LC_ALL, p);
break;
case COB_LC_USER:
if (cobglobptr->cob_locale_orig) {
p = setlocale (LC_ALL, cobglobptr->cob_locale_orig);
(void)setlocale (LC_NUMERIC, "C");
}
break;
case COB_LC_CLASS:
if (cobglobptr->cob_locale_ctype) {
p = setlocale (LC_CTYPE, cobglobptr->cob_locale_ctype);
}
break;
}
if (!p) {
cob_set_exception (COB_EC_LOCALE_MISSING);
return;
}
p = setlocale (LC_ALL, NULL);
if (p) {
if (cobglobptr->cob_locale) {
cob_free (cobglobptr->cob_locale);
}
cobglobptr->cob_locale = cob_strdup (p);
}
#else
cob_set_exception (COB_EC_LOCALE_MISSING);
#endif
}
#if 0 /* currently not used */
char *
cob_int_to_string (int i, char *number)
{
if (!number) return NULL;
sprintf (number, "%i", i);
return number;
}
char *
cob_int_to_formatted_bytestring (int i, char *number)
{
double d;
char *byte_unit;
if (!number) return NULL;
byte_unit = (char *) cob_fast_malloc (3);
if (i > (1024 * 1024)) {
d = i / 1024.0 / 1024.0;
byte_unit = (char *) "MB";
} else if (i > 1024) {
d = i / 1024.0;
byte_unit = (char *) "kB";
} else {
d = 0;
byte_unit = (char *) "B";
}
sprintf (number, "%3.2f %s", d, byte_unit);
return number;
}
#endif
/* concatenate two strings allocating a new one
and optionally free one of the strings
set str_to_free if the result is assigned to
one of the two original strings
*/
char *
cob_strcat (char *str1, char *str2, int str_to_free)
{
size_t l;
char *temp1, *temp2;
l = strlen (str1) + strlen (str2) + 1;
/*
* If one of the parameter is the buffer itself,
* we copy the buffer before continuing.
*/
if (str1 == strbuff) {
temp1 = cob_strdup (str1);
} else {
temp1 = str1;
}
if (str2 == strbuff) {
temp2 = cob_strdup (str2);
} else {
temp2 = str2;
}
if (strbuff) {
cob_free (strbuff);
}
strbuff = (char *) cob_fast_malloc (l);
sprintf (strbuff, "%s%s", temp1, temp2);
switch (str_to_free) {
case 1: cob_free (temp1);
break;
case 2: cob_free (temp2);
break;
default: break;
}
return strbuff;
}
char *
cob_strjoin (char **strarray, int size, char *separator)
{
char *result;
int i;
if (!strarray || size <= 0 || !separator) return NULL;
result = cob_strdup (strarray[0]);
for (i = 1; i < size; i++) {
result = cob_strcat (result, separator, 1);
result = cob_strcat (result, strarray[i], 1);
}
return result;
}
static void
var_print (const char *msg, const char *val, const char *default_val,
const unsigned int format)
{
#if 0 /* currently only format 0/1 used */
switch (format) {
case 0:
printf ("%-*.*s : ", CB_IMSG_SIZE, CB_IMSG_SIZE, msg);
break;
case 1: {
int lablen;
printf (" %s: ", _("env"));
lablen = CB_IMSG_SIZE - 2 - (int)strlen (_("env")) - 2;
printf ("%-*.*s : ", lablen, lablen, msg);
break;
}
case 2:
printf (" %-*.*s : ", CB_IMSG_SIZE, CB_IMSG_SIZE, msg);
break;
case 3:
printf (" %-*.*s : ", CB_IMSG_SIZE, CB_IMSG_SIZE, msg);
break;
default:
printf ("%-*.*s : ", CB_IMSG_SIZE, CB_IMSG_SIZE, msg);
break;
}
if (!val && (!default_val || default_val[0] == 0)) {
putchar ('\n');
return;
} else if (format != 0 && val && default_val &&
((format != 2 && val[0] == '0') || strcmp (val, default_val) == 0)) {
char dflt[40];
snprintf (dflt, 39, " %s", _("(default)"));
val = cob_strcat ((char *) default_val, dflt, 0);
} else if (!val && default_val) {
val = default_val;
}
#else
if (format == 0) {
printf ("%-*.*s : ", CB_IMSG_SIZE, CB_IMSG_SIZE, msg);
} else {
int lablen;
printf (" %s: ", _("env"));
lablen = CB_IMSG_SIZE - 2 - (int)strlen (_("env")) - 2;
printf ("%-*.*s : ", lablen, lablen, msg);
}
if (!val && (!default_val || default_val[0] == 0)) {
putchar ('\n');
return;
} else if (format == 1 && val && default_val &&
(val[0] == '0' || strcmp (val, default_val) == 0)) {
char dflt[40];
snprintf (dflt, 39, " %s", _("(default)"));
val = cob_strcat ((char *) default_val, dflt, 0);
} else if (!val && default_val) {
val = default_val;
}
#endif
if (!val && (!default_val || default_val[0] == 0)) {
putchar ('\n');
return;
} else if (format != 0 && val && default_val &&
((format != 2 && val[0] == '0') || strcmp (val, default_val) == 0)) {
char dflt[40];
snprintf (dflt, 39, " %s", _("(default)"));
val = cob_strcat ((char *) default_val, dflt, 0);
} else if (!val && default_val) {
val = default_val;
}
if (val && strlen (val) <= CB_IVAL_SIZE) {
printf ("%s", val);
putchar ('\n');
return;
}
{
char *p;
char *token;
size_t n;
p = cob_strdup (val);
n = 0;
token = strtok (p, " ");
for (; token; token = strtok (NULL, " ")) {
int toklen = (int)strlen (token) + 1;
if ((n + toklen) > CB_IVAL_SIZE) {
if (n) {
if (format == 2 || format == 3)
printf ("\n %*.*s", CB_IMSG_SIZE + 3,
CB_IMSG_SIZE + 3, " ");
else
printf ("\n%*.*s", CB_IMSG_SIZE + 3, CB_IMSG_SIZE + 3, " ");
}
n = 0;
}
printf ("%s%s", (n ? " " : ""), token);
n += toklen;
}
putchar ('\n');
cob_free (p);
}
}
/*
Expand a string with environment variable in it.
Return malloced string.
*/
char *
cob_expand_env_string (char *strval)
{
unsigned int i;
unsigned int j = 0;
unsigned int k = 0;
size_t envlen = 1280;
char *env;
char *str;
char ename[128] = { '\0' };
char *penv;
env = cob_malloc (envlen);
for (k = 0; strval[k] != 0; k++) {
/* String almost full?; Expand it */
if (j >= envlen - 128) {
env = cob_realloc (env, envlen, envlen + 256);
envlen += 256;
}
/* ${envname:default} */
if (strval[k] == '$' && strval[k + 1] == '{') {
k += 2;
for (i = 0; strval[k] != '}'
&& strval[k] != 0
&& strval[k] != ':'; k++) {
ename[i++] = strval[k];
}
ename[i++] = 0;
penv = getenv (ename);
if (penv == NULL) {
/* Copy 'default' value */
if (strval[k] == ':') {
k++;
/* ${name:-default} */
if (strval[k] == '-') {
k++;
}
while (strval[k] != '}' && strval[k] != 0) {
if (j >= envlen - 50) {
env = cob_realloc (env, envlen, envlen + 128);
envlen += 128;
}
env[j++] = strval[k++];
}
} else if (strcmp (ename, "COB_CONFIG_DIR") == 0) {
penv = (char *)COB_CONFIG_DIR;
} else if (strcmp (ename, "COB_COPY_DIR") == 0) {
penv = (char *)COB_COPY_DIR;
}
}
if (penv != NULL) {
if ((strlen (penv) + j) > (envlen - 128)) {
env = cob_realloc (env, envlen, strlen (penv) + 256);
envlen = strlen (penv) + 256;
}
j += sprintf (&env[j], "%s", penv);
penv = NULL;
}
while (strval[k] != '}' && strval[k] != 0) {
k++;
}
if (strval[k] == '}') {
k++;
}
k--;
} else if (strval[k] == '$'
&& strval[k+1] == '$') { /* Replace $$ with process-id */
j += sprintf (&env[j], "%d", cob_sys_getpid());
k++;
/* CHECME: possibly add $f /$b as basename of executable [or, when passed to cob_init the first name]
along with $d date as yyyymmdd and $t as hhmmss */
} else if (!isspace ((unsigned char)strval[k])) {
env[j++] = strval[k];
} else {
env[j++] = ' ';
}
}
env[j] = '\0';
str = cob_strdup (env);
cob_free (env);
return str;
}
/* Store 'integer' value in field for correct length (computed with sizeof (fieldtype)) */
static void
set_value (char *data, int len, cob_s64_t val)
{
/* keep in order of occurrence in data types, last nanoseconds for startup... */
if (len == sizeof (int)) {
*(int *)data = (int)val;
} else if (len == sizeof (short)) {
*(short *)data = (short)val;
} else if (len == sizeof (cob_s64_t)) {
*(cob_s64_t *)data = val;
} else {
*data = (char)val;
}
}
/* Get 'integer' value from field */
static cob_s64_t
get_value (char *data, int len)
{
if (len == sizeof (int)) {
return *(int *)data;
} else if (len == sizeof (short)) {
return *(short *)data;
} else if (len == sizeof (cob_s64_t)) {
return *(cob_s64_t *)data;
} else {
return *data;
}
}
static int
translate_boolean_to_int (const char* ptr)
{
if (ptr == NULL || *ptr == 0) {
return 2;
}
if (*(ptr + 1) == 0
&& (*ptr == '0' || *ptr == '1')) {
return COB_D2I (*ptr); /* 0 or 1 */
} else
/* pre-translated boolean "never" - not set" */
if (strcmp (ptr, "!") == 0) {
return -1;
} else
if (strcasecmp (ptr, "true") == 0
|| strcasecmp (ptr, "t") == 0
|| strcasecmp (ptr, "on") == 0
|| strcasecmp (ptr, "yes") == 0
|| strcasecmp (ptr, "y") == 0) {
return 1; /* True value */
} else
if (strcasecmp (ptr, "false") == 0
|| strcasecmp (ptr, "f") == 0
|| strcasecmp (ptr, "off") == 0
|| strcasecmp (ptr, "no") == 0
|| strcasecmp (ptr, "n") == 0) {
return 0; /* False value */
}
return 2;
}
/* Set runtime setting with given value */
static int /* returns 1 if any error, else 0 */
set_config_val (char *value, int pos)
{
register char *ptr = value;
char *str;
cob_s64_t numval = 0;
int i, slen;
const int data_type = gc_conf[pos].data_type;
const size_t data_loc = gc_conf[pos].data_loc;
const int data_len = gc_conf[pos].data_len;
char *data = ((char *)cobsetptr) + data_loc;
if (gc_conf[pos].enums) { /* Translate 'word' into alternate 'value' */
for (i = 0; gc_conf[pos].enums[i].match != NULL; i++) {
if (strcasecmp (value, gc_conf[pos].enums[i].match) == 0) {
ptr = value = (char *)gc_conf[pos].enums[i].value;
break;
}
if ((data_type & ENV_ENUMVAL) && strcasecmp (value, gc_conf[pos].enums[i].value) == 0) {
break;
}
}
if ((data_type & ENV_ENUM || data_type & ENV_ENUMVAL) /* Must be one of the 'enum' values */
&& gc_conf[pos].enums[i].match == NULL
&& (!(data_type & ENV_BOOL))) {
conf_runtime_error_value (ptr, pos);
fprintf (stderr, _("should be one of the following values: %s"), "");
for (i = 0; gc_conf[pos].enums[i].match != NULL; i++) {
if (i != 0) {
putc (',', stderr);
putc (' ', stderr);
}
fprintf (stderr, "%s", (char *)gc_conf[pos].enums[i].match);
if (data_type & ENV_ENUMVAL) {
fprintf (stderr, "(%s)", (char *)gc_conf[pos].enums[i].value);
}
}
putc ('\n', stderr);
fflush (stderr);
return 1;
}
}
if ((data_type & ENV_BOOL)) { /* Boolean: Yes/No, True/False,... */
numval = translate_boolean_to_int (ptr);
if (numval != -1
&& numval != 1
&& numval != 0) {
conf_runtime_error_value (ptr, pos);
conf_runtime_error (1, _("should be one of the following values: %s"), "true, false");
return 1;
}
if ((data_type & ENV_NOT)) { /* Negate logic for actual setting */
numval = !numval;
}
set_value (data, data_len, numval);
/* call internal routines that do post-processing */
if (data == (char *)&cobsetptr->cob_debugging_mode) {
/* Copy variables from settings (internal) to global structure, each time */
cobglobptr->cob_debugging_mode = cobsetptr->cob_debugging_mode;
} else if (data == (char *)&cobsetptr->cob_insert_mode) {
cob_settings_screenio ();
} else if (data == (char *)&cobsetptr->cob_debugging_mode) {
cob_switch[11 + 'D' - 'A'] = (int)numval;
} else if (data == (char *)&cobsetptr->cob_ls_nulls) {
cob_switch[11 + 'N' - 'A'] = (int)numval;
#if 0 /* TODO add in trunk */
} else if (data == (char *)&cobsetptr->cob_ls_tabs) {
cob_switch[11 + 'T' - 'A'] = (int)numval;
#endif
}
} else if ((data_type & ENV_UINT) /* Integer data, unsigned */
|| (data_type & ENV_SINT) /* Integer data, signed */
|| (data_type & ENV_SIZE) ) { /* Size: integer with K, M, G */
char sign = 0;
for (; *ptr == ' '; ptr++); /* skip leading space */
if (*ptr == '-'
|| *ptr == '+') {
if ((data_type & ENV_SINT) == 0) {
conf_runtime_error_value (ptr, pos);
/* CHECKME: likely cob_runtime_warning would be more reasonable */
conf_runtime_error (1, _("should be unsigned"));
return 1;
}
sign = *ptr;
ptr++;
}
if (IS_INVALID_DIGIT_DATA (*ptr)) {
conf_runtime_error_value (ptr, pos);
conf_runtime_error (1, _("should be numeric"));
return 1;
}
while (IS_VALID_DIGIT_DATA (*ptr)) {
numval = (numval * 10) + COB_D2I (*ptr++);
}
if (sign != 0
&& ( *ptr == '-'
|| *ptr == '+')) {
if ((data_type & ENV_SINT) == 0) {
conf_runtime_error_value (ptr, pos);
conf_runtime_error (1, _("should be unsigned"));
return 1;
}
sign = *ptr;
ptr++;
}
if ((data_type & ENV_SIZE) /* Size: any K, M, G */
&& *ptr != 0) {
switch (toupper ((unsigned char)*ptr)) {
case 'K':
numval = numval * 1024;
ptr++;
break;
case 'M':
if (numval < 4001) {
numval = numval * 1024 * 1024;
} else {
/* use max. guaranteed value for unsigned long
to raise a warning as max value is limit to one less */
numval = 4294967295UL;
}
ptr++;
break;
case 'G':
if (numval < 4) {
numval = numval * 1024 * 1024 * 1024;
} else {
/* use max. guaranteed value for unsigned long
to raise a warning as max value is limit to one less */
numval = 4294967295UL;
}
ptr++;
break;
}
}
for (; *ptr == ' '; ptr++); /* skip trailing space */
if (*ptr != 0) {
conf_runtime_error_value (ptr, pos);
conf_runtime_error (1, _("should be numeric"));
return 1;
}
if (sign == '-') {
numval = -numval;
}
if (gc_conf[pos].min_value > 0
&& numval < gc_conf[pos].min_value) {
conf_runtime_error_value (value, pos);
conf_runtime_error (1, _("minimum value: %lu"), gc_conf[pos].min_value);
return 1;
}
if (gc_conf[pos].max_value > 0
&& numval > gc_conf[pos].max_value) {
conf_runtime_error_value (value, pos);
conf_runtime_error (1, _("maximum value: %lu"), gc_conf[pos].max_value);
return 1;
}
set_value (data, data_len, numval);
/* call internal routines that do post-processing */
if (data == (char *)&cobsetptr->cob_mouse_flags
#ifdef HAVE_MOUSEINTERVAL /* possibly add an internal option for mouse support, too */
|| data == (char *)&cobsetptr->cob_mouse_interval
#endif
) {
cob_settings_screenio ();
}
} else if ((data_type & ENV_FILE)
|| (data_type & ENV_PATH)) { /* Path (environment expanded) to be stored as a string */
memcpy (&str, data, sizeof (char *));
if (str != NULL) {
cob_free ((void *)str);
}
str = cob_expand_env_string (value);
if ((data_type & ENV_FILE)
&& strchr (str, PATHSEP_CHAR) != NULL) {
conf_runtime_error_value (value, pos);
conf_runtime_error (1, _("should not contain '%c'"), PATHSEP_CHAR);
cob_free (str);
return 1;
}
memcpy (data, &str, sizeof (char *));
if (data_loc == offsetof (cob_settings, cob_preload_str)) {
cobsetptr->cob_preload_str_set = cob_strdup(str);
}
/* call internal routines that do post-processing */
if (data == (char *)cobsetptr->cob_trace_filename
&& cobsetptr->cob_trace_file != NULL) {
cob_new_trace_file ();
}
} else if (data_type & ENV_STR) { /* String (environment expanded) */
memcpy (&str, data, sizeof (char *));
if (str != NULL) {
cob_free ((void *)str);
}
str = cob_expand_env_string (value);
memcpy (data, &str, sizeof (char *));
if (data_loc == offsetof (cob_settings, cob_preload_str)) {
cobsetptr->cob_preload_str_set = cob_strdup(str);
}
/* call internal routines that do post-processing */
if (data == (void *)cobsetptr->cob_date) {
check_current_date ();
}
} else if ((data_type & ENV_CHAR)) { /* 'char' field inline */
memset (data, 0, data_len);
slen = (int)strlen (value);
if (slen > data_len) {
slen = data_len;
}
memcpy (data, value, slen);
}
return 0;
}
/* Set runtime setting by name with given value */
static int /* returns 1 if any error, else 0 */
set_config_val_by_name (char *value, const char *name, const char *func)
{
int i;
int ret = 1;
for (i = 0; i < NUM_CONFIG; i++) {
if (!strcmp (gc_conf[i].conf_name, name)) {
ret = set_config_val (value, i);
if (func) {
gc_conf[i].data_type |= STS_FNCSET;
gc_conf[i].set_by = FUNC_NAME_IN_DEFAULT;
gc_conf[i].default_val = func;
}
break;
}
}
return ret;
}
/* Return setting value as a 'string' */
static char *
get_config_val (char *value, int pos, char *orgvalue)
{
char *data;
char *str;
double dval;
cob_s64_t numval;
int i, data_type, data_len;
size_t data_loc;
data_type = gc_conf[pos].data_type;
data_loc = gc_conf[pos].data_loc;
data_len = gc_conf[pos].data_len;
data = ((char *)cobsetptr) + data_loc;
if (min_conf_length == 0) {
not_set = _("not set");
min_conf_length = (char) strlen (not_set) + 1;
if (min_conf_length < 6) {
min_conf_length = 6;
} else if (min_conf_length > 15) {
min_conf_length = 15;
}
}
strcpy (value, _("unknown"));
orgvalue[0] = 0;
if ((data_type & ENV_BOOL)) { /* Boolean: Yes/No, True/False,... */
numval = get_value (data, data_len);
if (numval == -1) {
#if 0 /* boolean "not set" - used for file specific settings (4.x feature) */
if (gc_conf[pos].enums == never) {
strcpy (value, "never");
} else {
strcpy (value, _("not set"));
}
#else
strcpy (value, "never");
#endif
} else {
if (data_type & ENV_NOT) {
numval = !numval;
}
if (numval) {
strcpy (value, _("yes"));
} else {
strcpy (value, _("no"));
}
}
} else if (data_type & ENV_UINT) { /* Integer data, unsigned */
numval = get_value (data, data_len);
sprintf (value, CB_FMT_LLU, numval);
} else if (data_type & ENV_SINT) { /* Integer data, signed */
numval = get_value (data, data_len);
sprintf (value, CB_FMT_LLD, numval);
} else if ((data_type & ENV_SIZE)) { /* Size: integer with K, M, G */
numval = get_value (data, data_len);
dval = (double) numval;
if (numval > (1024 * 1024 * 1024)) {
if ((numval % (1024 * 1024 * 1024)) == 0) {
sprintf (value, CB_FMT_LLD" GB", numval / (1024 * 1024 * 1024));
} else {
sprintf (value, "%.2f GB", dval / (1024.0 * 1024.0 * 1024.0));
}
} else if (numval > (1024 * 1024)) {
if ((numval % (1024 * 1024)) == 0) {
sprintf (value, CB_FMT_LLD" MB", numval / (1024 * 1024));
} else {
sprintf (value, "%.2f MB", dval / (1024.0 * 1024.0));
}
} else if (numval > 1024) {
if ((numval % 1024) == 0) {
sprintf (value, CB_FMT_LLD" KB", numval / 1024);
} else {
sprintf (value, "%.2f KB", dval / 1024.0);
}
} else {
sprintf (value, CB_FMT_LLD, numval);
}
/* TO-DO: Consolidate copy-and-pasted code! */
} else if (data_type & ENV_STR) { /* String stored as a string */
memcpy (&str, data, sizeof (char *));
if (data_loc == offsetof (cob_settings, cob_display_print_filename)
&& cobsetptr->cob_display_print_file) {
snprintf (value, COB_MEDIUM_MAX, _("set by %s"), "cob_set_runtime_option");
} else if (data_loc == offsetof (cob_settings, cob_display_punch_filename)
&& cobsetptr->cob_display_punch_file) {
snprintf (value, COB_MEDIUM_MAX, _("set by %s"), "cob_set_runtime_option");
} else if(data_loc == offsetof (cob_settings, cob_trace_filename)
&& cobsetptr->external_trace_file) {
snprintf (value, COB_MEDIUM_MAX, _("set by %s"), "cob_set_runtime_option");
} else if (str == NULL) {
snprintf (value, COB_MEDIUM_MAX, _("not set"));
} else {
snprintf (value, COB_MEDIUM_MAX, "'%s'", str);
}
} else if ((data_type & ENV_FILE)) { /* File/path stored as a string */
memcpy (&str, data, sizeof (char *));
/* TODO: add special cases here on merging rw-branch */
if (str == NULL) {
snprintf (value, COB_MEDIUM_MAX, _("not set"));
} else {
snprintf (value, COB_MEDIUM_MAX, "%s", str);
}
} else if ((data_type & ENV_PATH)) { /* Path stored as a string */
memcpy (&str, data, sizeof (char *));
if (str == NULL) {
snprintf (value, COB_MEDIUM_MAX, _("not set"));
} else {
snprintf (value, COB_MEDIUM_MAX, "%s", str);
}
} else if ((data_type & ENV_CHAR)) { /* 'char' field inline */
if (*(char *)data == 0) {
strcpy (value, "Nul");
} else if (isprint (*(unsigned char *)data)) {
sprintf (value, "'%s'", (char *)data);
} else {
sprintf (value, "0x%02X", *(char *)data);
}
}
value[COB_MEDIUM_MAX] = 0; /* fix warning */
if (gc_conf[pos].enums) { /* Translate 'word' into alternate 'value' */
for (i = 0; gc_conf[pos].enums[i].match != NULL; i++) {
if (strcasecmp (value, gc_conf[pos].enums[i].value) == 0) {
if (strcmp (value, "0") != 0
&& strcmp (value, gc_conf[pos].default_val) != 0) {
strcpy (orgvalue, value);
}
/* insert either value or translated "not set" */
if (strcmp (gc_conf[pos].enums[i].match, "not set") == 0) {
snprintf (value, COB_MEDIUM_MAX, _("not set"));
value[COB_MEDIUM_MAX] = 0; /* fix warning */
} else {
strcpy (value, gc_conf[pos].enums[i].match);
}
break;
}
}
if (gc_conf[pos].enums[i].match == NULL
&& gc_conf[pos].default_val != NULL
&& strcmp (value, gc_conf[pos].default_val) != 0) {
strcpy (orgvalue, value);
}
} else
if (!(gc_conf[pos].data_type & STS_ENVSET)
&& !(gc_conf[pos].data_type & STS_CNFSET)
&& !(gc_conf[pos].data_type & ENV_BOOL)
&& gc_conf[pos].default_val != NULL) {
strcpy (value, gc_conf[pos].default_val);
orgvalue[0] = 0;
}
if (gc_conf[pos].default_val != NULL
&& strcmp (orgvalue, gc_conf[pos].default_val) != 0) {
orgvalue[0] = 0;
} else if (strcmp (value, orgvalue) == 0) {
orgvalue[0] = 0;
}
return value;
}
static int
cb_lookup_config (char *keyword)
{
int i;
for (i = 0; i < NUM_CONFIG; i++) { /* Set value from config file */
if (gc_conf[i].conf_name
&& strcasecmp (keyword, gc_conf[i].conf_name) == 0) { /* Look for config file name */
break;
}
if (gc_conf[i].env_name
&& strcasecmp (keyword, gc_conf[i].env_name) == 0) { /* Catch using env var name */
break;
}
}
return i;
}
static int
cb_config_entry (char *buf, int line)
{
int i, j, k, old_type;
void *data;
char *str, qt;
char keyword[COB_MINI_BUFF], value[COB_SMALL_BUFF];
cob_source_line = line;
for (j = (int)strlen (buf); buf[j-1] == '\r' || buf[j-1] == '\n'; ) /* Remove CR LF */
buf[--j] = 0;
for (i = 0; isspace ((unsigned char)buf[i]); i++); /* drop leading spaces */
for (j = 0; buf[i] != 0 && buf[i] != ':' && !isspace ((unsigned char)buf[i]) && buf[i] != '=' && buf[i] != '#'; )
keyword[j++] = buf[i++];
keyword[j] = 0;
while (buf[i] != 0 && (isspace ((unsigned char)buf[i]) || buf[i] == ':' || buf[i] == '=')) i++;
if (buf[i] == '"'
|| buf[i] == '\'') {
qt = buf[i++];
for (j = 0; buf[i] != qt && buf[i] != 0; )
value[j++] = buf[i++];
} else {
for (j = 0; !isspace ((unsigned char)buf[i]) && buf[i] != '#' && buf[i] != 0; )
value[j++] = buf[i++];
}
value[j] = 0;
if (strcasecmp (keyword, "reset") != 0
&& strcasecmp (keyword, "include") != 0
&& strcasecmp (keyword, "includeif") != 0
&& strcasecmp (keyword, "setenv") != 0
&& strcasecmp (keyword, "unsetenv") != 0) {
i = cb_lookup_config (keyword);
if (i >= NUM_CONFIG) {
conf_runtime_error (1, _("unknown configuration tag '%s'"), keyword);
return -1;
}
}
if (strcmp (value, "") == 0) {
if (strcasecmp (keyword, "include") != 0
&& strcasecmp (keyword, "includeif")) {
conf_runtime_error(1, _("WARNING - '%s' without a value - ignored!"), keyword);
return 2;
} else {
conf_runtime_error (1, _("'%s' without a value!"), keyword);
return -1;
}
}
if (strcasecmp (keyword, "setenv") == 0 ) {
char value2[COB_SMALL_BUFF];
/* collect additional value and push into environment */
strcpy (value2, "");
/* check for := in value 2 and split, if necessary */
k = 0; while (value[k] != '=' && value[k] != ':' && value[k] != '"' && value[k] != '\'' && value[k] != 0) k++;
if (value[k] == '=' || value[k] == ':') {
i = i - (int)strlen (value + k);
value[k] = 0;
}
while (isspace ((unsigned char)buf[i]) || buf[i] == ':' || buf[i] == '=') i++;
if (buf[i] == '"'
|| buf[i] == '\'') {
qt = buf[i++];
for (j = 0; buf[i] != qt && buf[i] != 0; )
value2[j++] = buf[i++];
} else {
for (j = 0; !isspace ((unsigned char)buf[i]) && buf[i] != '#' && buf[i] != 0; )
value2[j++] = buf[i++];
}
value2[j] = 0;
if (strcmp (value2, "") == 0) {
conf_runtime_error (1, _("WARNING - '%s %s' without a value - ignored!"), keyword, value);
return 2;
}
/* check additional value for inline env vars ${varname:-default} */
str = cob_expand_env_string (value2);
(void)cob_setenv (value, str, 1);
cob_free (str);
for (i = 0; i < NUM_CONFIG; i++) { /* Set value from config file */
if (gc_conf[i].env_name
&& strcasecmp (value, gc_conf[i].env_name) == 0) {/* no longer cleared by runtime.cfg */
gc_conf[i].data_type &= ~STS_ENVCLR;
break;
}
}
return 0;
}
if (strcasecmp (keyword, "unsetenv") == 0) {
if ((getenv (value)) != NULL ) {
for (i = 0; i < NUM_CONFIG; i++) { /* Set value from config file */
if (gc_conf[i].env_name
&& strcasecmp (value, gc_conf[i].env_name) == 0) { /* Catch using env var name */
gc_conf[i].data_type |= STS_ENVCLR;
break;
}
}
(void)cob_unsetenv (value);
}
return 0;
}
if (strcasecmp (keyword, "include") == 0 ||
strcasecmp (keyword, "includeif") == 0) {
str = cob_expand_env_string (value);
strcpy (buf, str);
cob_free (str);
if (strcasecmp (keyword, "include") == 0) {
return 1;
} else {
return 3;
}
}
if (strcasecmp (keyword, "reset") == 0) {
i = cb_lookup_config (value);
if (i >= NUM_CONFIG) {
conf_runtime_error (1, _("unknown configuration tag '%s'"), value);
return -1;
}
gc_conf[i].data_type &= ~(STS_ENVSET | STS_CNFSET | STS_ENVCLR); /* Clear status */
gc_conf[i].data_type |= STS_RESET;
gc_conf[i].set_by = 0;
gc_conf[i].config_num = cobsetptr->cob_config_cur - 1;
if (gc_conf[i].default_val) {
set_config_val ((char *)gc_conf[i].default_val, i);
} else if ((gc_conf[i].data_type & ENV_STR)
|| (gc_conf[i].data_type & ENV_FILE)
|| (gc_conf[i].data_type & ENV_PATH)) { /* String/Path stored as a string */
data = (void *) ((char *)cobsetptr + gc_conf[i].data_loc);
memcpy (&str, data, sizeof (char *));
if (str != NULL) {
cob_free ((void *)str);
}
str = NULL;
memcpy (data, &str, sizeof (char *)); /* Reset pointer to NULL */
} else {
set_config_val ((char *)"0", i);
}
return 0;
}
i = cb_lookup_config (keyword);
if (i >= NUM_CONFIG) {
conf_runtime_error (1, _("unknown configuration tag '%s'"), keyword);
return -1;
}
old_type = gc_conf[i].data_type;
gc_conf[i].data_type |= STS_CNFSET;
if (!set_config_val (value, i)) {
gc_conf[i].data_type &= ~STS_RESET;
gc_conf[i].config_num = cobsetptr->cob_config_cur - 1;
if (gc_conf[i].env_group == GRP_HIDE) {
for (j = 0; j < NUM_CONFIG; j++) { /* Any alias present? */
if (j != i
&& gc_conf[i].data_loc == gc_conf[j].data_loc) {
gc_conf[j].data_type |= STS_CNFSET;
gc_conf[j].data_type &= ~STS_RESET;
gc_conf[j].config_num = gc_conf[i].config_num;
gc_conf[j].set_by = i;
}
}
}
} else {
gc_conf[i].data_type = old_type;
}
return 0;
}
static int
cob_load_config_file (const char *config_file, int isoptional)
{
char buff[COB_FILE_BUFF-10], filename[COB_FILE_BUFF];
char *penv;
int sub_ret, ret;
unsigned int i;
int line;
FILE *conf_fd;
for (i = 0; config_file[i] != 0 && config_file[i] != SLASH_CHAR; i++);
if (config_file[i] == 0) { /* Just a name, No directory */
if (access (config_file, F_OK) != 0) { /* and file does not exist */
/* check for path of previous configuration file (for includes) */
filename[0] = 0;
if (cobsetptr->cob_config_cur != 0) {
size_t size;
strncpy (buff,
cobsetptr->cob_config_file[cobsetptr->cob_config_cur - 1],
(size_t)COB_FILE_MAX-10);
size = strlen (buff);
if (size != 0 && buff[size] == SLASH_CHAR) buff[--size] = 0;
if (size != 0) {
snprintf (filename, (size_t)COB_FILE_MAX, "%s%c%s", buff, SLASH_CHAR,
config_file);
if (access (filename, F_OK) == 0) { /* and prefixed file exist */
config_file = filename; /* Prefix last directory */
} else {
filename[0] = 0;
}
}
}
if (filename[0] == 0) {
/* check for COB_CONFIG_DIR (use default if not in environment) */
penv = getenv ("COB_CONFIG_DIR");
if (penv != NULL) {
snprintf (filename, (size_t)COB_FILE_MAX, "%s%c%s",
penv, SLASH_CHAR, config_file);
} else {
snprintf (filename, (size_t)COB_FILE_MAX, "%s%c%s",
COB_CONFIG_DIR, SLASH_CHAR, config_file);
}
if (access (filename, F_OK) == 0) { /* and prefixed file exist */
config_file = filename; /* Prefix COB_CONFIG_DIR */
}
}
}
}
cob_source_file = config_file;
/* check for recursion */
for (i = 0; i < cobsetptr->cob_config_num; i++) {
if (strcmp (cobsetptr->cob_config_file[i], config_file) == 0) {
cob_source_line = 0;
conf_runtime_error (1, _("recursive inclusion"));
return -2;
}
}
/* Open the configuration file */
conf_fd = fopen (config_file, "r");
if (conf_fd == NULL && !isoptional) {
cob_source_line = 0;
conf_runtime_error (1, cob_get_strerror ());
if (cobsetptr->cob_config_file) {
cob_source_file = cobsetptr->cob_config_file[cobsetptr->cob_config_num-1];
}
return -1;
}
if (conf_fd != NULL) {
if (cobsetptr->cob_config_file == NULL) {
cobsetptr->cob_config_file = cob_malloc (sizeof (char *));
} else {
const size_t old_size = sizeof (char *) * cobsetptr->cob_config_num;
const size_t new_size = sizeof (char *) * (cobsetptr->cob_config_num + 1);
cobsetptr->cob_config_file = cob_realloc (cobsetptr->cob_config_file, old_size, new_size);
}
cobsetptr->cob_config_file[cobsetptr->cob_config_num++] = cob_strdup (config_file); /* Save config file name */
cobsetptr->cob_config_cur = cobsetptr->cob_config_num;
}
/* Read the configuration file */
ret = 0;
line = 0;
while ((conf_fd != NULL)
&& (fgets (buff, COB_SMALL_BUFF, conf_fd) != NULL) ) {
line++;
for (i = 0; isspace ((unsigned char)buff[i]); i++);
if (buff[i] == 0
|| buff[i] == '#'
|| buff[i] == '\r'
|| buff[i] == '\n')
continue; /* Skip comments and blank lines */
/* Evaluate config line */
sub_ret = cb_config_entry (buff, line);
/* Include another configuration file */
if (sub_ret == 1 || sub_ret == 3) {
cob_source_line = line;
sub_ret = cob_load_config_file (buff, sub_ret == 3);
cob_source_file = config_file;
if (sub_ret < 0) {
ret = -1;
cob_source_line = line;
conf_runtime_error (1, _("configuration file was included here"));
break;
}
}
if (sub_ret < ret) ret = sub_ret;
}
if (conf_fd) {
fclose (conf_fd);
cobsetptr->cob_config_cur--;
}
cob_source_file = NULL;
conf_fd = NULL;
return ret;
}
/*
* Load the GnuCOBOL runtime configuration information
*/
int
cob_load_config (void)
{
char *env;
char conf_file[COB_MEDIUM_BUFF];
int is_optional = 1, sts, i, j;
/* Get the name for the configuration file */
if ((env = getenv ("COB_RUNTIME_CONFIG")) != NULL && env[0]) {
strncpy (conf_file, env, (size_t)COB_MEDIUM_MAX);
conf_file[COB_MEDIUM_MAX] = 0;
is_optional = 0; /* If declared then it is NOT optional */
if (strchr (conf_file, PATHSEP_CHAR) != NULL) {
conf_runtime_error (0, _("invalid value '%s' for configuration tag '%s'"), conf_file, "COB_RUNTIME_CONFIG");
conf_runtime_error (1, _("should not contain '%c'"), PATHSEP_CHAR);
return -1;
}
} else {
/* check for COB_CONFIG_DIR (use default if not in environment) */
if ((env = getenv ("COB_CONFIG_DIR")) != NULL && env[0]) {
snprintf (conf_file, (size_t)COB_MEDIUM_MAX, "%s%c%s", env, SLASH_CHAR, "runtime.cfg");
} else {
snprintf (conf_file, (size_t)COB_MEDIUM_MAX, "%s%c%s", COB_CONFIG_DIR, SLASH_CHAR, "runtime.cfg");
}
conf_file[COB_MEDIUM_MAX] = 0; /* fixing code analyser warning */
is_optional = 1; /* If not present, then just use env vars */
if (strchr (conf_file, PATHSEP_CHAR) != NULL) {
conf_runtime_error (0, _("invalid value '%s' for configuration tag '%s'"), conf_file, "COB_CONFIG_DIR");
conf_runtime_error (1, _("should not contain '%c'"), PATHSEP_CHAR);
return -1;
}
}
sprintf (varseq_dflt, "%d", WITH_VARSEQ); /* Default comes from config.h */
for (i = 0; i < NUM_CONFIG; i++) {
gc_conf[i].data_type &= ~(STS_ENVSET | STS_CNFSET | STS_ENVCLR); /* Clear status */
}
sts = cob_load_config_file (conf_file, is_optional);
if (sts < 0) {
return sts;
}
cob_rescan_env_vals (); /* Check for possible environment variables */
/* Set with default value if present and not set otherwise */
for (i = 0; i < NUM_CONFIG; i++) {
if (gc_conf[i].default_val
&& !(gc_conf[i].data_type & STS_CNFSET)
&& !(gc_conf[i].data_type & STS_ENVSET)) {
for (j = 0; j < NUM_CONFIG; j++) { /* Any alias present? */
if (j != i
&& gc_conf[i].data_loc == gc_conf[j].data_loc)
break;
}
if (j < NUM_CONFIG) {
if (!(gc_conf[j].data_type & STS_CNFSET)
&& !(gc_conf[j].data_type & STS_ENVSET)) { /* alias not defined? */
set_config_val ((char *)gc_conf[i].default_val, i);
}
} else {
set_config_val ((char *)gc_conf[i].default_val, i); /* Set default value */
}
}
}
check_current_date ();
return 0;
}
/* output runtime warning for issues produced by external API functions */
void
cob_runtime_warning_external (const char *caller_name, const int cob_reference, const char *fmt, ...)
{
va_list args;
if (!cobsetptr->cob_display_warn) {
return;
}
if (!(caller_name && *caller_name)) caller_name = "unknown caller";
/* Prefix */
if (cob_reference) {
fflush (stderr); /* necessary as we write afterwards */
write_to_stderr_or_return_arr ("libcob: ");
cob_get_source_line ();
output_source_location ();
} else {
fprintf (stderr, "libcob: ");
}
fprintf (stderr, _("warning: "));
fprintf (stderr, "%s: ", caller_name);
/* Body */
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end (args);
/* Postfix */
putc ('\n', stderr);
fflush (stderr);
}
void
cob_runtime_warning_ss (const char *msg, const char *addition)
{
if (cobsetptr && !cobsetptr->cob_display_warn) {
return;
}
/* Prefix */
write_to_stderr_or_return_arr ("libcob: ");
output_source_location ();
write_to_stderr_or_return_str (warning_msgid);
/* Body */
write_to_stderr_or_return_str (msg);
if (addition) {
write_to_stderr_or_return_str (addition);
}
/* Postfix */
write_to_stderr_or_return_arr ("\n");
}
void
cob_runtime_warning (const char *fmt, ...)
{
va_list args;
if (cobsetptr && !cobsetptr->cob_display_warn) {
return;
}
fflush (stderr); /* necessary as we write afterwards */
/* Prefix */
write_to_stderr_or_return_arr ("libcob: ");
cob_get_source_line ();
output_source_location ();
fprintf (stderr, _("warning: ")); /* back to stdio */
/* Body */
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end (args);
/* Postfix */
putc ('\n', stderr);
fflush (stderr);
}
void
cob_runtime_hint (const char *fmt, ...)
{
va_list args;
/* Prefix */
fprintf (stderr, "%s", _("note: "));
/* Body */
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end (args);
/* Postfix */
putc ('\n', stderr);
fflush (stderr);
}
/* extra function for direct interaction with the debugger
when a new runtime error string is constructed */
static void COB_NOINLINE
cob_setup_runtime_error_str (const char *fmt, va_list ap)
{
const char *source_file;
unsigned int source_line;
char *p = runtime_err_str;
set_source_location (&source_file, &source_line);
if (source_file) {
if (source_line) {
sprintf (runtime_err_str, "%s:%u: ",
source_file, source_line);
} else {
sprintf (runtime_err_str, "%s: ",
source_file);
}
p = runtime_err_str + strlen (runtime_err_str);
}
vsprintf (p, fmt, ap);
}
void
cob_runtime_error (const char *fmt, ...)
{
struct handlerlist *h;
va_list ap;
int more_error_procedures = 1;
cob_get_source_line ();
va_start (ap, fmt);
cob_setup_runtime_error_str (fmt, ap);
va_end (ap);
#if 1 /* RXWRXW - Exit screen */
/* Exit screen mode early */
cob_exit_screen ();
#endif
if (hdlrs != NULL && !active_error_handler && cobglobptr) {
const char *err_source_file;
unsigned int err_source_line, err_module_statement = 0;
cob_module_ptr err_module_pointer = NULL;
cob_field *err_module_param0 = NULL;
cob_field err_field = {COB_ERRBUF_SIZE, NULL, &const_alpha_attr };
int call_params = cobglobptr->cob_call_params;
/* save error location */
set_source_location (&err_source_file, &err_source_line);
if (COB_MODULE_PTR) {
err_module_pointer = COB_MODULE_PTR;
err_module_statement = COB_MODULE_PTR->module_stmt;
err_module_param0 = COB_MODULE_PTR->cob_procedure_params[0];
COB_MODULE_PTR->cob_procedure_params[0] = &err_field;
}
/* run registered error handlers */
active_error_handler = 1;
h = hdlrs;
while (h != NULL) {
int (*current_handler)(char *) = h->proc;
struct handlerlist *hp = h;
h = h->next;
cob_free (hp);
if (more_error_procedures) {
/* fresh error buffer with guaranteed size */
char local_err_str[COB_ERRBUF_SIZE];
memcpy (local_err_str, runtime_err_str, COB_ERRBUF_SIZE);
err_field.data = (unsigned char *)local_err_str;
/* ensure that error handlers set their own locations */
cob_source_file = NULL;
cob_source_line = 0;
cobglobptr->cob_call_params = 1;
more_error_procedures = current_handler (local_err_str);
}
}
hdlrs = NULL;
active_error_handler = 0;
/* restore error location */
cob_source_file = err_source_file;
cob_source_line = err_source_line;
COB_MODULE_PTR = err_module_pointer;
if (COB_MODULE_PTR) {
COB_MODULE_PTR->module_stmt = err_module_statement;
COB_MODULE_PTR->cob_procedure_params[0] = err_module_param0;
}
cobglobptr->cob_call_params = call_params;
}
/* Prefix */
if (more_error_procedures) {
const char *source_file;
unsigned int source_line;
set_source_location (&source_file, &source_line);
fputs ("libcob: ", stderr);
if (source_file) {
fprintf (stderr, "%s:", source_file);
if (source_line) {
fprintf (stderr, "%u:", source_line);
}
fputc (' ', stderr);
}
fprintf (stderr, "%s: ", _("error"));
/* Body */
va_start (ap, fmt);
vfprintf (stderr, fmt, ap);
va_end (ap);
/* Postfix */
fputc ('\n', stderr);
fflush (stderr);
}
/* setup reason for optional module dump */
if (cob_initialized && abort_reason[0] == 0) {
#if 0 /* Is there a use in this message ?*/
fputc ('\n', stderr);
fprintf (stderr, _("abnormal termination - file contents may be incorrect"));
#endif
va_start (ap, fmt);
vsnprintf (abort_reason, COB_MINI_BUFF, fmt, ap);
va_end (ap);
}
}
void
cob_fatal_error (const enum cob_fatal_error fatal_error)
{
const char *msg;
unsigned char *file_status;
char *err_cause;
int status;
#ifdef _WIN32
char *p;
#endif
switch (fatal_error) {
#if 0 /* Currently not in use, should enter unknown error */
case COB_FERROR_NONE:
break;
#endif
/* Note: can be simply tested; therefore no exclusion */
case COB_FERROR_CANCEL:
cob_runtime_error (_("attempt to CANCEL active program"));
break;
/* Note: can be simply tested; therefore no exclusion */
case COB_FERROR_INITIALIZED:
#ifdef _WIN32
/* cob_unix_lf needs to be set before any error message is thrown,
as they would have wrong line endings otherwise */
p = getenv ("COB_UNIX_LF");
if (p && (*p == 'Y' || *p == 'y' ||
*p == 'T' || *p == 't' ||
*p == '1')) {
(void)_setmode (_fileno (stdin), _O_BINARY);
(void)_setmode (_fileno (stdout), _O_BINARY);
(void)_setmode (_fileno (stderr), _O_BINARY);
}
#endif
/* note: same message in call.c */
cob_runtime_error (_("cob_init() has not been called"));
break;
/* LCOV_EXCL_START */
case COB_FERROR_CODEGEN:
cob_runtime_error ("codegen error"); /* not translated by intent */
cob_runtime_error (_("Please report this!"));
break;
/* LCOV_EXCL_STOP */
/* Note: can be simply tested; therefore no exclusion */
case COB_FERROR_CHAINING:
cob_runtime_error (_("CALL of program with CHAINING clause"));
break;
/* LCOV_EXCL_START */
case COB_FERROR_STACK:
cob_runtime_error (_("stack overflow, possible PERFORM depth exceeded"));
break;
/* LCOV_EXCL_STOP */
/* Note: can be simply tested (GO TO DECLARATIVES); therefore no exclusion */
case COB_FERROR_GLOBAL:
cob_runtime_error (_("invalid entry/exit in GLOBAL USE procedure"));
break;
/* LCOV_EXCL_START */
case COB_FERROR_MEMORY:
cob_runtime_error (_("unable to allocate memory"));
break;
/* LCOV_EXCL_STOP */
/* LCOV_EXCL_START */
case COB_FERROR_MODULE:
cob_runtime_error (_("invalid entry into module"));
break;
/* LCOV_EXCL_STOP */
/* Note: can be simply tested; therefore no exclusion */
case COB_FERROR_RECURSIVE:
/* LCOV_EXCL_LINE */
if (cob_module_err) {
cob_runtime_error (_("recursive CALL from '%s' to '%s' which is NOT RECURSIVE"),
COB_MODULE_PTR->module_name, cob_module_err->module_name);
/* LCOV_EXCL_START */
/* Note: only in for old modules - not active with current generation */
} else {
cob_runtime_error (_("invalid recursive COBOL CALL to '%s'"),
COB_MODULE_PTR->module_name);
}
/* LCOV_EXCL_STOP */
break;
/* LCOV_EXCL_START */
case COB_FERROR_FREE:
cob_runtime_error (_("call to %s with NULL pointer"), "cob_free");
break;
/* LCOV_EXCL_STOP */
case COB_FERROR_FILE:
file_status = cobglobptr->cob_error_file->file_status;
status = COB_D2I (file_status[0]) * 10 + COB_D2I (file_status[1]);
switch (status) {
case COB_STATUS_10_END_OF_FILE:
msg = _("end of file");
break;
case COB_STATUS_14_OUT_OF_KEY_RANGE:
msg = _("key out of range");
break;
case COB_STATUS_21_KEY_INVALID:
msg = _("key order not ascending");
break;
case COB_STATUS_22_KEY_EXISTS:
msg = _("record key already exists");
break;
case COB_STATUS_23_KEY_NOT_EXISTS:
msg = _("record key does not exist");
break;
case COB_STATUS_30_PERMANENT_ERROR:
msg = _("permanent file error");
break;
case COB_STATUS_31_INCONSISTENT_FILENAME:
msg = _("inconsistent file name");
break;
case COB_STATUS_35_NOT_EXISTS:
msg = _("file does not exist");
break;
case COB_STATUS_37_PERMISSION_DENIED:
msg = _("permission denied");
break;
case COB_STATUS_39_CONFLICT_ATTRIBUTE:
msg = _("mismatch of fixed file attributes");
break;
case COB_STATUS_41_ALREADY_OPEN:
msg = _("file already open");
break;
case COB_STATUS_42_NOT_OPEN:
msg = _("file not open");
break;
case COB_STATUS_43_READ_NOT_DONE:
msg = _("READ must be executed first");
break;
case COB_STATUS_44_RECORD_OVERFLOW:
msg = _("record overflow");
break;
case COB_STATUS_46_READ_ERROR:
msg = _("READ after unsuccessful READ/START");
break;
case COB_STATUS_47_INPUT_DENIED:
msg = _("READ/START not allowed, file not open for input");
break;
case COB_STATUS_48_OUTPUT_DENIED:
msg = _("WRITE not allowed, file not open for output");
break;
case COB_STATUS_49_I_O_DENIED:
msg = _("DELETE/REWRITE not allowed, file not open for I-O");
break;
case COB_STATUS_51_RECORD_LOCKED:
msg = _("record locked by another file connector");
break;
case COB_STATUS_57_I_O_LINAGE:
msg = _("LINAGE values invalid");
break;
case COB_STATUS_61_FILE_SHARING:
msg = _("file sharing conflict");
break;
case COB_STATUS_71_BAD_CHAR:
msg = _("invalid data in LINE SEQUENTIAL file");
break;
/* LCOV_EXCL_START */
case COB_STATUS_91_NOT_AVAILABLE:
msg = _("runtime library is not configured for this operation");
break;
/* LCOV_EXCL_STOP */
/* LCOV_EXCL_START */
default:
msg = _("unknown file error");
break;
/* LCOV_EXCL_STOP */
}
err_cause = cob_get_filename_print (cobglobptr->cob_error_file, 1);
/* FIXME: additional check if referenced program has active code location */
if (cobglobptr->last_exception_statement == STMT_UNKNOWN) {
cob_runtime_error (_("%s (status = %02d) for file %s"),
msg, status, err_cause);
} else {
cob_runtime_error (_("%s (status = %02d) for file %s on %s"),
msg, status, err_cause,
cob_statement_name[cobglobptr->last_exception_statement]);
}
break;
/* LCOV_EXCL_START */
case COB_FERROR_FUNCTION:
cob_runtime_error (_("attempt to use non-implemented function"));
break;
case COB_FERROR_XML:
cob_runtime_error (_("attempt to use non-implemented XML I/O"));
break;
case COB_FERROR_JSON:
cob_runtime_error (_("attempt to use non-implemented JSON I/O"));
break;
default:
/* internal rare error, no need for translation */
cob_runtime_error ("unknown failure: %d", fatal_error);
break;
/* LCOV_EXCL_STOP */
}
cob_hard_failure ();
}
static void
conf_runtime_error_value (const char *value, const int pos)
{
const char *name = NULL;
if (gc_conf[pos].data_type & STS_CNFSET) {
name = gc_conf[pos].conf_name;
} else {
name = gc_conf[pos].env_name;
}
conf_runtime_error (0, _("invalid value '%s' for configuration tag '%s'"), value, name);
}
static void
conf_runtime_error (const int finish_error, const char *fmt, ...)
{
va_list args;
if (!conf_runtime_error_displayed) {
conf_runtime_error_displayed = 1;
fputs (_("configuration error:"), stderr);
putc ('\n', stderr);
}
/* Prefix; note: no need to strcmp as we check against
the value passed last time */
if (cob_source_file != last_runtime_error_file
|| cob_source_line != last_runtime_error_line) {
last_runtime_error_file = cob_source_file;
last_runtime_error_line = cob_source_line;
if (cob_source_file) {
fprintf (stderr, "%s", cob_source_file);
if (cob_source_line) {
fprintf (stderr, ":%u", cob_source_line);
}
} else {
fprintf (stderr, "%s", _("environment variables"));
}
fputc(':', stderr);
fputc(' ', stderr);
}
/* Body */
va_start (args, fmt);
vfprintf (stderr, fmt, args);
va_end (args);
/* Postfix */
if (!finish_error) {
putc (';', stderr);
putc ('\n', stderr);
putc ('\t', stderr);
} else {
putc ('\n', stderr);
fflush (stderr);
}
}
#if defined (COB_GEN_SCREENIO)
/* resolve curses library related version information
stores the information in the version_buffer parameter
returns the mouse info */
static const char *
get_screenio_and_mouse_info (char *version_buffer, size_t size, const int verbose)
{
const char *mouse_support = _("unknown");
int major, minor, patch;
#if defined (__PDCURSES__)
int opt1, opt2, opt3;
#if defined (PDC_FORCE_UTF8)
const int utf8 = 1;
#else
const int utf8 = 0;
#endif
#endif
#if defined (__PDCURSES__) || defined (NCURSES_VERSION)
#if defined (PDC_WIDE) || defined (NCURSES_WIDECHAR)
const int wide = 1;
#else
const int wide = 0;
#endif
#endif
char buff[56] = {'\0'};
memset (version_buffer, 0, size--);
if (verbose) {
initscr ();
}
#ifdef HAVE_HAS_MOUSE
if (verbose) {
int mouse_available = 0;
mousemask (ALL_MOUSE_EVENTS, NULL);
if (has_mouse () == TRUE) mouse_available = 1;
if (mouse_available) {
mouse_support = _("yes");
} else {
mouse_support = _("no");
}
}
#elif defined (HAVE_MOUSEMASK)
#if defined (__PDCURSES__)
/* CHECKME: that looks wrong - can't we test as above?
Double check with older PDCurses! */
mouse_support = _("yes");
#endif
#else
mouse_support = _("disabled");
#endif
#if defined (__PDCURSES__) || defined (NCURSES_VERSION)
#if defined (__PDCURSES__)
#if defined (PDC_VER_MAJOR)
#define CURSES_CMP_MAJOR PDC_VER_MAJOR
#define CURSES_CMP_MINOR PDC_VER_MINOR
#if PDC_VER_MAJOR > 3 || (PDC_VER_MAJOR == 3 && PDC_BUILD >= 3703)
#define RESOLVED_PDC_VER
{
PDC_VERSION ver;
PDC_get_version (&ver);
major = ver.major;
minor = ver.minor;
#ifdef __PDCURSESMOD__
patch = ver.change; /* note: PDCursesMod has an extra field */
#else
patch = ver.build % 100; /* note: PDCurses has it only "embedded" here */
#endif
opt1 = ver.csize * 8;
opt2 = ver.flags & PDC_VFLAG_WIDE;
opt3 = ver.flags & PDC_VFLAG_UTF8;
}
#else
COB_UNUSED (opt1);
COB_UNUSED (opt2);
COB_UNUSED (opt3);
#endif
#else
#define CURSES_CMP_MAJOR (PDC_BUILD / 1000)
#define CURSES_CMP_MINOR (PDC_BUILD - CURSES_CMP_MAJOR * 1000) / 100
COB_UNUSED (opt1);
COB_UNUSED (opt2);
COB_UNUSED (opt3);
#endif
#elif defined (NCURSES_VERSION)
#define CURSES_CMP_MAJOR NCURSES_VERSION_MAJOR
#define CURSES_CMP_MINOR NCURSES_VERSION_MINOR
#endif
#if !defined (RESOLVED_PDC_VER)
snprintf (version_buffer, size, "%s", curses_version ());
major = 0, minor = 0, patch = 0;
if ((sscanf (version_buffer, "%55s %55s %d.%d.%d", (char *)&buff, (char *)&buff, &major, &minor, &patch) < 4)
&& (sscanf (version_buffer, "%55s %d.%d.%d", (char *)&buff, &major, &minor, &patch) < 3)
&& (sscanf (version_buffer, "%d.%d.%d", &major, &minor, &patch) < 2)) {
major = 0, minor = 0;
}
#endif
if (major == CURSES_CMP_MAJOR && minor == CURSES_CMP_MINOR) {
snprintf (buff, 55, _("%s, version %d.%d.%d"), WITH_CURSES, major, minor, patch);
} else if (major != 0) {
snprintf (buff, 55, _("%s, version %d.%d.%d (compiled with %d.%d)"),
WITH_CURSES, major, minor, patch, CURSES_CMP_MAJOR, CURSES_CMP_MINOR);
} else {
snprintf (buff, 55, _("%s, version %s"), WITH_CURSES, version_buffer);
}
#if defined (RESOLVED_PDC_VER)
{
const int chtype_val = (int)sizeof (chtype) * 8;
char chtype_def[10] = { '\0' };
char wide_def[6] = {'\0'};
char utf8_def[6] = {'\0'};
const char *match;
if (chtype_val != opt1) {
match = "!";
} else {
match = "";
}
snprintf (chtype_def, 9, "%d[%d%s]", chtype_val, opt1, match);
if (wide != opt2) {
match = "!";
} else {
match = "";
}
snprintf (wide_def, 5, "%d[%d%s]", wide, opt2, match);
if (wide != opt2) {
match = "!";
} else {
match = "";
}
snprintf (utf8_def, 5, "%d[%d%s]", utf8, opt3, match);
snprintf (version_buffer, size, "%s (CHTYPE=%s, WIDE=%s, UTF8=%s)",
buff, chtype_def, wide_def, utf8_def);
}
#undef RESOLVED_PDC_VER
#elif defined (__PDCURSES__)
snprintf (version_buffer, size, "%s (CHTYPE=%d, WIDE=%d, UTF8=%d)", buff,
(int)sizeof (chtype) * 8, wide, utf8);
#else
snprintf (version_buffer, size, "%s (CHTYPE=%d, WIDE=%d)", buff,
(int)sizeof (chtype) * 8, wide);
#endif
#else /* defined (__PDCURSES__) || defined (NCURSES_VERSION) */
snprintf (version_buffer, size, "%s (CHTYPE=%d)", WITH_CURSES,
(int)sizeof (chtype) * 8);
#endif
if (verbose) {
size_t curr_size = strlen (version_buffer);
snprintf (version_buffer + curr_size, size - curr_size, " %s",
longname ());
endwin ();
}
return mouse_support;
}
#endif
/* resolve math library related version information
stores the information in the version_buffer parameter */
static void
get_math_info (char *version_buffer, size_t size, const int verbose)
{
int major, minor, patch;
size_t curr_size;
COB_UNUSED (verbose);
memset (version_buffer, 0, size--);
major = 0, minor = 0, patch = 0;
(void)sscanf (gmp_version, "%d.%d.%d", &major, &minor, &patch);
if (major == __GNU_MP_VERSION && minor == __GNU_MP_VERSION_MINOR) {
curr_size = snprintf (version_buffer, size, _("%s, version %d.%d.%d"), "GMP", major, minor, patch);
} else {
curr_size = snprintf (version_buffer, size, _("%s, version %d.%d.%d (compiled with %d.%d)"),
"GMP", major, minor, patch, __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR);
}
#if defined (mpir_version)
major = 0, minor = 0, patch = 0;
(void)sscanf (mpir_version, "%d.%d.%d", &major, &minor, &patch);
{
const char *deli = " - ";
curr_size += snprintf (version_buffer + curr_size, size - curr_size, "%s", deli);
}
if (major == __MPIR_VERSION && minor == __MPIR_VERSION_MINOR) {
snprintf (version_buffer + curr_size, size - curr_size,
_("%s, version %d.%d.%d"),
"MPIR", major, minor, patch);
} else {
snprintf (version_buffer + curr_size, size - curr_size,
_("%s, version %d.%d.%d (compiled with %d.%d)"),
"MPIR", major, minor, patch, __MPIR_VERSION, __MPIR_VERSION_MINOR);
}
#else
COB_UNUSED (curr_size);
#endif
}
/* internal library version as string,
note: the patchlevel may differ from the package one */
const char* libcob_version () {
/* FIXME: replace this define by a general one (COB_TREE_DEBUG) _was_ for debugging
the parse tree only ... */
#if defined (COB_TREE_DEBUG) || defined (_DEBUG)
{
int major, minor;
major = 0, minor = 0;
(void)sscanf (PACKAGE_VERSION, "%d.%d", &major, &minor);
/* LCOV_EXCL_START */
if (major != __LIBCOB_VERSION || minor != __LIBCOB_VERSION_MINOR) {
const char* version = CB_XSTRINGIFY (__LIBCOB_VERSION) "."
CB_XSTRINGIFY (__LIBCOB_VERSION_MINOR);
cob_runtime_error (_("version mismatch"));
cob_runtime_hint (_("%s has version %s.%d"), "libcob internally",
version, __LIBCOB_VERSION_PATCHLEVEL);
cob_runtime_hint (_("%s has version %s.%d"), "libcob package",
PACKAGE_VERSION, PATCH_LEVEL);
cob_hard_failure ();
}
/* LCOV_EXCL_STOP */
{
int check, patch;
patch = 0;
check = set_libcob_version (&major, &minor, &patch);
/* LCOV_EXCL_START */
if (check != 0 && check != 3) {
cob_runtime_error (_("version mismatch"));
/* untranslated as it is very unlikely to happen */
cob_runtime_hint ("internal version check differs at %d\n", check);
cob_hard_failure ();
}
/* LCOV_EXCL_STOP */
}
}
#endif
return CB_XSTRINGIFY (__LIBCOB_VERSION) "."
CB_XSTRINGIFY (__LIBCOB_VERSION_MINOR) "."
CB_XSTRINGIFY (__LIBCOB_VERSION_PATCHLEVEL);
}
/* internal library version set/compare,
if 'mayor' is not 0 on entry compares against the given
values, returns the parameter that is not matching
given parameters will be set to the internal values on exit
note: the patchlevel may differ from the package one */
int set_libcob_version (int *mayor, int *minor, int *patch) {
int ret = 0;
if (*mayor != 0) {
if (*mayor != __LIBCOB_VERSION) {
ret = 1;
} else if (*minor != __LIBCOB_VERSION_MINOR) {
ret = 2;
} else if (*patch != __LIBCOB_VERSION_PATCHLEVEL) {
ret = 3;
}
}
*mayor = __LIBCOB_VERSION;
*minor = __LIBCOB_VERSION_MINOR;
*patch = __LIBCOB_VERSION_PATCHLEVEL;
return ret;
}
static void set_cob_build_stamp (char *cob_build_stamp)
{
int status, day, year;
char month[64];
/* Set up build time stamp */
memset (cob_build_stamp, 0, (size_t)COB_MINI_BUFF);
memset (month, 0, sizeof (month));
day = 0;
year = 0;
status = sscanf (__DATE__, "%63s %d %d", month, &day, &year);
if (status == 3) {
snprintf (cob_build_stamp, (size_t)COB_MINI_MAX,
"%s %2.2d %4.4d %s", month, day, year, __TIME__);
} else {
snprintf (cob_build_stamp, (size_t)COB_MINI_MAX,
"%s %s", __DATE__, __TIME__);
}
}
/* provides a two line output for GnuCOBOL + C compiler and used libraries */
void
print_version_summary (void)
{
char cob_build_stamp[COB_MINI_BUFF];
set_cob_build_stamp (cob_build_stamp);
printf ("%s %s (%s), ",
PACKAGE_NAME, libcob_version(), cob_build_stamp);
/* note: some compilers use a very long identifier */
printf ("%s\n", GC_C_VERSION_PRF GC_C_VERSION);
printf ("%s %d.%d.%d",
#ifdef __MPIR_VERSION
"MPIR", __MPIR_VERSION, __MPIR_VERSION_MINOR, __MPIR_VERSION_PATCHLEVEL
#else
"GMP", __GNU_MP_VERSION, __GNU_MP_VERSION_MINOR, __GNU_MP_VERSION_PATCHLEVEL
#endif
);
#if defined (LIBXML_VERSION)
printf (", libxml2 %d.%d.%d",
LIBXML_VERSION / 10000,
(LIBXML_VERSION - (int)(LIBXML_VERSION / 10000) * 10000) / 100,
LIBXML_VERSION % 100);
#endif
#if defined (CJSON_VERSION_MAJOR)
printf (", cJSON %d.%d.%d",
CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR, CJSON_VERSION_PATCH);
#endif
#if defined (JSON_C_MAJOR_VERSION)
printf (", JSON-c %d.%d.%d",
JSON_C_MAJOR_VERSION, JSON_C_MINOR_VERSION, JSON_C_MICRO_VERSION);
#endif
#if defined (__PDCURSES__)
printf (", %s %d.%d",
#ifdef __PDCURSESMOD__
"PDCursesMod",
#else
"PDCurses",
#endif
CURSES_CMP_MAJOR, CURSES_CMP_MINOR);
#ifdef PDC_VER_CHANGE
printf (".%d", PDC_VER_CHANGE);
#elif defined (PDC_BUILD)
printf (" (%d)", PDC_BUILD);
#endif
#endif
#if defined (NCURSES_VERSION_MAJOR)
printf (", %s %d.%d.%d",
#ifdef NCURSES_WIDECHAR
"ncursesw",
#else
"ncurses",
#endif
NCURSES_VERSION_MAJOR, NCURSES_VERSION_MINOR, NCURSES_VERSION_PATCH);
#endif
#if defined (WITH_DB)
printf (", BDB %d.%d.%d",
DB_VERSION_MAJOR, DB_VERSION_MINOR, DB_VERSION_PATCH);
#endif
#if defined (WITH_CISAM)
printf (", C-ISAM");
#endif
#if defined (WITH_DISAM)
printf (", D-ISAM");
#endif
#if defined (WITH_VBISAM)
printf (", VB-ISAM");
#endif
putchar ('\n');
}
void
print_version (void)
{
char cob_build_stamp[COB_MINI_BUFF];
set_cob_build_stamp (cob_build_stamp);
printf ("libcob (%s) %s.%d\n",
PACKAGE_NAME, PACKAGE_VERSION, PATCH_LEVEL);
puts ("Copyright (C) 2023 Free Software Foundation, Inc.");
printf (_("License LGPLv3+: GNU LGPL version 3 or later <%s>"), "https://gnu.org/licenses/lgpl.html");
putchar ('\n');
puts (_("This is free software; see the source for copying conditions. There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE."));
printf (_("Written by %s"), "Keisuke Nishida, Roger While, Ron Norman, Simon Sobisch, Edward Hart");
putchar ('\n');
/* TRANSLATORS: This msgid is intended as the "Packaged" msgid, %s expands to date and time */
printf (_("Built %s"), cob_build_stamp);
putchar ('\n');
/* TRANSLATORS: This msgid is intended as the "Built" msgid, %s expands to date and time */
printf (_("Packaged %s"), COB_TAR_DATE);
putchar ('\n');
}
void
print_info (void)
{
print_info_detailed (0);
}
void
print_info_detailed (const int verbose)
{
char screenio_info[150];
const char *mouse_support;
char buff[56] = { '\0' };
char *s;
/* resolving screenio related information before anything else as this
function will possibly run initscr + endwin and therefore
may interfer with other output */
#if defined (COB_GEN_SCREENIO)
mouse_support = get_screenio_and_mouse_info
((char*)&screenio_info, sizeof (screenio_info), verbose);
#else
snprintf ((char *)&screenio_info, sizeof(screenio_info) - 1,
"%s", _("disabled"));
mouse_support = _("disabled");
#endif
print_version ();
putchar ('\n');
puts (_("build information"));
var_print (_("build environment"), COB_BLD_BUILD, "", 0);
var_print ("CC", COB_BLD_CC, "", 0);
/* Note: newline because most compilers define a long version string (> 30 characters) */
var_print (_("C version"), GC_C_VERSION_PRF GC_C_VERSION, "", 0);
var_print ("CPPFLAGS", COB_BLD_CPPFLAGS, "", 0);
var_print ("CFLAGS", COB_BLD_CFLAGS, "", 0);
var_print ("LD", COB_BLD_LD, "", 0);
var_print ("LDFLAGS", COB_BLD_LDFLAGS, "", 0);
putchar ('\n');
puts (_("GnuCOBOL information"));
var_print ("COB_MODULE_EXT", COB_MODULE_EXT, "", 0);
#if 0 /* only relevant for cobc */
var_print ("COB_OBJECT_EXT", COB_OBJECT_EXT, "", 0);
var_print ("COB_EXE_EXT", COB_EXE_EXT, "", 0);
#endif
#if defined (USE_LIBDL) || defined (_WIN32)
var_print (_("dynamic loading"), "system", "", 0);
#else
var_print (_("dynamic loading"), "libtool", "", 0);
#endif
if (verbose) {
#ifdef COB_PARAM_CHECK
var_print ("\"CBL_\" param check", _("enabled"), "", 0);
#else
var_print ("\"CBL_\" param check", _("disabled"), "", 0);
#endif
}
#ifdef COB_64_BIT_POINTER
var_print ("64bit-mode", _("yes"), "", 0);
#else
var_print ("64bit-mode", _("no"), "", 0);
#endif
#ifdef COB_LI_IS_LL
var_print ("BINARY-C-LONG", _("8 bytes"), "", 0);
#else
var_print ("BINARY-C-LONG", _("4 bytes"), "", 0);
#endif
#ifdef WORDS_BIGENDIAN
var_print (_("endianness"), _("big-endian"), "", 0);
#else
var_print (_("endianness"), _("little-endian"), "", 0);
#endif
#ifdef COB_EBCDIC_MACHINE
var_print (_("native EBCDIC"), _("yes"), "", 0);
#else
var_print (_("native EBCDIC"), _("no"), "", 0);
#endif
snprintf (buff, sizeof (buff), "%d", WITH_VARSEQ);
var_print (_("variable file format"), buff, "", 0);
if ((s = getenv ("COB_VARSEQ_FORMAT")) != NULL) {
var_print ("COB_VARSEQ_FORMAT", s, "", 1);
}
#ifdef WITH_SEQRA_EXTFH
var_print (_("sequential file handler"), "EXTFH", "", 0);
#else
var_print (_("sequential file handler"), _("built-in"), "", 0);
#endif
#if defined (WITH_INDEX_EXTFH)
var_print (_("indexed file handler"), "EXTFH", "", 0);
#elif defined (WITH_DB)
{
int major, minor, patch;
major = 0, minor = 0, patch = 0;
db_version (&major, &minor, &patch);
if (major == DB_VERSION_MAJOR && minor == DB_VERSION_MINOR) {
snprintf (buff, 55, _("%s, version %d.%d.%d"),
"BDB", major, minor, patch);
} else {
snprintf (buff, 55, _("%s, version %d.%d.%d (compiled with %d.%d)"),
"BDB", major, minor, patch, DB_VERSION_MAJOR, DB_VERSION_MINOR);
}
}
var_print (_("indexed file handler"), buff, "", 0);
#elif defined (WITH_CISAM)
var_print (_("indexed file handler"), "C-ISAM", "", 0);
#elif defined (WITH_DISAM)
var_print (_("indexed file handler"), "D-ISAM", "", 0);
#elif defined (WITH_VBISAM)
#if defined (VB_RTD)
var_print (_("indexed file handler"), "VBISAM (RTD)", "", 0);
#else
var_print (_("indexed file handler"), "VBISAM", "", 0);
#endif
#else
var_print (_("indexed file handler"), _("disabled"), "", 0);
#endif
{
char math_info[115];
get_math_info ((char*)&math_info, sizeof (math_info), verbose);
var_print (_("mathematical library"), (char *)&math_info, "", 0);
}
#ifdef WITH_XML2
{
int major, minor, patch;
major = LIBXML_VERSION / 10000;
minor = (LIBXML_VERSION - major * 10000) / 100 ;
patch = LIBXML_VERSION - major * 10000 - minor * 100;
snprintf (buff, 55, _("%s, version %d.%d.%d"),
"libxml2", major, minor, patch);
var_print (_("XML library"), buff, "", 0);
LIBXML_TEST_VERSION
xmlCleanupParser ();
}
#else
var_print (_("XML library"), _("disabled"), "", 0);
#endif
#if defined (WITH_CJSON)
{
int major, minor, patch;
major = 0, minor = 0, patch = 0;
(void)sscanf (cJSON_Version(), "%d.%d.%d", &major, &minor, &patch);
if (major == CJSON_VERSION_MAJOR && minor == CJSON_VERSION_MINOR) {
snprintf (buff, 55, _("%s, version %d.%d.%d"),
"cJSON", major, minor, patch);
} else {
snprintf (buff, 55, _("%s, version %d.%d.%d (compiled with %d.%d)"),
"cJSON", major, minor, patch, CJSON_VERSION_MAJOR, CJSON_VERSION_MINOR);
}
}
var_print (_("JSON library"), buff, "", 0);
#elif defined (WITH_JSON_C)
{
int major, minor, patch;
major = 0, minor = 0, patch = 0;
(void)sscanf (json_c_version (), "%d.%d.%d", &major, &minor, &patch);
if (major == JSON_C_MAJOR_VERSION && minor == JSON_C_MINOR_VERSION) {
snprintf (buff, 55, _("%s, version %d.%d.%d"),
"json-c", major, minor, patch);
} else {
snprintf (buff, 55, _("%s, version %d.%d.%d (compiled with %d.%d)"),
"json-c", major, minor, patch, JSON_C_MAJOR_VERSION, JSON_C_MINOR_VERSION);
}
}
var_print (_("JSON library"), buff, "", 0);
#else
var_print (_("JSON library"), _("disabled"), "", 0);
#endif
var_print (_("extended screen I/O"), (char*)&screenio_info, "", 0);
var_print (_("mouse support"), mouse_support, "", 0);
#ifdef COB_DEBUG_LOG
var_print ("DEBUG_LOG", _("enabled"), "", 0);
#endif
}
void
print_runtime_conf ()
{
unsigned int i, j, k, vl, dohdg, hdlen, plen, plen2;
char value[COB_MEDIUM_BUFF], orgvalue[COB_MINI_BUFF];
#ifdef ENABLE_NLS /* note: translated version of definition values */
#ifdef HAVE_SETLOCALE
const char *s;
#endif
setting_group[1] = _("CALL configuration");
setting_group[2] = _("File I/O configuration");
setting_group[3] = _("Screen I/O configuration");
setting_group[4] = _("Miscellaneous");
setting_group[5] = _("System configuration");
#endif
printf ("%s %s.%d ", PACKAGE_NAME, PACKAGE_VERSION, PATCH_LEVEL);
puts (_("runtime configuration"));
if (cobsetptr->cob_config_file) {
strncpy (value, _("via"), (size_t)COB_MEDIUM_MAX);
value[COB_MEDIUM_MAX] = 0;
hdlen = (unsigned int)strlen (value) + 3;
/* output path of main configuration file */
printf (" %s ", value);
plen = 80 - hdlen;
strncpy (value, cobsetptr->cob_config_file[0], (size_t)COB_MEDIUM_MAX);
value[COB_MEDIUM_MAX] = 0;
vl = (unsigned int)strlen (value);
for (k = 0; vl > plen; vl -= plen, k += plen) {
printf ("%.*s\n%-*s", plen, &value[k], hdlen, "");
}
printf ("%s\n", &value[k]);
/* output path of additional configuration files */
for (i = 1; i < cobsetptr->cob_config_num; i++) {
printf ("%*d ", hdlen - 2, i);
strncpy (value, cobsetptr->cob_config_file[i], (size_t)COB_MEDIUM_MAX);
value[COB_MEDIUM_MAX] = 0;
vl = (unsigned int)strlen (value);
for (k = 0; vl > plen; vl -= plen, k += plen) {
printf ("%.*s\n%-*s", plen, &value[k], hdlen, "");
}
printf ("%s\n", &value[k]);
}
}
putchar ('\n');
strcpy (value, "todo");
hdlen = 15;
for (i = 0; i < NUM_CONFIG; i++) {
j = (unsigned int)strlen (gc_conf[i].env_name);
if (j > hdlen)
hdlen = j;
j = (unsigned int)strlen (gc_conf[i].conf_name);
if (j > hdlen)
hdlen = j;
}
for (j = 1; j < GRP_MAX; j++) {
dohdg = 1;
for (i = 0; i < NUM_CONFIG; i++) {
if (gc_conf[i].env_group == j) {
if (dohdg) {
dohdg = 0;
if (j > 1) {
putchar ('\n');
}
printf (" %s\n", setting_group[j]);
}
/* Convert value back into string and display it */
get_config_val (value, i, orgvalue);
if ((gc_conf[i].data_type & STS_ENVSET)
|| (gc_conf[i].data_type & STS_FNCSET)) {
putchar (' ');
if (gc_conf[i].data_type & STS_FNCSET) {
printf (" ");
} else
if (gc_conf[i].data_type & STS_CNFSET) {
printf ("Ovr");
} else {
printf ("env");
if (gc_conf[i].data_loc == offsetof(cob_settings,cob_preload_str)
&& cobsetptr->cob_preload_str_set != NULL) {
printf (": %-*s : ", hdlen, gc_conf[i].env_name);
printf ("%s\n", cobsetptr->cob_preload_str_set);
printf ("eval");
}
}
printf (": %-*s : ", hdlen, gc_conf[i].env_name);
} else
if (gc_conf[i].data_type & STS_CNFSET) {
if ((gc_conf[i].data_type & STS_ENVCLR)) {
printf (" : %-*s : ", hdlen, gc_conf[i].env_name);
puts (_("... removed from environment"));
}
if (gc_conf[i].config_num > 0) {
printf (" %d ", gc_conf[i].config_num);
} else {
printf (" ");
}
if (gc_conf[i].data_loc == offsetof(cob_settings,cob_preload_str)
&& cobsetptr->cob_preload_str_set != NULL) {
printf (": %-*s : ",hdlen,
gc_conf[i].set_by > 0 ? gc_conf[i].env_name
: gc_conf[i].conf_name);
printf ("%s\n",cobsetptr->cob_preload_str_set);
printf ("eval");
}
if (gc_conf[i].set_by > 0) {
printf (": %-*s : ", hdlen, gc_conf[i].env_name);
} else {
printf (": %-*s : ", hdlen, gc_conf[i].conf_name);
}
} else if (gc_conf[i].env_name) {
if (gc_conf[i].config_num > 0){
printf (" %d ", gc_conf[i].config_num);
} else {
printf (" ");
}
printf (": %-*s : ", hdlen, gc_conf[i].env_name);
if ((gc_conf[i].data_type & STS_ENVCLR)) {
puts (_("... removed from environment"));
continue;
}
} else {
printf (" : %-*s : ", hdlen, gc_conf[i].conf_name);
}
vl = (unsigned int)strlen (value);
plen = 71 - hdlen;
if (vl < min_conf_length) {
plen2 = min_conf_length - vl;
} else if (vl == min_conf_length) {
plen2 = 1;
} else {
plen2 = 0;
}
for (k = 0; vl > plen; vl -= plen, k += plen) {
printf ("%.*s\n %-*s : ", plen, &value[k], hdlen, "");
}
printf ("%s", &value[k]);
printf ("%.*s", plen2, " ");
if (orgvalue[0]) {
printf (" (%s)", orgvalue);
}
if (gc_conf[i].set_by != 0) {
putchar (' ');
if (gc_conf[i].set_by != FUNC_NAME_IN_DEFAULT) {
printf (_("(set by %s)"), gc_conf[gc_conf[i].set_by].env_name);
} else {
printf (_("(set by %s)"), gc_conf[i].default_val);
}
}
if (!(gc_conf[i].data_type & STS_ENVSET)
&& !(gc_conf[i].data_type & STS_CNFSET)
&& !(gc_conf[i].data_type & STS_FNCSET)) {
putchar (' ');
if ((gc_conf[i].data_type & STS_RESET)) {
printf (_("(reset)"));
} else
if (strcmp (value, not_set) != 0) {
printf (_("(default)"));
} else
if (gc_conf[i].default_val
&& strcmp (gc_conf[i].default_val, not_set) == 0) {
printf (_("(default)"));
}
}
putchar ('\n');
}
}
}
#ifdef HAVE_SETLOCALE
#ifdef ENABLE_NLS
s = getenv ("LOCALEDIR");
printf (" : %-*s : %s\n", hdlen, "LOCALEDIR", s ? s : LOCALEDIR);
#endif
printf (" : %-*s : %s\n", hdlen, "LC_CTYPE", setlocale (LC_CTYPE, NULL));
printf (" : %-*s : %s\n", hdlen, "LC_NUMERIC", setlocale (LC_NUMERIC, NULL));
printf (" : %-*s : %s\n", hdlen, "LC_COLLATE", setlocale (LC_COLLATE, NULL));
#ifdef LC_MESSAGES
printf (" : %-*s : %s\n", hdlen, "LC_MESSAGES", setlocale (LC_MESSAGES, NULL));
#endif
printf (" : %-*s : %s\n", hdlen, "LC_MONETARY", setlocale (LC_MONETARY, NULL));
printf (" : %-*s : %s\n", hdlen, "LC_TIME", setlocale (LC_TIME, NULL));
#endif
}
cob_settings *
cob_get_settings_ptr ()
{
return cobsetptr;
}
void
cob_init_nomain (const int argc, char **argv)
{
check_mainhandle = 0;
cob_init (argc, argv);
}
void
cob_common_init (void *setptr)
{
#ifdef ENABLE_NLS
{
struct stat localest;
const char * localedir;
localedir = getenv ("LOCALEDIR");
if (localedir != NULL
&& !stat (localedir, &localest)
&& (S_ISDIR (localest.st_mode))) {
bindtextdomain (PACKAGE, localedir);
} else {
bindtextdomain (PACKAGE, LOCALEDIR);
}
textdomain (PACKAGE);
}
#endif
#ifdef _WIN32
/* Allows running tests under Win */
{
int use_unix_lf = 0;
char *s = getenv ("COB_UNIX_LF");
if (s != NULL) {
if (setptr) {
set_config_val_by_name (s, "unix_lf", NULL);
use_unix_lf = cobsetptr->cob_unix_lf;
} else
if (*s == 'Y' || *s == 'y' ||
*s == 'O' || *s == 'o' ||
*s == 'T' || *s == 't' ||
*s == '1') {
use_unix_lf = 1;
}
}
if (use_unix_lf) {
(void)_setmode (_fileno (stdin), _O_BINARY);
(void)_setmode (_fileno (stdout), _O_BINARY);
(void)_setmode (_fileno (stderr), _O_BINARY);
}
}
#endif
}
/* normal call, but longjmp back on exit (1),
errors (-1), hard errors (-2) or signals (-3) */
int
cob_call_with_exception_check (const char *name, const int argc, void **argv)
{
#ifndef COB_WITHOUT_JMP
int ret;
return_jmp_buffer_set = 1;
ret = setjmp (return_jmp_buf);
if (ret) {
return_jmp_buffer_set = 0;
return ret;
}
#endif
exit_code = cob_call (name, argc, argv);
return 0;
}
void
cob_init (const int argc, char **argv)
{
char *s;
#if defined (HAVE_READLINK) || defined (HAVE_GETEXECNAME)
const char *path;
#endif
int i;
/* Ensure initialization is only done once. Within generated modules and
libcob this is already ensured, but an external caller may call this
function again */
if (cob_initialized) {
#if 0 /* Simon: We may raise a runtime warning/error in the future here */
cob_runtime_warning ("%s called more than once", "cob_init");
#endif
return;
}
#ifdef __GLIBC__
{
/*
* GNU libc may write a stack trace to /dev/tty when malloc
* detects corruption. If LIBC_FATAL_STDERR_ is set to any
* nonempty string, it writes to stderr instead. See:
*https://code.woboq.org/userspace/glibc/sysdeps/posix/libc_fatal.c.html
*/
if (getenv ((const char*)"LIBC_FATAL_STDERR_") == NULL ) {
(void)putenv ((char*)"LIBC_FATAL_STDERR_=keep_off_the_grass");
}
}
#endif
cob_set_signal ();
cob_alloc_base = NULL;
cob_local_env = NULL;
cob_last_sfile = NULL;
commlnptr = NULL;
basext = NULL;
sort_keys = NULL;
sort_collate = NULL;
cob_source_file = NULL;
exit_hdlrs = NULL;
hdlrs = NULL;
commlncnt = 0;
sort_nkeys = 0;
cob_source_line = 0;
cob_local_env_size = 0;
current_arg = 1;
cob_argc = argc;
cob_argv = argv;
/* Get global structure */
cobglobptr = cob_malloc (sizeof (cob_global));
/* Get settings structure */
cobsetptr = cob_malloc (sizeof (cob_settings));
cob_initialized = 1;
#ifdef HAVE_SETLOCALE
/* Prime the locale from user settings */
s = setlocale (LC_ALL, "");
if (s) {
/* Save initial values */
cobglobptr->cob_locale_orig = cob_strdup (s);
s = setlocale (LC_CTYPE, NULL);
if (s) {
cobglobptr->cob_locale_ctype = cob_strdup (s);
}
s = setlocale (LC_COLLATE, NULL);
if (s) {
cobglobptr->cob_locale_collate = cob_strdup (s);
}
#ifdef LC_MESSAGES
s = setlocale (LC_MESSAGES, NULL);
if (s) {
cobglobptr->cob_locale_messages = cob_strdup (s);
}
#endif
s = setlocale (LC_MONETARY, NULL);
if (s) {
cobglobptr->cob_locale_monetary = cob_strdup (s);
}
s = setlocale (LC_NUMERIC, NULL);
if (s) {
cobglobptr->cob_locale_numeric = cob_strdup (s);
}
s = setlocale (LC_TIME, NULL);
if (s) {
cobglobptr->cob_locale_time = cob_strdup (s);
}
/* Set to standard "C" locale for COBOL */
setlocale (LC_NUMERIC, "C");
setlocale (LC_CTYPE, "C");
/* Save changed locale */
s = setlocale (LC_ALL, NULL);
if (s) {
cobglobptr->cob_locale = cob_strdup (s);
}
}
#endif
cob_init_sig_descriptions ();
cob_common_init (cobsetptr);
/* Load runtime configuration file */
if (unlikely (cob_load_config () < 0)) {
cob_hard_failure ();
}
/* Copy COB_PHYSICAL_CANCEL from settings (internal) to global structure */
cobglobptr->cob_physical_cancel = cobsetptr->cob_physical_cancel;
/* Internal Debug Log */
if (cobsetptr->cob_debug_log) {
#ifndef COB_DEBUG_LOG
cob_runtime_warning (_("compiler was not built with --enable-debug-log; COB_DEBUG_LOG ignored"));
#else
cob_debug_open ();
#endif
}
/* Call inits with cobsetptr to get the addresses of all */
/* Screen-IO might be needed for error outputs */
cob_init_screenio (cobglobptr, cobsetptr);
cob_init_cconv (cobglobptr);
cob_init_numeric (cobglobptr);
cob_init_strings (cobglobptr);
cob_init_move (cobglobptr, cobsetptr);
cob_init_intrinsic (cobglobptr);
cob_init_fileio (cobglobptr, cobsetptr);
cob_init_call (cobglobptr, cobsetptr, check_mainhandle);
cob_init_termio (cobglobptr, cobsetptr);
cob_init_reportio (cobglobptr, cobsetptr);
cob_init_mlio (cobglobptr);
/* Set up library routine stuff */
cobglobptr->cob_term_buff = cob_malloc ((size_t)COB_MEDIUM_BUFF);
/* Set switches */
for (i = 0; i <= COB_SWITCH_MAX; ++i) {
char switch_name[16];
sprintf (switch_name, "COB_SWITCH_%d", i);
s = getenv (switch_name);
if (s && (*s == '1' || strcasecmp (s, "ON") == 0)) {
cob_switch[i] = 1;
} else {
cob_switch[i] = 0;
}
}
#ifndef HAVE_DESIGNATED_INITS
init_statement_list ();
#endif
/* Get user name if not set via environment already */
if (cobsetptr->cob_user_name == NULL) {
#if defined (_WIN32)
/* note: only defined manual (needs additional link to advapi32): */
#if defined (HAVE_GETUSERNAME)
unsigned long bsiz = COB_MINI_MAX;
char user_name[COB_MINI_BUFF];
if (GetUserName (user_name, &bsiz)) {
set_config_val_by_name (user_name, "username", "GetUserName()");
}
#endif
#elif !defined(__OS400__)
s = getlogin ();
if (s) {
set_config_val_by_name (s, "username", "getlogin()");
}
#endif
#if 0 /* likely not needed, if unset then empty */
if (cobsetptr->cob_user_name == NULL) {
set_config_val_by_name (_("unknown"), "username", "cob_init()");
}
#endif
}
/* This must be last in this function as we do early return */
/* from certain ifdef's */
#ifdef _WIN32
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = GetModuleFileNameA (NULL, s, COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
#elif defined (HAVE_READLINK)
path = NULL;
if (!access ("/proc/self/exe", R_OK)) {
path = "/proc/self/exe";
} else if (!access ("/proc/curproc/file", R_OK)) {
path = "/proc/curproc/file";
} else if (!access ("/proc/self/path/a.out", R_OK)) {
path = "/proc/self/path/a.out";
}
if (path) {
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = (int)readlink (path, s, (size_t)COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
s[i] = 0;
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
}
#endif
#ifdef HAVE_GETEXECNAME
path = getexecname ();
if (path) {
#ifdef HAVE_REALPATH
s = cob_malloc ((size_t)COB_LARGE_BUFF);
if (realpath (path, s) != NULL) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
} else {
cobglobptr->cob_main_argv0 = cob_strdup (path);
}
cob_free (s);
#else
cobglobptr->cob_main_argv0 = cob_strdup (path);
#endif
return;
}
#endif
if (argc && argv && argv[0]) {
#if defined (HAVE_CANONICALIZE_FILE_NAME)
/* Returns malloced path or NULL */
cobglobptr->cob_main_argv0 = canonicalize_file_name (argv[0]);
#elif defined (HAVE_REALPATH)
s = cob_malloc ((size_t)COB_LARGE_BUFF);
if (realpath (argv[0], s) != NULL) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
}
cob_free (s);
#elif defined (_WIN32)
/* Returns malloced path or NULL */
cobglobptr->cob_main_argv0 = _fullpath (NULL, argv[0], 1);
#endif
if (!cobglobptr->cob_main_argv0) {
cobglobptr->cob_main_argv0 = cob_strdup (argv[0]);
}
} else {
cobglobptr->cob_main_argv0 = cob_strdup (_("unknown"));
}
/* The above must be last in this function as we do early return */
/* from certain ifdef's */
}
/*
* Set special runtime options:
* Currently this is only FILE * for trace and printer output
* or to reload the runtime configuration after changing environment
*/
void
cob_set_runtime_option (enum cob_runtime_option_switch opt, void *p)
{
switch (opt) {
case COB_SET_RUNTIME_TRACE_FILE:
cobsetptr->cob_trace_file = (FILE *)p;
if (p) {
cobsetptr->external_trace_file = 1;
} else {
cobsetptr->external_trace_file = 0;
}
break;
case COB_SET_RUNTIME_DISPLAY_PRINTER_FILE:
/* note: if set cob_display_print_file is always external */
cobsetptr->cob_display_print_file = (FILE *)p;
break;
case COB_SET_RUNTIME_DISPLAY_PUNCH_FILE:
/* note: if set cob_display_punch_file is always external */
if (cobsetptr->cob_display_punch_filename != NULL) {
/* if previously opened by libcob: close and free pointer to filename */
if (cobsetptr->cob_display_punch_file != NULL) {
fclose (cobsetptr->cob_display_punch_file);
}
cob_free (cobsetptr->cob_display_punch_filename);
cobsetptr->cob_display_punch_filename = NULL;
}
cobsetptr->cob_display_punch_file = (FILE *)p;
break;
case COB_SET_RUNTIME_DUMP_FILE:
/* note: if set cob_dump_file is always external (libcob only opens it on abort)
therefore we don't need to close the old one */
cobsetptr->cob_dump_file = (FILE *)p;
if (!cobsetptr->cob_dump_file) {
if (cobsetptr->cob_dump_filename) {
cob_free (cobsetptr->cob_dump_filename);
}
cobsetptr->cob_dump_filename = cob_strdup ("NONE");
}
break;
case COB_SET_RUNTIME_RESCAN_ENV:
cob_rescan_env_vals ();
break;
default:
cob_runtime_warning (_("%s called with unknown option: %d"),
"cob_set_runtime_option", opt);
}
return;
}
/*
* Return current value of special runtime options
*/
void *
cob_get_runtime_option (enum cob_runtime_option_switch opt)
{
switch (opt) {
case COB_SET_RUNTIME_TRACE_FILE:
return (void*)cobsetptr->cob_trace_file;
case COB_SET_RUNTIME_DISPLAY_PRINTER_FILE:
return (void*)cobsetptr->cob_display_print_file;
case COB_SET_RUNTIME_DISPLAY_PUNCH_FILE:
/* only externalize if not aquired by libcob */
if (cobsetptr->cob_display_punch_filename != NULL) {
return NULL;
}
return (void*)cobsetptr->cob_display_punch_file;
case COB_SET_RUNTIME_DUMP_FILE:
return (void*)cobsetptr->cob_dump_file;
default:
cob_runtime_error (_("%s called with unknown option: %d"),
"cob_get_runtime_option", opt);
}
return NULL;
}
/* output the COBOL-view of the stacktrace to the given target,
does an early exit if 'target' is NULL,
'target' is FILE * and should be flushed before */
void
cob_stack_trace (void *target)
{
if (target == NULL || !cobglobptr || !COB_MODULE_PTR) {
return;
}
dump_trace_started |= DUMP_TRACE_ACTIVE_TRACE;
cob_stack_trace_internal ((FILE *)target, 1, 0);
dump_trace_started ^= DUMP_TRACE_ACTIVE_TRACE;
}
static void
flush_target (FILE* target)
{
if (target == stderr
|| target == stdout) {
fflush (stdout);
fflush (stderr);
} else {
fflush (target);
}
}
/* output the COBOL-view of the stacktrace to the given target,
does an early exit if 'target' is NULL,
'target' is FILE *, output similar to GDBs backtrace command,
"count" to limit to the first / last entries,
REMARK: other than in GDB 0 means "full output" */
void
cob_backtrace (void *target, int count)
{
if (target == NULL) {
return;
}
if (!cobglobptr || !COB_MODULE_PTR) {
flush_target (target);
fputc (' ', target);
/* TRANSLATORS: This msgid is shown for a requested but empty stack trace. */
fputs (_("No COBOL runtime elements on stack."), target);
fputc ('\n', target);
return;
}
dump_trace_started |= DUMP_TRACE_ACTIVE_TRACE;
cob_stack_trace_internal ((FILE *)target, 0, count);
dump_trace_started ^= DUMP_TRACE_ACTIVE_TRACE;
}
/* internal output the procedure stack entry to the given target */
static void
output_procedure_stack_entry (const int file_no,
const char *section, const char *paragraph,
const char *source_file, const unsigned int source_line)
{
if (!section && !paragraph) {
return;
}
write_or_return_arr (file_no, "\n\t");
if (section && paragraph) {
write_or_return_str (file_no, paragraph);
write_or_return_arr (file_no, " OF ");
write_or_return_str (file_no, section);
} else {
if (section) {
write_or_return_str (file_no, section);
} else {
write_or_return_str (file_no, paragraph);
}
}
write_or_return_arr (file_no, " at ");
write_or_return_str (file_no, source_file);
write_or_return_arr (file_no, ":");
write_or_return_int (file_no, (int)source_line);
}
/* internal output the COBOL-view of the stacktrace to the given target */
void
cob_stack_trace_internal (FILE *target, int verbose, int count)
{
cob_module *mod;
int first_entry = 0;
int i, k;
int file_no;
/* exit early in the case of no module loaded at all,
possible to happen for example when aborted from cob_check_version of first module */
if (!COB_MODULE_PTR
|| ( COB_MODULE_PTR->module_stmt == 0
&& COB_MODULE_PTR->next == NULL)) {
return;
}
if (target == stderr) {
file_no = STDERR_FILENO;
} else {
flush_target (target);
file_no = fileno (target);
}
k = 0;
if (count < 0) {
for (mod = COB_MODULE_PTR, i = 0; mod; mod = mod->next, i++) {
if (mod->next == mod
|| k++ == MAX_MODULE_ITERS) {
break; /* messages in same checks below */
}
}
first_entry = i + count;
}
if (verbose) {
write_or_return_arr (file_no, "\n");
}
k = 0;
for (mod = COB_MODULE_PTR, i = 0; mod; mod = mod->next, i++) {
if (i < first_entry) {
continue;
}
if (count > 0 && count == i) {
break;
}
write_or_return_arr (file_no, " ");
if (mod->module_stmt != 0
&& mod->module_sources) {
const unsigned int source_file_num = COB_GET_FILE_NUM (mod->module_stmt);
const unsigned int source_line = COB_GET_LINE_NUM (mod->module_stmt);
const char *source_file = mod->module_sources[source_file_num];
if (!verbose) {
write_or_return_str (file_no, mod->module_name);
write_or_return_arr (file_no, " at ");
write_or_return_str (file_no, source_file);
write_or_return_arr (file_no, ":");
write_or_return_int (file_no, (int)source_line);
} else
if (mod->statement == STMT_UNKNOWN
&& !mod->section_name
&& !mod->paragraph_name) {
/* GC 3.1 output, now used for "no source location / no trace" case */
write_or_return_arr (file_no, "Last statement of ");
if (mod->module_type == COB_MODULE_TYPE_FUNCTION) {
write_or_return_arr (file_no, "FUNCTION ");
}
write_or_return_arr (file_no, "\"");
write_or_return_str (file_no, mod->module_name);
write_or_return_arr (file_no, "\" was at line ");
write_or_return_int (file_no, (int)source_line);
write_or_return_arr (file_no, " of ");
write_or_return_str (file_no, source_file);
} else
if (!mod->section_name && !mod->paragraph_name) {
/* special case: there _would_ be data,
but there's no procedure defined in the program */
write_or_return_arr (file_no, "Last statement of ");
if (mod->module_type == COB_MODULE_TYPE_FUNCTION) {
write_or_return_arr (file_no, "FUNCTION ");
}
write_or_return_arr (file_no, "\"");
write_or_return_str (file_no, mod->module_name);
write_or_return_arr (file_no, "\" was ");
write_or_return_str (file_no, cob_statement_name[mod->statement]);
write_or_return_arr (file_no, " at line ");
write_or_return_int (file_no, (int)source_line);
write_or_return_arr (file_no, " of ");
write_or_return_str (file_no, source_file);
} else {
/* common case when compiled with runtime checks enabled: statement and
procedure known - the later is printed from the stack entry with the
source location by the following call */
write_or_return_arr (file_no, "Last statement of ");
if (mod->module_type == COB_MODULE_TYPE_FUNCTION) {
write_or_return_arr (file_no, "FUNCTION ");
}
write_or_return_arr (file_no, "\"");
write_or_return_str (file_no, mod->module_name);
write_or_return_arr (file_no, "\" was ");
write_or_return_str (file_no, cob_statement_name[mod->statement]);
}
output_procedure_stack_entry (file_no, mod->section_name, mod->paragraph_name,
source_file, source_line);
if (mod->frame_ptr) {
struct cob_frame_ext *perform_ptr = mod->frame_ptr;
int frame_max = 512; /* max from -fstack-size */
while (frame_max--) {
const unsigned int ffile_num = COB_GET_FILE_NUM (perform_ptr->module_stmt);
const unsigned int fline = COB_GET_LINE_NUM (perform_ptr->module_stmt);
const char *ffile = mod->module_sources[ffile_num];
if (perform_ptr->section_name) {
/* marker for "root frame" - at ENTRY */
if (perform_ptr->section_name[0] == 0) {
write_or_return_arr (file_no, "\n\tENTRY ");
write_or_return_str (file_no, perform_ptr->paragraph_name);
write_or_return_arr (file_no, " at ");
write_or_return_str (file_no, ffile);
write_or_return_arr (file_no, ":");
write_or_return_int (file_no, (int)fline);
break;
}
}
output_procedure_stack_entry (file_no,
perform_ptr->section_name, perform_ptr->paragraph_name,
ffile, fline);
perform_ptr--;
}
}
} else {
if (verbose) {
write_or_return_arr (file_no, "Last statement of ");
if (mod->module_type == COB_MODULE_TYPE_FUNCTION) {
write_or_return_arr (file_no, "FUNCTION ");
}
write_or_return_arr (file_no, "\"");
write_or_return_str (file_no, mod->module_name);
if (mod->statement != STMT_UNKNOWN) {
write_or_return_arr (file_no, "\" was ");
write_or_return_str (file_no, cob_statement_name[mod->statement]);
} else {
write_or_return_arr (file_no, "\" unknown");
}
} else {
write_or_return_str (file_no, mod->module_name);
write_or_return_arr (file_no, " at unknown");
}
}
write_or_return_arr (file_no, "\n");
if (mod->next == mod) {
/* not translated as highly unexpected */
write_or_return_arr (file_no, "FIXME: recursive mod (stack trace)\n");
break;
}
if (k++ == MAX_MODULE_ITERS) {
/* not translated as highly unexpected */
write_or_return_arr (file_no,
"max module iterations exceeded, possible broken chain\n");
break;
}
}
if (mod) {
write_or_return_arr (file_no, " ");
write_or_return_str (file_no, more_stack_frames_msgid);
write_or_return_arr (file_no, "\n");
}
if (verbose && cob_argc != 0) {
size_t ia;
write_or_return_arr (file_no, " Started by ");
write_or_return_str (file_no, cob_argv[0]);
write_or_return_arr (file_no, "\n");
for (ia = 1; ia < (size_t)cob_argc; ++ia) {
write_or_return_arr (file_no, "\t");
write_or_return_str (file_no, cob_argv[ia]);
write_or_return_arr (file_no, "\n");
}
}
}
FILE *
cob_get_dump_file (void)
{
#if 1 /* new version as currently only COB_DUMP_TO_FILE is used */
if (cobsetptr->cob_dump_file != NULL) { /* If DUMP active, use that */
return cobsetptr->cob_dump_file;
} else if (cobsetptr->cob_dump_filename != NULL) { /* DUMP file defined */
if (cob_check_env_false(cobsetptr->cob_dump_filename)) {
return NULL;
}
cobsetptr->cob_dump_file = cob_open_logfile (cobsetptr->cob_dump_filename);
if (cobsetptr->cob_dump_file != NULL) {
return cobsetptr->cob_dump_file;
}
/* could not open the file
unset the filename for not referencing it later */
cob_free (cobsetptr->cob_dump_filename);
cobsetptr->cob_dump_filename = NULL;
/* Fall-through */
}
if (cobsetptr->cob_trace_file != NULL) { /* If TRACE active, use that */
return cobsetptr->cob_trace_file;
} else {
return stderr;
}
#else /* currently only COB_DUMP_TO_FILE used */
FILE *fp;
if (where == COB_DUMP_TO_FILE) {
fp = cobsetptr->cob_dump_file;
if (fp == NULL) {
if(cobsetptr->cob_trace_file != NULL) { /* If TRACE active, use that */
fp = cobsetptr->cob_trace_file;
} else if(cobsetptr->cob_dump_filename != NULL) { /* Dump file defined */
if (cob_check_env_false(cobsetptr->cob_dump_filename)) {
return NULL;
}
fp = fopen(cobsetptr->cob_dump_filename, "a");
if(fp == NULL)
fp = stderr;
cobsetptr->cob_dump_file = fp;
} else {
fp = stderr;
}
}
} else if (where == COB_DUMP_TO_PRINT) {
fp = cobsetptr->cob_display_print_file;
if (fp == NULL) {
if(cobsetptr->cob_trace_file != NULL) { /* If TRACE active, use that */
fp = cobsetptr->cob_trace_file;
} else {
fp = stdout;
}
}
} else {
fp = stderr;
}
return fp;
#endif
}
static void
cob_dump_module (char *reason)
{
cob_module *mod;
int wants_dump = 0;
int k;
/* Was any module compiled with -fdump? */
k = 0;
for (mod = COB_MODULE_PTR; mod; mod = mod->next) {
if (mod->flag_dump_ready) {
wants_dump = 1;
}
if (mod->next == mod) {
/* not translated as highly unexpected */
fputs ("FIXME: recursive mod (module dump)\n", stderr);
break;
}
if (k++ == MAX_MODULE_ITERS) {
/* not translated as highly unexpected */
fputs ("max module iterations exceeded, possible broken chain\n", stderr);
break;
}
if (mod->flag_dump_ready) {
break;
}
}
if (wants_dump) {
FILE *fp;
char *previous_locale = NULL;
#if 1 /* new version as currently only COB_DUMP_TO_FILE is used */
fp = cob_get_dump_file ();
#else
fp = cob_get_dump_file (COB_DUMP_TO_FILE);
#endif
/* explicit disabled dump */
if (fp == NULL) {
return;
}
if (fp != stderr) {
if (reason) {
if (reason[0] == 0) {
reason = (char *)_ ("unknown");
}
fputc ('\n', fp);
fprintf (fp, _("Module dump due to %s"), reason);
fputc ('\n', fp);
}
if (fp != stdout) {
/* was already sent to stderr before this function was called,
so skip here for stdout/stderr ... */
if (!(dump_trace_started & DUMP_TRACE_ACTIVE_TRACE)) {
dump_trace_started |= DUMP_TRACE_ACTIVE_TRACE;
cob_stack_trace_internal (fp, 1, 0);
dump_trace_started ^= DUMP_TRACE_ACTIVE_TRACE;
}
}
fflush (stdout);
} else {
fflush (stderr);
}
fputc ('\n', fp);
if (cobglobptr->cob_locale_ctype) {
previous_locale = setlocale (LC_CTYPE, NULL);
setlocale (LC_CTYPE, cobglobptr->cob_locale_ctype);
}
k = 0;
for (mod = COB_MODULE_PTR; mod; mod = mod->next) {
if (mod->module_cancel.funcint) {
int (*cancel_func)(const int);
cancel_func = mod->module_cancel.funcint;
fprintf (fp, _("Dump Program-Id %s from %s compiled %s"),
mod->module_name, mod->module_source, mod->module_formatted_date);
fputc ('\n', fp);
(void)cancel_func (-10);
fputc ('\n', fp);
}
if (mod->next == mod
|| k++ == MAX_MODULE_ITERS) {
break;
}
}
if (previous_locale) {
setlocale (LC_CTYPE, previous_locale);
}
if (fp != stdout && fp != stderr) {
char * fname = NULL;
if (cobsetptr->cob_dump_filename) {
fname = cobsetptr->cob_dump_filename;
} else
if (cobsetptr->cob_trace_file == fp
&& cobsetptr->cob_trace_filename != NULL
&& !cobsetptr->external_trace_file) {
fname = cobsetptr->cob_trace_filename;
}
if (fname != NULL) {
fputc ('\n', stderr);
fprintf (stderr, _("dump written to %s"), fname);
fputc ('\n', stderr);
fflush (stderr);
}
}
}
}
#ifdef COB_DEBUG_LOG
/******************************/
/* Routines for COB_DEBUG_LOG */
/******************************/
/* Check env var value and open log file */
/*
* Env var is COB_DEBUG_LOG
* Env Var string is a series of keyword=value parameters where keywords:
* L=x - options: T for trace level, W for warnings, N for normal, A for ALL
* M=yy - module: RW for report writer, the 2 char code is tabled and compared
* with the value coded on DEBUG_LOG("yy",("format",args));
* O=path/file - file name to write log data to, default is: cob_debug_log.$$
* note: replacements already done in common setting handling
*/
void
cob_debug_open (void)
{
char *debug_env = cobsetptr->cob_debug_log;
int i, j;
char module_name[4];
char log_opt;
char logfile[COB_SMALL_BUFF];
logfile[0] = 0;
for (i=0; debug_env[i] != 0; i++) {
/* skip separator */
if (debug_env[i] == ','
|| debug_env[i] == ';')
continue;
/* debugging flags (not include in file name) */
if (debug_env[i + 1] == '=') {
log_opt = debug_env[i];
i += 2;
switch (log_opt) {
case 'M': /* module to debug */
case 'm':
for (j = 0; j < 4; i++) {
if (debug_env[i] == ','
|| debug_env[i] == ';'
|| debug_env[i] == 0) {
break;
}
module_name[j++] = debug_env[i];
}
module_name[j] = 0;
/* note: special module ALL is checked later */
for (j = 0; j < 12 && cob_debug_modules[j][0] > ' '; j++) {
if (strcasecmp (cob_debug_modules[j], module_name) == 0) {
break;
}
}
if (j < 12 && cob_debug_modules[j][0] == ' ') {
strcpy (cob_debug_modules[j], module_name);
}
if (debug_env[i] == 0) i--;
break;
case 'L': /* logging options */
case 'l':
log_opt = debug_env[i];
switch (log_opt) {
case 'T': /* trace */
case 't':
cob_debug_log_time = cob_debug_level = 3;
break;
case 'W': /* warnings */
case 'w':
cob_debug_level = 2;
break;
case 'N': /* normal */
case 'n':
cob_debug_level = 0;
break;
case 'A': /* all */
case 'a':
cob_debug_level = 9;
break;
default: /* Unknown log option, just ignored for now */
i--;
break;
}
break;
case 'O': /* output name for logfile */
case 'o':
for (j = 0; j < COB_SMALL_MAX; i++) {
if (debug_env[i] == ','
|| debug_env[i] == ';'
|| debug_env[i] == 0) {
break;
}
logfile[j++] = debug_env[i];
}
logfile[j] = 0;
if (debug_env[i] == 0) i--;
break;
default: /* Unknown x=, just ignored for now */
break;
}
} else {
/* invalid character, just ignored for now */
/* note: this allows for L=WARNING (but also for L=WUMPUS) */
}
}
/* set default logfile if not given */
if (logfile[0] == 0) {
sprintf (logfile, "cob_debug_log.%d", cob_sys_getpid());
}
/* store filename for possible unlink (empty log file) */
cob_debug_file_name = cob_strdup (logfile);
/* ensure trace file is open if we use this as debug log and exit */
if (cobsetptr->cob_trace_filename &&
strcmp (cobsetptr->cob_trace_filename, cob_debug_file_name) == 0) {
cob_check_trace_file ();
cob_debug_file = cobsetptr->cob_trace_file;
return;
}
/* open logfile */
cob_debug_file = cob_open_logfile (cob_debug_file_name);
if (cob_debug_file == NULL) {
/* developer-only msg - not translated */
cob_runtime_error ("error '%s' opening COB_DEBUG_LOG '%s', resolved from '%s'",
cob_get_strerror (), cob_debug_file_name, cobsetptr->cob_debug_log);
return;
}
}
/* Determine if DEBUGLOG is to be allowed */
int
cob_debug_logit (int level, char *module)
{
int i;
if (cob_debug_file == NULL) {
return 1;
}
if (level > cob_debug_level) {
return 1;
}
for (i=0; i < 12 && cob_debug_modules[i][0] > ' '; i++) {
if (strcasecmp ("ALL", cob_debug_modules[i]) == 0) {
cob_debug_mod = (char*)module;
return 0; /* Logging is allowed */
}
if (strcasecmp (module,cob_debug_modules[i]) == 0) {
cob_debug_mod = (char*)&cob_debug_modules[i];
return 0; /* Logging is allowed */
}
}
return 1;
}
/* Write logging line */
static int cob_debug_hdr = 1;
static unsigned int cob_debug_prv_line = 0;
int
cob_debug_logger (const char *fmt, ...)
{
va_list ap;
int ln;
struct cob_time time;
if (cob_debug_file == NULL) {
return 0;
}
if (*fmt == '~') { /* Force line# out again to log file */
fmt++;
cob_debug_prv_line = -1;
cob_debug_hdr = 1;
}
if (cob_debug_hdr) {
cob_get_source_line ();
if (cob_debug_log_time) {
time = cob_get_current_datetime (DTR_FULL);
fprintf (cob_debug_file, "%02d:%02d:%02d.%02d ", time.hour, time.minute,
time.second, time.nanosecond / 10000000);
}
if (cob_debug_mod) {
fprintf (cob_debug_file, "%-3s:", cob_debug_mod);
}
if (cob_source_file) {
fprintf (cob_debug_file, " %s :", cob_source_file);
}
if (cob_source_line && cob_source_line != cob_debug_prv_line) {
fprintf (cob_debug_file, "%5d : ", cob_source_line);
cob_debug_prv_line = cob_source_line;
} else {
fprintf (cob_debug_file, "%5s : ", " ");
}
cob_debug_hdr = 0;
}
va_start (ap, fmt);
vfprintf (cob_debug_file, fmt, ap);
va_end (ap);
ln = strlen(fmt);
if (fmt[ln-1] == '\n') {
cob_debug_hdr = 1;
fflush (cob_debug_file);
}
return 0;
}
static int /* Return TRUE if word is repeated 16 times */
repeatWord(
char *match, /* 4 bytes to match */
char *mem) /* Memory area to match repeated value */
{
if(memcmp(match, &mem[0], 4) == 0
&& memcmp(match, &mem[4], 4) == 0
&& memcmp(match, &mem[8], 4) == 0
&& memcmp(match, &mem[12], 4) == 0)
return 1;
return 0;
}
/* Hexdump of memory */
int
cob_debug_dump (void *pMem, int len)
{
#define dMaxPerLine 24
#define dMaxHex ((dMaxPerLine*2)+(dMaxPerLine/4-1))
register int i, j, k;
register char c, *mem = pMem;
char lastWord[4];
char hex[dMaxHex+4],chr[dMaxPerLine+4];
int adrs = 0;
if (cob_debug_file == NULL)
return 0;
memset (lastWord,0xFD, 4);
for (i=0; i < len; ) {
for (j=k=0; j < dMaxPerLine && (i+j) < len; j++) {
k += sprintf(&hex[k],"%02X",mem[i+j]&0xFF);
if ((j % 4) == 3 )
hex[k++] = ' ';
}
if (k && hex[k-1] == ' ')
hex[k-1] = 0;
hex[k] = 0;
k = 0;
for (j=0; j<dMaxPerLine && (i+j)<len; j++) {
c = mem[i+j];
chr[k++] = c >= ' ' && c < 0x7f ? c : '.';
}
chr[k++] = 0;
fprintf (cob_debug_file," %6.6X : %-*s '%s'\n",adrs+i,dMaxHex,hex,chr);
if ((i + dMaxPerLine) < len )
memcpy( (char *)lastWord, (char *)&mem[i+dMaxPerLine-4], j<4?j:4);
i += dMaxPerLine;
if( (i + (16*2)) < len
&& repeatWord (lastWord, &mem[i])
&& repeatWord (lastWord, &mem[i+dMaxPerLine])) {
fprintf (cob_debug_file," %6.6X : ",adrs+i);
while (i < len - 16
&& repeatWord(lastWord,&mem[i]))
i += 16;
fprintf (cob_debug_file," thru %6.6X same as last word\n",adrs+i-1);
}
}
fflush (cob_debug_file);
return 0;
}
#endif /* end of 'COB_DEBUG_LOG' */
#ifndef HAVE_DESIGNATED_INITS
void
init_statement_list (void)
{
cob_statement_name[STMT_UNKNOWN] = "UNKNOWN";
#define COB_STATEMENT(ename,str) \
cob_statement_name[ename] = str;
#include "statement.def" /* located and installed next to common.h */
#undef COB_STATEMENT
}
#endif
|