1 /*
2 *  Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 *  Copyright (C) 2008-2008 - 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 #ifdef _MSC_VER
17 #ifndef MAX_PATH_SHORT
18 #define MAX_PATH_SHORT 260
19 #endif
20 //Only for windows
21 #pragma warning(disable : 4996) //It's not beautifull but that works !
22 #endif
23 
24 #include <iostream>
25 
26 #ifdef _MSC_VER
27 #define NOMINMAX
28 #include "windows.h"
29 #define putenv _putenv
30 
31 #else
32 
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/stat.h>
36 
37 #define  DIRMODE	0777
38 
39 #endif
40 
41 #include "context.hxx"
42 #include "configvariable.hxx"
43 
44 extern "C"
45 {
46 #include "setenvvar.h"
47 #include "sci_home.h"
48 #include "home.h"
49 #include "sci_path.h"
50 #include "sci_tmpdir.h"
51 #include "sci_malloc.h"
52 #include "getshortpathname.h"
53 }
54 
55 static void SetScilabVariables(void);
56 
57 
58 /*--------------------------------------------------------------------------*/
59 /**
60 * Les variables d'environnements SCI, and some others
61 * sont d�finies directement dans scilab
62 * scilex peut donc etre execut� seul
63 */
64 
65 /*--------------------------------------------------------------------------*/
66 /**
67 * Define SCI and some others Environments variables
68 */
SetScilabEnvironment(void)69 void SetScilabEnvironment(void)
70 {
71 #ifdef _MSC_VER
72     //windows check
73     SciEnvForWindows();
74 #endif
75 
76     SetScilabVariables();
77 }
78 
SetScilabVariables(void)79 static void SetScilabVariables(void)
80 {
81     //create SCI
82     defineSCI();
83     //create SCIHOME
84     defineSCIHOME();
85     //create TMPDIR
86     defineTMPDIR();
87     //create home
88     defineHOME();
89 }
90 
91 #ifdef _MSC_VER
SciEnvForWindows(void)92 void SciEnvForWindows(void)
93 {
94     char *SCIPathName = computeSCI();
95     /* Correction Bug 1579 */
96     if (!IsTheGoodShell())
97     {
98         if ( (!Set_Shell()) || (!IsTheGoodShell()))
99         {
100             std::cout << "Please modify ""ComSpec"" environment variable." << std::endl << "cmd.exe on W2K and more." << std::endl;
101         }
102     }
103 
104     SetScilabEnvironmentVariables(SCIPathName);
105     if (SCIPathName)
106     {
107         FREE(SCIPathName);
108         SCIPathName = NULL;
109     }
110 }
111 #endif
112 
113 #ifdef _MSC_VER
114 /*--------------------------------------------------------------------------*/
IsTheGoodShell(void)115 BOOL IsTheGoodShell(void)
116 {
117     bool bOK = false;
118     char shellCmd[PATH_MAX];
119     char drive[_MAX_DRIVE];
120     char dir[_MAX_DIR];
121     char fname[_MAX_FNAME];
122     char ext[_MAX_EXT];
123 
124     strcpy(shellCmd, "");
125     strcpy(fname, "");
126     GetEnvironmentVariableA("ComSpec", shellCmd, PATH_MAX);
127     os_splitpath(shellCmd, drive, dir, fname, ext);
128 
129     if (_stricmp(fname, "cmd") == 0)
130     {
131         bOK = true;
132     }
133 
134     return bOK;
135 }
136 
137 /*--------------------------------------------------------------------------*/
Set_Shell(void)138 BOOL Set_Shell(void)
139 {
140     bool bOK = false;
141     char env[_MAX_DRIVE + _MAX_DIR + _MAX_FNAME + _MAX_EXT + 10];
142     char *WINDIRPATH = NULL;
143 
144     WINDIRPATH = getenv ("SystemRoot");
145     sprintf(env, "ComSpec=%s\\system32\\cmd.exe", WINDIRPATH);
146 
147     if (putenv (env))
148     {
149         bOK = false;
150     }
151     else
152     {
153         bOK = true;
154     }
155 
156     return bOK;
157 }
158 
AddScilabBinDirectoryToPATHEnvironnementVariable(char * DefaultPath)159 static BOOL AddScilabBinDirectoryToPATHEnvironnementVariable(char *DefaultPath)
160 {
161 #define PATH_FORMAT "PATH=%s/bin;%s"
162 
163     BOOL bOK = FALSE;
164     char *PATH = NULL;
165     char *env = NULL;
166 
167     PATH = getenv("PATH");
168 
169     env = (char*) MALLOC(sizeof(char) * (strlen(PATH_FORMAT) + strlen(PATH) +
170                                          strlen(DefaultPath) + 1));
171     if (env)
172     {
173         sprintf(env, PATH_FORMAT, DefaultPath, PATH);
174         if (_putenv (env))
175         {
176             bOK = FALSE;
177         }
178         else
179         {
180             bOK = TRUE;
181         }
182         FREE(env);
183         env = NULL;
184     }
185     return bOK;
186 }
187 
Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB(void)188 void Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB(void)
189 {
190 #ifdef _MSC_VER
191     _putenv ("COMPILER=VC++");
192 #endif
193 
194     /* WIN32 variable Environment */
195 #ifdef _WIN32
196     _putenv ("WIN32=OK");
197 #endif
198 
199     /* WIN64 variable Environment */
200 #ifdef _WIN64
201     _putenv ("WIN64=OK");
202 #endif
203 
204     if ( GetSystemMetrics(SM_REMOTESESSION) )
205     {
206         _putenv ("SCILAB_MSTS_SESSION=OK");
207     }
208 }
209 
SetScilabEnvironmentVariables(char * DefaultSCIPATH)210 void SetScilabEnvironmentVariables(char *DefaultSCIPATH)
211 {
212     if (DefaultSCIPATH)
213     {
214         Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB();
215         AddScilabBinDirectoryToPATHEnvironnementVariable(DefaultSCIPATH);
216     }
217     else
218     {
219         /* Error */
220         exit(1);
221     }
222 
223 }
224 #endif
225 
226 /*--------------------------------------------------------------------------*/
AntislashToSlash(const char * pathwindows,char * pathunix)227 BOOL AntislashToSlash(const char *pathwindows, char *pathunix)
228 {
229     return convertSlash(pathwindows, pathunix, FALSE);
230 }
231 /*--------------------------------------------------------------------------*/
SlashToAntislash(const char * pathunix,char * pathwindows)232 BOOL SlashToAntislash(const char *pathunix, char *pathwindows)
233 {
234     return convertSlash(pathunix, pathwindows, TRUE);
235 }
236 /*--------------------------------------------------------------------------*/
convertSlash(const char * path_in,char * path_out,BOOL slashToAntislash)237 BOOL convertSlash(const char *path_in, char *path_out, BOOL slashToAntislash)
238 {
239     BOOL ret = FALSE;
240     if ( (path_in) && (path_out) )
241     {
242         int i = 0;
243         strcpy(path_out, path_in);
244         for (i = 0; i < (int)strlen(path_out); i++)
245         {
246             if ( slashToAntislash )
247             {
248                 if (path_in[i] == UNIX_SEPATATOR)
249                 {
250                     path_out[i] = WINDOWS_SEPATATOR;
251                     ret = TRUE;
252                 }
253             }
254             else
255             {
256                 if (path_in[i] == WINDOWS_SEPATATOR)
257                 {
258                     path_out[i] = UNIX_SEPATATOR;
259                     ret =  TRUE;
260                 }
261             }
262         }
263     }
264 
265     return ret;
266 }
267