File: bltBgexec.c

package info (click to toggle)
blt 2.5.3%2Bdfsg-3
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 24,864 kB
  • sloc: ansic: 133,724; tcl: 17,680; sh: 2,722; makefile: 799; cpp: 370
file content (2260 lines) | stat: -rw-r--r-- 60,905 bytes parent folder | download | duplicates (5)
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
/*
 * bltBgexec.c --
 *
 *	This module implements a background "exec" command for the
 *	BLT toolkit.
 *
 * Copyright 1993-1998 Lucent Technologies, Inc.
 *
 * Permission to use, copy, modify, and distribute this software and
 * its documentation for any purpose and without fee is hereby
 * granted, provided that the above copyright notice appear in all
 * copies and that both that the copyright notice and warranty
 * disclaimer appear in supporting documentation, and that the names
 * of Lucent Technologies any of their entities not be used in
 * advertising or publicity pertaining to distribution of the software
 * without specific, written prior permission.
 *
 * Lucent Technologies disclaims all warranties with regard to this
 * software, including all implied warranties of merchantability and
 * fitness.  In no event shall Lucent Technologies be liable for any
 * special, indirect or consequential damages or any damages
 * whatsoever resulting from loss of use, data or profits, whether in
 * an action of contract, negligence or other tortuous action, arising
 * out of or in connection with the use or performance of this
 * software.
 *
 *	The "bgexec" command was created by George Howlett.
 */

#include "bltInt.h"

#ifndef NO_BGEXEC

#include <fcntl.h>
#include <signal.h>
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#include <sys/types.h>

#include "bltWait.h"
#include "bltSwitch.h"

#if (TCL_MAJOR_VERSION == 7)
#define FILEHANDLER_USES_TCLFILES 1
#else
typedef int Tcl_File;
#endif

static Tcl_CmdProc BgexecCmd;

#ifdef WIN32
typedef struct {
    DWORD pid;
    HANDLE hProcess;
} Process;
#else
typedef int Process;
#endif

#if (TCL_VERSION_NUMBER <  _VERSION(8,1,0)) 
typedef void *Tcl_Encoding;	/* Make up dummy type for encoding.  */
#endif

#define ENCODING_ASCII		((Tcl_Encoding)NULL)
#define ENCODING_BINARY		((Tcl_Encoding)1)

/*
 *	As of Tcl 7.6, we're using our own version of the old
 *	Tcl_CreatePipeline routine.  I would have tried to use
 *	Tcl_OpenCommandChannel but you can't get at the array of
 *	process ids, unless of course you pry open the undocumented
 *	structure PipeStatus as clientData.  Nor could I figure out
 *	how to set one side of the pipe to be non-blocking.  The whole
 *	channel API seems overly complex for what its supposed to
 *	do. [And maybe that's why it keeps changing every release.]
 */
extern int Blt_CreatePipeline _ANSI_ARGS_((Tcl_Interp *interp, int argc,
	char **argv, Process **pidPtrPtr, int *inPipePtr, int *outPipePtr,
	int *errFilePtr));

#ifdef WIN32
#define read(fd, buf, size)	Blt_AsyncRead((fd),(buf),(size))
#define close(fd)		CloseHandle((HANDLE)fd)
#define Tcl_CreateFileHandler	Blt_CreateFileHandler
#define Tcl_DeleteFileHandler	Blt_DeleteFileHandler
#define kill			KillProcess
#define waitpid			WaitProcess
#endif

#define	EXPECTED_NUM_TIMERS	4

#define READ_AGAIN	(0)
#define READ_EOF	(-1)
#define READ_ERROR	(-2)

/* The wait-related definitions are taken from tclUnix.h */

#define TRACE_FLAGS (TCL_TRACE_WRITES | TCL_TRACE_UNSETS | TCL_GLOBAL_ONLY)

#define BLOCK_SIZE	1024	/* Size of allocation blocks for buffer */
#define DEF_BUFFER_SIZE	(BLOCK_SIZE * 8)
#define MAX_READS       100	/* Maximum number of successful reads
			         * before stopping to let Tcl catch up
			         * on events */

#ifndef NSIG
#define NSIG 		32	/* Number of signals available */
#endif /*NSIG*/

#ifndef SIGINT
#define SIGINT		2
#endif /* SIGINT */

#ifndef SIGQUIT
#define SIGQUIT		3
#endif /* SIGQUIT */

#ifndef SIGKILL
#define SIGKILL		9
#endif /* SIGKILL */

#ifndef SIGTERM
#define SIGTERM		14
#endif /* SIGTERM */

typedef struct {
    int number;
    char *name;
} SignalId;

static SignalId signalIds[] =
{
#ifdef SIGABRT
    {SIGABRT, "SIGABRT"},
#endif
#ifdef SIGALRM
    {SIGALRM, "SIGALRM"},
#endif
#ifdef SIGBUS
    {SIGBUS, "SIGBUS"},
#endif
#ifdef SIGCHLD
    {SIGCHLD, "SIGCHLD"},
#endif
#if defined(SIGCLD) && (!defined(SIGCHLD) || (SIGCLD != SIGCHLD))
    {SIGCLD, "SIGCLD"},
#endif
#ifdef SIGCONT
    {SIGCONT, "SIGCONT"},
#endif
#if defined(SIGEMT) && (!defined(SIGXCPU) || (SIGEMT != SIGXCPU))
    {SIGEMT, "SIGEMT"},
#endif
#ifdef SIGFPE
    {SIGFPE, "SIGFPE"},
#endif
#ifdef SIGHUP
    {SIGHUP, "SIGHUP"},
#endif
#ifdef SIGILL
    {SIGILL, "SIGILL"},
#endif
#ifdef SIGINT
    {SIGINT, "SIGINT"},
#endif
#ifdef SIGIO
    {SIGIO, "SIGIO"},
#endif
#if defined(SIGIOT) && (!defined(SIGABRT) || (SIGIOT != SIGABRT))
    {SIGIOT, "SIGIOT"},
#endif
#ifdef SIGKILL
    {SIGKILL, "SIGKILL"},
#endif
#if defined(SIGLOST) && (!defined(SIGIOT) || (SIGLOST != SIGIOT)) && (!defined(SIGURG) || (SIGLOST != SIGURG))
    {SIGLOST, "SIGLOST"},
#endif
#ifdef SIGPIPE
    {SIGPIPE, "SIGPIPE"},
#endif
#if defined(SIGPOLL) && (!defined(SIGIO) || (SIGPOLL != SIGIO))
    {SIGPOLL, "SIGPOLL"},
#endif
#ifdef SIGPROF
    {SIGPROF, "SIGPROF"},
#endif
#if defined(SIGPWR) && (!defined(SIGXFSZ) || (SIGPWR != SIGXFSZ))
    {SIGPWR, "SIGPWR"},
#endif
#ifdef SIGQUIT
    {SIGQUIT, "SIGQUIT"},
#endif
#ifdef SIGSEGV
    {SIGSEGV, "SIGSEGV"},
#endif
#ifdef SIGSTOP
    {SIGSTOP, "SIGSTOP"},
#endif
#ifdef SIGSYS
    {SIGSYS, "SIGSYS"},
#endif
#ifdef SIGTERM
    {SIGTERM, "SIGTERM"},
#endif
#ifdef SIGTRAP
    {SIGTRAP, "SIGTRAP"},
#endif
#ifdef SIGTSTP
    {SIGTSTP, "SIGTSTP"},
#endif
#ifdef SIGTTIN
    {SIGTTIN, "SIGTTIN"},
#endif
#ifdef SIGTTOU
    {SIGTTOU, "SIGTTOU"},
#endif
#if defined(SIGURG) && (!defined(SIGIO) || (SIGURG != SIGIO))
    {SIGURG, "SIGURG"},
#endif
#if defined(SIGUSR1) && (!defined(SIGIO) || (SIGUSR1 != SIGIO))
    {SIGUSR1, "SIGUSR1"},
#endif
#if defined(SIGUSR2) && (!defined(SIGURG) || (SIGUSR2 != SIGURG))
    {SIGUSR2, "SIGUSR2"},
#endif
#ifdef SIGVTALRM
    {SIGVTALRM, "SIGVTALRM"},
#endif
#ifdef SIGWINCH
    {SIGWINCH, "SIGWINCH"},
#endif
#ifdef SIGXCPU
    {SIGXCPU, "SIGXCPU"},
#endif
#ifdef SIGXFSZ
    {SIGXFSZ, "SIGXFSZ"},
#endif
    {-1, "unknown signal"},
};

/*
 * Sink buffer:
 *   ____________________
 *  |                    |  "size"	current allocated length of buffer.
 *  |                    |
 *  |--------------------|  "fill"      fill point (# characters in buffer).
 *  |  Raw               |
 *  |--------------------|  "mark"      Marks end of cooked characters.
 *  |                    |
 *  |  Cooked            |
 *  |                    |
 *  |                    |
 *  |--------------------|  "lastMark"  Mark end of processed characters.
 *  |                    |
 *  |                    |
 *  |  Processed         |
 *  |                    |
 *  |____________________| 0
 */
