unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
blob ed2cab7b51f6db457417400676f7fc2d0b8c8c7e 246195 bytes (raw)
name: src/process.c 	 # note: path name is non-authoritative(*)

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
 
/* Asynchronous subprocess control for GNU Emacs.

Copyright (C) 1985-1988, 1993-1996, 1998-1999, 2001-2018 Free Software
Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */


#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>		/* Some typedefs are used in sys/file.h.  */
#include <sys/file.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#include "lisp.h"

/* Only MS-DOS does not define `subprocesses'.  */
#ifdef subprocesses

#include <sys/socket.h>
#include <netdb.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#endif	/* subprocesses */

#ifdef HAVE_SETRLIMIT
# include <sys/resource.h>

/* If NOFILE_LIMIT.rlim_cur is greater than FD_SETSIZE, then
   NOFILE_LIMIT is the initial limit on the number of open files,
   which should be restored in child processes.  */
static struct rlimit nofile_limit;
#endif

#ifdef subprocesses

/* Are local (unix) sockets supported?  */
#if defined (HAVE_SYS_UN_H)
#if !defined (AF_LOCAL) && defined (AF_UNIX)
#define AF_LOCAL AF_UNIX
#endif
#ifdef AF_LOCAL
#define HAVE_LOCAL_SOCKETS
#include <sys/un.h>
#endif
#endif

#include <sys/ioctl.h>
#if defined (HAVE_NET_IF_H)
#include <net/if.h>
#endif /* HAVE_NET_IF_H */

#if defined (HAVE_IFADDRS_H)
/* Must be after net/if.h */
#include <ifaddrs.h>

/* We only use structs from this header when we use getifaddrs.  */
#if defined (HAVE_NET_IF_DL_H)
#include <net/if_dl.h>
#endif

#endif

#ifdef NEED_BSDTTY
#include <bsdtty.h>
#endif

#ifdef USG5_4
# include <sys/stream.h>
# include <sys/stropts.h>
#endif

#ifdef HAVE_UTIL_H
#include <util.h>
#endif

#ifdef HAVE_PTY_H
#include <pty.h>
#endif

#include <c-ctype.h>
#include <flexmember.h>
#include <sig2str.h>
#include <verify.h>

#endif	/* subprocesses */

#include "systime.h"
#include "systty.h"

#include "window.h"
#include "character.h"
#include "buffer.h"
#include "coding.h"
#include "process.h"
#include "frame.h"
#include "termopts.h"
#include "keyboard.h"
#include "blockinput.h"
#include "atimer.h"
#include "sysselect.h"
#include "syssignal.h"
#include "syswait.h"
#ifdef HAVE_GNUTLS
#include "gnutls.h"
#endif

#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */

#ifdef HAVE_GLIB
#include "xgselect.h"
#ifndef WINDOWSNT
#include <glib.h>
#endif
#endif

#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS
/* This is 0.1s in nanoseconds. */
#define ASYNC_RETRY_NSEC 100000000
#endif

#ifdef WINDOWSNT
extern int sys_select (int, fd_set *, fd_set *, fd_set *,
                       const struct timespec *, const sigset_t *);
#endif

/* Work around GCC 4.3.0 bug with strict overflow checking; see
   <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52904>.
   This bug appears to be fixed in GCC 5.1, so don't work around it there.  */
#if GNUC_PREREQ (4, 3, 0) && ! GNUC_PREREQ (5, 1, 0)
# pragma GCC diagnostic ignored "-Wstrict-overflow"
#endif
\f
/* True if keyboard input is on hold, zero otherwise.  */

static bool kbd_is_on_hold;

/* Nonzero means don't run process sentinels.  This is used
   when exiting.  */
bool inhibit_sentinels;

union u_sockaddr
{
  struct sockaddr sa;
  struct sockaddr_in in;
#ifdef AF_INET6
  struct sockaddr_in6 in6;
#endif
#ifdef HAVE_LOCAL_SOCKETS
  struct sockaddr_un un;
#endif
};

#ifdef subprocesses

#ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC 0
#endif
#ifndef SOCK_NONBLOCK
# define SOCK_NONBLOCK 0
#endif

/* True if ERRNUM represents an error where the system call would
   block if a blocking variant were used.  */
static bool
would_block (int errnum)
{
#ifdef EWOULDBLOCK
  if (EWOULDBLOCK != EAGAIN && errnum == EWOULDBLOCK)
    return true;
#endif
  return errnum == EAGAIN;
}

#ifndef HAVE_ACCEPT4

/* Emulate GNU/Linux accept4 and socket well enough for this module.  */

static int
close_on_exec (int fd)
{
  if (0 <= fd)
    fcntl (fd, F_SETFD, FD_CLOEXEC);
  return fd;
}

# undef accept4
# define accept4(sockfd, addr, addrlen, flags) \
    process_accept4 (sockfd, addr, addrlen, flags)
static int
accept4 (int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags)
{
  return close_on_exec (accept (sockfd, addr, addrlen));
}

static int
process_socket (int domain, int type, int protocol)
{
  return close_on_exec (socket (domain, type, protocol));
}
# undef socket
# define socket(domain, type, protocol) process_socket (domain, type, protocol)
#endif

#define NETCONN_P(p) (EQ (XPROCESS (p)->type, Qnetwork))
#define NETCONN1_P(p) (EQ (p->type, Qnetwork))
#define SERIALCONN_P(p) (EQ (XPROCESS (p)->type, Qserial))
#define SERIALCONN1_P(p) (EQ (p->type, Qserial))
#define PIPECONN_P(p) (EQ (XPROCESS (p)->type, Qpipe))
#define PIPECONN1_P(p) (EQ (p->type, Qpipe))

/* Number of events of change of status of a process.  */
static EMACS_INT process_tick;
/* Number of events for which the user or sentinel has been notified.  */
static EMACS_INT update_tick;

/* Define DATAGRAM_SOCKETS if datagrams can be used safely on
   this system.  We need to read full packets, so we need a
   "non-destructive" select.  So we require either native select,
   or emulation of select using FIONREAD.  */

#ifndef BROKEN_DATAGRAM_SOCKETS
# if defined HAVE_SELECT || defined USABLE_FIONREAD
#  if defined HAVE_SENDTO && defined HAVE_RECVFROM && defined EMSGSIZE
#   define DATAGRAM_SOCKETS
#  endif
# endif
#endif

#if defined HAVE_LOCAL_SOCKETS && defined DATAGRAM_SOCKETS
# define HAVE_SEQPACKET
#endif

#define READ_OUTPUT_DELAY_INCREMENT (TIMESPEC_RESOLUTION / 100)
#define READ_OUTPUT_DELAY_MAX       (READ_OUTPUT_DELAY_INCREMENT * 5)
#define READ_OUTPUT_DELAY_MAX_MAX   (READ_OUTPUT_DELAY_INCREMENT * 7)

/* Number of processes which have a non-zero read_output_delay,
   and therefore might be delayed for adaptive read buffering.  */

static int process_output_delay_count;

/* True if any process has non-nil read_output_skip.  */

static bool process_output_skip;

static void start_process_unwind (Lisp_Object);
static void create_process (Lisp_Object, char **, Lisp_Object);
#ifdef USABLE_SIGIO
static bool keyboard_bit_set (fd_set *);
#endif
static void deactivate_process (Lisp_Object);
static int status_notify (struct Lisp_Process *, struct Lisp_Process *);
static int read_process_output (Lisp_Object, int);
static void create_pty (Lisp_Object);
static void exec_sentinel (Lisp_Object, Lisp_Object);

/* Number of bits set in connect_wait_mask.  */
static int num_pending_connects;

/* The largest descriptor currently in use; -1 if none.  */
static int max_desc;

/* Set the external socket descriptor for Emacs to use when
   `make-network-process' is called with a non-nil
   `:use-external-socket' option.  The value should be either -1, or
   the file descriptor of a socket that is already bound.  */
static int external_sock_fd;

/* Indexed by descriptor, gives the process (if any) for that descriptor.  */
static Lisp_Object chan_process[FD_SETSIZE];
static void wait_for_socket_fds (Lisp_Object, char const *);

/* Alist of elements (NAME . PROCESS).  */
static Lisp_Object Vprocess_alist;

/* Buffered-ahead input char from process, indexed by channel.
   -1 means empty (no char is buffered).
   Used on sys V where the only way to tell if there is any
   output from the process is to read at least one char.
   Always -1 on systems that support FIONREAD.  */

static int proc_buffered_char[FD_SETSIZE];

/* Table of `struct coding-system' for each process.  */
static struct coding_system *proc_decode_coding_system[FD_SETSIZE];
static struct coding_system *proc_encode_coding_system[FD_SETSIZE];

#ifdef DATAGRAM_SOCKETS
/* Table of `partner address' for datagram sockets.  */
static struct sockaddr_and_len {
  struct sockaddr *sa;
  ptrdiff_t len;
} datagram_address[FD_SETSIZE];
#define DATAGRAM_CHAN_P(chan)	(datagram_address[chan].sa != 0)
#define DATAGRAM_CONN_P(proc)                                           \
  (PROCESSP (proc) &&                                                   \
   XPROCESS (proc)->infd >= 0 &&                                        \
   datagram_address[XPROCESS (proc)->infd].sa != 0)
#else
#define DATAGRAM_CONN_P(proc)	(0)
#endif

/* FOR_EACH_PROCESS (LIST_VAR, PROC_VAR) followed by a statement is
   a `for' loop which iterates over processes from Vprocess_alist.  */

#define FOR_EACH_PROCESS(list_var, proc_var)			\
  FOR_EACH_ALIST_VALUE (Vprocess_alist, list_var, proc_var)

/* These setters are used only in this file, so they can be private.  */
static void
pset_buffer (struct Lisp_Process *p, Lisp_Object val)
{
  p->buffer = val;
}
static void
pset_command (struct Lisp_Process *p, Lisp_Object val)
{
  p->command = val;
}
static void
pset_decode_coding_system (struct Lisp_Process *p, Lisp_Object val)
{
  p->decode_coding_system = val;
}
static void
pset_decoding_buf (struct Lisp_Process *p, Lisp_Object val)
{
  p->decoding_buf = val;
}
static void
pset_encode_coding_system (struct Lisp_Process *p, Lisp_Object val)
{
  p->encode_coding_system = val;
}
static void
pset_encoding_buf (struct Lisp_Process *p, Lisp_Object val)
{
  p->encoding_buf = val;
}
static void
pset_filter (struct Lisp_Process *p, Lisp_Object val)
{
  p->filter = NILP (val) ? Qinternal_default_process_filter : val;
}
static void
pset_log (struct Lisp_Process *p, Lisp_Object val)
{
  p->log = val;
}
static void
pset_mark (struct Lisp_Process *p, Lisp_Object val)
{
  p->mark = val;
}
static void
pset_thread (struct Lisp_Process *p, Lisp_Object val)
{
  p->thread = val;
}
static void
pset_name (struct Lisp_Process *p, Lisp_Object val)
{
  p->name = val;
}
static void
pset_plist (struct Lisp_Process *p, Lisp_Object val)
{
  p->plist = val;
}
static void
pset_sentinel (struct Lisp_Process *p, Lisp_Object val)
{
  p->sentinel = NILP (val) ? Qinternal_default_process_sentinel : val;
}
static void
pset_tty_name (struct Lisp_Process *p, Lisp_Object val)
{
  p->tty_name = val;
}
static void
pset_type (struct Lisp_Process *p, Lisp_Object val)
{
  p->type = val;
}
static void
pset_write_queue (struct Lisp_Process *p, Lisp_Object val)
{
  p->write_queue = val;
}
static void
pset_stderrproc (struct Lisp_Process *p, Lisp_Object val)
{
  p->stderrproc = val;
}

\f
static Lisp_Object
make_lisp_proc (struct Lisp_Process *p)
{
  return make_lisp_ptr (p, Lisp_Vectorlike);
}

enum fd_bits
{
  /* Read from file descriptor.  */
  FOR_READ = 1,
  /* Write to file descriptor.  */
  FOR_WRITE = 2,
  /* This descriptor refers to a keyboard.  Only valid if FOR_READ is
     set.  */
  KEYBOARD_FD = 4,
  /* This descriptor refers to a process.  */
  PROCESS_FD = 8,
  /* A non-blocking connect.  Only valid if FOR_WRITE is set.  */
  NON_BLOCKING_CONNECT_FD = 16
};

static struct fd_callback_data
{
  fd_callback func;
  void *data;
  /* Flags from enum fd_bits.  */
  int flags;
  /* If this fd is locked to a certain thread, this points to it.
     Otherwise, this is NULL.  If an fd is locked to a thread, then
     only that thread is permitted to wait on it.  */
  struct thread_state *thread;
  /* If this fd is currently being selected on by a thread, this
     points to the thread.  Otherwise it is NULL.  */
  struct thread_state *waiting_thread;
} fd_callback_info[FD_SETSIZE];


/* Add a file descriptor FD to be monitored for when read is possible.
   When read is possible, call FUNC with argument DATA.  */

void
add_read_fd (int fd, fd_callback func, void *data)
{
  add_keyboard_wait_descriptor (fd);

  fd_callback_info[fd].func = func;
  fd_callback_info[fd].data = data;
}

static void
add_non_keyboard_read_fd (int fd)
{
  eassert (fd >= 0 && fd < FD_SETSIZE);
  eassert (fd_callback_info[fd].func == NULL);

  fd_callback_info[fd].flags &= ~KEYBOARD_FD;
  fd_callback_info[fd].flags |= FOR_READ;
  if (fd > max_desc)
    max_desc = fd;
}

static void
add_process_read_fd (int fd)
{
  add_non_keyboard_read_fd (fd);
  fd_callback_info[fd].flags |= PROCESS_FD;
}

/* Stop monitoring file descriptor FD for when read is possible.  */

void
delete_read_fd (int fd)
{
  delete_keyboard_wait_descriptor (fd);

  if (fd_callback_info[fd].flags == 0)
    {
      fd_callback_info[fd].func = 0;
      fd_callback_info[fd].data = 0;
    }
}

/* Add a file descriptor FD to be monitored for when write is possible.
   When write is possible, call FUNC with argument DATA.  */

void
add_write_fd (int fd, fd_callback func, void *data)
{
  eassert (fd >= 0 && fd < FD_SETSIZE);

  fd_callback_info[fd].func = func;
  fd_callback_info[fd].data = data;
  fd_callback_info[fd].flags |= FOR_WRITE;
  if (fd > max_desc)
    max_desc = fd;
}

static void
add_non_blocking_write_fd (int fd)
{
  eassert (fd >= 0 && fd < FD_SETSIZE);
  eassert (fd_callback_info[fd].func == NULL);

  fd_callback_info[fd].flags |= FOR_WRITE | NON_BLOCKING_CONNECT_FD;
  if (fd > max_desc)
    max_desc = fd;
  ++num_pending_connects;
}

static void
recompute_max_desc (void)
{
  int fd;

  for (fd = max_desc; fd >= 0; --fd)
    {
      if (fd_callback_info[fd].flags != 0)
	{
	  max_desc = fd;
	  break;
	}
    }
}

/* Stop monitoring file descriptor FD for when write is possible.  */

void
delete_write_fd (int fd)
{
  if ((fd_callback_info[fd].flags & NON_BLOCKING_CONNECT_FD) != 0)
    {
      if (--num_pending_connects < 0)
	emacs_abort ();
    }
  fd_callback_info[fd].flags &= ~(FOR_WRITE | NON_BLOCKING_CONNECT_FD);
  if (fd_callback_info[fd].flags == 0)
    {
      fd_callback_info[fd].func = 0;
      fd_callback_info[fd].data = 0;

      if (fd == max_desc)
	recompute_max_desc ();
    }
}

static void
compute_input_wait_mask (fd_set *mask)
{
  int fd;

  FD_ZERO (mask);
  for (fd = 0; fd <= max_desc; ++fd)
    {
      if (fd_callback_info[fd].thread != NULL
	  && fd_callback_info[fd].thread != current_thread)
	continue;
      if (fd_callback_info[fd].waiting_thread != NULL
	  && fd_callback_info[fd].waiting_thread != current_thread)
	continue;
      if ((fd_callback_info[fd].flags & FOR_READ) != 0)
	{
	  FD_SET (fd, mask);
	  fd_callback_info[fd].waiting_thread = current_thread;
	}
    }
}

static void
compute_non_process_wait_mask (fd_set *mask)
{
  int fd;

  FD_ZERO (mask);
  for (fd = 0; fd <= max_desc; ++fd)
    {
      if (fd_callback_info[fd].thread != NULL
	  && fd_callback_info[fd].thread != current_thread)
	continue;
      if (fd_callback_info[fd].waiting_thread != NULL
	  && fd_callback_info[fd].waiting_thread != current_thread)
	continue;
      if ((fd_callback_info[fd].flags & FOR_READ) != 0
	  && (fd_callback_info[fd].flags & PROCESS_FD) == 0)
	{
	  FD_SET (fd, mask);
	  fd_callback_info[fd].waiting_thread = current_thread;
	}
    }
}

static void
compute_non_keyboard_wait_mask (fd_set *mask)
{
  int fd;

  FD_ZERO (mask);
  for (fd = 0; fd <= max_desc; ++fd)
    {
      if (fd_callback_info[fd].thread != NULL
	  && fd_callback_info[fd].thread != current_thread)
	continue;
      if (fd_callback_info[fd].waiting_thread != NULL
	  && fd_callback_info[fd].waiting_thread != current_thread)
	continue;
      if ((fd_callback_info[fd].flags & FOR_READ) != 0
	  && (fd_callback_info[fd].flags & KEYBOARD_FD) == 0)
	{
	  FD_SET (fd, mask);
	  fd_callback_info[fd].waiting_thread = current_thread;
	}
    }
}

static void
compute_write_mask (fd_set *mask)
{
  int fd;

  FD_ZERO (mask);
  for (fd = 0; fd <= max_desc; ++fd)
    {
      if (fd_callback_info[fd].thread != NULL
	  && fd_callback_info[fd].thread != current_thread)
	continue;
      if (fd_callback_info[fd].waiting_thread != NULL
	  && fd_callback_info[fd].waiting_thread != current_thread)
	continue;
      if ((fd_callback_info[fd].flags & FOR_WRITE) != 0)
	{
	  FD_SET (fd, mask);
	  fd_callback_info[fd].waiting_thread = current_thread;
	}
    }
}

static void
clear_waiting_thread_info (void)
{
  int fd;

  for (fd = 0; fd <= max_desc; ++fd)
    {
      if (fd_callback_info[fd].waiting_thread == current_thread)
	fd_callback_info[fd].waiting_thread = NULL;
    }
}

\f
/* Compute the Lisp form of the process status, p->status, from
   the numeric status that was returned by `wait'.  */

static Lisp_Object status_convert (int);

static void
update_status (struct Lisp_Process *p)
{
  eassert (p->raw_status_new);
  pset_status (p, status_convert (p->raw_status));
  p->raw_status_new = 0;
}

/*  Convert a process status word in Unix format to
    the list that we use internally.  */

static Lisp_Object
status_convert (int w)
{
  if (WIFSTOPPED (w))
    return Fcons (Qstop, Fcons (make_number (WSTOPSIG (w)), Qnil));
  else if (WIFEXITED (w))
    return Fcons (Qexit, Fcons (make_number (WEXITSTATUS (w)),
				WCOREDUMP (w) ? Qt : Qnil));
  else if (WIFSIGNALED (w))
    return Fcons (Qsignal, Fcons (make_number (WTERMSIG (w)),
				  WCOREDUMP (w) ? Qt : Qnil));
  else
    return Qrun;
}

/* True if STATUS is that of a process attempting connection.  */

static bool
connecting_status (Lisp_Object status)
{
  return CONSP (status) && EQ (XCAR (status), Qconnect);
}

/* Given a status-list, extract the three pieces of information
   and store them individually through the three pointers.  */

static void
decode_status (Lisp_Object l, Lisp_Object *symbol, Lisp_Object *code,
	       bool *coredump)
{
  Lisp_Object tem;

  if (connecting_status (l))
    l = XCAR (l);

  if (SYMBOLP (l))
    {
      *symbol = l;
      *code = make_number (0);
      *coredump = 0;
    }
  else
    {
      *symbol = XCAR (l);
      tem = XCDR (l);
      *code = XCAR (tem);
      tem = XCDR (tem);
      *coredump = !NILP (tem);
    }
}

/* Return a string describing a process status list.  */

static Lisp_Object
status_message (struct Lisp_Process *p)
{
  Lisp_Object status = p->status;
  Lisp_Object symbol, code;
  bool coredump;
  Lisp_Object string;

  decode_status (status, &symbol, &code, &coredump);

  if (EQ (symbol, Qsignal) || EQ (symbol, Qstop))
    {
      char const *signame;
      synchronize_system_messages_locale ();
      signame = strsignal (XFASTINT (code));
      if (signame == 0)
	string = build_string ("unknown");
      else
	{
	  int c1, c2;

	  string = build_unibyte_string (signame);
	  if (! NILP (Vlocale_coding_system))
	    string = (code_convert_string_norecord
		      (string, Vlocale_coding_system, 0));
	  c1 = STRING_CHAR (SDATA (string));
	  c2 = downcase (c1);
	  if (c1 != c2)
	    Faset (string, make_number (0), make_number (c2));
	}
      AUTO_STRING (suffix, coredump ? " (core dumped)\n" : "\n");
      return concat2 (string, suffix);
    }
  else if (EQ (symbol, Qexit))
    {
      if (NETCONN1_P (p))
	return build_string (XFASTINT (code) == 0
			     ? "deleted\n"
			     : "connection broken by remote peer\n");
      if (XFASTINT (code) == 0)
	return build_string ("finished\n");
      AUTO_STRING (prefix, "exited abnormally with code ");
      string = Fnumber_to_string (code);
      AUTO_STRING (suffix, coredump ? " (core dumped)\n" : "\n");
      return concat3 (prefix, string, suffix);
    }
  else if (EQ (symbol, Qfailed))
    {
      AUTO_STRING (format, "failed with code %s\n");
      return CALLN (Fformat, format, code);
    }
  else
    return Fcopy_sequence (Fsymbol_name (symbol));
}
\f
enum { PTY_NAME_SIZE = 24 };

/* Open an available pty, returning a file descriptor.
   Store into PTY_NAME the file name of the terminal corresponding to the pty.
   Return -1 on failure.  */

static int
allocate_pty (char pty_name[PTY_NAME_SIZE])
{
#ifdef HAVE_PTYS
  int fd;

#ifdef PTY_ITERATION
  PTY_ITERATION
#else
  register int c, i;
  for (c = FIRST_PTY_LETTER; c <= 'z'; c++)
    for (i = 0; i < 16; i++)
#endif
      {
#ifdef PTY_NAME_SPRINTF
	PTY_NAME_SPRINTF
#else
	sprintf (pty_name, "/dev/pty%c%x", c, i);
#endif /* no PTY_NAME_SPRINTF */

#ifdef PTY_OPEN
	PTY_OPEN;
#else /* no PTY_OPEN */
	fd = emacs_open (pty_name, O_RDWR | O_NONBLOCK, 0);
#endif /* no PTY_OPEN */

	if (fd >= 0)
	  {
#ifdef PTY_TTY_NAME_SPRINTF
	    PTY_TTY_NAME_SPRINTF
#else
	    sprintf (pty_name, "/dev/tty%c%x", c, i);
#endif /* no PTY_TTY_NAME_SPRINTF */

	    /* Set FD's close-on-exec flag.  This is needed even if
	       PT_OPEN calls posix_openpt with O_CLOEXEC, since POSIX
	       doesn't require support for that combination.
	       Do this after PTY_TTY_NAME_SPRINTF, which on some platforms
	       doesn't work if the close-on-exec flag is set (Bug#20555).
	       Multithreaded platforms where posix_openpt ignores
	       O_CLOEXEC (or where PTY_OPEN doesn't call posix_openpt)
	       have a race condition between the PTY_OPEN and here.  */
	    fcntl (fd, F_SETFD, FD_CLOEXEC);

	    /* Check to make certain that both sides are available.
	       This avoids a nasty yet stupid bug in rlogins.  */
	    if (faccessat (AT_FDCWD, pty_name, R_OK | W_OK, AT_EACCESS) != 0)
	      {
		emacs_close (fd);
		continue;
	      }
	    setup_pty (fd);
	    return fd;
	  }
      }
#endif /* HAVE_PTYS */
  return -1;
}

/* Allocate basically initialized process.  */

static struct Lisp_Process *
allocate_process (void)
{
  return ALLOCATE_ZEROED_PSEUDOVECTOR (struct Lisp_Process, pid, PVEC_PROCESS);
}

static Lisp_Object
make_process (Lisp_Object name)
{
  struct Lisp_Process *p = allocate_process ();
  /* Initialize Lisp data.  Note that allocate_process initializes all
     Lisp data to nil, so do it only for slots which should not be nil.  */
  pset_status (p, Qrun);
  pset_mark (p, Fmake_marker ());
  pset_thread (p, Fcurrent_thread ());

  /* Initialize non-Lisp data.  Note that allocate_process zeroes out all
     non-Lisp data, so do it only for slots which should not be zero.  */
  p->infd = -1;
  p->outfd = -1;
  for (int i = 0; i < PROCESS_OPEN_FDS; i++)
    p->open_fd[i] = -1;

#ifdef HAVE_GNUTLS
  verify (GNUTLS_STAGE_EMPTY == 0);
  eassert (p->gnutls_initstage == GNUTLS_STAGE_EMPTY);
  eassert (NILP (p->gnutls_boot_parameters));
#endif

  /* If name is already in use, modify it until it is unused.  */

  Lisp_Object name1 = name;
  for (printmax_t i = 1; ; i++)
    {
      Lisp_Object tem = Fget_process (name1);
      if (NILP (tem))
	break;
      char const suffix_fmt[] = "<%"pMd">";
      char suffix[sizeof suffix_fmt + INT_STRLEN_BOUND (printmax_t)];
      AUTO_STRING_WITH_LEN (lsuffix, suffix, sprintf (suffix, suffix_fmt, i));
      name1 = concat2 (name, lsuffix);
    }
  name = name1;
  pset_name (p, name);
  pset_sentinel (p, Qinternal_default_process_sentinel);
  pset_filter (p, Qinternal_default_process_filter);
  Lisp_Object val;
  XSETPROCESS (val, p);
  Vprocess_alist = Fcons (Fcons (name, val), Vprocess_alist);
  return val;
}

static void
remove_process (register Lisp_Object proc)
{
  register Lisp_Object pair;

  pair = Frassq (proc, Vprocess_alist);
  Vprocess_alist = Fdelq (pair, Vprocess_alist);

  deactivate_process (proc);
}

void
update_processes_for_thread_death (Lisp_Object dying_thread)
{
  Lisp_Object pair;

  for (pair = Vprocess_alist; !NILP (pair); pair = XCDR (pair))
    {
      Lisp_Object process = XCDR (XCAR (pair));
      if (EQ (XPROCESS (process)->thread, dying_thread))
	{
	  struct Lisp_Process *proc = XPROCESS (process);

	  pset_thread (proc, Qnil);
	  if (proc->infd >= 0)
	    fd_callback_info[proc->infd].thread = NULL;
	  if (proc->outfd >= 0)
	    fd_callback_info[proc->outfd].thread = NULL;
	}
    }
}

#ifdef HAVE_GETADDRINFO_A
static void
free_dns_request (Lisp_Object proc)
{
  struct Lisp_Process *p = XPROCESS (proc);

  if (p->dns_request->ar_result)
    freeaddrinfo (p->dns_request->ar_result);
  xfree (p->dns_request);
  p->dns_request = NULL;
}
#endif

\f
DEFUN ("processp", Fprocessp, Sprocessp, 1, 1, 0,
       doc: /* Return t if OBJECT is a process.  */)
  (Lisp_Object object)
{
  return PROCESSP (object) ? Qt : Qnil;
}

DEFUN ("get-process", Fget_process, Sget_process, 1, 1, 0,
       doc: /* Return the process named NAME, or nil if there is none.  */)
  (register Lisp_Object name)
{
  if (PROCESSP (name))
    return name;
  CHECK_STRING (name);
  return Fcdr (Fassoc (name, Vprocess_alist, Qnil));
}

/* This is how commands for the user decode process arguments.  It
   accepts a process, a process name, a buffer, a buffer name, or nil.
   Buffers denote the first process in the buffer, and nil denotes the
   current buffer.  */

static Lisp_Object
get_process (register Lisp_Object name)
{
  register Lisp_Object proc, obj;
  if (STRINGP (name))
    {
      obj = Fget_process (name);
      if (NILP (obj))
	obj = Fget_buffer (name);
      if (NILP (obj))
	error ("Process %s does not exist", SDATA (name));
    }
  else if (NILP (name))
    obj = Fcurrent_buffer ();
  else
    obj = name;

  /* Now obj should be either a buffer object or a process object.  */
  if (BUFFERP (obj))
    {
      if (NILP (BVAR (XBUFFER (obj), name)))
        error ("Attempt to get process for a dead buffer");
      proc = Fget_buffer_process (obj);
      if (NILP (proc))
        error ("Buffer %s has no process", SDATA (BVAR (XBUFFER (obj), name)));
    }
  else
    {
      CHECK_PROCESS (obj);
      proc = obj;
    }
  return proc;
}


/* Fdelete_process promises to immediately forget about the process, but in
   reality, Emacs needs to remember those processes until they have been
   treated by the SIGCHLD handler and waitpid has been invoked on them;
   otherwise they might fill up the kernel's process table.

   Some processes created by call-process are also put onto this list.

   Members of this list are (process-ID . filename) pairs.  The
   process-ID is a number; the filename, if a string, is a file that
   needs to be removed after the process exits.  */
static Lisp_Object deleted_pid_list;

void
record_deleted_pid (pid_t pid, Lisp_Object filename)
{
  deleted_pid_list = Fcons (Fcons (make_fixnum_or_float (pid), filename),
			    /* GC treated elements set to nil.  */
			    Fdelq (Qnil, deleted_pid_list));

}

DEFUN ("delete-process", Fdelete_process, Sdelete_process, 1, 1, 0,
       doc: /* Delete PROCESS: kill it and forget about it immediately.
PROCESS may be a process, a buffer, the name of a process or buffer, or
nil, indicating the current buffer's process.  */)
  (register Lisp_Object process)
{
  register struct Lisp_Process *p;

  process = get_process (process);
  p = XPROCESS (process);

#ifdef HAVE_GETADDRINFO_A
  if (p->dns_request)
    {
      /* Cancel the request.  Unless shutting down, wait until
	 completion.  Free the request if completely canceled. */

      bool canceled = gai_cancel (p->dns_request) != EAI_NOTCANCELED;
      if (!canceled && !inhibit_sentinels)
	{
	  struct gaicb const *req = p->dns_request;
	  while (gai_suspend (&req, 1, NULL) != 0)
	    continue;
	  canceled = true;
	}
      if (canceled)
	free_dns_request (process);
    }
#endif

  p->raw_status_new = 0;
  if (NETCONN1_P (p) || SERIALCONN1_P (p) || PIPECONN1_P (p))
    {
      pset_status (p, list2 (Qexit, make_number (0)));
      p->tick = ++process_tick;
      status_notify (p, NULL);
      redisplay_preserve_echo_area (13);
    }
  else
    {
      if (p->alive)
	record_kill_process (p, Qnil);

      if (p->infd >= 0)
	{
	  /* Update P's status, since record_kill_process will make the
	     SIGCHLD handler update deleted_pid_list, not *P.  */
	  Lisp_Object symbol;
	  if (p->raw_status_new)
	    update_status (p);
	  symbol = CONSP (p->status) ? XCAR (p->status) : p->status;
	  if (! (EQ (symbol, Qsignal) || EQ (symbol, Qexit)))
	    pset_status (p, list2 (Qsignal, make_number (SIGKILL)));

	  p->tick = ++process_tick;
	  status_notify (p, NULL);
	  redisplay_preserve_echo_area (13);
	}
    }
  remove_process (process);
  return Qnil;
}
\f
DEFUN ("process-status", Fprocess_status, Sprocess_status, 1, 1, 0,
       doc: /* Return the status of PROCESS.
The returned value is one of the following symbols:
run  -- for a process that is running.
stop -- for a process stopped but continuable.
exit -- for a process that has exited.
signal -- for a process that has got a fatal signal.
open -- for a network stream connection that is open.
listen -- for a network stream server that is listening.
closed -- for a network stream connection that is closed.
connect -- when waiting for a non-blocking connection to complete.
failed -- when a non-blocking connection has failed.
nil -- if arg is a process name and no such process exists.
PROCESS may be a process, a buffer, the name of a process, or
nil, indicating the current buffer's process.  */)
  (register Lisp_Object process)
{
  register struct Lisp_Process *p;
  register Lisp_Object status;

  if (STRINGP (process))
    process = Fget_process (process);
  else
    process = get_process (process);

  if (NILP (process))
    return process;

  p = XPROCESS (process);
  if (p->raw_status_new)
    update_status (p);
  status = p->status;
  if (CONSP (status))
    status = XCAR (status);
  if (NETCONN1_P (p) || SERIALCONN1_P (p) || PIPECONN1_P (p))
    {
      if (EQ (status, Qexit))
	status = Qclosed;
      else if (EQ (p->command, Qt))
	status = Qstop;
      else if (EQ (status, Qrun))
	status = Qopen;
    }
  return status;
}

DEFUN ("process-exit-status", Fprocess_exit_status, Sprocess_exit_status,
       1, 1, 0,
       doc: /* Return the exit status of PROCESS or the signal number that killed it.
If PROCESS has not yet exited or died, return 0.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  if (XPROCESS (process)->raw_status_new)
    update_status (XPROCESS (process));
  if (CONSP (XPROCESS (process)->status))
    return XCAR (XCDR (XPROCESS (process)->status));
  return make_number (0);
}

DEFUN ("process-id", Fprocess_id, Sprocess_id, 1, 1, 0,
       doc: /* Return the process id of PROCESS.
This is the pid of the external process which PROCESS uses or talks to.
For a network, serial, and pipe connections, this value is nil.  */)
  (register Lisp_Object process)
{
  pid_t pid;

  CHECK_PROCESS (process);
  pid = XPROCESS (process)->pid;
  return (pid ? make_fixnum_or_float (pid) : Qnil);
}

