summaryrefslogtreecommitdiffstats
path: root/gnu/libexec/uucp/uucico/uucico.c
blob: 63b7116c72f8482ed7808637d051e0f403f5423e (plain)
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
/* uucico.c
   This is the main UUCP communication program.

   Copyright (C) 1991, 1992, 1993, 1994, 1995 Ian Lance Taylor

   This file is part of the Taylor UUCP package.

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

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

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

   The author of the program may be contacted at ian@airs.com or
   c/o Cygnus Support, 48 Grove Street, Somerville, MA 02144.
   */

#include "uucp.h"

#if USE_RCS_ID
const char uucico_rcsid[] = "$FreeBSD$";
#endif

#include <ctype.h>

#if HAVE_LIMITS_H
#include <limits.h>
#else
#define LONG_MAX 2147483647L
#endif

#include "getopt.h"

#include "uudefs.h"
#include "uuconf.h"
#include "conn.h"
#include "prot.h"
#include "trans.h"
#include "system.h"

#if HAVE_ENCRYPTED_PASSWORDS
#ifndef crypt
extern char *crypt ();
#endif
#endif

/* Coherent already had a different meaning for the -c option.  What a
   pain.  */
#ifdef __COHERENT__
#define COHERENT_C_OPTION 1
#else
#define COHERENT_C_OPTION 0
#endif

/* Define the known protocols.  */

#define TCP_PROTO \
  (UUCONF_RELIABLE_ENDTOEND \
   | UUCONF_RELIABLE_RELIABLE \
   | UUCONF_RELIABLE_EIGHT)

static const struct sprotocol asProtocols[] =
{
  { 't', TCP_PROTO, 1, TRUE,
      asTproto_params, ftstart, ftshutdown, ftsendcmd, ztgetspace,
      ftsenddata, ftwait, ftfile },
  { 'e', TCP_PROTO, 1, TRUE,
      asEproto_params, festart, feshutdown, fesendcmd, zegetspace,
      fesenddata, fewait, fefile },
  { 'i', UUCONF_RELIABLE_EIGHT, 7, TRUE,
      asIproto_params, fistart, fishutdown, fisendcmd, zigetspace,
      fisenddata, fiwait, NULL },
  { 'a', UUCONF_RELIABLE_EIGHT, 1, TRUE,
      asZproto_params, fzstart, fzshutdown, fzsendcmd, zzgetspace,
      fzsenddata, fzwait, fzfile },
  { 'g', UUCONF_RELIABLE_EIGHT, 1, TRUE,
      asGproto_params, fgstart, fgshutdown, fgsendcmd, zggetspace,
      fgsenddata, fgwait, NULL },
  { 'G', UUCONF_RELIABLE_EIGHT, 1, TRUE,
      asGproto_params, fbiggstart, fgshutdown, fgsendcmd, zggetspace,
      fgsenddata, fgwait, NULL },
  { 'j', UUCONF_RELIABLE_EIGHT, 7, TRUE,
      asIproto_params, fjstart, fjshutdown, fisendcmd, zigetspace,
      fisenddata, fiwait, NULL },
  { 'f', UUCONF_RELIABLE_RELIABLE, 1, FALSE,
      asFproto_params, ffstart, ffshutdown, ffsendcmd, zfgetspace,
      ffsenddata, ffwait, fffile },
  { 'v', UUCONF_RELIABLE_EIGHT, 1, TRUE,
      asGproto_params, fvstart, fgshutdown, fgsendcmd, zggetspace,
      fgsenddata, fgwait, NULL },
  { 'y', UUCONF_RELIABLE_RELIABLE | UUCONF_RELIABLE_EIGHT, 1, TRUE,
      asYproto_params, fystart, fyshutdown, fysendcmd, zygetspace,
      fysenddata, fywait, fyfile }
};

#define CPROTOCOLS (sizeof asProtocols / sizeof asProtocols[0])

/* Locked system.  */
static boolean fLocked_system;
static struct uuconf_system sLocked_system;

/* Daemon structure holding information about the remote system (must
   be global so the error handler can see it.  */
static struct sdaemon sDaemon;

/* Open connection.  */
static struct sconnection *qConn;

/* uuconf global pointer; need to close the connection after a fatal
   error.  */
static pointer pUuconf;

/* This structure is passed to iuport_lock via uuconf_find_port.  */
struct spass
{
  boolean fmatched;
  boolean flocked;
  struct sconnection *qconn;
};

/* Local functions.  */

static void uusage P((void));
static void uhelp P((void));
static void uabort P((void));
static boolean fcall P((pointer puuconf, const char *zconfig, boolean fuuxqt,
			const struct uuconf_system *qsys,
			struct uuconf_port *qport, boolean fifwork,
			boolean fforce, boolean fdetach,
			boolean fquiet, boolean ftrynext));
static boolean fconn_call P((struct sdaemon *qdaemon,
			     struct uuconf_port *qport,
			     struct sstatus *qstat, int cretry,
			     boolean *pfcalled));
static boolean fdo_call P((struct sdaemon *qdaemon,
			   struct sstatus *qstat,
			   const struct uuconf_dialer *qdialer,
			   boolean *pfcalled, enum tstatus_type *pterr));
static int iuport_lock P((struct uuconf_port *qport, pointer pinfo));
static boolean flogin_prompt P((pointer puuconf, const char *zconfig,
				boolean fuuxqt, struct sconnection *qconn,
				const char *zlogin, const char **pzsystem));
static int icallin_cmp P((int iwhich, pointer pinfo, const char *zfile));
static boolean faccept_call P((pointer puuconf, const char *zconfig,
			       boolean fuuxqt, const char *zlogin,
			       struct sconnection *qconn,
			       const char **pzsystem));
static void uaccept_call_cleanup P((pointer puuconf,
				    struct uuconf_system *qfreesys,
				    struct uuconf_port *qport,
				    struct uuconf_port *qfreeport,
				    char *zloc));
static void uapply_proto_params P((pointer puuconf, int bproto,
				   struct uuconf_cmdtab *qcmds,
				   struct uuconf_proto_param *pas));
static boolean fsend_uucp_cmd P((struct sconnection *qconn,
				 const char *z));
static char *zget_uucp_cmd P((struct sconnection *qconn,
			      boolean frequired, boolean fstrip));
static char *zget_typed_line P((struct sconnection *qconn,
				boolean fstrip));

/* Long getopt options.  */
static const struct option asLongopts[] =
{
  { "quiet", no_argument, NULL, 2 },
  { "ifwork", no_argument, NULL, 'C' },
  { "nodetach", no_argument, NULL, 'D' },
  { "loop", no_argument, NULL, 'e' },
  { "force", no_argument, NULL, 'f'},
  { "stdin", required_argument, NULL, 'i' },
  { "prompt", no_argument, NULL, 'l' },
  { "port", required_argument, NULL, 'p' },
  { "nouuxqt", no_argument, NULL, 'q' },
  { "master", no_argument, NULL, 3 },
  { "slave", no_argument, NULL, 4 },
  { "system", required_argument, NULL, 's' },
  { "login", required_argument, NULL, 'u' },
  { "wait", no_argument, NULL, 'w' },
  { "try-next", no_argument, NULL, 'z' },
  { "config", required_argument, NULL, 'I' },
  { "debug", required_argument, NULL, 'x' },
  { "version", no_argument, NULL, 'v' },
  { "help", no_argument, NULL, 1 },
  { NULL, 0, NULL, 0 }
};

