about summary refs log tree commit diff stats
path: root/BUILD/VS2003/lynx/lynx_cfg.h
diff options
context:
space:
mode:
authorThomas E. Dickey <dickey@invisible-island.net>2024-03-21 08:19:13 +0000
committerThomas E. Dickey <dickey@invisible-island.net>2024-03-21 08:20:16 +0000
commit3d3885fb9f3b182a8173a424622a46ac3b9b4a34 (patch)
treeea31000ebe64e3d62146e0e9e46cac706d9acf3f /BUILD/VS2003/lynx/lynx_cfg.h
parent1743c6cf472c71de8ecbcbcfed7966be34a11743 (diff)
downloadlynx-snapshots-3d3885fb9f3b182a8173a424622a46ac3b9b4a34.tar.gz
snapshot of project "lynx", label v2-9-0e
Diffstat (limited to 'BUILD/VS2003/lynx/lynx_cfg.h')
0 files changed, 0 insertions, 0 deletions
n104'>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
/*
 * $LynxId: HTVMS_WaisUI.c,v 1.21 2020/01/21 22:00:50 tom Exp $
 *								HTVMS_WAISUI.c
 *
 *	Adaptation for Lynx by F.Macrides (macrides@sci.wfeb.edu)
 *
 *	30-May-1994 FM	Initial version.
 *
 *----------------------------------------------------------------------*/

/*
 *	Routines originally from UI.c -- FM
 *
 *----------------------------------------------------------------------*/
/* WIDE AREA INFORMATION SERVER SOFTWARE:
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * Brewster@think.com
 */

/*
 * this is a simple ui toolkit for building other ui's on top.
 * -brewster
 *
 * top level functions:
 *   generate_search_apdu
 *   generate_retrieval_apdu
 *   interpret_message
 *
 */

/* to do:
 *   generate multiple queries for long documents.
 *     this will crash if the file being retrieved is larger than 100k.
 *   do log_write()
 *
 */

#include <HTUtils.h>

#ifdef VMS
#include <HTVMS_WaisUI.h>
#include <HTVMS_WaisProt.h>
#include <HTTCP.h>

#undef MAXINT			/* we don't need it here, and www_tcp.h may conflict */
#include <math.h>

#include <LYexit.h>
#include <LYLeaks.h>

void log_write(char *s GCC_UNUSED)
{
    return;
}

/*----------------------------------------------------------------------*/

/* returns a pointer in the buffer of the first free byte.
   if it overflows, then NULL is returned
 */
char *generate_search_apdu(char *buff,	/* buffer to hold the apdu */
			   long *buff_len,	/* length of the buffer changed to reflect new data written */
			   char *seed_words,	/* string of the seed words */
			   char *database_name,
			   DocObj **docobjs,
			   long maxDocsRetrieved)
{
    /* local variables */

    SearchAPDU *search3;
    char *end_ptr;
    static char *database_names[2] =
    {"", 0};
    any refID;
    WAISSearch *query;

    refID.size = 1;
    refID.bytes = "3";

    database_names[0] = database_name;
    query = makeWAISSearch(seed_words,
			   docobjs,	/* DocObjsPtr */
			   0,
			   1,	/* DateFactor */
			   0,	/* BeginDateRange */
			   0,	/* EndDateRange */
			   maxDocsRetrieved
	);

    search3 = makeSearchAPDU(30,
			     5000,	/* should be large */
			     30,
			     1,	/* replace indicator */
			     "",	/* result set name */
			     database_names,	/* database name */
			     QT_RelevanceFeedbackQuery,		/* query_type */
			     0,	/* element name */
			     NULL,	/* reference ID */
			     query);

    end_ptr = writeSearchAPDU(search3, buff, buff_len);

    CSTFreeWAISSearch(query);
    freeSearchAPDU(search3);
    return (end_ptr);
}

/*----------------------------------------------------------------------*/

/* returns a pointer into the buffer of the next free byte.
   if it overflowed, then NULL is returned
 */

char *generate_retrieval_apdu(char *buff,
			      long *buff_len,	/* length of the buffer changed to reflect new data written */
			      any *docID,
			      long chunk_type,
			      long start,
			      long end,
			      char *type,
			      char *database_name)
{
    SearchAPDU *search;
    char *end_ptr;

    static char *database_names[2];
    static char *element_names[3];
    any refID;

    DocObj *DocObjs[2];
    any *query;			/* changed from char* by brewster */

    if (NULL == type)
	type = s_strdup("TEXT");

    database_names[0] = database_name;
    database_names[1] = NULL;

    element_names[0] = " ";
    element_names[1] = ES_DocumentText;
    element_names[2] = NULL;

    refID.size = 1;
    refID.bytes = "3";

    switch (chunk_type) {
    case CT_line:
	DocObjs[0] = makeDocObjUsingLines(docID, type, start, end);
	break;
    case CT_byte:
	DocObjs[0] = makeDocObjUsingBytes(docID, type, start, end);
	break;
    }
    DocObjs[1] = NULL;

    query = makeWAISTextQuery(DocObjs);
    search = makeSearchAPDU(10, 16, 15,
			    1,	/* replace indicator */
			    "FOO",	/* result set name */
			    database_names,	/* database name */
			    QT_TextRetrievalQuery,	/* query_type */
			    element_names,	/* element name */
			    &refID,	/* reference ID */
			    query);
    end_ptr = writeSearchAPDU(search, buff, buff_len);
    CSTFreeWAISTextQuery(query);
    freeSearchAPDU(search);
    return (end_ptr);
}

/*----------------------------------------------------------------------*/

/* this is a safe version of unix 'read' it does all the checking
 * and looping necessary
 * to those trying to modify the transport code to use non-UNIX streams:
 *  This is the function to modify!
 */
static long read_from_stream(int d, char *buf, long nbytes)
{
    long didRead;
    long toRead = nbytes;
    long totalRead = 0;		/* paranoia */

    while (toRead > 0) {
	didRead = NETREAD(d, buf, (int) toRead);
	if (didRead == HT_INTERRUPTED)
	    return (HT_INTERRUPTED);
	if (didRead == -1)	/* error */
	    return (-1);
	if (didRead == 0)	/* eof */
	    return (-2);	/* maybe this should return 0? */
	toRead -= didRead;
	buf += didRead;
	totalRead += didRead;
    }
    if (totalRead != nbytes)	/* we overread for some reason */
	return (-totalRead);	/* bad news */
    return (totalRead);
}

/*----------------------------------------------------------------------*/

/* returns the length of the response, 0 if an error */

static long transport_message(long connection,
			      char *request_message,
			      long request_length,
			      char *response_message,
			      long response_buffer_length)
{
    WAISMessage header;
    long response_length;
    int rv;

    /* Write out message.  Read back header.  Figure out response length. */

    if (request_length + HEADER_LENGTH !=
	NETWRITE(connection, request_message,
		 (int) (request_length + HEADER_LENGTH)))
	return 0;

    /* read for the first '0' */

    while (1) {
	rv = read_from_stream(connection, response_message, 1);
	if (rv == HT_INTERRUPTED)
	    return HT_INTERRUPTED;
	if (rv < 0)
	    return 0;
	if ('0' == response_message[0])
	    break;
    }

    rv = read_from_stream(connection, response_message + 1, HEADER_LENGTH - 1);
    if (rv == HT_INTERRUPTED)
	return HT_INTERRUPTED;
    if (rv < 0)
	return 0;

    readWAISPacketHeader(response_message, &header);
    {
	char length_array[11];

	LYStrNCpy(length_array, header.msg_len, 10);
	response_length = atol(length_array);
	/*
	   if(verbose){
	   printf("WAIS header: '%s' length_array: '%s'\n",
	   response_message, length_array);
	   }
	 */
	if (response_length > response_buffer_length) {
	    /* we got a message that is too long, therefore empty the message out,
	       and return 0 */
	    long i;

	    for (i = 0; i < response_length; i++) {
		rv = read_from_stream(connection,
				      response_message + HEADER_LENGTH,
				      1);
		if (rv == HT_INTERRUPTED)
		    return HT_INTERRUPTED;
		if (rv < 0)
		    return 0;
	    }
	    return (0);
	}
    }
    rv = read_from_stream(connection,
			  response_message + HEADER_LENGTH,
			  response_length);
    if (rv == HT_INTERRUPTED)
	return HT_INTERRUPTED;
    if (rv < 0)
	return 0;
    return (response_length);
}

