| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
#include "bltInt.h" |
| 37 |
#include "bltChain.h" |
| 38 |
#include <fcntl.h> |
| 39 |
|
| 40 |
#define PEEK_DEBUG 0 |
| 41 |
#define QUEUE_DEBUG 0 |
| 42 |
#define READER_DEBUG 0 |
| 43 |
#define ASYNC_DEBUG 0 |
| 44 |
#define KILL_DEBUG 0 |
| 45 |
|
| 46 |
typedef struct { |
| 47 |
DWORD pid; |
| 48 |
HANDLE hProcess; |
| 49 |
} Process; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
typedef enum ApplicationTypes { |
| 56 |
APPL_NONE, APPL_DOS, APPL_WIN3X, APPL_WIN32, APPL_INTERP |
| 57 |
} ApplicationType; |
| 58 |
|
| 59 |
#ifndef IMAGE_OS2_SIGNATURE |
| 60 |
# define IMAGE_OS2_SIGNATURE (0x454E) |
| 61 |
#endif |
| 62 |
#ifndef IMAGE_VXD_SIGNATURE |
| 63 |
# define IMAGE_VXD_SIGNATURE (0x454C) |
| 64 |
#endif |
| 65 |
|
| 66 |
#define PIPE_BUFSIZ (BUFSIZ*2) |
| 67 |
|
| 68 |
#define PIPE_PENDING (1<<13) |
| 69 |
#define PIPE_EOF (1<<14) |
| 70 |
#define PIPE_DELETED (1<<15) |
| 71 |
|
| 72 |
|
| 73 |
typedef struct { |
| 74 |
int flags; |
| 75 |
HANDLE hPipe; |
| 76 |
HANDLE thread; |
| 77 |
HANDLE parent; |
| 78 |
DWORD parentId; |
| 79 |
HWND hWindow; |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
HANDLE idleEvent; |
| 84 |
|
| 85 |
HANDLE readyEvent; |
| 86 |
|
| 87 |
|
| 88 |
DWORD lastError; |
| 89 |
|
| 90 |
char *buffer; |
| 91 |
size_t start, end; |
| 92 |
size_t size; |
| 93 |
|
| 94 |
Tcl_FileProc *proc; |
| 95 |
ClientData clientData; |
| 96 |
|
| 97 |
} PipeHandler; |
| 98 |
|
| 99 |
|
| 100 |
typedef struct { |
| 101 |
Tcl_Event header; |
| 102 |
|
| 103 |
|
| 104 |
PipeHandler *pipePtr; |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
} PipeEvent; |
| 109 |
|
| 110 |
static int initialized = 0; |
| 111 |
static Blt_Chain pipeChain; |
| 112 |
static CRITICAL_SECTION pipeCriticalSection; |
| 113 |
|
| 114 |
static DWORD WINAPI PipeWriterThread(void *clientData); |
| 115 |
static DWORD WINAPI PipeReaderThread(void *clientData); |
| 116 |
|
| 117 |
static Tcl_FreeProc DestroyPipe; |
| 118 |
|
| 119 |
extern void Blt_MapPid(HANDLE hProcess, DWORD pid); |
| 120 |
extern HINSTANCE TclWinGetTclInstance(void); |
| 121 |
extern void TclWinConvertError(DWORD lastError); |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static LRESULT CALLBACK |
| 153 |
NotifierWindowProc( |
| 154 |
HWND hWindow, |
| 155 |
UINT message, |
| 156 |
WPARAM wParam, |
| 157 |
LPARAM lParam) |
| 158 |
{ |
| 159 |
switch (message) { |
| 160 |
case WM_USER: |
| 161 |
case WM_TIMER: |
| 162 |
break; |
| 163 |
|
| 164 |
default: |
| 165 |
return DefWindowProc(hWindow, message, wParam, lParam); |
| 166 |
} |
| 167 |
|
| 168 |
Tcl_ServiceAll(); |
| 169 |
return 0; |
| 170 |
} |
| 171 |
|
| 172 |
static void |
| 173 |
WakeupNotifier(HWND hWindow) |
| 174 |
{ |
| 175 |
PostMessage(hWindow, WM_USER, 0, 0); |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
static HWND |
| 195 |
GetNotifierWindow(void) |
| 196 |
{ |
| 197 |
static HWND hWindow = NULL; |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
if (hWindow == NULL) { |
| 203 |
WNDCLASS class; |
| 204 |
HINSTANCE hInstance; |
| 205 |
|
| 206 |
memset(&class, 0, sizeof(WNDCLASS)); |
| 207 |
hInstance = TclWinGetTclInstance(); |
| 208 |
class.hInstance = hInstance; |
| 209 |
class.lpszClassName = "PipeNotifier"; |
| 210 |
class.lpfnWndProc = NotifierWindowProc; |
| 211 |
|
| 212 |
if (!RegisterClassA(&class)) { |
| 213 |
panic("Unable to register PipeNotifier window class"); |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
hWindow = CreateWindowA("PipeNotifier", "PipeNotifier", WS_TILED, |
| 219 |
0, 0, 0, 0, NULL, NULL, hInstance, NULL); |
| 220 |
} |
| 221 |
return hWindow; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
static int |
| 248 |
PeekOnPipe( |
| 249 |
PipeHandler *pipePtr, |
| 250 |
int *nAvailPtr) |
| 251 |
{ |
| 252 |
int state; |
| 253 |
|
| 254 |
*nAvailPtr = -1; |
| 255 |
#if PEEK_DEBUG |
| 256 |
PurifyPrintf("PEEK(%d): waiting for reader\n", pipePtr->hPipe); |
| 257 |
#endif |
| 258 |
state = WaitForSingleObject(pipePtr->readyEvent, 0); |
| 259 |
#if PEEK_DEBUG |
| 260 |
PurifyPrintf("PEEK(%d): state is %d\n", pipePtr->hPipe, state); |
| 261 |
#endif |
| 262 |
if (state == WAIT_TIMEOUT) { |
| 263 |
#if PEEK_DEBUG |
| 264 |
PurifyPrintf("PEEK(%d): try again, %d\n", pipePtr->hPipe, state); |
| 265 |
#endif |
| 266 |
errno = EAGAIN; |
| 267 |
return FALSE; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
if (state == WAIT_OBJECT_0) { |
| 274 |
int nAvail; |
| 275 |
|
| 276 |
nAvail = pipePtr->end - pipePtr->start; |
| 277 |
#if PEEK_DEBUG |
| 278 |
PurifyPrintf("PEEK(%d): Found %d bytes available\n", |
| 279 |
pipePtr->hPipe, nAvail); |
| 280 |
#endif |
| 281 |
if ((nAvail <= 0) && !(pipePtr->flags & PIPE_EOF)) { |
| 282 |
TclWinConvertError(pipePtr->lastError); |
| 283 |
#if PEEK_DEBUG |
| 284 |
PurifyPrintf("PEEK(%d): Error = %d\n", |
| 285 |
pipePtr->hPipe, pipePtr->lastError); |
| 286 |
#endif |
| 287 |
nAvail = -1; |
| 288 |
} |
| 289 |
*nAvailPtr = nAvail; |
| 290 |
} |
| 291 |
#if PEEK_DEBUG |
| 292 |
PurifyPrintf("PEEK(%d): Reseting events\n", pipePtr->hPipe); |
| 293 |
#endif |
| 294 |
return TRUE; |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
static int |
| 318 |
PipeEventProc(Tcl_Event * eventPtr, int flags) |
| 319 |
{ |
| 320 |
PipeHandler *pipePtr; |
| 321 |
|
| 322 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 323 |
return 0; |
| 324 |
} |
| 325 |
pipePtr = ((PipeEvent *) eventPtr)->pipePtr; |
| 326 |
if ((pipePtr != NULL) && !(pipePtr->flags & PIPE_DELETED)) { |
| 327 |
Tcl_Preserve(pipePtr); |
| 328 |
if (pipePtr->proc != NULL) { |
| 329 |
(*pipePtr->proc) (pipePtr->clientData, flags); |
| 330 |
} |
| 331 |
|
| 332 |
pipePtr->flags &= ~PIPE_PENDING; |
| 333 |
Tcl_Release(pipePtr); |
| 334 |
} |
| 335 |
return 1; |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
void |
| 355 |
SetupHandlers(ClientData clientData, int flags) |
| 356 |
{ |
| 357 |
Blt_Chain *chainPtr = (Blt_Chain *) clientData; |
| 358 |
register PipeHandler *pipePtr; |
| 359 |
Blt_ChainLink *linkPtr; |
| 360 |
int dontBlock, nBytes; |
| 361 |
Tcl_Time blockTime; |
| 362 |
|
| 363 |
if (!(flags & TCL_FILE_EVENTS)) { |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
dontBlock = FALSE; |
| 371 |
blockTime.sec = blockTime.usec = 0L; |
| 372 |
#if QUEUE_DEBUG |
| 373 |
PurifyPrintf("SetupHandlers: before loop\n"); |
| 374 |
#endif |
| 375 |
for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL; |
| 376 |
linkPtr = Blt_ChainNextLink(linkPtr)) { |
| 377 |
pipePtr = Blt_ChainGetValue(linkPtr); |
| 378 |
if (pipePtr->flags & PIPE_DELETED) { |
| 379 |
continue; |
| 380 |
} |
| 381 |
if (pipePtr->flags & TCL_READABLE) { |
| 382 |
if (PeekOnPipe(pipePtr, &nBytes)) { |
| 383 |
dontBlock = TRUE; |
| 384 |
} |
| 385 |
} |
| 386 |
if (pipePtr->flags & TCL_WRITABLE) { |
| 387 |
if (WaitForSingleObject(pipePtr->readyEvent, 0) != WAIT_TIMEOUT) { |
| 388 |
dontBlock = TRUE; |
| 389 |
} |
| 390 |
} |
| 391 |
} |
| 392 |
#if QUEUE_DEBUG |
| 393 |
PurifyPrintf("SetupHandlers: after loop\n"); |
| 394 |
#endif |
| 395 |
if (dontBlock) { |
| 396 |
Tcl_SetMaxBlockTime(&blockTime); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
static void |
| 417 |
CheckHandlers(ClientData clientData, int flags) |
| 418 |
{ |
| 419 |
Blt_Chain *chainPtr = (Blt_Chain *) clientData; |
| 420 |
PipeHandler *pipePtr; |
| 421 |
Blt_ChainLink *linkPtr; |
| 422 |
int queueEvent, nBytes; |
| 423 |
|
| 424 |
if ((flags & TCL_FILE_EVENTS) == 0) { |
| 425 |
return; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
for (linkPtr = Blt_ChainFirstLink(chainPtr); linkPtr != NULL; |
| 430 |
linkPtr = Blt_ChainNextLink(linkPtr)) { |
| 431 |
pipePtr = Blt_ChainGetValue(linkPtr); |
| 432 |
if (pipePtr->flags & (PIPE_PENDING | PIPE_DELETED)) { |
| 433 |
continue; |
| 434 |
|
| 435 |
|
| 436 |
} |
| 437 |
|
| 438 |
queueEvent = FALSE; |
| 439 |
if (pipePtr->flags & TCL_READABLE) { |
| 440 |
if (PeekOnPipe(pipePtr, &nBytes)) { |
| 441 |
queueEvent = TRUE; |
| 442 |
} |
| 443 |
} |
| 444 |
if (pipePtr->flags & TCL_WRITABLE) { |
| 445 |
if (WaitForSingleObject(pipePtr->readyEvent, 0) != WAIT_TIMEOUT) { |
| 446 |
queueEvent = TRUE; |
| 447 |
} |
| 448 |
} |
| 449 |
#if QUEUE_DEBUG |
| 450 |
PurifyPrintf("Queue event is %d \n", queueEvent); |
| 451 |
#endif |
| 452 |
if (queueEvent) { |
| 453 |
PipeEvent *eventPtr; |
| 454 |
|
| 455 |
pipePtr->flags |= PIPE_PENDING; |
| 456 |
eventPtr = Blt_Malloc(sizeof(PipeEvent)); |
| 457 |
assert(eventPtr); |
| 458 |
eventPtr->header.proc = PipeEventProc; |
| 459 |
eventPtr->pipePtr = pipePtr; |
| 460 |
Tcl_QueueEvent((Tcl_Event *) eventPtr, TCL_QUEUE_TAIL); |
| 461 |
} |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
static PipeHandler * |
| 466 |
CreatePipeHandler(HANDLE hFile, int flags) |
| 467 |
{ |
| 468 |
DWORD id; |
| 469 |
PipeHandler *pipePtr; |
| 470 |
LPTHREAD_START_ROUTINE threadProc; |
| 471 |
|
| 472 |
pipePtr = Blt_Calloc(1, sizeof(PipeHandler)); |
| 473 |
assert(pipePtr); |
| 474 |
|
| 475 |
pipePtr->hPipe = hFile; |
| 476 |
pipePtr->flags = flags; |
| 477 |
pipePtr->parentId = GetCurrentThreadId(); |
| 478 |
pipePtr->parent = GetCurrentThread(); |
| 479 |
pipePtr->hWindow = GetNotifierWindow(); |
| 480 |
pipePtr->readyEvent = CreateEvent( |
| 481 |
NULL, |
| 482 |
TRUE, |
| 483 |
FALSE, |
| 484 |
NULL); |
| 485 |
pipePtr->idleEvent = CreateEvent( |
| 486 |
NULL, |
| 487 |
FALSE, |
| 488 |
TRUE, |
| 489 |
NULL); |
| 490 |
|
| 491 |
if (flags & TCL_READABLE) { |
| 492 |
threadProc = (LPTHREAD_START_ROUTINE) PipeReaderThread; |
| 493 |
pipePtr->buffer = Blt_Calloc(1, PIPE_BUFSIZ); |
| 494 |
pipePtr->size = PIPE_BUFSIZ; |
| 495 |
} else { |
| 496 |
threadProc = (LPTHREAD_START_ROUTINE) PipeWriterThread; |
| 497 |
} |
| 498 |
|
| 499 |
pipePtr->thread = CreateThread( |
| 500 |
NULL, |
| 501 |
8000, |
| 502 |
threadProc, |
| 503 |
(DWORD *) pipePtr, |
| 504 |
0, |
| 505 |
&id); |
| 506 |
return pipePtr; |
| 507 |
} |
| 508 |
|
| 509 |
static void |
| 510 |
DestroyPipe(DestroyData data) |
| 511 |
{ |
| 512 |
PipeHandler *pipePtr = (PipeHandler *)data; |
| 513 |
|
| 514 |
if (pipePtr->buffer != NULL) { |
| 515 |
Blt_Free(pipePtr->buffer); |
| 516 |
} |
| 517 |
Blt_Free(pipePtr); |
| 518 |
} |
| 519 |
|
| 520 |
static void |
| 521 |
DeletePipeHandler(PipeHandler * pipePtr) |
| 522 |
{ |
| 523 |
#if KILL_DEBUG |
| 524 |
PurifyPrintf("DestroyPipeHandler(%d)\n", pipePtr->hPipe); |
| 525 |
#endif |
| 526 |
if ((pipePtr->flags & TCL_WRITABLE) && |
| 527 |
(pipePtr->hPipe != INVALID_HANDLE_VALUE)) { |
| 528 |
|
| 529 |
WaitForSingleObject(pipePtr->idleEvent, INFINITE); |
| 530 |
} |
| 531 |
if (pipePtr->hPipe != INVALID_HANDLE_VALUE) { |
| 532 |
CloseHandle(pipePtr->hPipe); |
| 533 |
} |
| 534 |
CloseHandle(pipePtr->readyEvent); |
| 535 |
CloseHandle(pipePtr->idleEvent); |
| 536 |
CloseHandle(pipePtr->thread); |
| 537 |
|
| 538 |
pipePtr->idleEvent = pipePtr->readyEvent = INVALID_HANDLE_VALUE; |
| 539 |
pipePtr->thread = pipePtr->hPipe = INVALID_HANDLE_VALUE; |
| 540 |
pipePtr->flags |= PIPE_DELETED; |
| 541 |
|
| 542 |
Tcl_EventuallyFree(pipePtr, DestroyPipe); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
static void |
| 561 |
PipeInit(void) |
| 562 |
{ |
| 563 |
initialized = TRUE; |
| 564 |
InitializeCriticalSection(&pipeCriticalSection); |
| 565 |
Blt_ChainInit(&pipeChain); |
| 566 |
Tcl_CreateEventSource(SetupHandlers, CheckHandlers, &pipeChain); |
| 567 |
} |
| 568 |
|
| 569 |
static PipeHandler * |
| 570 |
GetPipeHandler(HANDLE hPipe) |
| 571 |
{ |
| 572 |
PipeHandler *pipePtr; |
| 573 |
Blt_ChainLink *linkPtr; |
| 574 |
|
| 575 |
for (linkPtr = Blt_ChainFirstLink(&pipeChain); linkPtr != NULL; |
| 576 |
linkPtr = Blt_ChainNextLink(linkPtr)) { |
| 577 |
pipePtr = Blt_ChainGetValue(linkPtr); |
| 578 |
if ((pipePtr->hPipe == hPipe) && !(pipePtr->flags & PIPE_DELETED)){ |
| 579 |
return pipePtr; |
| 580 |
} |
| 581 |
} |
| 582 |
return NULL; |
| 583 |
} |
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
void |
| 601 |
Blt_PipeTeardown(void) |
| 602 |
{ |
| 603 |
Blt_ChainLink *linkPtr; |
| 604 |
PipeHandler *pipePtr; |
| 605 |
|
| 606 |
if (!initialized) { |
| 607 |
return; |
| 608 |
} |
| 609 |
initialized = FALSE; |
| 610 |
EnterCriticalSection(&pipeCriticalSection); |
| 611 |
for (linkPtr = Blt_ChainFirstLink(&pipeChain); linkPtr != NULL; |
| 612 |
linkPtr = Blt_ChainNextLink(linkPtr)) { |
| 613 |
pipePtr = Blt_ChainGetValue(linkPtr); |
| 614 |
if ((pipePtr != NULL) && !(pipePtr->flags & PIPE_DELETED)) { |
| 615 |
DeletePipeHandler(pipePtr); |
| 616 |
} |
| 617 |
} |
| 618 |
DestroyWindow(GetNotifierWindow()); |
| 619 |
UnregisterClassA("PipeNotifier", TclWinGetTclInstance()); |
| 620 |
|
| 621 |
Blt_ChainReset(&pipeChain); |
| 622 |
LeaveCriticalSection(&pipeCriticalSection); |
| 623 |
Tcl_DeleteEventSource(SetupHandlers, CheckHandlers, &pipeChain); |
| 624 |
DeleteCriticalSection(&pipeCriticalSection); |
| 625 |
} |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
|
| 634 |
|
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
static DWORD WINAPI |
| 645 |
PipeReaderThread(void *clientData) |
| 646 |
{ |
| 647 |
PipeHandler *pipePtr = (PipeHandler *) clientData; |
| 648 |
DWORD count; |
| 649 |
BOOL result; |
| 650 |
|
| 651 |
for (;;) { |
| 652 |
if (pipePtr->flags & PIPE_DELETED) { |
| 653 |
break; |
| 654 |
} |
| 655 |
|
| 656 |
|
| 657 |
#if READER_DEBUG |
| 658 |
PurifyPrintf("READER(%d): waiting\n", pipePtr->hPipe); |
| 659 |
#endif |
| 660 |
WaitForSingleObject(pipePtr->idleEvent, INFINITE); |
| 661 |
#if READER_DEBUG |
| 662 |
PurifyPrintf("READER(%d): ok\n", pipePtr->hPipe); |
| 663 |
#endif |
| 664 |
|
| 665 |
|
| 666 |
#if READER_DEBUG |
| 667 |
PurifyPrintf("READER(%d): before read\n", pipePtr->hPipe); |
| 668 |
#endif |
| 669 |
assert(pipePtr->start == pipePtr->end); |
| 670 |
result = ReadFile( |
| 671 |
pipePtr->hPipe, |
| 672 |
pipePtr->buffer, |
| 673 |
pipePtr->size, |
| 674 |
|
| 675 |
&count, |
| 676 |
NULL); |
| 677 |
|
| 678 |
if (result) { |
| 679 |
#if READER_DEBUG |
| 680 |
PurifyPrintf("READER(%d): after read. status=%d, count=%d\n", |
| 681 |
pipePtr->hPipe, result, count); |
| 682 |
#endif |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
pipePtr->start = 0; |
| 688 |
pipePtr->end = count; |
| 689 |
if (count == 0) { |
| 690 |
|
| 691 |
pipePtr->lastError = GetLastError(); |
| 692 |
if ((pipePtr->lastError == ERROR_BROKEN_PIPE) || |
| 693 |
(pipePtr->lastError == ERROR_HANDLE_EOF)) { |
| 694 |
pipePtr->flags |= PIPE_EOF; |
| 695 |
} |
| 696 |
#if READER_DEBUG |
| 697 |
PurifyPrintf("READER(%d): error is %s\n", |
| 698 |
pipePtr->hPipe, Blt_LastError()); |
| 699 |
#endif |
| 700 |
} |
| 701 |
WakeupNotifier(pipePtr->hWindow); |
| 702 |
SetEvent(pipePtr->readyEvent); |
| 703 |
if (count == 0) { |
| 704 |
#if READER_DEBUG |
| 705 |
PurifyPrintf("READER(%d): exiting\n", pipePtr->hPipe); |
| 706 |
#endif |
| 707 |
ExitThread(0); |
| 708 |
} |
| 709 |
} |
| 710 |
|
| 711 |
return 0; |
| 712 |
} |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
static DWORD WINAPI |
| 732 |
PipeWriterThread(void *clientData) |
| 733 |
{ |
| 734 |
PipeHandler *pipePtr = (PipeHandler *) clientData; |
| 735 |
DWORD count, bytesLeft; |
| 736 |
register char *ptr; |
| 737 |
|
| 738 |
for (;;) { |
| 739 |
if (pipePtr->flags & PIPE_DELETED) { |
| 740 |
break; |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
WaitForSingleObject(pipePtr->idleEvent, INFINITE); |
| 749 |
|
| 750 |
ptr = pipePtr->buffer; |
| 751 |
bytesLeft = pipePtr->end; |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
while (bytesLeft > 0) { |
| 756 |
if (!WriteFile(pipePtr->hPipe, ptr, bytesLeft, &count, NULL)) { |
| 757 |
pipePtr->lastError = GetLastError(); |
| 758 |
break; |
| 759 |
} |
| 760 |
bytesLeft -= count; |
| 761 |
ptr += count; |
| 762 |
} |
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
SetEvent(pipePtr->readyEvent); |
| 768 |
WakeupNotifier(pipePtr->hWindow); |
| 769 |
} |
| 770 |
|
| 771 |
return 0; |
| 772 |
} |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
|
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
static int |
| 796 |
TempFileName(char *name) |
| 797 |
|
| 798 |
{ |
| 799 |
if ((GetTempPath(MAX_PATH, name) > 0) && |
| 800 |
(GetTempFileName(name, "TCL", 0, name))) { |
| 801 |
return 1; |
| 802 |
} |
| 803 |
|
| 804 |
return GetTempFileName(".", "TCL", 0, name); |
| 805 |
} |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
static HANDLE |
| 823 |
OpenRedirectFile( |
| 824 |
const char *path, |
| 825 |
DWORD accessFlags, |
| 826 |
DWORD createFlags) |
| 827 |
{ |
| 828 |
HANDLE hFile; |
| 829 |
DWORD attribFlags; |
| 830 |
int useExisting; |
| 831 |
|
| 832 |
attribFlags = 0; |
| 833 |
useExisting = (createFlags & (TRUNCATE_EXISTING | OPEN_EXISTING)); |
| 834 |
if (useExisting) { |
| 835 |
attribFlags = GetFileAttributes(path); |
| 836 |
if (attribFlags == 0xFFFFFFFF) { |
| 837 |
attribFlags = 0; |
| 838 |
} |
| 839 |
} |
| 840 |
hFile = CreateFile(path, |
| 841 |
accessFlags, |
| 842 |
FILE_SHARE_READ | FILE_SHARE_WRITE, |
| 843 |
NULL, |
| 844 |
createFlags, |
| 845 |
attribFlags, |
| 846 |
NULL); |
| 847 |
|
| 848 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 849 |
DWORD lastError; |
| 850 |
|
| 851 |
lastError = GetLastError(); |
| 852 |
if ((lastError & 0xffffL) == ERROR_OPEN_FAILED) { |
| 853 |
lastError = (useExisting) |
| 854 |
? ERROR_FILE_NOT_FOUND : ERROR_FILE_EXISTS; |
| 855 |
} |
| 856 |
TclWinConvertError(lastError); |
| 857 |
return INVALID_HANDLE_VALUE; |
| 858 |
} |
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
if (createFlags & GENERIC_WRITE) { |
| 863 |
SetFilePointer(hFile, 0, NULL, FILE_END); |
| 864 |
} |
| 865 |
return hFile; |
| 866 |
} |
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
static HANDLE |
| 886 |
CreateTempFile(const char *data) |
| 887 |
|
| 888 |
{ |
| 889 |
char fileName[MAX_PATH + 1]; |
| 890 |
HANDLE hFile; |
| 891 |
DWORD lastError; |
| 892 |
|
| 893 |
if (!TempFileName(fileName)) { |
| 894 |
return INVALID_HANDLE_VALUE; |
| 895 |
} |
| 896 |
hFile = CreateFile( |
| 897 |
fileName, |
| 898 |
GENERIC_READ | GENERIC_WRITE, |
| 899 |
0, |
| 900 |
NULL, |
| 901 |
CREATE_ALWAYS, |
| 902 |
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, |
| 903 |
NULL); |
| 904 |
|
| 905 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 906 |
goto error; |
| 907 |
} |
| 908 |
if (data != NULL) { |
| 909 |
DWORD result, length; |
| 910 |
const char *p; |
| 911 |
const char *string; |
| 912 |
|
| 913 |
string = data; |
| 914 |
for (p = string; *p != '\0'; p++) { |
| 915 |
if (*p == '\n') { |
| 916 |
length = p - string; |
| 917 |
if (length > 0) { |
| 918 |
if (!WriteFile(hFile, string, length, &result, NULL)) { |
| 919 |
goto error; |
| 920 |
} |
| 921 |
} |
| 922 |
if (!WriteFile(hFile, "\r\n", 2, &result, NULL)) { |
| 923 |
goto error; |
| 924 |
} |
| 925 |
string = p + 1; |
| 926 |
} |
| 927 |
} |
| 928 |
length = p - string; |
| 929 |
if (length > 0) { |
| 930 |
if (!WriteFile(hFile, string, length, &result, NULL)) { |
| 931 |
goto error; |
| 932 |
} |
| 933 |
} |
| 934 |
if (SetFilePointer(hFile, 0, NULL, FILE_BEGIN) == (DWORD) - 1) { |
| 935 |
goto error; |
| 936 |
} |
| 937 |
} |
| 938 |
return hFile; |
| 939 |
|
| 940 |
error: |
| 941 |
lastError = GetLastError(); |
| 942 |
CloseHandle(hFile); |
| 943 |
DeleteFile(fileName); |
| 944 |
TclWinConvertError(lastError); |
| 945 |
return INVALID_HANDLE_VALUE; |
| 946 |
} |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
static BOOL |
| 965 |
HasConsole(void) |
| 966 |
{ |
| 967 |
HANDLE hFile; |
| 968 |
|
| 969 |
hFile = CreateFileA("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, |
| 970 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 971 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 972 |
return FALSE; |
| 973 |
} |
| 974 |
CloseHandle(hFile); |
| 975 |
return TRUE; |
| 976 |
} |
| 977 |
|
| 978 |
static ApplicationType |
| 979 |
GetApplicationType(const char *file, char *cmdPrefix) |
| 980 |
{ |
| 981 |
char *dot; |
| 982 |
HANDLE hFile; |
| 983 |
IMAGE_DOS_HEADER imageDosHeader; |
| 984 |
ULONG signature; |
| 985 |
BOOL result; |
| 986 |
DWORD offset; |
| 987 |
DWORD nBytes; |
| 988 |
ApplicationType type; |
| 989 |
|
| 990 |
dot = strrchr(file, '.'); |
| 991 |
if ((dot != NULL) && (strcasecmp(dot, ".bat") == 0)) { |
| 992 |
return APPL_DOS; |
| 993 |
} |
| 994 |
|
| 995 |
hFile = CreateFileA(file, GENERIC_READ, FILE_SHARE_READ, NULL, |
| 996 |
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 997 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 998 |
return APPL_NONE; |
| 999 |
} |
| 1000 |
type = APPL_NONE; |
| 1001 |
result = ReadFile(hFile, &imageDosHeader, sizeof(IMAGE_DOS_HEADER), |
| 1002 |
&nBytes, NULL); |
| 1003 |
if ((!result) || (nBytes != sizeof(IMAGE_DOS_HEADER))) { |
| 1004 |
goto done; |
| 1005 |
} |
| 1006 |
#if KILL_DEBUG |
| 1007 |
PurifyPrintf("magic number is %x\n", imageDosHeader.e_magic); |
| 1008 |
#endif |
| 1009 |
if (imageDosHeader.e_magic == 0x2123) { |
| 1010 |
register char *p; |
| 1011 |
register unsigned int i; |
| 1012 |
|
| 1013 |
offset = SetFilePointer(hFile, 2, NULL, FILE_BEGIN); |
| 1014 |
if (offset == (DWORD) - 1) { |
| 1015 |
goto done; |
| 1016 |
} |
| 1017 |
result = ReadFile(hFile, cmdPrefix, MAX_PATH + 1, &nBytes, NULL); |
| 1018 |
if ((!result) || (nBytes < 1)) { |
| 1019 |
goto done; |
| 1020 |
} |
| 1021 |
for (p = cmdPrefix, i = 0; i < nBytes; i++, p++) { |
| 1022 |
if ((*p == '\n') || (*p == '\r')) { |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
*p = '\0'; |
| 1027 |
type = APPL_INTERP; |
| 1028 |
goto done; |
| 1029 |
} |
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
if ((dot != NULL) && (strcmp(dot, ".com") == 0)) { |
| 1038 |
#if KILL_DEBUG |
| 1039 |
PurifyPrintf(".com\n"); |
| 1040 |
#endif |
| 1041 |
type = APPL_DOS; |
| 1042 |
goto done; |
| 1043 |
} |
| 1044 |
if (imageDosHeader.e_magic != IMAGE_DOS_SIGNATURE) { |
| 1045 |
#if KILL_DEBUG |
| 1046 |
PurifyPrintf("Application doesn't have correct sig?\n"); |
| 1047 |
#endif |
| 1048 |
} |
| 1049 |
if (imageDosHeader.e_lfarlc != sizeof(IMAGE_DOS_HEADER)) { |
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
#if KILL_DEBUG |
| 1058 |
PurifyPrintf("wrong reloc table address\n"); |
| 1059 |
#endif |
| 1060 |
type = APPL_DOS; |
| 1061 |
goto done; |
| 1062 |
} |
| 1063 |
offset = SetFilePointer(hFile, imageDosHeader.e_lfanew, NULL, FILE_BEGIN); |
| 1064 |
if (offset == (DWORD) - 1) { |
| 1065 |
goto done; |
| 1066 |
} |
| 1067 |
result = ReadFile(hFile, &signature, sizeof(ULONG), &nBytes, NULL); |
| 1068 |
if ((!result) || (nBytes != sizeof(ULONG))) { |
| 1069 |
goto done; |
| 1070 |
} |
| 1071 |
#if KILL_DEBUG |
| 1072 |
PurifyPrintf("signature is %x\n", signature); |
| 1073 |
#endif |
| 1074 |
switch (signature) { |
| 1075 |
case IMAGE_NT_SIGNATURE: |
| 1076 |
type = APPL_WIN32; |
| 1077 |
break; |
| 1078 |
case IMAGE_OS2_SIGNATURE: |
| 1079 |
type = APPL_WIN3X; |
| 1080 |
break; |
| 1081 |
case IMAGE_VXD_SIGNATURE: |
| 1082 |
type = APPL_WIN32; |
| 1083 |
break; |
| 1084 |
default: |
| 1085 |
type = APPL_DOS; |
| 1086 |
break; |
| 1087 |
} |
| 1088 |
done: |
| 1089 |
CloseHandle(hFile); |
| 1090 |
return type; |
| 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 |
static int |
| 1119 |
GetFullPath( |
| 1120 |
Tcl_Interp *interp, |
| 1121 |
const char *program, |
| 1122 |
char *fullPath, |
| 1123 |
char *cmdPrefix, |
| 1124 |
|
| 1125 |
ApplicationType * typePtr) |
| 1126 |
{ |
| 1127 |
TCHAR *rest; |
| 1128 |
DWORD attr; |
| 1129 |
int length; |
| 1130 |
char cmd[MAX_PATH + 5]; |
| 1131 |
register char **p; |
| 1132 |
|
| 1133 |
static char *dosExts[] = |
| 1134 |
{ |
| 1135 |
"", ".com", ".exe", ".bat", NULL |
| 1136 |
}; |
| 1137 |
|
| 1138 |
*typePtr = APPL_NONE; |
| 1139 |
|
| 1140 |
length = strlen(program); |
| 1141 |
strcpy(cmd, program); |
| 1142 |
cmdPrefix[0] = '\0'; |
| 1143 |
for (p = dosExts; *p != NULL; p++) { |
| 1144 |
cmd[length] = '\0'; |
| 1145 |
strcat(cmd, *p); |
| 1146 |
|
| 1147 |
|
| 1148 |
if (!SearchPath( |
| 1149 |
NULL, |
| 1150 |
cmd, |
| 1151 |
NULL, |
| 1152 |
MAX_PATH, |
| 1153 |
fullPath, |
| 1154 |
&rest)) { |
| 1155 |
continue; |
| 1156 |
} |
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
attr = GetFileAttributesA(fullPath); |
| 1162 |
if ((attr == (DWORD)-1) || (attr & FILE_ATTRIBUTE_DIRECTORY)) { |
| 1163 |
continue; |
| 1164 |
} |
| 1165 |
*typePtr = GetApplicationType(fullPath, cmdPrefix); |
| 1166 |
if (*typePtr != APPL_NONE) { |
| 1167 |
break; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
if (*typePtr == APPL_NONE) { |
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
static char *shellCmds[] = |
| 1176 |
{ |
| 1177 |
"copy", "del", "dir", "echo", "edit", "erase", "label", |
| 1178 |
"md", "rd", "ren", "start", "time", "type", "ver", "vol", NULL |
| 1179 |
}; |
| 1180 |
|
| 1181 |
for (p = shellCmds; *p != NULL; p++) { |
| 1182 |
if (((*p)[0] == program[0]) && (strcmp(*p, program) == 0)) { |
| 1183 |
break; |
| 1184 |
} |
| 1185 |
} |
| 1186 |
if (*p == NULL) { |
| 1187 |
Tcl_AppendResult(interp, "can't execute \"", program, |
| 1188 |
"\": no such file or directory", (char *)NULL); |
| 1189 |
return TCL_ERROR; |
| 1190 |
} |
| 1191 |
*typePtr = APPL_DOS; |
| 1192 |
strcpy(fullPath, program); |
| 1193 |
} |
| 1194 |
if ((*typePtr == APPL_DOS) || (*typePtr == APPL_WIN3X)) { |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
GetShortPathName(fullPath, fullPath, MAX_PATH); |
| 1201 |
} |
| 1202 |
return TCL_OK; |
| 1203 |
} |
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
static char * |
| 1222 |
ConcatCmdArgs( |
| 1223 |
int argc, |
| 1224 |
char **argv, |
| 1225 |
Tcl_DString *resultPtr) |
| 1226 |
{ |
| 1227 |
BOOL needQuote; |
| 1228 |
register const char *s; |
| 1229 |
register char *cp; |
| 1230 |
char *string; |
| 1231 |
register int count; |
| 1232 |
register int i; |
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
count = 0; |
| 1239 |
for (i = 0; i < argc; i++) { |
| 1240 |
needQuote = FALSE; |
| 1241 |
if (*argv[i] == '\0') { |
| 1242 |
needQuote = TRUE; |
| 1243 |
} |
| 1244 |
for (s = argv[i]; *s != '\0'; s++) { |
| 1245 |
if (*s == '"') { |
| 1246 |
register const char *bp; |
| 1247 |
|
| 1248 |
count++; |
| 1249 |
for (bp = s - 1; (*bp == '\\') && (bp >= argv[i]); bp--) { |
| 1250 |
count++; |
| 1251 |
} |
| 1252 |
} else if (isspace(*s)) { |
| 1253 |
needQuote = TRUE; |
| 1254 |
} |
| 1255 |
count++; |
| 1256 |
} |
| 1257 |
if (needQuote) { |
| 1258 |
count += 2; |
| 1259 |
} |
| 1260 |
count++; |
| 1261 |
} |
| 1262 |
|
| 1263 |
string = Blt_Malloc(count + 1); |
| 1264 |
assert(string); |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
|
| 1270 |
cp = string; |
| 1271 |
for (i = 0; i < argc; i++) { |
| 1272 |
needQuote = FALSE; |
| 1273 |
|
| 1274 |
if (*argv[i] == '\0') { |
| 1275 |
needQuote = TRUE; |
| 1276 |
} |
| 1277 |
for (s = argv[i]; *s != '\0'; s++) { |
| 1278 |
if (isspace(*s)) { |
| 1279 |
needQuote = TRUE; |
| 1280 |
} |
| 1281 |
} |
| 1282 |
if (needQuote) { |
| 1283 |
*cp++ = '"'; |
| 1284 |
} |
| 1285 |
for (s = argv[i]; *s; s++) { |
| 1286 |
if (*s == '"') { |
| 1287 |
register const char *bp; |
| 1288 |
|
| 1289 |
for (bp = s - 1; (*bp == '\\') && (bp >= argv[i]); bp--) { |
| 1290 |
*cp++ = '\\'; |
| 1291 |
} |
| 1292 |
*cp++ = '\\'; |
| 1293 |
} |
| 1294 |
*cp++ = *s; |
| 1295 |
} |
| 1296 |
if (needQuote) { |
| 1297 |
*cp++ = '"'; |
| 1298 |
} |
| 1299 |
*cp++ = ' '; |
| 1300 |
} |
| 1301 |
*cp = '\0'; |
| 1302 |
assert((cp - string) == count); |
| 1303 |
Tcl_DStringAppend(resultPtr, string, count); |
| 1304 |
Blt_Free(string); |
| 1305 |
return Tcl_DStringValue(resultPtr); |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
|
| 1321 |
|
| 1322 |
|
| 1323 |
|
| 1324 |
|
| 1325 |
|
| 1326 |
|
| 1327 |
|
| 1328 |
|
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
static int |
| 1333 |
StartProcess( |
| 1334 |
Tcl_Interp *interp, |
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
int argc, |
| 1339 |
char **argv, |
| 1340 |
HANDLE hStdin, |
| 1341 |
|
| 1342 |
|
| 1343 |
HANDLE hStdout, |
| 1344 |
|
| 1345 |
|
| 1346 |
HANDLE hStderr, |
| 1347 |
|
| 1348 |
|
| 1349 |
|
| 1350 |
HANDLE *hProcessPtr, |
| 1351 |
DWORD *pidPtr) |
| 1352 |
{ |
| 1353 |
int result, createFlags; |
| 1354 |
ApplicationType applType; |
| 1355 |
Tcl_DString dString; |
| 1356 |
char *command; |
| 1357 |
BOOL hasConsole; |
| 1358 |
#ifdef notdef |
| 1359 |
DWORD idleResult; |
| 1360 |
#endif |
| 1361 |
STARTUPINFOA si; |
| 1362 |
PROCESS_INFORMATION pi; |
| 1363 |
SECURITY_ATTRIBUTES securityAttrs; |
| 1364 |
HANDLE hProcess, hPipe; |
| 1365 |
char progPath[MAX_PATH]; |
| 1366 |
char cmdPrefix[MAX_PATH]; |
| 1367 |
|
| 1368 |
*hProcessPtr = INVALID_HANDLE_VALUE; |
| 1369 |
GetFullPath(interp, argv[0], progPath, cmdPrefix, &applType); |
| 1370 |
#if KILL_DEBUG |
| 1371 |
PurifyPrintf("Application type is %d\n", (int)applType); |
| 1372 |
#endif |
| 1373 |
if (applType == APPL_NONE) { |
| 1374 |
return TCL_ERROR; |
| 1375 |
} |
| 1376 |
result = TCL_ERROR; |
| 1377 |
|
| 1378 |
hProcess = GetCurrentProcess(); |
| 1379 |
|
| 1380 |
ZeroMemory(&si, sizeof(STARTUPINFOA)); |
| 1381 |
si.cb = sizeof(STARTUPINFOA); |
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
si.dwFlags = STARTF_USESTDHANDLES; |
| 1391 |
si.hStdInput = si.hStdOutput = si.hStdError = INVALID_HANDLE_VALUE; |
| 1392 |
|
| 1393 |
securityAttrs.nLength = sizeof(SECURITY_ATTRIBUTES); |
| 1394 |
securityAttrs.lpSecurityDescriptor = NULL; |
| 1395 |
securityAttrs.bInheritHandle = TRUE; |
| 1396 |
|
| 1397 |
|
| 1398 |
|
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
if (hStdin == INVALID_HANDLE_VALUE) { |
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
if (CreatePipe(&si.hStdInput, &hPipe, &securityAttrs, 0)) { |
| 1416 |
CloseHandle(hPipe); |
| 1417 |
} |
| 1418 |
} else { |
| 1419 |
DuplicateHandle(hProcess, hStdin, hProcess, &si.hStdInput, 0, TRUE, |
| 1420 |
DUPLICATE_SAME_ACCESS); |
| 1421 |
} |
| 1422 |
|
| 1423 |
if (si.hStdInput == INVALID_HANDLE_VALUE) { |
| 1424 |
Tcl_AppendResult(interp, "can't duplicate input handle: ", |
| 1425 |
Blt_LastError(), (char *)NULL); |
| 1426 |
goto closeHandles; |
| 1427 |
} |
| 1428 |
if (hStdout == INVALID_HANDLE_VALUE) { |
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
if ((Blt_GetPlatformId() == VER_PLATFORM_WIN32_WINDOWS) |
| 1444 |
&& (applType == APPL_DOS)) { |
| 1445 |
if (CreatePipe(&hPipe, &si.hStdOutput, &securityAttrs, 0)) { |
| 1446 |
CloseHandle(hPipe); |
| 1447 |
} |
| 1448 |
} else { |
| 1449 |
si.hStdOutput = CreateFileA("NUL:", GENERIC_WRITE, 0, |
| 1450 |
&securityAttrs, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1451 |
} |
| 1452 |
} else { |
| 1453 |
DuplicateHandle(hProcess, hStdout, hProcess, &si.hStdOutput, 0, TRUE, |
| 1454 |
DUPLICATE_SAME_ACCESS); |
| 1455 |
} |
| 1456 |
if (si.hStdOutput == INVALID_HANDLE_VALUE) { |
| 1457 |
Tcl_AppendResult(interp, "can't duplicate output handle: ", |
| 1458 |
Blt_LastError(), (char *)NULL); |
| 1459 |
goto closeHandles; |
| 1460 |
} |
| 1461 |
if (hStderr == INVALID_HANDLE_VALUE) { |
| 1462 |
|
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
si.hStdError = CreateFileA("NUL:", GENERIC_WRITE, 0, |
| 1467 |
&securityAttrs, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); |
| 1468 |
} else { |
| 1469 |
DuplicateHandle(hProcess, hStderr, hProcess, &si.hStdError, 0, TRUE, |
| 1470 |
DUPLICATE_SAME_ACCESS); |
| 1471 |
} |
| 1472 |
if (si.hStdError == INVALID_HANDLE_VALUE) { |
| 1473 |
Tcl_AppendResult(interp, "can't duplicate error handle: ", |
| 1474 |
Blt_LastError(), (char *)NULL); |
| 1475 |
goto closeHandles; |
| 1476 |
} |
| 1477 |
Tcl_DStringInit(&dString); |
| 1478 |
createFlags = 0; |
| 1479 |
hasConsole = HasConsole(); |
| 1480 |
if (!hasConsole) { |
| 1481 |
createFlags |= DETACHED_PROCESS; |
| 1482 |
} |
| 1483 |
|
| 1484 |
|
| 1485 |
|
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
if (applType == APPL_DOS) { |
| 1498 |
if (Blt_GetPlatformId() == VER_PLATFORM_WIN32_NT) { |
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
si.wShowWindow = SW_HIDE; |
| 1507 |
si.dwFlags |= STARTF_USESHOWWINDOW; |
| 1508 |
createFlags = CREATE_NEW_CONSOLE; |
| 1509 |
Tcl_DStringAppend(&dString, "cmd.exe /c ", -1); |
| 1510 |
} else { |
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
|
| 1531 |
|
| 1532 |
|
| 1533 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
if (!hasConsole) { |
| 1540 |
si.wShowWindow = SW_HIDE; |
| 1541 |
si.dwFlags |= STARTF_USESHOWWINDOW; |
| 1542 |
createFlags = CREATE_NEW_CONSOLE; |
| 1543 |
} |
| 1544 |
Tcl_DStringAppend(&dString, "tclpip" STRINGIFY(TCL_MAJOR_VERSION) |
| 1545 |
STRINGIFY(TCL_MINOR_VERSION) ".dll ", -1); |
| 1546 |
} |
| 1547 |
} else if (applType == APPL_INTERP) { |
| 1548 |
Tcl_DStringAppend(&dString, cmdPrefix, -1); |
| 1549 |
Tcl_DStringAppend(&dString, " ", -1); |
| 1550 |
} |
| 1551 |
argv[0] = progPath; |
| 1552 |
|
| 1553 |
command = ConcatCmdArgs(argc, argv, &dString); |
| 1554 |
#if KILL_DEBUG |
| 1555 |
PurifyPrintf("command is %s\n", command); |
| 1556 |
#endif |
| 1557 |
result = CreateProcess( |
| 1558 |
NULL, |
| 1559 |
(TCHAR *)command, |
| 1560 |
NULL, |
| 1561 |
NULL, |
| 1562 |
TRUE, |
| 1563 |
createFlags, |
| 1564 |
NULL, |
| 1565 |
NULL, |
| 1566 |
&si, |
| 1567 |
|
| 1568 |
|
| 1569 |
&pi); |
| 1570 |
|
| 1571 |
Tcl_DStringFree(&dString); |
| 1572 |
|
| 1573 |
if (!result) { |
| 1574 |
Tcl_AppendResult(interp, "can't execute \"", argv[0], "\": ", |
| 1575 |
Blt_LastError(), (char *)NULL); |
| 1576 |
goto closeHandles; |
| 1577 |
} |
| 1578 |
#if KILL_DEBUG |
| 1579 |
PurifyPrintf("Starting process with handle of %d\n", pi.hProcess); |
| 1580 |
PurifyPrintf("Starting process with id of %d\n", pi.dwProcessId); |
| 1581 |
#endif |
| 1582 |
if (applType == APPL_DOS) { |
| 1583 |
|
| 1584 |
WaitForSingleObject(hProcess, 50); |
| 1585 |
} |
| 1586 |
#ifdef notdef |
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
idleResult = WaitForInputIdle(pi.hProcess, 1000); |
| 1604 |
if (idleResult == (DWORD) - 1) { |
| 1605 |
#if KILL_DEBUG |
| 1606 |
PurifyPrintf("wait failed on %d: %s\n", pi.hProcess, Blt_LastError()); |
| 1607 |
#endif |
| 1608 |
} |
| 1609 |
#endif |
| 1610 |
CloseHandle(pi.hThread); |
| 1611 |
|
| 1612 |
*hProcessPtr = pi.hProcess; |
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
*pidPtr = pi.dwProcessId; |
| 1620 |
result = TCL_OK; |
| 1621 |
|
| 1622 |
closeHandles: |
| 1623 |
if (si.hStdInput != INVALID_HANDLE_VALUE) { |
| 1624 |
CloseHandle(si.hStdInput); |
| 1625 |
} |
| 1626 |
if (si.hStdOutput != INVALID_HANDLE_VALUE) { |
| 1627 |
CloseHandle(si.hStdOutput); |
| 1628 |
} |
| 1629 |
if (si.hStdError != INVALID_HANDLE_VALUE) { |
| 1630 |
CloseHandle(si.hStdError); |
| 1631 |
} |
| 1632 |
return result; |
| 1633 |
} |
| 1634 |
|
| 1635 |
|
| 1636 |
|
| 1637 |
|
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
|
| 1643 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
|
| 1654 |
|
| 1655 |
static HANDLE |
| 1656 |
FileForRedirect( |
| 1657 |
Tcl_Interp *interp, |
| 1658 |
char *spec, |
| 1659 |
|
| 1660 |
BOOL atOK, |
| 1661 |
|
| 1662 |
|
| 1663 |
char *arg, |
| 1664 |
|
| 1665 |
char *nextArg, |
| 1666 |
|
| 1667 |
|
| 1668 |
DWORD accessFlags, |
| 1669 |
|
| 1670 |
DWORD createFlags, |
| 1671 |
|
| 1672 |
int *skipPtr, |
| 1673 |
|
| 1674 |
int *closePtr) |
| 1675 |
|
| 1676 |
|
| 1677 |
{ |
| 1678 |
int writing = (accessFlags & GENERIC_WRITE); |
| 1679 |
Tcl_Channel chan; |
| 1680 |
HANDLE hFile; |
| 1681 |
|
| 1682 |
*skipPtr = 1; |
| 1683 |
*closePtr = FALSE; |
| 1684 |
if ((atOK) && (*spec == '@')) { |
| 1685 |
spec++; |
| 1686 |
if (*spec == '\0') { |
| 1687 |
spec = nextArg; |
| 1688 |
if (spec == NULL) { |
| 1689 |
goto badLastArg; |
| 1690 |
} |
| 1691 |
*skipPtr = 2; |
| 1692 |
} |
| 1693 |
if (*spec == '1' && spec[1] == 0) { |
| 1694 |
spec = "stdout"; |
| 1695 |
} |
| 1696 |
chan = Tcl_GetChannel(interp, spec, NULL); |
| 1697 |
if (chan == NULL) { |
| 1698 |
return INVALID_HANDLE_VALUE; |
| 1699 |
} |
| 1700 |
if (Tcl_GetChannelHandle(chan, (writing) ? TCL_WRITABLE : TCL_READABLE, |
| 1701 |
(ClientData *)&hFile) != TCL_OK) { |
| 1702 |
hFile = INVALID_HANDLE_VALUE; |
| 1703 |
} |
| 1704 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 1705 |
Tcl_AppendResult(interp, "channel \"", Tcl_GetChannelName(chan), |
| 1706 |
"\" wasn't opened for ", |
| 1707 |
((writing) ? "writing" : "reading"), (char *)NULL); |
| 1708 |
return INVALID_HANDLE_VALUE; |
| 1709 |
} |
| 1710 |
if (writing) { |
| 1711 |
|
| 1712 |
|
| 1713 |
|
| 1714 |
|
| 1715 |
|
| 1716 |
Tcl_Flush(chan); |
| 1717 |
} |
| 1718 |
} else { |
| 1719 |
char *name; |
| 1720 |
Tcl_DString dString; |
| 1721 |
|
| 1722 |
if (*spec == '\0') { |
| 1723 |
spec = nextArg; |
| 1724 |
if (spec == NULL) { |
| 1725 |
goto badLastArg; |
| 1726 |
} |
| 1727 |
*skipPtr = 2; |
| 1728 |
} |
| 1729 |
name = Tcl_TranslateFileName(interp, spec, &dString); |
| 1730 |
if (name != NULL) { |
| 1731 |
hFile = OpenRedirectFile(name, accessFlags, createFlags); |
| 1732 |
} else { |
| 1733 |
hFile = INVALID_HANDLE_VALUE; |
| 1734 |
} |
| 1735 |
Tcl_DStringFree(&dString); |
| 1736 |
|
| 1737 |
if (hFile == INVALID_HANDLE_VALUE) { |
| 1738 |
Tcl_AppendResult(interp, "can't ", (writing) ? "write" : "read", |
| 1739 |
" file \"", spec, "\": ", Tcl_PosixError(interp), |
| 1740 |
(char *)NULL); |
| 1741 |
return INVALID_HANDLE_VALUE; |
| 1742 |
} |
| 1743 |
*closePtr = TRUE; |
| 1744 |
} |
| 1745 |
return hFile; |
| 1746 |
|
| 1747 |
badLastArg: |
| 1748 |
Tcl_AppendResult(interp, "can't specify \"", arg, |
| 1749 |
"\" as last word in command", (char *)NULL); |
| 1750 |
return INVALID_HANDLE_VALUE; |
| 1751 |
} |
| 1752 |
|
| 1753 |
|
| 1754 |
|
| 1755 |
|
| 1756 |
|
| 1757 |
|
| 1758 |
|
| 1759 |
|
| 1760 |
|
| 1761 |
|
| 1762 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 1766 |
|
| 1767 |
|
| 1768 |
|
| 1769 |
|
| 1770 |
|
| 1771 |
|
| 1772 |
|
| 1773 |
|
| 1774 |
|
| 1775 |
|
| 1776 |
|
| 1777 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
int |
| 1782 |
Blt_CreatePipeline( |
| 1783 |
Tcl_Interp *interp, |
| 1784 |
int argc, |
| 1785 |
char **argv, |
| 1786 |
|
| 1787 |
|
| 1788 |
Process **procArrPtr, |
| 1789 |
|
| 1790 |
|
| 1791 |
|
| 1792 |
int *inPipePtr, |
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
int *outPipePtr, |
| 1799 |
|
| 1800 |
|
| 1801 |
|
| 1802 |
|
| 1803 |
|
| 1804 |
int *errPipePtr) |
| 1805 |
|
| 1806 |
|
| 1807 |
|
| 1808 |
|
| 1809 |
|
| 1810 |
|
| 1811 |
|
| 1812 |
|
| 1813 |
|
| 1814 |
|
| 1815 |
{ |
| 1816 |
Process *procArr = NULL; |
| 1817 |
|
| 1818 |
int nPids; |
| 1819 |
|
| 1820 |
int cmdCount; |
| 1821 |
|
| 1822 |
char *inputLiteral = NULL; |
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
HANDLE hStdin; |
| 1827 |
|
| 1828 |
|
| 1829 |
BOOL closeStdin; |
| 1830 |
|
| 1831 |
HANDLE hStdout; |
| 1832 |
|
| 1833 |
|
| 1834 |
BOOL closeStdout; |
| 1835 |
|
| 1836 |
HANDLE hStderr; |
| 1837 |
|
| 1838 |
|
| 1839 |
BOOL closeStderr; |
| 1840 |
|
| 1841 |
|
| 1842 |
HANDLE hInPipe, hOutPipe, hErrPipe; |
| 1843 |
char *p; |
| 1844 |
int skip, lastBar, lastArg, i, j, flags; |
| 1845 |
int pid; |
| 1846 |
BOOL atOK, errorToOutput; |
| 1847 |
Tcl_DString dString; |
| 1848 |
HANDLE hPipe; |
| 1849 |
HANDLE thisInput, thisOutput, thisError; |
| 1850 |
|
| 1851 |
if (inPipePtr != NULL) { |
| 1852 |
*inPipePtr = -1; |
| 1853 |
} |
| 1854 |
if (outPipePtr != NULL) { |
| 1855 |
*outPipePtr = -1; |
| 1856 |
} |
| 1857 |
if (errPipePtr != NULL) { |
| 1858 |
*errPipePtr = -1; |
| 1859 |
} |
| 1860 |
Tcl_DStringInit(&dString); |
| 1861 |
|
| 1862 |
hStdin = hStdout = hStderr = INVALID_HANDLE_VALUE; |
| 1863 |
hPipe = thisInput = thisOutput = INVALID_HANDLE_VALUE; |
| 1864 |
hInPipe = hOutPipe = hErrPipe = INVALID_HANDLE_VALUE; |
| 1865 |
closeStdin = closeStdout = closeStderr = FALSE; |
| 1866 |
nPids = 0; |
| 1867 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
|
| 1874 |
|
| 1875 |
|
| 1876 |
|
| 1877 |
|
| 1878 |
|
| 1879 |
|
| 1880 |
|
| 1881 |
lastBar = -1; |
| 1882 |
cmdCount = 1; |
| 1883 |
for (i = 0; i < argc; i++) { |
| 1884 |
skip = 0; |
| 1885 |
p = argv[i]; |
| 1886 |
switch (*p++) { |
| 1887 |
case '|': |
| 1888 |
if (*p == '&') { |
| 1889 |
p++; |
| 1890 |
} |
| 1891 |
if (*p == '\0') { |
| 1892 |
if ((i == (lastBar + 1)) || (i == (argc - 1))) { |
| 1893 |
Tcl_AppendResult(interp, |
| 1894 |
"illegal use of | or |& in command", |
| 1895 |
(char *)NULL); |
| 1896 |
goto error; |
| 1897 |
} |
| 1898 |
} |
| 1899 |
lastBar = i; |
| 1900 |
cmdCount++; |
| 1901 |
break; |
| 1902 |
|
| 1903 |
case '<': |
| 1904 |
if (closeStdin) { |
| 1905 |
closeStdin = FALSE; |
| 1906 |
CloseHandle(hStdin); |
| 1907 |
} |
| 1908 |
if (*p == '<') { |
| 1909 |
hStdin = INVALID_HANDLE_VALUE; |
| 1910 |
inputLiteral = p + 1; |
| 1911 |
skip = 1; |
| 1912 |
if (*inputLiteral == '\0') { |
| 1913 |
inputLiteral = argv[i + 1]; |
| 1914 |
if (inputLiteral == NULL) { |
| 1915 |
Tcl_AppendResult(interp, "can't specify \"", argv[i], |
| 1916 |
"\" as last word in command", (char *)NULL); |
| 1917 |
goto error; |
| 1918 |
} |
| 1919 |
skip = 2; |
| 1920 |
} |
| 1921 |
} else { |
| 1922 |
inputLiteral = NULL; |
| 1923 |
hStdin = FileForRedirect(interp, p, TRUE, argv[i], argv[i + 1], |
| 1924 |
GENERIC_READ, OPEN_EXISTING, &skip, &closeStdin); |
| 1925 |
if (hStdin == INVALID_HANDLE_VALUE) { |
| 1926 |
goto error; |
| 1927 |
} |
| 1928 |
} |
| 1929 |
break; |
| 1930 |
|
| 1931 |
case '>': |
| 1932 |
atOK = 1; |
| 1933 |
flags = CREATE_ALWAYS; |
| 1934 |
errorToOutput = FALSE; |
| 1935 |
if (*p == '>') { |
| 1936 |
p++; |
| 1937 |
atOK = 0; |
| 1938 |
flags = OPEN_ALWAYS; |
| 1939 |
} |
| 1940 |
if (*p == '&') { |
| 1941 |
if (closeStderr) { |
| 1942 |
closeStderr = FALSE; |
| 1943 |
CloseHandle(hStderr); |
| 1944 |
} |
| 1945 |
errorToOutput = TRUE; |
| 1946 |
p++; |
| 1947 |
} |
| 1948 |
if (closeStdout) { |
| 1949 |
closeStdout = FALSE; |
| 1950 |
CloseHandle(hStdout); |
| 1951 |
} |
| 1952 |
hStdout = FileForRedirect(interp, p, atOK, argv[i], argv[i + 1], |
| 1953 |
GENERIC_WRITE, flags, &skip, &closeStdout); |
| 1954 |
if (hStdout == INVALID_HANDLE_VALUE) { |
| 1955 |
goto error; |
| 1956 |
} |
| 1957 |
if (errorToOutput) { |
| 1958 |
closeStderr = FALSE; |
| 1959 |
hStderr = hStdout; |
| 1960 |
} |
| 1961 |
break; |
| 1962 |
|
| 1963 |
case '2': |
| 1964 |
if (*p != '>') { |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
p++; |
| 1968 |
atOK = TRUE; |
| 1969 |
flags = CREATE_ALWAYS; |
| 1970 |
if (*p == '>') { |
| 1971 |
p++; |
| 1972 |
atOK = FALSE; |
| 1973 |
flags = OPEN_ALWAYS; |
| 1974 |
} |
| 1975 |
if (closeStderr) { |
| 1976 |
closeStderr = FALSE; |
| 1977 |
CloseHandle(hStderr); |
| 1978 |
} |
| 1979 |
hStderr = FileForRedirect(interp, p, atOK, argv[i], argv[i + 1], |
| 1980 |
GENERIC_WRITE, flags, &skip, &closeStderr); |
| 1981 |
if (hStderr == INVALID_HANDLE_VALUE) { |
| 1982 |
goto error; |
| 1983 |
} |
| 1984 |
break; |
| 1985 |
} |
| 1986 |
|
| 1987 |
if (skip != 0) { |
| 1988 |
for (j = i + skip; j < argc; j++) { |
| 1989 |
argv[j - skip] = argv[j]; |
| 1990 |
} |
| 1991 |
argc -= skip; |
| 1992 |
i -= 1; |
| 1993 |
} |
| 1994 |
} |
| 1995 |
|
| 1996 |
if (hStdin == INVALID_HANDLE_VALUE) { |
| 1997 |
if (inputLiteral != NULL) { |
| 1998 |
|
| 1999 |
|
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
hStdin = CreateTempFile(inputLiteral); |
| 2004 |
if (hStdin == INVALID_HANDLE_VALUE) { |
| 2005 |
Tcl_AppendResult(interp, |
| 2006 |
"can't create input file for command: ", |
| 2007 |
Tcl_PosixError(interp), (char *)NULL); |
| 2008 |
goto error; |
| 2009 |
} |
| 2010 |
closeStdin = TRUE; |
| 2011 |
} else if (inPipePtr != NULL) { |
| 2012 |
|
| 2013 |
|
| 2014 |
|
| 2015 |
|
| 2016 |
|
| 2017 |
if (!CreatePipe(&hStdin, &hInPipe, NULL, 0)) { |
| 2018 |
Tcl_AppendResult(interp, |
| 2019 |
"can't create input pipe for command: ", |
| 2020 |
Tcl_PosixError(interp), (char *)NULL); |
| 2021 |
goto error; |
| 2022 |
} |
| 2023 |
closeStdin = TRUE; |
| 2024 |
} else { |
| 2025 |
|
| 2026 |
|
| 2027 |
|
| 2028 |
} |
| 2029 |
} |
| 2030 |
if (hStdout == INVALID_HANDLE_VALUE) { |
| 2031 |
if (outPipePtr != NULL) { |
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
if (!CreatePipe(&hOutPipe, &hStdout, NULL, 0)) { |
| 2038 |
Tcl_AppendResult(interp, |
| 2039 |
"can't create output pipe for command: ", |
| 2040 |
Tcl_PosixError(interp), (char *)NULL); |
| 2041 |
goto error; |
| 2042 |
} |
| 2043 |
closeStdout = TRUE; |
| 2044 |
} else { |
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
} |
| 2049 |
} |
| 2050 |
if (hStderr == INVALID_HANDLE_VALUE) { |
| 2051 |
if (errPipePtr != NULL) { |
| 2052 |
|
| 2053 |
|
| 2054 |
|
| 2055 |
|
| 2056 |
if (CreatePipe(&hErrPipe, &hStderr, NULL, 0) == 0) { |
| 2057 |
Tcl_AppendResult(interp, |
| 2058 |
"can't create error pipe for command: ", |
| 2059 |
Tcl_PosixError(interp), (char *)NULL); |
| 2060 |
goto error; |
| 2061 |
} |
| 2062 |
closeStderr = TRUE; |
| 2063 |
} else { |
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
} |
| 2068 |
} |
| 2069 |
|
| 2070 |
|
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
Tcl_ReapDetachedProcs(); |
| 2076 |
procArr = Blt_Malloc((unsigned)((cmdCount + 1) * sizeof(Process))); |
| 2077 |
assert(procArr); |
| 2078 |
thisInput = hStdin; |
| 2079 |
if (argc == 0) { |
| 2080 |
Tcl_AppendResult(interp, "invalid null command", (char *)NULL); |
| 2081 |
goto error; |
| 2082 |
} |
| 2083 |
#ifdef notdef |
| 2084 |
lastArg = 0; |
| 2085 |
#endif |
| 2086 |
for (i = 0; i < argc; i = lastArg + 1) { |
| 2087 |
BOOL joinThisError; |
| 2088 |
HANDLE hProcess; |
| 2089 |
|
| 2090 |
|
| 2091 |
argv[i] = Tcl_TranslateFileName(interp, argv[i], &dString); |
| 2092 |
if (argv[i] == NULL) { |
| 2093 |
goto error; |
| 2094 |
} |
| 2095 |
|
| 2096 |
joinThisError = FALSE; |
| 2097 |
for (lastArg = i; lastArg < argc; lastArg++) { |
| 2098 |
if (argv[lastArg][0] == '|') { |
| 2099 |
if (argv[lastArg][1] == '\0') { |
| 2100 |
break; |
| 2101 |
} |
| 2102 |
if ((argv[lastArg][1] == '&') && (argv[lastArg][2] == '\0')) { |
| 2103 |
joinThisError = TRUE; |
| 2104 |
break; |
| 2105 |
} |
| 2106 |
} |
| 2107 |
} |
| 2108 |
argv[lastArg] = NULL; |
| 2109 |
if ((lastArg - i) == 0) { |
| 2110 |
Tcl_AppendResult(interp, "invalid null command", (char *)NULL); |
| 2111 |
goto error; |
| 2112 |
} |
| 2113 |
|
| 2114 |
|
| 2115 |
|
| 2116 |
|
| 2117 |
|
| 2118 |
|
| 2119 |
if (lastArg == argc) { |
| 2120 |
thisOutput = hStdout; |
| 2121 |
} else { |
| 2122 |
if (CreatePipe(&hPipe, &thisOutput, NULL, 0) == 0) { |
| 2123 |
Tcl_AppendResult(interp, "can't create pipe: ", |
| 2124 |
Tcl_PosixError(interp), (char *)NULL); |
| 2125 |
goto error; |
| 2126 |
} |
| 2127 |
} |
| 2128 |
|
| 2129 |
if (joinThisError) { |
| 2130 |
thisError = thisOutput; |
| 2131 |
} else { |
| 2132 |
thisError = hStderr; |
| 2133 |
} |
| 2134 |
|
| 2135 |
if (StartProcess(interp, lastArg - i, argv + i, thisInput, thisOutput, |
| 2136 |
thisError, &hProcess, (DWORD *)&pid) != TCL_OK) { |
| 2137 |
goto error; |
| 2138 |
} |
| 2139 |
Tcl_DStringFree(&dString); |
| 2140 |
|
| 2141 |
procArr[nPids].hProcess = hProcess; |
| 2142 |
procArr[nPids].pid = pid; |
| 2143 |
nPids++; |
| 2144 |
|
| 2145 |
|
| 2146 |
|
| 2147 |
|
| 2148 |
|
| 2149 |
|
| 2150 |
if ((thisInput != INVALID_HANDLE_VALUE) && (thisInput != hStdin)) { |
| 2151 |
CloseHandle(thisInput); |
| 2152 |
} |
| 2153 |
thisInput = hPipe; |
| 2154 |
hPipe = INVALID_HANDLE_VALUE; |
| 2155 |
|
| 2156 |
if ((thisOutput != INVALID_HANDLE_VALUE) && (thisOutput != hStdout)) { |
| 2157 |
CloseHandle(thisOutput); |
| 2158 |
} |
| 2159 |
thisOutput = INVALID_HANDLE_VALUE; |
| 2160 |
} |
| 2161 |
|
| 2162 |
*procArrPtr = (Process *)procArr; |
| 2163 |
|
| 2164 |
if (inPipePtr != NULL) { |
| 2165 |
*inPipePtr = (int)hInPipe; |
| 2166 |
} |
| 2167 |
if (outPipePtr != NULL) { |
| 2168 |
*outPipePtr = (int)hOutPipe; |
| 2169 |
} |
| 2170 |
if (errPipePtr != NULL) { |
| 2171 |
*errPipePtr = (int)hErrPipe; |
| 2172 |
} |
| 2173 |
|
| 2174 |
|
| 2175 |
|
| 2176 |
cleanup: |
| 2177 |
Tcl_DStringFree(&dString); |
| 2178 |
|
| 2179 |
if (closeStdin) { |
| 2180 |
CloseHandle(hStdin); |
| 2181 |
} |
| 2182 |
if (closeStdout) { |
| 2183 |
CloseHandle(hStdout); |
| 2184 |
} |
| 2185 |
if (closeStderr) { |
| 2186 |
CloseHandle(hStderr); |
| 2187 |
} |
| 2188 |
return nPids; |
| 2189 |
|
| 2190 |
|
| 2191 |
|
| 2192 |
|
| 2193 |
|
| 2194 |
|
| 2195 |
error: |
| 2196 |
if (hPipe != INVALID_HANDLE_VALUE) { |
| 2197 |
CloseHandle(hPipe); |
| 2198 |
} |
| 2199 |
if ((thisOutput != INVALID_HANDLE_VALUE) && (thisOutput != hStdout)) { |
| 2200 |
CloseHandle(thisOutput); |
| 2201 |
} |
| 2202 |
if ((thisInput != INVALID_HANDLE_VALUE) && (thisInput != hStdin)) { |
| 2203 |
CloseHandle(thisInput); |
| 2204 |
} |
| 2205 |
if (hInPipe != INVALID_HANDLE_VALUE) { |
| 2206 |
CloseHandle(hInPipe); |
| 2207 |
} |
| 2208 |
if (hOutPipe != INVALID_HANDLE_VALUE) { |
| 2209 |
CloseHandle(hOutPipe); |
| 2210 |
} |
| 2211 |
if (hErrPipe != INVALID_HANDLE_VALUE) { |
| 2212 |
CloseHandle(hErrPipe); |
| 2213 |
} |
| 2214 |
if (procArr != NULL) { |
| 2215 |
for (i = 0; i < nPids; i++) { |
| 2216 |
if (procArr[i].hProcess != INVALID_HANDLE_VALUE) { |
| 2217 |
|
| 2218 |
|
| 2219 |
Tcl_DetachPids(1, (Tcl_Pid *)&(procArr[i].pid)); |
| 2220 |
} |
| 2221 |
} |
| 2222 |
Blt_Free(procArr); |
| 2223 |
} |
| 2224 |
nPids = -1; |
| 2225 |
goto cleanup; |
| 2226 |
} |
| 2227 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
void |
| 2246 |
Blt_CreateFileHandler( |
| 2247 |
int fd, |
| 2248 |
int flags, |
| 2249 |
Tcl_FileProc *proc, |
| 2250 |
ClientData clientData) |
| 2251 |
{ |
| 2252 |
PipeHandler *pipePtr; |
| 2253 |
|
| 2254 |
if (!initialized) { |
| 2255 |
PipeInit(); |
| 2256 |
} |
| 2257 |
if ((flags != TCL_READABLE) && (flags != TCL_WRITABLE)) { |
| 2258 |
return; |
| 2259 |
} |
| 2260 |
pipePtr = CreatePipeHandler((HANDLE) fd, flags); |
| 2261 |
pipePtr->proc = proc; |
| 2262 |
pipePtr->clientData = clientData; |
| 2263 |
|
| 2264 |
|
| 2265 |
EnterCriticalSection(&pipeCriticalSection); |
| 2266 |
Blt_ChainAppend(&pipeChain, pipePtr); |
| 2267 |
LeaveCriticalSection(&pipeCriticalSection); |
| 2268 |
} |
| 2269 |
|
| 2270 |
|
| 2271 |
|
| 2272 |
|
| 2273 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 2281 |
|
| 2282 |
|
| 2283 |
void |
| 2284 |
Blt_DeleteFileHandler(int fd) |
| 2285 |
{ |
| 2286 |
PipeHandler *pipePtr; |
| 2287 |
Blt_ChainLink *linkPtr; |
| 2288 |
HANDLE hPipe; |
| 2289 |
|
| 2290 |
if (!initialized) { |
| 2291 |
PipeInit(); |
| 2292 |
} |
| 2293 |
#if KILL_DEBUG |
| 2294 |
PurifyPrintf("Blt_DeleteFileHandler(%d)\n", fd); |
| 2295 |
#endif |
| 2296 |
hPipe = (HANDLE) fd; |
| 2297 |
EnterCriticalSection(&pipeCriticalSection); |
| 2298 |
|
| 2299 |
for (linkPtr = Blt_ChainFirstLink(&pipeChain); linkPtr != NULL; |
| 2300 |
linkPtr = Blt_ChainNextLink(linkPtr)) { |
| 2301 |
pipePtr = Blt_ChainGetValue(linkPtr); |
| 2302 |
if ((pipePtr->hPipe == hPipe) && !(pipePtr->flags & PIPE_DELETED)) { |
| 2303 |
Blt_ChainDeleteLink(&pipeChain, linkPtr); |
| 2304 |
DeletePipeHandler(pipePtr); |
| 2305 |
break; |
| 2306 |
} |
| 2307 |
} |
| 2308 |
LeaveCriticalSection(&pipeCriticalSection); |
| 2309 |
#if KILL_DEBUG |
| 2310 |
PurifyPrintf("Blt_DeleteFileHandler: done\n"); |
| 2311 |
#endif |
| 2312 |
} |
| 2313 |
|
| 2314 |
|
| 2315 |
|
| 2316 |
|
| 2317 |
|
| 2318 |
|
| 2319 |
|
| 2320 |
|
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
int |
| 2327 |
Blt_AsyncRead( |
| 2328 |
int f, |
| 2329 |
char *buffer, |
| 2330 |
unsigned int size) |
| 2331 |
{ |
| 2332 |
register PipeHandler *pipePtr; |
| 2333 |
unsigned int count; |
| 2334 |
int nBytes; |
| 2335 |
|
| 2336 |
#if ASYNC_DEBUG |
| 2337 |
PurifyPrintf("Blt_AsyncRead(f=%d)\n", f); |
| 2338 |
#endif |
| 2339 |
pipePtr = GetPipeHandler((HANDLE) f); |
| 2340 |
if ((pipePtr == NULL) || (pipePtr->flags & PIPE_DELETED)) { |
| 2341 |
errno = EBADF; |
| 2342 |
#if ASYNC_DEBUG |
| 2343 |
PurifyPrintf("Blt_AsyncRead: bad file\n"); |
| 2344 |
#endif |
| 2345 |
return -1; |
| 2346 |
} |
| 2347 |
if (!PeekOnPipe(pipePtr, &nBytes)) { |
| 2348 |
#if ASYNC_DEBUG |
| 2349 |
PurifyPrintf("Blt_AsyncRead: pipe is drained (nBytes=%d).\n", nBytes); |
| 2350 |
#endif |
| 2351 |
return -1; |
| 2352 |
} |
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
if (nBytes == -1) { |
| 2359 |
#if ASYNC_DEBUG |
| 2360 |
PurifyPrintf("Blt_AsyncRead: Error\n"); |
| 2361 |
#endif |
| 2362 |
return -1; |
| 2363 |
} |
| 2364 |
if (nBytes == 0) { |
| 2365 |
#if ASYNC_DEBUG |
| 2366 |
PurifyPrintf("Blt_AsyncRead: EOF\n"); |
| 2367 |
#endif |
| 2368 |
return 0; |
| 2369 |
} |
| 2370 |
count = pipePtr->end - pipePtr->start; |
| 2371 |
#if ASYNC_DEBUG |
| 2372 |
PurifyPrintf("Blt_AsyncRead: nBytes is %d, %d\n", nBytes, count); |
| 2373 |
#endif |
| 2374 |
assert(count == (unsigned int)nBytes); |
| 2375 |
if (size > count) { |
| 2376 |
size = count; |
| 2377 |
} |
| 2378 |
memcpy(buffer, pipePtr->buffer + pipePtr->start, size); |
| 2379 |
pipePtr->start += size; |
| 2380 |
if (pipePtr->start == pipePtr->end) { |
| 2381 |
#if ASYNC_DEBUG |
| 2382 |
PurifyPrintf("Blt_AsyncRead: signaling idle\n"); |
| 2383 |
#endif |
| 2384 |
ResetEvent(pipePtr->readyEvent); |
| 2385 |
SetEvent(pipePtr->idleEvent); |
| 2386 |
} |
| 2387 |
return size; |
| 2388 |
} |
| 2389 |
|
| 2390 |
|
| 2391 |
|
| 2392 |
|
| 2393 |
|
| 2394 |
|
| 2395 |
|
| 2396 |
|
| 2397 |
|
| 2398 |
|
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
int |
| 2403 |
Blt_AsyncWrite( |
| 2404 |
int f, |
| 2405 |
char *buffer, |
| 2406 |
unsigned int size) |
| 2407 |
{ |
| 2408 |
register PipeHandler *pipePtr; |
| 2409 |
|
| 2410 |
pipePtr = GetPipeHandler((HANDLE) f); |
| 2411 |
if ((pipePtr == NULL) || (pipePtr->flags & PIPE_DELETED)) { |
| 2412 |
errno = EBADF; |
| 2413 |
return -1; |
| 2414 |
} |
| 2415 |
if (WaitForSingleObject(pipePtr->readyEvent, 0) == WAIT_TIMEOUT) { |
| 2416 |
|
| 2417 |
|
| 2418 |
|
| 2419 |
|
| 2420 |
errno = EAGAIN; |
| 2421 |
return -1; |
| 2422 |
} |
| 2423 |
|
| 2424 |
if (pipePtr->lastError) { |
| 2425 |
TclWinConvertError(pipePtr->lastError); |
| 2426 |
pipePtr->lastError = 0; |
| 2427 |
return -1; |
| 2428 |
} |
| 2429 |
|
| 2430 |
if (size > pipePtr->size) { |
| 2431 |
char *ptr; |
| 2432 |
|
| 2433 |
ptr = Blt_Malloc(size); |
| 2434 |
assert(ptr); |
| 2435 |
Blt_Free(pipePtr->buffer); |
| 2436 |
pipePtr->buffer = ptr; |
| 2437 |
} |
| 2438 |
memcpy(pipePtr->buffer, buffer, size); |
| 2439 |
pipePtr->end = pipePtr->size = size; |
| 2440 |
ResetEvent(pipePtr->readyEvent); |
| 2441 |
SetEvent(pipePtr->idleEvent); |
| 2442 |
return size; |
| 2443 |
} |