typedef struct {
    char *name;			/* Name of the sink */

    char *doneVar;		/* Name of a Tcl variable (malloc'ed)
				 * set to the collected data of the
				 * last UNIX subprocess. */

    char *updateVar;		/* Name of a Tcl variable (malloc'ed)
				 * updated as data is read from the
				 * pipe. */

    char **updateCmd;		/* Start of a Tcl command executed
				 * whenever data is read from the
				 * pipe. */

#if (TCL_MAJOR_VERSION >= 8)
    Tcl_Obj **objv;		/*  */
    int objc;			/*  */
#endif

    int flags;			

    Tcl_File file;		/* Used for backward compatability
				 * with Tcl 7.5 */
    Tcl_Encoding encoding;
    int fd;			/* File descriptor of the pipe. */
    int status;

    int echo;			/* Indicates if the pipeline's stderr stream
				 * should be echoed */
    unsigned char *byteArr;	/* Stores pipeline output (malloc-ed):
				 * Initially points to static storage
				 */
    size_t size;		/* Size of dynamically allocated buffer. */

    size_t fill;		/* # of bytes read into the buffer. Marks
				 * the current fill point of the buffer. */

    size_t mark;		/* # of bytes translated (cooked). */
    size_t lastMark;		/* # of bytes as of the last read.
				 * This indicates the start of the new
				 * data in the buffer since the last
				 * time the "update" variable was
				 * set. */

    char *command;          /* Command callback. */
    unsigned char staticSpace[DEF_BUFFER_SIZE];	/* Static space */

} Sink;

#define SINK_BUFFERED		(1<<0)
#define SINK_KEEP_NL		(1<<1)
#define SINK_NOTIFY		(1<<2)

typedef struct {
    char *statVar;		/* Name of a Tcl variable set to the
				 * exit status of the last
				 * process. Setting this variable
				 * triggers the termination of all
				 * subprocesses (regardless whether
				 * they have already completed) */

    int signalNum;		/* If non-zero, indicates the signal
				 * to send subprocesses when cleaning
				 * up.*/
    int localVars;    /* Use local vars. */
    int keepNewline;		/* If non-zero, indicates to set Tcl
				 * output variables with trailing
				 * newlines intact */

    int lineBuffered;		/* If non-zero, indicates provide data
				 * to update variable and update proc on
				 * a line-by-line basis. */

    int interval;		/* Interval to poll for the exiting
				 * processes */
    char *outputEncodingName;	/* Name of decoding scheme to use when
				 * translating output data. */
    char *errorEncodingName;	/* Name of decoding scheme to use when
				 * translating output data. */

    /* Private */
    Tcl_Interp *interp;		/* Interpreter containing variables */

    int nProcs;			/* Number of processes in pipeline */
    Process *procArr;		/* Array of process tokens from pipeline.
				 * The token for Unix are pid_t, while
				 * for Win32 they're handles. */

    int traced;			/* Indicates that the status variable
				 * is currently being traced. */
    int detached;		/* Indicates that the pipeline is
				 * detached from standard I/O, running
				 * in the background. */
    int nTimers;		/* Number of timer handlers */
    Tcl_TimerToken *timerTokens;/* Array of tokens for timer handlers which 
				 * polls for the exit status of each
				 * sub-process. If zero, there's no
				 * timer handler queued. */

    int *exitCodePtr;		/* Pointer to a memory location to
				 * contain the last process' exit
				 * code. */
    int *donePtr;

    Sink sink1, sink2;
    int maxBytes;           /* Max string length. */
    int maxExceed;
    int varFlags;
    int doRaise;
    int closeKill;
    int cKilled;
} BackgroundInfo;


static Blt_SwitchParseProc StringToSignal;
static Blt_SwitchCustom killSignalSwitch =
{
    StringToSignal, (Blt_SwitchFreeProc *)NULL, (ClientData)0,
};

static Blt_SwitchSpec switchSpecs[] = 
{
    {BLT_SWITCH_INT, "-check",
	Blt_Offset(BackgroundInfo, interval), 0},
    {BLT_SWITCH_INT, "-closeonkill", 
         Blt_Offset(BackgroundInfo, closeKill), 0},
    {BLT_SWITCH_STRING, "-command", 
         Blt_Offset(BackgroundInfo, sink1.command), 0},
    {BLT_SWITCH_STRING, "-decodeerror", 
         Blt_Offset(BackgroundInfo, errorEncodingName), 0},
    {BLT_SWITCH_STRING, "-decodeoutput", 
         Blt_Offset(BackgroundInfo, outputEncodingName), 0},
    {BLT_SWITCH_BOOLEAN, "-echo", 
         Blt_Offset(BackgroundInfo, sink2.echo), 0},
    {BLT_SWITCH_STRING, "-error", 
         Blt_Offset(BackgroundInfo, sink2.doneVar), 0},
    {BLT_SWITCH_BOOLEAN, "-keepnewline", 
	Blt_Offset(BackgroundInfo, keepNewline), 0},
    {BLT_SWITCH_CUSTOM, "-killsignal", 
	Blt_Offset(BackgroundInfo, signalNum), 0, &killSignalSwitch},
    {BLT_SWITCH_STRING, "-lasterror", 
	Blt_Offset(BackgroundInfo, sink2.updateVar), 0},
    {BLT_SWITCH_STRING, "-lastoutput", 
	Blt_Offset(BackgroundInfo, sink1.updateVar), 0},
    {BLT_SWITCH_INT, "-limit",
	Blt_Offset(BackgroundInfo, maxBytes), 0},
    {BLT_SWITCH_BOOLEAN, "-linebuffered", 
	Blt_Offset(BackgroundInfo, lineBuffered), 0},
    {BLT_SWITCH_BOOLEAN, "-local", 
	Blt_Offset(BackgroundInfo, localVars), 0},
    {BLT_SWITCH_LIST, "-onerror", 
	Blt_Offset(BackgroundInfo, sink2.updateCmd), 0},
    {BLT_SWITCH_LIST, "-onoutput", 
	Blt_Offset(BackgroundInfo, sink1.updateCmd), 0},
    {BLT_SWITCH_STRING, "-output", 
        Blt_Offset(BackgroundInfo, sink1.doneVar), 0},
    {BLT_SWITCH_BOOLEAN, "-raise", 
	Blt_Offset(BackgroundInfo, doRaise), 0},
   /* {BLT_SWITCH_STRING, "-update", 
	Blt_Offset(BackgroundInfo, sink1.updateVar), 0},*/
    {BLT_SWITCH_END, NULL, 0, 0}
};

static char *VariableProc _ANSI_ARGS_((ClientData clientData,
	Tcl_Interp *interp, char *part1, char *part2, int flags));
static void TimerProc _ANSI_ARGS_((ClientData clientData));
static void StdoutProc _ANSI_ARGS_((ClientData clientData, int mask));
static void StderrProc _ANSI_ARGS_((ClientData clientData, int mask));
static void AddTimerHandler _ANSI_ARGS_((BackgroundInfo *bgPtr,int interval));
static void InterpDeleted _ANSI_ARGS_(( ClientData clientData, Tcl_Interp *interp));

/*
 *----------------------------------------------------------------------
 *
 * GetSignal --
 *
 *	Convert a string represent a signal number into its integer
 *	value.
 *
 * Results:
 *	The return value is a standard Tcl result.
 *
 *----------------------------------------------------------------------
 */
/*ARGSUSED*/
static int
StringToSignal(clientData, interp, switchName, string, record, offset)
    ClientData clientData;	/* Contains a pointer to the tabset containing
				 * this image. */
    Tcl_Interp *interp;		/* Interpreter to send results back to */
    char *switchName;		/* Not used. */
    char *string;		/* String representation */
    char *record;		/* Structure record */
    int offset;			/* Offset to field in structure */
{
    int *signalPtr = (int *)(record + offset);
    int signalNum;

    if ((string == NULL) || (*string == '\0')) {
	*signalPtr = 0;
	return TCL_OK;
    }
    if (isdigit(UCHAR(string[0]))) {
	if (Tcl_GetInt(interp, string, &signalNum) != TCL_OK) {
	    return TCL_ERROR;
	}
    } else {
	char *name;
	register SignalId *sigPtr;

	name = string;

	/*  Clip off any "SIG" prefix from the signal name */
	if ((name[0] == 'S') && (name[1] == 'I') && (name[2] == 'G')) {
	    name += 3;
	}
	signalNum = -1;
	for (sigPtr = signalIds; sigPtr->number > 0; sigPtr++) {
	    if (strcmp(sigPtr->name + 3, name) == 0) {
		signalNum = sigPtr->number;
		break;
	    }
	}
	if (signalNum < 0) {
	    Tcl_AppendResult(interp, "unknown signal \"", string, "\"",
		(char *)NULL);
	    return TCL_ERROR;
	}
    }
    if ((signalNum < 0) || (signalNum > NSIG)) {
	/* Outside range of signals */
	Tcl_AppendResult(interp, "signal number \"", string,
	    "\" is out of range", (char *)NULL);
	return TCL_ERROR;
    }
    *signalPtr = signalNum;
    return TCL_OK;
}


/*
 *----------------------------------------------------------------------
 *
 * GetSinkData --
 *
 *	Returns the data currently saved in the buffer
 *
 *----------------------------------------------------------------------
 */
static void
GetSinkData(sinkPtr, dataPtr, lengthPtr)
    Sink *sinkPtr;
    unsigned char **dataPtr;
    size_t *lengthPtr;
{
    size_t length;

    sinkPtr->byteArr[sinkPtr->mark] = '\0';
    length = sinkPtr->mark;
    if ((sinkPtr->mark > 0) && (sinkPtr->encoding != ENCODING_BINARY)) {
	unsigned char *last;

	last = sinkPtr->byteArr + (sinkPtr->mark - 1);
	if ((!(sinkPtr->flags & SINK_KEEP_NL)) && (*last == '\n')) {
	    length--;
	}
    }
    *dataPtr = sinkPtr->byteArr;
    *lengthPtr = length;
}