/*----------------------------------------------------------------------*/

/* returns the number of bytes written.  0 if an error */
long interpret_message(char *request_message,
		       long request_length,	/* length of the buffer */
		       char *response_message,
		       long response_buffer_length,
		       long connection,
		       boolean verbose GCC_UNUSED)
{
    long response_length;

    /* ?
       if(verbose){
       printf ("sending");
       if(hostname_internal && strlen(hostname_internal) > 0)
       printf(" to host %s", hostname_internal);
       if(service_name && strlen(service_name) > 0)
       printf(" for service %s", service_name);
       printf("\n");
       twais_dsply_rsp_apdu(request_message + HEADER_LENGTH,
       request_length);
       }

     */

    writeWAISPacketHeader(request_message,
			  request_length,
			  (long) 'z',	/* Z39.50 */
			  "wais      ",		/* server name */
			  (long) NO_COMPRESSION,	/* no compression */
			  (long) NO_ENCODING, (long) HEADER_VERSION);
    if (connection != 0) {
	response_length = transport_message(connection, request_message,
					    request_length,
					    response_message,
					    response_buffer_length);
	if (response_length == HT_INTERRUPTED)
	    return (HT_INTERRUPTED);
    } else
	return (0);

    return (response_length);
}

/*----------------------------------------------------------------------*/

/* modifies the string to exclude all seeker codes. sets length to
   the new length. */
static char *delete_seeker_codes(char *string, long *length)
{
    long original_count;	/* index into the original string */
    long new_count = 0;		/* index into the collapsed string */

    for (original_count = 0; original_count < *length; original_count++) {
	if (27 == string[original_count]) {
	    /* then we have an escape code */
	    /* if the next letter is '(' or ')', then ignore two letters */
	    if ('(' == string[original_count + 1] ||
		')' == string[original_count + 1])
		original_count += 1;	/* it is a term marker */
	    else
		original_count += 4;	/* it is a paragraph marker */
	} else
	    string[new_count++] = string[original_count];
    }
    *length = new_count;
    return (string);
}

/*----------------------------------------------------------------------*/

#if defined(VMS) && defined(__GNUC__)	/* 10-AUG-1995 [pr] */
/*
  Workaround for an obscure bug in gcc's 2.6.[123] and 2.7.0 vax/vms port;
  sometimes global variables will end up not being defined properly,
  causing first gas to assume they're routines, then the linker to complain
  about unresolved symbols, and finally the program to reference the wrong
  objects (provoking ACCVIO).  It's triggered by the specific ordering of
  variable usage in the source code, hence rarely appears.  This bug is
  fixed in gcc 2.7.1, and was not present in 2.6.0 and earlier.

   Make a reference to VAXCRTL's _ctype_[], and also one to this dummy
   variable itself to prevent any "defined but not used" warning.
 */
static __const void *__const ctype_dummy[] =
{&_ctype_, &ctype_dummy};
#endif /* VMS && __GNUC__ */

/* returns a pointer to a string with good stuff */
char *trim_junk(char *headline)
{
    long length = strlen(headline) + 1;		/* include the trailing null */
    size_t i;

    headline = delete_seeker_codes(headline, &length);
    /* delete leading spaces */
    for (i = 0; i < strlen(headline); i++) {
	if (isprint(headline[i])) {
	    break;
	}
    }
    headline = headline + i;
    /* delete trailing stuff */
    for (i = strlen(headline) - 1; i > 0; i--) {
	if (isprint(headline[i])) {
	    break;
	}
	headline[i] = '\0';
    }
    return (headline);
}

/*----------------------------------------------------------------------*/

/*
 *	Routines originally from ZProt.c -- FM
 *
 *----------------------------------------------------------------------*/
/* WIDE AREA INFORMATION SERVER SOFTWARE:`
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * 3.26.90	Harry Morris, morris@think.com
 * 3.30.90  Harry Morris - Changed any->bits to any->bytes
 * 4.11.90  HWM - generalized conditional includes (see c-dialect.h)
 */

#define RESERVE_SPACE_FOR_HEADER(spaceLeft)		\
	*spaceLeft -= HEADER_LEN;

#define RELEASE_HEADER_SPACE(spaceLeft)			\
	if (*spaceLeft > 0)				\
	  *spaceLeft += HEADER_LEN;

/*----------------------------------------------------------------------*/

InitResponseAPDU *makeInitResponseAPDU(boolean result,
				       boolean search,
				       boolean present,
				       boolean deleteIt,
				       boolean accessControl,
				       boolean resourceControl,
				       long prefSize,
				       long maxMsgSize,
				       char *auth,
				       char *id,
				       char *name,
				       char *version,
				       any *refID,
				       void *userInfo)
/* build an initResponse APDU with user specified information */
{
    InitResponseAPDU *init = (InitResponseAPDU *) s_malloc((size_t) sizeof(InitResponseAPDU));

    init->PDUType = initResponseAPDU;
    init->Result = result;
    init->willSearch = search;
    init->willPresent = present;
    init->willDelete = deleteIt;
    init->supportAccessControl = accessControl;
    init->supportResourceControl = resourceControl;
    init->PreferredMessageSize = prefSize;
    init->MaximumRecordSize = maxMsgSize;
    init->IDAuthentication = s_strdup(auth);
    init->ImplementationID = s_strdup(id);
    init->ImplementationName = s_strdup(name);
    init->ImplementationVersion = s_strdup(version);
    init->ReferenceID = duplicateAny(refID);
    init->UserInformationField = userInfo;	/* not copied! */

    return (init);
}

/*----------------------------------------------------------------------*/

void freeInitResponseAPDU(InitResponseAPDU *init)
/* free an initAPDU */
{
    s_free(init->IDAuthentication);
    s_free(init->ImplementationID);
    s_free(init->ImplementationName);
    s_free(init->ImplementationVersion);
    freeAny(init->ReferenceID);
    s_free(init);
}

/*----------------------------------------------------------------------*/

char *writeInitResponseAPDU(InitResponseAPDU *init, char *buffer, long *len)
/* write the initResponse to a buffer, adding system information */
{
    char *buf = buffer + HEADER_LEN;	/* leave room for the header-length-indicator */
    long size;
    bit_map *optionsBM = NULL;

    RESERVE_SPACE_FOR_HEADER(len);

    buf = writePDUType(init->PDUType, buf, len);
    buf = writeBoolean(init->Result, buf, len);
    buf = writeProtocolVersion(buf, len);

    optionsBM = makeBitMap((unsigned long) 5, init->willSearch, init->willPresent,
			   init->willDelete, init->supportAccessControl,
			   init->supportResourceControl);
    buf = writeBitMap(optionsBM, DT_Options, buf, len);
    freeBitMap(optionsBM);

    buf = writeNum(init->PreferredMessageSize,
		   DT_PreferredMessageSize,
		   buf,
		   len);
    buf = writeNum(init->MaximumRecordSize,
		   DT_MaximumRecordSize,
		   buf,
		   len);
    buf = writeString(init->IDAuthentication,
		      DT_IDAuthentication,
		      buf,
		      len);
    buf = writeString(init->ImplementationID,
		      DT_ImplementationID,
		      buf,
		      len);
    buf = writeString(init->ImplementationName,
		      DT_ImplementationName,
		      buf,
		      len);
    buf = writeString(init->ImplementationVersion,
		      DT_ImplementationVersion,
		      buf,
		      len);
    buf = writeAny(init->ReferenceID,
		   DT_ReferenceID,
		   buf,
		   len);

    /* go back and write the header-length-indicator */
    RELEASE_HEADER_SPACE(len);
    size = buf - buffer - HEADER_LEN;
    writeBinaryInteger(size, HEADER_LEN, buffer, len);

    if (init->UserInformationField != NULL)
	buf = writeInitResponseInfo(init, buf, len);

    return (buf);
}

/*----------------------------------------------------------------------*/

