interp: command should be created in the global namespace
[jimtcl.git] / jim-exec.c
blob2ec391b4d6fe206b2ebcec98e95e686555453c87
1 /*
2 * (c) 2008 Steve Bennett <steveb@workware.net.au>
4 * Implements the exec command for Jim
6 * Based on code originally from Tcl 6.7 by John Ousterhout.
7 * From that code:
9 * The Tcl_Fork and Tcl_WaitPids procedures are based on code
10 * contributed by Karl Lehenbauer, Mark Diekhans and Peter
11 * da Silva.
13 * Copyright 1987-1991 Regents of the University of California
14 * Permission to use, copy, modify, and distribute this
15 * software and its documentation for any purpose and without
16 * fee is hereby granted, provided that the above copyright
17 * notice appear in all copies. The University of California
18 * makes no representations about the suitability of this
19 * software for any purpose. It is provided "as is" without
20 * express or implied warranty.
23 #ifndef _GNU_SOURCE
24 #define _GNU_SOURCE
25 #endif
26 #include <string.h>
27 #include <ctype.h>
29 #include "jimautoconf.h"
30 #include <jim.h>
32 #if (!defined(HAVE_VFORK) || !defined(HAVE_WAITPID)) && !defined(__MINGW32__)
33 /* Poor man's implementation of exec with system()
34 * The system() call *may* do command line redirection, etc.
35 * The standard output is not available.
36 * Can't redirect filehandles.
38 static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
40 Jim_Obj *cmdlineObj = Jim_NewEmptyStringObj(interp);
41 int i, j;
42 int rc;
44 /* Create a quoted command line */
45 for (i = 1; i < argc; i++) {
46 int len;
47 const char *arg = Jim_GetString(argv[i], &len);
49 if (i > 1) {
50 Jim_AppendString(interp, cmdlineObj, " ", 1);
52 if (strpbrk(arg, "\\\" ") == NULL) {
53 /* No quoting required */
54 Jim_AppendString(interp, cmdlineObj, arg, len);
55 continue;
58 Jim_AppendString(interp, cmdlineObj, "\"", 1);
59 for (j = 0; j < len; j++) {
60 if (arg[j] == '\\' || arg[j] == '"') {
61 Jim_AppendString(interp, cmdlineObj, "\\", 1);
63 Jim_AppendString(interp, cmdlineObj, &arg[j], 1);
65 Jim_AppendString(interp, cmdlineObj, "\"", 1);
67 rc = system(Jim_String(cmdlineObj));
69 Jim_FreeNewObj(interp, cmdlineObj);
71 if (rc) {
72 Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0);
73 Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1));
74 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, 0));
75 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, rc));
76 Jim_SetGlobalVariableStr(interp, "errorCode", errorCode);
77 return JIM_ERR;
80 return JIM_OK;
83 int Jim_execInit(Jim_Interp *interp)
85 if (Jim_PackageProvide(interp, "exec", "1.0", JIM_ERRMSG))
86 return JIM_ERR;
88 Jim_CreateCommand(interp, "exec", Jim_ExecCmd, NULL, NULL);
89 return JIM_OK;
91 #else
92 /* Full exec implementation for unix and mingw */
94 #include <errno.h>
95 #include <signal.h>
96 #include "jim-signal.h"
97 #include "jimiocompat.h"
98 #include <sys/stat.h>
100 struct WaitInfoTable;
102 static char **JimOriginalEnviron(void);
103 static char **JimSaveEnv(char **env);
104 static void JimRestoreEnv(char **env);
105 static int JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv,
106 pidtype **pidArrayPtr, int *inPipePtr, int *outPipePtr, int *errFilePtr);
107 static void JimDetachPids(struct WaitInfoTable *table, int numPids, const pidtype *pidPtr);
108 static int JimCleanupChildren(Jim_Interp *interp, int numPids, pidtype *pidPtr, Jim_Obj *errStrObj);
109 static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv);
111 #if defined(__MINGW32__)
112 static pidtype JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId);
113 #endif
116 * If the last character of 'objPtr' is a newline, then remove
117 * the newline character.
119 static void Jim_RemoveTrailingNewline(Jim_Obj *objPtr)
121 int len;
122 const char *s = Jim_GetString(objPtr, &len);
124 if (len > 0 && s[len - 1] == '\n') {
125 objPtr->length--;
126 objPtr->bytes[objPtr->length] = '\0';
131 * Read from 'fd', append the data to strObj and close 'fd'.
132 * Returns 1 if data was added, 0 if not, or -1 on error.
134 static int JimAppendStreamToString(Jim_Interp *interp, int fd, Jim_Obj *strObj)
136 char buf[256];
137 FILE *fh = fdopen(fd, "r");
138 int ret = 0;
140 if (fh == NULL) {
141 return -1;
144 while (1) {
145 int retval = fread(buf, 1, sizeof(buf), fh);
146 if (retval > 0) {
147 ret = 1;
148 Jim_AppendString(interp, strObj, buf, retval);
150 if (retval != sizeof(buf)) {
151 break;
154 fclose(fh);
155 return ret;
159 * Builds the environment array from $::env
161 * If $::env is not set, simply returns environ.
163 * Otherwise allocates the environ array from the contents of $::env
165 * If the exec fails, memory can be freed via JimFreeEnv()
167 static char **JimBuildEnv(Jim_Interp *interp)
169 int i;
170 int size;
171 int num;
172 int n;
173 char **envptr;
174 char *envdata;
176 Jim_Obj *objPtr = Jim_GetGlobalVariableStr(interp, "env", JIM_NONE);
178 if (!objPtr) {
179 return JimOriginalEnviron();
182 /* We build the array as a single block consisting of the pointers followed by
183 * the strings. This has the advantage of being easy to allocate/free and being
184 * compatible with both unix and windows
187 /* Calculate the required size */
188 num = Jim_ListLength(interp, objPtr);
189 if (num % 2) {
190 /* Silently drop the last element if not a valid dictionary */
191 num--;
193 /* We need one \0 and one equal sign for each element.
194 * A list has at least one space for each element except the first.
195 * We need one extra char for the extra null terminator and one for the equal sign.
197 size = Jim_Length(objPtr) + 2;
199 envptr = Jim_Alloc(sizeof(*envptr) * (num / 2 + 1) + size);
200 envdata = (char *)&envptr[num / 2 + 1];
202 n = 0;
203 for (i = 0; i < num; i += 2) {
204 const char *s1, *s2;
205 Jim_Obj *elemObj;
207 Jim_ListIndex(interp, objPtr, i, &elemObj, JIM_NONE);
208 s1 = Jim_String(elemObj);
209 Jim_ListIndex(interp, objPtr, i + 1, &elemObj, JIM_NONE);
210 s2 = Jim_String(elemObj);
212 envptr[n] = envdata;
213 envdata += sprintf(envdata, "%s=%s", s1, s2);
214 envdata++;
215 n++;
217 envptr[n] = NULL;
218 *envdata = 0;
220 return envptr;
224 * Frees the environment allocated by JimBuildEnv()
226 * Must pass original_environ.
228 static void JimFreeEnv(char **env, char **original_environ)
230 if (env != original_environ) {
231 Jim_Free(env);
235 static Jim_Obj *JimMakeErrorCode(Jim_Interp *interp, pidtype pid, int waitStatus, Jim_Obj *errStrObj)
237 Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0);
239 if (pid == JIM_BAD_PID || pid == JIM_NO_PID) {
240 Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1));
241 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
242 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, -1));
244 else if (WIFEXITED(waitStatus)) {
245 Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1));
246 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
247 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus)));
249 else {
250 const char *type;
251 const char *action;
252 const char *signame;
254 if (WIFSIGNALED(waitStatus)) {
255 type = "CHILDKILLED";
256 action = "killed";
257 signame = Jim_SignalId(WTERMSIG(waitStatus));
259 else {
260 type = "CHILDSUSP";
261 action = "suspended";
262 signame = "none";
265 Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1));
267 if (errStrObj) {
268 /* Append the message to 'errStrObj' with a newline.
269 * The last newline will be stripped later
271 Jim_AppendStrings(interp, errStrObj, "child ", action, " by signal ", Jim_SignalId(WTERMSIG(waitStatus)), "\n", NULL);
274 Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid));
275 Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, signame, -1));
277 return errorCode;
281 * Create and store an appropriate value for the global variable $::errorCode
282 * Based on pid and waitStatus.
284 * Returns JIM_OK for a normal exit with code 0, otherwise returns JIM_ERR.
286 * Note that $::errorCode is left unchanged for a normal exit.
287 * Details of any abnormal exit is appended to the errStrObj, unless it is NULL.
289 static int JimCheckWaitStatus(Jim_Interp *interp, pidtype pid, int waitStatus, Jim_Obj *errStrObj)
291 if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) {
292 return JIM_OK;
294 Jim_SetGlobalVariableStr(interp, "errorCode", JimMakeErrorCode(interp, pid, waitStatus, errStrObj));
296 return JIM_ERR;
300 * Data structures of the following type are used by exec and
301 * wait to keep track of child processes.
304 struct WaitInfo
306 pidtype pid; /* Process id of child. */
307 int status; /* Status returned when child exited or suspended. */
308 int flags; /* Various flag bits; see below for definitions. */
311 /* This table is shared by exec and wait */
312 struct WaitInfoTable {
313 struct WaitInfo *info; /* Table of outstanding processes */
314 int size; /* Size of the allocated table */
315 int used; /* Number of entries in use */
316 int refcount; /* Free the table once the refcount drops to 0 */
320 * Flag bits in WaitInfo structures:
322 * WI_DETACHED - Non-zero means no-one cares about the
323 * process anymore. Ignore it until it
324 * exits, then forget about it.
327 #define WI_DETACHED 2
329 #define WAIT_TABLE_GROW_BY 4
331 static void JimFreeWaitInfoTable(struct Jim_Interp *interp, void *privData)
333 struct WaitInfoTable *table = privData;
335 if (--table->refcount == 0) {
336 Jim_Free(table->info);
337 Jim_Free(table);
341 static struct WaitInfoTable *JimAllocWaitInfoTable(void)
343 struct WaitInfoTable *table = Jim_Alloc(sizeof(*table));
344 table->info = NULL;
345 table->size = table->used = 0;
346 table->refcount = 1;
348 return table;
352 * Removes the given pid from the wait table.
354 * Returns 0 if OK or -1 if not found.
356 static int JimWaitRemove(struct WaitInfoTable *table, pidtype pid)
358 int i;
360 /* Find it in the table */
361 for (i = 0; i < table->used; i++) {
362 if (pid == table->info[i].pid) {
363 if (i != table->used - 1) {
364 table->info[i] = table->info[table->used - 1];
366 table->used--;
367 return 0;
370 return -1;
374 * The main [exec] command
376 static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
378 int outputId; /* File id for output pipe. -1 means command overrode. */
379 int errorId; /* File id for temporary file containing error output. */
380 pidtype *pidPtr;
381 int numPids, result;
382 int child_siginfo = 1;
383 Jim_Obj *childErrObj;
384 Jim_Obj *errStrObj;
385 struct WaitInfoTable *table = Jim_CmdPrivData(interp);
388 * See if the command is to be run in the background; if so, create
389 * the command, detach it, and return.
391 if (argc > 1 && Jim_CompareStringImmediate(interp, argv[argc - 1], "&")) {
392 Jim_Obj *listObj;
393 int i;
395 argc--;
396 numPids = JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, NULL, NULL);
397 if (numPids < 0) {
398 return JIM_ERR;
400 /* The return value is a list of the pids */
401 listObj = Jim_NewListObj(interp, NULL, 0);
402 for (i = 0; i < numPids; i++) {
403 Jim_ListAppendElement(interp, listObj, Jim_NewIntObj(interp, (long)pidPtr[i]));
405 Jim_SetResult(interp, listObj);
406 JimDetachPids(table, numPids, pidPtr);
407 Jim_Free(pidPtr);
408 return JIM_OK;
412 * Create the command's pipeline.
414 numPids =
415 JimCreatePipeline(interp, argc - 1, argv + 1, &pidPtr, NULL, &outputId, &errorId);
417 if (numPids < 0) {
418 return JIM_ERR;
421 result = JIM_OK;
423 errStrObj = Jim_NewStringObj(interp, "", 0);
425 /* Read from the output pipe until EOF */
426 if (outputId != -1) {
427 if (JimAppendStreamToString(interp, outputId, errStrObj) < 0) {
428 result = JIM_ERR;
429 Jim_SetResultErrno(interp, "error reading from output pipe");
433 /* Now wait for children to finish. Any abnormal results are appended to childErrObj */
434 childErrObj = Jim_NewStringObj(interp, "", 0);
435 Jim_IncrRefCount(childErrObj);
437 if (JimCleanupChildren(interp, numPids, pidPtr, childErrObj) != JIM_OK) {
438 result = JIM_ERR;
442 * Read the child's error output (if any) and put it into the result.
444 * Note that unlike Tcl, the presence of stderr output does not cause
445 * exec to return an error.
447 if (errorId != -1) {
448 int ret;
449 lseek(errorId, 0, SEEK_SET);
450 ret = JimAppendStreamToString(interp, errorId, errStrObj);
451 if (ret < 0) {
452 Jim_SetResultErrno(interp, "error reading from error pipe");
453 result = JIM_ERR;
455 else if (ret > 0) {
456 /* Got some error output, so discard the abnormal info string */
457 child_siginfo = 0;
461 if (child_siginfo) {
462 /* Append the child siginfo to the result */
463 Jim_AppendObj(interp, errStrObj, childErrObj);
465 Jim_DecrRefCount(interp, childErrObj);
467 /* Finally remove any trailing newline from the result */
468 Jim_RemoveTrailingNewline(errStrObj);
470 /* Set this as the result */
471 Jim_SetResult(interp, errStrObj);
473 return result;
477 * Does waitpid() on the given pid, and then removes the
478 * entry from the wait table.
480 * Returns the pid if OK and updates *statusPtr with the status,
481 * or JIM_BAD_PID if the pid was not in the table.
483 static pidtype JimWaitForProcess(struct WaitInfoTable *table, pidtype pid, int *statusPtr)
485 if (JimWaitRemove(table, pid) == 0) {
486 /* wait for it */
487 waitpid(pid, statusPtr, 0);
488 return pid;
491 /* Not found */
492 return JIM_BAD_PID;
496 * Indicates that one or more child processes have been placed in
497 * background and are no longer cared about.
498 * These children can be cleaned up with JimReapDetachedPids().
500 static void JimDetachPids(struct WaitInfoTable *table, int numPids, const pidtype *pidPtr)
502 int j;
504 for (j = 0; j < numPids; j++) {
505 /* Find it in the table */
506 int i;
507 for (i = 0; i < table->used; i++) {
508 if (pidPtr[j] == table->info[i].pid) {
509 table->info[i].flags |= WI_DETACHED;
510 break;
516 /* Use 'name getfd' to get the file descriptor associated with channel 'name'
517 * Returns the file descriptor or -1 on error
519 static int JimGetChannelFd(Jim_Interp *interp, const char *name)
521 Jim_Obj *objv[2];
523 objv[0] = Jim_NewStringObj(interp, name, -1);
524 objv[1] = Jim_NewStringObj(interp, "getfd", -1);
526 if (Jim_EvalObjVector(interp, 2, objv) == JIM_OK) {
527 jim_wide fd;
528 if (Jim_GetWide(interp, Jim_GetResult(interp), &fd) == JIM_OK) {
529 return fd;
532 return -1;
535 static void JimReapDetachedPids(struct WaitInfoTable *table)
537 struct WaitInfo *waitPtr;
538 int count;
539 int dest;
541 if (!table) {
542 return;
545 waitPtr = table->info;
546 dest = 0;
547 for (count = table->used; count > 0; waitPtr++, count--) {
548 if (waitPtr->flags & WI_DETACHED) {
549 int status;
550 pidtype pid = waitpid(waitPtr->pid, &status, WNOHANG);
551 if (pid == waitPtr->pid) {
552 /* Process has exited, so remove it from the table */
553 table->used--;
554 continue;
557 if (waitPtr != &table->info[dest]) {
558 table->info[dest] = *waitPtr;
560 dest++;
565 * wait ?-nohang? ?pid?
567 * An interface to waitpid(2)
569 * Returns a 3 element list.
571 * If the process has not exited or doesn't exist, returns:
573 * {NONE x x}
575 * If the process exited normally, returns:
577 * {CHILDSTATUS <pid> <exit-status>}
579 * If the process terminated on a signal, returns:
581 * {CHILDKILLED <pid> <signal>}
583 * Otherwise (core dump, stopped, continued, ...), returns:
585 * {CHILDSUSP <pid> none}
587 * With no arguments, reaps any finished background processes started by exec ... &
589 static int Jim_WaitCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
591 struct WaitInfoTable *table = Jim_CmdPrivData(interp);
592 int nohang = 0;
593 pidtype pid;
594 long pidarg;
595 int status;
596 Jim_Obj *errCodeObj;
598 /* With no arguments, reap detached children */
599 if (argc == 1) {
600 JimReapDetachedPids(table);
601 return JIM_OK;
604 if (argc > 1 && Jim_CompareStringImmediate(interp, argv[1], "-nohang")) {
605 nohang = 1;
607 if (argc != nohang + 2) {
608 Jim_WrongNumArgs(interp, 1, argv, "?-nohang? ?pid?");
609 return JIM_ERR;
611 if (Jim_GetLong(interp, argv[nohang + 1], &pidarg) != JIM_OK) {
612 return JIM_ERR;
615 pid = waitpid((pidtype)pidarg, &status, nohang ? WNOHANG : 0);
617 errCodeObj = JimMakeErrorCode(interp, pid, status, NULL);
619 if (pid != JIM_BAD_PID && (WIFEXITED(status) || WIFSIGNALED(status))) {
620 /* The process has finished. Remove it from the wait table if it exists there */
621 JimWaitRemove(table, pid);
623 Jim_SetResult(interp, errCodeObj);
624 return JIM_OK;
627 static int Jim_PidCommand(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
629 if (argc != 1) {
630 Jim_WrongNumArgs(interp, 1, argv, "");
631 return JIM_ERR;
634 Jim_SetResultInt(interp, (jim_wide)getpid());
635 return JIM_OK;
639 *----------------------------------------------------------------------
641 * JimCreatePipeline --
643 * Given an argc/argv array, instantiate a pipeline of processes
644 * as described by the argv.
646 * Results:
647 * The return value is a count of the number of new processes
648 * created, or -1 if an error occurred while creating the pipeline.
649 * *pidArrayPtr is filled in with the address of a dynamically
650 * allocated array giving the ids of all of the processes. It
651 * is up to the caller to free this array when it isn't needed
652 * anymore. If inPipePtr is non-NULL, *inPipePtr is filled in
653 * with the file id for the input pipe for the pipeline (if any):
654 * the caller must eventually close this file. If outPipePtr
655 * isn't NULL, then *outPipePtr is filled in with the file id
656 * for the output pipe from the pipeline: the caller must close
657 * this file. If errFilePtr isn't NULL, then *errFilePtr is filled
658 * with a file id that may be used to read error output after the
659 * pipeline completes.
661 * Side effects:
662 * Processes and pipes are created.
664 *----------------------------------------------------------------------
666 static int
667 JimCreatePipeline(Jim_Interp *interp, int argc, Jim_Obj *const *argv, pidtype **pidArrayPtr,
668 int *inPipePtr, int *outPipePtr, int *errFilePtr)
670 pidtype *pidPtr = NULL; /* Points to malloc-ed array holding all
671 * the pids of child processes. */
672 int numPids = 0; /* Actual number of processes that exist
673 * at *pidPtr right now. */
674 int cmdCount; /* Count of number of distinct commands
675 * found in argc/argv. */
676 const char *input = NULL; /* Describes input for pipeline, depending
677 * on "inputFile". NULL means take input
678 * from stdin/pipe. */
679 int input_len = 0; /* Length of input, if relevant */
681 #define FILE_NAME 0 /* input/output: filename */
682 #define FILE_APPEND 1 /* output only: filename, append */
683 #define FILE_HANDLE 2 /* input/output: filehandle */
684 #define FILE_TEXT 3 /* input only: input is actual text */
686 int inputFile = FILE_NAME; /* 1 means input is name of input file.
687 * 2 means input is filehandle name.
688 * 0 means input holds actual
689 * text to be input to command. */
691 int outputFile = FILE_NAME; /* 0 means output is the name of output file.
692 * 1 means output is the name of output file, and append.
693 * 2 means output is filehandle name.
694 * All this is ignored if output is NULL
696 int errorFile = FILE_NAME; /* 0 means error is the name of error file.
697 * 1 means error is the name of error file, and append.
698 * 2 means error is filehandle name.
699 * All this is ignored if error is NULL
701 const char *output = NULL; /* Holds name of output file to pipe to,
702 * or NULL if output goes to stdout/pipe. */
703 const char *error = NULL; /* Holds name of stderr file to pipe to,
704 * or NULL if stderr goes to stderr/pipe. */
705 int inputId = -1;
706 /* Readable file id input to current command in
707 * pipeline (could be file or pipe). -1
708 * means use stdin. */
709 int outputId = -1;
710 /* Writable file id for output from current
711 * command in pipeline (could be file or pipe).
712 * -1 means use stdout. */
713 int errorId = -1;
714 /* Writable file id for all standard error
715 * output from all commands in pipeline. -1
716 * means use stderr. */
717 int lastOutputId = -1;
718 /* Write file id for output from last command
719 * in pipeline (could be file or pipe).
720 * -1 means use stdout. */
721 int pipeIds[2]; /* File ids for pipe that's being created. */
722 int firstArg, lastArg; /* Indexes of first and last arguments in
723 * current command. */
724 int lastBar;
725 int i;
726 pidtype pid;
727 char **save_environ;
728 struct WaitInfoTable *table = Jim_CmdPrivData(interp);
730 /* Holds the args which will be used to exec */
731 char **arg_array = Jim_Alloc(sizeof(*arg_array) * (argc + 1));
732 int arg_count = 0;
734 if (inPipePtr != NULL) {
735 *inPipePtr = -1;
737 if (outPipePtr != NULL) {
738 *outPipePtr = -1;
740 if (errFilePtr != NULL) {
741 *errFilePtr = -1;
743 pipeIds[0] = pipeIds[1] = -1;
746 * First, scan through all the arguments to figure out the structure
747 * of the pipeline. Count the number of distinct processes (it's the
748 * number of "|" arguments). If there are "<", "<<", or ">" arguments
749 * then make note of input and output redirection and remove these
750 * arguments and the arguments that follow them.
752 cmdCount = 1;
753 lastBar = -1;
754 for (i = 0; i < argc; i++) {
755 const char *arg = Jim_String(argv[i]);
757 if (arg[0] == '<') {
758 inputFile = FILE_NAME;
759 input = arg + 1;
760 if (*input == '<') {
761 inputFile = FILE_TEXT;
762 input_len = Jim_Length(argv[i]) - 2;
763 input++;
765 else if (*input == '@') {
766 inputFile = FILE_HANDLE;
767 input++;
770 if (!*input && ++i < argc) {
771 input = Jim_GetString(argv[i], &input_len);
774 else if (arg[0] == '>') {
775 int dup_error = 0;
777 outputFile = FILE_NAME;
779 output = arg + 1;
780 if (*output == '>') {
781 outputFile = FILE_APPEND;
782 output++;
784 if (*output == '&') {
785 /* Redirect stderr too */
786 output++;
787 dup_error = 1;
789 if (*output == '@') {
790 outputFile = FILE_HANDLE;
791 output++;
793 if (!*output && ++i < argc) {
794 output = Jim_String(argv[i]);
796 if (dup_error) {
797 errorFile = outputFile;
798 error = output;
801 else if (arg[0] == '2' && arg[1] == '>') {
802 error = arg + 2;
803 errorFile = FILE_NAME;
805 if (*error == '@') {
806 errorFile = FILE_HANDLE;
807 error++;
809 else if (*error == '>') {
810 errorFile = FILE_APPEND;
811 error++;
813 if (!*error && ++i < argc) {
814 error = Jim_String(argv[i]);
817 else {
818 if (strcmp(arg, "|") == 0 || strcmp(arg, "|&") == 0) {
819 if (i == lastBar + 1 || i == argc - 1) {
820 Jim_SetResultString(interp, "illegal use of | or |& in command", -1);
821 goto badargs;
823 lastBar = i;
824 cmdCount++;
826 /* Either |, |& or a "normal" arg, so store it in the arg array */
827 arg_array[arg_count++] = (char *)arg;
828 continue;
831 if (i >= argc) {
832 Jim_SetResultFormatted(interp, "can't specify \"%s\" as last word in command", arg);
833 goto badargs;
837 if (arg_count == 0) {
838 Jim_SetResultString(interp, "didn't specify command to execute", -1);
839 badargs:
840 Jim_Free(arg_array);
841 return -1;
844 /* Must do this before vfork(), so do it now */
845 save_environ = JimSaveEnv(JimBuildEnv(interp));
848 * Set up the redirected input source for the pipeline, if
849 * so requested.
851 if (input != NULL) {
852 if (inputFile == FILE_TEXT) {
854 * Immediate data in command. Create temporary file and
855 * put data into file.
857 inputId = Jim_MakeTempFile(interp, NULL, 1);
858 if (inputId == -1) {
859 goto error;
861 if (write(inputId, input, input_len) != input_len) {
862 Jim_SetResultErrno(interp, "couldn't write temp file");
863 close(inputId);
864 goto error;
866 lseek(inputId, 0L, SEEK_SET);
868 else if (inputFile == FILE_HANDLE) {
869 int fd = JimGetChannelFd(interp, input);
871 if (fd < 0) {
872 goto error;
874 inputId = dup(fd);
876 else {
878 * File redirection. Just open the file.
880 inputId = Jim_OpenForRead(input);
881 if (inputId == -1) {
882 Jim_SetResultFormatted(interp, "couldn't read file \"%s\": %s", input, strerror(Jim_Errno()));
883 goto error;
887 else if (inPipePtr != NULL) {
888 if (pipe(pipeIds) != 0) {
889 Jim_SetResultErrno(interp, "couldn't create input pipe for command");
890 goto error;
892 inputId = pipeIds[0];
893 *inPipePtr = pipeIds[1];
894 pipeIds[0] = pipeIds[1] = -1;
898 * Set up the redirected output sink for the pipeline from one
899 * of two places, if requested.
901 if (output != NULL) {
902 if (outputFile == FILE_HANDLE) {
903 int fd = JimGetChannelFd(interp, output);
904 if (fd < 0) {
905 goto error;
907 lastOutputId = dup(fd);
909 else {
911 * Output is to go to a file.
913 lastOutputId = Jim_OpenForWrite(output, outputFile == FILE_APPEND);
914 if (lastOutputId == -1) {
915 Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", output, strerror(Jim_Errno()));
916 goto error;
920 else if (outPipePtr != NULL) {
922 * Output is to go to a pipe.
924 if (pipe(pipeIds) != 0) {
925 Jim_SetResultErrno(interp, "couldn't create output pipe");
926 goto error;
928 lastOutputId = pipeIds[1];
929 *outPipePtr = pipeIds[0];
930 pipeIds[0] = pipeIds[1] = -1;
932 /* If we are redirecting stderr with 2>filename or 2>@fileId, then we ignore errFilePtr */
933 if (error != NULL) {
934 if (errorFile == FILE_HANDLE) {
935 if (strcmp(error, "1") == 0) {
936 /* Special 2>@1 */
937 if (lastOutputId != -1) {
938 errorId = dup(lastOutputId);
940 else {
941 /* No redirection of stdout, so just use 2>@stdout */
942 error = "stdout";
945 if (errorId == -1) {
946 int fd = JimGetChannelFd(interp, error);
947 if (fd < 0) {
948 goto error;
950 errorId = dup(fd);
953 else {
955 * Output is to go to a file.
957 errorId = Jim_OpenForWrite(error, errorFile == FILE_APPEND);
958 if (errorId == -1) {
959 Jim_SetResultFormatted(interp, "couldn't write file \"%s\": %s", error, strerror(Jim_Errno()));
960 goto error;
964 else if (errFilePtr != NULL) {
966 * Set up the standard error output sink for the pipeline, if
967 * requested. Use a temporary file which is opened, then deleted.
968 * Could potentially just use pipe, but if it filled up it could
969 * cause the pipeline to deadlock: we'd be waiting for processes
970 * to complete before reading stderr, and processes couldn't complete
971 * because stderr was backed up.
973 errorId = Jim_MakeTempFile(interp, NULL, 1);
974 if (errorId == -1) {
975 goto error;
977 *errFilePtr = dup(errorId);
981 * Scan through the argc array, forking off a process for each
982 * group of arguments between "|" arguments.
985 pidPtr = Jim_Alloc(cmdCount * sizeof(*pidPtr));
986 for (i = 0; i < numPids; i++) {
987 pidPtr[i] = JIM_BAD_PID;
989 for (firstArg = 0; firstArg < arg_count; numPids++, firstArg = lastArg + 1) {
990 int pipe_dup_err = 0;
991 int origErrorId = errorId;
993 for (lastArg = firstArg; lastArg < arg_count; lastArg++) {
994 if (strcmp(arg_array[lastArg], "|") == 0) {
995 break;
997 if (strcmp(arg_array[lastArg], "|&") == 0) {
998 pipe_dup_err = 1;
999 break;
1003 if (lastArg == firstArg) {
1004 Jim_SetResultString(interp, "missing command to exec", -1);
1005 goto error;
1008 /* Replace | with NULL for execv() */
1009 arg_array[lastArg] = NULL;
1010 if (lastArg == arg_count) {
1011 outputId = lastOutputId;
1013 else {
1014 if (pipe(pipeIds) != 0) {
1015 Jim_SetResultErrno(interp, "couldn't create pipe");
1016 goto error;
1018 outputId = pipeIds[1];
1021 /* Need to do this before vfork() */
1022 if (pipe_dup_err) {
1023 errorId = outputId;
1026 /* Now fork the child */
1028 #ifdef __MINGW32__
1029 pid = JimStartWinProcess(interp, &arg_array[firstArg], save_environ, inputId, outputId, errorId);
1030 if (pid == JIM_BAD_PID) {
1031 Jim_SetResultFormatted(interp, "couldn't exec \"%s\"", arg_array[firstArg]);
1032 goto error;
1034 #else
1036 * Make a new process and enter it into the table if the fork
1037 * is successful.
1039 pid = vfork();
1040 if (pid < 0) {
1041 Jim_SetResultErrno(interp, "couldn't fork child process");
1042 goto error;
1044 if (pid == 0) {
1045 /* Child */
1046 if (inputId != -1) dup2(inputId, fileno(stdin));
1047 if (outputId != -1) dup2(outputId, fileno(stdout));
1048 if (errorId != -1) dup2(errorId, fileno(stderr));
1050 for (i = 3; (i <= outputId) || (i <= inputId) || (i <= errorId); i++) {
1051 close(i);
1054 /* Restore SIGPIPE behaviour */
1055 (void)signal(SIGPIPE, SIG_DFL);
1057 execvpe(arg_array[firstArg], &arg_array[firstArg], Jim_GetEnviron());
1059 fprintf(stderr, "couldn't exec \"%s\"\n", arg_array[firstArg]);
1060 #ifdef JIM_MAINTAINER
1062 /* Keep valgrind happy */
1063 static char *const false_argv[2] = {"false", NULL};
1064 execvp(false_argv[0],false_argv);
1066 #endif
1067 _exit(127);
1069 #endif
1071 /* parent */
1074 * Enlarge the wait table if there isn't enough space for a new
1075 * entry.
1077 if (table->used == table->size) {
1078 table->size += WAIT_TABLE_GROW_BY;
1079 table->info = Jim_Realloc(table->info, table->size * sizeof(*table->info));
1082 table->info[table->used].pid = pid;
1083 table->info[table->used].flags = 0;
1084 table->used++;
1086 pidPtr[numPids] = pid;
1088 /* Restore in case of pipe_dup_err */
1089 errorId = origErrorId;
1092 * Close off our copies of file descriptors that were set up for
1093 * this child, then set up the input for the next child.
1096 if (inputId != -1) {
1097 close(inputId);
1099 if (outputId != -1) {
1100 close(outputId);
1101 outputId = -1;
1103 inputId = pipeIds[0];
1104 pipeIds[0] = pipeIds[1] = -1;
1106 *pidArrayPtr = pidPtr;
1109 * All done. Cleanup open files lying around and then return.
1112 cleanup:
1113 if (inputId != -1) {
1114 close(inputId);
1116 if (lastOutputId != -1) {
1117 close(lastOutputId);
1119 if (errorId != -1) {
1120 close(errorId);
1122 Jim_Free(arg_array);
1124 JimRestoreEnv(save_environ);
1126 return numPids;
1129 * An error occurred. There could have been extra files open, such
1130 * as pipes between children. Clean them all up. Detach any child
1131 * processes that have been created.
1134 error:
1135 if ((inPipePtr != NULL) && (*inPipePtr != -1)) {
1136 close(*inPipePtr);
1137 *inPipePtr = -1;
1139 if ((outPipePtr != NULL) && (*outPipePtr != -1)) {
1140 close(*outPipePtr);
1141 *outPipePtr = -1;
1143 if ((errFilePtr != NULL) && (*errFilePtr != -1)) {
1144 close(*errFilePtr);
1145 *errFilePtr = -1;
1147 if (pipeIds[0] != -1) {
1148 close(pipeIds[0]);
1150 if (pipeIds[1] != -1) {
1151 close(pipeIds[1]);
1153 if (pidPtr != NULL) {
1154 for (i = 0; i < numPids; i++) {
1155 if (pidPtr[i] != JIM_BAD_PID) {
1156 JimDetachPids(table, 1, &pidPtr[i]);
1159 Jim_Free(pidPtr);
1161 numPids = -1;
1162 goto cleanup;
1166 *----------------------------------------------------------------------
1168 * JimCleanupChildren --
1170 * This is a utility procedure used to wait for child processes
1171 * to exit, record information about abnormal exits.
1173 * Results:
1174 * The return value is a standard Tcl result. If anything at
1175 * weird happened with the child processes, JIM_ERR is returned
1176 * and a structured message is left in $::errorCode.
1177 * If errStrObj is not NULL, abnormal exit details are appended to this object.
1179 * Side effects:
1180 * pidPtr is freed
1182 *----------------------------------------------------------------------
1185 static int JimCleanupChildren(Jim_Interp *interp, int numPids, pidtype *pidPtr, Jim_Obj *errStrObj)
1187 struct WaitInfoTable *table = Jim_CmdPrivData(interp);
1188 int result = JIM_OK;
1189 int i;
1191 /* Now check the return status of each child */
1192 for (i = 0; i < numPids; i++) {
1193 int waitStatus = 0;
1194 if (JimWaitForProcess(table, pidPtr[i], &waitStatus) != JIM_BAD_PID) {
1195 if (JimCheckWaitStatus(interp, pidPtr[i], waitStatus, errStrObj) != JIM_OK) {
1196 result = JIM_ERR;
1200 Jim_Free(pidPtr);
1202 return result;
1205 int Jim_execInit(Jim_Interp *interp)
1207 struct WaitInfoTable *waitinfo;
1208 if (Jim_PackageProvide(interp, "exec", "1.0", JIM_ERRMSG))
1209 return JIM_ERR;
1211 #ifdef SIGPIPE
1213 * Disable SIGPIPE signals: if they were allowed, this process
1214 * might go away unexpectedly if children misbehave. This code
1215 * can potentially interfere with other application code that
1216 * expects to handle SIGPIPEs.
1218 * By doing this in the init function, applications can override
1219 * this later. Note that child processes have SIGPIPE restored
1220 * to the default after vfork().
1222 (void)signal(SIGPIPE, SIG_IGN);
1223 #endif
1225 waitinfo = JimAllocWaitInfoTable();
1226 Jim_CreateCommand(interp, "exec", Jim_ExecCmd, waitinfo, JimFreeWaitInfoTable);
1227 waitinfo->refcount++;
1228 Jim_CreateCommand(interp, "wait", Jim_WaitCommand, waitinfo, JimFreeWaitInfoTable);
1229 Jim_CreateCommand(interp, "pid", Jim_PidCommand, 0, 0);
1231 return JIM_OK;
1234 #if defined(__MINGW32__)
1235 /* Windows-specific (mingw) implementation */
1237 static int
1238 JimWinFindExecutable(const char *originalName, char fullPath[MAX_PATH])
1240 int i;
1241 static char extensions[][5] = {".exe", "", ".bat"};
1243 for (i = 0; i < (int) (sizeof(extensions) / sizeof(extensions[0])); i++) {
1244 snprintf(fullPath, MAX_PATH, "%s%s", originalName, extensions[i]);
1246 if (SearchPath(NULL, fullPath, NULL, MAX_PATH, fullPath, NULL) == 0) {
1247 continue;
1249 if (GetFileAttributes(fullPath) & FILE_ATTRIBUTE_DIRECTORY) {
1250 continue;
1252 return 0;
1255 return -1;
1258 static char **JimSaveEnv(char **env)
1260 return env;
1263 static void JimRestoreEnv(char **env)
1265 JimFreeEnv(env, Jim_GetEnviron());
1268 static char **JimOriginalEnviron(void)
1270 return NULL;
1273 static Jim_Obj *
1274 JimWinBuildCommandLine(Jim_Interp *interp, char **argv)
1276 char *start, *special;
1277 int quote, i;
1279 Jim_Obj *strObj = Jim_NewStringObj(interp, "", 0);
1281 for (i = 0; argv[i]; i++) {
1282 if (i > 0) {
1283 Jim_AppendString(interp, strObj, " ", 1);
1286 if (argv[i][0] == '\0') {
1287 quote = 1;
1289 else {
1290 quote = 0;
1291 for (start = argv[i]; *start != '\0'; start++) {
1292 if (isspace(UCHAR(*start))) {
1293 quote = 1;
1294 break;
1298 if (quote) {
1299 Jim_AppendString(interp, strObj, "\"" , 1);
1302 start = argv[i];
1303 for (special = argv[i]; ; ) {
1304 if ((*special == '\\') && (special[1] == '\\' ||
1305 special[1] == '"' || (quote && special[1] == '\0'))) {
1306 Jim_AppendString(interp, strObj, start, special - start);
1307 start = special;
1308 while (1) {
1309 special++;
1310 if (*special == '"' || (quote && *special == '\0')) {
1312 * N backslashes followed a quote -> insert
1313 * N * 2 + 1 backslashes then a quote.
1316 Jim_AppendString(interp, strObj, start, special - start);
1317 break;
1319 if (*special != '\\') {
1320 break;
1323 Jim_AppendString(interp, strObj, start, special - start);
1324 start = special;
1326 if (*special == '"') {
1327 if (special == start) {
1328 Jim_AppendString(interp, strObj, "\"", 1);
1330 else {
1331 Jim_AppendString(interp, strObj, start, special - start);
1333 Jim_AppendString(interp, strObj, "\\\"", 2);
1334 start = special + 1;
1336 if (*special == '\0') {
1337 break;
1339 special++;
1341 Jim_AppendString(interp, strObj, start, special - start);
1342 if (quote) {
1343 Jim_AppendString(interp, strObj, "\"", 1);
1346 return strObj;
1350 * Note that inputId, etc. are osf_handles.
1352 static pidtype
1353 JimStartWinProcess(Jim_Interp *interp, char **argv, char **env, int inputId, int outputId, int errorId)
1355 STARTUPINFO startInfo;
1356 PROCESS_INFORMATION procInfo;
1357 HANDLE hProcess;
1358 char execPath[MAX_PATH];
1359 pidtype pid = JIM_BAD_PID;
1360 Jim_Obj *cmdLineObj;
1361 char *winenv;
1363 if (JimWinFindExecutable(argv[0], execPath) < 0) {
1364 return JIM_BAD_PID;
1366 argv[0] = execPath;
1368 hProcess = GetCurrentProcess();
1369 cmdLineObj = JimWinBuildCommandLine(interp, argv);
1372 * STARTF_USESTDHANDLES must be used to pass handles to child process.
1373 * Using SetStdHandle() and/or dup2() only works when a console mode
1374 * parent process is spawning an attached console mode child process.
1377 ZeroMemory(&startInfo, sizeof(startInfo));
1378 startInfo.cb = sizeof(startInfo);
1379 startInfo.dwFlags = STARTF_USESTDHANDLES;
1380 startInfo.hStdInput = INVALID_HANDLE_VALUE;
1381 startInfo.hStdOutput= INVALID_HANDLE_VALUE;
1382 startInfo.hStdError = INVALID_HANDLE_VALUE;
1385 * Duplicate all the handles which will be passed off as stdin, stdout
1386 * and stderr of the child process. The duplicate handles are set to
1387 * be inheritable, so the child process can use them.
1390 * If stdin was not redirected, input should come from the parent's stdin
1392 if (inputId == -1) {
1393 inputId = _fileno(stdin);
1395 DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(inputId), hProcess, &startInfo.hStdInput,
1396 0, TRUE, DUPLICATE_SAME_ACCESS);
1397 if (startInfo.hStdInput == INVALID_HANDLE_VALUE) {
1398 goto end;
1402 * If stdout was not redirected, output should go to the parent's stdout
1404 if (outputId == -1) {
1405 outputId = _fileno(stdout);
1407 DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(outputId), hProcess, &startInfo.hStdOutput,
1408 0, TRUE, DUPLICATE_SAME_ACCESS);
1409 if (startInfo.hStdOutput == INVALID_HANDLE_VALUE) {
1410 goto end;
1413 /* Ditto stderr */
1414 if (errorId == -1) {
1415 errorId = _fileno(stderr);
1417 DuplicateHandle(hProcess, (HANDLE)_get_osfhandle(errorId), hProcess, &startInfo.hStdError,
1418 0, TRUE, DUPLICATE_SAME_ACCESS);
1419 if (startInfo.hStdError == INVALID_HANDLE_VALUE) {
1420 goto end;
1423 /* If env is NULL, use the original environment.
1424 * If env[0] is NULL, use an empty environment.
1425 * Otherwise use the environment starting at env[0]
1427 if (env == NULL) {
1428 /* Use the original environment */
1429 winenv = NULL;
1431 else if (env[0] == NULL) {
1432 winenv = (char *)"\0";
1434 else {
1435 winenv = env[0];
1438 if (!CreateProcess(NULL, (char *)Jim_String(cmdLineObj), NULL, NULL, TRUE,
1439 0, winenv, NULL, &startInfo, &procInfo)) {
1440 goto end;
1444 * "When an application spawns a process repeatedly, a new thread
1445 * instance will be created for each process but the previous
1446 * instances may not be cleaned up. This results in a significant
1447 * virtual memory loss each time the process is spawned. If there
1448 * is a WaitForInputIdle() call between CreateProcess() and
1449 * CloseHandle(), the problem does not occur." PSS ID Number: Q124121
1452 WaitForInputIdle(procInfo.hProcess, 5000);
1453 CloseHandle(procInfo.hThread);
1455 pid = procInfo.hProcess;
1457 end:
1458 Jim_FreeNewObj(interp, cmdLineObj);
1459 if (startInfo.hStdInput != INVALID_HANDLE_VALUE) {
1460 CloseHandle(startInfo.hStdInput);
1462 if (startInfo.hStdOutput != INVALID_HANDLE_VALUE) {
1463 CloseHandle(startInfo.hStdOutput);
1465 if (startInfo.hStdError != INVALID_HANDLE_VALUE) {
1466 CloseHandle(startInfo.hStdError);
1468 return pid;
1471 #else
1473 static char **JimOriginalEnviron(void)
1475 return Jim_GetEnviron();
1478 static char **JimSaveEnv(char **env)
1480 char **saveenv = Jim_GetEnviron();
1481 Jim_SetEnviron(env);
1482 return saveenv;
1485 static void JimRestoreEnv(char **env)
1487 JimFreeEnv(Jim_GetEnviron(), env);
1488 Jim_SetEnviron(env);
1490 #endif
1491 #endif