| 1 |
/* |
| 2 |
* tclWinChan.c |
| 3 |
* |
| 4 |
* Channel drivers for Windows channels based on files, command pipes and |
| 5 |
* TCP sockets. |
| 6 |
* |
| 7 |
* Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 8 |
* |
| 9 |
* See the file "license.terms" for information on usage and redistribution of |
| 10 |
* this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 11 |
*/ |
| 12 |
|
| 13 |
#include "tclWinInt.h" |
| 14 |
#include "tclFileSystem.h" |
| 15 |
#include "tclIO.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* State flags used in the info structures below. |
| 19 |
*/ |
| 20 |
|
| 21 |
#define FILE_PENDING (1<<0) /* Message is pending in the queue. */ |
| 22 |
#define FILE_ASYNC (1<<1) /* Channel is non-blocking. */ |
| 23 |
#define FILE_APPEND (1<<2) /* File is in append mode. */ |
| 24 |
|
| 25 |
#define FILE_TYPE_SERIAL (FILE_TYPE_PIPE+1) |
| 26 |
#define FILE_TYPE_CONSOLE (FILE_TYPE_PIPE+2) |
| 27 |
|
| 28 |
/* |
| 29 |
* The following structure contains per-instance data for a file based channel. |
| 30 |
*/ |
| 31 |
|
| 32 |
typedef struct FileInfo { |
| 33 |
Tcl_Channel channel; /* Pointer to channel structure. */ |
| 34 |
int validMask; /* OR'ed combination of TCL_READABLE, |
| 35 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 36 |
* which operations are valid on the file. */ |
| 37 |
int watchMask; /* OR'ed combination of TCL_READABLE, |
| 38 |
* TCL_WRITABLE, or TCL_EXCEPTION: indicates |
| 39 |
* which events should be reported. */ |
| 40 |
int flags; /* State flags, see above for a list. */ |
| 41 |
HANDLE handle; /* Input/output file. */ |
| 42 |
struct FileInfo *nextPtr; /* Pointer to next registered file. */ |
| 43 |
int dirty; /* Boolean flag. Set if the OS may have data |
| 44 |
* pending on the channel. */ |
| 45 |
} FileInfo; |
| 46 |
|
| 47 |
typedef struct ThreadSpecificData { |
| 48 |
/* |
| 49 |
* List of all file channels currently open. |
| 50 |
*/ |
| 51 |
|
| 52 |
FileInfo *firstFilePtr; |
| 53 |
} ThreadSpecificData; |
| 54 |
|
| 55 |
static Tcl_ThreadDataKey dataKey; |
| 56 |
|
| 57 |
/* |
| 58 |
* The following structure is what is added to the Tcl event queue when file |
| 59 |
* events are generated. |
| 60 |
*/ |
| 61 |
|
| 62 |
typedef struct FileEvent { |
| 63 |
Tcl_Event header; /* Information that is standard for all |
| 64 |
* events. */ |
| 65 |
FileInfo *infoPtr; /* Pointer to file info structure. Note that |
| 66 |
* we still have to verify that the file |
| 67 |
* exists before dereferencing this |
| 68 |
* pointer. */ |
| 69 |
} FileEvent; |
| 70 |
|
| 71 |
/* |
| 72 |
* Static routines for this file: |
| 73 |
*/ |
| 74 |
|
| 75 |
static int FileBlockProc(ClientData instanceData, int mode); |
| 76 |
static void FileChannelExitHandler(ClientData clientData); |
| 77 |
static void FileCheckProc(ClientData clientData, int flags); |
| 78 |
static int FileCloseProc(ClientData instanceData, |
| 79 |
Tcl_Interp *interp); |
| 80 |
static int FileClose2Proc(ClientData instanceData, |
| 81 |
Tcl_Interp *interp, int flags); |
| 82 |
static int FileEventProc(Tcl_Event *evPtr, int flags); |
| 83 |
static int FileGetHandleProc(ClientData instanceData, |
| 84 |
int direction, ClientData *handlePtr); |
| 85 |
static ThreadSpecificData *FileInit(void); |
| 86 |
static int FileInputProc(ClientData instanceData, char *buf, |
| 87 |
int toRead, int *errorCode); |
| 88 |
static int FileOutputProc(ClientData instanceData, |
| 89 |
const char *buf, int toWrite, int *errorCode); |
| 90 |
static int FileSeekProc(ClientData instanceData, long offset, |
| 91 |
int mode, int *errorCode); |
| 92 |
static Tcl_WideInt FileWideSeekProc(ClientData instanceData, |
| 93 |
Tcl_WideInt offset, int mode, int *errorCode); |
| 94 |
static void FileSetupProc(ClientData clientData, int flags); |
| 95 |
static void FileWatchProc(ClientData instanceData, int mask); |
| 96 |
static void FileThreadActionProc(ClientData instanceData, |
| 97 |
int action); |
| 98 |
static int FileTruncateProc(ClientData instanceData, |
| 99 |
Tcl_WideInt length); |
| 100 |
static DWORD FileGetType(HANDLE handle); |
| 101 |
static int NativeIsComPort(const WCHAR *nativeName); |
| 102 |
static Tcl_Channel OpenFileChannel(HANDLE handle, char *channelName, |
| 103 |
int permissions, int appendMode); |
| 104 |
|
| 105 |
/* |
| 106 |
* This structure describes the channel type structure for file based IO. |
| 107 |
*/ |
| 108 |
|
| 109 |
static const Tcl_ChannelType fileChannelType = { |
| 110 |
"file", /* Type name. */ |
| 111 |
TCL_CHANNEL_VERSION_5, /* v5 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 |
FileClose2Proc, /* 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. */ |
| 126 |
FileTruncateProc /* Truncate proc. */ |
| 127 |
}; |
| 128 |
|
| 129 |
/* |
| 130 |
*---------------------------------------------------------------------- |
| 131 |
* |
| 132 |
* FileInit -- |
| 133 |
* |
| 134 |
* This function creates the window used to simulate file events. |
| 135 |
* |
| 136 |
* Results: |
| 137 |
* None. |
| 138 |
* |
| 139 |
* Side effects: |
| 140 |
* Creates a new window and creates an exit handler. |
| 141 |
* |
| 142 |
*---------------------------------------------------------------------- |
| 143 |
*/ |
| 144 |
|
| 145 |
static ThreadSpecificData * |
| 146 |
FileInit(void) |
| 147 |
{ |
| 148 |
ThreadSpecificData *tsdPtr = |
| 149 |
(ThreadSpecificData *)TclThreadDataKeyGet(&dataKey); |
| 150 |
|
| 151 |
if (tsdPtr == NULL) { |
| 152 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 153 |
tsdPtr->firstFilePtr = NULL; |
| 154 |
Tcl_CreateEventSource(FileSetupProc, FileCheckProc, NULL); |
| 155 |
Tcl_CreateThreadExitHandler(FileChannelExitHandler, NULL); |
| 156 |
} |
| 157 |
return tsdPtr; |
| 158 |
} |
| 159 |
|
| 160 |
/* |
| 161 |
*---------------------------------------------------------------------- |
| 162 |
* |
| 163 |
* FileChannelExitHandler -- |
| 164 |
* |
| 165 |
* This function is called to cleanup the channel driver before Tcl is |
| 166 |
* unloaded. |
| 167 |
* |
| 168 |
* Results: |
| 169 |
* None. |
| 170 |
* |
| 171 |
* Side effects: |
| 172 |
* Destroys the communication window. |
| 173 |
* |
| 174 |
*---------------------------------------------------------------------- |
| 175 |
*/ |
| 176 |
|
| 177 |
static void |
| 178 |
FileChannelExitHandler( |
| 179 |
ClientData clientData) /* Old window proc */ |
| 180 |
{ |
| 181 |
(void)clientData; |
| 182 |
|
| 183 |
Tcl_DeleteEventSource(FileSetupProc, FileCheckProc, NULL); |
| 184 |
} |
| 185 |
|
| 186 |
/* |
| 187 |
*---------------------------------------------------------------------- |
| 188 |
* |
| 189 |
* FileSetupProc -- |
| 190 |
* |
| 191 |
* This function is invoked before Tcl_DoOneEvent blocks waiting for an |
| 192 |
* event. |
| 193 |
* |
| 194 |
* Results: |
| 195 |
* None. |
| 196 |
* |
| 197 |
* Side effects: |
| 198 |
* Adjusts the block time if needed. |
| 199 |
* |
| 200 |
*---------------------------------------------------------------------- |
| 201 |
*/ |
| 202 |
|
| 203 |
void |
| 204 |
FileSetupProc( |
| 205 |
ClientData data, /* Not used. */ |
| 206 |
int flags) /* Event flags as passed to Tcl_DoOneEvent. */ |
| 207 |
{ |
| 208 |
FileInfo *infoPtr; |
| 209 |
Tcl_Time blockTime = { 0, 0 }; |
| 210 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 211 |
(void)data; |
| 212 |
|
| 213 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
/* |
| 218 |
* Check to see if there is a ready file. If so, poll. |
| 219 |
*/ |
| 220 |
|
| 221 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 222 |
infoPtr = infoPtr->nextPtr) { |
| 223 |
if (infoPtr->watchMask) { |
| 224 |
Tcl_SetMaxBlockTime(&blockTime); |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
/* |
| 231 |
*---------------------------------------------------------------------- |
| 232 |
* |
| 233 |
* FileCheckProc -- |
| 234 |
* |
| 235 |
* This function is called by Tcl_DoOneEvent to check the file event |
| 236 |
* source for events. |
| 237 |
* |
| 238 |
* Results: |
| 239 |
* None. |
| 240 |
* |
| 241 |
* Side effects: |
| 242 |
* May queue an event. |
| 243 |
* |
| 244 |
*---------------------------------------------------------------------- |
| 245 |
*/ |
| 246 |
|
| 247 |
static void |
| 248 |
FileCheckProc( |
| 249 |
ClientData data, /* Not used. */ |
| 250 |
int flags) /* Event flags as passed to Tcl_DoOneEvent. */ |
| 251 |
{ |
| 252 |
FileEvent *evPtr; |
| 253 |
FileInfo *infoPtr; |
| 254 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 255 |
(void)data; |
| 256 |
|
| 257 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 258 |
return; |
| 259 |
} |
| 260 |
|
| 261 |
/* |
| 262 |
* Queue events for any ready files that don't already have events queued |
| 263 |
* (caused by persistent states that won't generate WinSock events). |
| 264 |
*/ |
| 265 |
|
| 266 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 267 |
infoPtr = infoPtr->nextPtr) { |
| 268 |
if (infoPtr->watchMask && !(infoPtr->flags & FILE_PENDING)) { |
| 269 |
infoPtr->flags |= FILE_PENDING; |
| 270 |
evPtr = (FileEvent *)ckalloc(sizeof(FileEvent)); |
| 271 |
evPtr->header.proc = FileEventProc; |
| 272 |
evPtr->infoPtr = infoPtr; |
| 273 |
Tcl_QueueEvent((Tcl_Event *) evPtr, TCL_QUEUE_TAIL); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
/* |
| 279 |
*---------------------------------------------------------------------- |
| 280 |
* |
| 281 |
* FileEventProc -- |
| 282 |
* |
| 283 |
* This function is invoked by Tcl_ServiceEvent when a file event reaches |
| 284 |
* the front of the event queue. This function invokes Tcl_NotifyChannel |
| 285 |
* on the file. |
| 286 |
* |
| 287 |
* Results: |
| 288 |
* Returns 1 if the event was handled, meaning it should be removed from |
| 289 |
* the queue. Returns 0 if the event was not handled, meaning it should |
| 290 |
* stay on the queue. The only time the event isn't handled is if the |
| 291 |
* TCL_FILE_EVENTS flag bit isn't set. |
| 292 |
* |
| 293 |
* Side effects: |
| 294 |
* Whatever the notifier callback does. |
| 295 |
* |
| 296 |
*---------------------------------------------------------------------- |
| 297 |
*/ |
| 298 |
|
| 299 |
static int |
| 300 |
FileEventProc( |
| 301 |
Tcl_Event *evPtr, /* Event to service. */ |
| 302 |
int flags) /* Flags that indicate what events to handle, |
| 303 |
* such as TCL_FILE_EVENTS. */ |
| 304 |
{ |
| 305 |
FileEvent *fileEvPtr = (FileEvent *)evPtr; |
| 306 |
FileInfo *infoPtr; |
| 307 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 308 |
|
| 309 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 310 |
return 0; |
| 311 |
} |
| 312 |
|
| 313 |
/* |
| 314 |
* Search through the list of watched files for the one whose handle |
| 315 |
* matches the event. We do this rather than simply dereferencing the |
| 316 |
* handle in the event so that files can be deleted while the event is in |
| 317 |
* the queue. |
| 318 |
*/ |
| 319 |
|
| 320 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 321 |
infoPtr = infoPtr->nextPtr) { |
| 322 |
if (fileEvPtr->infoPtr == infoPtr) { |
| 323 |
infoPtr->flags &= ~(FILE_PENDING); |
| 324 |
Tcl_NotifyChannel(infoPtr->channel, infoPtr->watchMask); |
| 325 |
break; |
| 326 |
} |
| 327 |
} |
| 328 |
return 1; |
| 329 |
} |
| 330 |
|
| 331 |
/* |
| 332 |
*---------------------------------------------------------------------- |
| 333 |
* |
| 334 |
* FileBlockProc -- |
| 335 |
* |
| 336 |
* Set blocking or non-blocking mode on channel. |
| 337 |
* |
| 338 |
* Results: |
| 339 |
* 0 if successful, errno when failed. |
| 340 |
* |
| 341 |
* Side effects: |
| 342 |
* Sets the device into blocking or non-blocking mode. |
| 343 |
* |
| 344 |
*---------------------------------------------------------------------- |
| 345 |
*/ |
| 346 |
|
| 347 |
static int |
| 348 |
FileBlockProc( |
| 349 |
ClientData instanceData, /* Instance data for channel. */ |
| 350 |
int mode) /* TCL_MODE_BLOCKING or |
| 351 |
* TCL_MODE_NONBLOCKING. */ |
| 352 |
{ |
| 353 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 354 |
|
| 355 |
/* |
| 356 |
* Files on Windows can not be switched between blocking and nonblocking, |
| 357 |
* hence we have to emulate the behavior. This is done in the input |
| 358 |
* function by checking against a bit in the state. We set or unset the |
| 359 |
* bit here to cause the input function to emulate the correct behavior. |
| 360 |
*/ |
| 361 |
|
| 362 |
if (mode == TCL_MODE_NONBLOCKING) { |
| 363 |
infoPtr->flags |= FILE_ASYNC; |
| 364 |
} else { |
| 365 |
infoPtr->flags &= ~(FILE_ASYNC); |
| 366 |
} |
| 367 |
return 0; |
| 368 |
} |
| 369 |
|
| 370 |
/* |
| 371 |
*---------------------------------------------------------------------- |
| 372 |
* |
| 373 |
* FileCloseProc/FileClose2Proc -- |
| 374 |
* |
| 375 |
* Closes the IO channel. |
| 376 |
* |
| 377 |
* Results: |
| 378 |
* 0 if successful, the value of errno if failed. |
| 379 |
* |
| 380 |
* Side effects: |
| 381 |
* Closes the physical channel |
| 382 |
* |
| 383 |
*---------------------------------------------------------------------- |
| 384 |
*/ |
| 385 |
|
| 386 |
static int |
| 387 |
FileCloseProc( |
| 388 |
ClientData instanceData, /* Pointer to FileInfo structure. */ |
| 389 |
Tcl_Interp *interp) /* Not used. */ |
| 390 |
{ |
| 391 |
FileInfo *fileInfoPtr = (FileInfo *)instanceData; |
| 392 |
FileInfo *infoPtr; |
| 393 |
ThreadSpecificData *tsdPtr; |
| 394 |
int errorCode = 0; |
| 395 |
(void)interp; |
| 396 |
|
| 397 |
/* |
| 398 |
* Remove the file from the watch list. |
| 399 |
*/ |
| 400 |
|
| 401 |
FileWatchProc(instanceData, 0); |
| 402 |
|
| 403 |
/* |
| 404 |
* Don't close the Win32 handle if the handle is a standard channel during |
| 405 |
* the thread exit process. Otherwise, one thread may kill the stdio of |
| 406 |
* another. |
| 407 |
*/ |
| 408 |
|
| 409 |
if (!TclInThreadExit() |
| 410 |
|| ((GetStdHandle(STD_INPUT_HANDLE) != fileInfoPtr->handle) |
| 411 |
&& (GetStdHandle(STD_OUTPUT_HANDLE) != fileInfoPtr->handle) |
| 412 |
&& (GetStdHandle(STD_ERROR_HANDLE) != fileInfoPtr->handle))) { |
| 413 |
if (CloseHandle(fileInfoPtr->handle) == FALSE) { |
| 414 |
TclWinConvertError(GetLastError()); |
| 415 |
errorCode = errno; |
| 416 |
} |
| 417 |
} |
| 418 |
|
| 419 |
/* |
| 420 |
* See if this FileInfo* is still on the thread local list. |
| 421 |
*/ |
| 422 |
|
| 423 |
tsdPtr = TCL_TSD_INIT(&dataKey); |
| 424 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 425 |
infoPtr = infoPtr->nextPtr) { |
| 426 |
if (infoPtr == fileInfoPtr) { |
| 427 |
/* |
| 428 |
* This channel exists on the thread local list. It should have |
| 429 |
* been removed by an earlier Threadaction call, but do that now |
| 430 |
* since just deallocating fileInfoPtr would leave an deallocated |
| 431 |
* pointer on the thread local list. |
| 432 |
*/ |
| 433 |
|
| 434 |
FileThreadActionProc(fileInfoPtr,TCL_CHANNEL_THREAD_REMOVE); |
| 435 |
break; |
| 436 |
} |
| 437 |
} |
| 438 |
ckfree(fileInfoPtr); |
| 439 |
return errorCode; |
| 440 |
} |
| 441 |
|
| 442 |
static int |
| 443 |
FileClose2Proc( |
| 444 |
ClientData instanceData, /* Pointer to FileInfo structure. */ |
| 445 |
Tcl_Interp *interp, /* Not used. */ |
| 446 |
int flags) |
| 447 |
{ |
| 448 |
if ((flags & (TCL_CLOSE_READ | TCL_CLOSE_WRITE)) == 0) { |
| 449 |
return FileCloseProc(instanceData, interp); |
| 450 |
} |
| 451 |
return EINVAL; |
| 452 |
} |
| 453 |
|
| 454 |
/* |
| 455 |
*---------------------------------------------------------------------- |
| 456 |
* |
| 457 |
* FileSeekProc -- |
| 458 |
* |
| 459 |
* Seeks on a file-based channel. Returns the new position. |
| 460 |
* |
| 461 |
* Results: |
| 462 |
* -1 if failed, the new position if successful. If failed, it also sets |
| 463 |
* *errorCodePtr to the error code. |
| 464 |
* |
| 465 |
* Side effects: |
| 466 |
* Moves the location at which the channel will be accessed in future |
| 467 |
* operations. |
| 468 |
* |
| 469 |
*---------------------------------------------------------------------- |
| 470 |
*/ |
| 471 |
|
| 472 |
static int |
| 473 |
FileSeekProc( |
| 474 |
ClientData instanceData, /* File state. */ |
| 475 |
long offset, /* Offset to seek to. */ |
| 476 |
int mode, /* Relative to where should we seek? */ |
| 477 |
int *errorCodePtr) /* To store error code. */ |
| 478 |
{ |
| 479 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 480 |
LONG newPos, newPosHigh, oldPos, oldPosHigh; |
| 481 |
DWORD moveMethod; |
| 482 |
|
| 483 |
*errorCodePtr = 0; |
| 484 |
if (mode == SEEK_SET) { |
| 485 |
moveMethod = FILE_BEGIN; |
| 486 |
} else if (mode == SEEK_CUR) { |
| 487 |
moveMethod = FILE_CURRENT; |
| 488 |
} else { |
| 489 |
moveMethod = FILE_END; |
| 490 |
} |
| 491 |
|
| 492 |
/* |
| 493 |
* Save our current place in case we need to roll-back the seek. |
| 494 |
*/ |
| 495 |
|
| 496 |
oldPosHigh = 0; |
| 497 |
oldPos = (int)SetFilePointer(infoPtr->handle, 0, &oldPosHigh, FILE_CURRENT); |
| 498 |
if (oldPos == (LONG)INVALID_SET_FILE_POINTER) { |
| 499 |
DWORD winError = GetLastError(); |
| 500 |
|
| 501 |
if (winError != NO_ERROR) { |
| 502 |
TclWinConvertError(winError); |
| 503 |
*errorCodePtr = errno; |
| 504 |
return -1; |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
newPosHigh = (offset < 0 ? -1 : 0); |
| 509 |
newPos = (int)SetFilePointer(infoPtr->handle, offset, &newPosHigh, moveMethod); |
| 510 |
if (newPos == (LONG)INVALID_SET_FILE_POINTER) { |
| 511 |
DWORD winError = GetLastError(); |
| 512 |
|
| 513 |
if (winError != NO_ERROR) { |
| 514 |
TclWinConvertError(winError); |
| 515 |
*errorCodePtr = errno; |
| 516 |
return -1; |
| 517 |
} |
| 518 |
} |
| 519 |
|
| 520 |
/* |
| 521 |
* Check for expressability in our return type, and roll-back otherwise. |
| 522 |
*/ |
| 523 |
|
| 524 |
if (newPosHigh != 0) { |
| 525 |
*errorCodePtr = EOVERFLOW; |
| 526 |
SetFilePointer(infoPtr->handle, oldPos, &oldPosHigh, FILE_BEGIN); |
| 527 |
return -1; |
| 528 |
} |
| 529 |
return (int) newPos; |
| 530 |
} |
| 531 |
|
| 532 |
/* |
| 533 |
*---------------------------------------------------------------------- |
| 534 |
* |
| 535 |
* FileWideSeekProc -- |
| 536 |
* |
| 537 |
* Seeks on a file-based channel. Returns the new position. |
| 538 |
* |
| 539 |
* Results: |
| 540 |
* -1 if failed, the new position if successful. If failed, it also sets |
| 541 |
* *errorCodePtr to the error code. |
| 542 |
* |
| 543 |
* Side effects: |
| 544 |
* Moves the location at which the channel will be accessed in future |
| 545 |
* operations. |
| 546 |
* |
| 547 |
*---------------------------------------------------------------------- |
| 548 |
*/ |
| 549 |
|
| 550 |
static Tcl_WideInt |
| 551 |
FileWideSeekProc( |
| 552 |
ClientData instanceData, /* File state. */ |
| 553 |
Tcl_WideInt offset, /* Offset to seek to. */ |
| 554 |
int mode, /* Relative to where should we seek? */ |
| 555 |
int *errorCodePtr) /* To store error code. */ |
| 556 |
{ |
| 557 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 558 |
DWORD moveMethod; |
| 559 |
LONG newPos, newPosHigh; |
| 560 |
|
| 561 |
*errorCodePtr = 0; |
| 562 |
if (mode == SEEK_SET) { |
| 563 |
moveMethod = FILE_BEGIN; |
| 564 |
} else if (mode == SEEK_CUR) { |
| 565 |
moveMethod = FILE_CURRENT; |
| 566 |
} else { |
| 567 |
moveMethod = FILE_END; |
| 568 |
} |
| 569 |
|
| 570 |
newPosHigh = Tcl_WideAsLong(offset >> 32); |
| 571 |
newPos = (int)SetFilePointer(infoPtr->handle, Tcl_WideAsLong(offset), |
| 572 |
&newPosHigh, moveMethod); |
| 573 |
if (newPos == (LONG)INVALID_SET_FILE_POINTER) { |
| 574 |
DWORD winError = GetLastError(); |
| 575 |
|
| 576 |
if (winError != NO_ERROR) { |
| 577 |
TclWinConvertError(winError); |
| 578 |
*errorCodePtr = errno; |
| 579 |
return -1; |
| 580 |
} |
| 581 |
} |
| 582 |
return (((Tcl_WideInt)((unsigned)newPos)) | (Tcl_LongAsWide(newPosHigh) << 32)); |
| 583 |
} |
| 584 |
|
| 585 |
/* |
| 586 |
*---------------------------------------------------------------------- |
| 587 |
* |
| 588 |
* FileTruncateProc -- |
| 589 |
* |
| 590 |
* Truncates a file-based channel. Returns the error code. |
| 591 |
* |
| 592 |
* Results: |
| 593 |
* 0 if successful, POSIX-y error code if it failed. |
| 594 |
* |
| 595 |
* Side effects: |
| 596 |
* Truncates the file, may move file pointers too. |
| 597 |
* |
| 598 |
*---------------------------------------------------------------------- |
| 599 |
*/ |
| 600 |
|
| 601 |
static int |
| 602 |
FileTruncateProc( |
| 603 |
ClientData instanceData, /* File state. */ |
| 604 |
Tcl_WideInt length) /* Length to truncate at. */ |
| 605 |
{ |
| 606 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 607 |
LONG newPos, newPosHigh, oldPos, oldPosHigh; |
| 608 |
|
| 609 |
/* |
| 610 |
* Save where we were... |
| 611 |
*/ |
| 612 |
|
| 613 |
oldPosHigh = 0; |
| 614 |
oldPos = (int)SetFilePointer(infoPtr->handle, 0, &oldPosHigh, FILE_CURRENT); |
| 615 |
if (oldPos == (LONG)INVALID_SET_FILE_POINTER) { |
| 616 |
DWORD winError = GetLastError(); |
| 617 |
if (winError != NO_ERROR) { |
| 618 |
TclWinConvertError(winError); |
| 619 |
return errno; |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
/* |
| 624 |
* Move to where we want to truncate |
| 625 |
*/ |
| 626 |
|
| 627 |
newPosHigh = Tcl_WideAsLong(length >> 32); |
| 628 |
newPos = (int)SetFilePointer(infoPtr->handle, Tcl_WideAsLong(length), |
| 629 |
&newPosHigh, FILE_BEGIN); |
| 630 |
if (newPos == (LONG)INVALID_SET_FILE_POINTER) { |
| 631 |
DWORD winError = GetLastError(); |
| 632 |
if (winError != NO_ERROR) { |
| 633 |
TclWinConvertError(winError); |
| 634 |
return errno; |
| 635 |
} |
| 636 |
} |
| 637 |
|
| 638 |
/* |
| 639 |
* Perform the truncation (unlike POSIX ftruncate(), we needed to move to |
| 640 |
* the location to truncate at first). |
| 641 |
*/ |
| 642 |
|
| 643 |
if (!SetEndOfFile(infoPtr->handle)) { |
| 644 |
TclWinConvertError(GetLastError()); |
| 645 |
return errno; |
| 646 |
} |
| 647 |
|
| 648 |
/* |
| 649 |
* Move back. If this last step fails, we don't care; it's just a "best |
| 650 |
* effort" attempt to restore our file pointer to where it was. |
| 651 |
*/ |
| 652 |
|
| 653 |
SetFilePointer(infoPtr->handle, oldPos, &oldPosHigh, FILE_BEGIN); |
| 654 |
return 0; |
| 655 |
} |
| 656 |
|
| 657 |
/* |
| 658 |
*---------------------------------------------------------------------- |
| 659 |
* |
| 660 |
* FileInputProc -- |
| 661 |
* |
| 662 |
* Reads input from the IO channel into the buffer given. Returns count |
| 663 |
* of how many bytes were actually read, and an error indication. |
| 664 |
* |
| 665 |
* Results: |
| 666 |
* A count of how many bytes were read is returned and an error |
| 667 |
* indication is returned in an output argument. |
| 668 |
* |
| 669 |
* Side effects: |
| 670 |
* Reads input from the actual channel. |
| 671 |
* |
| 672 |
*---------------------------------------------------------------------- |
| 673 |
*/ |
| 674 |
|
| 675 |
static int |
| 676 |
FileInputProc( |
| 677 |
ClientData instanceData, /* File state. */ |
| 678 |
char *buf, /* Where to store data read. */ |
| 679 |
int bufSize, /* Num bytes available in buffer. */ |
| 680 |
int *errorCode) /* Where to store error code. */ |
| 681 |
{ |
| 682 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 683 |
DWORD bytesRead; |
| 684 |
|
| 685 |
*errorCode = 0; |
| 686 |
|
| 687 |
/* |
| 688 |
* TODO: This comment appears to be out of date. We *do* have a |
| 689 |
* console driver, over in tclWinConsole.c. After some Windows |
| 690 |
* developer confirms, this comment should be revised. |
| 691 |
* |
| 692 |
* Note that we will block on reads from a console buffer until a full |
| 693 |
* line has been entered. The only way I know of to get around this is to |
| 694 |
* write a console driver. We should probably do this at some point, but |
| 695 |
* for now, we just block. The same problem exists for files being read |
| 696 |
* over the network. |
| 697 |
*/ |
| 698 |
|
| 699 |
if (ReadFile(infoPtr->handle, (LPVOID) buf, (DWORD) bufSize, &bytesRead, |
| 700 |
(LPOVERLAPPED) NULL) != FALSE) { |
| 701 |
return (int)bytesRead; |
| 702 |
} |
| 703 |
|
| 704 |
TclWinConvertError(GetLastError()); |
| 705 |
*errorCode = errno; |
| 706 |
if (errno == EPIPE) { |
| 707 |
return 0; |
| 708 |
} |
| 709 |
return -1; |
| 710 |
} |
| 711 |
|
| 712 |
/* |
| 713 |
*---------------------------------------------------------------------- |
| 714 |
* |
| 715 |
* FileOutputProc -- |
| 716 |
* |
| 717 |
* Writes the given output on the IO channel. Returns count of how many |
| 718 |
* characters were actually written, and an error indication. |
| 719 |
* |
| 720 |
* Results: |
| 721 |
* A count of how many characters were written is returned and an error |
| 722 |
* indication is returned in an output argument. |
| 723 |
* |
| 724 |
* Side effects: |
| 725 |
* Writes output on the actual channel. |
| 726 |
* |
| 727 |
*---------------------------------------------------------------------- |
| 728 |
*/ |
| 729 |
|
| 730 |
static int |
| 731 |
FileOutputProc( |
| 732 |
ClientData instanceData, /* File state. */ |
| 733 |
const char *buf, /* The data buffer. */ |
| 734 |
int toWrite, /* How many bytes to write? */ |
| 735 |
int *errorCode) /* Where to store error code. */ |
| 736 |
{ |
| 737 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 738 |
DWORD bytesWritten; |
| 739 |
|
| 740 |
*errorCode = 0; |
| 741 |
|
| 742 |
/* |
| 743 |
* If we are writing to a file that was opened with O_APPEND, we need to |
| 744 |
* seek to the end of the file before writing the current buffer. |
| 745 |
*/ |
| 746 |
|
| 747 |
if (infoPtr->flags & FILE_APPEND) { |
| 748 |
SetFilePointer(infoPtr->handle, 0, NULL, FILE_END); |
| 749 |
} |
| 750 |
|
| 751 |
if (WriteFile(infoPtr->handle, (LPVOID) buf, (DWORD) toWrite, |
| 752 |
&bytesWritten, (LPOVERLAPPED) NULL) == FALSE) { |
| 753 |
TclWinConvertError(GetLastError()); |
| 754 |
*errorCode = errno; |
| 755 |
return -1; |
| 756 |
} |
| 757 |
infoPtr->dirty = 1; |
| 758 |
return (int)bytesWritten; |
| 759 |
} |
| 760 |
|
| 761 |
/* |
| 762 |
*---------------------------------------------------------------------- |
| 763 |
* |
| 764 |
* FileWatchProc -- |
| 765 |
* |
| 766 |
* Called by the notifier to set up to watch for events on this channel. |
| 767 |
* |
| 768 |
* Results: |
| 769 |
* None. |
| 770 |
* |
| 771 |
* Side effects: |
| 772 |
* None. |
| 773 |
* |
| 774 |
*---------------------------------------------------------------------- |
| 775 |
*/ |
| 776 |
|
| 777 |
static void |
| 778 |
FileWatchProc( |
| 779 |
ClientData instanceData, /* File state. */ |
| 780 |
int mask) /* What events to watch for; OR-ed combination |
| 781 |
* of TCL_READABLE, TCL_WRITABLE and |
| 782 |
* TCL_EXCEPTION. */ |
| 783 |
{ |
| 784 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 785 |
Tcl_Time blockTime = { 0, 0 }; |
| 786 |
|
| 787 |
/* |
| 788 |
* Since the file is always ready for events, we set the block time to |
| 789 |
* zero so we will poll. |
| 790 |
*/ |
| 791 |
|
| 792 |
infoPtr->watchMask = mask & infoPtr->validMask; |
| 793 |
if (infoPtr->watchMask) { |
| 794 |
Tcl_SetMaxBlockTime(&blockTime); |
| 795 |
} |
| 796 |
} |
| 797 |
|
| 798 |
/* |
| 799 |
*---------------------------------------------------------------------- |
| 800 |
* |
| 801 |
* FileGetHandleProc -- |
| 802 |
* |
| 803 |
* Called from Tcl_GetChannelHandle to retrieve OS handles from a file |
| 804 |
* based channel. |
| 805 |
* |
| 806 |
* Results: |
| 807 |
* Returns TCL_OK with the fd in handlePtr, or TCL_ERROR if there is no |
| 808 |
* handle for the specified direction. |
| 809 |
* |
| 810 |
* Side effects: |
| 811 |
* None. |
| 812 |
* |
| 813 |
*---------------------------------------------------------------------- |
| 814 |
*/ |
| 815 |
|
| 816 |
static int |
| 817 |
FileGetHandleProc( |
| 818 |
ClientData instanceData, /* The file state. */ |
| 819 |
int direction, /* TCL_READABLE or TCL_WRITABLE */ |
| 820 |
ClientData *handlePtr) /* Where to store the handle. */ |
| 821 |
{ |
| 822 |
FileInfo *infoPtr = (FileInfo *)instanceData; |
| 823 |
|
| 824 |
if (direction & infoPtr->validMask) { |
| 825 |
*handlePtr = (ClientData) infoPtr->handle; |
| 826 |
return TCL_OK; |
| 827 |
} else { |
| 828 |
return TCL_ERROR; |
| 829 |
} |
| 830 |
} |
| 831 |
|
| 832 |
/* |
| 833 |
*---------------------------------------------------------------------- |
| 834 |
* |
| 835 |
* TclpOpenFileChannel -- |
| 836 |
* |
| 837 |
* Open an File based channel on Unix systems. |
| 838 |
* |
| 839 |
* Results: |
| 840 |
* The new channel or NULL. If NULL, the output argument errorCodePtr is |
| 841 |
* set to a POSIX error. |
| 842 |
* |
| 843 |
* Side effects: |
| 844 |
* May open the channel and may cause creation of a file on the file |
| 845 |
* system. |
| 846 |
* |
| 847 |
*---------------------------------------------------------------------- |
| 848 |
*/ |
| 849 |
|
| 850 |
Tcl_Channel |
| 851 |
TclpOpenFileChannel( |
| 852 |
Tcl_Interp *interp, /* Interpreter for error reporting; can be |
| 853 |
* NULL. */ |
| 854 |
Tcl_Obj *pathPtr, /* Name of file to open. */ |
| 855 |
int mode, /* POSIX mode. */ |
| 856 |
int permissions) /* If the open involves creating a file, with |
| 857 |
* what modes to create it? */ |
| 858 |
{ |
| 859 |
Tcl_Channel channel = 0; |
| 860 |
int channelPermissions = 0; |
| 861 |
DWORD accessMode = 0, createMode, shareMode, flags; |
| 862 |
const WCHAR *nativeName; |
| 863 |
HANDLE handle; |
| 864 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 865 |
TclFile readFile = NULL, writeFile = NULL; |
| 866 |
|
| 867 |
nativeName = (const WCHAR *)Tcl_FSGetNativePath(pathPtr); |
| 868 |
if (nativeName == NULL) { |
| 869 |
if (interp != (Tcl_Interp *) NULL) { |
| 870 |
/* |
| 871 |
* We need this just to ensure we return the correct error messages under |
| 872 |
* some circumstances (relative paths only), so because the normalization |
| 873 |
* is very expensive, don't invoke it for native or absolute paths. |
| 874 |
* Note: since paths starting with ~ are absolute, it also considers tilde expansion, |
| 875 |
* (proper error message of tests *io-40.17 "tilde substitution in open") |
| 876 |
*/ |
| 877 |
if ( |
| 878 |
( |
| 879 |
( |
| 880 |
!TclFSCwdIsNative() && |
| 881 |
(Tcl_FSGetPathType(pathPtr) != TCL_PATH_ABSOLUTE) |
| 882 |
) || |
| 883 |
(*TclGetString(pathPtr) == '~') /* possible tilde expansion */ |
| 884 |
) && |
| 885 |
Tcl_FSGetNormalizedPath(interp, pathPtr) == NULL |
| 886 |
) { |
| 887 |
return NULL; |
| 888 |
} |
| 889 |
|
| 890 |
Tcl_AppendResult(interp, "couldn't open \"", |
| 891 |
TclGetString(pathPtr), "\": filename is invalid on this platform", |
| 892 |
NULL); |
| 893 |
} |
| 894 |
return NULL; |
| 895 |
} |
| 896 |
|
| 897 |
switch (mode & O_ACCMODE) { |
| 898 |
case O_RDONLY: |
| 899 |
accessMode = GENERIC_READ; |
| 900 |
channelPermissions = TCL_READABLE; |
| 901 |
break; |
| 902 |
case O_WRONLY: |
| 903 |
accessMode = GENERIC_WRITE; |
| 904 |
channelPermissions = TCL_WRITABLE; |
| 905 |
break; |
| 906 |
case O_RDWR: |
| 907 |
accessMode = (GENERIC_READ | GENERIC_WRITE); |
| 908 |
channelPermissions = (TCL_READABLE | TCL_WRITABLE); |
| 909 |
break; |
| 910 |
default: |
| 911 |
Tcl_Panic("TclpOpenFileChannel: invalid mode value"); |
| 912 |
break; |
| 913 |
} |
| 914 |
|
| 915 |
/* |
| 916 |
* Map the creation flags to the NT create mode. |
| 917 |
*/ |
| 918 |
|
| 919 |
switch (mode & (O_CREAT | O_EXCL | O_TRUNC)) { |
| 920 |
case (O_CREAT | O_EXCL): |
| 921 |
case (O_CREAT | O_EXCL | O_TRUNC): |
| 922 |
createMode = CREATE_NEW; |
| 923 |
break; |
| 924 |
case (O_CREAT | O_TRUNC): |
| 925 |
createMode = CREATE_ALWAYS; |
| 926 |
break; |
| 927 |
case O_CREAT: |
| 928 |
createMode = OPEN_ALWAYS; |
| 929 |
break; |
| 930 |
case O_TRUNC: |
| 931 |
case (O_TRUNC | O_EXCL): |
| 932 |
createMode = TRUNCATE_EXISTING; |
| 933 |
break; |
| 934 |
default: |
| 935 |
createMode = OPEN_EXISTING; |
| 936 |
break; |
| 937 |
} |
| 938 |
|
| 939 |
/* |
| 940 |
* [2413550] Avoid double-open of serial ports on Windows |
| 941 |
* Special handling for Windows serial ports by a "name-hint" |
| 942 |
* to directly open it with the OVERLAPPED flag set. |
| 943 |
*/ |
| 944 |
|
| 945 |
if( NativeIsComPort(nativeName) ) { |
| 946 |
|
| 947 |
handle = TclWinSerialOpen(INVALID_HANDLE_VALUE, nativeName, accessMode); |
| 948 |
if (handle == INVALID_HANDLE_VALUE) { |
| 949 |
TclWinConvertError(GetLastError()); |
| 950 |
if (interp != (Tcl_Interp *) NULL) { |
| 951 |
Tcl_AppendResult(interp, "couldn't open serial \"", |
| 952 |
TclGetString(pathPtr), "\": ", |
| 953 |
Tcl_PosixError(interp), NULL); |
| 954 |
} |
| 955 |
return NULL; |
| 956 |
} |
| 957 |
|
| 958 |
/* |
| 959 |
* For natively-named Windows serial ports we are done. |
| 960 |
*/ |
| 961 |
channel = TclWinOpenSerialChannel(handle, channelName, |
| 962 |
channelPermissions); |
| 963 |
|
| 964 |
return channel; |
| 965 |
} |
| 966 |
/* |
| 967 |
* If the file is being created, get the file attributes from the |
| 968 |
* permissions argument, else use the existing file attributes. |
| 969 |
*/ |
| 970 |
|
| 971 |
if (mode & O_CREAT) { |
| 972 |
if (permissions & S_IWRITE) { |
| 973 |
flags = FILE_ATTRIBUTE_NORMAL; |
| 974 |
} else { |
| 975 |
flags = FILE_ATTRIBUTE_READONLY; |
| 976 |
} |
| 977 |
} else { |
| 978 |
flags = GetFileAttributesW(nativeName); |
| 979 |
if (flags == 0xFFFFFFFF) { |
| 980 |
flags = 0; |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
/* |
| 985 |
* Set up the file sharing mode. We want to allow simultaneous access. |
| 986 |
*/ |
| 987 |
|
| 988 |
shareMode = FILE_SHARE_READ | FILE_SHARE_WRITE; |
| 989 |
|
| 990 |
/* |
| 991 |
* Now we get to create the file. |
| 992 |
*/ |
| 993 |
|
| 994 |
handle = CreateFileW(nativeName, accessMode, shareMode, |
| 995 |
NULL, createMode, flags, (HANDLE) NULL); |
| 996 |
|
| 997 |
if (handle == INVALID_HANDLE_VALUE) { |
| 998 |
DWORD err = GetLastError(); |
| 999 |
|
| 1000 |
if ((err & 0xFFFFL) == ERROR_OPEN_FAILED) { |
| 1001 |
err = (mode & O_CREAT) ? ERROR_FILE_EXISTS : ERROR_FILE_NOT_FOUND; |
| 1002 |
} |
| 1003 |
TclWinConvertError(err); |
| 1004 |
if (interp != (Tcl_Interp *) NULL) { |
| 1005 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1006 |
"couldn't open \"%s\": %s", |
| 1007 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1008 |
} |
| 1009 |
return NULL; |
| 1010 |
} |
| 1011 |
|
| 1012 |
channel = NULL; |
| 1013 |
|
| 1014 |
switch (FileGetType(handle)) { |
| 1015 |
case FILE_TYPE_SERIAL: |
| 1016 |
/* |
| 1017 |
* Natively named serial ports "com1-9", "\\\\.\\comXX" are |
| 1018 |
* already done with the code above. |
| 1019 |
* Here we handle all other serial port names. |
| 1020 |
* |
| 1021 |
* Reopen channel for OVERLAPPED operation. Normally this shouldn't |
| 1022 |
* fail, because the channel exists. |
| 1023 |
*/ |
| 1024 |
|
| 1025 |
handle = TclWinSerialOpen(handle, nativeName, accessMode); |
| 1026 |
if (handle == INVALID_HANDLE_VALUE) { |
| 1027 |
TclWinConvertError(GetLastError()); |
| 1028 |
if (interp != (Tcl_Interp *) NULL) { |
| 1029 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1030 |
"couldn't reopen serial \"%s\": %s", |
| 1031 |
TclGetString(pathPtr), Tcl_PosixError(interp))); |
| 1032 |
} |
| 1033 |
return NULL; |
| 1034 |
} |
| 1035 |
channel = TclWinOpenSerialChannel(handle, channelName, |
| 1036 |
channelPermissions); |
| 1037 |
break; |
| 1038 |
case FILE_TYPE_CONSOLE: |
| 1039 |
channel = TclWinOpenConsoleChannel(handle, channelName, |
| 1040 |
channelPermissions); |
| 1041 |
break; |
| 1042 |
case FILE_TYPE_PIPE: |
| 1043 |
if (channelPermissions & TCL_READABLE) { |
| 1044 |
readFile = TclWinMakeFile(handle); |
| 1045 |
} |
| 1046 |
if (channelPermissions & TCL_WRITABLE) { |
| 1047 |
writeFile = TclWinMakeFile(handle); |
| 1048 |
} |
| 1049 |
channel = TclpCreateCommandChannel(readFile, writeFile, NULL, 0, NULL); |
| 1050 |
break; |
| 1051 |
case FILE_TYPE_CHAR: |
| 1052 |
case FILE_TYPE_DISK: |
| 1053 |
case FILE_TYPE_UNKNOWN: |
| 1054 |
channel = OpenFileChannel(handle, channelName, |
| 1055 |
channelPermissions, (mode & O_APPEND) ? FILE_APPEND : 0); |
| 1056 |
break; |
| 1057 |
|
| 1058 |
default: |
| 1059 |
/* |
| 1060 |
* The handle is of an unknown type, probably /dev/nul equivalent or |
| 1061 |
* possibly a closed handle. |
| 1062 |
*/ |
| 1063 |
|
| 1064 |
channel = NULL; |
| 1065 |
Tcl_SetObjResult(interp, Tcl_ObjPrintf( |
| 1066 |
"couldn't open \"%s\": bad file type", |
| 1067 |
TclGetString(pathPtr))); |
| 1068 |
Tcl_SetErrorCode(interp, "TCL", "VALUE", "CHANNEL", "BAD_TYPE", |
| 1069 |
NULL); |
| 1070 |
break; |
| 1071 |
} |
| 1072 |
|
| 1073 |
return channel; |
| 1074 |
} |
| 1075 |
|
| 1076 |
/* |
| 1077 |
*---------------------------------------------------------------------- |
| 1078 |
* |
| 1079 |
* Tcl_MakeFileChannel -- |
| 1080 |
* |
| 1081 |
* Creates a Tcl_Channel from an existing platform specific file handle. |
| 1082 |
* |
| 1083 |
* Results: |
| 1084 |
* The Tcl_Channel created around the preexisting file. |
| 1085 |
* |
| 1086 |
* Side effects: |
| 1087 |
* None. |
| 1088 |
* |
| 1089 |
*---------------------------------------------------------------------- |
| 1090 |
*/ |
| 1091 |
|
| 1092 |
Tcl_Channel |
| 1093 |
Tcl_MakeFileChannel( |
| 1094 |
ClientData rawHandle, /* OS level handle */ |
| 1095 |
int mode) /* OR'ed combination of TCL_READABLE and |
| 1096 |
* TCL_WRITABLE to indicate file mode. */ |
| 1097 |
{ |
| 1098 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) && !defined(__clang__) |
| 1099 |
TCLEXCEPTION_REGISTRATION registration; |
| 1100 |
#endif |
| 1101 |
char channelName[16 + TCL_INTEGER_SPACE]; |
| 1102 |
Tcl_Channel channel = NULL; |
| 1103 |
HANDLE handle = (HANDLE) rawHandle; |
| 1104 |
HANDLE dupedHandle; |
| 1105 |
TclFile readFile = NULL, writeFile = NULL; |
| 1106 |
BOOL result; |
| 1107 |
|
| 1108 |
if (mode == 0) { |
| 1109 |
return NULL; |
| 1110 |
} |
| 1111 |
|
| 1112 |
switch (FileGetType(handle)) { |
| 1113 |
case FILE_TYPE_SERIAL: |
| 1114 |
channel = TclWinOpenSerialChannel(handle, channelName, mode); |
| 1115 |
break; |
| 1116 |
case FILE_TYPE_CONSOLE: |
| 1117 |
channel = TclWinOpenConsoleChannel(handle, channelName, mode); |
| 1118 |
break; |
| 1119 |
case FILE_TYPE_PIPE: |
| 1120 |
if (mode & TCL_READABLE) { |
| 1121 |
readFile = TclWinMakeFile(handle); |
| 1122 |
} |
| 1123 |
if (mode & TCL_WRITABLE) { |
| 1124 |
writeFile = TclWinMakeFile(handle); |
| 1125 |
} |
| 1126 |
channel = TclpCreateCommandChannel(readFile, writeFile, NULL, 0, NULL); |
| 1127 |
break; |
| 1128 |
|
| 1129 |
case FILE_TYPE_DISK: |
| 1130 |
case FILE_TYPE_CHAR: |
| 1131 |
channel = OpenFileChannel(handle, channelName, mode, 0); |
| 1132 |
break; |
| 1133 |
|
| 1134 |
case FILE_TYPE_UNKNOWN: |
| 1135 |
default: |
| 1136 |
/* |
| 1137 |
* The handle is of an unknown type. Test the validity of this OS |
| 1138 |
* handle by duplicating it, then closing the dupe. The Win32 API |
| 1139 |
* doesn't provide an IsValidHandle() function, so we have to emulate |
| 1140 |
* it here. This test will not work on a console handle reliably, |
| 1141 |
* which is why we can't test every handle that comes into this |
| 1142 |
* function in this way. |
| 1143 |
*/ |
| 1144 |
|
| 1145 |
result = DuplicateHandle(GetCurrentProcess(), handle, |
| 1146 |
GetCurrentProcess(), &dupedHandle, 0, FALSE, |
| 1147 |
DUPLICATE_SAME_ACCESS); |
| 1148 |
|
| 1149 |
if (result == 0) { |
| 1150 |
/* |
| 1151 |
* Unable to make a duplicate. It's definitely invalid at this |
| 1152 |
* point. |
| 1153 |
*/ |
| 1154 |
|
| 1155 |
return NULL; |
| 1156 |
} |
| 1157 |
|
| 1158 |
/* |
| 1159 |
* Use structured exception handling (Win32 SEH) to protect the close |
| 1160 |
* of this duped handle which might throw EXCEPTION_INVALID_HANDLE. |
| 1161 |
*/ |
| 1162 |
|
| 1163 |
result = 0; |
| 1164 |
#if defined(HAVE_NO_SEH) && !defined(_WIN64) && !defined(__clang__) |
| 1165 |
/* |
| 1166 |
* Don't have SEH available, do things the hard way. Note that this |
| 1167 |
* needs to be one block of asm, to avoid stack imbalance; also, it is |
| 1168 |
* illegal for one asm block to contain a jump to another. |
| 1169 |
*/ |
| 1170 |
|
| 1171 |
__asm__ __volatile__ ( |
| 1172 |
|
| 1173 |
/* |
| 1174 |
* Pick up parameters before messing with the stack |
| 1175 |
*/ |
| 1176 |
|
| 1177 |
"movl %[dupedHandle], %%ebx" "\n\t" |
| 1178 |
|
| 1179 |
/* |
| 1180 |
* Construct an TCLEXCEPTION_REGISTRATION to protect the call to |
| 1181 |
* CloseHandle. |
| 1182 |
*/ |
| 1183 |
|
| 1184 |
"leal %[registration], %%edx" "\n\t" |
| 1185 |
"movl %%fs:0, %%eax" "\n\t" |
| 1186 |
"movl %%eax, 0x0(%%edx)" "\n\t" /* link */ |
| 1187 |
"leal 1f, %%eax" "\n\t" |
| 1188 |
"movl %%eax, 0x4(%%edx)" "\n\t" /* handler */ |
| 1189 |
"movl %%ebp, 0x8(%%edx)" "\n\t" /* ebp */ |
| 1190 |
"movl %%esp, 0xC(%%edx)" "\n\t" /* esp */ |
| 1191 |
"movl $0, 0x10(%%edx)" "\n\t" /* status */ |
| 1192 |
|
| 1193 |
/* |
| 1194 |
* Link the TCLEXCEPTION_REGISTRATION on the chain. |
| 1195 |
*/ |
| 1196 |
|
| 1197 |
"movl %%edx, %%fs:0" "\n\t" |
| 1198 |
|
| 1199 |
/* |
| 1200 |
* Call CloseHandle(dupedHandle). |
| 1201 |
*/ |
| 1202 |
|
| 1203 |
"pushl %%ebx" "\n\t" |
| 1204 |
"call _CloseHandle@4" "\n\t" |
| 1205 |
|
| 1206 |
/* |
| 1207 |
* Come here on normal exit. Recover the TCLEXCEPTION_REGISTRATION |
| 1208 |
* and put a TRUE status return into it. |
| 1209 |
*/ |
| 1210 |
|
| 1211 |
"movl %%fs:0, %%edx" "\n\t" |
| 1212 |
"movl $1, %%eax" "\n\t" |
| 1213 |
"movl %%eax, 0x10(%%edx)" "\n\t" |
| 1214 |
"jmp 2f" "\n" |
| 1215 |
|
| 1216 |
/* |
| 1217 |
* Come here on an exception. Recover the TCLEXCEPTION_REGISTRATION |
| 1218 |
*/ |
| 1219 |
|
| 1220 |
"1:" "\t" |
| 1221 |
"movl %%fs:0, %%edx" "\n\t" |
| 1222 |
"movl 0x8(%%edx), %%edx" "\n\t" |
| 1223 |
|
| 1224 |
/* |
| 1225 |
* Come here however we exited. Restore context from the |
| 1226 |
* TCLEXCEPTION_REGISTRATION in case the stack is unbalanced. |
| 1227 |
*/ |
| 1228 |
|
| 1229 |
"2:" "\t" |
| 1230 |
"movl 0xC(%%edx), %%esp" "\n\t" |
| 1231 |
"movl 0x8(%%edx), %%ebp" "\n\t" |
| 1232 |
"movl 0x0(%%edx), %%eax" "\n\t" |
| 1233 |
"movl %%eax, %%fs:0" "\n\t" |
| 1234 |
|
| 1235 |
: |
| 1236 |
/* No outputs */ |
| 1237 |
: |
| 1238 |
[registration] "m" (registration), |
| 1239 |
[dupedHandle] "m" (dupedHandle) |
| 1240 |
: |
| 1241 |
"%eax", "%ebx", "%ecx", "%edx", "%esi", "%edi", "memory" |
| 1242 |
); |
| 1243 |
result = registration.status; |
| 1244 |
#else |
| 1245 |
#ifndef HAVE_NO_SEH |
| 1246 |
__try { |
| 1247 |
#endif |
| 1248 |
CloseHandle(dupedHandle); |
| 1249 |
result = 1; |
| 1250 |
#ifndef HAVE_NO_SEH |
| 1251 |
} __except (EXCEPTION_EXECUTE_HANDLER) {} |
| 1252 |
#endif |
| 1253 |
#endif |
| 1254 |
if (result == FALSE) { |
| 1255 |
return NULL; |
| 1256 |
} |
| 1257 |
|
| 1258 |
/* |
| 1259 |
* Fall through, the handle is valid. |
| 1260 |
* |
| 1261 |
* Create the undefined channel, anyways, because we know the handle |
| 1262 |
* is valid to something. |
| 1263 |
*/ |
| 1264 |
|
| 1265 |
channel = OpenFileChannel(handle, channelName, mode, 0); |
| 1266 |
} |
| 1267 |
|
| 1268 |
return channel; |
| 1269 |
} |
| 1270 |
|
| 1271 |
/* |
| 1272 |
*---------------------------------------------------------------------- |
| 1273 |
* |
| 1274 |
* TclpGetDefaultStdChannel -- |
| 1275 |
* |
| 1276 |
* Constructs a channel for the specified standard OS handle. |
| 1277 |
* |
| 1278 |
* Results: |
| 1279 |
* Returns the specified default standard channel, or NULL. |
| 1280 |
* |
| 1281 |
* Side effects: |
| 1282 |
* May cause the creation of a standard channel and the underlying file. |
| 1283 |
* |
| 1284 |
*---------------------------------------------------------------------- |
| 1285 |
*/ |
| 1286 |
|
| 1287 |
Tcl_Channel |
| 1288 |
TclpGetDefaultStdChannel( |
| 1289 |
int type) /* One of TCL_STDIN, TCL_STDOUT, or |
| 1290 |
* TCL_STDERR. */ |
| 1291 |
{ |
| 1292 |
Tcl_Channel channel; |
| 1293 |
HANDLE handle; |
| 1294 |
int mode = -1; |
| 1295 |
const char *bufMode = NULL; |
| 1296 |
DWORD handleId = (DWORD) -1; |
| 1297 |
/* Standard handle to retrieve. */ |
| 1298 |
|
| 1299 |
switch (type) { |
| 1300 |
case TCL_STDIN: |
| 1301 |
handleId = STD_INPUT_HANDLE; |
| 1302 |
mode = TCL_READABLE; |
| 1303 |
bufMode = "line"; |
| 1304 |
break; |
| 1305 |
case TCL_STDOUT: |
| 1306 |
handleId = STD_OUTPUT_HANDLE; |
| 1307 |
mode = TCL_WRITABLE; |
| 1308 |
bufMode = "line"; |
| 1309 |
break; |
| 1310 |
case TCL_STDERR: |
| 1311 |
handleId = STD_ERROR_HANDLE; |
| 1312 |
mode = TCL_WRITABLE; |
| 1313 |
bufMode = "none"; |
| 1314 |
break; |
| 1315 |
default: |
| 1316 |
Tcl_Panic("TclGetDefaultStdChannel: Unexpected channel type"); |
| 1317 |
break; |
| 1318 |
} |
| 1319 |
|
| 1320 |
handle = GetStdHandle(handleId); |
| 1321 |
|
| 1322 |
/* |
| 1323 |
* Note that we need to check for 0 because Windows may return 0 if this |
| 1324 |
* is not a console mode application, even though this is not a valid |
| 1325 |
* handle. |
| 1326 |
*/ |
| 1327 |
|
| 1328 |
if ((handle == INVALID_HANDLE_VALUE) || (handle == 0)) { |
| 1329 |
return (Tcl_Channel) NULL; |
| 1330 |
} |
| 1331 |
|
| 1332 |
channel = Tcl_MakeFileChannel(handle, mode); |
| 1333 |
|
| 1334 |
if (channel == NULL) { |
| 1335 |
return (Tcl_Channel) NULL; |
| 1336 |
} |
| 1337 |
|
| 1338 |
/* |
| 1339 |
* Set up the normal channel options for stdio handles. |
| 1340 |
*/ |
| 1341 |
|
| 1342 |
if (Tcl_SetChannelOption(NULL,channel,"-translation","auto")!=TCL_OK || |
| 1343 |
Tcl_SetChannelOption(NULL,channel,"-eofchar","\032 {}")!=TCL_OK || |
| 1344 |
Tcl_SetChannelOption(NULL,channel,"-buffering",bufMode)!=TCL_OK) { |
| 1345 |
Tcl_Close(NULL, channel); |
| 1346 |
return (Tcl_Channel) NULL; |
| 1347 |
} |
| 1348 |
return channel; |
| 1349 |
} |
| 1350 |
|
| 1351 |
/* |
| 1352 |
*---------------------------------------------------------------------- |
| 1353 |
* |
| 1354 |
* OpenFileChannel -- |
| 1355 |
* |
| 1356 |
* Constructs a File channel for the specified standard OS handle. This |
| 1357 |
* is a helper function to break up the construction of channels into |
| 1358 |
* File, Console, or Serial. |
| 1359 |
* |
| 1360 |
* Results: |
| 1361 |
* Returns the new channel, or NULL. |
| 1362 |
* |
| 1363 |
* Side effects: |
| 1364 |
* May open the channel and may cause creation of a file on the file |
| 1365 |
* system. |
| 1366 |
* |
| 1367 |
*---------------------------------------------------------------------- |
| 1368 |
*/ |
| 1369 |
|
| 1370 |
Tcl_Channel |
| 1371 |
OpenFileChannel( |
| 1372 |
HANDLE handle, /* Win32 HANDLE to swallow */ |
| 1373 |
char *channelName, /* Buffer to receive channel name */ |
| 1374 |
int permissions, /* OR'ed combination of TCL_READABLE, |
| 1375 |
* TCL_WRITABLE, or TCL_EXCEPTION, indicating |
| 1376 |
* which operations are valid on the file. */ |
| 1377 |
int appendMode) /* OR'ed combination of bits indicating what |
| 1378 |
* additional configuration of the channel is |
| 1379 |
* present. */ |
| 1380 |
{ |
| 1381 |
FileInfo *infoPtr; |
| 1382 |
ThreadSpecificData *tsdPtr = FileInit(); |
| 1383 |
|
| 1384 |
/* |
| 1385 |
* See if a channel with this handle already exists. |
| 1386 |
*/ |
| 1387 |
|
| 1388 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 1389 |
infoPtr = infoPtr->nextPtr) { |
| 1390 |
if (infoPtr->handle == (HANDLE) handle) { |
| 1391 |
return (permissions==infoPtr->validMask) ? infoPtr->channel : NULL; |
| 1392 |
} |
| 1393 |
} |
| 1394 |
|
| 1395 |
infoPtr = (FileInfo *)ckalloc(sizeof(FileInfo)); |
| 1396 |
|
| 1397 |
/* |
| 1398 |
* TIP #218. Removed the code inserting the new structure into the global |
| 1399 |
* list. This is now handled in the thread action callbacks, and only |
| 1400 |
* there. |
| 1401 |
*/ |
| 1402 |
|
| 1403 |
infoPtr->nextPtr = NULL; |
| 1404 |
infoPtr->validMask = permissions; |
| 1405 |
infoPtr->watchMask = 0; |
| 1406 |
infoPtr->flags = appendMode; |
| 1407 |
infoPtr->handle = handle; |
| 1408 |
infoPtr->dirty = 0; |
| 1409 |
snprintf(channelName, 16 + TCL_INTEGER_SPACE, "file%" TCL_Z_MODIFIER "x", (size_t) infoPtr); |
| 1410 |
|
| 1411 |
infoPtr->channel = Tcl_CreateChannel(&fileChannelType, channelName, |
| 1412 |
infoPtr, permissions); |
| 1413 |
|
| 1414 |
/* |
| 1415 |
* Files have default translation of AUTO and ^Z eof char, which means |
| 1416 |
* that a ^Z will be accepted as EOF when reading. |
| 1417 |
*/ |
| 1418 |
|
| 1419 |
Tcl_SetChannelOption(NULL, infoPtr->channel, "-translation", "auto"); |
| 1420 |
Tcl_SetChannelOption(NULL, infoPtr->channel, "-eofchar", "\032 {}"); |
| 1421 |
|
| 1422 |
return infoPtr->channel; |
| 1423 |
} |
| 1424 |
|
| 1425 |
/* |
| 1426 |
*---------------------------------------------------------------------- |
| 1427 |
* |
| 1428 |
* TclWinFlushDirtyChannels -- |
| 1429 |
* |
| 1430 |
* Flush all dirty channels to disk, so that requesting the size of any |
| 1431 |
* file returns the correct value. |
| 1432 |
* |
| 1433 |
* Results: |
| 1434 |
* None. |
| 1435 |
* |
| 1436 |
* Side effects: |
| 1437 |
* Information is actually written to disk now, rather than later. Don't |
| 1438 |
* call this too often, or there will be a performance hit (i.e. only |
| 1439 |
* call when we need to ask for the size of a file). |
| 1440 |
* |
| 1441 |
*---------------------------------------------------------------------- |
| 1442 |
*/ |
| 1443 |
|
| 1444 |
void |
| 1445 |
TclWinFlushDirtyChannels(void) |
| 1446 |
{ |
| 1447 |
FileInfo *infoPtr; |
| 1448 |
ThreadSpecificData *tsdPtr = FileInit(); |
| 1449 |
|
| 1450 |
/* |
| 1451 |
* Flush all channels which are dirty, i.e. may have data pending in the |
| 1452 |
* OS. |
| 1453 |
*/ |
| 1454 |
|
| 1455 |
for (infoPtr = tsdPtr->firstFilePtr; infoPtr != NULL; |
| 1456 |
infoPtr = infoPtr->nextPtr) { |
| 1457 |
if (infoPtr->dirty) { |
| 1458 |
FlushFileBuffers(infoPtr->handle); |
| 1459 |
infoPtr->dirty = 0; |
| 1460 |
} |
| 1461 |
} |
| 1462 |
} |
| 1463 |
|
| 1464 |
/* |
| 1465 |
*---------------------------------------------------------------------- |
| 1466 |
* |
| 1467 |
* FileThreadActionProc -- |
| 1468 |
* |
| 1469 |
* Insert or remove any thread local refs to this channel. |
| 1470 |
* |
| 1471 |
* Results: |
| 1472 |
* None. |
| 1473 |
* |
| 1474 |
* Side effects: |
| 1475 |
* Changes thread local list of valid channels. |
| 1476 |
* |
| 1477 |
*---------------------------------------------------------------------- |
| 1478 |
*/ |
| 1479 |
|
| 1480 |
static void |
| 1481 |
FileThreadActionProc( |
| 1482 |
ClientData instanceData, |
| 1483 |
int action) |
| 1484 |
{ |
| 1485 |
ThreadSpecificData *tsdPtr = TCL_TSD_INIT(&dataKey); |
| 1486 |
FileInfo *infoPtr = ( FileInfo *)instanceData; |
| 1487 |
|
| 1488 |
if (action == TCL_CHANNEL_THREAD_INSERT) { |
| 1489 |
infoPtr->nextPtr = tsdPtr->firstFilePtr; |
| 1490 |
tsdPtr->firstFilePtr = infoPtr; |
| 1491 |
} else { |
| 1492 |
FileInfo **nextPtrPtr; |
| 1493 |
int removed = 0; |
| 1494 |
|
| 1495 |
for (nextPtrPtr = &(tsdPtr->firstFilePtr); (*nextPtrPtr) != NULL; |
| 1496 |
nextPtrPtr = &((*nextPtrPtr)->nextPtr)) { |
| 1497 |
if ((*nextPtrPtr) == infoPtr) { |
| 1498 |
(*nextPtrPtr) = infoPtr->nextPtr; |
| 1499 |
removed = 1; |
| 1500 |
break; |
| 1501 |
} |
| 1502 |
} |
| 1503 |
|
| 1504 |
/* |
| 1505 |
* This could happen if the channel was created in one thread and then |
| 1506 |
* moved to another without updating the thread local data in each |
| 1507 |
* thread. |
| 1508 |
*/ |
| 1509 |
|
| 1510 |
if (!removed) { |
| 1511 |
Tcl_Panic("file info ptr not on thread channel list"); |
| 1512 |
} |
| 1513 |
} |
| 1514 |
} |
| 1515 |
|
| 1516 |
/* |
| 1517 |
*---------------------------------------------------------------------- |
| 1518 |
* |
| 1519 |
* FileGetType -- |
| 1520 |
* |
| 1521 |
* Given a file handle, return its type |
| 1522 |
* |
| 1523 |
* Results: |
| 1524 |
* None. |
| 1525 |
* |
| 1526 |
* Side effects: |
| 1527 |
* None. |
| 1528 |
* |
| 1529 |
*---------------------------------------------------------------------- |
| 1530 |
*/ |
| 1531 |
|
| 1532 |
DWORD |
| 1533 |
FileGetType( |
| 1534 |
HANDLE handle) /* Opened file handle */ |
| 1535 |
{ |
| 1536 |
DWORD type; |
| 1537 |
|
| 1538 |
type = GetFileType(handle); |
| 1539 |
|
| 1540 |
/* |
| 1541 |
* If the file is a character device, we need to try to figure out whether |
| 1542 |
* it is a serial port, a console, or something else. We test for the |
| 1543 |
* console case first because this is more common. |
| 1544 |
*/ |
| 1545 |
|
| 1546 |
if ((type == FILE_TYPE_CHAR) |
| 1547 |
|| ((type == FILE_TYPE_UNKNOWN) && !GetLastError())) { |
| 1548 |
DWORD consoleParams; |
| 1549 |
|
| 1550 |
if (GetConsoleMode(handle, &consoleParams)) { |
| 1551 |
type = FILE_TYPE_CONSOLE; |
| 1552 |
} else { |
| 1553 |
DCB dcb; |
| 1554 |
|
| 1555 |
dcb.DCBlength = sizeof(DCB); |
| 1556 |
if (GetCommState(handle, &dcb)) { |
| 1557 |
type = FILE_TYPE_SERIAL; |
| 1558 |
} |
| 1559 |
} |
| 1560 |
} |
| 1561 |
|
| 1562 |
return type; |
| 1563 |
} |
| 1564 |
|
| 1565 |
/* |
| 1566 |
*---------------------------------------------------------------------- |
| 1567 |
* |
| 1568 |
* NativeIsComPort -- |
| 1569 |
* |
| 1570 |
* Determines if a path refers to a Windows serial port. |
| 1571 |
* A simple and efficient solution is to use a "name hint" to detect |
| 1572 |
* COM ports by their filename instead of resorting to a syscall |
| 1573 |
* to detect serialness after the fact. |
| 1574 |
* The following patterns cover common serial port names: |
| 1575 |
* COM[1-9] |
| 1576 |
* \\.\COM[0-9]+ |
| 1577 |
* |
| 1578 |
* Results: |
| 1579 |
* 1 = serial port, 0 = not. |
| 1580 |
* |
| 1581 |
*---------------------------------------------------------------------- |
| 1582 |
*/ |
| 1583 |
|
| 1584 |
static int |
| 1585 |
NativeIsComPort( |
| 1586 |
const WCHAR *nativePath) /* Path of file to access, native encoding. */ |
| 1587 |
{ |
| 1588 |
const WCHAR *p = (const WCHAR *) nativePath; |
| 1589 |
int i, len = (int)wcslen(p); |
| 1590 |
|
| 1591 |
/* |
| 1592 |
* 1. Look for com[1-9]:? |
| 1593 |
*/ |
| 1594 |
|
| 1595 |
if ( (len == 4) && (_wcsnicmp(p, L"com", 3) == 0) ) { |
| 1596 |
/* |
| 1597 |
* The 4th character must be a digit 1..9 |
| 1598 |
*/ |
| 1599 |
|
| 1600 |
if ( (p[3] < L'1') || (p[3] > L'9') ) { |
| 1601 |
return 0; |
| 1602 |
} |
| 1603 |
return 1; |
| 1604 |
} |
| 1605 |
|
| 1606 |
/* |
| 1607 |
* 2. Look for \\.\com[0-9]+ |
| 1608 |
*/ |
| 1609 |
|
| 1610 |
if ((len >= 8) && (_wcsnicmp(p, L"\\\\.\\com", 7) == 0)) { |
| 1611 |
/* |
| 1612 |
* Charaters 8..end must be a digits 0..9 |
| 1613 |
*/ |
| 1614 |
|
| 1615 |
for ( i=7; i<len; i++ ) { |
| 1616 |
if ( (p[i] < '0') || (p[i] > '9') ) { |
| 1617 |
return 0; |
| 1618 |
} |
| 1619 |
} |
| 1620 |
return 1; |
| 1621 |
} |
| 1622 |
return 0; |
| 1623 |
} |
| 1624 |
|
| 1625 |
/* |
| 1626 |
* Local Variables: |
| 1627 |
* mode: c |
| 1628 |
* c-basic-offset: 4 |
| 1629 |
* fill-column: 78 |
| 1630 |
* End: |
| 1631 |
*/ |