char *readInitResponseAPDU(InitResponseAPDU **init, char *buffer)
{
    char *buf = buffer;
    boolean search, present, delete, accessControl, resourceControl;
    long prefSize, maxMsgSize;
    char *auth, *id, *name, *version;
    long size;
    pdu_type pduType;
    bit_map *versionBM = NULL;
    bit_map *optionsBM = NULL;
    boolean result;
    any *refID = NULL;
    void *userInfo = NULL;

    auth = id = name = version = NULL;
    refID = NULL;

    /* read required part */
    buf = readBinaryInteger(&size, HEADER_LEN, buf);
    buf = readPDUType(&pduType, buf);
    buf = readBoolean(&result, buf);
    buf = readBitMap(&versionBM, buf);
    buf = readBitMap(&optionsBM, buf);
    buf = readNum(&prefSize, buf);
    buf = readNum(&maxMsgSize, buf);

    /* decode optionsBM */
    search = bitAtPos(0, optionsBM);
    present = bitAtPos(1, optionsBM);
    delete = bitAtPos(2, optionsBM);
    accessControl = bitAtPos(3, optionsBM);
    resourceControl = bitAtPos(4, optionsBM);

    /* read optional part */
    while (buf < (buffer + size + HEADER_LEN)) {
	data_tag tag = peekTag(buf);

	switch (tag) {
	case DT_IDAuthentication:
	    buf = readString(&auth, buf);
	    break;
	case DT_ImplementationID:
	    buf = readString(&id, buf);
	    break;
	case DT_ImplementationName:
	    buf = readString(&name, buf);
	    break;
	case DT_ImplementationVersion:
	    buf = readString(&version, buf);
	    break;
	case DT_ReferenceID:
	    buf = readAny(&refID, buf);
	    break;
	default:
	    freeBitMap(versionBM);
	    freeBitMap(optionsBM);
	    s_free(auth);
	    s_free(id);
	    s_free(name);
	    s_free(version);
	    freeAny(refID);
	    REPORT_READ_ERROR(buf);
	    break;
	}
    }

    buf = readInitResponseInfo(&userInfo, buf);
    if (buf == NULL) {
	freeBitMap(versionBM);
	freeBitMap(optionsBM);
	s_free(auth);
	s_free(id);
	s_free(name);
	s_free(version);
	freeAny(refID);
    }
    RETURN_ON_NULL(buf);

    /* construct the basic init object */
    *init = makeInitResponseAPDU(result,
				 search,
				 present,
				 delete,
				 accessControl,
				 resourceControl,
				 prefSize,
				 maxMsgSize,
				 auth,
				 id,
				 name,
				 version,
				 refID,
				 userInfo);

    freeBitMap(versionBM);
    freeBitMap(optionsBM);
    s_free(auth);
    s_free(id);
    s_free(name);
    s_free(version);
    freeAny(refID);

    return (buf);
}

/*----------------------------------------------------------------------*/

InitResponseAPDU *replyToInitAPDU(InitAPDU * init, boolean result, void *userInfo)
/* respond to an init message in the default way - echoing back
   the init info
 */
{
    InitResponseAPDU *initResp;

    initResp = makeInitResponseAPDU(result,
				    init->willSearch,
				    init->willPresent,
				    init->willDelete,
				    init->supportAccessControl,
				    init->supportResourceControl,
				    init->PreferredMessageSize,
				    init->MaximumRecordSize,
				    init->IDAuthentication,
				    defaultImplementationID(),
				    defaultImplementationName(),
				    defaultImplementationVersion(),
				    init->ReferenceID,
				    userInfo);
    return (initResp);
}

/*----------------------------------------------------------------------*/

SearchAPDU *makeSearchAPDU(long small,
			   long large,
			   long medium,
			   boolean replace,
			   char *name,
			   char **databases,
			   char *type,
			   char **elements,
			   any *refID,
			   void *queryInfo)
{
    char *ptr = NULL;
    long i;
    SearchAPDU *query = (SearchAPDU *) s_malloc((size_t) sizeof(SearchAPDU));

    query->PDUType = searchAPDU;
    query->SmallSetUpperBound = small;
    query->LargeSetLowerBound = large;
    query->MediumSetPresentNumber = medium;
    query->ReplaceIndicator = replace;
    query->ResultSetName = s_strdup(name);
    query->DatabaseNames = NULL;
    if (databases != NULL) {
	for (i = 0, ptr = databases[i]; ptr != NULL; ptr = databases[++i]) {
	    if (query->DatabaseNames == NULL)
		query->DatabaseNames = (char **) s_malloc((size_t) (sizeof(char
									   *)
								    * 2));

	    else
		query->DatabaseNames = (char **) s_realloc((char *) query->DatabaseNames,
							   (size_t) (sizeof(char
									    *) *
								     (i + 2)));

	    query->DatabaseNames[i] = s_strdup(ptr);
	    query->DatabaseNames[i + 1] = NULL;
	}
    }
    query->QueryType = s_strdup(type);
    query->ElementSetNames = NULL;
    if (elements != NULL) {
	for (i = 0, ptr = elements[i]; ptr != NULL; ptr = elements[++i]) {
	    if (query->ElementSetNames == NULL)
		query->ElementSetNames =
		    (char **) s_malloc((size_t) (sizeof(char *) * 2));

	    else
		query->ElementSetNames = (char **) s_realloc((char *) query->ElementSetNames,
							     (size_t) (sizeof(char
									      *) *
								       (i + 2)));

	    query->ElementSetNames[i] = s_strdup(ptr);
	    query->ElementSetNames[i + 1] = NULL;
	}
    }
    query->ReferenceID = duplicateAny(refID);
    query->Query = queryInfo;	/* not copied! */
    return (query);
}

/*----------------------------------------------------------------------*/

void freeSearchAPDU(SearchAPDU *query)
{
    s_free(query->ResultSetName);
    s_free(query->QueryType);
    doList((void **) query->DatabaseNames, fs_free);	/* can't use the macro here ! */
    s_free(query->DatabaseNames);
    doList((void **) query->ElementSetNames, fs_free);	/* can't use the macro here ! */
    s_free(query->ElementSetNames);
    freeAny(query->ReferenceID);
    s_free(query);
}

/*----------------------------------------------------------------------*/

#define DB_DELIMITER	"\037"	/* hex 1F occurs between each database name */
#define ES_DELIMITER_1	"\037"	/* separates database name from element name */
#define ES_DELIMITER_2	"\036"	/* hex 1E separates <db,es> groups from one another */