/*
 *----------------------------------------------------------------------
 *
 * NextBlock --
 *
 *	Returns the next block of data since the last time this
 *	routine was called.
 *
 *---------------------------------------------------------------------- 
 */
static unsigned char *
NextBlock(sinkPtr, lengthPtr)
    Sink *sinkPtr;
    int *lengthPtr;
{
    unsigned char *string;
    int length;

    string = sinkPtr->byteArr + sinkPtr->lastMark;
    length = sinkPtr->mark - sinkPtr->lastMark;
    sinkPtr->lastMark = sinkPtr->mark;
    if (length > 0) {
	if ((!(sinkPtr->flags & SINK_KEEP_NL)) && 
	    (string[length - 1] == '\n')) {
	    length--;
	}
	*lengthPtr = length;
	return string;
    }
    *lengthPtr = length;
    return NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * NextLine --
 *
 *	Returns the next line of data.
 *
 *----------------------------------------------------------------------
 */
static unsigned char *
NextLine(sinkPtr, lengthPtr)
    Sink *sinkPtr;
    int *lengthPtr;
{
    if (sinkPtr->mark > sinkPtr->lastMark) {
	unsigned char *string;
	int newBytes;
	register int i;

	string = sinkPtr->byteArr + sinkPtr->lastMark;
	newBytes = sinkPtr->mark - sinkPtr->lastMark;
	for (i = 0; i < newBytes; i++) {
	    if (string[i] == '\n') {
		int length;
		
		length = i + 1;
		sinkPtr->lastMark += length;
		if (!(sinkPtr->flags & SINK_KEEP_NL)) {
		    length--;		/* Backup over the newline. */
		}
		*lengthPtr = length;
		return string;
	    }
	}
	/* Newline not found.  On errors or EOF, also return a partial line. */
	if (sinkPtr->status < 0) {
	    *lengthPtr = newBytes;
	    sinkPtr->lastMark = sinkPtr->mark;
	    return string;
	}
    }
    *lengthPtr = 0;
    return NULL;
}
/*
 *----------------------------------------------------------------------
 *
 * ResetSink --
 *
 *	Removes the bytes already processed from the buffer, possibly
 *	resetting it to empty.  This used when we don't care about
 *	keeping all the data collected from the channel (no -output
 *	flag and the process is detached).
 *
 *---------------------------------------------------------------------- 
 */
static void
ResetSink(sinkPtr)
    Sink *sinkPtr;
{ 
    if ((sinkPtr->flags & SINK_BUFFERED) && 
	(sinkPtr->fill > sinkPtr->lastMark)) {
	register size_t i, j;

	/* There may be bytes remaining in the buffer, awaiting
	 * another read before we see the next newline.  So move the
	 * bytes to the front of the array. */

 	for (i = 0, j = sinkPtr->lastMark; j < sinkPtr->fill; i++, j++) {
	    sinkPtr->byteArr[i] = sinkPtr->byteArr[j];
	}
	/* Move back the fill point and processed point. */
	sinkPtr->fill -= sinkPtr->lastMark;
	sinkPtr->mark -= sinkPtr->lastMark;
    } else {
	sinkPtr->mark = sinkPtr->fill = 0;
    }
    sinkPtr->lastMark = 0;
}

/*
 *----------------------------------------------------------------------
 *
 * InitSink --
 *
 *	Initializes the buffer's storage.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Storage is cleared.
 *
 *----------------------------------------------------------------------
 */
static void
InitSink(bgPtr, sinkPtr, name, encoding)
    BackgroundInfo *bgPtr;
    Sink *sinkPtr;
    char *name;
    Tcl_Encoding encoding;
{
    sinkPtr->name = name;
    sinkPtr->echo = FALSE;
    sinkPtr->fd = -1;
    sinkPtr->file = (Tcl_File)0;
    sinkPtr->byteArr = sinkPtr->staticSpace;
    sinkPtr->size = DEF_BUFFER_SIZE;
    sinkPtr->encoding = encoding;
    if (bgPtr->keepNewline) {
	sinkPtr->flags |= SINK_KEEP_NL;
    }
    if (bgPtr->lineBuffered) {
	sinkPtr->flags |= SINK_BUFFERED;
    }	
    if ((sinkPtr->updateCmd != NULL) || 
	(sinkPtr->updateVar != NULL) ||
	(sinkPtr->echo)) {
	sinkPtr->flags |= SINK_NOTIFY;
    }
#if (TCL_MAJOR_VERSION >= 8)
    if (sinkPtr->updateCmd != NULL) {
	Tcl_Obj **objArr;
	char **p;
	int count;
	register int i;

	count = 0;
	for (p = sinkPtr->updateCmd; *p != NULL; p++) {
	    count++;
	}
	objArr = Blt_Malloc((count + 1) * sizeof(Tcl_Obj *));
	for (i = 0; i < count; i++) {
	    objArr[i] = Tcl_NewStringObj(sinkPtr->updateCmd[i], -1);
	    Tcl_IncrRefCount(objArr[i]);
	}
	sinkPtr->objv = objArr;
	sinkPtr->objc = count + 1;
    }
#endif
    ResetSink(sinkPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * FreeSinkBuffer --
 *
 *	Frees the buffer's storage, freeing any malloc'ed space.
 *
 * Results:
 *	None.
 *
 *----------------------------------------------------------------------
 */
static void
FreeSinkBuffer(sinkPtr)
    Sink *sinkPtr;
{
    if (sinkPtr->byteArr != sinkPtr->staticSpace) {
	Blt_Free(sinkPtr->byteArr);
    }
    sinkPtr->fd = -1;
    sinkPtr->file = (Tcl_File)0;
#if (TCL_MAJOR_VERSION >= 8)
    if (sinkPtr->objv != NULL) {
	register int i;

	for (i = 0; i < sinkPtr->objc - 1; i++) {
	    Tcl_DecrRefCount(sinkPtr->objv[i]);
	}
	Blt_Free(sinkPtr->objv);
    }
#endif
}


/*
 *----------------------------------------------------------------------
 *
 * ExtendSinkBuffer --
 *
 *	Doubles the size of the current buffer.
 *
 * Results:
 *	None.
 *
 *----------------------------------------------------------------------
 */
static int
ExtendSinkBuffer(sinkPtr)
    Sink *sinkPtr;
{
    unsigned char *arrayPtr;
    register unsigned char *srcPtr, *destPtr, *endPtr;
    /*
     * Allocate a new array, double the old size
     */
    sinkPtr->size += sinkPtr->size;
    arrayPtr = Blt_Malloc(sizeof(unsigned char) * sinkPtr->size);
    if (arrayPtr == NULL) {
	return -1;
    }
    srcPtr = sinkPtr->byteArr;
    endPtr = sinkPtr->byteArr + sinkPtr->fill;
    destPtr = arrayPtr;
    while (srcPtr < endPtr) {
	*destPtr++ = *srcPtr++;
    }
    if (sinkPtr->byteArr != sinkPtr->staticSpace) {
	Blt_Free(sinkPtr->byteArr);
    }
    sinkPtr->byteArr = arrayPtr;

    return (sinkPtr->size - sinkPtr->fill); /* Return bytes left. */
}

/*
 *----------------------------------------------------------------------
 *
 * ReadBytes --
 *
 *	Reads and appends any available data from a given file descriptor
 *	to the buffer.
 *
 * Results:
 *	Returns TCL_OK when EOF is found, TCL_RETURN if reading
 *	data would block, and TCL_ERROR if an error occurred.
 *
 *----------------------------------------------------------------------
 */
static void
ReadBytes(bgPtr, sinkPtr)
    BackgroundInfo *bgPtr;
    Sink *sinkPtr;
{
    int nBytes, bytesLeft;
    register int i;
    unsigned char *array;

    /*
     * ------------------------------------------------------------------
     *
     * 	Worry about indefinite postponement.
     *
     * 	Typically we want to stay in the read loop as long as it takes
     * 	to collect all the data that's currently available.  But if
     * 	it's coming in at a constant high rate, we need to arbitrarily
     * 	break out at some point. This allows for both setting the
     * 	update variable and the Tk program to handle idle events.
     *
     * ------------------------------------------------------------------
     */

    for (i = 0; i < MAX_READS; i++) {

	/* Allocate a larger buffer when the number of remaining bytes
	 * is below the threshold BLOCK_SIZE.  */

	bytesLeft = sinkPtr->size - sinkPtr->fill;

	if (bytesLeft < BLOCK_SIZE) {
	    bytesLeft = ExtendSinkBuffer(sinkPtr);
	    if (bytesLeft < 0) {
		sinkPtr->status = READ_ERROR;
		return;
	    }
	}
	array = sinkPtr->byteArr + sinkPtr->fill;

	/*
	 * Read into a buffer but make sure we leave room for a
	 * trailing NUL byte.
	 */
	nBytes = read(sinkPtr->fd, array, bytesLeft - 1);
	if (nBytes == 0) {	/* EOF: break out of loop. */
	    sinkPtr->status = READ_EOF;
	    return;
	}
	if (nBytes < 0) {
#ifdef O_NONBLOCK
#define BLOCKED		EAGAIN
#else
#define BLOCKED		EWOULDBLOCK
#endif /*O_NONBLOCK*/
	    /* Either an error has occurred or no more data is
	     * currently available to read.  */
	    if (errno == BLOCKED) {
		sinkPtr->status = READ_AGAIN;
		return;
	    }
	    sinkPtr->byteArr[0] = '\0';
	    sinkPtr->status = READ_ERROR;
	    return;
	}
	sinkPtr->fill += nBytes;
	sinkPtr->byteArr[sinkPtr->fill] = '\0';
    }
    sinkPtr->status = nBytes;
}

#define IsOpenSink(sinkPtr)  ((sinkPtr)->fd != -1)

static void
CloseSink(bgPtr, interp, sinkPtr) 
    BackgroundInfo *bgPtr;
    Tcl_Interp *interp;
    Sink *sinkPtr;
{
    unsigned char *data = NULL;
    size_t length;
    
    if (IsOpenSink(sinkPtr)) {
	close(sinkPtr->fd);
#ifdef FILEHANDLER_USES_TCLFILES
	Tcl_DeleteFileHandler(sinkPtr->file);
	Tcl_FreeFile(sinkPtr->file);
#else
	Tcl_DeleteFileHandler(sinkPtr->fd);
#endif
	sinkPtr->file = (Tcl_File)0;
	sinkPtr->fd = -1;

#if WINDEBUG
	PurifyPrintf("CloseSink: set done var %s\n", sinkPtr->name);
#endif
	if (sinkPtr->doneVar != NULL && !Tcl_InterpDeleted(interp)) {
	    /* 
	     * If data is to be collected, set the "done" variable
	     * with the contents of the buffer.  
	     */
	    GetSinkData(sinkPtr, &data, &length);
#if (TCL_VERSION_NUMBER <  _VERSION(8,1,0)) 
	    data[length] = '\0';
	    if (Tcl_SetVar(interp, sinkPtr->doneVar, data, 
			   bgPtr->varFlags) == NULL) {
		Tcl_BackgroundError(interp);
	    }
#else
	    if (Tcl_SetVar2Ex(interp, sinkPtr->doneVar, NULL, 
			      Tcl_NewByteArrayObj(data, length),
			      (bgPtr->varFlags | TCL_LEAVE_ERR_MSG)) == NULL) {
		Tcl_BackgroundError(interp);
	    }
#endif
	}
#if WINDEBUG
	PurifyPrintf("CloseSink %s: done\n", sinkPtr->name);
#endif
    }
}

static void
SinkCallback(interp, sinkPtr, status) 
    Tcl_Interp *interp;
    Sink *sinkPtr;
    char *status;
{
    unsigned char *data = NULL;
    size_t length;
    if (sinkPtr->command != NULL && !Tcl_InterpDeleted(interp)) {
        Tcl_DString dStr;
        Tcl_DStringInit(&dStr);
        GetSinkData(sinkPtr, &data, &length);
        Tcl_DStringAppend(&dStr, sinkPtr->command, -1);
        Tcl_DStringAppendElement(&dStr, (char *)data);
        Tcl_DStringAppendElement(&dStr, status);
        if (Tcl_GlobalEval(interp, Tcl_DStringValue(&dStr)) != TCL_OK) {
            Tcl_BackgroundError(interp);
        }
        Tcl_DStringFree(&dStr);
    }
}
/*
 *----------------------------------------------------------------------
 *
 * CookSink --
 *
 *	For Windows, translate CR/NL combinations to NL alone.
 *
 * Results:
 *	None.
 *
 * Side Effects:
 *	The size of the byte array may shrink and array contents
 *	shifted as carriage returns are found and removed.
 *
 *----------------------------------------------------------------------
 */
static void
CookSink(interp, sinkPtr)
    Tcl_Interp *interp;
    Sink *sinkPtr;
{
    unsigned char *srcPtr, *endPtr;
#ifdef WIN32
    size_t oldMark;

    oldMark = sinkPtr->mark;
#endif
    if (sinkPtr->encoding == ENCODING_BINARY) { /* binary */
	/* No translation needed. */
	sinkPtr->mark = sinkPtr->fill; 
    } else if (sinkPtr->encoding == ENCODING_ASCII) { /* ascii */
#if (TCL_VERSION_NUMBER <  _VERSION(8,1,0)) 
	/* Convert NUL bytes to question marks. */
	srcPtr = sinkPtr->byteArr + sinkPtr->mark;
	endPtr = sinkPtr->byteArr + sinkPtr->fill;
	while (srcPtr < endPtr) {
	    if (*srcPtr == '\0') {
		*srcPtr = '?';
	    }
	    srcPtr++;
	}
#endif /* < 8.1.0 */
	/* One-to-one translation. mark == fill. */
	sinkPtr->mark = sinkPtr->fill;
#if (TCL_VERSION_NUMBER >= _VERSION(8,1,0)) 
    } else { /* unicode. */
	int nSrcCooked, nCooked;
	int result;
	size_t cookedSize, spaceLeft, needed;
	size_t nRaw, nLeftOver;
	unsigned char *destPtr;
	unsigned char *raw, *cooked;
	unsigned char leftover[100];
	
	raw = sinkPtr->byteArr + sinkPtr->mark;
	nRaw = sinkPtr->fill - sinkPtr->mark;
	/* Ideally, the cooked buffer size should be smaller */
	cookedSize = nRaw * TCL_UTF_MAX + 1;
	cooked = Blt_Malloc(cookedSize);
	result = Tcl_ExternalToUtf(interp, sinkPtr->encoding, 
			(char *)raw, nRaw, 0, NULL, (char *)cooked, 
			cookedSize, &nSrcCooked, &nCooked, NULL);
	nLeftOver = 0;
	if (result == TCL_CONVERT_MULTIBYTE) {
	    /* 
	     * Last multibyte sequence wasn't completed.  Save the
	     * extra characters in a temporary buffer.  
	     */
	    nLeftOver = (nRaw - nSrcCooked);
	    srcPtr = sinkPtr->byteArr + (sinkPtr->mark + nSrcCooked); 
	    endPtr = srcPtr + nLeftOver;
	    destPtr = leftover;
	    while (srcPtr < endPtr) {
		*destPtr++ = *srcPtr++;
	    }
	} 
	/*
	 * Create a bigger 
	 */
						 
	needed = nLeftOver + nCooked;
	spaceLeft = sinkPtr->size - sinkPtr->mark;
	if (spaceLeft >= needed) {
	    spaceLeft = ExtendSinkBuffer(sinkPtr);
	}
	assert(spaceLeft > needed);
	/* 
	 * Replace the characters from the mark with the translated 
	 * characters.
	 */
	srcPtr = cooked;
	endPtr = cooked + nCooked;
	destPtr = sinkPtr->byteArr + sinkPtr->mark;
	while (srcPtr < endPtr) {
	    *destPtr++ = *srcPtr++;
	}
	/* Add the number of newly translated characters to the mark */
	sinkPtr->mark += nCooked;
	
	srcPtr = leftover;
	endPtr = leftover + nLeftOver;
	while (srcPtr < endPtr) {
	    *destPtr++ = *srcPtr++;
	}
	sinkPtr->fill = sinkPtr->mark + nLeftOver;
#endif /* >= 8.1.0  */
    }
#ifdef WIN32
    /* 
     * Translate CRLF character sequences to LF characters.  We have to
     * do this after converting the string to UTF from UNICODE.
     */
    if (sinkPtr->encoding != ENCODING_BINARY) {
	int count;
	unsigned char *destPtr;

	destPtr = srcPtr = sinkPtr->byteArr + oldMark;
	endPtr = sinkPtr->byteArr + sinkPtr->fill;
	*endPtr = '\0';
	count = 0;
	for (endPtr--; srcPtr < endPtr; srcPtr++) {
	    if ((*srcPtr == '\r') && (*(srcPtr + 1) == '\n')) {
		count++;
		continue;		/* Skip the CR in CR/LF sequences. */
	    }
	    if (srcPtr != destPtr) {
		*destPtr = *srcPtr;	/* Collapse the string, overwriting
					 * the \r's encountered. */
	    }
	    destPtr++;
	}
	sinkPtr->mark -= count;
	sinkPtr->fill -= count;
	*destPtr = *srcPtr;	/* Copy the last byte */
	if (*destPtr == '\r') {
	    sinkPtr->mark--;
	}
    }
#endif /* WIN32 */
}

#ifdef WIN32
/*
 *----------------------------------------------------------------------
 *
 * WaitProcess --
 *
 *	Emulates the waitpid system call under the Win32 API.
 *
 * Results:
 *	Returns 0 if the process is still alive, -1 on an error, or
 *	the pid on a clean close.
 *
 * Side effects:
 *	Unless WNOHANG is set and the wait times out, the process
 *	information record will be deleted and the process handle
 *	will be closed.
 *
 *----------------------------------------------------------------------
 */
#define WINDEBUG 0
static int
WaitProcess(
    Process child,
    int *statusPtr,
    int flags)
{
    int result;
    DWORD status, exitCode;
    int timeout;

#if WINDEBUG
    PurifyPrintf("WAITPID(%x)\n", child.hProcess);
#endif
    *statusPtr = 0;
    if (child.hProcess == INVALID_HANDLE_VALUE) {
	errno = EINVAL;
	return -1;
    }
#if WINDEBUG
    PurifyPrintf("WAITPID: waiting for 0x%x\n", child.hProcess);
#endif
    timeout = (flags & WNOHANG) ? 0 : INFINITE;
    status = WaitForSingleObject(child.hProcess, timeout);
				 
#if WINDEBUG
    PurifyPrintf("WAITPID: wait status is %d\n", status);
#endif
    switch (status) {
    case WAIT_FAILED:
	errno = ECHILD;
	*statusPtr = ECHILD;
	result = -1;
	break;

    case WAIT_TIMEOUT:
	if (timeout == 0) {
	    return 0;		/* Try again */
	}
	result = 0;
	break;

    default:
    case WAIT_ABANDONED:
    case WAIT_OBJECT_0:
	GetExitCodeProcess(child.hProcess, &exitCode);
	*statusPtr = ((exitCode << 8) & 0xff00);
#if WINDEBUG
	PurifyPrintf("WAITPID: exit code of %d is %d (%x)\n", child.hProcess,
	    *statusPtr, exitCode);
#endif
	result = child.pid;
	assert(result != -1);
	break;
    }
    CloseHandle(child.hProcess);
    return result;
}

static BOOL CALLBACK
EnumWindowsProc(HWND hWnd, LPARAM lParam)
{
    DWORD pid = 0;
    Process *procPtr = (Process *)lParam;

    GetWindowThreadProcessId(hWnd, &pid);
    if (pid == procPtr->pid) {
	PostMessage(hWnd, WM_CLOSE, 0, 0);
    }
    return TRUE;
}

/*
 *----------------------------------------------------------------------
 *
 * KillProcess --
 *
 *	Emulates the UNIX kill system call under Win32 API.
 *
 * Results:
 *	Returns 0 if the process is killed, -1 on an error.
 *
 * Side effects:
 *	Process is terminated.
 *
 *----------------------------------------------------------------------
 */
static int
KillProcess(Process proc, int signal)
{
    DWORD status;

    if ((proc.hProcess == NULL) || (proc.hProcess == INVALID_HANDLE_VALUE)) {
	errno = EINVAL;
	return -1;
    }

    EnumWindows(EnumWindowsProc, (LPARAM)&proc);

    /* 
     * Wait on the handle. If it signals, great. If it times out,
     * then call TerminateProcess on it.  
     *
     * On Windows 95/98 this also has the added benefit of stopping
     * KERNEL32.dll from dumping.  The 2 second number is arbitrary.
     * (1 second seems to fail intermittently).
     */
    status = WaitForSingleObject(proc.hProcess, 2000);
    if (status == WAIT_OBJECT_0) {
	return 0;
    }
    if (!TerminateProcess(proc.hProcess, 1)) {
#if WINDEBUG
	PurifyPrintf("can't terminate process (handle=%d): %s\n",
		     proc.hProcess, Blt_LastError());
#endif /* WINDEBUG */
	return -1;
    }
    return 0;
}

#endif /* WIN32 */

#if (TCL_VERSION_NUMBER < _VERSION(8,1,0)) 

static void
NotifyOnUpdate(bgPtr, interp, sinkPtr, data, nBytes)
    BackgroundInfo *bgPtr;
    Tcl_Interp *interp;
    Sink *sinkPtr;
    unsigned char *data;
    int nBytes;
{
    char save;

#if WINDEBUG_0
    PurifyPrintf("read %s\n", data);
#endif
    if (data[0] == '\0') {
	return;
    }
    save = data[nBytes];
    data[nBytes] = '\0';
    if (sinkPtr->echo) {
	Tcl_Channel channel;
	
	channel = Tcl_GetStdChannel(TCL_STDERR);
	if (channel == NULL) {
	    Tcl_AppendResult(interp, "can't get stderr channel", (char *)NULL);
	    Tcl_BackgroundError(interp);
	    sinkPtr->echo = FALSE;
	} else {
	    Tcl_Write(channel, data, nBytes);
	    if (save == '\n') {
		Tcl_Write(channel, "\n", 1);
	    }
	    Tcl_Flush(channel);
	}
    }
    if (sinkPtr->updateCmd != NULL) {
	Tcl_DString dString;
	int result;
	register char **p;

	Tcl_DStringInit(&dString);
	for (p = sinkPtr->updateCmd; *p != NULL; p++) {
	    Tcl_DStringAppendElement(&dString, *p);
	}
	Tcl_DStringAppendElement(&dString, data);
	result = Tcl_GlobalEval(interp, Tcl_DStringValue(&dString));
	Tcl_DStringFree(&dString);
	if (result != TCL_OK) {
	    Tcl_BackgroundError(interp);
	}
    }
    if (sinkPtr->updateVar != NULL) {
	int flags;
	char *result;

	flags = (bgPtr->varFlags | TCL_LEAVE_ERR_MSG);
	result = Tcl_SetVar(interp, sinkPtr->updateVar, data, flags);
	if (result == NULL) {
	    Tcl_BackgroundError(interp);
	}
    }
    data[nBytes] = save;
}

#else 

static void
NotifyOnUpdate(bgPtr, interp, sinkPtr, data, nBytes)
    BackgroundInfo *bgPtr;
    Tcl_Interp *interp;
    Sink *sinkPtr;
    unsigned char *data;
    int nBytes;
{
    Tcl_Obj *objPtr;

#if WINDEBUG_0
    PurifyPrintf("read %s\n", data);
#endif
    if ((nBytes == 0) || (data[0] == '\0')) {
	return;
    }
    if (sinkPtr->echo) {
	Tcl_Channel channel;
	
	channel = Tcl_GetStdChannel(TCL_STDERR);
	if (channel == NULL) {
	    Tcl_AppendResult(interp, "can't get stderr channel", (char *)NULL);
	    Tcl_BackgroundError(interp);
	    sinkPtr->echo = FALSE;
	} else {
	    if (data[nBytes] == '\n') {
		objPtr = Tcl_NewByteArrayObj(data, nBytes + 1);
	    } else {
		objPtr = Tcl_NewByteArrayObj(data, nBytes);
	    }
	    Tcl_WriteObj(channel, objPtr);
	    Tcl_Flush(channel);
	}
    }

    objPtr = Tcl_NewByteArrayObj(data, nBytes);
    Tcl_IncrRefCount(objPtr);
    if (sinkPtr->objv != NULL) {
	int result;

	sinkPtr->objv[sinkPtr->objc - 1] = objPtr;
	result = Tcl_EvalObjv(interp, sinkPtr->objc, sinkPtr->objv, TCL_GLOBAL_ONLY);
	if (result != TCL_OK) {
	    Tcl_BackgroundError(interp);
	}
    }
    if (sinkPtr->updateVar != NULL) {
	Tcl_Obj *result;

	result = Tcl_SetVar2Ex(interp, sinkPtr->updateVar, NULL, objPtr, 
	       (bgPtr->varFlags | TCL_LEAVE_ERR_MSG));
	if (result == NULL) {
	    Tcl_BackgroundError(interp);
	}
    }
    Tcl_DecrRefCount(objPtr);
}

#endif /* < 8.1.0 */

static int
CollectData(bgPtr, sinkPtr)
    BackgroundInfo *bgPtr;
    Sink *sinkPtr;
{
    if ((bgPtr->detached) && (sinkPtr->doneVar == NULL) && (sinkPtr->command == NULL)) {
	ResetSink(sinkPtr);
    }
    ReadBytes(bgPtr, sinkPtr);
    CookSink(bgPtr->interp, sinkPtr);
    if ((sinkPtr->mark > sinkPtr->lastMark) && 
	(sinkPtr->flags & SINK_NOTIFY)) {
	unsigned char *data;
	int length;

	if (sinkPtr->flags & SINK_BUFFERED) {
	    /* For line-by-line updates, call NotifyOnUpdate for each
	     * new complete line.  */
	    while ((data = NextLine(sinkPtr, &length)) != NULL) {
		NotifyOnUpdate(bgPtr, bgPtr->interp, sinkPtr, data, length);
 	    }
	} else {
	    data = NextBlock(sinkPtr, &length);
	    NotifyOnUpdate(bgPtr, bgPtr->interp, sinkPtr, data, length);
	}
    }
    if (bgPtr->maxBytes>0) {
        if (sinkPtr->mark >= bgPtr->maxBytes) {
            bgPtr->maxExceed = 1;
            return TCL_BREAK;
        }
    }
    if (sinkPtr->status >= 0) {
	return TCL_OK;
    }
    if (sinkPtr->status == READ_ERROR) {
	Tcl_AppendResult(bgPtr->interp, "can't read data from ", sinkPtr->name,
	    ": ", Tcl_PosixError(bgPtr->interp), (char *)NULL);
	Tcl_BackgroundError(bgPtr->interp);
	return TCL_ERROR;
    }
#if WINDEBUG
    PurifyPrintf("CollectData %s: done\n", sinkPtr->name);
#endif
    return TCL_RETURN;
}

/*
 *----------------------------------------------------------------------
 *
 * CreateSinkHandler --
 *
 *	Creates a file handler for the given sink.  The file
 *	descriptor is also set for non-blocking I/O.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	The memory allocated to the BackgroundInfo structure released.
 *
 *----------------------------------------------------------------------
 */
static int
CreateSinkHandler(bgPtr, sinkPtr, proc)
    BackgroundInfo *bgPtr;
    Sink *sinkPtr;
    Tcl_FileProc *proc;
{
#ifndef WIN32
    int flags;

    flags = fcntl(sinkPtr->fd, F_GETFL);
#ifdef O_NONBLOCK
    flags |= O_NONBLOCK;
#else
    flags |= O_NDELAY;
#endif
    if (fcntl(sinkPtr->fd, F_SETFL, flags) < 0) {
	Tcl_AppendResult(bgPtr->interp, "can't set file descriptor ",
	    Blt_Itoa(sinkPtr->fd), " to non-blocking:",
	    Tcl_PosixError(bgPtr->interp), (char *)NULL);
	return TCL_ERROR;
    }
#endif /* WIN32 */
#ifdef FILEHANDLER_USES_TCLFILES
    sinkPtr->file = Tcl_GetFile((ClientData)sinkPtr->fd, TCL_UNIX_FD);
    Tcl_CreateFileHandler(sinkPtr->file, TCL_READABLE, proc, bgPtr);
#else
    Tcl_CreateFileHandler(sinkPtr->fd, TCL_READABLE, proc, bgPtr);
#endif /* FILEHANDLER_USES_TCLFILES */
    return TCL_OK;
}

static void
DisableTriggers(bgPtr)
    BackgroundInfo *bgPtr;	/* Background info record. */
{
    int k;

    if (bgPtr->traced) {
	Tcl_UntraceVar(bgPtr->interp, bgPtr->statVar, TRACE_FLAGS, 
		VariableProc, bgPtr);
	bgPtr->traced = FALSE;
    }
    if (IsOpenSink(&bgPtr->sink1)) {
	CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink1);
    }
    if (IsOpenSink(&bgPtr->sink2)) {
	CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink2);
    }
    for( k = 0 ; k < bgPtr->nTimers ; k++ ){
      Tcl_DeleteTimerHandler(bgPtr->timerTokens[k]);
    }
    bgPtr->nTimers = 0;
    if (bgPtr->donePtr != NULL) {
	*bgPtr->donePtr = TRUE;
    }
}


/*
 *----------------------------------------------------------------------
 *
 * FreeBackgroundInfo --
 *
 *	Releases the memory allocated for the backgrounded process.
 *
 *----------------------------------------------------------------------
 */
static void
FreeBackgroundInfo(bgPtr)
    BackgroundInfo *bgPtr;
{
    Blt_FreeSwitches(bgPtr->interp, switchSpecs, (char *)bgPtr, 0);
    if (bgPtr->statVar != NULL) {
	Blt_Free(bgPtr->statVar);
    }
    if (bgPtr->procArr != NULL) {
	Blt_Free(bgPtr->procArr);
    }
    if (bgPtr->timerTokens != NULL) {
	Blt_Free(bgPtr->timerTokens);
    }
    Blt_Free(bgPtr);
}

/*
 *----------------------------------------------------------------------
 *
 * DestroyBackgroundInfo --
 *
 * 	This procedure is invoked by Tcl_EventuallyFree or Tcl_Release
 * 	to clean up the internal structure (BackgroundInfo) at a safe
 * 	time (when no one is using it anymore).
 *
 * Results:
 *	None.b
 *
 * Side effects:
 *	The memory allocated to the BackgroundInfo structure released.
 *
 *----------------------------------------------------------------------
 */
/* ARGSUSED */
static void
DestroyBackgroundInfo(bgPtr)
    BackgroundInfo *bgPtr;	/* Background info record. */
{
    Tcl_DontCallWhenDeleted(bgPtr->interp, InterpDeleted, (ClientData)bgPtr);
    DisableTriggers(bgPtr);
    FreeSinkBuffer(&bgPtr->sink2);
    FreeSinkBuffer(&bgPtr->sink1);
    if (bgPtr->procArr != NULL) {
	register int i;

	for (i = 0; i < bgPtr->nProcs; i++) {
	    if (bgPtr->signalNum > 0) {
		kill(bgPtr->procArr[i], bgPtr->signalNum);
	    }
#ifdef WIN32
	    Tcl_DetachPids(1, (Tcl_Pid *)&bgPtr->procArr[i].pid);
#else
#if (TCL_MAJOR_VERSION == 7)
	    Tcl_DetachPids(1, &bgPtr->procArr[i]);
#else
	    Tcl_DetachPids(1, (Tcl_Pid *)&bgPtr->procArr[i]);
#endif /* TCL_MAJOR_VERSION == 7 */
#endif /* WIN32 */
	}
    }
    FreeBackgroundInfo(bgPtr);
    Tcl_ReapDetachedProcs();
}

/*
 * ----------------------------------------------------------------------
 *
 * VariableProc --
 *
 *	Kills all currently running subprocesses (given the specified
 *	signal). This procedure is called when the user sets the status
 *	variable associated with this group of child subprocesses.
 *
 * Results:
 *	Always returns NULL.  Only called from a variable trace.
 *
 * Side effects:
 *	The subprocesses are signaled for termination using the
 *	specified kill signal.  Additionally, any resources allocated
 *	to track the subprocesses is released.
 *
 * ----------------------------------------------------------------------
 */
/* ARGSUSED */
static char *
VariableProc(
    ClientData clientData,	/* File output information. */
    Tcl_Interp *interp,
    char *part1,
    char *part2,		/* Not Used. */
    int flags)
{
    if (flags & TRACE_FLAGS) {
	BackgroundInfo *bgPtr = clientData;

	/* Kill all child processes that remain alive. */
	if ((bgPtr->procArr != NULL) && (bgPtr->signalNum > 0)) {
	    register int i;

	    for (i = 0; i < bgPtr->nProcs; i++) {
		kill(bgPtr->procArr[i], bgPtr->signalNum);
	    }
             if (bgPtr->closeKill>0 && bgPtr->nProcs>0) {
                 bgPtr->cKilled = 1;
                 AddTimerHandler( bgPtr, bgPtr->closeKill ) ;
             }
         }
    }
    return NULL;
}

/*
 *----------------------------------------------------------------------
 *
 * TimerProc --
 *
 *	This is a timer handler procedure which gets called
 *	periodically to reap any of the sub-processes if they have
 *	terminated.  After the last process has terminated, the
 *	contents of standard output are stored
 *	in the output variable, which triggers the cleanup proc (using
 *	a variable trace). The status the last process to exit is
 *	written to the status variable.
 *
 * Results:
 *	None.  Called from the Tcl event loop.
 *
 * Side effects:
 *	Many. The contents of procArr is shifted, leaving only those
 *	sub-processes which have not yet terminated.  If there are
 *	still subprocesses left, this procedure is placed in the timer
 *	queue again. Otherwise the output and possibly the status
 *	variables are updated.  The former triggers the cleanup
 *	routine which will destroy the information and resources
 *	associated with these background processes.
 *
 *----------------------------------------------------------------------
 */
static void
TimerProc(clientData)
    ClientData clientData;
{
    BackgroundInfo *bgPtr = clientData;
    register int i;
    unsigned int lastPid;
    int pid;
    enum PROCESS_STATUS { 
	PROCESS_EXITED, PROCESS_STOPPED, PROCESS_KILLED, PROCESS_UNKNOWN,
	PROCESS_MAX
    } pcode;
    WAIT_STATUS_TYPE waitStatus, lastStatus;
    int nLeft;			/* Number of processes still not reaped */
    char string[200];
    Tcl_DString dString;
    int code;
    CONST char *result;

    lastPid = (unsigned int)-1;
    *((int *)&waitStatus) = 0;
    *((int *)&lastStatus) = 0;

    nLeft = 0;
    for (i = 0; i < bgPtr->nProcs; i++) {
#ifdef WIN32
	pid = WaitProcess(bgPtr->procArr[i], (int *)&waitStatus, WNOHANG);
#else
	pid = waitpid(bgPtr->procArr[i], (int *)&waitStatus, WNOHANG);
#endif
	if (pid == 0) {		/*  Process has not terminated yet */
	    if (nLeft < i) {
		bgPtr->procArr[nLeft] = bgPtr->procArr[i];
	    }
	    nLeft++;		/* Count the number of processes left */
	} else if (pid != -1) {
	    /*
	     * Save the status information associated with the subprocess.
	     * We'll use it only if this is the last subprocess to be reaped.
	     */
	    lastStatus = waitStatus;
	    lastPid = (unsigned int)pid;
	}
    }
    bgPtr->nProcs = nLeft;

    if ((nLeft > 0) || (IsOpenSink(&bgPtr->sink1)) || 
	(IsOpenSink(&bgPtr->sink2))) {
	/* Keep polling for the status of the children that are left */
         if (bgPtr->closeKill>0 && bgPtr->cKilled) {
             if (IsOpenSink(&bgPtr->sink1)) {
                 CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink1);
             }
             if (IsOpenSink(&bgPtr->sink2)) {
                 CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink2);
             }
         }
         AddTimerHandler( bgPtr, bgPtr->interval ) ;
#if WINDEBUG
	PurifyPrintf("schedule TimerProc(nProcs=%d)\n", nLeft);
#endif
	return;
    }

    /*
     * All child processes have completed.  Set the status variable
     * with the status of the last process reaped.  The status is a
     * list of an error token, the exit status, and a message.
     */

    code = WEXITSTATUS(lastStatus);
    Tcl_DStringInit(&dString);
    if (bgPtr->maxExceed) {
        Tcl_DStringAppendElement(&dString, "MAXBYTES");
        pcode = PROCESS_MAX;
    } else if (WIFEXITED(lastStatus)) {
	Tcl_DStringAppendElement(&dString, "EXITED");
	pcode = PROCESS_EXITED;
    } else if (WIFSIGNALED(lastStatus)) {
	Tcl_DStringAppendElement(&dString, "KILLED");
	pcode = PROCESS_KILLED;
	code = -1;
    } else if (WIFSTOPPED(lastStatus)) {
	Tcl_DStringAppendElement(&dString, "STOPPED");
	pcode = PROCESS_STOPPED;
	code = -1;
    } else {
	Tcl_DStringAppendElement(&dString, "UNKNOWN");
	pcode = PROCESS_UNKNOWN;
    }
