exec fixed about errcatch and mode argument.
[scilab.git] / scilab / modules / functions / sci_gateway / c / sci_exec.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  * Copyright (C) 2009 - 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 "gw_functions.h"
15 #include "api_scilab.h"
16 #include "machine.h"
17 #include "recursionFunction.h"
18 #include "localization.h"
19 #include "Scierror.h"
20 #include "MALLOC.h"
21 #include "BOOL.h"
22 /*--------------------------------------------------------------------------*/
23 extern int C2F(intexec)(); /* fortran */
24 /*--------------------------------------------------------------------------*/
25 #define ERRCATCH_KEYWORD "errcatch"
26 /*--------------------------------------------------------------------------*/
27 int C2F(sci_exec)(char *fname, unsigned long fname_len)
28 {
29     if ( isRecursionCallToFunction() )
30     {
31         C2F(intexec)(fname, fname_len);
32     }
33     else
34     {
35         SciErr sciErr;
36
37         int iType1                                              = 0;
38         int iType2                                              = 0;
39         int iType3                                              = 0;
40
41         int *piAddressVarOne = NULL;
42         int *piAddressVarTwo = NULL;
43         int *piAddressVarThree = NULL;
44
45         CheckRhs(1, 3);
46         CheckLhs(0, 1);
47
48         sciErr = getVarAddressFromPosition(pvApiCtx, 1, &piAddressVarOne);
49         if (sciErr.iErr)
50         {
51             printError(&sciErr, 0);
52             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
53             return 0;
54         }
55
56         sciErr = getVarType(pvApiCtx, piAddressVarOne, &iType1);
57         if (sciErr.iErr)
58         {
59             printError(&sciErr, 0);
60             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
61             return 0;
62         }
63
64         if (Rhs > 1)
65         {
66             sciErr = getVarAddressFromPosition(pvApiCtx, 2, &piAddressVarTwo);
67             if (sciErr.iErr)
68             {
69                 printError(&sciErr, 0);
70                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
71                 return 0;
72             }
73
74             sciErr = getVarType(pvApiCtx, piAddressVarTwo, &iType2);
75             if (sciErr.iErr)
76             {
77                 printError(&sciErr, 0);
78                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
79                 return 0;
80             }
81         }
82
83         if (Rhs > 2)
84         {
85             sciErr = getVarAddressFromPosition(pvApiCtx, 3, &piAddressVarThree);
86             if (sciErr.iErr)
87             {
88                 printError(&sciErr, 0);
89                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
90                 return 0;
91             }
92
93             sciErr = getVarType(pvApiCtx, piAddressVarThree, &iType3);
94             if (sciErr.iErr)
95             {
96                 printError(&sciErr, 0);
97                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
98                 return 0;
99             }
100         }
101
102         if ((iType1 == sci_matrix) || (iType1 == sci_strings) || (iType1 == sci_c_function) |  (iType1 == sci_u_function))
103         {
104             int m1 = 0, n1 = 0;
105
106             /*  sci_c_function, sci_u_function not managed by getVarDimension */
107             if ( (iType1 != sci_c_function) && (iType1 != sci_u_function) )
108             {
109                 sciErr = getVarDimension(pvApiCtx, piAddressVarOne, &m1, &n1);
110                 if (sciErr.iErr)
111                 {
112                     printError(&sciErr, 0);
113                     Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 1);
114                     return 0;
115                 }
116
117                 if (m1 * n1 != 1)
118                 {
119                     Scierror(999, _("%s: Wrong size for input argument #%d.\n"), fname, 1);
120                     return 0;
121                 }
122             }
123
124             if (Rhs > 1)
125             {
126                 if ((iType2 != sci_matrix) && (iType2 != sci_strings))
127                 {
128                     Scierror(999, _("%s: Wrong type for input argument #%d: A scalar or a string expected.\n"), fname, 2);
129                     return 0;
130                 }
131                 else
132                 {
133                     if (iType2 == sci_matrix)
134                     {
135                         int m2 = 0, n2 = 0;
136                         double *pdVarTwo = NULL;
137                         int iVarTwo = 0;
138
139                         sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarTwo, &m2, &n2, &pdVarTwo);
140                         if (sciErr.iErr)
141                         {
142                             printError(&sciErr, 0);
143                             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
144                             return 0;
145                         }
146
147                         if (m2 * n2 != 1)
148                         {
149                             Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 2);
150                             return 0;
151                         }
152
153                         iVarTwo = (int)pdVarTwo[0];
154                         if (*pdVarTwo != (double)iVarTwo)
155                         {
156                             Scierror(999, _("%s: Wrong value for input argument #%d: An integer expected.\n"), fname, 2);
157                             return 0;
158                         }
159                     }
160                     else /* sci_strings */
161                     {
162                         int m2 = 0, n2 = 0;
163                         char *pStVarTwo = NULL;
164                         int lenStVarTwo = 0;
165
166                         sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, NULL);
167                         if (sciErr.iErr)
168                         {
169                             printError(&sciErr, 0);
170                             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
171                             return 0;
172                         }
173
174                         pStVarTwo = (char*)MALLOC(sizeof(char) * (lenStVarTwo + 1));
175                         if (pStVarTwo)
176                         {
177                             sciErr = getMatrixOfString(pvApiCtx, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
178                             if (sciErr.iErr)
179                             {
180                                 printError(&sciErr, 0);
181                                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 2);
182                                 return 0;
183                             }
184
185                             if (strcmp(pStVarTwo, ERRCATCH_KEYWORD))
186                             {
187                                 Scierror(999, _("%s: Wrong value for input argument #%d: 'errcatch' expected.\n"), fname, 2);
188                                 FREE(pStVarTwo);
189                                 pStVarTwo = NULL;
190                                 return 0;
191                             }
192
193                             FREE(pStVarTwo);
194                             pStVarTwo = NULL;
195                         }
196                         else
197                         {
198                             Scierror(999, _("%s: Memory allocation error.\n"), fname);
199                             return 0;
200                         }
201                     }
202                 }
203             }
204
205             if (Rhs > 2)
206             {
207                 BOOL bCombo = FALSE;
208                 if ((iType3 != sci_matrix) && (iType3 != sci_strings))
209                 {
210                     Scierror(999, _("%s: Wrong type for input argument #%d: A scalar or a string expected.\n"), fname, 3);
211                     return 0;
212                 }
213                 else
214                 {
215                     if (iType3 == sci_matrix)
216                     {
217                         int m3 = 0, n3 = 0;
218                         double *pdVarThree = NULL;
219                         int iVarThree = 0;
220
221                         sciErr = getMatrixOfDouble(pvApiCtx, piAddressVarThree, &m3, &n3, &pdVarThree);
222                         if (sciErr.iErr)
223                         {
224                             printError(&sciErr, 0);
225                             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
226                             return 0;
227                         }
228
229                         if (m3 * n3 != 1)
230                         {
231                             Scierror(999, _("%s: Wrong size for input argument #%d: A scalar expected.\n"), fname, 3);
232                             return 0;
233                         }
234
235                         iVarThree = (int)pdVarThree[0];
236                         if (*pdVarThree != (double)iVarThree)
237                         {
238                             Scierror(999, _("%s: Wrong value for input argument #%d: An integer expected.\n"), fname, 3);
239                             return 0;
240                         }
241                     }
242                     else  /* sci_strings */
243                     {
244                         int m3 = 0, n3 = 0;
245                         char *pStVarThree = NULL;
246                         int lenStVarThree = 0;
247
248                         sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, NULL);
249                         if (sciErr.iErr)
250                         {
251                             printError(&sciErr, 0);
252                             Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
253                             return 0;
254                         }
255
256                         pStVarThree = (char*)MALLOC(sizeof(char) * (lenStVarThree + 1));
257                         if (pStVarThree)
258                         {
259                             sciErr = getMatrixOfString(pvApiCtx, piAddressVarThree, &m3, &n3, &lenStVarThree, &pStVarThree);
260                             if (sciErr.iErr)
261                             {
262                                 printError(&sciErr, 0);
263                                 Scierror(999, _("%s: Can not read input argument #%d.\n"), fname, 3);
264                                 return 0;
265                             }
266
267                             if (strcmp(pStVarThree, ERRCATCH_KEYWORD))
268                             {
269                                 Scierror(999, _("%s: Wrong value for input argument #%d: 'errcatch' expected.\n"), fname, 3);
270                                 FREE(pStVarThree);
271                                 pStVarThree = NULL;
272                                 return 0;
273                             }
274
275                             FREE(pStVarThree);
276                             pStVarThree = NULL;
277                         }
278                         else
279                         {
280                             Scierror(999, _("%s : Memory allocation error.\n"), fname);
281                             return 0;
282                         }
283                     }
284                 }
285
286                 if ((iType2 == sci_strings) && (iType3 == sci_matrix))
287                 {
288                     bCombo = TRUE;
289                 }
290
291                 if (!bCombo)
292                 {
293                     Scierror(999, _("%s: Wrong value(s) for input argument(s).\n"), fname, 3);
294                     return 0;
295                 }
296             }
297
298             /* input parameters seem good */
299             C2F(intexec)(fname, fname_len);
300         }
301         else
302         {
303             Scierror(999, _("%s: Wrong type for input argument #%d: A scalar or a string expected.\n"), fname, 1);
304         }
305     }
306     return 0;
307 }
308 /*--------------------------------------------------------------------------*/