char *writeSearchAPDU(SearchAPDU *query, char *buffer, long *len)
{
    char *buf = buffer + HEADER_LEN;	/* leave room for the header-length-indicator */
    long size, i;
    char *ptr = NULL;
    char *scratch = NULL;

    RESERVE_SPACE_FOR_HEADER(len);

    buf = writePDUType(query->PDUType, buf, len);
    buf = writeBinaryInteger(query->SmallSetUpperBound, (size_t) 3, buf, len);
    buf = writeBinaryInteger(query->LargeSetLowerBound, (size_t) 3, buf, len);
    buf = writeBinaryInteger(query->MediumSetPresentNumber, (size_t) 3, buf, len);
    buf = writeBoolean(query->ReplaceIndicator, buf, len);
    buf = writeString(query->ResultSetName, DT_ResultSetName, buf, len);
    /* write database names */
    if (query->DatabaseNames != NULL) {
	for (i = 0, scratch = NULL, ptr = query->DatabaseNames[i]; ptr != NULL;
	     ptr = query->DatabaseNames[++i]) {
	    if (scratch == NULL)
		scratch = s_strdup(ptr);
	    else {
		size_t newScratchSize = (size_t) (strlen(scratch) +
						  strlen(ptr) + 2);

		scratch = (char *) s_realloc(scratch, newScratchSize);
		s_strncat(scratch, DB_DELIMITER, 2, newScratchSize);
		s_strncat(scratch, ptr, strlen(ptr) + 1, newScratchSize);
	    }
	}
	buf = writeString(scratch, DT_DatabaseNames, buf, len);
	s_free(scratch);
    }
    buf = writeString(query->QueryType, DT_QueryType, buf, len);
    /* write element set names */
    if (query->ElementSetNames != NULL) {
	for (i = 0, scratch = NULL, ptr = query->ElementSetNames[i];
	     ptr != NULL;
	     ptr = query->ElementSetNames[++i]) {
	    if (scratch == NULL) {
		if (query->ElementSetNames[i + 1] == NULL)	/* there is a single element set name */
		{
		    scratch = (char *) s_malloc((size_t) strlen(ptr) + 2);
		    StrNCpy(scratch, ES_DELIMITER_1, 2);
		    s_strncat(scratch, ptr, strlen(ptr) + 1, strlen(ptr) + 2);
		} else {	/* this is the first of a series of element set names */
		    size_t newScratchSize = (size_t) (strlen(ptr) +
						      strlen(query->ElementSetNames[i
										    + 1])
						      + 2);

		    scratch = s_strdup(ptr);	/* the database name */
		    ptr = query->ElementSetNames[++i];	/* the element set name */
		    scratch = (char *) s_realloc(scratch, newScratchSize);
		    s_strncat(scratch, ES_DELIMITER_1, 2, newScratchSize);
		    s_strncat(scratch, ptr, strlen(ptr) + 1, newScratchSize);
		}
	    } else {
		char *esPtr = query->ElementSetNames[++i];	/* the element set name */
		size_t newScratchSize = (size_t) (strlen(scratch) +
						  strlen(ptr) +
						  strlen(esPtr) +
						  3);

		scratch = (char *) s_realloc(scratch, newScratchSize);
		s_strncat(scratch, ES_DELIMITER_2, 2, newScratchSize);
		s_strncat(scratch, ptr, strlen(ptr) + 1, newScratchSize);
		s_strncat(scratch, ES_DELIMITER_1, 2, newScratchSize);
		s_strncat(scratch, esPtr, strlen(esPtr) + 1, newScratchSize);
	    }
	}
	buf = writeString(scratch, DT_ElementSetNames, buf, len);
	s_free(scratch);
    }
    buf = writeAny(query->ReferenceID, DT_ReferenceID, buf, len);

    /* go back and write the header-length-indicator */
    RELEASE_HEADER_SPACE(len);
    size = buf - buffer - HEADER_LEN;
    writeBinaryInteger(size, HEADER_LEN, buffer, len);

    if (query->Query != NULL)
	buf = writeSearchInfo(query, buf, len);

    return (buf);
}

/*----------------------------------------------------------------------*/

SearchResponseAPDU *makeSearchResponseAPDU(long result,
					   long count,
					   long recordsReturned,
					   long nextPos,
					   long resultStatus,
					   long presentStatus,
					   any *refID,
					   void *records)
{
    SearchResponseAPDU *query =
    (SearchResponseAPDU *) s_malloc((size_t) sizeof(SearchResponseAPDU));

    query->PDUType = searchResponseAPDU;
    query->SearchStatus = result;
    query->ResultCount = count;
    query->NumberOfRecordsReturned = recordsReturned;
    query->NextResultSetPosition = nextPos;
    query->ResultSetStatus = resultStatus;
    query->PresentStatus = presentStatus;
    query->ReferenceID = duplicateAny(refID);
    query->DatabaseDiagnosticRecords = records;
    return (query);
}

/*----------------------------------------------------------------------*/

void freeSearchResponseAPDU(SearchResponseAPDU *queryResponse)
{
    freeAny(queryResponse->ReferenceID);
    s_free(queryResponse);
}

/*----------------------------------------------------------------------*/

char *writeSearchResponseAPDU(SearchResponseAPDU *queryResponse, char *buffer,
			      long *len)
{
    char *buf = buffer + HEADER_LEN;	/* leave room for the header-length-indicator */
    long size;

    RESERVE_SPACE_FOR_HEADER(len);

    buf = writePDUType(queryResponse->PDUType,
		       buf,
		       len);
    buf = writeBinaryInteger(queryResponse->SearchStatus,
			     (size_t) 1,
			     buf,
			     len);
    buf = writeBinaryInteger(queryResponse->ResultCount,
			     (size_t) 3,
			     buf,
			     len);
    buf = writeBinaryInteger(queryResponse->NumberOfRecordsReturned,
			     (size_t) 3,
			     buf,
			     len);
    buf = writeBinaryInteger(queryResponse->NextResultSetPosition,
			     (size_t) 3,
			     buf,
			     len);
    buf = writeNum(queryResponse->ResultSetStatus,
		   DT_ResultSetStatus,
		   buf,
		   len);
    buf = writeNum(queryResponse->PresentStatus,
		   DT_PresentStatus,
		   buf,
		   len);
    buf = writeAny(queryResponse->ReferenceID,
		   DT_ReferenceID,
		   buf,
		   len);

    /* go back and write the header-length-indicator */
    RELEASE_HEADER_SPACE(len);
    size = buf - buffer - HEADER_LEN;
    writeBinaryInteger(size, HEADER_LEN, buffer, len);

    if (queryResponse->DatabaseDiagnosticRecords != NULL)
	buf = writeSearchResponseInfo(queryResponse, buf, len);

    return (buf);
}

/*----------------------------------------------------------------------*/

char *readSearchResponseAPDU(SearchResponseAPDU **queryResponse, char *buffer)
{
    char *buf = buffer;
    long size;
    pdu_type pduType;
    long result, count, recordsReturned, nextPos;
    long resultStatus, presentStatus;
    any *refID = NULL;
    void *userInfo = NULL;

    /* read required part */
    buf = readBinaryInteger(&size, HEADER_LEN, buf);
    buf = readPDUType(&pduType, buf);
    buf = readBinaryInteger(&result, (size_t) 1, buf);
    buf = readBinaryInteger(&count, (size_t) 3, buf);
    buf = readBinaryInteger(&recordsReturned, (size_t) 3, buf);
    buf = readBinaryInteger(&nextPos, (size_t) 3, buf);

    resultStatus = presentStatus = UNUSED;
    refID = NULL;

    /* read optional part */
    while (buf < (buffer + size + HEADER_LEN)) {
	data_tag tag = peekTag(buf);

	switch (tag) {
	case DT_ResultSetStatus:
	    buf = readNum(&resultStatus, buf);
	    break;
	case DT_PresentStatus:
	    buf = readNum(&presentStatus, buf);
	    break;
	case DT_ReferenceID:
	    buf = readAny(&refID, buf);
	    break;
	default:
	    freeAny(refID);
	    REPORT_READ_ERROR(buf);
	    break;
	}
    }

    buf = readSearchResponseInfo(&userInfo, buf);
    if (buf == NULL)
	freeAny(refID);
    RETURN_ON_NULL(buf);

    /* construct the search object */
    *queryResponse = makeSearchResponseAPDU(result,
					    count,
					    recordsReturned,
					    nextPos,
					    (long) resultStatus,
					    (long) presentStatus,
					    refID,
					    userInfo);

    freeAny(refID);

    return (buf);
}

/*
 *	Routines originally from ZUtil.c -- FM
 *
 *----------------------------------------------------------------------*/
/* WIDE AREA INFORMATION SERVER SOFTWARE:
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * 3.26.90	Harry Morris, morris@think.com
 * 3.30.90  Harry Morris - Changed any->bits to any->bytes
 * 4.11.90  HWM - fixed include file names, changed
 *		- writeCompressedIntegerWithPadding() to
 *		  writeCompressedIntWithPadding()
 *		- generalized conditional includes (see c-dialect.h)
 * 3.7.91   Jonny Goldman.  Replaced "short" in makeBitMap with "int" line 632.
 */

char *readErrorPosition = NULL;	/* pos where buf stopped making sense */

/*----------------------------------------------------------------------*/
/* A note on error handling
   read - these are low level routines, they do not check the type tags
   which (sometimes) precede the data (this is done by the higher
   level functions which call these functions).  There is no
   attempt made to check that the reading does not exceed the read
   buffer.  Such cases should be very rare and usually will be
   caught by the calling functions. (note - it is unlikely that
   a series of low level reads will go far off the edge without
   triggering a type error.  However, it is possible for a single
   bad read in an array function (eg. readAny) to attempt to read a
   large amount, possibly causing a segmentation violation or out
   of memory condition.
 */
/*----------------------------------------------------------------------*/

