| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "Yap.h"
 | 
					
						
							|  |  |  | #include "Yatom.h"
 | 
					
						
							| 
									
										
										
										
											2009-10-23 14:22:17 +01:00
										 |  |  | #include "YapHeap.h"
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | #include "yapio.h"
 | 
					
						
							|  |  |  | #include "Foreign.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #if LOAD_SHL
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <dl.h>
 | 
					
						
							|  |  |  | #include <malloc.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2002-09-23 17:06:13 +00:00
										 |  |  |  *   YAP_FindExecutable(argv[0]) should be called on yap initialization to | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |  *   locate the executable of Yap | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  | void Yap_FindExecutable(char *name) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-06-17 00:29:01 +01:00
										 |  |  | void * | 
					
						
							|  |  |  | Yap_LoadForeignFile(char *file, int flags) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   /* not implemented */ | 
					
						
							|  |  |  |   return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Yap_CallForeignFile(void *handle, char *f) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return FALSE; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | int | 
					
						
							|  |  |  | Yap_CloseForeignFile(void *handle) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return -1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * LoadForeign(ofiles,libs,proc_name,init_proc) dynamically loads foreign | 
					
						
							|  |  |  |  * code files and libraries and locates an initialization routine | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-11 17:38:10 +00:00
										 |  |  | static Int | 
					
						
							|  |  |  | LoadForeign( StringList ofiles, StringList libs, | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 		 char *proc_name, YapInitProc *init_proc ) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /* *init_proc is initialised to NULL in load_foreign.c */ | 
					
						
							|  |  |  |   int init_missing = -1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   int n, i; | 
					
						
							|  |  |  |   struct shl_symbol *p; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while( ofiles ) { | 
					
						
							|  |  |  |     int valid_fname; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /* shl_load wants to follow the LD_CONFIG_PATH */ | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |     valid_fname = Yap_TrueFileName( AtomName(ofiles->name), LOCAL_FileNameBuf, TRUE ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if( !valid_fname ) { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |       strcpy( LOCAL_ErrorSay, "%% Trying to open non-existing file in LoadForeign" ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return LOAD_FAILLED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |     ofiles->handle = Yap_AllocCodeSpace( sizeof(shl_t) ); | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |     *(shl_t *)ofiles->handle = shl_load( LOCAL_FileNameBuf, BIND_DEFERRED, 0 ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     if( *(shl_t *)ofiles->handle == NULL ) { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |       strncpy( LOCAL_ErrorSay, strerror(errno), MAX_ERROR_MSG_SIZE ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return LOAD_FAILLED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if( init_missing ) { | 
					
						
							|  |  |  |       init_missing = shl_findsym( ofiles->handle, proc_name, | 
					
						
							|  |  |  | 				  TYPE_PROCEDURE, init_proc ); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ofiles = ofiles->next; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if( init_missing ) { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |     strcpy( LOCAL_ErrorSay, "Could not locate initialization routine" ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     return LOAD_FAILLED; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   while( libs ) { | 
					
						
							| 
									
										
										
										
											2010-12-12 18:28:55 +00:00
										 |  |  |     char *s = AtomName(lib->s); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if( s[0] == '-' ) { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |       strcpy( LOCAL_FileNameBuf, "lib" ); | 
					
						
							|  |  |  |       strcat( LOCAL_FileNameBuf, s+2 ); | 
					
						
							|  |  |  |       strcat( LOCAL_FileNameBuf, ".sl" ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     } | 
					
						
							|  |  |  |     else { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |       strcpy( LOCAL_FileNameBuf, s ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |     *(shl_t *)libs->handle = shl_load( LOCAL_FileNameBuf, BIND_DEFERRED, 0 ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |     if( *(shl_t *)libs->handle == NULL ) { | 
					
						
							| 
									
										
										
										
											2011-05-23 16:19:47 +01:00
										 |  |  |       strncpy( LOCAL_ErrorSay, strerror(errno), MAX_ERROR_MSG_SIZE ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       return LOAD_FAILLED; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     libs = libs->next; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return LOAD_SUCCEEDED; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-11 17:38:10 +00:00
										 |  |  | Int | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  | Yap_LoadForeign(StringList ofiles, StringList libs, | 
					
						
							| 
									
										
										
										
											2002-11-11 17:38:10 +00:00
										 |  |  | 	       char *proc_name,	YapInitProc *init_proc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   return LoadForeign(ofiles, libs, proc_name, init_proc); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  | Yap_ShutdownLoadForeign( void ) | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | { | 
					
						
							|  |  |  |   ForeignObj *f_code; | 
					
						
							|  |  |  |   int err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   f_code = ForeignCodeLoaded; | 
					
						
							|  |  |  |   while( f_code != NULL ) { | 
					
						
							|  |  |  |     StringList objs, libs; | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     objs = f_code->objs; | 
					
						
							|  |  |  |     while( objs ) { | 
					
						
							|  |  |  |       err = shl_unload( *(shl_t *)objs->handle ); | 
					
						
							|  |  |  |       if( err ) { | 
					
						
							|  |  |  | 	/* dunno how to properly report an error here */ | 
					
						
							|  |  |  | 	perror( NULL ); | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |       Yap_FreeCodeSpace( objs->handle ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       objs = objs->next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     libs = f_code->libs; | 
					
						
							|  |  |  |     while( libs ) { | 
					
						
							|  |  |  |       err = shl_unload( *(shl_t *)libs->handle ); | 
					
						
							|  |  |  |       if( err ) { | 
					
						
							|  |  |  | 	/* dunno how to properly report an error here */ | 
					
						
							|  |  |  | 	perror( NULL ); | 
					
						
							|  |  |  | 	return; | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  |       Yap_FreeCodeSpace( libs->handle ); | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  |       libs = libs->next; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     f_code = f_code->next; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-11-11 17:38:10 +00:00
										 |  |  | Int | 
					
						
							| 
									
										
										
										
											2002-11-18 18:18:05 +00:00
										 |  |  | Yap_ReLoadForeign(StringList ofiles, StringList libs, | 
					
						
							| 
									
										
										
										
											2001-04-09 19:54:03 +00:00
										 |  |  | 		  char *proc_name, YapInitProc *init_proc) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   ShutdownLoadForeign(); | 
					
						
							|  |  |  |   return( LoadForeign( ofiles, libs, proc_name, init_proc ) ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  | dunno what this one is supposed to do, no load_* defines it | 
					
						
							|  |  |  | void	STD_PROTO(ReOpenLoadForeign,(void)); | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 |