sl@0: /* sl@0: * tclPipe.c -- sl@0: * sl@0: * This file contains the generic portion of the command channel sl@0: * driver as well as various utility routines used in managing sl@0: * subprocesses. sl@0: * sl@0: * Copyright (c) 1997 by Sun Microsystems, Inc. sl@0: * Portions Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiaries. All rights reserved. sl@0: * sl@0: * See the file "license.terms" for information on usage and redistribution sl@0: * of this file, and for a DISCLAIMER OF ALL WARRANTIES. sl@0: * sl@0: * RCS: @(#) $Id: tclPipe.c,v 1.7.2.5 2006/03/16 00:35:58 andreas_kupries Exp $ sl@0: */ sl@0: sl@0: #include "tclInt.h" sl@0: #include "tclPort.h" sl@0: #if defined(__SYMBIAN32__) sl@0: #include "tclSymbianGlobals.h" sl@0: #include sl@0: sl@0: /* sl@0: * The following macros convert between TclFile's and fd's. The conversion sl@0: * simple involves shifting fd's up by one to ensure that no valid fd is ever sl@0: * the same as NULL. Note that this code is duplicated from tclUnixPipe.c sl@0: */ sl@0: sl@0: #define MakeFile(fd) ((TclFile)((fd)+1)) sl@0: #define GetFd(file) (((int)file)-1) sl@0: sl@0: #endif sl@0: sl@0: /* sl@0: * A linked list of the following structures is used to keep track sl@0: * of child processes that have been detached but haven't exited sl@0: * yet, so we can make sure that they're properly "reaped" (officially sl@0: * waited for) and don't lie around as zombies cluttering the sl@0: * system. sl@0: */ sl@0: sl@0: typedef struct Detached { sl@0: Tcl_Pid pid; /* Id of process that's been detached sl@0: * but isn't known to have exited. */ sl@0: struct Detached *nextPtr; /* Next in list of all detached sl@0: * processes. */ sl@0: } Detached; sl@0: sl@0: static Detached *detList = NULL; /* List of all detached proceses. */ sl@0: TCL_DECLARE_MUTEX(pipeMutex) /* Guard access to detList. */ sl@0: sl@0: /* sl@0: * Declarations for local procedures defined in this file: sl@0: */ sl@0: sl@0: static TclFile FileForRedirect _ANSI_ARGS_((Tcl_Interp *interp, sl@0: CONST char *spec, int atOk, CONST char *arg, sl@0: CONST char *nextArg, int flags, int *skipPtr, sl@0: int *closePtr, int *releasePtr)); sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * FileForRedirect -- sl@0: * sl@0: * This procedure does much of the work of parsing redirection sl@0: * operators. It handles "@" if specified and allowed, and a file sl@0: * name, and opens the file if necessary. sl@0: * sl@0: * Results: sl@0: * The return value is the descriptor number for the file. If an sl@0: * error occurs then NULL is returned and an error message is left sl@0: * in the interp's result. Several arguments are side-effected; see sl@0: * the argument list below for details. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: static TclFile sl@0: FileForRedirect(interp, spec, atOK, arg, nextArg, flags, skipPtr, closePtr, sl@0: releasePtr) sl@0: Tcl_Interp *interp; /* Intepreter to use for error reporting. */ sl@0: CONST char *spec; /* Points to character just after sl@0: * redirection character. */ sl@0: int atOK; /* Non-zero means that '@' notation can be sl@0: * used to specify a channel, zero means that sl@0: * it isn't. */ sl@0: CONST char *arg; /* Pointer to entire argument containing sl@0: * spec: used for error reporting. */ sl@0: CONST char *nextArg; /* Next argument in argc/argv array, if needed sl@0: * for file name or channel name. May be sl@0: * NULL. */ sl@0: int flags; /* Flags to use for opening file or to sl@0: * specify mode for channel. */ sl@0: int *skipPtr; /* Filled with 1 if redirection target was sl@0: * in spec, 2 if it was in nextArg. */ sl@0: int *closePtr; /* Filled with one if the caller should sl@0: * close the file when done with it, zero sl@0: * otherwise. */ sl@0: int *releasePtr; sl@0: { sl@0: int writing = (flags & O_WRONLY); sl@0: Tcl_Channel chan; sl@0: TclFile file; sl@0: sl@0: *skipPtr = 1; sl@0: if ((atOK != 0) && (*spec == '@')) { sl@0: spec++; sl@0: if (*spec == '\0') { sl@0: spec = nextArg; sl@0: if (spec == NULL) { sl@0: goto badLastArg; sl@0: } sl@0: *skipPtr = 2; sl@0: } sl@0: chan = Tcl_GetChannel(interp, spec, NULL); sl@0: if (chan == (Tcl_Channel) NULL) { sl@0: return NULL; sl@0: } sl@0: file = TclpMakeFile(chan, writing ? TCL_WRITABLE : TCL_READABLE); sl@0: if (file == NULL) { sl@0: Tcl_AppendResult(interp, "channel \"", Tcl_GetChannelName(chan), sl@0: "\" wasn't opened for ", sl@0: ((writing) ? "writing" : "reading"), (char *) NULL); sl@0: return NULL; sl@0: } sl@0: *releasePtr = 1; sl@0: if (writing) { sl@0: sl@0: /* sl@0: * Be sure to flush output to the file, so that anything sl@0: * written by the child appears after stuff we've already sl@0: * written. sl@0: */ sl@0: sl@0: Tcl_Flush(chan); sl@0: } sl@0: } else { sl@0: CONST char *name; sl@0: Tcl_DString nameString; sl@0: sl@0: if (*spec == '\0') { sl@0: spec = nextArg; sl@0: if (spec == NULL) { sl@0: goto badLastArg; sl@0: } sl@0: *skipPtr = 2; sl@0: } sl@0: name = Tcl_TranslateFileName(interp, spec, &nameString); sl@0: if (name == NULL) { sl@0: return NULL; sl@0: } sl@0: file = TclpOpenFile(name, flags); sl@0: Tcl_DStringFree(&nameString); sl@0: if (file == NULL) { sl@0: Tcl_AppendResult(interp, "couldn't ", sl@0: ((writing) ? "write" : "read"), " file \"", spec, "\": ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: return NULL; sl@0: } sl@0: *closePtr = 1; sl@0: } sl@0: return file; sl@0: sl@0: badLastArg: sl@0: Tcl_AppendResult(interp, "can't specify \"", arg, sl@0: "\" as last word in command", (char *) NULL); sl@0: return NULL; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_DetachPids -- sl@0: * sl@0: * This procedure is called to indicate that one or more child sl@0: * processes have been placed in background and will never be sl@0: * waited for; they should eventually be reaped by sl@0: * Tcl_ReapDetachedProcs. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * None. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C void sl@0: Tcl_DetachPids(numPids, pidPtr) sl@0: int numPids; /* Number of pids to detach: gives size sl@0: * of array pointed to by pidPtr. */ sl@0: Tcl_Pid *pidPtr; /* Array of pids to detach. */ sl@0: { sl@0: register Detached *detPtr; sl@0: int i; sl@0: sl@0: Tcl_MutexLock(&pipeMutex); sl@0: for (i = 0; i < numPids; i++) { sl@0: detPtr = (Detached *) ckalloc(sizeof(Detached)); sl@0: detPtr->pid = pidPtr[i]; sl@0: detPtr->nextPtr = detList; sl@0: detList = detPtr; sl@0: } sl@0: Tcl_MutexUnlock(&pipeMutex); sl@0: sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_ReapDetachedProcs -- sl@0: * sl@0: * This procedure checks to see if any detached processes have sl@0: * exited and, if so, it "reaps" them by officially waiting on sl@0: * them. It should be called "occasionally" to make sure that sl@0: * all detached processes are eventually reaped. sl@0: * sl@0: * Results: sl@0: * None. sl@0: * sl@0: * Side effects: sl@0: * Processes are waited on, so that they can be reaped by the sl@0: * system. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C void sl@0: Tcl_ReapDetachedProcs() sl@0: { sl@0: register Detached *detPtr; sl@0: Detached *nextPtr, *prevPtr; sl@0: int status; sl@0: Tcl_Pid pid; sl@0: sl@0: Tcl_MutexLock(&pipeMutex); sl@0: for (detPtr = detList, prevPtr = NULL; detPtr != NULL; ) { sl@0: pid = Tcl_WaitPid(detPtr->pid, &status, WNOHANG); sl@0: if ((pid == 0) || ((pid == (Tcl_Pid) -1) && (errno != ECHILD))) { sl@0: prevPtr = detPtr; sl@0: detPtr = detPtr->nextPtr; sl@0: continue; sl@0: } sl@0: nextPtr = detPtr->nextPtr; sl@0: if (prevPtr == NULL) { sl@0: detList = detPtr->nextPtr; sl@0: } else { sl@0: prevPtr->nextPtr = detPtr->nextPtr; sl@0: } sl@0: ckfree((char *) detPtr); sl@0: detPtr = nextPtr; sl@0: } sl@0: Tcl_MutexUnlock(&pipeMutex); sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclCleanupChildren -- sl@0: * sl@0: * This is a utility procedure used to wait for child processes sl@0: * to exit, record information about abnormal exits, and then sl@0: * collect any stderr output generated by them. sl@0: * sl@0: * Results: sl@0: * The return value is a standard Tcl result. If anything at sl@0: * weird happened with the child processes, TCL_ERROR is returned sl@0: * and a message is left in the interp's result. sl@0: * sl@0: * Side effects: sl@0: * If the last character of the interp's result is a newline, then it sl@0: * is removed unless keepNewline is non-zero. File errorId gets sl@0: * closed, and pidPtr is freed back to the storage allocator. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclCleanupChildren(interp, numPids, pidPtr, errorChan) sl@0: Tcl_Interp *interp; /* Used for error messages. */ sl@0: int numPids; /* Number of entries in pidPtr array. */ sl@0: Tcl_Pid *pidPtr; /* Array of process ids of children. */ sl@0: Tcl_Channel errorChan; /* Channel for file containing stderr output sl@0: * from pipeline. NULL means there isn't any sl@0: * stderr output. */ sl@0: { sl@0: int result = TCL_OK; sl@0: int i, abnormalExit, anyErrorInfo; sl@0: Tcl_Pid pid; sl@0: WAIT_STATUS_TYPE waitStatus; sl@0: CONST char *msg; sl@0: unsigned long resolvedPid; sl@0: sl@0: abnormalExit = 0; sl@0: for (i = 0; i < numPids; i++) { sl@0: /* sl@0: * We need to get the resolved pid before we wait on it as sl@0: * the windows implimentation of Tcl_WaitPid deletes the sl@0: * information such that any following calls to TclpGetPid sl@0: * fail. sl@0: */ sl@0: resolvedPid = TclpGetPid(pidPtr[i]); sl@0: pid = Tcl_WaitPid(pidPtr[i], (int *) &waitStatus, 0); sl@0: if (pid == (Tcl_Pid) -1) { sl@0: result = TCL_ERROR; sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: msg = Tcl_PosixError(interp); sl@0: if (errno == ECHILD) { sl@0: /* sl@0: * This changeup in message suggested by Mark Diekhans sl@0: * to remind people that ECHILD errors can occur on sl@0: * some systems if SIGCHLD isn't in its default state. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: // change message to remove all references to signals. sl@0: msg = sl@0: "child process lost."; sl@0: #else sl@0: msg = sl@0: "child process lost (is SIGCHLD ignored or trapped?)"; sl@0: #endif sl@0: } sl@0: Tcl_AppendResult(interp, "error waiting for process to exit: ", sl@0: msg, (char *) NULL); sl@0: } sl@0: continue; sl@0: } sl@0: sl@0: /* sl@0: * Create error messages for unusual process exits. An sl@0: * extra newline gets appended to each error message, but sl@0: * it gets removed below (in the same fashion that an sl@0: * extra newline in the command's output is removed). sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: // process the return status of the child. sl@0: if (waitStatus != 0) { sl@0: char msg1[TCL_INTEGER_SPACE], msg2[TCL_INTEGER_SPACE]; sl@0: sl@0: result = TCL_ERROR; sl@0: TclFormatInt(msg1, (long) resolvedPid); sl@0: sl@0: if (waitStatus < 0) { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: TclFormatInt(msg2, (long) waitStatus); sl@0: Tcl_AppendResult(interp, sl@0: "child wait status is error (pid ", msg1, ") status: ", msg2, sl@0: (char *) NULL); sl@0: } sl@0: } else { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: TclFormatInt(msg2, (long) waitStatus); sl@0: Tcl_SetErrorCode(interp, "CHILDSTATUS (pid: ", msg1, ") status: ", msg2, sl@0: (char *) NULL); sl@0: } sl@0: abnormalExit = 1; sl@0: } sl@0: #else sl@0: if (!WIFEXITED(waitStatus) || (WEXITSTATUS(waitStatus) != 0)) { sl@0: char msg1[TCL_INTEGER_SPACE], msg2[TCL_INTEGER_SPACE]; sl@0: sl@0: result = TCL_ERROR; sl@0: TclFormatInt(msg1, (long) resolvedPid); sl@0: if (WIFEXITED(waitStatus)) { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: TclFormatInt(msg2, WEXITSTATUS(waitStatus)); sl@0: Tcl_SetErrorCode(interp, "CHILDSTATUS", msg1, msg2, sl@0: (char *) NULL); sl@0: } sl@0: abnormalExit = 1; sl@0: } else if (WIFSIGNALED(waitStatus)) { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: CONST char *p; sl@0: sl@0: p = Tcl_SignalMsg((int) (WTERMSIG(waitStatus))); sl@0: Tcl_SetErrorCode(interp, "CHILDKILLED", msg1, sl@0: Tcl_SignalId((int) (WTERMSIG(waitStatus))), p, sl@0: (char *) NULL); sl@0: Tcl_AppendResult(interp, "child killed: ", p, "\n", sl@0: (char *) NULL); sl@0: } sl@0: } else if (WIFSTOPPED(waitStatus)) { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: CONST char *p; sl@0: sl@0: p = Tcl_SignalMsg((int) (WSTOPSIG(waitStatus))); sl@0: Tcl_SetErrorCode(interp, "CHILDSUSP", msg1, sl@0: Tcl_SignalId((int) (WSTOPSIG(waitStatus))), sl@0: p, (char *) NULL); sl@0: Tcl_AppendResult(interp, "child suspended: ", p, "\n", sl@0: (char *) NULL); sl@0: } sl@0: } else { sl@0: if (interp != (Tcl_Interp *) NULL) { sl@0: Tcl_AppendResult(interp, sl@0: "child wait status didn't make sense\n", sl@0: (char *) NULL); sl@0: } sl@0: } sl@0: #endif sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Read the standard error file. If there's anything there, sl@0: * then return an error and add the file's contents to the result sl@0: * string. sl@0: */ sl@0: sl@0: anyErrorInfo = 0; sl@0: if (errorChan != NULL) { sl@0: sl@0: /* sl@0: * Make sure we start at the beginning of the file. sl@0: */ sl@0: sl@0: if (interp != NULL) { sl@0: int count; sl@0: Tcl_Obj *objPtr; sl@0: sl@0: Tcl_Seek(errorChan, (Tcl_WideInt)0, SEEK_SET); sl@0: objPtr = Tcl_NewObj(); sl@0: count = Tcl_ReadChars(errorChan, objPtr, -1, 0); sl@0: if (count < 0) { sl@0: result = TCL_ERROR; sl@0: Tcl_DecrRefCount(objPtr); sl@0: Tcl_ResetResult(interp); sl@0: Tcl_AppendResult(interp, "error reading stderr output file: ", sl@0: Tcl_PosixError(interp), NULL); sl@0: } else if (count > 0) { sl@0: anyErrorInfo = 1; sl@0: Tcl_SetObjResult(interp, objPtr); sl@0: result = TCL_ERROR; sl@0: } else { sl@0: Tcl_DecrRefCount(objPtr); sl@0: } sl@0: } sl@0: Tcl_Close(NULL, errorChan); sl@0: } sl@0: sl@0: /* sl@0: * If a child exited abnormally but didn't output any error information sl@0: * at all, generate an error message here. sl@0: */ sl@0: sl@0: if ((abnormalExit != 0) && (anyErrorInfo == 0) && (interp != NULL)) { sl@0: Tcl_AppendResult(interp, "child process exited abnormally", sl@0: (char *) NULL); sl@0: } sl@0: return result; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * TclCreatePipeline -- sl@0: * sl@0: * Given an argc/argv array, instantiate a pipeline of processes sl@0: * as described by the argv. sl@0: * sl@0: * This procedure is unofficially exported for use by BLT. sl@0: * sl@0: * Results: sl@0: * The return value is a count of the number of new processes sl@0: * created, or -1 if an error occurred while creating the pipeline. sl@0: * *pidArrayPtr is filled in with the address of a dynamically sl@0: * allocated array giving the ids of all of the processes. It sl@0: * is up to the caller to free this array when it isn't needed sl@0: * anymore. If inPipePtr is non-NULL, *inPipePtr is filled in sl@0: * with the file id for the input pipe for the pipeline (if any): sl@0: * the caller must eventually close this file. If outPipePtr sl@0: * isn't NULL, then *outPipePtr is filled in with the file id sl@0: * for the output pipe from the pipeline: the caller must close sl@0: * this file. If errFilePtr isn't NULL, then *errFilePtr is filled sl@0: * with a file id that may be used to read error output after the sl@0: * pipeline completes. sl@0: * sl@0: * Side effects: sl@0: * Processes and pipes are created. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: int sl@0: TclCreatePipeline(interp, argc, argv, pidArrayPtr, inPipePtr, sl@0: outPipePtr, errFilePtr) sl@0: Tcl_Interp *interp; /* Interpreter to use for error reporting. */ sl@0: int argc; /* Number of entries in argv. */ sl@0: CONST char **argv; /* Array of strings describing commands in sl@0: * pipeline plus I/O redirection with <, sl@0: * <<, >, etc. Argv[argc] must be NULL. */ sl@0: Tcl_Pid **pidArrayPtr; /* Word at *pidArrayPtr gets filled in with sl@0: * address of array of pids for processes sl@0: * in pipeline (first pid is first process sl@0: * in pipeline). */ sl@0: TclFile *inPipePtr; /* If non-NULL, input to the pipeline comes sl@0: * from a pipe (unless overridden by sl@0: * redirection in the command). The file sl@0: * id with which to write to this pipe is sl@0: * stored at *inPipePtr. NULL means command sl@0: * specified its own input source. */ sl@0: TclFile *outPipePtr; /* If non-NULL, output to the pipeline goes sl@0: * to a pipe, unless overriden by redirection sl@0: * in the command. The file id with which to sl@0: * read frome this pipe is stored at sl@0: * *outPipePtr. NULL means command specified sl@0: * its own output sink. */ sl@0: TclFile *errFilePtr; /* If non-NULL, all stderr output from the sl@0: * pipeline will go to a temporary file sl@0: * created here, and a descriptor to read sl@0: * the file will be left at *errFilePtr. sl@0: * The file will be removed already, so sl@0: * closing this descriptor will be the end sl@0: * of the file. If this is NULL, then sl@0: * all stderr output goes to our stderr. sl@0: * If the pipeline specifies redirection sl@0: * then the file will still be created sl@0: * but it will never get any data. */ sl@0: { sl@0: Tcl_Pid *pidPtr = NULL; /* Points to malloc-ed array holding all sl@0: * the pids of child processes. */ sl@0: int numPids; /* Actual number of processes that exist sl@0: * at *pidPtr right now. */ sl@0: int cmdCount; /* Count of number of distinct commands sl@0: * found in argc/argv. */ sl@0: CONST char *inputLiteral = NULL; /* If non-null, then this points to a sl@0: * string containing input data (specified sl@0: * via <<) to be piped to the first process sl@0: * in the pipeline. */ sl@0: TclFile inputFile = NULL; /* If != NULL, gives file to use as input for sl@0: * first process in pipeline (specified via < sl@0: * or <@). */ sl@0: int inputClose = 0; /* If non-zero, then inputFile should be sl@0: * closed when cleaning up. */ sl@0: int inputRelease = 0; sl@0: TclFile outputFile = NULL; /* Writable file for output from last command sl@0: * in pipeline (could be file or pipe). NULL sl@0: * means use stdout. */ sl@0: int outputClose = 0; /* If non-zero, then outputFile should be sl@0: * closed when cleaning up. */ sl@0: int outputRelease = 0; sl@0: TclFile errorFile = NULL; /* Writable file for error output from all sl@0: * commands in pipeline. NULL means use sl@0: * stderr. */ sl@0: int errorClose = 0; /* If non-zero, then errorFile should be sl@0: * closed when cleaning up. */ sl@0: int errorRelease = 0; sl@0: CONST char *p; sl@0: CONST char *nextArg; sl@0: int skip, lastBar, lastArg, i, j, atOK, flags, needCmd, errorToOutput = 0; sl@0: Tcl_DString execBuffer; sl@0: TclFile pipeIn; sl@0: TclFile curInFile, curOutFile, curErrFile; sl@0: Tcl_Channel channel; sl@0: #ifdef __SYMBIAN32__ sl@0: int fifoResult; sl@0: int ReadFifoFd; sl@0: int WriteFifoFd; sl@0: int tmpReadFifoFd; sl@0: #endif sl@0: sl@0: if (inPipePtr != NULL) { sl@0: *inPipePtr = NULL; sl@0: } sl@0: if (outPipePtr != NULL) { sl@0: *outPipePtr = NULL; sl@0: } sl@0: if (errFilePtr != NULL) { sl@0: *errFilePtr = NULL; sl@0: } sl@0: sl@0: Tcl_DStringInit(&execBuffer); sl@0: sl@0: pipeIn = NULL; sl@0: curInFile = NULL; sl@0: curOutFile = NULL; sl@0: numPids = 0; sl@0: sl@0: /* sl@0: * First, scan through all the arguments to figure out the structure sl@0: * of the pipeline. Process all of the input and output redirection sl@0: * arguments and remove them from the argument list in the pipeline. sl@0: * Count the number of distinct processes (it's the number of "|" sl@0: * arguments plus one) but don't remove the "|" arguments because sl@0: * they'll be used in the second pass to seperate the individual sl@0: * child processes. Cannot start the child processes in this pass sl@0: * because the redirection symbols may appear anywhere in the sl@0: * command line -- e.g., the '<' that specifies the input to the sl@0: * entire pipe may appear at the very end of the argument list. sl@0: */ sl@0: sl@0: lastBar = -1; sl@0: cmdCount = 1; sl@0: needCmd = 1; sl@0: for (i = 0; i < argc; i++) { sl@0: errorToOutput = 0; sl@0: skip = 0; sl@0: p = argv[i]; sl@0: switch (*p++) { sl@0: case '|': sl@0: if (*p == '&') { sl@0: p++; sl@0: } sl@0: if (*p == '\0') { sl@0: if ((i == (lastBar + 1)) || (i == (argc - 1))) { sl@0: Tcl_SetResult(interp, sl@0: "illegal use of | or |& in command", sl@0: TCL_STATIC); sl@0: goto error; sl@0: } sl@0: } sl@0: lastBar = i; sl@0: cmdCount++; sl@0: needCmd = 1; sl@0: break; sl@0: sl@0: case '<': sl@0: if (inputClose != 0) { sl@0: inputClose = 0; sl@0: TclpCloseFile(inputFile); sl@0: } sl@0: if (inputRelease != 0) { sl@0: inputRelease = 0; sl@0: TclpReleaseFile(inputFile); sl@0: } sl@0: if (*p == '<') { sl@0: inputFile = NULL; sl@0: inputLiteral = p + 1; sl@0: skip = 1; sl@0: if (*inputLiteral == '\0') { sl@0: inputLiteral = ((i + 1) == argc) ? NULL : argv[i + 1]; sl@0: if (inputLiteral == NULL) { sl@0: Tcl_AppendResult(interp, "can't specify \"", argv[i], sl@0: "\" as last word in command", (char *) NULL); sl@0: goto error; sl@0: } sl@0: skip = 2; sl@0: } sl@0: } else { sl@0: nextArg = ((i + 1) == argc) ? NULL : argv[i + 1]; sl@0: inputLiteral = NULL; sl@0: inputFile = FileForRedirect(interp, p, 1, argv[i], sl@0: nextArg, O_RDONLY, &skip, &inputClose, &inputRelease); sl@0: if (inputFile == NULL) { sl@0: goto error; sl@0: } sl@0: } sl@0: break; sl@0: sl@0: case '>': sl@0: atOK = 1; sl@0: flags = O_WRONLY | O_CREAT | O_TRUNC; sl@0: if (*p == '>') { sl@0: p++; sl@0: atOK = 0; sl@0: sl@0: /* sl@0: * Note that the O_APPEND flag only has an effect on POSIX sl@0: * platforms. On Windows, we just have to carry on regardless. sl@0: */ sl@0: sl@0: flags = O_WRONLY | O_CREAT | O_APPEND; sl@0: } sl@0: if (*p == '&') { sl@0: if (errorClose != 0) { sl@0: errorClose = 0; sl@0: TclpCloseFile(errorFile); sl@0: } sl@0: errorToOutput = 1; sl@0: p++; sl@0: } sl@0: sl@0: /* sl@0: * Close the old output file, but only if the error file is sl@0: * not also using it. sl@0: */ sl@0: sl@0: if (outputClose != 0) { sl@0: outputClose = 0; sl@0: if (errorFile == outputFile) { sl@0: errorClose = 1; sl@0: } else { sl@0: TclpCloseFile(outputFile); sl@0: } sl@0: } sl@0: if (outputRelease != 0) { sl@0: outputRelease = 0; sl@0: if (errorFile == outputFile) { sl@0: errorRelease = 1; sl@0: } else { sl@0: TclpReleaseFile(outputFile); sl@0: } sl@0: } sl@0: nextArg = ((i + 1) == argc) ? NULL : argv[i + 1]; sl@0: outputFile = FileForRedirect(interp, p, atOK, argv[i], sl@0: nextArg, flags, &skip, &outputClose, &outputRelease); sl@0: if (outputFile == NULL) { sl@0: goto error; sl@0: } sl@0: if (errorToOutput) { sl@0: if (errorClose != 0) { sl@0: errorClose = 0; sl@0: TclpCloseFile(errorFile); sl@0: } sl@0: if (errorRelease != 0) { sl@0: errorRelease = 0; sl@0: TclpReleaseFile(errorFile); sl@0: } sl@0: errorFile = outputFile; sl@0: } sl@0: break; sl@0: sl@0: case '2': sl@0: if (*p != '>') { sl@0: break; sl@0: } sl@0: p++; sl@0: atOK = 1; sl@0: flags = O_WRONLY | O_CREAT | O_TRUNC; sl@0: if (*p == '>') { sl@0: p++; sl@0: atOK = 0; sl@0: flags = O_WRONLY | O_CREAT; sl@0: } sl@0: if (errorClose != 0) { sl@0: errorClose = 0; sl@0: TclpCloseFile(errorFile); sl@0: } sl@0: if (errorRelease != 0) { sl@0: errorRelease = 0; sl@0: TclpReleaseFile(errorFile); sl@0: } sl@0: if (atOK && p[0] == '@' && p[1] == '1' && p[2] == '\0') { sl@0: /* sl@0: * Special case handling of 2>@1 to redirect stderr to the sl@0: * exec/open output pipe as well. This is meant for the end sl@0: * of the command string, otherwise use |& between commands. sl@0: */ sl@0: if (i != argc - 1) { sl@0: Tcl_AppendResult(interp, "must specify \"", argv[i], sl@0: "\" as last word in command", (char *) NULL); sl@0: goto error; sl@0: } sl@0: errorFile = outputFile; sl@0: errorToOutput = 2; sl@0: skip = 1; sl@0: } else { sl@0: nextArg = ((i + 1) == argc) ? NULL : argv[i + 1]; sl@0: errorFile = FileForRedirect(interp, p, atOK, argv[i], sl@0: nextArg, flags, &skip, &errorClose, &errorRelease); sl@0: if (errorFile == NULL) { sl@0: goto error; sl@0: } sl@0: } sl@0: break; sl@0: sl@0: default: sl@0: /* Got a command word, not a redirection */ sl@0: needCmd = 0; sl@0: break; sl@0: } sl@0: sl@0: if (skip != 0) { sl@0: for (j = i + skip; j < argc; j++) { sl@0: argv[j - skip] = argv[j]; sl@0: } sl@0: argc -= skip; sl@0: i -= 1; sl@0: } sl@0: } sl@0: sl@0: if (needCmd) { sl@0: /* We had a bar followed only by redirections. */ sl@0: sl@0: Tcl_SetResult(interp, sl@0: "illegal use of | or |& in command", sl@0: TCL_STATIC); sl@0: goto error; sl@0: } sl@0: sl@0: if (inputFile == NULL) { sl@0: if (inputLiteral != NULL) { sl@0: /* sl@0: * The input for the first process is immediate data coming from sl@0: * Tcl. Create a temporary file for it and put the data into the sl@0: * file. sl@0: */ sl@0: inputFile = TclpCreateTempFile(inputLiteral); sl@0: if (inputFile == NULL) { sl@0: Tcl_AppendResult(interp, sl@0: "couldn't create input file for command: ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: goto error; sl@0: } sl@0: #ifdef __SYMBIAN32__ sl@0: // keep the file name for IPC sl@0: strcpy(inFileName ,tmpFileName); sl@0: #endif sl@0: inputClose = 1; sl@0: } else if (inPipePtr != NULL) { sl@0: /* sl@0: * The input for the first process in the pipeline is to sl@0: * come from a pipe that can be written from by the caller. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: tmpnam(inFileName); sl@0: fifoResult = mkfifo(inFileName,S_IXGRP); sl@0: if(fifoResult == -1) sl@0: { sl@0: //fifo creation failure. sl@0: fprintf(stderr,"\n*** failure mkfifo errno is %d***\n",errno); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: tmpReadFifoFd = open(inFileName,O_RDONLY | O_NONBLOCK); sl@0: sl@0: if(tmpReadFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure tmpReadFifoFd Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: WriteFifoFd = open(inFileName,O_WRONLY); sl@0: sl@0: if(WriteFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: *inPipePtr = MakeFile(WriteFifoFd); sl@0: inputFile = *inPipePtr; sl@0: } sl@0: } sl@0: sl@0: if ( close(tmpReadFifoFd) != 0) sl@0: { sl@0: printf("\n*** failure tmpReadFifoFd Fifo close ***\n"); sl@0: goto error; sl@0: } sl@0: #else sl@0: if (TclpCreatePipe(&inputFile, inPipePtr) == 0) { sl@0: Tcl_AppendResult(interp, sl@0: "couldn't create input pipe for command: ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: goto error; sl@0: } sl@0: inputClose = 1; sl@0: #endif sl@0: } else { sl@0: /* sl@0: * The input for the first process comes from stdin. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: tmpnam(inFileName); sl@0: fifoResult = mkfifo(inFileName,S_IXGRP); sl@0: if(fifoResult == -1) sl@0: { sl@0: //fifo creation failure. sl@0: fprintf(stderr,"\n*** failure mkfifo errno is %d***\n",errno); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: tmpReadFifoFd = open(inFileName,O_RDONLY | O_NONBLOCK); sl@0: sl@0: if(tmpReadFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure tmpReadFifoFd Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: sl@0: WriteFifoFd = open(inFileName,O_WRONLY); sl@0: sl@0: if(WriteFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: inputFile = MakeFile(WriteFifoFd); sl@0: } sl@0: } sl@0: sl@0: if (dup2(0/*fd for TCL_STDIN*/ , WriteFifoFd) == -1) sl@0: { sl@0: fprintf(stderr, sl@0: "%d inputFile couldn't be redirected & dup failed ", errno); sl@0: goto error; sl@0: } sl@0: sl@0: if ( close(tmpReadFifoFd) != 0) sl@0: { sl@0: printf("\n*** failure tmpReadFifoFd Fifo close ***\n"); sl@0: goto error; sl@0: } sl@0: sl@0: strcpy(inFileName,"STD"); sl@0: #else sl@0: channel = Tcl_GetStdChannel(TCL_STDIN); sl@0: if (channel != NULL) { sl@0: inputFile = TclpMakeFile(channel, TCL_READABLE); sl@0: } sl@0: #endif sl@0: if (inputFile != NULL) { sl@0: inputRelease = 1; sl@0: } sl@0: } sl@0: } sl@0: sl@0: if (outputFile == NULL) { sl@0: if (outPipePtr != NULL) { sl@0: /* sl@0: * Output from the last process in the pipeline is to go to a sl@0: * pipe that can be read by the caller. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: tmpnam(outFileName); sl@0: fifoResult = mkfifo(outFileName,S_IXGRP); sl@0: if(fifoResult == -1) sl@0: { sl@0: //fifo creation failure. sl@0: fprintf(stderr,"\n*** failure mkfifo errno is %d***\n",errno); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: ReadFifoFd = open(outFileName,O_RDONLY | O_NONBLOCK); sl@0: sl@0: if(ReadFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: *outPipePtr = MakeFile(ReadFifoFd); sl@0: outputFile = *outPipePtr; sl@0: } sl@0: } sl@0: #else sl@0: if (TclpCreatePipe(outPipePtr, &outputFile) == 0) { sl@0: Tcl_AppendResult(interp, sl@0: "couldn't create output pipe for command: ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: goto error; sl@0: } sl@0: outputClose = 1; sl@0: #endif sl@0: } else { sl@0: /* sl@0: * The output for the last process goes to stdout. sl@0: */ sl@0: #ifdef __SYMBIAN32__ sl@0: tmpnam(outFileName); sl@0: fifoResult = mkfifo(outFileName,S_IXGRP); sl@0: if(fifoResult == -1) sl@0: { sl@0: //fifo creation failure. sl@0: fprintf(stderr,"\n*** failure mkfifo errno is %d***\n",errno); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: ReadFifoFd = open(outFileName,O_RDONLY | O_NONBLOCK); sl@0: sl@0: if(ReadFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: outputFile = MakeFile(ReadFifoFd); sl@0: } sl@0: } sl@0: sl@0: if (dup2(1/*fd for TCL_STDOUT*/ , ReadFifoFd) == -1) sl@0: { sl@0: fprintf(stderr, sl@0: "%d outputFile couldn't be redirected & dup failed ", errno); sl@0: goto error; sl@0: } sl@0: sl@0: outputRelease = 1; sl@0: strcpy(outFileName,"STD"); sl@0: #else sl@0: channel = Tcl_GetStdChannel(TCL_STDOUT); sl@0: if (channel) { sl@0: outputFile = TclpMakeFile(channel, TCL_WRITABLE); sl@0: if (outputFile != NULL) { sl@0: outputRelease = 1; sl@0: } sl@0: } sl@0: #endif sl@0: } sl@0: } sl@0: sl@0: if (errorFile == NULL) { sl@0: if (errorToOutput == 2) { sl@0: /* sl@0: * Handle 2>@1 special case at end of cmd line sl@0: */ sl@0: errorFile = outputFile; sl@0: #ifdef __SYMBIAN32__ sl@0: strcpy(errFileName, outFileName); sl@0: #endif sl@0: } else if (errFilePtr != NULL) { sl@0: /* sl@0: * Set up the standard error output sink for the pipeline, if sl@0: * requested. Use a temporary file which is opened, then deleted. sl@0: * Could potentially just use pipe, but if it filled up it could sl@0: * cause the pipeline to deadlock: we'd be waiting for processes sl@0: * to complete before reading stderr, and processes couldn't sl@0: * complete because stderr was backed up. sl@0: */ sl@0: sl@0: errorFile = TclpCreateTempFile(NULL); sl@0: if (errorFile == NULL) { sl@0: Tcl_AppendResult(interp, sl@0: "couldn't create error file for command: ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: goto error; sl@0: } sl@0: #ifdef __SYMBIAN32__ sl@0: strcpy(errFileName ,tmpFileName); sl@0: #endif sl@0: *errFilePtr = errorFile; sl@0: } else { sl@0: /* sl@0: * Errors from the pipeline go to stderr. sl@0: */ sl@0: sl@0: channel = Tcl_GetStdChannel(TCL_STDERR); sl@0: if (channel) { sl@0: errorFile = TclpMakeFile(channel, TCL_WRITABLE); sl@0: if (errorFile != NULL) { sl@0: errorRelease = 1; sl@0: } sl@0: } sl@0: #ifdef __SYMBIAN32__ sl@0: strcpy(errFileName,"STD"); sl@0: #endif sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * Scan through the argc array, creating a process for each sl@0: * group of arguments between the "|" characters. sl@0: */ sl@0: sl@0: Tcl_ReapDetachedProcs(); sl@0: pidPtr = (Tcl_Pid *) ckalloc((unsigned) (cmdCount * sizeof(Tcl_Pid))); sl@0: sl@0: curInFile = inputFile; sl@0: sl@0: for (i = 0; i < argc; i = lastArg + 1) { sl@0: int result, joinThisError; sl@0: Tcl_Pid pid; sl@0: CONST char *oldName; sl@0: sl@0: /* sl@0: * Convert the program name into native form. sl@0: */ sl@0: sl@0: if (Tcl_TranslateFileName(interp, argv[i], &execBuffer) == NULL) { sl@0: goto error; sl@0: } sl@0: sl@0: /* sl@0: * Find the end of the current segment of the pipeline. sl@0: */ sl@0: sl@0: joinThisError = 0; sl@0: for (lastArg = i; lastArg < argc; lastArg++) { sl@0: if (argv[lastArg][0] == '|') { sl@0: if (argv[lastArg][1] == '\0') { sl@0: break; sl@0: } sl@0: if ((argv[lastArg][1] == '&') && (argv[lastArg][2] == '\0')) { sl@0: joinThisError = 1; sl@0: break; sl@0: } sl@0: } sl@0: } sl@0: sl@0: /* sl@0: * If this is the last segment, use the specified outputFile. sl@0: * Otherwise create an intermediate pipe. pipeIn will become the sl@0: * curInFile for the next segment of the pipe. sl@0: */ sl@0: sl@0: if (lastArg == argc) { sl@0: curOutFile = outputFile; sl@0: } else { sl@0: argv[lastArg] = NULL; sl@0: #ifdef __SYMBIAN32__ sl@0: tmpnam(outFileName); sl@0: fifoResult = mkfifo(outFileName,S_IXGRP); sl@0: if(fifoResult == -1) sl@0: { sl@0: //fifo creation failure. sl@0: fprintf(stderr,"\n*** failure mkfifo errno is %d***\n",errno); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: ReadFifoFd = open(outFileName,O_RDONLY); sl@0: sl@0: if(ReadFifoFd == -1) sl@0: { sl@0: //Failed to open the Fifo sl@0: printf("\n*** failure Fifo Open ***\n"); sl@0: goto error; sl@0: } sl@0: else sl@0: { sl@0: pipeIn = MakeFile(ReadFifoFd); sl@0: } sl@0: } sl@0: #else sl@0: if (TclpCreatePipe(&pipeIn, &curOutFile) == 0) { sl@0: Tcl_AppendResult(interp, "couldn't create pipe: ", sl@0: Tcl_PosixError(interp), (char *) NULL); sl@0: goto error; sl@0: } sl@0: #endif sl@0: } sl@0: sl@0: if (joinThisError != 0) { sl@0: curErrFile = curOutFile; sl@0: } else { sl@0: curErrFile = errorFile; sl@0: } sl@0: sl@0: /* sl@0: * Restore argv[i], since a caller wouldn't expect the contents of sl@0: * argv to be modified. sl@0: */ sl@0: sl@0: oldName = argv[i]; sl@0: argv[i] = Tcl_DStringValue(&execBuffer); sl@0: result = TclpCreateProcess(interp, lastArg - i, argv + i, sl@0: curInFile, curOutFile, curErrFile, &pid); sl@0: argv[i] = oldName; sl@0: if (result != TCL_OK) { sl@0: goto error; sl@0: } sl@0: Tcl_DStringFree(&execBuffer); sl@0: sl@0: pidPtr[numPids] = pid; sl@0: numPids++; sl@0: sl@0: /* sl@0: * Close off our copies of file descriptors that were set up for sl@0: * this child, then set up the input for the next child. sl@0: */ sl@0: sl@0: if ((curInFile != NULL) && (curInFile != inputFile)) { sl@0: TclpCloseFile(curInFile); sl@0: } sl@0: curInFile = pipeIn; sl@0: pipeIn = NULL; sl@0: sl@0: if ((curOutFile != NULL) && (curOutFile != outputFile)) { sl@0: TclpCloseFile(curOutFile); sl@0: } sl@0: curOutFile = NULL; sl@0: } sl@0: sl@0: *pidArrayPtr = pidPtr; sl@0: sl@0: /* sl@0: * All done. Cleanup open files lying around and then return. sl@0: */ sl@0: sl@0: cleanup: sl@0: Tcl_DStringFree(&execBuffer); sl@0: sl@0: if (inputClose) { sl@0: TclpCloseFile(inputFile); sl@0: } else if (inputRelease) { sl@0: TclpReleaseFile(inputFile); sl@0: } sl@0: if (outputClose) { sl@0: TclpCloseFile(outputFile); sl@0: } else if (outputRelease) { sl@0: TclpReleaseFile(outputFile); sl@0: } sl@0: if (errorClose) { sl@0: TclpCloseFile(errorFile); sl@0: } else if (errorRelease) { sl@0: TclpReleaseFile(errorFile); sl@0: } sl@0: return numPids; sl@0: sl@0: /* sl@0: * An error occurred. There could have been extra files open, such sl@0: * as pipes between children. Clean them all up. Detach any child sl@0: * processes that have been created. sl@0: */ sl@0: sl@0: error: sl@0: if (pipeIn != NULL) { sl@0: TclpCloseFile(pipeIn); sl@0: } sl@0: if ((curOutFile != NULL) && (curOutFile != outputFile)) { sl@0: TclpCloseFile(curOutFile); sl@0: } sl@0: if ((curInFile != NULL) && (curInFile != inputFile)) { sl@0: TclpCloseFile(curInFile); sl@0: } sl@0: if ((inPipePtr != NULL) && (*inPipePtr != NULL)) { sl@0: TclpCloseFile(*inPipePtr); sl@0: *inPipePtr = NULL; sl@0: } sl@0: if ((outPipePtr != NULL) && (*outPipePtr != NULL)) { sl@0: TclpCloseFile(*outPipePtr); sl@0: *outPipePtr = NULL; sl@0: } sl@0: if ((errFilePtr != NULL) && (*errFilePtr != NULL)) { sl@0: TclpCloseFile(*errFilePtr); sl@0: *errFilePtr = NULL; sl@0: } sl@0: if (pidPtr != NULL) { sl@0: for (i = 0; i < numPids; i++) { sl@0: if (pidPtr[i] != (Tcl_Pid) -1) { sl@0: Tcl_DetachPids(1, &pidPtr[i]); sl@0: } sl@0: } sl@0: ckfree((char *) pidPtr); sl@0: } sl@0: numPids = -1; sl@0: goto cleanup; sl@0: } sl@0: sl@0: /* sl@0: *---------------------------------------------------------------------- sl@0: * sl@0: * Tcl_OpenCommandChannel -- sl@0: * sl@0: * Opens an I/O channel to one or more subprocesses specified sl@0: * by argc and argv. The flags argument determines the sl@0: * disposition of the stdio handles. If the TCL_STDIN flag is sl@0: * set then the standard input for the first subprocess will sl@0: * be tied to the channel: writing to the channel will provide sl@0: * input to the subprocess. If TCL_STDIN is not set, then sl@0: * standard input for the first subprocess will be the same as sl@0: * this application's standard input. If TCL_STDOUT is set then sl@0: * standard output from the last subprocess can be read from the sl@0: * channel; otherwise it goes to this application's standard sl@0: * output. If TCL_STDERR is set, standard error output for all sl@0: * subprocesses is returned to the channel and results in an error sl@0: * when the channel is closed; otherwise it goes to this sl@0: * application's standard error. If TCL_ENFORCE_MODE is not set, sl@0: * then argc and argv can redirect the stdio handles to override sl@0: * TCL_STDIN, TCL_STDOUT, and TCL_STDERR; if it is set, then it sl@0: * is an error for argc and argv to override stdio channels for sl@0: * which TCL_STDIN, TCL_STDOUT, and TCL_STDERR have been set. sl@0: * sl@0: * Results: sl@0: * A new command channel, or NULL on failure with an error sl@0: * message left in interp. sl@0: * sl@0: * Side effects: sl@0: * Creates processes, opens pipes. sl@0: * sl@0: *---------------------------------------------------------------------- sl@0: */ sl@0: sl@0: EXPORT_C Tcl_Channel sl@0: Tcl_OpenCommandChannel(interp, argc, argv, flags) sl@0: Tcl_Interp *interp; /* Interpreter for error reporting. Can sl@0: * NOT be NULL. */ sl@0: int argc; /* How many arguments. */ sl@0: CONST char **argv; /* Array of arguments for command pipe. */ sl@0: int flags; /* Or'ed combination of TCL_STDIN, TCL_STDOUT, sl@0: * TCL_STDERR, and TCL_ENFORCE_MODE. */ sl@0: { sl@0: TclFile *inPipePtr, *outPipePtr, *errFilePtr; sl@0: TclFile inPipe, outPipe, errFile; sl@0: int numPids; sl@0: Tcl_Pid *pidPtr; sl@0: Tcl_Channel channel; sl@0: sl@0: inPipe = outPipe = errFile = NULL; sl@0: sl@0: inPipePtr = (flags & TCL_STDIN) ? &inPipe : NULL; sl@0: outPipePtr = (flags & TCL_STDOUT) ? &outPipe : NULL; sl@0: errFilePtr = (flags & TCL_STDERR) ? &errFile : NULL; sl@0: sl@0: numPids = TclCreatePipeline(interp, argc, argv, &pidPtr, inPipePtr, sl@0: outPipePtr, errFilePtr); sl@0: sl@0: if (numPids < 0) { sl@0: goto error; sl@0: } sl@0: sl@0: /* sl@0: * Verify that the pipes that were created satisfy the sl@0: * readable/writable constraints. sl@0: */ sl@0: sl@0: if (flags & TCL_ENFORCE_MODE) { sl@0: if ((flags & TCL_STDOUT) && (outPipe == NULL)) { sl@0: Tcl_AppendResult(interp, "can't read output from command:", sl@0: " standard output was redirected", (char *) NULL); sl@0: goto error; sl@0: } sl@0: if ((flags & TCL_STDIN) && (inPipe == NULL)) { sl@0: Tcl_AppendResult(interp, "can't write input to command:", sl@0: " standard input was redirected", (char *) NULL); sl@0: goto error; sl@0: } sl@0: } sl@0: sl@0: channel = TclpCreateCommandChannel(outPipe, inPipe, errFile, sl@0: numPids, pidPtr); sl@0: sl@0: if (channel == (Tcl_Channel) NULL) { sl@0: Tcl_AppendResult(interp, "pipe for command could not be created", sl@0: (char *) NULL); sl@0: goto error; sl@0: } sl@0: return channel; sl@0: sl@0: error: sl@0: if (numPids > 0) { sl@0: Tcl_DetachPids(numPids, pidPtr); sl@0: ckfree((char *) pidPtr); sl@0: } sl@0: if (inPipe != NULL) { sl@0: TclpCloseFile(inPipe); sl@0: } sl@0: if (outPipe != NULL) { sl@0: TclpCloseFile(outPipe); sl@0: } sl@0: if (errFile != NULL) { sl@0: TclpCloseFile(errFile); sl@0: } sl@0: return NULL; sl@0: }