811902594cc9cb0ba650400e29bd8f2779981849
[scilab.git] / scilab / modules / core / src / c / SendScilabJobs.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2007 - INRIA - Allan CORNET
4  * 
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at    
9  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
10  *
11  */
12 #include "CallScilab.h"
13 #include "MALLOC.h"
14 #include "scirun.h"
15 #include "localization.h"
16 #ifdef _MSC_VER
17 #include "strdup_windows.h"
18 #endif
19 /*--------------------------------------------------------------------------*/
20 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove);
21 static BOOL RemoveComments(char *line);
22 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs);
23 static BOOL SetLastJob(char *JOB);
24 static char *lastjob=NULL;
25 /*--------------------------------------------------------------------------*/
26 /* see CallScilab.h more informations*/
27 /*--------------------------------------------------------------------------*/
28 int SendScilabJob(char *job)
29 {
30         int retCode = -1;
31         int lencommand=0;
32         static char *command=NULL;
33         
34         char ScirunCommand[]="Err=execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;";
35         char ClearTmpVariables[]="clear TMP_EXEC_STRING;clear Err;quit;";
36         
37         lencommand=(int)strlen(job);
38         command=MALLOC(sizeof(char)*(lencommand+1));
39
40         if (command)
41         {
42                 /* clear prev. Err , TMP_EXEC_STRING scilab variables */
43                 C2F(scirun)(ClearTmpVariables,(long int)strlen(ClearTmpVariables));
44
45                 strcpy(command,job);
46                 SetLastJob(command);
47
48                 /* Creation of a temp variable in Scilab */
49                 if (!C2F(cwritechain)("TMP_EXEC_STRING",&lencommand,(char*)command,(int)strlen("TMP_EXEC_STRING"),(int)strlen(command)) )
50                 {
51                         /* Problem */
52                         fprintf(stderr,"Error: SendScilabJob (1) 'TMP_EXEC_STRING'.\n");
53                         retCode = -1;
54
55                         if (command) {FREE(command);command=NULL;}
56                         return retCode;
57                 }
58                 else
59                 {
60                         int m=0,n=0,lp=0;
61
62                         C2F(scirun)(ScirunCommand,(long int)strlen(ScirunCommand));
63
64                         /* get error code from scilab */
65
66                         if ( ! C2F(cmatptr)("Err", &m, &n, &lp,(unsigned long)strlen("Err")))
67                         {
68                                 fprintf(stderr,"Error : SendScilabJob (2) 'Err'.\n");
69                                 retCode = -2;
70                         }
71                         else
72                         {
73                                 if (m*n == 1)
74                                 {
75                                         double code = -1;
76                                         ReadMatrix("Err", &m, &n, &code);
77                                         retCode = (int) code ;
78                                 }
79                                 else 
80                                 {
81                                         fprintf(stderr,"Error : SendScilabJob (3) 'Err'.\n");   
82                                         retCode = -3;
83                                 }
84                         }
85
86                         /* clear prev. Err , TMP_EXEC_STRING scilab variables */
87                         C2F(scirun)(ClearTmpVariables,(long int)strlen(ClearTmpVariables));
88                 }
89                 if (command) {FREE(command);command=NULL;}
90         }
91         else
92         {
93                 fprintf(stderr,"Error : SendScilabJob (4) 'command' MALLOC.\n");
94                 retCode = -4;
95         }
96
97         return retCode;
98 }
99 /*--------------------------------------------------------------------------*/
100 static BOOL SetLastJob(char *JOB)
101 {
102         
103         if (lastjob) { FREE(lastjob);lastjob=NULL;}
104
105         if (JOB)
106         {
107                 lastjob = strdup(JOB);
108                 if (lastjob)
109                 {
110                         return TRUE;
111                 }
112         }
113         return FALSE;
114 }
115 /*--------------------------------------------------------------------------*/
116 BOOL GetLastJob(char *JOB,int nbcharsJOB)
117 {
118         if (JOB)
119         {
120                 if ((int)strlen(lastjob)<nbcharsJOB)
121                 {
122                         strcpy(JOB,lastjob);
123                 }
124                 else strncpy(JOB,lastjob,nbcharsJOB);
125                 return TRUE;
126         }
127         return FALSE;
128 }
129 /*--------------------------------------------------------------------------*/
130 int SendScilabJobs(char **jobs,int numberjobs)
131 {
132         #define BUFFERSECURITYSIZE 64
133
134         int retcode=-10;
135
136         if (jobs)
137         {
138                 int i=0;
139                 int nbcharsjobs=0;
140                 char *bufCommands=NULL;
141                 char **LOCALJOBS=NULL;
142
143                 int jobsloop=0;
144
145                 LOCALJOBS=(char**)MALLOC(sizeof(char*)*numberjobs);
146
147                 if (LOCALJOBS)
148                 {
149                         for (i=0;i<numberjobs;i++)
150                         {
151                                 if (jobs[i])
152                                 {
153                                         nbcharsjobs = nbcharsjobs+(int)strlen(jobs[i]);
154                                         LOCALJOBS[i] = strdup(jobs[i]);
155                                         if (LOCALJOBS[i] == NULL)
156                                         {
157                                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
158                                                 fprintf(stderr,"Error : SendScilabJobs (1) 'LOCALJOBS[%d] MALLOC'.\n",i);       
159                                                 return retcode;
160                                         }
161                                 }
162                                 else
163                                 {
164                                         fprintf(stderr,"Error : SendScilabJobs (2) 'jobs[%d] == NULL'.\n",i);   
165                                         return retcode;
166                                 }
167                         }
168
169                         bufCommands = (char*)MALLOC( sizeof(char)*(nbcharsjobs+numberjobs+BUFFERSECURITYSIZE) );
170
171                         if (bufCommands)
172                         {
173                                 strcpy(bufCommands,"");
174
175                                 for (jobsloop=0;jobsloop<numberjobs;jobsloop++)
176                                 {
177                                         if (jobs[jobsloop])
178                                         {
179                                                 char *currentline=NULL;
180                                                 BOOL AddSemiColon;
181
182                                                 if (jobsloop == 0) AddSemiColon=FALSE;
183                                                 else  AddSemiColon=TRUE;
184
185                                 DOTDOTLOOP:
186                                                 currentline=LOCALJOBS[jobsloop];
187
188                                                 RemoveCharsFromEOL(currentline,'\n');
189                                                 RemoveComments(currentline);
190                                                 RemoveCharsFromEOL(currentline,' ');
191
192                                                 if (RemoveCharsFromEOL(currentline,'.'))
193                                                 {
194                                                         RemoveCharsFromEOL(currentline,' ');
195                                                         strcat(bufCommands,currentline);
196                                                         jobsloop++;
197                                                         AddSemiColon=FALSE;
198                                                         goto DOTDOTLOOP;
199                                                 }
200                                                 else
201                                                 {
202                                                         if (!AddSemiColon)
203                                                         {
204                                                                 AddSemiColon=TRUE;
205                                                                 strcat(currentline,";");
206                                                         }
207                                                         else
208                                                         {
209                                                                 strcat(bufCommands,";");
210                                                         }
211                                                         
212                                                         strcat(bufCommands,currentline);
213                                                 }
214                                         }
215                                 }
216
217                                 retcode=SendScilabJob(bufCommands);
218
219                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
220                         }
221                         else
222                         {
223                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
224                                 fprintf(stderr,"Error : SendScilabJobs (3) 'bufCommands MALLOC'.\n");   
225                                 return retcode;
226                         }
227                 }
228                 else
229                 {
230                         CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
231                         fprintf(stderr,"Error : SendScilabJobs (4) 'LOCALJOBS == NULL'.\n");    
232                         return retcode;
233                 }
234         }
235         else
236         {
237                 fprintf(stderr,"Error : SendScilabJobs (5) 'jobs == NULL'.\n");
238                 retcode = -10;
239         }
240         
241         return retcode;
242 }
243 /*--------------------------------------------------------------------------*/
244 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove)
245 {
246         int l=0;
247         BOOL bOK=FALSE;
248         int len=0;
249
250         len=(int)strlen(line);
251         for (l=len-1;l>0;l--)
252         {
253                 if (line[l] == CharToRemove)
254                 {
255                         line[l] = '\0';
256                         bOK=TRUE;
257                 }
258                 else break;
259         }
260         return bOK;
261 }
262 /*--------------------------------------------------------------------------*/
263 static BOOL RemoveComments(char *line)
264 {
265         int l=0;
266         int len=0;
267         int idx=-1;
268
269         len=(int)strlen(line);
270         for (l=len-1;l>0;l--)
271         {
272                 if (line[l] == '/') 
273                 {
274                         if (l-1>0)
275                         {
276                                 if (line[l-1] == '/') 
277                                 {
278                                         idx=l-1;
279                                         l=l-2;
280                                 }
281                         }
282                 }
283         }
284
285         if (idx>=0) line[idx]='\0';
286         
287         return FALSE;
288 }
289 /*--------------------------------------------------------------------------*/
290 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs)
291 {
292
293         if (bufCommands) {FREE(bufCommands);bufCommands=NULL;}
294
295         if (LOCALJOBS)
296         {
297                 int i=0;
298                 for (i=0; i < numberjobs;i++)
299                 {
300                         if (LOCALJOBS[i]) {FREE(LOCALJOBS[i]);LOCALJOBS[i]=NULL;}
301                 }
302                 LOCALJOBS=NULL;
303         }
304
305         
306         return TRUE;
307
308 }
309 /*--------------------------------------------------------------------------*/