ba39f8e4b51467dd4902de99087e5021cff87a6f
[scilab.git] / scilab / modules / call_scilab / src / c / call_scilab.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2005 - INRIA - Allan CORNET
4 * Copyright (C) 2010 - DIGITEO - Allan CORNET
5 *
6 * This file must be used under the terms of the CeCILL.
7 * This source file is licensed as described in the file COPYING, which
8 * you should have received as part of this distribution.  The terms
9 * are also available at
10 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
11 *
12 */
13
14 #include <string.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include "BOOL.h"
18 #include "call_scilab.h"
19 #include "lasterror.h"          /* clearInternalLastError, getInternalLastErrorValue */
20 #include "sci_malloc.h"
21 #include "configvariable_interface.h"
22 #include "fromc.h"
23 #include "LaunchScilabSignal.h"
24 #include "isdir.h"
25 #include "sci_path.h"
26 #include "scilabDefaults.h"
27 #include "sci_tmpdir.h"
28 #include "scirun.h"
29 #include "storeCommand.h"
30 #include "FigureList.h"
31 #include "api_scilab.h"
32 #include "call_scilab_engine_state.h"
33 #include "os_string.h"
34 #include "charEncoding.h"
35 #include "InitScilab.h"
36 #include "scilabRead.h"
37 #include "scilabWrite.hxx"
38
39 #ifdef _MSC_VER
40 #include "SetScilabEnvironmentVariables.h"
41 #include "getScilabDirectory.h"
42 #include <Windows.h>
43 #endif
44
45 extern char *getCmdLine(void);
46
47 static void TermPrintf(const char *text)
48 {
49     //std::cout << text;
50     printf("%s", text);
51 }
52
53
54
55 /*--------------------------------------------------------------------------*/
56 static CALL_SCILAB_ENGINE_STATE csEngineState = CALL_SCILAB_ENGINE_STOP;
57 /*--------------------------------------------------------------------------*/
58 void DisableInteractiveMode(void)
59 {
60     setScilabMode(SCILAB_NWNI);
61 }
62
63 /*--------------------------------------------------------------------------*/
64 BOOL StartScilab(char *SCIpath, char *ScilabStartup, int Stacksize)
65 {
66     return Call_ScilabOpen(SCIpath, TRUE, ScilabStartup, Stacksize) == 0;
67 }
68
69 /*--------------------------------------------------------------------------*/
70 /**
71 * Start Scilab engine
72 * Function created in the context of javasci v2.
73 * This function is just like StartScilab but provides more error messages
74 * in case or error. For now, it is only used in javasci v2 but it might
75 * be public sooner or later.
76 * @return
77 * 0: success
78 * -1: already running
79 * -2: Could not find SCI
80 * -3: No existing directory
81 * 10001: Stacksize failed (not enought memory ?).
82 * Any other positive integer: A Scilab internal error
83 */
84
85 #define FORMAT_SCRIPT_STARTUP "_errorCall_ScilabOpen = exec(\"%s\", \"errcatch\", -1); exit(_errorCall_ScilabOpen);"
86
87 int Call_ScilabOpen(char *SCIpath, BOOL advancedMode, char *ScilabStartup, int Stacksize)
88 {
89     char *InitStringToScilab = NULL;
90     static int iflag = -1, ierr = 0;
91     ScilabEngineInfo* pSEI = NULL;
92
93     setScilabMode(SCILAB_API);
94     if (advancedMode == FALSE)
95     {
96         DisableInteractiveMode();
97     }
98
99     if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STARTED)
100     {
101         return -1;
102     }
103
104     SetFromCToON();
105
106     if (SCIpath == NULL)        /* No SCIpath provided... */
107     {
108 #ifndef _MSC_VER
109         /* Other doesn't */
110         fprintf(stderr, "StartScilab: Could not find SCI\n");
111         return -2;
112 #endif
113     }
114     else
115     {
116         if (!isdir(SCIpath))
117         {
118             /* Check if the directory actually exists */
119             fprintf(stderr, "StartScilab: Could not find the directory %s\n", SCIpath);
120             return -3;
121         }
122     }
123
124     pSEI = InitScilabEngineInfo();
125     if (ScilabStartup)
126     {
127         int lengthStringToScilab = (int)(strlen(FORMAT_SCRIPT_STARTUP) + strlen(ScilabStartup) + 1);
128         InitStringToScilab = (char *)MALLOC(lengthStringToScilab * sizeof(char));
129         sprintf(InitStringToScilab, FORMAT_SCRIPT_STARTUP, ScilabStartup);
130         pSEI->iNoStart = 1;
131     }
132
133     setScilabInputMethod(&getCmdLine);
134     setScilabOutputMethod(&TermPrintf);
135
136     /* Scilab Initialization */
137     pSEI->pstFile = InitStringToScilab;
138     pSEI->iNoJvm = 1;
139     pSEI->iConsoleMode = 1;
140     ierr = StartScilabEngine(pSEI);
141     FREE(pSEI);
142
143     if (InitStringToScilab)
144     {
145         FREE(InitStringToScilab);
146         InitStringToScilab = NULL;
147     }
148
149     if (ierr)
150     {
151         return ierr;
152     }
153
154     setCallScilabEngineState(CALL_SCILAB_ENGINE_STARTED);
155
156     return 0;
157 }
158
159 /*--------------------------------------------------------------------------*/
160 BOOL TerminateScilab(char *ScilabQuit)
161 {
162     if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STARTED)
163     {
164         ScilabEngineInfo* pSEI = InitScilabEngineInfo();
165         pSEI->pstFile = ScilabQuit;
166         StopScilabEngine(pSEI);
167
168         ReleaseLaunchScilabSignal();
169         setCallScilabEngineState(CALL_SCILAB_ENGINE_STOP);
170
171         /* restore default mode */
172         setScilabMode(SCILAB_API);
173
174         return TRUE;
175     }
176     return FALSE;
177 }
178
179 /*--------------------------------------------------------------------------*/
180 /**
181 * function called javasci
182 */
183 void ScilabDoOneEvent(void)
184 {
185     if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STARTED)
186     {
187         if (getScilabMode() != SCILAB_NWNI)
188         {
189 #if 0
190             C2F(scirun) ("quit;", (int)strlen("quit;"));
191 #endif
192         }
193     }
194 }
195
196 /*--------------------------------------------------------------------------*/
197 int ScilabHaveAGraph(void)
198 {
199     if (getCallScilabEngineState() == CALL_SCILAB_ENGINE_STARTED)
200     {
201         return sciHasFigures();
202     }
203     return 0;
204 }
205
206 /*--------------------------------------------------------------------------*/
207 CALL_SCILAB_ENGINE_STATE setCallScilabEngineState(CALL_SCILAB_ENGINE_STATE state)
208 {
209     csEngineState = state;
210     return csEngineState;
211 }
212
213 /*--------------------------------------------------------------------------*/
214 CALL_SCILAB_ENGINE_STATE getCallScilabEngineState(void)
215 {
216     return csEngineState;
217 }
218
219 /*--------------------------------------------------------------------------*/
220 sci_types getVariableType(char *varName)
221 {
222     int iSciType = -1;
223     SciErr sciErr = getNamedVarType(NULL, (char*)varName, &iSciType);
224
225     if (sciErr.iErr == API_ERROR_NAMED_UNDEFINED_VAR)
226     {
227         return (sci_types) - 2;
228     }
229
230     if (sciErr.iErr)
231     {
232         printError(&sciErr, 0);
233         return (sci_types) - 1;
234     }
235     return (sci_types) iSciType;
236 }
237
238 /*--------------------------------------------------------------------------*/
239
240 /**
241  * Call the Scilab function getLastErrorMessage
242  * Take the result (a matrix of string) and concatenate into a single string
243  * This is way easier to manage in swig.
244 */
245 char *getLastErrorMessageSingle(void)
246 {
247     return wide_string_to_UTF8(getLastErrorMessage());
248 }
249
250 /*--------------------------------------------------------------------------*/
251 int getLastErrorValue(void)
252 {
253     /* defined in lasterror.h */
254     // FIXME : Call system_env function
255     //return getInternalLastErrorValue();
256     return 0;
257 }
258 /*--------------------------------------------------------------------------*/