diagnosticRecord *makeDiag(boolean surrogate, char *code, char *addInfo)
{
    diagnosticRecord *diag =
    (diagnosticRecord *) s_malloc((size_t) sizeof(diagnosticRecord));

    diag->SURROGATE = surrogate;
    MemCpy(diag->DIAG, code, DIAGNOSTIC_CODE_SIZE);
    diag->ADDINFO = s_strdup(addInfo);

    return (diag);
}

/*----------------------------------------------------------------------*/

void freeDiag(diagnosticRecord * diag)
{
    if (diag != NULL) {
	if (diag->ADDINFO != NULL)
	    s_free(diag->ADDINFO);
	s_free(diag);
    }
}

/*----------------------------------------------------------------------*/

#define END_OF_RECORD	0x1D

char *writeDiag(diagnosticRecord * diag, char *buffer, long *len)
/* diagnostics (as per Appendix D) have a very weird format - this changes
   in SR-1
 */
{
    char *buf = buffer;
    long length;

    if (diag == NULL)		/* handle unspecified optional args */
	return (buf);

    buf = writeTag(DT_DatabaseDiagnosticRecords, buf, len);
    CHECK_FOR_SPACE_LEFT(0, len);

    length = 3;
    if (diag->ADDINFO != NULL)
	length += strlen(diag->ADDINFO);

    if (length >= 0xFFFF)	/* make sure the length is reasonable */
    {
	length = 0xFFFF - 1;
	diag->ADDINFO[0xFFFF - 3 - 1] = '\0';
    }

    buf = writeBinaryInteger(length, 2, buf, len);

    CHECK_FOR_SPACE_LEFT(1, len);
    buf[0] = diag->DIAG[0];
    buf++;

    CHECK_FOR_SPACE_LEFT(1, len);
    buf[0] = diag->DIAG[1];
    buf++;

    if (length > 3) {
	CHECK_FOR_SPACE_LEFT(3, len);
	MemCpy(buf, diag->ADDINFO, length - 3);
	buf += length - 3;
    }

    CHECK_FOR_SPACE_LEFT(1, len);
    buf[0] = diag->SURROGATE;
    buf++;

    CHECK_FOR_SPACE_LEFT(1, len);
    buf[0] = END_OF_RECORD;
    buf++;

    return (buf);
}

/*----------------------------------------------------------------------*/

char *readDiag(diagnosticRecord ** diag, char *buffer)
{
    char *buf = buffer;
    diagnosticRecord *d = (diagnosticRecord *) s_malloc((size_t) sizeof(diagnosticRecord));
    data_tag tag;
    long len;

    buf = readTag(&tag, buf);

    buf = readBinaryInteger(&len, 2, buf);

    d->DIAG[0] = buf[0];
    d->DIAG[1] = buf[1];
    d->DIAG[2] = '\0';

    if (len > 3) {
	d->ADDINFO = (char *) s_malloc((size_t) (len - 3 + 1));
	MemCpy(d->ADDINFO, (char *) (buf + 2), len - 3);
	d->ADDINFO[len - 3] = '\0';
    } else
	d->ADDINFO = NULL;

    d->SURROGATE = buf[len - 1];

    *diag = d;

    return (buf + len + 1);
}

/*----------------------------------------------------------------------*/

#define continueBit	0x80
#define dataMask	0x7F
#define dataBits	7

char *writeCompressedInteger(unsigned long num, char *buf, long *len)
/* write a binary integer in the format described on p. 40.
   this might be sped up
*/
{
    char byte;
    unsigned long i;
    unsigned long size;

    size = writtenCompressedIntSize(num);
    CHECK_FOR_SPACE_LEFT(size, len);

    for (i = size - 1; i != 0; i--) {
	byte = num & dataMask;
	if (i != (size - 1))	/* turn on continue bit */
	    byte = (char) (byte | continueBit);
	buf[i] = byte;
	num = num >> dataBits;	/* don't and here */
    }

    return (buf + size);
}

/*----------------------------------------------------------------------*/

char *readCompressedInteger(unsigned long *num, char *buf)
/* read a binary integer in the format described on p. 40.
   this might be sped up
*/
{
    long i = 0;
    unsigned char byte;

    *num = 0;

    do {
	byte = buf[i++];
	*num = *num << dataBits;
	*num += (byte & dataMask);
    }
    while (byte & continueBit);

    return (buf + i);
}

/*----------------------------------------------------------------------*/

#define pad	128		/* high bit is set */

char *writeCompressedIntWithPadding(unsigned long num,
				    unsigned long size,
				    char *buffer,
				    long *len)
/* Like writeCompressedInteger, except writes padding (128) to make
   sure that size bytes are used.  This can be read correctly by
   readCompressedInteger()
*/
{
    char *buf = buffer;
    unsigned long needed, padding;
    long i;

    CHECK_FOR_SPACE_LEFT(size, len);

    needed = writtenCompressedIntSize(num);
    padding = size - needed;
    i = padding - 1;

    for (i = padding - 1; i >= 0; i--) {
	buf[i] = pad;
    }

    buf = writeCompressedInteger(num, buf + padding, len);

    return (buf);
}

/*----------------------------------------------------------------------*/

unsigned long writtenCompressedIntSize(unsigned long num)
/* return the number of bytes needed to represent the value num in
   compressed format.  currently limited to 4 bytes
 */
{
    if (num < CompressedInt1Byte)
	return (1);
    else if (num < CompressedInt2Byte)
	return (2);
    else if (num < CompressedInt3Byte)
	return (3);
    else
	return (4);
}

/*----------------------------------------------------------------------*/

char *writeTag(data_tag tag, char *buf, long *len)
/* write out a data tag */
{
    return (writeCompressedInteger(tag, buf, len));
}

/*----------------------------------------------------------------------*/

char *readTag(data_tag *tag, char *buf)
/* read a data tag */
{
    return (readCompressedInteger(tag, buf));
}

/*----------------------------------------------------------------------*/

unsigned long writtenTagSize(data_tag tag)
{
    return (writtenCompressedIntSize(tag));
}

/*----------------------------------------------------------------------*/

data_tag peekTag(char *buf)
/* read a data tag without advancing the buffer */
{
    data_tag tag;

    readTag(&tag, buf);
    return (tag);
}

/*----------------------------------------------------------------------*/

any *makeAny(unsigned long size, char *data)
{
    any *a = (any *) s_malloc((size_t) sizeof(any));

    a->size = size;
    a->bytes = data;
    return (a);
}

/*----------------------------------------------------------------------*/

void freeAny(any *a)
/* destroy an any and its associated data.  Assumes a->bytes was
   allocated using the s_malloc family of libraries
 */
{
    if (a != NULL) {
	if (a->bytes != NULL)
	    s_free(a->bytes);
	s_free(a);
    }
}

/*----------------------------------------------------------------------*/

any *duplicateAny(any *a)
{
    any *copy = NULL;

    if (a == NULL)
	return (NULL);

    copy = (any *) s_malloc((size_t) sizeof(any));

    copy->size = a->size;
    if (a->bytes == NULL)
	copy->bytes = NULL;
    else {
	copy->bytes = (char *) s_malloc((size_t) copy->size);
	MemCpy(copy->bytes, a->bytes, copy->size);
    }
    return (copy);
}

/*----------------------------------------------------------------------*/

char *writeAny(any *a, data_tag tag, char *buffer, long *len)
/* write an any + tag and size info */
{
    char *buf = buffer;

    if (a == NULL)		/* handle unspecified optional args */
	return (buf);

    /* write the tags */
    buf = writeTag(tag, buf, len);
    buf = writeCompressedInteger(a->size, buf, len);

    /* write the bytes */
    CHECK_FOR_SPACE_LEFT(a->size, len);
    MemCpy(buf, a->bytes, a->size);

    return (buf + a->size);
}

/*----------------------------------------------------------------------*/

char *readAny(any **anAny, char *buffer)
/* read an any + tag and size info */
{
    char *buf;
    any *a;
    data_tag tag;

    a = (any *) s_malloc((size_t) sizeof(any));

    buf = buffer;

    buf = readTag(&tag, buf);

    buf = readCompressedInteger(&a->size, buf);

    /* now simply copy the bytes */
    a->bytes = (char *) s_malloc((size_t) a->size);
    MemCpy(a->bytes, buf, a->size);
    *anAny = a;

    return (buf + a->size);
}

