9efebb97179783686949973dbf1c3631144a126f
[scilab.git] / scilab / modules / tclsci / sci_gateway / c / sci_TCL_EvalStr.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2005 */
3 /* Allan CORNET */
4 /*--------------------------------------------------------------------------*/
5 #include "TCL_Global.h"
6 #include "gw_tclsci.h"
7 #include "Scierror.h"
8 #include "localization.h"
9 /*--------------------------------------------------------------------------*/
10 int C2F(sci_TCL_EvalStr) _PARAMS((char *fname,unsigned long l))
11
12         CheckRhs(1,2);
13         CheckLhs(1,1);
14
15         if (GetType(1) == sci_strings)
16         {
17                 char **Str=NULL;
18                 int m1,n1,i,j,RET;
19                 int m2,n2,l2;
20         Tcl_Interp *TCLinterpreter=NULL;
21         char **ReturnArrayString=NULL;
22         int k=0;
23
24                 GetRhsVar(1,MATRIX_OF_STRING_DATATYPE,&m1,&n1,&Str);
25
26                 if (TCLinterp == NULL)
27                 {
28                         Scierror(999,_("%s : Error main TCL interpreter not initialized.\n"),fname);
29                         return 0;
30                 }
31
32                 if (Rhs==2)
33                 {
34                         /* two arguments given - get a pointer on the slave interpreter */
35                         if (GetType(2) == sci_strings)
36                         {
37                                 GetRhsVar(2,STRING_DATATYPE,&m2,&n2,&l2);
38                                 TCLinterpreter=Tcl_GetSlave(TCLinterp,cstk(l2));
39                                 if (TCLinterpreter==NULL)
40                                 {
41                                         Scierror(999,_("%s: No such slave interpreter.\n"),fname);
42                                         return 0;
43                                 }
44                         }
45                         else
46                         {
47                                 Scierror(999,_("%s : Argument type must be character string.\n"),fname);
48                                  return 0;
49                         }
50                 }
51                 else
52                 {
53                         /* only one argument given - use the main interpreter */
54                         TCLinterpreter=TCLinterp;
55                 }
56
57         ReturnArrayString = (char **) MALLOC(m1*n1*sizeof(char **));
58
59                 for (i = 0; i<m1*n1 ;i++)
60                 {
61                         char *RetStr=NULL;
62                         char *AsciiFromUTF8=NULL;
63
64                         RET=Tcl_Eval(TCLinterpreter,Str[i]);
65
66                         if (RET==TCL_ERROR)
67                         {
68                 const char *trace = Tcl_GetVar(TCLinterpreter, "errorInfo", TCL_GLOBAL_ONLY);
69                                 if(Err>0)
70                                 {
71                                         Scierror(999,"%s, ScilabEval error at line %i\n %s.\n",fname,i+1,(char *)trace);
72                                 }
73                                 else
74                                 {
75                                         Scierror(999,"%s, %s at line %i\n       %s\n",fname,TCLinterpreter->result,i+1,(char *)trace);
76                                 }
77                                 if (Str) for (i = 0; i<m1*n1 ;i++)
78                                 {
79                                         if (Str[i]) 
80                                         {
81                                                 FREE(Str[i]);
82                                                 Str[i]=NULL;
83                                         }
84                                 }
85                                 if (Str) {FREE(Str); Str=NULL;}
86                                 return 0;
87             } else {
88                 /* return result of the successful evaluation of the script */
89                 /* return a matrix of string results */
90                 RetStr = (char*)Tcl_GetStringResult(TCLinterpreter);
91                 AsciiFromUTF8=UTF8toANSI(TCLinterpreter,RetStr);
92                 ReturnArrayString[k++]=AsciiFromUTF8;
93             }
94                 }
95                 CreateVarFromPtr(Rhs+1,MATRIX_OF_STRING_DATATYPE, &m1, &n1, ReturnArrayString);
96                 LhsVar(1)=Rhs+1;
97                 C2F(putlhsvar)();
98
99                 for (i=0;i<n1;i++) for (j=0;j<m1;j++) { FREE(ReturnArrayString[i+n1*j]);ReturnArrayString[i+n1*j]=NULL; }
100                 FREE(ReturnArrayString);
101         }
102         else
103         {
104                 Scierror(999,_("Argument type incorrect: Must be a string or vector of strings.\n"));
105                 return 0;
106         }
107         
108         return 0;
109 }
110 /*--------------------------------------------------------------------------*/