int
main (argc, argv)
     int argc;
     char **argv;
{
  /* -c: Whether to be quiet.  */
  boolean fquiet = FALSE;
  /* -C: Only call the system if there is work.  */
  boolean fifwork = FALSE;
  /* -D: don't detach from controlling terminal.  */
  boolean fdetach = TRUE;
  /* -e: Whether to do an endless loop of accepting calls.  */
  boolean fendless = FALSE;
  /* -f: Whether to force a call despite status of previous call.  */
  boolean fforce = FALSE;
  /* -i type: type of port to use for stdin.  */
  enum uuconf_porttype tstdintype = UUCONF_PORTTYPE_STDIN;
  /* -I file: configuration file name.  */
  const char *zconfig = NULL;
  /* -l: Whether to give a single login prompt.  */
  boolean flogin = FALSE;
  /* -P port: port to use; in master mode, call out on this port.  In
     slave mode, accept logins on this port.  If port not specified,
     then in master mode figure it out for each system, and in slave
     mode use stdin and stdout.  */
  const char *zport = NULL;
  /* -q: Whether to start uuxqt when done.  */
  boolean fuuxqt = TRUE;
  /* -r1: Whether we are the master.  */
  boolean fmaster = FALSE;
  /* -s,-S system: system to call.  */
  const char *zsystem = NULL;
  /* -u: Login name to use.  */
  const char *zlogin = NULL;
  /* -w: Whether to wait for a call after doing one.  */
  boolean fwait = FALSE;
  /* -z: Try next alternate if call fails.  */
  boolean ftrynext = FALSE;
  const char *zopts;
  int iopt;
  struct uuconf_port *qport;
  struct uuconf_port sport;
  boolean fret = TRUE;
  pointer puuconf;
  int iuuconf;
#if DEBUG > 1
  int iholddebug;
#endif

  zProgram = argv[0];

  /* When uucico is invoked by login, the first character of the
     program will be a dash.  We don't want that.  */
  if (*zProgram == '-')
    ++zProgram;

#if COHERENT_C_OPTION
  zopts = "c:CDefi:I:lp:qr:s:S:u:x:X:vwz";
#else
  zopts = "cCDefi:I:lp:qr:s:S:u:x:X:vwz";
#endif

  while ((iopt = getopt_long (argc, argv, zopts,
			      asLongopts, (int *) NULL)) != EOF)
    {
#if COHERENT_C_OPTION
      if (iopt == 'c')
	{
	  iopt = 's';
	  fifwork = TRUE;
	}
#endif
      switch (iopt)
	{
	case 2:
	case 'c':
	  /* Don't warn if a call is attempted at a bad time, and
	     don't print the "No work" message.  */
	  fquiet = TRUE;
	  break;

	case 'C':
	  fifwork = TRUE;
	  break;

	case 'D':
	  /* Don't detach from controlling terminal.  */
	  fdetach = FALSE;
	  break;

	case 'e':
	  /* Do an endless loop of accepting calls.  */
	  fendless = TRUE;
	  break;

	case 'f':
	  /* Force a call even if it hasn't been long enough since the last
	     failed call.  */
	  fforce = TRUE;
	  break;

	case 'i':
	  /* Type of port to use for standard input.  Only TLI is
	     supported here, and only if HAVE_TLI is true.  This
	     permits the Network Listener to tell uucico to use TLI
	     I/O calls.  */
	  if (strcasecmp (optarg, "tli") != 0)
	    fprintf (stderr, "%s: unsupported port type \"%s\"\n",
		     zProgram, optarg);
	  else
	    {
#if HAVE_TLI
	      tstdintype = UUCONF_PORTTYPE_TLI;
#else
	      fprintf (stderr, "%s: not compiled with TLI support\n",
		       zProgram);
#endif
	    }
	  break;

	case 'l':
	  /* Prompt for login name and password.  */
	  flogin = TRUE;
	  break;

	case 'p':
	  /* Port to use  */
	  zport = optarg;
	  break;

	case 'q':
	  /* Don't start uuxqt.  */
	  fuuxqt = FALSE;
	  break;

	case 'r':
	  /* Set mode: -r1 for master, -r0 for slave (default)  */
	  if (strcmp (optarg, "1") == 0)
	    fmaster = TRUE;
	  else if (strcmp (optarg, "0") == 0)
	    fmaster = FALSE;
	  else
	    uusage ();
	  break;
    
	case 's':
	  /* Set system name  */
	  zsystem = optarg;
	  fmaster = TRUE;
	  break;

	case 'S':
	  /* Set system name and force call like -f  */
	  zsystem = optarg;
	  fforce = TRUE;
	  fmaster = TRUE;
	  break;

	case 'u':
	  /* Some versions of uucpd invoke uucico with a -u argument
	     specifying the login name.  If invoked by a privileged
	     user, we use it instead of the result of
	     zsysdep_login_name.  */
	  if (fsysdep_privileged ())
	    zlogin = optarg;
	  else
	    fprintf (stderr,
		     "%s: ignoring command line login name: not a privileged user\n",
		     zProgram);
	  break;

	case 'w':
	  /* Call out and then wait for a call in  */
	  fwait = TRUE;
	  break;

	case 'z':
	  /* Try next alternate if call fails.  */
	  ftrynext = TRUE;
	  break;

	case 'I':
	  /* Set configuration file name (default is in sysdep.h).  */
	  if (fsysdep_other_config (optarg))
	    zconfig = optarg;
	  break;

	case 'x':
	case 'X':
#if DEBUG > 1
	  /* Set debugging level.  */
	  iDebug |= idebug_parse (optarg);
#endif
	  break;

	case 'v':
	  /* Print version and exit.  */
	  printf ("%s: Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
		  zProgram, VERSION);
	  exit (EXIT_SUCCESS);
	  /*NOTREACHED*/

	case 4:
	  /* --slave.  */
	  fmaster = FALSE;
	  break;

	case 3:
	  /* --master.  */
	  fmaster = TRUE;
	  break;

	case 1:
	  /* --help.  */
	  uhelp ();
	  exit (EXIT_SUCCESS);
	  /*NOTREACHED*/

	case 0:
	  /* Long option found, and flag value set.  */
	  break;

	default:
	  uusage ();
	  /*NOTREACHED*/
	}
    }

  if (optind != argc)
    uusage ();

  if (fwait && zport == NULL)
    {
      fprintf (stderr, "%s: -w requires -p", zProgram);
      uusage ();
    }

  iuuconf = uuconf_init (&puuconf, (const char *) NULL, zconfig);
  if (iuuconf != UUCONF_SUCCESS)
    ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
  pUuconf = puuconf;

#if DEBUG > 1
  {
    const char *zdebug;

    iuuconf = uuconf_debuglevel (puuconf, &zdebug);
    if (iuuconf != UUCONF_SUCCESS)
      ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
    if (zdebug != NULL)
      iDebug |= idebug_parse (zdebug);
  }
#endif

  /* If a port was named, get its information.  */
  if (zport == NULL)
    qport = NULL;
  else
    {
      iuuconf = uuconf_find_port (puuconf, zport, (long) 0, (long) 0,
				  (int (*) P((struct uuconf_port *,
					      pointer))) NULL,
				  (pointer) NULL, &sport);
      if (iuuconf == UUCONF_NOT_FOUND)
	ulog (LOG_FATAL, "%s: port not found", zport);
      else if (iuuconf != UUCONF_SUCCESS)
	ulog_uuconf (LOG_FATAL, puuconf, iuuconf);
      qport = &sport;
    }

#ifdef SIGINT
  usysdep_signal (SIGINT);
#endif
#ifdef SIGHUP
  usysdep_signal (SIGHUP);
#endif
#ifdef SIGQUIT
  usysdep_signal (SIGQUIT);
#endif
#ifdef SIGTERM
  usysdep_signal (SIGTERM);
#endif
#ifdef SIGPIPE
  usysdep_signal (SIGPIPE);
#endif

  usysdep_initialize (puuconf, INIT_SUID);

  ulog_to_file (puuconf, TRUE);
  ulog_fatal_fn (uabort);

  if (fmaster)
    {
      if (zsystem != NULL)
	{
	  /* A system was named.  Call it.  */
	  iuuconf = uuconf_system_info (puuconf, zsystem,
					&sLocked_system);
	  if (iuuconf == UUCONF_NOT_FOUND)
	    ulog (LOG_FATAL, "%s: System not found", zsystem);
	  else if (iuuconf != UUCONF_SUCCESS)
	    ulog_uuconf (LOG_FATAL, puuconf, iuuconf);

	  /* Detach from the controlling terminal for the call.  This
	     probably makes sense only on Unix.  We want the modem
	     line to become the controlling terminal.  */
	  if (fdetach &&
	      (qport == NULL
	       || qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN))
	    usysdep_detach ();

	  ulog_system (sLocked_system.uuconf_zname);

#if DEBUG > 1
	  iholddebug = iDebug;
	  if (sLocked_system.uuconf_zdebug != NULL)
	    iDebug |= idebug_parse (sLocked_system.uuconf_zdebug);
#endif

	  if (! fsysdep_lock_system (&sLocked_system))
	    {
	      ulog (LOG_ERROR, "System already locked");
	      fret = FALSE;
	    }
	  else
	    {
	      fLocked_system = TRUE;
	      fret = fcall (puuconf, zconfig, fuuxqt, &sLocked_system, qport,
			    fifwork, fforce, fdetach, fquiet, ftrynext);
	      if (fLocked_system)
		{
		  (void) fsysdep_unlock_system (&sLocked_system);
		  fLocked_system = FALSE;
		}
	    }
#if DEBUG > 1
	  iDebug = iholddebug;
#endif
	  ulog_system ((const char *) NULL);
	  (void) uuconf_system_free (puuconf, &sLocked_system);
	}
      else
	{
	  char **pznames, **pz;
	  int c, i;
	  boolean fdidone;

	  /* Call all systems which have work to do.  */
	  fret = TRUE;
	  fdidone = FALSE;

	  iuuconf = uuconf_system_names (puuconf, &pznames, 0);
	  if (iuuconf != UUCONF_SUCCESS)
	    ulog_uuconf (LOG_FATAL, puuconf, iuuconf);

	  /* Randomize the order in which we call the systems.  */
	  c = 0;
	  for (pz = pznames; *pz != NULL; pz++)
	    c++;

	  srand ((unsigned int) ixsysdep_time ((long *) NULL));
	  for (i = c - 1; i > 0; i--)
	    {
	      int iuse;
	      char *zhold;

	      iuse = rand () % (i + 1);
	      zhold = pznames[i];
	      pznames[i] = pznames[iuse];
	      pznames[iuse] = zhold;
	    }

	  for (pz = pznames; *pz != NULL && ! FGOT_SIGNAL (); pz++)
	    {
	      iuuconf = uuconf_system_info (puuconf, *pz,
					    &sLocked_system);
	      if (iuuconf != UUCONF_SUCCESS)
		{
		  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
		  xfree ((pointer) *pz);
		  continue;
		}

	      if (fsysdep_has_work (&sLocked_system))
		{
		  fdidone = TRUE;

		  /* Detach from the controlling terminal.  On Unix
		     this means that we will wind up forking a new
		     process for each system we call.  */
		  if (fdetach
		      && (qport == NULL
			  || qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN))
		    usysdep_detach ();

		  ulog_system (sLocked_system.uuconf_zname);

#if DEBUG > 1
		  iholddebug = iDebug;
		  if (sLocked_system.uuconf_zdebug != NULL)
		    iDebug |= idebug_parse (sLocked_system.uuconf_zdebug);
#endif

		  if (! fsysdep_lock_system (&sLocked_system))
		    {
		      ulog (LOG_ERROR, "System already locked");
		      fret = FALSE;
		    }
		  else
		    {
		      fLocked_system = TRUE;
		      if (! fcall (puuconf, zconfig, fuuxqt, &sLocked_system,
				   qport, TRUE, fforce, fdetach, fquiet,
				   ftrynext))
			fret = FALSE;

		      /* Now ignore any SIGHUP that we got.  */
		      afSignal[INDEXSIG_SIGHUP] = FALSE;

		      if (fLocked_system)
			{
			  (void) fsysdep_unlock_system (&sLocked_system);
			  fLocked_system = FALSE;
			}
		    }
#if DEBUG > 1
		  iDebug = iholddebug;
#endif
		  ulog_system ((const char *) NULL);
		}

	      (void) uuconf_system_free (puuconf, &sLocked_system);
	      xfree ((pointer) *pz);
	    }

	  xfree ((pointer) pznames);

	  if (! fdidone && ! fquiet)
	    ulog (LOG_NORMAL, "No work");
	}

      /* If requested, wait for calls after dialing out.  */
      if (fwait)
	{
	  fendless = TRUE;
	  fmaster = FALSE;
	}
    }

  if (! fmaster)
    {
      struct sconnection sconn;
      boolean flocked;

      /* If a port was specified by name, we go into endless loop
	 mode.  In this mode, we wait for calls and prompt them with
	 "login:" and "Password:", so that they think we are a regular
	 UNIX system.  If we aren't in endless loop mode, we have been
	 called by some other system.  If flogin is TRUE, we prompt
	 with "login:" and "Password:" a single time.  */

      fret = TRUE;
      zsystem = NULL;

      if (! fconn_init (qport, &sconn, tstdintype))
	fret = FALSE;

      if (qport != NULL)
	{
	  /* We are not using standard input.  Detach from the
	     controlling terminal, so that the port we are about to
	     use becomes our controlling terminal.  */
	  if (fdetach
	      && qport->uuconf_ttype != UUCONF_PORTTYPE_STDIN)
	    usysdep_detach ();
	}

      if (fconn_lock (&sconn, TRUE))
	flocked = TRUE;
      else
	{
	  flocked = FALSE;
	  ulog (LOG_ERROR, "%s: Port already locked",
		qport->uuconf_zname);
	  fret = FALSE;
	}

      if (fret)
	{
	  if (! fconn_open (&sconn, (long) 0, (long) 0, TRUE))
	    fret = FALSE;
	  qConn = &sconn;
	}

      if (fret)
	{
	  if (fendless)
	    {
	      while (! FGOT_SIGNAL ()
		     && flogin_prompt (puuconf, zconfig, fuuxqt, &sconn,
				       (const char *) NULL,
				       (const char **) NULL))
		{
		  /* Close and reopen the port in between calls.  */
		  if (! fconn_close (&sconn, puuconf,
				     (struct uuconf_dialer *) NULL,
				     TRUE)
		      || ! fconn_open (&sconn, (long) 0, (long) 0, TRUE))
		    break;
		}
	      fret = FALSE;
	    }
	  else
	    {
	      if (flogin)
		fret = flogin_prompt (puuconf, zconfig, fuuxqt, &sconn,
				      zlogin, &zsystem);
	      else
		{
#if DEBUG > 1
		  iholddebug = iDebug;
#endif
		  if (zlogin == NULL)
		    zlogin = zsysdep_login_name ();
		  fret = faccept_call (puuconf, zconfig, fuuxqt, zlogin,
				       &sconn, &zsystem);
#if DEBUG > 1
		  iDebug = iholddebug;
#endif
		}
	    }
	}

      if (qConn != NULL)
	{
	  if (! fconn_close (&sconn, puuconf, (struct uuconf_dialer *) NULL,
			     fret))
	    fret = FALSE;
	  qConn = NULL;
	}

      if (flocked)
	(void) fconn_unlock (&sconn);

      uconn_free (&sconn);
    }

  ulog_close ();
  ustats_close ();

  /* If we got a SIGTERM, perhaps because the system is going down,
     don't run uuxqt.  We go ahead and run it for any other signal,
     since I think they indicate more temporary conditions.  */
  if (afSignal[INDEXSIG_SIGTERM])
    fuuxqt = FALSE;

  if (fuuxqt)
    {
      int irunuuxqt;

      iuuconf = uuconf_runuuxqt (puuconf, &irunuuxqt);
      if (iuuconf != UUCONF_SUCCESS)
	ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      else if (irunuuxqt == UUCONF_RUNUUXQT_ONCE)
	{
	  /* Detach from the controlling terminal before starting up uuxqt,
	     so that it runs as a true daemon.  */
	  if (fdetach)
	    usysdep_detach ();

	  if (! fspawn_uuxqt (FALSE, zsystem, zconfig))
	    fret = FALSE;
	}
    }

  usysdep_exit (fret);

  /* Avoid complaints about not returning.  */
  return 0;
}

