51400550fcb3278f1994e8565c100c890e1144cb
[scilab.git] / scilab / modules / io / sci_gateway / c / sci_getenv.c
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2006 - INRIA - Allan CORNET
4  * Copyright (C) 2009 - DIGITEO - Allan CORNET
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13 #include <stdio.h>
14 #include "gw_io.h"
15 #include "MALLOC.h"
16 #include "freeArrayOfString.h"
17 #include "localization.h"
18 #include "Scierror.h"
19 #include "stack-c.h"
20 #include "api_scilab.h"
21 #include "getenvc.h"
22 #include "PATH_MAX.h"
23 /*--------------------------------------------------------------------------*/
24 int sci_getenv(char *fname, int* _piKey)
25 {
26         SciErr sciErr;
27         int ierr = 0;
28         char *default_env_value = NULL;
29         char *env_value = NULL;
30         int length_env = 0;
31         char *env_name = NULL;
32
33         int m1 = 0, n1 = 0;
34         int *piAddressVarOne = NULL;
35         int iType1      = 0;
36         char *pStVarOne = NULL;
37         int lenStVarOne = 0;
38
39         int m2 = 0, n2 = 0;
40         int *piAddressVarTwo = NULL;
41         int iType2      = 0;
42         char *pStVarTwo = NULL;
43         int lenStVarTwo = 0;
44
45         Rhs = Max(Rhs,0);
46
47         CheckRhs(1,2);
48         CheckLhs(1,1);
49
50         if (Rhs == 2)
51         {
52                 sciErr = getVarAddressFromPosition(_piKey, 2, &piAddressVarTwo);
53                 if(sciErr.iErr)
54                 {
55                         printError(&sciErr, 0);
56                         return 0;
57                 }
58
59                 sciErr = getVarType(_piKey, piAddressVarTwo, &iType2);
60                 if(sciErr.iErr)
61                 {
62                         printError(&sciErr, 0);
63                         return 0;
64                 }
65
66                 if (iType2  != sci_strings )
67                 {
68                         Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
69                         return 0;
70                 }
71
72                 sciErr = getMatrixOfString(_piKey, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&pStVarTwo);
73                 if(sciErr.iErr)
74                 {
75                         printError(&sciErr, 0);
76                         return 0;
77                 }
78
79                 if ( (m2 != n2) && (n2 != 1) ) 
80                 {
81                         Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,2);
82                         return 0;
83                 }
84
85                 pStVarTwo = (char*)MALLOC(sizeof(char)*(lenStVarTwo + 1));
86                 if (pStVarTwo)
87                 {
88                         sciErr = getMatrixOfString(_piKey, piAddressVarTwo,&m2,&n2,&lenStVarTwo,&pStVarTwo);
89                         if(sciErr.iErr)
90                         {
91                                 printError(&sciErr, 0);
92                                 return 0;
93                         }
94                 }
95                 else
96                 {
97                         Scierror(999,_("%s: Memory allocation error.\n"),fname);
98                         return 0;
99                 }
100         }
101
102         sciErr = getVarAddressFromPosition(_piKey, 1, &piAddressVarOne);
103         if(sciErr.iErr)
104         {
105                 printError(&sciErr, 0);
106                 return 0;
107         }
108
109         sciErr = getVarType(_piKey, piAddressVarOne, &iType1);
110         if(sciErr.iErr)
111         {
112                 printError(&sciErr, 0);
113                 return 0;
114         }
115
116         if (iType1  != sci_strings )
117         {
118                 if (pStVarTwo) {FREE(pStVarTwo); pStVarTwo = NULL;}
119                 Scierror(999,_("%s: Wrong type for input argument #%d: A string expected.\n"),fname,2);
120                 return 0;
121         }
122
123         sciErr = getMatrixOfString(_piKey, piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
124         if(sciErr.iErr)
125         {
126                 printError(&sciErr, 0);
127                 return 0;
128         }
129
130         if ( (m1 != n1) && (n1 != 1) ) 
131         {
132                 if (pStVarTwo) {FREE(pStVarTwo); pStVarTwo = NULL;}
133                 Scierror(999,_("%s: Wrong size for input argument #%d: A string expected.\n"),fname,1);
134                 return 0;
135         }
136
137         pStVarOne = (char*)MALLOC(sizeof(char)*(lenStVarOne + 1));
138         if (pStVarOne)
139         {
140                 sciErr = getMatrixOfString(_piKey, piAddressVarOne,&m1,&n1,&lenStVarOne,&pStVarOne);
141                 if(sciErr.iErr)
142                 {
143                         printError(&sciErr, 0);
144                         return 0;
145                 }
146         }
147         else
148         {
149                 if (pStVarTwo) {FREE(pStVarTwo); pStVarTwo = NULL;}
150                 Scierror(999,_("%s: Memory allocation error.\n"),fname);
151                 return 0;
152         }
153
154         #ifdef _MSC_VER
155         length_env = _MAX_ENV;
156         #else
157         length_env = bsiz;
158         #endif
159
160         default_env_value =  pStVarTwo;
161         env_name = pStVarOne;
162
163         env_value = (char*)MALLOC( (length_env + 1) *sizeof(char) );
164
165         if (env_value == NULL)
166         {
167                 if (default_env_value) {FREE(default_env_value); default_env_value = NULL;}
168                 if (env_name) {FREE(env_name); env_name = NULL;}
169
170                 Scierror(999,_("%s: No more memory.\n"), fname);
171                 return 0;
172         }
173         else
174         {
175                 int m_out = 1, n_out = 1;
176                 int iflag = 0;
177
178                 getenvc(&ierr, env_name, env_value, &length_env, &iflag);
179
180                 if (ierr == 0)
181                 {
182                         sciErr = createMatrixOfString(_piKey, Rhs + 1, m_out, n_out, &env_value);
183                         if(sciErr.iErr)
184                         {
185                                 printError(&sciErr, 0);
186                                 return 0;
187                         }
188
189                         LhsVar(1) = Rhs + 1;
190                         C2F(putlhsvar)();       
191                 }
192                 else
193                 {
194                         if (default_env_value)
195                         {
196                                 sciErr = createMatrixOfString(_piKey, Rhs + 1, m_out, n_out, &default_env_value);
197                                 if(sciErr.iErr)
198                                 {
199                                         printError(&sciErr, 0);
200                                         return 0;
201                                 }
202
203                                 LhsVar(1) = Rhs + 1;
204                                 C2F(putlhsvar)();       
205                         }
206                         else
207                         {
208                                 Scierror(999,_("%s: Undefined environment variable %s.\n"), fname, env_name);
209                         }
210                 }
211
212                 if (default_env_value) {FREE(default_env_value); default_env_value = NULL;}
213                 if (env_name) {FREE(env_name); env_name = NULL;}
214                 if (env_value) {FREE(env_value); env_value = NULL;}
215
216         }
217         return 0;
218 }
219 /*--------------------------------------------------------------------------*/