Update of localization ...
[scilab.git] / scilab / modules / tclsci / src / c / ScilabEval.c
1 /*--------------------------------------------------------------------------*/
2 /* INRIA 2005 */
3 /* Allan CORNET */
4 /*--------------------------------------------------------------------------*/ 
5 #include "TCL_Global.h"
6 #include "ScilabEval.h"
7 #include "sciprint.h"
8 #include "sciprint_full.h"
9 #include "Scierror.h"
10 #include "localization.h"
11 #include "tksynchro.h"
12 #include "../../localization/includes/localization.h"
13 #include "syncexec.h"
14 #include "dynamic_menus.h"
15 /*--------------------------------------------------------------------------*/
16 /* what's the max number of commands in the queue ??*/
17 #define arbitrary_max_queued_callbacks 20
18 #define AddCharacters 4
19 /*--------------------------------------------------------------------------*/
20 static int c_n1 = -1;     
21 /*--------------------------------------------------------------------------*/
22 int TCL_EvalScilabCmd(ClientData clientData,Tcl_Interp * theinterp,int objc,CONST char ** argv)
23 {
24   int ns,ierr,seq;
25   char *command;
26   
27   char *comm[arbitrary_max_queued_callbacks];
28   int   seqf[arbitrary_max_queued_callbacks];
29   int nc,ncomm=-1;
30
31   if (C2F(iop).ddt==-1)
32   {
33         /* trace for debugging */
34     int argc=1;
35         char *AsciiFromUTF8=NULL;
36         char *msg=_("TCL_EvalScilabCmd %s");
37
38         AsciiFromUTF8=MALLOC(sizeof(char)*(strlen(argv[1])+AddCharacters));
39
40     /* UTF to ANSI */
41         Tcl_UtfToExternal(theinterp, NULL, argv[1], (int)strlen(argv[1]), 0, NULL, AsciiFromUTF8, (int)(strlen(argv[1])+AddCharacters), NULL, NULL,NULL);
42
43     sciprint_full(msg,AsciiFromUTF8);
44         if (msg) {FREE(msg);msg=NULL;}
45
46     while (argv[++argc]) sciprint(" %s",argv[argc]);
47     sciprint("\n");
48
49         if (AsciiFromUTF8){FREE(AsciiFromUTF8);AsciiFromUTF8=NULL;}
50   }
51
52   if (argv[1] != (char *)0)
53   {
54           char *AsciiFromUTF8=NULL;
55           AsciiFromUTF8=MALLOC(sizeof(char)*(strlen(argv[1])+AddCharacters));
56
57           /* UTF to ANSI */
58           Tcl_UtfToExternal(theinterp, NULL, argv[1], (int)strlen(argv[1]), 0, NULL, AsciiFromUTF8, (int)(strlen(argv[1])+AddCharacters), NULL, NULL,NULL);
59
60     if (strlen(AsciiFromUTF8)>=bsiz)
61         {
62       command = (char *) MALLOC (bsiz * sizeof (char));
63       if (command == (char *) 0)
64       {
65                 sciprint(_("TCL_EvalScilabCmd: No more memory.\n"));
66         return TCL_ERROR;
67       }
68       memset(command,'\0',bsiz);
69       strncpy(command,AsciiFromUTF8,bsiz-1);
70           sciprint(_("Warning: ScilabEval command is too long and has been truncated to %d characters!\n"),bsiz-1);
71     }
72         else
73         {
74       command = (char *) MALLOC ((strlen (AsciiFromUTF8) + 1) * sizeof (char));
75       if (command == (char *) 0)
76       {
77                   sciprint(_("TCL_EvalScilabCmd: No more memory.\n"));
78           return TCL_ERROR;
79       }
80       strcpy(command,AsciiFromUTF8);
81     }
82         if (AsciiFromUTF8){FREE(AsciiFromUTF8);AsciiFromUTF8=NULL;}
83
84     if ( (argv[2] != (char *)0) && (strncmp(argv[2],"sync",4)==0) )
85         {
86       /* sync or sync seq */
87       C2F(tksynchro)(&c_n1);  /* set sciprompt to -1 (scilab busy) */
88       seq= ( (argv[3] != (char *)0) && (strncmp(argv[3],"seq",3)==0) );
89       ns=(int)strlen(command); 
90       if (C2F(iop).ddt==-1)
91           {
92                   char *msg=" Execution starts for %s";
93                   sciprint_full(msg,command);
94                   if (msg){FREE(msg);msg=NULL;}
95           sciprint("\n");
96           }
97       C2F(syncexec)(command,&ns,&ierr,&seq,ns);
98       if (C2F(iop).ddt==-1)
99           {
100                   char *msg=" Execution ends for %s";
101                   sciprint_full(msg,command);
102                   if (msg){FREE(msg);msg=NULL;}
103           sciprint("\n");
104           }
105       C2F(tksynchro)(&C2F(recu).paus);
106       if (ierr != 0) return TCL_ERROR;
107     }
108     else if (strncmp(command,"flush",5)==0)
109         {
110       /* flush */
111       if (C2F(iop).ddt==-1) sciprint(" Flushing starts for queued commands.\n");
112       while (ismenu() && ncomm<arbitrary_max_queued_callbacks-1)
113           {
114         ncomm++;
115         comm[ncomm] = (char *) MALLOC (bsiz+1);
116         if (comm[ncomm] == (char *) 0)
117         {
118                         sciprint(_("TCL_EvalScilabCmd: No more memory.\n"));
119                         return TCL_ERROR;
120         }
121         seqf[ncomm]=GetCommand (comm[ncomm]);
122       }
123       if (ismenu()) sciprint(_("Warning: Too many callbacks in queue!\n"));
124       for (nc = 0 ; nc <= ncomm ; nc++ )
125           {
126         C2F(tksynchro)(&c_n1);  /* set sciprompt to -1 (scilab busy) */
127         if (C2F(iop).ddt==-1)
128         {
129               if (seqf[nc]==0)
130                   {
131                           char *msg=" Flushed execution starts for %s - No option";
132                           sciprint_full(msg,comm[nc]);
133                           if (msg){FREE(msg);msg=NULL;}
134               sciprint("\n");
135                   }
136               else
137                   {
138                           char *msg=" Flushed execution starts for %s - seq";
139                           sciprint_full(msg,comm[nc]);
140                           if (msg){FREE(msg);msg=NULL;}
141               sciprint("\n");
142                   }
143         }
144         ns=(int)strlen(comm[nc]);
145         C2F(syncexec)(comm[nc],&ns,&ierr,&(seqf[nc]),ns);
146         if (C2F(iop).ddt==-1)
147         {
148                         char *msg=" Flushed execution ends for %s";
149                         sciprint_full(msg,comm[nc]);
150                         if (msg){FREE(msg);msg=NULL;}
151             sciprint("\n");
152         }
153         FREE(comm[nc]);
154         C2F(tksynchro)(&C2F(recu).paus);
155         if (ierr != 0) return TCL_ERROR;
156       }
157       if (C2F(iop).ddt==-1) sciprint(" Flushing ends\n");
158     }
159     else
160         {
161       /* seq or no option */
162       StoreCommand(command); 
163
164       if ( (argv[2] != (char *)0) && (strncmp(argv[2],"seq",3)==0) )
165           {
166         SetCommandflag(1);
167       }
168       else
169           {
170                 /* unknown option */
171         Tcl_SetResult(theinterp,NULL,NULL);
172           }
173     }
174     FREE(command);
175
176   } 
177   else
178   {
179         /* ScilabEval called without argument */
180         Scierror(999,_("ScilabEval: at least one argument is required.\n"));
181   }
182
183   return TCL_OK;
184 }