summaryrefslogtreecommitdiffstats
path: root/scilab/modules/windows_tools/src/cpp/scilab_windows/SetScilabEnvironmentVariables.cpp
blob: d0e65a1d6f5febdb76efefd6e279dac5892fb63c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) INRIA - Allan CORNET
* Copyright (C) DIGITEO - 2010 - Allan CORNET
*
* This file must be used under the terms of the CeCILL.
* This source file is licensed as described in the file COPYING, which
* you should have received as part of this distribution.  The terms
* are also available at
* http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
*
*/

/*--------------------------------------------------------------------------*/
#include <windows.h>
#include <stdio.h>
extern "C"
{
#include "SetScilabEnvironmentVariables.h"
#include "PATH_MAX.h"
#include "sci_path.h"
#include "sci_malloc.h"
#include "getScilabDirectory.h"
#include "scilabDefaults.h"
#include "ConvertSlash.h"
#include "charEncoding.h"
#include "getshortpathname.h"
#include "stristr.h"
#include "strsubst.h"
#include "os_string.h"
}
/*--------------------------------------------------------------------------*/
static BOOL IsTheGoodShell(void);
static BOOL Set_Shell(void);
static BOOL Set_SCI_PATH(char *DefaultPath);
static BOOL Set_HOME_PATH(char *DefaultPath);
static BOOL AddScilabBinDirectoryToPATHEnvironnementVariable(char *DefaultPath);
static BOOL Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB(void);
/*--------------------------------------------------------------------------*/
/**
* Set some environment variablesSCI, and some others
*/
void SciEnvForWindows(void)
{
    char *SCIPathName = getScilabDirectory(TRUE);

    /* Correction Bug 1579 */
    if (!IsTheGoodShell())
    {
        if ( (!Set_Shell()) || (!IsTheGoodShell()))
        {
            MessageBox(NULL,
                       "Please modify ""ComSpec"" environment variable.\ncmd.exe on W2K and more.",
                       "Warning", MB_ICONWARNING | MB_OK);
        }
    }

    SetScilabEnvironmentVariables(SCIPathName);
    if (SCIPathName)
    {
        FREE(SCIPathName);
        SCIPathName = NULL;
    }
}
/*--------------------------------------------------------------------------*/
/* set env variables (used when calling scilab from * other programs) */
void SetScilabEnvironmentVariables(char *DefaultSCIPATH)
{
    if (DefaultSCIPATH)
    {
        Set_SCI_PATH(DefaultSCIPATH);
        Set_HOME_PATH(DefaultSCIPATH);
        Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB();
        AddScilabBinDirectoryToPATHEnvironnementVariable(DefaultSCIPATH);
    }
    else
    {
        /* Error */
        exit(1);
    }

}
/*--------------------------------------------------------------------------*/
BOOL Set_SCI_PATH(char *DefaultPath)
{
    BOOL bOK = FALSE;
    char *ShortPath = NULL;

    /* to be sure that it's unix 8.3 format */
    /* c:/progra~1/scilab-5.0 */
    ShortPath = getshortpathname(DefaultPath, &bOK);
    if (ShortPath)
    {
        char env[PATH_MAX + 1 + 10];
        AntislashToSlash(ShortPath, ShortPath);

        sprintf (env, "SCI=%s", ShortPath);
        setSCI(ShortPath);

        if (ShortPath)
        {
            FREE(ShortPath);
            ShortPath = NULL;
        }

        if (_putenv (env))
        {
            bOK = FALSE;
        }
        else
        {
            bOK = TRUE;
        }
    }
    return bOK;
}
/*--------------------------------------------------------------------------*/
BOOL Set_HOME_PATH(char *DefaultPath)
{
    wchar_t *wHOME = _wgetenv(L"HOME");
    if (wHOME == NULL)
    {
        wchar_t *wUserProfile = _wgetenv(L"USERPROFILE");
        if (wUserProfile)
        {
            return SetEnvironmentVariableW(L"HOME", wUserProfile);
        }
        else
        {
            /* if USERPROFILE is not defined , we use default profile */
            wchar_t *wAllUsersProfile = _wgetenv(L"ALLUSERSPROFILE");
            if (wAllUsersProfile)
            {
                return SetEnvironmentVariableW(L"HOME", wUserProfile);
            }
            else
            {
                BOOL bRes = FALSE;
                wchar_t *wDefault = to_wide_string(DefaultPath);
                if (wDefault)
                {
                    bRes = SetEnvironmentVariableW(L"HOME", wDefault);
                    FREE(wDefault);
                    wDefault = NULL;
                }
                return bRes;
            }
        }
    }
    return TRUE;
}
/*--------------------------------------------------------------------------*/
BOOL Set_SOME_ENVIRONMENTS_VARIABLES_FOR_SCILAB(void)
{
    BOOL bOK = TRUE;

#ifdef _MSC_VER
    _putenv ("COMPILER=VC++");
#endif

    /* WIN32 variable Environment */
#ifdef _WIN32
    _putenv ("WIN32=OK");
#endif

    /* WIN64 variable Environment */
#ifdef _WIN64
    _putenv ("WIN64=OK");
#endif

    if ( GetSystemMetrics(SM_REMOTESESSION) )
    {
        _putenv ("SCILAB_MSTS_SESSION=OK");
    }

    return bOK;
}
/*--------------------------------------------------------------------------*/
BOOL IsTheGoodShell(void)
{
    char shellCmd[PATH_MAX];
    char drive[_MAX_DRIVE];
    char dir[_MAX_DIR];
    char fname[_MAX_FNAME];
    char ext[_MAX_EXT];

    strcpy(shellCmd, "");
    strcpy(fname, "");
    GetEnvironmentVariable("ComSpec", shellCmd, PATH_MAX);
    os_splitpath(shellCmd, drive, dir, fname, ext);

    if (_stricmp(fname, "cmd") == 0)
    {
        return TRUE;
    }

    return FALSE;
}
/*--------------------------------------------------------------------------*/
BOOL Set_Shell(void)
{
    BOOL bOK = FALSE;
    char env[_MAX_DRIVE + _MAX_DIR + _MAX_FNAME + _MAX_EXT + 10];
    char *WINDIRPATH = NULL;

    WINDIRPATH = getenv ("SystemRoot");
    sprintf(env, "ComSpec=%s\\system32\\cmd.exe", WINDIRPATH);

    if (_putenv (env))
    {
        bOK = FALSE;
    }
    else
    {
        bOK = TRUE;
    }

    if (WINDIRPATH)
    {
        FREE(WINDIRPATH);
        WINDIRPATH = NULL;
    }
    return bOK;
}
/*--------------------------------------------------------------------------*/
static BOOL AddScilabBinDirectoryToPATHEnvironnementVariable(char *DefaultPath)
{
#define SCILAB_BIN_PATH "%s/bin"
#define NEW_PATH "PATH=%s;%s"

    BOOL bOK = FALSE;
    char *PATH = NULL;
    char *env = NULL;
    char scilabBinPath[MAX_PATH];
    char *scilabBinPathConverted;

    PATH = getenv("PATH");

    env = (char*) MALLOC(sizeof(char) * (strlen(NEW_PATH) + strlen(PATH) +
                                         strlen(DefaultPath) + 1));
    if (env)
    {
        sprintf(scilabBinPath, SCILAB_BIN_PATH, DefaultPath);

        scilabBinPathConverted = (char*) MALLOC(MAX_PATH * sizeof(char));
#ifdef _MSC_VER
        scilabBinPathConverted = strsub(scilabBinPath, "/", "\\");
#else
        scilabBinPathConverted = strdup(scilabBinPath);
#endif
        if (stristr(PATH, scilabBinPathConverted) == 0)
        {
            sprintf(env, NEW_PATH, scilabBinPathConverted, PATH);
            if (_putenv (env))
            {
                bOK = FALSE;
            }
            else
            {
                bOK = TRUE;
            }
            FREE(env);
            env = NULL;
        }

        FREE(scilabBinPathConverted);
    }
    return bOK;
}
/*--------------------------------------------------------------------------*/