update projects (examples) for Visual Studio 2008
[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 <stdio.h>
13 #include "CallScilab.h"
14 #include "MALLOC.h"
15 #include "scirun.h"
16 #include "localization.h"
17 #include "stack-c.h"
18 #include "freeArrayOfString.h"
19 #ifdef _MSC_VER
20 #include "strdup_windows.h"
21 #endif
22 /*--------------------------------------------------------------------------*/
23 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove);
24 static BOOL RemoveComments(char *line);
25 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs);
26 static BOOL SetLastJob(char *JOB);
27 static char *lastjob=NULL;
28 /*--------------------------------------------------------------------------*/
29 /* see CallScilab.h more informations*/
30 /*--------------------------------------------------------------------------*/
31 int SendScilabJob(char *job)
32 {
33         int retCode = -1;
34         int lencommand=0;
35         char *command=NULL;
36         
37         static char ScirunCommand[]="Err=execstr(TMP_EXEC_STRING,\"errcatch\",\"n\");quit;";
38         static char ClearTmpVariables[]="clear TMP_EXEC_STRING;clear Err;quit;";
39         
40         command = strdup(job);
41         lencommand = (int)strlen(command);
42
43         if (command)
44         {
45                 /* clear prev. Err , TMP_EXEC_STRING scilab variables */
46                 C2F(scirun)(ClearTmpVariables,(long int)strlen(ClearTmpVariables));
47
48                 SetLastJob(command);
49
50                 /* Creation of a temp variable in Scilab */
51                 if (!C2F(cwritechain)("TMP_EXEC_STRING",&lencommand,(char*)command,(int)strlen("TMP_EXEC_STRING"),(int)strlen(command)) )
52                 {
53                         /* Problem */
54                         fprintf(stderr,"Error: SendScilabJob (1) 'TMP_EXEC_STRING'.\n");
55                         retCode = -1;
56
57                         if (command) {FREE(command);command=NULL;}
58                         return retCode;
59                 }
60                 else
61                 {
62                         int m=0,n=0,lp=0;
63
64                         C2F(scirun)(ScirunCommand,(long int)strlen(ScirunCommand));
65
66                         /* get error code from scilab */
67
68                         if ( ! C2F(cmatptr)("Err", &m, &n, &lp,(unsigned long)strlen("Err")))
69                         {
70                                 fprintf(stderr,"Error : SendScilabJob (2) 'Err'.\n");
71                                 retCode = -2;
72                         }
73                         else
74                         {
75                                 if (m*n == 1)
76                                 {
77                                         double code = -1;
78                                         ReadMatrix("Err", &m, &n, &code);
79                                         retCode = (int) code ;
80                                 }
81                                 else 
82                                 {
83                                         fprintf(stderr,"Error : SendScilabJob (3) 'Err'.\n");   
84                                         retCode = -3;
85                                 }
86                         }
87
88                         /* clear prev. Err , TMP_EXEC_STRING scilab variables */
89                         C2F(scirun)(ClearTmpVariables,(long int)strlen(ClearTmpVariables));
90                 }
91                 if (command) {FREE(command);command=NULL;}
92         }
93         else
94         {
95                 fprintf(stderr,"Error : SendScilabJob (4) 'command' MALLOC.\n");
96                 retCode = -4;
97         }
98
99         return retCode;
100 }
101 /*--------------------------------------------------------------------------*/
102 static BOOL SetLastJob(char *JOB)
103 {
104         
105         if (lastjob) { FREE(lastjob);lastjob=NULL;}
106
107         if (JOB)
108         {
109                 lastjob = strdup(JOB);
110                 if (lastjob)
111                 {
112                         return TRUE;
113                 }
114         }
115         return FALSE;
116 }
117 /*--------------------------------------------------------------------------*/
118 BOOL GetLastJob(char *JOB,int nbcharsJOB)
119 {
120         if (JOB)
121         {
122                 if ((int)strlen(lastjob)<nbcharsJOB)
123                 {
124                         strcpy(JOB,lastjob);
125                 }
126                 else strncpy(JOB,lastjob,nbcharsJOB);
127                 return TRUE;
128         }
129         return FALSE;
130 }
131 /*--------------------------------------------------------------------------*/
132 int SendScilabJobs(char **jobs,int numberjobs)
133 {
134         #define BUFFERSECURITYSIZE 64
135
136         char *bufCommands = NULL;
137         int retcode=-10;
138
139         if (jobs)
140         {
141                 char **LOCALJOBS = (char**)MALLOC(sizeof(char*)*numberjobs);
142
143                 if (LOCALJOBS)
144                 {
145                         int i = 0;
146                         int nbcharsjobs = 0;
147
148                         for (i = 0; i < numberjobs ;i++)
149                         {
150                                 if (jobs[i])
151                                 {
152                                         nbcharsjobs = nbcharsjobs + (int)strlen(jobs[i]);
153                                         LOCALJOBS[i] = strdup(jobs[i]);
154                                         if (LOCALJOBS[i] == NULL)
155                                         {
156                                                 CleanBuffers(bufCommands,LOCALJOBS,i);
157                                                 fprintf(stderr,"Error : SendScilabJobs (1) 'LOCALJOBS[%d] MALLOC'.\n",i);       
158                                                 return retcode;
159                                         }
160                                 }
161                                 else
162                                 {
163                                         fprintf(stderr,"Error : SendScilabJobs (2) 'jobs[%d] == NULL'.\n",i);   
164                                         return retcode;
165                                 }
166                         }
167
168                         bufCommands = (char*)MALLOC( sizeof(char)*(nbcharsjobs+numberjobs+BUFFERSECURITYSIZE) );
169
170                         if (bufCommands)
171                         {
172                                 int jobsloop = 0;
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 = strdup(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
218                                 retcode = SendScilabJob(bufCommands);
219
220                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
221                         }
222                         else
223                         {
224                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
225                                 fprintf(stderr,"Error : SendScilabJobs (3) 'bufCommands MALLOC'.\n");   
226                                 return retcode;
227                         }
228                 }
229                 else
230                 {
231                         CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
232                         fprintf(stderr,"Error : SendScilabJobs (4) 'LOCALJOBS == NULL'.\n");    
233                         return retcode;
234                 }
235         }
236         else
237         {
238                 fprintf(stderr,"Error : SendScilabJobs (5) 'jobs == NULL'.\n");
239                 retcode = -10;
240         }
241         
242         return retcode;
243 }
244 /*--------------------------------------------------------------------------*/
245 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove)
246 {
247         int l=0;
248         BOOL bOK=FALSE;
249         int len=0;
250
251         len=(int)strlen(line);
252         for (l=len-1;l>0;l--)
253         {
254                 if (line[l] == CharToRemove)
255                 {
256                         line[l] = '\0';
257                         bOK=TRUE;
258                 }
259                 else break;
260         }
261         return bOK;
262 }
263 /*--------------------------------------------------------------------------*/
264 static BOOL RemoveComments(char *line)
265 {
266         int l=0;
267         int len=0;
268         int idx=-1;
269
270         len=(int)strlen(line);
271         for (l=len-1;l>0;l--)
272         {
273                 if (line[l] == '/') 
274                 {
275                         if (l-1>0)
276                         {
277                                 if (line[l-1] == '/') 
278                                 {
279                                         idx=l-1;
280                                         l=l-2;
281                                 }
282                         }
283                 }
284         }
285
286         if (idx>=0) line[idx]='\0';
287         
288         return FALSE;
289 }
290 /*--------------------------------------------------------------------------*/
291 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs)
292 {
293
294         if (bufCommands) {FREE(bufCommands);bufCommands=NULL;}
295         freeArrayOfString(LOCALJOBS,numberjobs);
296         return TRUE;
297
298 }
299 /*--------------------------------------------------------------------------*/