diff options
author | Steve Bennett <steveb@workware.net.au> | 2010-09-10 15:20:17 +1000 |
---|---|---|
committer | Steve Bennett <steveb@workware.net.au> | 2010-10-15 11:02:51 +1000 |
commit | ec978d041463c9effdb17018f064df29592f8d40 (patch) | |
tree | 502ca4cc1d79fbb1b397706ca0047bbd49ae507d /jim-exec.c | |
parent | 388ccb1bf2a4055f194cb300822689f6b5ce3a6b (diff) | |
download | jimtcl-ec978d041463c9effdb17018f064df29592f8d40.zip jimtcl-ec978d041463c9effdb17018f064df29592f8d40.tar.gz jimtcl-ec978d041463c9effdb17018f064df29592f8d40.tar.bz2 |
Set $::errorCode from exec
This allows the return code from a failed 'exec' command to be retrieved.
Also support -errorcode in catch and return.
Signed-off-by: Steve Bennett <steveb@workware.net.au>
Diffstat (limited to 'jim-exec.c')
-rw-r--r-- | jim-exec.c | 49 |
1 files changed, 38 insertions, 11 deletions
@@ -93,22 +93,49 @@ static void JimTrimTrailingNewline(Jim_Interp *interp) */ static int JimCheckWaitStatus(Jim_Interp *interp, int pid, int waitStatus) { - /* REVISIT: Child exit status is lost here */ - if (WIFEXITED(waitStatus) && WEXITSTATUS(waitStatus) == 0) { - return JIM_OK; + Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); + int rc = JIM_ERR; + + if (WIFEXITED(waitStatus)) { + if (WEXITSTATUS(waitStatus) == 0) { + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "NONE", -1)); + rc = JIM_OK; + } + else { + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(waitStatus))); + } } - else if (WIFSIGNALED(waitStatus)) { + else { + const char *type; + const char *action; + + if (WIFSIGNALED(waitStatus)) { + type = "CHILDKILLED"; + action = "killed"; + } + else { + type = "CHILDSUSP"; + action = "suspended"; + } + + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, type, -1)); + #ifdef jim_ext_signal - Jim_SetResultFormatted(interp, "child killed by signal %s", - Jim_SignalId(WTERMSIG(waitStatus))); + Jim_SetResultFormatted(interp, "child %s by signal %s", action, Jim_SignalId(WTERMSIG(waitStatus))); + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, Jim_SignalId(WTERMSIG(waitStatus)), -1)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, Jim_SignalName(WTERMSIG(waitStatus)), -1)); #else - Jim_SetResultFormatted(interp, "child killed by signal %d", WTERMSIG(waitStatus)); + Jim_SetResultFormatted(interp, "child %s by signal %d", action, WTERMSIG(waitStatus)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus))); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, pid)); + Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus))); #endif } - else if (WIFSTOPPED(waitStatus)) { - Jim_SetResultString(interp, "child suspended", -1); - } - return JIM_ERR; + Jim_SetGlobalVariableStr(interp, "errorCode", errorCode); + return rc; } #if defined(HAVE_FORK) && !defined(HAVE_NO_FORK) |