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