DEFUN ("process-name", Fprocess_name, Sprocess_name, 1, 1, 0,
       doc: /* Return the name of PROCESS, as a string.
This is the name of the program invoked in PROCESS,
possibly modified to make it unique among process names.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->name;
}

DEFUN ("process-command", Fprocess_command, Sprocess_command, 1, 1, 0,
       doc: /* Return the command that was executed to start PROCESS.
This is a list of strings, the first string being the program executed
and the rest of the strings being the arguments given to it.
For a network or serial or pipe connection, this is nil (process is running)
or t (process is stopped).  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->command;
}

DEFUN ("process-tty-name", Fprocess_tty_name, Sprocess_tty_name, 1, 1, 0,
       doc: /* Return the name of the terminal PROCESS uses, or nil if none.
This is the terminal that the process itself reads and writes on,
not the name of the pty that Emacs uses to talk with that terminal.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->tty_name;
}

DEFUN ("set-process-buffer", Fset_process_buffer, Sset_process_buffer,
       2, 2, 0,
       doc: /* Set buffer associated with PROCESS to BUFFER (a buffer, or nil).
Return BUFFER.  */)
  (register Lisp_Object process, Lisp_Object buffer)
{
  struct Lisp_Process *p;

  CHECK_PROCESS (process);
  if (!NILP (buffer))
    CHECK_BUFFER (buffer);
  p = XPROCESS (process);
  pset_buffer (p, buffer);
  if (NETCONN1_P (p) || SERIALCONN1_P (p) || PIPECONN1_P (p))
    pset_childp (p, Fplist_put (p->childp, QCbuffer, buffer));
  setup_process_coding_systems (process);
  return buffer;
}

DEFUN ("process-buffer", Fprocess_buffer, Sprocess_buffer,
       1, 1, 0,
       doc: /* Return the buffer PROCESS is associated with.
The default process filter inserts output from PROCESS into this buffer.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->buffer;
}

DEFUN ("process-mark", Fprocess_mark, Sprocess_mark,
       1, 1, 0,
       doc: /* Return the marker for the end of the last output from PROCESS.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->mark;
}

static void
set_process_filter_masks (struct Lisp_Process *p)
{
  if (EQ (p->filter, Qt) && !EQ (p->status, Qlisten))
    delete_read_fd (p->infd);
  else if (EQ (p->filter, Qt)
	   /* Network or serial process not stopped:  */
	   && !EQ (p->command, Qt))
    add_process_read_fd (p->infd);
}

DEFUN ("set-process-filter", Fset_process_filter, Sset_process_filter,
       2, 2, 0,
       doc: /* Give PROCESS the filter function FILTER; nil means default.
A value of t means stop accepting output from the process.

When a process has a non-default filter, its buffer is not used for output.
Instead, each time it does output, the entire string of output is
passed to the filter.

The filter gets two arguments: the process and the string of output.
The string argument is normally a multibyte string, except:
- if the process's input coding system is no-conversion or raw-text,
  it is a unibyte string (the non-converted input).  */)
  (Lisp_Object process, Lisp_Object filter)
{
  CHECK_PROCESS (process);
  struct Lisp_Process *p = XPROCESS (process);

  /* Don't signal an error if the process's input file descriptor
     is closed.  This could make debugging Lisp more difficult,
     for example when doing something like

     (setq process (start-process ...))
     (debug)
     (set-process-filter process ...)  */

  if (NILP (filter))
    filter = Qinternal_default_process_filter;

  pset_filter (p, filter);

  if (p->infd >= 0)
    set_process_filter_masks (p);

  if (NETCONN1_P (p) || SERIALCONN1_P (p) || PIPECONN1_P (p))
    pset_childp (p, Fplist_put (p->childp, QCfilter, filter));
  setup_process_coding_systems (process);
  return filter;
}

DEFUN ("process-filter", Fprocess_filter, Sprocess_filter,
       1, 1, 0,
       doc: /* Return the filter function of PROCESS.
See `set-process-filter' for more info on filter functions.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->filter;
}

DEFUN ("set-process-sentinel", Fset_process_sentinel, Sset_process_sentinel,
       2, 2, 0,
       doc: /* Give PROCESS the sentinel SENTINEL; nil for default.
The sentinel is called as a function when the process changes state.
It gets two arguments: the process, and a string describing the change.  */)
  (register Lisp_Object process, Lisp_Object sentinel)
{
  struct Lisp_Process *p;

  CHECK_PROCESS (process);
  p = XPROCESS (process);

  if (NILP (sentinel))
    sentinel = Qinternal_default_process_sentinel;

  pset_sentinel (p, sentinel);
  if (NETCONN1_P (p) || SERIALCONN1_P (p) || PIPECONN1_P (p))
    pset_childp (p, Fplist_put (p->childp, QCsentinel, sentinel));
  return sentinel;
}

DEFUN ("process-sentinel", Fprocess_sentinel, Sprocess_sentinel,
       1, 1, 0,
       doc: /* Return the sentinel of PROCESS.
See `set-process-sentinel' for more info on sentinels.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->sentinel;
}

DEFUN ("set-process-thread", Fset_process_thread, Sset_process_thread,
       2, 2, 0,
       doc: /* Set the locking thread of PROCESS to be THREAD.
If THREAD is nil, the process is unlocked.  */)
  (Lisp_Object process, Lisp_Object thread)
{
  struct Lisp_Process *proc;
  struct thread_state *tstate;

  CHECK_PROCESS (process);
  if (NILP (thread))
    tstate = NULL;
  else
    {
      CHECK_THREAD (thread);
      tstate = XTHREAD (thread);
    }

  proc = XPROCESS (process);
  pset_thread (proc, thread);
  if (proc->infd >= 0)
    fd_callback_info[proc->infd].thread = tstate;
  if (proc->outfd >= 0)
    fd_callback_info[proc->outfd].thread = tstate;

  return thread;
}

DEFUN ("process-thread", Fprocess_thread, Sprocess_thread,
       1, 1, 0,
       doc: /* Ret the locking thread of PROCESS.
If PROCESS is unlocked, this function returns nil.  */)
  (Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->thread;
}

DEFUN ("set-process-window-size", Fset_process_window_size,
       Sset_process_window_size, 3, 3, 0,
       doc: /* Tell PROCESS that it has logical window size WIDTH by HEIGHT.
Value is t if PROCESS was successfully told about the window size,
nil otherwise.  */)
  (Lisp_Object process, Lisp_Object height, Lisp_Object width)
{
  CHECK_PROCESS (process);

  /* All known platforms store window sizes as 'unsigned short'.  */
  CHECK_RANGED_INTEGER (height, 0, USHRT_MAX);
  CHECK_RANGED_INTEGER (width, 0, USHRT_MAX);

  if (NETCONN_P (process)
      || XPROCESS (process)->infd < 0
      || (set_window_size (XPROCESS (process)->infd,
			   XINT (height), XINT (width))
	  < 0))
    return Qnil;
  else
    return Qt;
}

DEFUN ("set-process-inherit-coding-system-flag",
       Fset_process_inherit_coding_system_flag,
       Sset_process_inherit_coding_system_flag, 2, 2, 0,
       doc: /* Determine whether buffer of PROCESS will inherit coding-system.
If the second argument FLAG is non-nil, then the variable
`buffer-file-coding-system' of the buffer associated with PROCESS
will be bound to the value of the coding system used to decode
the process output.

This is useful when the coding system specified for the process buffer
leaves either the character code conversion or the end-of-line conversion
unspecified, or if the coding system used to decode the process output
is more appropriate for saving the process buffer.

Binding the variable `inherit-process-coding-system' to non-nil before
starting the process is an alternative way of setting the inherit flag
for the process which will run.

This function returns FLAG.  */)
  (register Lisp_Object process, Lisp_Object flag)
{
  CHECK_PROCESS (process);
  XPROCESS (process)->inherit_coding_system_flag = !NILP (flag);
  return flag;
}

DEFUN ("set-process-query-on-exit-flag",
       Fset_process_query_on_exit_flag, Sset_process_query_on_exit_flag,
       2, 2, 0,
       doc: /* Specify if query is needed for PROCESS when Emacs is exited.
If the second argument FLAG is non-nil, Emacs will query the user before
exiting or killing a buffer if PROCESS is running.  This function
returns FLAG.  */)
  (register Lisp_Object process, Lisp_Object flag)
{
  CHECK_PROCESS (process);
  XPROCESS (process)->kill_without_query = NILP (flag);
  return flag;
}

DEFUN ("process-query-on-exit-flag",
       Fprocess_query_on_exit_flag, Sprocess_query_on_exit_flag,
       1, 1, 0,
       doc: /* Return the current value of query-on-exit flag for PROCESS.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return (XPROCESS (process)->kill_without_query ? Qnil : Qt);
}

DEFUN ("process-contact", Fprocess_contact, Sprocess_contact,
       1, 2, 0,
       doc: /* Return the contact info of PROCESS; t for a real child.
For a network or serial or pipe connection, the value depends on the
optional KEY arg.  If KEY is nil, value is a cons cell of the form
\(HOST SERVICE) for a network connection or (PORT SPEED) for a serial
connection; it is t for a pipe connection.  If KEY is t, the complete
contact information for the connection is returned, else the specific
value for the keyword KEY is returned.  See `make-network-process',
`make-serial-process', or `make-pipe-process' for the list of keywords.
If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed.  */)
  (Lisp_Object process, Lisp_Object key)
{
  Lisp_Object contact;

  CHECK_PROCESS (process);
  contact = XPROCESS (process)->childp;

#ifdef DATAGRAM_SOCKETS

  if (NETCONN_P (process))
    wait_for_socket_fds (process, "process-contact");

  if (DATAGRAM_CONN_P (process)
      && (EQ (key, Qt) || EQ (key, QCremote)))
    contact = Fplist_put (contact, QCremote,
			  Fprocess_datagram_address (process));
#endif

  if ((!NETCONN_P (process) && !SERIALCONN_P (process) && !PIPECONN_P (process))
      || EQ (key, Qt))
    return contact;
  if (NILP (key) && NETCONN_P (process))
    return list2 (Fplist_get (contact, QChost),
		  Fplist_get (contact, QCservice));
  if (NILP (key) && SERIALCONN_P (process))
    return list2 (Fplist_get (contact, QCport),
		  Fplist_get (contact, QCspeed));
  /* FIXME: Return a meaningful value (e.g., the child end of the pipe)
     if the pipe process is useful for purposes other than receiving
     stderr.  */
  if (NILP (key) && PIPECONN_P (process))
    return Qt;
  return Fplist_get (contact, key);
}

DEFUN ("process-plist", Fprocess_plist, Sprocess_plist,
       1, 1, 0,
       doc: /* Return the plist of PROCESS.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return XPROCESS (process)->plist;
}

DEFUN ("set-process-plist", Fset_process_plist, Sset_process_plist,
       2, 2, 0,
       doc: /* Replace the plist of PROCESS with PLIST.  Return PLIST.  */)
  (Lisp_Object process, Lisp_Object plist)
{
  CHECK_PROCESS (process);
  CHECK_LIST (plist);

  pset_plist (XPROCESS (process), plist);
  return plist;
}

#if 0 /* Turned off because we don't currently record this info
	 in the process.  Perhaps add it.  */
DEFUN ("process-connection", Fprocess_connection, Sprocess_connection, 1, 1, 0,
       doc: /* Return the connection type of PROCESS.
The value is nil for a pipe, t or `pty' for a pty, or `stream' for
a socket connection.  */)
  (Lisp_Object process)
{
  return XPROCESS (process)->type;
}
#endif

DEFUN ("process-type", Fprocess_type, Sprocess_type, 1, 1, 0,
       doc: /* Return the connection type of PROCESS.
The value is either the symbol `real', `network', `serial', or `pipe'.
PROCESS may be a process, a buffer, the name of a process or buffer, or
nil, indicating the current buffer's process.  */)
  (Lisp_Object process)
{
  Lisp_Object proc;
  proc = get_process (process);
  return XPROCESS (proc)->type;
}

DEFUN ("format-network-address", Fformat_network_address, Sformat_network_address,
       1, 2, 0,
       doc: /* Convert network ADDRESS from internal format to a string.
A 4 or 5 element vector represents an IPv4 address (with port number).
An 8 or 9 element vector represents an IPv6 address (with port number).
If optional second argument OMIT-PORT is non-nil, don't include a port
number in the string, even when present in ADDRESS.
Return nil if format of ADDRESS is invalid.  */)
  (Lisp_Object address, Lisp_Object omit_port)
{
  if (NILP (address))
    return Qnil;

  if (STRINGP (address))  /* AF_LOCAL */
    return address;

  if (VECTORP (address))  /* AF_INET or AF_INET6 */
    {
      register struct Lisp_Vector *p = XVECTOR (address);
      ptrdiff_t size = p->header.size;
      Lisp_Object args[10];
      int nargs, i;
      char const *format;

      if (size == 4 || (size == 5 && !NILP (omit_port)))
	{
	  format = "%d.%d.%d.%d";
	  nargs = 4;
	}
      else if (size == 5)
	{
	  format = "%d.%d.%d.%d:%d";
	  nargs = 5;
	}
      else if (size == 8 || (size == 9 && !NILP (omit_port)))
	{
	  format = "%x:%x:%x:%x:%x:%x:%x:%x";
	  nargs = 8;
	}
      else if (size == 9)
	{
	  format = "[%x:%x:%x:%x:%x:%x:%x:%x]:%d";
	  nargs = 9;
	}
      else
	return Qnil;

      AUTO_STRING (format_obj, format);
      args[0] = format_obj;

      for (i = 0; i < nargs; i++)
	{
	  if (! RANGED_INTEGERP (0, p->contents[i], 65535))
	    return Qnil;

	  if (nargs <= 5         /* IPv4 */
	      && i < 4           /* host, not port */
	      && XINT (p->contents[i]) > 255)
	    return Qnil;

	  args[i + 1] = p->contents[i];
	}

      return Fformat (nargs + 1, args);
    }

  if (CONSP (address))
    {
      AUTO_STRING (format, "<Family %d>");
      return CALLN (Fformat, format, Fcar (address));
    }

  return Qnil;
}

DEFUN ("process-list", Fprocess_list, Sprocess_list, 0, 0, 0,
       doc: /* Return a list of all processes that are Emacs sub-processes.  */)
  (void)
{
  return Fmapcar (Qcdr, Vprocess_alist);
}
\f
/* Starting asynchronous inferior processes.  */

DEFUN ("make-process", Fmake_process, Smake_process, 0, MANY, 0,
       doc: /* Start a program in a subprocess.  Return the process object for it.

This is similar to `start-process', but arguments are specified as
keyword/argument pairs.  The following arguments are defined:

:name NAME -- NAME is name for process.  It is modified if necessary
to make it unique.

:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
with the process.  Process output goes at end of that buffer, unless
you specify a filter function to handle the output.  BUFFER may be
also nil, meaning that this process is not associated with any buffer.

:command COMMAND -- COMMAND is a list starting with the program file
name, followed by strings to give to the program as arguments.

:coding CODING -- If CODING is a symbol, it specifies the coding
system used for both reading and writing for this process.  If CODING
is a cons (DECODING . ENCODING), DECODING is used for reading, and
ENCODING is used for writing.

:noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
the process is running.  If BOOL is not given, query before exiting.

:stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
In the stopped state, a process does not accept incoming data, but you
can send outgoing data.  The stopped state is cleared by
`continue-process' and set by `stop-process'.

:connection-type TYPE -- TYPE is control type of device used to
communicate with subprocesses.  Values are `pipe' to use a pipe, `pty'
to use a pty, or nil to use the default specified through
`process-connection-type'.

:filter FILTER -- Install FILTER as the process filter.

:sentinel SENTINEL -- Install SENTINEL as the process sentinel.

:stderr STDERR -- STDERR is either a buffer or a pipe process attached
to the standard error of subprocess.  Specifying this implies
`:connection-type' is set to `pipe'.

usage: (make-process &rest ARGS)  */)
  (ptrdiff_t nargs, Lisp_Object *args)
{
  Lisp_Object buffer, name, command, program, proc, contact, current_dir, tem;
  Lisp_Object xstderr, stderrproc;
  ptrdiff_t count = SPECPDL_INDEX ();

  if (nargs == 0)
    return Qnil;

  /* Save arguments for process-contact and clone-process.  */
  contact = Flist (nargs, args);

  buffer = Fplist_get (contact, QCbuffer);
  if (!NILP (buffer))
    buffer = Fget_buffer_create (buffer);

  /* Make sure that the child will be able to chdir to the current
     buffer's current directory, or its unhandled equivalent.  We
     can't just have the child check for an error when it does the
     chdir, since it's in a vfork.  */
  current_dir = encode_current_directory ();

  name = Fplist_get (contact, QCname);
  CHECK_STRING (name);

  command = Fplist_get (contact, QCcommand);
  if (CONSP (command))
    program = XCAR (command);
  else
    program = Qnil;

  if (!NILP (program))
    CHECK_STRING (program);

  bool query_on_exit = NILP (Fplist_get (contact, QCnoquery));

  stderrproc = Qnil;
  xstderr = Fplist_get (contact, QCstderr);
  if (PROCESSP (xstderr))
    {
      if (!PIPECONN_P (xstderr))
	error ("Process is not a pipe process");
      stderrproc = xstderr;
    }
  else if (!NILP (xstderr))
    {
      CHECK_STRING (program);
      stderrproc = CALLN (Fmake_pipe_process,
			  QCname,
			  concat2 (name, build_string (" stderr")),
			  QCbuffer,
			  Fget_buffer_create (xstderr),
			  QCnoquery,
			  query_on_exit ? Qnil : Qt);
    }

  proc = make_process (name);
  record_unwind_protect (start_process_unwind, proc);

  pset_childp (XPROCESS (proc), Qt);
  eassert (NILP (XPROCESS (proc)->plist));
  pset_type (XPROCESS (proc), Qreal);
  pset_buffer (XPROCESS (proc), buffer);
  pset_sentinel (XPROCESS (proc), Fplist_get (contact, QCsentinel));
  pset_filter (XPROCESS (proc), Fplist_get (contact, QCfilter));
  pset_command (XPROCESS (proc), Fcopy_sequence (command));

  if (!query_on_exit)
    XPROCESS (proc)->kill_without_query = 1;
  if (tem = Fplist_get (contact, QCstop), !NILP (tem))
    pset_command (XPROCESS (proc), Qt);

  tem = Fplist_get (contact, QCconnection_type);
  if (EQ (tem, Qpty))
    XPROCESS (proc)->pty_flag = true;
  else if (EQ (tem, Qpipe))
    XPROCESS (proc)->pty_flag = false;
  else if (NILP (tem))
    XPROCESS (proc)->pty_flag = !NILP (Vprocess_connection_type);
  else
    report_file_error ("Unknown connection type", tem);

  if (!NILP (stderrproc))
    {
      pset_stderrproc (XPROCESS (proc), stderrproc);

      XPROCESS (proc)->pty_flag = false;
    }

#ifdef HAVE_GNUTLS
  /* AKA GNUTLS_INITSTAGE(proc).  */
  verify (GNUTLS_STAGE_EMPTY == 0);
  eassert (XPROCESS (proc)->gnutls_initstage == GNUTLS_STAGE_EMPTY);
  eassert (NILP (XPROCESS (proc)->gnutls_cred_type));
#endif

  XPROCESS (proc)->adaptive_read_buffering
    = (NILP (Vprocess_adaptive_read_buffering) ? 0
       : EQ (Vprocess_adaptive_read_buffering, Qt) ? 1 : 2);

  /* Make the process marker point into the process buffer (if any).  */
  if (BUFFERP (buffer))
    set_marker_both (XPROCESS (proc)->mark, buffer,
		     BUF_ZV (XBUFFER (buffer)),
		     BUF_ZV_BYTE (XBUFFER (buffer)));

  USE_SAFE_ALLOCA;

  {
    /* Decide coding systems for communicating with the process.  Here
       we don't setup the structure coding_system nor pay attention to
       unibyte mode.  They are done in create_process.  */

    /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
    Lisp_Object coding_systems = Qt;
    Lisp_Object val, *args2;

    tem = Fplist_get (contact, QCcoding);
    if (!NILP (tem))
      {
	val = tem;
	if (CONSP (val))
	  val = XCAR (val);
      }
    else
      val = Vcoding_system_for_read;
    if (NILP (val))
      {
	ptrdiff_t nargs2 = 3 + XINT (Flength (command));
	Lisp_Object tem2;
	SAFE_ALLOCA_LISP (args2, nargs2);
	ptrdiff_t i = 0;
	args2[i++] = Qstart_process;
	args2[i++] = name;
	args2[i++] = buffer;
	for (tem2 = command; CONSP (tem2); tem2 = XCDR (tem2))
	  args2[i++] = XCAR (tem2);
	if (!NILP (program))
	  coding_systems = Ffind_operation_coding_system (nargs2, args2);
	if (CONSP (coding_systems))
	  val = XCAR (coding_systems);
	else if (CONSP (Vdefault_process_coding_system))
	  val = XCAR (Vdefault_process_coding_system);
      }
    pset_decode_coding_system (XPROCESS (proc), val);

    if (!NILP (tem))
      {
	val = tem;
	if (CONSP (val))
	  val = XCDR (val);
      }
    else
      val = Vcoding_system_for_write;
    if (NILP (val))
      {
	if (EQ (coding_systems, Qt))
	  {
	    ptrdiff_t nargs2 = 3 + XINT (Flength (command));
	    Lisp_Object tem2;
	    SAFE_ALLOCA_LISP (args2, nargs2);
	    ptrdiff_t i = 0;
	    args2[i++] = Qstart_process;
	    args2[i++] = name;
	    args2[i++] = buffer;
	    for (tem2 = command; CONSP (tem2); tem2 = XCDR (tem2))
	      args2[i++] = XCAR (tem2);
	    if (!NILP (program))
	      coding_systems = Ffind_operation_coding_system (nargs2, args2);
	  }
	if (CONSP (coding_systems))
	  val = XCDR (coding_systems);
	else if (CONSP (Vdefault_process_coding_system))
	  val = XCDR (Vdefault_process_coding_system);
      }
    pset_encode_coding_system (XPROCESS (proc), val);
    /* Note: At this moment, the above coding system may leave
       text-conversion or eol-conversion unspecified.  They will be
       decided after we read output from the process and decode it by
       some coding system, or just before we actually send a text to
       the process.  */
  }


  pset_decoding_buf (XPROCESS (proc), empty_unibyte_string);
  eassert (XPROCESS (proc)->decoding_carryover == 0);
  pset_encoding_buf (XPROCESS (proc), empty_unibyte_string);

  XPROCESS (proc)->inherit_coding_system_flag
    = !(NILP (buffer) || !inherit_process_coding_system);

  if (!NILP (program))
    {
      Lisp_Object program_args = XCDR (command);

      /* If program file name is not absolute, search our path for it.
	 Put the name we will really use in TEM.  */
      if (!IS_DIRECTORY_SEP (SREF (program, 0))
	  && !(SCHARS (program) > 1
	       && IS_DEVICE_SEP (SREF (program, 1))))
	{
	  tem = Qnil;
	  openp (Vexec_path, program, Vexec_suffixes, &tem,
		 make_number (X_OK), false);
	  if (NILP (tem))
	    report_file_error ("Searching for program", program);
	  tem = Fexpand_file_name (tem, Qnil);
	}
      else
	{
	  if (!NILP (Ffile_directory_p (program)))
	    error ("Specified program for new process is a directory");
	  tem = program;
	}

      /* Remove "/:" from TEM.  */
      tem = remove_slash_colon (tem);

      Lisp_Object arg_encoding = Qnil;

      /* Encode the file name and put it in NEW_ARGV.
	 That's where the child will use it to execute the program.  */
      tem = list1 (ENCODE_FILE (tem));
      ptrdiff_t new_argc = 1;

      /* Here we encode arguments by the coding system used for sending
	 data to the process.  We don't support using different coding
	 systems for encoding arguments and for encoding data sent to the
	 process.  */

      for (Lisp_Object tem2 = program_args; CONSP (tem2); tem2 = XCDR (tem2))
	{
	  Lisp_Object arg = XCAR (tem2);
	  CHECK_STRING (arg);
	  if (STRING_MULTIBYTE (arg))
	    {
	      if (NILP (arg_encoding))
		arg_encoding = (complement_process_encoding_system
				(XPROCESS (proc)->encode_coding_system));
	      arg = code_convert_string_norecord (arg, arg_encoding, 1);
	    }
	  tem = Fcons (arg, tem);
	  new_argc++;
	}

      /* Now that everything is encoded we can collect the strings into
	 NEW_ARGV.  */
      char **new_argv;
      SAFE_NALLOCA (new_argv, 1, new_argc + 1);
      new_argv[new_argc] = 0;

      for (ptrdiff_t i = new_argc - 1; i >= 0; i--)
	{
	  new_argv[i] = SSDATA (XCAR (tem));
	  tem = XCDR (tem);
	}

      create_process (proc, new_argv, current_dir);
    }
  else
    create_pty (proc);

  SAFE_FREE ();
  return unbind_to (count, proc);
}

/* If PROC doesn't have its pid set, then an error was signaled and
   the process wasn't started successfully, so remove it.  */
static void
start_process_unwind (Lisp_Object proc)
{
  if (XPROCESS (proc)->pid <= 0 && XPROCESS (proc)->pid != -2)
    remove_process (proc);
}

/* If *FD_ADDR is nonnegative, close it, and mark it as closed.  */

static void
close_process_fd (int *fd_addr)
{
  int fd = *fd_addr;
  if (0 <= fd)
    {
      *fd_addr = -1;
      emacs_close (fd);
    }
}

/* Indexes of file descriptors in open_fds.  */
enum
  {
    /* The pipe from Emacs to its subprocess.  */
    SUBPROCESS_STDIN,
    WRITE_TO_SUBPROCESS,

    /* The main pipe from the subprocess to Emacs.  */
    READ_FROM_SUBPROCESS,
    SUBPROCESS_STDOUT,

    /* The pipe from the subprocess to Emacs that is closed when the
       subprocess execs.  */
    READ_FROM_EXEC_MONITOR,
    EXEC_MONITOR_OUTPUT
  };

verify (PROCESS_OPEN_FDS == EXEC_MONITOR_OUTPUT + 1);

static void
create_process (Lisp_Object process, char **new_argv, Lisp_Object current_dir)
{
  struct Lisp_Process *p = XPROCESS (process);
  int inchannel, outchannel;
  pid_t pid;
  int vfork_errno;
  int forkin, forkout, forkerr = -1;
  bool pty_flag = 0;
  char pty_name[PTY_NAME_SIZE];
  Lisp_Object lisp_pty_name = Qnil;
  sigset_t oldset;

  inchannel = outchannel = -1;

  if (p->pty_flag)
    outchannel = inchannel = allocate_pty (pty_name);

  if (inchannel >= 0)
    {
      p->open_fd[READ_FROM_SUBPROCESS] = inchannel;
#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
      /* On most USG systems it does not work to open the pty's tty here,
	 then close it and reopen it in the child.  */
      /* Don't let this terminal become our controlling terminal
	 (in case we don't have one).  */
      forkout = forkin = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
      if (forkin < 0)
	report_file_error ("Opening pty", Qnil);
      p->open_fd[SUBPROCESS_STDIN] = forkin;
#else
      forkin = forkout = -1;
#endif /* not USG, or USG_SUBTTY_WORKS */
      pty_flag = 1;
      lisp_pty_name = build_string (pty_name);
    }
  else
    {
      if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0
	  || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0)
	report_file_error ("Creating pipe", Qnil);
      forkin = p->open_fd[SUBPROCESS_STDIN];
      outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
      inchannel = p->open_fd[READ_FROM_SUBPROCESS];
      forkout = p->open_fd[SUBPROCESS_STDOUT];

      if (!NILP (p->stderrproc))
	{
	  struct Lisp_Process *pp = XPROCESS (p->stderrproc);

	  forkerr = pp->open_fd[SUBPROCESS_STDOUT];

	  /* Close unnecessary file descriptors.  */
	  close_process_fd (&pp->open_fd[WRITE_TO_SUBPROCESS]);
	  close_process_fd (&pp->open_fd[SUBPROCESS_STDIN]);
	}
    }

#ifndef WINDOWSNT
  if (emacs_pipe (p->open_fd + READ_FROM_EXEC_MONITOR) != 0)
    report_file_error ("Creating pipe", Qnil);
#endif

  fcntl (inchannel, F_SETFL, O_NONBLOCK);
  fcntl (outchannel, F_SETFL, O_NONBLOCK);

  /* Record this as an active process, with its channels.  */
  chan_process[inchannel] = process;
  p->infd = inchannel;
  p->outfd = outchannel;

  /* Previously we recorded the tty descriptor used in the subprocess.
     It was only used for getting the foreground tty process, so now
     we just reopen the device (see emacs_get_tty_pgrp) as this is
     more portable (see USG_SUBTTY_WORKS above).  */

  p->pty_flag = pty_flag;
  pset_status (p, Qrun);

  if (!EQ (p->command, Qt))
    add_process_read_fd (inchannel);

  /* This may signal an error.  */
  setup_process_coding_systems (process);

  block_input ();
  block_child_signal (&oldset);

#ifndef WINDOWSNT
  /* vfork, and prevent local vars from being clobbered by the vfork.  */
  Lisp_Object volatile current_dir_volatile = current_dir;
  Lisp_Object volatile lisp_pty_name_volatile = lisp_pty_name;
  char **volatile new_argv_volatile = new_argv;
  int volatile forkin_volatile = forkin;
  int volatile forkout_volatile = forkout;
  int volatile forkerr_volatile = forkerr;
  struct Lisp_Process *p_volatile = p;

#ifdef DARWIN_OS
  /* Darwin doesn't let us run setsid after a vfork, so use fork when
     necessary.  Also, reset SIGCHLD handling after a vfork, as
     apparently macOS can mistakenly deliver SIGCHLD to the child.  */
  if (pty_flag)
    pid = fork ();
  else
    {
      pid = vfork ();
      if (pid == 0)
	signal (SIGCHLD, SIG_DFL);
    }
#else
  pid = vfork ();
#endif

  current_dir = current_dir_volatile;
  lisp_pty_name = lisp_pty_name_volatile;
  new_argv = new_argv_volatile;
  forkin = forkin_volatile;
  forkout = forkout_volatile;
  forkerr = forkerr_volatile;
  p = p_volatile;

  pty_flag = p->pty_flag;

  if (pid == 0)
#endif /* not WINDOWSNT */
    {
      /* Make the pty be the controlling terminal of the process.  */
#ifdef HAVE_PTYS
      /* First, disconnect its current controlling terminal.
	 Do this even if !PTY_FLAG; see Bug#30762.  */
      setsid ();
      /* Make the pty's terminal the controlling terminal.  */
      if (pty_flag && forkin >= 0)
	{
#ifdef TIOCSCTTY
	  /* We ignore the return value
	     because faith@cs.unc.edu says that is necessary on Linux.  */
	  ioctl (forkin, TIOCSCTTY, 0);
#endif
	}
#if defined (LDISC1)
      if (pty_flag && forkin >= 0)
	{
	  struct termios t;
	  tcgetattr (forkin, &t);
	  t.c_lflag = LDISC1;
	  if (tcsetattr (forkin, TCSANOW, &t) < 0)
	    emacs_perror ("create_process/tcsetattr LDISC1");
	}
#else
#if defined (NTTYDISC) && defined (TIOCSETD)
      if (pty_flag && forkin >= 0)
	{
	  /* Use new line discipline.  */
	  int ldisc = NTTYDISC;
	  ioctl (forkin, TIOCSETD, &ldisc);
	}
#endif
#endif
#ifdef TIOCNOTTY
      /* In 4.3BSD, the TIOCSPGRP bug has been fixed, and now you
	 can do TIOCSPGRP only to the process's controlling tty.  */
      if (pty_flag)
	{
	  /* I wonder: would just ioctl (0, TIOCNOTTY, 0) work here?
	     I can't test it since I don't have 4.3.  */
	  int j = emacs_open (DEV_TTY, O_RDWR, 0);
	  if (j >= 0)
	    {
	      ioctl (j, TIOCNOTTY, 0);
	      emacs_close (j);
	    }
	}
#endif /* TIOCNOTTY */

#if !defined (DONT_REOPEN_PTY)
/*** There is a suggestion that this ought to be a
     conditional on TIOCSPGRP, or !defined TIOCSCTTY.
     Trying the latter gave the wrong results on Debian GNU/Linux 1.1;
     that system does seem to need this code, even though
     both TIOCSCTTY is defined.  */
	/* Now close the pty (if we had it open) and reopen it.
	   This makes the pty the controlling terminal of the subprocess.  */
      if (pty_flag)
	{

	  /* I wonder if emacs_close (emacs_open (SSDATA (lisp_pty_name), ...))
	     would work?  */
	  if (forkin >= 0)
	    emacs_close (forkin);
	  forkout = forkin = emacs_open (SSDATA (lisp_pty_name), O_RDWR, 0);

	  if (forkin < 0)
	    {
	      emacs_perror (SSDATA (lisp_pty_name));
	      _exit (EXIT_CANCELED);
	    }

	}
#endif /* not DONT_REOPEN_PTY */

#ifdef SETUP_SLAVE_PTY
      if (pty_flag)
	{
	  SETUP_SLAVE_PTY;
	}
#endif /* SETUP_SLAVE_PTY */
#endif /* HAVE_PTYS */

      signal (SIGINT, SIG_DFL);
      signal (SIGQUIT, SIG_DFL);
#ifdef SIGPROF
      signal (SIGPROF, SIG_DFL);
#endif

      /* Emacs ignores SIGPIPE, but the child should not.  */
      signal (SIGPIPE, SIG_DFL);

      /* Stop blocking SIGCHLD in the child.  */
      unblock_child_signal (&oldset);

      if (pty_flag)
	child_setup_tty (forkout);

      if (forkerr < 0)
	forkerr = forkout;
#ifdef WINDOWSNT
      pid = child_setup (forkin, forkout, forkerr, new_argv, 1, current_dir);
#else  /* not WINDOWSNT */
      child_setup (forkin, forkout, forkerr, new_argv, 1, current_dir);
#endif /* not WINDOWSNT */
    }

  /* Back in the parent process.  */

  vfork_errno = errno;
  p->pid = pid;
  if (pid >= 0)
    p->alive = 1;

  /* Stop blocking in the parent.  */
  unblock_child_signal (&oldset);
  unblock_input ();

  if (pid < 0)
    report_file_errno ("Doing vfork", Qnil, vfork_errno);
  else
    {
      /* vfork succeeded.  */

      /* Close the pipe ends that the child uses, or the child's pty.  */
      close_process_fd (&p->open_fd[SUBPROCESS_STDIN]);
      close_process_fd (&p->open_fd[SUBPROCESS_STDOUT]);

#ifdef WINDOWSNT
      register_child (pid, inchannel);
#endif /* WINDOWSNT */

      pset_tty_name (p, lisp_pty_name);

#ifndef WINDOWSNT
      /* Wait for child_setup to complete in case that vfork is
	 actually defined as fork.  The descriptor
	 XPROCESS (proc)->open_fd[EXEC_MONITOR_OUTPUT]
	 of a pipe is closed at the child side either by close-on-exec
	 on successful execve or the _exit call in child_setup.  */
      {
	char dummy;

	close_process_fd (&p->open_fd[EXEC_MONITOR_OUTPUT]);
	emacs_read (p->open_fd[READ_FROM_EXEC_MONITOR], &dummy, 1);
	close_process_fd (&p->open_fd[READ_FROM_EXEC_MONITOR]);
      }
#endif
      if (!NILP (p->stderrproc))
	{
	  struct Lisp_Process *pp = XPROCESS (p->stderrproc);
	  close_process_fd (&pp->open_fd[SUBPROCESS_STDOUT]);
	}
    }
}