/*----------------------------------------------------------------------*/

unsigned long writtenAnySize(data_tag tag, any *a)
{
    unsigned long size;

    if (a == NULL)
	return (0);

    size = writtenTagSize(tag);
    size += writtenCompressedIntSize(a->size);
    size += a->size;
    return (size);
}

/*----------------------------------------------------------------------*/

any *stringToAny(char *s)
{
    any *a = NULL;

    if (s == NULL)
	return (NULL);

    a = (any *) s_malloc((size_t) sizeof(any));

    a->size = strlen(s);
    a->bytes = (char *) s_malloc((size_t) a->size);
    MemCpy(a->bytes, s, a->size);
    return (a);
}

/*----------------------------------------------------------------------*/

char *anyToString(any *a)
{
    char *s = NULL;

    if (a == NULL)
	return (NULL);

    s = s_malloc((size_t) (a->size + 1));
    MemCpy(s, a->bytes, a->size);
    s[a->size] = '\0';
    return (s);
}

/*----------------------------------------------------------------------*/

char *writeString(char *s, data_tag tag, char *buffer, long *len)
/* Write a C style string.  The terminating null is not written.
   This function is not part of the Z39.50 spec.  It is provided
   for the convenience of those wishing to pass C strings in
   the place of an any.
 */
{
    char *buf = buffer;
    any *data = NULL;

    if (s == NULL)
	return (buffer);	/* handle unused optional item before making an any */
    data = (any *) s_malloc((size_t) sizeof(any));

    data->size = strlen(s);
    data->bytes = s;		/* save a copy here by not using stringToAny() */
    buf = writeAny(data, tag, buf, len);
    s_free(data);		/* don't use freeAny() since it will free s too */
    return (buf);
}

/*----------------------------------------------------------------------*/

char *readString(char **s, char *buffer)
/* Read an any and convert it into a C style string.
   This function is not part of the Z39.50 spec.  It is provided
   for the convenience of those wishing to pass C strings in
   the place of an any.
 */
{
    any *data = NULL;
    char *buf = readAny(&data, buffer);

    *s = anyToString(data);
    freeAny(data);
    return (buf);
}

/*----------------------------------------------------------------------*/

unsigned long writtenStringSize(data_tag tag, char *s)
{
    unsigned long size;

    if (s == NULL)
	return (0);

    size = writtenTagSize(tag);
    size += writtenCompressedIntSize(size);
    size += strlen(s);
    return (size);
}

/*----------------------------------------------------------------------*/

any *longToAny(long num)
/* a convenience function */
{
    char s[40];

    sprintf(s, "%ld", num);

    return (stringToAny(s));
}

/*----------------------------------------------------------------------*/

long anyToLong(any *a)
/* a convenience function */
{
    long num;
    char *str = NULL;

    str = anyToString(a);
    sscanf(str, "%ld", &num);	/* could check the result and return
				   an error */
    s_free(str);
    return (num);
}

/*----------------------------------------------------------------------*/

#define bitsPerByte	8

bit_map *makeBitMap(unsigned long numBits, ...)
/* construct and return a bitmap with numBits elements */
{
    va_list ap;
    unsigned long i, j;
    bit_map *bm = NULL;

    LYva_start(ap, numBits);

    bm = (bit_map *) s_malloc((size_t) sizeof(bit_map));

    bm->size = (unsigned long) (ceil((double) numBits / bitsPerByte));
    bm->bytes = (char *) s_malloc((size_t) bm->size);

    /* fill up the bits */
    for (i = 0; i < bm->size; i++)	/* iterate over bytes */
    {
	char byte = 0;

	for (j = 0; j < bitsPerByte; j++)	/* iterate over bits */
	{
	    if ((i * bitsPerByte + j) < numBits) {
		boolean bit = false;

		bit = (boolean) va_arg(ap, boolean);

		if (bit) {
		    byte = byte | (1 << (bitsPerByte - j - 1));
		}
	    }
	}
	bm->bytes[i] = byte;
    }

    va_end(ap);
    return (bm);
}

/*----------------------------------------------------------------------*/

void freeBitMap(bit_map *bm)
/* destroy a bit map created by makeBitMap() */
{
    s_free(bm->bytes);
    s_free(bm);
}

/*----------------------------------------------------------------------*/

/* use this routine to interpret a bit map.  pos specifies the bit
   number.  bit 0 is the Leftmost bit of the first byte.
   Could do bounds checking.
 */

boolean bitAtPos(unsigned long pos, bit_map *bm)
{
    if (pos > bm->size * bitsPerByte)
	return false;
    else
	return ((bm->bytes[(pos / bitsPerByte)] &
		 (0x80 >> (pos % bitsPerByte))) ?
		true : false);
}

/*----------------------------------------------------------------------*/

char *writeBitMap(bit_map *bm, data_tag tag, char *buffer, long *len)
/* write a bitmap + type and size info */
{
    return (writeAny((any *) bm, tag, buffer, len));
}

/*----------------------------------------------------------------------*/

char *readBitMap(bit_map **bm, char *buffer)
/* read a bitmap + type and size info */
{
    char *c;

    c = readAny((any **) bm, buffer);
    return (c);
}

/*----------------------------------------------------------------------*/

char *writeByte(unsigned long byte, char *buf, long *len)
{
    CHECK_FOR_SPACE_LEFT(1, len);
    buf[0] = byte & 0xFF;	/* we really only want the first byte */
    return (buf + 1);
}

/*----------------------------------------------------------------------*/

char *readByte(unsigned char *byte, char *buf)
{
    *byte = buf[0];
    return (buf + 1);
}

/*----------------------------------------------------------------------*/

char *writeBoolean(boolean flag, char *buf, long *len)
{
    return (writeByte(flag, buf, len));
}

/*----------------------------------------------------------------------*/

char *readBoolean(boolean *flag, char *buffer)
{
    unsigned char byte;
    char *buf = readByte(&byte, buffer);

    *flag = (byte == true) ? true : false;
    return (buf);
}

/*----------------------------------------------------------------------*/

char *writePDUType(pdu_type pduType, char *buf, long *len)
/* PDUType is a single byte */
{
    return (writeBinaryInteger((long) pduType, (unsigned long) 1, buf, len));
}

/*----------------------------------------------------------------------*/

char *readPDUType(pdu_type *pduType, char *buf)
/* PDUType is a single byte */
{
    return (readBinaryInteger((long *) pduType, (unsigned long) 1, buf));
}

/*----------------------------------------------------------------------*/

pdu_type peekPDUType(char *buf)
/* read the next pdu without advancing the buffer, Note that this
   function is to be used on a buffer that is known to contain an
   APDU.  The pdu_type is written HEADER_LEN bytes into the buffer
 */
{
    pdu_type pdu;

    readPDUType(&pdu, buf + HEADER_LEN);
    return (pdu);
}

/*----------------------------------------------------------------------*/

#define BINARY_INTEGER_BYTES	sizeof(long)	/* the number of bytes used by
						   a "binary integer" */
char *writeBinaryInteger(long num, unsigned long size, char *buf, long *len)
/* write out first size bytes of num - no type info
  XXX should this take unsigned longs instead ???  */
{
    long i;
    char byte;

    if (size < 1 || size > BINARY_INTEGER_BYTES)
	return (NULL);		/* error */

    CHECK_FOR_SPACE_LEFT(size, len);

    for (i = size - 1; i >= 0; i--) {
	byte = (char) (num & 255);
	buf[i] = byte;
	num = num >> bitsPerByte;	/* don't and here */
    }

    return (buf + size);
}

/*----------------------------------------------------------------------*/

char *readBinaryInteger(long *num, unsigned long size, char *buf)
/* read in first size bytes of num - no type info
  XXX this should take unsigned longs instead !!! */
{
    unsigned long i;
    unsigned char byte;

    if (size < 1 || size > BINARY_INTEGER_BYTES)
	return (buf);		/* error */
    *num = 0;

    for (i = 0; i < size; i++) {
	byte = buf[i];
	*num = *num << bitsPerByte;
	*num += byte;
    }

    return (buf + size);
}