/* Print out a usage message and die.  */

static void
uusage ()
{
  fprintf (stderr, "Usage: %s [options]\n", zProgram);
  fprintf (stderr, "Use %s --help for help\n", zProgram);
  exit (EXIT_FAILURE);
}

/* Print a help message.  */

static void
uhelp ()
{
  printf ("Taylor UUCP %s, copyright (C) 1991, 92, 93, 94, 1995 Ian Lance Taylor\n",
	   VERSION);
  printf ("Usage: %s [options]\n", zProgram);
  printf (" -s,-S,--system system: Call system (-S implies -f)\n");
  printf (" -f,--force: Force call despite system status\n");
  printf (" -r state: 1 for master, 0 for slave (default)\n");
  printf (" --master: Act as master\n");
  printf (" --slave: Act as slave (default)\n");
  printf (" -p,--port port: Specify port\n");
  printf (" -l,--prompt: prompt for login name and password\n");
  printf (" -e,--loop: Endless loop of login prompts and daemon execution\n");
  printf (" -w,--wait: After calling out, wait for incoming calls\n");
  printf (" -q,--nouuxqt: Don't start uuxqt when done\n");
  printf (" -c,--quiet: Don't log bad time or no work warnings\n");
  printf (" -C,--ifwork: Only call named system if there is work\n");
  printf (" -D,--nodetach: Don't detach from controlling terminal\n");
  printf (" -u,--login: Set login name (privileged users only)\n");
  printf (" -i,--stdin type: Type of standard input (only TLI supported)\n");
  printf (" -z,--try-next: If a call fails, try the next alternate\n");
  printf (" -x,-X,--debug debug: Set debugging level\n");
#if HAVE_TAYLOR_CONFIG
  printf (" -I,--config file: Set configuration file to use\n");
#endif /* HAVE_TAYLOR_CONFIG */
  printf (" -v,--version: Print version and exit\n");
  printf (" --help: Print help and exit\n");
}

/* This function is called when a LOG_FATAL error occurs.  */

static void
uabort ()
{
  if (fLocked_system)
    ufailed (&sDaemon);

  ulog_user ((const char *) NULL);

  if (qConn != NULL)
    {
      (void) fconn_close (qConn, pUuconf, (struct uuconf_dialer *) NULL,
			  FALSE);
      (void) fconn_unlock (qConn);
      uconn_free (qConn);
    }

  if (fLocked_system)
    {
      (void) fsysdep_unlock_system (&sLocked_system);
      fLocked_system = FALSE;
    }

  ulog_system ((const char *) NULL);

  ulog_close ();
  ustats_close ();

  usysdep_exit (FALSE);
}

/* The number of seconds in one day.  We must cast to long for this
   to be calculated correctly on a machine with 16 bit ints.  */
#define SECS_PER_DAY ((long) 24 * (long) 60 * (long) 60)

/* Call another system, trying all the possible sets of calling
   instructions.  The qsys argument is the system to call.  The qport
   argument is the port to use, and may be NULL.  If the fifwork
   argument is TRUE, the call is only placed if there is work to be
   done.  If the fforce argument is TRUE, a call is forced even if not
   enough time has passed since the last failed call.  If the fquiet
   argument is FALSE (the normal case), then a warning is given if
   calls are not permitted at this time.  */

static boolean
fcall (puuconf, zconfig, fuuxqt, qorigsys, qport, fifwork, fforce, fdetach,
       fquiet, ftrynext)
     pointer puuconf;
     const char *zconfig;
     boolean fuuxqt;
     const struct uuconf_system *qorigsys;
     struct uuconf_port *qport;
     boolean fifwork;
     boolean fforce;
     boolean fdetach;
     boolean fquiet;
     boolean ftrynext;
{
  struct sstatus sstat;
  long inow;
  boolean fbadtime, fnevertime, ffoundwork;
  const struct uuconf_system *qsys;

  if (! fsysdep_get_status (qorigsys, &sstat, (boolean *) NULL))
    return FALSE;
  ubuffree (sstat.zstring);

  /* Make sure it's been long enough since the last failed call, and
     that we haven't exceeded the maximum number of retries.  Even if
     we are over the limit on retries, we permit a call to be made if
     24 hours have passed.  This 24 hour limit is still controlled by
     the retry time.  We ignore times in the future, presumably the
     result of some sort of error.  */
  inow = ixsysdep_time ((long *) NULL);
  if (! fforce)
    {
      if (qorigsys->uuconf_cmax_retries > 0
	  && sstat.cretries >= qorigsys->uuconf_cmax_retries
	  && sstat.ilast <= inow
	  && sstat.ilast + SECS_PER_DAY > inow)
	{
	  ulog (LOG_ERROR, "Too many retries");
	  return FALSE;
	}

      if ((sstat.ttype == STATUS_COMPLETE
	   ? sstat.ilast + qorigsys->uuconf_csuccess_wait > inow
	   : sstat.ilast + sstat.cwait > inow)
	  && sstat.ilast <= inow)
	{
	  ulog (LOG_NORMAL, "Retry time not reached");
	  return FALSE;
	}
    }

  sDaemon.puuconf = puuconf;
  sDaemon.zconfig = zconfig;
  if (! fuuxqt)
    sDaemon.irunuuxqt = UUCONF_RUNUUXQT_NEVER;
  else
    {
      int iuuconf;

      iuuconf = uuconf_runuuxqt (puuconf, &sDaemon.irunuuxqt);
      if (iuuconf != UUCONF_SUCCESS)
	ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
    }

  fbadtime = TRUE;
  fnevertime = TRUE;
  ffoundwork = FALSE;

  for (qsys = qorigsys; qsys != NULL; qsys = qsys->uuconf_qalternate)
    {
      int cretry;
      boolean fany, fret, fcalled;

      if (FGOT_SIGNAL ())
	return FALSE;

      if (! qsys->uuconf_fcall || qsys->uuconf_qtimegrade == NULL)
	continue;

      fnevertime = FALSE;

      /* Make sure this is a legal time to call.  */
      if (! ftimespan_match (qsys->uuconf_qtimegrade, (long *) NULL,
			     &cretry))
	continue;

      fbadtime = FALSE;

      sDaemon.qsys = qsys;
      sDaemon.zlocalname = NULL;
      sDaemon.qconn = NULL;
      sDaemon.qproto = NULL;
      sDaemon.cchans = 1;
      sDaemon.clocal_size = -1;
      sDaemon.cremote_size = -1;
      sDaemon.cmax_ever = -2;
      sDaemon.cmax_receive = -1;
      sDaemon.csent = 0;
      sDaemon.creceived = 0;
      sDaemon.cxfiles_received = 0;
      sDaemon.ifeatures = 0;
      sDaemon.frequest_hangup = FALSE;
      sDaemon.fhangup_requested = FALSE;
      sDaemon.fhangup = FALSE;
      sDaemon.fmaster = TRUE;
      sDaemon.fcaller = TRUE;
      sDaemon.ireliable = 0;
      sDaemon.bgrade = '\0';

      /* Queue up any work there is to do.  */
      if (! fqueue (&sDaemon, &fany))
	return FALSE;

      /* If we are only supposed to call if there is work, and there
	 isn't any work, check the next alternates.  We can't give up
	 at this point because there might be some other alternates
	 with fewer restrictions on grade or file transfer size.  */
      if (fifwork && ! fany)
	{
	  uclear_queue (&sDaemon);
	  continue;
	}

      ffoundwork = TRUE;

      fret = fconn_call (&sDaemon, qport, &sstat, cretry, &fcalled);

      uclear_queue (&sDaemon);

      if (fret)
	return TRUE;
      if (fcalled && ! ftrynext)
	return FALSE;

      /* Now we have to dump that port so that we can aquire a new
	 one.  On Unix this means that we will fork and get a new
	 process ID, so we must unlock and relock the system.  */
      if (fdetach)
	{
	  (void) fsysdep_unlock_system (&sLocked_system);
	  fLocked_system = FALSE;
	  usysdep_detach ();
	  if (! fsysdep_lock_system (&sLocked_system))
	    return FALSE;
	  fLocked_system = TRUE;
	}
    }

  /* We only get here if no call succeeded.  If fbadtime is TRUE it
     was the wrong time for all the alternates.  Otherwise, if
     ffoundwork is FALSE there was no work for any of the alternates.
     Otherwise, we attempted a call and fconn_call logged an error
     message.  */

  if (fbadtime)
    {
      if (! fquiet)
	ulog (LOG_NORMAL, "Wrong time to call");

      /* Update the status, unless the system can never be called.  If
	 the system can never be called, there is little point to
	 putting in a ``wrong time to call'' message.  We don't change
	 the number of retries, although we do set the wait until the
	 next retry to 0.  */
      if (! fnevertime)
	{
	  sstat.ttype = STATUS_WRONG_TIME;
	  sstat.ilast = inow;
	  sstat.cwait = 0;
	  (void) fsysdep_set_status (qorigsys, &sstat);
	}
    }
  else if (! ffoundwork)
    {
      if (! fquiet)
	ulog (LOG_NORMAL, "No work");
      return TRUE;
    }

  return FALSE;
}

/* Find a port to use when calling a system, open a connection, and
   dial the system.  The actual call is done in fdo_call.  This
   routine is responsible for opening and closing the connection.  */

