static void JimInterpCopyVariable(Jim_Interp *target, Jim_Interp *source, const char *var, const char *default_value) { Jim_Obj *value = Jim_GetGlobalVariableStr(source, var, JIM_NONE); const char *str; str = value ? Jim_String(value) : default_value; if (str) { Jim_SetGlobalVariableStr(target, var, Jim_NewStringObj(target, str, -1)); } }
/* * Create error messages for unusual process exits. An * extra newline gets appended to each error message, but * it gets removed below (in the same fashion that an * extra newline in the command's output is removed). */ static int JimCheckWaitStatus(Jim_Interp *interp, pidtype pid, int waitStatus) { 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, (long)pid)); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WEXITSTATUS(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 %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 %s by signal %d", action, WTERMSIG(waitStatus)); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus))); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, (long)pid)); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, WTERMSIG(waitStatus))); #endif } Jim_SetGlobalVariableStr(interp, "errorCode", errorCode); return rc; }
/* Poor man's implementation of exec with system() * The system() call *may* do command line redirection, etc. * The standard output is not available. * Can't redirect filehandles. */ static int Jim_ExecCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv) { Jim_Obj *cmdlineObj = Jim_NewEmptyStringObj(interp); int i, j; int rc; /* Create a quoted command line */ for (i = 1; i < argc; i++) { int len; const char *arg = Jim_GetString(argv[i], &len); if (i > 1) { Jim_AppendString(interp, cmdlineObj, " ", 1); } if (strpbrk(arg, "\\\" ") == NULL) { /* No quoting required */ Jim_AppendString(interp, cmdlineObj, arg, len); continue; } Jim_AppendString(interp, cmdlineObj, "\"", 1); for (j = 0; j < len; j++) { if (arg[j] == '\\' || arg[j] == '"') { Jim_AppendString(interp, cmdlineObj, "\\", 1); } Jim_AppendString(interp, cmdlineObj, &arg[j], 1); } Jim_AppendString(interp, cmdlineObj, "\"", 1); } rc = system(Jim_String(cmdlineObj)); Jim_FreeNewObj(interp, cmdlineObj); if (rc) { Jim_Obj *errorCode = Jim_NewListObj(interp, NULL, 0); Jim_ListAppendElement(interp, errorCode, Jim_NewStringObj(interp, "CHILDSTATUS", -1)); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, 0)); Jim_ListAppendElement(interp, errorCode, Jim_NewIntObj(interp, rc)); Jim_SetGlobalVariableStr(interp, "errorCode", errorCode); return JIM_ERR; } return JIM_OK; }
void initialize() { interpreter_ = Jim_CreateInterp(); Jim_RegisterCoreCommands(interpreter_); // Register extensions ::Jim_clockInit(interpreter_); ::Jim_regexpInit(interpreter_); // Register built in commands for (auto * cmd : builtInProcs()) Jim_CreateCommand(interpreter_, cmd->name(), cmdProc, cmd, nullptr); for (auto * cmd : builtInSubCmdProcs()) Jim_CreateCommand(interpreter_, cmd->name(), subCmdProc, cmd, nullptr); // Register built in variables for (auto * var : builtInVariables()) Jim_SetGlobalVariableStr(interpreter_, var->name(), var->defaultValue()); #if DEBUG // Use the file from the source directory Jim_EvalFileGlobal(interpreter_, "../src/ScriptingLib.tcl"); #else Jim_EvalSource(interpreter_, __FILE__, __LINE__, std::string((char *)&ScriptingLib[0], BX_COUNTOF(ScriptingLib)).c_str()); #endif std::string configFile = ""; #if BX_PLATFORM_LINUX || BX_PLATFORM_OSX // Try to load the config file char * home = getenv("HOME"); if (home) configFile = std::string(home) + "/." + CONFIG_FILE; else configFile = "~/." + CONFIG_FILE; #else char pwd[1024]; configFile = std::string(bx::pwd(pwd, 1024)) + "/" + CONFIG_FILE; #endif logInfo("Loading config file: ", configFile); Jim_EvalFileGlobal(interpreter_, configFile.c_str()); }