/*----------------------------------------------------------------------*/

unsigned long writtenCompressedBinIntSize(long num)
/* return the number of bytes needed to represent the value num.
   currently limited to max of 4 bytes
   Only compresses for positive nums - negatives get whole 4 bytes
 */
{
    if (num < 0L)
	return (4);
    else if (num < 256L)	/* 2**8 */
	return (1);
    else if (num < 65536L)	/* 2**16 */
	return (2);
    else if (num < 16777216L)	/* 2**24 */
	return (3);
    else
	return (4);
}

/*----------------------------------------------------------------------*/

char *writeNum(long num, data_tag tag, char *buffer, long *len)
/* write a binary integer + size and tag info */
{
    char *buf = buffer;
    long size = writtenCompressedBinIntSize(num);

    if (num == UNUSED)
	return (buffer);

    buf = writeTag(tag, buf, len);
    buf = writeCompressedInteger(size, buf, len);
    buf = writeBinaryInteger(num, (unsigned long) size, buf, len);
    return (buf);
}

/*----------------------------------------------------------------------*/

char *readNum(long *num, char *buffer)
/* read a binary integer + size and tag info */
{
    char *buf = buffer;
    data_tag tag;
    unsigned long size;
    unsigned long val;

    buf = readTag(&tag, buf);
    buf = readCompressedInteger(&val, buf);
    size = (unsigned long) val;
    buf = readBinaryInteger(num, size, buf);
    return (buf);
}

/*----------------------------------------------------------------------*/

unsigned long writtenNumSize(data_tag tag, long num)
{
    long dataSize = writtenCompressedBinIntSize(num);
    long size;

    size = writtenTagSize(tag);	/* space for the tag */
    size += writtenCompressedIntSize(dataSize);		/* space for the size */
    size += dataSize;		/* space for the data */

    return (size);
}

/*----------------------------------------------------------------------*/

typedef void (voidfunc) (void *);

void doList(void **list, voidfunc * func)
/* call func on each element of the NULL terminated list of pointers */
{
    register long i;
    register void *ptr = NULL;

    if (list == NULL)
	return;
    for (i = 0, ptr = list[i]; ptr != NULL; ptr = list[++i])
	(*func) (ptr);
}

/*----------------------------------------------------------------------*/

char *writeProtocolVersion(char *buf, long *len)
/* write a bitmap describing the protocols available */
{
    static bit_map *version = NULL;

    if (version == NULL) {
	version = makeBitMap((unsigned long) 1, true);	/* version 1! */
    }

    return (writeBitMap(version, DT_ProtocolVersion, buf, len));
}

/*----------------------------------------------------------------------*/

char *defaultImplementationID(void)
{
    static char ImplementationID[] = "TMC";

    return (ImplementationID);
}

/*----------------------------------------------------------------------*/

char *defaultImplementationName(void)
{
    static char ImplementationName[] = "Thinking Machines Corporation Z39.50";

    return (ImplementationName);
}

/*----------------------------------------------------------------------*/

char *defaultImplementationVersion(void)
{
    static char ImplementationVersion[] = "2.0A";

    return (ImplementationVersion);
}

/*----------------------------------------------------------------------*/

/*
 *	Routines originally from ZType1.c -- FM
 *
 *----------------------------------------------------------------------*/
/* WIDE AREA INFORMATION SERVER SOFTWARE:
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * 3.26.90	Harry Morris, morris@think.com
 * 4.11.90  HWM - generalized conditional includes (see c-dialect.h)
 */
/*----------------------------------------------------------------------*/

query_term *makeAttributeTerm(char *use,
			      char *relation,
			      char *position,
			      char *structure,
			      char *truncation,
			      char *completeness,
			      any *term)
{
    query_term *qt = (query_term *) s_malloc((size_t) sizeof(query_term));

    qt->TermType = TT_Attribute;

    /* copy in the attributes */
    LYStrNCpy(qt->Use, use, ATTRIBUTE_SIZE);
    LYStrNCpy(qt->Relation, relation, ATTRIBUTE_SIZE);
    LYStrNCpy(qt->Position, position, ATTRIBUTE_SIZE);
    LYStrNCpy(qt->Structure, structure, ATTRIBUTE_SIZE);
    LYStrNCpy(qt->Truncation, truncation, ATTRIBUTE_SIZE);
    LYStrNCpy(qt->Completeness, completeness, ATTRIBUTE_SIZE);

    qt->Term = duplicateAny(term);

    qt->ResultSetID = NULL;

    return (qt);
}

/*----------------------------------------------------------------------*/

query_term *makeResultSetTerm(any *resultSet)
{
    query_term *qt = (query_term *) s_malloc((size_t) sizeof(query_term));

    qt->TermType = TT_ResultSetID;

    qt->ResultSetID = duplicateAny(resultSet);

    qt->Term = NULL;

    return (qt);
}

/*----------------------------------------------------------------------*/

query_term *makeOperatorTerm(char *operatorCode)
{
    query_term *qt = (query_term *) s_malloc((size_t) sizeof(query_term));

    qt->TermType = TT_Operator;

    LYStrNCpy(qt->Operator, operatorCode, OPERATOR_SIZE);

    qt->Term = NULL;
    qt->ResultSetID = NULL;

    return (qt);
}

/*----------------------------------------------------------------------*/

void freeTerm(void *param)
{
    query_term *qt = (query_term *) param;

    switch (qt->TermType) {
    case TT_Attribute:
	freeAny(qt->Term);
	break;
    case TT_ResultSetID:
	freeAny(qt->ResultSetID);
	break;
    case TT_Operator:
	/* do nothing */
	break;
    default:
	panic("Implementation error: Unknown term type %ld",
	      qt->TermType);
	break;
    }
    s_free(qt);
}

/*----------------------------------------------------------------------*/

#define ATTRIBUTE_LIST_SIZE	ATTRIBUTE_SIZE * 6
#define AT_DELIMITER	" "

char *writeQueryTerm(query_term *qt, char *buffer, long *len)
{
    char *buf = buffer;
    char attributes[ATTRIBUTE_LIST_SIZE];

    switch (qt->TermType) {
    case TT_Attribute:
	LYStrNCpy(attributes, qt->Use, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, AT_DELIMITER, sizeof(AT_DELIMITER) + 1, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, qt->Relation, ATTRIBUTE_SIZE, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, AT_DELIMITER, sizeof(AT_DELIMITER) + 1, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, qt->Position, ATTRIBUTE_SIZE, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, AT_DELIMITER, sizeof(AT_DELIMITER) + 1, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, qt->Structure, ATTRIBUTE_SIZE, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, AT_DELIMITER, sizeof(AT_DELIMITER) + 1, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, qt->Truncation, ATTRIBUTE_SIZE, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, AT_DELIMITER, sizeof(AT_DELIMITER) + 1, ATTRIBUTE_LIST_SIZE);
	s_strncat(attributes, qt->Completeness, ATTRIBUTE_SIZE, ATTRIBUTE_LIST_SIZE);
	buf = writeString(attributes, DT_AttributeList, buf, len);
	buf = writeAny(qt->Term, DT_Term, buf, len);
	break;
    case TT_ResultSetID:
	buf = writeAny(qt->ResultSetID, DT_ResultSetID, buf, len);
	break;
    case TT_Operator:
	buf = writeString(qt->Operator, DT_Operator, buf, len);
	break;
    default:
	panic("Implementation error: Unknown term type %ld",
	      qt->TermType);
	break;
    }

    return (buf);
}

/*----------------------------------------------------------------------*/

