Bug Summary

File:OMCompiler/Compiler/runtime/System_omc.c
Warning:line 1017, column 19
Value stored to 'tok' during its initialization is never read

Annotated Source Code

[?] Use j/k keys for keyboard navigation

1/*
2 * This file is part of OpenModelica.
3 *
4 * Copyright (c) 1998-2010, Linköpings University,
5 * Department of Computer and Information Science,
6 * SE-58183 Linköping, Sweden.
7 *
8 * All rights reserved.
9 *
10 * THIS PROGRAM IS PROVIDED UNDER THE TERMS OF THIS OSMC PUBLIC
11 * LICENSE (OSMC-PL). ANY USE, REPRODUCTION OR DISTRIBUTION OF
12 * THIS PROGRAM CONSTITUTES RECIPIENT'S ACCEPTANCE OF THE OSMC
13 * PUBLIC LICENSE.
14 *
15 * The OpenModelica software and the Open Source Modelica
16 * Consortium (OSMC) Public License (OSMC-PL) are obtained
17 * from Linköpings University, either from the above address,
18 * from the URL: http://www.ida.liu.se/projects/OpenModelica
19 * and in the OpenModelica distribution.
20 *
21 * This program is distributed WITHOUT ANY WARRANTY; without
22 * even the implied warranty of MERCHANTABILITY or FITNESS
23 * FOR A PARTICULAR PURPOSE, EXCEPT AS EXPRESSLY SET FORTH
24 * IN THE BY RECIPIENT SELECTED SUBSIDIARY LICENSE CONDITIONS
25 * OF OSMC-PL.
26 *
27 * See the full OSMC Public License conditions for more details.
28 *
29 */
30
31#if defined(_MSC_VER) || defined(__MINGW32__)
32 #define WIN32_LEAN_AND_MEAN
33 #include <windows.h>
34#endif
35
36#ifdef __cplusplus
37extern "C"
38{
39#endif
40
41#if defined(__linux__1) && !defined(_GNU_SOURCE1)
42#define _GNU_SOURCE1 1
43#endif
44
45#include <ctype.h> /* for toupper */
46#include <limits.h>
47#include <stdlib.h>
48#include "omc_msvc.h"
49#include "omc_file.h"
50#include "openmodelica.h"
51#include "meta_modelica.h"
52#include "ModelicaUtilities.h"
53#define ADD_METARECORD_DEFINITIONSstatic static
54#include "OpenModelicaBootstrappingHeader.h"
55#include "systemimpl.c"
56
57extern void System_writeFile(const char* filename, const char* data)
58{
59 if (SystemImpl__writeFile(filename, data))
60 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
61}
62
63extern void System_appendFile(const char *filename, const char *data)
64{
65 if (SystemImpl__appendFile(filename, data))
66 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
67}
68
69extern char* System_readFile(const char* filename)
70{
71 return SystemImpl__readFile(filename);
72}
73
74extern const char* System_stringReplace(const char* str, const char* source, const char* target)
75{
76 char* res = _replace(str,source,target);
77 if (res == NULL((void*)0))
78 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
79 return res;
80}
81
82extern const char* System_makeC89Identifier(const char* str)
83{
84 int i=0, len=strlen(str);
85 char *res = omc_alloc_interface.malloc_strdup(str);
86 if (!((res[0]>='a' && res[0]<='z') || (res[0]>='A' && res[0]<='Z'))) {
87 res[0] = '_';
88 }
89 for (i=1; i<len; i++) {
90 if (!((res[i]>='a' && res[i]<='z') || (res[i]>='A' && res[i]<='Z') || (res[i]>='0' && res[i]<='9'))) {
91 res[i] = '_';
92 }
93 }
94 return res;
95}
96
97extern int System_stringFind(const char* str, const char* searchStr)
98{
99 const char *found = strstr(str, searchStr);
100 if (found == NULL((void*)0))
101 return -1;
102 else
103 return found-str;
104}
105
106extern const char* System_stringFindString(const char* str, const char* searchStr)
107{
108 const char *found = strstr(str, searchStr);
109 if (found == NULL((void*)0))
110 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
111 return strcpy(ModelicaAllocateString(strlen(found)), found);
112}
113
114extern void System_realtimeTick(int ix)
115{
116 if (ix < 0 || ix >= NUM_USER_RT_CLOCKS32) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
117 rt_tick(ix);
118}
119
120extern double System_realtimeTock(int ix)
121{
122 if (ix < 0 || ix >= NUM_USER_RT_CLOCKS32) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
123 return rt_tock(ix);
124}
125
126extern void System_realtimeClear(int ix)
127{
128 if (ix < 0 || ix >= NUM_USER_RT_CLOCKS32) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
129 rt_clear(ix);
130}
131
132extern int System_realtimeNtick(int ix)
133{
134 if (ix < 0 || ix >= NUM_USER_RT_CLOCKS32) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
135 return rt_ncall(ix);
136}
137
138extern const char* System_getCCompiler()
139{
140 return cc;
141}
142
143extern const char* System_getCXXCompiler()
144{
145 return cxx;
146}
147
148extern const char* System_getOMPCCompiler()
149{
150 return omp_cc;
151}
152
153extern const char* System_getLinker()
154{
155 return linker;
156}
157
158extern const char* System_getLDFlags()
159{
160 return ldflags;
161}
162
163extern const char* System_getCFlags()
164{
165 return cflags;
166}
167
168extern const char* System_trim(const char* str, const char* chars_to_remove)
169{
170 return SystemImpl__trim(str,chars_to_remove);
171}
172
173extern const char* System_trimWhitespace(const char* str)
174{
175 return SystemImpl__trim(str," \f\n\r\t\v");
176}
177
178extern const char* System_trimChar(const char* str, const char* char_to_remove)
179{
180 if (!char_to_remove[0] || char_to_remove[1])
181 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
182 return MMC_STRINGDATA(SystemImpl__trimChar(str,*char_to_remove))(((struct mmc_string*)((void*)((char*)(SystemImpl__trimChar(str
,*char_to_remove)) - 3)))->data)
;
183}
184
185extern const char* System_basename(const char* str)
186{
187 const char *res = SystemImpl__basename(str);
188 return strcpy(ModelicaAllocateString(strlen(res)), res);
189}
190
191extern const char* System_dirname(const char* str)
192{
193 char *cpy = omc_alloc_interface.malloc_strdup(str);
194 char *res = NULL((void*)0);
195#if defined(_MSC_VER)
196 char drive[_MAX_DRIVE], dir[_MAX_DIR], filename[_MAX_FNAME], extension[_MAX_EXT];
197 _splitpath(str, drive, dir, filename, extension);
198 sprintf(cpy, "%s/%s/",drive,dir);
199 res = cpy;
200#else
201 res = dirname(cpy);
202#endif
203 return res;
204}
205
206#if defined(OPENMODELICA_BOOTSTRAPPING_STAGE_1)
207extern const char* System_getRTLibs()
208{
209 return "DUMMY RT LIBS";
210}
211
212extern const char* System_getRTLibsSim()
213{
214 return "DUMMY RT LIBS";
215}
216
217extern const char* System_getRTLibsFMU()
218{
219 return "DUMMY RT LIBS";
220}
221
222extern const char* System_getExeExt()
223{
224 return ".exe";
225}
226
227extern const char* System_getDllExt()
228{
229 return CONFIG_DLL_EXT".so";
230}
231
232extern const char* System_getMakeCommand()
233{
234 return "make";
235}
236
237extern const char* System_os()
238{
239 return "Windows_NT";
240}
241
242extern const char* System_configureCommandLine()
243{
244 return "Dummy configure";
245}
246
247extern const char* System_platform()
248{
249 return "Dummy platform";
250}
251
252extern const char* System_pathDelimiter()
253{
254 return "/";
255}
256
257extern const char* System_groupDelimiter()
258{
259 return ";";
260}
261
262extern const char* System_getCorbaLibs()
263{
264 return "Dummy CORBA libs";
265}
266
267extern void* System_getRuntimeLibs()
268{
269 return "Dummy system libs";
270}
271
272const char* System_getTriple()
273{
274 return "DEFAULT_TRIPLE";
275}
276#endif
277
278extern int System_strncmp(const char *str1, const char *str2, int len)
279{
280 int res= strncmp(str1,str2,len);
281 /* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
282 if (res>0) res = 1;
283 else if (res<0) res = -1;
284 return res;
285}
286
287extern int System_strcmp(const char *str1, const char *str2)
288{
289 int res = strcmp(str1,str2);
290 /* adrpo: 2010-10-07, return -1, 0, +1 so we can pattern match on it directly! */
291 if (res>0) res = 1;
292 else if (res<0) res = -1;
293 return res;
294}
295
296extern int System_strcmp_offset(const char *str1, int offset1, int length1, const char *str2, int offset2, int length2)
297{
298 int n = length1 > length2 ? length1 : length2;
299 int res = strncmp(str1+offset1-1, str2+offset2-1, n);
300 if (res>0) res = 1;
301 else if (res<0) res = -1;
302 return res;
303}
304
305extern int System_getHasExpandableConnectors()
306{
307 return hasExpandableConnectors;
308}
309
310extern void System_setHasExpandableConnectors(int b)
311{
312 hasExpandableConnectors = b;
313}
314
315extern int System_getHasOverconstrainedConnectors()
316{
317 return hasOverconstrainedConnectors;
318}
319
320extern void System_setHasOverconstrainedConnectors(int b)
321{
322 hasOverconstrainedConnectors = b;
323}
324
325extern int System_getPartialInstantiation()
326{
327 return isPartialInstantiation;
328}
329
330extern void System_setPartialInstantiation(int b)
331{
332 isPartialInstantiation = b;
333}
334
335extern int System_getHasInnerOuterDefinitions()
336{
337 return hasInnerOuterDefinitions;
338}
339
340extern void System_setHasInnerOuterDefinitions(int b)
341{
342 hasInnerOuterDefinitions = b;
343}
344
345extern int System_getHasStreamConnectors()
346{
347 return hasStreamConnectors;
348}
349
350extern void System_setHasStreamConnectors(int b)
351{
352 hasStreamConnectors = b;
353}
354
355extern int System_getUsesCardinality()
356{
357 return usesCardinality;
358}
359
360extern void System_setUsesCardinality(int b)
361{
362 usesCardinality = b;
363}
364
365extern void* System_strtok(const char *str0, const char *delimit)
366{
367 char *s;
368 void *res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
369 char *str = omc_alloc_interface.malloc_strdup(str0);
370 char *saveptr;
371 s=strtok_r(str,delimit,&saveptr);
372 if (s == NULL((void*)0)) {
373 return res;
374 }
375 res = mmc_mk_cons(mmc_mk_scon(s),res);
376 while ((s=strtok_r(NULL((void*)0),delimit,&saveptr))) {
377 res = mmc_mk_cons(mmc_mk_scon(s),res);
378 }
379 return listReverse(res);
380}
381
382extern char* System_substring(const char *str, int start, int stop)
383{
384 char* substring = NULL((void*)0);
385 int startIndex = start;
386 int stopIndex = stop;
387 int len1 = strlen(str);
388 int len2 = 0;
389 void *res = NULL((void*)0);
390
391 /* Check arguments */
392 if ( startIndex < 1 )
393 {
394 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
395 }
396 if ( stopIndex == -999 )
397 {
398 stopIndex = startIndex;
399 } else if ( stopIndex < startIndex ) {
400 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
401 } else if ( stopIndex > len1 ) {
402 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
403 }
404
405 /* Allocate memory and copy string */
406 len2 = stopIndex - startIndex + 1;
407 substring = (char*)ModelicaAllocateString(len2);
408 strncpy(substring, &str[startIndex-1], len2);
409 substring[len2] = '\0';
410
411 return substring;
412}
413
414extern char* System_toupper(const char *str)
415{
416 int i;
417 char* strToUpper = strcpy(ModelicaAllocateString(strlen(str)),str);
418 for (i = 0; i < strlen(strToUpper); i++)
419 {
420 strToUpper[i] = toupper(strToUpper[i])(__extension__ ({ int __res; if (sizeof (strToUpper[i]) > 1
) { if (__builtin_constant_p (strToUpper[i])) { int __c = (strToUpper
[i]); __res = __c < -128 || __c > 255 ? __c : (*__ctype_toupper_loc
())[__c]; } else __res = toupper (strToUpper[i]); } else __res
= (*__ctype_toupper_loc ())[(int) (strToUpper[i])]; __res; }
))
;
421 }
422 return strToUpper;
423}
424
425extern char* System_tolower(const char *str)
426{
427 int i;
428 char* strToLower = strcpy(ModelicaAllocateString(strlen(str)),str);
429 for (i = 0; i < strlen(strToLower); i++)
430 {
431 strToLower[i] = tolower(strToLower[i])(__extension__ ({ int __res; if (sizeof (strToLower[i]) > 1
) { if (__builtin_constant_p (strToLower[i])) { int __c = (strToLower
[i]); __res = __c < -128 || __c > 255 ? __c : (*__ctype_tolower_loc
())[__c]; } else __res = tolower (strToLower[i]); } else __res
= (*__ctype_tolower_loc ())[(int) (strToLower[i])]; __res; }
))
;
432 }
433 return strToLower;
434}
435
436const char* System_getClassnamesForSimulation()
437{
438 if(class_names_for_simulation)
439 return strcpy(ModelicaAllocateString(strlen(class_names_for_simulation)),class_names_for_simulation);
440 else
441 return "{}";
442}
443
444void System_setClassnamesForSimulation(const char *class_names)
445{
446 class_names_for_simulation = omc_alloc_interface.malloc_strdup(class_names);
447}
448
449extern double System_getVariableValue(double _timeStamp, void* _timeValues, void* _varValues)
450{
451 double res = 0;
452 if (SystemImpl__getVariableValue(_timeStamp,_timeValues,_varValues,&res))
453 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
454 return res;
455}
456
457extern void* System_getFileModificationTime(const char *fileName)
458{
459 struct stat attrib; // create a file attribute structure
460 double elapsedTime; // the time elapsed as double
461 int result; // the result of the function call
462
463 if (stat( fileName, &attrib ) != 0) {
464 return mmc_mk_none()((void*)((char*)(&(mmc_none).header) + 3));
465 } else {
466 return mmc_mk_some(mmc_mk_rcon(difftime(attrib.st_mtimest_mtim.tv_sec, 0))); // the file modification time
467 }
468}
469
470#if defined(__MINGW32__) || defined(_MSC_VER)
471void* System_moFiles(const char *directory)
472{
473 void *res;
474 WIN32_FIND_DATA FileData;
475 BOOL more = TRUE1;
476 char pattern[1024];
477 HANDLE sh;
478 sprintf(pattern, "%s\\*.mo", directory);
479 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
480 sh = FindFirstFile(pattern, &FileData);
481 if (sh != INVALID_HANDLE_VALUE) {
482 while(more) {
483 if (strcmp(FileData.cFileName,"package.mo") != 0)
484 {
485 res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
486 }
487 more = FindNextFile(sh, &FileData);
488 }
489 if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
490 }
491 return res;
492}
493#else
494void* System_moFiles(const char *directory)
495{
496 int i,count;
497 void *res;
498 struct dirent **files;
499 select_from_dir = directory;
500 count = scandir(directory, &files, file_select_mo, NULL((void*)0));
501 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
502 for (i=0; i<count; i++)
503 {
504 res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
505 free(files[i]);
506 }
507 return res;
508}
509#endif
510
511#if defined(__MINGW32__) || defined(_MSC_VER)
512void* System_mocFiles(const char *directory)
513{
514 void *res;
515 WIN32_FIND_DATA FileData;
516 BOOL more = TRUE1;
517 char pattern[1024];
518 HANDLE sh;
519 sprintf(pattern, "%s\\*.moc", directory);
520 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
521 sh = FindFirstFile(pattern, &FileData);
522 if (sh != INVALID_HANDLE_VALUE) {
523 while(more) {
524 if (strcmp(FileData.cFileName,"package.moc") != 0)
525 {
526 res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
527 }
528 more = FindNextFile(sh, &FileData);
529 }
530 if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
531 }
532 return res;
533}
534#else
535void* System_mocFiles(const char *directory)
536{
537 int i,count;
538 void *res;
539 struct dirent **files;
540 select_from_dir = directory;
541 count = scandir(directory, &files, file_select_moc, NULL((void*)0));
542 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
543 for (i=0; i<count; i++)
544 {
545 res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
546 free(files[i]);
547 }
548 return res;
549}
550#endif
551
552extern int System_lookupFunction(int _inLibHandle, const char* _inFunc)
553{
554 int res = SystemImpl__lookupFunction(_inLibHandle, _inFunc);
555 if (res == -1) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
556 return res;
557}
558
559extern void System_freeFunction(int _inFuncHandle, int printDebug)
560{
561 if (SystemImpl__freeFunction(_inFuncHandle, printDebug)) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
562}
563
564extern void System_freeLibrary(int _inLibHandle, int printDebug)
565{
566 if (SystemImpl__freeLibrary(_inLibHandle, printDebug)) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
567}
568
569extern int System_userIsRoot()
570{
571 return CONFIG_USER_IS_ROOT(geteuid() == 0 ? 1 : 0);
572}
573
574extern int System_getuid()
575{
576#if defined(__MINGW32__) || defined(_MSC_VER)
577 return 0;
578#else
579 return getuid();
580#endif
581}
582
583extern const char* System_readEnv(const char *envname)
584{
585 char *envvalue = getenv(envname);
586 if (envvalue == NULL((void*)0)) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
587 return strcpy(ModelicaAllocateString(strlen(envvalue)),envvalue);
588}
589
590extern void System_getCurrentDateTime(int* sec, int* min, int* hour, int* mday, int* mon, int* year)
591{
592 time_t t;
593 struct tm* localTime;
594 time( &t );
595 localTime = localtime(&t);
596 *sec = localTime->tm_sec;
597 *min = localTime->tm_min;
598 *hour = localTime->tm_hour;
599 *mday = localTime->tm_mday;
600 *mon = localTime->tm_mon + 1;
601 *year = localTime->tm_year + 1900;
602}
603
604extern const char* System_getUUIDStr()
605{
606 const char *res = SystemImpl__getUUIDStr();
607 return strcpy(ModelicaAllocateString(strlen(res)),res);
608}
609
610extern int System_loadLibrary(const char *name, int printDebug)
611{
612 int res = SystemImpl__loadLibrary(name, printDebug);
613 if (res == -1) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
614 return res;
615}
616
617#if defined(__MINGW32__) || defined(_MSC_VER)
618void* System_subDirectories(const char *directory)
619{
620 void *res;
621 WIN32_FIND_DATA FileData;
622 BOOL more = TRUE1;
623 char pattern[1024];
624 HANDLE sh;
625
626 sprintf(pattern, "%s\\*.*", directory);
627
628 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
629 sh = FindFirstFile(pattern, &FileData);
630 if (sh != INVALID_HANDLE_VALUE) {
631 while(more) {
632 if (strcmp(FileData.cFileName,"..") != 0 &&
633 strcmp(FileData.cFileName,".") != 0 &&
634 (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
635 {
636 res = mmc_mk_cons(mmc_mk_scon(FileData.cFileName),res);
637 }
638 more = FindNextFile(sh, &FileData);
639 }
640 if (sh != INVALID_HANDLE_VALUE) FindClose(sh);
641 }
642 return res;
643}
644#else
645void* System_subDirectories(const char *directory)
646{
647 int i,count;
648 void *res;
649 struct dirent **files;
650 select_from_dir = directory;
651 count = scandir(directory, &files, file_select_directories, NULL((void*)0));
652 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
653 for (i=0; i<count; i++)
654 {
655 res = mmc_mk_cons(mmc_mk_scon(files[i]->d_name),res);
656 free(files[i]);
657 }
658 return res;
659}
660#endif
661
662extern void* System_regex(const char* str, const char* re, int maxn, int extended, int sensitive, int *nmatch)
663{
664 void *res;
665 int i = 0;
666#if !defined(_MSC_VER)
667 void *matches[maxn];
668#else
669 void **matches = omc_alloc_interface.malloc(sizeof(void*)*maxn);
670#endif
671 *nmatch = OpenModelica_regexImpl(str,re,maxn,extended,sensitive,mmc_mk_scon,(void**)&matches);
672 res = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
673 for (i=maxn-1; i>=0; i--) {
674 res = mmc_mk_cons(matches[i],res);
675 }
676#if defined(_MSC_VER)
677 GC_free(matches);
678#endif
679 return res;
680}
681
682extern char* System_escapedString(char* str, int nl)
683{
684 char *res = omc__escapedString(str,nl);
685 if (res == NULL((void*)0)) return str;
686 return res;
687}
688
689extern char* System_unescapedString(char* str)
690{
691 char *res = SystemImpl__unescapedString(str);
692 if (res == NULL((void*)0)) return str;
693 return res;
694}
695
696extern char* System_unquoteIdentifier(char *str)
697{
698 return SystemImpl__unquoteIdentifier(str);
699}
700
701extern char* System_getCurrentTimeStr()
702{
703 time_t t;
704 struct tm* localTime;
705 char * dateStr;
706 time( &t );
707 localTime = localtime(&t);
708 dateStr = asctime(localTime);
709 if (dateStr)
710 return dateStr;
711 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
712}
713
714void System_resetTimer()
715{
716 /* reset the timer */
717 timerIntervalTime = 0;
718 timerCummulatedTime = 0;
719 timerTime = 0;
720 timerStackIdx = 0;
721}
722
723void System_startTimer()
724{
725 /* start the timer if not already started */
726 if (!timerStackIdx)
727 {
728 rt_tick(RT_CLOCK_SPECIAL_STOPWATCH32);
729 }
730 pushTimerStack();
731}
732
733void System_stopTimer()
734{
735 popTimerStack();
736}
737
738double System_getTimerElapsedTime()
739{
740 /* get the cummulated timer time */
741 return rt_tock(RT_CLOCK_SPECIAL_STOPWATCH32) - timerStack[0];
742}
743
744double System_getTimerIntervalTime()
745{
746 return timerIntervalTime;
747}
748
749double System_getTimerCummulatedTime()
750{
751 return timerCummulatedTime;
752}
753
754int System_getTimerStackIndex()
755{
756 return timerStackIdx;
757}
758
759extern void System_uriToClassAndPath(const char *uri, const char **scheme, char **name, char **path)
760{
761 int res = SystemImpl__uriToClassAndPath(uri, scheme, name, path);
762 // TODO: Fix memory leak by using the external interface
763 if (res) MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
764}
765
766extern const char* System_modelicaPlatform()
767{
768 return CONFIG_MODELICA_SPEC_PLATFORM"linux64";
769}
770
771extern const char* System_openModelicaPlatform()
772{
773 return CONFIG_OPENMODELICA_SPEC_PLATFORM"x86_64-linux";
774}
775
776extern const char* System_gccDumpMachine()
777{
778 return CONFIG_GCC_DUMPMACHINE"";
779}
780
781extern const char* System_gccVersion()
782{
783 return CONFIG_GCC_VERSION"";
784}
785
786extern void System_getGCStatus(double *used, double *allocated)
787{
788 *allocated = GC_get_heap_size();
789 *used = *allocated - GC_get_free_bytes();
790}
791
792extern const char* System_snprintff(const char *fmt, int len, double d)
793{
794 char *buf;
795 if (len < 0) {
796 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
797 }
798 buf = ModelicaAllocateString(len);
799 if (snprintf(buf,len,fmt,d) >= len) {
800 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
801 }
802 return buf;
803}
804
805extern const char* System_sprintff(const char *fmt, double d)
806{
807 char *buf;
808 const int buf_size = 20;
809 buf = ModelicaAllocateString(buf_size);
810
811 int len = snprintf(buf, buf_size, fmt, d);
812
813 if (len < 0) {
814 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
815 }
816
817 if (len >= buf_size) {
818 buf = ModelicaAllocateString(len + 1);
819 snprintf(buf, len, fmt, d);
820 }
821
822 return buf;
823}
824
825extern const char* System_realpath(const char *path)
826{
827#if defined(__MINGW32__) || defined(_MSC_VER)
828 MULTIBYTE_TO_WIDECHAR_LENGTH(path, unicodePathLength);
829 MULTIBYTE_TO_WIDECHAR_VAR(path, unicodePath, unicodePathLength);
830
831 DWORD bufLen = 0;
832 bufLen = GetFullPathNameW(unicodePath, bufLen, NULL((void*)0), NULL((void*)0));
833 if (!bufLen) {
834 fprintf(stderrstderr, "GetFullPathNameW failed. %lu\n", GetLastError()1L);
835 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
836 }
837
838 WCHAR unicodeFullPath[bufLen];
839 if (!GetFullPathNameW(unicodePath, bufLen, unicodeFullPath, NULL((void*)0))) {
840 MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
841 fprintf(stderrstderr, "GetFullPathNameW failed. %lu\n", GetLastError()1L);
842 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
843 }
844 MULTIBYTE_OR_WIDECHAR_VAR_FREE(unicodePath);
845
846 WIDECHAR_TO_MULTIBYTE_LENGTH(unicodeFullPath, bufferLength);
847 WIDECHAR_TO_MULTIBYTE_VAR(unicodeFullPath, buffer, bufferLength);
848 SystemImpl__toWindowsSeperators(buffer, bufferLength);
849 char *res = omc_alloc_interface.malloc_strdup(buffer);
850 MULTIBYTE_OR_WIDECHAR_VAR_FREE(buffer);
851 return res;
852#else
853 char buf[PATH_MAX4096];
854 if (realpath(path, buf) == NULL((void*)0)) {
855 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
856 }
857 return omc_alloc_interface.malloc_strdup(buf);
858#endif
859}
860
861extern int System_fileIsNewerThan(const char *file1, const char *file2)
862{
863 int res = SystemImpl__fileIsNewerThan(file1, file2);
864 if (res == -1)
865 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
866 return res;
867}
868
869typedef void* voidp;
870
871/* Work in progress: Threading support in OMC */
872typedef struct thread_data {
873 pthread_mutex_t mutex;
874 modelica_metatype (*fn)(threadData_t*,modelica_metatype);
875 int fail;
876 int current;
877 int len;
878 void **commands;
879 void **status;
880 threadData_t *parent;
881} thread_data;
882
883static void* System_launchParallelTasksThread(void *in)
884{
885 int exitstatus = 1;
886 int n;
887 thread_data *data = (thread_data*) in;
888 while (1) {
889 int fail = 1;
890 pthread_mutex_lock(&data->mutex);
891 n = data->current++;
892 pthread_mutex_unlock(&data->mutex);
893 if (data->fail || n >= data->len) break;
894 MMC_TRY_TOP(){ threadData_t threadDataOnStack = {0}, *oldThreadData = (threadData_t
*)pthread_getspecific(mmc_thread_data_key),*threadData = &
threadDataOnStack; pthread_setspecific(mmc_thread_data_key,threadData
); pthread_mutex_init(&threadData->parentMutex,((void*
)0)); mmc_init_stackoverflow_fast(threadData, oldThreadData);
{ jmp_buf new_mmc_jumper, *old_jumper __attribute__((unused)
) = threadData->mmc_jumper; threadData->mmc_jumper = &
new_mmc_jumper; if (_setjmp (new_mmc_jumper) == 0) { threadData
->mmc_stack_overflow_jumper = threadData->mmc_jumper;
895 threadData->parent = data->parent;
896 threadData->mmc_thread_work_exit = threadData->mmc_jumper;
897 data->status[n] = data->fn(threadData,data->commands[n]);
898 fail = 0;
899 MMC_CATCH_TOP()pthread_setspecific(mmc_thread_data_key,oldThreadData); } else
{pthread_setspecific(mmc_thread_data_key,oldThreadData);;}}}
900 if (fail) {
901 data->fail = 1;
902 }
903 }
904 return NULL((void*)0);
905}
906
907static void* System_launchParallelTasksSerial(threadData_t *threadData, void *dataLst, modelica_metatype (*fn)(threadData_t *,modelica_metatype))
908{
909 void *result = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
910 while (!listEmpty(dataLst)((*(mmc_uint_t*)((void*)((char*)(dataLst) - 3))) == (((0) <<
10) + (((0) & 255) << 2)))
) {
911 result = mmc_mk_cons(fn(threadData, MMC_CAR(dataLst)(*(void**)(((void*)((void**)(((void*)((char*)(dataLst) - 3)))
+ (1)))))
),result);
912 dataLst = MMC_CDR(dataLst)(*(void**)(((void*)((void**)(((void*)((char*)(dataLst) - 3)))
+ (2)))))
;
913 }
914 return listReverse(dataLst);
915}
916
917extern void* System_launchParallelTasks(threadData_t *threadData, int numThreads, void *dataLst, modelica_metatype (*fn)(threadData_t *,modelica_metatype))
918{
919 int len = listLength(dataLst), i;
920 void *result = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
921 thread_data data = {0};
922#if !defined(_MSC_VER)
923 void *commands[len];
924 void *status[len];
925 pthread_t th[numThreads];
926 int isInteger = 0;
927
928#if defined(__MINGW32__)
929 /* adrpo: set thread stack size on Windows to 4MB */
930 pthread_attr_t attr;
931 if (pthread_attr_init(&attr))
932 {
933 const char *tok[1] = {strerror(errno(*__errno_location ()))};
934 data.fail = 1;
935 c_add_message(NULL((void*)0),5999,
936 ErrorType_scripting,
937 ErrorLevel_internal,
938 gettext("System.launchParallelTasks: failed to initialize the pthread attributes: %s")dcgettext (((void*)0), "System.launchParallelTasks: failed to initialize the pthread attributes: %s"
, 5)
,
939 NULL((void*)0),
940 0);
941 MMC_THROW_INTERNAL(){longjmp(*threadData->mmc_jumper,1);};
942 }
943 /* try to set a stack size of 4MB */
944 if (pthread_attr_setstacksize(&attr, 4194304))
945 {
946 /* did not work, try half 2MB */
947 if (pthread_attr_setstacksize(&attr, 2097152))
948 {
949 /* did not work, try half 1MB */
950 if (pthread_attr_setstacksize(&attr, 1048576))
951 {
952 const char *tok[1] = {strerror(errno(*__errno_location ()))};
953 data.fail = 1;
954 c_add_message(NULL((void*)0),5999,
955 ErrorType_scripting,
956 ErrorLevel_internal,
957 gettext("System.launchParallelTasks: failed to set the pthread stack size to 1MB: %s")dcgettext (((void*)0), "System.launchParallelTasks: failed to set the pthread stack size to 1MB: %s"
, 5)
,
958 NULL((void*)0),
959 0);
960 MMC_THROW_INTERNAL(){longjmp(*threadData->mmc_jumper,1);};
961 }
962 }
963 }
964#endif
965
966#else /* MSVC */
967 void **commands = (void**) omc_alloc_interface.malloc(sizeof(void*)*len);
968 void **status = (void**) omc_alloc_interface.malloc(sizeof(void*)*len);
969 pthread_t *th = (pthread_t*) omc_alloc_interface.malloc(sizeof(pthread_t)*numThreads);
970#endif
971 if (len == 0) {
972 return mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
973 } else if (numThreads == 1 || len == 1) {
974 return System_launchParallelTasksSerial(threadData,dataLst,fn);
975 }
976
977 /* Make sure we get nothing unexpected here */
978 memset(commands, 0, len*sizeof(void*));
979 memset(status, 0, len*sizeof(void*));
980 memset(th, 0, numThreads*sizeof(pthread_t));
981
982 pthread_mutex_init(&data.mutex,NULL((void*)0));
983 data.fn = fn;
984 data.current = 0;
985 data.len = len;
986 data.commands = commands;
987 data.status = status;
988 data.fail = 0;
989 data.parent = threadData;
990 for (i=0; i<len; i++, dataLst = MMC_CDR(dataLst)(*(void**)(((void*)((void**)(((void*)((char*)(dataLst) - 3)))
+ (2)))))
) {
991 commands[i] = MMC_CAR(dataLst)(*(void**)(((void*)((void**)(((void*)((char*)(dataLst) - 3)))
+ (1)))))
;
992 status[i] = 0; /* just in case */
993 }
994 numThreads = numThreads > len ? len : numThreads;
995 for (i=0; i<numThreads; i++) {
996 if (GC_pthread_create(&th[i],
997#if defined(__MINGW32__)
998 &attr,
999#else
1000 NULL((void*)0),
1001#endif
1002 System_launchParallelTasksThread,&data)) {
1003 /* GC_pthread_create failed. We need to join already created threads though... */
1004 const char *tok[1] = {strerror(errno(*__errno_location ()))};
1005 data.fail = 1;
1006 c_add_message(NULL((void*)0),5999,
1007 ErrorType_scripting,
1008 ErrorLevel_internal,
1009 gettext("System.launchParallelTasks: Failed to create thread: %s")dcgettext (((void*)0), "System.launchParallelTasks: Failed to create thread: %s"
, 5)
,
1010 NULL((void*)0),
1011 0);
1012 break;
1013 }
1014 }
1015 for (i=0; i<numThreads; i++) {
1016 if (th[i] && GC_pthread_join(th[i], NULL((void*)0))) {
1017 const char *tok[1] = {strerror(errno(*__errno_location ()))};
Value stored to 'tok' during its initialization is never read
1018 data.fail = 1;
1019 c_add_message(NULL((void*)0),5999,
1020 ErrorType_scripting,
1021 ErrorLevel_internal,
1022 gettext("System.launchParallelTasks: Failed to join thread: %s")dcgettext (((void*)0), "System.launchParallelTasks: Failed to join thread: %s"
, 5)
,
1023 NULL((void*)0),
1024 0);
1025 }
1026 }
1027 if (data.fail) {
1028 MMC_THROW_INTERNAL(){longjmp(*threadData->mmc_jumper,1);};
1029 }
1030 if (data.current < len) {
1031 c_add_message(NULL((void*)0),5999,
1032 ErrorType_scripting,
1033 ErrorLevel_internal,
1034 gettext("System.launchParallelTasks: We seem to have executed fewer tasks than expected.")dcgettext (((void*)0), "System.launchParallelTasks: We seem to have executed fewer tasks than expected."
, 5)
,
1035 NULL((void*)0),
1036 0);
1037 MMC_THROW_INTERNAL(){longjmp(*threadData->mmc_jumper,1);};
1038 }
1039 isInteger = MMC_IS_INTEGER(status[0])(0 == ((mmc_sint_t) (status[0]) & 1));
1040 for (i=len-1; i>=0; i--) {
1041 if (isInteger != MMC_IS_INTEGER(status[i])(0 == ((mmc_sint_t) (status[i]) & 1))) {
1042 c_add_message(NULL((void*)0),5999,
1043 ErrorType_scripting,
1044 ErrorLevel_internal,
1045 gettext("System.launchParallelTasks: Got mismatched results types. Was there a thread synchronization error?")dcgettext (((void*)0), "System.launchParallelTasks: Got mismatched results types. Was there a thread synchronization error?"
, 5)
,
1046 NULL((void*)0),
1047 0);
1048 MMC_THROW_INTERNAL(){longjmp(*threadData->mmc_jumper,1);};
1049 }
1050 result = mmc_mk_cons(status[i], result);
1051 }
1052 return result;
1053}
1054
1055void System_initGarbageCollector(void)
1056{
1057 SystemImpl__initGarbageCollector();
1058}
1059
1060void System_threadFail(threadData_t *threadData)
1061{
1062 if (0 == threadData->mmc_thread_work_exit) {
1063 fprintf(stderrstderr, "System_threadFail called in something that is not a worker thread!\nThe application will now exit!\n");
1064 abort();
1065 }
1066 longjmp(*threadData->mmc_thread_work_exit,1);
1067}
1068
1069/* TODO: Remove once we make a new tarball */
1070int System_isRML()
1071{
1072 return 0;
1073}
1074
1075extern void* System_strtokIncludingDelimiters(const char *str0, const char *delimit)
1076{
1077 char* str = (char*)str0;
1078 mmc_uint_t len = strlen(str);
1079 char* cp = NULL((void*)0);
1080 char *d = (char*)delimit;
1081 mmc_uint_t dlen = strlen(d);
1082 void *lst = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
1083 void *slst = mmc_mk_nil()((void*)((char*)(&(mmc_nil).header) + 3));
1084 char* s = str;
1085 char* stmp;
1086 mmc_uint_t start = 0, end = 0;
1087 /* len + 3 in pos signifies that there is no delimiter in the string */
1088 mmc_uint_t pos = len + 3;
1089
1090 /* fail if delimiter is bigger than string */
1091 if (dlen > len)
1092 {
1093 MMC_THROW(){longjmp(*((threadData_t*)pthread_getspecific(mmc_thread_data_key
))->mmc_jumper,1);}
;
1094 }
1095
1096 /* add 0 to the list! */
1097 lst = mmc_mk_cons(mmc_mk_icon(0), lst);
1098
1099 /* find the first delimiter */
1100 while ((cp = strstr(s, d)) != NULL((void*)0))
1101 {
1102 s = cp + dlen;
1103 pos = (cp - str);
1104 /* check if the position is already in the list */
1105 /* in the list add only the end */
1106 if (pos == MMC_UNTAGFIXNUM(MMC_CAR(lst))(((mmc_sint_t) ((*(void**)(((void*)((void**)(((void*)((char*)
(lst) - 3))) + (1))))))) >> 1)
)
1107 {
1108 lst = mmc_mk_cons(mmc_mk_icon((mmc_sint_t)(void*)(pos+dlen)), lst);
1109 }
1110 else /* not in the list, add both */
1111 {
1112 lst = mmc_mk_cons(mmc_mk_icon(pos), lst);
1113 lst = mmc_mk_cons(mmc_mk_icon((mmc_sint_t)(void*)(pos+dlen)), lst);
1114 }
1115 }
1116 /* this means it was not found in the entire string */
1117 if (pos == (len + 3))
1118 {
1119 /* return the empty list */
1120 return slst;
1121 }
1122
1123 /* add len to the list! */
1124 if ((len) != MMC_UNTAGFIXNUM(MMC_CAR(lst))(((mmc_sint_t) ((*(void**)(((void*)((void**)(((void*)((char*)
(lst) - 3))) + (1))))))) >> 1)
)
1125 {
1126 lst = mmc_mk_cons(mmc_mk_icon(len), lst);
1127 }
1128
1129 /*
1130 * BIG NOTE! the list of indexes is reversed, it starts closer to len!
1131 */
1132 /* now we walk the list and build the string list */
1133 while( MMC_GETHDR(lst)(*(mmc_uint_t*)((void*)((char*)(lst) - 3))) == MMC_CONSHDR(((2) << 10) + (((1) & 255) << 2)) )
1134 {
1135 end = MMC_UNTAGFIXNUM(MMC_CAR(lst))(((mmc_sint_t) ((*(void**)(((void*)((void**)(((void*)((char*)
(lst) - 3))) + (1))))))) >> 1)
;
1136 lst = MMC_CDR(lst)(*(void**)(((void*)((void**)(((void*)((char*)(lst) - 3))) + (
2)))))
;
1137 /* break if we reached the last in the list */
1138 if (MMC_GETHDR(lst)(*(mmc_uint_t*)((void*)((char*)(lst) - 3))) == MMC_NILHDR(((0) << 10) + (((0) & 255) << 2)))
1139 {
1140 break;
1141 }
1142 start = MMC_UNTAGFIXNUM(MMC_CAR(lst))(((mmc_sint_t) ((*(void**)(((void*)((void**)(((void*)((char*)
(lst) - 3))) + (1))))))) >> 1)
;
1143 /* create stmp */
1144 pos = end - start;
1145 stmp = (char*)malloc((pos+1) * sizeof(char));
1146 strncpy(stmp, str + start, pos);
1147 stmp[pos] = '\0';
1148 slst = mmc_mk_cons(mmc_mk_scon(stmp), slst);
1149 free(stmp);
1150 }
1151 return slst;
1152}
1153
1154#ifdef __cplusplus
1155}
1156#endif