#ifdef WIN32
    sprintf(string, "%u", lastPid);
    Tcl_DStringAppendElement(&dString, string);
#else
    Tcl_DStringAppendElement(&dString, Blt_Itoa(lastPid));
#endif
    Tcl_DStringAppendElement(&dString, Blt_Itoa(code));
    switch(pcode) {
    case PROCESS_MAX:
	Tcl_DStringAppendElement(&dString, "max bytes exceeded");
	break;
    case PROCESS_EXITED:
        if (bgPtr->cKilled) {
            Tcl_DStringAppendElement(&dString, "kill signal");
        } else {
            Tcl_DStringAppendElement(&dString, "child completed normally");
	}
	break;
    case PROCESS_KILLED:
	Tcl_DStringAppendElement(&dString, 
				Tcl_SignalMsg((int)(WTERMSIG(lastStatus))));
	break;
    case PROCESS_STOPPED:
	Tcl_DStringAppendElement(&dString, 
		 Tcl_SignalMsg((int)(WSTOPSIG(lastStatus))));
	break;
    case PROCESS_UNKNOWN:
	sprintf(string, "child completed with unknown status 0x%x",
	    *((int *)&lastStatus));
	Tcl_DStringAppendElement(&dString, string);
	break;
    }
    if (bgPtr->exitCodePtr != NULL) {
	*bgPtr->exitCodePtr = code;
    }
    DisableTriggers(bgPtr);
    result = Tcl_SetVar(bgPtr->interp, bgPtr->statVar, 
	Tcl_DStringValue(&dString), bgPtr->varFlags | TCL_LEAVE_ERR_MSG);
    SinkCallback(bgPtr->interp, &bgPtr->sink1, Tcl_DStringValue(&dString));
    Tcl_DStringFree(&dString);
    if (result == NULL) {
	Tcl_BackgroundError(bgPtr->interp);
    }
    if (bgPtr->detached) {
	DestroyBackgroundInfo(bgPtr);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * Stdoutproc --
 *
 *	This procedure is called when output from the detached pipeline
 *	is available.  The output is read and saved in a buffer in the
 *	BackgroundInfo structure.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Data is stored in the buffer.  This character array may
 *	be increased as more space is required to contain the output
 *	of the pipeline.
 *
 *----------------------------------------------------------------------
 */
/* ARGSUSED */
static void
StdoutProc(clientData, mask)
    ClientData clientData;	/* File output information. */
    int mask;			/* Not used. */
{
    BackgroundInfo *bgPtr = clientData;

    if (CollectData(bgPtr, &bgPtr->sink1) == TCL_OK) {
	return;
    }
    /*
     * Either EOF or an error has occurred.  In either case, close the
     * sink. Note that closing the sink will also remove the file
     * handler, so this routine will not be called again.
     */
    CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink1);

    /*
     * If both sinks (stdout and stderr) are closed, this doesn't
     * necessarily mean that the process has terminated.  Set up a
     * timer handler to periodically poll for the exit status of each
     * process.  Initially check at the next idle interval.
     */
    if (!IsOpenSink(&bgPtr->sink2)) {
	AddTimerHandler( bgPtr, 0 ) ;
    }
}