char *readQueryTerm(query_term **qt, char *buffer)
{
    char *buf = buffer;
    char *attributeList = NULL;
    char *operator = NULL;
    any *term;
    char *use = NULL;
    char *relation = NULL;
    char *position = NULL;
    char *structure = NULL;
    char *truncation = NULL;
    char *completeness;
    any *resultSetID = NULL;
    data_tag tag;

    tag = peekTag(buffer);

    switch (tag) {
    case DT_AttributeList:
	buf = readString(&attributeList, buf);
	buf = readAny(&term, buf);
	use = strtok(attributeList, AT_DELIMITER);
	relation = strtok(NULL, AT_DELIMITER);
	position = strtok(NULL, AT_DELIMITER);
	structure = strtok(NULL, AT_DELIMITER);
	truncation = strtok(NULL, AT_DELIMITER);
	completeness = strtok(NULL, AT_DELIMITER);
	*qt = makeAttributeTerm(use, relation, position, structure,
				truncation, completeness, term);
	s_free(attributeList);
	freeAny(term);
	break;
    case DT_ResultSetID:
	buf = readAny(&resultSetID, buf);
	*qt = makeResultSetTerm(resultSetID);
	freeAny(resultSetID);
	break;
    case DT_Operator:
	buf = readString(&operator, buf);
	*qt = makeOperatorTerm(operator);
	s_free(operator);
	break;
    default:
	REPORT_READ_ERROR(buf);
	break;
    }

    return (buf);
}

/*----------------------------------------------------------------------*/

static unsigned long getQueryTermSize(query_term *qt);

static unsigned long getQueryTermSize(query_term *qt)
/* figure out how many bytes it will take to write this query */
{
    unsigned long size = 0;
    static char attributes[] = "11 22 33 44 55 66";	/* we just need this to

							   calculate its written
							   size */

    switch (qt->TermType) {
    case TT_Attribute:
	size = writtenStringSize(DT_AttributeList, attributes);
	size += writtenAnySize(DT_Term, qt->Term);
	break;
    case TT_ResultSetID:
	size = writtenAnySize(DT_ResultSetID, qt->ResultSetID);
	break;
    case TT_Operator:
	size = writtenStringSize(DT_Operator, qt->Operator);
	break;
    default:
	panic("Implementation error: Unknown term type %ld",
	      qt->TermType);
	break;
    }

    return (size);
}

/*----------------------------------------------------------------------*/

/* A query is simply a null terminated list of query terms.  For
   transmission, a query is written into an any which is sent as
   the user information field. */

any *writeQuery(query_term **terms)
{
    any *info = NULL;
    char *writePos = NULL;
    char *data = NULL;
    unsigned long size = 0;
    long remaining = 0;
    long i;
    query_term *qt = NULL;

    if (terms == NULL)
	return (NULL);

    /* calculate the size of write buffer */
    for (i = 0, qt = terms[i]; qt != NULL; qt = terms[++i])
	size += getQueryTermSize(qt);

    data = (char *) s_malloc((size_t) size);

    /* write the terms */
    writePos = data;
    remaining = size;
    for (i = 0, qt = terms[i]; qt != NULL; qt = terms[++i])
	writePos = writeQueryTerm(qt, writePos, &remaining);

    info = makeAny(size, data);

    return (info);
}

/*----------------------------------------------------------------------*/

query_term **readQuery(any *info)
{
    char *readPos = info->bytes;
    query_term **terms = NULL;
    query_term *qt = NULL;
    long numTerms = 0L;
    char tmp[100];

    sprintf(tmp, "readquery: bytes: %ld", info->size);
    log_write(tmp);

    while (readPos < info->bytes + info->size) {
	readPos = readQueryTerm(&qt, readPos);

	if (terms == NULL) {
	    terms = (query_term **) s_malloc((size_t) (sizeof(query_term *) * 2));
	} else {
	    terms =
		(query_term **) s_realloc((char *) terms,
					  (size_t) (sizeof(query_term *) *
						      (numTerms + 2)));
	}
	if (qt == NULL)
	    log_write("qt = null");
	terms[numTerms++] = qt;
	terms[numTerms] = NULL;
    }

    return (terms);
}

/*----------------------------------------------------------------------*/

/*
 *	Routines originally from panic.c -- FM
 *
 *----------------------------------------------------------------------*/
/* WIDE AREA INFORMATION SERVER SOFTWARE:
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * Morris@think.com
 */

/* panic is an error system interface.  On the Mac, it will pop
 * up a little window to explain the problem.
 * On a unix box, it will print out the error and call perror()
 */

/*----------------------------------------------------------------------*/

static void exitAction(long error);

static void exitAction(long error GCC_UNUSED)
{
    exit_immediately(EXIT_SUCCESS);
}

/*----------------------------------------------------------------------*/

#define PANIC_HEADER "Fatal Error:  "

void panic(char *format, ...)
{
    va_list ap;			/* the variable arguments */

    fprintf(stderr, PANIC_HEADER);
    LYva_start(ap, format);	/* init ap */
    vfprintf(stderr, format, ap);	/* print the contents */
    va_end(ap);			/* free ap */
    fflush(stderr);

    exitAction(0);
}

/*----------------------------------------------------------------------*/

/*
 *	Routines originally from cutil.c -- FM
 *
 *----------------------------------------------------------------------*/
/* Wide AREA INFORMATION SERVER SOFTWARE
 * No guarantees or restrictions.  See the readme file for the full standard
 * disclaimer.
 *
 * 3.26.90	Harry Morris, morris@think.com
 * 4.11.90  HWM - generalized conditional includes (see c-dialect.h)
 */

/*----------------------------------------------------------------------*/

void fs_checkPtr(void *ptr)
/* If the ptr is NULL, give an error */
{
    if (ptr == NULL)
	panic("checkPtr found a NULL pointer");
}

/*----------------------------------------------------------------------*/

void *fs_malloc(size_t size)
/* does safety checks and optional accounting */
{
    register void *ptr = NULL;

    ptr = (void *) calloc((size_t) size, (size_t) 1);
    s_checkPtr(ptr);

    return (ptr);
}

/*----------------------------------------------------------------------*/

void *fs_realloc(void *ptr, size_t size)
/* does safety checks and optional accounting
   note - we don't know how big ptr's memory is, so we can't ensure
   that any new memory allocated is NULLed!
 */
{
    register void *nptr = NULL;

    if (ptr == NULL)		/* this is really a malloc */
	return (s_malloc(size));

    nptr = (void *) realloc(ptr, size);
    s_checkPtr(ptr);

    return (nptr);
}

/*----------------------------------------------------------------------*/

void fs_free(void *ptr)
/* does safety checks and optional accounting */
{
    if (ptr != NULL)		/* some non-ansi compilers/os's can't handle freeing null */
    {				/* if we knew the size of this block of memory, we could clear it - oh well */
	free(ptr);
	ptr = NULL;
    }
}

/*----------------------------------------------------------------------*/

char *s_strdup(char *s)

/* return a copy of s.  This is identical to the standard library routine
   strdup(), except that it is safe.  If s == NULL or malloc fails,
   appropriate action is taken.
 */
{
    unsigned long len;
    char *copy = NULL;

    if (s == NULL)		/* safety check to postpone stupid errors */
	return (NULL);

    len = strlen(s);		/* length of string - terminator */
    copy = (char *) s_malloc((size_t) (sizeof(char) * (len + 1)));

    StrNCpy(copy, s, len + 1);
    return (copy);
}

/*----------------------------------------------------------------------*/

char *fs_strncat(char *dst, char *src, size_t maxToAdd, size_t maxTotal)

/* like strncat, except the fourth argument limits the maximum total
   length of the resulting string
 */
{
    size_t dstSize = strlen(dst);
    size_t srcSize = strlen(src);

    if (dstSize + srcSize < maxTotal)	/* use regular old strncat */
	return (StrNCat(dst, src, maxToAdd));
    else {
	size_t truncateTo = maxTotal - dstSize - 1;
	char saveChar = src[truncateTo];
	char *result = NULL;

	src[truncateTo] = '\0';
	result = StrNCat(dst, src, maxToAdd);
	src[truncateTo] = saveChar;
	return (result);
    }
}

/*----------------------------------------------------------------------*/

char char_downcase(unsigned long long_ch)
{
    unsigned char ch = long_ch & 0xFF;	/* just want one byte */

    /* when ansi is the way of the world, this can be tolower */
    return (((ch >= 'A') && (ch <= 'Z')) ? (ch + 'a' - 'A') : ch);
}

char *string_downcase(char *word)
{
    long i = 0;

    while (word[i] != '\0') {
	word[i] = char_downcase((unsigned long) word[i]);
	i++;
    }
    return (word);
}

/*----------------------------------------------------------------------*/
#endif /* VMS */