1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* tclWinPipe.c --
*
* This file implements the Windows-specific exec pipeline functions,
* the "pipe" channel driver, and the "pid" Tcl command.
*
* Copyright (c) 1996-1997 by Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclWinPipe.c,v 1.35.2.5 2004/09/08 23:03:30 dgp Exp $
*/
#include "tclWinInt.h"
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/*
* tclWinPipe.c --
*
* This file implements the Windows-specific exec pipeline functions,
* the "pipe" channel driver, and the "pid" Tcl command.
*
* Copyright (c) 1996-1997 by Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* RCS: @(#) $Id: tclWinPipe.c,v 1.35.2.6 2004/10/28 18:47:40 dgp Exp $
*/
#include "tclWinInt.h"
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
|
| ︙ | | | ︙ | |
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
|
char *start,*end;
Tcl_DString pipeDll;
Tcl_DStringInit(&pipeDll);
Tcl_DStringAppend(&pipeDll, TCL_PIPE_DLL, -1);
tclExePtr = Tcl_NewStringObj(TclpFindExecutable(""), -1);
start = Tcl_GetStringFromObj(tclExePtr, &i);
for (end = start + (i-1); end > start; end--) {
if (*end == '/')
break;
}
if (*end != '/') {
Tcl_Panic("no / in executable path name");
}
i = (end - start) + 1;
pipeDllPtr = Tcl_NewStringObj(start, i);
Tcl_AppendToObj(pipeDllPtr, Tcl_DStringValue(&pipeDll), -1);
|
|
>
|
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
|
char *start,*end;
Tcl_DString pipeDll;
Tcl_DStringInit(&pipeDll);
Tcl_DStringAppend(&pipeDll, TCL_PIPE_DLL, -1);
tclExePtr = Tcl_NewStringObj(TclpFindExecutable(""), -1);
start = Tcl_GetStringFromObj(tclExePtr, &i);
for (end = start + (i-1); end > start; end--) {
if (*end == '/') {
break;
}
}
if (*end != '/') {
Tcl_Panic("no / in executable path name");
}
i = (end - start) + 1;
pipeDllPtr = Tcl_NewStringObj(start, i);
Tcl_AppendToObj(pipeDllPtr, Tcl_DStringValue(&pipeDll), -1);
|
| ︙ | | | ︙ | |
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
|
/*
* Prime the path. Add a space separator if we were primed with
* something.
*/
Tcl_DStringAppend(&ds, Tcl_DStringValue(linePtr), -1);
if (Tcl_DStringLength(linePtr) > 0) Tcl_DStringAppend(&ds, " ", 1);
for (i = 0; i < argc; i++) {
if (i == 0) {
arg = executable;
} else {
arg = argv[i];
Tcl_DStringAppend(&ds, " ", 1);
}
quote = 0;
if (arg[0] == '\0') {
quote = 1;
} else {
int count;
Tcl_UniChar ch;
for (start = arg; *start != '\0'; start += count) {
count = Tcl_UtfToUniChar(start, &ch);
if (Tcl_UniCharIsSpace(ch)) { /* INTL: ISO space. */
quote = 1;
break;
}
}
}
if (quote) {
Tcl_DStringAppend(&ds, "\"", 1);
}
start = arg;
for (special = arg; ; ) {
if ((*special == '\\') &&
(special[1] == '\\' || special[1] == '"' || (quote && special[1] == '\0'))) {
Tcl_DStringAppend(&ds, start, (int) (special - start));
start = special;
while (1) {
special++;
if (*special == '"' || (quote && *special == '\0')) {
/*
* N backslashes followed a quote -> insert
|
|
>
>
|
|
|
|
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
|
/*
* Prime the path. Add a space separator if we were primed with
* something.
*/
Tcl_DStringAppend(&ds, Tcl_DStringValue(linePtr), -1);
if (Tcl_DStringLength(linePtr) > 0) {
Tcl_DStringAppend(&ds, " ", 1);
}
for (i = 0; i < argc; i++) {
if (i == 0) {
arg = executable;
} else {
arg = argv[i];
Tcl_DStringAppend(&ds, " ", 1);
}
quote = 0;
if (arg[0] == '\0') {
quote = 1;
} else {
int count;
Tcl_UniChar ch;
for (start = arg; *start != '\0'; start += count) {
count = Tcl_UtfToUniChar(start, &ch);
if (Tcl_UniCharIsSpace(ch)) { /* INTL: ISO space. */
quote = 1;
break;
}
}
}
if (quote) {
Tcl_DStringAppend(&ds, "\"", 1);
}
start = arg;
for (special = arg; ; ) {
if ((*special == '\\') && (special[1] == '\\' ||
special[1] == '"' || (quote && special[1] == '\0'))) {
Tcl_DStringAppend(&ds, start, (int) (special - start));
start = special;
while (1) {
special++;
if (*special == '"' || (quote && *special == '\0')) {
/*
* N backslashes followed a quote -> insert
|
| ︙ | | | ︙ | |
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
|
Tcl_Channel errChan;
int errorCode, result;
PipeInfo *infoPtr, **nextPtrPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
DWORD exitCode;
errorCode = 0;
if ((!flags || (flags == TCL_CLOSE_READ))
&& (pipePtr->readFile != NULL)) {
/*
* Clean up the background thread if necessary. Note that this
* must be done before we can close the file, since the
* thread may be blocking trying to read from the pipe.
*/
if (pipePtr->readThread) {
|
<
|
|
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
|
Tcl_Channel errChan;
int errorCode, result;
PipeInfo *infoPtr, **nextPtrPtr;
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
DWORD exitCode;
errorCode = 0;
if ((!flags || flags == TCL_CLOSE_READ) && (pipePtr->readFile != NULL)) {
/*
* Clean up the background thread if necessary. Note that this
* must be done before we can close the file, since the
* thread may be blocking trying to read from the pipe.
*/
if (pipePtr->readThread) {
|
| ︙ | | | ︙ | |
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
|
SetEvent(pipePtr->stopReader);
/*
* Wait at most 20 milliseconds for the reader thread to close.
*/
if (WaitForSingleObject(pipePtr->readThread, 20)
== WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* become readable in ReadFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the reader
* thread to fall out of ReadFile with a FALSE. (below) is
* not the correct way to do this, but will stay here until
|
|
|
|
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
|
SetEvent(pipePtr->stopReader);
/*
* Wait at most 20 milliseconds for the reader thread to close.
*/
if (WaitForSingleObject(pipePtr->readThread,
20) == WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* become readable in ReadFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the reader
* thread to fall out of ReadFile with a FALSE. (below) is
* not the correct way to do this, but will stay here until
|
| ︙ | | | ︙ | |
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
|
}
if (TclpCloseFile(pipePtr->readFile) != 0) {
errorCode = errno;
}
pipePtr->validMask &= ~TCL_READABLE;
pipePtr->readFile = NULL;
}
if ((!flags || (flags & TCL_CLOSE_WRITE))
&& (pipePtr->writeFile != NULL)) {
if (pipePtr->writeThread) {
/*
* Wait for the writer thread to finish the current buffer,
* then terminate the thread and close the handles. If the
* channel is nonblocking, there should be no pending write
* operations.
*/
|
<
|
<
|
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
|
}
if (TclpCloseFile(pipePtr->readFile) != 0) {
errorCode = errno;
}
pipePtr->validMask &= ~TCL_READABLE;
pipePtr->readFile = NULL;
}
if ((!flags || flags & TCL_CLOSE_WRITE) && (pipePtr->writeFile != NULL)) {
if (pipePtr->writeThread) {
/*
* Wait for the writer thread to finish the current buffer,
* then terminate the thread and close the handles. If the
* channel is nonblocking, there should be no pending write
* operations.
*/
|
| ︙ | | | ︙ | |
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
|
SetEvent(pipePtr->stopWriter);
/*
* Wait at most 20 milliseconds for the reader thread to close.
*/
if (WaitForSingleObject(pipePtr->writeThread, 20)
== WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* consume input in WriteFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the writer
* thread to fall out of WriteFile with a FALSE. (below) is
* not the correct way to do this, but will stay here until
|
|
|
|
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
|
SetEvent(pipePtr->stopWriter);
/*
* Wait at most 20 milliseconds for the reader thread to close.
*/
if (WaitForSingleObject(pipePtr->writeThread,
20) == WAIT_TIMEOUT) {
/*
* The thread must be blocked waiting for the pipe to
* consume input in WriteFile(). There isn't a clean way
* to exit the thread from this condition. We should
* terminate the child process instead to get the writer
* thread to fall out of WriteFile with a FALSE. (below) is
* not the correct way to do this, but will stay here until
|
| ︙ | | | ︙ | |
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
|
mask = 0;
if ((infoPtr->watchMask & TCL_WRITABLE) &&
(WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT)) {
mask = TCL_WRITABLE;
}
filePtr = (WinFile*) ((PipeInfo*)infoPtr)->readFile;
if ((infoPtr->watchMask & TCL_READABLE) &&
(WaitForRead(infoPtr, 0) >= 0)) {
if (infoPtr->readFlags & PIPE_EOF) {
mask = TCL_READABLE;
} else {
mask |= TCL_READABLE;
}
}
|
|
<
|
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
|
mask = 0;
if ((infoPtr->watchMask & TCL_WRITABLE) &&
(WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT)) {
mask = TCL_WRITABLE;
}
filePtr = (WinFile*) ((PipeInfo*)infoPtr)->readFile;
if ((infoPtr->watchMask & TCL_READABLE) && (WaitForRead(infoPtr,0) >= 0)) {
if (infoPtr->readFlags & PIPE_EOF) {
mask = TCL_READABLE;
} else {
mask |= TCL_READABLE;
}
}
|
| ︙ | | | ︙ | |
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
|
char buf[TCL_INTEGER_SPACE];
if (objc > 2) {
Tcl_WrongNumArgs(interp, 1, objv, "?channelId?");
return TCL_ERROR;
}
if (objc == 1) {
resultPtr = Tcl_GetObjResult(interp);
wsprintfA(buf, "%lu", (unsigned long) getpid());
Tcl_SetStringObj(resultPtr, buf, -1);
} else {
chan = Tcl_GetChannel(interp, Tcl_GetStringFromObj(objv[1], NULL),
NULL);
if (chan == (Tcl_Channel) NULL) {
return TCL_ERROR;
}
chanTypePtr = Tcl_GetChannelType(chan);
if (chanTypePtr != &pipeChannelType) {
return TCL_OK;
}
pipePtr = (PipeInfo *) Tcl_GetChannelInstanceData(chan);
resultPtr = Tcl_GetObjResult(interp);
for (i = 0; i < pipePtr->numPids; i++) {
wsprintfA(buf, "%lu", TclpGetPid(pipePtr->pidPtr[i]));
Tcl_ListObjAppendElement(/*interp*/ NULL, resultPtr,
Tcl_NewStringObj(buf, -1));
}
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
|
<
|
|
>
|
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
|
char buf[TCL_INTEGER_SPACE];
if (objc > 2) {
Tcl_WrongNumArgs(interp, 1, objv, "?channelId?");
return TCL_ERROR;
}
if (objc == 1) {
wsprintfA(buf, "%lu", (unsigned long) getpid());
Tcl_SetObjResult(interp, Tcl_NewStringObj(buf, -1));
} else {
chan = Tcl_GetChannel(interp, Tcl_GetStringFromObj(objv[1], NULL),
NULL);
if (chan == (Tcl_Channel) NULL) {
return TCL_ERROR;
}
chanTypePtr = Tcl_GetChannelType(chan);
if (chanTypePtr != &pipeChannelType) {
return TCL_OK;
}
pipePtr = (PipeInfo *) Tcl_GetChannelInstanceData(chan);
resultPtr = Tcl_NewObj();
for (i = 0; i < pipePtr->numPids; i++) {
wsprintfA(buf, "%lu", TclpGetPid(pipePtr->pidPtr[i]));
Tcl_ListObjAppendElement(/*interp*/ NULL, resultPtr,
Tcl_NewStringObj(buf, -1));
}
Tcl_SetObjResult(interp, resultPtr);
}
return TCL_OK;
}
/*
*----------------------------------------------------------------------
*
|
| ︙ | | | ︙ | |
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
|
/*
* Try waiting for 0 bytes. This will block until some data is
* available on NT, but will return immediately on Win 95. So,
* if no data is available after the first read, we block until
* we can read a single byte off of the pipe.
*/
if ((ReadFile(handle, NULL, 0, &count, NULL) == FALSE)
|| (PeekNamedPipe(handle, NULL, 0, NULL, &count,
NULL) == FALSE)) {
/*
* The error is a result of an EOF condition, so set the
* EOF bit before signalling the main thread.
*/
err = GetLastError();
if (err == ERROR_BROKEN_PIPE) {
|
|
|
<
|
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
|
/*
* Try waiting for 0 bytes. This will block until some data is
* available on NT, but will return immediately on Win 95. So,
* if no data is available after the first read, we block until
* we can read a single byte off of the pipe.
*/
if (ReadFile(handle, NULL, 0, &count, NULL) == FALSE ||
PeekNamedPipe(handle, NULL, 0, NULL, &count, NULL) == FALSE) {
/*
* The error is a result of an EOF condition, so set the
* EOF bit before signalling the main thread.
*/
err = GetLastError();
if (err == ERROR_BROKEN_PIPE) {
|
| ︙ | | | ︙ | |