/*
 *----------------------------------------------------------------------
 *
 * StderrProc --
 *
 *	This procedure is called when error from the detached pipeline
 *	is available.  The error is read and saved in a buffer in the
 *	BackgroundInfo structure.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Data is stored in the buffer.  This character array may
 *	be increased as more space is required to contain the stderr
 *	of the pipeline.
 *
 *----------------------------------------------------------------------
 */
/* ARGSUSED */
static void
StderrProc(clientData, mask)
    ClientData clientData;	/* File output information. */
    int mask;			/* Not used. */
{
    BackgroundInfo *bgPtr = clientData;
    
    if (CollectData(bgPtr, &bgPtr->sink2) == TCL_OK) {
	return;
    }
    /*
     * Either EOF or an error has occurred.  In either case, close the
     * sink. Note that closing the sink will also remove the file
     * handler, so this routine will not be called again.
     */
    CloseSink(bgPtr, bgPtr->interp, &bgPtr->sink2);

    /*
     * If both sinks (stdout and stderr) are closed, this doesn't
     * necessarily mean that the process has terminated.  Set up a
     * timer handler to periodically poll for the exit status of each
     * process.  Initially check at the next idle interval.
     */
    if (!IsOpenSink(&bgPtr->sink1)) {
	AddTimerHandler( bgPtr, 0 ) ;
    }
}