static void
create_pty (Lisp_Object process)
{
  struct Lisp_Process *p = XPROCESS (process);
  char pty_name[PTY_NAME_SIZE];
  int pty_fd = !p->pty_flag ? -1 : allocate_pty (pty_name);

  if (pty_fd >= 0)
    {
      p->open_fd[SUBPROCESS_STDIN] = pty_fd;
#if ! defined (USG) || defined (USG_SUBTTY_WORKS)
      /* On most USG systems it does not work to open the pty's tty here,
	 then close it and reopen it in the child.  */
      /* Don't let this terminal become our controlling terminal
	 (in case we don't have one).  */
      int forkout = emacs_open (pty_name, O_RDWR | O_NOCTTY, 0);
      if (forkout < 0)
	report_file_error ("Opening pty", Qnil);
      p->open_fd[WRITE_TO_SUBPROCESS] = forkout;
#if defined (DONT_REOPEN_PTY)
      /* In the case that vfork is defined as fork, the parent process
	 (Emacs) may send some data before the child process completes
	 tty options setup.  So we setup tty before forking.  */
      child_setup_tty (forkout);
#endif /* DONT_REOPEN_PTY */
#endif /* not USG, or USG_SUBTTY_WORKS */

      fcntl (pty_fd, F_SETFL, O_NONBLOCK);

      /* Record this as an active process, with its channels.
	 As a result, child_setup will close Emacs's side of the pipes.  */
      chan_process[pty_fd] = process;
      p->infd = pty_fd;
      p->outfd = pty_fd;

      /* Previously we recorded the tty descriptor used in the subprocess.
	 It was only used for getting the foreground tty process, so now
	 we just reopen the device (see emacs_get_tty_pgrp) as this is
	 more portable (see USG_SUBTTY_WORKS above).  */

      p->pty_flag = 1;
      pset_status (p, Qrun);
      setup_process_coding_systems (process);

      add_process_read_fd (pty_fd);

      pset_tty_name (p, build_string (pty_name));
    }

  p->pid = -2;
}

DEFUN ("make-pipe-process", Fmake_pipe_process, Smake_pipe_process,
       0, MANY, 0,
       doc: /* Create and return a bidirectional pipe process.

In Emacs, pipes are represented by process objects, so input and
output work as for subprocesses, and `delete-process' closes a pipe.
However, a pipe process has no process id, it cannot be signaled,
and the status codes are different from normal processes.

Arguments are specified as keyword/argument pairs.  The following
arguments are defined:

:name NAME -- NAME is the name of the process.  It is modified if necessary to make it unique.

:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
with the process.  Process output goes at the end of that buffer,
unless you specify a filter function to handle the output.  If BUFFER
is not given, the value of NAME is used.

:coding CODING -- If CODING is a symbol, it specifies the coding
system used for both reading and writing for this process.  If CODING
is a cons (DECODING . ENCODING), DECODING is used for reading, and
ENCODING is used for writing.

:noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
the process is running.  If BOOL is not given, query before exiting.

:stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
In the stopped state, a pipe process does not accept incoming data,
but you can send outgoing data.  The stopped state is cleared by
`continue-process' and set by `stop-process'.

:filter FILTER -- Install FILTER as the process filter.

:sentinel SENTINEL -- Install SENTINEL as the process sentinel.

usage:  (make-pipe-process &rest ARGS)  */)
  (ptrdiff_t nargs, Lisp_Object *args)
{
  Lisp_Object proc, contact;
  struct Lisp_Process *p;
  Lisp_Object name, buffer;
  Lisp_Object tem;
  ptrdiff_t specpdl_count;
  int inchannel, outchannel;

  if (nargs == 0)
    return Qnil;

  contact = Flist (nargs, args);

  name = Fplist_get (contact, QCname);
  CHECK_STRING (name);
  proc = make_process (name);
  specpdl_count = SPECPDL_INDEX ();
  record_unwind_protect (remove_process, proc);
  p = XPROCESS (proc);

  if (emacs_pipe (p->open_fd + SUBPROCESS_STDIN) != 0
      || emacs_pipe (p->open_fd + READ_FROM_SUBPROCESS) != 0)
    report_file_error ("Creating pipe", Qnil);
  outchannel = p->open_fd[WRITE_TO_SUBPROCESS];
  inchannel = p->open_fd[READ_FROM_SUBPROCESS];

  fcntl (inchannel, F_SETFL, O_NONBLOCK);
  fcntl (outchannel, F_SETFL, O_NONBLOCK);

#ifdef WINDOWSNT
  register_aux_fd (inchannel);
#endif

  /* Record this as an active process, with its channels.  */
  chan_process[inchannel] = proc;
  p->infd = inchannel;
  p->outfd = outchannel;

  if (inchannel > max_desc)
    max_desc = inchannel;

  buffer = Fplist_get (contact, QCbuffer);
  if (NILP (buffer))
    buffer = name;
  buffer = Fget_buffer_create (buffer);
  pset_buffer (p, buffer);

  pset_childp (p, contact);
  pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist)));
  pset_type (p, Qpipe);
  pset_sentinel (p, Fplist_get (contact, QCsentinel));
  pset_filter (p, Fplist_get (contact, QCfilter));
  eassert (NILP (p->log));
  if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
    p->kill_without_query = 1;
  if (tem = Fplist_get (contact, QCstop), !NILP (tem))
    pset_command (p, Qt);
  eassert (! p->pty_flag);

  if (!EQ (p->command, Qt))
    add_process_read_fd (inchannel);
  p->adaptive_read_buffering
    = (NILP (Vprocess_adaptive_read_buffering) ? 0
       : EQ (Vprocess_adaptive_read_buffering, Qt) ? 1 : 2);

  /* Make the process marker point into the process buffer (if any).  */
  if (BUFFERP (buffer))
    set_marker_both (p->mark, buffer,
		     BUF_ZV (XBUFFER (buffer)),
		     BUF_ZV_BYTE (XBUFFER (buffer)));

  {
    /* Setup coding systems for communicating with the network stream.  */

    /* Qt denotes we have not yet called Ffind_operation_coding_system.  */
    Lisp_Object coding_systems = Qt;
    Lisp_Object val;

    tem = Fplist_get (contact, QCcoding);
    val = Qnil;
    if (!NILP (tem))
      {
	val = tem;
	if (CONSP (val))
	  val = XCAR (val);
      }
    else if (!NILP (Vcoding_system_for_read))
      val = Vcoding_system_for_read;
    else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
	     || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
      /* We dare not decode end-of-line format by setting VAL to
	 Qraw_text, because the existing Emacs Lisp libraries
	 assume that they receive bare code including a sequence of
	 CR LF.  */
      val = Qnil;
    else
      {
	if (CONSP (coding_systems))
	  val = XCAR (coding_systems);
	else if (CONSP (Vdefault_process_coding_system))
	  val = XCAR (Vdefault_process_coding_system);
	else
	  val = Qnil;
      }
    pset_decode_coding_system (p, val);

    if (!NILP (tem))
      {
	val = tem;
	if (CONSP (val))
	  val = XCDR (val);
      }
    else if (!NILP (Vcoding_system_for_write))
      val = Vcoding_system_for_write;
    else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
      val = Qnil;
    else
      {
	if (CONSP (coding_systems))
	  val = XCDR (coding_systems);
	else if (CONSP (Vdefault_process_coding_system))
	  val = XCDR (Vdefault_process_coding_system);
	else
	  val = Qnil;
      }
    pset_encode_coding_system (p, val);
  }
  /* This may signal an error.  */
  setup_process_coding_systems (proc);

  specpdl_ptr = specpdl + specpdl_count;

  return proc;
}

\f
/* Convert an internal struct sockaddr to a lisp object (vector or string).
   The address family of sa is not included in the result.  */

Lisp_Object
conv_sockaddr_to_lisp (struct sockaddr *sa, ptrdiff_t len)
{
  Lisp_Object address;
  ptrdiff_t i;
  unsigned char *cp;
  struct Lisp_Vector *p;

  /* Workaround for a bug in getsockname on BSD: Names bound to
     sockets in the UNIX domain are inaccessible; getsockname returns
     a zero length name.  */
  if (len < offsetof (struct sockaddr, sa_family) + sizeof (sa->sa_family))
    return empty_unibyte_string;

  switch (sa->sa_family)
    {
    case AF_INET:
      {
	DECLARE_POINTER_ALIAS (sin, struct sockaddr_in, sa);
	len = sizeof (sin->sin_addr) + 1;
	address = Fmake_vector (make_number (len), Qnil);
	p = XVECTOR (address);
	p->contents[--len] = make_number (ntohs (sin->sin_port));
	cp = (unsigned char *) &sin->sin_addr;
	break;
      }
#ifdef AF_INET6
    case AF_INET6:
      {
	DECLARE_POINTER_ALIAS (sin6, struct sockaddr_in6, sa);
	DECLARE_POINTER_ALIAS (ip6, uint16_t, &sin6->sin6_addr);
	len = sizeof (sin6->sin6_addr) / 2 + 1;
	address = Fmake_vector (make_number (len), Qnil);
	p = XVECTOR (address);
	p->contents[--len] = make_number (ntohs (sin6->sin6_port));
	for (i = 0; i < len; i++)
	  p->contents[i] = make_number (ntohs (ip6[i]));
	return address;
      }
#endif
#ifdef HAVE_LOCAL_SOCKETS
    case AF_LOCAL:
      {
	DECLARE_POINTER_ALIAS (sockun, struct sockaddr_un, sa);
        ptrdiff_t name_length = len - offsetof (struct sockaddr_un, sun_path);
        /* If the first byte is NUL, the name is a Linux abstract
           socket name, and the name can contain embedded NULs.  If
           it's not, we have a NUL-terminated string.  Be careful not
           to walk past the end of the object looking for the name
           terminator, however.  */
        if (name_length > 0 && sockun->sun_path[0] != '\0')
          {
            const char *terminator
	      = memchr (sockun->sun_path, '\0', name_length);

            if (terminator)
              name_length = terminator - (const char *) sockun->sun_path;
          }

	return make_unibyte_string (sockun->sun_path, name_length);
      }
#endif
    default:
      len -= offsetof (struct sockaddr, sa_family) + sizeof (sa->sa_family);
      address = Fcons (make_number (sa->sa_family),
		       Fmake_vector (make_number (len), Qnil));
      p = XVECTOR (XCDR (address));
      cp = (unsigned char *) &sa->sa_family + sizeof (sa->sa_family);
      break;
    }

  i = 0;
  while (i < len)
    p->contents[i++] = make_number (*cp++);

  return address;
}

/* Convert an internal struct addrinfo to a Lisp object.  */

static Lisp_Object
conv_addrinfo_to_lisp (struct addrinfo *res)
{
  Lisp_Object protocol = make_number (res->ai_protocol);
  eassert (XINT (protocol) == res->ai_protocol);
  return Fcons (protocol, conv_sockaddr_to_lisp (res->ai_addr, res->ai_addrlen));
}


/* Get family and required size for sockaddr structure to hold ADDRESS.  */

static ptrdiff_t
get_lisp_to_sockaddr_size (Lisp_Object address, int *familyp)
{
  struct Lisp_Vector *p;

  if (VECTORP (address))
    {
      p = XVECTOR (address);
      if (p->header.size == 5)
	{
	  *familyp = AF_INET;
	  return sizeof (struct sockaddr_in);
	}
#ifdef AF_INET6
      else if (p->header.size == 9)
	{
	  *familyp = AF_INET6;
	  return sizeof (struct sockaddr_in6);
	}
#endif
    }
#ifdef HAVE_LOCAL_SOCKETS
  else if (STRINGP (address))
    {
      *familyp = AF_LOCAL;
      return sizeof (struct sockaddr_un);
    }
#endif
  else if (CONSP (address) && TYPE_RANGED_INTEGERP (int, XCAR (address))
	   && VECTORP (XCDR (address)))
    {
      struct sockaddr *sa;
      p = XVECTOR (XCDR (address));
      if (MAX_ALLOCA - sizeof sa->sa_family < p->header.size)
	return 0;
      *familyp = XINT (XCAR (address));
      return p->header.size + sizeof (sa->sa_family);
    }
  return 0;
}

/* Convert an address object (vector or string) to an internal sockaddr.

   The address format has been basically validated by
   get_lisp_to_sockaddr_size, but this does not mean FAMILY is valid;
   it could have come from user data.  So if FAMILY is not valid,
   we return after zeroing *SA.  */

static void
conv_lisp_to_sockaddr (int family, Lisp_Object address, struct sockaddr *sa, int len)
{
  register struct Lisp_Vector *p;
  register unsigned char *cp = NULL;
  register int i;
  EMACS_INT hostport;

  memset (sa, 0, len);

  if (VECTORP (address))
    {
      p = XVECTOR (address);
      if (family == AF_INET)
	{
	  DECLARE_POINTER_ALIAS (sin, struct sockaddr_in, sa);
	  len = sizeof (sin->sin_addr) + 1;
	  hostport = XINT (p->contents[--len]);
	  sin->sin_port = htons (hostport);
	  cp = (unsigned char *)&sin->sin_addr;
	  sa->sa_family = family;
	}
#ifdef AF_INET6
      else if (family == AF_INET6)
	{
	  DECLARE_POINTER_ALIAS (sin6, struct sockaddr_in6, sa);
	  DECLARE_POINTER_ALIAS (ip6, uint16_t, &sin6->sin6_addr);
	  len = sizeof (sin6->sin6_addr) / 2 + 1;
	  hostport = XINT (p->contents[--len]);
	  sin6->sin6_port = htons (hostport);
	  for (i = 0; i < len; i++)
	    if (INTEGERP (p->contents[i]))
	      {
		int j = XFASTINT (p->contents[i]) & 0xffff;
		ip6[i] = ntohs (j);
	      }
	  sa->sa_family = family;
	  return;
	}
#endif
      else
	return;
    }
  else if (STRINGP (address))
    {
#ifdef HAVE_LOCAL_SOCKETS
      if (family == AF_LOCAL)
	{
	  DECLARE_POINTER_ALIAS (sockun, struct sockaddr_un, sa);
	  cp = SDATA (address);
	  for (i = 0; i < sizeof (sockun->sun_path) && *cp; i++)
	    sockun->sun_path[i] = *cp++;
	  sa->sa_family = family;
	}
#endif
      return;
    }
  else
    {
      p = XVECTOR (XCDR (address));
      cp = (unsigned char *)sa + sizeof (sa->sa_family);
    }

  for (i = 0; i < len; i++)
    if (INTEGERP (p->contents[i]))
      *cp++ = XFASTINT (p->contents[i]) & 0xff;
}

#ifdef DATAGRAM_SOCKETS
DEFUN ("process-datagram-address", Fprocess_datagram_address, Sprocess_datagram_address,
       1, 1, 0,
       doc: /* Get the current datagram address associated with PROCESS.
If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed.  */)
  (Lisp_Object process)
{
  int channel;

  CHECK_PROCESS (process);

  if (NETCONN_P (process))
    wait_for_socket_fds (process, "process-datagram-address");

  if (!DATAGRAM_CONN_P (process))
    return Qnil;

  channel = XPROCESS (process)->infd;
  return conv_sockaddr_to_lisp (datagram_address[channel].sa,
				datagram_address[channel].len);
}

DEFUN ("set-process-datagram-address", Fset_process_datagram_address, Sset_process_datagram_address,
       2, 2, 0,
       doc: /* Set the datagram address for PROCESS to ADDRESS.
Return nil upon error setting address, ADDRESS otherwise.

If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed.  */)
  (Lisp_Object process, Lisp_Object address)
{
  int channel;
  int family;
  ptrdiff_t len;

  CHECK_PROCESS (process);

  if (NETCONN_P (process))
    wait_for_socket_fds (process, "set-process-datagram-address");

  if (!DATAGRAM_CONN_P (process))
    return Qnil;

  channel = XPROCESS (process)->infd;

  len = get_lisp_to_sockaddr_size (address, &family);
  if (len == 0 || datagram_address[channel].len != len)
    return Qnil;
  conv_lisp_to_sockaddr (family, address, datagram_address[channel].sa, len);
  return address;
}
#endif
\f

static const struct socket_options {
  /* The name of this option.  Should be lowercase version of option
     name without SO_ prefix.  */
  const char *name;
  /* Option level SOL_...  */
  int optlevel;
  /* Option number SO_...  */
  int optnum;
  enum { SOPT_UNKNOWN, SOPT_BOOL, SOPT_INT, SOPT_IFNAME, SOPT_LINGER } opttype;
  enum { OPIX_NONE = 0, OPIX_MISC = 1, OPIX_REUSEADDR = 2 } optbit;
} socket_options[] =
  {
#ifdef SO_BINDTODEVICE
    { ":bindtodevice", SOL_SOCKET, SO_BINDTODEVICE, SOPT_IFNAME, OPIX_MISC },
#endif
#ifdef SO_BROADCAST
    { ":broadcast", SOL_SOCKET, SO_BROADCAST, SOPT_BOOL, OPIX_MISC },
#endif
#ifdef SO_DONTROUTE
    { ":dontroute", SOL_SOCKET, SO_DONTROUTE, SOPT_BOOL, OPIX_MISC },
#endif
#ifdef SO_KEEPALIVE
    { ":keepalive", SOL_SOCKET, SO_KEEPALIVE, SOPT_BOOL, OPIX_MISC },
#endif
#ifdef SO_LINGER
    { ":linger", SOL_SOCKET, SO_LINGER, SOPT_LINGER, OPIX_MISC },
#endif
#ifdef SO_OOBINLINE
    { ":oobinline", SOL_SOCKET, SO_OOBINLINE, SOPT_BOOL, OPIX_MISC },
#endif
#ifdef SO_PRIORITY
    { ":priority", SOL_SOCKET, SO_PRIORITY, SOPT_INT, OPIX_MISC },
#endif
#ifdef SO_REUSEADDR
    { ":reuseaddr", SOL_SOCKET, SO_REUSEADDR, SOPT_BOOL, OPIX_REUSEADDR },
#endif
    { 0, 0, 0, SOPT_UNKNOWN, OPIX_NONE }
  };

/* Set option OPT to value VAL on socket S.

   Return (1<<socket_options[OPT].optbit) if option is known, 0 otherwise.
   Signals an error if setting a known option fails.
*/

static int
set_socket_option (int s, Lisp_Object opt, Lisp_Object val)
{
  char *name;
  const struct socket_options *sopt;
  int ret = 0;

  CHECK_SYMBOL (opt);

  name = SSDATA (SYMBOL_NAME (opt));
  for (sopt = socket_options; sopt->name; sopt++)
    if (strcmp (name, sopt->name) == 0)
      break;

  switch (sopt->opttype)
    {
    case SOPT_BOOL:
      {
	int optval;
	optval = NILP (val) ? 0 : 1;
	ret = setsockopt (s, sopt->optlevel, sopt->optnum,
			  &optval, sizeof (optval));
	break;
      }

    case SOPT_INT:
      {
	int optval;
	if (TYPE_RANGED_INTEGERP (int, val))
	  optval = XINT (val);
	else
	  error ("Bad option value for %s", name);
	ret = setsockopt (s, sopt->optlevel, sopt->optnum,
			  &optval, sizeof (optval));
	break;
      }

#ifdef SO_BINDTODEVICE
    case SOPT_IFNAME:
      {
	char devname[IFNAMSIZ + 1];

	/* This is broken, at least in the Linux 2.4 kernel.
	   To unbind, the arg must be a zero integer, not the empty string.
	   This should work on all systems.   KFS. 2003-09-23.  */
	memset (devname, 0, sizeof devname);
	if (STRINGP (val))
	  {
	    char *arg = SSDATA (val);
	    int len = min (strlen (arg), IFNAMSIZ);
	    memcpy (devname, arg, len);
	  }
	else if (!NILP (val))
	  error ("Bad option value for %s", name);
	ret = setsockopt (s, sopt->optlevel, sopt->optnum,
			  devname, IFNAMSIZ);
	break;
      }
#endif

#ifdef SO_LINGER
    case SOPT_LINGER:
      {
	struct linger linger;

	linger.l_onoff = 1;
	linger.l_linger = 0;
	if (TYPE_RANGED_INTEGERP (int, val))
	  linger.l_linger = XINT (val);
	else
	  linger.l_onoff = NILP (val) ? 0 : 1;
	ret = setsockopt (s, sopt->optlevel, sopt->optnum,
			  &linger, sizeof (linger));
	break;
      }
#endif

    default:
      return 0;
    }

  if (ret < 0)
    {
      int setsockopt_errno = errno;
      report_file_errno ("Cannot set network option", list2 (opt, val),
			 setsockopt_errno);
    }

  return (1 << sopt->optbit);
}


DEFUN ("set-network-process-option",
       Fset_network_process_option, Sset_network_process_option,
       3, 4, 0,
       doc: /* For network process PROCESS set option OPTION to value VALUE.
See `make-network-process' for a list of options and values.
If optional fourth arg NO-ERROR is non-nil, don't signal an error if
OPTION is not a supported option, return nil instead; otherwise return t.

If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed. */)
  (Lisp_Object process, Lisp_Object option, Lisp_Object value, Lisp_Object no_error)
{
  int s;
  struct Lisp_Process *p;

  CHECK_PROCESS (process);
  p = XPROCESS (process);
  if (!NETCONN1_P (p))
    error ("Process is not a network process");

  wait_for_socket_fds (process, "set-network-process-option");

  s = p->infd;
  if (s < 0)
    error ("Process is not running");

  if (set_socket_option (s, option, value))
    {
      pset_childp (p, Fplist_put (p->childp, option, value));
      return Qt;
    }

  if (NILP (no_error))
    error ("Unknown or unsupported option");

  return Qnil;
}

\f
DEFUN ("serial-process-configure",
       Fserial_process_configure,
       Sserial_process_configure,
       0, MANY, 0,
       doc: /* Configure speed, bytesize, etc. of a serial process.

Arguments are specified as keyword/argument pairs.  Attributes that
are not given are re-initialized from the process's current
configuration (available via the function `process-contact') or set to
reasonable default values.  The following arguments are defined:

:process PROCESS
:name NAME
:buffer BUFFER
:port PORT
-- Any of these arguments can be given to identify the process that is
to be configured.  If none of these arguments is given, the current
buffer's process is used.

:speed SPEED -- SPEED is the speed of the serial port in bits per
second, also called baud rate.  Any value can be given for SPEED, but
most serial ports work only at a few defined values between 1200 and
115200, with 9600 being the most common value.  If SPEED is nil, the
serial port is not configured any further, i.e., all other arguments
are ignored.  This may be useful for special serial ports such as
Bluetooth-to-serial converters which can only be configured through AT
commands.  A value of nil for SPEED can be used only when passed
through `make-serial-process' or `serial-term'.

:bytesize BYTESIZE -- BYTESIZE is the number of bits per byte, which
can be 7 or 8.  If BYTESIZE is not given or nil, a value of 8 is used.

:parity PARITY -- PARITY can be nil (don't use parity), the symbol
`odd' (use odd parity), or the symbol `even' (use even parity).  If
PARITY is not given, no parity is used.

:stopbits STOPBITS -- STOPBITS is the number of stopbits used to
terminate a byte transmission.  STOPBITS can be 1 or 2.  If STOPBITS
is not given or nil, 1 stopbit is used.

:flowcontrol FLOWCONTROL -- FLOWCONTROL determines the type of
flowcontrol to be used, which is either nil (don't use flowcontrol),
the symbol `hw' (use RTS/CTS hardware flowcontrol), or the symbol `sw'
\(use XON/XOFF software flowcontrol).  If FLOWCONTROL is not given, no
flowcontrol is used.

`serial-process-configure' is called by `make-serial-process' for the
initial configuration of the serial port.

Examples:

\(serial-process-configure :process "/dev/ttyS0" :speed 1200)

\(serial-process-configure
    :buffer "COM1" :stopbits 1 :parity \\='odd :flowcontrol \\='hw)

\(serial-process-configure :port "\\\\.\\COM13" :bytesize 7)

usage: (serial-process-configure &rest ARGS)  */)
  (ptrdiff_t nargs, Lisp_Object *args)
{
  struct Lisp_Process *p;
  Lisp_Object contact = Qnil;
  Lisp_Object proc = Qnil;

  contact = Flist (nargs, args);

  proc = Fplist_get (contact, QCprocess);
  if (NILP (proc))
    proc = Fplist_get (contact, QCname);
  if (NILP (proc))
    proc = Fplist_get (contact, QCbuffer);
  if (NILP (proc))
    proc = Fplist_get (contact, QCport);
  proc = get_process (proc);
  p = XPROCESS (proc);
  if (!EQ (p->type, Qserial))
    error ("Not a serial process");

  if (NILP (Fplist_get (p->childp, QCspeed)))
    return Qnil;

  serial_configure (p, contact);
  return Qnil;
}

DEFUN ("make-serial-process", Fmake_serial_process, Smake_serial_process,
       0, MANY, 0,
       doc: /* Create and return a serial port process.

In Emacs, serial port connections are represented by process objects,
so input and output work as for subprocesses, and `delete-process'
closes a serial port connection.  However, a serial process has no
process id, it cannot be signaled, and the status codes are different
from normal processes.

`make-serial-process' creates a process and a buffer, on which you
probably want to use `process-send-string'.  Try \\[serial-term] for
an interactive terminal.  See below for examples.

Arguments are specified as keyword/argument pairs.  The following
arguments are defined:

:port PORT -- (mandatory) PORT is the path or name of the serial port.
For example, this could be "/dev/ttyS0" on Unix.  On Windows, this
could be "COM1", or "\\\\.\\COM10" for ports higher than COM9 (double
the backslashes in strings).

:speed SPEED -- (mandatory) is handled by `serial-process-configure',
which this function calls.

:name NAME -- NAME is the name of the process.  If NAME is not given,
the value of PORT is used.

:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
with the process.  Process output goes at the end of that buffer,
unless you specify a filter function to handle the output.  If BUFFER
is not given, the value of NAME is used.

:coding CODING -- If CODING is a symbol, it specifies the coding
system used for both reading and writing for this process.  If CODING
is a cons (DECODING . ENCODING), DECODING is used for reading, and
ENCODING is used for writing.

:noquery BOOL -- When exiting Emacs, query the user if BOOL is nil and
the process is running.  If BOOL is not given, query before exiting.

:stop BOOL -- Start process in the `stopped' state if BOOL is non-nil.
In the stopped state, a serial process does not accept incoming data,
but you can send outgoing data.  The stopped state is cleared by
`continue-process' and set by `stop-process'.

:filter FILTER -- Install FILTER as the process filter.

:sentinel SENTINEL -- Install SENTINEL as the process sentinel.

:plist PLIST -- Install PLIST as the initial plist of the process.

:bytesize
:parity
:stopbits
:flowcontrol
-- This function calls `serial-process-configure' to handle these
arguments.

The original argument list, possibly modified by later configuration,
is available via the function `process-contact'.

Examples:

\(make-serial-process :port "/dev/ttyS0" :speed 9600)

\(make-serial-process :port "COM1" :speed 115200 :stopbits 2)

\(make-serial-process :port "\\\\.\\COM13" :speed 1200 :bytesize 7 :parity \\='odd)

\(make-serial-process :port "/dev/tty.BlueConsole-SPP-1" :speed nil)

usage:  (make-serial-process &rest ARGS)  */)
  (ptrdiff_t nargs, Lisp_Object *args)
{
  int fd = -1;
  Lisp_Object proc, contact, port;
  struct Lisp_Process *p;
  Lisp_Object name, buffer;
  Lisp_Object tem, val;
  ptrdiff_t specpdl_count;

  if (nargs == 0)
    return Qnil;

  contact = Flist (nargs, args);

  port = Fplist_get (contact, QCport);
  if (NILP (port))
    error ("No port specified");
  CHECK_STRING (port);

  if (NILP (Fplist_member (contact, QCspeed)))
    error (":speed not specified");
  if (!NILP (Fplist_get (contact, QCspeed)))
    CHECK_NUMBER (Fplist_get (contact, QCspeed));

  name = Fplist_get (contact, QCname);
  if (NILP (name))
    name = port;
  CHECK_STRING (name);
  proc = make_process (name);
  specpdl_count = SPECPDL_INDEX ();
  record_unwind_protect (remove_process, proc);
  p = XPROCESS (proc);

  fd = serial_open (port);
  p->open_fd[SUBPROCESS_STDIN] = fd;
  p->infd = fd;
  p->outfd = fd;
  if (fd > max_desc)
    max_desc = fd;
  chan_process[fd] = proc;

  buffer = Fplist_get (contact, QCbuffer);
  if (NILP (buffer))
    buffer = name;
  buffer = Fget_buffer_create (buffer);
  pset_buffer (p, buffer);

  pset_childp (p, contact);
  pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist)));
  pset_type (p, Qserial);
  pset_sentinel (p, Fplist_get (contact, QCsentinel));
  pset_filter (p, Fplist_get (contact, QCfilter));
  eassert (NILP (p->log));
  if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
    p->kill_without_query = 1;
  if (tem = Fplist_get (contact, QCstop), !NILP (tem))
    pset_command (p, Qt);
  eassert (! p->pty_flag);

  if (!EQ (p->command, Qt))
    add_process_read_fd (fd);

  if (BUFFERP (buffer))
    {
      set_marker_both (p->mark, buffer,
		       BUF_ZV (XBUFFER (buffer)),
		       BUF_ZV_BYTE (XBUFFER (buffer)));
    }

  tem = Fplist_member (contact, QCcoding);
  if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
    tem = Qnil;

  val = Qnil;
  if (!NILP (tem))
    {
      val = XCAR (XCDR (tem));
      if (CONSP (val))
	val = XCAR (val);
    }
  else if (!NILP (Vcoding_system_for_read))
    val = Vcoding_system_for_read;
  else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
	   || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
    val = Qnil;
  pset_decode_coding_system (p, val);

  val = Qnil;
  if (!NILP (tem))
    {
      val = XCAR (XCDR (tem));
      if (CONSP (val))
	val = XCDR (val);
    }
  else if (!NILP (Vcoding_system_for_write))
    val = Vcoding_system_for_write;
  else if ((!NILP (buffer) && NILP (BVAR (XBUFFER (buffer), enable_multibyte_characters)))
	   || (NILP (buffer) && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
    val = Qnil;
  pset_encode_coding_system (p, val);

  setup_process_coding_systems (proc);
  pset_decoding_buf (p, empty_unibyte_string);
  eassert (p->decoding_carryover == 0);
  pset_encoding_buf (p, empty_unibyte_string);
  p->inherit_coding_system_flag
    = !(!NILP (tem) || NILP (buffer) || !inherit_process_coding_system);

  Fserial_process_configure (nargs, args);

  specpdl_ptr = specpdl + specpdl_count;

  return proc;
}

static void
set_network_socket_coding_system (Lisp_Object proc, Lisp_Object host,
				  Lisp_Object service, Lisp_Object name)
{
  Lisp_Object tem;
  struct Lisp_Process *p = XPROCESS (proc);
  Lisp_Object contact = p->childp;
  Lisp_Object coding_systems = Qt;
  Lisp_Object val;

  tem = Fplist_member (contact, QCcoding);
  if (!NILP (tem) && (!CONSP (tem) || !CONSP (XCDR (tem))))
    tem = Qnil;  /* No error message (too late!).  */

  /* Setup coding systems for communicating with the network stream.  */
  /* Qt denotes we have not yet called Ffind_operation_coding_system.  */

  if (!NILP (tem))
    {
      val = XCAR (XCDR (tem));
      if (CONSP (val))
	val = XCAR (val);
    }
  else if (!NILP (Vcoding_system_for_read))
    val = Vcoding_system_for_read;
  else if ((!NILP (p->buffer)
	    && NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters)))
	   || (NILP (p->buffer)
	       && NILP (BVAR (&buffer_defaults, enable_multibyte_characters))))
    /* We dare not decode end-of-line format by setting VAL to
       Qraw_text, because the existing Emacs Lisp libraries
       assume that they receive bare code including a sequence of
       CR LF.  */
    val = Qnil;
  else
    {
      if (NILP (host) || NILP (service))
	coding_systems = Qnil;
      else
	coding_systems = CALLN (Ffind_operation_coding_system,
				Qopen_network_stream, name, p->buffer,
				host, service);
      if (CONSP (coding_systems))
	val = XCAR (coding_systems);
      else if (CONSP (Vdefault_process_coding_system))
	val = XCAR (Vdefault_process_coding_system);
      else
	val = Qnil;
    }
  pset_decode_coding_system (p, val);

  if (!NILP (tem))
    {
      val = XCAR (XCDR (tem));
      if (CONSP (val))
	val = XCDR (val);
    }
  else if (!NILP (Vcoding_system_for_write))
    val = Vcoding_system_for_write;
  else if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
    val = Qnil;
  else
    {
      if (EQ (coding_systems, Qt))
	{
	  if (NILP (host) || NILP (service))
	    coding_systems = Qnil;
	  else
	    coding_systems = CALLN (Ffind_operation_coding_system,
				    Qopen_network_stream, name, p->buffer,
				    host, service);
	}
      if (CONSP (coding_systems))
	val = XCDR (coding_systems);
      else if (CONSP (Vdefault_process_coding_system))
	val = XCDR (Vdefault_process_coding_system);
      else
	val = Qnil;
    }
  pset_encode_coding_system (p, val);

  pset_decoding_buf (p, empty_unibyte_string);
  p->decoding_carryover = 0;
  pset_encoding_buf (p, empty_unibyte_string);

  p->inherit_coding_system_flag
    = !(!NILP (tem) || NILP (p->buffer) || !inherit_process_coding_system);
}

