/usr/sbin/gspl-ptrinstall is in gnuspool 1.7ubuntu1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
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 | #! /usr/bin/perl
##
## Ptrinstall - script to set up printers for GNUspool
##
## Copyright 2008 Free Software Foundation, Inc.
##
## This program is free software: you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program. If not, see <http://www.gnu.org/licenses/>.
##
use Socket;
# Various options:
# Generally:
# Keyword, default/type, description, [-switch, program default]
# ! before default means don't prompt for it do it another way
# ! before switch means insert only if not specified
$Options = {
PARALLEL => [
[ "open", 30, "Timeout on open before giving up" ],
[ "offline", 300, "Timeout on write before regarding device as offline" ],
[ "canhang", "N", "Running processes attached to device hard to kill" ],
[ "outbuffer", 1024, "Size of output buffer in bytes" ],
[ "reopen", "N", "Close and reopen device after each job" ] ],
USB => [ # USB is a bit dodgy - so deal with as parallel for the time being
[ "open", 30, "Timeout on open before giving up" ],
[ "offline", 300, "Timeout on write before regarding device as offline" ],
[ "canhang", "N", "Running processes attached to device hard to kill" ],
[ "outbuffer", 1024, "Size of output buffer in bytes" ],
[ "reopen", "N", "Close and reopen device after each job" ] ],
SERIAL => [ # Serial is a subset of the baud rates leaving out stupid slow ones
[ "baud", [ 9600, 1200, 1800, 2400, 4800, 9600, 19200, 38400 ], "Baudrate" ],
[ "ixon", "Y", "Set xon/xoff" ],
[ "ixany", "N", "Set xon/xoff with any character release" ],
[ "csize", [ 8, 5, 6, 7, 8], "Character size" ],
[ "stopbits", [1, 1, 2], "Stop bits" ],
[ "parenb", "N", "Parity enabled" ],
[ "parodd", "N", "Odd parity" ],
[ "clocal", "N", "No modem control" ],
[ "open", 30, "Timeout on open before giving up" ],
[ "offline", 300, "Timeout on write before regarding device as offline" ],
[ "canhang", "N", "Running processes attached to device hard to kill" ],
[ "outbuffer", 1024, "Size of output buffer in bytes" ],
[ "reopen", "N", "Close and reopen device after each job" ],
[ "onlcr", "N", "Insert CR before each newline" ] ],
STDNET => [ # General network parameters
[ "open", 30, "Timeout on open before giving up" ],
[ "offline", 300, "Timeout on write before regarding device as offline" ],
[ "close", 10000, "Time to wait for close to complete" ],
[ "postclose", 1, "Time to wait after close" ],
[ "canhang", "N", "Running processes attached to device hard to kill" ],
[ "outbuffer", 1024, "Size of output buffer in bytes" ],
[ "reopen", "Y", "Restart server after each job" ],
[ "logerror", "Y", "Log error messages to system log" ],
[ "fberror", "Y", "Display error messages on screen" ] ],
LPDNET => [ # Specific to LPD
[ "host", '!$SPOOLDEV', "Host address of server", 'H' ],
[ "ctrlfile", "!SDATADIR/xtlpc-ctrl", "Control file", 'f' ],
[ "outip", "!h", "Outgoing host name", 'S' ],
[ "lpdname", "!S", "Printer name for protocol", 'P' ],
[ "nonull", "Y", "Do not send null jobs", 'N' ],
[ "resp", "N", "Use reserved port", '!U' ],
[ "loops", 3, "Attempts to connect", 'l', 3 ],
[ "loopwait", 1, "Seconds to wait between connect attempts", 'L', 1 ],
[ "itimeout", 5, "Input timeout (for response packets", 'I', 5 ],
[ "otimeout", 5, "Output timeout (response receiving data", 'O', 5 ],
[ "retries", 0, "Number of retries after timeouts", 'R', 0 ],
[ "linger", 'f:0', "Linger time (may be fractional)", 's', 0 ] ],
TELNET => [ # Specific to telnet
[ "host", '!$SPOOLDEV', "Host name of server", 'h' ],
[ "port", 'p:9100', "Output port", 'p', 9100 ],
[ "loops", 3, "Attempts to connect", 'l', 3 ],
[ "loopwait", 1, "Seconds to wait between connect attempts", 'L' ],
[ "endsleep", 0, "Time for process to sleep at end of each job", 't', 0 ],
[ "linger", 'f:0', "Linger time (may be fractional", 's', 0 ] ],
FTP => [ # Specific to FTP
[ "host", '!$SPOOLDEV', "Host name of server", 'h' ],
[ "myhost", '!h', "IP to send from", 'A' ],
[ "port", 'p:ftp', "Control port", 'p', "ftp" ],
[ "username", "s:", "User name", 'u' ],
[ "password", "s:", "Password", 'w' ],
[ "directory", 's:', "Directory name on server", 'D' ],
[ "outfile", "s:", "Output file on server", 'o' ],
[ "textmode", "N", "Force text mode", 't' ],
[ "timeout", 750, "Timeout for select (ms)", 'T', 750 ],
[ "maintimeout", 30000, "Timeout for FTP (ms)", 'R', 30000 ] ],
XTLHP => [ # Specific to SNMP
[ "host", '!$SPOOLDEV', "Host name of server", 'h' ],
[ "configfile", "!SDATADIR/xtsnmpdef", "Config file", 'f' ],
[ "ctrlfile", "!SDATADIR/xtlhp-ctrl", "Control file", 'c' ],
[ "port", 'p:9100', "Output port", 'p', 9100 ],
[ "myhost", '!h', "Outgoing host name (sometimes different)", 'H' ],
[ "commun", "s:public", "SNMP Community", 'C', "public"],
[ "timeout", "f:1", "UDP timeout", 'T', 1 ],
[ "snmpport", "p:snmp", "SNMP Port", 'S', "snmp" ],
[ "blksize", 10240, "Block size", 'b', 10240 ],
[ "next", "N", "Get next on SNMP var fetches", 'N' ] ],
STDOPTS => [ # Spooler opts
[ "addcr", "N", "Add CR before each newline (text only!)" ],
[ "retain", "N", "Retain all jobs on queue after printing" ],
[ "norange", "N", "Ignore page ranges" ],
[ "inclpage1", "N", "Always print page 1 when printing ranges" ],
[ "single", "N", "Single-job mode" ],
[ "onecopy", "N", "Limit to one copy - handled elsewhere" ] ],
LPDOPTS => [ # LPD variant
[ "addcr", "N", "Add CR before each newline (text only!)" ],
[ "retain", "N", "Retain all jobs on queue after printing" ],
[ "norange", "N", "Ignore page ranges" ],
[ "inclpage1", "N", "Always print page 1 when printing ranges" ],
[ "single", "N", "Single-job mode" ],
[ "onecopy", "N", "Limit to one copy - handled elsewhere" ] ] } ;
# Change previous way of recording port types
$Canonports = { LPD => "LPDNET", "REVERSE TELNET" => "TELNET", "TELNET-SNMP" => "XTLHP", "OTHER" => "OTHER" };
########################################################################
#
# Functions for formatting display
#
########################################################################
# Underline a message
sub underline {
my $fh = shift;
my $txt = shift;
my $und = shift || '-';
print $fh $txt,"\n", $und x length($txt),"\n\n";
}
# Nice formatted column
sub multicol {
my $fh = shift;
my $arr = shift;
my @cw;
for my $a (@$arr) {
my $c = 0;
for my $e (@$a) {
$cw[$c] = length($e) if $cw[$c] < length($e);
$c++;
}
}
for my $a (@$arr) {
my @e = @$a;
my $c = 0;
while ($#e > 0) {
my $i = shift @e;
print $fh $i, ' ' x ($cw[$c] - length($i) + 1);
$c++;
}
my $i = shift @e;
print $fh "$i\n";
}
}
########################################################################
#
# Get chars from standard input routines
#
########################################################################
# Function to get terminal stuff before we start
sub getekchars {
# Extract terminal control characters from stty output into %Schar
open(ST, "stty -a|");
while (<ST>) {
chop;
s/\b(intr|quit|erase|kill)\s*=\s*([^;]*);/$Schar{$1}=$2/eg;
}
close ST;
# Easier if we use "ord"
for my $s (keys %Schar) {
# Special case ^? for delete
if ($Schar{$s} eq '^?') {
$Schar{$s} = 127;
}
else {
$Schar{$s} = ord($1) & 31 if $Schar{$s} =~ /^\^(.)/;
}
}
# Remember how to reset terminal
$Resetty = `stty -g`;
chop $Resetty;
# Remember F1 key for help - but we may not have it available
# Remember clear screen sequence
$Func1 = `tput kf1`;
$Clear = `tput clear`;
}
# Get a character from the terminal - using raw mode so we can recognise
# if a function key is pressed
sub getchar {
system("stty raw -echo time 3");
my $res;
my $cnt;
do {
$cnt = sysread STDIN, $res, 10;
} while $cnt == 0;
system("stty $Resetty");
$res;
}
sub splurgehelp {
my $help = shift;
my $hf = $Config{SDATADIR} . "/Psetups/pihelp/$help.txt";
print $Clear;
if (open(HF, $hf)) {
print while <HF>;
close HF;
}
else {
print "Sorry no help file for $help\n";
}
# Throw away char
getchar;
}
# Get a line normally terminated by \r or \n
# Sets global $Hadfunc1
sub getline {
my $help = shift; # Name of help file
my $prompt = shift; # Prompt
my $res = ""; # Result so far
print $prompt;
for (;;) {
my $ch = getchar;
if ($ch eq "\r" || $ch eq "\n") { # End of input
print "\n"; # Echo it
return $res;
}
if (length($ch) > 1) { # Function key
if ($ch eq $Func1) { # Help key F1
# Set we had function key one so ? doesn't get used for help any more
$Hadfunc1 = 1;
splurgehelp($help);
# Clear screen again, give prompt and result so far
print $Clear, $prompt, $res;
}
redo;
}
# Provide ? as a help key only if the guy hasn't managed
# to press F1 sometime.
unless ($ch ne '?' || $Hadfunc1) {
splurgehelp($help);
print $Clear, $prompt, $res;
redo;
}
# Quit if interrupt or quit pressed
my $och = ord($ch);
if ($och == $Schar{'intr'} || $och == $Schar{'quit'}) {
print "\n";
exit 0;
}
# Emulate kill char
if ($och == $Schar{'kill'}) {
$res = "";
print "\n$prompt";
redo;
}
# Emulate erase char
if ($och == $Schar{'erase'}) {
$res = substr $res, 0, length($res)-1 unless length($res) == 0;
print "\b \b";
redo;
}
# Ignore non-printing chars
redo if $och < 32 || $och > 126;
$res .= $ch;
print $ch;
}
}
# Prompt for new printer name
sub getnewptrname {
my $descr = shift;
for (;;) {
$ptr = getline('newptrname', "$descr: ");
return $ptr if $ptr =~ /^[a-z]\w*$/;
print "Invalid printer name $ptr please try again\n";
}
}
# Generalised ask question routine
# Param 1 help file
# Param 2 prompt
# Param 3 default response or existing response may be omitted
# Param 4 Possibilities array may be omitted
sub askq {
my $help = shift;
my $prompt = shift;
my $def = shift;
my $poss = shift;
# Manufacture full prompt
my $Pr = "$prompt ";
$Pr .= "(" . join('|', @$poss) . ") " if $poss;
$Pr .= "[$def]" if length($def) != 0;
$Pr .= ": ";
my $resp;
for (;;) {
$resp = getline($help, $Pr);
if (length($resp) == 0) {
return $def if length($def) != 0;
print "Please give a response\n";
next;
}
return $resp unless $poss;
# If part response find first match
for my $p (@$poss) {
return $p if length($resp) <= length($p) && substr(lc($p), 0, length($resp)) eq lc($resp);
}
print "Unknown response $resp\n";
}
}
# Ask yes or no help, prompt, default
sub askyorn {
my $help = shift;
my $prompt = shift;
my $def = shift;
$def = $def ? 'y': 'n';
my $ans = askq($help, $prompt, $def, [ 'y', 'n' ]);
$ans eq 'y';
}
# Ask number help, prompt, default
sub asknum {
my $help = shift;
my $prompt = shift;
my $def = shift;
for (;;) {
my $resp = getline($help, "$prompt [$def]: ");
return $def if length($resp) == 0;
return $resp if $resp =~ /^\d+$/;
print "Please give a numeric value\n";
}
}
# Ask and check IP address - give help and prompt
sub askip {
my $help = shift;
my $prompt = shift;
for (;;) {
my $dev = askq($help, $prompt);
return $dev if inet_aton($dev);
print "Invalid host/device $dev please try again\n";
}
}
# Ask port name or number
sub askport {
my $help = shift;
my $prompt = shift;
my $def = shift;
for (;;) {
my $port = askq($help, $prompt, $def);
return $port if $port =~ /^\d+$/;
return $port if getservbyname($port,"tcp");
print "Sorry unknown port $port\n";
}
}
# Ask linger time might be fractional
sub askling {
my $prompt = shift;
my $def = shift;
for (;;) {
my $resp = getline("linger", "$prompt [$def]: ");
return $def if length($resp) == 0;
return $resp if $resp =~ /^\d+(\.\d+)?$/;
print "Please give a numeric (possibly fractional)value\n";
}
}
# Adjust parameters
sub adjparam {
my ($res, $pname, $name, $def, $descr) = @_;
my $field = $res->{$pname};
# Second parameter gives argument type:default or ! for no questions
# Special cases are integer for just an integer
# Y or N for yes or no default that
if (ref($def) eq 'ARRAY') {
my @opts = @$def;
my $dflt = shift @opts;
if (defined($field->{$name}) && $field->{$name} ne $dflt) {
$descr .= "(default $dflt)";
$dflt = $field->{$name};
}
$field->{$name} = askq("param", $descr, $dflt, \@opts);
return;
}
return if $def =~ /!/; # No questions
# Yes or No case
if ($def =~ /^[yn]$/i) {
$field->{$name} = askyorn("paramyorn", $descr, uc($field->{$name}) eq 'Y')? 'Y': 'N';
}
elsif ($def =~ /^\d+$/) {
# Integer only case
# If updating offer current value as default and also show default
if (defined($field->{$name}) && $field->{$name} != $def) {
$descr .= "(default $def)";
$def = $field->{$name};
}
$field->{$name} = asknum('numparam', $descr, $def);
}
else {
# Alternative types of codes of form type:default
my ($code,$deflt) = split(':', $def);
# When updating show default and give existing value instead of default
if (length($deflt) != 0 && defined($field->{$name}) && $field->{$name} ne $deflt) {
$descr .= "(default $deflt)";
$deflt = $field->{$name};
}
if ($code eq 'p') { # Port number
$field->{$name} = askport('portnum', $descr, $deflt);
}
elsif ($code eq 'h') { # Host name
$field->{$name} = askip('ipaddr', $descr);
}
elsif ($code eq 'f') { # Fractional (linger time)
$field->{$name} = askling($descr, $deflt);
}
elsif ($code eq 's') { # String
$field->{$name} = askq("string", $descr, $deflt);
}
}
}
# Set up options we haven't got defined yet
sub init_def_options {
my $res = shift;
my $cls = shift;
for my $o (@{$Options->{$cls}}) {
my ($kw, $def) = @$o;
next if defined($res->{$cls}->{$kw});
next if $def =~ /!/;
my ($code,$deflt) = split(':', $def);
$res->{$cls}->{$kw} = length($deflt) > 0? $deflt: $code;
}
}
# Run over option list and ask questions
sub ask_optionlist {
my $res = shift;
my $cls = shift;
for my $o (@{$Options->{$cls}}) {
my ($name, $val, $descr) = @$o;
adjparam($res, $cls, $name, $val, $descr); # Need don't ask!!!!
}
}
# Get port number or apply default
sub getportordefault {
my $pname = shift;
my $def = shift;
my @plist = getservbyname($pname, 'tcp');
return $plist[2] if @plist;
$def;
}
# Look for program on PATH variable
sub findonpath {
my $prog = shift;
for my $p (split(':', $ENV{PATH})) {
next unless $p =~ m|^/|;
return "$p/$prog" if -x "$p/$prog";
}
undef;
}
########################################################################
#
# Spooler manips
#
########################################################################
# Is spooler running
sub isrunning {
system("$bindir/gspl-plist >/dev/null 2>&1") == 0;
}
# Parse master config file and set up %Config
sub parse_mconfig {
# First set up defaults as per build
$Config{SPOOLDIR} = eval "\"/var/spool/gnuspool\"";
$Config{SPROGDIR} = eval "\"${exec_prefix}/lib/gnuspool\"";
$Config{SDATADIR} = eval "\"${datarootdir}/gnuspool\"";
$Config{SPOOLPT} = eval "\"/etc/gnuspool/ptrconf\"";
# If there is a master config file relocating stuff read it in and
# appropriately reset
if (open(MC, "/etc/gnuspool/gnuspool.conf")) {
while (<MC>) {
chop;
s/#.*//;
next unless /(\w+)[:=](\S+)/;
$Config{$1} = $2;
}
close MC;
}
}
# Check user path set up.
# Otherwise add it to current PATH
sub check_userpath {
die "Cannot find splist in $bindir is this set up correctly?\n" unless -f "$bindir/gspl-plist";
for my $p (split(':', $ENV{'PATH'})) {
return if $p eq $bindir;
}
print "****Note: **** Adding $bindir to PATH\n";
$ENV{PATH} = "$bindir:$ENV{PATH}";
}
# Set up defaults for device
sub gendevdef {
my $def = shift;
return $def->[0] if ref($def) eq 'ARRAY';
return 'N' if $def eq 'Y';
return substr $def, 2 if $def =~ /^\w:/;
$def;
}
# Apply options for device
sub applyopt {
my $ptrres = shift;
my $wh = shift;
my $porttype = $ptrres->{PORT};
my $opt = $Options->{$wh};
my $ptopt = $ptrres->{$porttype};
for my $o (@$opt) {
my ($kw, $def, $expl, $sw) = @$o;
next if defined $ptopt->{$kw} or $def =~ /^!/;
$ptopt->{$kw} = $sw =~ /!/? 'N': gendevdef($def);
}
}
# Rejig read .device entries
sub parse_devtype {
my $ptrres = shift;
my $porttype = $ptrres->{PORT};
my $opt = $Options->{$porttype};
my $ptopt = $ptrres->{$porttype};
if ($porttype eq 'SERIAL') {
for my $b (5 .. 8) {
if (defined $ptopt->{"cs%b"}) {
$ptopt->{'csize'} = $b;
last;
}
}
$ptopt->{'stopbits'} = 2 if defined $ptopt->{'twostop'};
}
applyopt($ptrres, $porttype);
1;
}
# Parse network=command arguments in .device file to deduce options
sub parse_netwargs {
my $ptrres = shift;
my $nwcmd = shift;
my $porttype = $ptrres->{PORT};
# Canonicalise arguments to command
# Currently assume no quotes
my @rargs = split /\s+/, $nwcmd;
shift @rargs; # Get rid of program name
my @args;
while (@rargs) {
my $a = shift @rargs;
if ($a =~ /^(-.)(.+)/) {
push @args, $1, $2;
}
else {
push @args, $a;
}
}
my $ptopts = $ptrres->{$porttype}; # What we're trying to set
# Get ourselves a lookup table of option flags
my %fllu;
for my $o (@{$Options->{$porttype}}) {
my $fl = $o->[3];
$fl = substr $fl, 1 if (substr $fl, 0) eq '!'; # Cover invert case ('resp' on LPDNET)
$fllu{$fl} = $o;
}
while (@args) {
my $arg = shift @args;
next unless $arg =~ /^-([.])/;
my $o = $fllu{$1};
next unless defined $o;
my ($name, $def, $expl, $fl) = @$o;
# Y or N options don't take an argument and check for invert response
$ptopts->{$name} = $def !~ /^[YN]$/? shift @args: $fl =~ /!/? 'N': 'Y';
}
}
# Parse network=command in .device file
sub parse_netwcmd {
my $ptrres = shift;
my $nwcmd = shift;
my $porttype = $ptrres->{PORT};
applyopt($ptrres, 'STDNET');
my %ckprog = (LPDNET => 'xtlpc', FTP => 'xtftp', XTLHP => 'xtlhp', TELNET => 'xtelnet');
if (defined $ckprog{$porttype}) {
return 0 unless $nwcmd =~ /$ckprog{$porttype}/;
parse_netwargs($ptrres, $nwcmd);
}
else {
$ptrres->{$porttype} = { COMMAND => $nwcmd };
}
# Cover things not defined
applyopt($ptrres, $porttype) if defined $Options->{$porttype};
1;
}
# Parse .device file
sub parse_device {
my $ptrres = shift;
my $printername = $ptrres->{PRINTER};
my $file = shift;
my $porttype;
my $nwcmd;
my %portopts;
if (open(DVF, $file)) {
while (<DVF>) {
chop;
if (/#\s*Porttype:\s*(\w+)/) {
$porttype = uc $1;
$porttype = $Canonports->{$porttype} unless defined $Options->{$porttype};
$ptrres->{PORT} = $porttype;
}
elsif (/^(\w+)(?:\s+(\w+))?\s*$/) {
$portopts{$1} = $2? $2: 'Y';
}
elsif (/^network=(.*)/) {
$nwcmd = $1;
}
}
close DVF;
}
# If we didn't get a port type try to find out from keywords
unless ($porttype) {
if ($nwcmd) {
if ($nwcmd =~ /xtlpc/) {
$porttype = 'LPDNET';
}
elsif ($nwcmd =~ /xtftp/) {
$porttype = 'FTP';
}
elsif ($nwcmd =~ /xtlhp/) {
$porttype = 'XTLHP';
}
elsif ($nwcmd =~ /xtelnet/) {
$porttype = 'TELNET';
}
else {
$porttype = 'OTHER';
}
}
else {
if (defined($portopts{'baud'}) || defined($portopts{'parenb'})) {
$porttype = 'SERIAL';
}
else {
$porttype = 'PARALLEL';
}
}
}
return 0 if $porttype =~ /Custom/i;
$ptrres->{PORT} = $porttype;
$ptrres->{$porttype} = \%portopts;
if ($porttype =~ /PARALLEL|SERIAL|USB/) {
if ($nwcmd) {
print "***Warning network print command for $printername\ndefined as $porttype - $file must have been edited\n";
return 0;
}
return 0 unless parse_devtype($ptrres);
}
else {
unless ($nwcmd) {
print "***Warning no network print command for $printername\n defined as $porttype - $file must have been edited\n";
return 0;
}
return 0 unless parse_netwcmd($ptrres, $nwcmd);
}
1;
}
# Parse default file
sub parse_default {
my $ptrres = shift;
my $printername = $ptrres->{PRINTER};
my $file = shift;
my ($emul, $model);
if (open(DFF, $file)) {
while (<DFF>) {
chop;
if (/#\s*Ptrtype:\s*(.*)/) {
$emul = $1;
}
elsif (/#\s*Set up for\s*(.*)/) {
$model = $2;
}
elsif (/#\s*Postscript emulation/) {
$ptrres->{HASPS} = 'Y';
}
elsif (/#\s*Using ghostscript/ && findonpath('gs')) {
$ptrres->{GS} = 'Y';
}
elsif (/#\s*Paper:\s*(\w+)/) {
$ptrres->{GSSIZE} = $1;
}
elsif (/^(\w+)$/) {
$ptrres->{STDOPTS}->{$1} = 'Y';
}
elsif (/^-(\w+)$/) {
$ptrres->{STDOPTS}->{$1} = 'N';
}
}
close DFF;
}
return 0 if $emul =~ /Custom/i;
$ptrres->{TYPE} = $emul if $emul;
$ptrres->{MODEL} = $model if $model;
for my $o (@{$Options->{STDOPTS}}) {
my ($kw, $def, $expl) = @$o;
next if defined $ptrres->{STDOPTS}->{$kw};
$ptrres->{STDOPTS}->{$kw} = gendevdef($def);
}
1;
}
# List defined printers
sub list_defptrs {
my $ptrdir = $Config{SPOOLPT};
my %plist;
my %clist;
if (opendir(PTDIR, $ptrdir)) {
while (my $de = readdir(PTDIR)) {
my $sd = "$ptrdir/$de";
next if $de =~ /^[-.]/ || ! -d $sd;
if (-l $sd) {
my $lc = readlink($sd);
next if $lc =~ m|/| || $lc eq $de;
$clist{$de} = $lc;
}
else {
next unless -f "$sd/default";
my $rp = { PRINTER => $de };
if (-f "$sd/.device") {
next unless parse_device($rp, "$sd/.device");
}
else {
next unless parse_device($rp, "$sd/default");
}
next unless parse_default($rp, "$sd/default");
$plist{$de} = $rp;
}
}
closedir(PTDIR);
}
for my $c (keys %clist) {
my $ct = $clist{$c};
while (defined $clist{$ct}) {
$ct = $clist{$ct};
}
$plist{$c} = { TYPE => 'Clone', CLONEOF => $ct, PRINTER => $c };
}
%plist;
}
# Get printers if thing running
sub getptrs_online {
my $tempfile = "tmppl$$";
unless (system("$bindir/gspl-plist -N -l -F '%p:%d:%e' >$tempfile") == 0 && open(TF, $tempfile)) {
unlink $tempfile;
return undef;
}
my %result;
while (<TF>) {
chop;
my ($ptr,$dev,$descr) = split('\s*:\s*');
$result{$ptr} = { DESCR => $descr };
if ($dev =~ /<(.*)>/) {
$dev = $1;
$result{$ptr}->{NETWORK} = 'Y';
}
$result{$ptr}->{DEV} = $dev;
}
close TF;
unlink $tempfile;
%result;
}
# Get printers if scheduler not running
sub getptrs_offline {
my $spooldir = $Config{SPOOLDIR};
my $tempfile = "tmppl$$";
my $cplist = "$sbindir/gspl-cplist";
my $ptrfile = "$spooldir/spshed_pfile";
return undef unless -x $cplist && -r $ptrfile;
return undef unless system("$cplist $ptrfile $tempfile") == 0;
return undef unless open(TF, $tempfile);
my %result;
while (<TF>) {
chop;
next unless my ($net, $dev, $descr, $ptr) = /^gspl-padd\s+-\w\s+-(\w)\s+-\w\s+[-A-Pa-p]+\s+-l\s+'(.*?)'\s+-D\s+'(.*?)'\s+(\w+)\s+'.*'/;
$result{$ptr} = { DEV => $dev, DESCR => $descr };
$result{$ptr}->{NETWORK} = 'Y' if $net eq 'N';
}
close TF;
unlink $tempfile;
%result;
}
# Page through printers list
sub pagedisp_ptrs {
open(OUT, "|$pager");
underline(\*OUT, "Installed printers", "=");
for my $p (sort keys %Ptrdeflist) {
my $pd = $Ptrdeflist{$p};
next unless $pd->{INSTALLED} eq 'Y';
underline(\*OUT, $p, '-');
my @ao;
push @ao, [ "Description:", $pd->{DESCR} ];
push @ao, [ "Printer emulation:", $pd->{TYPE} ];
if ($pd->{NETWORK}) {
push @ao, [ "Address/Host:", $pd->{DEV} ];
push @ao, [ "Protocol:", $pd->{PORT} ];
}
else {
push @ao, [ "Port type:", $pd->{PORT} ];
push @ao, [ "Device:", $pd->{DEV} ];
}
multicol(\*OUT, \@ao);
print OUT "\n\n";
}
my @dnoti;
for my $p (sort keys %Ptrdeflist) {
push @dnoti, $p unless $Ptrdeflist{$p}->{INSTALLED} eq 'Y';
}
if (@dnoti) {
underline(\*OUT, "Defined but not Installed", "=");
for my $p (@dnoti) {
my $pd = $Ptrdeflist{$p};
underline(\*OUT, $p, '-');
my @ao;
push @ao, [ "Printer emulation:", $pd->{TYPE} ];
push @ao, [ "Port type:", $pd->{PORT} ];
multicol(\*OUT, \@ao);
print OUT "\n\n";
}
}
close OUT;
}
# List parallel ports.
sub list_parports {
my $all = shift;
my @lps = glob('/dev/lp[0-9]');
my @res;
for my $lp (@lps) {
next unless -c $lp;
my ($num) = $lp =~ m|/dev/lp(\d)|;
my $pd = "/proc/sys/dev/parport/parport$num/autoprobe";
unless (open(PD, $pd)) {
push @res, { DEV => $lp } if $all;
next;
}
while (<PD>) {
next unless /MODEL:\s*(.*);/;
push @res, { DEV => $lp, MODEL => $1, NAME => $1 };
}
close PD;
}
\@res;
}
# Try to connect to a port
sub tryconnect {
my $host = shift;
my $port = shift;
my $proto = getprotobyname('tcp');
my $sin = sockaddr_in($port, inet_aton($host));
socket(SOCKFD, PF_INET, SOCK_STREAM, $proto);
my $ret = connect(SOCKFD, $sin);
close SOCKFD;
$ret;
}
# Get type of network connection
sub getnetworkconn {
my $res = shift;
my $ptr = $res->{PRINTER};
$res->{NETWORK} = 'Y';
$res->{DEV} = askip("ptrip", "Please give the host name or IP address of the $ptr");
if (askyorn("isconn", "Is $ptr connected, if so can I probe it", 1)) {
# Case where we can probe the printer
my @oports;
for my $prt (["LPD", "LPD", 515], ["Telnet", "Reverse telnet", 9100], ["FTP", "FTP", 21]) {
if (tryconnect($res->{DEV}, $prt->[2])) {
push @oports, $prt;
$res->{TRYSNMP} = 1 if $prt->[2] == 9100;
}
}
if ($#oports >= 0) {
# One or more ports active
$res->{ALIVE} = 'Y';
if ($#oports == 0) {
# Only one port available
if (askyorn("onlyconn", "$ptr seems to have $oports[0]->[1] connection available. Use that", 1)) {
my $p = uc $oports[0]->[0];
$p = $Canonports->{$p} unless defined $Options->{$p};
$res->{PORT} = $p;
return;
}
print "Sorry confused about this printer please try again\n";
exit 0;
}
# Two or more available
my @names;
map { push @names, $_->[1]; } @oports;
push @names, "None of those";
print "Your printer offers several protocols for sending data\n";
my $ans = askq("proto", "Which to use", $names[0], \@names);
for my $a (@oports) {
if ($ans eq $a->[1]) {
my $p = uc $a->[0];
$p = $Canonports->{$p} unless defined $Options->{$p};
$res->{PORT} = $p;
return;
}
}
$res->{PORT} = "Other";
return;
}
}
# Have to ask
print <<EOT;
I need to ask you about the protocol for speaking to your printer.
I don't mean TCP/IP - that's the lower level "connection" protocol.
Protocols might be:
LPD protocol - the most common
Reverse Telnet - with and without "SNMP" including JetDirect
FTP - available on some printers
Some other protocol with a printer interface program which
you must tell me about.
All of the above may have different options to select different
printers - this is especially true of multi-port devices.
Sorry but we cannot bypass this question!
EOT
my $ptype = askq("proto", "Please give protocol type", "LPD", [ "LPD", "Reverse Telnet", "FTP", "Other" ]);
$ptype = uc $ptype;
$ptype = $Canonports->{$ptype} unless defined $Options->{$ptype};
$res->{PORT} = $ptype;
}
# Get standard network params
sub getstdnetparam {
my $res = shift;
init_def_options($res, STDNET);
ask_optionlist($res, STDNET) if askyorn("adjstd", "Do you want to adjust the standard network parameters", 0);
}
# Get LPD-specific network params
sub getlpdparam {
my $res = shift;
init_def_options($res, LPDNET);
if (askyorn("adjlpd", "Do you want to adjust the LPD parameters", 0)) {
$res->{LPDNET}->{outip} = askip("outiph", "Outgoing IP/host") if askyorn("outip", "Do you need to give a different outgoing IP address/host", 0);
$res->{LPDNET}->{lpdname} = askq("destptr", "Destination printer name") if askyorn("diffptr", "Does your interface require a different printer name than $res->{PRINTER}");
ask_optionlist($res, LPDNET);
}
}
# Get telnet-specific network params
sub gettelnetparam {
my $res = shift;
my $type = askyorn("usnmp", "Use SNMP", 1)? "XTLHP": "TELNET";
$res->{PORT} = $type;
init_def_options($res, $type);
$res->{$type}->{myhost} = askip('outiph', "Outgoing IP/host") if $type eq "XTLHP" && askyorn("outip", "Do you need to give a different outgoing IP address/host", 0);
ask_optionlist($res, $type) if askyorn("adjtelnet", "Do you want to adjust the telnet parameters", 0);
}
# Get FTP-specific network params
sub getftpparam {
my $res = shift;
init_def_options($res, FTP);
ask_optionlist($res, FTP) if askyorn("adjftp", "Do you want to adjust the FTP parameters", 0);
}
# Get other network params
sub getotherparam {
my $res = shift;
my $list = $Config{SDATADIR} . "/Psetups/servers.list";
my %servs;
if (open(SLIST, $list)) {
while (<SLIST>) {
chop;
s/#.*//;
next unless /^(\w+)\s*:\s*(.*)/;
my $descr = $1;
my $cmd = $2;
next if $cmd =~ /SPROGDIR/;
my $prog = $cmd;
$prog =~ s/\s+.*//;
next unless -x $prog;
$servs{$descr} = $cmd;
}
close SLIST;
if (%servs) {
my @poss = sort keys %servs;
if ($#poss == 0) {
if (askyorn("possdriv", "You seem to have a $poss[0] driver - use this", 1)) {
$res->{PORT} = $poss[0];
$res->{$poss[0]} = {COMMAND => $servs{$poss[0]}};
return;
}
}
else {
print "Possible network drivers: @poss\n";
if (askyorn("possdrivm", "Use one of those", 1)) {
my $drv = askq("driver", "Select driver", $poss[0], \@poss);
$res->{PORT} = $drv;
$res->{$drv} = {COMMAND => $servs{$drv} };
}
}
}
}
my $port = $res->{PORT};
print <<EOM;
I shall have to ask you what command or driver is used to send data
to the network. It should be something that takes data on standard input
and sends it to the printer.
Please abort this and contact us if you have any queries.
EOM
my $prog = askq("pprogram", "Program to send to $res->{PRINTER}");
if ($prog =~ m|^/|) {
exit 0 unless -x $prog || askyorn("noprogf", "$prog not found on path - are you sure", 0);
}
else {
exit 0 unless findonpath($prog) || askyorn("noprog", "$prog not found on path - are you sure", 0);
}
print "Please give the arguments for $prog - specify the host name with \$SPOOLDEV\n";
my $args = askq("pprogargs", "Arguments for $prog");
$res->{$port} = {COMMAND => "$prog $args"};
}
# Get network parameters according to type
sub getnetworkparam {
my $res = shift;
my $update = shift;
$res->{EDITED} = 'Y';
getnetworkconn($res) unless $update && !askyorn("chnet", "Change network connection details", 0);
getstdnetparam($res) unless $update && !askyorn("chbasic", "Change basic network connection parameters", 0);
my $port = $res->{PORT};
if ($port eq 'LPDNET') {
getlpdparam($res) unless $update && !askyorn("chlpd", "Change LPD-specific parameters", 0);
}
elsif ($port eq 'TELNET') {
gettelnetparam($res) unless $update && !askyorn("chtelnet", "Change Telnet-specific paramters", 0);
}
elsif ($port eq 'FTP') {
getftpparam($res) unless $update && !askyorn("chftp", "Change FTP-specific parameters", 0);
}
else {
getotherparam($res) unless $update && !askyorn("chnetcomm", "Change network command specific paramters", 0);
}
}
# Get device parameters
sub getdevparam {
my $res = shift;
my $ptype = shift;
my $update = shift;
$ptype = uc $ptype;
my $des = lc $ptype;
$res->{PORT} = $ptype;
$res->{EDITED} = 'Y';
init_def_options($res, $ptype);
ask_optionlist($res, $ptype) if askyorn("chport", "Do you want to adjust the $des port parameters", 0);
}
# Discover interface type and apply
sub getinterface {
my $res = shift;
my $update = shift;
my $type;
if ($update) {
$type = $res->{PORT};
if (askyorn("chinter", "Do you want to change the interface type, currently $type", 0)) {
undef $res->{$type};
undef $res->{NETWORK};
undef $res->{STDNET};
$res->{EDITED} = 'Y';
$type = askq("newconntype", "New connection type", "Network", [ "Serial", "Parallel", "USB", "Network"]);
}
elsif ($res->{NETWORK} eq 'Y') {
getnetworkparam($res, 1);
return;
}
else {
getdevparam($res, $type, 1);
return;
}
}
else {
$type = askq("connect", "How is $res->{PRINTER} connected", "Network", [ "Serial", "Parallel", "USB", "Network" ]);
}
if ($type eq 'Network') {
getnetworkparam($res, 0);
}
else {
getdevparam($res, $type, 0);
}
}
# For linux installs where we notice a printer attached to parallel port early on
sub install_parallel {
my ($ptr, $dev, $model) = @_;
my $ptrdets = { PRINTER => $ptr, DEV => $dev, NAME => $model };
getdevparam($ptrdets, 'PARALLEL', 0);
askptremul($ptrdets) unless getptremul($ptrdets);
getspoolopts($ptrdets);
write_devfile($ptrdets);
write_defaultfile($ptrdets);
$Ptrdeflist{$ptr} = $ptrdets;
proc_install($ptr) if isrunning;
}
# Display printer
sub dispptr {
my $ptr = shift;
return ( $ptr->{DEV}, $ptr->{NAME} ) if $ptr->{NAME};
($ptr->{DEV}, "Unknown printer type");
}
# Get linux parallel port info
sub getlinuxparallel {
my $res = shift;
my $update = shift;
my $pplist = list_parports(1);
my @nilist;
for my $p (@$pplist) {
push @nilist, $p unless !$update && $Idevs{$p->{DEV}};
}
if ($#nilist < 0) {
print "There do not seem to be any parallel ports available\n";
exit 0 unless askyorn("confpara", "Are you sure", 0);
}
elsif ($#nilist == 0) {
my $p = $nilist[0];
my $j = join(' which is ', dispptr($p));
if (askyorn("usepara", "Use $j", 1)) {
$res->{DEV} = $p->{DEV};
$res->{NAME} = $p->{NAME} if $p->{NAME};
}
}
else {
my @short;
my %wh;
print "Possible printers and types:\n\n";
for my $s (@nilist) {
print join(' which is ', dispptr($s)), "\n";
my $sh = $s->{DEV};
$sh =~ s/\/dev\///;
push @short, $sh;
$wh{$sh} = $s;
}
my $dev = askq("ptrtype", "Please select device", $short[0], \@short);
$dev = $wh{$dev};
$res->{DEV} = $dev->{DEV};
$res->{NAME} = $dev->{NAME} if $dev->{NAME};
}
}
# Get printer type using SNMP
sub getsnmptype {
my $res = shift;
my $getsnmp = "$sbindir/gspl-getsnmp";
return unless -x $getsnmp;
my $nm = `$getsnmp -N -h $res->{DEV} 1.3`;
return unless length($nm) > 0;
chop $nm;
print "Your printer appears to be a $nm\n";
$res->{NAME} = $nm;
}
# Ask printer type
sub askptrtype {
my $res = shift;
my $list = $Config{SDATADIR} . "/Psetups/printers.list";
if (-f $list && $Pmenu) {
print "First please give the type of the printer.\nIt may be listed in the list we supply\n";
if (askyorn("trylist", "Try supplied list", 1)) {
open(LIST, $list);
my $tf = "pl$$";
open(TF, ">$tf");
while (<LIST>) {
chop;
s/#.*//;
next unless /:/;
my ($ptr,$emul,$ijs,$col) = split(/\s*:\s*/);
print TF $ptr,"\n";
}
close LIST;
close TF;
$resline = `$Pmenu -f $tf -h'Please select printer type and press ENTER or Q to exit'`;
chomp $resline;
unlink $tf;
$res->{NAME} = $resline if length($resline) != 0;
}
}
$res->{NAME} = askq("pmodel", "Please give printer name/model") unless $res->{NAME};
}
# Ask if OK to use Ghostscript
sub askusegs {
my $res = shift;
return unless $res->{TYPE} =~ /ljet|pcl/i;
$res->{TYPE} = 'PCL';
if (findonpath('gs') && askyorn('usegs', 'Use ghostscript for PS (even if printer supports it)', 1)) {
unless (defined $res->{MODEL}) {
if (askyorn("cptr", "Does this printer print colour", 0)) {
$res->{MODEL} = 'HP Color LaserJet';
$res->{COLOUR} = 'Y';
}
else {
$res->{MODEL} = 'HP LaserJet 6';
}
}
$res->{GS} = 'Y';
$res->{GSSIZE} = askq("psize", "Standard paper size", "a4", [ "a4", "11x7", "letter", "legal", "ledger", "note"]);
$res->{DEFCOLOUR} = 'Y' if $res->{COLOUR} eq 'Y' && askyorn("defcolour", "Print colour by default", 0);
}
}
# Get printer emulation
sub getptremul {
my $res = shift;
my $name = $res->{NAME};
my $list = $Config{SDATADIR} . "/Psetups/printers.list";
return 0 unless open(LIST, $list);
my $ret = 0;
while (<LIST>) {
chop;
s/#.*//;
next unless /:/;
my ($ptr,$emul,$ijs,$col) = split(/\s*:\s*/);
if (lc $ptr eq lc $name) {
$res->{TYPE} = $emul;
$res->{MODEL} = $ijs if $ijs;
$res->{HASPS} = 'Y' if $emul =~ /ps/i;
$res->{COLOUR} = 'Y' if $col eq 'C';
$ret = 1;
last;
}
}
close LIST;
askusegs($res) if $ret;
$ret;
}
# Ask printer emulation
sub askptremul {
my $res = shift;
$res->{NAME} = askq("pmodel", "Please give printer name/model") unless $res->{NAME};
$res->{TYPE} = askq("pemul", "Please give emulation", "PCL", [ "Epson", "PCL", "IBM", "Other" ]);
if ($res->{TYPE} eq 'PCL') {
$res->{HASPS} = 'Y' if askyorn('emps', "Does printer emulate PostScript also", 0);
askusegs($res);
}
}
# Get type of printer
sub getptrtype {
my $res = shift;
my $update = shift;
$res->{EDITED} = 'Y';
getlinuxparallel($res, $update) if $res->{PORT} eq 'PARALLEL' && $Systemtype eq 'Linux';
getsnmptype($res) if $res->{NETWORK} eq 'Y' && $res->{ALIVE} eq 'Y' && $res->{TRYSNMP};
askptrtype($res) unless $res->{NAME};
askptremul($res) unless getptremul($res);
}
# Get header per copy parameter
sub sethdrpercopy {
my $res = shift;
my $cl = shift;
$res->{$cl}->{hdrpercopy} = askyorn('banneach', 'Do you want banners on each copy of multiple copies', 0)? 'Y': 'N';
}
# Get the various header options
sub gethdropts {
my $res = shift;
my $islpd = shift;
my $cl = $islpd? 'LPDOPTS': 'STDOPTS';
if (askyorn('userbann', 'Do you want users to set on/off banner pages themselves', !$islpd)) {
sethdrpercopy($res, $cl);
}
elsif (askyorn('nohdr', 'Do you want to force banner pages off', $islpd)) {
$res->{$cl}->{hdrpercopy} = 'N';
$res->{$cl}->{nohdr} = 'Y';
$res->{$cl}->{forcehdr} = 'N';
}
else {
$res->{$cl}->{nohdr} = 'N';
$res->{$cl}->{forcehdr} = 'Y';
sethdrpercopy($res, $cl);
}
}
# Copy the options on one name to another name
# This is used for shuffling between "STDOPTS" and "LPDOPTS"
sub copyopts {
my $res = shift;
my $old = shift;
my $new = shift;
for my $k (keys %{$res->{$old}}) {
$res->{$new}->{$k} = $res->{$old}->{$k};
}
}
# Get spooler options
sub getspoolopts {
my $res = shift;
my $update = shift;
if ($res->{PORT} eq 'LPDNET') {
copyopts($res, 'STDOPTS', 'LPDOPTS');
init_def_options($res, 'LPDOPTS');
init_def_options($res, 'LPDHDROPTS');
gethdropts($res, 1) if askyorn('chbann', 'Adjust banner printing options', 0);
ask_optionlist($res, 'LPDOPTS') if askyorn('chmisc', 'Adjust miscellaneous options', 0);
copyopts($res, 'LPDOPTS', 'STDOPTS');
}
else {
init_def_options($res, 'STDOPTS');
init_def_options($res, 'STDHDROPTS');
gethdropts($res, 0) if askyorn('chbann', 'Adjust banner printing options', 0);
ask_optionlist($res, 'STDOPTS') if askyorn('chmisc', 'Adjust miscellaneous options', 0);
}
}
# Write out device file
sub out_device {
my $file = shift;
my $ptrdets = shift;
my $porttype = $ptrdets->{PORT};
my $optv = $Options->{$porttype};
my $optp = $ptrdets->{$porttype};
for my $o (@$optv) {
my ($kw, $def, $expl) = @$o;
if (defined $optp->{$kw}) { # Take as always defined
print $file "\n# $expl\n";
my $val = $optp->{$kw};
print $file "#" if $val eq 'N';
if ($val =~ /^[YN]$/) {
print $file "$kw\n";
}
elsif ($kw eq 'csize') {
print $file "cs$val\n";
}
elsif ($kw eq 'stopbits') {
print $file '#' if $val != 2;
print $file "twostop\n";
}
else {
print $file "$kw $val\n";
}
}
}
}
# Write out network command
sub out_network {
my $file = shift;
my $ptrdets = shift;
my $porttype = $ptrdets->{PORT};
my $optv = $Options->{STDNET};
my $optp = $ptrdets->{STDNET};
for my $o (@$optv) {
my ($kw, $def, $expl) = @$o;
if (defined $optp->{$kw}) { # Take as always defined
print $file "\n# $expl\n";
my $val = $optp->{$kw};
print $file "#" if $val eq 'N';
if ($val =~ /^[YN]$/) {
print $file "$kw\n";
}
else {
print $file "$kw $val\n";
}
}
}
$optv = $Options->{$porttype};
$optp = $ptrdets->{$porttype};
if ($porttype eq 'OTHER') {
print $file "\n# Network command to print as specified\n";
print $file "network=$optp->{COMMAND}\n";
}
else {
my $sprog = $Config{SPROGDIR};
my $sdata = $Config{SDATADIR};
print $file "\n# Network command for protocol type\n";
my %ckprog = (LPDNET => 'xtlpc', FTP => 'xtftp', XTLHP => 'xtlhp', TELNET => 'xtelnet');
print $file "network=\${SPROGDIR:-$sprog}/$ckprog{$porttype}";
for my $o (@$optv) {
my ($kw, $def, $expl, $arg, $progdef) = @$o;
my $val = $optp->{$kw};
if (defined $val) {
if ($val eq 'N') {
print $file " -$1" if $arg =~ /!(.)/;
}
elsif ($val eq 'Y') {
print $file " -$arg" unless $arg =~ /!/;
}
elsif (length($val) > 0) {
next if $val =~ /^\d+$/ && $progdef =~ /^\d+$/ && $val == $progdef;
print $file " -$arg $val" if $val ne $progdef;
}
}
elsif (!ref($def) && $def =~ /^!(.*)/) {
my $str = $1;
if (length($str) > 1) {
$str =~ s/SPROGDIR/\${SPROGDIR:-$sprog}/;
$str =~ s/SDATADIR/\${SDATADIR:-$sdata}/;
print $file " -$arg $str";
}
}
}
print $file "\n";
}
}
# Write .device file
sub write_devfile {
my $ptrdets = shift;
my $printer = $ptrdets->{PRINTER};
my $porttype = $ptrdets->{PORT};
my $ptrdir = $Config{SPOOLPT};
my $dirname = "$ptrdir/$printer";
unless (-d $dirname) {
unless (mkdir $dirname, 0775) {
print "Sorry cannot create directory $dirname for printer\n";
exit 1;
}
chown $Spooluid, $Spoolgid, $dirname;
}
my $filename = "$dirname/.device";
unless (open(DEVF, ">$filename")) {
print "Sorry cannot create $filename\n";
exit 2;
}
chown $Spooluid, $Spoolgid, $filename;
my @tbits = localtime;
my $dat = sprintf "%.2d/%.2d/%.2d", $tbits[3], $tbits[4]+1, $tbits[5]%100;
my $pt = ucfirst lc $porttype;
print DEVF <<EOF;
# Device file created $dat
# Porttype: $pt
#
# This file contains parameters for the above interface.
# It is read in ahead of any "setup file" such as "default"
# and should contain interface-only features.
#
# Please do not edit this file directly without changing "Porttype" above
# to "custom".
EOF
if ($porttype =~ /PARALLEL|SERIAL|USB/) {
out_device(\*DEVF, $ptrdets);
}
else {
out_network(\*DEVF, $ptrdets);
}
close DEVF;
}
# Output segment of file
sub out_splurge {
my $fileout = shift;
my $filein = shift;
my $ptrdets = shift;
my $sprog = $Config{SPROGDIR};
my $sdata = $Config{SDATADIR};
my $path = "$sdata/Psetups/$filein.pi";
my $gssize = $ptrdets->{GSSIZE};
my $model = $ptrdets->{MODEL};
my $gs = findonpath('gs');
my $ijs = findonpath('hpijs');
if (open(EP, $path)) {
while (<EP>) {
s/SPROGDIR/\${SPROGDIR:-$sprog}/;
s/SDATADIR/\${SDATADIR:-$sdata}/;
s/\bGS\b/$gs/;
s/\bIJS\b/$ijs/;
s/\bGSSIZE\b/$gssize/;
s/\bMODEL\b/$model/;
print $fileout $_;
}
close EP;
}
else {
print $fileout "# Sorry didn't find $filein template\n";
}
}
# Output stuff for epson
sub out_epson {
my $file = shift;
my $ptrdets = shift;
out_splurge($file, "Epson", $ptrdets);
}
# Output stuff for PCL
sub out_pcl {
my $file = shift;
my $ptrdets = shift;
out_splurge($file, "PCL1", $ptrdets);
if ($ptrdets->{GS} eq 'Y') {
my $pcl = "PCL2";
if ($ptrdets->{COLOUR} eq 'Y') {
$pcl .= "dbw" unless $ptrdets-{DEFCOLOUR} eq 'Y';
}
else {
$pcl .= "bw";
}
out_splurge($file, $pcl, $ptrdets);
}
elsif ($ptrdets->{HASPS} eq 'Y') {
out_splurge($file, "PCLPS", $ptrdets);
}
else {
out_splurge($file, "PCLnoPS", $ptrdets);
}
}
# Output stuff for IBM
sub out_ibm {
my $file = shift;
my $ptrdets = shift;
out_splurge($file, "IBM", $ptrdets);
}
# Output stuff for "Other"
sub out_other {
my $file = shift;
my $ptrdets = shift;
print $file "# This is a null file for future adjustment\n";
}
# Create "default" file for form type.
sub write_defaultfile {
my $ptrdets = shift;
my $printer = $ptrdets->{PRINTER};
my $ptrdir = $Config{SPOOLPT};
my $ptrtype = $ptrdets->{TYPE};
my $dirname = "$ptrdir/$printer";
my $filename = "$dirname/default";
unless (open(DEFF, ">$filename")) {
print "Sorry cannot create $filename\n";
exit 3;
}
chown $Spooluid, $Spoolgid, $filename;
print DEFF <<EOF;
# Ptrtype: $ptrtype
# This is the "default" setup file for $printer.
# It is used to handle forms for which there isn't a specific setup file given
# with the name of the formtype (most of the time).
# If you make changes to the file other than with this program
# please change the printer type above to "Custom"
# so that your changes don't get wiped out.
EOF
print DEFF "# Ptrname: $ptrdets->{NAME}\n" if $ptrdets->{NAME};
print DEFF "# Set up for $ptrdets->{MODEL}\n" if $ptrdets->{MODEL};
print DEFF "# Postscript emulation\n" if $ptrdets->{HASPS} eq 'Y';
print DEFF "# Using ghostscript\n" if $ptrdets->{GS} eq 'Y';
print DEFF "# Paper: $ptrdets->{GSSIZE}\n" if $ptrdets->{GSSIZE};
print DEFF <<EOF;
# The following options relate to header pages - the default settings
# "nohdr" means that header (banner) pages are never printed whatever
# a user may set on his/her job.
# "forcehdr" means that headers are always printed whatever the user
# may set.
# Note that you can restore the defaults with either -nohdr or -forcehdr.
# You may want this for specific suffixes.
# "hdrpercopy" forces a header on each of multiple copies
EOF
my $optp = $ptrdets->{STDOPTS};
if ($optp->{nohdr} eq 'Y') {
print DEFF "nohdr\n";
}
elsif ($optp->{forcehdr} eq 'Y') {
print DEFF "forcehdr\n";
}
print DEFF "hdrpercopy\n" if $optp->{hdrpercopy} eq 'Y';
my $optv = $Options->{STDOPTS};
for my $o (@$optv) {
my ($kw, $def, $expl) = @$o;
print DEFF "\n# $expl\n";
print DEFF "#" unless $optp->{$kw} eq 'Y';
print DEFF "$kw\n";
}
if ($ptrtype eq 'Epson') {
out_epson(\*DEFF, $ptrdets);
}
elsif ($ptrtype eq 'PCL') {
out_pcl(\*DEFF, $ptrdets);
}
elsif ($ptrtype eq 'IBM') {
out_ibm(\*DEFF, $ptrdets);
}
else {
out_other(\*DEFF, $ptrdets);
}
close DEFF;
}
sub makeclone {
my $newptr = shift;
my $oldptr = shift;
my $ptrdir = $Config{SPOOLPT};
symlink $oldptr, "$ptrdir/$newptr" or die "Cannot create link";
$Ptrdeflist{$newptr} = { TYPE => 'Clone', CLONEOF => $oldptr, PRINTER => $newptr };
proc_install($newptr) if askyorn('installptr', "Install $newptr", 1);
}
# Create printer def
sub proc_create {
my $Insptr = shift;
if (askyorn('makeclone', "Do you want $Insptr to be a clone of another printer", 0)) {
my @possp;
for my $p (sort keys %Ptrdeflist) {
push @possp, [ $p, $Ptrdeflist{$p}->{DEV} ] if $Ptrdeflist{$p}->{TYPE} ne 'Clone';
}
if ($#possp < 0) {
print "Sorry no printers yet to clone\n";
exit 0;
}
if ($#possp == 0) {
my $cl = $possp[0]->[0];
makeclone($Insptr, $cl) if askyorn('makecloneof', "Make clone of $cl", 1);
exit 0;
}
if ($#possp < 4 || !$Pmenu) {
my @pnames;
for my $p (@possp) {
push @pnames, $p->[0];
}
makeclone($Insptr, askq("w2clone", "Which printer", $pnames[0], \@pnames));
}
else {
my $tf = "pl";
open(TF, ">$tf");
for my $p (@possp) {
print TF "$p->[0]:$p->[1]\n";
}
close TF;
my $resline = `$Pmenu -f $tf -d : -n -h 'Please select printer to clone'`;
chomp $resline;
unlink $tf;
makeclone($Insptr, $possp[$resline-1]) if length($resline) != 0;
}
exit 0;
}
else {
my $ptrdets = { PRINTER => $Insptr };
getinterface($ptrdets, 0);
getptrtype($ptrdets, 0);
getspoolopts($ptrdets, 0);
write_devfile($ptrdets);
write_defaultfile($ptrdets);
$Ptrdeflist{$Insptr} = $ptrdets;
}
}
# Modify printer def
sub proc_modify {
my $Insptr = shift;
my $spoolrunning = shift;
my $isinst = shift;
# Get the existing description
my $ptrdets = $Ptrdeflist{$Insptr};
if ($ptrdets->{TYPE} eq 'Clone') {
return unless askyorn('splitclone', "As this printer is a clone, do you want to split it", 1);
my $ptrdir = $Config{SPOOLPT};
my $cloned = $ptrdets->{CLONEOF};
unlink "$ptrdir/$Insptr";
system("chdir $ptrdir;cp -r -p $cloned $Insptr");
}
else {
getinterface($ptrdets, 1) if askyorn('chinter', "Adjust interface details", 0);
getptrtype($ptrdets, 1) if askyorn('chtype', "Adjust type of printer", 0);
getspoolopts($ptrdets, 1) if askyorn('chspool', "Adjust spool options", 0);
return if $ptrdets->{EDITED} ne 'Y';
write_devfile($ptrdets);
write_defaultfile($ptrdets);
}
}
sub setdefform {
my $form = shift;
return if length($form) == 0;
system("$bindir/gspl-uchange -D -f $form -F $form -A");
my $me = $ENV{'SUDO_USER'};
system("$bindir/gspl-uchange -p ALL $me") if length($me) != 0;
}
# Install printer
sub proc_install {
my $ptr = shift;
my $ptrdets = $Ptrdeflist{$ptr};
my $cloned = $ptrdets;
$cloned = $Ptrdeflist{$ptrdets->{CLONEOF}} if $ptrdets->{TYPE} eq 'Clone';
$descr = askq("pdescr", "Please give a description for this printer") unless $ptrdets->{DESCR};
$descr =~ y/[a-z][A-Z] //cd;
$ptrdets->{DESCR} = $descr;
my $dev;
if ($ptrdets->{DEV}) {
$dev = $ptrdets->{DEV};
}
else {
if ($cloned->{NETWORK} eq 'Y') {
$ptrdets->{DEV} = askip('destip', "Please give the host name or IP for this printer");
}
else {
my $port = $cloned->{PORT};
my $lport = ucfirst lc $port;
for (;;) {
$dev = "/dev/" . getline("devname", "$lport device: /dev/");
unless (-c $dev) {
print "$dev is not a suitable device\n";
redo;
}
if ($Idevs{$dev}) {
print "Sorry, already using $dev\n";
redo;
}
last;
}
}
}
my @args;
push @args, "$bindir/gspl-padd";
if ($cloned->{NETWORK} ne 'Y') {
my @sbits = stat $dev;
if ($sbits[4] != $Spooluid) {
if (askyorn('notowned', "$dev is not owned by gnuspool - fix", 1)) {
chown $Spooluid, $Spoolgid, $dev;
}
elsif ($sbits[5] == $Spoolgid) {
if (($sbits[2] & 0020) == 0) {
return unless askyorn('notgrpwrt', "$dev not group writable - fix", 1);
chmod (($sbits[2] & 07777) | 0020, $dev);
}
}
else {
if (($sbits[2] & 002) == 0) {
return unless askyorn('notwrld', "$dev not world writable - fix", 1);
chmod (($sbits[2] & 07777) | 0002, $dev);
}
}
}
$dev =~ s|/dev/||;
}
else {
push @args,"-N";
}
push @args, '-l';
push @args, $dev;
push @args, "-D";
push @args, "'$ptrdets->{DESCR}'";
push @args, $ptr;
if ($cloned->{GSSIZE}) {
push @args, $cloned->{GSSIZE};
}
else {
my $ftype = askq('ftype', 'Form type name', 'a4', [ 'a4', 'letter', 'legal', 'standard' ]);
push @args, $ftype;
# The first time we start up, the default form type is set to "standard"
# If we are setting some other form type in a printer lets set the default
# form type to that.
my $defform = `$bindir/gspl-ulist -S -F %f`;
chop $defform;
setdefform($ftype) if $defform eq 'standard' || askyorn('defform', "set $ftype as default form type", 0);
}
if (system(join(' ', @args)) == 0) {
print "$ptr added successfully\n";
}
else {
print "Sorry - could not add $ptr\n";
}
}
# Perform operation - return true if success
sub proc_ptrop {
my $ptrdef = shift;
my $prog = shift;
my $dev = $ptrdef->{DEV};
$dev =~ s|/dev/||;
my @args;
push @args, "$bindir/$prog";
push @args, "-l", $dev if $dev;
push @args, $ptrdef->{PRINTER};
system(join(' ', @args)) == 0;
}
# Is printer running?
sub isrunning_ptr {
my $ptrdef = shift;
proc_ptrop($ptrdef, "gspl-pstat");
}
# Stop printer
sub proc_ptrstop {
my $ptrdef = shift;
proc_ptrop($ptrdef, "gspl-pstop");
}
# Stop printer if running and return whether it was
sub proc_stopifrunning {
my $ptr = shift;
my $ptrdets = $Ptrdeflist{$ptr};
return 0 if isrunning_ptr($ptrdets);
proc_ptrstop($ptrdets);
1;
}
# Restart printer
sub proc_restartprin {
my $ptr = shift;
my $ptrdets = $Ptrdeflist{$ptr};
proc_ptrop($ptrdets, 'gspl-start');
}
# Uninstall printer
sub proc_uninstall {
my $ptr = shift; # FIXME for clones
my $ptrdets = $Ptrdeflist{$ptr};
if (isrunning_ptr($ptrdets)) {
exit 0 unless askyorn('ptrrun', "ptr (on $ptrdets->{DEV}) is running - are you sure", 0);
die "Sorry could not stop $ptr\n" unless proc_ptrstop($ptrdets);
}
die "Sorry could not delete $ptr\n" unless proc_ptrop($ptrdets, "gspl-pdel");
print "$ptr uninstalled OK\n";
}
# Remove printer def
sub proc_remove {
my $ptr = shift;
my $ptrdir = $Config{SPOOLPT};
system("rm -rf $ptrdir/$ptr");
}
################################################################################
# #
# Start here #
# #
################################################################################
# Optional argument giving printer name
die "You have to be super-user to run this" unless $> == 0;
$Systemtype = `uname -s`;
chop $Systemtype;
($Spoolu, $Spoolp, $Spooluid, $Spoolgid) = getpwnam('gnuspool') or die "Cannot find gnuspool id";
undef $Spoolu;
undef $Spoolp;
if ($#ARGV >= 0) {
die "Usage: $0 [ptrname]\n" if $#ARGV > 0;
$Insptr = shift @ARGV;
die "Invalid printer name format $Insptr\n" unless $Insptr =~ /^[a-z]\w+$/;
}
# May need pager later
$prefix = "/usr";
$exec_prefix = "${prefix}";
$datarootdir = "/usr/share";
$bindir = "${exec_prefix}/bin";
$sbindir = "${exec_prefix}/sbin";
$Pmenu = "$sbindir/gspl-pmenu";
die "Cannot find pmenu program looking in $sbindir" unless -x $Pmenu;
# Need to check we've got PATH set up correctly with the binaries in
check_userpath;
# Parse config in case directories relocated
# (Need this before we go into python)
parse_mconfig;
if (findonpath('python')) {
my $pyversion = `python --version 2>&1`;
chop $pyversion;
my ($maj,$min);
if ((($maj,$min) = $pyversion =~ /(\d+)\.(\d+)/) && ($maj > 2 || ($maj >= 2 && $min >= 6))) {
$pyprog = $Config{SDATADIR} . "/ptrinstall/ptrinstall.py";
$c = system("python $pyprog");
exit 0 if $c == 0;
$ec = $c >> 8;
if ($ec == 20) {
print <<EOT;
You seem to have Python installed but not PyQt4.
If you install this you should be able to install printers using a GUI interface.
EOT
}
elsif ($ec == 21) {
print <<EOT;
You seem to have Python installed and PyQt4 but are missing the
auto-generated config.py.
If you install this you should be able to install printers using a GUI interface.
EOT
}
else {
print <<EOT;
Sorry but the Python interface failed.
Please try to install it or mend it if it is installed.
If you install this you should be able to install printers using a GUI interface.
EOT
}
}
}
getekchars;
$pager = findonpath('less') || findonpath('more') || findonpath('pg') || 'cat';
print <<EOT;
Welcome to the Printer Setup system.
Please answer questions - mostly y or n as required (suggested default given)
Press F1 if available for help otherwise ?
=============================================================================
EOT
# Get exiting printers - defined and installed
%Ptrdeflist = list_defptrs;
(%Ptrinslist = getptrs_online) || (%Ptrinslist = getptrs_offline) || print "Cannot find installed printers (may not be an error)\n";
# Note list of devices used by existing printers
for my $id (values %Ptrinslist) {
$Idevs{"/dev/$id->{DEV}"} = 1 unless $id->{NETWORK};
}
# Merge in installed printers
while (my ($p, $pi) = each %Ptrinslist) {
my $d = $Ptrdeflist{$p};
unless (defined $d) {
print "*** Confused about $p which is installed but not defined\n";
next;
}
$d->{INSTALLED} = 'Y';
if ($pi->{NETWORK} eq 'Y') {
$d->{NETWORK} = 'Y';
$d->{DEV} = $pi->{DEV};
}
else {
$d->{DEV} = "/dev/" . $pi->{DEV};
}
$d->{DESCR} = $pi->{DESCR};
}
undef %Ptrinslist;
# Get printer name if needed
unless ($Insptr) {
if ($Systemtype eq 'Linux') {
my $pplist = list_parports(0);
for my $p (@$pplist) {
unless ($Idevs{$p->{DEV}}) {
print "You appear to have $p->{MODEL} on $p->{DEV}\n";
if (askyorn('instpara', "Do you want to install that", 1)) {
$Insptr = getnewptrname("Please give a name for the printer");
install_parallel($Insptr, $p->{DEV}, $p->{MODEL});
exit 0;
}
}
}
}
pagedisp_ptrs if askyorn('displayexist', "Display existing printers", 1);
$Insptr = getnewptrname("Printer to install or edit");
}
# See if it's a completely new printer
if ($Ptrdeflist{$Insptr}) {
# Printer is defined
if ($Ptrdeflist{$Insptr}->{INSTALLED} eq 'Y') {
# Printer is defined and installed
if (isrunning) {
my $ans = askq("modorrem", "Do you want to modify or remove $Insptr", "m", [ 'm', 'r' ]);
if ($ans eq 'm') {
my $wasrunning = proc_stopifrunning($Insptr);
proc_modify($Insptr, 1, 1);
if ($wasrunning) {
proc_restartprin($Insptr) if askyorn('restartnow', "Restart $Insptr now", 1);
}
else {
proc_restartprin($Insptr) if askyorn('startnow', "Start $Insptr now", 0);
}
}
elsif (askyorn('justunist', "Do you just want to uninstall $Insptr", 1)) {
proc_uninstall($Insptr);
}
else {
if (askyorn('comprem', "This will completely remove $Insptr are you sure", 0)) {
proc_uninstall($Insptr);
proc_remove($Insptr);
}
}
}
elsif (askyorn('modexist', "Do you want to modify $Insptr", 1)) {
proc_modify($Insptr, 0, 1);
}
}
else {
# Not installed
if (isrunning) {
if (askyorn('modptr', "Do you want to modify $Insptr", 1)) {
proc_modify($Insptr, 1, 0);
proc_install($Insptr) if askyorn("instptr", "Do you want to install $Insptr", 1);
}
elsif (askyorn('instptr', "Do you want to install $Insptr", 1)) {
proc_install($Insptr);
}
elsif (askyorn('deldef', "Delete definition of $Insptr", 0)) {
proc_remove($Insptr);
}
}
else {
if (askyorn('moddef', "Do you want to change definition of $Insptr", 1)) {
proc_modify($Insptr, 0, 0);
}
elsif (askyorn('deldef', "Do you want to remove the definition of $Insptr", 0)) {
proc_remove($Insptr);
}
}
}
}
else {
proc_create($Insptr);
proc_install($Insptr) if isrunning && askyorn('instnow', "Do you want to install $Insptr", 1);
}
|