static void
AddTimerHandler( bgPtr, interval )
    BackgroundInfo *bgPtr;
    int interval;
{
    if(!(bgPtr->timerTokens)){
      bgPtr->timerTokens = Blt_Calloc(EXPECTED_NUM_TIMERS, sizeof(Tcl_TimerToken *));
    }
    if( ++bgPtr->nTimers >= EXPECTED_NUM_TIMERS ){
      bgPtr->timerTokens = Blt_Realloc( bgPtr->timerTokens,
					bgPtr->nTimers * sizeof(Tcl_TimerToken *) ) ;
    }
    bgPtr->timerTokens[bgPtr->nTimers-1] = Tcl_CreateTimerHandler(interval, 
	                                                          TimerProc,
								  bgPtr);
}

static void
InterpDeleted(clientData, interp)
ClientData clientData;	/* Thread-specific data. */
Tcl_Interp *interp;		/* Current interpreter. */
{
    BackgroundInfo *bgPtr = (BackgroundInfo *)clientData;
    DestroyBackgroundInfo(bgPtr);
}

void
MakeLocal( char *ns, char **str) {
    Tcl_DString dStr;
    if (ns == NULL) return;
    if (!strcmp(ns,"::")) return;
    if (!strncmp(*str,"::",2)) return;
    Tcl_DStringInit(&dStr);
    Tcl_DStringAppend(&dStr, ns, -1);
    Tcl_DStringAppend(&dStr, "::", -1);
    Tcl_DStringAppend(&dStr, *str, -1);
    Blt_Free(*str);
    *str = Blt_Strdup( Tcl_DStringValue(&dStr) );
    Tcl_DStringFree(&dStr);
}
/*
 *----------------------------------------------------------------------
 *
 * BgexecCmd --
 *
 *	This procedure is invoked to process the "bgexec" Tcl command.
 *	See the user documentation for details on what it does.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */
