2c92b2561358324c464bd5f5eac92b3f7937960d
7 #define LIB_PATH "cells"
10 // pour ajouter une autre lib, ajouter ':'<lib>
11 #define DEFAULT_LIBS "gen_builtin_functions.so:gen_API.so:beg_API.so:ctk_API.so:"\
12 "database_API.so:fcl_API.so:mbk_API.so:"\
13 "sim_API.so:spi_API.so:stm_API.so:ttv_API.so"
15 chain_list
*API_DEFINES
=NULL
;
16 chain_list
*API_ACTION_INIT
=NULL
;
17 chain_list
*API_ACTION_END
=NULL
;
18 chain_list
*API_ACTION_RESTART
=NULL
;
19 chain_list
*API_ACTION_TOPLEVEL
=NULL
;
20 int token_already_taken
=0;
22 /* ------------------------------------------------------------------------- */
26 return (t_arg
*)mbkalloc (sizeof (struct t_arg
));
29 /* ------------------------------------------------------------------------- */
31 int TypeCheck (t_arg
*arg
, char *type
)
33 return strcmp (arg
->TYPE
, type
);
36 /* ------------------------------------------------------------------------- */
38 int GetPointer (t_arg
*arg
)
43 /* ------------------------------------------------------------------------- */
45 void SetPointer (t_arg
*arg
, int p
)
50 /* ------------------------------------------------------------------------- */
52 void SetType (t_arg
*arg
, char *t
)
54 arg
->TYPE
= mbkstrdup (t
);
58 void *grab_special_function(char *prefix
, char *name
)
62 sprintf(temp
,"wrap_%s",prefix
);
63 if ((c
=strrchr(temp
,'.'))!=NULL
)
66 func
=GetDynamicFunction(temp
);
70 void *nowrap_grab_special_function(char *prefix
, char *name
)
75 if ((c
=strrchr(temp
,'.'))!=NULL
)
78 func
=GetDynamicFunction(temp
);
82 //typedef int (*libfunc_type)(t_arg **ret, t_arg **prm, int n_params, char *errstr);
83 //typedef void (*voidfunc)();
84 typedef t_arg
** (*targfunc
)(int *);
86 void grab_special_functions(char *prefix
)
93 func
=grab_special_function(prefix
,"_AtLoad_Initialize");
96 if (func(&ret
, NULL
, 0 , buf
)!=0)
98 avt_errmsg(API_ERRMSG
, "001", AVT_ERROR
, prefix
, buf
);
99 //fprintf(stderr,"Error executing %s_AtLoad_Initialize() : %s\n", prefix, buf);
104 func
=grab_special_function(prefix
,"_Action_Initialize");
105 if (func
!=NULL
) API_ACTION_INIT
=addchain(API_ACTION_INIT
, func
);
106 func
=grab_special_function(prefix
,"_Action_Terminate");
107 if (func
!=NULL
) API_ACTION_END
=addchain(API_ACTION_END
, func
);
108 func
=grab_special_function(prefix
,"_Restart");
109 if (func
!=NULL
) API_ACTION_RESTART
=addchain(API_ACTION_RESTART
, func
);
110 func
=grab_special_function(prefix
,"_TopLevel");
111 if (func
!=NULL
) API_ACTION_TOPLEVEL
=addchain(API_ACTION_TOPLEVEL
, func
);
113 func0
=(targfunc
)nowrap_grab_special_function(prefix
, "_getdefines");
123 API_DEFINES
=addchain(API_DEFINES
, tmp
[i
]);
129 static int libs_are_already_loaded
=0;
131 int trytoopenlib(char *path
, char *name
, char *mes
, char *where
)
133 char *tmp
, newlibpath
[4096], pwd
[2048], *c
;
134 // LD_LIBRARY_PATH first
135 if(dlopen(name
,RTLD_LAZY
|RTLD_GLOBAL
)!=NULL
) return 1;
138 strcpy(newlibpath
,path
);
140 tmp
=strchr(newlibpath
,':');
145 sprintf(pwd
,"%s/%s",path
,name
);
146 if(dlopen(pwd
,RTLD_LAZY
|RTLD_GLOBAL
)!=NULL
) return 1;
147 if (strlen(mes
)==0 || strstr(mes
," such ")!=NULL
)
149 if ((c
=dlerror())!=NULL
)
152 strcpy(mes
,"unknown error");
156 tmp
=strchr(path
,':');
159 sprintf(pwd
,"%s/%s",path
,name
);
160 if(dlopen(pwd
,RTLD_LAZY
|RTLD_GLOBAL
)!=NULL
) return 1;
161 if (strlen(mes
)==0 || strstr(mes
," such ")!=NULL
)
163 if ((c
=dlerror())!=NULL
)
166 strcpy(mes
,"unknown error");
172 static char *getdistpath()
176 e
=getenv("AVT_TOOLS_DIR");
179 e
=getenv("AVT_DISTRIB_DIR");
180 if (e
==NULL
) return NULL
;
181 sprintf(temp
,"%s/api_lib",e
);
182 return mbkstrdup(temp
);
184 if (getenv("AVTOS")==NULL
)
185 sprintf(temp
,"%s/tools/Solaris/api_lib",e
);
187 sprintf(temp
,"%s/tools/%s/api_lib",e
,getenv("AVTOS"));
188 return mbkstrdup(temp
);
191 void LoadDynamicLibraries(FILE *debug
)
193 char path
[4096], *tpath
, *libpath
, libpath0
[4096], *tmp
, newlibpath
[4096];
194 char message
[4096], location
[2048];
195 char *DEFAULT_LIB_PATH
;
197 if (libs_are_already_loaded
) return;
198 libs_are_already_loaded
=1;
200 DEFAULT_LIB_PATH
=getdistpath();
201 tpath
=V_STR_TAB
[__API_LIB_PATH
].VALUE
;
202 if (DEFAULT_LIB_PATH
!=NULL
)
205 sprintf(path
,"%s:%s",tpath
, DEFAULT_LIB_PATH
);
207 strcpy(path
,DEFAULT_LIB_PATH
);
208 mbkfree(DEFAULT_LIB_PATH
);
218 tpath
=V_STR_TAB
[__API_LIBS
].VALUE
;
221 sprintf(libpath0
,"%s:%s",DEFAULT_LIBS
,tpath
);
225 libpath
=DEFAULT_LIBS
;
229 strcpy(newlibpath
,libpath
);
231 tmp
=strchr(newlibpath
,':');
236 if (debug
) fprintf(debug
,"Opening dynamic library '%s' ... ",libpath
);
238 if(!trytoopenlib(path
,libpath
, message
, location
))
242 fprintf(debug
,"failed\n\t");
243 fprintf(debug
,"%s\n",message
);
246 avt_errmsg(API_ERRMSG
, "002", AVT_WARNING
, libpath
, message
);
247 // avt_fprintf(stderr,"[¤4warning¤.] could not open dynamic library '¤+%s¤.'\n\treason: %s\n", libpath, message);
248 grab_special_functions(libpath
); // API en static peut etre
253 grab_special_functions(libpath
);
255 if (debug
) fprintf(debug
,"done (%s)\n",location
);
260 tmp
=strchr(libpath
,':');
263 if (debug
) fprintf(debug
,"Opening dynamic library '%s' ... ",libpath
);
265 if(!trytoopenlib(path
,libpath
,message
,location
))
269 fprintf(debug
,"failed\n\t");
270 fprintf(debug
,"%s\n",message
);
273 avt_errmsg(API_ERRMSG
, "002", AVT_WARNING
, libpath
, message
);
274 // avt_fprintf(stderr,"[¤4warning¤.] could not open dynamic library '¤+%s¤.'\n\treason: %s\n", libpath, message);
275 grab_special_functions(libpath
); // API en static peut etre
280 grab_special_functions(libpath
);
282 if (debug
) fprintf(debug
,"done (%s)\n",location
);
288 void *GetDynamicFunction(char *function_name
)
292 libfunc
=dlsym(RTLD_DEFAULT
,function_name
);
294 libfunc
=dlsym(NULL
, function_name
);
299 char *getdistpath_2(char *temp
)
302 e
=V_STR_TAB
[__AVT_TEMPLATE_DIR
].VALUE
;
305 e
=getenv("AVT_TOOLS_DIR");
311 sprintf(temp
,"%s/gns_templates",e
);
318 static chain_list
*splitenv()
324 e
=V_STR_TAB
[__GENIUS_LIB_PATH
].VALUE
;
327 strcpy(temp
, WORK_LIB
);
328 strcat(temp
, "/"LIB_PATH
);
329 cl
=addchain(cl
, mbkstrdup(temp
));
339 cl
=addchain(cl
, mbkstrdup(start
));
343 cl
=addchain(cl
, mbkstrdup(start
));
346 cl
=addchain(cl
, mbkstrdup(getdistpath_2(temp
)));
351 static void freespitenv(chain_list
*cl
)
354 for (ch
=cl
; ch
!=NULL
; ch
=ch
->NEXT
)
360 static FILE *findfile(char *name
, chain_list
*paths
, char *temp
)
368 return mbkfopen(name
,NULL
, READ_TEXT
);
371 for (cl
=paths
; cl
!=NULL
; cl
=cl
->NEXT
)
373 sprintf(temp
,"%s/%s", (char *)cl
->DATA
, name
);
374 if ((f
=mbkfopen(temp
,NULL
, READ_TEXT
))!=NULL
) break;
379 FILE *APIFindFile(lib_entry
*p
)
387 f
= findfile(p
->name
, PATHS
, temp
);
392 p
->name
=mbkstrdup(temp
);
399 int ReadAllCorVHDL(ExecutionContext
*genius_ec
, lib_entry
*files_list
, FILE *debug
)
410 for (p
=files_list
;p
;p
=p
->NEXT
)
414 if (strcasecmp(p
->name
,"none")!=0)
416 f
= findfile(p
->name
, PATHS
, temp
);
421 fprintf(debug
, "Opening model file %s....\n",p
->name
);
423 p
->name
=mbkstrdup(temp
);
424 r
=strrchr(p
->name
, '.');
425 if (r
==NULL
|| strcasecmp(r
, ".tcl")!=0)
426 res
=APIParseFile(f
, p
->name
, genius_ec
, p
->paramset
);
431 nb
=fread(r
,sizeof(char),1000000, f
);
433 if (Tcl_EvalEx((Tcl_Interp
*)TCL_INTERPRETER
, r
, -1, TCL_EVAL_GLOBAL
)==TCL_ERROR
)
434 avt_errmsg(GNS_ERRMSG
, "165", AVT_ERROR
, p
->name
, ((Tcl_Interp
*)TCL_INTERPRETER
)->errorLine
, Tcl_GetStringResult((Tcl_Interp
*)TCL_INTERPRETER
));
441 avt_errmsg(GNS_ERRMSG
, "166", AVT_ERROR
, p
->name
);
448 f
= findfile(p
->name
, PATHS
, temp
);
452 p
->name
=mbkstrdup(temp
);
459 APIVerify_C_Functions (genius_ec
);
460 if (APICheckCFunctions (genius_ec
)) return 1;
464 void APICallApiInitFunctions()
471 for (p
=API_ACTION_INIT
; p
!=NULL
; p
=p
->NEXT
)
473 func
=(libfunc_type
)p
->DATA
;
474 func(&ret
, NULL
, 0 , buf
);
475 mbkfree(ret
->TYPE
); mbkfree(ret
);
479 void APICallApiTerminateFunctions()
486 for (p
=API_ACTION_END
; p
!=NULL
; p
=p
->NEXT
)
488 func
=(libfunc_type
)p
->DATA
;
489 func(&ret
, NULL
, 0 , buf
);
490 mbkfree(ret
->TYPE
); mbkfree(ret
);
494 void APIPreprocess(char *src
, char *dest
, chain_list
*defines
)
496 char *cs
, *cd
, *start
;
498 api_define_type
*adt
;
503 while (*cs
!='$' && *cs
!='\0') *(cd
++)=*(cs
++);
508 while (*cs
>' ' && *cs
!='$' && *cs
!='\0') cs
++;
514 for (cl
=defines
; cl
!=NULL
; cl
=cl
->NEXT
)
516 adt
=(api_define_type
*)cl
->DATA
;
517 if (strcasecmp(adt
->ORIG
, start
)==0) break;
521 while (start
!=cs
) *(cd
++)=*(start
++);
524 for (start
=adt
->DEST
; *start
!='\0'; start
++)
533 while (start
!=cs
) *(cd
++)=*(start
++);
537 while (*cs
!='$' && *cs
!='\0') *(cd
++)=*(cs
++);
542 static ht
*tclfunc
=NULL
;
544 int api_has_tcl_func(char *name
)
548 if (tclfunc
==NULL
) tclfunc
=addht(10);
549 name
=sensitive_namealloc(name
);
550 if ((l
=gethtitem(tclfunc
, name
))!=EMPTYHT
)
553 if (Tcl_GetCommandInfo((Tcl_Interp
*)TCL_INTERPRETER
, name
, &tci
)!=0) l
=1;
555 addhtitem(tclfunc
, name
, l
);