static boolean
fconn_call (qdaemon, qport, qstat, cretry, pfcalled)
     struct sdaemon *qdaemon;
     struct uuconf_port *qport;
     struct sstatus *qstat;
     int cretry;
     boolean *pfcalled;
{
  pointer puuconf;
  const struct uuconf_system *qsys;
  struct uuconf_port sport;
  struct sconnection sconn;
  enum tstatus_type terr;
  boolean fret;

  puuconf = qdaemon->puuconf;
  qsys = qdaemon->qsys;

  *pfcalled = FALSE;

  /* Ignore any SIGHUP signal we may have received up to this point.
     This is needed on Unix because we may have gotten one from the
     shell before we detached from the controlling terminal.  */
  afSignal[INDEXSIG_SIGHUP] = FALSE;

  /* If no port was specified on the command line, use any port
     defined for the system.  To select the system port: 1) see if
     port information was specified directly; 2) see if a port was
     named; 3) get an available port given the baud rate.  We don't
     change the system status if a port is unavailable; i.e. we don't
     force the system to wait for the retry time.  */
  if (qport == NULL)
    qport = qsys->uuconf_qport;
  if (qport != NULL)
    {
      if (! fconn_init (qport, &sconn, UUCONF_PORTTYPE_UNKNOWN))
	return FALSE;
      if (! fconn_lock (&sconn, FALSE))
	{
	  ulog (LOG_ERROR, "%s: Port already locked",
		qport->uuconf_zname);
	  return FALSE;
	}
    }
  else
    {
      struct spass s;
      int iuuconf;

      s.fmatched = FALSE;
      s.flocked = FALSE;
      s.qconn = &sconn;
      iuuconf = uuconf_find_port (puuconf, qsys->uuconf_zport,
				  qsys->uuconf_ibaud,
				  qsys->uuconf_ihighbaud,
				  iuport_lock, (pointer) &s,
				  &sport);
      if (iuuconf == UUCONF_NOT_FOUND)
	{
	  if (s.fmatched)
	    ulog (LOG_ERROR, "All matching ports in use");
	  else
	    ulog (LOG_ERROR, "No matching ports");
	  return FALSE;
	}
      else if (iuuconf != UUCONF_SUCCESS)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  if (s.flocked)
	    {
	      (void) fconn_unlock (&sconn);
	      uconn_free (&sconn);
	    }
	  return FALSE;
	}
    }

  if (! fconn_open (&sconn, qsys->uuconf_ibaud, qsys->uuconf_ihighbaud,
		    FALSE))
    {
      terr = STATUS_PORT_FAILED;
      fret = FALSE;
    }
  else
    {
      struct uuconf_dialer *qdialer;
      struct uuconf_dialer sdialer;
      enum tdialerfound tdialer;

      if (qsys->uuconf_zalternate == NULL)
	ulog (LOG_NORMAL, "Calling system %s (port %s)", qsys->uuconf_zname,
	      zLdevice == NULL ? (char *) "unknown" : zLdevice);
      else
	ulog (LOG_NORMAL, "Calling system %s (alternate %s, port %s)",
	      qsys->uuconf_zname, qsys->uuconf_zalternate,
	  zLdevice == NULL ? (char *) "unknown" : zLdevice);

      qdialer = NULL;

      if (! fconn_dial (&sconn, puuconf, qsys, qsys->uuconf_zphone,
			&sdialer, &tdialer))
	{
	  tdialer = DIALERFOUND_FALSE;
	  terr = STATUS_DIAL_FAILED;
	  fret = FALSE;
	}
      else
	{
	  qdaemon->qconn = &sconn;
	  if (tdialer == DIALERFOUND_FALSE)
	    qdialer = NULL;
	  else
	    qdialer = &sdialer;
	  fret = fdo_call (qdaemon, qstat, qdialer, pfcalled, &terr);
	}

      (void) fconn_close (&sconn, puuconf, qdialer, fret);

      if (tdialer == DIALERFOUND_FREE)
	(void) uuconf_dialer_free (puuconf, &sdialer);
    }

  if (! fret)
    {
      DEBUG_MESSAGE2 (DEBUG_HANDSHAKE, "Call failed: %d (%s)",
		      (int) terr, azStatus[(int) terr]);
      qstat->ttype = terr;
      qstat->cretries++;
      qstat->ilast = ixsysdep_time ((long *) NULL);
      if (cretry == 0)
	qstat->cwait = CRETRY_WAIT (qstat->cretries);
      else
	qstat->cwait = cretry * 60;
      (void) fsysdep_set_status (qsys, qstat);
    }

  (void) fconn_unlock (&sconn);
  uconn_free (&sconn);

  if (qport == NULL)
    (void) uuconf_port_free (puuconf, &sport);

  return fret;
}

/* Do the actual work of calling another system.  The qsys argument is
   the system to call, the qconn argument is the connection to use,
   the qstat argument holds the current status of the ssystem, and the
   qdialer argument holds the dialer being used (it may be NULL).  If
   we log in successfully, set *pfcalled to TRUE; this is used to
   distinguish a failed dial from a failure during the call.  If an
   error occurs *pterr is set to the status type to record.  */

static boolean
fdo_call (qdaemon, qstat, qdialer, pfcalled, pterr)
     struct sdaemon *qdaemon;
     struct sstatus *qstat;
     const struct uuconf_dialer *qdialer;
     boolean *pfcalled;
     enum tstatus_type *pterr;
{
  pointer puuconf;
  const struct uuconf_system *qsys;
  struct sconnection *qconn;
  int iuuconf;
  int istrip;
  boolean fstrip;
  const char *zport;
  char *zstr;
  long istart_time;
  char *zlog;

  puuconf = qdaemon->puuconf;
  qsys = qdaemon->qsys;
  qconn = qdaemon->qconn;

  iuuconf = uuconf_strip (puuconf, &istrip);
  if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      return FALSE;
    }
  fstrip = (istrip & UUCONF_STRIP_PROTO) != 0;

  *pterr = STATUS_LOGIN_FAILED;

  if (qconn->qport == NULL)
    zport = "unknown";
  else
    zport = qconn->qport->uuconf_zname;
  if (! fchat (qconn, puuconf, &qsys->uuconf_schat, qsys,
	       (const struct uuconf_dialer *) NULL,
	       (const char *) NULL, FALSE, zport,
	       iconn_baud (qconn)))
    return FALSE;

  *pfcalled = TRUE;
  istart_time = ixsysdep_time ((long *) NULL);

  *pterr = STATUS_HANDSHAKE_FAILED;

  /* We should now see "Shere" from the other system.  Newer systems
     send "Shere=foo" where foo is the remote name.  */
  zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  if (zstr == NULL)
    return FALSE;

  if (strncmp (zstr, "Shere", 5) != 0)
    {
      ulog (LOG_ERROR, "Bad startup string (expected \"Shere\" got \"%s\")",
	    zstr);
      ubuffree (zstr);
      return FALSE;
    }

  ulog (LOG_NORMAL, "Login successful");

  qstat->ttype = STATUS_TALKING;
  qstat->ilast = ixsysdep_time ((long *) NULL);
  qstat->cretries = 0;
  qstat->cwait = 0;
  if (! fsysdep_set_status (qsys, qstat))
    return FALSE;

  if (zstr[5] == '=')
    {
      const char *zheresys;
      size_t clen;
      int icmp;

      /* Some UUCP packages only provide seven characters in the Shere
	 machine name.  Others only provide fourteen.  */
      zheresys = zstr + 6;
      clen = strlen (zheresys);
      if (clen == 7 || clen == 14)
	icmp = strncmp (zheresys, qsys->uuconf_zname, clen);
      else
	icmp = strcmp (zheresys, qsys->uuconf_zname);
      if (icmp != 0)
	{
	  if (qsys->uuconf_pzalias != NULL)
	    {
	      char **pz;

	      for (pz = qsys->uuconf_pzalias; *pz != NULL; pz++)
		{
		  if (clen == 7 || clen == 14)
		    icmp = strncmp (zheresys, *pz, clen);
		  else
		    icmp = strcmp (zheresys, *pz);
		  if (icmp == 0)
		    break;
		}
	    }
	  if (icmp != 0)
	    {
	      ulog (LOG_ERROR, "Called wrong system (%s)", zheresys);
	      ubuffree (zstr);
	      return FALSE;
	    }
	}
    }
#if DEBUG > 1
  else if (zstr[5] != '\0')
    DEBUG_MESSAGE1 (DEBUG_HANDSHAKE,
		    "fdo_call: Strange Shere: %s", zstr);