/* ARGSUSED */
static int
BgexecCmd(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
    int *outFdPtr, *errFdPtr;
    int nProcs;
    Process *pidPtr;
    char *lastArg;
    BackgroundInfo *bgPtr;
    int i, term = 0, tail = 0;
    int detached;
    Tcl_Encoding encoding;

    if (argc < 3) {
	Tcl_AppendResult(interp, "wrong # args: should be \"", argv[0],
	    " varName ?options? command ?arg...?\"", (char *)NULL);
	return TCL_ERROR;
    }

    /* Check if the command line is to be run detached (the last
     * argument is "&") */
    lastArg = argv[argc - 1];
    detached = ((lastArg[0] == '&') && (lastArg[1] == '\0'));
    if (detached) {
	argc--;
	argv[argc] = NULL;	/* Remove the '&' argument */
    }
    bgPtr = Blt_Calloc(1, sizeof(BackgroundInfo));
    assert(bgPtr);

    /* Initialize the background information record */
    bgPtr->interp = interp;
    bgPtr->signalNum = SIGKILL;
    bgPtr->nProcs = -1;
    bgPtr->interval = 1000;
    bgPtr->detached = detached;
    bgPtr->keepNewline = TRUE;
    bgPtr->statVar = Blt_Strdup(argv[1]);
    bgPtr->timerTokens = NULL;
    bgPtr->nTimers = 0;
    bgPtr->closeKill = 0;

    /* Try to clean up any detached processes */
    Tcl_ReapDetachedProcs();

    for (i=2; i<argc; i += 2) {
        if (!strcmp(argv[i],"--")) {
            term = i+1;
            tail = (argc-i);
            break;
        }
    }
    i = Blt_ProcessSwitches(interp, switchSpecs, argc - 2 - tail, argv + 2, 
	(char *)bgPtr, BLT_SWITCH_ARGV_PARTIAL);
    if (i < 0) {
	FreeBackgroundInfo(bgPtr);
	return TCL_ERROR;
    }
    bgPtr->varFlags = TCL_GLOBAL_ONLY;
    if (bgPtr->localVars) {
        if (detached) {
            bgPtr->varFlags = TCL_NAMESPACE_ONLY;
        } else {
            bgPtr->varFlags = 0;
        }
    }
    if (term) {
        i = term;
    } else {
        i += 2;
    }
    /* Must be at least one argument left as the command to execute. */
    if (argc <= i) {
	Tcl_AppendResult(interp, "missing command to execute: should be \"",
	    argv[0], " varName ?options? command ?arg...?\"", (char *)NULL);
	FreeBackgroundInfo(bgPtr);
	return TCL_ERROR;
    }

    if (bgPtr->localVars && (detached)) {
        char *ns = NULL;
        Tcl_Namespace *nsPtr;
        nsPtr = Tcl_GetCurrentNamespace(interp);
        
        if (nsPtr != NULL) {
            ns = nsPtr->fullName;
        }
        if (bgPtr->statVar) MakeLocal(ns, &bgPtr->statVar);
        if (bgPtr->sink1.doneVar) MakeLocal(ns, &bgPtr->sink1.doneVar);
        if (bgPtr->sink1.command) MakeLocal(ns, &bgPtr->sink1.command);
        if (bgPtr->sink1.updateVar) MakeLocal(ns, &bgPtr->sink1.updateVar);
        if (bgPtr->sink2.doneVar) MakeLocal(ns, &bgPtr->sink2.doneVar);
        if (bgPtr->sink2.command) MakeLocal(ns, &bgPtr->sink2.command);
        if (bgPtr->sink2.updateVar) MakeLocal(ns, &bgPtr->sink2.updateVar);
    }
    
    /* Put a trace on the exit status variable.  The will also allow
     * the user to prematurely terminate the pipeline by simply
     * setting it.  */
    if (Tcl_TraceVar(interp, bgPtr->statVar, TRACE_FLAGS, VariableProc, bgPtr) != TCL_OK) {
        FreeBackgroundInfo(bgPtr);
        return TCL_ERROR;
    }
    bgPtr->traced = TRUE;

    encoding = ENCODING_ASCII;
    if (bgPtr->outputEncodingName != NULL) {
	if (strcmp(bgPtr->outputEncodingName, "binary") == 0) {
	    encoding = ENCODING_BINARY;
	} else {
#if (TCL_VERSION_NUMBER >= _VERSION(8,1,0)) 
	    encoding = Tcl_GetEncoding(interp, bgPtr->outputEncodingName);
	    if (encoding == NULL) {
		goto error;
	    }
#endif
	}
    }
    InitSink(bgPtr, &bgPtr->sink1, "stdout", encoding);
    if (bgPtr->errorEncodingName != NULL) {
	if (strcmp(bgPtr->errorEncodingName, "binary") == 0) {
	    encoding = ENCODING_BINARY;
	} else {
#if (TCL_VERSION_NUMBER >= _VERSION(8,1,0)) 
	    encoding = Tcl_GetEncoding(interp, bgPtr->errorEncodingName);
	    if (encoding == NULL) {
		goto error;
	    }
#endif
	}
    }
    InitSink(bgPtr, &bgPtr->sink2, "stderr", encoding);

    outFdPtr = errFdPtr = (int *)NULL;
#ifdef WIN32
    if ((!bgPtr->detached) || 
	(bgPtr->sink1.doneVar != NULL) || 
	(bgPtr->sink1.command != NULL) || 
	(bgPtr->sink1.updateVar != NULL) || 
	(bgPtr->sink1.updateCmd != NULL)) {
	outFdPtr = &bgPtr->sink1.fd;
    }
#else
    outFdPtr = &bgPtr->sink1.fd;
#endif
    if ((bgPtr->sink2.doneVar != NULL) || 
	(bgPtr->sink2.updateVar != NULL) ||
	(bgPtr->sink2.updateCmd != NULL) || 
	(bgPtr->sink2.echo)) {
	errFdPtr = &bgPtr->sink2.fd;
    }
    nProcs = Blt_CreatePipeline(interp, argc - i, argv + i, &pidPtr,
	(int *)NULL, outFdPtr, errFdPtr);
    if (nProcs < 0) {
	goto error;
    }
    bgPtr->procArr = pidPtr;
    bgPtr->nProcs = nProcs;

    if (bgPtr->sink1.fd == -1) {

	/* If output has been redirected, start polling immediately
	 * for the exit status of each process.  Normally, this is
	 * done only after stdout has been closed by the last process,
	 * but here stdout has been redirected. The default polling
	 * interval is every 1 second.  */

	AddTimerHandler( bgPtr, bgPtr->interval ) ;

    } else if (CreateSinkHandler(bgPtr, &bgPtr->sink1, StdoutProc) != TCL_OK) {
	goto error;
    }
    if ((bgPtr->sink2.fd != -1) &&
	(CreateSinkHandler(bgPtr, &bgPtr->sink2, StderrProc) != TCL_OK)) {
 	goto error;
    }
    Tcl_CallWhenDeleted(interp, InterpDeleted, (ClientData)bgPtr);
    if (bgPtr->detached) {	
	char string[200];

	/* If detached, return a list of the child process ids instead
	 * of the output of the pipeline. */
	for (i = 0; i < nProcs; i++) {
#ifdef WIN32
	    sprintf(string, "%u", (unsigned int)bgPtr->procArr[i].pid);
#else 
	    sprintf(string, "%d", bgPtr->procArr[i]);
#endif
	    Tcl_AppendElement(interp, string);
	}
    } else {
	int exitCode;
	int done, doRaise;

	bgPtr->exitCodePtr = &exitCode;
	bgPtr->donePtr = &done;

	exitCode = done = 0;
	while (!done) {
	    Tcl_DoOneEvent(0);
	}
	DisableTriggers(bgPtr);
         if ((exitCode == 0 || bgPtr->doRaise==0) && (bgPtr->sink1.doneVar == NULL) && (bgPtr->sink1.command == NULL)) {
	    unsigned char *data;
	    size_t length;

	    /* Return the output of the pipeline. */
	    GetSinkData(&bgPtr->sink1, &data, &length);
#if (TCL_VERSION_NUMBER <  _VERSION(8,1,0)) 
	    data[length] = '\0';
	    Tcl_SetResult(interp, data, TCL_VOLATILE);
#else
	    Tcl_SetObjResult(interp, Tcl_NewByteArrayObj(data, length));
#endif
	}
	/* Clean up resources used. */
        doRaise = bgPtr->doRaise;
	DestroyBackgroundInfo(bgPtr);
	if ((doRaise) && exitCode != 0) {
	    Tcl_AppendResult(interp, "child process exited abnormally",
		(char *)NULL);
	    return TCL_ERROR;
	}
    }
    return TCL_OK;
  error:
    DisableTriggers(bgPtr);
    DestroyBackgroundInfo(bgPtr);
    return TCL_ERROR;
}


