int executeCommand(wchar_t *wcommand) { int iErr = -1; while(!isEmptyCommandQueue()) { Sleep(1); } // lock interpreter ONLY ONE Scilab while (bLockExec) { Sleep(1); } bLockExec = true; if (isNamedVarExist(L"__ERROR_EXECSTR__")) { scirun(L"clear __ERROR_EXECSTR__;quit"); } if (createNamedSingleWideString(L"__EXECSTR_STRING__", wcommand)) { if (isNamedVarExist(L"__EXECSTR_STRING__")) { std::wstring command_execstr(L"__ERROR_EXECSTR__ = execstr(__EXECSTR_STRING__, \"errcatch\", \"n\");quit"); scirun((wchar_t*)command_execstr.c_str()); if (isNamedVarExist(L"__ERROR_EXECSTR__")) { double dError = 0; if (getNamedScalarDouble(L"__ERROR_EXECSTR__", &dError)) { iErr = (int)dError; } } } } if (isNamedVarExist(L"__ERROR_EXECSTR__")) { scirun(L"clear __ERROR_EXECSTR__;quit"); } if (isNamedVarExist(L"__EXECSTR_STRING__")) { scirun(L"clear __EXECSTR_STRING__;quit"); } scirun(L"quit"); // unlock interpreter bLockExec = false; return iErr; }
int sci_deleteNamedVariable(char *fname, unsigned long fname_len) { SciErr sciErr; int iRet = 0; int* piAddr = NULL; char* pstVarName = NULL; CheckRhs(1, 1); CheckLhs(1, 1); sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddr); if (sciErr.iErr) { printError(&sciErr, 0); return 1; } if (getAllocatedSingleString(pvApiCtx, piAddr, &pstVarName)) { //error return 1; } if (isNamedVarExist(pvApiCtx, pstVarName)) { iRet = deleteNamedVariable(pvApiCtx, pstVarName); } createScalarBoolean(pvApiCtx, Rhs + 1, iRet); AssignOutputVariable(pvApiCtx, 1) = Rhs + 1; return 0; }
/*--------------------------------------------------------------------------*/ void C2F(texmacsin)(char buffer[], int *buf_size, int *len_line, int *eof, long int dummy1) { #define STDIN_ID 5 #define TEXMACSLIB "texmacslib" int nr = 0, info = 0; if (first == 1) { if (isNamedVarExist(pvApiCtx, TEXMACSLIB) == 0) { if (getWarningMode()) { fprintf(stdout, _("Please install texmacs ATOMS module: atomsInstall('texmacs')\n\n"), _("Warning")); } } fprintf(stdout, "%cverbatim:", DATA_BEGIN); } next_input (); first = 0; *eof = 0; info = LineRead(STDIN_ID, buffer, *buf_size, len_line, &nr); fprintf(stdout, "%cverbatim:", DATA_BEGIN); *len_line = *len_line - 1; }
char * getUnnamedVariable(void) { char buffer[128]; int i = 0; do { sprintf(buffer, "%s%i", "unnamed", i++); } while (isNamedVarExist(pvApiCtx, buffer)); return strdup(buffer); }
/*--------------------------------------------------------------------------*/ int deleteNamedVariable(void* _pvCtx, const char* _pstName) { SciErr sciErr; sciErr.iErr = 0; sciErr.iMsgCount = 0; int iVarID[nsiz]; int iZero = 0; int il; int sRhs = Rhs; int sLhs = Lhs; int sTop = Top; if (isNamedVarExist(_pvCtx, _pstName) == 0) { return 0; } if (!checkNamedVarFormat(_pvCtx, _pstName)) { addErrorMessage(&sciErr, API_ERROR_INVALID_NAME, _("%s: Invalid variable name."), "createNamedComplexZMatrixOfDouble"); return 0; } //get varId from varName C2F(str2name)(_pstName, iVarID, (int)strlen(_pstName)); //create a null matrix a the Top of the stack Top = Top + 1; il = iadr(*Lstk(Top)); *istk(il) = 0; *Lstk(Top + 1) = *Lstk(Top) + 1; Rhs = 0; //Replace existing value by null matrix to delete it C2F(stackp) (iVarID, &iZero); Rhs = sRhs; Lhs = sLhs; Top = sTop ; if (C2F(iop).err > 0/* || C2F(errgst).err1 > 0*/) { return 0; } //No Idea :x Fin = 1; return 1; }
int ScilabGateway::import(char * fname, const int envId, void * pvApiCtx) { SciErr err; int rows, cols; char ** className = 0; std::string * name = 0; int named = 1; int * addr = 0; int ret = 0; int nbArgs = Rhs; int error = 0; char * cwd = 0; CheckInputArgumentAtLeast(pvApiCtx, 1); ScilabAbstractEnvironment & env = ScilabEnvironments::getEnvironment(envId); OptionsHelper & helper = env.getOptionsHelper(); ScilabGatewayOptions & options = env.getGatewayOptions(); OptionsHelper::setCopyOccurred(false); ScilabObjects::initialization(env, pvApiCtx); options.setIsNew(false); err = getVarAddressFromPosition(pvApiCtx, Rhs, &addr); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (isBooleanType(pvApiCtx, addr)) { nbArgs = Rhs - 1; if (getScalarBoolean(pvApiCtx, addr, &named)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } if (nbArgs == 1) { err = getVarAddressFromPosition(pvApiCtx, 1, &addr); if (err.iErr) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (!isStringType(pvApiCtx, addr)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), 1); } if (getAllocatedMatrixOfString(pvApiCtx, addr, &rows, &cols, &className)) { throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } else { className = (char**)MALLOC(sizeof(char *) * nbArgs); for (int i = 1; i <= nbArgs; i++) { err = getVarAddressFromPosition(pvApiCtx, i, &addr); if (err.iErr) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } if (!isStringType(pvApiCtx, addr)) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Wrong type for input argument #%d: A String expected."), i); } if (getAllocatedSingleString(pvApiCtx, addr, &(className[i - 1]))) { freeAllocatedMatrixOfString(1, i, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Invalid variable: cannot retrieve the data")); } } rows = 1; cols = nbArgs; } if (named) { name = new std::string[rows * cols]; for (int i = 0; i < rows * cols; i++) { name[i] = std::string(className[i]); if (helper.getUseLastName()) { std::size_t pos = name[i].find_last_of('.'); if (pos != std::string::npos) { if (pos == name[i].size() - 1) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot end with a dot.")); } name[i] = name[i].substr(pos + 1); } } else { std::size_t pos = name[i].find_first_of('.'); if (pos != std::string::npos) { if (pos == 0) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("The class name cannot start with a dot.")); } name[i] = name[i].substr(0, pos); } } if (isNamedVarExist(pvApiCtx, name[i].c_str())) { addr = 0; err = getVarAddressFromName(pvApiCtx, name[i].c_str(), &addr); if (err.iErr || addr == 0 || !ScilabObjects::isValidExternal(addr, pvApiCtx) || ScilabObjects::getEnvironmentId(addr, pvApiCtx) != envId) { freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("A variable with this name is already existing")); } } } } if (!named && rows * cols != Lhs) { freeAllocatedMatrixOfString(rows, cols, className); throw ScilabAbstractEnvironmentException(__LINE__, __FILE__, gettext("Bad number of output arguments"), __FILE__, __LINE__); } const int type = helper.getNewAllowed() ? EXTERNAL_CLASS : EXTERNAL_OBJECT; cwd = scigetcwd(&error); if (error) { FREE(cwd); cwd = 0; } for (int i = 0; i < rows * cols; i++) { try { ret = env.loadclass(className[i], cwd, static_cast<bool>(named), helper.getAllowReload()); } catch (std::exception & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); if (named) { delete[] name; } throw; } if (named) { try { ScilabObjects::createNamedEnvironmentObject(type, name[i].c_str(), ret, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); delete[] name; throw; } } else { try { ScilabObjects::createEnvironmentObjectAtPos(type, Rhs + i + 1, ret, envId, pvApiCtx); } catch (ScilabAbstractEnvironmentException & e) { FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); env.removeobject(ret); throw; } LhsVar(i + 1) = Rhs + i + 1; } } FREE(cwd); freeAllocatedMatrixOfString(rows, cols, className); if (named) { delete[] name; LhsVar(1) = 0; } PutLhsVar(); return 0; }
BOOL isExistingVariable(char *variableName) { int iExisting = isNamedVarExist(NULL, variableName); return iExisting != 0; /* 0 = not existing variable */ }