File: CallScilab.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (270 lines) | stat: -rw-r--r-- 6,551 bytes parent folder | download
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
/*-----------------------------------------------------------------------------------*/
/* INRIA 2005 */
/* Allan CORNET */
/*-----------------------------------------------------------------------------------*/
#include "CallScilab.h"
#ifdef WIN32
#include "../os_specific/win_mem_alloc.h" /* MALLOC */
#else
#include "../os_specific/sci_mem_alloc.h" /* MALLOC */
#endif

/*-----------------------------------------------------------------------------------*/
static char DefaultScilabStartup[]="SCI/scilab.star";
static char DefaultScilabQuit[]="SCI/scilab.quit";
static char DefaultSCIenv[]="../..";
static int DefaultStacksize=1000000;
static int StartScilabIsOK=FALSE;
/*-----------------------------------------------------------------------------------*/
extern int setenvc(char *string,char *value);
extern int C2F(inisci)(int *,int *,int *);
extern void C2F(settmpdir) (void);
extern int C2F(scirun)(char * startup, int lstartup);
extern void C2F (tmpdirc) (void);
extern int C2F(ismenu)();
extern void SetFromCToON(void);
extern int version_flag(void); 
extern void sciGetIdFigure (int *vect, int *id, int *flag);
extern int IsFromC(void);
extern void C2F(freegmem)(void);
extern void C2F(freemem)(void);
#ifdef WITH_TK
extern int CloseTCLsci(void);
#endif
/*-----------------------------------------------------------------------------------*/
#ifdef WIN32
extern char *GetScilabDirectory(BOOL UnixStyle);
extern void initTCLTK(void);
extern void start_sci_gtk();
extern void InitWindowGraphDll(void);
extern void TextMessage1 (int ctrlflag);
#else
extern int C2F(sxevents)();
#endif
/*-----------------------------------------------------------------------------------*/
#ifdef WIN32
static void SetSciEnv(void)
{
  extern void set_sci_env(char *DefaultSCIPATH);
  
  char *ScilabDirectory=NULL;

  ScilabDirectory=GetScilabDirectory(TRUE);

  if (ScilabDirectory == NULL)
  {
	MessageBox (NULL, "Error", "GetScilabDirectory()", MB_ICONSTOP | MB_OK);
	exit(1);
  }
  set_sci_env(ScilabDirectory);

  if (ScilabDirectory){FREE(ScilabDirectory);ScilabDirectory=NULL;}		
  
}
#endif
/*-----------------------------------------------------------------------------------*/
int StartScilab(char *SCIpath,char *ScilabStartup,int *Stacksize)
{
	int bOK=FALSE;

	char *SCIpathUsed=NULL;
	char *ScilabStartupUsed=NULL;
	char *InitStringToScilab=NULL;
	int StacksizeUsed=0;
	int lengthStringToScilab=0;

	static int iflag=-1,ierr=0;

	if (StartScilabIsOK) return bOK;

	if (SCIpath==NULL)
	{
		#ifdef WIN32
			SetSciEnv();
		#else
		{
			char env[2048];
			sprintf(env,"SCI=%s",SCIpath);
			putenv(env);
		}
		#endif
	}
	else
	{
		char env[2048];
		sprintf(env,"SCI=%s",SCIpath);
		putenv(env);
	}

	if (ScilabStartup==NULL)
	{
		ScilabStartupUsed=(char*)MALLOC((strlen(DefaultScilabStartup)+1)*sizeof(char));
		sprintf(ScilabStartupUsed,"%s",DefaultScilabStartup);
	}
	else
	{
		ScilabStartupUsed=(char*)MALLOC((strlen(DefaultScilabStartup)+1)*sizeof(char));
		sprintf(ScilabStartupUsed,"%s",ScilabStartup);
	}
	
	if (Stacksize==NULL)
	{
		StacksizeUsed=DefaultStacksize;
	}
	else
	{
		StacksizeUsed=*Stacksize;
	}

	SetFromCToON();

	/* Scilab Initialization */ 
	#ifdef WITH_TK
	 initTCLTK(); /* TCLTK Init. */
  #endif
	C2F(inisci)(&iflag,&StacksizeUsed,&ierr);

	if ( ierr > 0 ) 
    {
	  bOK=FALSE;
      return bOK;
    }

	/* running the startup */ 
	C2F(settmpdir)();
#if WIN32
	InitWindowGraphDll();
	start_sci_gtk() ;
#endif

	lengthStringToScilab=(int)(strlen("exec(\"SCI/scilab.star\",-1);quit;")+strlen(ScilabStartupUsed));
	InitStringToScilab=(char*)MALLOC(lengthStringToScilab*sizeof(char));
	sprintf(InitStringToScilab,"exec(\"%s\",-1);quit;",ScilabStartupUsed);

	
	C2F(scirun)(InitStringToScilab,strlen(InitStringToScilab));

	if (ScilabStartupUsed) {FREE(ScilabStartupUsed);ScilabStartupUsed=NULL;}
	if (InitStringToScilab) {FREE(InitStringToScilab);InitStringToScilab=NULL;}

	bOK=TRUE;
	StartScilabIsOK=TRUE;

	return bOK;
}
/*-----------------------------------------------------------------------------------*/
int TerminateScilab(char *ScilabQuit)
{
	int bOK=FALSE;

	if (StartScilabIsOK)
	{
		char *ScilabQuitUsed=NULL;
		char *QuitStringToScilab=NULL;
		int lengthStringToScilab=0;

		if (ScilabQuit==NULL)
		{
			ScilabQuitUsed=(char*)MALLOC((strlen(DefaultScilabQuit)+1)*sizeof(char));
			sprintf(ScilabQuitUsed,"%s",DefaultScilabQuit);
		}
		else
		{
			ScilabQuitUsed=(char*)MALLOC((strlen(ScilabQuit)+1)*sizeof(char));
			sprintf(ScilabQuitUsed,"%s",ScilabQuit);
		}

		lengthStringToScilab=(int)(strlen("exec('SCI/scilab.quit',-1);quit;")+strlen(ScilabQuitUsed));

		QuitStringToScilab=(char*)MALLOC(lengthStringToScilab*sizeof(char));
		sprintf(QuitStringToScilab,"exec(\"%s\",-1);quit;",ScilabQuitUsed);

		/* C2F(scirun)(QuitStringToScilab,strlen(QuitStringToScilab)); */

		if (QuitStringToScilab) {FREE(QuitStringToScilab);QuitStringToScilab=NULL;}
		if (ScilabQuitUsed) {FREE(ScilabQuitUsed);ScilabQuitUsed=NULL;}

		#if WIN32
			C2F(freegmem)();
			C2F(freemem)();
		#endif

		#ifdef WITH_TK
			CloseTCLsci();
		#endif

		StartScilabIsOK=FALSE;
		bOK=TRUE;
	}

	return bOK;
}
/*-----------------------------------------------------------------------------------*/
int SendScilabJob(char *job)
{
  int m,n,lp;
  double code=-1;

  static char buf[1024];
  char format[]="Err=execstr('%s','errcatch','n');quit;";
  
  if ((strlen(format)+strlen(job))>= 1024)
  {
	fprintf(stderr,"Error : SendScilabJob string 'job' too long \n");
	return (int) code;
  }

  sprintf(buf,format,job);

  C2F(scirun)(buf,strlen(buf));

  if ( ! C2F(cmatptr)("Err", &m, &n, &lp,strlen("Err")))
  {
	  fprintf(stderr,"Error : SendScilabJob (cmatptr) 'Err' \n");
  }
  else
  {
	  if (m*n == 1)
	  {
		ReadMatrix("Err", &m, &n, &code);
	  }
	  else fprintf(stderr,"Error : SendScilabJob (ReadMatrix Err)\n");
  }

  return (int) code;
}
/*-----------------------------------------------------------------------------------*/
void ScilabDoOneEvent(void)
{
#if WIN32
	TextMessage1 (1);
#else 
	C2F(sxevents)();
#endif

	while(C2F(ismenu)()==1 ) 
	{
		C2F(scirun)("quit;",(int)strlen("quit;"));
	}
}
/*-----------------------------------------------------------------------------------*/
int ScilabHaveAGraph(void)
{
	integer iflag =0,ids,num;
	int vInt=0;

	if (version_flag() == 0)
	{
		sciGetIdFigure (&ids,&num,&iflag);
		if (num > 0) vInt=1;

	}/* NG end*/
	else
	{
		C2F(getwins)(&num,&ids ,&iflag);
		if (num > 0) vInt=1;
	} 

	return vInt;
}
/*-----------------------------------------------------------------------------------*/