1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
|
/* $OpenBSD: unix.c,v 1.4 1996/10/15 08:07:58 downsj Exp $ */
/* vi:set ts=4 sw=4:
*
* VIM - Vi IMproved by Bram Moolenaar
* OS/2 port by Paul Slootman
*
* Do ":help uganda" in Vim to read copying and usage conditions.
* Do ":help credits" in Vim to see a list of people who contributed.
*/
/*
* unix.c -- code for all flavors of Unix (BSD, SYSV, SVR4, POSIX, ...)
* Also for OS/2, using the excellent EMX package!!!
*
* A lot of this file was originally written by Juergen Weigert and later
* changed beyond recognition.
*/
/*
* Some systems have a prototype for select() that has (int *) instead of
* (fd_set *), which is wrong. This define removes that prototype. We include
* our own prototype in osdef.h.
*/
#define select select_declared_wrong
#include "vim.h"
#include "globals.h"
#include "option.h"
#include "proto.h"
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#include "unixunix.h" /* unix includes for unix.c only */
/*
* Use this prototype for select, some include files have a wrong prototype
*/
#undef select
#if defined(HAVE_SELECT)
extern int select __ARGS((int, fd_set *, fd_set *, fd_set *, struct timeval *));
#endif
/*
* end of autoconf section. To be extended...
*/
/* Are the following #ifdefs still required? And why? Is that for X11? */
#if defined(ESIX) || defined(M_UNIX) && !defined(SCO)
# ifdef SIGWINCH
# undef SIGWINCH
# endif
# ifdef TIOCGWINSZ
# undef TIOCGWINSZ
# endif
#endif
#if defined(SIGWINDOW) && !defined(SIGWINCH) /* hpux 9.01 has it */
# define SIGWINCH SIGWINDOW
#endif
#if defined(HAVE_X11) && defined(WANT_X11)
# include <X11/Xlib.h>
# include <X11/Xutil.h>
# include <X11/Xatom.h>
Window x11_window = 0;
Display *x11_display = NULL;
int got_x_error = FALSE;
static int get_x11_windis __ARGS((void));
static void set_x11_title __ARGS((char_u *));
static void set_x11_icon __ARGS((char_u *));
#endif
static int get_x11_title __ARGS((int));
static int get_x11_icon __ARGS((int));
static void may_core_dump __ARGS((void));
static int Read __ARGS((char_u *, long));
static int WaitForChar __ARGS((long));
static int RealWaitForChar __ARGS((int, long));
static void fill_inbuf __ARGS((int));
#if defined(SIGWINCH)
static RETSIGTYPE sig_winch __ARGS(SIGPROTOARG);
#endif
#if defined(SIGALRM) && defined(HAVE_X11) && defined(WANT_X11)
static RETSIGTYPE sig_alarm __ARGS(SIGPROTOARG);
#endif
static RETSIGTYPE deathtrap __ARGS(SIGPROTOARG);
static void catch_signals __ARGS((RETSIGTYPE (*func)()));
#ifndef __EMX__
static int have_wildcard __ARGS((int, char_u **));
static int have_dollars __ARGS((int, char_u **));
#endif
static int do_resize = FALSE;
static char_u *oldtitle = NULL;
static char_u *fixedtitle = (char_u *)"Thanks for flying Vim";
static char_u *oldicon = NULL;
#ifndef __EMX__
static char_u *extra_shell_arg = NULL;
static int show_shell_mess = TRUE;
#endif
static int deadly_signal = 0; /* The signal we caught */
#ifdef SYS_SIGLIST_DECLARED
/*
* I have seen
* extern char *_sys_siglist[NSIG];
* on Irix, Linux, NetBSD and Solaris. It contains a nice list of strings
* that describe the signals. That is nearly what we want here. But
* autoconf does only check for sys_siglist (without the underscore), I
* do not want to change everything today.... jw.
* This is why AC_DECL_SYS_SIGLIST is commented out in configure.in
*/
#endif
static struct
{
int sig; /* Signal number, eg. SIGSEGV etc */
char *name; /* Signal name (not char_u!). */
} signal_info[] =
{
#ifdef SIGHUP
{SIGHUP, "HUP"},
#endif
#ifdef SIGINT
{SIGINT, "INT"},
#endif
#ifdef SIGQUIT
{SIGQUIT, "QUIT"},
#endif
#ifdef SIGILL
{SIGILL, "ILL"},
#endif
#ifdef SIGTRAP
{SIGTRAP, "TRAP"},
#endif
#ifdef SIGABRT
{SIGABRT, "ABRT"},
#endif
#ifdef SIGEMT
{SIGEMT, "EMT"},
#endif
#ifdef SIGFPE
{SIGFPE, "FPE"},
#endif
#ifdef SIGBUS
{SIGBUS, "BUS"},
#endif
#ifdef SIGSEGV
{SIGSEGV, "SEGV"},
#endif
#ifdef SIGSYS
{SIGSYS, "SYS"},
#endif
#ifdef SIGALRM
{SIGALRM, "ALRM"},
#endif
#ifdef SIGTERM
{SIGTERM, "TERM"},
#endif
#ifdef SIGVTALRM
{SIGVTALRM, "VTALRM"},
#endif
#ifdef SIGPROF
{SIGPROF, "PROF"},
#endif
#ifdef SIGXCPU
{SIGXCPU, "XCPU"},
#endif
#ifdef SIGXFSZ
{SIGXFSZ, "XFSZ"},
#endif
#ifdef SIGUSR1
{SIGUSR1, "USR1"},
#endif
#ifdef SIGUSR2
{SIGUSR2, "USR2"},
#endif
{-1, "Unknown!"}
};
void
mch_write(s, len)
char_u *s;
int len;
{
#ifdef USE_GUI
if (gui.in_use && !gui.dying)
{
gui_write(s, len);
if (p_wd)
gui_mch_wait_for_chars(p_wd);
}
else
#endif
{
write(1, (char *)s, len);
if (p_wd) /* Unix is too fast, slow down a bit more */
RealWaitForChar(0, p_wd);
}
}
/*
* mch_inchar(): low level input funcion.
* Get a characters from the keyboard.
* Return the number of characters that are available.
* If wtime == 0 do not wait for characters.
* If wtime == n wait a short time for characters.
* If wtime == -1 wait forever for characters.
*/
int
mch_inchar(buf, maxlen, wtime)
char_u *buf;
int maxlen;
long wtime; /* don't use "time", MIPS cannot handle it */
{
int len;
#ifdef USE_GUI
if (gui.in_use)
{
if (!gui_mch_wait_for_chars(wtime))
return 0;
return Read(buf, (long)maxlen);
}
#endif
if (wtime >= 0)
{
while (WaitForChar(wtime) == 0) /* no character available */
{
if (!do_resize) /* return if not interrupted by resize */
return 0;
set_winsize(0, 0, FALSE);
do_resize = FALSE;
}
}
else /* wtime == -1 */
{
/*
* If there is no character available within 'updatetime' seconds
* flush all the swap files to disk
* Also done when interrupted by SIGWINCH.
*/
if (WaitForChar(p_ut) == 0)
updatescript(0);
}
for (;;) /* repeat until we got a character */
{
if (do_resize) /* window changed size */
{
set_winsize(0, 0, FALSE);
do_resize = FALSE;
}
/*
* we want to be interrupted by the winch signal
*/
WaitForChar(-1L);
if (do_resize) /* interrupted by SIGWINCHsignal */
continue;
/*
* For some terminals we only get one character at a time.
* We want the get all available characters, so we could keep on
* trying until none is available
* For some other terminals this is quite slow, that's why we don't do
* it.
*/
len = Read(buf, (long)maxlen);
if (len > 0)
{
#ifdef OS2
int i;
for (i = 0; i < len; i++)
if (buf[i] == 0)
buf[i] = K_NUL;
#endif
return len;
}
}
}
/*
* return non-zero if a character is available
*/
int
mch_char_avail()
{
#ifdef USE_GUI
if (gui.in_use)
{
gui_mch_update();
return !is_input_buf_empty();
}
#endif
return WaitForChar(0L);
}
long
mch_avail_mem(special)
int special;
{
#ifdef __EMX__
return ulimit(3, 0L); /* always 32MB? */
#else
return 0x7fffffff; /* virtual memory eh */
#endif
}
void
mch_delay(msec, ignoreinput)
long msec;
int ignoreinput;
{
if (ignoreinput)
#ifndef HAVE_SELECT
poll(NULL, 0, (int)msec);
#else
# ifdef __EMX__
_sleep2(msec);
# else
{
struct timeval tv;
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * 1000;
select(0, NULL, NULL, NULL, &tv);
}
# endif /* __EMX__ */
#endif /* HAVE_SELECT */
else
#ifdef USE_GUI
if (gui.in_use)
gui_mch_wait_for_chars(msec);
else
#endif
WaitForChar(msec);
}
#if defined(SIGWINCH)
/*
* We need correct potatotypes, otherwise mean compilers will barf when the
* second argument to signal() is ``wrong''.
* Let me try it with a few tricky defines from my own osdef.h (jw).
*/
static RETSIGTYPE
sig_winch SIGDEFARG(sigarg)
{
/* this is not required on all systems, but it doesn't hurt anybody */
signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
do_resize = TRUE;
SIGRETURN;
}
#endif
#if defined(SIGALRM) && defined(HAVE_X11) && defined(WANT_X11)
/*
* signal function for alarm().
*/
static RETSIGTYPE
sig_alarm SIGDEFARG(sigarg)
{
/* doesn't do anything, just to break a system call */
SIGRETURN;
}
#endif
void
mch_resize()
{
do_resize = TRUE;
}
/*
* This function handles deadly signals.
* It tries to preserve any swap file and exit properly.
* (partly from Elvis).
*/
static RETSIGTYPE
deathtrap SIGDEFARG(sigarg)
{
static int entered = 0;
#ifdef SIGHASARG
int i;
/* try to find the name of this signal */
for (i = 0; signal_info[i].sig != -1; i++)
if (sigarg == signal_info[i].sig)
break;
deadly_signal = sigarg;
#endif
/*
* If something goes wrong after entering here, we may get here again.
* When this happens, give a message and try to exit nicely (resetting the
* terminal mode, etc.)
* When this happens twice, just exit, don't even try to give a message,
* stack may be corrupt or something weird.
*/
if (entered == 2)
{
may_core_dump();
exit(7);
}
if (entered++)
{
OUTSTR("Vim: Double signal, exiting\n");
flushbuf();
getout(1);
}
sprintf((char *)IObuff, "Vim: Caught %s %s\n",
#ifdef SIGHASARG
"deadly signal", signal_info[i].name);
#else
"some", "deadly signal");
#endif
preserve_exit(); /* preserve files and exit */
SIGRETURN;
}
/*
* If the machine has job control, use it to suspend the program,
* otherwise fake it by starting a new shell.
* When running the GUI iconify the window.
*/
void
mch_suspend()
{
#ifdef USE_GUI
if (gui.in_use)
{
gui_mch_iconify();
return;
}
#endif
#ifdef SIGTSTP
flushbuf(); /* needed to make cursor visible on some systems */
settmode(0);
flushbuf(); /* needed to disable mouse on some systems */
kill(0, SIGTSTP); /* send ourselves a STOP signal */
/*
* Set oldtitle to NULL, so the current title is obtained again.
*/
if (oldtitle != fixedtitle)
{
vim_free(oldtitle);
oldtitle = NULL;
}
settmode(1);
#else
MSG_OUTSTR("new shell started\n");
(void)call_shell(NULL, SHELL_COOKED);
#endif
need_check_timestamps = TRUE;
}
void
mch_windinit()
{
Columns = 80;
Rows = 24;
flushbuf();
(void)mch_get_winsize();
#if defined(SIGWINCH)
/*
* WINDOW CHANGE signal is handled with sig_winch().
*/
signal(SIGWINCH, (RETSIGTYPE (*)())sig_winch);
#endif
/*
* We want the STOP signal to work, to make mch_suspend() work
*/
#ifdef SIGTSTP
signal(SIGTSTP, SIG_DFL);
#endif
/*
* We want to ignore breaking of PIPEs.
*/
#ifdef SIGPIPE
signal(SIGPIPE, SIG_IGN);
#endif
/*
* Arrange for other signals to gracefully shutdown Vim.
*/
catch_signals(deathtrap);
}
static void
catch_signals(func)
RETSIGTYPE (*func)();
{
int i;
for (i = 0; signal_info[i].sig != -1; i++)
signal(signal_info[i].sig, func);
}
void
reset_signals()
{
catch_signals(SIG_DFL);
}
/*
* Check_win checks whether we have an interactive window.
*/
int
mch_check_win(argc, argv)
int argc;
char **argv;
{
if (isatty(1))
return OK;
return FAIL;
}
int
mch_check_input()
{
if (isatty(0))
return OK;
return FAIL;
}
#if defined(HAVE_X11) && defined(WANT_X11)
/*
* X Error handler, otherwise X just exits! (very rude) -- webb
*/
static int
x_error_handler(dpy, error_event)
Display *dpy;
XErrorEvent *error_event;
{
XGetErrorText(dpy, error_event->error_code, (char *)IObuff, IOSIZE);
STRCAT(IObuff, "\nVim: Got X error\n");
#if 1
preserve_exit(); /* preserve files and exit */
#else
printf(IObuff); /* print error message and continue */
/* Makes my system hang */
#endif
return 0; /* NOTREACHED */
}
/*
* Another X Error handler, just used to check for errors.
*/
static int
x_error_check(dpy, error_event)
Display *dpy;
XErrorEvent *error_event;
{
got_x_error = TRUE;
return 0;
}
/*
* try to get x11 window and display
*
* return FAIL for failure, OK otherwise
*/
static int
get_x11_windis()
{
char *winid;
XTextProperty text_prop;
int (*old_handler)();
static int result = -1;
static int x11_display_opened_here = FALSE;
/* X just exits if it finds an error otherwise! */
XSetErrorHandler(x_error_handler);
#ifdef USE_GUI_X11
if (gui.in_use)
{
/*
* If the X11 display was opened here before, for the window where Vim
* was started, close that one now to avoid a memory leak.
*/
if (x11_display_opened_here && x11_display != NULL)
{
XCloseDisplay(x11_display);
x11_display = NULL;
x11_display_opened_here = FALSE;
}
return gui_get_x11_windis(&x11_window, &x11_display);
}
#endif
if (result != -1) /* Have already been here and set this */
return result; /* Don't do all these X calls again */
/*
* If WINDOWID not set, should try another method to find out
* what the current window number is. The only code I know for
* this is very complicated.
* We assume that zero is invalid for WINDOWID.
*/
if (x11_window == 0 && (winid = getenv("WINDOWID")) != NULL)
x11_window = (Window)atol(winid);
if (x11_window != 0 && x11_display == NULL)
{
#ifdef SIGALRM
RETSIGTYPE (*sig_save)();
/*
* Opening the Display may hang if the DISPLAY setting is wrong, or
* the network connection is bad. Set an alarm timer to get out.
*/
sig_save = (RETSIGTYPE (*)())signal(SIGALRM,
(RETSIGTYPE (*)())sig_alarm);
alarm(2);
#endif
x11_display = XOpenDisplay(NULL);
#ifdef SIGALRM
alarm(0);
signal(SIGALRM, (RETSIGTYPE (*)())sig_save);
#endif
if (x11_display != NULL)
{
/*
* Try to get the window title. I don't actually want it yet, so
* there may be a simpler call to use, but this will cause the
* error handler x_error_check() to be called if anything is wrong,
* such as the window pointer being invalid (as can happen when the
* user changes his DISPLAY, but not his WINDOWID) -- webb
*/
old_handler = XSetErrorHandler(x_error_check);
got_x_error = FALSE;
if (XGetWMName(x11_display, x11_window, &text_prop))
XFree((void *)text_prop.value);
XSync(x11_display, False);
if (got_x_error)
{
/* Maybe window id is bad */
x11_window = 0;
XCloseDisplay(x11_display);
x11_display = NULL;
}
else
x11_display_opened_here = TRUE;
XSetErrorHandler(old_handler);
}
}
if (x11_window == 0 || x11_display == NULL)
return (result = FAIL);
return (result = OK);
}
/*
* Determine original x11 Window Title
*/
static int
get_x11_title(test_only)
int test_only;
{
XTextProperty text_prop;
int retval = FALSE;
if (get_x11_windis() == OK)
{
/* Get window name if any */
if (XGetWMName(x11_display, x11_window, &text_prop))
{
if (text_prop.value != NULL)
{
retval = TRUE;
if (!test_only)
oldtitle = strsave((char_u *)text_prop.value);
}
XFree((void *)text_prop.value);
}
}
if (oldtitle == NULL && !test_only) /* could not get old title */
oldtitle = fixedtitle;
return retval;
}
/*
* Determine original x11 Window icon
*/
static int
get_x11_icon(test_only)
int test_only;
{
XTextProperty text_prop;
int retval = FALSE;
if (get_x11_windis() == OK)
{
/* Get icon name if any */
if (XGetWMIconName(x11_display, x11_window, &text_prop))
{
if (text_prop.value != NULL)
{
retval = TRUE;
if (!test_only)
oldicon = strsave((char_u *)text_prop.value);
}
XFree((void *)text_prop.value);
}
}
/* could not get old icon, use terminal name */
if (oldicon == NULL && !test_only)
{
if (STRNCMP(term_strings[KS_NAME], "builtin_", 8) == 0)
oldicon = term_strings[KS_NAME] + 8;
else
oldicon = term_strings[KS_NAME];
}
return retval;
}
/*
* Set x11 Window Title
*
* get_x11_windis() must be called before this and have returned OK
*/
static void
set_x11_title(title)
char_u *title;
{
#if XtSpecificationRelease >= 4
XTextProperty text_prop;
text_prop.value = title;
text_prop.nitems = STRLEN(title);
text_prop.encoding = XA_STRING;
text_prop.format = 8;
XSetWMName(x11_display, x11_window, &text_prop);
#else
XStoreName(x11_display, x11_window, (char *)title);
#endif
XFlush(x11_display);
}
/*
* Set x11 Window icon
*
* get_x11_windis() must be called before this and have returned OK
*/
static void
set_x11_icon(icon)
char_u *icon;
{
#if XtSpecificationRelease >= 4
XTextProperty text_prop;
text_prop.value = icon;
text_prop.nitems = STRLEN(icon);
text_prop.encoding = XA_STRING;
text_prop.format = 8;
XSetWMIconName(x11_display, x11_window, &text_prop);
#else
XSetIconName(x11_display, x11_window, (char *)icon);
#endif
XFlush(x11_display);
}
#else /* HAVE_X11 && WANT_X11 */
static int
get_x11_title(test_only)
int test_only;
{
if (!test_only)
oldtitle = fixedtitle;
return FALSE;
}
static int
get_x11_icon(test_only)
int test_only;
{
if (!test_only)
{
if (STRNCMP(term_strings[KS_NAME], "builtin_", 8) == 0)
oldicon = term_strings[KS_NAME] + 8;
else
oldicon = term_strings[KS_NAME];
}
return FALSE;
}
#endif /* HAVE_X11 && WANT_X11 */
int
mch_can_restore_title()
{
#ifdef USE_GUI
/*
* If GUI is (going to be) used, we can always set the window title.
* Saves a bit of time, because the X11 display server does not need to be
* contacted.
*/
if (gui.starting || gui.in_use)
return TRUE;
#endif
return get_x11_title(TRUE);
}
int
mch_can_restore_icon()
{
#ifdef USE_GUI
/*
* If GUI is (going to be) used, we can always set the icon name.
* Saves a bit of time, because the X11 display server does not need to be
* contacted.
*/
if (gui.starting || gui.in_use)
return TRUE;
#endif
return get_x11_icon(TRUE);
}
/*
* Set the window title and icon.
* Currently only works for x11.
*/
void
mch_settitle(title, icon)
char_u *title;
char_u *icon;
{
int type = 0;
if (term_strings[KS_NAME] == NULL) /* no terminal name (yet) */
return;
if (title == NULL && icon == NULL) /* nothing to do */
return;
/*
* if the window ID and the display is known, we may use X11 calls
*/
#if defined(HAVE_X11) && defined(WANT_X11)
if (get_x11_windis() == OK)
type = 1;
#endif
/*
* Note: if terminal is xterm, title is set with escape sequence rather
* than x11 calls, because the x11 calls don't always work
*/
if (is_xterm(term_strings[KS_NAME]))
type = 2;
if (is_iris_ansi(term_strings[KS_NAME]))
type = 3;
if (type)
{
if (title != NULL)
{
if (oldtitle == NULL) /* first call, save title */
(void)get_x11_title(FALSE);
switch(type)
{
#if defined(HAVE_X11) && defined(WANT_X11)
case 1: set_x11_title(title); /* x11 */
break;
#endif
case 2: outstrn((char_u *)"\033]2;"); /* xterm */
outstrn(title);
outchar(Ctrl('G'));
flushbuf();
break;
case 3: outstrn((char_u *)"\033P1.y"); /* iris-ansi */
outstrn(title);
outstrn((char_u *)"\234");
flushbuf();
break;
}
}
if (icon != NULL)
{
if (oldicon == NULL) /* first call, save icon */
get_x11_icon(FALSE);
switch(type)
{
#if defined(HAVE_X11) && defined(WANT_X11)
case 1: set_x11_icon(icon); /* x11 */
break;
#endif
case 2: outstrn((char_u *)"\033]1;"); /* xterm */
outstrn(icon);
outchar(Ctrl('G'));
flushbuf();
break;
case 3: outstrn((char_u *)"\033P3.y"); /* iris-ansi */
outstrn(icon);
outstrn((char_u *)"\234");
flushbuf();
break;
}
}
}
}
int
is_xterm(name)
char_u *name;
{
if (name == NULL)
return FALSE;
return (vim_strnicmp(name, (char_u *)"xterm", (size_t)5) == 0 ||
STRCMP(name, "builtin_xterm") == 0);
}
int
is_iris_ansi(name)
char_u *name;
{
if (name == NULL)
return FALSE;
return (vim_strnicmp(name, (char_u *)"iris-ansi", (size_t)9) == 0 ||
STRCMP(name, "builtin_iris-ansi") == 0);
}
/*
* Return TRUE if "name" is a terminal for which 'ttyfast' should be set.
* This should include all windowed terminal emulators.
*/
int
is_fastterm(name)
char_u *name;
{
if (name == NULL)
return FALSE;
if (is_xterm(name) || is_iris_ansi(name))
return TRUE;
return (vim_strnicmp(name, (char_u *)"hpterm", (size_t)6) == 0 ||
vim_strnicmp(name, (char_u *)"sun-cmd", (size_t)7) == 0 ||
vim_strnicmp(name, (char_u *)"screen", (size_t)6) == 0 ||
vim_strnicmp(name, (char_u *)"dtterm", (size_t)6) == 0);
}
/*
* Restore the window/icon title.
* which is one of:
* 1 Just restore title
* 2 Just restore icon
* 3 Restore title and icon
*/
void
mch_restore_title(which)
int which;
{
mch_settitle((which & 1) ? oldtitle : NULL, (which & 2) ? oldicon : NULL);
}
/*
* Insert user name in s[len].
* Return OK if a name found.
*/
int
mch_get_user_name(s, len)
char_u *s;
int len;
{
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
struct passwd *pw;
#endif
uid_t uid;
uid = getuid();
#if defined(HAVE_PWD_H) && defined(HAVE_GETPWUID)
if ((pw = getpwuid(uid)) != NULL &&
pw->pw_name != NULL && *pw->pw_name != NUL)
{
STRNCPY(s, pw->pw_name, len);
return OK;
}
#endif
sprintf((char *)s, "%d", (int)uid); /* assumes s is long enough */
return FAIL; /* a number is not a name */
}
/*
* Insert host name is s[len].
*/
#ifdef HAVE_SYS_UTSNAME_H
void
mch_get_host_name(s, len)
char_u *s;
int len;
{
struct utsname vutsname;
uname(&vutsname);
STRNCPY(s, vutsname.nodename, len);
}
#else /* HAVE_SYS_UTSNAME_H */
# ifdef HAVE_SYS_SYSTEMINFO_H
# define gethostname(nam, len) sysinfo(SI_HOSTNAME, nam, len)
# endif
void
mch_get_host_name(s, len)
char_u *s;
int len;
{
gethostname((char *)s, len);
}
#endif /* HAVE_SYS_UTSNAME_H */
/*
* return process ID
*/
long
mch_get_pid()
{
return (long)getpid();
}
#if !defined(HAVE_STRERROR) && defined(USE_GETCWD)
static char *strerror __ARGS((int));
static char *
strerror(err)
int err;
{
extern int sys_nerr;
extern char *sys_errlist[];
static char er[20];
if (err > 0 && err < sys_nerr)
return (sys_errlist[err]);
sprintf(er, "Error %d", err);
return er;
}
#endif
/*
* Get name of current directory into buffer 'buf' of length 'len' bytes.
* Return OK for success, FAIL for failure.
*/
int
mch_dirname(buf, len)
char_u *buf;
int len;
{
#if defined(USE_GETCWD)
if (getcwd((char *)buf, len) == NULL)
{
STRCPY(buf, strerror(errno));
return FAIL;
}
return OK;
#else
return (getwd((char *)buf) != NULL ? OK : FAIL);
#endif
}
#ifdef __EMX__
/*
* Replace all slashes by backslashes.
*/
static void
slash_adjust(p)
char_u *p;
{
while (*p)
{
if (*p == '/')
*p = '\\';
++p;
}
}
#endif
/*
* Get absolute filename into buffer 'buf' of length 'len' bytes.
*
* return FAIL for failure, OK for success
*/
int
FullName(fname, buf, len, force)
char_u *fname, *buf;
int len;
int force; /* also expand when already absolute path name */
{
int l;
#ifdef OS2
int only_drive; /* only a drive letter is specified in file name */
#endif
#ifdef HAVE_FCHDIR
int fd = -1;
static int dont_fchdir = FALSE; /* TRUE when fchdir() doesn't work */
#endif
char_u olddir[MAXPATHL];
char_u *p;
char_u c;
int retval = OK;
if (fname == NULL) /* always fail */
{
*buf = NUL;
return FAIL;
}
*buf = 0;
if (force || !isFullName(fname)) /* if forced or not an absolute path */
{
/*
* If the file name has a path, change to that directory for a moment,
* and then do the getwd() (and get back to where we were).
* This will get the correct path name with "../" things.
*/
#ifdef OS2
only_drive = 0;
if (((p = vim_strrchr(fname, '/')) != NULL) ||
((p = vim_strrchr(fname, '\\')) != NULL) ||
(((p = vim_strchr(fname, ':')) != NULL) && ++only_drive))
#else
if ((p = vim_strrchr(fname, '/')) != NULL)
#endif
{
#ifdef HAVE_FCHDIR
/*
* Use fchdir() if possible, it's said to be faster and more
* reliable. But on SunOS 4 it might not work. Check this by
* doing a fchdir() right now.
*/
if (!dont_fchdir)
{
fd = open(".", O_RDONLY | O_EXTRA);
if (fd >= 0 && fchdir(fd) < 0)
{
close(fd);
fd = -1;
dont_fchdir = TRUE; /* don't try again */
}
}
#endif
if (
#ifdef HAVE_FCHDIR
fd < 0 &&
#endif
mch_dirname(olddir, MAXPATHL) == FAIL)
{
p = NULL; /* can't get current dir: don't chdir */
retval = FAIL;
}
else
{
#ifdef OS2
/*
* compensate for case where ':' from "D:" was the only
* path separator detected in the file name; the _next_
* character has to be removed, and then restored later.
*/
if (only_drive)
p++;
#endif
c = *p;
*p = NUL;
if (vim_chdir((char *)fname))
retval = FAIL;
else
fname = p + 1;
*p = c;
#ifdef OS2
if (only_drive)
{
p--;
if (retval != FAIL)
fname--;
}
#endif
}
}
if (mch_dirname(buf, len) == FAIL)
{
retval = FAIL;
*buf = NUL;
}
l = STRLEN(buf);
if (l && buf[l - 1] != '/')
STRCAT(buf, "/");
if (p != NULL)
{
#ifdef HAVE_FCHDIR
if (fd >= 0)
{
fchdir(fd);
close(fd);
}
else
#endif
vim_chdir((char *)olddir);
}
}
STRCAT(buf, fname);
#ifdef OS2
slash_adjust(buf);
#endif
return retval;
}
/*
* return TRUE is fname is an absolute path name
*/
int
isFullName(fname)
char_u *fname;
{
#ifdef __EMX__
return _fnisabs(fname);
#else
return (*fname == '/' || *fname == '~');
#endif
}
/*
* get file permissions for 'name'
*/
long
getperm(name)
char_u *name;
{
struct stat statb;
if (stat((char *)name, &statb))
return -1;
return statb.st_mode;
}
/*
* set file permission for 'name' to 'perm'
*
* return FAIL for failure, OK otherwise
*/
int
setperm(name, perm)
char_u *name;
int perm;
{
return (chmod((char *)name, (mode_t)perm) == 0 ? OK : FAIL);
}
/*
* return TRUE if "name" is a directory
* return FALSE if "name" is not a directory
* return FALSE for error
*/
int
mch_isdir(name)
char_u *name;
{
struct stat statb;
if (stat((char *)name, &statb))
return FALSE;
#ifdef _POSIX_SOURCE
return (S_ISDIR(statb.st_mode) ? TRUE : FALSE);
#else
return ((statb.st_mode & S_IFMT) == S_IFDIR ? TRUE : FALSE);
#endif
}
void
mch_windexit(r)
int r;
{
settmode(0);
exiting = TRUE;
mch_settitle(oldtitle, oldicon); /* restore xterm title */
stoptermcap();
outchar('\n');
flushbuf();
ml_close_all(TRUE); /* remove all memfiles */
may_core_dump();
exit(r);
}
static void
may_core_dump()
{
if (deadly_signal != 0)
{
signal(deadly_signal, SIG_DFL);
kill(getpid(), deadly_signal); /* Die using the signal we caught */
}
}
static int curr_tmode = 0; /* contains current raw/cooked mode (0 = cooked) */
void
mch_settmode(raw)
int raw;
{
static int first = TRUE;
/* Why is NeXT excluded here (and not in unixunix.h)? */
#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
/* for "new" tty systems */
# ifdef HAVE_TERMIOS_H
static struct termios told;
struct termios tnew;
# else
static struct termio told;
struct termio tnew;
# endif
# ifdef TIOCLGET
static unsigned long tty_local;
# endif
if (raw)
{
if (first)
{
first = FALSE;
# ifdef TIOCLGET
ioctl(0, TIOCLGET, &tty_local);
# endif
# if defined(HAVE_TERMIOS_H)
tcgetattr(0, &told);
# else
ioctl(0, TCGETA, &told);
# endif
}
tnew = told;
/*
* ICRNL enables typing ^V^M
*/
tnew.c_iflag &= ~ICRNL;
tnew.c_lflag &= ~(ICANON | ECHO | ISIG | ECHOE
# if defined(IEXTEN) && !defined(MINT)
| IEXTEN /* IEXTEN enables typing ^V on SOLARIS */
/* but it breaks function keys on MINT */
# endif
);
# ifdef ONLCR /* don't map NL -> CR NL, we do it ourselves */
tnew.c_oflag &= ~ONLCR;
# endif
tnew.c_cc[VMIN] = 1; /* return after 1 char */
tnew.c_cc[VTIME] = 0; /* don't wait */
# if defined(HAVE_TERMIOS_H)
tcsetattr(0, TCSANOW, &tnew);
# else
ioctl(0, TCSETA, &tnew);
# endif
}
else
{
# if defined(HAVE_TERMIOS_H)
tcsetattr(0, TCSANOW, &told);
# else
ioctl(0, TCSETA, &told);
# endif
# ifdef TIOCLGET
ioctl(0, TIOCLSET, &tty_local);
# endif
}
#else
# ifndef TIOCSETN
# define TIOCSETN TIOCSETP /* for hpux 9.0 */
# endif
/* for "old" tty systems */
static struct sgttyb ttybold;
struct sgttyb ttybnew;
if (raw)
{
if (first)
{
first = FALSE;
ioctl(0, TIOCGETP, &ttybold);
}
ttybnew = ttybold;
ttybnew.sg_flags &= ~(CRMOD | ECHO);
ttybnew.sg_flags |= RAW;
ioctl(0, TIOCSETN, &ttybnew);
}
else
ioctl(0, TIOCSETN, &ttybold);
#endif
curr_tmode = raw;
}
/*
* Try to get the code for "t_kb" from the stty setting
*
* Even if termcap claims a backspace key, the user's setting *should*
* prevail. stty knows more about reality than termcap does, and if
* somebody's usual erase key is DEL (which, for most BSD users, it will
* be), they're going to get really annoyed if their erase key starts
* doing forward deletes for no reason. (Eric Fischer)
*/
void
get_stty()
{
char_u buf[2];
char_u *p;
/* Why is NeXT excluded here (and not in unixunix.h)? */
#if defined(ECHOE) && defined(ICANON) && (defined(HAVE_TERMIO_H) || defined(HAVE_TERMIOS_H)) && !defined(__NeXT__)
/* for "new" tty systems */
# ifdef HAVE_TERMIOS_H
struct termios keys;
# else
struct termio keys;
# endif
# if defined(HAVE_TERMIOS_H)
if (tcgetattr(0, &keys) != -1)
# else
if (ioctl(0, TCGETA, &keys) != -1)
# endif
{
buf[0] = keys.c_cc[VERASE];
#else
/* for "old" tty systems */
struct sgttyb keys;
if (ioctl(0, TIOCGETP, &keys) != -1)
{
buf[0] = keys.sg_erase;
#endif
buf[1] = NUL;
add_termcode((char_u *)"kb", buf);
/*
* If <BS> and <DEL> are now the same, redefine <DEL>.
*/
p = find_termcode((char_u *)"kD");
if (p != NULL && p[0] == buf[0] && p[1] == buf[1])
do_fixdel();
}
#if 0
} /* to keep cindent happy */
#endif
}
#ifdef USE_MOUSE
/*
* set mouse clicks on or off (only works for xterms)
*/
void
mch_setmouse(on)
int on;
{
static int ison = FALSE;
if (on == ison) /* return quickly if nothing to do */
return;
if (is_xterm(term_strings[KS_NAME]))
{
if (on)
outstrn((char_u *)"\033[?1000h"); /* xterm: enable mouse events */
else
outstrn((char_u *)"\033[?1000l"); /* xterm: disable mouse events */
}
ison = on;
}
#endif
/*
* set screen mode, always fails.
*/
int
mch_screenmode(arg)
char_u *arg;
{
EMSG("Screen mode setting not supported");
return FAIL;
}
/*
* Try to get the current window size:
* 1. with an ioctl(), most accurate method
* 2. from the environment variables LINES and COLUMNS
* 3. from the termcap
* 4. keep using the old values
*/
int
mch_get_winsize()
{
int old_Rows = Rows;
int old_Columns = Columns;
char_u *p;
#ifdef USE_GUI
if (gui.in_use)
return gui_mch_get_winsize();
#endif
Columns = 0;
Rows = 0;
/*
* For OS/2 use _scrsize().
*/
# ifdef __EMX__
{
int s[2];
_scrsize(s);
Columns = s[0];
Rows = s[1];
}
# endif
/*
* 1. try using an ioctl. It is the most accurate method.
*
* Try using TIOCGWINSZ first, some systems that have it also define TIOCGSIZE
* but don't have a struct ttysize.
*/
# ifdef TIOCGWINSZ
{
struct winsize ws;
if (ioctl(0, TIOCGWINSZ, &ws) == 0)
{
Columns = ws.ws_col;
Rows = ws.ws_row;
}
}
# else /* TIOCGWINSZ */
# ifdef TIOCGSIZE
{
struct ttysize ts;
if (ioctl(0, TIOCGSIZE, &ts) == 0)
{
Columns = ts.ts_cols;
Rows = ts.ts_lines;
}
}
# endif /* TIOCGSIZE */
# endif /* TIOCGWINSZ */
/*
* 2. get size from environment
*/
if (Columns == 0 || Rows == 0)
{
if ((p = (char_u *)getenv("LINES")))
Rows = atoi((char *)p);
if ((p = (char_u *)getenv("COLUMNS")))
Columns = atoi((char *)p);
}
#ifdef HAVE_TGETENT
/*
* 3. try reading the termcap
*/
if (Columns == 0 || Rows == 0)
getlinecol(); /* get "co" and "li" entries from termcap */
#endif
/*
* 4. If everything fails, use the old values
*/
if (Columns <= 0 || Rows <= 0)
{
Columns = old_Columns;
Rows = old_Rows;
return FAIL;
}
check_winsize();
/* if size changed: screenalloc will allocate new screen buffers */
return OK;
}
void
mch_set_winsize()
{
char_u string[10];
#ifdef USE_GUI
if (gui.in_use)
{
gui_mch_set_winsize();
return;
}
#endif
/* try to set the window size to Rows and Columns */
if (is_iris_ansi(term_strings[KS_NAME]))
{
sprintf((char *)string, "\033[203;%ld;%ld/y", Rows, Columns);
outstrn(string);
flushbuf();
screen_start(); /* don't know where cursor is now */
}
}
int
call_shell(cmd, options)
char_u *cmd;
int options; /* SHELL_FILTER if called by do_filter() */
/* SHELL_COOKED if term needs cooked mode */
/* SHELL_EXPAND if called by ExpandWildCards() */
{
#ifdef USE_SYSTEM /* use system() to start the shell: simple but slow */
int x;
#ifndef __EMX__
char_u newcmd[1024]; /* only needed for unix */
#else /* __EMX__ */
/*
* Set the preferred shell in the EMXSHELL environment variable (but
* only if it is different from what is already in the environment).
* Emx then takes care of whether to use "/c" or "-c" in an
* intelligent way. Simply pass the whole thing to emx's system() call.
* Emx also starts an interactive shell if system() is passed an empty
* string.
*/
char_u *p, *old;
if (((old = getenv("EMXSHELL")) == NULL) || strcmp(old, p_sh))
{
/* should check HAVE_SETENV, but I know we don't have it. */
p = alloc(10 + strlen(p_sh));
if (p)
{
sprintf(p, "EMXSHELL=%s", p_sh);
putenv(p); /* don't free the pointer! */
}
}
#endif
flushbuf();
if (options & SHELL_COOKED)
settmode(0); /* set to cooked mode */
#ifdef __EMX__
if (cmd == NULL)
x = system(""); /* this starts an interactive shell in emx */
else
x = system(cmd);
if (x == -1) /* system() returns -1 when error occurs in starting shell */
{
MSG_OUTSTR("\nCannot execute shell ");
msg_outstr(p_sh);
msg_outchar('\n');
}
#else /* not __EMX__ */
if (cmd == NULL)
x = system(p_sh);
else
{
sprintf(newcmd, "%s %s %s \"%s\"", p_sh,
extra_shell_arg == NULL ? "" : (char *)extra_shell_arg,
(char *)p_shcf,
(char *)cmd);
x = system(newcmd);
}
if (x == 127)
{
MSG_OUTSTR("\nCannot execute shell sh\n");
}
#endif /* __EMX__ */
else if (x && !expand_interactively)
{
msg_outchar('\n');
msg_outnum((long)x);
MSG_OUTSTR(" returned\n");
}
settmode(1); /* set to raw mode */
#ifdef OS2
/* external command may change the window size in OS/2, so check it */
mch_get_winsize();
#endif
resettitle();
return (x ? FAIL : OK);
#else /* USE_SYSTEM */ /* don't use system(), use fork()/exec() */
#define EXEC_FAILED 122 /* Exit code when shell didn't execute. Don't use
127, some shell use that already */
char_u newcmd[1024];
int pid;
#ifdef HAVE_UNION_WAIT
union wait status;
#else
int status = -1;
#endif
int retval = FAIL;
char **argv = NULL;
int argc;
int i;
char_u *p;
int inquote;
#ifdef USE_GUI
int pty_master_fd = -1; /* for pty's */
int pty_slave_fd = -1;
char *tty_name;
int fd_toshell[2]; /* for pipes */
int fd_fromshell[2];
int pipe_error = FALSE;
# ifdef HAVE_SETENV
char envbuf[50];
# else
static char envbuf_Rows[20];
static char envbuf_Columns[20];
# endif
#endif
int did_settmode = FALSE; /* TRUE when settmode(1) called */
flushbuf();
if (options & SHELL_COOKED)
settmode(0); /* set to cooked mode */
/*
* 1: find number of arguments
* 2: separate them and built argv[]
*/
STRCPY(newcmd, p_sh);
for (i = 0; i < 2; ++i)
{
p = newcmd;
inquote = FALSE;
argc = 0;
for (;;)
{
if (i == 1)
argv[argc] = (char *)p;
++argc;
while (*p && (inquote || (*p != ' ' && *p != TAB)))
{
if (*p == '"')
inquote = !inquote;
++p;
}
if (*p == NUL)
break;
if (i == 1)
*p++ = NUL;
p = skipwhite(p);
}
if (i == 0)
{
argv = (char **)alloc((unsigned)((argc + 4) * sizeof(char *)));
if (argv == NULL) /* out of memory */
goto error;
}
}
if (cmd != NULL)
{
if (extra_shell_arg != NULL)
argv[argc++] = (char *)extra_shell_arg;
argv[argc++] = (char *)p_shcf;
argv[argc++] = (char *)cmd;
}
argv[argc] = NULL;
#ifdef tower32
/*
* reap lost children (seems necessary on NCR Tower,
* although I don't have a clue why...) (Slootman)
*/
while (wait(&status) != 0 && errno != ECHILD)
; /* do it again, if necessary */
#endif
#ifdef USE_GUI
/*
* First try at using a pseudo-tty to get the stdin/stdout of the executed
* command into the current window for the GUI.
*/
if (gui.in_use && show_shell_mess)
{
/*
* Try to open a master pty.
* If this works, open the slave pty.
* If the slave can't be opened, close the master pty.
*/
if (p_guipty)
{
pty_master_fd = OpenPTY(&tty_name); /* open pty */
if (pty_master_fd >= 0 && ((pty_slave_fd =
open(tty_name, O_RDWR | O_EXTRA)) < 0))
{
close(pty_master_fd);
pty_master_fd = -1;
}
}
/*
* If opening a pty didn't work, try using pipes.
*/
if (pty_master_fd < 0)
{
pipe_error = (pipe(fd_toshell) < 0);
if (!pipe_error) /* pipe create OK */
{
pipe_error = (pipe(fd_fromshell) < 0);
if (pipe_error) /* pipe create failed */
{
close(fd_toshell[0]);
close(fd_toshell[1]);
}
}
if (pipe_error)
{
MSG_OUTSTR("\nCannot create pipes\n");
flushbuf();
}
}
}
if (!pipe_error) /* pty or pipe opened or not used */
#endif
{
if ((pid = fork()) == -1) /* maybe we should use vfork() */
{
MSG_OUTSTR("\nCannot fork\n");
#ifdef USE_GUI
if (gui.in_use && show_shell_mess)
{
if (pty_master_fd >= 0) /* close the pseudo tty */
{
close(pty_master_fd);
close(pty_slave_fd);
}
else /* close the pipes */
{
close(fd_toshell[0]);
close(fd_toshell[1]);
close(fd_fromshell[0]);
close(fd_fromshell[1]);
}
}
#endif
}
else if (pid == 0) /* child */
{
reset_signals(); /* handle signals normally */
if (!show_shell_mess)
{
int fd;
/*
* Don't want to show any message from the shell. Can't just
* close stdout and stderr though, because some systems will
* break if you try to write to them after that, so we must
* use dup() to replace them with something else -- webb
*/
fd = open("/dev/null", O_WRONLY | O_EXTRA);
fclose(stdout);
fclose(stderr);
/*
* If any of these open()'s and dup()'s fail, we just continue
* anyway. It's not fatal, and on most systems it will make
* no difference at all. On a few it will cause the execvp()
* to exit with a non-zero status even when the completion
* could be done, which is nothing too serious. If the open()
* or dup() failed we'd just do the same thing ourselves
* anyway -- webb
*/
if (fd >= 0)
{
/* To replace stdout (file descriptor 1) */
dup(fd);
/* To replace stderr (file descriptor 2) */
dup(fd);
/* Don't need this now that we've duplicated it */
close(fd);
}
}
#ifdef USE_GUI
else if (gui.in_use)
{
#ifdef HAVE_SETSID
(void)setsid();
#endif
#ifdef TIOCSCTTY
/* try to become controlling tty (probably doesn't work,
* unless run by root) */
ioctl(pty_slave_fd, TIOCSCTTY, (char *)NULL);
#endif
/* Simulate to have a dumb terminal (for now) */
#ifdef HAVE_SETENV
setenv("TERM", "dumb", 1);
sprintf((char *)envbuf, "%ld", Rows);
setenv("ROWS", (char *)envbuf, 1);
sprintf((char *)envbuf, "%ld", Columns);
setenv("COLUMNS", (char *)envbuf, 1);
#else
/*
* Putenv does not copy the string, it has to remain valid.
* Use a static array to avoid loosing allocated memory.
*/
putenv("TERM=dumb");
sprintf(envbuf_Rows, "ROWS=%ld", Rows);
putenv(envbuf_Rows);
sprintf(envbuf_Columns, "COLUMNS=%ld", Columns);
putenv(envbuf_Columns);
#endif
if (pty_master_fd >= 0)
{
close(pty_master_fd); /* close master side of pty */
/* set up stdin/stdout/stderr for the child */
close(0);
dup(pty_slave_fd);
close(1);
dup(pty_slave_fd);
close(2);
dup(pty_slave_fd);
close(pty_slave_fd); /* has been dupped, close it now */
}
else
{
/* set up stdin for the child */
close(fd_toshell[1]);
close(0);
dup(fd_toshell[0]);
close(fd_toshell[0]);
/* set up stdout for the child */
close(fd_fromshell[0]);
close(1);
dup(fd_fromshell[1]);
close(fd_fromshell[1]);
/* set up stderr for the child */
close(2);
dup(1);
}
}
#endif
/*
* There is no type cast for the argv, because the type may be
* different on different machines. This may cause a warning
* message with strict compilers, don't worry about it.
*/
execvp(argv[0], argv);
exit(EXEC_FAILED); /* exec failed, return failure code */
}
else /* parent */
{
/*
* While child is running, ignore terminating signals.
*/
catch_signals(SIG_IGN);
#ifdef USE_GUI
/*
* For the GUI we redirect stdin, stdout and stderr to our window.
*/
if (gui.in_use && show_shell_mess)
{
#define BUFLEN 100 /* length for buffer, pseudo tty limit is 128 */
char_u buffer[BUFLEN];
int len;
int p_more_save;
int old_State;
int read_count;
int c;
int toshell_fd;
int fromshell_fd;
if (pty_master_fd >= 0)
{
close(pty_slave_fd); /* close slave side of pty */
fromshell_fd = pty_master_fd;
toshell_fd = dup(pty_master_fd);
}
else
{
close(fd_toshell[0]);
close(fd_fromshell[1]);
toshell_fd = fd_toshell[1];
fromshell_fd = fd_fromshell[0];
}
/*
* Write to the child if there are typed characters.
* Read from the child if there are characters available.
* Repeat the reading a few times if more characters are
* available. Need to check for typed keys now and then, but
* not too often (delays when no chars are available).
* This loop is quit if no characters can be read from the pty
* (WaitForChar detected special condition), or there are no
* characters available and the child has exited.
* Only check if the child has exited when there is no more
* output. The child may exit before all the output has
* been printed.
*
* Currently this busy loops!
* This can probably dead-lock when the write blocks!
*/
p_more_save = p_more;
p_more = FALSE;
old_State = State;
State = EXTERNCMD; /* don't redraw at window resize */
for (;;)
{
/*
* Check if keys have been typed, write them to the child
* if there are any. Don't do this if we are expanding
* wild cards (would eat typeahead).
*/
if (!(options & SHELL_EXPAND) &&
(len = mch_inchar(buffer, BUFLEN - 1, 10)) != 0)
{
/*
* For pipes:
* Check for CTRL-C: sent interrupt signal to child.
* Check for CTRL-D: EOF, close pipe to child.
*/
if (len == 1 && (pty_master_fd < 0 || cmd != NULL))
{
#ifdef SIGINT
if (buffer[0] == Ctrl('C'))
/* send SIGINT to all processes in our group */
kill(0, SIGINT);
#endif
if (pty_master_fd < 0 && toshell_fd >= 0 &&
buffer[0] == Ctrl('D'))
{
close(toshell_fd);
toshell_fd = -1;
}
}
/* replace K_BS by <BS> and K_DEL by <DEL> */
for (i = 0; i < len; ++i)
{
if (buffer[i] == CSI && len - i > 2)
{
c = TERMCAP2KEY(buffer[i + 1], buffer[i + 2]);
if (c == K_DEL || c == K_BS)
{
vim_memmove(buffer + i + 1, buffer + i + 3,
(size_t)(len - i - 2));
if (c == K_DEL)
buffer[i] = DEL;
else
buffer[i] = Ctrl('H');
len -= 2;
}
}
else if (buffer[i] == '\r')
buffer[i] = '\n';
}
/*
* For pipes: echo the typed characters.
* For a pty this does not seem to work.
*/
if (pty_master_fd < 0)
{
for (i = 0; i < len; ++i)
if (buffer[i] == '\n' || buffer[i] == '\b')
msg_outchar(buffer[i]);
else
msg_outtrans_len(buffer + i, 1);
windgoto(msg_row, msg_col);
flushbuf();
}
/*
* Write the characters to the child, unless EOF has
* been typed for pipes. Ignore errors.
*/
if (toshell_fd >= 0)
write(toshell_fd, (char *)buffer, (size_t)len);
}
/*
* Check if the child has any characters to be printed.
* Read them and write them to our window.
* Repeat this a few times as long as there is something
* to do, avoid the 10ms wait for mch_inchar().
* TODO: This should handle escape sequences.
*/
for (read_count = 0; read_count < 10 &&
RealWaitForChar(fromshell_fd, 10); ++read_count)
{
len = read(fromshell_fd, (char *)buffer,
(size_t)(BUFLEN - 1));
if (len <= 0) /* end of file or error */
goto finished;
buffer[len] = NUL;
msg_outstr(buffer);
windgoto(msg_row, msg_col);
cursor_on();
flushbuf();
}
/*
* Check if the child still exists when we finished
* outputting all characters.
*/
if (read_count == 0 &&
#ifdef __NeXT__
wait4(pid, &status, WNOHANG, (struct rusage *) 0) &&
#else
waitpid(pid, &status, WNOHANG) &&
#endif
WIFEXITED(status))
break;
}
finished:
p_more = p_more_save;
State = old_State;
if (toshell_fd >= 0)
close(toshell_fd);
close(fromshell_fd);
}
#endif /* USE_GUI */
/*
* Wait until child has exited.
*/
#ifdef ECHILD
/* Don't stop waiting when a signal (e.g. SIGWINCH) is received. */
while (wait(&status) == -1 && errno != ECHILD)
;
#else
wait(&status);
#endif
/*
* Set to raw mode right now, otherwise a CTRL-C after
* catch_signals will kill Vim.
*/
settmode(1);
did_settmode = TRUE;
catch_signals(deathtrap);
/*
* Check the window size, in case it changed while executing the
* external command.
*/
mch_get_winsize();
if (WIFEXITED(status))
{
i = WEXITSTATUS(status);
if (i)
{
if (i == EXEC_FAILED)
{
MSG_OUTSTR("\nCannot execute shell ");
msg_outtrans(p_sh);
msg_outchar('\n');
}
else if (!expand_interactively)
{
msg_outchar('\n');
msg_outnum((long)i);
MSG_OUTSTR(" returned\n");
}
}
else
retval = OK;
}
else
MSG_OUTSTR("\nCommand terminated\n");
}
}
vim_free(argv);
error:
if (!did_settmode)
settmode(1); /* always set to raw mode */
resettitle();
return retval;
#endif /* USE_SYSTEM */
}
/*
* The input characters are buffered to be able to check for a CTRL-C.
* This should be done with signals, but I don't know how to do that in
* a portable way for a tty in RAW mode.
*/
/*
* Internal typeahead buffer. Includes extra space for long key code
* descriptions which would otherwise overflow. The buffer is considered full
* when only this extra space (or part of it) remains.
*/
#define INBUFLEN 250
static char_u inbuf[INBUFLEN + MAX_KEY_CODE_LEN];
static int inbufcount = 0; /* number of chars in inbuf[] */
/*
* is_input_buf_full(), is_input_buf_empty(), add_to_input_buf(), and
* trash_input_buf() are functions for manipulating the input buffer. These
* are used by the gui_* calls when a GUI is used to handle keyboard input.
*
* NOTE: These functions will be identical in msdos.c etc, and should probably
* be taken out and put elsewhere, but at the moment inbuf is only local.
*/
int
is_input_buf_full()
{
return (inbufcount >= INBUFLEN);
}
int
is_input_buf_empty()
{
return (inbufcount == 0);
}
/* Add the given bytes to the input buffer */
void
add_to_input_buf(s, len)
char_u *s;
int len;
{
if (inbufcount + len > INBUFLEN + MAX_KEY_CODE_LEN)
return; /* Shouldn't ever happen! */
while (len--)
inbuf[inbufcount++] = *s++;
}
/* Remove everything from the input buffer. Called when ^C is found */
void
trash_input_buf()
{
inbufcount = 0;
}
static int
Read(buf, maxlen)
char_u *buf;
long maxlen;
{
if (inbufcount == 0) /* if the buffer is empty, fill it */
fill_inbuf(TRUE);
if (maxlen > inbufcount)
maxlen = inbufcount;
vim_memmove(buf, inbuf, (size_t)maxlen);
inbufcount -= maxlen;
if (inbufcount)
vim_memmove(inbuf, inbuf + maxlen, (size_t)inbufcount);
return (int)maxlen;
}
void
mch_breakcheck()
{
#ifdef USE_GUI
if (gui.in_use)
{
gui_mch_update();
return;
}
#endif /* USE_GUI */
/*
* Check for CTRL-C typed by reading all available characters.
* In cooked mode we should get SIGINT, no need to check.
*/
if (curr_tmode && RealWaitForChar(0, 0L)) /* if characters available */
fill_inbuf(FALSE);
}
static void
fill_inbuf(exit_on_error)
int exit_on_error;
{
int len;
int try;
#ifdef USE_GUI
if (gui.in_use)
{
gui_mch_update();
return;
}
#endif
if (is_input_buf_full())
return;
/*
* Fill_inbuf() is only called when we really need a character.
* If we can't get any, but there is some in the buffer, just return.
* If we can't get any, and there isn't any in the buffer, we give up and
* exit Vim.
*/
for (try = 0; try < 100; ++try)
{
len = read(0, (char *)inbuf + inbufcount,
(size_t)(INBUFLEN - inbufcount));
if (len > 0)
break;
if (!exit_on_error)
return;
}
if (len <= 0)
{
windgoto((int)Rows - 1, 0);
fprintf(stderr, "Vim: Error reading input, exiting...\n");
ml_sync_all(FALSE, TRUE); /* preserve all swap files */
getout(1);
}
while (len-- > 0)
{
/*
* if a CTRL-C was typed, remove it from the buffer and set got_int
*/
if (inbuf[inbufcount] == 3)
{
/* remove everything typed before the CTRL-C */
vim_memmove(inbuf, inbuf + inbufcount, (size_t)(len + 1));
inbufcount = 0;
got_int = TRUE;
}
++inbufcount;
}
}
/*
* Wait "msec" msec until a character is available from the keyboard or from
* inbuf[]. msec == -1 will block forever.
* When a GUI is being used, this will never get called -- webb
*/
static int
WaitForChar(msec)
long msec;
{
if (inbufcount) /* something in inbuf[] */
return 1;
return RealWaitForChar(0, msec);
}
/*
* Wait "msec" msec until a character is available from file descriptor "fd".
* Time == -1 will block forever.
* When a GUI is being used, this will not be used for input -- webb
*/
static int
RealWaitForChar(fd, msec)
int fd;
long msec;
{
#ifndef HAVE_SELECT
struct pollfd fds;
fds.fd = fd;
fds.events = POLLIN;
return (poll(&fds, 1, (int)msec) > 0); /* is this correct when fd != 0?? */
#else
struct timeval tv;
fd_set rfds, efds;
# ifdef __EMX__
/* don't check for incoming chars if not in raw mode, because select()
* always returns TRUE then (in some version of emx.dll) */
if (curr_tmode == 0)
return 0;
# endif
if (msec >= 0)
{
tv.tv_sec = msec / 1000;
tv.tv_usec = (msec % 1000) * (1000000/1000);
}
/*
* Select on ready for reading and exceptional condition (end of file).
*/
FD_ZERO(&rfds); /* calls bzero() on a sun */
FD_ZERO(&efds);
FD_SET(fd, &rfds);
#ifndef __QNX__
/* For QNX select() always returns 1 if this is set. Why? */
FD_SET(fd, &efds);
#endif
return (select(fd + 1, &rfds, NULL, &efds, (msec >= 0) ? &tv : NULL) > 0);
#endif
}
/*
* ExpandWildCards() - this code does wild-card pattern matching using the shell
*
* return OK for success, FAIL for error (you may lose some memory) and put
* an error message in *file.
*
* num_pat is number of input patterns
* pat is array of pointers to input patterns
* num_file is pointer to number of matched file names
* file is pointer to array of pointers to matched file names
* On Unix we do not check for files only yet
* list_notfound is ignored
*/
#ifndef SEEK_SET
# define SEEK_SET 0
#endif
#ifndef SEEK_END
# define SEEK_END 2
#endif
int
ExpandWildCards(num_pat, pat, num_file, file, files_only, list_notfound)
int num_pat;
char_u **pat;
int *num_file;
char_u ***file;
int files_only;
int list_notfound;
{
int i;
size_t len;
char_u *p;
#ifdef __EMX__
# define EXPL_ALLOC_INC 16
char_u **expl_files;
size_t files_alloced, files_free;
char_u *buf;
int has_wildcard;
*num_file = 0; /* default: no files found */
files_alloced = EXPL_ALLOC_INC; /* how much space is allocated */
files_free = EXPL_ALLOC_INC; /* how much space is not used */
*file = (char_u **) alloc(sizeof(char_u **) * files_alloced);
if (*file == NULL)
return FAIL;
for (; num_pat > 0; num_pat--, pat++)
{
expl_files = NULL;
if (vim_strchr(*pat, '$') || vim_strchr(*pat, '~'))
{
/* expand environment var or home dir */
buf = alloc(MAXPATHL);
if (buf == NULL)
return FAIL;
expand_env(*pat, buf, MAXPATHL);
}
else
{
buf = strsave(*pat);
}
expl_files = NULL;
has_wildcard = mch_has_wildcard(buf); /* (still) wildcards in there? */
if (has_wildcard) /* yes, so expand them */
expl_files = (char_u **)_fnexplode(buf);
/*
* return value of buf if no wildcards left,
* OR if no match AND list_notfound is true.
*/
if (!has_wildcard || (expl_files == NULL && list_notfound))
{ /* simply save the current contents of *buf */
expl_files = (char_u **)alloc(sizeof(char_u **) * 2);
if (expl_files != NULL)
{
expl_files[0] = strsave(buf);
expl_files[1] = NULL;
}
}
vim_free(buf);
/*
* Count number of names resulting from expansion,
* At the same time add a backslash to the end of names that happen to
* be directories, and replace slashes with backslashes.
*/
if (expl_files)
{
for (i = 0; (p = expl_files[i]) != NULL; i++, (*num_file)++)
{
if (--files_free == 0)
{
/* need more room in table of pointers */
files_alloced += EXPL_ALLOC_INC;
*file = (char_u **) realloc(*file,
sizeof(char_u **) * files_alloced);
if (*file == NULL)
{
emsg(e_outofmem);
*num_file = 0;
return FAIL;
}
files_free = EXPL_ALLOC_INC;
}
slash_adjust(p);
if (mch_isdir(p))
{
len = strlen(p);
if (((*file)[*num_file] = alloc(len + 2)) != NULL)
{
strcpy((*file)[*num_file], p);
(*file)[*num_file][len] = '\\';
(*file)[*num_file][len+1] = 0;
}
}
else
{
(*file)[*num_file] = strsave(p);
}
/*
* Error message already given by either alloc or strsave.
* Should return FAIL, but returning OK works also.
*/
if ((*file)[*num_file] == NULL)
break;
}
_fnexplodefree((char **)expl_files);
}
}
return OK;
#else /* __EMX__ */
int dir;
char_u *tempname;
char_u *command;
FILE *fd;
char_u *buffer;
int use_glob = FALSE;
*num_file = 0; /* default: no files found */
*file = (char_u **)"";
/*
* If there are no wildcards, just copy the names to allocated memory.
* Saves a lot of time, because we don't have to start a new shell.
*/
if (!have_wildcard(num_pat, pat))
{
*file = (char_u **)alloc(num_pat * sizeof(char_u *));
if (*file == NULL)
{
*file = (char_u **)"";
return FAIL;
}
for (i = 0; i < num_pat; i++)
(*file)[i] = strsave(pat[i]);
*num_file = num_pat;
return OK;
}
/*
* get a name for the temp file
*/
if ((tempname = vim_tempname('o')) == NULL)
{
emsg(e_notmp);
return FAIL;
}
/*
* let the shell expand the patterns and write the result into the temp file
* If we use csh, glob will work better than echo.
*/
if ((len = STRLEN(p_sh)) >= 3 && STRCMP(p_sh + len - 3, "csh") == 0)
use_glob = TRUE;
len = STRLEN(tempname) + 12;
for (i = 0; i < num_pat; ++i) /* count the length of the patterns */
len += STRLEN(pat[i]) + 3;
command = alloc(len);
if (command == NULL)
{
vim_free(tempname);
return FAIL;
}
if (use_glob)
STRCPY(command, "glob >"); /* build the shell command */
else
STRCPY(command, "echo >"); /* build the shell command */
STRCAT(command, tempname);
for (i = 0; i < num_pat; ++i)
{
#ifdef USE_SYSTEM
STRCAT(command, " \""); /* need extra quotes because we */
STRCAT(command, pat[i]); /* start the shell twice */
STRCAT(command, "\"");
#else
STRCAT(command, " ");
STRCAT(command, pat[i]);
#endif
}
if (expand_interactively)
show_shell_mess = FALSE;
/*
* If we use -f then shell variables set in .cshrc won't get expanded.
* vi can do it, so we will too, but it is only necessary if there is a "$"
* in one of the patterns, otherwise we can still use the fast option.
*/
if (use_glob && !have_dollars(num_pat, pat)) /* Use csh fast option */
extra_shell_arg = (char_u *)"-f";
i = call_shell(command, SHELL_EXPAND); /* execute it */
extra_shell_arg = NULL;
show_shell_mess = TRUE;
vim_free(command);
if (i == FAIL) /* call_shell failed */
{
vim_remove(tempname);
vim_free(tempname);
/*
* With interactive completion, the error message is not printed.
* However with USE_SYSTEM, I don't know how to turn off error messages
* from the shell, so screen may still get messed up -- webb.
*/
#ifndef USE_SYSTEM
if (!expand_interactively)
#endif
{
must_redraw = CLEAR; /* probably messed up screen */
msg_outchar('\n'); /* clear bottom line quickly */
cmdline_row = Rows - 1; /* continue on last line */
}
return FAIL;
}
/*
* read the names from the file into memory
*/
fd = fopen((char *)tempname, "r");
if (fd == NULL)
{
emsg2(e_notopen, tempname);
vim_free(tempname);
return FAIL;
}
fseek(fd, 0L, SEEK_END);
len = ftell(fd); /* get size of temp file */
fseek(fd, 0L, SEEK_SET);
buffer = alloc(len + 1);
if (buffer == NULL)
{
vim_remove(tempname);
vim_free(tempname);
fclose(fd);
return FAIL;
}
i = fread((char *)buffer, 1, len, fd);
fclose(fd);
vim_remove(tempname);
if (i != len)
{
emsg2(e_notread, tempname);
vim_free(tempname);
vim_free(buffer);
return FAIL;
}
vim_free(tempname);
if (use_glob) /* file names are separated with NUL */
{
buffer[len] = NUL; /* make sure the buffers ends in NUL */
i = 0;
for (p = buffer; p < buffer + len; ++p)
if (*p == NUL) /* count entry */
++i;
if (len)
++i; /* count last entry */
}
else /* file names are separated with SPACE */
{
buffer[len] = '\n'; /* make sure the buffers ends in NL */
p = buffer;
for (i = 0; *p != '\n'; ++i) /* count number of entries */
{
while (*p != ' ' && *p != '\n') /* skip entry */
++p;
p = skipwhite(p); /* skip to next entry */
}
}
if (i == 0)
{
/*
* Can happen when using /bin/sh and typing ":e $NO_SUCH_VAR^I".
* /bin/sh will happily expand it to nothing rather than returning an
* error; and hey, it's good to check anyway -- webb.
*/
vim_free(buffer);
*file = (char_u **)"";
return FAIL;
}
*num_file = i;
*file = (char_u **)alloc(sizeof(char_u *) * i);
if (*file == NULL)
{
vim_free(buffer);
*file = (char_u **)"";
return FAIL;
}
/*
* Isolate the individual file names.
*/
p = buffer;
for (i = 0; i < *num_file; ++i)
{
(*file)[i] = p;
if (use_glob)
{
while (*p && p < buffer + len) /* skip entry */
++p;
++p; /* skip NUL */
}
else
{
while (*p != ' ' && *p != '\n') /* skip entry */
++p;
if (*p == '\n') /* last entry */
*p = NUL;
else
{
*p++ = NUL;
p = skipwhite(p); /* skip to next entry */
}
}
}
/*
* Move the file names to allocated memory.
*/
for (i = 0; i < *num_file; ++i)
{
/* Require the files to exist. Helps when using /bin/sh */
if (expand_interactively)
{
struct stat st;
int j;
if (stat((char *)((*file)[i]), &st) < 0)
{
for (j = i; j + 1 < *num_file; ++j)
(*file)[j] = (*file)[j + 1];
--*num_file;
--i;
continue;
}
}
/* if file doesn't exist don't add '/' */
dir = (mch_isdir((*file)[i]));
p = alloc((unsigned)(STRLEN((*file)[i]) + 1 + dir));
if (p)
{
STRCPY(p, (*file)[i]);
if (dir)
STRCAT(p, "/");
}
(*file)[i] = p;
}
vim_free(buffer);
if (*num_file == 0) /* rejected all entries */
{
vim_free(*file);
*file = (char_u **)"";
return FAIL;
}
return OK;
#endif /* __EMX__ */
}
int
mch_has_wildcard(p)
char_u *p;
{
for ( ; *p; ++p)
{
if (*p == '\\' && p[1] != NUL)
++p;
else if (vim_strchr((char_u *)"*?[{`~$", *p) != NULL)
return TRUE;
}
return FALSE;
}
#ifndef __EMX__
static int
have_wildcard(num, file)
int num;
char_u **file;
{
register int i;
for (i = 0; i < num; i++)
if (mch_has_wildcard(file[i]))
return 1;
return 0;
}
static int
have_dollars(num, file)
int num;
char_u **file;
{
register int i;
for (i = 0; i < num; i++)
if (vim_strchr(file[i], '$') != NULL)
return TRUE;
return FALSE;
}
#endif /* ifndef __EMX__ */
#ifndef HAVE_RENAME
/*
* Scaled-down version of rename, which is missing in Xenix.
* This version can only move regular files and will fail if the
* destination exists.
*/
int
rename(src, dest)
const char *src, *dest;
{
struct stat st;
if (stat(dest, &st) >= 0) /* fail if destination exists */
return -1;
if (link(src, dest) != 0) /* link file to new name */
return -1;
if (vim_remove(src) == 0) /* delete link to old name */
return 0;
return -1;
}
#endif /* !HAVE_RENAME */
|