2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2008-2008 - DIGITEO - Antoine ELIAS
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
7 * This file is hereby licensed under the terms of the GNU GPL v2.0,
8 * pursuant to article 5.3.4 of the CeCILL v.2.1.
9 * This file was originally licensed under the terms of the CeCILL v2.1,
10 * and continues to be available under such terms.
11 * For more information, see the COPYING file which you should have received
12 * along with this program.
17 #pragma warning(disable : 4996) //It's not beautifull but that works !
26 #include <libxml/xpath.h>
27 #include <libxml/xmlreader.h>
29 #include "sci_malloc.h"
30 #include "funcmanager.hxx"
31 #include "execvisitor.hxx"
32 #include "configvariable.hxx"
33 #include "module_declaration.hxx"
38 #include "findfiles.h"
39 #include "configvariable_interface.h"
40 #include "os_string.h"
43 #define BASENAMEMODULESFILE L"etc/modules.xml"
45 bool FileExist(std::string _szFile);
46 bool FileExist(std::wstring _szFile);
47 char *GetXmlFileEncoding(const std::string& _filename);
49 FuncManager* FuncManager::me = NULL;
51 FuncManager* FuncManager::getInstance()
55 me = new FuncManager();
56 me->CreateModuleList();
57 /*get module activation list from xml file*/
58 if (me->GetModules() == true)
60 if (me->AppendModules() == false)
76 void FuncManager::destroyInstance()
85 FuncManager::FuncManager(void)
90 FuncManager::~FuncManager(void)
94 bool FuncManager::GetModules()
96 std::wstring szModulesFilename;
98 std::wstring szPath = ConfigVariable::getSCIPath();
101 std::wcout << L"The SCI environment variable is not set." << std::endl;
105 szModulesFilename = szPath + L"/";
106 szModulesFilename += BASENAMEMODULESFILE;
108 if (FileExist(szModulesFilename))
110 m_szXmlFile = szModulesFilename;
114 std::wcout << L"Cannot load the module declaration file: " << szModulesFilename << std::endl;
120 bool FuncManager::AppendModules()
122 char* pstTemp = wide_string_to_UTF8(m_szXmlFile.c_str());
124 char *encoding = GetXmlFileEncoding(pstTemp);
126 /* Don't care about line return / empty line */
127 xmlKeepBlanksDefault(0);
128 /* check if the XML file has been encoded with utf8 (unicode) or not */
129 if (stricmp("utf-8", encoding) == 0)
132 xmlXPathContextPtr xpathCtxt = NULL;
133 xmlXPathObjectPtr xpathObj = NULL;
137 doc = xmlParseFile (pstTemp);
141 std::cout << "Error: Could not parse file " << pstTemp << std::endl;
148 xpathCtxt = xmlXPathNewContext(doc);
149 xpathObj = xmlXPathEval((const xmlChar*)"//modules/module", xpathCtxt);
151 if (xpathObj && xpathObj->nodesetval->nodeMax)
153 /* the Xpath has been understood and there are node */
154 for (int i = 0 ; i < xpathObj->nodesetval->nodeNr ; i++)
157 xmlAttrPtr attrib = xpathObj->nodesetval->nodeTab[i]->properties;
158 /* Get the properties of <module> */
159 while (attrib != NULL)
161 /* loop until when have read all the attributes */
162 if (xmlStrEqual (attrib->name, (const xmlChar*)"name"))
164 /* we found the tag name */
165 const char *str = (const char*)attrib->children->content;
170 name = os_strdup(str);
172 else if (xmlStrEqual(attrib->name, (const xmlChar*)"activate"))
174 /* we found the tag activate */
175 const char *str = (const char*)attrib->children->content;
176 if (stricmp(str, "yes") == 0 || strcmp(str, "1") == 0)
181 attrib = attrib->next;
184 if ((name) && (strlen(name) > 0) && (activate))
186 wchar_t* pstName = to_wide_string(name);
187 if (VerifyModule(pstName))
189 m_ModuleName.push_back(pstName);
193 std::wcout << pstName << " module not found." << std::endl;
209 xmlXPathFreeObject(xpathObj);
213 xmlXPathFreeContext(xpathCtxt);
219 std::cout << "Error: Not a valid module file " << pstTemp << " (encoding not 'utf-8') Encoding '" << encoding << "' found." << std::endl;
224 ConfigVariable::setModuleList(m_ModuleName);
230 bool FuncManager::VerifyModule(wchar_t* _pszModuleName)
232 std::wstring SciPath = ConfigVariable::getSCIPath();
235 std::wcout << L"The SCI environment variable is not set." << std::endl;
239 std::wstring FullPathModuleName = SciPath + L"/modules/" + _pszModuleName + L"/etc/" + _pszModuleName + START_EXT;
241 /* ajouter d'autres tests d'existences */
243 if (!FileExist(FullPathModuleName))
251 bool FileExist(std::string _szFile)
253 bool bReturn = false;
254 std::fstream filestr(_szFile.c_str(), std::fstream::in);
255 bReturn = !filestr.fail();
260 bool FileExist(std::wstring _szFile)
262 bool bReturn = false;
263 char *pstFile = wide_string_to_UTF8(_szFile.c_str());
264 std::wfstream filestr(pstFile, std::wfstream::in);
265 bReturn = !filestr.fail();
271 char *GetXmlFileEncoding(const std::string& _filename)
273 #define DEFAULT_ENCODING "UTF-8"
275 char *encoding = NULL;
276 xmlDocPtr doc = NULL;
279 encoding = os_strdup(DEFAULT_ENCODING);
281 doc = xmlParseFile(_filename.c_str());
291 encoding = os_strdup((char*)doc->encoding);
299 bool FuncManager::CreateModuleList(void)
301 m_ModuleMap[L"elementary_functions"] = std::pair<GW_MOD, GW_MOD>(&ElemFuncModule::Load, &ElemFuncModule::Unload);
302 m_ModuleMap[L"sparse"] = std::pair<GW_MOD, GW_MOD>(&SparseModule::Load, &SparseModule::Unload);
303 m_ModuleMap[L"boolean"] = std::pair<GW_MOD, GW_MOD>(&BooleanModule::Load, &BooleanModule::Unload);
304 m_ModuleMap[L"integer"] = std::pair<GW_MOD, GW_MOD>(&IntegerModule::Load, &IntegerModule::Unload);
305 m_ModuleMap[L"core"] = std::pair<GW_MOD, GW_MOD>(&CoreModule::Load, &CoreModule::Unload);
306 m_ModuleMap[L"io"] = std::pair<GW_MOD, GW_MOD>(&IoModule::Load, &IoModule::Unload);
307 m_ModuleMap[L"functions"] = std::pair<GW_MOD, GW_MOD>(&FunctionsModule::Load, &FunctionsModule::Unload);
308 m_ModuleMap[L"output_stream"] = std::pair<GW_MOD, GW_MOD>(&OutputStreamModule::Load, &OutputStreamModule::Unload);
309 m_ModuleMap[L"matio"] = std::pair<GW_MOD, GW_MOD>(&MatioModule::Load, &MatioModule::Unload);
310 m_ModuleMap[L"fileio"] = std::pair<GW_MOD, GW_MOD>(&FileioModule::Load, &FileioModule::Unload);
311 m_ModuleMap[L"gui"] = std::pair<GW_MOD, GW_MOD>(&GuiModule::Load, &GuiModule::Unload);
312 m_ModuleMap[L"time"] = std::pair<GW_MOD, GW_MOD>(&TimeModule::Load, &TimeModule::Unload);
313 m_ModuleMap[L"string"] = std::pair<GW_MOD, GW_MOD>(&StringModule::Load, &StringModule::Unload);
314 m_ModuleMap[L"scinotes"] = std::pair<GW_MOD, GW_MOD>(&ScinotesModule::Load, &ScinotesModule::Unload);
315 m_ModuleMap[L"localization"] = std::pair<GW_MOD, GW_MOD>(&LocalizationModule::Load, &LocalizationModule::Unload);
316 m_ModuleMap[L"helptools"] = std::pair<GW_MOD, GW_MOD>(&HelptoolsModule::Load, &HelptoolsModule::Unload);
317 m_ModuleMap[L"hdf5"] = std::pair<GW_MOD, GW_MOD>(&Hdf5Module::Load, &Hdf5Module::Unload);
318 m_ModuleMap[L"dynamic_link"] = std::pair<GW_MOD, GW_MOD>(&DynamicLinkModule::Load, &DynamicLinkModule::Unload);
319 m_ModuleMap[L"action_binding"] = std::pair<GW_MOD, GW_MOD>(&ActionBindingModule::Load, &ActionBindingModule::Unload);
320 m_ModuleMap[L"history_manager"] = std::pair<GW_MOD, GW_MOD>(&HistoryManagerModule::Load, &HistoryManagerModule::Unload);
321 m_ModuleMap[L"console"] = std::pair<GW_MOD, GW_MOD>(&ConsoleModule::Load, &ConsoleModule::Unload);
322 m_ModuleMap[L"signal_processing"] = std::pair<GW_MOD, GW_MOD>(&SignalProcessingModule::Load, &SignalProcessingModule::Unload);
323 m_ModuleMap[L"linear_algebra"] = std::pair<GW_MOD, GW_MOD>(&LinearAlgebraModule::Load, &LinearAlgebraModule::Unload);
324 m_ModuleMap[L"statistics"] = std::pair<GW_MOD, GW_MOD>(&StatisticsModule::Load, &StatisticsModule::Unload);
325 m_ModuleMap[L"differential_equations"] = std::pair<GW_MOD, GW_MOD>(&DifferentialEquationsModule::Load, &DifferentialEquationsModule::Unload);
326 m_ModuleMap[L"cacsd"] = std::pair<GW_MOD, GW_MOD>(&CacsdModule::Load, &CacsdModule::Unload);
327 m_ModuleMap[L"spreadsheet"] = std::pair<GW_MOD, GW_MOD>(&SpreadsheetModule::Load, &SpreadsheetModule::Unload);
328 m_ModuleMap[L"randlib"] = std::pair<GW_MOD, GW_MOD>(&RandlibModule::Load, &RandlibModule::Unload);
329 m_ModuleMap[L"graphics"] = std::pair<GW_MOD, GW_MOD>(&GraphicsModule::Load, &GraphicsModule::Unload);
330 m_ModuleMap[L"interpolation"] = std::pair<GW_MOD, GW_MOD>(&InterpolationModule::Load, &InterpolationModule::Unload);
331 m_ModuleMap[L"sound"] = std::pair<GW_MOD, GW_MOD>(&SoundModule::Load, &SoundModule::Unload);
332 m_ModuleMap[L"umfpack"] = std::pair<GW_MOD, GW_MOD>(&UmfpackModule::Load, &UmfpackModule::Unload);
333 m_ModuleMap[L"optimization"] = std::pair<GW_MOD, GW_MOD>(&OptimizationModule::Load, &OptimizationModule::Unload);
334 m_ModuleMap[L"special_functions"] = std::pair<GW_MOD, GW_MOD>(&SpecialFunctionsModule::Load, &SpecialFunctionsModule::Unload);
335 m_ModuleMap[L"graphic_export"] = std::pair<GW_MOD, GW_MOD>(&GraphicExportModule::Load, &GraphicExportModule::Unload);
336 m_ModuleMap[L"polynomials"] = std::pair<GW_MOD, GW_MOD>(&PolynomialsModule::Load, &PolynomialsModule::Unload);
337 m_ModuleMap[L"arnoldi"] = std::pair<GW_MOD, GW_MOD>(&ArnoldiModule::Load, &ArnoldiModule::Unload);
338 m_ModuleMap[L"data_structures"] = std::pair<GW_MOD, GW_MOD>(&DataStructuresModule::Load, &DataStructuresModule::Unload);
339 m_ModuleMap[L"call_scilab"] = std::pair<GW_MOD, GW_MOD>(&CallScilabModule::Load, &CallScilabModule::Unload);
340 m_ModuleMap[L"completion"] = std::pair<GW_MOD, GW_MOD>(&CompletionModule::Load, &CompletionModule::Unload);
341 m_ModuleMap[L"xml"] = std::pair<GW_MOD, GW_MOD>(&XmlModule::Load, &XmlModule::Unload);
342 m_ModuleMap[L"scicos"] = std::pair<GW_MOD, GW_MOD>(&ScicosModule::Load, &ScicosModule::Unload);
343 m_ModuleMap[L"xcos"] = std::pair<GW_MOD, GW_MOD>(&XcosModule::Load, &XcosModule::Unload);
344 m_ModuleMap[L"fftw"] = std::pair<GW_MOD, GW_MOD>(&FFTWModule::Load, &FFTWModule::Unload);
345 m_ModuleMap[L"mpi"] = std::pair<GW_MOD, GW_MOD>(&MPIModule::Load, &MPIModule::Unload);
346 m_ModuleMap[L"external_objects"] = std::pair<GW_MOD, GW_MOD>(&ExternalObjectsModule::Load, &ExternalObjectsModule::Unload);
347 m_ModuleMap[L"external_objects_java"] = std::pair<GW_MOD, GW_MOD>(&ExternalObjectsJavaModule::Load, &ExternalObjectsJavaModule::Unload);
348 m_ModuleMap[L"preferences"] = std::pair<GW_MOD, GW_MOD>(&PreferencesModule::Load, &PreferencesModule::Unload);
349 m_ModuleMap[L"slint"] = std::pair<GW_MOD, GW_MOD>(&SlintModule::Load, &SlintModule::Unload);
350 m_ModuleMap[L"coverage"] = std::pair<GW_MOD, GW_MOD>(&CoverageModule::Load, &SlintModule::Unload);
352 if (ConfigVariable::getScilabMode() != SCILAB_NWNI)
354 m_ModuleMap[L"tclsci"] = std::pair<GW_MOD, GW_MOD>(&TclsciModule::Load, &TclsciModule::Unload);
355 m_ModuleMap[L"jvm"] = std::pair<GW_MOD, GW_MOD>(&JvmModule::Load, &JvmModule::Unload);
356 m_ModuleMap[L"ui_data"] = std::pair<GW_MOD, GW_MOD>(&UiDataModule::Load, &UiDataModule::Unload);
359 m_ModuleMap[L"windows_tools"] = std::pair<GW_MOD, GW_MOD>(&WindowsToolsModule::Load, &WindowsToolsModule::Unload);
364 bool FuncManager::ExecuteFile(const std::wstring& _stFile)
368 parser.parseFile(_stFile, ConfigVariable::getSCIPath());
370 if (parser.getExitStatus() == Parser::Failed)
372 std::wostringstream ostr;
373 ostr << _W("Unable to execute : ") << _stFile << std::endl;
374 scilabWriteW(ostr.str().c_str());
375 delete parser.getTree();
379 //save current prompt mode
380 int oldVal = ConfigVariable::getPromptMode();
381 //set mode silent for errors
382 ConfigVariable::setPromptMode(-1);
385 ast::ExecVisitor exec;
386 parser.getTree()->accept(exec);
388 catch (const ast::InternalError& ie)
390 scilabWriteW(ie.GetErrorMessage().c_str());
393 //restore previous prompt mode
394 ConfigVariable::setPromptMode(oldVal);
395 delete parser.getTree();
399 bool FuncManager::LoadModules()
402 for (const auto & it : m_ModuleName)
404 ModuleMap::iterator itModule = m_ModuleMap.find(it);
405 if (itModule != m_ModuleMap.end())
407 //call ::Load function
408 itModule->second.first();
415 bool FuncManager::EndModules()
418 for (const auto & it : m_ModuleName)
427 bool FuncManager::UnloadModules()
430 for (const auto & it : m_ModuleName)
432 ModuleMap::iterator itModule = m_ModuleMap.find(it);
433 if (itModule != m_ModuleMap.end())
435 //call ::Unload function
436 itModule->second.second();
440 //Unload shared libraries
441 int iCount = ConfigVariable::getDynModuleCount();
442 DynLibHandle* libs = ConfigVariable::getAllDynModule();
443 for (int i = 0 ; i < iCount ; i++)
445 FreeDynLibrary(libs[i]);
448 ConfigVariable::cleanDynModule();
453 bool FuncManager::ExecuteStartFile(const std::wstring& _stModule)
455 //build .start filename
456 std::wstring stPath = ConfigVariable::getSCIPath();
457 stPath += MODULE_DIR;
463 return ExecuteFile(stPath);
466 bool FuncManager::ExecuteQuitFile(const std::wstring& _stModule)
468 //build .quit filename
469 std::wstring stPath = ConfigVariable::getSCIPath();
470 stPath += MODULE_DIR;
476 return ExecuteFile(stPath);