os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/win/tclWinChan.c
Update contrib.
4 * Channel drivers for Windows channels based on files, command
5 * pipes and TCP sockets.
7 * Copyright (c) 1995-1997 Sun Microsystems, Inc.
9 * See the file "license.terms" for information on usage and redistribution
10 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 * RCS: @(#) $Id: tclWinChan.c,v 1.30.2.5 2006/08/30 17:53:28 hobbs Exp $
15 #include "tclWinInt.h"
19 * State flags used in the info structures below.
22 #define FILE_PENDING (1<<0) /* Message is pending in the queue. */
23 #define FILE_ASYNC (1<<1) /* Channel is non-blocking. */
24 #define FILE_APPEND (1<<2) /* File is in append mode. */
26 #define FILE_TYPE_SERIAL (FILE_TYPE_PIPE+1)
27 #define FILE_TYPE_CONSOLE (FILE_TYPE_PIPE+2)
30 * The following structure contains per-instance data for a file based channel.
33 typedef struct FileInfo {
34 Tcl_Channel channel; /* Pointer to channel structure. */
35 int validMask; /* OR'ed combination of TCL_READABLE,
36 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
37 * which operations are valid on the file. */
38 int watchMask; /* OR'ed combination of TCL_READABLE,
39 * TCL_WRITABLE, or TCL_EXCEPTION: indicates
40 * which events should be reported. */
41 int flags; /* State flags, see above for a list. */
42 HANDLE handle; /* Input/output file. */
43 struct FileInfo *nextPtr; /* Pointer to next registered file. */
44 int dirty; /* Boolean flag. Set if the OS may have data
45 * pending on the channel */
48 typedef struct ThreadSpecificData {
50 * List of all file channels currently open.
53 FileInfo *firstFilePtr;
56 static Tcl_ThreadDataKey dataKey;
59 * The following structure is what is added to the Tcl event queue when
60 * file events are generated.
63 typedef struct FileEvent {
64 Tcl_Event header; /* Information that is standard for
66 FileInfo *infoPtr; /* Pointer to file info structure. Note
67 * that we still have to verify that the
68 * file exists before dereferencing this
73 * Static routines for this file:
76 static int FileBlockProc _ANSI_ARGS_((ClientData instanceData,
78 static void FileChannelExitHandler _ANSI_ARGS_((
79 ClientData clientData));
80 static void FileCheckProc _ANSI_ARGS_((ClientData clientData,
82 static int FileCloseProc _ANSI_ARGS_((ClientData instanceData,
84 static int FileEventProc _ANSI_ARGS_((Tcl_Event *evPtr,
86 static int FileGetHandleProc _ANSI_ARGS_((ClientData instanceData,
87 int direction, ClientData *handlePtr));
88 static ThreadSpecificData *FileInit _ANSI_ARGS_((void));
89 static int FileInputProc _ANSI_ARGS_((ClientData instanceData,
90 char *buf, int toRead, int *errorCode));
91 static int FileOutputProc _ANSI_ARGS_((ClientData instanceData,
92 CONST char *buf, int toWrite, int *errorCode));
93 static int FileSeekProc _ANSI_ARGS_((ClientData instanceData,
94 long offset, int mode, int *errorCode));
95 static Tcl_WideInt FileWideSeekProc _ANSI_ARGS_((ClientData instanceData,
96 Tcl_WideInt offset, int mode, int *errorCode));
97 static void FileSetupProc _ANSI_ARGS_((ClientData clientData,
99 static void FileWatchProc _ANSI_ARGS_((ClientData instanceData,
101 static void FileThreadActionProc _ANSI_ARGS_ ((
102 ClientData instanceData, int action));
103 static DWORD FileGetType _ANSI_ARGS_((HANDLE handle));
106 * This structure describes the channel type structure for file based IO.
109 static Tcl_ChannelType fileChannelType = {
110 "file", /* Type name. */
111 TCL_CHANNEL_VERSION_4, /* v4 channel */
112 FileCloseProc, /* Close proc. */
113 FileInputProc, /* Input proc. */
114 FileOutputProc, /* Output proc. */
115 FileSeekProc, /* Seek proc. */
116 NULL, /* Set option proc. */
117 NULL, /* Get option proc. */
118 FileWatchProc, /* Set up the notifier to watch the channel. */
119 FileGetHandleProc, /* Get an OS handle from channel. */
120 NULL, /* close2proc. */
121 FileBlockProc, /* Set blocking or non-blocking mode.*/
122 NULL, /* flush proc. */
123 NULL, /* handler proc. */
124 FileWideSeekProc, /* Wide seek proc. */
125 FileThreadActionProc, /* Thread action proc. */
131 * Unlike Borland and Microsoft, we don't register exception handlers
132 * by pushing registration records onto the runtime stack. Instead, we
133 * register them by creating an EXCEPTION_REGISTRATION within the activation
137 typedef struct EXCEPTION_REGISTRATION {
138 struct EXCEPTION_REGISTRATION* link;
139 EXCEPTION_DISPOSITION (*handler)( struct _EXCEPTION_RECORD*, void*,
140 struct _CONTEXT*, void* );
144 } EXCEPTION_REGISTRATION;
149 *----------------------------------------------------------------------
153 * This function creates the window used to simulate file events.
159 * Creates a new window and creates an exit handler.
161 *----------------------------------------------------------------------
164 static ThreadSpecificData *
167 ThreadSpecificData *tsdPtr =
168 (ThreadSpecificData *)TclThreadDataKeyGet(&dataKey);
169 if (tsdPtr == NULL) {
170 tsdPtr = TCL_TSD_INIT(&dataKey);
171 tsdPtr->firstFilePtr = NULL;
172 Tcl_CreateEventSource(FileSetupProc, FileCheckProc, NULL);
173 Tcl_CreateThreadExitHandler(FileChannelExitHandler, NULL);
179 *----------------------------------------------------------------------
181 * FileChannelExitHandler --
183 * This function is called to cleanup the channel driver before
190 * Destroys the communication window.
192 *----------------------------------------------------------------------
196 FileChannelExitHandler(clientData)
197 ClientData clientData; /* Old window proc */
199 Tcl_DeleteEventSource(FileSetupProc, FileCheckProc, NULL);
203 *----------------------------------------------------------------------
207 * This procedure is invoked before Tcl_DoOneEvent blocks waiting
214 * Adjusts the block time if needed.
216 *----------------------------------------------------------------------
220 FileSetupProc(data, flags)
221 ClientData data; /* Not used. */
222 int flags; /* Event flags as passed to Tcl_DoOneEvent. */
225 Tcl_Time blockTime = { 0, 0 };
226 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
228 if (!(flags & TCL_FILE_EVENTS)) {
233 * Check to see if there is a ready file. If so, poll.
236 for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL;
237 infoPtr = infoPtr->nextPtr) {
238 if (infoPtr->watchMask) {
239 Tcl_SetMaxBlockTime(&blockTime);
246 *----------------------------------------------------------------------
250 * This procedure is called by Tcl_DoOneEvent to check the file
251 * event source for events.
257 * May queue an event.
259 *----------------------------------------------------------------------
263 FileCheckProc(data, flags)
264 ClientData data; /* Not used. */
265 int flags; /* Event flags as passed to Tcl_DoOneEvent. */
269 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
271 if (!(flags & TCL_FILE_EVENTS)) {
276 * Queue events for any ready files that don't already have events
277 * queued (caused by persistent states that won't generate WinSock
281 for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL;
282 infoPtr = infoPtr->nextPtr) {
283 if (infoPtr->watchMask && !(infoPtr->flags & FILE_PENDING)) {
284 infoPtr->flags |= FILE_PENDING;
285 evPtr = (FileEvent *) ckalloc(sizeof(FileEvent));
286 evPtr->header.proc = FileEventProc;
287 evPtr->infoPtr = infoPtr;
288 Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL);
293 /*----------------------------------------------------------------------
297 * This function is invoked by Tcl_ServiceEvent when a file event
298 * reaches the front of the event queue. This procedure invokes
299 * Tcl_NotifyChannel on the file.
302 * Returns 1 if the event was handled, meaning it should be removed
303 * from the queue. Returns 0 if the event was not handled, meaning
304 * it should stay on the queue. The only time the event isn't
305 * handled is if the TCL_FILE_EVENTS flag bit isn't set.
308 * Whatever the notifier callback does.
310 *----------------------------------------------------------------------
314 FileEventProc(evPtr, flags)
315 Tcl_Event *evPtr; /* Event to service. */
316 int flags; /* Flags that indicate what events to
317 * handle, such as TCL_FILE_EVENTS. */
319 FileEvent *fileEvPtr = (FileEvent *)evPtr;
321 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
323 if (!(flags & TCL_FILE_EVENTS)) {
328 * Search through the list of watched files for the one whose handle
329 * matches the event. We do this rather than simply dereferencing
330 * the handle in the event so that files can be deleted while the
331 * event is in the queue.
334 for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL;
335 infoPtr = infoPtr->nextPtr) {
336 if (fileEvPtr->infoPtr == infoPtr) {
337 infoPtr->flags &= ~(FILE_PENDING);
338 Tcl_NotifyChannel(infoPtr->channel, infoPtr->watchMask);
346 *----------------------------------------------------------------------
350 * Set blocking or non-blocking mode on channel.
353 * 0 if successful, errno when failed.
356 * Sets the device into blocking or non-blocking mode.
358 *----------------------------------------------------------------------
362 FileBlockProc(instanceData, mode)
363 ClientData instanceData; /* Instance data for channel. */
364 int mode; /* TCL_MODE_BLOCKING or
365 * TCL_MODE_NONBLOCKING. */
367 FileInfo *infoPtr = (FileInfo *) instanceData;
370 * Files on Windows can not be switched between blocking and nonblocking,
371 * hence we have to emulate the behavior. This is done in the input
372 * function by checking against a bit in the state. We set or unset the
373 * bit here to cause the input function to emulate the correct behavior.
376 if (mode == TCL_MODE_NONBLOCKING) {
377 infoPtr->flags |= FILE_ASYNC;
379 infoPtr->flags &= ~(FILE_ASYNC);
385 *----------------------------------------------------------------------
389 * Closes the IO channel.
392 * 0 if successful, the value of errno if failed.
395 * Closes the physical channel
397 *----------------------------------------------------------------------
401 FileCloseProc(instanceData, interp)
402 ClientData instanceData; /* Pointer to FileInfo structure. */
403 Tcl_Interp *interp; /* Not used. */
405 FileInfo *fileInfoPtr = (FileInfo *) instanceData;
407 ThreadSpecificData *tsdPtr;
411 * Remove the file from the watch list.
414 FileWatchProc(instanceData, 0);
417 * Don't close the Win32 handle if the handle is a standard channel
418 * during the thread exit process. Otherwise, one thread may kill
419 * the stdio of another.
422 if (!TclInThreadExit()
423 || ((GetStdHandle(STD_INPUT_HANDLE) != fileInfoPtr->handle)
424 && (GetStdHandle(STD_OUTPUT_HANDLE) != fileInfoPtr->handle)
425 && (GetStdHandle(STD_ERROR_HANDLE) != fileInfoPtr->handle))) {
426 if (CloseHandle(fileInfoPtr->handle) == FALSE) {
427 TclWinConvertError(GetLastError());
433 * See if this FileInfo* is still on the thread local list.
435 tsdPtr = TCL_TSD_INIT(&dataKey);
436 for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL;
437 infoPtr = infoPtr->nextPtr) {
438 if (infoPtr == fileInfoPtr) {
440 * This channel exists on the thread local list. It should
441 * have been removed by an earlier Thread Action call,
442 * but do that now since just deallocating fileInfoPtr would
443 * leave an deallocated pointer on the thread local list.
445 FileThreadActionProc(fileInfoPtr,TCL_CHANNEL_THREAD_REMOVE);
449 ckfree((char *)fileInfoPtr);
454 *----------------------------------------------------------------------
458 * Seeks on a file-based channel. Returns the new position.
461 * -1 if failed, the new position if successful. If failed, it
462 * also sets *errorCodePtr to the error code.
465 * Moves the location at which the channel will be accessed in
468 *----------------------------------------------------------------------
472 FileSeekProc(instanceData, offset, mode, errorCodePtr)
473 ClientData instanceData; /* File state. */
474 long offset; /* Offset to seek to. */
475 int mode; /* Relative to where should we seek? */
476 int *errorCodePtr; /* To store error code. */
478 FileInfo *infoPtr = (FileInfo *) instanceData;
480 DWORD newPos, newPosHigh;
481 DWORD oldPos, oldPosHigh;
484 if (mode == SEEK_SET) {
485 moveMethod = FILE_BEGIN;
486 } else if (mode == SEEK_CUR) {
487 moveMethod = FILE_CURRENT;
489 moveMethod = FILE_END;
493 * Save our current place in case we need to roll-back the seek.
495 oldPosHigh = (DWORD)0;
496 oldPos = SetFilePointer(infoPtr->handle, (LONG)0, &oldPosHigh,
498 if (oldPos == INVALID_SET_FILE_POINTER) {
499 DWORD winError = GetLastError();
500 if (winError != NO_ERROR) {
501 TclWinConvertError(winError);
502 *errorCodePtr = errno;
507 newPosHigh = (DWORD)(offset < 0 ? -1 : 0);
508 newPos = SetFilePointer(infoPtr->handle, (LONG) offset, &newPosHigh,
510 if (newPos == INVALID_SET_FILE_POINTER) {
511 DWORD winError = GetLastError();
512 if (winError != NO_ERROR) {
513 TclWinConvertError(winError);
514 *errorCodePtr = errno;
520 * Check for expressability in our return type, and roll-back otherwise.
522 if (newPosHigh != 0) {
523 *errorCodePtr = EOVERFLOW;
524 SetFilePointer(infoPtr->handle, (LONG)oldPos, &oldPosHigh, FILE_BEGIN);
531 *----------------------------------------------------------------------
533 * FileWideSeekProc --
535 * Seeks on a file-based channel. Returns the new position.
538 * -1 if failed, the new position if successful. If failed, it
539 * also sets *errorCodePtr to the error code.
542 * Moves the location at which the channel will be accessed in
545 *----------------------------------------------------------------------
549 FileWideSeekProc(instanceData, offset, mode, errorCodePtr)
550 ClientData instanceData; /* File state. */
551 Tcl_WideInt offset; /* Offset to seek to. */
552 int mode; /* Relative to where should we seek? */
553 int *errorCodePtr; /* To store error code. */
555 FileInfo *infoPtr = (FileInfo *) instanceData;
557 DWORD newPos, newPosHigh;
560 if (mode == SEEK_SET) {
561 moveMethod = FILE_BEGIN;
562 } else if (mode == SEEK_CUR) {
563 moveMethod = FILE_CURRENT;
565 moveMethod = FILE_END;
568 newPosHigh = (DWORD)(offset >> 32);
569 newPos = SetFilePointer(infoPtr->handle, (LONG) offset, &newPosHigh,
571 if (newPos == INVALID_SET_FILE_POINTER) {
572 DWORD winError = GetLastError();
573 if (winError != NO_ERROR) {
574 TclWinConvertError(winError);
575 *errorCodePtr = errno;
579 return ((Tcl_WideInt) newPos) | (((Tcl_WideInt) newPosHigh) << 32);
583 *----------------------------------------------------------------------
587 * Reads input from the IO channel into the buffer given. Returns
588 * count of how many bytes were actually read, and an error indication.
591 * A count of how many bytes were read is returned and an error
592 * indication is returned in an output argument.
595 * Reads input from the actual channel.
597 *----------------------------------------------------------------------
601 FileInputProc(instanceData, buf, bufSize, errorCode)
602 ClientData instanceData; /* File state. */
603 char *buf; /* Where to store data read. */
604 int bufSize; /* How much space is available
606 int *errorCode; /* Where to store error code. */
612 infoPtr = (FileInfo *) instanceData;
615 * Note that we will block on reads from a console buffer until a
616 * full line has been entered. The only way I know of to get
617 * around this is to write a console driver. We should probably
618 * do this at some point, but for now, we just block. The same
619 * problem exists for files being read over the network.
622 if (ReadFile(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &bytesRead,
623 (LPOVERLAPPED) NULL) != FALSE) {
627 TclWinConvertError(GetLastError());
629 if (errno == EPIPE) {
636 *----------------------------------------------------------------------
640 * Writes the given output on the IO channel. Returns count of how
641 * many characters were actually written, and an error indication.
644 * A count of how many characters were written is returned and an
645 * error indication is returned in an output argument.
648 * Writes output on the actual channel.
650 *----------------------------------------------------------------------
654 FileOutputProc(instanceData, buf, toWrite, errorCode)
655 ClientData instanceData; /* File state. */
656 CONST char *buf; /* The data buffer. */
657 int toWrite; /* How many bytes to write? */
658 int *errorCode; /* Where to store error code. */
660 FileInfo *infoPtr = (FileInfo *) instanceData;
666 * If we are writing to a file that was opened with O_APPEND, we need to
667 * seek to the end of the file before writing the current buffer.
670 if (infoPtr->flags & FILE_APPEND) {
671 SetFilePointer(infoPtr->handle, 0, NULL, FILE_END);
674 if (WriteFile(infoPtr->handle, (LPVOID) buf, (DWORD) toWrite, &bytesWritten,
675 (LPOVERLAPPED) NULL) == FALSE) {
676 TclWinConvertError(GetLastError());
685 *----------------------------------------------------------------------
689 * Called by the notifier to set up to watch for events on this
698 *----------------------------------------------------------------------
702 FileWatchProc(instanceData, mask)
703 ClientData instanceData; /* File state. */
704 int mask; /* What events to watch for; OR-ed
705 * combination of TCL_READABLE,
706 * TCL_WRITABLE and TCL_EXCEPTION. */
708 FileInfo *infoPtr = (FileInfo *) instanceData;
709 Tcl_Time blockTime = { 0, 0 };
712 * Since the file is always ready for events, we set the block time
713 * to zero so we will poll.
716 infoPtr->watchMask = mask & infoPtr->validMask;
717 if (infoPtr->watchMask) {
718 Tcl_SetMaxBlockTime(&blockTime);
723 *----------------------------------------------------------------------
725 * FileGetHandleProc --
727 * Called from Tcl_GetChannelHandle to retrieve OS handles from
728 * a file based channel.
731 * Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if
732 * there is no handle for the specified direction.
737 *----------------------------------------------------------------------
741 FileGetHandleProc(instanceData, direction, handlePtr)
742 ClientData instanceData; /* The file state. */
743 int direction; /* TCL_READABLE or TCL_WRITABLE */
744 ClientData *handlePtr; /* Where to store the handle. */
746 FileInfo *infoPtr = (FileInfo *) instanceData;
748 if (direction & infoPtr->validMask) {
749 *handlePtr = (ClientData) infoPtr->handle;
758 *----------------------------------------------------------------------
760 * TclpOpenFileChannel --
762 * Open an File based channel on Unix systems.
765 * The new channel or NULL. If NULL, the output argument
766 * errorCodePtr is set to a POSIX error.
769 * May open the channel and may cause creation of a file on the
772 *----------------------------------------------------------------------
776 TclpOpenFileChannel(interp, pathPtr, mode, permissions)
777 Tcl_Interp *interp; /* Interpreter for error reporting;
779 Tcl_Obj *pathPtr; /* Name of file to open. */
780 int mode; /* POSIX mode. */
781 int permissions; /* If the open involves creating a
782 * file, with what modes to create
785 Tcl_Channel channel = 0;
786 int channelPermissions;
787 DWORD accessMode, createMode, shareMode, flags;
788 CONST TCHAR *nativeName;
790 char channelName[16 + TCL_INTEGER_SPACE];
791 TclFile readFile = NULL;
792 TclFile writeFile = NULL;
794 nativeName = (TCHAR*) Tcl_FSGetNativePath(pathPtr);
795 if (nativeName == NULL) {
799 switch (mode & (O_RDONLY | O_WRONLY | O_RDWR)) {
801 accessMode = GENERIC_READ;
802 channelPermissions = TCL_READABLE;
805 accessMode = GENERIC_WRITE;
806 channelPermissions = TCL_WRITABLE;
809 accessMode = (GENERIC_READ | GENERIC_WRITE);
810 channelPermissions = (TCL_READABLE | TCL_WRITABLE);
813 panic("TclpOpenFileChannel: invalid mode value");
818 * Map the creation flags to the NT create mode.
821 switch (mode & (O_CREAT | O_EXCL | O_TRUNC)) {
822 case (O_CREAT | O_EXCL):
823 case (O_CREAT | O_EXCL | O_TRUNC):
824 createMode = CREATE_NEW;
826 case (O_CREAT | O_TRUNC):
827 createMode = CREATE_ALWAYS;
830 createMode = OPEN_ALWAYS;
833 case (O_TRUNC | O_EXCL):
834 createMode = TRUNCATE_EXISTING;
837 createMode = OPEN_EXISTING;
842 * If the file is being created, get the file attributes from the
843 * permissions argument, else use the existing file attributes.
846 if (mode & O_CREAT) {
847 if (permissions & S_IWRITE) {
848 flags = FILE_ATTRIBUTE_NORMAL;
850 flags = FILE_ATTRIBUTE_READONLY;
853 flags = (*tclWinProcs->getFileAttributesProc)(nativeName);
854 if (flags == 0xFFFFFFFF) {
860 * Set up the file sharing mode. We want to allow simultaneous access.
863 shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE;
866 * Now we get to create the file.
869 handle = (*tclWinProcs->createFileProc)(nativeName, accessMode,
870 shareMode, NULL, createMode, flags, (HANDLE) NULL);
872 if (handle == INVALID_HANDLE_VALUE) {
874 err = GetLastError();
875 if ((err & 0xffffL) == ERROR_OPEN_FAILED) {
876 err = (mode & O_CREAT) ? ERROR_FILE_EXISTS : ERROR_FILE_NOT_FOUND;
878 TclWinConvertError(err);
879 if (interp != (Tcl_Interp *) NULL) {
880 Tcl_AppendResult(interp, "couldn't open \"",
881 Tcl_GetString(pathPtr), "\": ",
882 Tcl_PosixError(interp), (char *) NULL);
889 switch ( FileGetType(handle) ) {
890 case FILE_TYPE_SERIAL:
892 * Reopen channel for OVERLAPPED operation
893 * Normally this shouldn't fail, because the channel exists
895 handle = TclWinSerialReopen(handle, nativeName, accessMode);
896 if (handle == INVALID_HANDLE_VALUE) {
897 TclWinConvertError(GetLastError());
898 if (interp != (Tcl_Interp *) NULL) {
899 Tcl_AppendResult(interp, "couldn't reopen serial \"",
900 Tcl_GetString(pathPtr), "\": ",
901 Tcl_PosixError(interp), (char *) NULL);
905 channel = TclWinOpenSerialChannel(handle, channelName,
908 case FILE_TYPE_CONSOLE:
909 channel = TclWinOpenConsoleChannel(handle, channelName,
913 if (channelPermissions & TCL_READABLE) {
914 readFile = TclWinMakeFile(handle);
916 if (channelPermissions & TCL_WRITABLE) {
917 writeFile = TclWinMakeFile(handle);
919 channel = TclpCreateCommandChannel(readFile, writeFile, NULL, 0, NULL);
923 case FILE_TYPE_UNKNOWN:
924 channel = TclWinOpenFileChannel(handle, channelName,
926 (mode & O_APPEND) ? FILE_APPEND : 0);
931 * The handle is of an unknown type, probably /dev/nul equivalent
932 * or possibly a closed handle.
936 Tcl_AppendResult(interp, "couldn't open \"",
937 Tcl_GetString(pathPtr), "\": ",
938 "bad file type", (char *) NULL);
946 *----------------------------------------------------------------------
948 * Tcl_MakeFileChannel --
950 * Creates a Tcl_Channel from an existing platform specific file
954 * The Tcl_Channel created around the preexisting file.
959 *----------------------------------------------------------------------
963 Tcl_MakeFileChannel(rawHandle, mode)
964 ClientData rawHandle; /* OS level handle */
965 int mode; /* ORed combination of TCL_READABLE and
966 * TCL_WRITABLE to indicate file mode. */
969 EXCEPTION_REGISTRATION registration;
971 char channelName[16 + TCL_INTEGER_SPACE];
972 Tcl_Channel channel = NULL;
973 HANDLE handle = (HANDLE) rawHandle;
975 TclFile readFile = NULL;
976 TclFile writeFile = NULL;
983 switch (FileGetType(handle))
985 case FILE_TYPE_SERIAL:
986 channel = TclWinOpenSerialChannel(handle, channelName, mode);
988 case FILE_TYPE_CONSOLE:
989 channel = TclWinOpenConsoleChannel(handle, channelName, mode);
992 if (mode & TCL_READABLE)
994 readFile = TclWinMakeFile(handle);
996 if (mode & TCL_WRITABLE)
998 writeFile = TclWinMakeFile(handle);
1000 channel = TclpCreateCommandChannel(readFile, writeFile, NULL, 0, NULL);
1003 case FILE_TYPE_DISK:
1004 case FILE_TYPE_CHAR:
1005 channel = TclWinOpenFileChannel(handle, channelName, mode, 0);
1008 case FILE_TYPE_UNKNOWN:
1011 * The handle is of an unknown type. Test the validity of this OS
1012 * handle by duplicating it, then closing the dupe. The Win32 API
1013 * doesn't provide an IsValidHandle() function, so we have to emulate
1014 * it here. This test will not work on a console handle reliably,
1015 * which is why we can't test every handle that comes into this
1016 * function in this way.
1019 result = DuplicateHandle(GetCurrentProcess(), handle,
1020 GetCurrentProcess(), &dupedHandle, 0, FALSE,
1021 DUPLICATE_SAME_ACCESS);
1025 * Unable to make a duplicate. It's definately invalid at this
1033 * Use structured exception handling (Win32 SEH) to protect the close
1034 * of this duped handle which might throw EXCEPTION_INVALID_HANDLE.
1040 CloseHandle(dupedHandle);
1042 } __except (EXCEPTION_EXECUTE_HANDLER) {}
1045 * Don't have SEH available, do things the hard way.
1046 * Note that this needs to be one block of asm, to avoid stack
1047 * imbalance; also, it is illegal for one asm block to contain
1048 * a jump to another.
1051 __asm__ __volatile__ (
1054 * Pick up parameters before messing with the stack
1057 "movl %[dupedHandle], %%ebx" "\n\t"
1060 * Construct an EXCEPTION_REGISTRATION to protect the
1061 * call to CloseHandle
1063 "leal %[registration], %%edx" "\n\t"
1064 "movl %%fs:0, %%eax" "\n\t"
1065 "movl %%eax, 0x0(%%edx)" "\n\t" /* link */
1066 "leal 1f, %%eax" "\n\t"
1067 "movl %%eax, 0x4(%%edx)" "\n\t" /* handler */
1068 "movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */
1069 "movl %%esp, 0xc(%%edx)" "\n\t" /* esp */
1070 "movl $0, 0x10(%%edx)" "\n\t" /* status */
1072 /* Link the EXCEPTION_REGISTRATION on the chain */
1074 "movl %%edx, %%fs:0" "\n\t"
1076 /* Call CloseHandle( dupedHandle ) */
1078 "pushl %%ebx" "\n\t"
1079 "call _CloseHandle@4" "\n\t"
1082 * Come here on normal exit. Recover the EXCEPTION_REGISTRATION
1083 * and put a TRUE status return into it.
1086 "movl %%fs:0, %%edx" "\n\t"
1087 "movl $1, %%eax" "\n\t"
1088 "movl %%eax, 0x10(%%edx)" "\n\t"
1092 * Come here on an exception. Recover the EXCEPTION_REGISTRATION
1096 "movl %%fs:0, %%edx" "\n\t"
1097 "movl 0x8(%%edx), %%edx" "\n\t"
1100 * Come here however we exited. Restore context from the
1101 * EXCEPTION_REGISTRATION in case the stack is unbalanced.
1105 "movl 0xc(%%edx), %%esp" "\n\t"
1106 "movl 0x8(%%edx), %%ebp" "\n\t"
1107 "movl 0x0(%%edx), %%eax" "\n\t"
1108 "movl %%eax, %%fs:0" "\n\t"
1113 [registration] "m" (registration),
1114 [dupedHandle] "m" (dupedHandle)
1116 "%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory"
1118 result = registration.status;
1121 if (result == FALSE) {
1125 /* Fall through, the handle is valid. */
1128 * Create the undefined channel, anyways, because we know the handle
1129 * is valid to something.
1132 channel = TclWinOpenFileChannel(handle, channelName, mode, 0);
1139 *----------------------------------------------------------------------
1141 * TclpGetDefaultStdChannel --
1143 * Constructs a channel for the specified standard OS handle.
1146 * Returns the specified default standard channel, or NULL.
1149 * May cause the creation of a standard channel and the underlying
1152 *----------------------------------------------------------------------
1156 TclpGetDefaultStdChannel(type)
1157 int type; /* One of TCL_STDIN, TCL_STDOUT, TCL_STDERR. */
1159 Tcl_Channel channel;
1163 DWORD handleId; /* Standard handle to retrieve. */
1168 handleId = STD_INPUT_HANDLE;
1169 mode = TCL_READABLE;
1173 handleId = STD_OUTPUT_HANDLE;
1174 mode = TCL_WRITABLE;
1178 handleId = STD_ERROR_HANDLE;
1179 mode = TCL_WRITABLE;
1183 panic("TclGetDefaultStdChannel: Unexpected channel type");
1187 handle = GetStdHandle(handleId);
1190 * Note that we need to check for 0 because Windows may return 0 if this
1191 * is not a console mode application, even though this is not a valid
1195 if ((handle == INVALID_HANDLE_VALUE) || (handle == 0)) {
1196 return (Tcl_Channel) NULL;
1199 channel = Tcl_MakeFileChannel(handle, mode);
1201 if (channel == NULL) {
1202 return (Tcl_Channel) NULL;
1206 * Set up the normal channel options for stdio handles.
1209 if ((Tcl_SetChannelOption((Tcl_Interp *) NULL, channel, "-translation",
1210 "auto") == TCL_ERROR)
1211 || (Tcl_SetChannelOption((Tcl_Interp *) NULL, channel, "-eofchar",
1212 "\032 {}") == TCL_ERROR)
1213 || (Tcl_SetChannelOption((Tcl_Interp *) NULL, channel,
1214 "-buffering", bufMode) == TCL_ERROR)) {
1215 Tcl_Close((Tcl_Interp *) NULL, channel);
1216 return (Tcl_Channel) NULL;
1224 *----------------------------------------------------------------------
1226 * TclWinOpenFileChannel --
1228 * Constructs a File channel for the specified standard OS handle.
1229 * This is a helper function to break up the construction of
1230 * channels into File, Console, or Serial.
1233 * Returns the new channel, or NULL.
1236 * May open the channel and may cause creation of a file on the
1239 *----------------------------------------------------------------------
1243 TclWinOpenFileChannel(handle, channelName, permissions, appendMode)
1250 ThreadSpecificData *tsdPtr;
1252 tsdPtr = FileInit();
1255 * See if a channel with this handle already exists.
1258 for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL;
1259 infoPtr = infoPtr->nextPtr) {
1260 if (infoPtr->handle == (HANDLE) handle) {
1261 return (permissions == infoPtr->validMask) ? infoPtr->channel : NULL;
1265 infoPtr = (FileInfo *) ckalloc((unsigned) sizeof(FileInfo));
1266 /* TIP #218. Removed the code inserting the new structure
1267 * into the global list. This is now handled in the thread
1268 * action callbacks, and only there.
1270 infoPtr->nextPtr = NULL;
1271 infoPtr->validMask = permissions;
1272 infoPtr->watchMask = 0;
1273 infoPtr->flags = appendMode;
1274 infoPtr->handle = handle;
1276 wsprintfA(channelName, "file%lx", (int) infoPtr);
1278 infoPtr->channel = Tcl_CreateChannel(&fileChannelType, channelName,
1279 (ClientData) infoPtr, permissions);
1282 * Files have default translation of AUTO and ^Z eof char, which
1283 * means that a ^Z will be accepted as EOF when reading.
1286 Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto");
1287 Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}");
1289 return infoPtr->channel;
1294 *----------------------------------------------------------------------
1296 * TclWinFlushDirtyChannels --
1298 * Flush all dirty channels to disk, so that requesting the
1299 * size of any file returns the correct value.
1305 * Information is actually written to disk now, rather than
1306 * later. Don't call this too often, or there will be a
1307 * performance hit (i.e. only call when we need to ask for
1308 * the size of a file).
1310 *----------------------------------------------------------------------
1314 TclWinFlushDirtyChannels ()
1317 ThreadSpecificData *tsdPtr;
1319 tsdPtr = FileInit();
1322 * Flush all channels which are dirty, i.e. may have data pending
1326 for (infoPtr = tsdPtr->firstFilePtr;
1328 infoPtr = infoPtr->nextPtr) {
1329 if (infoPtr->dirty) {
1330 FlushFileBuffers(infoPtr->handle);
1337 *----------------------------------------------------------------------
1339 * FileThreadActionProc --
1341 * Insert or remove any thread local refs to this channel.
1347 * Changes thread local list of valid channels.
1349 *----------------------------------------------------------------------
1353 FileThreadActionProc (instanceData, action)
1354 ClientData instanceData;
1357 ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey);
1358 FileInfo *infoPtr = (FileInfo *) instanceData;
1360 if (action == TCL_CHANNEL_THREAD_INSERT) {
1361 infoPtr->nextPtr = tsdPtr->firstFilePtr;
1362 tsdPtr->firstFilePtr = infoPtr;
1364 FileInfo **nextPtrPtr;
1367 for (nextPtrPtr = &(tsdPtr->firstFilePtr); (*nextPtrPtr) != NULL;
1368 nextPtrPtr = &((*nextPtrPtr)->nextPtr)) {
1369 if ((*nextPtrPtr) == infoPtr) {
1370 (*nextPtrPtr) = infoPtr->nextPtr;
1377 * This could happen if the channel was created in one thread
1378 * and then moved to another without updating the thread
1379 * local data in each thread.
1383 panic("file info ptr not on thread channel list");
1390 *----------------------------------------------------------------------
1394 * Given a file handle, return its type
1402 *----------------------------------------------------------------------
1407 HANDLE handle; /* Opened file handle */
1410 DWORD consoleParams;
1413 type = GetFileType(handle);
1416 * If the file is a character device, we need to try to figure out
1417 * whether it is a serial port, a console, or something else. We
1418 * test for the console case first because this is more common.
1421 if (type == FILE_TYPE_CHAR || (type == FILE_TYPE_UNKNOWN && !GetLastError())) {
1422 if (GetConsoleMode(handle, &consoleParams)) {
1423 type = FILE_TYPE_CONSOLE;
1425 dcb.DCBlength = sizeof( DCB ) ;
1426 if (GetCommState(handle, &dcb)) {
1427 type = FILE_TYPE_SERIAL;