Fix some valgrind issues
[scilab.git] / scilab / modules / core / src / c / getversion.c
1 /*
2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2010 - DIGITEO - 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 #include <string.h>
14 #include "getversion.h"
15 #include "version.h"
16 #include "scilabmode.h"
17 #include "scilabDefaults.h"
18 #include "getenvc.h"
19 #ifdef _MSC_VER
20 #include "strdup_windows.h"
21 #endif
22 #include "with_module.h"
23 #include "loadversion.h"
24 #include "freeArrayOfString.h"
25 #include "MALLOC.h"
26 #include "../../../io/includes/getenvc.h"
27 /*--------------------------------------------------------------------------*/
28 #define TCLSCI_MODULE_NAME "tclsci"
29 #define TCLTK_OPTION_STRING "tk"
30 #define MODELICAC_OPTION_STRING "modelicac"
31 #define X86_STRING "x86"
32 #define X64_STRING "x64"
33 #define ICC_STRING "ICC"
34 #define VC_STRING "VC++"
35 #define GCC_STRING "GCC"
36 #define PGI_STRING "PGI"
37 #define SUN_STRING "SUN"
38 #define UNKNOW_STRING "UKN"
39 #define RELEASE_STRING "release"
40 #define DEBUG_STRING "debug"
41 #define DEFAULT_VERSION_ARRAY_SIZE 4
42 #define SCILAB_STRING "scilab"
43 /*--------------------------------------------------------------------------*/
44 int *getScilabVersion(int *sizeArrayReturned)
45 {
46     int *returnedArray = (int *)MALLOC(sizeof(int) * DEFAULT_VERSION_ARRAY_SIZE);
47     *sizeArrayReturned = 0;
48     if (returnedArray)
49     {
50         returnedArray[0] = (int)SCI_VERSION_MAJOR;
51         returnedArray[1] = (int)SCI_VERSION_MINOR;
52         returnedArray[2] = (int)SCI_VERSION_MAINTENANCE;
53         returnedArray[3] = (int)SCI_VERSION_TIMESTAMP;
54
55         *sizeArrayReturned = DEFAULT_VERSION_ARRAY_SIZE;
56     }
57     return returnedArray;
58 }
59 /*--------------------------------------------------------------------------*/
60 int getScilabVersionMajor()
61 {
62     return (int)SCI_VERSION_MAJOR;
63 }
64 /*--------------------------------------------------------------------------*/
65 int getScilabVersionMinor()
66 {
67     return (int)SCI_VERSION_MINOR;
68 }
69 /*--------------------------------------------------------------------------*/
70 int getScilabVersionMaintenance()
71 {
72     return (int)SCI_VERSION_MAINTENANCE;
73 }
74 /*--------------------------------------------------------------------------*/
75 int getScilabVersionTimestamp()
76 {
77     return (int)SCI_VERSION_TIMESTAMP;
78 }
79 /*--------------------------------------------------------------------------*/
80 char *getScilabVersionAsString(void)
81 {
82     return strdup(SCI_VERSION_STRING);
83 }
84 /*--------------------------------------------------------------------------*/
85 int* getModuleVersion(char *modulename, int *sizeArrayReturned)
86 {
87     int *returnedArray = NULL;
88
89     if (modulename)
90     {
91         if (strcmp(modulename, SCILAB_STRING) == 0)
92         {
93             returnedArray = getScilabVersion(sizeArrayReturned);
94             return returnedArray;
95         }
96     }
97
98     if (with_module(modulename))
99     {
100 #define LineMax 1024
101         char versionstring[LineMax];
102         int version_module_major = 0;
103         int version_module_minor = 0;
104         int version_module_maintenance = 0;
105         int version_module_revision = 0;
106
107         if (getversionmodule(modulename, &version_module_major, &version_module_minor,
108                              &version_module_maintenance, versionstring,
109                              &version_module_revision))
110         {
111             returnedArray = (int *)MALLOC(sizeof(int) * DEFAULT_VERSION_ARRAY_SIZE);
112             if (returnedArray)
113             {
114                 returnedArray[0] = version_module_major;
115                 returnedArray[1] = version_module_minor;
116                 returnedArray[2] = version_module_maintenance;
117                 returnedArray[3] = version_module_revision;
118                 *sizeArrayReturned = DEFAULT_VERSION_ARRAY_SIZE;
119             }
120             else
121             {
122                 *sizeArrayReturned = 0;
123             }
124         }
125     }
126     else
127     {
128         *sizeArrayReturned = 0;
129     }
130     return returnedArray;
131 }
132 /*--------------------------------------------------------------------------*/
133 char *getModuleVersionInfoAsString(char *modulename)
134 {
135     char *infoString = NULL;
136
137     if (modulename)
138     {
139         if (strcmp(modulename, SCILAB_STRING) == 0)
140         {
141             infoString = getScilabVersionAsString();
142             return infoString;
143         }
144     }
145
146     if (with_module(modulename))
147     {
148 #define LineMax 1024
149         char versionstring[LineMax];
150         int version_module_major = 0;
151         int version_module_minor = 0;
152         int version_module_maintenance = 0;
153         int version_module_revision = 0;
154         if (getversionmodule(modulename, &version_module_major, &version_module_minor,
155                              &version_module_maintenance, versionstring,
156                              &version_module_revision))
157         {
158             infoString = strdup(versionstring);
159         }
160     }
161     return infoString;
162 }
163 /*--------------------------------------------------------------------------*/
164 char **getScilabVersionOptions(int *sizeArrayReturned)
165 {
166     char **options = NULL;
167     int nbOptions = 2;
168     *sizeArrayReturned = 0;
169
170     options = (char**)MALLOC(sizeof(char*) * nbOptions);
171     if (options)
172     {
173         options[0] = getCompilerUsedToBuildScilab();
174         options[1] = getCompilerArchitecture();
175
176         if (with_tk())
177         {
178             options = REALLOC(options, sizeof(char*) * (nbOptions + 1));
179             if (options)
180             {
181                 options[nbOptions] = strdup(TCLTK_OPTION_STRING);
182                 nbOptions++;
183             }
184             else
185             {
186                 freeArrayOfString(options, nbOptions);
187                 return NULL;
188             }
189         }
190
191         if (with_modelica_compiler())
192         {
193             options = REALLOC(options, sizeof(char*) * (nbOptions + 1));
194             if (options)
195             {
196                 options[nbOptions] = strdup(MODELICAC_OPTION_STRING);
197                 nbOptions++;
198             }
199             else
200             {
201                 freeArrayOfString(options, nbOptions);
202                 return NULL;
203             }
204         }
205
206         options = REALLOC(options, sizeof(char*) * (nbOptions + 1));
207         if (options)
208         {
209             options[nbOptions] = getReleaseMode();
210             nbOptions++;
211         }
212         else
213         {
214             freeArrayOfString(options, nbOptions);
215             return NULL;
216         }
217
218         options = REALLOC(options, sizeof(char*) * (nbOptions + 1));
219         if (options)
220         {
221             options[nbOptions] = getReleaseDate();
222             nbOptions++;
223         }
224         else
225         {
226             freeArrayOfString(options, nbOptions);
227             return NULL;
228         }
229
230         options = REALLOC(options, sizeof(char*) * (nbOptions + 1));
231         if (options)
232         {
233             options[nbOptions] = getReleaseTime();
234             nbOptions++;
235         }
236         else
237         {
238             freeArrayOfString(options, nbOptions);
239             return NULL;
240         }
241
242         *sizeArrayReturned = nbOptions;
243     }
244     return options;
245 }
246 /*--------------------------------------------------------------------------*/
247 char *getReleaseMode(void)
248 {
249 #ifdef NDEBUG
250     return strdup(RELEASE_STRING);
251 #else
252     return strdup(DEBUG_STRING);
253 #endif
254 }
255 /*--------------------------------------------------------------------------*/
256 char *getReleaseDate(void)
257 {
258     return strdup(__DATE__);
259 }
260 /*--------------------------------------------------------------------------*/
261 char *getReleaseTime(void)
262 {
263     return strdup(__TIME__);
264 }
265 /*--------------------------------------------------------------------------*/
266 char *getCompilerUsedToBuildScilab(void)
267 {
268 #ifdef __INTEL_COMPILER
269     return strdup(ICC_STRING);
270 #else
271 #ifdef _MSC_VER
272     return strdup(VC_STRING);
273 #else
274 #ifdef __GNUC__
275     return strdup(GCC_STRING);
276 #else
277 #ifdef __PGI
278     return strdup(PGI_STRING);
279 #else
280 #if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
281     return strdup(SUN_STRING);
282 #else
283     return strdup(UNKNOW_STRING);
284 #endif
285 #endif
286 #endif
287 #endif
288 #endif
289 }
290 /*--------------------------------------------------------------------------*/
291 char *getCompilerArchitecture(void)
292 {
293 #ifdef _MSC_VER
294 #ifdef _WIN64
295     return strdup(X64_STRING);
296 #else
297     return strdup(X86_STRING);
298 #endif
299 #else
300 #ifdef _LP64
301     return strdup(X64_STRING);
302 #else
303     return strdup(X86_STRING);
304 #endif
305 #endif
306 }
307 /*--------------------------------------------------------------------------*/
308 BOOL with_modelica_compiler(void)
309 {
310     char *fullpathModelicac = searchEnv(ModelicacName, "PATH");
311     if (fullpathModelicac)
312     {
313         FREE(fullpathModelicac);
314         fullpathModelicac = NULL;
315         return TRUE;
316     }
317     return FALSE;
318 }
319 /*--------------------------------------------------------------------------*/
320 BOOL with_tk(void)
321 {
322     return with_module(TCLSCI_MODULE_NAME) && (getScilabMode() != SCILAB_NWNI);
323 }
324 /*--------------------------------------------------------------------------*/