#endif

  ubuffree (zstr);

  /* We now send "S" name switches, where name is our UUCP name.  If
     we are using sequence numbers with this system, we send a -Q
     argument with the sequence number.  If the call-timegrade command
     was used, we send a -p argument and a -vgrade= argument with the
     grade to send us (we send both argument to make it more likely
     that one is recognized).  We always send a -N (for new) switch
     indicating what new features we support.  */
  {
    long ival;
    char bgrade;
    char *zsend;
    boolean fret;

    /* Determine the grade we should request of the other system.  A
       '\0' means that no restrictions have been made.  */
    if (! ftimespan_match (qsys->uuconf_qcalltimegrade, &ival,
			   (int *) NULL))
      bgrade = '\0';
    else
      bgrade = (char) ival;

    /* Determine the name we will call ourselves.  */
    if (qsys->uuconf_zlocalname != NULL)
      qdaemon->zlocalname = qsys->uuconf_zlocalname;
    else
      {
	iuuconf = uuconf_localname (puuconf, &qdaemon->zlocalname);
	if (iuuconf == UUCONF_NOT_FOUND)
	  {
	    qdaemon->zlocalname = zsysdep_localname ();
	    if (qdaemon->zlocalname == NULL)
	      return FALSE;
	  }
	else if (iuuconf != UUCONF_SUCCESS)
	  {
	    ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	    return FALSE;
	  }
      }	    

    zsend = zbufalc (strlen (qdaemon->zlocalname) + 70);
    if (! qsys->uuconf_fsequence)
      {
	if (bgrade == '\0')
	  sprintf (zsend, "S%s -R -N0%o", qdaemon->zlocalname,
		   (unsigned int) (FEATURE_SIZES
				   | FEATURE_EXEC
				   | FEATURE_RESTART));
	else
	  sprintf (zsend, "S%s -p%c -vgrade=%c -R -N0%o",
		   qdaemon->zlocalname, bgrade, bgrade,
		   (unsigned int) (FEATURE_SIZES
				   | FEATURE_EXEC
				   | FEATURE_RESTART));
      }
    else
      {
	long iseq;

	iseq = ixsysdep_get_sequence (qsys);
	if (iseq < 0)
	  return FALSE;
	if (bgrade == '\0')
	  sprintf (zsend, "S%s -Q%ld -R -N0%o", qdaemon->zlocalname, iseq,
		   (unsigned int) (FEATURE_SIZES
				   | FEATURE_EXEC
				   | FEATURE_RESTART));
	else
	  sprintf (zsend, "S%s -Q%ld -p%c -vgrade=%c -R -N0%o",
		   qdaemon->zlocalname, iseq, bgrade, bgrade,
		   (unsigned int) (FEATURE_SIZES
				   | FEATURE_EXEC
				   | FEATURE_RESTART));
      }

    fret = fsend_uucp_cmd (qconn, zsend);
    ubuffree (zsend);
    if (! fret)
	return FALSE;
  }

  /* Now we should see ROK or Rreason where reason gives a cryptic
     reason for failure.  If we are talking to a counterpart, we will
     get back ROKN, possibly with a feature bitfield attached.  */
  zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  if (zstr == NULL)
    return FALSE;

  if (zstr[0] != 'R')
    {
      ulog (LOG_ERROR, "Bad response to handshake string (%s)",
	    zstr);
      ubuffree (zstr);
      return FALSE;
    }

  if (strncmp (zstr + 1, "OKN", sizeof "OKN" - 1) == 0)
    {
      if (zstr[sizeof "ROKN" - 1] == '\0')
	qdaemon->ifeatures |= FEATURE_SIZES | FEATURE_V103;
      else
	qdaemon->ifeatures |= (int) strtol (zstr + sizeof "ROKN" - 1,
					   (char **) NULL, 0);
    }
  else if (strncmp (zstr + 1, "OK", sizeof "OK" - 1) == 0)
    {
      if (zstr[sizeof "ROK" - 1] != '\0')
	{
	  char *zopt;

	  /* SVR4 UUCP returns options following the ROK string.  */
	  zopt = zstr + sizeof "ROK" - 1;
	  while (*zopt != '\0')
	    {
	      char b;
	      long c;
	      char *zend;

	      b = *zopt++;
	      if (isspace (b) || b != '-')
		continue;
	      switch (*zopt)
		{
		case 'R':
		  qdaemon->ifeatures |= (FEATURE_RESTART
					 | FEATURE_SVR4
					 | FEATURE_SIZES);
		  break;
		case 'U':
		  c = strtol (zopt, &zend, 0);
		  if (c > 0 && c <= LONG_MAX / (long) 512)
		    qdaemon->cmax_receive = c * (long) 512;
		  zopt = zend;
		  break;
		}
	      while (*zopt != '\0' && ! isspace (*zopt))
		++zopt;
	    }
	}
    }
  else if (strcmp (zstr + 1, "CB") == 0)
    {
      ulog (LOG_NORMAL, "Remote system will call back");
      qstat->ttype = STATUS_COMPLETE;
      (void) fsysdep_set_status (qsys, qstat);
      ubuffree (zstr);
      return TRUE;
    }
  else
    {
      ulog (LOG_ERROR, "Handshake failed (%s)", zstr + 1);
      ubuffree (zstr);
      return FALSE;
    }

  ubuffree (zstr);

  /* The slave should now send \020Pprotos\0 where protos is a list of
     supported protocols.  Each protocol is a single character.  */
  zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  if (zstr == NULL)
    return FALSE;

  if (zstr[0] != 'P')
    {
      ulog (LOG_ERROR, "Bad protocol handshake (%s)", zstr);
      ubuffree (zstr);
      return FALSE;
    }

  /* Determine the reliability characteristics of the connection by
     combining information for the port and the dialer.  If we have no
     information, default to a reliable eight-bit full-duplex
     connection.  */
  if (qconn->qport != NULL
      && (qconn->qport->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
    qdaemon->ireliable = qconn->qport->uuconf_ireliable;
  if (qdialer != NULL
      && (qdialer->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
    {
      if (qdaemon->ireliable != 0)
	qdaemon->ireliable &= qdialer->uuconf_ireliable;
      else
	qdaemon->ireliable = qdialer->uuconf_ireliable;
    }
  if (qdaemon->ireliable == 0)
    qdaemon->ireliable = (UUCONF_RELIABLE_RELIABLE
			  | UUCONF_RELIABLE_EIGHT
			  | UUCONF_RELIABLE_FULLDUPLEX
			  | UUCONF_RELIABLE_SPECIFIED);

  /* Now decide which protocol to use.  The system and the port may
     have their own list of protocols.  */
  {
    int i;
    char ab[5];

    i = CPROTOCOLS;
    if (qsys->uuconf_zprotocols != NULL
	|| (qconn->qport != NULL
	    && qconn->qport->uuconf_zprotocols != NULL))
      {
	const char *zproto;

	if (qsys->uuconf_zprotocols != NULL)
	  zproto = qsys->uuconf_zprotocols;
	else
	  zproto = qconn->qport->uuconf_zprotocols;
	for (; *zproto != '\0'; zproto++)
	  {
	    if (strchr (zstr + 1, *zproto) != NULL)
	      {
		for (i = 0; i < CPROTOCOLS; i++)
		  if (asProtocols[i].bname == *zproto)
		    break;
		if (i < CPROTOCOLS)
		  break;
	      }
	  }
      }
    else
      {
	/* If neither the system nor the port specified a list of
	   protocols, we want only protocols that match the known
	   reliability of the dialer and the port.  */
	for (i = 0; i < CPROTOCOLS; i++)
	  {
	    int ipr;

	    ipr = asProtocols[i].ireliable;
	    if ((ipr & qdaemon->ireliable) != ipr)
	      continue;
	    if (strchr (zstr + 1, asProtocols[i].bname) != NULL)
	      break;
	  }
      }

    ubuffree (zstr);

    if (i >= CPROTOCOLS)
      {
	(void) fsend_uucp_cmd (qconn, "UN");
	ulog (LOG_ERROR, "No mutually supported protocols");
	return FALSE;
      }

    qdaemon->qproto = &asProtocols[i];

    /* If we are using a half-duplex line, act as though we have only
       a single channel; otherwise we might start a send and a receive
       at the same time.  */
    if ((qdaemon->ireliable & UUCONF_RELIABLE_FULLDUPLEX) == 0)
      qdaemon->cchans = 1;
    else
      qdaemon->cchans = asProtocols[i].cchans;

    sprintf (ab, "U%c", qdaemon->qproto->bname);
    if (! fsend_uucp_cmd (qconn, ab))
      return FALSE;
  }

  /* Run any protocol parameter commands.  */
  if (qdaemon->qproto->qcmds != NULL)
    {
      if (qsys->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, qdaemon->qproto->bname,
			     qdaemon->qproto->qcmds,
			     qsys->uuconf_qproto_params);
      if (qconn->qport != NULL
	  && qconn->qport->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, qdaemon->qproto->bname,
			     qdaemon->qproto->qcmds,
			     qconn->qport->uuconf_qproto_params);
      if (qdialer != NULL
	  && qdialer->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, qdaemon->qproto->bname,
			     qdaemon->qproto->qcmds,
			     qdialer->uuconf_qproto_params);
    }

  /* Turn on the selected protocol.  */
  if (! (*qdaemon->qproto->pfstart) (qdaemon, &zlog))
    return FALSE;
  if (zlog == NULL)
    {
      zlog = zbufalc (sizeof "protocol ''" + 1);
      sprintf (zlog, "protocol '%c'", qdaemon->qproto->bname);
    }
  ulog (LOG_NORMAL, "Handshake successful (%s)", zlog);
  ubuffree (zlog);

  *pterr = STATUS_FAILED;

  {
    boolean fret;
    long iend_time;

    fret = floop (qdaemon);

    /* Now send the hangup message.  As the caller, we send six O's
       and expect to receive seven O's.  We send the six O's twice to
       help the other side.  We don't worry about errors here.  */
    if (fsend_uucp_cmd (qconn, "OOOOOO")
	&& fsend_uucp_cmd (qconn, "OOOOOO"))
      {
	int i, fdone;

	/* We look for the remote hangup string to ensure that the
	   modem has sent out our hangup string.  This is only
	   necessary because some versions of UUCP complain if they
	   don't get the hangup string.  The remote site should send 7
	   O's, but some versions of UUCP only send 6.  We look for
	   the string several times because supposedly some
	   implementations send some garbage after the last packet but
	   before the hangup string.  */
	for (i = 0; i < 25; i++)
	  {
	    zstr = zget_uucp_cmd (qconn, FALSE, fstrip);
	    if (zstr == NULL)
	      break;
	    fdone = strstr (zstr, "OOOOOO") != NULL;
	    ubuffree (zstr);
	    if (fdone)
	      break;
	  }
      }

    iend_time = ixsysdep_time ((long *) NULL);

    ulog (LOG_NORMAL, "Call complete (%ld seconds %ld bytes %ld bps)",
	  iend_time - istart_time,
	  qdaemon->csent + qdaemon->creceived,
	  (iend_time != istart_time
	   ? (qdaemon->csent + qdaemon->creceived) / (iend_time - istart_time)
	   : 0));

    if (fret)
      {
	qstat->ttype = STATUS_COMPLETE;
	qstat->ilast = iend_time;
	(void) fsysdep_set_status (qsys, qstat);
      }

    if (qdaemon->irunuuxqt == UUCONF_RUNUUXQT_PERCALL
	|| (qdaemon->irunuuxqt > 0 && qdaemon->cxfiles_received > 0))
      (void) fspawn_uuxqt (TRUE, qdaemon->qsys->uuconf_zname,
			   qdaemon->zconfig);

    return fret;
  }
}

/* This routine is called via uuconf_find_port when a matching port is
   found.  It tries to lock the port.  If it fails, it returns
   UUCONF_NOT_FOUND to force uuconf_find_port to continue searching
   for the next matching port.  */

static int
iuport_lock (qport, pinfo)
     struct uuconf_port *qport;
     pointer pinfo;
{
  struct spass *q = (struct spass *) pinfo;

  q->fmatched = TRUE;

  if (! fconn_init (qport, q->qconn, UUCONF_PORTTYPE_UNKNOWN))
    return UUCONF_NOT_FOUND;
  else if (! fconn_lock (q->qconn, FALSE))
    {
      uconn_free (q->qconn);
      return UUCONF_NOT_FOUND;
    }
  else
    {
      q->flocked = TRUE;
      return UUCONF_SUCCESS;
    }
}

/* The information structure used for the uuconf_callin comparison
   function.  */

struct scallin_info
{
  const char *zuser;
  const char *zpass;
};

/* Prompt for a login name and a password, and run as the slave.  */

static boolean
flogin_prompt (puuconf, zconfig, fuuxqt, qconn, zlogin, pzsystem)
     pointer puuconf;
     const char *zconfig;
     boolean fuuxqt;
     struct sconnection *qconn;
     const char *zlogin;
     const char **pzsystem;
{
  int iuuconf;
  int istrip;
  boolean fstrip;
  char *zuser, *zpass;
  boolean fret;
  struct scallin_info s;

  if (pzsystem != NULL)
    *pzsystem = NULL;

  DEBUG_MESSAGE0 (DEBUG_HANDSHAKE, "flogin_prompt: Waiting for login");

  iuuconf = uuconf_strip (puuconf, &istrip);
  if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      return FALSE;
    }
  fstrip = (istrip & UUCONF_STRIP_LOGIN) != 0;

  zuser = NULL;
  if (zlogin == NULL)
    {
      do
	{
	  ubuffree (zuser);
	  if (! fconn_write (qconn, "login: ", sizeof "login: " - 1))
	    return FALSE;
	  zuser = zget_typed_line (qconn, fstrip);
	}
      while (zuser != NULL && *zuser == '\0');

      if (zuser == NULL)
	return TRUE;

      zlogin = zuser;
    }

  if (! fconn_write (qconn, "Password:", sizeof "Password:" - 1))
    {
      ubuffree (zuser);
      return FALSE;
    }

  zpass = zget_typed_line (qconn, fstrip);
  if (zpass == NULL)
    {
      ubuffree (zuser);
      return TRUE;
    }

  fret = TRUE;

  s.zuser = zlogin;
  s.zpass = zpass;
  iuuconf = uuconf_callin (puuconf, icallin_cmp, &s);

  ubuffree (zpass);

  if (iuuconf == UUCONF_NOT_FOUND)
    ulog (LOG_ERROR, "Bad login");
  else if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      fret = FALSE;
    }
  else
    {
#if DEBUG > 1
      int iholddebug;
#endif

      /* We ignore the return value of faccept_call because we really
	 don't care whether the call succeeded or not.  We are going
	 to reset the port anyhow.  */
#if DEBUG > 1
      iholddebug = iDebug;
#endif
      (void) faccept_call (puuconf, zconfig, fuuxqt, zlogin, qconn, pzsystem);
#if DEBUG > 1
      iDebug = iholddebug;
#endif
    }

  ubuffree (zuser);

  return fret;
}

/* The comparison function which we pass to uuconf_callin.  This
   expands escape sequences in the login name, and either encrypts or
   expands escape sequences in the password.  */

static int
icallin_cmp (iwhich, pinfo, zfile)
     int iwhich;
     pointer pinfo;
     const char *zfile;
{
  struct scallin_info *qinfo = (struct scallin_info *) pinfo;
  char *zcopy;
  int icmp;

#if HAVE_ENCRYPTED_PASSWORDS
  if (iwhich != 0)
    return strcmp (crypt (qinfo->zpass, zfile), zfile) == 0;
#endif

  zcopy = zbufcpy (zfile);
  (void) cescape (zcopy);
  if (iwhich == 0)
    icmp = strcmp (qinfo->zuser, zcopy);
  else
    icmp = strcmp (qinfo->zpass, zcopy);
  ubuffree (zcopy);
  return icmp == 0;
}

/* Accept a call from a remote system.  If pqsys is not NULL, *pqsys
   will be set to the system that called in if known.  */

static boolean
faccept_call (puuconf, zconfig, fuuxqt, zlogin, qconn, pzsystem)
     pointer puuconf;
     const char *zconfig;
     boolean fuuxqt;
     const char *zlogin;
     struct sconnection *qconn;
     const char **pzsystem;
{
  long istart_time;
  int iuuconf;
  int istrip;
  boolean fstrip;
  const char *zport;
  struct uuconf_port *qport;
  struct uuconf_port sport;
  struct uuconf_dialer *qdialer;
  struct uuconf_dialer sdialer;
  boolean ftcp_port;
  char *zsend, *zspace;
  boolean fret;
  char *zstr;
  struct uuconf_system ssys;
  const struct uuconf_system *qsys;
  const struct uuconf_system *qany;
  char *zloc;
  struct sstatus sstat;
  boolean fgotseq, fgotn;
  int i;
  char *zlog;
  char *zgrade;

  if (pzsystem != NULL)
    *pzsystem = NULL;

  ulog (LOG_NORMAL, "Incoming call (login %s port %s)", zlogin,
	zLdevice == NULL ? (char *) "unknown" : zLdevice);

  istart_time = ixsysdep_time ((long *) NULL);

  iuuconf = uuconf_strip (puuconf, &istrip);
  if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    (struct uuconf_port *) NULL,
			    &sport, (char *) NULL);
      return FALSE;
    }
  fstrip = (istrip & UUCONF_STRIP_PROTO) != 0;

  /* Figure out protocol parameters determined by the port.  If no
     port was specified we're reading standard input, so try to get
     the port name and read information from the port file.  We only
     use the port information to get protocol parameters; we don't
     want to start treating the port as though it were a modem, for
     example.  */
  if (qconn->qport != NULL)
    {
      qport = qconn->qport;
      zport = qport->uuconf_zname;
      ftcp_port = FALSE;
    }
  else
    {
      zport = zsysdep_port_name (&ftcp_port);
      if (zport == NULL)
	{
	  qport = NULL;
	  zport = "unknown";
	}
      else
	{
	  iuuconf = uuconf_find_port (puuconf, zport, (long) 0, (long) 0,
				      (int (*) P((struct uuconf_port *,
						  pointer pinfo))) NULL,
				      (pointer) NULL,
				      &sport);
	  if (iuuconf == UUCONF_NOT_FOUND)
	    qport = NULL;
	  else if (iuuconf != UUCONF_SUCCESS)
	    {
	      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
				    (struct uuconf_port *) NULL,
				    &sport, (char *) NULL);
	      return FALSE;
	    }
	  else
	    qport = &sport;
	}
    }

  /* If we've managed to figure out that this is a modem port, now try
     to get protocol parameters from the dialer.  */
  qdialer = NULL;
  if (qport != NULL)
    {
      if (qport->uuconf_ttype == UUCONF_PORTTYPE_MODEM)
	{
	  if (qport->uuconf_u.uuconf_smodem.uuconf_pzdialer != NULL)
	    {
	      const char *zdialer;

	      zdialer = qport->uuconf_u.uuconf_smodem.uuconf_pzdialer[0];
	      iuuconf = uuconf_dialer_info (puuconf, zdialer, &sdialer);
	      if (iuuconf == UUCONF_SUCCESS)
		qdialer = &sdialer;
	    }
	  else
	    qdialer = qport->uuconf_u.uuconf_smodem.uuconf_qdialer;
	}	  
      else if (qport->uuconf_ttype == UUCONF_PORTTYPE_TCP
	       || (qport->uuconf_ttype == UUCONF_PORTTYPE_TLI
		   && (qport->uuconf_ireliable
		       & UUCONF_RELIABLE_SPECIFIED) == 0))
	ftcp_port = TRUE;
    }

  sDaemon.puuconf = puuconf;
  sDaemon.zconfig = zconfig;
  if (! fuuxqt)
    sDaemon.irunuuxqt = UUCONF_RUNUUXQT_NEVER;
  else
    {
      iuuconf = uuconf_runuuxqt (puuconf, &sDaemon.irunuuxqt);
      if (iuuconf != UUCONF_SUCCESS)
	ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
    }
  sDaemon.qsys = NULL;
  sDaemon.zlocalname = NULL;
  sDaemon.qconn = qconn;
  sDaemon.qproto = NULL;
  sDaemon.cchans = 1;
  sDaemon.clocal_size = -1;
  sDaemon.cremote_size = -1;
  sDaemon.cmax_ever = -2;
  sDaemon.cmax_receive = -1;
  sDaemon.csent = 0;
  sDaemon.creceived = 0;
  sDaemon.cxfiles_received = 0;
  sDaemon.ifeatures = 0;
  sDaemon.frequest_hangup = FALSE;
  sDaemon.fhangup_requested = FALSE;
  sDaemon.fhangup = FALSE;
  sDaemon.fmaster = FALSE;
  sDaemon.fcaller = FALSE;
  sDaemon.ireliable = 0;
  sDaemon.bgrade = UUCONF_GRADE_LOW;

  /* Get the local name to use.  If uuconf_login_localname returns a
     value, it is not always freed up, although it should be.  */
  iuuconf = uuconf_login_localname (puuconf, zlogin, &zloc);
  if (iuuconf == UUCONF_SUCCESS)
    sDaemon.zlocalname = zloc;
  else if (iuuconf == UUCONF_NOT_FOUND)
    {
      sDaemon.zlocalname = zsysdep_localname ();
      if (sDaemon.zlocalname == NULL)
	{
	  uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
				qport, &sport, (char *) NULL);
	  return FALSE;
	}
    }
  else
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    qport, &sport, (char *) NULL);
      return FALSE;
    }

  /* Tell the remote system who we are.   */
  zsend = zbufalc (strlen (sDaemon.zlocalname) + sizeof "Shere=");
  sprintf (zsend, "Shere=%s", sDaemon.zlocalname);
  fret = fsend_uucp_cmd (qconn, zsend);
  ubuffree (zsend);
  if (! fret)
    {
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    qport, &sport, zloc);
      return FALSE;
    }

  zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  if (zstr == NULL)
    {
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    qport, &sport, zloc);
      return FALSE;
    }

  if (zstr[0] != 'S')
    {
      ulog (LOG_ERROR, "Bad introduction string");
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    qport, &sport, zloc);
      return FALSE;
    }

  zspace = strchr (zstr, ' ');
  if (zspace != NULL)
    *zspace = '\0';

  iuuconf = uuconf_system_info (puuconf, zstr + 1, &ssys);
  if (iuuconf == UUCONF_NOT_FOUND)
    {
      char *zscript;

      /* Run the remote.unknown script, if appropriate.  */
      iuuconf = uuconf_remote_unknown (puuconf, &zscript);
      if (iuuconf == UUCONF_SUCCESS)
	{
	  if (! fsysdep_unknown_caller (zscript, zstr + 1))
	    {
	      xfree ((pointer) zscript);
	      (void) fsend_uucp_cmd (qconn, "RYou are unknown to me");
	      ubuffree (zstr);
	      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
				    qport, &sport, zloc);
	      return FALSE;
	    }
	  xfree ((pointer) zscript);
	}
      else if (iuuconf != UUCONF_NOT_FOUND)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  ubuffree (zstr);
	  uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
				qport, &sport, zloc);
	  return FALSE;
	}

      if (! funknown_system (puuconf, zstr + 1, &ssys))
	{
	  (void) fsend_uucp_cmd (qconn, "RYou are unknown to me");
	  ulog (LOG_ERROR, "Call from unknown system %s", zstr + 1);
	  ubuffree (zstr);
	  uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
				qport, &sport, zloc);
	  return FALSE;
	}
    }
  else if (iuuconf != UUCONF_SUCCESS)
    {
      ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, (struct uuconf_system *) NULL,
			    qport, &sport, zloc);
      return FALSE;
    }

  qany = NULL;
  for (qsys = &ssys; qsys != NULL; qsys = qsys->uuconf_qalternate)
    {
      if (! qsys->uuconf_fcalled)
	continue;

      if (qsys->uuconf_zcalled_login == NULL
	  || strcmp (qsys->uuconf_zcalled_login, "ANY") == 0)
	{
	  if (qany == NULL)
	    qany = qsys;
	}
      else if (strcmp (qsys->uuconf_zcalled_login, zlogin) == 0)
	break;
    }

  if (qsys == NULL && qany != NULL)
    {
      iuuconf = uuconf_validate (puuconf, qany, zlogin);
      if (iuuconf == UUCONF_SUCCESS)
	qsys = qany;
      else if (iuuconf != UUCONF_NOT_FOUND)
	{
	  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
	  ubuffree (zstr);
	  uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
	  return FALSE;
	}
    }

  if (qsys == NULL)
    {
      (void) fsend_uucp_cmd (qconn, "RLOGIN");
      ulog (LOG_ERROR, "System %s used wrong login name %s",
	    zstr + 1, zlogin);
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  sDaemon.qsys = qsys;

  if (pzsystem != NULL)
    *pzsystem = zbufcpy (qsys->uuconf_zname);

  ulog_system (qsys->uuconf_zname);

#if DEBUG > 1
  if (qsys->uuconf_zdebug != NULL)
    iDebug |= idebug_parse (qsys->uuconf_zdebug);
#endif

  /* See if we are supposed to call the system back.  This will queue
     up an empty command.  It would be better to actually call back
     directly at this point as well.  */
  if (qsys->uuconf_fcallback)
    {
      (void) fsend_uucp_cmd (qconn, "RCB");
      ulog (LOG_NORMAL, "Will call back");

      /* Clear any existing status.  */
      sstat.ttype = STATUS_COMPLETE;
      sstat.cretries = 0;
      sstat.ilast = ixsysdep_time ((long *) NULL);
      sstat.cwait = 0;
      (void) fsysdep_set_status (qsys, &sstat);

      ubuffree (zsysdep_spool_commands (qsys, UUCONF_GRADE_HIGH, 0,
					(const struct scmd *) NULL));
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return TRUE;
    }

  /* We only permit one call at a time from a remote system.  Lock it.  */
  if (! fsysdep_lock_system (qsys))
    {
      if (qsys->uuconf_fsequence)
	{
	  /* At this point the calling system has already incremented
	     its sequence number, so we increment ours.  This will
	     only cause a mismatch if the other system is not what it
	     says it is.  */
	  (void) ixsysdep_get_sequence (qsys);
	}
      (void) fsend_uucp_cmd (qconn, "RLCK");
      ulog (LOG_ERROR, "System already locked");
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }
  sLocked_system = *qsys;
  fLocked_system = TRUE;

  /* Set the system status.  We don't care what the status was before.
     We also don't want to kill the conversation just because we can't
     output the .Status file, so we ignore any errors.  */
  sstat.ttype = STATUS_TALKING;
  sstat.cretries = 0;
  sstat.ilast = ixsysdep_time ((long *) NULL);
  sstat.cwait = 0;
  (void) fsysdep_set_status (qsys, &sstat);

  /* Check the arguments of the remote system, if any.  */
  fgotseq = FALSE;
  fgotn = FALSE;
  if (zspace != NULL)
    {
      char **paz;
      char **pzset;

      ++zspace;

      /* Break the introduction line up into arguments.  */
      paz = (char **) xmalloc ((strlen (zspace) / 2 + 2) * sizeof (char *));
      pzset = paz;
      *pzset++ = NULL;
      while (TRUE)
	{
	  while (*zspace != '\0' && isspace (BUCHAR (*zspace)))
	    ++zspace;
	  if (*zspace == '\0')
	    break;
	  *pzset++ = zspace;
	  ++zspace;
	  while (*zspace != '\0' && ! isspace (BUCHAR (*zspace)))
	    ++zspace;
	  if (*zspace == '\0')
	    break;
	  *zspace++ = '\0';
	}

      if (pzset != paz + 1)
	{
	  int iopt;

	  *pzset = NULL;

	  /* We are going to use getopt to parse the arguments.  We
	     must clear optind to force getopt to reinitialize, and
	     clear opterr to prevent getopt from printing an error
	     message.  This approach assumes we are using the GNU
	     getopt, which is distributed with the program anyhow.  */
	  optind = 0;
	  opterr = 0;
	  
	  while ((iopt = getopt (pzset - paz, paz,
				 "N::p:Q:RU:v:x:")) != EOF)
	    {
	      long iseq;
	      long c;
	      char b;
	      int iwant;

	      switch (iopt)
		{
		case 'N':
		  /* This is used to indicate support for Taylor UUCP
		     extensions.  An plain -N mean support for size
		     negotiation.  If -N is followed by a number (with
		     no intervening space), the number is a bit field
		     of feature flags as defined in trans.h.  Note
		     that the argument may start with 0x for hex or 0
		     for octal.  */
		  fgotn = TRUE;
		  if (optarg == NULL)
		    sDaemon.ifeatures |= FEATURE_SIZES | FEATURE_V103;
		  else
		    sDaemon.ifeatures |= (int) strtol (optarg,
						       (char **) NULL,
						       0);
		  break;

		case 'p':
		  /* The argument is the lowest grade of work the
		     local system should send.  */
		  if (UUCONF_GRADE_LEGAL (optarg[0]))
		    sDaemon.bgrade = optarg[0];
		  break;

		case 'Q':
		  /* The conversation sequence number.  */
		  iseq = strtol (optarg, (char **) NULL, 10);
		  if (qsys->uuconf_fsequence
		      && iseq != ixsysdep_get_sequence (qsys))
		    {
		      (void) fsend_uucp_cmd (qconn, "RBADSEQ");
		      ulog (LOG_ERROR, "Out of sequence call rejected");
		      sstat.ttype = STATUS_FAILED;
		      (void) fsysdep_set_status (qsys, &sstat);
		      xfree ((pointer) paz);
		      ubuffree (zstr);
		      uaccept_call_cleanup (puuconf, &ssys, qport, &sport,
					    zloc);
		      return FALSE;
		    }
		  fgotseq = TRUE;
		  break;

		case 'R':
		  /* The remote system supports file restart.  */
		  sDaemon.ifeatures |= FEATURE_RESTART;
		  break;

		case 'U':
		  /* The maximum file size the remote system is
		     prepared to received, in blocks where each block
		     is 512 bytes.  */
		  c = strtol (optarg, (char **) NULL, 0);
		  if (c > 0 && c < LONG_MAX / (long) 512)
		    sDaemon.cmax_receive = c * (long) 512;
		  break;

		case 'v':
		  /* -vgrade=X can be used to set the lowest grade of
		     work the local system should send.  */
		  if (strncmp (optarg, "grade=", sizeof "grade=" - 1) == 0)
		    {
		      b = optarg[sizeof "grade=" - 1];
		      if (UUCONF_GRADE_LEGAL (b))
			sDaemon.bgrade = b;
		    }
		  break;

		case 'x':
		  iwant = (int) strtol (optarg, (char **) NULL, 10);
#if DEBUG > 1
		  if (iwant <= 9)
		    iwant = (1 << iwant) - 1;
		  if (qsys->uuconf_zmax_remote_debug != NULL)
		    iwant &= idebug_parse (qsys->uuconf_zmax_remote_debug);
		  else
		    iwant &= DEBUG_ABNORMAL | DEBUG_CHAT | DEBUG_HANDSHAKE;
		  if ((iDebug | iwant) != iDebug)
		    {
		      iDebug |= iwant;
		      ulog (LOG_NORMAL, "Setting debugging mode to 0%o",
			    iDebug);
		    }
#endif
		  break;

		default:
		  break;
		}
	    }
	}

      xfree ((pointer) paz);
    }

  ubuffree (zstr);

  if (qsys->uuconf_fsequence && ! fgotseq)
    {
      (void) fsend_uucp_cmd (qconn, "RBADSEQ");
      ulog (LOG_ERROR, "No sequence number (call rejected)");
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  /* We recognized the system, and the sequence number (if any) was
     OK.  Send an ROK, and send a list of protocols.  If we got the -N
     switch, send ROKN to confirm it; if the -N switch was followed by
     a feature bitfield, return our own feature bitfield.  */
  {
    char ab[20];
    const char *zreply;

    if (! fgotn)
      {
	if ((sDaemon.ifeatures & FEATURE_RESTART) == 0)
	  zreply = "ROK";
	else
	  {
	    /* We got -R without -N, so assume that this is SVR4 UUCP.
	       SVR4 UUCP expects ROK -R to signal support for file
	       restart.  */
	    sDaemon.ifeatures |= FEATURE_SVR4 | FEATURE_SIZES;
	    zreply = "ROK -R";
	  }
      }
    else if ((sDaemon.ifeatures & FEATURE_V103) != 0)
      zreply = "ROKN";
    else
      {
	sprintf (ab, "ROKN0%o",
		 (unsigned int) (FEATURE_SIZES
				 | FEATURE_EXEC
				 | FEATURE_RESTART));
	zreply = ab;
      }
    if (! fsend_uucp_cmd (qconn, zreply))
      {
	sstat.ttype = STATUS_FAILED;
	(void) fsysdep_set_status (qsys, &sstat);
	uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
	return FALSE;
      }
  }

  /* Determine the reliability of the connection based on the
     reliability of the port and the dialer.  If we have no
     information, default to a reliable eight-bit full-duplex
     connection.  */
  if (ftcp_port)
    sDaemon.ireliable = (UUCONF_RELIABLE_SPECIFIED
			 | UUCONF_RELIABLE_ENDTOEND
			 | UUCONF_RELIABLE_RELIABLE
			 | UUCONF_RELIABLE_EIGHT
			 | UUCONF_RELIABLE_FULLDUPLEX);
  else
    {
      if (qport != NULL
	  && (qport->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
	sDaemon.ireliable = qport->uuconf_ireliable;
      if (qdialer != NULL
	  && (qdialer->uuconf_ireliable & UUCONF_RELIABLE_SPECIFIED) != 0)
	{
	  if (sDaemon.ireliable != 0)
	    sDaemon.ireliable &= qdialer->uuconf_ireliable;
	  else
	    sDaemon.ireliable = qdialer->uuconf_ireliable;
	}
      if (sDaemon.ireliable == 0)
	sDaemon.ireliable = (UUCONF_RELIABLE_RELIABLE
			     | UUCONF_RELIABLE_EIGHT
			     | UUCONF_RELIABLE_FULLDUPLEX
			     | UUCONF_RELIABLE_SPECIFIED);
    }

  if (qsys->uuconf_zprotocols != NULL ||
      (qport != NULL && qport->uuconf_zprotocols != NULL))
    {
      const char *zprotos;

      if (qsys->uuconf_zprotocols != NULL)
	zprotos = qsys->uuconf_zprotocols;
      else
	zprotos = qport->uuconf_zprotocols;
      zsend = zbufalc (strlen (zprotos) + 2);
      sprintf (zsend, "P%s", zprotos);
    }
  else
    {
      char *zset;

      zsend = zbufalc (CPROTOCOLS + 2);
      zset = zsend;
      *zset++ = 'P';

      /* If the system did not specify a list of protocols, we want
	 only protocols that match the known reliability of the dialer
	 and the port.  */
      for (i = 0; i < CPROTOCOLS; i++)
	{
	  int ipr;

	  ipr = asProtocols[i].ireliable;
	  if ((ipr & sDaemon.ireliable) != ipr)
	    continue;
	  *zset++ = asProtocols[i].bname;
	}
      *zset = '\0';
    }

  fret = fsend_uucp_cmd (qconn, zsend);
  ubuffree (zsend);
  if (! fret)
    {
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }
    
  /* The master will now send back the selected protocol.  */
  zstr = zget_uucp_cmd (qconn, TRUE, fstrip);
  if (zstr == NULL)
    {
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  if (zstr[0] != 'U')
    {
      ulog (LOG_ERROR, "Bad protocol response string");
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  if (zstr[1] == 'N')
    {
      ulog (LOG_ERROR, "No supported protocol");
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      ubuffree (zstr);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  for (i = 0; i < CPROTOCOLS; i++)
    if (asProtocols[i].bname == zstr[1])
      break;

  ubuffree (zstr);

  if (i >= CPROTOCOLS)
    {
      ulog (LOG_ERROR, "No supported protocol");
      sstat.ttype = STATUS_FAILED;
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  sDaemon.qproto = &asProtocols[i];

  /* If we are using a half-duplex line, act as though we have only a
     single channel; otherwise we might start a send and a receive at
     the same time.  */
  if ((sDaemon.ireliable & UUCONF_RELIABLE_FULLDUPLEX) == 0)
    sDaemon.cchans = 1;
  else
    sDaemon.cchans = asProtocols[i].cchans;

  /* Run the chat script for when a call is received.  */
  if (! fchat (qconn, puuconf, &qsys->uuconf_scalled_chat, qsys,
	       (const struct uuconf_dialer *) NULL, (const char *) NULL,
	       FALSE, zport, iconn_baud (qconn)))
    {
      sstat.ttype = STATUS_FAILED;
      sstat.ilast = ixsysdep_time ((long *) NULL);
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  /* Run any protocol parameter commands.  */
  if (sDaemon.qproto->qcmds != NULL)
    {
      if (qsys->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, sDaemon.qproto->bname,
			     sDaemon.qproto->qcmds,
			     qsys->uuconf_qproto_params);
      if (qport != NULL
	  && qport->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, sDaemon.qproto->bname,
			     sDaemon.qproto->qcmds,
			     qport->uuconf_qproto_params);
      if (qdialer != NULL
	  && qdialer->uuconf_qproto_params != NULL)
	uapply_proto_params (puuconf, sDaemon.qproto->bname,
			     sDaemon.qproto->qcmds,
			     qdialer->uuconf_qproto_params);
    }

  /* We don't need the dialer information any more.  */
  if (qdialer == &sdialer)
    (void) uuconf_dialer_free (puuconf, &sdialer);

  /* Turn on the selected protocol and get any jobs queued for the
     system.  */
  if (! (*sDaemon.qproto->pfstart) (&sDaemon, &zlog)
      || ! fqueue (&sDaemon, (boolean *) NULL))
    {
      uclear_queue (&sDaemon);
      sstat.ttype = STATUS_FAILED;
      sstat.ilast = ixsysdep_time ((long *) NULL);
      (void) fsysdep_set_status (qsys, &sstat);
      uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);
      return FALSE;
    }

  if (zlog == NULL)
    {
      zlog = zbufalc (sizeof "protocol ''" + 1);
      sprintf (zlog, "protocol '%c'", sDaemon.qproto->bname);
    }

  zgrade = zbufalc (sizeof "grade  " + 1);
  if (sDaemon.bgrade == UUCONF_GRADE_LOW)
    *zgrade = '\0';
  else
    sprintf (zgrade, "grade %c ", sDaemon.bgrade);

  /* If we are using HAVE_HDB_LOGGING, then the previous ``incoming
     call'' message went to the general log, since we didn't know the
     system name at that point.  In that case, we repeat the port and
     login names.  */
#if HAVE_HDB_LOGGING
  ulog (LOG_NORMAL, "Handshake successful (login %s port %s %s%s)",
	zlogin,
	zLdevice == NULL ? "unknown" : zLdevice,
	zgrade, zlog);
#else /* ! HAVE_HDB_LOGGING */
  ulog (LOG_NORMAL, "Handshake successful (%s%s)", zgrade, zlog);
#endif /* ! HAVE_HDB_LOGGING */

  ubuffree (zlog);
  ubuffree (zgrade);

  {
    long iend_time;

    fret = floop (&sDaemon);

    /* Hangup.  As the answerer, we send seven O's and expect to
       receive six O's.  We send the seven O's twice to help the other
       side.  We don't worry about errors here.  */
    if (fsend_uucp_cmd (qconn, "OOOOOOO")
	&& fsend_uucp_cmd (qconn, "OOOOOOO"))
      {
	int fdone;

	/* We look for the remote hangup string to ensure that the
	   modem has sent out our hangup string.  This is only
	   necessary because some versions of UUCP complain if they
	   don't get the hangup string.  We look for the string
	   several times because supposedly some implementations send
	   some garbage after the last packet but before the hangup
	   string.  */
	for (i = 0; i < 25; i++)
	  {
	    zstr = zget_uucp_cmd (qconn, FALSE, fstrip);
	    if (zstr == NULL)
	      break;
	    fdone = strstr (zstr, "OOOOOO") != NULL;
	    ubuffree (zstr);
	    if (fdone)
	      break;
	  }
      }

    iend_time = ixsysdep_time ((long *) NULL);

    ulog (LOG_NORMAL, "Call complete (%ld seconds %ld bytes %ld bps)",
	  iend_time - istart_time,
	  sDaemon.csent + sDaemon.creceived,
	  (iend_time != istart_time
	   ? (sDaemon.csent + sDaemon.creceived) / (iend_time - istart_time)
	   : 0));

    uclear_queue (&sDaemon);

    if (fret)
      sstat.ttype = STATUS_COMPLETE;
    else
      sstat.ttype = STATUS_FAILED;
    sstat.ilast = iend_time;
    (void) fsysdep_set_status (qsys, &sstat);

    if (sDaemon.irunuuxqt == UUCONF_RUNUUXQT_PERCALL
	|| (sDaemon.irunuuxqt > 0 && sDaemon.cxfiles_received > 0))
      (void) fspawn_uuxqt (TRUE, qsys->uuconf_zname, zconfig);

    uaccept_call_cleanup (puuconf, &ssys, qport, &sport, zloc);

    return fret;
  }
}

/* Clean up after faccept_call.  */

static void
uaccept_call_cleanup (puuconf, qfreesys, qport, qfreeport, zloc)
     pointer puuconf;
     struct uuconf_system *qfreesys;
     struct uuconf_port *qport;
     struct uuconf_port *qfreeport;
     char *zloc;
{
  if (fLocked_system)
    {
      (void) fsysdep_unlock_system (&sLocked_system);
      fLocked_system = FALSE;
    }
  if (qfreesys != NULL)
    (void) uuconf_system_free (puuconf, qfreesys);
  if (qport == qfreeport)
    (void) uuconf_port_free (puuconf, qfreeport);
  xfree ((pointer) zloc);
  ulog_system ((const char *) NULL);
}

/* Apply protocol parameters, once we know the protocol.  */

static void
uapply_proto_params (puuconf, bproto, qcmds, pas)
     pointer puuconf;
     int bproto;
     struct uuconf_cmdtab *qcmds;
     struct uuconf_proto_param *pas;
{
  struct uuconf_proto_param *qp;

  for (qp = pas; qp->uuconf_bproto != '\0'; qp++)
    {
      if (qp->uuconf_bproto == bproto)
	{
	  struct uuconf_proto_param_entry *qe;

	  for (qe = qp->uuconf_qentries; qe->uuconf_cargs > 0; qe++)
	    {
	      int iuuconf;

	      iuuconf = uuconf_cmd_args (puuconf, qe->uuconf_cargs,
					 qe->uuconf_pzargs, qcmds,
					 (pointer) NULL,
					 (uuconf_cmdtabfn) NULL, 0,
					 (pointer) NULL);
	      if (UUCONF_ERROR_VALUE (iuuconf) != UUCONF_SUCCESS)
		{
		  ulog (LOG_ERROR, "Error in %c protocol parameters",
			bproto);
		  ulog_uuconf (LOG_ERROR, puuconf, iuuconf);
		}
	    }

	  break;
	}
    }
}

/* Send a string to the other system beginning with a DLE
   character and terminated with a null byte.  This is only
   used when no protocol is in force.  */

static boolean
fsend_uucp_cmd (qconn, z)
     struct sconnection *qconn;
     const char *z;
{
  size_t cwrite;
  char *zalc;
  boolean fret;

  DEBUG_MESSAGE1 (DEBUG_HANDSHAKE, "fsend_uucp_cmd: Sending \"%s\"", z);

  cwrite = strlen (z) + 2;

  zalc = zbufalc (cwrite);
  zalc[0] = '\020';
  memcpy (zalc + 1, z, cwrite - 1);

  fret = fconn_write (qconn, zalc, cwrite);
  ubuffree (zalc);
  return fret;
}

/* Get a UUCP command beginning with a DLE character and ending with a
   null byte.  This is only used when no protocol is in force.  This
   implementation has the potential of being seriously slow.  It also
   doesn't have any real error recovery.  The frequired argument is
   passed as TRUE if we need the string; we don't care that much if
   we're closing down the connection anyhow.  */

#define CTIMEOUT (120)
#define CSHORTTIMEOUT (10)
#define CINCREMENT (100)

static char *
zget_uucp_cmd (qconn, frequired, fstrip)
     struct sconnection *qconn;
     boolean frequired;
     boolean fstrip;
{
  char *zalc;
  size_t calc;
  size_t cgot;
  boolean fintro;
  long iendtime;
  int ctimeout;
#if DEBUG > 1
  int cchars;
  int iolddebug;
#endif

  iendtime = ixsysdep_time ((long *) NULL);
  if (frequired)
    iendtime += CTIMEOUT;
  else
    iendtime += CSHORTTIMEOUT;

#if DEBUG > 1
  cchars = 0;
  iolddebug = iDebug;
  if (FDEBUGGING (DEBUG_HANDSHAKE))
    {
      ulog (LOG_DEBUG_START, "zget_uucp_cmd: Got \"");
      iDebug &=~ (DEBUG_INCOMING | DEBUG_PORT);
    }
#endif

  zalc = NULL;
  calc = 0;
  cgot = 0;
  fintro = FALSE;
  while ((ctimeout = (int) (iendtime - ixsysdep_time ((long *) NULL))) > 0)
    {
      int b;
      
      b = breceive_char (qconn, ctimeout, frequired);
      /* Now b == -1 on timeout, -2 on error.  */
      if (b < 0)
	{
#if DEBUG > 1
	  if (FDEBUGGING (DEBUG_HANDSHAKE))
	    {
	      ulog (LOG_DEBUG_END, "\" (%s)",
		    b == -1 ? "timeout" : "error");
	      iDebug = iolddebug;
	    }
#endif
	  if (b == -1 && frequired)
	    ulog (LOG_ERROR, "Timeout");
	  ubuffree (zalc);
	  return NULL;
	}

      /* Apparently some systems use parity on these strings, so we
	 optionally strip the parity bit.  */
      if (fstrip)
	b &= 0x7f;

#if DEBUG > 1
      if (FDEBUGGING (DEBUG_HANDSHAKE))
	{
	  char ab[5];

	  ++cchars;
	  if (cchars > 60)
	    {
	      ulog (LOG_DEBUG_END, "\"");
	      ulog (LOG_DEBUG_START, "zget_uucp_cmd: Got \"");
	      cchars = 0;
	    }
	  (void) cdebug_char (ab, b);
	  ulog (LOG_DEBUG_CONTINUE, "%s", ab);
	}
#endif

      if (! fintro)
	{
	  if (b == '\020')
	    fintro = TRUE;
	  continue;
	}

      /* If we see another DLE, something has gone wrong; continue
	 as though this were the first one we saw.  */
      if (b == '\020')
	{
	  cgot = 0;
	  continue;
	}

      /* Some systems send a trailing \n on the Shere line.  As far as
	 I can tell this line can never contain a \n, so this
	 modification should be safe enough.  */
      if (b == '\r' || b == '\n')
	b = '\0';

      if (cgot >= calc)
	{
	  char *znew;

	  calc += CINCREMENT;
	  znew = zbufalc (calc);
	  if (cgot > 0)
	    memcpy (znew, zalc, cgot);
	  ubuffree (zalc);
	  zalc = znew;
	}

      zalc[cgot] = (char) b;
      ++cgot;

      if (b == '\0')
	{
#if DEBUG > 1
	  if (FDEBUGGING (DEBUG_HANDSHAKE))
	    {
	      ulog (LOG_DEBUG_END, "\"");
	      iDebug = iolddebug;
	    }
#endif
	  return zalc;
	}
    }

#if DEBUG > 1
  if (FDEBUGGING (DEBUG_HANDSHAKE))
    {
      ulog (LOG_DEBUG_END, "\" (timeout)");
      iDebug = iolddebug;
    }
#endif

  ubuffree (zalc);

  if (frequired)
    ulog (LOG_ERROR, "Timeout");
  return NULL;
}

/* Read a sequence of characters up to a newline or carriage return,
   and return the line without the line terminating character.
   Remember whether the last string we returned ended in \r; if it
   did, ignore a leading \n to account for \r\n pairs.  */

static char *
zget_typed_line (qconn, fstrip)
     struct sconnection *qconn;
     boolean fstrip;
{
  static boolean flastcr; 
  char *zalc;
  size_t calc;
  size_t cgot;

#if DEBUG > 1
  int cchars;
  int iolddebug;

  cchars = 0;
  iolddebug = iDebug;
  if (FDEBUGGING (DEBUG_CHAT))
    {
      ulog (LOG_DEBUG_START, "zget_typed_line: Got \"");
      iDebug &=~ (DEBUG_INCOMING | DEBUG_PORT);
    }
#endif

  zalc = NULL;
  calc = 0;
  cgot = 0;
  while (TRUE)
    {
      int b;
      
      b = breceive_char (qconn, CTIMEOUT, FALSE);

      /* Now b == -1 on timeout, -2 on error.  */

      if (b == -2 || FGOT_SIGNAL ())
	{
#if DEBUG > 1
	  if (FDEBUGGING (DEBUG_CHAT))
	    {
	      ulog (LOG_DEBUG_END, "\" (error)");
	      iDebug = iolddebug;
	    }
#endif
	  ubuffree (zalc);
	  flastcr = FALSE;
	  return NULL;
	}

      if (b == -1)
	{
	  flastcr = FALSE;
	  continue;
	}

      /* Optionally strip the parity bit.  */
      if (fstrip)
	b &= 0x7f;

#if DEBUG > 1
      if (FDEBUGGING (DEBUG_CHAT))
	{
	  char ab[5];

	  ++cchars;
	  if (cchars > 60)
	    {
	      ulog (LOG_DEBUG_END, "\"");
	      ulog (LOG_DEBUG_START, "zget_typed_line: Got \"");
	      cchars = 0;
	    }
	  (void) cdebug_char (ab, b);
	  ulog (LOG_DEBUG_CONTINUE, "%s", ab);
	}
#endif

      if (b == '\n' && cgot == 0 && flastcr)
	{
	  /* Ignore \n in \r\n pair.  */
	  flastcr = FALSE;
	  continue;
	}

      flastcr = FALSE;

      if (cgot >= calc)
	{
	  char *znew;

	  calc += CINCREMENT;
	  znew = zbufalc (calc);
	  if (cgot > 0)
	    memcpy (znew, zalc, cgot);
	  ubuffree (zalc);
	  zalc = znew;
	}

      if (b == '\n')
	b = '\0';
      else if (b == '\r')
	{
	  flastcr = TRUE;
	  b = '\0';
	}

      zalc[cgot] = (char) b;
      ++cgot;

      if (b == '\0')
	{
#if DEBUG > 1
	  if (FDEBUGGING (DEBUG_CHAT))
	    {
	      ulog (LOG_DEBUG_END, "\"");
	      iDebug = iolddebug;
	    }
#endif
	  return zalc;
	}
    }
}

/* Spawn a uuxqt job.  This probably belongs in some other file, but I
   don't have a good place for it.  */

boolean
fspawn_uuxqt (ffork, zsys, zconfig)
     boolean ffork;
     const char *zsys;
     const char *zconfig;
{
  char *zsysarg;
  char *zconfigarg;
  boolean fret;

  if (zsys == NULL)
    zsysarg = NULL;
  else
    {
      zsysarg = zbufalc (sizeof "-s" + strlen (zsys));
      sprintf (zsysarg, "-s%s", zsys);
    }

  if (zconfig == NULL)
    zconfigarg = NULL;
  else
    {
      zconfigarg = zbufalc (sizeof "-I" + strlen (zconfig));
      sprintf (zconfigarg, "-I%s", zconfig);
      if (zsysarg == NULL)
	{
	  zsysarg = zconfigarg;
	  zconfigarg = NULL;
	}
    }

  fret = fsysdep_run (ffork, "uuxqt", zsysarg, zconfigarg);

  ubuffree (zsysarg);
  ubuffree (zconfigarg);

  return fret;
}
OpenPOWER on IntegriCloud