36798ca75997ff5cc2ecee92a6ba186599d073a7
[scilab.git] / scilab / modules / windows_tools / sci_gateway / c / sci_dos.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 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
13 /*--------------------------------------------------------------------------*/
14 #include <stdio.h>
15 #include "gw_windows_tools.h"
16 #include "PATH_MAX.h"
17 #include "stack-c.h"
18 #include "MALLOC.h" /* MALLOC */
19 #include "spawncommand.h"
20 #include "sciprint.h"
21 #include "sci_tmpdir.h"
22 #include "FileExist.h"
23 #include "localization.h"
24 #include "Scierror.h"
25 #include "freeArrayOfString.h"
26 #include "getshortpathname.h"
27 #include "api_scilab.h"
28 /*--------------------------------------------------------------------------*/
29 #define BUFSIZE 4096
30 /*--------------------------------------------------------------------------*/
31 static int PrintOuput(char **ouput,int nbrlines);
32 /*--------------------------------------------------------------------------*/
33 int sci_dos(char *fname, int* _piKey)
34 {
35         SciErr sciErr;
36         int *piAddressVarOne = NULL;
37         int iType1      = 0;
38         int m1 = 0, n1 = 0;
39         char *pStVarOne = NULL;
40         int lenStVarOne = 0;
41
42         char **Output = NULL;
43         int numberoflines = 0;
44         BOOL ECHOMODE = FALSE;
45         
46         CheckRhs(1,2);
47         CheckLhs(1,3);
48
49         if (Rhs == 2)
50         {
51                 int *piAddressVarTwo = NULL;
52                 int m2 = 0, n2 = 0;
53                 int iType2 = 0;
54                 char *pStVarTwo = NULL;
55                 int lenStVarTwo = 0;
56
57                 sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
58                 if(sciErr.iErr)
59                 {
60                         printError(&sciErr, 0);
61                         return 0;
62                 }
63
64                 sciErr = getVarType(_piKey, piAddressVarTwo, &iType2);
65                 if(sciErr.iErr)
66                 {
67                         printError(&sciErr, 0);
68                         return 0;
69                 }
70
71                 if (iType2  != sci_strings )
72                 {
73                         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
74                         return 0;
75                 }
76
77                 sciErr = getMatrixOfString(_piKey, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
78                 if(sciErr.iErr)
79                 {
80                         printError(&sciErr, 0);
81                         return 0;
82                 }
83
84                 if ( (m2 != n2) && (n2 != 1) ) 
85                 {
86                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
87                         return 0;
88                 }
89
90                 pStVarTwo = (char*)MALLOC(sizeof(char)*(lenStVarTwo + 1));
91                 if (pStVarTwo)
92                 {
93                         sciErr = getMatrixOfString(_piKey, piAddressVarTwo, &m2, &n2, &lenStVarTwo, &pStVarTwo);
94                         if(sciErr.iErr)
95                         {
96                                 printError(&sciErr, 0);
97                                 return 0;
98                         }
99
100                         if ( strcmp(pStVarTwo, "-echo") )
101                         {
102                                 FREE(pStVarTwo); pStVarTwo = NULL;
103                                 Scierror(999,_("%s: Wrong value for input argument #%d: '%s' expected.\n"),fname,2,"-echo");
104                                 return 0;
105                         }
106                         else
107                         {
108                                 ECHOMODE = TRUE;
109                         }
110                 }
111                 else
112                 {
113                         Scierror(999,_("%s: No more memory.\n"),fname);
114                         return 0;
115                 }
116         }
117
118         sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
119         if(sciErr.iErr)
120         {
121                 printError(&sciErr, 0);
122                 return 0;
123         }
124
125         sciErr = getVarType(_piKey, piAddressVarOne, &iType1);
126         if(sciErr.iErr)
127         {
128                 printError(&sciErr, 0);
129                 return 0;
130         }
131
132         if (iType1  != sci_strings )
133         {
134                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,1);
135                 return 0;
136         }
137
138         sciErr = getMatrixOfString(_piKey, piAddressVarOne,&m1, &n1, &lenStVarOne, &pStVarOne);
139         if(sciErr.iErr)
140         {
141                 printError(&sciErr, 0);
142                 return 0;
143         }
144
145         if ( (m1 != n1) && (n1 != 1) ) 
146         {
147                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
148                 return 0;
149         }
150
151         pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
152         if (pStVarOne)
153         {
154                 double exitCode = 0.;
155                 BOOL DetachProcessOption = FALSE;
156                 BOOL *StatusExit = NULL;
157
158                 sciErr = getMatrixOfString(_piKey, piAddressVarOne, &m1, &n1, &lenStVarOne, &pStVarOne);
159                 if(sciErr.iErr)
160                 {
161                         printError(&sciErr, 0);
162                         return 0;
163                 }
164
165                 DetachProcessOption = DetectDetachProcessInCommandLine(pStVarOne);
166                 exitCode = (double)spawncommand(pStVarOne, DetachProcessOption);
167                 FREE(pStVarOne); pStVarOne = NULL;
168
169                 StatusExit = (BOOL*)MALLOC(sizeof(BOOL));
170
171                 if (DetachProcessOption)
172                 {
173                         if ( strlen(pipeSpawnErr.OutputBuffer) )
174                         {
175                                 /* StdErr will be "Output" */
176                                 *StatusExit = FALSE;
177                                 Output = CreateOuput(&pipeSpawnErr,DetachProcessOption);
178                                 numberoflines = pipeSpawnErr.NumberOfLines;
179                         }
180                         else
181                         {
182                                 /* StdOut will be "Output" */
183                                 *StatusExit = TRUE;
184                                 Output = CreateOuput(&pipeSpawnOut,DetachProcessOption);
185                                 numberoflines = pipeSpawnOut.NumberOfLines;
186                         }
187                 }
188                 else
189                 {
190                         char FileTMPDir[PATH_MAX + 16];
191                         BOOL bConvert = FALSE;
192
193                         char *TMPDirLong = getTMPDIR();
194                         char *TMPDirShort = getshortpathname(TMPDirLong, &bConvert);
195
196                         sprintf(FileTMPDir,"%s\\DOS.OK",TMPDirLong);
197                         FREE(TMPDirLong); TMPDirLong = NULL;
198                         FREE(TMPDirShort); TMPDirShort = NULL;
199
200                         if (FileExist(FileTMPDir))
201                         {
202                                 DeleteFile(FileTMPDir);
203                                 /* StdOut will be "Output" */
204                                 *StatusExit = TRUE;
205                                 Output = CreateOuput(&pipeSpawnOut,DetachProcessOption);
206                                 numberoflines = pipeSpawnOut.NumberOfLines;
207                         }
208                         else
209                         {
210                                 /* StdErr will be "Output" */
211                                 *StatusExit = FALSE;
212                                 Output = CreateOuput(&pipeSpawnErr,DetachProcessOption);
213                                 numberoflines = pipeSpawnErr.NumberOfLines;
214                         }
215                 }
216
217                 if (ECHOMODE) PrintOuput(Output, numberoflines);
218
219                 if (Lhs == 1)
220                 {
221                         int m_out = 1, n_out = 1;
222                         sciErr = createMatrixOfBoolean(_piKey, Rhs + 1, m_out, n_out, StatusExit);
223                         if(sciErr.iErr)
224                         {
225                                 printError(&sciErr, 0);
226                                 return 0;
227                         }
228
229                         LhsVar(1) = Rhs + 1;
230                 }
231                 else
232                 {
233                         int m_out2 = 1;
234                         int n_out2 = 1;
235
236                         if (Output && Output[0])
237                         {
238                                 int m_out1 = numberoflines;
239                                 int n_out1 = 1;
240                                 sciErr = createMatrixOfString(_piKey, Rhs + 1, m_out1, n_out1, Output);
241                         }
242                         else
243                         {
244                                 /* returns [] */
245                                 int m_out1 = 0;
246                                 int n_out1 = 0;
247                                 sciErr = createMatrixOfDouble(_piKey, Rhs + 1, m_out1, n_out1, NULL);
248                         }
249
250                         if(sciErr.iErr)
251                         {
252                                 printError(&sciErr, 0);
253                                 return 0;
254                         }
255
256                         LhsVar(1) = Rhs + 1;
257
258                         sciErr = createMatrixOfBoolean(_piKey, Rhs + 2, m_out2, n_out2, StatusExit);
259                         if(sciErr.iErr)
260                         {
261                                 printError(&sciErr, 0);
262                                 return 0;
263                         }
264
265                         LhsVar(2) = Rhs + 2;
266                 }
267
268                 if (Lhs > 2)
269                 {
270                         int m_out3 = 1, n_out3 = 1;
271                         sciErr = createMatrixOfDouble(_piKey, Rhs + 3, m_out3, n_out3, &exitCode); 
272                         if(sciErr.iErr)
273                         {
274                                 printError(&sciErr, 0);
275                                 return 0;
276                         }
277
278                         LhsVar(3) = Rhs + 3;
279                 }
280
281                 C2F(putlhsvar)();
282
283                 if (StatusExit) {FREE(StatusExit); StatusExit = NULL;}
284                 freeArrayOfString(Output, numberoflines);
285
286                 ClosePipeInfo (pipeSpawnOut);
287                 ClosePipeInfo (pipeSpawnErr);
288
289         }
290         else
291         {
292                 Scierror(999,_("%s: No more memory.\n"),fname);
293         }
294         
295         return 0;
296 }
297 /*--------------------------------------------------------------------------*/
298 static int PrintOuput(char **ouput,int nbrlines)
299 {
300         if (ouput)
301         {
302                 int i = 0;
303                 for(i = 0;i<nbrlines;i++) 
304                 {
305                         if (ouput[i])
306                         {
307                                 sciprint("%s\n",ouput[i]);
308                         }
309                 }
310                 return TRUE;
311         }
312         return FALSE;
313 }
314 /*--------------------------------------------------------------------------*/
315