#ifdef HAVE_GNUTLS
static void
finish_after_tls_connection (Lisp_Object proc)
{
  struct Lisp_Process *p = XPROCESS (proc);
  Lisp_Object contact = p->childp;
  Lisp_Object result = Qt;

  if (!NILP (Ffboundp (Qnsm_verify_connection)))
    result = call3 (Qnsm_verify_connection,
		    proc,
		    Fplist_get (contact, QChost),
		    Fplist_get (contact, QCservice));

  if (NILP (result))
    {
      pset_status (p, list2 (Qfailed,
			     build_string ("The Network Security Manager stopped the connections")));
      deactivate_process (proc);
    }
  else if (p->outfd < 0)
    {
      /* The counterparty may have closed the connection (especially
	 if the NSM prompt above take a long time), so recheck the file
	 descriptor here. */
      pset_status (p, Qfailed);
      deactivate_process (proc);
    }
  else if ((fd_callback_info[p->outfd].flags & NON_BLOCKING_CONNECT_FD) == 0)
    {
      /* If we cleared the connection wait mask before we did the TLS
	 setup, then we have to say that the process is finally "open"
	 here. */
      pset_status (p, Qrun);
      /* Execute the sentinel here.  If we had relied on status_notify
	 to do it later, it will read input from the process before
	 calling the sentinel.  */
      exec_sentinel (proc, build_string ("open\n"));
    }
}
#endif

static void
connect_network_socket (Lisp_Object proc, Lisp_Object addrinfos,
                        Lisp_Object use_external_socket_p)
{
  ptrdiff_t count = SPECPDL_INDEX ();
  int s = -1, outch, inch;
  int xerrno = 0;
  int family;
  struct sockaddr *sa = NULL;
  int ret;
  ptrdiff_t addrlen;
  struct Lisp_Process *p = XPROCESS (proc);
  Lisp_Object contact = p->childp;
  int optbits = 0;
  int socket_to_use = -1;

  if (!NILP (use_external_socket_p))
    {
      socket_to_use = external_sock_fd;

      /* Ensure we don't consume the external socket twice.  */
      external_sock_fd = -1;
    }

  /* Do this in case we never enter the while-loop below.  */
  s = -1;

  while (!NILP (addrinfos))
    {
      Lisp_Object addrinfo = XCAR (addrinfos);
      addrinfos = XCDR (addrinfos);
      int protocol = XINT (XCAR (addrinfo));
      Lisp_Object ip_address = XCDR (addrinfo);

#ifdef WINDOWSNT
    retry_connect:
#endif

      addrlen = get_lisp_to_sockaddr_size (ip_address, &family);
      if (sa)
	free (sa);
      sa = xmalloc (addrlen);
      conv_lisp_to_sockaddr (family, ip_address, sa, addrlen);

      s = socket_to_use;
      if (s < 0)
	{
	  int socktype = p->socktype | SOCK_CLOEXEC;
	  if (p->is_non_blocking_client)
	    socktype |= SOCK_NONBLOCK;
	  s = socket (family, socktype, protocol);
	  if (s < 0)
	    {
	      xerrno = errno;
	      continue;
	    }
	}

      if (p->is_non_blocking_client && ! (SOCK_NONBLOCK && socket_to_use < 0))
	{
	  ret = fcntl (s, F_SETFL, O_NONBLOCK);
	  if (ret < 0)
	    {
	      xerrno = errno;
	      emacs_close (s);
	      s = -1;
	      if (0 <= socket_to_use)
		break;
	      continue;
	    }
	}

#ifdef DATAGRAM_SOCKETS
      if (!p->is_server && p->socktype == SOCK_DGRAM)
	break;
#endif /* DATAGRAM_SOCKETS */

      /* Make us close S if quit.  */
      record_unwind_protect_int (close_file_unwind, s);

      /* Parse network options in the arg list.  We simply ignore anything
	 which isn't a known option (including other keywords).  An error
	 is signaled if setting a known option fails.  */
      {
	Lisp_Object params = contact, key, val;

	while (!NILP (params))
	  {
	    key = XCAR (params);
	    params = XCDR (params);
	    val = XCAR (params);
	    params = XCDR (params);
	    optbits |= set_socket_option (s, key, val);
	  }
      }

      if (p->is_server)
	{
	  /* Configure as a server socket.  */

	  /* SO_REUSEADDR = 1 is default for server sockets; must specify
	     explicit :reuseaddr key to override this.  */
#ifdef HAVE_LOCAL_SOCKETS
	  if (family != AF_LOCAL)
#endif
	    if (!(optbits & (1 << OPIX_REUSEADDR)))
	      {
		int optval = 1;
		if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval))
		  report_file_error ("Cannot set reuse option on server socket", Qnil);
	      }

          /* If passed a socket descriptor, it should be already bound. */
	  if (socket_to_use < 0 && bind (s, sa, addrlen) != 0)
	    report_file_error ("Cannot bind server socket", Qnil);

#ifdef HAVE_GETSOCKNAME
	  if (p->port == 0
#ifdef HAVE_LOCAL_SOCKETS
	      && family != AF_LOCAL
#endif
	      )
	    {
	      struct sockaddr_in sa1;
	      socklen_t len1 = sizeof (sa1);
#ifdef AF_INET6
	      /* The code below assumes the port is at the same offset
		 and of the same width in both IPv4 and IPv6
		 structures, but the standards don't guarantee that,
		 so verify it here.  */
	      struct sockaddr_in6 sa6;
	      verify ((offsetof (struct sockaddr_in, sin_port)
		       == offsetof (struct sockaddr_in6, sin6_port))
		      && sizeof (sa1.sin_port) == sizeof (sa6.sin6_port));
#endif
	      DECLARE_POINTER_ALIAS (psa1, struct sockaddr, &sa1);
	      if (getsockname (s, psa1, &len1) == 0)
		{
		  Lisp_Object service = make_number (ntohs (sa1.sin_port));
		  contact = Fplist_put (contact, QCservice, service);
		  /* Save the port number so that we can stash it in
		     the process object later.  */
		  DECLARE_POINTER_ALIAS (psa, struct sockaddr_in, sa);
		  psa->sin_port = sa1.sin_port;
		}
	    }
#endif

	  if (p->socktype != SOCK_DGRAM && listen (s, p->backlog))
	    report_file_error ("Cannot listen on server socket", Qnil);

	  break;
	}

      maybe_quit ();

      ret = connect (s, sa, addrlen);
      xerrno = errno;

      if (ret == 0 || xerrno == EISCONN)
	{
	  /* The unwind-protect will be discarded afterwards.  */
	  break;
	}

      if (p->is_non_blocking_client && xerrno == EINPROGRESS)
	break;

#ifndef WINDOWSNT
      if (xerrno == EINTR)
	{
	  /* Unlike most other syscalls connect() cannot be called
	     again.  (That would return EALREADY.)  The proper way to
	     wait for completion is pselect().  */
	  int sc;
	  socklen_t len;
	  fd_set fdset;
	retry_select:
	  FD_ZERO (&fdset);
	  FD_SET (s, &fdset);
	  maybe_quit ();
	  sc = pselect (s + 1, NULL, &fdset, NULL, NULL, NULL);
	  if (sc == -1)
	    {
	      if (errno == EINTR)
		goto retry_select;
	      else
		report_file_error ("Failed select", Qnil);
	    }
	  eassert (sc > 0);

	  len = sizeof xerrno;
	  eassert (FD_ISSET (s, &fdset));
	  if (getsockopt (s, SOL_SOCKET, SO_ERROR, &xerrno, &len) < 0)
	    report_file_error ("Failed getsockopt", Qnil);
	  if (xerrno == 0)
	    break;
	  if (NILP (addrinfos))
	    report_file_errno ("Failed connect", Qnil, xerrno);
	}
#endif /* !WINDOWSNT */

      /* Discard the unwind protect closing S.  */
      specpdl_ptr = specpdl + count;
      emacs_close (s);
      s = -1;
      if (0 <= socket_to_use)
	break;

#ifdef WINDOWSNT
      if (xerrno == EINTR)
	goto retry_connect;
#endif
    }

  if (s >= 0)
    {
#ifdef DATAGRAM_SOCKETS
      if (p->socktype == SOCK_DGRAM)
	{
	  if (datagram_address[s].sa)
	    emacs_abort ();

	  datagram_address[s].sa = xmalloc (addrlen);
	  datagram_address[s].len = addrlen;
	  if (p->is_server)
	    {
	      Lisp_Object remote;
	      memset (datagram_address[s].sa, 0, addrlen);
	      if (remote = Fplist_get (contact, QCremote), !NILP (remote))
		{
		  int rfamily;
		  ptrdiff_t rlen = get_lisp_to_sockaddr_size (remote, &rfamily);
		  if (rlen != 0 && rfamily == family
		      && rlen == addrlen)
		    conv_lisp_to_sockaddr (rfamily, remote,
					   datagram_address[s].sa, rlen);
		}
	    }
	  else
	    memcpy (datagram_address[s].sa, sa, addrlen);
	}
#endif

      contact = Fplist_put (contact, p->is_server? QClocal: QCremote,
			    conv_sockaddr_to_lisp (sa, addrlen));
#ifdef HAVE_GETSOCKNAME
      if (!p->is_server)
	{
	  struct sockaddr_storage sa1;
	  socklen_t len1 = sizeof (sa1);
	  DECLARE_POINTER_ALIAS (psa1, struct sockaddr, &sa1);
	  if (getsockname (s, psa1, &len1) == 0)
	    contact = Fplist_put (contact, QClocal,
				  conv_sockaddr_to_lisp (psa1, len1));
	}
#endif
    }

  if (s < 0)
    {
      /* If non-blocking got this far - and failed - assume non-blocking is
	 not supported after all.  This is probably a wrong assumption, but
	 the normal blocking calls to open-network-stream handles this error
	 better.  */
      if (p->is_non_blocking_client)
	return;

      report_file_errno ((p->is_server
			  ? "make server process failed"
			  : "make client process failed"),
			 contact, xerrno);
    }

  inch = s;
  outch = s;

  chan_process[inch] = proc;

  fcntl (inch, F_SETFL, O_NONBLOCK);

  p = XPROCESS (proc);
  p->open_fd[SUBPROCESS_STDIN] = inch;
  p->infd  = inch;
  p->outfd = outch;

  /* Discard the unwind protect for closing S, if any.  */
  specpdl_ptr = specpdl + count;

  if (p->is_server && p->socktype != SOCK_DGRAM)
    pset_status (p, Qlisten);

  /* Make the process marker point into the process buffer (if any).  */
  if (BUFFERP (p->buffer))
    set_marker_both (p->mark, p->buffer,
		     BUF_ZV (XBUFFER (p->buffer)),
		     BUF_ZV_BYTE (XBUFFER (p->buffer)));

  if (p->is_non_blocking_client)
    {
      /* We may get here if connect did succeed immediately.  However,
	 in that case, we still need to signal this like a non-blocking
	 connection.  */
      if (! (connecting_status (p->status)
	     && EQ (XCDR (p->status), addrinfos)))
	pset_status (p, Fcons (Qconnect, addrinfos));
      if ((fd_callback_info[inch].flags & NON_BLOCKING_CONNECT_FD) == 0)
	add_non_blocking_write_fd (inch);
    }
  else
    /* A server may have a client filter setting of Qt, but it must
       still listen for incoming connects unless it is stopped.  */
    if ((!EQ (p->filter, Qt) && !EQ (p->command, Qt))
	|| (EQ (p->status, Qlisten) && NILP (p->command)))
      add_process_read_fd (inch);

  if (inch > max_desc)
    max_desc = inch;

  /* Set up the masks based on the process filter. */
  set_process_filter_masks (p);

  setup_process_coding_systems (proc);

#ifdef HAVE_GNUTLS
  /* Continue the asynchronous connection. */
  if (!NILP (p->gnutls_boot_parameters))
    {
      Lisp_Object boot, params = p->gnutls_boot_parameters;

      boot = Fgnutls_boot (proc, XCAR (params), XCDR (params));
      p->gnutls_boot_parameters = Qnil;

      if (p->gnutls_initstage == GNUTLS_STAGE_READY)
	/* Run sentinels, etc. */
	finish_after_tls_connection (proc);
      else if (p->gnutls_initstage != GNUTLS_STAGE_HANDSHAKE_TRIED)
	{
	  deactivate_process (proc);
	  if (NILP (boot))
	    pset_status (p, list2 (Qfailed,
				   build_string ("TLS negotiation failed")));
	  else
	    pset_status (p, list2 (Qfailed, boot));
	}
    }
#endif

}

/* Create a network stream/datagram client/server process.  Treated
   exactly like a normal process when reading and writing.  Primary
   differences are in status display and process deletion.  A network
   connection has no PID; you cannot signal it.  All you can do is
   stop/continue it and deactivate/close it via delete-process.  */

DEFUN ("make-network-process", Fmake_network_process, Smake_network_process,
       0, MANY, 0,
       doc: /* Create and return a network server or client process.

In Emacs, network connections are represented by process objects, so
input and output work as for subprocesses and `delete-process' closes
a network connection.  However, a network process has no process id,
it cannot be signaled, and the status codes are different from normal
processes.

Arguments are specified as keyword/argument pairs.  The following
arguments are defined:

:name NAME -- NAME is name for process.  It is modified if necessary
to make it unique.

:buffer BUFFER -- BUFFER is the buffer (or buffer-name) to associate
with the process.  Process output goes at end of that buffer, unless
you specify a filter function to handle the output.  BUFFER may be
also nil, meaning that this process is not associated with any buffer.

:host HOST -- HOST is name of the host to connect to, or its IP
address.  The symbol `local' specifies the local host.  If specified
for a server process, it must be a valid name or address for the local
host, and only clients connecting to that address will be accepted.

:service SERVICE -- SERVICE is name of the service desired, or an
integer specifying a port number to connect to.  If SERVICE is t,
a random port number is selected for the server.  A port number can
be specified as an integer string, e.g., "80", as well as an integer.

:type TYPE -- TYPE is the type of connection.  The default (nil) is a
stream type connection, `datagram' creates a datagram type connection,
`seqpacket' creates a reliable datagram connection.

:family FAMILY -- FAMILY is the address (and protocol) family for the
service specified by HOST and SERVICE.  The default (nil) is to use
whatever address family (IPv4 or IPv6) that is defined for the host
and port number specified by HOST and SERVICE.  Other address families
supported are:
  local -- for a local (i.e. UNIX) address specified by SERVICE.
  ipv4  -- use IPv4 address family only.
  ipv6  -- use IPv6 address family only.

:local ADDRESS -- ADDRESS is the local address used for the connection.
This parameter is ignored when opening a client process. When specified
for a server process, the FAMILY, HOST and SERVICE args are ignored.

:remote ADDRESS -- ADDRESS is the remote partner's address for the
connection.  This parameter is ignored when opening a stream server
process.  For a datagram server process, it specifies the initial
setting of the remote datagram address.  When specified for a client
process, the FAMILY, HOST, and SERVICE args are ignored.

The format of ADDRESS depends on the address family:
- An IPv4 address is represented as a vector of integers [A B C D P]
corresponding to numeric IP address A.B.C.D and port number P.
- A local address is represented as a string with the address in the
local address space.
- An "unsupported family" address is represented by a cons (F . AV)
where F is the family number and AV is a vector containing the socket
address data with one element per address data byte.  Do not rely on
this format in portable code, as it may depend on implementation
defined constants, data sizes, and data structure alignment.

:coding CODING -- If CODING is a symbol, it specifies the coding
system used for both reading and writing for this process.  If CODING
is a cons (DECODING . ENCODING), DECODING is used for reading, and
ENCODING is used for writing.

:nowait BOOL -- If NOWAIT is non-nil for a stream type client
process, return without waiting for the connection to complete;
instead, the sentinel function will be called with second arg matching
"open" (if successful) or "failed" when the connect completes.
Default is to use a blocking connect (i.e. wait) for stream type
connections.

:noquery BOOL -- Query the user unless BOOL is non-nil, and process is
running when Emacs is exited.

:stop BOOL -- Start process in the `stopped' state if BOOL non-nil.
In the stopped state, a server process does not accept new
connections, and a client process does not handle incoming traffic.
The stopped state is cleared by `continue-process' and set by
`stop-process'.

:filter FILTER -- Install FILTER as the process filter.

:filter-multibyte BOOL -- If BOOL is non-nil, strings given to the
process filter are multibyte, otherwise they are unibyte.
If this keyword is not specified, the strings are multibyte.

:sentinel SENTINEL -- Install SENTINEL as the process sentinel.

:log LOG -- Install LOG as the server process log function.  This
function is called when the server accepts a network connection from a
client.  The arguments are SERVER, CLIENT, and MESSAGE, where SERVER
is the server process, CLIENT is the new process for the connection,
and MESSAGE is a string.

:plist PLIST -- Install PLIST as the new process's initial plist.

:tls-parameters LIST -- is a list that should be supplied if you're
opening a TLS connection.  The first element is the TLS type (either
`gnutls-x509pki' or `gnutls-anon'), and the remaining elements should
be a keyword list accepted by gnutls-boot (as returned by
`gnutls-boot-parameters').

:server QLEN -- if QLEN is non-nil, create a server process for the
specified FAMILY, SERVICE, and connection type (stream or datagram).
If QLEN is an integer, it is used as the max. length of the server's
pending connection queue (also known as the backlog); the default
queue length is 5.  Default is to create a client process.

The following network options can be specified for this connection:

:broadcast BOOL    -- Allow send and receive of datagram broadcasts.
:dontroute BOOL    -- Only send to directly connected hosts.
:keepalive BOOL    -- Send keep-alive messages on network stream.
:linger BOOL or TIMEOUT -- Send queued messages before closing.
:oobinline BOOL    -- Place out-of-band data in receive data stream.
:priority INT      -- Set protocol defined priority for sent packets.
:reuseaddr BOOL    -- Allow reusing a recently used local address
                      (this is allowed by default for a server process).
:bindtodevice NAME -- bind to interface NAME.  Using this may require
                      special privileges on some systems.
:use-external-socket BOOL -- Use any pre-allocated sockets that have
                             been passed to Emacs.  If Emacs wasn't
                             passed a socket, this option is silently
                             ignored.


Consult the relevant system programmer's manual pages for more
information on using these options.


A server process will listen for and accept connections from clients.
When a client connection is accepted, a new network process is created
for the connection with the following parameters:

- The client's process name is constructed by concatenating the server
process's NAME and a client identification string.
- If the FILTER argument is non-nil, the client process will not get a
separate process buffer; otherwise, the client's process buffer is a newly
created buffer named after the server process's BUFFER name or process
NAME concatenated with the client identification string.
- The connection type and the process filter and sentinel parameters are
inherited from the server process's TYPE, FILTER and SENTINEL.
- The client process's contact info is set according to the client's
addressing information (typically an IP address and a port number).
- The client process's plist is initialized from the server's plist.

Notice that the FILTER and SENTINEL args are never used directly by
the server process.  Also, the BUFFER argument is not used directly by
the server process, but via the optional :log function, accepted (and
failed) connections may be logged in the server process's buffer.

The original argument list, modified with the actual connection
information, is available via the `process-contact' function.

usage: (make-network-process &rest ARGS)  */)
  (ptrdiff_t nargs, Lisp_Object *args)
{
  Lisp_Object proc;
  Lisp_Object contact;
  struct Lisp_Process *p;
  const char *portstring UNINIT;
  char portbuf[INT_BUFSIZE_BOUND (EMACS_INT)];
#ifdef HAVE_LOCAL_SOCKETS
  struct sockaddr_un address_un;
#endif
  EMACS_INT port = 0;
  Lisp_Object tem;
  Lisp_Object name, buffer, host, service, address;
  Lisp_Object filter, sentinel, use_external_socket_p;
  Lisp_Object addrinfos = Qnil;
  int socktype;
  int family = -1;
  enum { any_protocol = 0 };
#ifdef HAVE_GETADDRINFO_A
  struct gaicb *dns_request = NULL;
#endif
  ptrdiff_t count = SPECPDL_INDEX ();

  if (nargs == 0)
    return Qnil;

  /* Save arguments for process-contact and clone-process.  */
  contact = Flist (nargs, args);

#ifdef WINDOWSNT
  /* Ensure socket support is loaded if available.  */
  init_winsock (TRUE);
#endif

  /* :type TYPE  (nil: stream, datagram */
  tem = Fplist_get (contact, QCtype);
  if (NILP (tem))
    socktype = SOCK_STREAM;
#ifdef DATAGRAM_SOCKETS
  else if (EQ (tem, Qdatagram))
    socktype = SOCK_DGRAM;
#endif
#ifdef HAVE_SEQPACKET
  else if (EQ (tem, Qseqpacket))
    socktype = SOCK_SEQPACKET;
#endif
  else
    error ("Unsupported connection type");

  name = Fplist_get (contact, QCname);
  buffer = Fplist_get (contact, QCbuffer);
  filter = Fplist_get (contact, QCfilter);
  sentinel = Fplist_get (contact, QCsentinel);
  use_external_socket_p = Fplist_get (contact, QCuse_external_socket);

  CHECK_STRING (name);

  /* :local ADDRESS or :remote ADDRESS */
  tem = Fplist_get (contact, QCserver);
  if (NILP (tem))
    address = Fplist_get (contact, QCremote);
  else
    address = Fplist_get (contact, QClocal);
  if (!NILP (address))
    {
      host = service = Qnil;

      if (!get_lisp_to_sockaddr_size (address, &family))
	error ("Malformed :address");

      addrinfos = list1 (Fcons (make_number (any_protocol), address));
      goto open_socket;
    }

  /* :family FAMILY -- nil (for Inet), local, or integer.  */
  tem = Fplist_get (contact, QCfamily);
  if (NILP (tem))
    {
#ifdef AF_INET6
      family = AF_UNSPEC;
#else
      family = AF_INET;
#endif
    }
#ifdef HAVE_LOCAL_SOCKETS
  else if (EQ (tem, Qlocal))
    family = AF_LOCAL;
#endif
#ifdef AF_INET6
  else if (EQ (tem, Qipv6))
    family = AF_INET6;
#endif
  else if (EQ (tem, Qipv4))
    family = AF_INET;
  else if (TYPE_RANGED_INTEGERP (int, tem))
    family = XINT (tem);
  else
    error ("Unknown address family");

  /* :service SERVICE -- string, integer (port number), or t (random port).  */
  service = Fplist_get (contact, QCservice);

  /* :host HOST -- hostname, ip address, or 'local for localhost.  */
  host = Fplist_get (contact, QChost);
  if (NILP (host))
    {
      /* The "connection" function gets it bind info from the address we're
	 given, so use this dummy address if nothing is specified. */
#ifdef HAVE_LOCAL_SOCKETS
      if (family != AF_LOCAL)
#endif
	host = build_string ("127.0.0.1");
    }
  else
    {
      if (EQ (host, Qlocal))
	/* Depending on setup, "localhost" may map to different IPv4 and/or
	   IPv6 addresses, so it's better to be explicit (Bug#6781).  */
	host = build_string ("127.0.0.1");
      CHECK_STRING (host);
    }

#ifdef HAVE_LOCAL_SOCKETS
  if (family == AF_LOCAL)
    {
      if (!NILP (host))
	{
	  message (":family local ignores the :host property");
	  contact = Fplist_put (contact, QChost, Qnil);
	  host = Qnil;
	}
      CHECK_STRING (service);
      if (sizeof address_un.sun_path <= SBYTES (service))
	error ("Service name too long");
      addrinfos = list1 (Fcons (make_number (any_protocol), service));
      goto open_socket;
    }
#endif

  /* Slow down polling to every ten seconds.
     Some kernels have a bug which causes retrying connect to fail
     after a connect.  Polling can interfere with gethostbyname too.  */
#ifdef POLL_FOR_INPUT
  if (socktype != SOCK_DGRAM)
    {
      record_unwind_protect_void (run_all_atimers);
      bind_polling_period (10);
    }
#endif

  if (!NILP (host))
    {
      ptrdiff_t portstringlen ATTRIBUTE_UNUSED;

      /* SERVICE can either be a string or int.
	 Convert to a C string for later use by getaddrinfo.  */
      if (EQ (service, Qt))
	{
	  portstring = "0";
	  portstringlen = 1;
	}
      else if (INTEGERP (service))
	{
	  portstring = portbuf;
	  portstringlen = sprintf (portbuf, "%"pI"d", XINT (service));
	}
      else
	{
	  CHECK_STRING (service);
	  portstring = SSDATA (service);
	  portstringlen = SBYTES (service);
	}

#ifdef HAVE_GETADDRINFO_A
      if (!NILP (Fplist_get (contact, QCnowait)))
	{
	  ptrdiff_t hostlen = SBYTES (host);
	  struct req
	  {
	    struct gaicb gaicb;
	    struct addrinfo hints;
	    char str[FLEXIBLE_ARRAY_MEMBER];
	  } *req = xmalloc (FLEXSIZEOF (struct req, str,
					hostlen + 1 + portstringlen + 1));
	  dns_request = &req->gaicb;
	  dns_request->ar_name = req->str;
	  dns_request->ar_service = req->str + hostlen + 1;
	  dns_request->ar_request = &req->hints;
	  dns_request->ar_result = NULL;
	  memset (&req->hints, 0, sizeof req->hints);
	  req->hints.ai_family = family;
	  req->hints.ai_socktype = socktype;
	  strcpy (req->str, SSDATA (host));
	  strcpy (req->str + hostlen + 1, portstring);

	  int ret = getaddrinfo_a (GAI_NOWAIT, &dns_request, 1, NULL);
	  if (ret)
	    error ("%s/%s getaddrinfo_a error %d",
		   SSDATA (host), portstring, ret);

	  goto open_socket;
	}
#endif /* HAVE_GETADDRINFO_A */
    }

  /* If we have a host, use getaddrinfo to resolve both host and service.
     Otherwise, use getservbyname to lookup the service.  */

  if (!NILP (host))
    {
      struct addrinfo *res, *lres;
      int ret;

      maybe_quit ();

      struct addrinfo hints;
      memset (&hints, 0, sizeof hints);
      hints.ai_family = family;
      hints.ai_socktype = socktype;

      ret = getaddrinfo (SSDATA (host), portstring, &hints, &res);
      if (ret)
#ifdef HAVE_GAI_STRERROR
	{
	  synchronize_system_messages_locale ();
	  char const *str = gai_strerror (ret);
	  if (! NILP (Vlocale_coding_system))
	    str = SSDATA (code_convert_string_norecord
			  (build_string (str), Vlocale_coding_system, 0));
	  error ("%s/%s %s", SSDATA (host), portstring, str);
	}
#else
	error ("%s/%s getaddrinfo error %d", SSDATA (host), portstring, ret);
#endif

      for (lres = res; lres; lres = lres->ai_next)
	addrinfos = Fcons (conv_addrinfo_to_lisp (lres), addrinfos);

      addrinfos = Fnreverse (addrinfos);

      freeaddrinfo (res);

      goto open_socket;
    }

  /* No hostname has been specified (e.g., a local server process).  */

  if (EQ (service, Qt))
    port = 0;
  else if (INTEGERP (service))
    port = XINT (service);
  else
    {
      CHECK_STRING (service);

      port = -1;
      if (SBYTES (service) != 0)
	{
	  /* Allow the service to be a string containing the port number,
	     because that's allowed if you have getaddrbyname.  */
	  char *service_end;
	  long int lport = strtol (SSDATA (service), &service_end, 10);
	  if (service_end == SSDATA (service) + SBYTES (service))
	    port = lport;
	  else
	    {
	      struct servent *svc_info
		= getservbyname (SSDATA (service),
				 socktype == SOCK_DGRAM ? "udp" : "tcp");
	      if (svc_info)
		port = ntohs (svc_info->s_port);
	    }
	}
    }

  if (! (0 <= port && port < 1 << 16))
    {
      AUTO_STRING (unknown_service, "Unknown service: %s");
      xsignal1 (Qerror, CALLN (Fformat, unknown_service, service));
    }

 open_socket:

  if (!NILP (buffer))
    buffer = Fget_buffer_create (buffer);

  /* Unwind bind_polling_period.  */
  unbind_to (count, Qnil);

  proc = make_process (name);
  record_unwind_protect (remove_process, proc);
  p = XPROCESS (proc);
  pset_childp (p, contact);
  pset_plist (p, Fcopy_sequence (Fplist_get (contact, QCplist)));
  pset_type (p, Qnetwork);

  pset_buffer (p, buffer);
  pset_sentinel (p, sentinel);
  pset_filter (p, filter);
  pset_log (p, Fplist_get (contact, QClog));
  if (tem = Fplist_get (contact, QCnoquery), !NILP (tem))
    p->kill_without_query = 1;
  if ((tem = Fplist_get (contact, QCstop), !NILP (tem)))
    pset_command (p, Qt);
  eassert (p->pid == 0);
  p->backlog = 5;
  eassert (! p->is_non_blocking_client);
  eassert (! p->is_server);
  p->port = port;
  p->socktype = socktype;
#ifdef HAVE_GETADDRINFO_A
  eassert (! p->dns_request);
#endif
#ifdef HAVE_GNUTLS
  tem = Fplist_get (contact, QCtls_parameters);
  CHECK_LIST (tem);
  p->gnutls_boot_parameters = tem;
#endif

  set_network_socket_coding_system (proc, host, service, name);

  /* :server BOOL */
  tem = Fplist_get (contact, QCserver);
  if (!NILP (tem))
    {
      /* Don't support network sockets when non-blocking mode is
	 not available, since a blocked Emacs is not useful.  */
      p->is_server = true;
      if (TYPE_RANGED_INTEGERP (int, tem))
	p->backlog = XINT (tem);
    }

  /* :nowait BOOL */
  if (!p->is_server && socktype != SOCK_DGRAM
      && !NILP (Fplist_get (contact, QCnowait)))
    p->is_non_blocking_client = true;

  bool postpone_connection = false;
#ifdef HAVE_GETADDRINFO_A
  /* With async address resolution, the list of addresses is empty, so
     postpone connecting to the server. */
  if (!p->is_server && NILP (addrinfos))
    {
      p->dns_request = dns_request;
      p->status = list1 (Qconnect);
      postpone_connection = true;
    }
#endif
  if (! postpone_connection)
    connect_network_socket (proc, addrinfos, use_external_socket_p);

  specpdl_ptr = specpdl + count;
  return proc;
}

\f
#ifdef HAVE_NET_IF_H

#ifdef SIOCGIFCONF
static Lisp_Object
network_interface_list (void)
{
  struct ifconf ifconf;
  struct ifreq *ifreq;
  void *buf = NULL;
  ptrdiff_t buf_size = 512;
  int s;
  Lisp_Object res;
  ptrdiff_t count;

  s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  if (s < 0)
    return Qnil;
  count = SPECPDL_INDEX ();
  record_unwind_protect_int (close_file_unwind, s);

  do
    {
      buf = xpalloc (buf, &buf_size, 1, INT_MAX, 1);
      ifconf.ifc_buf = buf;
      ifconf.ifc_len = buf_size;
      if (ioctl (s, SIOCGIFCONF, &ifconf))
	{
	  emacs_close (s);
	  xfree (buf);
	  return Qnil;
	}
    }
  while (ifconf.ifc_len == buf_size);

  res = unbind_to (count, Qnil);
  ifreq = ifconf.ifc_req;
  while ((char *) ifreq < (char *) ifconf.ifc_req + ifconf.ifc_len)
    {
      struct ifreq *ifq = ifreq;
#ifdef HAVE_STRUCT_IFREQ_IFR_ADDR_SA_LEN
#define SIZEOF_IFREQ(sif)						\
      ((sif)->ifr_addr.sa_len < sizeof (struct sockaddr)		\
       ? sizeof (*(sif)) : sizeof ((sif)->ifr_name) + (sif)->ifr_addr.sa_len)

      int len = SIZEOF_IFREQ (ifq);
#else
      int len = sizeof (*ifreq);
#endif
      char namebuf[sizeof (ifq->ifr_name) + 1];
      ifreq = (struct ifreq *) ((char *) ifreq + len);

      if (ifq->ifr_addr.sa_family != AF_INET)
	continue;

      memcpy (namebuf, ifq->ifr_name, sizeof (ifq->ifr_name));
      namebuf[sizeof (ifq->ifr_name)] = 0;
      res = Fcons (Fcons (build_string (namebuf),
			  conv_sockaddr_to_lisp (&ifq->ifr_addr,
						 sizeof (struct sockaddr))),
		   res);
    }

  xfree (buf);
  return res;
}
#endif /* SIOCGIFCONF */

