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