1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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.8 1999/07/31 01:24:25 redman Exp $
* RCS: @(#) $Id: tclWinPipe.c,v 1.9 1999/12/09 14:44:11 hobbs Exp $
*/
#include "tclWinInt.h"
#include <dos.h>
#include <fcntl.h>
#include <io.h>
|
| ︙ | | |
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
|
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
|
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
#define APPL_DOS 1
#define APPL_WIN3X 2
#define APPL_WIN32 3
/*
* The following constants and structures are used to encapsulate the state
* of various types of files used in a pipeline.
* This used to have a 1 && 2 that supported Win32s.
*/
#define WIN32S_PIPE 1 /* Win32s emulated pipe. */
#define WIN32S_TMPFILE 2 /* Win32s emulated temporary file. */
#define WIN_FILE 3 /* Basic Win32 file. */
/*
* This structure encapsulates the common state associated with all file
* types used in a pipeline.
*/
typedef struct WinFile {
int type; /* One of the file types defined above. */
HANDLE handle; /* Open file handle. */
} WinFile;
/*
* The following structure is used to keep track of temporary files under
* Win32s and delete the disk file when the open handle is closed.
* The type field will be WIN32S_TMPFILE.
*/
typedef struct TmpFile {
WinFile file; /* Common part. */
char name[MAX_PATH]; /* Name of temp file. */
} TmpFile;
/*
* The following structure represents a synchronous pipe under Win32s.
* The type field will be WIN32S_PIPE. The handle field will refer to
* an open file when Tcl is reading from the "pipe", otherwise it is
* INVALID_HANDLE_VALUE.
*/
typedef struct WinPipe {
WinFile file; /* Common part. */
struct WinPipe *otherPtr; /* Pointer to the WinPipe structure that
* corresponds to the other end of this
* pipe. */
char *fileName; /* The name of the staging file that gets
* the data written to this pipe. Malloc'd.
* and shared by both ends of the pipe. Only
* when both ends are freed will fileName be
* freed and the file it refers to deleted. */
} WinPipe;
/*
* This list is used to map from pids to process handles.
*/
typedef struct ProcInfo {
HANDLE hProcess;
DWORD dwProcessId;
|
| ︙ | | |
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
-
+
|
#define PIPE_ASYNC (1<<1) /* Channel is non-blocking. */
/*
* Bit masks used in the sharedFlags field of the PipeInfo structure below.
*/
#define PIPE_EOF (1<<2) /* Pipe has reached EOF. */
#define PIPE_EXTRABYTE (1<<3) /* The reader thread has consumed one byte. */
#define PIPE_EXTRABYTE (1<<3) /* The reader thread has consumed one byte. */
/*
* This structure describes per-instance data for a pipe based channel.
*/
typedef struct PipeInfo {
struct PipeInfo *nextPtr; /* Pointer to next registered pipe. */
|
| ︙ | | |
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
-
-
|
* Declarations for functions used only in this file.
*/
static int ApplicationType(Tcl_Interp *interp,
const char *fileName, char *fullName);
static void BuildCommandLine(const char *executable, int argc,
char **argv, Tcl_DString *linePtr);
static void CopyChannel(HANDLE dst, HANDLE src);
static BOOL HasConsole(void);
static char * MakeTempFile(Tcl_DString *namePtr);
static int PipeBlockModeProc(ClientData instanceData, int mode);
static void PipeCheckProc(ClientData clientData, int flags);
static int PipeClose2Proc(ClientData instanceData,
Tcl_Interp *interp, int flags);
static int PipeEventProc(Tcl_Event *evPtr, int flags);
static void PipeExitHandler(ClientData clientData);
static int PipeGetHandleProc(ClientData instanceData,
|
| ︙ | | |
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
|
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
-
-
+
-
-
-
+
|
* Look to see if any events are already pending. If they are, poll.
*/
for (infoPtr = tsdPtr->firstPipePtr; infoPtr != NULL;
infoPtr = infoPtr->nextPtr) {
if (infoPtr->watchMask & TCL_WRITABLE) {
filePtr = (WinFile*) infoPtr->writeFile;
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForSingleObject(infoPtr->writable, 0)
if (WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT) {
!= WAIT_TIMEOUT)) {
block = 0;
}
}
if (infoPtr->watchMask & TCL_READABLE) {
filePtr = (WinFile*) infoPtr->readFile;
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForRead(infoPtr, 0) >= 0)) {
if (WaitForRead(infoPtr, 0) >= 0) {
block = 0;
}
}
}
if (!block) {
Tcl_SetMaxBlockTime(&blockTime);
}
|
| ︙ | | |
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
|
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
-
+
-
-
+
-
-
+
-
-
+
-
-
-
+
+
-
|
/*
* Queue an event if the pipe is signaled for reading or writing.
*/
needEvent = 0;
filePtr = (WinFile*) infoPtr->writeFile;
if (infoPtr->watchMask & TCL_WRITABLE) {
if ((infoPtr->watchMask & TCL_WRITABLE) &&
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForSingleObject(infoPtr->writable, 0)
(WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT)) {
!= WAIT_TIMEOUT)) {
needEvent = 1;
needEvent = 1;
}
}
filePtr = (WinFile*) infoPtr->readFile;
if (infoPtr->watchMask & TCL_READABLE) {
if ((infoPtr->watchMask & TCL_READABLE) &&
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForRead(infoPtr, 0) >= 0)) {
needEvent = 1;
(WaitForRead(infoPtr, 0) >= 0)) {
needEvent = 1;
}
}
if (needEvent) {
infoPtr->flags |= PIPE_PENDING;
evPtr = (PipeEvent *) ckalloc(sizeof(PipeEvent));
evPtr->header.proc = PipeEventProc;
evPtr->infoPtr = infoPtr;
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
+
-
-
+
-
|
}
}
if (SetFilePointer(handle, 0, NULL, FILE_BEGIN) == 0xFFFFFFFF) {
goto error;
}
}
/*
* Under Win32s a file created with FILE_FLAG_DELETE_ON_CLOSE won't
* actually be deleted when it is closed, so we have to do it ourselves.
*/
if (TclWinGetPlatformId() == VER_PLATFORM_WIN32s) {
TmpFile *tmpFilePtr = (TmpFile *) ckalloc(sizeof(TmpFile));
tmpFilePtr->file.type = WIN32S_TMPFILE;
tmpFilePtr->file.handle = handle;
lstrcpyA(tmpFilePtr->name, (char *) name);
return (TclFile) tmpFilePtr;
} else {
return TclWinMakeFile(handle);
return TclWinMakeFile(handle);
}
error:
TclWinConvertError(GetLastError());
CloseHandle(handle);
(*tclWinProcs->deleteFileProc)((TCHAR *) name);
return NULL;
}
/*
*----------------------------------------------------------------------
*
* TclpCreatePipe --
*
* Creates an anonymous pipe. Under Win32s, creates a temp file
* Creates an anonymous pipe.
* that is used to simulate a pipe.
*
* Results:
* Returns 1 on success, 0 on failure.
*
* Side effects:
* Creates a pipe.
*
|
| ︙ | | |
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
|
772
773
774
775
776
777
778
779
780
781
782
783
784
785
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
{
HANDLE readHandle, writeHandle;
if (CreatePipe(&readHandle, &writeHandle, NULL, 0) != 0) {
*readPipe = TclWinMakeFile(readHandle);
*writePipe = TclWinMakeFile(writeHandle);
return 1;
}
if (TclWinGetPlatformId() == VER_PLATFORM_WIN32s) {
WinPipe *readPipePtr, *writePipePtr;
char buf[MAX_PATH];
int bytes;
if (TempFileName((WCHAR *) buf) != 0) {
bytes = strlen((char *) buf) + 1;
readPipePtr = (WinPipe *) ckalloc(sizeof(WinPipe));
writePipePtr = (WinPipe *) ckalloc(sizeof(WinPipe));
readPipePtr->file.type = WIN32S_PIPE;
readPipePtr->otherPtr = writePipePtr;
readPipePtr->fileName = (char *) ckalloc(bytes);
lstrcpyA(readPipePtr->fileName, buf);
readPipePtr->file.handle = INVALID_HANDLE_VALUE;
writePipePtr->file.type = WIN32S_PIPE;
writePipePtr->otherPtr = readPipePtr;
writePipePtr->fileName = readPipePtr->fileName;
writePipePtr->file.handle = INVALID_HANDLE_VALUE;
*readPipe = (TclFile) readPipePtr;
*writePipe = (TclFile) writePipePtr;
return 1;
}
}
TclWinConvertError(GetLastError());
return 0;
}
/*
|
| ︙ | | |
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
|
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
|
-
-
-
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
*/
int
TclpCloseFile(
TclFile file) /* The file to close. */
{
WinFile *filePtr = (WinFile *) file;
WinPipe *pipePtr;
switch (filePtr->type) {
case WIN_FILE:
case WIN32S_TMPFILE:
/*
* Don't close the Win32 handle if the handle is a standard channel
* during the exit process. Otherwise, one thread may kill the stdio
* of another.
* during the exit process. Otherwise, one thread may kill the
* stdio of another.
*/
if (!TclInExit()
|| ((GetStdHandle(STD_INPUT_HANDLE) != filePtr->handle)
&& (GetStdHandle(STD_OUTPUT_HANDLE) != filePtr->handle)
&& (GetStdHandle(STD_ERROR_HANDLE) != filePtr->handle))) {
if (CloseHandle(filePtr->handle) == FALSE) {
TclWinConvertError(GetLastError());
ckfree((char *) filePtr);
return -1;
}
}
/*
* Simulate deleting the file on close for Win32s.
*/
if (filePtr->type == WIN32S_TMPFILE) {
DeleteFileA(((TmpFile *) filePtr)->name);
}
break;
case WIN32S_PIPE:
pipePtr = (WinPipe *) file;
if (pipePtr->otherPtr != NULL) {
pipePtr->otherPtr->otherPtr = NULL;
} else {
if (pipePtr->file.handle != INVALID_HANDLE_VALUE) {
CloseHandle(pipePtr->file.handle);
}
DeleteFileA(pipePtr->fileName);
ckfree((char *) pipePtr->fileName);
}
break;
default:
panic("TclpCloseFile: unexpected file type");
}
ckfree((char *) filePtr);
|
| ︙ | | |
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
|
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
|
-
-
+
+
-
|
/*
*----------------------------------------------------------------------
*
* TclpCreateProcess --
*
* Create a child process that has the specified files as its
* standard input, output, and error. The child process runs
* synchronously under Win32s and asynchronously under Windows NT
* and Windows 95, and runs with the same environment variables
* asynchronously under Windows NT and Windows 9x, and runs
* with the same environment variables as the creating process.
* as the creating process.
*
* The complete Windows search path is searched to find the specified
* executable. If an executable by the given name is not found,
* automatically tries appending ".com", ".exe", and ".bat" to the
* executable name.
*
* Results:
|
| ︙ | | |
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
|
940
941
942
943
944
945
946
947
948
949
950
951
952
953
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
applType = ApplicationType(interp, argv[0], execPath);
if (applType == APPL_NONE) {
return TCL_ERROR;
}
result = TCL_ERROR;
Tcl_DStringInit(&cmdLine);
if (TclWinGetPlatformId() == VER_PLATFORM_WIN32s) {
/*
* Under Win32s, there are no pipes. In order to simulate pipe
* behavior, the child processes are run synchronously and their
* I/O is redirected from/to temporary files before the next
* stage of the pipeline is started.
*/
MSG msg;
DWORD status;
DWORD args[4];
void *trans[5];
char *inputFileName, *outputFileName;
Tcl_DString inputTempFile, outputTempFile;
BuildCommandLine(execPath, argc, argv, &cmdLine);
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
Tcl_DStringInit(&inputTempFile);
Tcl_DStringInit(&outputTempFile);
outputHandle = INVALID_HANDLE_VALUE;
inputFileName = NULL;
outputFileName = NULL;
if (inputFile != NULL) {
filePtr = (WinFile *) inputFile;
switch (filePtr->type) {
case WIN_FILE:
case WIN32S_TMPFILE: {
h = INVALID_HANDLE_VALUE;
inputFileName = MakeTempFile(&inputTempFile);
if (inputFileName != NULL) {
h = CreateFileA((char *) inputFileName,
GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0,
NULL);
}
if (h == INVALID_HANDLE_VALUE) {
Tcl_AppendResult(interp, "couldn't duplicate input handle: ",
Tcl_PosixError(interp), (char *) NULL);
goto end32s;
}
CopyChannel(h, filePtr->handle);
CloseHandle(h);
break;
}
case WIN32S_PIPE: {
inputFileName = (char *) ((WinPipe *) inputFile)->fileName;
break;
}
}
}
if (inputFileName == NULL) {
inputFileName = "nul";
}
if (outputFile != NULL) {
filePtr = (WinFile *) outputFile;
if (filePtr->type == WIN_FILE) {
outputFileName = MakeTempFile(&outputTempFile);
if (outputFileName == NULL) {
Tcl_AppendResult(interp, "couldn't duplicate output handle: ",
Tcl_PosixError(interp), (char *) NULL);
goto end32s;
}
outputHandle = filePtr->handle;
} else if (filePtr->type == WIN32S_PIPE) {
outputFileName = (char *) ((WinPipe *) outputFile)->fileName;
}
}
if (outputFileName == NULL) {
outputFileName = "nul";
}
if (applType == APPL_DOS) {
args[0] = (DWORD) Tcl_DStringValue(&cmdLine);
args[1] = (DWORD) inputFileName;
args[2] = (DWORD) outputFileName;
trans[0] = &args[0];
trans[1] = &args[1];
trans[2] = &args[2];
trans[3] = NULL;
if (TclWinSynchSpawn(args, 0, trans, pidPtr) != 0) {
result = TCL_OK;
}
} else if (applType == APPL_WIN3X) {
args[0] = (DWORD) Tcl_DStringValue(&cmdLine);
trans[0] = &args[0];
trans[1] = NULL;
if (TclWinSynchSpawn(args, 1, trans, pidPtr) != 0) {
result = TCL_OK;
}
} else {
if (CreateProcessA(NULL, Tcl_DStringValue(&cmdLine),
NULL, NULL, FALSE, DETACHED_PROCESS, NULL, NULL,
&startInfo, &procInfo) != 0) {
CloseHandle(procInfo.hThread);
while (1) {
if (GetExitCodeProcess(procInfo.hProcess, &status) == FALSE) {
break;
}
if (status != STILL_ACTIVE) {
break;
}
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
*pidPtr = (Tcl_Pid) procInfo.hProcess;
if (*pidPtr != 0) {
TclWinAddProcess(procInfo.hProcess, procInfo.dwProcessId);
}
result = TCL_OK;
}
}
if (result != TCL_OK) {
TclWinConvertError(GetLastError());
Tcl_AppendResult(interp, "couldn't execute \"", argv[0],
"\": ", Tcl_PosixError(interp), (char *) NULL);
}
end32s:
if (outputHandle != INVALID_HANDLE_VALUE) {
/*
* Now copy stuff from temp file to actual output handle. Don't
* close outputHandle because it is associated with the output
* file owned by the caller.
*/
h = CreateFileA(outputFileName, GENERIC_READ, 0, NULL, OPEN_ALWAYS,
0, NULL);
if (h != INVALID_HANDLE_VALUE) {
CopyChannel(outputHandle, h);
}
CloseHandle(h);
}
if (inputFileName == Tcl_DStringValue(&inputTempFile)) {
DeleteFileA(inputFileName);
}
if (outputFileName == Tcl_DStringValue(&outputTempFile)) {
DeleteFileA(outputFileName);
}
Tcl_DStringFree(&inputTempFile);
Tcl_DStringFree(&outputTempFile);
Tcl_DStringFree(&cmdLine);
return result;
}
hProcess = GetCurrentProcess();
/*
* STARTF_USESTDHANDLES must be used to pass handles to child process.
* Using SetStdHandle() and/or dup2() only works when a console mode
* parent process is spawning an attached console mode child process.
*/
|
| ︙ | | |
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
|
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
|
Tcl_WinUtfToTChar(Tcl_DStringValue(&ds), Tcl_DStringLength(&ds), linePtr);
Tcl_DStringFree(&ds);
}
/*
*----------------------------------------------------------------------
*
* MakeTempFile --
*
* Helper function for TclpCreateProcess under Win32s. Makes a
* temporary file that _won't_ go away automatically when it's file
* handle is closed. Used for simulated pipes, which are written
* in one pass and reopened and read in the next pass.
*
* Results:
* namePtr is filled with the name of the temporary file.
*
* Side effects:
* A temporary file with the name specified by namePtr is created.
* The caller is responsible for deleting this temporary file.
*
*----------------------------------------------------------------------
*/
static char *
MakeTempFile(namePtr)
Tcl_DString *namePtr; /* Initialized Tcl_DString that is filled
* with the name of the temporary file that
* was created. */
{
char name[MAX_PATH];
if (TempFileName((WCHAR *) name) == 0) {
return NULL;
}
Tcl_DStringAppend(namePtr, name, -1);
return Tcl_DStringValue(namePtr);
}
/*
*----------------------------------------------------------------------
*
* CopyChannel --
*
* Helper function used by TclpCreateProcess under Win32s. Copies
* what remains of source file to destination file; source file
* pointer need not be positioned at the beginning of the file if
* all of source file is not desired, but data is copied up to end
* of source file.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
CopyChannel(
HANDLE dst, /* Destination file. */
HANDLE src) /* Source file. */
{
char buf[8192];
DWORD dwRead, dwWrite;
while (ReadFile(src, buf, sizeof(buf), &dwRead, NULL) != FALSE) {
if (dwRead == 0) {
break;
}
if (WriteFile(dst, buf, dwRead, &dwWrite, NULL) == FALSE) {
break;
}
}
}
/*
*----------------------------------------------------------------------
*
* TclpCreateCommandChannel --
*
* This function is called by Tcl_OpenCommandChannel to perform
* the platform specific channel initialization for a command
* channel.
*
* Results:
|
| ︙ | | |
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
|
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
|
-
-
-
-
-
-
-
-
-
-
|
int numPids, /* The number of pids in the pid array. */
Tcl_Pid *pidPtr) /* An array of process identifiers. */
{
char channelName[16 + TCL_INTEGER_SPACE];
int channelId;
DWORD id;
PipeInfo *infoPtr = (PipeInfo *) ckalloc((unsigned) sizeof(PipeInfo));
OSVERSIONINFO os;
int useThreads;
/*
* Fetch the OS version info.
*/
os.dwOSVersionInfoSize = sizeof(os);
GetVersionEx(&os);
useThreads = (os.dwPlatformId != VER_PLATFORM_WIN32s);
PipeInit();
infoPtr->watchMask = 0;
infoPtr->flags = 0;
infoPtr->readFlags = 0;
infoPtr->readFile = readFile;
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
+
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
-
-
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
|
/*
* Use one of the fds associated with the channel as the
* channel id.
*/
if (readFile) {
WinPipe *pipePtr = (WinPipe *) readFile;
if (pipePtr->file.type == WIN32S_PIPE
&& pipePtr->file.handle == INVALID_HANDLE_VALUE) {
pipePtr->file.handle = CreateFileA(pipePtr->fileName, GENERIC_READ,
0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
}
channelId = (int) pipePtr->file.handle;
channelId = (int) ((WinFile*)readFile)->handle;
} else if (writeFile) {
channelId = (int) ((WinFile*)writeFile)->handle;
} else if (errorFile) {
channelId = (int) ((WinFile*)errorFile)->handle;
} else {
channelId = 0;
}
infoPtr->validMask = 0;
infoPtr->threadId = Tcl_GetCurrentThread();
if (readFile != NULL) {
if (useThreads) {
/*
* Start the background reader thread.
*/
/*
* Start the background reader thread.
*/
infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->readThread = CreateThread(NULL, 8000, PipeReaderThread,
infoPtr, 0, &id);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
infoPtr->readable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startReader = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->readThread = CreateThread(NULL, 8000, PipeReaderThread,
infoPtr, 0, &id);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
} else {
infoPtr->readThread = 0;
}
infoPtr->validMask |= TCL_READABLE;
} else {
infoPtr->readThread = 0;
}
if (writeFile != NULL) {
if (useThreads) {
/*
* Start the background writeer thwrite.
*/
/*
* Start the background writeer thwrite.
*/
infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->writeThread = CreateThread(NULL, 8000, PipeWriterThread,
infoPtr, 0, &id);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
infoPtr->writable = CreateEvent(NULL, TRUE, TRUE, NULL);
infoPtr->startWriter = CreateEvent(NULL, FALSE, FALSE, NULL);
infoPtr->writeThread = CreateThread(NULL, 8000, PipeWriterThread,
infoPtr, 0, &id);
SetThreadPriority(infoPtr->readThread, THREAD_PRIORITY_HIGHEST);
} else {
infoPtr->writeThread = 0;
}
infoPtr->validMask |= TCL_WRITABLE;
}
/*
* For backward compatibility with previous versions of Tcl, we
* use "file%d" as the base name for pipes even though it would
* be more natural to use "pipe%d".
|
| ︙ | | |
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
|
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
|
-
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
-
|
*nextPtrPtr = infoPtr->nextPtr;
break;
}
}
/*
* Wrap the error file into a channel and give it to the cleanup
* routine. If we are running in Win32s, just delete the error file
* routine.
* immediately, because it was never used.
*/
if (pipePtr->errorFile) {
WinFile *filePtr;
OSVERSIONINFO os;
os.dwOSVersionInfoSize = sizeof(os);
GetVersionEx(&os);
if (os.dwPlatformId == VER_PLATFORM_WIN32s) {
TclpCloseFile(pipePtr->errorFile);
errChan = NULL;
} else {
filePtr = (WinFile*)pipePtr->errorFile;
errChan = Tcl_MakeFileChannel((ClientData) filePtr->handle,
TCL_READABLE);
ckfree((char *) filePtr);
filePtr = (WinFile*)pipePtr->errorFile;
errChan = Tcl_MakeFileChannel((ClientData) filePtr->handle,
TCL_READABLE);
ckfree((char *) filePtr);
}
} else {
errChan = NULL;
}
result = TclCleanupChildren(interp, pipePtr->numPids, pipePtr->pidPtr,
errChan);
|
| ︙ | | |
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
|
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
|
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
-
+
-
-
-
+
+
+
-
-
-
-
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
-
-
+
+
+
+
+
-
-
-
+
+
+
-
-
+
+
-
-
|
{
PipeInfo *infoPtr = (PipeInfo *) instanceData;
WinFile *filePtr = (WinFile*) infoPtr->readFile;
DWORD count, bytesRead = 0;
int result;
*errorCode = 0;
if (filePtr->type == WIN32S_PIPE) {
if (((WinPipe *)filePtr)->otherPtr != NULL) {
panic("PipeInputProc: child process isn't finished writing");
}
if (filePtr->handle == INVALID_HANDLE_VALUE) {
filePtr->handle = CreateFileA(((WinPipe *)filePtr)->fileName,
GENERIC_READ, 0, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL,
NULL);
}
if (filePtr->handle == INVALID_HANDLE_VALUE) {
goto error;
}
} else {
/*
* Synchronize with the reader thread.
*/
/*
* Synchronize with the reader thread.
*/
result = WaitForRead(infoPtr, (infoPtr->flags & PIPE_ASYNC) ? 0 : 1);
result = WaitForRead(infoPtr, (infoPtr->flags & PIPE_ASYNC) ? 0 : 1);
/*
* If an error occurred, return immediately.
*/
/*
* If an error occurred, return immediately.
*/
if (result == -1) {
*errorCode = errno;
return -1;
}
if (result == -1) {
*errorCode = errno;
return -1;
}
if (infoPtr->readFlags & PIPE_EXTRABYTE) {
/*
* The reader thread consumed 1 byte as a side effect of
* waiting so we need to move it into the buffer.
*/
if (infoPtr->readFlags & PIPE_EXTRABYTE) {
/*
* The reader thread consumed 1 byte as a side effect of
* waiting so we need to move it into the buffer.
*/
*buf = infoPtr->extraByte;
infoPtr->readFlags &= ~PIPE_EXTRABYTE;
buf++;
bufSize--;
bytesRead = 1;
*buf = infoPtr->extraByte;
infoPtr->readFlags &= ~PIPE_EXTRABYTE;
buf++;
bufSize--;
bytesRead = 1;
/*
* If further read attempts would block, return what we have.
*/
/*
* If further read attempts would block, return what we have.
*/
if (result == 0) {
return bytesRead;
if (result == 0) {
return bytesRead;
}
}
}
/*
* Attempt to read bufSize bytes. The read will return immediately
* if there is any data available. Otherwise it will block until
* at least one byte is available or an EOF occurs.
*/
if (ReadFile(filePtr->handle, (LPVOID) buf, (DWORD) bufSize, &count,
(LPOVERLAPPED) NULL) == TRUE) {
return bytesRead + count;
} else if (bytesRead) {
/*
* Ignore errors if we have data to return.
*/
return bytesRead;
}
error:
TclWinConvertError(GetLastError());
if (errno == EPIPE) {
infoPtr->readFlags |= PIPE_EOF;
return 0;
}
*errorCode = errno;
return -1;
|
| ︙ | | |
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
|
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
|
-
+
-
+
-
-
+
-
-
-
-
+
+
+
-
-
+
-
-
-
-
-
-
-
+
+
+
+
+
+
-
|
*/
if (!infoPtr) {
return 1;
}
/*
* If we aren't on Win32s, check to see if the pipe is readable. Note
* Check to see if the pipe is readable. Note
* that we can't tell if a pipe is writable, so we always report it
* as being writable unless we have detected EOF.
*/
filePtr = (WinFile*) ((PipeInfo*)infoPtr)->writeFile;
mask = 0;
if (infoPtr->watchMask & TCL_WRITABLE) {
if ((infoPtr->watchMask & TCL_WRITABLE) &&
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForSingleObject(infoPtr->writable, 0)
(WaitForSingleObject(infoPtr->writable, 0) != WAIT_TIMEOUT)) {
!= WAIT_TIMEOUT)) {
mask = TCL_WRITABLE;
}
}
mask = TCL_WRITABLE;
}
filePtr = (WinFile*) ((PipeInfo*)infoPtr)->readFile;
if (infoPtr->watchMask & TCL_READABLE) {
if ((infoPtr->watchMask & TCL_READABLE) &&
if ((filePtr->type == WIN32S_PIPE)
|| (WaitForRead(infoPtr, 0) >= 0)) {
if (infoPtr->readFlags & PIPE_EOF) {
mask = TCL_READABLE;
} else {
mask |= TCL_READABLE;
}
(WaitForRead(infoPtr, 0) >= 0)) {
if (infoPtr->readFlags & PIPE_EOF) {
mask = TCL_READABLE;
} else {
mask |= TCL_READABLE;
}
}
}
/*
* Inform the channel of the events.
*/
Tcl_NotifyChannel(infoPtr->channel, infoPtr->watchMask & mask);
|
| ︙ | | |
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
|
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
|
-
-
-
-
-
-
-
-
-
-
|
ClientData *handlePtr) /* Where to store the handle. */
{
PipeInfo *infoPtr = (PipeInfo *) instanceData;
WinFile *filePtr;
if (direction == TCL_READABLE && infoPtr->readFile) {
filePtr = (WinFile*) infoPtr->readFile;
if (filePtr->type == WIN32S_PIPE) {
if (filePtr->handle == INVALID_HANDLE_VALUE) {
filePtr->handle = CreateFileA(((WinPipe *)filePtr)->fileName,
GENERIC_READ, 0, NULL, OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
}
if (filePtr->handle == INVALID_HANDLE_VALUE) {
return TCL_ERROR;
}
}
*handlePtr = (ClientData) filePtr->handle;
return TCL_OK;
}
if (direction == TCL_WRITABLE && infoPtr->writeFile) {
filePtr = (WinFile*) infoPtr->writeFile;
*handlePtr = (ClientData) filePtr->handle;
return TCL_OK;
|
| ︙ | | |