| ︙ | | |
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
-
-
-
+
+
+
|
TCL_DECLARE_MUTEX(pipeMutex) /* Guard access to detList. */
/*
* Declarations for local functions defined in this file:
*/
static TclFile FileForRedirect(Tcl_Interp *interp, const char *spec,
int atOk, const char *arg, const char *nextArg,
int flags, int *skipPtr, int *closePtr,
int *releasePtr);
bool atOK, const char *arg, const char *nextArg,
int flags, int *skipPtr, bool *closePtr,
bool *releasePtr);
/*
*----------------------------------------------------------------------
*
* FileForRedirect --
*
* This function does much of the work of parsing redirection operators.
|
| ︙ | | |
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
-
-
+
+
-
+
-
+
-
+
-
+
|
*/
static TclFile
FileForRedirect(
Tcl_Interp *interp, /* Interpreter to use for error reporting. */
const char *spec, /* Points to character just after redirection
* character. */
int atOK, /* Non-zero means that '@' notation can be
* used to specify a channel, zero means that
bool atOK, /* True means that '@' notation can be
* used to specify a channel, false means that
* it isn't. */
const char *arg, /* Pointer to entire argument containing spec:
* used for error reporting. */
const char *nextArg, /* Next argument in argc/argv array, if needed
* for file name or channel name. May be
* NULL. */
int flags, /* Flags to use for opening file or to specify
* mode for channel. */
int *skipPtr, /* Filled with 1 if redirection target was in
* spec, 2 if it was in nextArg. */
int *closePtr, /* Filled with one if the caller should close
bool *closePtr, /* Filled with one if the caller should close
* the file when done with it, zero
* otherwise. */
int *releasePtr)
bool *releasePtr)
{
int writing = (flags & O_WRONLY);
bool writing = (flags & O_WRONLY);
Tcl_Channel chan;
TclFile file;
*skipPtr = 1;
if ((atOK != 0) && (*spec == '@')) {
if (atOK && (*spec == '@')) {
spec++;
if (*spec == '\0') {
spec = nextArg;
if (spec == NULL) {
goto badLastArg;
}
*skipPtr = 2;
|
| ︙ | | |
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
-
+
|
TclPrintfResult(interp, "channel \"%s\" wasn't opened for %s",
TclGetChannelName(chan),
(writing ? "writing" : "reading"));
TclSetErrorCode(interp, "TCL", "OPERATION", "EXEC", "BADCHAN");
}
return NULL;
}
*releasePtr = 1;
*releasePtr = true;
if (writing) {
/*
* Be sure to flush output to the file, so that anything written
* by the child appears after stuff we've already written.
*/
Tcl_Flush(chan);
|
| ︙ | | |
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
|
-
+
|
Tcl_DStringFree(&nameString);
if (file == NULL) {
TclPrintfResult(interp, "couldn't %s file \"%s\": %s",
(writing ? "write" : "read"), spec,
Tcl_PosixError(interp));
return NULL;
}
*closePtr = 1;
*closePtr = true;
}
return file;
badLastArg:
TclPrintfResult(interp,
"can't specify \"%s\" as last word in command", arg);
TclSetErrorCode(interp, "TCL", "OPERATION", "EXEC", "SYNTAX");
|
| ︙ | | |
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
-
-
+
+
-
+
|
Tcl_Interp *interp, /* Used for error messages. */
Tcl_Size numPids, /* Number of entries in pidPtr array. */
Tcl_Pid *pidPtr, /* Array of process ids of children. */
Tcl_Channel errorChan) /* Channel for file containing stderr output
* from pipeline. NULL means there isn't any
* stderr output. */
{
int result = TCL_OK;
int code, abnormalExit, anyErrorInfo;
int result = TCL_OK, code;
bool abnormalExit, anyErrorInfo;
TclProcessWaitStatus waitStatus;
Tcl_Obj *msg, *error;
abnormalExit = 0;
abnormalExit = false;
for (Tcl_Size i = 0; i < numPids; i++) {
waitStatus = TclProcessWait(pidPtr[i], 0, &code, &msg, &error);
if (waitStatus == TCL_PROCESS_ERROR) {
result = TCL_ERROR;
if (interp != NULL) {
Tcl_SetObjErrorCode(interp, error);
Tcl_SetObjResult(interp, msg);
|
| ︙ | | |
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
-
+
-
+
-
+
-
+
|
if (waitStatus != TCL_PROCESS_EXITED || code != 0) {
result = TCL_ERROR;
if (waitStatus == TCL_PROCESS_EXITED) {
if (interp != NULL) {
Tcl_SetObjErrorCode(interp, error);
}
abnormalExit = 1;
abnormalExit = true;
} else if (interp != NULL) {
Tcl_SetObjErrorCode(interp, error);
Tcl_SetObjResult(interp, msg);
}
Tcl_DecrRefCount(error);
Tcl_DecrRefCount(msg);
}
}
/*
* Read the standard error file. If there's anything there, then return an
* error and add the file's contents to the result string.
*/
anyErrorInfo = 0;
anyErrorInfo = false;
if (errorChan != NULL) {
/*
* Make sure we start at the beginning of the file.
*/
if (interp != NULL) {
Tcl_Size count;
Tcl_Obj *objPtr;
Tcl_Seek(errorChan, 0, SEEK_SET);
TclNewObj(objPtr);
count = Tcl_ReadChars(errorChan, objPtr, TCL_INDEX_NONE, 0);
if (count == -1) {
result = TCL_ERROR;
Tcl_DecrRefCount(objPtr);
Tcl_ResetResult(interp);
TclPrintfResult(interp, "error reading stderr output file: %s",
Tcl_PosixError(interp));
} else if (count > 0) {
anyErrorInfo = 1;
anyErrorInfo = true;
Tcl_SetObjResult(interp, objPtr);
result = TCL_ERROR;
} else {
Tcl_DecrRefCount(objPtr);
}
}
Tcl_CloseEx(NULL, errorChan, 0);
}
/*
* If a child exited abnormally but didn't output any error information at
* all, generate an error message here.
*/
if ((abnormalExit != 0) && (anyErrorInfo == 0) && (interp != NULL)) {
if (abnormalExit && !anyErrorInfo && (interp != NULL)) {
TclPrintfResult(interp, "child process exited abnormally");
}
return result;
}
/*
*----------------------------------------------------------------------
|
| ︙ | | |
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
|
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
-
+
-
+
-
+
-
+
-
+
-
-
+
+
-
-
+
+
-
-
+
|
/* If non-null, then this points to a string
* containing input data (specified via <<) to
* be piped to the first process in the
* pipeline. */
TclFile inputFile = NULL; /* If != NULL, gives file to use as input for
* first process in pipeline (specified via <
* or <@). */
int inputClose = 0; /* If non-zero, then inputFile should be
bool inputClose = false; /* If non-zero, then inputFile should be
* closed when cleaning up. */
int inputRelease = 0;
bool inputRelease = false;
TclFile outputFile = NULL; /* Writable file for output from last command
* in pipeline (could be file or pipe). NULL
* means use stdout. */
int outputClose = 0; /* If non-zero, then outputFile should be
bool outputClose = false; /* If non-zero, then outputFile should be
* closed when cleaning up. */
int outputRelease = 0;
bool outputRelease = false;
TclFile errorFile = NULL; /* Writable file for error output from all
* commands in pipeline. NULL means use
* stderr. */
int errorClose = 0; /* If non-zero, then errorFile should be
bool errorClose = false; /* If non-zero, then errorFile should be
* closed when cleaning up. */
int errorRelease = 0;
const char *p;
bool errorRelease = false;
const char *p, *nextArg;
const char *nextArg;
int skip, atOK, flags, needCmd, errorToOutput = 0;
int skip, flags, errorToOutput = 0;
bool atOK, needCmd;
Tcl_Size lastArg, lastBar;
Tcl_DString execBuffer;
TclFile pipeIn;
TclFile curInFile, curOutFile, curErrFile;
TclFile pipeIn, curInFile, curOutFile, curErrFile;
Tcl_Channel channel;
if (inPipePtr != NULL) {
*inPipePtr = NULL;
}
if (outPipePtr != NULL) {
*outPipePtr = NULL;
|
| ︙ | | |
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
|
-
+
-
+
-
-
+
+
-
-
+
+
|
* appear anywhere in the command line - e.g., the '<' that specifies the
* input to the entire pipe may appear at the very end of the argument
* list.
*/
lastBar = -1;
cmdCount = 1;
needCmd = 1;
needCmd = true;
for (Tcl_Size i = 0; i < argc; i++) {
errorToOutput = 0;
skip = 0;
p = argv[i];
switch (*p++) {
case '|':
if (*p == '&') {
p++;
}
if (*p == '\0') {
if ((i == (lastBar + 1)) || (i == (argc - 1))) {
TclPrintfResult(interp, "illegal use of | or |& in command");
TclSetErrorCode(interp, "TCL", "OPERATION", "EXEC",
"PIPESYNTAX");
goto error;
}
}
lastBar = i;
cmdCount++;
needCmd = 1;
needCmd = true;
break;
case '<':
if (inputClose != 0) {
inputClose = 0;
if (inputClose) {
inputClose = false;
TclpCloseFile(inputFile);
}
if (inputRelease != 0) {
inputRelease = 0;
if (inputRelease) {
inputRelease = false;
TclpReleaseFile(inputFile);
}
if (*p == '<') {
inputFile = NULL;
inputLiteral = p + 1;
skip = 1;
if (*inputLiteral == '\0') {
|
| ︙ | | |
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
|
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
|
-
+
-
+
-
+
-
-
+
+
-
+
-
-
+
+
-
+
-
-
+
+
-
-
+
+
-
+
-
+
-
-
+
+
-
-
+
+
|
if (inputFile == NULL) {
goto error;
}
}
break;
case '>':
atOK = 1;
atOK = true;
flags = O_WRONLY | O_CREAT | O_TRUNC;
if (*p == '>') {
p++;
atOK = 0;
atOK = false;
/*
* Note that the O_APPEND flag only has an effect on POSIX
* platforms. On Windows, we just have to carry on regardless.
*/
flags = O_WRONLY | O_CREAT | O_APPEND;
}
if (*p == '&') {
if (errorClose != 0) {
errorClose = 0;
errorClose = false;
TclpCloseFile(errorFile);
}
errorToOutput = 1;
p++;
}
/*
* Close the old output file, but only if the error file is not
* also using it.
*/
if (outputClose != 0) {
outputClose = 0;
if (outputClose) {
outputClose = false;
if (errorFile == outputFile) {
errorClose = 1;
errorClose = true;
} else {
TclpCloseFile(outputFile);
}
}
if (outputRelease != 0) {
outputRelease = 0;
if (outputRelease) {
outputRelease = false;
if (errorFile == outputFile) {
errorRelease = 1;
errorRelease = true;
} else {
TclpReleaseFile(outputFile);
}
}
nextArg = ((i + 1) == argc) ? NULL : argv[i + 1];
outputFile = FileForRedirect(interp, p, atOK, argv[i], nextArg,
flags, &skip, &outputClose, &outputRelease);
if (outputFile == NULL) {
goto error;
}
if (errorToOutput) {
if (errorClose != 0) {
errorClose = 0;
if (errorClose) {
errorClose = false;
TclpCloseFile(errorFile);
}
if (errorRelease != 0) {
errorRelease = 0;
if (errorRelease) {
errorRelease = false;
TclpReleaseFile(errorFile);
}
errorFile = outputFile;
}
break;
case '2':
if (*p != '>') {
break;
}
p++;
atOK = 1;
atOK = true;
flags = O_WRONLY | O_CREAT | O_TRUNC;
if (*p == '>') {
p++;
atOK = 0;
atOK = false;
/*
* Note that the O_APPEND flag only has an effect on POSIX
* platforms. On Windows, we just have to carry on regardless.
*/
flags = O_WRONLY | O_CREAT | O_APPEND;
}
if (errorClose != 0) {
errorClose = 0;
if (errorClose) {
errorClose = false;
TclpCloseFile(errorFile);
}
if (errorRelease != 0) {
errorRelease = 0;
if (errorRelease) {
errorRelease = false;
TclpReleaseFile(errorFile);
}
if (atOK && p[0] == '@' && p[1] == '1' && p[2] == '\0') {
/*
* Special case handling of 2>@1 to redirect stderr to the
* exec/open output pipe as well. This is meant for the end of
* the command string, otherwise use |& between commands.
|
| ︙ | | |
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
|
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
|
-
+
|
break;
default:
/*
* Got a command word, not a redirection.
*/
needCmd = 0;
needCmd = false;
break;
}
if (skip != 0) {
for (Tcl_Size j = i + skip; j < argc; j++) {
argv[j - skip] = argv[j];
}
|
| ︙ | | |
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
|
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
|
-
+
-
+
-
+
-
+
-
+
|
inputFile = TclpCreateTempFile(inputLiteral);
if (inputFile == NULL) {
TclPrintfResult(interp,
"couldn't create input file for command: %s",
Tcl_PosixError(interp));
goto error;
}
inputClose = 1;
inputClose = true;
} else if (inPipePtr != NULL) {
/*
* The input for the first process in the pipeline is to come from
* a pipe that can be written from by the caller.
*/
if (TclpCreatePipe(&inputFile, inPipePtr) == 0) {
TclPrintfResult(interp,
"couldn't create input pipe for command: %s",
Tcl_PosixError(interp));
goto error;
}
inputClose = 1;
inputClose = true;
} else {
/*
* The input for the first process comes from stdin.
*/
channel = Tcl_GetStdChannel(TCL_STDIN);
if (channel != NULL) {
inputFile = TclpMakeFile(channel, TCL_READABLE);
if (inputFile != NULL) {
inputRelease = 1;
inputRelease = true;
}
}
}
}
if (outputFile == NULL) {
if (outPipePtr != NULL) {
/*
* Output from the last process in the pipeline is to go to a pipe
* that can be read by the caller.
*/
if (TclpCreatePipe(outPipePtr, &outputFile) == 0) {
TclPrintfResult(interp,
"couldn't create output pipe for command: %s",
Tcl_PosixError(interp));
goto error;
}
outputClose = 1;
outputClose = true;
} else {
/*
* The output for the last process goes to stdout.
*/
channel = Tcl_GetStdChannel(TCL_STDOUT);
if (channel) {
outputFile = TclpMakeFile(channel, TCL_WRITABLE);
if (outputFile != NULL) {
outputRelease = 1;
outputRelease = true;
}
}
}
}
if (errorFile == NULL) {
if (errorToOutput == 2) {
|
| ︙ | | |
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
-
+
-
+
+
-
+
-
+
|
* Errors from the pipeline go to stderr.
*/
channel = Tcl_GetStdChannel(TCL_STDERR);
if (channel) {
errorFile = TclpMakeFile(channel, TCL_WRITABLE);
if (errorFile != NULL) {
errorRelease = 1;
errorRelease = true;
}
}
}
}
/*
* Scan through the argc array, creating a process for each group of
* arguments between the "|" characters.
*/
Tcl_ReapDetachedProcs();
pidPtr = (Tcl_Pid *)Tcl_Alloc(cmdCount * sizeof(Tcl_Pid));
curInFile = inputFile;
for (Tcl_Size i = 0; i < argc; i = lastArg + 1) {
int result, joinThisError;
int result;
bool joinThisError;
Tcl_Pid pid;
const char *oldName;
/*
* Convert the program name into native form.
*/
if (Tcl_TranslateFileName(interp, argv[i], &execBuffer) == NULL) {
goto error;
}
/*
* Find the end of the current segment of the pipeline.
*/
joinThisError = 0;
joinThisError = false;
for (lastArg = i; lastArg < argc; lastArg++) {
if (argv[lastArg][0] != '|') {
continue;
}
if (argv[lastArg][1] == '\0') {
break;
}
if ((argv[lastArg][1] == '&') && (argv[lastArg][2] == '\0')) {
joinThisError = 1;
joinThisError = true;
break;
}
}
/*
* If this is the last segment, use the specified outputFile.
* Otherwise create an intermediate pipe. pipeIn will become the
|
| ︙ | | |
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
|
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
|
-
+
|
if (TclpCreatePipe(&pipeIn, &curOutFile) == 0) {
TclPrintfResult(interp, "couldn't create pipe: %s",
Tcl_PosixError(interp));
goto error;
}
}
if (joinThisError != 0) {
if (joinThisError) {
curErrFile = curOutFile;
} else {
curErrFile = errorFile;
}
/*
* Restore argv[i], since a caller wouldn't expect the contents of
|
| ︙ | | |