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
|
/* os_dep.c
* (c) 2002 Mikulas Patocka
* This file is a part of the Links program, released under GPL.
*/
#include "links.h"
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#if defined(HAVE_LIBGPM) && defined(HAVE_GPM_H)
#define USE_GPM
#endif
#ifdef USE_GPM
#include <gpm.h>
#endif
/* prototypes */
int get_e(char *);
void sigwinch(void *);
void exec_new_links(struct terminal *, unsigned char *, unsigned char *, unsigned char *);
void open_in_new_twterm(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_xterm(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_screen(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_vio(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_fullscreen(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_win32(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_be(struct terminal *, unsigned char *, unsigned char *);
void open_in_new_g(struct terminal *, unsigned char *, unsigned char *);
int is_safe_in_shell(unsigned char c)
{
return c == '@' || c == '+' || c == '-' || c == '.' || c == ',' || c == '=' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || c == '_' || (c >= 'a' && c <= 'z');
}
int is_safe_in_url(unsigned char c)
{
return is_safe_in_shell(c) || c == ':' || c == '/' || c >= 0x80;
}
void check_shell_security(unsigned char **cmd)
{
unsigned char *c = *cmd;
while (*c) {
if (!is_safe_in_shell(*c)) *c = '_';
c++;
}
}
int check_shell_url(unsigned char *url)
{
while (*url) {
if (!is_safe_in_url(*url)) return -1;
url++;
}
return 0;
}
unsigned char *escape_path(unsigned char *path)
{
unsigned char *result;
size_t i;
if (strchr(path, '"')) return stracpy(path);
for (i = 0; path[i]; i++) if (!is_safe_in_url(path[i])) goto do_esc;
return stracpy(path);
do_esc:
result = stracpy("\"");
add_to_strn(&result, path);
add_to_strn(&result, "\"");
return result;
}
int get_e(char *env)
{
char *v;
if ((v = getenv(env))) return atoi(v);
return 0;
}
void ignore_signals(void)
{
signal(SIGPIPE, SIG_IGN);
#ifdef SIGXFSZ
signal(SIGXFSZ, SIG_IGN);
#endif
}
char *clipboard = NULL;
#if defined(WIN32)
#include <windows.h>
#endif
#if defined(OS2)
#define INCL_MOU
#define INCL_VIO
#define INCL_DOSPROCESS
#define INCL_DOSERRORS
#define INCL_DOSMODULEMGR
#define INCL_WIN
#define INCL_WINCLIPBOARD
#define INCL_WINSWITCHLIST
#include <os2.h>
#include <io.h>
#include <process.h>
#include <sys/video.h>
#ifdef HAVE_SYS_FMUTEX_H
#include <sys/builtin.h>
#include <sys/fmutex.h>
#endif
#ifdef X2
/* from xf86sup - XFree86 OS/2 support driver */
#include <pty.h>
#endif
#endif
#if defined(O_SIZE) && defined(__EMX__)
int open_prealloc(char *name, int flags, int mode, off_t siz)
{
return open(name, flags | O_SIZE, mode, (unsigned long)siz);
}
void prealloc_truncate(int h, off_t siz)
{
ftruncate(h, siz);
}
#endif
/* Terminal size */
#ifdef WIN32
/* Cygwin has a bug and loses SIGWINCH sometimes, so poll it */
static void winch_thread(void *p, int l)
{
static int old_xsize, old_ysize;
static int cur_xsize, cur_ysize;
if (get_terminal_size(0, &old_xsize, &old_ysize)) return;
while (1) {
if (get_terminal_size(1, &cur_xsize, &cur_ysize)) return;
if ((old_xsize != cur_xsize) || (old_ysize != cur_ysize)) {
old_xsize = cur_xsize;
old_ysize = cur_ysize;
raise(SIGWINCH);
}
sleep(1);
}
}
static void win32_resize_poll(void)
{
static int winch_thread_running = 0;
if (!winch_thread_running) {
if (start_thread(winch_thread, NULL, 0) >= 0)
winch_thread_running = 1;
}
}
#endif
#if defined(UNIX) || defined(BEOS) || defined(RISCOS) || defined(ATHEOS) || defined(WIN32) || defined(SPAD)
void sigwinch(void *s)
{
((void (*)(void))s)();
}
void handle_terminal_resize(int fd, void (*fn)(void))
{
install_signal_handler(SIGWINCH, sigwinch, fn, 0);
#ifdef WIN32
win32_resize_poll();
#endif
}
void unhandle_terminal_resize(int fd)
{
install_signal_handler(SIGWINCH, NULL, NULL, 0);
}
int get_terminal_size(int fd, int *x, int *y)
{
struct winsize ws;
if (!x || !y) return -1;
if (ioctl(1, TIOCGWINSZ, &ws) != -1) {
if (!(*x = ws.ws_col) && !(*x = get_e("COLUMNS"))) *x = 80;
if (!(*y = ws.ws_row) && !(*y = get_e("LINES"))) *y = 24;
return 0;
} else {
if (!(*x = get_e("COLUMNS"))) *x = 80;
if (!(*y = get_e("LINES"))) *y = 24;
}
return 0;
}
#elif defined(OS2)
#define A_DECL(type, var) type var##1, var##2, *var = _THUNK_PTR_STRUCT_OK(&var##1) ? &var##1 : &var##2
int is_xterm(void)
{
static int xt = -1;
if (xt == -1) xt = !!getenv("WINDOWID");
return xt;
}
int winch_pipe[2];
int winch_thread_running = 0;
#define WINCH_SLEEPTIME 500 /* time in ms for winch thread to sleep */
static void winch_thread(void)
{
/* A thread which regularly checks whether the size of
window has changed. Then raise SIGWINCH or notifiy
the thread responsible to handle this. */
static int old_xsize, old_ysize;
static int cur_xsize, cur_ysize;
ignore_signals();
if (get_terminal_size(1, &old_xsize, &old_ysize)) return;
while (1) {
if (get_terminal_size(1, &cur_xsize, &cur_ysize)) return;
if ((old_xsize != cur_xsize) || (old_ysize != cur_ysize)) {
old_xsize = cur_xsize;
old_ysize = cur_ysize;
write(winch_pipe[1], "x", 1);
/* Resizing may take some time. So don't send a flood
of requests?! */
_sleep2(2*WINCH_SLEEPTIME);
}
else
_sleep2(WINCH_SLEEPTIME);
}
}
static void winch(void *s)
{
char c;
while (can_read(winch_pipe[0]) && read(winch_pipe[0], &c, 1) == 1);
((void (*)(void))s)();
}
void handle_terminal_resize(int fd, void (*fn)(void))
{
if (!is_xterm()) return;
if (!winch_thread_running) {
if (c_pipe(winch_pipe) < 0) return;
winch_thread_running = 1;
_beginthread((void (*)(void *))winch_thread, NULL, 0x32000, NULL);
}
set_handlers(winch_pipe[0], winch, NULL, NULL, fn);
}
void unhandle_terminal_resize(int fd)
{
set_handlers(winch_pipe[0], NULL, NULL, NULL, NULL);
}
int get_terminal_size(int fd, int *x, int *y)
{
if (!x || !y) return -1;
if (is_xterm()) {
#ifdef X2
/* int fd; */
int arc;
struct winsize win;
/* fd = STDIN_FILENO; */
arc = ptioctl(1, TIOCGWINSZ, &win);
if (arc) {
*x = 80;
*y = 24;
return 0;
}
*y = win.ws_row;
*x = win.ws_col;
goto set_default;
#else
*x = 80; *y = 24;
return 0;
#endif
} else {
int a[2] = { 0, 0 };
_scrsize(a);
*x = a[0];
*y = a[1];
set_default:
if (*x == 0) {
*x = get_e("COLUMNS");
if (*x == 0) *x = 80;
}
if (*y == 0) {
*y = get_e("LINES");
if (*y == 0) *y = 24;
}
}
return 0;
}
#elif defined(WIN32)
#endif
/* Pipe */
#if defined(UNIX) || defined(BEOS) || defined(RISCOS) || defined(ATHEOS) || defined(SPAD)
void set_bin(int fd)
{
}
int c_pipe(int *fd)
{
return pipe(fd);
}
#elif defined(OS2) || defined(WIN32)
void set_bin(int fd)
{
setmode(fd, O_BINARY);
}
int c_pipe(int *fd)
{
int r = pipe(fd);
if (!r) set_bin(fd[0]), set_bin(fd[1]);
return r;
}
#endif
/* Filename */
int check_file_name(unsigned char *file)
{
return 1; /* !!! FIXME */
}
/* Exec */
int can_twterm(void) /* Check if it make sense to call a twterm. */
{
static int xt = -1;
if (xt == -1) xt = !!getenv("TWDISPLAY");
return xt;
}
#if defined(UNIX) || defined(SPAD)
int is_xterm(void)
{
static int xt = -1;
if (xt == -1) xt = getenv("DISPLAY") && *getenv("DISPLAY");
return xt;
}
#elif defined(BEOS) || defined(ATHEOS)
int is_xterm(void)
{
return 0;
}
#elif defined(WIN32)
int is_xterm(void)
{
static int xt = -1;
if (xt == -1) xt = !!getenv("WINDOWID");
return xt;
}
#elif defined(RISCOS)
int is_xterm(void)
{
return 1;
}
#endif
tcount resize_count = 0;
void close_fork_tty(void)
{
struct terminal *t;
struct download *d;
struct connection *c;
struct k_conn *k;
foreach (t, terminals) if (t->fdin > 0) close(t->fdin);
foreach (d, downloads) if (d->handle > 0) close(d->handle);
foreach (c, queue) close_socket(&c->sock1), close_socket(&c->sock2);
foreach (k, keepalive_connections) close(k->conn);
}
#if defined(WIN32)
void get_path_to_exe(void)
{
/* Standard method (argv[0]) doesn't work, if links is executed from
symlink --- it returns symlink name and cmd.exe is unable to start
it */
unsigned r;
static unsigned char path[4096];
r = GetModuleFileName(NULL, path, sizeof path);
if (r <= 0 || r >= sizeof path) {
path_to_exe = g_argv[0];
return;
}
path_to_exe = path;
}
#elif defined(OS2)
void get_path_to_exe(void)
{
/* If you spawn links with quotation marks from cmd.exe,
the quotation marks will be present in g_argv[0] ... and will
prevent executing it */
static char path[270];
PPIB pib = NULL;
path_to_exe = g_argv[0];
/*if (!strchr(path_to_exe, ' ') && !strchr(path_to_exe, '"')) return;*/
DosGetInfoBlocks(NULL, &pib);
if (!pib) return;
if (DosQueryModuleName(pib->pib_hmte, sizeof path, path)) return;
path_to_exe = path;
}
#else
void get_path_to_exe(void)
{
path_to_exe = g_argv[0];
}
#endif
#if defined(UNIX) || defined(BEOS) || defined(RISCOS) || defined(ATHEOS) || defined(SPAD)
#if defined(BEOS) && defined(HAVE_SETPGID)
int exe(char *path, int fg)
{
pid_t p;
int s;
fg=fg; /* ignore flag */
if (!(p = fork())) {
setpgid(0, 0);
system(path);
_exit(0);
}
if (p > 0) waitpid(p, &s, 0);
else return system(path);
return 0;
}
#else
/* UNIX */
int exe(char *path, int fg)
{
#ifdef G
if (F && drv->exec) return drv->exec(path, fg);
#endif
#ifdef SIGTSTP
signal(SIGTSTP, SIG_DFL);
#endif
#ifdef SIGCONT
signal(SIGCONT, SIG_DFL);
#endif
#ifdef SIGWINCH
signal(SIGWINCH, SIG_DFL);
#endif
return system(path);
}
#endif
/* clipboard -> links */
unsigned char *get_clipboard_text(struct terminal *term)
{
#ifdef GRDRV_X
if(term && term->dev && term->dev->drv && !strcmp(term->dev->drv->name,"x")) {
return x_get_clipboard_text();
}
#endif
return stracpy(clipboard);
}
/* links -> clipboard */
void set_clipboard_text(struct terminal *term, unsigned char *data)
{
#ifdef GRDRV_X
if(term && term->dev && term->dev->drv && !strcmp(term->dev->drv->name,"x")) {
x_set_clipboard_text(term->dev, data);
return;
}
#endif
if (clipboard) mem_free(clipboard);
clipboard = stracpy(data);
}
int clipboard_support(struct terminal *term)
{
#ifdef GRDRV_X
if(term && term->dev && term->dev->drv && !strcmp(term->dev->drv->name,"x")) {
return 1;
}
#endif
return 0;
}
void set_window_title(unsigned char *title)
{
/* !!! FIXME */
}
unsigned char *get_window_title(void)
{
/* !!! FIXME */
return NULL;
}
int resize_window(int x, int y)
{
return -1;
}
#elif defined(WIN32)
static int is_winnt(void)
{
OSVERSIONINFO v;
v.dwOSVersionInfoSize = sizeof v;
if (!GetVersionEx(&v)) return 0;
return v.dwPlatformId >= VER_PLATFORM_WIN32_NT;
}
#define WIN32_START_STRING "start /wait "
int exe(char *path, int fg)
{
/* This is very tricky. We must have exactly 3 arguments, the first
one shell and the second one "/c", otherwise Cygwin would quote
the arguments and trash them */
int ct;
char buffer[1024];
char buffer2[1024];
pid_t pid;
unsigned char *x1;
char *arg;
x1 = GETSHELL;
if (!x1) x1 = DEFAULT_SHELL;
arg = alloca(strlen(WIN32_START_STRING) + 3 + strlen(path) + 1);
strcpy(arg, WIN32_START_STRING);
if (*path == '"' && strlen(x1) >= 7 && !strcasecmp(x1 + strlen(x1) - 7, "cmd.exe")) strcat(arg, "\"\" ");
strcat(arg, path);
ct = GetConsoleTitle(buffer, sizeof buffer);
if (!(pid = fork())) {
int i;
/* Win98 crashes if we spawn command.com and have some sockets open */
for (i = 0; i < FD_SETSIZE; i++) close(i);
open("nul", O_RDONLY);
open("nul", O_WRONLY);
open("nul", O_WRONLY);
execlp(x1, x1, "/c", arg, NULL);
_exit(1);
}
if (!is_winnt()) {
sleep(1);
if (ct && GetConsoleTitle(buffer2, sizeof buffer2) && !casecmp(buffer2, "start", 5)) {
SetConsoleTitle(buffer);
}
}
if (pid != -1) waitpid(pid, NULL, 0);
return 0;
}
unsigned char *get_clipboard_text(struct terminal *term)
{
char buffer[256];
unsigned char *str, *s, *d;
int l;
int r;
int h = open("/dev/clipboard", O_RDONLY);
if (h == -1) return stracpy(clipboard);
set_bin(h); /* O_TEXT doesn't work on clipboard handle */
str = init_str();
l = 0;
while ((r = hard_read(h, buffer, sizeof buffer)) > 0)
add_bytes_to_str(&str, &l, buffer, r);
close(h);
for (s = str, d = str; *s; s++)
if (!(s[0] == '\r' && s[1] == '\n')) *d++ = *s;
*d = 0;
return str;
}
/* Putting Czech characters to clipboard doesn't work, but it should be fixed
rather in Cygwin than here */
void set_clipboard_text(struct terminal *term, unsigned char *data)
{
unsigned char *conv_data;
int l;
int h;
if (clipboard) mem_free(clipboard);
clipboard = stracpy(data);
h = open("/dev/clipboard", O_WRONLY);
if (h == -1) return;
set_bin(h); /* O_TEXT doesn't work on clipboard handle */
conv_data = init_str();
l = 0;
for (; *data; data++)
if (*data == '\n') add_to_str(&conv_data, &l, "\r\n");
else add_chr_to_str(&conv_data, &l, *data);
hard_write(h, conv_data, l);
mem_free(conv_data);
close(h);
}
int clipboard_support(struct terminal *term)
{
return 1;
}
static int get_windows_cp(void)
{
char str[6];
int cp, idx;
static int win_cp_idx = -1;
if (win_cp_idx != -1) return win_cp_idx;
cp = GetConsoleOutputCP();
if (cp <= 0 || cp >= 100000) return 0;
sprintf(str, "%d", cp);
if ((idx = get_cp_index(str)) < 0) return 0;
win_cp_idx = idx;
return idx;
}
static int get_utf8_cp(void)
{
static int idx = -1;
return idx >= 0 ? idx : (idx = get_cp_index("utf-8"));
}
void set_window_title(unsigned char *title)
{
unsigned char *t;
struct conv_table *ct;
if (is_xterm()) return;
ct = get_translation_table(get_utf8_cp(), get_windows_cp());
t = convert_string(ct, title, strlen(title), NULL);
SetConsoleTitle(t);
mem_free(t);
}
unsigned char *get_window_title(void)
{
struct conv_table *ct;
int r;
char buffer[1024];
if (is_xterm()) return NULL;
if (!(r = GetConsoleTitle(buffer, sizeof buffer))) return NULL;
ct = get_translation_table(get_windows_cp(), get_utf8_cp());
return convert_string(ct, buffer, r, NULL);
}
static void call_resize(unsigned char *x1, int x, int y)
{
pid_t pid;
unsigned char arg[40];
sprintf(arg, "mode %d,%d", x, y);
if (!(pid = fork())) {
int i;
/* Win98 crashes if we spawn command.com and have some sockets open */
for (i = 0; i < FD_SETSIZE; i++) if (i != 1 && i != 2) close(i);
open("nul", O_WRONLY);
execlp(x1, x1, "/c", arg, NULL);
_exit(1);
}
if (pid != -1) waitpid(pid, NULL, 0);
}
int resize_window(int x, int y)
{
int old_x, old_y;
int ct = 0, fullscreen = 0;
char buffer[1024];
unsigned char *x1;
if (is_xterm()) return -1;
if (get_terminal_size(1, &old_x, &old_y)) return -1;
x1 = GETSHELL;
if (!x1) x1 = DEFAULT_SHELL;
if (!is_winnt()) {
ct = GetConsoleTitle(buffer, sizeof buffer);
}
call_resize(x1, x, y);
if (!is_winnt()) {
int new_x, new_y;
/* If we resize console on Win98 in fullscreen mode, it won't be
notified by Cygwin (it is valid for all Cygwin apps). So we must
switch to windowed mode, resize it again (twice, because resizing
to the same size won't have an effect) and switch back to full-screen
mode. */
/* I'm not sure what's the behavior on WinNT 4. Anybody wants to test?
*/
if (!fullscreen && !get_terminal_size(1, &new_x, &new_y) && (new_x != x || new_y != y)) {
fullscreen = 1;
keybd_event(VK_MENU, 0x38, 0, 0);
keybd_event(VK_RETURN, 0x1c, 0, 0);
keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
if (y != 25) call_resize(x1, 80, 25);
else call_resize(x1, 80, 50);
call_resize(x1, x, y);
if (get_terminal_size(1, &new_x, &new_y) || new_x != x || new_y != y) call_resize(x1, old_x, old_y);
keybd_event(VK_MENU, 0x38, 0, 0);
keybd_event(VK_RETURN, 0x1c, 0, 0);
keybd_event(VK_RETURN, 0x1c, KEYEVENTF_KEYUP, 0);
keybd_event(VK_MENU, 0x38, KEYEVENTF_KEYUP, 0);
}
if (ct) SetConsoleTitle(buffer);
}
return 0;
}
#elif defined(OS2)
#ifdef G
_fmutex fd_mutex;
int fd_mutex_init = 0;
#endif
int exe(char *path, int fg)
{
int flags = P_SESSION;
pid_t pid;
int ret;
#ifdef G
int old0 = 0, old1 = 1, old2 = 2;
#endif
char *shell;
fg=fg; /* ignore flag */
if (!(shell = GETSHELL)) shell = DEFAULT_SHELL;
if (is_xterm()) flags |= P_BACKGROUND;
#ifdef G
if (F) {
if (!fd_mutex_init) {
if (_fmutex_create(&fd_mutex, 0)) return -1;
fd_mutex_init = 1;
}
_fmutex_request(&fd_mutex, _FMR_IGNINT);
old0 = dup(0);
old1 = dup(1);
old2 = dup(2);
close(0);
close(1);
close(2);
open("con", O_RDONLY);
open("con", O_WRONLY);
open("con", O_WRONLY);
}
#endif
pid = spawnlp(flags, shell, shell, "/c", path, NULL);
#ifdef G
if (F) {
dup2(old0, 0);
dup2(old1, 1);
dup2(old2, 2);
close(old0);
close(old1);
close(old2);
_fmutex_release(&fd_mutex);
}
#endif
if (pid != -1) waitpid(pid, &ret, 0);
else ret = -1;
return ret;
}
unsigned char *get_clipboard_text(struct terminal *term)
{
PTIB tib;
PPIB pib;
HAB hab;
HMQ hmq;
ULONG oldType;
char *ret = NULL;
DosGetInfoBlocks(&tib, &pib);
oldType = pib->pib_ultype;
pib->pib_ultype = 3;
if ((hab = WinInitialize(0)) != NULLHANDLE) {
if ((hmq = WinCreateMsgQueue(hab, 0)) != NULLHANDLE) {
if (WinOpenClipbrd(hab)) {
ULONG fmtInfo = 0;
if (WinQueryClipbrdFmtInfo(hab, CF_TEXT, &fmtInfo)!=FALSE)
{
ULONG selClipText = WinQueryClipbrdData(hab, CF_TEXT);
if (selClipText)
{
char *u;
PCHAR pchClipText = (PCHAR)selClipText;
ret = mem_alloc(strlen(pchClipText)+1);
strcpy(ret, pchClipText);
while ((u = strchr(ret, 13))) memmove(u, u + 1, strlen(u + 1) + 1);
}
}
WinCloseClipbrd(hab);
}
#ifdef G
if (F && ret) {
static int cp = -1;
struct conv_table *ct;
unsigned char *d;
if (cp == -1) {
int c = WinQueryCp(hmq);
unsigned char a[64];
snprintf(a, 64, "%d", c);
if ((cp = get_cp_index(a)) < 0 || is_cp_special(cp)) cp = 0;
}
ct = get_translation_table(cp, get_cp_index("utf-8"));
d = convert_string(ct, ret, strlen(ret), NULL);
mem_free(ret);
ret = d;
}
#endif
WinDestroyMsgQueue(hmq);
}
WinTerminate(hab);
}
pib->pib_ultype = oldType;
return ret;
}
void set_clipboard_text(struct terminal * term, unsigned char *data)
{
PTIB tib;
PPIB pib;
HAB hab;
HMQ hmq;
ULONG oldType;
unsigned char *d = NULL;
DosGetInfoBlocks(&tib, &pib);
oldType = pib->pib_ultype;
pib->pib_ultype = 3;
if ((hab = WinInitialize(0)) != NULLHANDLE) {
if ((hmq = WinCreateMsgQueue(hab, 0)) != NULLHANDLE) {
#ifdef G
if (F) {
static int cp = -1;
struct conv_table *ct;
if (cp == -1) {
int c = WinQueryCp(hmq);
unsigned char a[64];
snprintf(a, 64, "%d", c);
if ((cp = get_cp_index(a)) < 0 || is_cp_special(cp)) cp = 0;
}
ct = get_translation_table(get_cp_index("utf-8"), cp);
d = convert_string(ct, data, strlen(data), NULL);
data = d;
}
#endif
if(WinOpenClipbrd(hab)) {
PVOID pvShrObject = NULL;
if (DosAllocSharedMem(&pvShrObject, NULL, strlen(data)+1, PAG_COMMIT | PAG_WRITE | OBJ_GIVEABLE) == NO_ERROR) {
strcpy(pvShrObject, data);
WinEmptyClipbrd(hab);
WinSetClipbrdData(hab, (ULONG)pvShrObject, CF_TEXT, CFI_POINTER);
}
WinCloseClipbrd(hab);
}
WinDestroyMsgQueue(hmq);
}
WinTerminate(hab);
}
pib->pib_ultype = oldType;
if (d) mem_free(d);
}
int clipboard_support(struct terminal *term)
{
return 1;
}
unsigned char *get_window_title(void)
{
#ifndef OS2_DEBUG
/*char *org_switch_title;*/
char *org_win_title = NULL;
static PTIB tib = NULL;
static PPIB pib = NULL;
ULONG oldType;
HSWITCH hSw = NULLHANDLE;
SWCNTRL swData;
HAB hab;
HMQ hmq;
/* save current process title */
if (!pib) DosGetInfoBlocks(&tib, &pib);
oldType = pib->pib_ultype;
memset(&swData, 0, sizeof swData);
if (hSw == NULLHANDLE) hSw = WinQuerySwitchHandle(0, pib->pib_ulpid);
if (hSw!=NULLHANDLE && !WinQuerySwitchEntry(hSw, &swData)) {
/*org_switch_title = mem_alloc(strlen(swData.szSwtitle)+1);
strcpy(org_switch_title, swData.szSwtitle);*/
/* Go to PM */
pib->pib_ultype = 3;
if ((hab = WinInitialize(0)) != NULLHANDLE) {
if ((hmq = WinCreateMsgQueue(hab, 0)) != NULLHANDLE) {
org_win_title = mem_alloc(MAXNAMEL+1);
WinQueryWindowText(swData.hwnd, MAXNAMEL+1, org_win_title);
org_win_title[MAXNAMEL] = 0;
/* back From PM */
WinDestroyMsgQueue(hmq);
}
WinTerminate(hab);
}
pib->pib_ultype = oldType;
}
return org_win_title;
#else
return NULL;
#endif
}
void set_window_title(unsigned char *title)
{
#ifndef OS2_DEBUG
static PTIB tib;
static PPIB pib;
ULONG oldType;
static HSWITCH hSw;
SWCNTRL swData;
HAB hab;
HMQ hmq;
if (!title) return;
if (!pib) DosGetInfoBlocks(&tib, &pib);
oldType = pib->pib_ultype;
memset(&swData, 0, sizeof swData);
if (hSw == NULLHANDLE) hSw = WinQuerySwitchHandle(0, pib->pib_ulpid);
if (hSw!=NULLHANDLE && !WinQuerySwitchEntry(hSw, &swData)) {
strncpy(swData.szSwtitle, title, MAXNAMEL-1);
swData.szSwtitle[MAXNAMEL-1] = 0;
WinChangeSwitchEntry(hSw, &swData);
/* Go to PM */
pib->pib_ultype = 3;
if ((hab = WinInitialize(0)) != NULLHANDLE) {
if ((hmq = WinCreateMsgQueue(hab, 0)) != NULLHANDLE) {
if(swData.hwnd)
WinSetWindowText(swData.hwnd, title);
/* back From PM */
WinDestroyMsgQueue(hmq);
}
WinTerminate(hab);
}
}
pib->pib_ultype = oldType;
#endif
}
int resize_window(int x, int y)
{
int xfont, yfont;
A_DECL(VIOMODEINFO, vmi);
resize_count++;
if (is_xterm()) return -1;
vmi->cb = sizeof(*vmi);
if (VioGetMode(vmi, 0)) return -1;
vmi->col = x;
vmi->row = y;
/*debug("%d %d %d", vmi->buf_length, vmi->full_length, vmi->partial_length);*/
for (xfont = 9; xfont >= 8; xfont--)
for (yfont = 16; yfont >= 8; yfont--) {
vmi->hres = x * xfont;
vmi->vres = y * yfont;
if (vmi->vres <= 400) vmi->vres = 400;
else if (vmi->vres <= 480) vmi->vres = 480;
vmi->buf_length = vmi->full_length = vmi->partial_length = x * ((vmi->vres + yfont - 1) / yfont) * 2;
vmi->full_length = (vmi->full_length + 4095) & ~4095;
vmi->partial_length = (vmi->partial_length + 4095) & ~4095;
if (!VioSetMode(vmi, 0)) return 0;
}
return -1;
}
#endif
/* Threads */
struct tdata {
void (*fn)(void *, int);
int h;
unsigned char data[1];
};
#if defined(HAVE_BEGINTHREAD) || defined(BEOS) || defined(HAVE_PTHREADS) || defined(HAVE_ATHEOS_THREADS_H)
static void bgt(struct tdata *t)
{
ignore_signals();
t->fn(t->data, t->h);
write(t->h, "x", 1);
close(t->h);
free(t);
}
#ifdef HAVE_PTHREADS
static void *bgpt(struct tdata *t)
{
bgt(t);
return NULL;
}
#endif
#ifdef HAVE_ATHEOS_THREADS_H
#include <atheos/threads.h>
static uint32 abgt(void *t)
{
bgt(t);
return 0;
}
#endif
#endif
#if defined(UNIX) || defined(OS2) || defined(RISCOS) || defined(ATHEOS) || defined(SPAD) || defined(WIN32)
void terminate_osdep(void) {}
#endif
#ifndef BEOS
void block_stdin(void) {}
void unblock_stdin(void) {}
#endif
#if defined(BEOS)
#include <be/kernel/OS.h>
int thr_sem_init = 0;
sem_id thr_sem;
struct list_head active_threads = { &active_threads, &active_threads };
struct active_thread {
struct active_thread *next;
struct active_thread *prev;
thread_id tid;
void (*fn)(void *);
void *data;
};
int32 started_thr(void *data)
{
struct active_thread *thrd = data;
thrd->fn(thrd->data);
if (acquire_sem(thr_sem) < B_NO_ERROR) return 0;
del_from_list(thrd);
free(thrd);
release_sem(thr_sem);
return 0;
}
int start_thr(void (*fn)(void *), void *data, unsigned char *name)
{
struct active_thread *thrd;
int tid;
if (!thr_sem_init) {
if ((thr_sem = create_sem(0, "thread_sem")) < B_NO_ERROR) return -1;
thr_sem_init = 1;
} else if (acquire_sem(thr_sem) < B_NO_ERROR) return -1;
if (!(thrd = malloc(sizeof(struct active_thread)))) goto rel;
thrd->fn = fn;
thrd->data = data;
if ((tid = thrd->tid = spawn_thread(started_thr, name, B_NORMAL_PRIORITY, thrd)) < B_NO_ERROR) {
free(thrd);
rel:
release_sem(thr_sem);
return -1;
}
resume_thread(thrd->tid);
add_to_list(active_threads, thrd);
release_sem(thr_sem);
return tid;
}
void terminate_osdep(void)
{
struct list_head *p;
struct active_thread *thrd;
if (acquire_sem(thr_sem) < B_NO_ERROR) return;
foreach(thrd, active_threads) kill_thread(thrd->tid);
while ((p = active_threads.next) != &active_threads) {
del_from_list(p);
free(p);
}
release_sem(thr_sem);
}
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
int p[2];
struct tdata *t;
if (c_pipe(p) < 0) return -1;
if (!(t = malloc(sizeof(struct tdata) + l))) return -1;
t->fn = fn;
t->h = p[1];
memcpy(t->data, ptr, l);
if (start_thr((void (*)(void *))bgt, t, "links_thread") < 0) {
close(p[0]);
close(p[1]);
mem_free(t);
return -1;
}
return p[0];
}
#elif defined(HAVE_BEGINTHREAD)
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
int p[2];
struct tdata *t;
if (c_pipe(p) < 0) return -1;
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFL, O_NONBLOCK);
if (!(t = malloc(sizeof(struct tdata) + l))) return -1;
t->fn = fn;
t->h = p[1];
memcpy(t->data, ptr, l);
if (_beginthread((void (*)(void *))bgt, NULL, 65536, t) == -1) {
close(p[0]);
close(p[1]);
mem_free(t);
return -1;
}
return p[0];
}
#ifdef HAVE_READ_KBD
int tp = -1;
int ti = -1;
static void input_thread(void *p)
{
char c[2];
int h = (int)p;
ignore_signals();
while (1) {
/*c[0] = _read_kbd(0, 1, 1);
if (c[0]) if (write(h, c, 1) <= 0) break;
else {
int w;
printf("1");fflush(stdout);
c[1] = _read_kbd(0, 1, 1);
printf("2");fflush(stdout);
w = write(h, c, 2);
printf("3");fflush(stdout);
if (w <= 0) break;
if (w == 1) if (write(h, c+1, 1) <= 0) break;
printf("4");fflush(stdout);
}*/
/* for the records:
_read_kbd(0, 1, 1) will
read a char, don't echo it, wait for one available and
accept CTRL-C.
Knowing that, I suggest we replace this call completly!
*/
*c = _read_kbd(0, 1, 1);
write(h, c, 1);
}
close(h);
}
#endif /* #ifdef HAVE_READ_KBD */
#if defined(HAVE_MOUOPEN) && !defined(USE_GPM)
#define USING_OS2_MOUSE
#ifdef HAVE_SYS_FMUTEX_H
_fmutex mouse_mutex;
int mouse_mutex_init = 0;
#endif
int mouse_h = -1;
struct os2_mouse_spec {
int p[2];
void (*fn)(void *, unsigned char *, int);
void *data;
unsigned char buffer[sizeof(struct event)];
int bufptr;
int terminate;
};
static void mouse_thread(void *p)
{
int status;
struct os2_mouse_spec *oms = p;
A_DECL(HMOU, mh);
A_DECL(MOUEVENTINFO, ms);
A_DECL(USHORT, rd);
A_DECL(USHORT, mask);
struct event ev;
ignore_signals();
ev.ev = EV_MOUSE;
if (MouOpen(NULL, mh)) goto ret;
mouse_h = *mh;
*mask = MOUSE_MOTION_WITH_BN1_DOWN | MOUSE_BN1_DOWN |
MOUSE_MOTION_WITH_BN2_DOWN | MOUSE_BN2_DOWN |
MOUSE_MOTION_WITH_BN3_DOWN | MOUSE_BN3_DOWN |
MOUSE_MOTION;
MouSetEventMask(mask, *mh);
*rd = MOU_WAIT;
status = -1;
while (1) {
/*int w, ww;*/
if (MouReadEventQue(ms, rd, *mh)) break;
#ifdef HAVE_SYS_FMUTEX_H
_fmutex_request(&mouse_mutex, _FMR_IGNINT);
#endif
if (!oms->terminate) MouDrawPtr(*mh);
#ifdef HAVE_SYS_FMUTEX_H
_fmutex_release(&mouse_mutex);
#endif
ev.x = ms->col;
ev.y = ms->row;
/*debug("status: %d %d %d", ms->col, ms->row, ms->fs);*/
if (ms->fs & (MOUSE_BN1_DOWN | MOUSE_BN2_DOWN | MOUSE_BN3_DOWN)) ev.b = status = B_DOWN | (ms->fs & MOUSE_BN1_DOWN ? B_LEFT : ms->fs & MOUSE_BN2_DOWN ? B_MIDDLE : B_RIGHT);
else if (ms->fs & (MOUSE_MOTION_WITH_BN1_DOWN | MOUSE_MOTION_WITH_BN2_DOWN | MOUSE_MOTION_WITH_BN3_DOWN)) {
int b = ms->fs & MOUSE_MOTION_WITH_BN1_DOWN ? B_LEFT : ms->fs & MOUSE_MOTION_WITH_BN2_DOWN ? B_MIDDLE : B_RIGHT;
if (status == -1) b |= B_DOWN;
else b |= B_DRAG;
ev.b = status = b;
}
else {
if (status == -1) continue;
ev.b = (status & BM_BUTT) | B_UP;
status = -1;
}
if (hard_write(oms->p[1], (unsigned char *)&ev, sizeof(struct event)) != sizeof(struct event)) break;
}
#ifdef HAVE_SYS_FMUTEX_H
_fmutex_request(&mouse_mutex, _FMR_IGNINT);
#endif
mouse_h = -1;
MouClose(*mh);
#ifdef HAVE_SYS_FMUTEX_H
_fmutex_release(&mouse_mutex);
#endif
ret:
close(oms->p[1]);
/*free(oms);*/
}
static void mouse_handle(struct os2_mouse_spec *oms)
{
int r;
if ((r = read(oms->p[0], oms->buffer + oms->bufptr, sizeof(struct event) - oms->bufptr)) <= 0) {
unhandle_mouse(oms);
return;
}
if ((oms->bufptr += r) == sizeof(struct event)) {
oms->bufptr = 0;
oms->fn(oms->data, oms->buffer, sizeof(struct event));
}
}
void *handle_mouse(int cons, void (*fn)(void *, unsigned char *, int), void *data)
{
struct os2_mouse_spec *oms;
if (is_xterm()) return NULL;
#ifdef HAVE_SYS_FMUTEX_H
if (!mouse_mutex_init) {
if (_fmutex_create(&mouse_mutex, 0)) return NULL;
mouse_mutex_init = 1;
}
#endif
/* This is never freed but it's allocated only once */
if (!(oms = malloc(sizeof(struct os2_mouse_spec)))) return NULL;
oms->fn = fn;
oms->data = data;
oms->bufptr = 0;
oms->terminate = 0;
if (c_pipe(oms->p)) {
free(oms);
return NULL;
}
_beginthread(mouse_thread, NULL, 0x10000, (void *)oms);
set_handlers(oms->p[0], (void (*)(void *))mouse_handle, NULL, NULL, oms);
return oms;
}
void unhandle_mouse(void *om)
{
struct os2_mouse_spec *oms = om;
want_draw();
oms->terminate = 1;
set_handlers(oms->p[0], NULL, NULL, NULL, NULL);
close(oms->p[0]);
done_draw();
}
void want_draw(void)
{
A_DECL(NOPTRRECT, pa);
#ifdef HAVE_SYS_FMUTEX_H
if (mouse_mutex_init) _fmutex_request(&mouse_mutex, _FMR_IGNINT);
#endif
if (mouse_h != -1) {
static int x = -1, y = -1;
static tcount c = -1;
if (x == -1 || y == -1 || (c != resize_count)) get_terminal_size(1, &x, &y), c = resize_count;
pa->row = 0;
pa->col = 0;
pa->cRow = y - 1;
pa->cCol = x - 1;
MouRemovePtr(pa, mouse_h);
}
}
void done_draw(void)
{
#ifdef HAVE_SYS_FMUTEX_H
if (mouse_mutex_init) _fmutex_release(&mouse_mutex);
#endif
}
#endif /* if HAVE_MOUOPEN */
#elif defined(HAVE_CLONE)
/* This is maybe buggy... */
#include <sched.h>
struct thread_stack {
struct thread_stack *next;
struct thread_stack *prev;
int pid;
void *stack;
void (*fn)(void *, int);
int h;
int l;
unsigned char data[1];
};
void bglt(struct thread_stack *ts)
{
ts->fn(ts->data, ts->h);
write(ts->h, "x", 1);
close(ts->h);
}
struct list_head thread_stacks = { &thread_stacks, &thread_stacks };
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
struct thread_stack *ts;
int p[2];
int f;
if (c_pipe(p) < 0) return -1;
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFL, O_NONBLOCK);
/*if (!(t = malloc(sizeof(struct tdata) + l))) return -1;
t->fn = fn;
t->h = p[1];
memcpy(t->data, ptr, l);*/
foreach(ts, thread_stacks) {
if (ts->pid == -1 || kill(ts->pid, 0)) {
if (ts->l >= l) goto ts_ok;
else {
struct thread_stack *tts = ts;
ts = ts->prev;
del_from_list(tts); free(tts->stack); free(tts);
}
}
}
if (!(ts = malloc(sizeof(struct thread_stack) + l))) goto fail;
if (!(ts->stack = malloc(0x10000))) {
free(ts);
goto fail;
}
ts->l = l;
add_to_list(thread_stacks, ts);
ts_ok:
ts->fn = fn;
ts->h = p[1];
memcpy(ts->data, ptr, l);
if ((ts->pid = __clone((int (*)(void *))bglt, (char *)ts->stack + 0x8000, CLONE_VM | CLONE_FS | CLONE_FILES | CLONE_SIGHAND | SIGCHLD, ts)) == -1) {
fail:
close(p[0]);
close(p[1]);
return -1;
}
return p[0];
}
#elif defined(HAVE_PTHREADS)
#include <pthread.h>
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
pthread_t thread;
struct tdata *t;
int p[2];
if (c_pipe(p) < 0) return -1;
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFL, O_NONBLOCK);
if (!(t = malloc(sizeof(struct tdata) + l))) return -1;
t->fn = fn;
t->h = p[1];
memcpy(t->data, ptr, l);
if (pthread_create(&thread, NULL, (void *(*)(void *))bgpt, t)) {
close(p[0]);
close(p[1]);
mem_free(t);
return -1;
}
return p[0];
}
#elif defined(HAVE_ATHEOS_THREADS_H) && defined(HAVE_SPAWN_THREAD) && defined(HAVE_RESUME_THREAD)
#include <atheos/threads.h>
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
int p[2];
thread_id f;
struct tdata *t;
if (c_pipe(p) < 0) return -1;
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFL, O_NONBLOCK);
if (!(t = malloc(sizeof(struct tdata) + l))) return -1;
t->fn = fn;
t->h = p[1];
memcpy(t->data, ptr, l);
if ((f = spawn_thread("links_lookup", abgt, 0, 0, t)) == -1) {
close(p[0]);
close(p[1]);
mem_free(t);
return -1;
}
resume_thread(f);
return p[0];
}
#else /* HAVE_BEGINTHREAD */
int start_thread(void (*fn)(void *, int), void *ptr, int l)
{
int p[2];
pid_t f;
if (c_pipe(p) < 0) return -1;
fcntl(p[0], F_SETFL, O_NONBLOCK);
fcntl(p[1], F_SETFL, O_NONBLOCK);
if (!(f = fork())) {
close_fork_tty();
close(p[0]);
fn(ptr, p[1]);
write(p[1], "x", 1);
close(p[1]);
_exit(0);
}
if (f == -1) {
close(p[0]);
close(p[1]);
return -1;
}
close(p[1]);
return p[0];
}
#endif
#ifndef USING_OS2_MOUSE
void want_draw(void) {}
void done_draw(void) {}
#endif
int get_output_handle(void) { return 1; }
#if defined(OS2)
int get_ctl_handle(void) { return get_input_handle(); }
#else
int get_ctl_handle(void) { return 0; }
#endif
#if defined(BEOS)
#elif defined(HAVE_BEGINTHREAD) && defined(HAVE_READ_KBD)
int get_input_handle(void)
{
int fd[2];
if (ti != -1) return ti;
if (is_xterm()) return 0;
if (c_pipe(fd) < 0) return 0;
ti = fd[0];
tp = fd[1];
_beginthread(input_thread, NULL, 0x10000, (void *)tp);
/*
#if defined(HAVE_MOUOPEN) && !defined(USE_GPM)
_beginthread(mouse_thread, NULL, 0x10000, (void *)tp);
#endif
*/
return fd[0];
}
#else
int get_input_handle(void)
{
return 0;
}
#endif /* defined(HAVE_BEGINTHREAD) && defined(HAVE_READ_KBD) */
void os_cfmakeraw(struct termios *t)
{
#ifdef HAVE_CFMAKERAW
cfmakeraw(t);
#ifdef VMIN
t->c_cc[VMIN] = 1;
#endif
#else
t->c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP|INLCR|IGNCR|ICRNL|IXON);
t->c_oflag &= ~OPOST;
t->c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN);
t->c_cflag &= ~(CSIZE|PARENB);
t->c_cflag |= CS8;
t->c_cc[VMIN] = 1;
t->c_cc[VTIME] = 0;
#endif
}
#ifdef USE_GPM
struct gpm_mouse_spec {
int h;
void (*fn)(void *, unsigned char *, int);
void *data;
};
static void gpm_mouse_in(struct gpm_mouse_spec *gms)
{
Gpm_Event gev;
struct event ev;
if (Gpm_GetEvent(&gev) <= 0) {
set_handlers(gms->h, NULL, NULL, NULL, NULL);
return;
}
ev.ev = EV_MOUSE;
ev.x = gev.x - 1;
ev.y = gev.y - 1;
if (ev.x < 0) ev.x = 0;
if (ev.y < 0) ev.y = 0;
if (gev.buttons & GPM_B_LEFT) ev.b = B_LEFT;
else if (gev.buttons & GPM_B_MIDDLE) ev.b = B_MIDDLE;
else if (gev.buttons & GPM_B_RIGHT) ev.b = B_RIGHT;
else return;
if (gev.type & GPM_DOWN) ev.b |= B_DOWN;
else if (gev.type & GPM_UP) ev.b |= B_UP;
else if (gev.type & GPM_DRAG) ev.b |= B_DRAG;
else return;
gms->fn(gms->data, (char *)&ev, sizeof(struct event));
}
/* GPM installs its own signal handlers and we don't want them */
sigset_t gpm_sigset;
char gpm_sigset_valid;
#ifdef SIGWINCH
struct sigaction gpm_winch;
char gpm_winch_valid;
#endif
#ifdef SIGTSTP
struct sigaction gpm_tstp;
char gpm_tstp_valid;
#endif
static void save_gpm_signals(void)
{
sigset_t sig;
sigemptyset(&sig);
#ifdef SIGWINCH
sigaddset(&sig, SIGWINCH);
#endif
#ifdef SIGTSTP
sigaddset(&sig, SIGTSTP);
#endif
gpm_sigset_valid = !sigprocmask(SIG_BLOCK, &sig, &gpm_sigset);
#ifdef SIGWINCH
gpm_winch_valid = !sigaction(SIGWINCH, NULL, &gpm_winch);
#endif
#ifdef SIGTSTP
gpm_tstp_valid = !sigaction(SIGTSTP, NULL, &gpm_tstp);
#endif
}
static void restore_gpm_signals(void)
{
#ifdef SIGWINCH
if (gpm_winch_valid) sigaction(SIGWINCH, &gpm_winch, NULL);
#endif
#ifdef SIGTSTP
if (gpm_tstp_valid) sigaction(SIGTSTP, &gpm_tstp, NULL);
#endif
if (gpm_sigset_valid) sigprocmask(SIG_SETMASK, &gpm_sigset, NULL);
}
void *handle_mouse(int cons, void (*fn)(void *, unsigned char *, int), void *data)
{
int h;
Gpm_Connect conn;
struct gpm_mouse_spec *gms;
conn.eventMask = ~GPM_MOVE;
conn.defaultMask = GPM_MOVE;
conn.minMod = 0;
conn.maxMod = 0;
save_gpm_signals();
h = Gpm_Open(&conn, cons);
restore_gpm_signals();
if (h < 0) return NULL;
gms = mem_alloc(sizeof(struct gpm_mouse_spec));
gms->h = h;
gms->fn = fn;
gms->data = data;
set_handlers(h, (void (*)(void *))gpm_mouse_in, NULL, NULL, gms);
return gms;
}
void unhandle_mouse(void *h)
{
struct gpm_mouse_spec *gms = h;
set_handlers(gms->h, NULL, NULL, NULL, NULL);
save_gpm_signals();
Gpm_Close();
restore_gpm_signals();
mem_free(gms);
}
#elif !defined(USING_OS2_MOUSE)
void *handle_mouse(int cons, void (*fn)(void *, unsigned char *, int), void *data) { return NULL; }
void unhandle_mouse(void *data) { }
#endif /* #ifdef USE_GPM */
#if defined(OS2)
int get_system_env(void)
{
if (is_xterm()) return 0;
return ENV_OS2VIO; /* !!! FIXME: telnet */
}
#elif defined(BEOS)
int get_system_env(void)
{
unsigned char *term = getenv("TERM");
if (!term || (upcase(term[0]) == 'B' && upcase(term[1]) == 'E')) return ENV_BE;
return 0;
}
#elif defined(WIN32)
int get_system_env(void)
{
if (is_xterm()) return 0;
return ENV_WIN32;
}
#else
int get_system_env(void)
{
return 0;
}
#endif
void exec_new_links(struct terminal *term, unsigned char *xterm, unsigned char *exe, unsigned char *param)
{
unsigned char *str;
str = mem_alloc(strlen(xterm) + 1 + strlen(exe) + 1 + strlen(param) + 1);
if (*xterm) sprintf(str, "%s %s %s", xterm, exe, param);
else sprintf(str, "%s %s", exe, param);
exec_on_terminal(term, str, "", 2);
mem_free(str);
}
void open_in_new_twterm(struct terminal *term, unsigned char *exe, unsigned char *param)
{
unsigned char *twterm;
if (!(twterm = getenv("LINKS_TWTERM"))) twterm = "twterm -e";
exec_new_links(term, twterm, exe, param);
}
void open_in_new_xterm(struct terminal *term, unsigned char *exe, unsigned char *param)
{
unsigned char *xterm;
if (!(xterm = getenv("LINKS_XTERM"))) xterm = "x-terminal-emulator -e";
exec_new_links(term, xterm, exe, param);
}
void open_in_new_screen(struct terminal *term, unsigned char *exe, unsigned char *param)
{
exec_new_links(term, "screen", exe, param);
}
#ifdef OS2
void open_in_new_vio(struct terminal *term, unsigned char *exe, unsigned char *param)
{
unsigned char *x = stracpy("\"");
add_to_strn(&x, exe);
add_to_strn(&x, "\"");
exec_new_links(term, "start \"Links\" /c /f /win", x, param);
mem_free(x);
}
void open_in_new_fullscreen(struct terminal *term, unsigned char *exe, unsigned char *param)
{
unsigned char *x = stracpy("\"");
add_to_strn(&x, exe);
add_to_strn(&x, "\"");
exec_new_links(term, "start \"Links\" /c /f /fs", x, param);
mem_free(x);
}
#endif
#ifdef WIN32
void open_in_new_win32(struct terminal *term, unsigned char *exe, unsigned char *param)
{
exec_new_links(term, "", exe, param);
}
#endif
#ifdef BEOS
void open_in_new_be(struct terminal *term, unsigned char *exe, unsigned char *param)
{
exec_new_links(term, "Terminal", exe, param);
}
#endif
#ifdef G
void open_in_new_g(struct terminal *term, unsigned char *exe, unsigned char *param)
{
void *info;
unsigned char *target=NULL;
int len;
int base = 0;
unsigned char *url = "";
if (!cmpbeg(param, "-target "))
{
unsigned char *p;
target=param+strlen("-target ");
for (p=target;*p!=' '&&*p;p++);
*p=0;
param=p+1;
}
if (!cmpbeg(param, "-base-session ")) {
base = atoi(param + strlen("-base-session "));
} else {
url = param;
}
if ((info = create_session_info(base, url, target, &len))) attach_g_terminal(info, len);
}
#endif
struct {
int env;
void (*fn)(struct terminal *term, unsigned char *, unsigned char *);
unsigned char *text;
unsigned char *hk;
} oinw[] = {
{ENV_XWIN, open_in_new_xterm, TEXT_(T_XTERM), TEXT_(T_HK_XTERM)},
{ENV_TWIN, open_in_new_twterm, TEXT_(T_TWTERM), TEXT_(T_HK_TWTERM)},
{ENV_SCREEN, open_in_new_screen, TEXT_(T_SCREEN), TEXT_(T_HK_SCREEN)},
#ifdef OS2
{ENV_OS2VIO, open_in_new_vio, TEXT_(T_WINDOW), TEXT_(T_HK_WINDOW)},
{ENV_OS2VIO, open_in_new_fullscreen, TEXT_(T_FULL_SCREEN), TEXT_(T_HK_FULL_SCREEN)},
#endif
#ifdef WIN32
{ENV_WIN32, open_in_new_win32, TEXT_(T_WINDOW), TEXT_(T_HK_WINDOW)},
#endif
#ifdef BEOS
{ENV_BE, open_in_new_be, TEXT_(T_BEOS_TERMINAL), TEXT_(T_HK_BEOS_TERMINAL)},
#endif
#ifdef G
{ENV_G, open_in_new_g, TEXT_(T_WINDOW), TEXT_(T_HK_WINDOW)},
#endif
{0, NULL, NULL, NULL}
};
struct open_in_new *get_open_in_new(int environment)
{
int i;
struct open_in_new *oin = DUMMY;
int noin = 0;
if (anonymous) return NULL;
if (environment & ENV_G) environment = ENV_G;
for (i = 0; oinw[i].env; i++) if ((environment & oinw[i].env) == oinw[i].env) {
if ((unsigned)noin > MAXINT / sizeof(struct open_in_new) - 2) overalloc();
oin = mem_realloc(oin, (noin + 2) * sizeof(struct open_in_new));
oin[noin].text = oinw[i].text;
oin[noin].hk = oinw[i].hk;
oin[noin].fn = oinw[i].fn;
noin++;
oin[noin].text = NULL;
oin[noin].hk = NULL;
oin[noin].fn = NULL;
}
if (oin == DUMMY) return NULL;
return oin;
}
int can_resize_window(int environment)
{
if (environment & (ENV_OS2VIO | ENV_WIN32)) return 1;
return 0;
}
int can_open_os_shell(int environment)
{
#ifdef OS2
if (environment & ENV_XWIN) return 0;
#endif
return 1;
}
#ifndef OS2
void set_highpri(void)
{
}
#else
void set_highpri(void)
{
DosSetPriority(PRTYS_PROCESS, PRTYC_FOREGROUNDSERVER, 0, 0);
}
#endif
#ifndef HAVE_SNPRINTF
#define B_SZ 65536
char snprtintf_buffer[B_SZ];
int my_snprintf(char *str, int n, char *f, ...)
{
int i;
va_list l;
if (!n) return -1;
va_start(l, f);
vsprintf(snprtintf_buffer, f, l);
va_end(l);
i = strlen(snprtintf_buffer);
if (i >= B_SZ) {
error("String size too large!");
va_end(l);
fatal_tty_exit();
exit(RET_FATAL);
}
if (i >= n) {
memcpy(str, snprtintf_buffer, n);
str[n - 1] = 0;
va_end(l);
return -1;
}
strcpy(str, snprtintf_buffer);
va_end(l);
return i;
}
#endif
#ifndef HAVE_MEMMOVE
#define MEMMOVE
typedef long word; /* "word" used for optimal copy speed */
#define wsize sizeof(word)
#define wmask (wsize - 1)
/*
* Copy a block of memory, handling overlap.
* This is the routine that actually implements
* (the portable versions of) bcopy, memcpy, and memmove.
*/
void *
memmove(dst0, src0, length)
void *dst0;
const void *src0;
size_t length;
{
char *dst = dst0;
const char *src = src0;
size_t t;
unsigned long u;
if (length == 0 || dst == src) /* nothing to do */
goto done;
/*
* Macros: loop-t-times; and loop-t-times, t>0
*/
#define TLOOP(s) if (t) TLOOP1(s)
#define TLOOP1(s) do { s; } while (--t)
if ((unsigned long)dst < (unsigned long)src) {
/*
* Copy forward.
*/
u = (unsigned long)src; /* only need low bits */
if ((u | (unsigned long)dst) & wmask) {
/*
* Try to align operands. This cannot be done
* unless the low bits match.
*/
if ((u ^ (unsigned long)dst) & wmask || length < wsize)
t = length;
else
t = wsize - (size_t)(u & wmask);
length -= t;
TLOOP1(*dst++ = *src++);
}
/*
* Copy whole words, then mop up any trailing bytes.
*/
t = length / wsize;
TLOOP(*(word *)(void *)dst = *(const word *)(const void *)src; src += wsize; dst += wsize);
t = length & wmask;
TLOOP(*dst++ = *src++);
} else {
/*
* Copy backwards. Otherwise essentially the same.
* Alignment works as before, except that it takes
* (t&wmask) bytes to align, not wsize-(t&wmask).
*/
src += length;
dst += length;
u = (unsigned long)src;
if ((u | (unsigned long)dst) & wmask) {
if ((u ^ (unsigned long)dst) & wmask || length <= wsize)
t = length;
else
t = (size_t)(u & wmask);
length -= t;
TLOOP1(*--dst = *--src);
}
t = length / wsize;
TLOOP(src -= wsize; dst -= wsize; *(word *)(void *)dst = *(const word *)(const void *)src);
t = length & wmask;
TLOOP(*--dst = *--src);
}
done:
#if defined(MEMCOPY) || defined(MEMMOVE)
return (dst0);
#else
return;
#endif
}
#endif
#ifndef HAVE_RAISE
int
raise(s)
int s;
{
#ifdef HAVE_GETPID
return(kill(getpid(), s));
#else
return 0;
#endif
};
#endif
#ifndef HAVE_STRTOUL
/****yes bad fix***/
unsigned long strtoul(const char *nptr, char **endptr, int base) {
return (unsigned long)strtol(nptr,endptr,base);
};
#endif
#ifndef HAVE_STRERROR
char **sys_errlist;
char *strerror(int errnum) { return sys_errlist[errnum];};
#endif
#ifndef HAVE_GETTIMEOFDAY
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
if (tv) tv->tv_sec = time(NULL), tv->tv_usec = 0;
if (tz) tz->tz_minuteswest = tz->tz_dsttime = 0;
return 0;
}
#endif
#ifndef HAVE_STRCSPN
size_t strcspn(const char *s, const char *reject)
{
size_t r;
for (r = 0; *s; r++, s++) {
const char *rj;
for (rj = reject; *rj; rj++) if (*s == *rj) goto brk;
}
brk:
return r;
}
#endif
#ifndef HAVE_STRSTR
char *strstr(const char *haystack, const char *needle)
{
size_t hs = strlen(haystack);
size_t ns = strlen(needle);
while (hs >= ns) {
if (!memcmp(haystack, needle, ns)) return haystack;
haystack++, hs--;
}
return NULL;
}
#endif
#ifndef HAVE_TEMPNAM
char *tempnam(const char *dir, const char *pfx)
{
static int counter = 0;
unsigned char *d, *s, *a;
int l;
if (!(d = getenv("TMPDIR"))) {
if (dir) d = (unsigned char *)dir;
else if (!(d = getenv("TMP")) && !(d = getenv("TEMP"))) {
#ifdef P_tmpdir
d = P_tmpdir;
#else
d = "/tmp";
#endif
}
}
l = 0;
s = init_str();
add_to_str(&s, &l, d);
if (s[0] && s[strlen(s) - 1] != '/') add_chr_to_str(&s, &l, '/');
add_to_str(&s, &l, (unsigned char *)pfx);
add_num_to_str(&s, &l, counter++);
a = strdup(s);
mem_free(s);
return a;
}
#endif
|