#if defined (SIOCGIFADDR) || defined (SIOCGIFHWADDR) || defined (SIOCGIFFLAGS)

struct ifflag_def {
  int flag_bit;
  const char *flag_sym;
};

static const struct ifflag_def ifflag_table[] = {
#ifdef IFF_UP
  { IFF_UP,		"up" },
#endif
#ifdef IFF_BROADCAST
  { IFF_BROADCAST,	"broadcast" },
#endif
#ifdef IFF_DEBUG
  { IFF_DEBUG,		"debug" },
#endif
#ifdef IFF_LOOPBACK
  { IFF_LOOPBACK,	"loopback" },
#endif
#ifdef IFF_POINTOPOINT
  { IFF_POINTOPOINT,	"pointopoint" },
#endif
#ifdef IFF_RUNNING
  { IFF_RUNNING,	"running" },
#endif
#ifdef IFF_NOARP
  { IFF_NOARP,		"noarp" },
#endif
#ifdef IFF_PROMISC
  { IFF_PROMISC,	"promisc" },
#endif
#ifdef IFF_NOTRAILERS
#ifdef NS_IMPL_COCOA
  /* Really means smart, notrailers is obsolete.  */
  { IFF_NOTRAILERS,	"smart" },
#else
  { IFF_NOTRAILERS,	"notrailers" },
#endif
#endif
#ifdef IFF_ALLMULTI
  { IFF_ALLMULTI,	"allmulti" },
#endif
#ifdef IFF_MASTER
  { IFF_MASTER,		"master" },
#endif
#ifdef IFF_SLAVE
  { IFF_SLAVE,		"slave" },
#endif
#ifdef IFF_MULTICAST
  { IFF_MULTICAST,	"multicast" },
#endif
#ifdef IFF_PORTSEL
  { IFF_PORTSEL,	"portsel" },
#endif
#ifdef IFF_AUTOMEDIA
  { IFF_AUTOMEDIA,	"automedia" },
#endif
#ifdef IFF_DYNAMIC
  { IFF_DYNAMIC,	"dynamic" },
#endif
#ifdef IFF_OACTIVE
  { IFF_OACTIVE,	"oactive" }, /* OpenBSD: transmission in progress.  */
#endif
#ifdef IFF_SIMPLEX
  { IFF_SIMPLEX,	"simplex" }, /* OpenBSD: can't hear own transmissions.  */
#endif
#ifdef IFF_LINK0
  { IFF_LINK0,		"link0" }, /* OpenBSD: per link layer defined bit.  */
#endif
#ifdef IFF_LINK1
  { IFF_LINK1,		"link1" }, /* OpenBSD: per link layer defined bit.  */
#endif
#ifdef IFF_LINK2
  { IFF_LINK2,		"link2" }, /* OpenBSD: per link layer defined bit.  */
#endif
  { 0, 0 }
};

static Lisp_Object
network_interface_info (Lisp_Object ifname)
{
  struct ifreq rq;
  Lisp_Object res = Qnil;
  Lisp_Object elt;
  int s;
  bool any = 0;
  ptrdiff_t count;
#if (! (defined SIOCGIFHWADDR && defined HAVE_STRUCT_IFREQ_IFR_HWADDR)	\
     && defined HAVE_GETIFADDRS && defined LLADDR)
  struct ifaddrs *ifap;
#endif

  CHECK_STRING (ifname);

  if (sizeof rq.ifr_name <= SBYTES (ifname))
    error ("interface name too long");
  lispstpcpy (rq.ifr_name, ifname);

  s = socket (AF_INET, SOCK_STREAM | SOCK_CLOEXEC, 0);
  if (s < 0)
    return Qnil;
  count = SPECPDL_INDEX ();
  record_unwind_protect_int (close_file_unwind, s);

  elt = Qnil;
#if defined (SIOCGIFFLAGS) && defined (HAVE_STRUCT_IFREQ_IFR_FLAGS)
  if (ioctl (s, SIOCGIFFLAGS, &rq) == 0)
    {
      int flags = rq.ifr_flags;
      const struct ifflag_def *fp;
      int fnum;

      /* If flags is smaller than int (i.e. short) it may have the high bit set
         due to IFF_MULTICAST.  In that case, sign extending it into
         an int is wrong.  */
      if (flags < 0 && sizeof (rq.ifr_flags) < sizeof (flags))
        flags = (unsigned short) rq.ifr_flags;

      any = 1;
      for (fp = ifflag_table; flags != 0 && fp->flag_sym; fp++)
	{
	  if (flags & fp->flag_bit)
	    {
	      elt = Fcons (intern (fp->flag_sym), elt);
	      flags -= fp->flag_bit;
	    }
	}
      for (fnum = 0; flags && fnum < 32; flags >>= 1, fnum++)
	{
	  if (flags & 1)
	    {
	      elt = Fcons (make_number (fnum), elt);
	    }
	}
    }
#endif
  res = Fcons (elt, res);

  elt = Qnil;
#if defined (SIOCGIFHWADDR) && defined (HAVE_STRUCT_IFREQ_IFR_HWADDR)
  if (ioctl (s, SIOCGIFHWADDR, &rq) == 0)
    {
      Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil);
      register struct Lisp_Vector *p = XVECTOR (hwaddr);
      int n;

      any = 1;
      for (n = 0; n < 6; n++)
	p->contents[n] = make_number (((unsigned char *)
				       &rq.ifr_hwaddr.sa_data[0])
				      [n]);
      elt = Fcons (make_number (rq.ifr_hwaddr.sa_family), hwaddr);
    }
#elif defined (HAVE_GETIFADDRS) && defined (LLADDR)
  if (getifaddrs (&ifap) != -1)
    {
      Lisp_Object hwaddr = Fmake_vector (make_number (6), Qnil);
      register struct Lisp_Vector *p = XVECTOR (hwaddr);
      struct ifaddrs *it;

      for (it = ifap; it != NULL; it = it->ifa_next)
        {
	  DECLARE_POINTER_ALIAS (sdl, struct sockaddr_dl, it->ifa_addr);
          unsigned char linkaddr[6];
          int n;

          if (it->ifa_addr->sa_family != AF_LINK
              || strcmp (it->ifa_name, SSDATA (ifname)) != 0
              || sdl->sdl_alen != 6)
            continue;

          memcpy (linkaddr, LLADDR (sdl), sdl->sdl_alen);
          for (n = 0; n < 6; n++)
            p->contents[n] = make_number (linkaddr[n]);

          elt = Fcons (make_number (it->ifa_addr->sa_family), hwaddr);
          break;
        }
    }
#ifdef HAVE_FREEIFADDRS
  freeifaddrs (ifap);
#endif

#endif /* HAVE_GETIFADDRS && LLADDR */

  res = Fcons (elt, res);

  elt = Qnil;
#if defined (SIOCGIFNETMASK) && (defined (HAVE_STRUCT_IFREQ_IFR_NETMASK) || defined (HAVE_STRUCT_IFREQ_IFR_ADDR))
  if (ioctl (s, SIOCGIFNETMASK, &rq) == 0)
    {
      any = 1;
#ifdef HAVE_STRUCT_IFREQ_IFR_NETMASK
      elt = conv_sockaddr_to_lisp (&rq.ifr_netmask, sizeof (rq.ifr_netmask));
#else
      elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
#endif
    }
#endif
  res = Fcons (elt, res);

  elt = Qnil;
#if defined (SIOCGIFBRDADDR) && defined (HAVE_STRUCT_IFREQ_IFR_BROADADDR)
  if (ioctl (s, SIOCGIFBRDADDR, &rq) == 0)
    {
      any = 1;
      elt = conv_sockaddr_to_lisp (&rq.ifr_broadaddr, sizeof (rq.ifr_broadaddr));
    }
#endif
  res = Fcons (elt, res);

  elt = Qnil;
#if defined (SIOCGIFADDR) && defined (HAVE_STRUCT_IFREQ_IFR_ADDR)
  if (ioctl (s, SIOCGIFADDR, &rq) == 0)
    {
      any = 1;
      elt = conv_sockaddr_to_lisp (&rq.ifr_addr, sizeof (rq.ifr_addr));
    }
#endif
  res = Fcons (elt, res);

  return unbind_to (count, any ? res : Qnil);
}
#endif	/* !SIOCGIFADDR && !SIOCGIFHWADDR && !SIOCGIFFLAGS */
#endif	/* defined (HAVE_NET_IF_H) */

DEFUN ("network-interface-list", Fnetwork_interface_list,
       Snetwork_interface_list, 0, 0, 0,
       doc: /* Return an alist of all network interfaces and their network address.
Each element is a cons, the car of which is a string containing the
interface name, and the cdr is the network address in internal
format; see the description of ADDRESS in `make-network-process'.

If the information is not available, return nil.  */)
  (void)
{
#if (defined HAVE_NET_IF_H && defined SIOCGIFCONF) || defined WINDOWSNT
  return network_interface_list ();
#else
  return Qnil;
#endif
}

DEFUN ("network-interface-info", Fnetwork_interface_info,
       Snetwork_interface_info, 1, 1, 0,
       doc: /* Return information about network interface named IFNAME.
The return value is a list (ADDR BCAST NETMASK HWADDR FLAGS),
where ADDR is the layer 3 address, BCAST is the layer 3 broadcast address,
NETMASK is the layer 3 network mask, HWADDR is the layer 2 address, and
FLAGS is the current flags of the interface.

Data that is unavailable is returned as nil.  */)
  (Lisp_Object ifname)
{
#if ((defined HAVE_NET_IF_H			       \
      && (defined SIOCGIFADDR || defined SIOCGIFHWADDR \
	  || defined SIOCGIFFLAGS))		       \
     || defined WINDOWSNT)
  return network_interface_info (ifname);
#else
  return Qnil;
#endif
}

/* Turn off input and output for process PROC.  */

static void
deactivate_process (Lisp_Object proc)
{
  int inchannel;
  struct Lisp_Process *p = XPROCESS (proc);
  int i;

#ifdef HAVE_GNUTLS
  /* Delete GnuTLS structures in PROC, if any.  */
  emacs_gnutls_deinit (proc);
#endif /* HAVE_GNUTLS */

  if (p->read_output_delay > 0)
    {
      if (--process_output_delay_count < 0)
	process_output_delay_count = 0;
      p->read_output_delay = 0;
      p->read_output_skip = 0;
    }

  /* Beware SIGCHLD hereabouts.  */

  for (i = 0; i < PROCESS_OPEN_FDS; i++)
    close_process_fd (&p->open_fd[i]);

  inchannel = p->infd;
  if (inchannel >= 0)
    {
      p->infd  = -1;
      p->outfd = -1;
#ifdef DATAGRAM_SOCKETS
      if (DATAGRAM_CHAN_P (inchannel))
	{
	  xfree (datagram_address[inchannel].sa);
	  datagram_address[inchannel].sa = 0;
	  datagram_address[inchannel].len = 0;
	}
#endif
      chan_process[inchannel] = Qnil;
      delete_read_fd (inchannel);
      if ((fd_callback_info[inchannel].flags & NON_BLOCKING_CONNECT_FD) != 0)
	delete_write_fd (inchannel);
      if (inchannel == max_desc)
	recompute_max_desc ();
    }
}

\f
DEFUN ("accept-process-output", Faccept_process_output, Saccept_process_output,
       0, 4, 0,
       doc: /* Allow any pending output from subprocesses to be read by Emacs.
It is given to their filter functions.
Optional argument PROCESS means do not return until output has been
received from PROCESS.

Optional second argument SECONDS and third argument MILLISEC
specify a timeout; return after that much time even if there is
no subprocess output.  If SECONDS is a floating point number,
it specifies a fractional number of seconds to wait.
The MILLISEC argument is obsolete and should be avoided.

If optional fourth argument JUST-THIS-ONE is non-nil, accept output
from PROCESS only, suspending reading output from other processes.
If JUST-THIS-ONE is an integer, don't run any timers either.
Return non-nil if we received any output from PROCESS (or, if PROCESS
is nil, from any process) before the timeout expired.  */)
  (Lisp_Object process, Lisp_Object seconds, Lisp_Object millisec,
   Lisp_Object just_this_one)
{
  intmax_t secs;
  int nsecs;

  if (! NILP (process))
    {
      CHECK_PROCESS (process);
      struct Lisp_Process *proc = XPROCESS (process);

      /* Can't wait for a process that is dedicated to a different
	 thread.  */
      if (!EQ (proc->thread, Qnil) && !EQ (proc->thread, Fcurrent_thread ()))
	{
	  Lisp_Object proc_thread_name = XTHREAD (proc->thread)->name;

	  if (STRINGP (proc_thread_name))
	    error ("Attempt to accept output from process %s locked to thread %s",
		   SDATA (proc->name), SDATA (proc_thread_name));
	  else
	    error ("Attempt to accept output from process %s locked to thread %p",
		   SDATA (proc->name), XTHREAD (proc->thread));
	}
    }
  else
    just_this_one = Qnil;

  if (!NILP (millisec))
    { /* Obsolete calling convention using integers rather than floats.  */
      CHECK_NUMBER (millisec);
      if (NILP (seconds))
	seconds = make_float (XINT (millisec) / 1000.0);
      else
	{
	  CHECK_NUMBER (seconds);
	  seconds = make_float (XINT (millisec) / 1000.0 + XINT (seconds));
	}
    }

  secs = 0;
  nsecs = -1;

  if (!NILP (seconds))
    {
      if (INTEGERP (seconds))
	{
	  if (XINT (seconds) > 0)
	    {
	      secs = XINT (seconds);
	      nsecs = 0;
	    }
	}
      else if (FLOATP (seconds))
	{
	  if (XFLOAT_DATA (seconds) > 0)
	    {
	      struct timespec t = dtotimespec (XFLOAT_DATA (seconds));
	      secs = min (t.tv_sec, WAIT_READING_MAX);
	      nsecs = t.tv_nsec;
	    }
	}
      else
	wrong_type_argument (Qnumberp, seconds);
    }
  else if (! NILP (process))
    nsecs = 0;

  return
    ((wait_reading_process_output (secs, nsecs, 0, 0,
				   Qnil,
				   !NILP (process) ? XPROCESS (process) : NULL,
				   (NILP (just_this_one) ? 0
				    : !INTEGERP (just_this_one) ? 1 : -1))
      <= 0)
     ? Qnil : Qt);
}

/* Accept a connection for server process SERVER on CHANNEL.  */

static EMACS_INT connect_counter = 0;

static void
server_accept_connection (Lisp_Object server, int channel)
{
  Lisp_Object buffer;
  Lisp_Object contact, host, service;
  struct Lisp_Process *ps = XPROCESS (server);
  struct Lisp_Process *p;
  int s;
  union u_sockaddr saddr;
  socklen_t len = sizeof saddr;
  ptrdiff_t count;

  s = accept4 (channel, &saddr.sa, &len, SOCK_CLOEXEC);

  if (s < 0)
    {
      int code = errno;
      if (!would_block (code) && !NILP (ps->log))
	call3 (ps->log, server, Qnil,
	       concat3 (build_string ("accept failed with code"),
			Fnumber_to_string (make_number (code)),
			build_string ("\n")));
      return;
    }

  count = SPECPDL_INDEX ();
  record_unwind_protect_int (close_file_unwind, s);

  connect_counter++;

  /* Setup a new process to handle the connection.  */

  /* Generate a unique identification of the caller, and build contact
     information for this process.  */
  host = Qt;
  service = Qnil;
  Lisp_Object args[11];
  int nargs = 0;
  AUTO_STRING (procname_format_in, "%s <%d.%d.%d.%d:%d>");
  AUTO_STRING (procname_format_in6, "%s <[%x:%x:%x:%x:%x:%x:%x:%x]:%d>");
  AUTO_STRING (procname_format_default, "%s <%d>");
  switch (saddr.sa.sa_family)
    {
    case AF_INET:
      {
	args[nargs++] = procname_format_in;
	nargs++;
	unsigned char *ip = (unsigned char *)&saddr.in.sin_addr.s_addr;
	service = make_number (ntohs (saddr.in.sin_port));
	for (int i = 0; i < 4; i++)
	  args[nargs++] = make_number (ip[i]);
	args[nargs++] = service;
      }
      break;

#ifdef AF_INET6
    case AF_INET6:
      {
	args[nargs++] = procname_format_in6;
	nargs++;
	DECLARE_POINTER_ALIAS (ip6, uint16_t, &saddr.in6.sin6_addr);
	service = make_number (ntohs (saddr.in.sin_port));
	for (int i = 0; i < 8; i++)
	  args[nargs++] = make_number (ip6[i]);
	args[nargs++] = service;
      }
      break;
#endif

    default:
      args[nargs++] = procname_format_default;
      nargs++;
      args[nargs++] = make_number (connect_counter);
      break;
    }

  /* Create a new buffer name for this process if it doesn't have a
     filter.  The new buffer name is based on the buffer name or
     process name of the server process concatenated with the caller
     identification.  */

  if (!(EQ (ps->filter, Qinternal_default_process_filter)
	|| EQ (ps->filter, Qt)))
    buffer = Qnil;
  else
    {
      buffer = ps->buffer;
      if (!NILP (buffer))
	buffer = Fbuffer_name (buffer);
      else
	buffer = ps->name;
      if (!NILP (buffer))
	{
	  args[1] = buffer;
	  buffer = Fget_buffer_create (Fformat (nargs, args));
	}
    }

  /* Generate a unique name for the new server process.  Combine the
     server process name with the caller identification.  */

  args[1] = ps->name;
  Lisp_Object name = Fformat (nargs, args);
  Lisp_Object proc = make_process (name);

  chan_process[s] = proc;

  fcntl (s, F_SETFL, O_NONBLOCK);

  p = XPROCESS (proc);

  /* Build new contact information for this setup.  */
  contact = Fcopy_sequence (ps->childp);
  contact = Fplist_put (contact, QCserver, Qnil);
  contact = Fplist_put (contact, QChost, host);
  if (!NILP (service))
    contact = Fplist_put (contact, QCservice, service);
  contact = Fplist_put (contact, QCremote,
			conv_sockaddr_to_lisp (&saddr.sa, len));
#ifdef HAVE_GETSOCKNAME
  len = sizeof saddr;
  if (getsockname (s, &saddr.sa, &len) == 0)
    contact = Fplist_put (contact, QClocal,
			  conv_sockaddr_to_lisp (&saddr.sa, len));
#endif

  pset_childp (p, contact);
  pset_plist (p, Fcopy_sequence (ps->plist));
  pset_type (p, Qnetwork);

  pset_buffer (p, buffer);
  pset_sentinel (p, ps->sentinel);
  pset_filter (p, ps->filter);
  eassert (NILP (p->command));
  eassert (p->pid == 0);

  /* Discard the unwind protect for closing S.  */
  specpdl_ptr = specpdl + count;

  p->open_fd[SUBPROCESS_STDIN] = s;
  p->infd  = s;
  p->outfd = s;
  pset_status (p, Qrun);

  /* Client processes for accepted connections are not stopped initially.  */
  if (!EQ (p->filter, Qt))
    add_process_read_fd (s);
  if (s > max_desc)
    max_desc = s;

  /* Setup coding system for new process based on server process.
     This seems to be the proper thing to do, as the coding system
     of the new process should reflect the settings at the time the
     server socket was opened; not the current settings.  */

  pset_decode_coding_system (p, ps->decode_coding_system);
  pset_encode_coding_system (p, ps->encode_coding_system);
  setup_process_coding_systems (proc);

  pset_decoding_buf (p, empty_unibyte_string);
  eassert (p->decoding_carryover == 0);
  pset_encoding_buf (p, empty_unibyte_string);

  p->inherit_coding_system_flag
    = (NILP (buffer) ? 0 : ps->inherit_coding_system_flag);

  AUTO_STRING (dash, "-");
  AUTO_STRING (nl, "\n");
  Lisp_Object host_string = STRINGP (host) ? host : dash;

  if (!NILP (ps->log))
    {
      AUTO_STRING (accept_from, "accept from ");
      call3 (ps->log, server, proc, concat3 (accept_from, host_string, nl));
    }

  AUTO_STRING (open_from, "open from ");
  exec_sentinel (proc, concat3 (open_from, host_string, nl));
}

#ifdef HAVE_GETADDRINFO_A
static Lisp_Object
check_for_dns (Lisp_Object proc)
{
  struct Lisp_Process *p = XPROCESS (proc);
  Lisp_Object addrinfos = Qnil;

  /* Sanity check. */
  if (! p->dns_request)
    return Qnil;

  int ret = gai_error (p->dns_request);
  if (ret == EAI_INPROGRESS)
    return Qt;

  /* We got a response. */
  if (ret == 0)
    {
      struct addrinfo *res;

      for (res = p->dns_request->ar_result; res; res = res->ai_next)
	addrinfos = Fcons (conv_addrinfo_to_lisp (res), addrinfos);

      addrinfos = Fnreverse (addrinfos);
    }
  /* The DNS lookup failed. */
  else if (connecting_status (p->status))
    {
      deactivate_process (proc);
      pset_status (p, (list2
		       (Qfailed,
			concat3 (build_string ("Name lookup of "),
				 build_string (p->dns_request->ar_name),
				 build_string (" failed")))));
    }

  free_dns_request (proc);

  /* This process should not already be connected (or killed). */
  if (! connecting_status (p->status))
    return Qnil;

  return addrinfos;
}

#endif /* HAVE_GETADDRINFO_A */

static void
wait_for_socket_fds (Lisp_Object process, char const *name)
{
  while (XPROCESS (process)->infd < 0
	 && connecting_status (XPROCESS (process)->status))
    {
      add_to_log ("Waiting for socket from %s...", build_string (name));
      wait_reading_process_output (0, 20 * 1000 * 1000, 0, 0, Qnil, NULL, 0);
    }
}

static void
wait_while_connecting (Lisp_Object process)
{
  while (connecting_status (XPROCESS (process)->status))
    {
      add_to_log ("Waiting for connection...");
      wait_reading_process_output (0, 20 * 1000 * 1000, 0, 0, Qnil, NULL, 0);
    }
}

static void
wait_for_tls_negotiation (Lisp_Object process)
{
#ifdef HAVE_GNUTLS
  while (XPROCESS (process)->gnutls_p
	 && XPROCESS (process)->gnutls_initstage != GNUTLS_STAGE_READY)
    {
      add_to_log ("Waiting for TLS...");
      wait_reading_process_output (0, 20 * 1000 * 1000, 0, 0, Qnil, NULL, 0);
    }
#endif
}

static void
wait_reading_process_output_unwind (int data)
{
  clear_waiting_thread_info ();
  waiting_for_user_input_p = data;
}

/* This is here so breakpoints can be put on it.  */
static void
wait_reading_process_output_1 (void)
{
}

/* Read and dispose of subprocess output while waiting for timeout to
   elapse and/or keyboard input to be available.

   TIME_LIMIT is:
     timeout in seconds
     If negative, gobble data immediately available but don't wait for any.

   NSECS is:
     an additional duration to wait, measured in nanoseconds
     If TIME_LIMIT is zero, then:
       If NSECS == 0, there is no limit.
       If NSECS > 0, the timeout consists of NSECS only.
       If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.

   READ_KBD is:
     0 to ignore keyboard input, or
     1 to return when input is available, or
    -1 meaning caller will actually read the input, so don't throw to
       the quit handler

   DO_DISPLAY means redisplay should be done to show subprocess
     output that arrives.

   If WAIT_FOR_CELL is a cons cell, wait until its car is non-nil
     (and gobble terminal input into the buffer if any arrives).

   If WAIT_PROC is specified, wait until something arrives from that
     process.

   If JUST_WAIT_PROC is nonzero, handle only output from WAIT_PROC
     (suspending output from other processes).  A negative value
     means don't run any timers either.

   Return positive if we received input from WAIT_PROC (or from any
   process if WAIT_PROC is null), zero if we attempted to receive
   input but got none, and negative if we didn't even try.  */

int
wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
			     bool do_display,
			     Lisp_Object wait_for_cell,
			     struct Lisp_Process *wait_proc, int just_wait_proc)
{
  int channel, nfds;
  fd_set Available;
  fd_set Writeok;
  bool check_write;
  int check_delay;
  bool no_avail;
  int xerrno;
  Lisp_Object proc;
  struct timespec timeout, end_time, timer_delay;
  struct timespec got_output_end_time = invalid_timespec ();
  enum { MINIMUM = -1, TIMEOUT, INFINITY } wait;
  int got_some_output = -1;
  uintmax_t prev_wait_proc_nbytes_read = wait_proc ? wait_proc->nbytes_read : 0;
#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS
  bool retry_for_async;
#endif
  ptrdiff_t count = SPECPDL_INDEX ();

  /* Close to the current time if known, an invalid timespec otherwise.  */
  struct timespec now = invalid_timespec ();

  eassert (wait_proc == NULL
	   || EQ (wait_proc->thread, Qnil)
	   || XTHREAD (wait_proc->thread) == current_thread);

  FD_ZERO (&Available);
  FD_ZERO (&Writeok);

  if (time_limit == 0 && nsecs == 0 && wait_proc && !NILP (Vinhibit_quit)
      && !(CONSP (wait_proc->status)
	   && EQ (XCAR (wait_proc->status), Qexit)))
    message1 ("Blocking call to accept-process-output with quit inhibited!!");

  record_unwind_protect_int (wait_reading_process_output_unwind,
			     waiting_for_user_input_p);
  waiting_for_user_input_p = read_kbd;

  if (TYPE_MAXIMUM (time_t) < time_limit)
    time_limit = TYPE_MAXIMUM (time_t);

  if (time_limit < 0 || nsecs < 0)
    wait = MINIMUM;
  else if (time_limit > 0 || nsecs > 0)
    {
      wait = TIMEOUT;
      now = current_timespec ();
      end_time = timespec_add (now, make_timespec (time_limit, nsecs));
    }
  else
    wait = INFINITY;

