2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2006 - INRIA - Antoine ELIAS
5 * This file must be used under the terms of the CeCILL.
6 * This source file is licensed as described in the file COPYING, which
7 * you should have received as part of this distribution. The terms
8 * are also available at
9 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
14 //#include "AnalysisVisitor.hxx"
16 #include "functions_gw.hxx"
17 //#include "debugvisitor.hxx"
18 #include "execvisitor.hxx"
19 #include "mutevisitor.hxx"
20 #include "printvisitor.hxx"
21 #include "visitor_common.hxx"
22 #include "scilabWrite.hxx"
23 #include "scilabexception.hxx"
24 #include "configvariable.hxx"
25 #include "threadmanagement.hxx"
33 #include "sci_malloc.h"
34 #include "os_string.h"
37 #include "localization.h"
38 #include "os_string.h"
41 #define MUTE_FLAG L"n"
42 #define NO_MUTE_FLAG L"m"
45 using namespace types;
47 /*--------------------------------------------------------------------------*/
48 Function::ReturnValue sci_execstr(types::typed_list &in, int _iRetCount, types::typed_list &out)
51 bool bErrCatch = false;
53 wchar_t* pstMsg = NULL;
55 wchar_t *pstCommand = NULL;
58 if (in.size() < 1 || in.size() > 3)
60 Scierror(999, _("%s: Wrong number of input arguments: %d to %d expected.\n"), "execstr" , 1, 3);
61 return Function::Error;
68 if (in[1]->isString() == false || in[1]->getAs<types::String>()->getSize() != 1)
70 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "execstr", 2);
71 return Function::Error;
74 String* pS = in[1]->getAs<types::String>();
75 if (os_wcsicmp(pS->get(0), L"errcatch") == 0)
81 Scierror(999, _("%s: Wrong value for input argument #%d: 'errcatch' expected.\n"), "execstr", 2);
82 return Function::Error;
91 if (in[2]->isString() == false || in[2]->getAs<types::String>()->getSize() != 1)
93 Scierror(999, _("%s: Wrong type for input argument #%d: A string expected.\n"), "execstr", 3);
94 return Function::Error;
97 if (os_wcsicmp(in[2]->getAs<types::String>()->get(0), MUTE_FLAG) == 0)
101 else if (os_wcsicmp(in[2]->getAs<types::String>()->get(0), NO_MUTE_FLAG) == 0)
107 Scierror(999, _("%s: Wrong value for input argument #%d: '%s' or '%s' expected.\n"), "execstr", 3, MUTE_FLAG, NO_MUTE_FLAG);
108 return Function::Error;
113 if (in[0]->isDouble() && in[0]->getAs<Double>()->getSize() == 0)
116 out.push_back(Double::Empty());
120 if (in[0]->isString() == false || (in[0]->getAs<types::String>()->getRows() != 1 && in[0]->getAs<types::String>()->getCols() != 1))
122 Scierror(999, _("%s: Wrong type for input argument #%d: Vector of strings expected.\n"), "execstr", 1);
123 return Function::Error;
126 String* pS = in[0]->getAs<types::String>();
127 int iTotalLen = pS->getSize(); //add \n after each string
128 for (int i = 0 ; i < pS->getSize() ; i++)
130 iTotalLen += (int)wcslen(pS->get(i));
133 pstCommand = (wchar_t*)MALLOC(sizeof(wchar_t) * (iTotalLen + 1));//+1 for null termination
135 for (int i = 0, iPos = 0 ; i < pS->getSize() ; i++)
137 wcscpy(pstCommand + iPos, pS->get(i));
138 iPos = (int)wcslen(pstCommand);
139 pstCommand[iPos++] = L'\n';
140 pstCommand[iPos] = 0;
143 ThreadManagement::LockParser();
144 parser.parse(pstCommand);
146 if (parser.getExitStatus() != Parser::Succeded)
150 out.push_back(new Double(999));
151 //to lock last error information
152 ConfigVariable::setLastErrorCall();
153 ConfigVariable::setLastErrorMessage(parser.getErrorMessage());
154 ConfigVariable::setLastErrorNumber(999);
155 ThreadManagement::UnlockParser();
160 char* pst = wide_string_to_UTF8(parser.getErrorMessage());
161 Scierror(999, "%s", pst);
163 ThreadManagement::UnlockParser();
164 return Function::Error;
168 if (ConfigVariable::getSerialize())
170 ast::Exp* temp = parser.getTree();
171 if (ConfigVariable::getTimed())
173 pExp = callTyper(temp, L"execstr");
177 pExp = callTyper(temp);
184 pExp = parser.getTree();
187 ThreadManagement::UnlockParser();
191 return Function::Error;
194 //save current prompt mode
195 int iPromptMode = ConfigVariable::getPromptMode();
196 ConfigVariable::setPromptMode(-1);
198 if (ConfigVariable::getAnalyzerOptions() == 1)
200 //analysis::AnalysisVisitor analysis;
201 //pExp->accept(analysis);
202 //ast::DebugVisitor debugMe;
203 //pExp->accept(debugMe);
206 ast::SeqExp* pSeqExp = pExp->getAs<SeqExp>();
208 // add execstr in list of macro called
209 // to manage line displayed when error occured.
210 ConfigVariable::macroFirstLine_begin(1);
214 ExecVisitor execExps;
215 pSeqExp->accept(execExps);
217 catch (ast::ScilabMessage sm)
219 if (bErrCatch == false && bMute == false)
221 ConfigVariable::macroFirstLine_end();
222 ConfigVariable::setPromptMode(iPromptMode);
226 ConfigVariable::resetWhereError();
227 iErr = ConfigVariable::getLastErrorNumber();
232 out.push_back(new Double(iErr));
233 //to lock last error information
234 ConfigVariable::setLastErrorCall();
236 ConfigVariable::resetError();
239 ConfigVariable::macroFirstLine_end();
240 ConfigVariable::setPromptMode(iPromptMode);
245 /*--------------------------------------------------------------------------*/