Many files cleaned
[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 of a temp variable in Scilab */
39                 if (!C2F(cwritechain)("TMP_EXEC_STRING",&lencommand,(char*)command,(int)strlen("TMP_EXEC_STRING"),(int)strlen(command)) )
40                 {
41                         /* Problem */
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         
93         if (lastjob) { FREE(lastjob);lastjob=NULL;}
94
95         if (JOB)
96         {
97                 lastjob = MALLOC(sizeof(char)*(strlen(JOB)+1)); 
98                 if (lastjob)
99                 {
100                         strcpy(lastjob,JOB);
101                         return TRUE;
102                 }
103         }
104         return FALSE;
105 }
106 /*--------------------------------------------------------------------------*/
107 BOOL GetLastJob(char *JOB,int nbcharsJOB)
108 {
109         if (JOB)
110         {
111                 if ((int)strlen(lastjob)<nbcharsJOB)
112                 {
113                         strcpy(JOB,lastjob);
114                 }
115                 else strncpy(JOB,lastjob,nbcharsJOB);
116                 return TRUE;
117         }
118         return FALSE;
119 }
120 /*--------------------------------------------------------------------------*/
121 int SendScilabJobs(char **jobs,int numberjobs)
122 {
123         #define BUFFERSECURITYSIZE 64
124
125         int retcode=-10;
126
127         if (jobs)
128         {
129                 int i=0;
130                 int nbcharsjobs=0;
131                 char *bufCommands=NULL;
132                 char **LOCALJOBS=NULL;
133
134                 int jobsloop=0;
135
136                 LOCALJOBS=(char**)MALLOC(sizeof(char*)*numberjobs);
137
138                 if (LOCALJOBS)
139                 {
140                         for (i=0;i<numberjobs;i++)
141                         {
142                                 if (jobs[i])
143                                 {
144                                         nbcharsjobs = nbcharsjobs+(int)strlen(jobs[i]);
145                                         LOCALJOBS[i]=(char*)MALLOC( sizeof(char)*(strlen(jobs[i])+BUFFERSECURITYSIZE) );
146                                         if (LOCALJOBS[i])
147                                         {
148                                                 strcpy(LOCALJOBS[i],jobs[i]);
149                                         }
150                                         else
151                                         {
152                                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
153                                                 fprintf(stderr,"Error : SendScilabJobs (1) 'LOCALJOBS[%d] MALLOC'.\n",i);       
154                                                 return retcode;
155                                         }
156                                 }
157                                 else
158                                 {
159                                         fprintf(stderr,"Error : SendScilabJobs (2) 'jobs[%d] == NULL'.\n",i);   
160                                         return retcode;
161                                 }
162                         }
163
164                         bufCommands = (char*)MALLOC( sizeof(char)*(nbcharsjobs+numberjobs+BUFFERSECURITYSIZE) );
165
166                         if (bufCommands)
167                         {
168                                 strcpy(bufCommands,"");
169
170                                 for (jobsloop=0;jobsloop<numberjobs;jobsloop++)
171                                 {
172                                         if (jobs[jobsloop])
173                                         {
174                                                 char *currentline=NULL;
175                                                 BOOL AddSemiColon;
176
177                                                 if (jobsloop == 0) AddSemiColon=FALSE;
178                                                 else  AddSemiColon=TRUE;
179
180                                 DOTDOTLOOP:
181                                                 currentline=LOCALJOBS[jobsloop];
182
183                                                 RemoveCharsFromEOL(currentline,'\n');
184                                                 RemoveComments(currentline);
185                                                 RemoveCharsFromEOL(currentline,' ');
186
187                                                 if (RemoveCharsFromEOL(currentline,'.'))
188                                                 {
189                                                         RemoveCharsFromEOL(currentline,' ');
190                                                         strcat(bufCommands,currentline);
191                                                         jobsloop++;
192                                                         AddSemiColon=FALSE;
193                                                         goto DOTDOTLOOP;
194                                                 }
195                                                 else
196                                                 {
197                                                         if (!AddSemiColon)
198                                                         {
199                                                                 AddSemiColon=TRUE;
200                                                                 strcat(currentline,";");
201                                                         }
202                                                         else
203                                                         {
204                                                                 strcat(bufCommands,";");
205                                                         }
206                                                         
207                                                         strcat(bufCommands,currentline);
208                                                 }
209                                         }
210                                 }
211
212                                 retcode=SendScilabJob(bufCommands);
213
214                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
215                         }
216                         else
217                         {
218                                 CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
219                                 fprintf(stderr,"Error : SendScilabJobs (3) 'bufCommands MALLOC'.\n");   
220                                 return retcode;
221                         }
222                 }
223                 else
224                 {
225                         CleanBuffers(bufCommands,LOCALJOBS,numberjobs);
226                         fprintf(stderr,"Error : SendScilabJobs (4) 'LOCALJOBS == NULL'.\n");    
227                         return retcode;
228                 }
229         }
230         else
231         {
232                 fprintf(stderr,"Error : SendScilabJobs (5) 'jobs == NULL'.\n");
233                 retcode = -10;
234         }
235         
236         return retcode;
237 }
238 /*--------------------------------------------------------------------------*/
239 static BOOL RemoveCharsFromEOL(char *line,char CharToRemove)
240 {
241         int l=0;
242         BOOL bOK=FALSE;
243         int len=0;
244
245         len=(int)strlen(line);
246         for (l=len-1;l>0;l--)
247         {
248                 if (line[l] == CharToRemove)
249                 {
250                         line[l] = '\0';
251                         bOK=TRUE;
252                 }
253                 else break;
254         }
255         return bOK;
256 }
257 /*--------------------------------------------------------------------------*/
258 static BOOL RemoveComments(char *line)
259 {
260         int l=0;
261         int len=0;
262         int idx=-1;
263
264         len=(int)strlen(line);
265         for (l=len-1;l>0;l--)
266         {
267                 if (line[l] == '/') 
268                 {
269                         if (l-1>0)
270                         {
271                                 if (line[l-1] == '/') 
272                                 {
273                                         idx=l-1;
274                                         l=l-2;
275                                 }
276                         }
277                 }
278         }
279
280         if (idx>=0) line[idx]='\0';
281         
282         return FALSE;
283 }
284 /*--------------------------------------------------------------------------*/
285 static BOOL CleanBuffers(char *bufCommands,char **LOCALJOBS,int numberjobs)
286 {
287
288         if (bufCommands) {FREE(bufCommands);bufCommands=NULL;}
289
290         if (LOCALJOBS)
291         {
292                 int i=0;
293                 for (i=0; i < numberjobs;i++)
294                 {
295                         if (LOCALJOBS[i]) {FREE(LOCALJOBS[i]);LOCALJOBS[i]=NULL;}
296                 }
297                 LOCALJOBS=NULL;
298         }
299
300         
301         return TRUE;
302
303 }
304 /*--------------------------------------------------------------------------*/