static int
KillCmd(clientData, interp, argc, argv)
    ClientData clientData;	/* Thread-specific data. */
    Tcl_Interp *interp;		/* Current interpreter. */
    int argc;			/* Number of arguments. */
    char **argv;		/* Argument strings. */
{
     char *string;
     int signalNum = SIGKILL;
     int pid;
#ifdef WIN32
     HANDLE h;
#endif

     if (argc>3) {
         Tcl_AppendResult(interp, "too many args", 0);
         return TCL_ERROR;
     }
     if (argc<2) {
         Tcl_AppendResult(interp, "pid required", 0);
         return TCL_ERROR;
     }
     if (Tcl_GetInt(interp, argv[1], &pid) != TCL_OK) {
         return TCL_ERROR;
     }

#ifdef WIN32
     if (argc!=2) {
         Tcl_AppendResult(interp, "windows does not support signo", 0);
         return TCL_ERROR;
     }
     if (!(h = OpenProcess (PROCESS_TERMINATE, 0, pid))) {
	     Tcl_AppendResult (interp, "invalid pid or access", NULL);
	     return TCL_ERROR;
     }

     if (TerminateProcess (h, 1) == 0) {
	     Tcl_AppendResult (interp, "TerminateProcess failure", NULL);
	     return TCL_ERROR;
     }

     CloseHandle (h);
#else
     if (argc==3) {
        string = argv[2];
        if (isdigit(UCHAR(string[0]))) {
	    if (Tcl_GetInt(interp, string, &signalNum) != TCL_OK) {
 	       return TCL_ERROR;
	   }
        } else {
	    char *name;
	    register SignalId *sigPtr;

	    name = string;

	    /*  Clip off any "SIG" prefix from the signal name */
	    if ((name[0] == 'S') && (name[1] == 'I') && (name[2] == 'G')) {
	        name += 3;
	    }
	    signalNum = -1;
	    for (sigPtr = signalIds; sigPtr->number > 0; sigPtr++) {
	        if (strcmp(sigPtr->name + 3, name) == 0) {
		    signalNum = sigPtr->number;
		    break;
	        }
	    }
	    if (signalNum < 0) {
	        Tcl_AppendResult(interp, "unknown signal \"", string, "\"",
		    (char *)NULL);
	        return TCL_ERROR;
	    }
        }
        if ((signalNum < 0) || (signalNum > NSIG)) {
	    /* Outside range of signals */
	    Tcl_AppendResult(interp, "signal number \"", string,
	        "\" is out of range", (char *)NULL);
	    return TCL_ERROR;
        }
     }
     kill(pid, signalNum);
#endif
     return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * Blt_BgexecInit --
 *
 *	This procedure is invoked to initialize the "bgexec" Tcl
 *	command.  See the user documentation for details on what it
 *	does.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	See the user documentation.
 *
 *----------------------------------------------------------------------
 */
int
Blt_BgexecInit(interp)
    Tcl_Interp *interp;
{
    static Blt_CmdSpec cmdSpec = {"bgexec", BgexecCmd, };
    static Blt_CmdSpec cmdSpec2 = {"kill", KillCmd, };

    if (Blt_InitCmd(interp, "blt", &cmdSpec) == NULL) {
	return TCL_ERROR;
    }
    if (Blt_InitCmd(interp, "blt", &cmdSpec2) == NULL) {
	return TCL_ERROR;
    }
    return TCL_OK;
}
#endif /* NO_BGEXEC */