Added some missing headers reported by /usr/lib/build/checks-data/check_gcc_output
[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  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 #include <string.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "call_scilab.h"
16 #include "MALLOC.h"
17 #include "scilabmode.h"
18 #include "fromc.h"
19 #include "LaunchScilabSignal.h"
20 #include "localization.h"
21 #include "isdir.h"
22 #include "setgetSCIpath.h"
23 #include "scilabDefaults.h"
24 #include "tmpdir.h"
25 #include "inisci-c.h"
26 #include "scirun.h"
27 #include "scilabmode.h"
28 #include "sciquit.h"
29 #include "storeCommand.h"
30 #include "WindowList.h"
31 #include "../../core/src/c/TerminateCore.h"
32
33 #ifdef _MSC_VER
34 #include "SetScilabEnvironmentVariables.h"
35 #include "getScilabDirectory.h"
36 #include "strdup_windows.h"
37 #endif
38
39 #ifdef _MSC_VER
40 #define putenv _putenv
41 #endif
42 /*--------------------------------------------------------------------------*/
43 static BOOL StartScilabIsOK = FALSE;
44 /*--------------------------------------------------------------------------*/
45 #ifdef _MSC_VER
46 static void SetSciEnv(void)
47 {
48   char *ScilabDirectory=NULL;
49
50   ScilabDirectory = getScilabDirectory(TRUE);
51
52   if (ScilabDirectory == NULL)
53   {
54         MessageBox (NULL, "getScilabDirectory()", "Error" , MB_ICONSTOP | MB_OK);
55         exit(1);
56   }
57   SetScilabEnvironmentVariables(ScilabDirectory);
58
59   if (ScilabDirectory){FREE(ScilabDirectory);ScilabDirectory=NULL;}
60
61 }
62 #endif
63 /*--------------------------------------------------------------------------*/
64 void DisableInteractiveMode(void)
65 {
66         setScilabMode(SCILAB_NWNI);
67 }
68 /*--------------------------------------------------------------------------*/
69 BOOL StartScilab(char *SCIpath,char *ScilabStartup,int *Stacksize)
70 {
71         #define FORMAT_SCRIPT_STARTUP "exec(\"%s\",-1);quit;"
72         char *ScilabStartupUsed = NULL;
73         char *InitStringToScilab = NULL;
74         int StacksizeUsed = 0;
75         int lengthStringToScilab = 0;
76
77         static int iflag = -1, ierr = 0;
78
79         if (StartScilabIsOK) return FALSE;
80
81         SetFromCToON();
82
83         InitializeLaunchScilabSignal();
84
85         if (SCIpath == NULL) /* No SCIpath provided... */
86         {
87                 #ifdef _MSC_VER
88                 SetSciEnv(); /* Windows has a way to detect it */
89                 #else 
90                 /* Other doesn't */
91                 fprintf(stderr,"StartScilab: Could not find SCI\n");
92                 return FALSE;
93                 #endif
94         }
95         else
96         {
97                 if (!isdir(SCIpath)){
98                         /* Check if the directory actually exists */
99                         fprintf(stderr,"StartScilab: Could not find the directory %s\n",SCIpath);
100                         return FALSE;
101                 }else{
102                         char env[2048];
103                         setSCIpath(SCIpath);
104                         sprintf(env,"SCI=%s",SCIpath);
105                         putenv(env);
106                 }
107         }
108
109         if (ScilabStartup == NULL)
110         {
111                 ScilabStartupUsed = strdup(DEFAULTSCILABSTARTUP);
112         }
113         else
114         {
115                 ScilabStartupUsed = strdup(ScilabStartup);
116         }
117
118         if (Stacksize==NULL)
119         {
120                 StacksizeUsed = DEFAULTSTACKSIZE;
121         }
122         else
123         {
124                 StacksizeUsed = *Stacksize;
125         }
126
127         /* creates TMPDIR */
128         C2F(settmpdir)();
129
130         /* Scilab Initialization */
131         C2F(inisci)(&iflag,&StacksizeUsed,&ierr);
132         if ( ierr > 0 ) return FALSE;
133
134         lengthStringToScilab = (int)(strlen(FORMAT_SCRIPT_STARTUP) + strlen(ScilabStartupUsed + 1));
135         InitStringToScilab = (char*)MALLOC(lengthStringToScilab*sizeof(char));
136         sprintf(InitStringToScilab,FORMAT_SCRIPT_STARTUP,ScilabStartupUsed);
137
138         C2F(scirun)(InitStringToScilab,(long int)strlen(InitStringToScilab));
139
140         if (ScilabStartupUsed) {FREE(ScilabStartupUsed);ScilabStartupUsed=NULL;}
141         if (InitStringToScilab) {FREE(InitStringToScilab);InitStringToScilab=NULL;}
142
143         StartScilabIsOK = TRUE;
144
145         return TRUE;
146 }
147 /*--------------------------------------------------------------------------*/
148 BOOL TerminateScilab(char *ScilabQuit)
149 {
150         if (StartScilabIsOK)
151         {
152                 if (getScilabMode() != SCILAB_NWNI)
153                 {
154                         ExitScilab();
155                 }
156                 else
157                 {
158                         TerminateCorePart2();
159                 }
160                 StartScilabIsOK = FALSE;
161                 return TRUE;
162         }
163
164         return FALSE;
165 }
166 /*--------------------------------------------------------------------------*/
167 /**
168 * function called javasci
169 */
170 void ScilabDoOneEvent(void)
171 {
172         if ( getScilabMode() != SCILAB_NWNI )
173         {
174                 C2F(scirun)("quit;",(int)strlen("quit;"));
175         }
176 }
177 /*--------------------------------------------------------------------------*/
178 int ScilabHaveAGraph(void)
179 {
180   return sciHasFigures();
181 }
182 /*--------------------------------------------------------------------------*/