1 /*
2  *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  *  Copyright (C) 2010 - DIGITEO - Antoine ELIAS
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
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.
13  *
14  */
15 
16 #include <stdlib.h>
17 
18 #include "configvariable.hxx"
19 
20 #include "string.hxx"
21 #include "context.hxx"
22 
23 extern "C"
24 {
25 #include "sci_malloc.h"
26 #include "sci_path.h"
27 #include "os_string.h"
28 #include "charEncoding.h"
29 #include "PATH_MAX.h"
30 #include "setenvc.h"
31 #include "getenvc.h"
32 #include "setenvvar.h"
33 #include "getshortpathname.h"
34 #include "getlongpathname.h"
35 }
36 
getSCI(void)37 char *getSCI(void)
38 {
39     return wide_string_to_UTF8(ConfigVariable::getSCIPath().c_str());
40 }
41 
42 /*--------------------------------------------------------------------------*/
getSCIW(void)43 wchar_t *getSCIW(void)
44 {
45     return os_wcsdup(ConfigVariable::getSCIPath().c_str());
46 }
47 
48 /*--------------------------------------------------------------------------*/
setSCIW(const wchar_t * _sci_path)49 void setSCIW(const wchar_t* _sci_path)
50 {
51     char* pstPath = wide_string_to_UTF8(_sci_path);
52     setSCI(pstPath);
53     FREE(pstPath);
54 }
55 /*--------------------------------------------------------------------------*/
setSCI(const char * _sci_path)56 void setSCI(const char* _sci_path)
57 {
58     //
59     BOOL bConvertOK = FALSE;
60     char* ShortPath = getshortpathname(_sci_path, &bConvertOK);
61     char* LongPath = getlongpathname(_sci_path, &bConvertOK);
62 
63 
64     //SCI
65     char *pstSlash = new char[strlen(_sci_path) + 1];
66     AntislashToSlash(ShortPath, pstSlash);
67     wchar_t* pwstSCI = to_wide_string(pstSlash);
68     types::String *pSSCI = new types::String(pwstSCI);
69     symbol::Context::getInstance()->put(symbol::Symbol(L"SCI"), pSSCI);
70 
71     //WSCI
72     wchar_t* pwstWSCI = NULL;
73 #ifdef _MSC_VER
74     char *pstBackSlash = NULL;
75     pstBackSlash = new char[strlen(LongPath) + 1];
76     SlashToAntislash(LongPath, pstBackSlash);
77     pwstWSCI = to_wide_string(pstBackSlash);
78     types::String *pSWSCI = new types::String(pwstWSCI);
79     symbol::Context::getInstance()->put(symbol::Symbol(L"WSCI"), pSWSCI);
80     delete[] pstBackSlash;
81 #else
82     pwstWSCI = to_wide_string(_sci_path);
83 #endif
84 
85     std::wstring wst(pwstWSCI);
86     ConfigVariable::setSCIPath(wst);
87 
88     FREE(pwstWSCI);
89     FREE(pwstSCI);
90     if (pstSlash)
91     {
92         delete[] pstSlash;
93     }
94 
95     if (ShortPath)
96     {
97         FREE(ShortPath);
98     }
99 
100     if (LongPath)
101     {
102         FREE(LongPath);
103     }
104 }
105 /*--------------------------------------------------------------------------*/
putenvSCIW(const wchar_t * _sci_path)106 void putenvSCIW(const wchar_t* _sci_path)
107 {
108     char* pstTemp = wide_string_to_UTF8(_sci_path);
109     putenvSCI(pstTemp);
110     FREE(pstTemp);
111     return;
112 }
113 /*--------------------------------------------------------------------------*/
putenvSCI(const char * _sci_path)114 void putenvSCI(const char* _sci_path)
115 {
116     char *ShortPath = NULL;
117     char *CopyOfDefaultPath = NULL;
118 
119     /* to be sure that it's unix 8.3 format */
120     /* c:/progra~1/scilab-5.0 */
121     BOOL bConvertOK = FALSE;
122     ShortPath = getshortpathname(_sci_path, &bConvertOK);
123 
124     CopyOfDefaultPath = new char[strlen(_sci_path) + 1];
125     //GetShortPathName(_sci_path,ShortPath,PATH_MAX);
126     AntislashToSlash(ShortPath, CopyOfDefaultPath);
127 
128     setenvc("SCI", ShortPath);
129 
130     delete[] CopyOfDefaultPath;
131     FREE(ShortPath);
132 }
133 /*--------------------------------------------------------------------------*/
getenvSCIW()134 wchar_t* getenvSCIW()
135 {
136     char *SciPath = getenvSCI();
137     wchar_t* pstTemp = to_wide_string(SciPath);
138     delete[] SciPath;
139     return pstTemp;
140 }
141 /*--------------------------------------------------------------------------*/
getenvSCI()142 char* getenvSCI()
143 {
144     int ierr, iflag = 0;
145     int lbuf = PATH_MAX;
146     char *SciPath = new char[PATH_MAX];
147 
148     if (SciPath)
149     {
150         getenvc(&ierr, "SCI", SciPath, &lbuf, &iflag);
151 
152         if (ierr == 1)
153         {
154             delete[] SciPath;
155             return NULL;
156         }
157     }
158 
159     return SciPath;
160 }
161 /*--------------------------------------------------------------------------*/
computeSCIW()162 wchar_t* computeSCIW()
163 {
164     char* pstTemp = computeSCI();
165     wchar_t* pstReturn = to_wide_string(pstTemp);
166     delete[] pstTemp;
167     return pstReturn;
168 }
169 /*--------------------------------------------------------------------------*/
170 //windows : find main DLL and extract path
171 //linux and macos : scilab script fill SCI env variable
172 #ifdef _MSC_VER
computeSCI()173 char* computeSCI()
174 {
175     char ScilabModuleName[MAX_PATH + 1];
176     char drive[_MAX_DRIVE];
177     char dir[_MAX_DIR];
178     char fname[_MAX_FNAME];
179     char ext[_MAX_EXT];
180     char SciPathName[_MAX_DRIVE + _MAX_DIR + 5];
181 
182     char *DirTmp = NULL;
183 
184     if (!GetModuleFileNameA((HINSTANCE)GetModuleHandleA("core"), ScilabModuleName, MAX_PATH))
185     {
186         return NULL;
187     }
188 
189     os_splitpath(ScilabModuleName, drive, dir, fname, ext);
190 
191     if (dir[strlen(dir) - 1] == '\\')
192     {
193         dir[strlen(dir) - 1] = '\0';
194     }
195 
196     DirTmp = strrchr(dir, '\\');
197     if (strlen(dir) - strlen(DirTmp) > 0)
198     {
199         dir[strlen(dir) - strlen(DirTmp)] = '\0';
200     }
201     else
202     {
203         return NULL;
204     }
205 
206     os_makepath(SciPathName, drive, dir, NULL, NULL);
207 
208     for (int i = 0 ; i < static_cast<int>(strlen(SciPathName)) ; i++)
209     {
210         if (SciPathName[i] == '\\')
211         {
212             SciPathName[i] = '/';
213         }
214     }
215     SciPathName[strlen(SciPathName) - 1] = '\0';
216     return os_strdup(SciPathName);
217 }
218 #else
computeSCI()219 char* computeSCI()
220 {
221     int ierr, iflag = 0;
222     int lbuf = PATH_MAX;
223     char *SciPathName = new char[PATH_MAX];
224 
225     if (SciPathName)
226     {
227         getenvc(&ierr, "SCI", SciPathName, &lbuf, &iflag);
228 
229         if (ierr == 1)
230         {
231             std::cerr << "SCI environment variable not defined." << std::endl;
232             exit(1);
233         }
234     }
235 
236     return SciPathName;
237 }
238 #endif
239 /*--------------------------------------------------------------------------*/
defineSCI()240 void defineSCI()
241 {
242     wchar_t* sci_path = computeSCIW();
243     setSCIW(sci_path);
244     putenvSCIW(sci_path);
245     FREE(sci_path);
246 }
247 
248