  while (1)
    {
      bool process_skipped = false;

      /* If calling from keyboard input, do not quit
	 since we want to return C-g as an input character.
	 Otherwise, do pending quit if requested.  */
      if (read_kbd >= 0)
	maybe_quit ();
      else if (pending_signals)
	process_pending_signals ();

      /* Exit now if the cell we're waiting for became non-nil.  */
      if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
	break;

#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS
      {
	Lisp_Object process_list_head, aproc;
	struct Lisp_Process *p;

	retry_for_async = false;
	FOR_EACH_PROCESS(process_list_head, aproc)
	  {
	    p = XPROCESS (aproc);

	    if (! wait_proc || p == wait_proc)
	      {
#ifdef HAVE_GETADDRINFO_A
		/* Check for pending DNS requests. */
		if (p->dns_request)
		  {
		    Lisp_Object addrinfos = check_for_dns (aproc);
		    if (!NILP (addrinfos) && !EQ (addrinfos, Qt))
		      connect_network_socket (aproc, addrinfos, Qnil);
		    else
		      retry_for_async = true;
		  }
#endif
#ifdef HAVE_GNUTLS
		/* Continue TLS negotiation. */
		if (p->gnutls_initstage == GNUTLS_STAGE_HANDSHAKE_TRIED
		    && p->is_non_blocking_client)
		  {
		    gnutls_try_handshake (p);
		    p->gnutls_handshakes_tried++;

		    if (p->gnutls_initstage == GNUTLS_STAGE_READY)
		      {
			gnutls_verify_boot (aproc, Qnil);
			finish_after_tls_connection (aproc);
		      }
		    else
		      {
			retry_for_async = true;
			if (p->gnutls_handshakes_tried
			    > GNUTLS_EMACS_HANDSHAKES_LIMIT)
			  {
			    deactivate_process (aproc);
			    pset_status (p, list2 (Qfailed,
						   build_string ("TLS negotiation failed")));
			  }
		      }
		  }
#endif
	      }
	  }
      }
#endif /* GETADDRINFO_A or GNUTLS */

      /* Compute time from now till when time limit is up.  */
      /* Exit if already run out.  */
      if (wait == TIMEOUT)
	{
	  if (!timespec_valid_p (now))
	    now = current_timespec ();
	  if (timespec_cmp (end_time, now) <= 0)
	    break;
	  timeout = timespec_sub (end_time, now);
	}
      else
	timeout = make_timespec (wait < TIMEOUT ? 0 : 100000, 0);

      /* Normally we run timers here.
	 But not if wait_for_cell; in those cases,
	 the wait is supposed to be short,
	 and those callers cannot handle running arbitrary Lisp code here.  */
      if (NILP (wait_for_cell)
	  && just_wait_proc >= 0)
	{
	  do
	    {
	      unsigned old_timers_run = timers_run;
	      struct buffer *old_buffer = current_buffer;
	      Lisp_Object old_window = selected_window;

	      timer_delay = timer_check ();

	      /* If a timer has run, this might have changed buffers
		 an alike.  Make read_key_sequence aware of that.  */
	      if (timers_run != old_timers_run
		  && (old_buffer != current_buffer
		      || !EQ (old_window, selected_window))
		  && waiting_for_user_input_p == -1)
		record_asynch_buffer_change ();

	      if (timers_run != old_timers_run && do_display)
		/* We must retry, since a timer may have requeued itself
		   and that could alter the time_delay.  */
		redisplay_preserve_echo_area (9);
	      else
		break;
	    }
	  while (!detect_input_pending ());

	  /* If there is unread keyboard input, also return.  */
	  if (read_kbd != 0
	      && requeued_events_pending_p ())
	    break;

          /* This is so a breakpoint can be put here.  */
          if (!timespec_valid_p (timer_delay))
              wait_reading_process_output_1 ();
        }

      /* Cause C-g and alarm signals to take immediate action,
	 and cause input available signals to zero out timeout.

	 It is important that we do this before checking for process
	 activity.  If we get a SIGCHLD after the explicit checks for
	 process activity, timeout is the only way we will know.  */
      if (read_kbd < 0)
	set_waiting_for_input (&timeout);

      /* If status of something has changed, and no input is
	 available, notify the user of the change right away.  After
	 this explicit check, we'll let the SIGCHLD handler zap
	 timeout to get our attention.  */
      if (update_tick != process_tick)
	{
	  fd_set Atemp;
	  fd_set Ctemp;

          if (kbd_on_hold_p ())
            FD_ZERO (&Atemp);
          else
            compute_input_wait_mask (&Atemp);
	  compute_write_mask (&Ctemp);

	  timeout = make_timespec (0, 0);
	  if ((thread_select (pselect, max_desc + 1,
			      &Atemp,
			      (num_pending_connects > 0 ? &Ctemp : NULL),
			      NULL, &timeout, NULL)
	       <= 0))
	    {
	      /* It's okay for us to do this and then continue with
		 the loop, since timeout has already been zeroed out.  */
	      clear_waiting_for_input ();
	      got_some_output = status_notify (NULL, wait_proc);
	      if (do_display) redisplay_preserve_echo_area (13);
	    }
	}

      /* Don't wait for output from a non-running process.  Just
	 read whatever data has already been received.  */
      if (wait_proc && wait_proc->raw_status_new)
	update_status (wait_proc);
      if (wait_proc
	  && ! EQ (wait_proc->status, Qrun)
	  && ! connecting_status (wait_proc->status))
	{
	  bool read_some_bytes = false;

	  clear_waiting_for_input ();

	  /* If data can be read from the process, do so until exhausted.  */
	  if (wait_proc->infd >= 0)
	    {
	      XSETPROCESS (proc, wait_proc);

	      while (true)
		{
		  int nread = read_process_output (proc, wait_proc->infd);
		  if (nread < 0)
		    {
		      if (errno == EIO || would_block (errno))
			break;
		    }
		  else
		    {
		      if (got_some_output < nread)
			got_some_output = nread;
		      if (nread == 0)
			break;
		      read_some_bytes = true;
		    }
		}
	    }

	  if (read_some_bytes && do_display)
	    redisplay_preserve_echo_area (10);

	  break;
	}

      /* Wait till there is something to do.  */

      if (wait_proc && just_wait_proc)
	{
	  if (wait_proc->infd < 0)  /* Terminated.  */
	    break;
	  FD_SET (wait_proc->infd, &Available);
	  check_delay = 0;
          check_write = 0;
	}
      else if (!NILP (wait_for_cell))
	{
	  compute_non_process_wait_mask (&Available);
	  check_delay = 0;
	  check_write = 0;
	}
      else
	{
	  if (! read_kbd)
	    compute_non_keyboard_wait_mask (&Available);
	  else
	    compute_input_wait_mask (&Available);
	  compute_write_mask (&Writeok);
 	  check_delay = wait_proc ? 0 : process_output_delay_count;
	  check_write = true;
	}

      /* If frame size has changed or the window is newly mapped,
	 redisplay now, before we start to wait.  There is a race
	 condition here; if a SIGIO arrives between now and the select
	 and indicates that a frame is trashed, the select may block
	 displaying a trashed screen.  */
      if (frame_garbaged && do_display)
	{
	  clear_waiting_for_input ();
	  redisplay_preserve_echo_area (11);
	  if (read_kbd < 0)
	    set_waiting_for_input (&timeout);
	}

      /* Skip the `select' call if input is available and we're
	 waiting for keyboard input or a cell change (which can be
	 triggered by processing X events).  In the latter case, set
	 nfds to 1 to avoid breaking the loop.  */
      no_avail = 0;
      if ((read_kbd || !NILP (wait_for_cell))
	  && detect_input_pending ())
	{
	  nfds = read_kbd ? 0 : 1;
	  no_avail = 1;
	  FD_ZERO (&Available);
	}
      else
	{
	  /* Set the timeout for adaptive read buffering if any
	     process has non-zero read_output_skip and non-zero
	     read_output_delay, and we are not reading output for a
	     specific process.  It is not executed if
	     Vprocess_adaptive_read_buffering is nil.  */
	  if (process_output_skip && check_delay > 0)
	    {
	      int adaptive_nsecs = timeout.tv_nsec;
	      if (timeout.tv_sec > 0 || adaptive_nsecs > READ_OUTPUT_DELAY_MAX)
		adaptive_nsecs = READ_OUTPUT_DELAY_MAX;
	      for (channel = 0; check_delay > 0 && channel <= max_desc; channel++)
		{
		  proc = chan_process[channel];
		  if (NILP (proc))
		    continue;
		  /* Find minimum non-zero read_output_delay among the
		     processes with non-zero read_output_skip.  */
		  if (XPROCESS (proc)->read_output_delay > 0)
		    {
		      check_delay--;
		      if (!XPROCESS (proc)->read_output_skip)
			continue;
		      FD_CLR (channel, &Available);
		      process_skipped = true;
		      XPROCESS (proc)->read_output_skip = 0;
		      if (XPROCESS (proc)->read_output_delay < adaptive_nsecs)
			adaptive_nsecs = XPROCESS (proc)->read_output_delay;
		    }
		}
	      timeout = make_timespec (0, adaptive_nsecs);
	      process_output_skip = 0;
	    }

	  /* If we've got some output and haven't limited our timeout
	     with adaptive read buffering, limit it. */
	  if (got_some_output > 0 && !process_skipped
	      && (timeout.tv_sec
		  || timeout.tv_nsec > READ_OUTPUT_DELAY_INCREMENT))
	    timeout = make_timespec (0, READ_OUTPUT_DELAY_INCREMENT);


	  if (NILP (wait_for_cell) && just_wait_proc >= 0
	      && timespec_valid_p (timer_delay)
	      && timespec_cmp (timer_delay, timeout) < 0)
	    {
	      if (!timespec_valid_p (now))
		now = current_timespec ();
	      struct timespec timeout_abs = timespec_add (now, timeout);
	      if (!timespec_valid_p (got_output_end_time)
		  || timespec_cmp (timeout_abs, got_output_end_time) < 0)
		got_output_end_time = timeout_abs;
	      timeout = timer_delay;
	    }
	  else
	    got_output_end_time = invalid_timespec ();

	  /* NOW can become inaccurate if time can pass during pselect.  */
	  if (timeout.tv_sec > 0 || timeout.tv_nsec > 0)
	    now = invalid_timespec ();

#if defined HAVE_GETADDRINFO_A || defined HAVE_GNUTLS
	  if (retry_for_async
	      && (timeout.tv_sec > 0 || timeout.tv_nsec > ASYNC_RETRY_NSEC))
	    {
	      timeout.tv_sec = 0;
	      timeout.tv_nsec = ASYNC_RETRY_NSEC;
	    }
#endif

/* Non-macOS HAVE_GLIB builds call thread_select in xgselect.c.  */
#if defined HAVE_GLIB && !defined HAVE_NS
	  nfds = xg_select (max_desc + 1,
			    &Available, (check_write ? &Writeok : 0),
			    NULL, &timeout, NULL);
#elif defined HAVE_NS
          /* And NS builds call thread_select in ns_select. */
          nfds = ns_select (max_desc + 1,
			    &Available, (check_write ? &Writeok : 0),
			    NULL, &timeout, NULL);
#else  /* !HAVE_GLIB */
	  nfds = thread_select (pselect, max_desc + 1,
				&Available,
				(check_write ? &Writeok : 0),
				NULL, &timeout, NULL);
#endif	/* !HAVE_GLIB */

#ifdef HAVE_GNUTLS
          /* GnuTLS buffers data internally.  In lowat mode it leaves
             some data in the TCP buffers so that select works, but
             with custom pull/push functions we need to check if some
             data is available in the buffers manually.  */
          if (nfds == 0)
	    {
	      fd_set tls_available;
	      int set = 0;

	      FD_ZERO (&tls_available);
	      if (! wait_proc)
		{
		  /* We're not waiting on a specific process, so loop
		     through all the channels and check for data.
		     This is a workaround needed for some versions of
		     the gnutls library -- 2.12.14 has been confirmed
		     to need it.  See
		     http://comments.gmane.org/gmane.emacs.devel/145074 */
		  for (channel = 0; channel < FD_SETSIZE; ++channel)
		    if (! NILP (chan_process[channel]))
		      {
			struct Lisp_Process *p =
			  XPROCESS (chan_process[channel]);
			if (p && p->gnutls_p && p->gnutls_state
			    && ((emacs_gnutls_record_check_pending
				 (p->gnutls_state))
				> 0))
			  {
			    nfds++;
			    eassert (p->infd == channel);
			    FD_SET (p->infd, &tls_available);
			    set++;
			  }
		      }
		}
	      else
		{
		  /* Check this specific channel.  */
		  if (wait_proc->gnutls_p /* Check for valid process.  */
		      && wait_proc->gnutls_state
		      /* Do we have pending data?  */
		      && ((emacs_gnutls_record_check_pending
			   (wait_proc->gnutls_state))
			  > 0))
		    {
		      nfds = 1;
		      eassert (0 <= wait_proc->infd);
		      /* Set to Available.  */
		      FD_SET (wait_proc->infd, &tls_available);
		      set++;
		    }
		}
	      if (set)
		Available = tls_available;
	    }
#endif
	}

      xerrno = errno;

      /* Make C-g and alarm signals set flags again.  */
      clear_waiting_for_input ();

      /*  If we woke up due to SIGWINCH, actually change size now.  */
      do_pending_window_change (0);

      if (nfds == 0)
	{
          /* Exit the main loop if we've passed the requested timeout,
             or have read some bytes from our wait_proc (either directly
             in this call or indirectly through timers / process filters),
             or aren't skipping processes and got some output and
             haven't lowered our timeout due to timers or SIGIO and
             have waited a long amount of time due to repeated
             timers.  */
	  struct timespec huge_timespec
	    = make_timespec (TYPE_MAXIMUM (time_t), 2 * TIMESPEC_RESOLUTION);
	  struct timespec cmp_time = huge_timespec;
	  if (wait < TIMEOUT
              || (wait_proc
                  && wait_proc->nbytes_read != prev_wait_proc_nbytes_read))
	    break;
	  if (wait == TIMEOUT)
	    cmp_time = end_time;
	  if (!process_skipped && got_some_output > 0
	      && (timeout.tv_sec > 0 || timeout.tv_nsec > 0))
	    {
	      if (!timespec_valid_p (got_output_end_time))
		break;
	      if (timespec_cmp (got_output_end_time, cmp_time) < 0)
		cmp_time = got_output_end_time;
	    }
	  if (timespec_cmp (cmp_time, huge_timespec) < 0)
	    {
	      now = current_timespec ();
	      if (timespec_cmp (cmp_time, now) <= 0)
		break;
	    }
	}

      if (nfds < 0)
	{
	  if (xerrno == EINTR)
	    no_avail = 1;
	  else if (xerrno == EBADF)
	    emacs_abort ();
	  else
	    report_file_errno ("Failed select", Qnil, xerrno);
	}

      /* Check for keyboard input.  */
      /* If there is any, return immediately
	 to give it higher priority than subprocesses.  */

      if (read_kbd != 0)
	{
	  unsigned old_timers_run = timers_run;
	  struct buffer *old_buffer = current_buffer;
	  Lisp_Object old_window = selected_window;
	  bool leave = false;

	  if (detect_input_pending_run_timers (do_display))
	    {
	      swallow_events (do_display);
	      if (detect_input_pending_run_timers (do_display))
		leave = true;
	    }

	  /* If a timer has run, this might have changed buffers
	     an alike.  Make read_key_sequence aware of that.  */
	  if (timers_run != old_timers_run
	      && waiting_for_user_input_p == -1
	      && (old_buffer != current_buffer
	      || !EQ (old_window, selected_window)))
	    record_asynch_buffer_change ();

	  if (leave)
	    break;
	}

      /* If there is unread keyboard input, also return.  */
      if (read_kbd != 0
	  && requeued_events_pending_p ())
	break;

      /* If we are not checking for keyboard input now,
	 do process events (but don't run any timers).
	 This is so that X events will be processed.
	 Otherwise they may have to wait until polling takes place.
	 That would causes delays in pasting selections, for example.

	 (We used to do this only if wait_for_cell.)  */
      if (read_kbd == 0 && detect_input_pending ())
	{
	  swallow_events (do_display);
#if 0  /* Exiting when read_kbd doesn't request that seems wrong, though.  */
	  if (detect_input_pending ())
	    break;
#endif
	}

      /* Exit now if the cell we're waiting for became non-nil.  */
      if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
	break;

#ifdef USABLE_SIGIO
      /* If we think we have keyboard input waiting, but didn't get SIGIO,
	 go read it.  This can happen with X on BSD after logging out.
	 In that case, there really is no input and no SIGIO,
	 but select says there is input.  */

      if (read_kbd && interrupt_input
	  && keyboard_bit_set (&Available) && ! noninteractive)
	handle_input_available_signal (SIGIO);
#endif

      /* If checking input just got us a size-change event from X,
	 obey it now if we should.  */
      if (read_kbd || ! NILP (wait_for_cell))
	do_pending_window_change (0);

      /* Check for data from a process.  */
      if (no_avail || nfds == 0)
	continue;

      for (channel = 0; channel <= max_desc; ++channel)
        {
          struct fd_callback_data *d = &fd_callback_info[channel];
          if (d->func
	      && ((d->flags & FOR_READ
		   && FD_ISSET (channel, &Available))
		  || ((d->flags & FOR_WRITE)
		      && FD_ISSET (channel, &Writeok))))
            d->func (channel, d->data);
	}

      for (channel = 0; channel <= max_desc; channel++)
	{
	  if (FD_ISSET (channel, &Available)
	      && ((fd_callback_info[channel].flags & (KEYBOARD_FD | PROCESS_FD))
		  == PROCESS_FD))
	    {
	      int nread;

	      /* If waiting for this channel, arrange to return as
		 soon as no more input to be processed.  No more
		 waiting.  */
	      proc = chan_process[channel];
	      if (NILP (proc))
		continue;

	      /* If this is a server stream socket, accept connection.  */
	      if (EQ (XPROCESS (proc)->status, Qlisten))
		{
		  server_accept_connection (proc, channel);
		  continue;
		}

	      /* Read data from the process, starting with our
		 buffered-ahead character if we have one.  */

	      nread = read_process_output (proc, channel);
	      if ((!wait_proc || wait_proc == XPROCESS (proc))
		  && got_some_output < nread)
		got_some_output = nread;
	      if (nread > 0)
		{
		  /* Vacuum up any leftovers without waiting.  */
		  if (wait_proc == XPROCESS (proc))
		    wait = MINIMUM;
		  /* Since read_process_output can run a filter,
		     which can call accept-process-output,
		     don't try to read from any other processes
		     before doing the select again.  */
		  FD_ZERO (&Available);

		  if (do_display)
		    redisplay_preserve_echo_area (12);
		}
	      else if (nread == -1 && would_block (errno))
		;
#ifdef HAVE_PTYS
	      /* On some OSs with ptys, when the process on one end of
		 a pty exits, the other end gets an error reading with
		 errno = EIO instead of getting an EOF (0 bytes read).
		 Therefore, if we get an error reading and errno =
		 EIO, just continue, because the child process has
		 exited and should clean itself up soon (e.g. when we
		 get a SIGCHLD).  */
	      else if (nread == -1 && errno == EIO)
		{
		  struct Lisp_Process *p = XPROCESS (proc);

		  /* Clear the descriptor now, so we only raise the
		     signal once.  */
		  delete_read_fd (channel);

		  if (p->pid == -2)
		    {
		      /* If the EIO occurs on a pty, the SIGCHLD handler's
			 waitpid call will not find the process object to
			 delete.  Do it here.  */
		      p->tick = ++process_tick;
		      pset_status (p, Qfailed);
		    }
		}
#endif /* HAVE_PTYS */
	      /* If we can detect process termination, don't consider the
		 process gone just because its pipe is closed.  */
	      else if (nread == 0 && !NETCONN_P (proc) && !SERIALCONN_P (proc)
		       && !PIPECONN_P (proc))
		;
	      else if (nread == 0 && PIPECONN_P (proc))
		{
		  /* Preserve status of processes already terminated.  */
		  XPROCESS (proc)->tick = ++process_tick;
		  deactivate_process (proc);
		  if (EQ (XPROCESS (proc)->status, Qrun))
		    pset_status (XPROCESS (proc),
				 list2 (Qexit, make_number (0)));
		}
	      else
		{
		  /* Preserve status of processes already terminated.  */
		  XPROCESS (proc)->tick = ++process_tick;
		  deactivate_process (proc);
		  if (XPROCESS (proc)->raw_status_new)
		    update_status (XPROCESS (proc));
		  if (EQ (XPROCESS (proc)->status, Qrun))
		    pset_status (XPROCESS (proc),
				 list2 (Qexit, make_number (256)));
		}
	    }
	  if (FD_ISSET (channel, &Writeok)
	      && (fd_callback_info[channel].flags
		  & NON_BLOCKING_CONNECT_FD) != 0)
	    {
	      struct Lisp_Process *p;

	      delete_write_fd (channel);

	      proc = chan_process[channel];
	      if (NILP (proc))
		continue;

	      p = XPROCESS (proc);

#ifndef WINDOWSNT
	      {
		socklen_t xlen = sizeof (xerrno);
		if (getsockopt (channel, SOL_SOCKET, SO_ERROR, &xerrno, &xlen))
		  xerrno = errno;
	      }
#else
	      /* On MS-Windows, getsockopt clears the error for the
		 entire process, which may not be the right thing; see
		 w32.c.  Use getpeername instead.  */
	      {
		struct sockaddr pname;
		socklen_t pnamelen = sizeof (pname);

		/* If connection failed, getpeername will fail.  */
		xerrno = 0;
		if (getpeername (channel, &pname, &pnamelen) < 0)
		  {
		    /* Obtain connect failure code through error slippage.  */
		    char dummy;
		    xerrno = errno;
		    if (errno == ENOTCONN && read (channel, &dummy, 1) < 0)
		      xerrno = errno;
		  }
	      }
#endif
	      if (xerrno)
		{
		  Lisp_Object addrinfos
		    = connecting_status (p->status) ? XCDR (p->status) : Qnil;
		  if (!NILP (addrinfos))
		    XSETCDR (p->status, XCDR (addrinfos));
		  else
		    {
		      p->tick = ++process_tick;
		      pset_status (p, list2 (Qfailed, make_number (xerrno)));
		    }
		  deactivate_process (proc);
		  if (!NILP (addrinfos))
		    connect_network_socket (proc, addrinfos, Qnil);
		}
	      else
		{
#ifdef HAVE_GNUTLS
		  /* If we have an incompletely set up TLS connection,
		     then defer the sentinel signaling until
		     later. */
		  if (NILP (p->gnutls_boot_parameters)
		      && !p->gnutls_p)
#endif
		    {
		      pset_status (p, Qrun);
		      /* Execute the sentinel here.  If we had relied on
			 status_notify to do it later, it will read input
			 from the process before calling the sentinel.  */
		      exec_sentinel (proc, build_string ("open\n"));
		    }

		  if (0 <= p->infd && !EQ (p->filter, Qt)
		      && !EQ (p->command, Qt))
		    add_process_read_fd (p->infd);
		}
	    }
	}			/* End for each file descriptor.  */
    }				/* End while exit conditions not met.  */

  unbind_to (count, Qnil);

  /* If calling from keyboard input, do not quit
     since we want to return C-g as an input character.
     Otherwise, do pending quit if requested.  */
  if (read_kbd >= 0)
    {
      /* Prevent input_pending from remaining set if we quit.  */
      clear_input_pending ();
      maybe_quit ();
    }

  /* Timers and/or process filters that we have run could have themselves called
     `accept-process-output' (and by that indirectly this function), thus
     possibly reading some (or all) output of wait_proc without us noticing it.
     This could potentially lead to an endless wait (dealt with earlier in the
     function) and/or a wrong return value (dealt with here).  */
  if (wait_proc && wait_proc->nbytes_read != prev_wait_proc_nbytes_read)
    got_some_output = min (INT_MAX, (wait_proc->nbytes_read
                                     - prev_wait_proc_nbytes_read));

  return got_some_output;
}
\f
/* Given a list (FUNCTION ARGS...), apply FUNCTION to the ARGS.  */

static Lisp_Object
read_process_output_call (Lisp_Object fun_and_args)
{
  return apply1 (XCAR (fun_and_args), XCDR (fun_and_args));
}

static Lisp_Object
read_process_output_error_handler (Lisp_Object error_val)
{
  cmd_error_internal (error_val, "error in process filter: ");
  Vinhibit_quit = Qt;
  update_echo_area ();
  Fsleep_for (make_number (2), Qnil);
  return Qt;
}

static void
read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
				    ssize_t nbytes,
				    struct coding_system *coding);

/* Read pending output from the process channel,
   starting with our buffered-ahead character if we have one.
   Yield number of decoded characters read.

   This function reads at most 4096 characters.
   If you want to read all available subprocess output,
   you must call it repeatedly until it returns zero.

   The characters read are decoded according to PROC's coding-system
   for decoding.  */

static int
read_process_output (Lisp_Object proc, int channel)
{
  ssize_t nbytes;
  struct Lisp_Process *p = XPROCESS (proc);
  struct coding_system *coding = proc_decode_coding_system[channel];
  int carryover = p->decoding_carryover;
  enum { readmax = 4096 };
  ptrdiff_t count = SPECPDL_INDEX ();
  Lisp_Object odeactivate;
  char chars[sizeof coding->carryover + readmax];

  if (carryover)
    /* See the comment above.  */
    memcpy (chars, SDATA (p->decoding_buf), carryover);

#ifdef DATAGRAM_SOCKETS
  /* We have a working select, so proc_buffered_char is always -1.  */
  if (DATAGRAM_CHAN_P (channel))
    {
      socklen_t len = datagram_address[channel].len;
      nbytes = recvfrom (channel, chars + carryover, readmax,
			 0, datagram_address[channel].sa, &len);
    }
  else
#endif
    {
      bool buffered = proc_buffered_char[channel] >= 0;
      if (buffered)
	{
	  chars[carryover] = proc_buffered_char[channel];
	  proc_buffered_char[channel] = -1;
	}
#ifdef HAVE_GNUTLS
      if (p->gnutls_p && p->gnutls_state)
	nbytes = emacs_gnutls_read (p, chars + carryover + buffered,
				    readmax - buffered);
      else
#endif
	nbytes = emacs_read (channel, chars + carryover + buffered,
			     readmax - buffered);
      if (nbytes > 0 && p->adaptive_read_buffering)
	{
	  int delay = p->read_output_delay;
	  if (nbytes < 256)
	    {
	      if (delay < READ_OUTPUT_DELAY_MAX_MAX)
		{
		  if (delay == 0)
		    process_output_delay_count++;
		  delay += READ_OUTPUT_DELAY_INCREMENT * 2;
		}
	    }
	  else if (delay > 0 && nbytes == readmax - buffered)
	    {
	      delay -= READ_OUTPUT_DELAY_INCREMENT;
	      if (delay == 0)
		process_output_delay_count--;
	    }
	  p->read_output_delay = delay;
	  if (delay)
	    {
	      p->read_output_skip = 1;
	      process_output_skip = 1;
	    }
	}
      nbytes += buffered;
      nbytes += buffered && nbytes <= 0;
    }

  p->decoding_carryover = 0;

  /* At this point, NBYTES holds number of bytes just received
     (including the one in proc_buffered_char[channel]).  */
  if (nbytes <= 0)
    {
      if (nbytes < 0 || coding->mode & CODING_MODE_LAST_BLOCK)
	return nbytes;
      coding->mode |= CODING_MODE_LAST_BLOCK;
    }

  /* Ignore carryover, it's been added by a previous iteration already.  */
  p->nbytes_read += nbytes;

  /* Now set NBYTES how many bytes we must decode.  */
  nbytes += carryover;

  odeactivate = Vdeactivate_mark;
  /* There's no good reason to let process filters change the current
     buffer, and many callers of accept-process-output, sit-for, and
     friends don't expect current-buffer to be changed from under them.  */
  record_unwind_current_buffer ();

  read_and_dispose_of_process_output (p, chars, nbytes, coding);

  /* Handling the process output should not deactivate the mark.  */
  Vdeactivate_mark = odeactivate;

  unbind_to (count, Qnil);
  return nbytes;
}

static void
read_and_dispose_of_process_output (struct Lisp_Process *p, char *chars,
				    ssize_t nbytes,
				    struct coding_system *coding)
{
  Lisp_Object outstream = p->filter;
  Lisp_Object text;
  bool outer_running_asynch_code = running_asynch_code;
  int waiting = waiting_for_user_input_p;

#if 0
  Lisp_Object obuffer, okeymap;
  XSETBUFFER (obuffer, current_buffer);
  okeymap = BVAR (current_buffer, keymap);
#endif

  /* We inhibit quit here instead of just catching it so that
     hitting ^G when a filter happens to be running won't screw
     it up.  */
  specbind (Qinhibit_quit, Qt);
  specbind (Qlast_nonmenu_event, Qt);

  /* In case we get recursively called,
     and we already saved the match data nonrecursively,
     save the same match data in safely recursive fashion.  */
  if (outer_running_asynch_code)
    {
      Lisp_Object tem;
      /* Don't clobber the CURRENT match data, either!  */
      tem = Fmatch_data (Qnil, Qnil, Qnil);
      restore_search_regs ();
      record_unwind_save_match_data ();
      Fset_match_data (tem, Qt);
    }

  /* For speed, if a search happens within this code,
     save the match data in a special nonrecursive fashion.  */
  running_asynch_code = 1;

  decode_coding_c_string (coding, (unsigned char *) chars, nbytes, Qt);
  text = coding->dst_object;
  Vlast_coding_system_used = CODING_ID_NAME (coding->id);
  /* A new coding system might be found.  */
  if (!EQ (p->decode_coding_system, Vlast_coding_system_used))
    {
      pset_decode_coding_system (p, Vlast_coding_system_used);

      /* Don't call setup_coding_system for
	 proc_decode_coding_system[channel] here.  It is done in
	 detect_coding called via decode_coding above.  */

      /* If a coding system for encoding is not yet decided, we set
	 it as the same as coding-system for decoding.

	 But, before doing that we must check if
	 proc_encode_coding_system[p->outfd] surely points to a
	 valid memory because p->outfd will be changed once EOF is
	 sent to the process.  */
      if (NILP (p->encode_coding_system) && p->outfd >= 0
	  && proc_encode_coding_system[p->outfd])
	{
	  pset_encode_coding_system
	    (p, coding_inherit_eol_type (Vlast_coding_system_used, Qnil));
	  setup_coding_system (p->encode_coding_system,
			       proc_encode_coding_system[p->outfd]);
	}
    }

  if (coding->carryover_bytes > 0)
    {
      if (SCHARS (p->decoding_buf) < coding->carryover_bytes)
	pset_decoding_buf (p, make_uninit_string (coding->carryover_bytes));
      memcpy (SDATA (p->decoding_buf), coding->carryover,
	      coding->carryover_bytes);
      p->decoding_carryover = coding->carryover_bytes;
    }
  if (SBYTES (text) > 0)
    /* FIXME: It's wrong to wrap or not based on debug-on-error, and
       sometimes it's simply wrong to wrap (e.g. when called from
       accept-process-output).  */
    internal_condition_case_1 (read_process_output_call,
			       list3 (outstream, make_lisp_proc (p), text),
			       !NILP (Vdebug_on_error) ? Qnil : Qerror,
			       read_process_output_error_handler);

  /* If we saved the match data nonrecursively, restore it now.  */
  restore_search_regs ();
  running_asynch_code = outer_running_asynch_code;

  /* Restore waiting_for_user_input_p as it was
     when we were called, in case the filter clobbered it.  */
  waiting_for_user_input_p = waiting;

#if 0 /* Call record_asynch_buffer_change unconditionally,
	 because we might have changed minor modes or other things
	 that affect key bindings.  */
  if (! EQ (Fcurrent_buffer (), obuffer)
      || ! EQ (current_buffer->keymap, okeymap))
#endif
    /* But do it only if the caller is actually going to read events.
       Otherwise there's no need to make him wake up, and it could
       cause trouble (for example it would make sit_for return).  */
    if (waiting_for_user_input_p == -1)
      record_asynch_buffer_change ();
}

DEFUN ("internal-default-process-filter", Finternal_default_process_filter,
       Sinternal_default_process_filter, 2, 2, 0,
       doc: /* Function used as default process filter.
This inserts the process's output into its buffer, if there is one.
Otherwise it discards the output.  */)
  (Lisp_Object proc, Lisp_Object text)
{
  struct Lisp_Process *p;
  ptrdiff_t opoint;

  CHECK_PROCESS (proc);
  p = XPROCESS (proc);
  CHECK_STRING (text);

  if (!NILP (p->buffer) && BUFFER_LIVE_P (XBUFFER (p->buffer)))
    {
      Lisp_Object old_read_only;
      ptrdiff_t old_begv, old_zv;
      ptrdiff_t old_begv_byte, old_zv_byte;
      ptrdiff_t before, before_byte;
      ptrdiff_t opoint_byte;
      struct buffer *b;

      Fset_buffer (p->buffer);
      opoint = PT;
      opoint_byte = PT_BYTE;
      old_read_only = BVAR (current_buffer, read_only);
      old_begv = BEGV;
      old_zv = ZV;
      old_begv_byte = BEGV_BYTE;
      old_zv_byte = ZV_BYTE;

      bset_read_only (current_buffer, Qnil);

      /* Insert new output into buffer at the current end-of-output
	 marker, thus preserving logical ordering of input and output.  */
      if (XMARKER (p->mark)->buffer)
	set_point_from_marker (p->mark);
      else
	SET_PT_BOTH (ZV, ZV_BYTE);
      before = PT;
      before_byte = PT_BYTE;

      /* If the output marker is outside of the visible region, save
	 the restriction and widen.  */
      if (! (BEGV <= PT && PT <= ZV))
	Fwiden ();

      /* Adjust the multibyteness of TEXT to that of the buffer.  */
      if (NILP (BVAR (current_buffer, enable_multibyte_characters))
	  != ! STRING_MULTIBYTE (text))
	text = (STRING_MULTIBYTE (text)
		? Fstring_as_unibyte (text)
		: Fstring_to_multibyte (text));
      /* Insert before markers in case we are inserting where
	 the buffer's mark is, and the user's next command is Meta-y.  */
      insert_from_string_before_markers (text, 0, 0,
					 SCHARS (text), SBYTES (text), 0);

      /* Make sure the process marker's position is valid when the
	 process buffer is changed in the signal_after_change above.
	 W3 is known to do that.  */
      if (BUFFERP (p->buffer)
	  && (b = XBUFFER (p->buffer), b != current_buffer))
	set_marker_both (p->mark, p->buffer, BUF_PT (b), BUF_PT_BYTE (b));
      else
	set_marker_both (p->mark, p->buffer, PT, PT_BYTE);

      update_mode_lines = 23;

      /* Make sure opoint and the old restrictions
	 float ahead of any new text just as point would.  */
      if (opoint >= before)
	{
	  opoint += PT - before;
	  opoint_byte += PT_BYTE - before_byte;
	}
      if (old_begv > before)
	{
	  old_begv += PT - before;
	  old_begv_byte += PT_BYTE - before_byte;
	}
      if (old_zv >= before)
	{
	  old_zv += PT - before;
	  old_zv_byte += PT_BYTE - before_byte;
	}

      /* If the restriction isn't what it should be, set it.  */
      if (old_begv != BEGV || old_zv != ZV)
	Fnarrow_to_region (make_number (old_begv), make_number (old_zv));

      bset_read_only (current_buffer, old_read_only);
      SET_PT_BOTH (opoint, opoint_byte);
    }
  return Qnil;
}
\f
/* Sending data to subprocess.  */

/* In send_process, when a write fails temporarily,
   wait_reading_process_output is called.  It may execute user code,
   e.g. timers, that attempts to write new data to the same process.
   We must ensure that data is sent in the right order, and not
   interspersed half-completed with other writes (Bug#10815).  This is
   handled by the write_queue element of struct process.  It is a list
   with each entry having the form

   (string . (offset . length))

   where STRING is a lisp string, OFFSET is the offset into the
   string's byte sequence from which we should begin to send, and
   LENGTH is the number of bytes left to send.  */

/* Create a new entry in write_queue.
   INPUT_OBJ should be a buffer, string Qt, or Qnil.
   BUF is a pointer to the string sequence of the input_obj or a C
   string in case of Qt or Qnil.  */

static void
write_queue_push (struct Lisp_Process *p, Lisp_Object input_obj,
                  const char *buf, ptrdiff_t len, bool front)
{
  ptrdiff_t offset;
  Lisp_Object entry, obj;

  if (STRINGP (input_obj))
    {
      offset = buf - SSDATA (input_obj);
      obj = input_obj;
    }
  else
    {
      offset = 0;
      obj = make_unibyte_string (buf, len);
    }

  entry = Fcons (obj, Fcons (make_number (offset), make_number (len)));

  if (front)
    pset_write_queue (p, Fcons (entry, p->write_queue));
  else
    pset_write_queue (p, nconc2 (p->write_queue, list1 (entry)));
}

/* Remove the first element in the write_queue of process P, put its
   contents in OBJ, BUF and LEN, and return true.  If the
   write_queue is empty, return false.  */

static bool
write_queue_pop (struct Lisp_Process *p, Lisp_Object *obj,
		 const char **buf, ptrdiff_t *len)
{
  Lisp_Object entry, offset_length;
  ptrdiff_t offset;

  if (NILP (p->write_queue))
    return 0;

  entry = XCAR (p->write_queue);
  pset_write_queue (p, XCDR (p->write_queue));

  *obj = XCAR (entry);
  offset_length = XCDR (entry);

  *len = XINT (XCDR (offset_length));
  offset = XINT (XCAR (offset_length));
  *buf = SSDATA (*obj) + offset;

  return 1;
}

/* Send some data to process PROC.
   BUF is the beginning of the data; LEN is the number of characters.
   OBJECT is the Lisp object that the data comes from.  If OBJECT is
   nil or t, it means that the data comes from C string.

   If OBJECT is not nil, the data is encoded by PROC's coding-system
   for encoding before it is sent.

   This function can evaluate Lisp code and can garbage collect.  */

