#include "tclInt.h"
#include "tclPort.h"
typedef struct Detached {
Tcl_Pid pid;
struct Detached *nextPtr;
} Detached;
static Detached *detList = NULL;
static TclFile FileForRedirect _ANSI_ARGS_((Tcl_Interp *interp,
char *spec, int atOk, char *arg, char *nextArg,
int flags, int *skipPtr, int *closePtr, int *releasePtr));
static TclFile
FileForRedirect(interp, spec, atOK, arg, nextArg, flags, skipPtr, closePtr,
releasePtr)
Tcl_Interp *interp;
char *spec;
char *arg;
int atOK;
char *nextArg;
int flags;
int *skipPtr;
int *closePtr;
int *releasePtr;
{
int writing = (flags & O_WRONLY);
Tcl_Channel chan;
TclFile file;
*skipPtr = 1;
if ((atOK != 0) && (*spec == '@')) {
spec++;
if (*spec == '\0') {
spec = nextArg;
if (spec == NULL) {
goto badLastArg;
}
*skipPtr = 2;
}
chan = Tcl_GetChannel(interp, spec, NULL);
if (chan == (Tcl_Channel) NULL) {
return NULL;
}
file = TclpMakeFile(chan, writing ? TCL_WRITABLE : TCL_READABLE);
if (file == NULL) {
Tcl_AppendResult(interp, "channel \"", Tcl_GetChannelName(chan),
"\" wasn't opened for ",
((writing) ? "writing" : "reading"), (char *) NULL);
return NULL;
}
*releasePtr = 1;
if (writing) {
Tcl_Flush(chan);
}
} else {
char *name;
Tcl_DString nameString;
if (*spec == '\0') {
spec = nextArg;
if (spec == NULL) {
goto badLastArg;
}
*skipPtr = 2;
}
name = Tcl_TranslateFileName(interp, spec, &nameString);
if (name != NULL) {
file = TclpOpenFile(name, flags);
} else {
file = NULL;
}
Tcl_DStringFree(&nameString);
if (file == NULL) {
Tcl_AppendResult(interp, "couldn't ",
((writing) ? "write" : "read"), " file \"", spec, "\": ",
Tcl_PosixError(interp), (char *) NULL);
return NULL;
}
*closePtr = 1;
}
return file;
badLastArg:
Tcl_AppendResult(interp, "can't specify \"", arg,
"\" as last word in command", (char *) NULL);
return NULL;
}
void
Tcl_DetachPids(numPids, pidPtr)
int numPids;
Tcl_Pid *pidPtr;
{
register Detached *detPtr;
int i;
for (i = 0; i < numPids; i++) {
detPtr = (Detached *) ckalloc(sizeof(Detached));
detPtr->pid = pidPtr[i];
detPtr->nextPtr = detList;
detList = detPtr;
}
}
void
Tcl_ReapDetachedProcs()
{
register Detached *detPtr;
Detached *nextPtr, *prevPtr;
int status;
Tcl_Pid pid;
for (detPtr = detList, prevPtr = NULL; detPtr != NULL; ) {
pid = Tcl_WaitPid(detPtr->pid, &status, WNOHANG);
if ((pid == 0) || ((pid == (Tcl_Pid) -1) && (errno != ECHILD))) {
prevPtr = detPtr;
detPtr = detPtr->nextPtr;
continue;
}
nextPtr = detPtr->nextPtr;
if (prevPtr == NULL) {
detList = detPtr->nextPtr;
} else {
prevPtr->nextPtr = detPtr->nextPtr;
}
ckfree((char *) detPtr);
detPtr = nextPtr;
}
}
int
TclCleanupChildren(interp, numPids, pidPtr, errorChan)
Tcl_Interp *interp;
int numPids;
Tcl_Pid *pidPtr;
Tcl_Channel errorChan;
{
int result = TCL_OK;
int i, abnormalExit, anyErrorInfo;
Tcl_Pid pid;
WAIT_STATUS_TYPE waitStatus;
char *msg;
abnormalExit = 0;
for (i = 0; i < numPids; i++) {
pid = Tcl_WaitPid(pidPtr[i], (int *) &waitStatus, 0);
if (pid == (Tcl_Pid) -1) {
result = TCL_ERROR;
if (interp != (Tcl_Interp *) NULL) {
msg = Tcl_PosixError(interp);
if (errno == ECHILD) {
msg =
"child process lost (is SIGCHLD ignored or trapped?)";
}
Tcl_AppendResult(interp, "error waiting for process to exit: ",
msg, (char *) NULL);
}
continue;
}
if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) {
char msg1[20], msg2[20];
result = TCL_ERROR;
sprintf(msg1, "%ld", TclpGetPid(pid));
if (WIFEXITED(waitStatus)) {
if (interp != (Tcl_Interp *) NULL) {
sprintf(msg2, "%d", WEXITSTATUS(waitStatus));
Tcl_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2,
(char *) NULL);
}
abnormalExit = 1;
} else if (WIFSIGNALED(waitStatus)) {
if (interp != (Tcl_Interp *) NULL) {
char *p;
p = Tcl_SignalMsg((int) (WTERMSIG(waitStatus)));
Tcl_SetErrorCode(interp, "CHILDKILLED", msg1,
Tcl_SignalId((int) (WTERMSIG(waitStatus))), p,
(char *) NULL);
Tcl_AppendResult(interp, "child killed: ", p, "\n",
(char *) NULL);
}
} else if (WIFSTOPPED(waitStatus)) {
if (interp != (Tcl_Interp *) NULL) {
char *p;
p = Tcl_SignalMsg((int) (WSTOPSIG(waitStatus)));
Tcl_SetErrorCode(interp, "CHILDSUSP", msg1,
Tcl_SignalId((int) (WSTOPSIG(waitStatus))),
p, (char *) NULL);
Tcl_AppendResult(interp, "child suspended: ", p, "\n",
(char *) NULL);
}
} else {
if (interp != (Tcl_Interp *) NULL) {
Tcl_AppendResult(interp,
"child wait status didn't make sense\n",
(char *) NULL);
}
}
}
}
anyErrorInfo = 0;
if (errorChan != NULL) {
Tcl_Seek(errorChan, 0L, SEEK_SET);
if (interp != (Tcl_Interp *) NULL) {
while (1) {
#define BUFFER_SIZE 1000
char buffer[BUFFER_SIZE+1];
int count;
count = Tcl_Read(errorChan, buffer, BUFFER_SIZE);
if (count == 0) {
break;
}
result = TCL_ERROR;
if (count < 0) {
Tcl_AppendResult(interp,
"error reading stderr output file: ",
Tcl_PosixError(interp), (char *) NULL);
break;
}
buffer[count] = 0;
Tcl_AppendResult(interp, buffer, (char *) NULL);
anyErrorInfo = 1;
}
}
Tcl_Close((Tcl_Interp *) NULL, errorChan);
}
if (abnormalExit && !anyErrorInfo && (interp != (Tcl_Interp *) NULL)) {
Tcl_AppendResult(interp, "child process exited abnormally",
(char *) NULL);
}
return result;
}
int
TclCreatePipeline(interp, argc, argv, pidArrayPtr, inPipePtr,
outPipePtr, errFilePtr)
Tcl_Interp *interp;
int argc;
char **argv;
Tcl_Pid **pidArrayPtr;
TclFile *inPipePtr;
TclFile *outPipePtr;
TclFile *errFilePtr;
{
Tcl_Pid *pidPtr = NULL;
int numPids;
int cmdCount;
char *inputLiteral = NULL;
TclFile inputFile = NULL;
int inputClose = 0;
int inputRelease = 0;
TclFile outputFile = NULL;
int outputClose = 0;
int outputRelease = 0;
TclFile errorFile = NULL;
int errorClose = 0;
int errorRelease = 0;
int joinError = 0;
char *p;
int skip, lastBar, lastArg, i, j, atOK, flags, errorToOutput;
Tcl_DString execBuffer;
TclFile pipeIn;
TclFile curInFile, curOutFile, curErrFile;
Tcl_Channel channel;
if (inPipePtr != NULL) {
*inPipePtr = NULL;
}
if (outPipePtr != NULL) {
*outPipePtr = NULL;
}
if (errFilePtr != NULL) {
*errFilePtr = NULL;
}
Tcl_DStringInit(&execBuffer);
pipeIn = NULL;
curInFile = NULL;
curOutFile = NULL;
numPids = 0;
lastBar = -1;
cmdCount = 1;
for (i = 0; i < argc; i++) {
skip = 0;
p = argv[i];
switch (*p++) {
case '|':
if (*p == '&') {
p++;
}
if (*p == '\0') {
if ((i == (lastBar + 1)) || (i == (argc - 1))) {
Tcl_SetResult(interp,
"illegal use of | or |& in command",
TCL_STATIC);
goto error;
}
}
lastBar = i;
cmdCount++;
break;
case '<':
if (inputClose != 0) {
inputClose = 0;
TclpCloseFile(inputFile);
}
if (inputRelease != 0) {
inputRelease = 0;
TclpReleaseFile(inputFile);
}
if (*p == '<') {
inputFile = NULL;
inputLiteral = p + 1;
skip = 1;
if (*inputLiteral == '\0') {
inputLiteral = argv[i + 1];
if (inputLiteral == NULL) {
Tcl_AppendResult(interp, "can't specify \"", argv[i],
"\" as last word in command", (char *) NULL);
goto error;
}
skip = 2;
}
} else {
inputLiteral = NULL;
inputFile = FileForRedirect(interp, p, 1, argv[i],
argv[i + 1], O_RDONLY, &skip, &inputClose, &inputRelease);
if (inputFile == NULL) {
goto error;
}
}
break;
case '>':
atOK = 1;
flags = O_WRONLY | O_CREAT | O_TRUNC;
errorToOutput = 0;
if (*p == '>') {
p++;
atOK = 0;
flags = O_WRONLY | O_CREAT;
}
if (*p == '&') {
if (errorClose != 0) {
errorClose = 0;
TclpCloseFile(errorFile);
}
errorToOutput = 1;
p++;
}
if (outputClose != 0) {
outputClose = 0;
if (errorFile == outputFile) {
errorClose = 1;
} else {
TclpCloseFile(outputFile);
}
}
if (outputRelease != 0) {
outputRelease = 0;
if (errorFile == outputFile) {
errorRelease = 1;
} else {
TclpReleaseFile(outputFile);
}
}
outputFile = FileForRedirect(interp, p, atOK, argv[i],
argv[i + 1], flags, &skip, &outputClose, &outputRelease);
if (outputFile == NULL) {
goto error;
}
if (errorToOutput) {
if (errorClose != 0) {
errorClose = 0;
TclpCloseFile(errorFile);
}
if (errorRelease != 0) {
errorRelease = 0;
TclpReleaseFile(errorFile);
}
errorFile = outputFile;
}
break;
case '2':
if (*p != '>') {
break;
}
p++;
atOK = 1;
flags = O_WRONLY | O_CREAT | O_TRUNC;
if (*p == '>') {
p++;
atOK = 0;
flags = O_WRONLY | O_CREAT;
}
if (errorClose != 0) {
errorClose = 0;
TclpCloseFile(errorFile);
}
if (errorRelease != 0) {
errorRelease = 0;
TclpReleaseFile(errorFile);
}
joinError = 0;
if (*p == '@'
&& strcmp ((p[1] == '\0' ? argv[i + 1] : p + 1),
"stdout") == 0) {
joinError = 1;
if (p[1] == '\0') {
skip = 2;
} else {
skip = 1;
}
} else {
errorFile = FileForRedirect(interp, p, atOK, argv[i],
argv[i + 1], flags, &skip, &errorClose, &errorRelease);
if (errorFile == NULL) {
goto error;
}
}
break;
}
if (skip != 0) {
for (j = i + skip; j < argc; j++) {
argv[j - skip] = argv[j];
}
argc -= skip;
i -= 1;
}
}
if (inputFile == NULL) {
if (inputLiteral != NULL) {
inputFile = TclpCreateTempFile(inputLiteral, NULL);
if (inputFile == NULL) {
Tcl_AppendResult(interp,
"couldn't create input file for command: ",
Tcl_PosixError(interp), (char *) NULL);
goto error;
}
inputClose = 1;
} else if (inPipePtr != NULL) {
if (TclpCreatePipe(&inputFile, inPipePtr) == 0) {
Tcl_AppendResult(interp,
"couldn't create input pipe for command: ",
Tcl_PosixError(interp), (char *) NULL);
goto error;
}
inputClose = 1;
} else {
channel = Tcl_GetStdChannel(TCL_STDIN);
if (channel != NULL) {
inputFile = TclpMakeFile(channel, TCL_READABLE);
if (inputFile != NULL) {
inputRelease = 1;
}
}
}
}
if (outputFile == NULL) {
if (outPipePtr != NULL) {
if (TclpCreatePipe(outPipePtr, &outputFile) == 0) {
Tcl_AppendResult(interp,
"couldn't create output pipe for command: ",
Tcl_PosixError(interp), (char *) NULL);
goto error;
}
outputClose = 1;
} else {
channel = Tcl_GetStdChannel(TCL_STDOUT);
if (channel) {
outputFile = TclpMakeFile(channel, TCL_WRITABLE);
if (outputFile != NULL) {
outputRelease = 1;
}
}
}
}
if (errorFile == NULL) {
if (joinError) {
errorFile = outputFile;
} else if (errFilePtr != NULL) {
errorFile = TclpCreateTempFile(NULL, NULL);
if (errorFile == NULL) {
Tcl_AppendResult(interp,
"couldn't create error file for command: ",
Tcl_PosixError(interp), (char *) NULL);
goto error;
}
*errFilePtr = errorFile;
} else {
channel = Tcl_GetStdChannel(TCL_STDERR);
if (channel) {
errorFile = TclpMakeFile(channel, TCL_WRITABLE);
if (errorFile != NULL) {
errorRelease = 1;
}
}
}
}
Tcl_ReapDetachedProcs();
pidPtr = (Tcl_Pid *) ckalloc((unsigned) (cmdCount * sizeof(Tcl_Pid)));
curInFile = inputFile;
for (i = 0; i < argc; i = lastArg + 1) {
int joinThisError;
Tcl_Pid pid;
argv[i] = Tcl_TranslateFileName(interp, argv[i], &execBuffer);
if (argv[i] == NULL) {
goto error;
}
joinThisError = 0;
for (lastArg = i; lastArg < argc; lastArg++) {
if (argv[lastArg][0] == '|') {
if (argv[lastArg][1] == '\0') {
break;
}
if ((argv[lastArg][1] == '&') && (argv[lastArg][2] == '\0')) {
joinThisError = 1;
break;
}
}
}
argv[lastArg] = NULL;
if (lastArg == argc) {
curOutFile = outputFile;
} else {
if (TclpCreatePipe(&pipeIn, &curOutFile) == 0) {
Tcl_AppendResult(interp, "couldn't create pipe: ",
Tcl_PosixError(interp), (char *) NULL);
goto error;
}
}
if (joinThisError != 0) {
curErrFile = curOutFile;
} else {
curErrFile = errorFile;
}
if (TclpCreateProcess(interp, lastArg - i, argv + i,
curInFile, curOutFile, curErrFile, &pid) != TCL_OK) {
goto error;
}
Tcl_DStringFree(&execBuffer);
pidPtr[numPids] = pid;
numPids++;
if ((curInFile != NULL) && (curInFile != inputFile)) {
TclpCloseFile(curInFile);
}
curInFile = pipeIn;
pipeIn = NULL;
if ((curOutFile != NULL) && (curOutFile != outputFile)) {
TclpCloseFile(curOutFile);
}
curOutFile = NULL;
}
*pidArrayPtr = pidPtr;
cleanup:
Tcl_DStringFree(&execBuffer);
if (inputClose) {
TclpCloseFile(inputFile);
} else if (inputRelease) {
TclpReleaseFile(inputFile);
}
if (outputClose) {
TclpCloseFile(outputFile);
} else if (outputRelease) {
TclpReleaseFile(outputFile);
}
if (errorClose) {
TclpCloseFile(errorFile);
} else if (errorRelease) {
TclpReleaseFile(errorFile);
}
return numPids;
error:
if (pipeIn != NULL) {
TclpCloseFile(pipeIn);
}
if ((curOutFile != NULL) && (curOutFile != outputFile)) {
TclpCloseFile(curOutFile);
}
if ((curInFile != NULL) && (curInFile != inputFile)) {
TclpCloseFile(curInFile);
}
if ((inPipePtr != NULL) && (*inPipePtr != NULL)) {
TclpCloseFile(*inPipePtr);
*inPipePtr = NULL;
}
if ((outPipePtr != NULL) && (*outPipePtr != NULL)) {
TclpCloseFile(*outPipePtr);
*outPipePtr = NULL;
}
if ((errFilePtr != NULL) && (*errFilePtr != NULL)) {
TclpCloseFile(*errFilePtr);
*errFilePtr = NULL;
}
if (pidPtr != NULL) {
for (i = 0; i < numPids; i++) {
if (pidPtr[i] != (Tcl_Pid) -1) {
Tcl_DetachPids(1, &pidPtr[i]);
}
}
ckfree((char *) pidPtr);
}
numPids = -1;
goto cleanup;
}
Tcl_Channel
Tcl_OpenCommandChannel(interp, argc, argv, flags)
Tcl_Interp *interp;
int argc;
char **argv;
int flags;
{
TclFile *inPipePtr, *outPipePtr, *errFilePtr;
TclFile inPipe, outPipe, errFile;
int numPids;
Tcl_Pid *pidPtr;
Tcl_Channel channel;
inPipe = outPipe = errFile = NULL;
inPipePtr = (flags & TCL_STDIN) ? &inPipe : NULL;
outPipePtr = (flags & TCL_STDOUT) ? &outPipe : NULL;
errFilePtr = (flags & TCL_STDERR) ? &errFile : NULL;
numPids = TclCreatePipeline(interp, argc, argv, &pidPtr, inPipePtr,
outPipePtr, errFilePtr);
if (numPids < 0) {
goto error;
}
if (flags & TCL_ENFORCE_MODE) {
if ((flags & TCL_STDOUT) && (outPipe == NULL)) {
Tcl_AppendResult(interp, "can't read output from command:",
" standard output was redirected", (char *) NULL);
goto error;
}
if ((flags & TCL_STDIN) && (inPipe == NULL)) {
Tcl_AppendResult(interp, "can't write input to command:",
" standard input was redirected", (char *) NULL);
goto error;
}
}
channel = TclpCreateCommandChannel(outPipe, inPipe, errFile,
numPids, pidPtr);
if (channel == (Tcl_Channel) NULL) {
Tcl_AppendResult(interp, "pipe for command could not be created",
(char *) NULL);
goto error;
}
return channel;
error:
if (numPids > 0) {
Tcl_DetachPids(numPids, pidPtr);
ckfree((char *) pidPtr);
}
if (inPipe != NULL) {
TclpCloseFile(inPipe);
}
if (outPipe != NULL) {
TclpCloseFile(outPipe);
}
if (errFile != NULL) {
TclpCloseFile(errFile);
}
return NULL;
}