| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include "bltInt.h" |
| 18 |
#include <fcntl.h> |
| 19 |
#include <signal.h> |
| 20 |
|
| 21 |
#include "bltWait.h" |
| 22 |
|
| 23 |
#if (TCL_MAJOR_VERSION == 7) |
| 24 |
typedef pid_t Tcl_Pid; |
| 25 |
|
| 26 |
#define FILEHANDLER_USES_TCLFILES 1 |
| 27 |
|
| 28 |
static int |
| 29 |
Tcl_GetChannelHandle(channel, direction, clientDataPtr) |
| 30 |
Tcl_Channel channel; |
| 31 |
int direction; |
| 32 |
ClientData *clientDataPtr; |
| 33 |
{ |
| 34 |
Tcl_File file; |
| 35 |
|
| 36 |
file = Tcl_GetChannelFile(channel, direction); |
| 37 |
if (file == NULL) { |
| 38 |
return TCL_ERROR; |
| 39 |
} |
| 40 |
*clientDataPtr = (ClientData)Tcl_GetFileInfo(file, NULL); |
| 41 |
return TCL_OK; |
| 42 |
} |
| 43 |
|
| 44 |
#else |
| 45 |
typedef int Tcl_File; |
| 46 |
#endif |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
static int |
| 65 |
OpenFile(fname, mode) |
| 66 |
char *fname; |
| 67 |
int mode; |
| 68 |
{ |
| 69 |
int fd; |
| 70 |
|
| 71 |
fd = open(fname, mode, 0666); |
| 72 |
if (fd != -1) { |
| 73 |
fcntl(fd, F_SETFD, FD_CLOEXEC); |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if (mode & O_WRONLY) { |
| 81 |
lseek(fd, 0, SEEK_END); |
| 82 |
} |
| 83 |
return fd; |
| 84 |
} |
| 85 |
return -1; |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
static int |
| 107 |
CreateTempFile(contents) |
| 108 |
char *contents; |
| 109 |
{ |
| 110 |
char fileName[L_tmpnam]; |
| 111 |
int fd; |
| 112 |
size_t length = (contents == NULL) ? 0 : strlen(contents); |
| 113 |
|
| 114 |
mkstemp(fileName); |
| 115 |
fd = OpenFile(fileName, O_RDWR | O_CREAT | O_TRUNC); |
| 116 |
unlink(fileName); |
| 117 |
|
| 118 |
if ((fd >= 0) && (length > 0)) { |
| 119 |
for (;;) { |
| 120 |
if (write(fd, contents, length) != -1) { |
| 121 |
break; |
| 122 |
} else if (errno != EINTR) { |
| 123 |
close(fd); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
} |
| 127 |
lseek(fd, 0, SEEK_SET); |
| 128 |
} |
| 129 |
return fd; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
static int |
| 149 |
CreatePipe(inFilePtr, outFilePtr) |
| 150 |
int *inFilePtr; |
| 151 |
int *outFilePtr; |
| 152 |
{ |
| 153 |
int pipeIds[2]; |
| 154 |
|
| 155 |
if (pipe(pipeIds) != 0) { |
| 156 |
return 0; |
| 157 |
} |
| 158 |
fcntl(pipeIds[0], F_SETFD, FD_CLOEXEC); |
| 159 |
fcntl(pipeIds[1], F_SETFD, FD_CLOEXEC); |
| 160 |
|
| 161 |
*inFilePtr = pipeIds[0]; |
| 162 |
*outFilePtr = pipeIds[1]; |
| 163 |
return 1; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
static int |
| 183 |
CloseFile(fd) |
| 184 |
int fd; |
| 185 |
{ |
| 186 |
if ((fd == 0) || (fd == 1) || (fd == 2)) { |
| 187 |
return 0; |
| 188 |
} |
| 189 |
#if (TCL_MAJOR_VERSION > 7) |
| 190 |
Tcl_DeleteFileHandler(fd); |
| 191 |
#endif |
| 192 |
return close(fd); |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
static void |
| 214 |
RestoreSignals() |
| 215 |
{ |
| 216 |
#ifdef SIGABRT |
| 217 |
signal(SIGABRT, SIG_DFL); |
| 218 |
#endif |
| 219 |
#ifdef SIGALRM |
| 220 |
signal(SIGALRM, SIG_DFL); |
| 221 |
#endif |
| 222 |
#ifdef SIGFPE |
| 223 |
signal(SIGFPE, SIG_DFL); |
| 224 |
#endif |
| 225 |
#ifdef SIGHUP |
| 226 |
signal(SIGHUP, SIG_DFL); |
| 227 |
#endif |
| 228 |
#ifdef SIGILL |
| 229 |
signal(SIGILL, SIG_DFL); |
| 230 |
#endif |
| 231 |
#ifdef SIGINT |
| 232 |
signal(SIGINT, SIG_DFL); |
| 233 |
#endif |
| 234 |
#ifdef SIGPIPE |
| 235 |
signal(SIGPIPE, SIG_DFL); |
| 236 |
#endif |
| 237 |
#ifdef SIGQUIT |
| 238 |
signal(SIGQUIT, SIG_DFL); |
| 239 |
#endif |
| 240 |
#ifdef SIGSEGV |
| 241 |
signal(SIGSEGV, SIG_DFL); |
| 242 |
#endif |
| 243 |
#ifdef SIGTERM |
| 244 |
signal(SIGTERM, SIG_DFL); |
| 245 |
#endif |
| 246 |
#ifdef SIGUSR1 |
| 247 |
signal(SIGUSR1, SIG_DFL); |
| 248 |
#endif |
| 249 |
#ifdef SIGUSR2 |
| 250 |
signal(SIGUSR2, SIG_DFL); |
| 251 |
#endif |
| 252 |
#ifdef SIGCHLD |
| 253 |
signal(SIGCHLD, SIG_DFL); |
| 254 |
#endif |
| 255 |
#ifdef SIGCONT |
| 256 |
signal(SIGCONT, SIG_DFL); |
| 257 |
#endif |
| 258 |
#ifdef SIGTSTP |
| 259 |
signal(SIGTSTP, SIG_DFL); |
| 260 |
#endif |
| 261 |
#ifdef SIGTTIN |
| 262 |
signal(SIGTTIN, SIG_DFL); |
| 263 |
#endif |
| 264 |
#ifdef SIGTTOU |
| 265 |
signal(SIGTTOU, SIG_DFL); |
| 266 |
#endif |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
static int |
| 289 |
SetupStdFile(fd, type) |
| 290 |
int fd; |
| 291 |
int type; |
| 292 |
{ |
| 293 |
int targetFd = 0; |
| 294 |
int direction = 0; |
| 295 |
|
| 296 |
|
| 297 |
switch (type) { |
| 298 |
case TCL_STDIN: |
| 299 |
targetFd = 0; |
| 300 |
direction = TCL_READABLE; |
| 301 |
break; |
| 302 |
case TCL_STDOUT: |
| 303 |
targetFd = 1; |
| 304 |
direction = TCL_WRITABLE; |
| 305 |
break; |
| 306 |
case TCL_STDERR: |
| 307 |
targetFd = 2; |
| 308 |
direction = TCL_WRITABLE; |
| 309 |
break; |
| 310 |
} |
| 311 |
if (fd < 0) { |
| 312 |
Tcl_Channel channel; |
| 313 |
|
| 314 |
channel = Tcl_GetStdChannel(type); |
| 315 |
if (channel) { |
| 316 |
Tcl_GetChannelHandle(channel, direction, (ClientData *)&fd); |
| 317 |
} |
| 318 |
} |
| 319 |
if (fd >= 0) { |
| 320 |
if (fd != targetFd) { |
| 321 |
if (dup2(fd, targetFd) == -1) { |
| 322 |
return 0; |
| 323 |
} |
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
fcntl(targetFd, F_SETFD, 0); |
| 331 |
} else { |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
fcntl(fd, F_SETFD, 0); |
| 337 |
} |
| 338 |
} else { |
| 339 |
close(targetFd); |
| 340 |
} |
| 341 |
return 1; |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
static int |
| 370 |
CreateProcess(interp, argc, argv, inputFile, outputFile, errorFile, pidPtr) |
| 371 |
Tcl_Interp *interp; |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
int argc; |
| 376 |
char **argv; |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
int inputFile; |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
int outputFile; |
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
int errorFile; |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
int *pidPtr; |
| 396 |
|
| 397 |
|
| 398 |
{ |
| 399 |
int errPipeIn, errPipeOut; |
| 400 |
int joinThisError, count, status, fd; |
| 401 |
char errSpace[200]; |
| 402 |
int pid; |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
Tcl_DString *dsArray; |
| 407 |
char **newArgv; |
| 408 |
int i; |
| 409 |
|
| 410 |
errPipeIn = errPipeOut = -1; |
| 411 |
pid = -1; |
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
if (CreatePipe(&errPipeIn, &errPipeOut) == 0) { |
| 419 |
Tcl_AppendResult(interp, "can't create pipe: ", |
| 420 |
Tcl_PosixError(interp), (char *)NULL); |
| 421 |
goto error; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
dsArray = (Tcl_DString *) ckalloc(argc * sizeof(Tcl_DString)); |
| 430 |
newArgv = (char **) ckalloc((argc+1) * sizeof(char *)); |
| 431 |
newArgv[argc] = NULL; |
| 432 |
for (i = 0; i < argc; i++) { |
| 433 |
newArgv[i] = Tcl_UtfToExternalDString(NULL, argv[i], -1, &dsArray[i]); |
| 434 |
} |
| 435 |
|
| 436 |
joinThisError = (errorFile == outputFile); |
| 437 |
pid = fork(); |
| 438 |
if (pid == 0) { |
| 439 |
fd = errPipeOut; |
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
if (!SetupStdFile(inputFile, TCL_STDIN) || |
| 446 |
!SetupStdFile(outputFile, TCL_STDOUT) || |
| 447 |
(!joinThisError && !SetupStdFile(errorFile, TCL_STDERR)) || |
| 448 |
(joinThisError && |
| 449 |
((dup2(1, 2) == -1) || (fcntl(2, F_SETFD, 0) != 0)))) { |
| 450 |
sprintf(errSpace, "%dforked process can't set up input/output: ", |
| 451 |
errno); |
| 452 |
write(fd, errSpace, (size_t) strlen(errSpace)); |
| 453 |
_exit(1); |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
RestoreSignals(); |
| 460 |
|
| 461 |
execvp(newArgv[0], newArgv); |
| 462 |
sprintf(errSpace, "%dcan't execute \"%.150s\": ", errno, argv[0]); |
| 463 |
write(fd, errSpace, (size_t) strlen(errSpace)); |
| 464 |
_exit(1); |
| 465 |
} |
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
for (i = 0; i < argc; i++) { |
| 472 |
Tcl_DStringFree(&dsArray[i]); |
| 473 |
} |
| 474 |
ckfree((char *) dsArray); |
| 475 |
ckfree((char *) newArgv); |
| 476 |
|
| 477 |
if (pid == -1) { |
| 478 |
Tcl_AppendResult(interp, "can't fork child process: ", |
| 479 |
Tcl_PosixError(interp), (char *)NULL); |
| 480 |
goto error; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
CloseFile(errPipeOut); |
| 489 |
errPipeOut = -1; |
| 490 |
|
| 491 |
fd = errPipeIn; |
| 492 |
count = read(fd, errSpace, (size_t) (sizeof(errSpace) - 1)); |
| 493 |
if (count > 0) { |
| 494 |
char *end; |
| 495 |
|
| 496 |
errSpace[count] = 0; |
| 497 |
errno = strtol(errSpace, &end, 10); |
| 498 |
Tcl_AppendResult(interp, end, Tcl_PosixError(interp), (char *)NULL); |
| 499 |
goto error; |
| 500 |
} |
| 501 |
CloseFile(errPipeIn); |
| 502 |
*pidPtr = pid; |
| 503 |
return TCL_OK; |
| 504 |
|
| 505 |
error: |
| 506 |
if (pid != -1) { |
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| •511 |
Tcl_WaitPid((Tcl_Pid)(intptr_t)pid, &status, WNOHANG); |
| 512 |
} |
| 513 |
if (errPipeIn >= 0) { |
| 514 |
CloseFile(errPipeIn); |
| 515 |
} |
| 516 |
if (errPipeOut >= 0) { |
| 517 |
CloseFile(errPipeOut); |
| 518 |
} |
| 519 |
return TCL_ERROR; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
static int |
| 544 |
FileForRedirect(interp, spec, atOK, arg, nextArg, flags, skipPtr, closePtr) |
| 545 |
Tcl_Interp *interp; |
| 546 |
char *spec; |
| 547 |
|
| 548 |
char *arg; |
| 549 |
|
| 550 |
int atOK; |
| 551 |
|
| 552 |
|
| 553 |
char *nextArg; |
| 554 |
|
| 555 |
|
| 556 |
int flags; |
| 557 |
|
| 558 |
int *skipPtr; |
| 559 |
|
| 560 |
int *closePtr; |
| 561 |
|
| 562 |
|
| 563 |
{ |
| 564 |
int writing = (flags & O_WRONLY); |
| 565 |
Tcl_Channel chan; |
| 566 |
int fd; |
| 567 |
int direction; |
| 568 |
|
| 569 |
*skipPtr = 1; |
| 570 |
if ((atOK != 0) && (*spec == '@')) { |
| 571 |
spec++; |
| 572 |
if (*spec == '\0') { |
| 573 |
spec = nextArg; |
| 574 |
if (spec == NULL) { |
| 575 |
goto badLastArg; |
| 576 |
} |
| 577 |
*skipPtr = 2; |
| 578 |
} |
| 579 |
if (*spec == '1' && spec[1] == 0) { |
| 580 |
spec = "stdout"; |
| 581 |
} |
| 582 |
chan = Tcl_GetChannel(interp, spec, NULL); |
| 583 |
if (chan == NULL) { |
| 584 |
return -1; |
| 585 |
} |
| 586 |
direction = (writing) ? TCL_WRITABLE : TCL_READABLE; |
| 587 |
if (Tcl_GetChannelHandle(chan, direction, (ClientData *)&fd) != TCL_OK) { |
| 588 |
fd = -1; |
| 589 |
} |
| 590 |
if (fd < 0) { |
| 591 |
Tcl_AppendResult(interp, "channel \"", Tcl_GetChannelName(chan), |
| 592 |
"\" wasn't opened for ", |
| 593 |
((writing) ? "writing" : "reading"), (char *)NULL); |
| 594 |
return -1; |
| 595 |
} |
| 596 |
if (writing) { |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
Tcl_Flush(chan); |
| 603 |
} |
| 604 |
} else { |
| 605 |
char *name; |
| 606 |
Tcl_DString nameString; |
| 607 |
|
| 608 |
if (*spec == '\0') { |
| 609 |
spec = nextArg; |
| 610 |
if (spec == NULL) { |
| 611 |
goto badLastArg; |
| 612 |
} |
| 613 |
*skipPtr = 2; |
| 614 |
} |
| 615 |
name = Tcl_TranslateFileName(interp, spec, &nameString); |
| 616 |
|
| 617 |
if (name != NULL) { |
| 618 |
fd = OpenFile(name, flags); |
| 619 |
} else { |
| 620 |
fd = -1; |
| 621 |
} |
| 622 |
Tcl_DStringFree(&nameString); |
| 623 |
if (fd < 0) { |
| 624 |
Tcl_AppendResult(interp, "can't ", |
| 625 |
((writing) ? "write" : "read"), " file \"", spec, "\": ", |
| 626 |
Tcl_PosixError(interp), (char *)NULL); |
| 627 |
return -1; |
| 628 |
} |
| 629 |
*closePtr = 1; |
| 630 |
} |
| 631 |
return fd; |
| 632 |
|
| 633 |
badLastArg: |
| 634 |
Tcl_AppendResult(interp, "can't specify \"", arg, |
| 635 |
"\" as last word in command", (char *)NULL); |
| 636 |
return -1; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
int |
| 669 |
Blt_CreatePipeline(interp, argc, argv, pidArrayPtr, inPipePtr, |
| 670 |
outPipePtr, errPipePtr) |
| 671 |
Tcl_Interp *interp; |
| 672 |
int argc; |
| 673 |
char **argv; |
| 674 |
|
| 675 |
|
| 676 |
int **pidArrayPtr; |
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
int *inPipePtr; |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
int *outPipePtr; |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
int *errPipePtr; |
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
{ |
| 704 |
int *pidPtr = NULL; |
| 705 |
|
| 706 |
int nPids; |
| 707 |
|
| 708 |
int cmdCount; |
| 709 |
|
| 710 |
char *inputLiteral = NULL; |
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
int inputFd = -1; |
| 715 |
|
| 716 |
|
| 717 |
int inputClose = 0; |
| 718 |
|
| 719 |
int outputFd = -1; |
| 720 |
|
| 721 |
|
| 722 |
int outputClose = 0; |
| 723 |
|
| 724 |
int errorFd = -1; |
| 725 |
|
| 726 |
|
| 727 |
int errorClose = 0; |
| 728 |
|
| 729 |
char *p; |
| 730 |
int skip, lastBar, lastArg, i, j, atOK, flags, errorToOutput; |
| 731 |
Tcl_DString execBuffer; |
| 732 |
int pipeIn; |
| 733 |
int curInFd, curOutFd, curErrFd; |
| 734 |
|
| 735 |
if (inPipePtr != NULL) { |
| 736 |
*inPipePtr = -1; |
| 737 |
} |
| 738 |
if (outPipePtr != NULL) { |
| 739 |
*outPipePtr = -1; |
| 740 |
} |
| 741 |
if (errPipePtr != NULL) { |
| 742 |
*errPipePtr = -1; |
| 743 |
} |
| 744 |
Tcl_DStringInit(&execBuffer); |
| 745 |
|
| 746 |
pipeIn = curInFd = curOutFd = -1; |
| 747 |
nPids = 0; |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
|
| 762 |
lastBar = -1; |
| 763 |
cmdCount = 1; |
| 764 |
for (i = 0; i < argc; i++) { |
| 765 |
skip = 0; |
| 766 |
p = argv[i]; |
| 767 |
switch (*p++) { |
| 768 |
case '\\': |
| 769 |
continue; |
| 770 |
|
| 771 |
case '|': |
| 772 |
if (*p == '&') { |
| 773 |
p++; |
| 774 |
} |
| 775 |
if (*p == '\0') { |
| 776 |
if ((i == (lastBar + 1)) || (i == (argc - 1))) { |
| 777 |
Tcl_AppendResult(interp, |
| 778 |
"illegal use of | or |& in command", |
| 779 |
(char *)NULL); |
| 780 |
goto error; |
| 781 |
} |
| 782 |
} |
| 783 |
lastBar = i; |
| 784 |
cmdCount++; |
| 785 |
break; |
| 786 |
|
| 787 |
case '<': |
| 788 |
if (inputClose != 0) { |
| 789 |
inputClose = 0; |
| 790 |
CloseFile(inputFd); |
| 791 |
} |
| 792 |
if (*p == '<') { |
| 793 |
inputFd = -1; |
| 794 |
inputLiteral = p + 1; |
| 795 |
skip = 1; |
| 796 |
if (*inputLiteral == '\0') { |
| 797 |
inputLiteral = argv[i + 1]; |
| 798 |
if (inputLiteral == NULL) { |
| 799 |
Tcl_AppendResult(interp, "can't specify \"", argv[i], |
| 800 |
"\" as last word in command", (char *)NULL); |
| 801 |
goto error; |
| 802 |
} |
| 803 |
skip = 2; |
| 804 |
} |
| 805 |
} else { |
| 806 |
inputLiteral = NULL; |
| 807 |
inputFd = FileForRedirect(interp, p, 1, argv[i], argv[i + 1], |
| 808 |
O_RDONLY, &skip, &inputClose); |
| 809 |
if (inputFd < 0) { |
| 810 |
goto error; |
| 811 |
} |
| 812 |
} |
| 813 |
break; |
| 814 |
|
| 815 |
case '>': |
| 816 |
atOK = 1; |
| 817 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 818 |
errorToOutput = 0; |
| 819 |
if (*p == '>') { |
| 820 |
p++; |
| 821 |
atOK = 0; |
| 822 |
flags = O_WRONLY | O_CREAT; |
| 823 |
} |
| 824 |
if (*p == '&') { |
| 825 |
if (errorClose != 0) { |
| 826 |
errorClose = 0; |
| 827 |
CloseFile(errorFd); |
| 828 |
} |
| 829 |
errorToOutput = 1; |
| 830 |
p++; |
| 831 |
} |
| 832 |
if (outputClose != 0) { |
| 833 |
outputClose = 0; |
| 834 |
CloseFile(outputFd); |
| 835 |
} |
| 836 |
outputFd = FileForRedirect(interp, p, atOK, argv[i], argv[i + 1], |
| 837 |
flags, &skip, &outputClose); |
| 838 |
if (outputFd < 0) { |
| 839 |
goto error; |
| 840 |
} |
| 841 |
if (errorToOutput) { |
| 842 |
errorClose = 0; |
| 843 |
errorFd = outputFd; |
| 844 |
} |
| 845 |
break; |
| 846 |
|
| 847 |
case '2': |
| 848 |
if (*p != '>') { |
| 849 |
break; |
| 850 |
} |
| 851 |
p++; |
| 852 |
atOK = 1; |
| 853 |
flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 854 |
if (*p == '>') { |
| 855 |
p++; |
| 856 |
atOK = 0; |
| 857 |
flags = O_WRONLY | O_CREAT; |
| 858 |
} |
| 859 |
if (errorClose != 0) { |
| 860 |
errorClose = 0; |
| 861 |
CloseFile(errorFd); |
| 862 |
} |
| 863 |
errorFd = FileForRedirect(interp, p, atOK, argv[i], argv[i + 1], |
| 864 |
flags, &skip, &errorClose); |
| 865 |
if (errorFd < 0) { |
| 866 |
goto error; |
| 867 |
} |
| 868 |
break; |
| 869 |
} |
| 870 |
|
| 871 |
if (skip != 0) { |
| 872 |
for (j = i + skip; j < argc; j++) { |
| 873 |
argv[j - skip] = argv[j]; |
| 874 |
} |
| 875 |
argc -= skip; |
| 876 |
i -= 1; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
if (inputFd == -1) { |
| 881 |
if (inputLiteral != NULL) { |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
inputFd = CreateTempFile(inputLiteral); |
| 888 |
if (inputFd < 0) { |
| 889 |
Tcl_AppendResult(interp, |
| 890 |
"can't create input file for command: ", |
| 891 |
Tcl_PosixError(interp), (char *)NULL); |
| 892 |
goto error; |
| 893 |
} |
| 894 |
inputClose = 1; |
| 895 |
} else if (inPipePtr != NULL) { |
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
if (CreatePipe(&inputFd, inPipePtr) == 0) { |
| 902 |
Tcl_AppendResult(interp, |
| 903 |
"can't create input pipe for command: ", |
| 904 |
Tcl_PosixError(interp), (char *)NULL); |
| 905 |
goto error; |
| 906 |
} |
| 907 |
inputClose = 1; |
| 908 |
} else { |
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
inputFd = 0; |
| 914 |
} |
| 915 |
} |
| 916 |
if (outputFd == -1) { |
| 917 |
if (outPipePtr != NULL) { |
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
if (CreatePipe(outPipePtr, &outputFd) == 0) { |
| 924 |
Tcl_AppendResult(interp, |
| 925 |
"can't create output pipe for command: ", |
| 926 |
Tcl_PosixError(interp), (char *)NULL); |
| 927 |
goto error; |
| 928 |
} |
| 929 |
outputClose = 1; |
| 930 |
} else { |
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
outputFd = 1; |
| 935 |
} |
| 936 |
} |
| 937 |
if (errorFd == -1) { |
| 938 |
if (errPipePtr != NULL) { |
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
if (CreatePipe(errPipePtr, &errorFd) == 0) { |
| 944 |
Tcl_AppendResult(interp, |
| 945 |
"can't create error pipe for command: ", |
| 946 |
Tcl_PosixError(interp), (char *)NULL); |
| 947 |
goto error; |
| 948 |
} |
| 949 |
errorClose = 1; |
| 950 |
} else { |
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
errorFd = 2; |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
Tcl_ReapDetachedProcs(); |
| 963 |
pidPtr = Blt_Malloc((unsigned)(cmdCount * sizeof(int))); |
| 964 |
|
| 965 |
curInFd = inputFd; |
| 966 |
|
| 967 |
lastArg = 0; |
| 968 |
for (i = 0; i < argc; i = lastArg + 1) { |
| 969 |
int joinThisError; |
| 970 |
int pid; |
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
argv[i] = Tcl_TranslateFileName(interp, argv[i], &execBuffer); |
| 977 |
if (argv[i] == NULL) { |
| 978 |
goto error; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
joinThisError = 0; |
| 984 |
for (lastArg = i; lastArg < argc; lastArg++) { |
| 985 |
if (argv[lastArg][0] == '|') { |
| 986 |
if (argv[lastArg][1] == '\0') { |
| 987 |
break; |
| 988 |
} |
| 989 |
if ((argv[lastArg][1] == '&') && (argv[lastArg][2] == '\0')) { |
| 990 |
joinThisError = 1; |
| 991 |
break; |
| 992 |
} |
| 993 |
} |
| 994 |
} |
| 995 |
argv[lastArg] = NULL; |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
if (lastArg == argc) { |
| 1004 |
curOutFd = outputFd; |
| 1005 |
} else { |
| 1006 |
if (CreatePipe(&pipeIn, &curOutFd) == 0) { |
| 1007 |
Tcl_AppendResult(interp, "can't create pipe: ", |
| 1008 |
Tcl_PosixError(interp), (char *)NULL); |
| 1009 |
goto error; |
| 1010 |
} |
| 1011 |
} |
| 1012 |
|
| 1013 |
if (joinThisError != 0) { |
| 1014 |
curErrFd = curOutFd; |
| 1015 |
} else { |
| 1016 |
curErrFd = errorFd; |
| 1017 |
} |
| 1018 |
|
| 1019 |
if (CreateProcess(interp, lastArg - i, argv + i, |
| 1020 |
curInFd, curOutFd, curErrFd, &pid) != TCL_OK) { |
| 1021 |
goto error; |
| 1022 |
} |
| 1023 |
Tcl_DStringFree(&execBuffer); |
| 1024 |
|
| 1025 |
pidPtr[nPids] = pid; |
| 1026 |
nPids++; |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
if ((curInFd >= 0) && (curInFd != inputFd)) { |
| 1035 |
CloseFile(curInFd); |
| 1036 |
} |
| 1037 |
curInFd = pipeIn; |
| 1038 |
pipeIn = -1; |
| 1039 |
|
| 1040 |
if ((curOutFd >= 0) && (curOutFd != outputFd)) { |
| 1041 |
CloseFile(curOutFd); |
| 1042 |
} |
| 1043 |
curOutFd = -1; |
| 1044 |
} |
| 1045 |
|
| 1046 |
*pidArrayPtr = pidPtr; |
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
cleanup: |
| 1053 |
Tcl_DStringFree(&execBuffer); |
| 1054 |
|
| 1055 |
if (inputClose) { |
| 1056 |
CloseFile(inputFd); |
| 1057 |
} |
| 1058 |
if (outputClose) { |
| 1059 |
CloseFile(outputFd); |
| 1060 |
} |
| 1061 |
if (errorClose) { |
| 1062 |
CloseFile(errorFd); |
| 1063 |
} |
| 1064 |
return nPids; |
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
error: |
| 1073 |
if (pipeIn >= 0) { |
| 1074 |
CloseFile(pipeIn); |
| 1075 |
} |
| 1076 |
if ((curOutFd >= 0) && (curOutFd != outputFd)) { |
| 1077 |
CloseFile(curOutFd); |
| 1078 |
} |
| 1079 |
if ((curInFd >= 0) && (curInFd != inputFd)) { |
| 1080 |
CloseFile(curInFd); |
| 1081 |
} |
| 1082 |
if ((inPipePtr != NULL) && (*inPipePtr >= 0)) { |
| 1083 |
CloseFile(*inPipePtr); |
| 1084 |
*inPipePtr = -1; |
| 1085 |
} |
| 1086 |
if ((outPipePtr != NULL) && (*outPipePtr >= 0)) { |
| 1087 |
CloseFile(*outPipePtr); |
| 1088 |
*outPipePtr = -1; |
| 1089 |
} |
| 1090 |
if ((errPipePtr != NULL) && (*errPipePtr >= 0)) { |
| 1091 |
CloseFile(*errPipePtr); |
| 1092 |
*errPipePtr = -1; |
| 1093 |
} |
| 1094 |
if (pidPtr != NULL) { |
| 1095 |
for (i = 0; i < nPids; i++) { |
| 1096 |
if (pidPtr[i] != -1) { |
| 1097 |
#if (TCL_MAJOR_VERSION == 7) |
| 1098 |
Tcl_DetachPids(1, &pidPtr[i]); |
| 1099 |
#else |
| 1100 |
Tcl_DetachPids(1, (Tcl_Pid *)&pidPtr[i]); |
| 1101 |
#endif |
| 1102 |
} |
| 1103 |
} |
| 1104 |
Blt_Free(pidPtr); |
| 1105 |
} |
| 1106 |
nPids = -1; |
| 1107 |
goto cleanup; |
| 1108 |
} |