static void
send_process (Lisp_Object proc, const char *buf, ptrdiff_t len,
	      Lisp_Object object)
{
  struct Lisp_Process *p = XPROCESS (proc);
  ssize_t rv;
  struct coding_system *coding;

  if (NETCONN_P (proc))
    {
      wait_while_connecting (proc);
      wait_for_tls_negotiation (proc);
    }

  if (p->raw_status_new)
    update_status (p);
  if (! EQ (p->status, Qrun))
    error ("Process %s not running", SDATA (p->name));
  if (p->outfd < 0)
    error ("Output file descriptor of %s is closed", SDATA (p->name));

  coding = proc_encode_coding_system[p->outfd];
  Vlast_coding_system_used = CODING_ID_NAME (coding->id);

  if ((STRINGP (object) && STRING_MULTIBYTE (object))
      || (BUFFERP (object)
	  && !NILP (BVAR (XBUFFER (object), enable_multibyte_characters)))
      || EQ (object, Qt))
    {
      pset_encode_coding_system
	(p, complement_process_encoding_system (p->encode_coding_system));
      if (!EQ (Vlast_coding_system_used, p->encode_coding_system))
	{
	  /* The coding system for encoding was changed to raw-text
	     because we sent a unibyte text previously.  Now we are
	     sending a multibyte text, thus we must encode it by the
	     original coding system specified for the current process.

	     Another reason we come here is that the coding system
	     was just complemented and a new one was returned by
	     complement_process_encoding_system.  */
	  setup_coding_system (p->encode_coding_system, coding);
	  Vlast_coding_system_used = p->encode_coding_system;
	}
      coding->src_multibyte = 1;
    }
  else
    {
      coding->src_multibyte = 0;
      /* For sending a unibyte text, character code conversion should
	 not take place but EOL conversion should.  So, setup raw-text
	 or one of the subsidiary if we have not yet done it.  */
      if (CODING_REQUIRE_ENCODING (coding))
	{
	  if (CODING_REQUIRE_FLUSHING (coding))
	    {
	      /* But, before changing the coding, we must flush out data.  */
	      coding->mode |= CODING_MODE_LAST_BLOCK;
	      send_process (proc, "", 0, Qt);
	      coding->mode &= CODING_MODE_LAST_BLOCK;
	    }
	  setup_coding_system (raw_text_coding_system
			       (Vlast_coding_system_used),
			       coding);
	  coding->src_multibyte = 0;
	}
    }
  coding->dst_multibyte = 0;

  if (CODING_REQUIRE_ENCODING (coding))
    {
      coding->dst_object = Qt;
      if (BUFFERP (object))
	{
	  ptrdiff_t from_byte, from, to;
	  ptrdiff_t save_pt, save_pt_byte;
	  struct buffer *cur = current_buffer;

	  set_buffer_internal (XBUFFER (object));
	  save_pt = PT, save_pt_byte = PT_BYTE;

	  from_byte = PTR_BYTE_POS ((unsigned char *) buf);
	  from = BYTE_TO_CHAR (from_byte);
	  to = BYTE_TO_CHAR (from_byte + len);
	  TEMP_SET_PT_BOTH (from, from_byte);
	  encode_coding_object (coding, object, from, from_byte,
				to, from_byte + len, Qt);
	  TEMP_SET_PT_BOTH (save_pt, save_pt_byte);
	  set_buffer_internal (cur);
	}
      else if (STRINGP (object))
	{
	  encode_coding_object (coding, object, 0, 0, SCHARS (object),
				SBYTES (object), Qt);
	}
      else
	{
	  coding->dst_object = make_unibyte_string (buf, len);
	  coding->produced = len;
	}

      len = coding->produced;
      object = coding->dst_object;
      buf = SSDATA (object);
    }

  /* If there is already data in the write_queue, put the new data
     in the back of queue.  Otherwise, ignore it.  */
  if (!NILP (p->write_queue))
    write_queue_push (p, object, buf, len, 0);

  do   /* while !NILP (p->write_queue) */
    {
      ptrdiff_t cur_len = -1;
      const char *cur_buf;
      Lisp_Object cur_object;

      /* If write_queue is empty, ignore it.  */
      if (!write_queue_pop (p, &cur_object, &cur_buf, &cur_len))
	{
	  cur_len = len;
	  cur_buf = buf;
	  cur_object = object;
	}

      while (cur_len > 0)
	{
	  /* Send this batch, using one or more write calls.  */
	  ptrdiff_t written = 0;
	  int outfd = p->outfd;
#ifdef DATAGRAM_SOCKETS
	  if (DATAGRAM_CHAN_P (outfd))
	    {
	      rv = sendto (outfd, cur_buf, cur_len,
			   0, datagram_address[outfd].sa,
			   datagram_address[outfd].len);
	      if (rv >= 0)
		written = rv;
	      else if (errno == EMSGSIZE)
		report_file_error ("Sending datagram", proc);
	    }
	  else
#endif
	    {
#ifdef HAVE_GNUTLS
	      if (p->gnutls_p && p->gnutls_state)
		written = emacs_gnutls_write (p, cur_buf, cur_len);
	      else
#endif
		written = emacs_write_sig (outfd, cur_buf, cur_len);
	      rv = (written ? 0 : -1);
	      if (p->read_output_delay > 0
		  && p->adaptive_read_buffering == 1)
		{
		  p->read_output_delay = 0;
		  process_output_delay_count--;
		  p->read_output_skip = 0;
		}
	    }

	  if (rv < 0)
	    {
	      if (would_block (errno))
		/* Buffer is full.  Wait, accepting input;
		   that may allow the program
		   to finish doing output and read more.  */
		{
#ifdef BROKEN_PTY_READ_AFTER_EAGAIN
		  /* A gross hack to work around a bug in FreeBSD.
		     In the following sequence, read(2) returns
		     bogus data:

		     write(2)	 1022 bytes
		     write(2)   954 bytes, get EAGAIN
		     read(2)   1024 bytes in process_read_output
		     read(2)     11 bytes in process_read_output

		     That is, read(2) returns more bytes than have
		     ever been written successfully.  The 1033 bytes
		     read are the 1022 bytes written successfully
		     after processing (for example with CRs added if
		     the terminal is set up that way which it is
		     here).  The same bytes will be seen again in a
		     later read(2), without the CRs.  */

		  if (errno == EAGAIN)
		    {
		      int flags = FWRITE;
		      ioctl (p->outfd, TIOCFLUSH, &flags);
		    }
#endif /* BROKEN_PTY_READ_AFTER_EAGAIN */

		  /* Put what we should have written in wait_queue.  */
		  write_queue_push (p, cur_object, cur_buf, cur_len, 1);
		  wait_reading_process_output (0, 20 * 1000 * 1000,
					       0, 0, Qnil, NULL, 0);
		  /* Reread queue, to see what is left.  */
		  break;
		}
	      else if (errno == EPIPE)
		{
		  p->raw_status_new = 0;
		  pset_status (p, list2 (Qexit, make_number (256)));
		  p->tick = ++process_tick;
		  deactivate_process (proc);
		  error ("process %s no longer connected to pipe; closed it",
			 SDATA (p->name));
		}
	      else
		/* This is a real error.  */
		report_file_error ("Writing to process", proc);
	    }
	  cur_buf += written;
	  cur_len -= written;
	}
    }
  while (!NILP (p->write_queue));
}

DEFUN ("process-send-region", Fprocess_send_region, Sprocess_send_region,
       3, 3, 0,
       doc: /* Send current contents of region as input to PROCESS.
PROCESS may be a process, a buffer, the name of a process or buffer, or
nil, indicating the current buffer's process.
Called from program, takes three arguments, PROCESS, START and END.
If the region is more than 500 characters long,
it is sent in several bunches.  This may happen even for shorter regions.
Output from processes can arrive in between bunches.

If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed.  */)
  (Lisp_Object process, Lisp_Object start, Lisp_Object end)
{
  Lisp_Object proc = get_process (process);
  ptrdiff_t start_byte, end_byte;

  validate_region (&start, &end);

  start_byte = CHAR_TO_BYTE (XINT (start));
  end_byte = CHAR_TO_BYTE (XINT (end));

  if (XINT (start) < GPT && XINT (end) > GPT)
    move_gap_both (XINT (start), start_byte);

  if (NETCONN_P (proc))
    wait_while_connecting (proc);

  send_process (proc, (char *) BYTE_POS_ADDR (start_byte),
		end_byte - start_byte, Fcurrent_buffer ());

  return Qnil;
}

DEFUN ("process-send-string", Fprocess_send_string, Sprocess_send_string,
       2, 2, 0,
       doc: /* Send PROCESS the contents of STRING as input.
PROCESS may be a process, a buffer, the name of a process or buffer, or
nil, indicating the current buffer's process.
If STRING is more than 500 characters long,
it is sent in several bunches.  This may happen even for shorter strings.
Output from processes can arrive in between bunches.

If PROCESS is a non-blocking network process that hasn't been fully
set up yet, this function will block until socket setup has completed.  */)
  (Lisp_Object process, Lisp_Object string)
{
  CHECK_STRING (string);
  Lisp_Object proc = get_process (process);
  send_process (proc, SSDATA (string),
		SBYTES (string), string);
  return Qnil;
}
\f
/* Return the foreground process group for the tty/pty that
   the process P uses.  */
static pid_t
emacs_get_tty_pgrp (struct Lisp_Process *p)
{
  pid_t gid = -1;

#ifdef TIOCGPGRP
  if (ioctl (p->infd, TIOCGPGRP, &gid) == -1 && ! NILP (p->tty_name))
    {
      int fd;
      /* Some OS:es (Solaris 8/9) does not allow TIOCGPGRP from the
	 master side.  Try the slave side.  */
      fd = emacs_open (SSDATA (p->tty_name), O_RDONLY, 0);

      if (fd != -1)
	{
	  ioctl (fd, TIOCGPGRP, &gid);
	  emacs_close (fd);
	}
    }
#endif /* defined (TIOCGPGRP ) */

  return gid;
}

DEFUN ("process-running-child-p", Fprocess_running_child_p,
       Sprocess_running_child_p, 0, 1, 0,
       doc: /* Return non-nil if PROCESS has given the terminal to a
child.  If the operating system does not make it possible to find out,
return t.  If we can find out, return the numeric ID of the foreground
process group.  */)
  (Lisp_Object process)
{
  /* Initialize in case ioctl doesn't exist or gives an error,
     in a way that will cause returning t.  */
  Lisp_Object proc = get_process (process);
  struct Lisp_Process *p = XPROCESS (proc);

  if (!EQ (p->type, Qreal))
    error ("Process %s is not a subprocess",
	   SDATA (p->name));
  if (p->infd < 0)
    error ("Process %s is not active",
	   SDATA (p->name));

  pid_t gid = emacs_get_tty_pgrp (p);

  if (gid == p->pid)
    return Qnil;
  if (gid != -1)
    return make_number (gid);
  return Qt;
}
\f
/* Send a signal number SIGNO to PROCESS.
   If CURRENT_GROUP is t, that means send to the process group
   that currently owns the terminal being used to communicate with PROCESS.
   This is used for various commands in shell mode.
   If CURRENT_GROUP is lambda, that means send to the process group
   that currently owns the terminal, but only if it is NOT the shell itself.

   If NOMSG is false, insert signal-announcements into process's buffers
   right away.

   If we can, we try to signal PROCESS by sending control characters
   down the pty.  This allows us to signal inferiors who have changed
   their uid, for which kill would return an EPERM error.  */

static void
process_send_signal (Lisp_Object process, int signo, Lisp_Object current_group,
		     bool nomsg)
{
  Lisp_Object proc;
  struct Lisp_Process *p;
  pid_t gid;
  bool no_pgrp = 0;

  proc = get_process (process);
  p = XPROCESS (proc);

  if (!EQ (p->type, Qreal))
    error ("Process %s is not a subprocess",
	   SDATA (p->name));
  if (p->infd < 0)
    error ("Process %s is not active",
	   SDATA (p->name));

  if (!p->pty_flag)
    current_group = Qnil;

  /* If we are using pgrps, get a pgrp number and make it negative.  */
  if (NILP (current_group))
    /* Send the signal to the shell's process group.  */
    gid = p->pid;
  else
    {
#ifdef SIGNALS_VIA_CHARACTERS
      /* If possible, send signals to the entire pgrp
	 by sending an input character to it.  */

      struct termios t;
      cc_t *sig_char = NULL;

      tcgetattr (p->infd, &t);

      switch (signo)
	{
	case SIGINT:
	  sig_char = &t.c_cc[VINTR];
	  break;

	case SIGQUIT:
	  sig_char = &t.c_cc[VQUIT];
	  break;

  	case SIGTSTP:
#ifdef VSWTCH
	  sig_char = &t.c_cc[VSWTCH];
#else
	  sig_char = &t.c_cc[VSUSP];
#endif
	  break;
	}

      if (sig_char && *sig_char != CDISABLE)
	{
	  send_process (proc, (char *) sig_char, 1, Qnil);
	  return;
	}
      /* If we can't send the signal with a character,
	 fall through and send it another way.  */

      /* The code above may fall through if it can't
	 handle the signal.  */
#endif /* defined (SIGNALS_VIA_CHARACTERS) */

#ifdef TIOCGPGRP
      /* Get the current pgrp using the tty itself, if we have that.
	 Otherwise, use the pty to get the pgrp.
	 On pfa systems, saka@pfu.fujitsu.co.JP writes:
	 "TIOCGPGRP symbol defined in sys/ioctl.h at E50.
	 But, TIOCGPGRP does not work on E50 ;-P works fine on E60"
	 His patch indicates that if TIOCGPGRP returns an error, then
	 we should just assume that p->pid is also the process group id.  */

      gid = emacs_get_tty_pgrp (p);

      if (gid == -1)
	/* If we can't get the information, assume
	   the shell owns the tty.  */
	gid = p->pid;

      /* It is not clear whether anything really can set GID to -1.
	 Perhaps on some system one of those ioctls can or could do so.
	 Or perhaps this is vestigial.  */
      if (gid == -1)
	no_pgrp = 1;
#else  /* ! defined (TIOCGPGRP) */
      /* Can't select pgrps on this system, so we know that
	 the child itself heads the pgrp.  */
      gid = p->pid;
#endif /* ! defined (TIOCGPGRP) */

      /* If current_group is lambda, and the shell owns the terminal,
	 don't send any signal.  */
      if (EQ (current_group, Qlambda) && gid == p->pid)
	return;
    }

#ifdef SIGCONT
  if (signo == SIGCONT)
    {
      p->raw_status_new = 0;
      pset_status (p, Qrun);
      p->tick = ++process_tick;
      if (!nomsg)
	{
	  status_notify (NULL, NULL);
	  redisplay_preserve_echo_area (13);
	}
    }
#endif

#ifdef TIOCSIGSEND
  /* Work around a HP-UX 7.0 bug that mishandles signals to subjobs.
     We don't know whether the bug is fixed in later HP-UX versions.  */
  if (! NILP (current_group) && ioctl (p->infd, TIOCSIGSEND, signo) != -1)
    return;
#endif

  /* If we don't have process groups, send the signal to the immediate
     subprocess.  That isn't really right, but it's better than any
     obvious alternative.  */
  pid_t pid = no_pgrp ? gid : - gid;

  /* Do not kill an already-reaped process, as that could kill an
     innocent bystander that happens to have the same process ID.  */
  sigset_t oldset;
  block_child_signal (&oldset);
  if (p->alive)
    kill (pid, signo);
  unblock_child_signal (&oldset);
}

DEFUN ("internal-default-interrupt-process",
       Finternal_default_interrupt_process,
       Sinternal_default_interrupt_process, 0, 2, 0,
       doc: /* Default function to interrupt process PROCESS.
It shall be the last element in list `interrupt-process-functions'.
See function `interrupt-process' for more details on usage.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  process_send_signal (process, SIGINT, current_group, 0);
  return process;
}

DEFUN ("interrupt-process", Finterrupt_process, Sinterrupt_process, 0, 2, 0,
       doc: /* Interrupt process PROCESS.
PROCESS may be a process, a buffer, or the name of a process or buffer.
No arg or nil means current buffer's process.
Second arg CURRENT-GROUP non-nil means send signal to
the current process-group of the process's controlling terminal
rather than to the process's own process group.
If the process is a shell, this means interrupt current subjob
rather than the shell.

If CURRENT-GROUP is `lambda', and if the shell owns the terminal,
don't send the signal.

This function calls the functions of `interrupt-process-functions' in
the order of the list, until one of them returns non-`nil'.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  return CALLN (Frun_hook_with_args_until_success, Qinterrupt_process_functions,
		process, current_group);
}

DEFUN ("kill-process", Fkill_process, Skill_process, 0, 2, 0,
       doc: /* Kill process PROCESS.  May be process or name of one.
See function `interrupt-process' for more details on usage.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  process_send_signal (process, SIGKILL, current_group, 0);
  return process;
}

DEFUN ("quit-process", Fquit_process, Squit_process, 0, 2, 0,
       doc: /* Send QUIT signal to process PROCESS.  May be process or name of one.
See function `interrupt-process' for more details on usage.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  process_send_signal (process, SIGQUIT, current_group, 0);
  return process;
}

DEFUN ("stop-process", Fstop_process, Sstop_process, 0, 2, 0,
       doc: /* Stop process PROCESS.  May be process or name of one.
See function `interrupt-process' for more details on usage.
If PROCESS is a network or serial or pipe connection, inhibit handling
of incoming traffic.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)
			     || PIPECONN_P (process)))
    {
      struct Lisp_Process *p;

      p = XPROCESS (process);
      if (NILP (p->command)
	  && p->infd >= 0)
	delete_read_fd (p->infd);
      pset_command (p, Qt);
      return process;
    }
#ifndef SIGTSTP
  error ("No SIGTSTP support");
#else
  process_send_signal (process, SIGTSTP, current_group, 0);
#endif
  return process;
}

DEFUN ("continue-process", Fcontinue_process, Scontinue_process, 0, 2, 0,
       doc: /* Continue process PROCESS.  May be process or name of one.
See function `interrupt-process' for more details on usage.
If PROCESS is a network or serial process, resume handling of incoming
traffic.  */)
  (Lisp_Object process, Lisp_Object current_group)
{
  if (PROCESSP (process) && (NETCONN_P (process) || SERIALCONN_P (process)
			     || PIPECONN_P (process)))
    {
      struct Lisp_Process *p;

      p = XPROCESS (process);
      if (EQ (p->command, Qt)
	  && p->infd >= 0
	  && (!EQ (p->filter, Qt) || EQ (p->status, Qlisten)))
	{
	  add_process_read_fd (p->infd);
#ifdef WINDOWSNT
	  if (fd_info[ p->infd ].flags & FILE_SERIAL)
	    PurgeComm (fd_info[ p->infd ].hnd, PURGE_RXABORT | PURGE_RXCLEAR);
#else /* not WINDOWSNT */
	  tcflush (p->infd, TCIFLUSH);
#endif /* not WINDOWSNT */
	}
      pset_command (p, Qnil);
      return process;
    }
#ifdef SIGCONT
    process_send_signal (process, SIGCONT, current_group, 0);
#else
    error ("No SIGCONT support");
#endif
  return process;
}

/* Return the integer value of the signal whose abbreviation is ABBR,
   or a negative number if there is no such signal.  */
static int
abbr_to_signal (char const *name)
{
  int i, signo;
  char sigbuf[20]; /* Large enough for all valid signal abbreviations.  */

  if (!strncmp (name, "SIG", 3) || !strncmp (name, "sig", 3))
    name += 3;

  for (i = 0; i < sizeof sigbuf; i++)
    {
      sigbuf[i] = c_toupper (name[i]);
      if (! sigbuf[i])
	return str2sig (sigbuf, &signo) == 0 ? signo : -1;
    }

  return -1;
}

DEFUN ("signal-process", Fsignal_process, Ssignal_process,
       2, 2, "sProcess (name or number): \nnSignal code: ",
       doc: /* Send PROCESS the signal with code SIGCODE.
PROCESS may also be a number specifying the process id of the
process to signal; in this case, the process need not be a child of
this Emacs.
SIGCODE may be an integer, or a symbol whose name is a signal name.  */)
  (Lisp_Object process, Lisp_Object sigcode)
{
  pid_t pid;
  int signo;

  if (STRINGP (process))
    {
      Lisp_Object tem = Fget_process (process);
      if (NILP (tem))
	tem = string_to_number (SSDATA (process), 10, S2N_OVERFLOW_TO_FLOAT);
      process = tem;
    }
  else if (!NUMBERP (process))
    process = get_process (process);

  if (NILP (process))
    return process;

  if (NUMBERP (process))
    CONS_TO_INTEGER (process, pid_t, pid);
  else
    {
      CHECK_PROCESS (process);
      pid = XPROCESS (process)->pid;
      if (pid <= 0)
	error ("Cannot signal process %s", SDATA (XPROCESS (process)->name));
    }

  if (INTEGERP (sigcode))
    {
      CHECK_TYPE_RANGED_INTEGER (int, sigcode);
      signo = XINT (sigcode);
    }
  else
    {
      char *name;

      CHECK_SYMBOL (sigcode);
      name = SSDATA (SYMBOL_NAME (sigcode));

      signo = abbr_to_signal (name);
      if (signo < 0)
	error ("Undefined signal name %s", name);
    }

  return make_number (kill (pid, signo));
}

DEFUN ("process-send-eof", Fprocess_send_eof, Sprocess_send_eof, 0, 1, 0,
       doc: /* Make PROCESS see end-of-file in its input.
EOF comes after any text already sent to it.
PROCESS may be a process, a buffer, the name of a process or buffer, or
nil, indicating the current buffer's process.
If PROCESS is a network connection, or is a process communicating
through a pipe (as opposed to a pty), then you cannot send any more
text to PROCESS after you call this function.
If PROCESS is a serial process, wait until all output written to the
process has been transmitted to the serial port.  */)
  (Lisp_Object process)
{
  Lisp_Object proc;
  struct coding_system *coding = NULL;
  int outfd;

  proc = get_process (process);

  if (NETCONN_P (proc))
    wait_while_connecting (proc);

  if (DATAGRAM_CONN_P (proc))
    return process;


  outfd = XPROCESS (proc)->outfd;
  if (outfd >= 0)
    coding = proc_encode_coding_system[outfd];

  /* Make sure the process is really alive.  */
  if (XPROCESS (proc)->raw_status_new)
    update_status (XPROCESS (proc));
  if (! EQ (XPROCESS (proc)->status, Qrun))
    error ("Process %s not running", SDATA (XPROCESS (proc)->name));

  if (coding && CODING_REQUIRE_FLUSHING (coding))
    {
      coding->mode |= CODING_MODE_LAST_BLOCK;
      send_process (proc, "", 0, Qnil);
    }

  if (XPROCESS (proc)->pty_flag)
    send_process (proc, "\004", 1, Qnil);
  else if (EQ (XPROCESS (proc)->type, Qserial))
    {
#ifndef WINDOWSNT
      if (tcdrain (XPROCESS (proc)->outfd) != 0)
	report_file_error ("Failed tcdrain", Qnil);
#endif /* not WINDOWSNT */
      /* Do nothing on Windows because writes are blocking.  */
    }
  else
    {
      struct Lisp_Process *p = XPROCESS (proc);
      int old_outfd = p->outfd;
      int new_outfd;

#ifdef HAVE_SHUTDOWN
      /* If this is a network connection, or socketpair is used
	 for communication with the subprocess, call shutdown to cause EOF.
	 (In some old system, shutdown to socketpair doesn't work.
	 Then we just can't win.)  */
      if (0 <= old_outfd
	  && (EQ (p->type, Qnetwork) || p->infd == old_outfd))
	shutdown (old_outfd, 1);
#endif
      close_process_fd (&p->open_fd[WRITE_TO_SUBPROCESS]);
      new_outfd = emacs_open (NULL_DEVICE, O_WRONLY, 0);
      if (new_outfd < 0)
	report_file_error ("Opening null device", Qnil);
      p->open_fd[WRITE_TO_SUBPROCESS] = new_outfd;
      p->outfd = new_outfd;

      if (!proc_encode_coding_system[new_outfd])
	proc_encode_coding_system[new_outfd]
	  = xmalloc (sizeof (struct coding_system));
      if (old_outfd >= 0)
	{
	  *proc_encode_coding_system[new_outfd]
	    = *proc_encode_coding_system[old_outfd];
	  memset (proc_encode_coding_system[old_outfd], 0,
		  sizeof (struct coding_system));
	}
      else
	setup_coding_system (p->encode_coding_system,
			     proc_encode_coding_system[new_outfd]);
    }
  return process;
}
\f
/* The main Emacs thread records child processes in three places:

   - Vprocess_alist, for asynchronous subprocesses, which are child
     processes visible to Lisp.

   - deleted_pid_list, for child processes invisible to Lisp,
     typically because of delete-process.  These are recorded so that
     the processes can be reaped when they exit, so that the operating
     system's process table is not cluttered by zombies.

   - the local variable PID in Fcall_process, call_process_cleanup and
     call_process_kill, for synchronous subprocesses.
     record_unwind_protect is used to make sure this process is not
     forgotten: if the user interrupts call-process and the child
     process refuses to exit immediately even with two C-g's,
     call_process_kill adds PID's contents to deleted_pid_list before
     returning.

   The main Emacs thread invokes waitpid only on child processes that
   it creates and that have not been reaped.  This avoid races on
   platforms such as GTK, where other threads create their own
   subprocesses which the main thread should not reap.  For example,
   if the main thread attempted to reap an already-reaped child, it
   might inadvertently reap a GTK-created process that happened to
   have the same process ID.  */

/* LIB_CHILD_HANDLER is a SIGCHLD handler that Emacs calls while doing
   its own SIGCHLD handling.  On POSIXish systems, glib needs this to
   keep track of its own children.  GNUstep is similar.  */

static void dummy_handler (int sig) {}
static signal_handler_t volatile lib_child_handler;

/* Handle a SIGCHLD signal by looking for known child processes of
   Emacs whose status have changed.  For each one found, record its
   new status.

   All we do is change the status; we do not run sentinels or print
   notifications.  That is saved for the next time keyboard input is
   done, in order to avoid timing errors.

   ** WARNING: this can be called during garbage collection.
   Therefore, it must not be fooled by the presence of mark bits in
   Lisp objects.

   ** USG WARNING: Although it is not obvious from the documentation
   in signal(2), on a USG system the SIGCLD handler MUST NOT call
   signal() before executing at least one wait(), otherwise the
   handler will be called again, resulting in an infinite loop.  The
   relevant portion of the documentation reads "SIGCLD signals will be
   queued and the signal-catching function will be continually
   reentered until the queue is empty".  Invoking signal() causes the
   kernel to reexamine the SIGCLD queue.  Fred Fish, UniSoft Systems
   Inc.

   ** Malloc WARNING: This should never call malloc either directly or
   indirectly; if it does, that is a bug.  */

static void
handle_child_signal (int sig)
{
  Lisp_Object tail, proc;

  /* Find the process that signaled us, and record its status.  */

  /* The process can have been deleted by Fdelete_process, or have
     been started asynchronously by Fcall_process.  */
  for (tail = deleted_pid_list; CONSP (tail); tail = XCDR (tail))
    {
      bool all_pids_are_fixnums
	= (MOST_NEGATIVE_FIXNUM <= TYPE_MINIMUM (pid_t)
	   && TYPE_MAXIMUM (pid_t) <= MOST_POSITIVE_FIXNUM);
      Lisp_Object head = XCAR (tail);
      Lisp_Object xpid;
      if (! CONSP (head))
	continue;
      xpid = XCAR (head);
      if (all_pids_are_fixnums ? INTEGERP (xpid) : NUMBERP (xpid))
	{
	  pid_t deleted_pid;
	  if (INTEGERP (xpid))
	    deleted_pid = XINT (xpid);
	  else
	    deleted_pid = XFLOAT_DATA (xpid);
	  if (child_status_changed (deleted_pid, 0, 0))
	    {
	      if (STRINGP (XCDR (head)))
		unlink (SSDATA (XCDR (head)));
	      XSETCAR (tail, Qnil);
	    }
	}
    }

  /* Otherwise, if it is asynchronous, it is in Vprocess_alist.  */
  FOR_EACH_PROCESS (tail, proc)
    {
      struct Lisp_Process *p = XPROCESS (proc);
      int status;

      if (p->alive
	  && child_status_changed (p->pid, &status, WUNTRACED | WCONTINUED))
	{
	  /* Change the status of the process that was found.  */
	  p->tick = ++process_tick;
	  p->raw_status = status;
	  p->raw_status_new = 1;

	  /* If process has terminated, stop waiting for its output.  */
	  if (WIFSIGNALED (status) || WIFEXITED (status))
	    {
	      bool clear_desc_flag = 0;
	      p->alive = 0;
	      if (p->infd >= 0)
		clear_desc_flag = 1;

	      /* clear_desc_flag avoids a compiler bug in Microsoft C.  */
	      if (clear_desc_flag)
		delete_read_fd (p->infd);
	    }
	}
    }

  lib_child_handler (sig);
#ifdef NS_IMPL_GNUSTEP
  /* NSTask in GNUstep sets its child handler each time it is called.
     So we must re-set ours.  */
  catch_child_signal ();
#endif
}

static void
deliver_child_signal (int sig)
{
  deliver_process_signal (sig, handle_child_signal);
}
\f

static Lisp_Object
exec_sentinel_error_handler (Lisp_Object error_val)
{
  /* Make sure error_val is a cons cell, as all the rest of error
     handling expects that, and will barf otherwise.  */
  if (!CONSP (error_val))
    error_val = Fcons (Qerror, error_val);
  cmd_error_internal (error_val, "error in process sentinel: ");
  Vinhibit_quit = Qt;
  update_echo_area ();
  Fsleep_for (make_number (2), Qnil);
  return Qt;
}

static void
exec_sentinel (Lisp_Object proc, Lisp_Object reason)
{
  Lisp_Object sentinel, odeactivate;
  struct Lisp_Process *p = XPROCESS (proc);
  ptrdiff_t count = SPECPDL_INDEX ();
  bool outer_running_asynch_code = running_asynch_code;
  int waiting = waiting_for_user_input_p;

  if (inhibit_sentinels)
    return;

  odeactivate = Vdeactivate_mark;
#if 0
  Lisp_Object obuffer, okeymap;
  XSETBUFFER (obuffer, current_buffer);
  okeymap = BVAR (current_buffer, keymap);
#endif

  /* There's no good reason to let sentinels change the current
     buffer, and many callers of accept-process-output, sit-for, and
     friends don't expect current-buffer to be changed from under them.  */
  record_unwind_current_buffer ();

  sentinel = p->sentinel;

  /* Inhibit quit so that random quits don't screw up a running filter.  */
  specbind (Qinhibit_quit, Qt);
  specbind (Qlast_nonmenu_event, Qt); /* Why? --Stef  */

  /* In case we get recursively called,
     and we already saved the match data nonrecursively,
     save the same match data in safely recursive fashion.  */
  if (outer_running_asynch_code)
    {
      Lisp_Object tem;
      tem = Fmatch_data (Qnil, Qnil, Qnil);
      restore_search_regs ();
      record_unwind_save_match_data ();
      Fset_match_data (tem, Qt);
    }

  /* For speed, if a search happens within this code,
     save the match data in a special nonrecursive fashion.  */
  running_asynch_code = 1;

  internal_condition_case_1 (read_process_output_call,
			     list3 (sentinel, proc, reason),
			     !NILP (Vdebug_on_error) ? Qnil : Qerror,
			     exec_sentinel_error_handler);

  /* If we saved the match data nonrecursively, restore it now.  */
  restore_search_regs ();
  running_asynch_code = outer_running_asynch_code;

  Vdeactivate_mark = odeactivate;

  /* Restore waiting_for_user_input_p as it was
     when we were called, in case the filter clobbered it.  */
  waiting_for_user_input_p = waiting;

#if 0
  if (! EQ (Fcurrent_buffer (), obuffer)
      || ! EQ (current_buffer->keymap, okeymap))
#endif
    /* But do it only if the caller is actually going to read events.
       Otherwise there's no need to make him wake up, and it could
       cause trouble (for example it would make sit_for return).  */
    if (waiting_for_user_input_p == -1)
      record_asynch_buffer_change ();

  unbind_to (count, Qnil);
}

/* Report all recent events of a change in process status
   (either run the sentinel or output a message).
   This is usually done while Emacs is waiting for keyboard input
   but can be done at other times.

   Return positive if any input was received from WAIT_PROC (or from
   any process if WAIT_PROC is null), zero if input was attempted but
   none received, and negative if we didn't even try.  */

static int
status_notify (struct Lisp_Process *deleting_process,
	       struct Lisp_Process *wait_proc)
{
  Lisp_Object proc;
  Lisp_Object tail, msg;
  int got_some_output = -1;

  tail = Qnil;
  msg = Qnil;

  /* Set this now, so that if new processes are created by sentinels
     that we run, we get called again to handle their status changes.  */
  update_tick = process_tick;

  FOR_EACH_PROCESS (tail, proc)
    {
      Lisp_Object symbol;
      register struct Lisp_Process *p = XPROCESS (proc);

      if (p->tick != p->update_tick)
	{
	  p->update_tick = p->tick;

	  /* If process is still active, read any output that remains.  */
	  while (! EQ (p->filter, Qt)
		 && ! connecting_status (p->status)
		 && ! EQ (p->status, Qlisten)
		 /* Network or serial process not stopped:  */
		 && ! EQ (p->command, Qt)
		 && p->infd >= 0
		 && p != deleting_process)
	    {
	      int nread = read_process_output (proc, p->infd);
	      if ((!wait_proc || wait_proc == XPROCESS (proc))
		  && got_some_output < nread)
		got_some_output = nread;
	      if (nread <= 0)
		break;
	    }

	  /* Get the text to use for the message.  */
	  if (p->raw_status_new)
	    update_status (p);
	  msg = status_message (p);

	  /* If process is terminated, deactivate it or delete it.  */
	  symbol = p->status;
	  if (CONSP (p->status))
	    symbol = XCAR (p->status);

	  if (EQ (symbol, Qsignal) || EQ (symbol, Qexit)
	      || EQ (symbol, Qclosed))
	    {
	      if (delete_exited_processes)
		remove_process (proc);
	      else
		deactivate_process (proc);
	    }

	  /* The actions above may have further incremented p->tick.
	     So set p->update_tick again so that an error in the sentinel will
	     not cause this code to be run again.  */
	  p->update_tick = p->tick;
	  /* Now output the message suitably.  */
	  exec_sentinel (proc, msg);
	  if (BUFFERP (p->buffer))
	    /* In case it uses %s in mode-line-format.  */
	    bset_update_mode_line (XBUFFER (p->buffer));
	}
    } /* end for */

  return got_some_output;
}

DEFUN ("internal-default-process-sentinel", Finternal_default_process_sentinel,
       Sinternal_default_process_sentinel, 2, 2, 0,
       doc: /* Function used as default sentinel for processes.
This inserts a status message into the process's buffer, if there is one.  */)
     (Lisp_Object proc, Lisp_Object msg)
{
  Lisp_Object buffer, symbol;
  struct Lisp_Process *p;
  CHECK_PROCESS (proc);
  p = XPROCESS (proc);
  buffer = p->buffer;
  symbol = p->status;
  if (CONSP (symbol))
    symbol = XCAR (symbol);

  if (!EQ (symbol, Qrun) && !NILP (buffer))
    {
      Lisp_Object tem;
      struct buffer *old = current_buffer;
      ptrdiff_t opoint, opoint_byte;
      ptrdiff_t before, before_byte;

      /* Avoid error if buffer is deleted
	 (probably that's why the process is dead, too).  */
      if (!BUFFER_LIVE_P (XBUFFER (buffer)))
	return Qnil;
      Fset_buffer (buffer);

      if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
	msg = (code_convert_string_norecord
	       (msg, Vlocale_coding_system, 1));

      opoint = PT;
      opoint_byte = PT_BYTE;
      /* Insert new output into buffer
	 at the current end-of-output marker,
	 thus preserving logical ordering of input and output.  */
      if (XMARKER (p->mark)->buffer)
	Fgoto_char (p->mark);
      else
	SET_PT_BOTH (ZV, ZV_BYTE);

      before = PT;
      before_byte = PT_BYTE;

      tem = BVAR (current_buffer, read_only);
      bset_read_only (current_buffer, Qnil);
      insert_string ("\nProcess ");
      { /* FIXME: temporary kludge.  */
	Lisp_Object tem2 = p->name; Finsert (1, &tem2); }
      insert_string (" ");
      Finsert (1, &msg);
      bset_read_only (current_buffer, tem);
      set_marker_both (p->mark, p->buffer, PT, PT_BYTE);

      if (opoint >= before)
	SET_PT_BOTH (opoint + (PT - before),
		     opoint_byte + (PT_BYTE - before_byte));
      else
	SET_PT_BOTH (opoint, opoint_byte);

      set_buffer_internal (old);
    }
  return Qnil;
}

