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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
|
/*
* Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
* Copyright (C) INRIA/ENPC
* Copyright (C) 2008 - INRIA - 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 <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef _MSC_VER
#include <windows.h>
#endif
#include "core_math.h"
#include "dynamic_link.h"
#include "men_Sutils.h"
#include "addinter.h"
#include "do_error_number.h"
#include "stack-c.h"
#include "MALLOC.h" /* MALLOC */
#include "sciprint.h"
#include "Funtab.h"
#include "warningmode.h"
#include "localization.h"
#include "Scierror.h"
#ifdef _MSC_VER
#include "ExceptionMessage.h"
#endif
/* size of interface name */
/* scilab limitation to nlgh characters (24)*/
#define INTERFSIZE nlgh + 1
/*-----------------------------------------------------------------------------------*/
typedef struct
{
char name[INTERFSIZE]; /** name of interface **/
void (*func)(); /** entrypoint for the interface **/
int Nshared; /** id of the shared library **/
BOOL ok; /** flag set to TRUE if entrypoint can be used **/
} InterfaceElement;
/*-----------------------------------------------------------------------------------*/
InterfaceElement *DynInterf = NULL;
static int MaxInterfaces = MAXDYNINTERF;
/*-----------------------------------------------------------------------------------*/
static int LastInterf = 0;
static void initializeInterfaces(void);
static BOOL reallocDynInterf(void);
/*-----------------------------------------------------------------------------------*/
int AddInterfaceToScilab(char *filenamelib, char *spname, char **fcts, int sizefcts)
{
int IdLib = -1; /* Id of library */
int idinput = -1; /* Id of a function */
int ierr1 = 0;
int one = 1;
char **subname = NULL;
int ierr = 0;
int i = 0;
int inum = 0;
int k1 = 0;
initializeLink();
initializeInterfaces();
/** Try to unlink the interface if it was previously linked **/
for ( i = 0 ; i < LastInterf ; i++)
{
if (strcmp(spname, DynInterf[i].name) == 0)
{
unlinksharedlib(&DynInterf[i].Nshared);
break;
}
}
/** Try to find a free position in the interface table : inum **/
inum = -1;
for ( i = 0 ; i < LastInterf ; i++)
{
if ( DynInterf[i].ok == 0 )
{
inum = i;
}
}
inum = ( inum == -1 ) ? LastInterf : inum ;
/** Linking Files and add entry point name iname */
if ( inum >= MaxInterfaces )
{
/* Try to resize DynInterf */
if ( ( !reallocDynInterf() ) || ( inum >= MaxInterfaces ) )
{
return -1;
}
}
subname = (char **)MALLOC(sizeof (char*));
subname[0] = spname;
/* link then search */
/* Trying with the fortran symbol */
IdLib = scilabLink(idinput, filenamelib, subname, one, TRUE, &ierr1);
if (ierr1 != 0)
{
/* Haven't been able to find the symbol. Try C symbol */
IdLib = scilabLink(idinput, filenamelib, subname, one, FALSE, &ierr1);
}
subname[0] = NULL;
if (subname)
{
FREE(subname);
subname = NULL;
}
if ( IdLib < 0 )
{
return IdLib;
}
/** store the linked function in the interface function table DynInterf **/
DynInterf[inum].Nshared = IdLib;
if ( SearchInDynLinks(spname, &DynInterf[inum].func) < 0 )
{
/* Maximum number of dynamic interfaces */
return -6;
}
else
{
strncpy(DynInterf[inum].name, spname, INTERFSIZE);
DynInterf[inum].ok = TRUE;
}
if ( inum == LastInterf )
{
LastInterf++;
}
k1 = inum + 1;
for (i = 0; i < sizefcts; i++)
{
int id[nsiz], zero = 0, three = 3, fptr = 0, fptr1 = 0, four = 4;
/* find a previous functions with same name */
C2F(cvname)(id, fcts[i], &zero, (unsigned long)strlen(fcts[i]));
fptr1 = fptr = (DynInterfStart + k1) * 1000 + (i + 1);
/* clear previous def set fptr1 to 0*/
C2F(funtab)(id, &fptr1, &four, "NULL_NAME", 0);
/* reinstall */
C2F(funtab)(id, &fptr, &three, fcts[i], (unsigned long)strlen(fcts[i]));
}
return ierr;
}
/*-----------------------------------------------------------------------------------*/
static void initializeInterfaces(void)
{
static int first_entry_interfaces = 0;
if ( first_entry_interfaces == 0)
{
if (DynInterf == NULL)
{
DynInterf = (InterfaceElement*)MALLOC(sizeof(InterfaceElement) * MaxInterfaces);
if (DynInterf)
{
int i = 0;
for ( i = 0 ; i < MaxInterfaces ; i++)
{
strcpy(DynInterf[i].name, "");
DynInterf[i].func = NULL;
DynInterf[i].Nshared = -1;
DynInterf[i].ok = FALSE;
}
}
}
first_entry_interfaces++;
}
}
/*-----------------------------------------------------------------------------------*/
/*********************************
* used in unlinksharedlib(i)
*********************************/
void RemoveInterf(int id)
{
int i = 0;
for ( i = 0 ; i < LastInterf ; i++ )
{
if ( DynInterf[i].Nshared == id )
{
DynInterf[i].ok = FALSE;
break;
}
}
}
/*-----------------------------------------------------------------------------------*/
/************************************************
* Used when one want to call a function added
* with addinterf the dynamic interface number
* is given by *k - (DynInterfStart+1)
************************************************/
void C2F(userlk)(int *k)
{
int k1 = *k - (DynInterfStart + 1) ;
int imes = 9999;
if ( k1 >= LastInterf || k1 < 0 )
{
if (getWarningMode())
{
Scierror(999, _("Error: Not a valid internal routine number %d.\n"), *k);
}
SciError(imes);
return;
}
if ( DynInterf[k1].ok == 1 )
{
#ifdef _MSC_VER
#ifndef _DEBUG
_try
{
(*DynInterf[k1].func)();
}
_except (EXCEPTION_EXECUTE_HANDLER)
{
ExceptionMessage(GetExceptionCode(), DynInterf[k1].name);
}
#else
(*DynInterf[k1].func)();
#endif
#else
(*DynInterf[k1].func)();
#endif
}
else
{
if (getWarningMode())
{
sciprint(_("Interface %s not linked.\n"), DynInterf[k1].name);
}
SciError(imes);
return;
}
}
/*-----------------------------------------------------------------------------------*/
static BOOL reallocDynInterf(void)
{
/* increase table of interfaces by 2 */
int newMaxInterfaces = MaxInterfaces * 2;
if (newMaxInterfaces < ENTRYMAX)
{
if (DynInterf)
{
int i = 0;
InterfaceElement *newDynInterf = NULL;
newDynInterf = (InterfaceElement*)REALLOC(DynInterf, sizeof(InterfaceElement) * newMaxInterfaces);
if (newDynInterf == NULL)
{
return FALSE;
}
DynInterf = newDynInterf;
for ( i = MaxInterfaces ; i < newMaxInterfaces ; i++)
{
strcpy(DynInterf[i].name, "");
DynInterf[i].func = NULL;
DynInterf[i].Nshared = -1;
DynInterf[i].ok = FALSE;
}
MaxInterfaces = newMaxInterfaces;
return TRUE;
}
else
{
return FALSE;
}
}
return FALSE;
}
/*-----------------------------------------------------------------------------------*/
|