\f
DEFUN ("set-process-coding-system", Fset_process_coding_system,
       Sset_process_coding_system, 1, 3, 0,
       doc: /* Set coding systems of PROCESS to DECODING and ENCODING.
DECODING will be used to decode subprocess output and ENCODING to
encode subprocess input. */)
  (Lisp_Object process, Lisp_Object decoding, Lisp_Object encoding)
{
  CHECK_PROCESS (process);

  struct Lisp_Process *p = XPROCESS (process);

  Fcheck_coding_system (decoding);
  Fcheck_coding_system (encoding);
  encoding = coding_inherit_eol_type (encoding, Qnil);
  pset_decode_coding_system (p, decoding);
  pset_encode_coding_system (p, encoding);

  /* If the sockets haven't been set up yet, the final setup part of
     this will be called asynchronously. */
  if (p->infd < 0 || p->outfd < 0)
    return Qnil;

  setup_process_coding_systems (process);

  return Qnil;
}

DEFUN ("process-coding-system",
       Fprocess_coding_system, Sprocess_coding_system, 1, 1, 0,
       doc: /* Return a cons of coding systems for decoding and encoding of PROCESS.  */)
  (register Lisp_Object process)
{
  CHECK_PROCESS (process);
  return Fcons (XPROCESS (process)->decode_coding_system,
		XPROCESS (process)->encode_coding_system);
}

DEFUN ("set-process-filter-multibyte", Fset_process_filter_multibyte,
       Sset_process_filter_multibyte, 2, 2, 0,
       doc: /* Set multibyteness of the strings given to PROCESS's filter.
If FLAG is non-nil, the filter is given multibyte strings.
If FLAG is nil, the filter is given unibyte strings.  In this case,
all character code conversion except for end-of-line conversion is
suppressed.  */)
  (Lisp_Object process, Lisp_Object flag)
{
  CHECK_PROCESS (process);

  struct Lisp_Process *p = XPROCESS (process);
  if (NILP (flag))
    pset_decode_coding_system
      (p, raw_text_coding_system (p->decode_coding_system));

  /* If the sockets haven't been set up yet, the final setup part of
     this will be called asynchronously. */
  if (p->infd < 0 || p->outfd < 0)
    return Qnil;

  setup_process_coding_systems (process);

  return Qnil;
}

DEFUN ("process-filter-multibyte-p", Fprocess_filter_multibyte_p,
       Sprocess_filter_multibyte_p, 1, 1, 0,
       doc: /* Return t if a multibyte string is given to PROCESS's filter.*/)
  (Lisp_Object process)
{
  CHECK_PROCESS (process);
  struct Lisp_Process *p = XPROCESS (process);
  if (p->infd < 0)
    return Qnil;
  struct coding_system *coding = proc_decode_coding_system[p->infd];
  return (CODING_FOR_UNIBYTE (coding) ? Qnil : Qt);
}


\f

# ifdef HAVE_GPM

void
add_gpm_wait_descriptor (int desc)
{
  add_keyboard_wait_descriptor (desc);
}

void
delete_gpm_wait_descriptor (int desc)
{
  delete_keyboard_wait_descriptor (desc);
}

# endif

# ifdef USABLE_SIGIO

/* Return true if *MASK has a bit set
   that corresponds to one of the keyboard input descriptors.  */

static bool
keyboard_bit_set (fd_set *mask)
{
  int fd;

  for (fd = 0; fd <= max_desc; fd++)
    if (FD_ISSET (fd, mask)
	&& ((fd_callback_info[fd].flags & (FOR_READ | KEYBOARD_FD))
	    == (FOR_READ | KEYBOARD_FD)))
      return 1;

  return 0;
}
# endif

#else  /* not subprocesses */

/* This is referenced in thread.c:run_thread (which is never actually
   called, since threads are not enabled for this configuration.  */
void
update_processes_for_thread_death (Lisp_Object dying_thread)
{
}

/* Defined in msdos.c.  */
extern int sys_select (int, fd_set *, fd_set *, fd_set *,
		       struct timespec *, void *);

/* Implementation of wait_reading_process_output, assuming that there
   are no subprocesses.  Used only by the MS-DOS build.

   Wait for timeout to elapse and/or keyboard input to be available.

   TIME_LIMIT is:
     timeout in seconds
     If negative, gobble data immediately available but don't wait for any.

   NSECS is:
     an additional duration to wait, measured in nanoseconds
     If TIME_LIMIT is zero, then:
       If NSECS == 0, there is no limit.
       If NSECS > 0, the timeout consists of NSECS only.
       If NSECS < 0, gobble data immediately, as if TIME_LIMIT were negative.

   READ_KBD is:
     0 to ignore keyboard input, or
     1 to return when input is available, or
     -1 means caller will actually read the input, so don't throw to
       the quit handler.

   see full version for other parameters. We know that wait_proc will
     always be NULL, since `subprocesses' isn't defined.

   DO_DISPLAY means redisplay should be done to show subprocess
   output that arrives.

   Return -1 signifying we got no output and did not try.  */

int
wait_reading_process_output (intmax_t time_limit, int nsecs, int read_kbd,
			     bool do_display,
			     Lisp_Object wait_for_cell,
			     struct Lisp_Process *wait_proc, int just_wait_proc)
{
  register int nfds;
  struct timespec end_time, timeout;
  enum { MINIMUM = -1, TIMEOUT, INFINITY } wait;

  if (TYPE_MAXIMUM (time_t) < time_limit)
    time_limit = TYPE_MAXIMUM (time_t);

  if (time_limit < 0 || nsecs < 0)
    wait = MINIMUM;
  else if (time_limit > 0 || nsecs > 0)
    {
      wait = TIMEOUT;
      end_time = timespec_add (current_timespec (),
                               make_timespec (time_limit, nsecs));
    }
  else
    wait = INFINITY;

  /* Turn off periodic alarms (in case they are in use)
     and then turn off any other atimers,
     because the select emulator uses alarms.  */
  stop_polling ();
  turn_on_atimers (0);

  while (1)
    {
      bool timeout_reduced_for_timers = false;
      fd_set waitchannels;
      int xerrno;

      /* If calling from keyboard input, do not quit
	 since we want to return C-g as an input character.
	 Otherwise, do pending quit if requested.  */
      if (read_kbd >= 0)
	maybe_quit ();

      /* Exit now if the cell we're waiting for became non-nil.  */
      if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
	break;

      /* Compute time from now till when time limit is up.  */
      /* Exit if already run out.  */
      if (wait == TIMEOUT)
	{
	  struct timespec now = current_timespec ();
	  if (timespec_cmp (end_time, now) <= 0)
	    break;
	  timeout = timespec_sub (end_time, now);
	}
      else
	timeout = make_timespec (wait < TIMEOUT ? 0 : 100000, 0);

      /* If our caller will not immediately handle keyboard events,
	 run timer events directly.
	 (Callers that will immediately read keyboard events
	 call timer_delay on their own.)  */
      if (NILP (wait_for_cell))
	{
	  struct timespec timer_delay;

	  do
	    {
	      unsigned old_timers_run = timers_run;
	      timer_delay = timer_check ();
	      if (timers_run != old_timers_run && do_display)
		/* We must retry, since a timer may have requeued itself
		   and that could alter the time delay.  */
		redisplay_preserve_echo_area (14);
	      else
		break;
	    }
	  while (!detect_input_pending ());

	  /* If there is unread keyboard input, also return.  */
	  if (read_kbd != 0
	      && requeued_events_pending_p ())
	    break;

	  if (timespec_valid_p (timer_delay))
	    {
	      if (timespec_cmp (timer_delay, timeout) < 0)
		{
		  timeout = timer_delay;
		  timeout_reduced_for_timers = true;
		}
	    }
	}

      /* Cause C-g and alarm signals to take immediate action,
	 and cause input available signals to zero out timeout.  */
      if (read_kbd < 0)
	set_waiting_for_input (&timeout);

      /* If a frame has been newly mapped and needs updating,
	 reprocess its display stuff.  */
      if (frame_garbaged && do_display)
	{
	  clear_waiting_for_input ();
	  redisplay_preserve_echo_area (15);
	  if (read_kbd < 0)
	    set_waiting_for_input (&timeout);
	}

      /* Wait till there is something to do.  */
      FD_ZERO (&waitchannels);
      if (read_kbd && detect_input_pending ())
	nfds = 0;
      else
	{
	  if (read_kbd || !NILP (wait_for_cell))
	    FD_SET (0, &waitchannels);
	  nfds = pselect (1, &waitchannels, NULL, NULL, &timeout, NULL);
	}

      xerrno = errno;

      /* Make C-g and alarm signals set flags again.  */
      clear_waiting_for_input ();

      /*  If we woke up due to SIGWINCH, actually change size now.  */
      do_pending_window_change (0);

      if (wait < INFINITY && nfds == 0 && ! timeout_reduced_for_timers)
	/* We waited the full specified time, so return now.  */
	break;

      if (nfds == -1)
	{
	  /* If the system call was interrupted, then go around the
	     loop again.  */
	  if (xerrno == EINTR)
	    FD_ZERO (&waitchannels);
	  else
	    report_file_errno ("Failed select", Qnil, xerrno);
	}

      /* Check for keyboard input.  */

      if (read_kbd
	  && detect_input_pending_run_timers (do_display))
	{
	  swallow_events (do_display);
	  if (detect_input_pending_run_timers (do_display))
	    break;
	}

      /* If there is unread keyboard input, also return.  */
      if (read_kbd
	  && requeued_events_pending_p ())
	break;

      /* If wait_for_cell. check for keyboard input
	 but don't run any timers.
	 ??? (It seems wrong to me to check for keyboard
	 input at all when wait_for_cell, but the code
	 has been this way since July 1994.
	 Try changing this after version 19.31.)  */
      if (! NILP (wait_for_cell)
	  && detect_input_pending ())
	{
	  swallow_events (do_display);
	  if (detect_input_pending ())
	    break;
	}

      /* Exit now if the cell we're waiting for became non-nil.  */
      if (! NILP (wait_for_cell) && ! NILP (XCAR (wait_for_cell)))
	break;
    }

  start_polling ();

  return -1;
}

#endif	/* not subprocesses */

/* The following functions are needed even if async subprocesses are
   not supported.  Some of them are no-op stubs in that case.  */

#ifdef HAVE_TIMERFD

/* Add FD, which is a descriptor returned by timerfd_create,
   to the set of non-keyboard input descriptors.  */

void
add_timer_wait_descriptor (int fd)
{
  add_read_fd (fd, timerfd_callback, NULL);
  fd_callback_info[fd].flags &= ~KEYBOARD_FD;
}

#endif /* HAVE_TIMERFD */

/* If program file NAME starts with /: for quoting a magic
   name, remove that, preserving the multibyteness of NAME.  */

Lisp_Object
remove_slash_colon (Lisp_Object name)
{
  return
    (SREF (name, 0) == '/' && SREF (name, 1) == ':'
     ? make_specified_string (SSDATA (name) + 2, SCHARS (name) - 2,
			      SBYTES (name) - 2, STRING_MULTIBYTE (name))
     : name);
}

/* Add DESC to the set of keyboard input descriptors.  */

void
add_keyboard_wait_descriptor (int desc)
{
#ifdef subprocesses /* Actually means "not MSDOS".  */
  eassert (desc >= 0 && desc < FD_SETSIZE);
  fd_callback_info[desc].flags &= ~PROCESS_FD;
  fd_callback_info[desc].flags |= (FOR_READ | KEYBOARD_FD);
  if (desc > max_desc)
    max_desc = desc;
#endif
}

/* From now on, do not expect DESC to give keyboard input.  */

void
delete_keyboard_wait_descriptor (int desc)
{
#ifdef subprocesses
  eassert (desc >= 0 && desc < FD_SETSIZE);

  fd_callback_info[desc].flags &= ~(FOR_READ | KEYBOARD_FD | PROCESS_FD);

  if (desc == max_desc)
    recompute_max_desc ();
#endif
}

/* Setup coding systems of PROCESS.  */

void
setup_process_coding_systems (Lisp_Object process)
{
#ifdef subprocesses
  struct Lisp_Process *p = XPROCESS (process);
  int inch = p->infd;
  int outch = p->outfd;
  Lisp_Object coding_system;

  if (inch < 0 || outch < 0)
    return;

  if (!proc_decode_coding_system[inch])
    proc_decode_coding_system[inch] = xmalloc (sizeof (struct coding_system));
  coding_system = p->decode_coding_system;
  if (EQ (p->filter, Qinternal_default_process_filter)
      && BUFFERP (p->buffer))
    {
      if (NILP (BVAR (XBUFFER (p->buffer), enable_multibyte_characters)))
	coding_system = raw_text_coding_system (coding_system);
    }
  setup_coding_system (coding_system, proc_decode_coding_system[inch]);

  if (!proc_encode_coding_system[outch])
    proc_encode_coding_system[outch] = xmalloc (sizeof (struct coding_system));
  setup_coding_system (p->encode_coding_system,
		       proc_encode_coding_system[outch]);
#endif
}

DEFUN ("get-buffer-process", Fget_buffer_process, Sget_buffer_process, 1, 1, 0,
       doc: /* Return the (or a) live process associated with BUFFER.
BUFFER may be a buffer or the name of one.
Return nil if all processes associated with BUFFER have been
deleted or killed.  */)
  (register Lisp_Object buffer)
{
#ifdef subprocesses
  register Lisp_Object buf, tail, proc;

  if (NILP (buffer)) return Qnil;
  buf = Fget_buffer (buffer);
  if (NILP (buf)) return Qnil;

  FOR_EACH_PROCESS (tail, proc)
    if (EQ (XPROCESS (proc)->buffer, buf))
      return proc;
#endif	/* subprocesses */
  return Qnil;
}

DEFUN ("process-inherit-coding-system-flag",
       Fprocess_inherit_coding_system_flag, Sprocess_inherit_coding_system_flag,
       1, 1, 0,
       doc: /* Return the value of inherit-coding-system flag for PROCESS.
If this flag is t, `buffer-file-coding-system' of the buffer
associated with PROCESS will inherit the coding system used to decode
the process output.  */)
  (register Lisp_Object process)
{
#ifdef subprocesses
  CHECK_PROCESS (process);
  return XPROCESS (process)->inherit_coding_system_flag ? Qt : Qnil;
#else
  /* Ignore the argument and return the value of
     inherit-process-coding-system.  */
  return inherit_process_coding_system ? Qt : Qnil;
#endif
}

/* Kill all processes associated with `buffer'.
   If `buffer' is nil, kill all processes.  */

void
kill_buffer_processes (Lisp_Object buffer)
{
#ifdef subprocesses
  Lisp_Object tail, proc;

  FOR_EACH_PROCESS (tail, proc)
    if (NILP (buffer) || EQ (XPROCESS (proc)->buffer, buffer))
      {
	if (NETCONN_P (proc) || SERIALCONN_P (proc) || PIPECONN_P (proc))
	  Fdelete_process (proc);
	else if (XPROCESS (proc)->infd >= 0)
	  process_send_signal (proc, SIGHUP, Qnil, 1);
      }
#else  /* subprocesses */
  /* Since we have no subprocesses, this does nothing.  */
#endif /* subprocesses */
}

DEFUN ("waiting-for-user-input-p", Fwaiting_for_user_input_p,
       Swaiting_for_user_input_p, 0, 0, 0,
       doc: /* Return non-nil if Emacs is waiting for input from the user.
This is intended for use by asynchronous process output filters and sentinels.  */)
  (void)
{
#ifdef subprocesses
  return (waiting_for_user_input_p ? Qt : Qnil);
#else
  return Qnil;
#endif
}

/* Stop reading input from keyboard sources.  */

void
hold_keyboard_input (void)
{
  kbd_is_on_hold = 1;
}

/* Resume reading input from keyboard sources.  */

void
unhold_keyboard_input (void)
{
  kbd_is_on_hold = 0;
}

/* Return true if keyboard input is on hold, zero otherwise.  */

bool
kbd_on_hold_p (void)
{
  return kbd_is_on_hold;
}

\f
/* Enumeration of and access to system processes a-la ps(1).  */

DEFUN ("list-system-processes", Flist_system_processes, Slist_system_processes,
       0, 0, 0,
       doc: /* Return a list of numerical process IDs of all running processes.
If this functionality is unsupported, return nil.

See `process-attributes' for getting attributes of a process given its ID.  */)
  (void)
{
  return list_system_processes ();
}

DEFUN ("process-attributes", Fprocess_attributes,
       Sprocess_attributes, 1, 1, 0,
       doc: /* Return attributes of the process given by its PID, a number.

Value is an alist where each element is a cons cell of the form

    (KEY . VALUE)

If this functionality is unsupported, the value is nil.

See `list-system-processes' for getting a list of all process IDs.

The KEYs of the attributes that this function may return are listed
below, together with the type of the associated VALUE (in parentheses).
Not all platforms support all of these attributes; unsupported
attributes will not appear in the returned alist.
Unless explicitly indicated otherwise, numbers can have either
integer or floating point values.

 euid    -- Effective user User ID of the process (number)
 user    -- User name corresponding to euid (string)
 egid    -- Effective user Group ID of the process (number)
 group   -- Group name corresponding to egid (string)
 comm    -- Command name (executable name only) (string)
 state   -- Process state code, such as "S", "R", or "T" (string)
 ppid    -- Parent process ID (number)
 pgrp    -- Process group ID (number)
 sess    -- Session ID, i.e. process ID of session leader (number)
 ttname  -- Controlling tty name (string)
 tpgid   -- ID of foreground process group on the process's tty (number)
 minflt  -- number of minor page faults (number)
 majflt  -- number of major page faults (number)
 cminflt -- cumulative number of minor page faults (number)
 cmajflt -- cumulative number of major page faults (number)
 utime   -- user time used by the process, in (current-time) format,
              which is a list of integers (HIGH LOW USEC PSEC)
 stime   -- system time used by the process (current-time)
 time    -- sum of utime and stime (current-time)
 cutime  -- user time used by the process and its children (current-time)
 cstime  -- system time used by the process and its children (current-time)
 ctime   -- sum of cutime and cstime (current-time)
 pri     -- priority of the process (number)
 nice    -- nice value of the process (number)
 thcount -- process thread count (number)
 start   -- time the process started (current-time)
 vsize   -- virtual memory size of the process in KB's (number)
 rss     -- resident set size of the process in KB's (number)
 etime   -- elapsed time the process is running, in (HIGH LOW USEC PSEC) format
 pcpu    -- percents of CPU time used by the process (floating-point number)
 pmem    -- percents of total physical memory used by process's resident set
              (floating-point number)
 args    -- command line which invoked the process (string).  */)
  ( Lisp_Object pid)
{
  return system_process_attributes (pid);
}

#ifdef subprocesses
/* Arrange to catch SIGCHLD if this hasn't already been arranged.
   Invoke this after init_process_emacs, and after glib and/or GNUstep
   futz with the SIGCHLD handler, but before Emacs forks any children.
   This function's caller should block SIGCHLD.  */

void
catch_child_signal (void)
{
  struct sigaction action, old_action;
  sigset_t oldset;
  emacs_sigaction_init (&action, deliver_child_signal);
  block_child_signal (&oldset);
  sigaction (SIGCHLD, &action, &old_action);
  eassert (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN
	   || ! (old_action.sa_flags & SA_SIGINFO));

  if (old_action.sa_handler != deliver_child_signal)
    lib_child_handler
      = (old_action.sa_handler == SIG_DFL || old_action.sa_handler == SIG_IGN
	 ? dummy_handler
	 : old_action.sa_handler);
  unblock_child_signal (&oldset);
}
#endif	/* subprocesses */

/* Limit the number of open files to the value it had at startup.  */

void
restore_nofile_limit (void)
{
#ifdef HAVE_SETRLIMIT
  if (FD_SETSIZE < nofile_limit.rlim_cur)
    setrlimit (RLIMIT_NOFILE, &nofile_limit);
#endif
}

\f
/* This is not called "init_process" because that is the name of a
   Mach system call, so it would cause problems on Darwin systems.  */
void
init_process_emacs (int sockfd)
{
#ifdef subprocesses
  int i;

  inhibit_sentinels = 0;

#ifndef CANNOT_DUMP
  if (! noninteractive || initialized)
#endif
    {
#if defined HAVE_GLIB && !defined WINDOWSNT
      /* Tickle glib's child-handling code.  Ask glib to wait for Emacs itself;
	 this should always fail, but is enough to initialize glib's
	 private SIGCHLD handler, allowing catch_child_signal to copy
	 it into lib_child_handler.  */
      g_source_unref (g_child_watch_source_new (getpid ()));
#endif
      catch_child_signal ();
    }

#ifdef HAVE_SETRLIMIT
  /* Don't allocate more than FD_SETSIZE file descriptors for Emacs itself.  */
  if (getrlimit (RLIMIT_NOFILE, &nofile_limit) != 0)
    nofile_limit.rlim_cur = 0;
  else if (FD_SETSIZE < nofile_limit.rlim_cur)
    {
      struct rlimit rlim = nofile_limit;
      rlim.rlim_cur = FD_SETSIZE;
      if (setrlimit (RLIMIT_NOFILE, &rlim) != 0)
	nofile_limit.rlim_cur = 0;
    }
#endif

  external_sock_fd = sockfd;
  Lisp_Object sockname = Qnil;
# if HAVE_GETSOCKNAME
  if (0 <= sockfd)
    {
      union u_sockaddr sa;
      socklen_t salen = sizeof sa;
      if (getsockname (sockfd, &sa.sa, &salen) == 0)
	sockname = conv_sockaddr_to_lisp (&sa.sa, salen);
    }
# endif
  Vinternal__daemon_sockname = sockname;

  max_desc = -1;
  memset (fd_callback_info, 0, sizeof (fd_callback_info));

  num_pending_connects = 0;

  process_output_delay_count = 0;
  process_output_skip = 0;

  /* Don't do this, it caused infinite select loops.  The display
     method should call add_keyboard_wait_descriptor on stdin if it
     needs that.  */
#if 0
  FD_SET (0, &input_wait_mask);
#endif

  Vprocess_alist = Qnil;
  deleted_pid_list = Qnil;
  for (i = 0; i < FD_SETSIZE; i++)
    {
      chan_process[i] = Qnil;
      proc_buffered_char[i] = -1;
    }
  memset (proc_decode_coding_system, 0, sizeof proc_decode_coding_system);
  memset (proc_encode_coding_system, 0, sizeof proc_encode_coding_system);
#ifdef DATAGRAM_SOCKETS
  memset (datagram_address, 0, sizeof datagram_address);
#endif

#if defined (DARWIN_OS)
  /* PTYs are broken on Darwin < 6, but are sometimes useful for interactive
     processes.  As such, we only change the default value.  */
 if (initialized)
  {
    char const *release = (STRINGP (Voperating_system_release)
			   ? SSDATA (Voperating_system_release)
			   : 0);
    if (!release || !release[0] || (release[0] < '7' && release[1] == '.')) {
      Vprocess_connection_type = Qnil;
    }
  }
#endif
#endif	/* subprocesses */
  kbd_is_on_hold = 0;
}

void
syms_of_process (void)
{
#ifdef subprocesses

  DEFSYM (Qprocessp, "processp");
  DEFSYM (Qrun, "run");
  DEFSYM (Qstop, "stop");
  DEFSYM (Qsignal, "signal");

  /* Qexit is already staticpro'd by syms_of_eval; don't staticpro it
     here again.  */

  DEFSYM (Qopen, "open");
  DEFSYM (Qclosed, "closed");
  DEFSYM (Qconnect, "connect");
  DEFSYM (Qfailed, "failed");
  DEFSYM (Qlisten, "listen");
  DEFSYM (Qlocal, "local");
  DEFSYM (Qipv4, "ipv4");
#ifdef AF_INET6
  DEFSYM (Qipv6, "ipv6");
#endif
  DEFSYM (Qdatagram, "datagram");
  DEFSYM (Qseqpacket, "seqpacket");

  DEFSYM (QCport, ":port");
  DEFSYM (QCspeed, ":speed");
  DEFSYM (QCprocess, ":process");

  DEFSYM (QCbytesize, ":bytesize");
  DEFSYM (QCstopbits, ":stopbits");
  DEFSYM (QCparity, ":parity");
  DEFSYM (Qodd, "odd");
  DEFSYM (Qeven, "even");
  DEFSYM (QCflowcontrol, ":flowcontrol");
  DEFSYM (Qhw, "hw");
  DEFSYM (Qsw, "sw");
  DEFSYM (QCsummary, ":summary");

  DEFSYM (Qreal, "real");
  DEFSYM (Qnetwork, "network");
  DEFSYM (Qserial, "serial");
  DEFSYM (QCbuffer, ":buffer");
  DEFSYM (QChost, ":host");
  DEFSYM (QCservice, ":service");
  DEFSYM (QClocal, ":local");
  DEFSYM (QCremote, ":remote");
  DEFSYM (QCcoding, ":coding");
  DEFSYM (QCserver, ":server");
  DEFSYM (QCnowait, ":nowait");
  DEFSYM (QCsentinel, ":sentinel");
  DEFSYM (QCuse_external_socket, ":use-external-socket");
  DEFSYM (QCtls_parameters, ":tls-parameters");
  DEFSYM (Qnsm_verify_connection, "nsm-verify-connection");
  DEFSYM (QClog, ":log");
  DEFSYM (QCnoquery, ":noquery");
  DEFSYM (QCstop, ":stop");
  DEFSYM (QCplist, ":plist");
  DEFSYM (QCcommand, ":command");
  DEFSYM (QCconnection_type, ":connection-type");
  DEFSYM (QCstderr, ":stderr");
  DEFSYM (Qpty, "pty");
  DEFSYM (Qpipe, "pipe");

  DEFSYM (Qlast_nonmenu_event, "last-nonmenu-event");

  staticpro (&Vprocess_alist);
  staticpro (&deleted_pid_list);

#endif	/* subprocesses */

  DEFSYM (QCname, ":name");
  DEFSYM (QCtype, ":type");

  DEFSYM (Qeuid, "euid");
  DEFSYM (Qegid, "egid");
  DEFSYM (Quser, "user");
  DEFSYM (Qgroup, "group");
  DEFSYM (Qcomm, "comm");
  DEFSYM (Qstate, "state");
  DEFSYM (Qppid, "ppid");
  DEFSYM (Qpgrp, "pgrp");
  DEFSYM (Qsess, "sess");
  DEFSYM (Qttname, "ttname");
  DEFSYM (Qtpgid, "tpgid");
  DEFSYM (Qminflt, "minflt");
  DEFSYM (Qmajflt, "majflt");
  DEFSYM (Qcminflt, "cminflt");
  DEFSYM (Qcmajflt, "cmajflt");
  DEFSYM (Qutime, "utime");
  DEFSYM (Qstime, "stime");
  DEFSYM (Qtime, "time");
  DEFSYM (Qcutime, "cutime");
  DEFSYM (Qcstime, "cstime");
  DEFSYM (Qctime, "ctime");
#ifdef subprocesses
  DEFSYM (Qinternal_default_process_sentinel,
	  "internal-default-process-sentinel");
  DEFSYM (Qinternal_default_process_filter,
	  "internal-default-process-filter");
#endif
  DEFSYM (Qpri, "pri");
  DEFSYM (Qnice, "nice");
  DEFSYM (Qthcount, "thcount");
  DEFSYM (Qstart, "start");
  DEFSYM (Qvsize, "vsize");
  DEFSYM (Qrss, "rss");
  DEFSYM (Qetime, "etime");
  DEFSYM (Qpcpu, "pcpu");
  DEFSYM (Qpmem, "pmem");
  DEFSYM (Qargs, "args");

  DEFVAR_BOOL ("delete-exited-processes", delete_exited_processes,
	       doc: /* Non-nil means delete processes immediately when they exit.
A value of nil means don't delete them until `list-processes' is run.  */);

  delete_exited_processes = 1;

#ifdef subprocesses
  DEFVAR_LISP ("process-connection-type", Vprocess_connection_type,
	       doc: /* Control type of device used to communicate with subprocesses.
Values are nil to use a pipe, or t or `pty' to use a pty.
The value has no effect if the system has no ptys or if all ptys are busy:
then a pipe is used in any case.
The value takes effect when `start-process' is called.  */);
  Vprocess_connection_type = Qt;

  DEFVAR_LISP ("process-adaptive-read-buffering", Vprocess_adaptive_read_buffering,
	       doc: /* If non-nil, improve receive buffering by delaying after short reads.
On some systems, when Emacs reads the output from a subprocess, the output data
is read in very small blocks, potentially resulting in very poor performance.
This behavior can be remedied to some extent by setting this variable to a
non-nil value, as it will automatically delay reading from such processes, to
allow them to produce more output before Emacs tries to read it.
If the value is t, the delay is reset after each write to the process; any other
non-nil value means that the delay is not reset on write.
The variable takes effect when `start-process' is called.  */);
  Vprocess_adaptive_read_buffering = Qt;

  DEFVAR_LISP ("interrupt-process-functions", Vinterrupt_process_functions,
	       doc: /* List of functions to be called for `interrupt-process'.
The arguments of the functions are the same as for `interrupt-process'.
These functions are called in the order of the list, until one of them
returns non-`nil'.  */);
  Vinterrupt_process_functions = list1 (Qinternal_default_interrupt_process);

  DEFVAR_LISP ("internal--daemon-sockname", Vinternal__daemon_sockname,
	       doc: /* Name of external socket passed to Emacs, or nil if none.  */);
  Vinternal__daemon_sockname = Qnil;

  DEFSYM (Qinternal_default_interrupt_process,
	  "internal-default-interrupt-process");
  DEFSYM (Qinterrupt_process_functions, "interrupt-process-functions");

  defsubr (&Sprocessp);
  defsubr (&Sget_process);
  defsubr (&Sdelete_process);
  defsubr (&Sprocess_status);
  defsubr (&Sprocess_exit_status);
  defsubr (&Sprocess_id);
  defsubr (&Sprocess_name);
  defsubr (&Sprocess_tty_name);
  defsubr (&Sprocess_command);
  defsubr (&Sset_process_buffer);
  defsubr (&Sprocess_buffer);
  defsubr (&Sprocess_mark);
  defsubr (&Sset_process_filter);
  defsubr (&Sprocess_filter);
  defsubr (&Sset_process_sentinel);
  defsubr (&Sprocess_sentinel);
  defsubr (&Sset_process_thread);
  defsubr (&Sprocess_thread);
  defsubr (&Sset_process_window_size);
  defsubr (&Sset_process_inherit_coding_system_flag);
  defsubr (&Sset_process_query_on_exit_flag);
  defsubr (&Sprocess_query_on_exit_flag);
  defsubr (&Sprocess_contact);
  defsubr (&Sprocess_plist);
  defsubr (&Sset_process_plist);
  defsubr (&Sprocess_list);
  defsubr (&Smake_process);
  defsubr (&Smake_pipe_process);
  defsubr (&Sserial_process_configure);
  defsubr (&Smake_serial_process);
  defsubr (&Sset_network_process_option);
  defsubr (&Smake_network_process);
  defsubr (&Sformat_network_address);
  defsubr (&Snetwork_interface_list);
  defsubr (&Snetwork_interface_info);
#ifdef DATAGRAM_SOCKETS
  defsubr (&Sprocess_datagram_address);
  defsubr (&Sset_process_datagram_address);
#endif
  defsubr (&Saccept_process_output);
  defsubr (&Sprocess_send_region);
  defsubr (&Sprocess_send_string);
  defsubr (&Sinternal_default_interrupt_process);
  defsubr (&Sinterrupt_process);
  defsubr (&Skill_process);
  defsubr (&Squit_process);
  defsubr (&Sstop_process);
  defsubr (&Scontinue_process);
  defsubr (&Sprocess_running_child_p);
  defsubr (&Sprocess_send_eof);
  defsubr (&Ssignal_process);
  defsubr (&Swaiting_for_user_input_p);
  defsubr (&Sprocess_type);
  defsubr (&Sinternal_default_process_sentinel);
  defsubr (&Sinternal_default_process_filter);
  defsubr (&Sset_process_coding_system);
  defsubr (&Sprocess_coding_system);
  defsubr (&Sset_process_filter_multibyte);
  defsubr (&Sprocess_filter_multibyte_p);

 {
   Lisp_Object subfeatures = Qnil;
   const struct socket_options *sopt;

#define ADD_SUBFEATURE(key, val) \
  subfeatures = pure_cons (pure_cons (key, pure_cons (val, Qnil)), subfeatures)

   ADD_SUBFEATURE (QCnowait, Qt);
#ifdef DATAGRAM_SOCKETS
   ADD_SUBFEATURE (QCtype, Qdatagram);
#endif
#ifdef HAVE_SEQPACKET
   ADD_SUBFEATURE (QCtype, Qseqpacket);
#endif
#ifdef HAVE_LOCAL_SOCKETS
   ADD_SUBFEATURE (QCfamily, Qlocal);
#endif
   ADD_SUBFEATURE (QCfamily, Qipv4);
#ifdef AF_INET6
   ADD_SUBFEATURE (QCfamily, Qipv6);
#endif
#ifdef HAVE_GETSOCKNAME
   ADD_SUBFEATURE (QCservice, Qt);
#endif
   ADD_SUBFEATURE (QCserver, Qt);

   for (sopt = socket_options; sopt->name; sopt++)
     subfeatures = pure_cons (intern_c_string (sopt->name), subfeatures);

   Fprovide (intern_c_string ("make-network-process"), subfeatures);
 }

#endif	/* subprocesses */

  defsubr (&Sget_buffer_process);
  defsubr (&Sprocess_inherit_coding_system_flag);
  defsubr (&Slist_system_processes);
  defsubr (&Sprocess_attributes);
}

debug log:

solving ed2cab7b51f ...
found ed2cab7b51f in https://git.savannah.gnu.org/cgit/emacs.git

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).